FreeRDP
Loading...
Searching...
No Matches
SdlWindow Class Reference

#include <sdl_window.hpp>

Public Member Functions

 SdlWindow (const std::string &title, Sint32 startupX, Sint32 startupY, Sint32 width, Sint32 height, Uint32 flags)
 SdlWindow (const SdlWindow &other)=delete
 SdlWindow (SdlWindow &&other) noexcept
SdlWindow & operator= (const SdlWindow &other)=delete
SdlWindow & operator= (SdlWindow &&other)=delete
Uint32 id () const
int displayIndex () const
SDL_Rect rect () const
SDL_Window * window () const
Sint32 offsetX () const
void setOffsetX (Sint32 x)
void setOffsetY (Sint32 y)
Sint32 offsetY () const
bool grabKeyboard (bool enable)
bool grabMouse (bool enable)
void setBordered (bool bordered)
void raise ()
void resizeable (bool use)
void fullscreen (bool enter)
void minimize ()
bool fill (Uint8 r=0x00, Uint8 g=0x00, Uint8 b=0x00, Uint8 a=0xff)
bool blit (SDL_Surface *surface, SDL_Rect src, SDL_Rect &dst)
void updateSurface ()
 SdlWindow (SdlWindow &&other) noexcept
 SdlWindow (const SdlWindow &other)=delete
SdlWindow & operator= (const SdlWindow &other)=delete
SdlWindow & operator= (SdlWindow &&other)=delete
SDL_WindowID id () const
SDL_DisplayID displayIndex () const
SDL_Rect rect () const
SDL_Rect bounds () const
SDL_Window * window () const
SDL_Renderer * renderer () const
Sint32 offsetX () const
void setOffsetX (Sint32 x)
void setOffsetY (Sint32 y)
Sint32 offsetY () const
SDL_Point layoutPosition () const
void setLayoutPosition (const SDL_Point &pos)
rdpMonitor monitor (bool isPrimary) const
void setMonitor (rdpMonitor monitor)
float scale () const
SDL_DisplayOrientation orientation () const
bool grabKeyboard (bool enable)
bool grabMouse (bool enable)
void setBordered (bool bordered)
void raise ()
void resizeable (bool use)
void fullscreen (bool enter, bool forceOriginalDisplay)
void minimize ()
bool resizeToScale ()
bool resize (const SDL_Point &size)
bool drawRect (SDL_Surface *surface, SDL_Point offset, const SDL_Rect &srcRect)
bool drawRects (SDL_Surface *surface, SDL_Point offset, const std::vector< SDL_Rect > &rects={})
bool drawScaledRect (SDL_Surface *surface, const SDL_FPoint &scale, const SDL_Rect &srcRect)
bool drawScaledRects (SDL_Surface *surface, const SDL_FPoint &scale, const std::vector< SDL_Rect > &rects={})
bool fill (Uint8 r=0x00, Uint8 g=0x00, Uint8 b=0x00, Uint8 a=0xff)
bool blit (SDL_Surface *surface, const SDL_Rect &src, SDL_Rect &dst)
bool upload (SDL_Surface *surface, const SDL_Rect &src)
bool render (const SDL_Rect &src, const SDL_Rect &dst)
bool paintResizeFrame (SDL_Surface *surface, SDL_Point off, bool contentChanged, const SDL_Rect &inset={ 0, 0, 0, 0 }, bool fillRevealed=true, bool dashedBorder=true)
void updateSurface (const SdlOverlay *overlay=nullptr)
const char * rendererName () const
bool setVideoOverlay (Uint64 id, const void *pixels, int pitch, int w, int h, const SDL_FRect &dst, const std::vector< SDL_FRect > &clip, bool visible)
 redirected video id, drawn over the session at each present at dst (render coordinates), only in clip (render coordinates) when not empty: what the page draws over the video (player controls) stays visible. pixels (BGRA, wxh) is uploaded when not null
void pruneVideoOverlays (const std::function< bool(Uint64)> &keep)
 drops the overlays keep returns false for

Static Public Member Functions

static SdlWindow create (SDL_DisplayID id, const std::string &title, Uint32 flags, Uint32 width=0, Uint32 height=0)
static SdlWindow create (SDL_DisplayID id, const std::string &title, Uint32 flags, const SDL_Rect &rect)
static SdlWindow createPopup (SDL_Window *parent, const SDL_Rect &rect, bool transparent, bool tooltip=false)
static rdpMonitor query (SDL_DisplayID id, bool forceAsPrimary=false)

Protected Types

enum  HighDPIMode { MODE_INVALID , MODE_NONE , MODE_WINDOWS , MODE_MACOS }

Protected Member Functions

 SdlWindow (SDL_DisplayID id, const std::string &title, const SDL_Rect &rect, Uint32 flags)
 SdlWindow (SDL_Window *parent, const SDL_Rect &rect, bool transparent, bool tooltip)

Static Protected Member Functions

static bool fill (SDL_Window *window, Uint8 r=0x00, Uint8 g=0x00, Uint8 b=0x00, Uint8 a=0xff)
static rdpMonitor query (SDL_Window *window, SDL_DisplayID id, bool forceAsPrimary=false)
static SDL_Rect rect (SDL_Window *window, bool forceAsPrimary=false)
static SDL_Rect rect (SDL_DisplayID id, bool forceAsPrimary=false)
static bool tryFallback (bool isFullscreen)
static enum HighDPIMode isHighDPIWindowsMode (SDL_Window *window)

Detailed Description

FreeRDP: A Remote Desktop Protocol Implementation SDL Client

Copyright 2023 Armin Novak armin.nosp@m..nov.nosp@m.ak@th.nosp@m.inca.nosp@m.st.co.nosp@m.m Copyright 2023 Thincast Technologies GmbH

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Definition at line 25 of file SDL2/sdl_window.hpp.

Member Enumeration Documentation

◆ HighDPIMode

enum SdlWindow::HighDPIMode
protected

Definition at line 134 of file SDL3/sdl_window.hpp.

135 {
136 MODE_INVALID,
137 MODE_NONE,
138 MODE_WINDOWS,
139 MODE_MACOS
140 };

Constructor & Destructor Documentation

◆ SdlWindow() [1/4]

SdlWindow::SdlWindow ( const std::string & title,
Sint32 startupX,
Sint32 startupY,
Sint32 width,
Sint32 height,
Uint32 flags )

FreeRDP: A Remote Desktop Protocol Implementation SDL Client

Copyright 2023 Armin Novak armin.nosp@m..nov.nosp@m.ak@th.nosp@m.inca.nosp@m.st.co.nosp@m.m Copyright 2023 Thincast Technologies GmbH

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Definition at line 23 of file SDL2/sdl_window.cpp.

25 : _window(SDL_CreateWindow(title.c_str(), startupX, startupY, width, height, flags))
26{
27}

◆ SdlWindow() [2/4]

SdlWindow::SdlWindow ( SdlWindow && other)
noexcept

Definition at line 29 of file SDL2/sdl_window.cpp.

30 : _window(other._window), _offset_x(other._offset_x), _offset_y(other._offset_y)
31{
32 other._window = nullptr;
33}

◆ ~SdlWindow()

SdlWindow::~SdlWindow ( )

Definition at line 35 of file SDL2/sdl_window.cpp.

36{
37 SDL_DestroyWindow(_window);
38}

◆ SdlWindow() [3/4]

SdlWindow::SdlWindow ( SDL_DisplayID id,
const std::string & title,
const SDL_Rect & rect,
Uint32 flags )
protected

FreeRDP: A Remote Desktop Protocol Implementation SDL Client

Copyright 2023 Armin Novak armin.nosp@m..nov.nosp@m.ak@th.nosp@m.inca.nosp@m.st.co.nosp@m.m Copyright 2023 Thincast Technologies GmbH

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Definition at line 30 of file SDL3/sdl_window.cpp.

32 : _initialW(rect.w), _initialH(rect.h), _displayID(id)
33{
34 float pd = SDL_GetDisplayContentScale(id);
35 if (pd <= 0.0f)
36 pd = 1.0f;
37 const int createW = static_cast<int>(std::ceil(static_cast<float>(rect.w) / pd));
38 const int createH = static_cast<int>(std::ceil(static_cast<float>(rect.h) / pd));
39
40 auto props = SDL_CreateProperties();
41 SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, title.c_str());
42 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, rect.x);
43 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, rect.y);
44 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, createW);
45 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, createH);
46 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_RESIZABLE_BOOLEAN, true);
47
48 if (flags & SDL_WINDOW_HIGH_PIXEL_DENSITY)
49 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIGH_PIXEL_DENSITY_BOOLEAN, true);
50
51 if (flags & SDL_WINDOW_FULLSCREEN)
52 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_FULLSCREEN_BOOLEAN, true);
53
54 if (flags & SDL_WINDOW_BORDERLESS)
55 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_BORDERLESS_BOOLEAN, true);
56
57 if (flags & SDL_WINDOW_TRANSPARENT)
58 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_TRANSPARENT_BOOLEAN, true);
59
60 /* RAIL windows are created hidden until first paint to avoid black flash. */
61 if (flags & SDL_WINDOW_HIDDEN)
62 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIDDEN_BOOLEAN, true);
63
64 _window = SDL_CreateWindowWithProperties(props);
65 SDL_DestroyProperties(props);
66 SDL_SetHint(SDL_HINT_APP_NAME, "");
67 std::ignore = SDL_SyncWindow(_window);
68
69 _renderer = SDL_CreateRenderer(_window, nullptr);
70
71 std::ignore = resizeToScale();
72
73 _monitor = query(_window, id, true);
74}

◆ SdlWindow() [4/4]

SdlWindow::SdlWindow ( SDL_Window * parent,
const SDL_Rect & rect,
bool transparent,
bool tooltip )
protected

Definition at line 912 of file SDL3/sdl_window.cpp.

913 : _initialW(rect.w), _initialH(rect.h)
914{
915 auto props = SDL_CreateProperties();
916 SDL_SetPointerProperty(props, SDL_PROP_WINDOW_CREATE_PARENT_POINTER, parent);
917 SDL_SetBooleanProperty(props,
918 tooltip ? SDL_PROP_WINDOW_CREATE_TOOLTIP_BOOLEAN
919 : SDL_PROP_WINDOW_CREATE_MENU_BOOLEAN,
920 true);
921 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_FOCUSABLE_BOOLEAN, false);
922#if defined(SDL_PROP_WINDOW_CREATE_CONSTRAIN_POPUP_BOOLEAN)
923 /* The server places RemoteApp popups; a moved one would map clicks to other points. */
924 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_CONSTRAIN_POPUP_BOOLEAN, false);
925#endif
926 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_BORDERLESS_BOOLEAN, true);
927 /* Transparent so menus' genuine per-pixel alpha (corners/shadow) isn't rendered black. */
928 if (transparent)
929 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_TRANSPARENT_BOOLEAN, true);
930 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, rect.x);
931 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, rect.y);
932 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, rect.w);
933 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, rect.h);
934 /* Hidden until first paint, like the app constructor. */
935 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIDDEN_BOOLEAN, true);
936
937 auto isHighDpi = true;
938 if (parent)
939 {
940 auto flags = SDL_GetWindowFlags(parent);
941 if ((flags & SDL_WINDOW_HIGH_PIXEL_DENSITY) != 0)
942 isHighDpi = true;
943 }
944
945 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIGH_PIXEL_DENSITY_BOOLEAN, isHighDpi);
946
947 _window = SDL_CreateWindowWithProperties(props);
948 SDL_DestroyProperties(props);
949 if (_window)
950 {
951 std::ignore = SDL_SyncWindow(_window);
952 _renderer = SDL_CreateRenderer(_window, nullptr);
953 _displayID = SDL_GetDisplayForWindow(_window);
954 }
955}

Member Function Documentation

◆ blit() [1/2]

bool SdlWindow::blit ( SDL_Surface * surface,
const SDL_Rect & src,
SDL_Rect & dst )
nodiscard

Definition at line 633 of file SDL3/sdl_window.cpp.

634{
635 return upload(surface, srcRect) && render(srcRect, dstRect);
636}

◆ blit() [2/2]

bool SdlWindow::blit ( SDL_Surface * surface,
SDL_Rect src,
SDL_Rect & dst )

Definition at line 187 of file SDL2/sdl_window.cpp.

188{
189 auto screen = SDL_GetWindowSurface(_window);
190 if (!screen || !surface)
191 return false;
192 if (!SDL_SetClipRect(surface, &srcRect))
193 return true;
194 if (!SDL_SetClipRect(screen, &dstRect))
195 return true;
196 auto rc = SDL_BlitScaled(surface, &srcRect, screen, &dstRect);
197 if (rc != 0)
198 {
199 SDL_LogError(SDL_LOG_CATEGORY_RENDER, "SDL_BlitScaled: %s [%d]", sdl_error_string(rc), rc);
200 }
201 return rc == 0;
202}

◆ bounds()

SDL_Rect SdlWindow::bounds ( ) const
nodiscard

Definition at line 127 of file SDL3/sdl_window.cpp.

128{
129 SDL_Rect rect = {};
130 if (_window)
131 {
132 if (!SDL_GetWindowPosition(_window, &rect.x, &rect.y))
133 return {};
134 if (!SDL_GetWindowSize(_window, &rect.w, &rect.h))
135 return {};
136 }
137 return rect;
138}

◆ create() [1/2]

SdlWindow SdlWindow::create ( SDL_DisplayID id,
const std::string & title,
Uint32 flags,
const SDL_Rect & rect )
staticnodiscard

Definition at line 905 of file SDL3/sdl_window.cpp.

907{
908 return SdlWindow{ id, title, rect, flags };
909}
SdlWindow(const std::string &title, Sint32 startupX, Sint32 startupY, Sint32 width, Sint32 height, Uint32 flags)

◆ create() [2/2]

SdlWindow SdlWindow::create ( SDL_DisplayID id,
const std::string & title,
Uint32 flags,
Uint32 width = 0,
Uint32 height = 0 )
staticnodiscard

Definition at line 880 of file SDL3/sdl_window.cpp.

882{
883 flags |= SDL_WINDOW_HIGH_PIXEL_DENSITY;
884
885 SDL_Rect rect = { static_cast<int>(SDL_WINDOWPOS_CENTERED_DISPLAY(id)),
886 static_cast<int>(SDL_WINDOWPOS_CENTERED_DISPLAY(id)), static_cast<int>(width),
887 static_cast<int>(height) };
888
889 if ((flags & SDL_WINDOW_FULLSCREEN) != 0)
890 {
891 std::ignore = SDL_GetDisplayBounds(id, &rect);
892 }
893
894 SdlWindow window{ id, title, rect, flags };
895
896 if ((flags & SDL_WINDOW_FULLSCREEN) != 0)
897 {
898 window.setOffsetX(rect.x);
899 window.setOffsetY(rect.y);
900 }
901
902 return window;
903}

◆ createPopup()

SdlWindow SdlWindow::createPopup ( SDL_Window * parent,
const SDL_Rect & rect,
bool transparent,
bool tooltip = false )
staticnodiscard

Definition at line 957 of file SDL3/sdl_window.cpp.

959{
960 return SdlWindow{ parent, rect, transparent, tooltip };
961}

◆ displayIndex()

SDL_DisplayID SdlWindow::displayIndex ( ) const
nodiscard

Definition at line 47 of file SDL2/sdl_window.cpp.

48{
49 if (!_window)
50 return 0;
51 return SDL_GetWindowDisplayIndex(_window);
52}

◆ drawRect()

bool SdlWindow::drawRect ( SDL_Surface * surface,
SDL_Point offset,
const SDL_Rect & srcRect )
nodiscard

Definition at line 345 of file SDL3/sdl_window.cpp.

346{
347 WINPR_ASSERT(surface);
348 SDL_Rect dstRect = { offset.x + srcRect.x, offset.y + srcRect.y, srcRect.w, srcRect.h };
349 return blit(surface, srcRect, dstRect);
350}

◆ drawRects()

bool SdlWindow::drawRects ( SDL_Surface * surface,
SDL_Point offset,
const std::vector< SDL_Rect > & rects = {} )
nodiscard

Definition at line 352 of file SDL3/sdl_window.cpp.

354{
355 if (rects.empty())
356 {
357 return drawRect(surface, offset, { 0, 0, surface->w, surface->h });
358 }
359 for (auto& srcRect : rects)
360 {
361 if (!drawRect(surface, offset, srcRect))
362 return false;
363 }
364 return true;
365}

◆ drawScaledRect()

bool SdlWindow::drawScaledRect ( SDL_Surface * surface,
const SDL_FPoint & scale,
const SDL_Rect & srcRect )
nodiscard

Definition at line 383 of file SDL3/sdl_window.cpp.

385{
386 auto dstRect = scaledDstRect(scale, srcRect);
387 return blit(surface, srcRect, dstRect);
388}

◆ drawScaledRects()

bool SdlWindow::drawScaledRects ( SDL_Surface * surface,
const SDL_FPoint & scale,
const std::vector< SDL_Rect > & rects = {} )
nodiscard

Definition at line 390 of file SDL3/sdl_window.cpp.

392{
393 if (rects.empty())
394 {
395 return drawScaledRect(surface, scale, { 0, 0, surface->w, surface->h });
396 }
397
398 /* Upload every dirty rect before drawing any of them: scaling filters linearly, so the
399 * edge of each drawn rect also samples the texels just outside it. Drawn one by one,
400 * those can be a neighbouring dirty rect that is not uploaded yet, and the old picture
401 * stays behind as a faint line along the edge until that area changes again. */
402 for (const auto& srcRect : rects)
403 {
404 if (!upload(surface, srcRect))
405 return false;
406 }
407 for (const auto& srcRect : rects)
408 {
409 if (!render(srcRect, scaledDstRect(scale, srcRect)))
410 return false;
411 }
412 return true;
413}

◆ fill() [1/2]

bool SdlWindow::fill ( SDL_Window * window,
Uint8 r = 0x00,
Uint8 g = 0x00,
Uint8 b = 0x00,
Uint8 a = 0xff )
staticnodiscardprotected

Definition at line 429 of file SDL3/sdl_window.cpp.

430{
431 auto surface = SDL_GetWindowSurface(window);
432 if (!surface)
433 return false;
434 SDL_Rect rect = { 0, 0, surface->w, surface->h };
435 auto color = SDL_MapSurfaceRGBA(surface, r, g, b, a);
436
437 return SDL_FillSurfaceRect(surface, &rect, color);
438}

◆ fill() [2/2]

bool SdlWindow::fill ( Uint8 r = 0x00,
Uint8 g = 0x00,
Uint8 b = 0x00,
Uint8 a = 0xff )

Definition at line 175 of file SDL2/sdl_window.cpp.

176{
177 auto surface = SDL_GetWindowSurface(_window);
178 if (!surface)
179 return false;
180 SDL_Rect rect = { 0, 0, surface->w, surface->h };
181 auto color = SDL_MapRGBA(surface->format, r, g, b, a);
182
183 SDL_FillRect(surface, &rect, color);
184 return true;
185}

◆ fullscreen() [1/2]

void SdlWindow::fullscreen ( bool enter)

Definition at line 131 of file SDL2/sdl_window.cpp.

132{
133 auto curFlags = SDL_GetWindowFlags(_window);
134
135 if (enter)
136 {
137 if (!(curFlags & SDL_WINDOW_BORDERLESS))
138 {
139 auto idx = SDL_GetWindowDisplayIndex(_window);
140 SDL_DisplayMode mode = {};
141 SDL_GetCurrentDisplayMode(idx, &mode);
142
143 SDL_RestoreWindow(_window); // Maximize so we can see the caption and
144 // bits
145 SDL_SetWindowBordered(_window, SDL_FALSE);
146 SDL_SetWindowPosition(_window, 0, 0);
147#if SDL_VERSION_ATLEAST(2, 0, 16)
148 SDL_SetWindowAlwaysOnTop(_window, SDL_TRUE);
149#endif
150 SDL_RaiseWindow(_window);
151 SDL_SetWindowSize(_window, mode.w, mode.h);
152 }
153 }
154 else
155 {
156 if (curFlags & SDL_WINDOW_BORDERLESS)
157 {
158
159 SDL_SetWindowBordered(_window, SDL_TRUE);
160#if SDL_VERSION_ATLEAST(2, 0, 16)
161 SDL_SetWindowAlwaysOnTop(_window, SDL_FALSE);
162#endif
163 SDL_RaiseWindow(_window);
164 SDL_MinimizeWindow(_window); // Maximize so we can see the caption and bits
165 SDL_MaximizeWindow(_window); // Maximize so we can see the caption and bits
166 }
167 }
168}

◆ fullscreen() [2/2]

void SdlWindow::fullscreen ( bool enter,
bool forceOriginalDisplay )

Definition at line 244 of file SDL3/sdl_window.cpp.

245{
246 if (enter && forceOriginalDisplay && _displayID != 0)
247 {
248 /* Move the window to the desired display. We should not wait
249 * for the window to be moved, because some backends can refuse
250 * the move. The intent of moving the window is enough for SDL
251 * to decide which display will be used for fullscreen. */
252 SDL_Rect rect = {};
253 std::ignore = SDL_GetDisplayBounds(_displayID, &rect);
254 std::ignore = SDL_SetWindowPosition(_window, rect.x, rect.y);
255 }
256 std::ignore = SDL_SetWindowFullscreen(_window, enter);
257 std::ignore = SDL_SyncWindow(_window);
258}

◆ grabKeyboard()

bool SdlWindow::grabKeyboard ( bool enable)

Definition at line 90 of file SDL2/sdl_window.cpp.

91{
92 if (!_window)
93 return false;
94#if SDL_VERSION_ATLEAST(2, 0, 16)
95 SDL_SetWindowKeyboardGrab(_window, enable ? SDL_TRUE : SDL_FALSE);
96 return true;
97#else
98 SDL_LogError(SDL_LOG_CATEGORY_INPUT, "Keyboard grabbing not supported by SDL2 < 2.0.16");
99 return false;
100#endif
101}

◆ grabMouse()

bool SdlWindow::grabMouse ( bool enable)

Definition at line 103 of file SDL2/sdl_window.cpp.

104{
105 if (!_window)
106 return false;
107#if SDL_VERSION_ATLEAST(2, 0, 16)
108 SDL_SetWindowMouseGrab(_window, enable ? SDL_TRUE : SDL_FALSE);
109#else
110 SDL_SetWindowGrab(_window, enable ? SDL_TRUE : SDL_FALSE);
111#endif
112 return true;
113}

◆ id()

SDL_WindowID SdlWindow::id ( ) const
nodiscard

Definition at line 40 of file SDL2/sdl_window.cpp.

41{
42 if (!_window)
43 return 0;
44 return SDL_GetWindowID(_window);
45}

◆ isHighDPIWindowsMode()

SdlWindow::HighDPIMode SdlWindow::isHighDPIWindowsMode ( SDL_Window * window)
staticnodiscardprotected

Definition at line 588 of file SDL3/sdl_window.cpp.

589{
590 if (!window)
591 return MODE_INVALID;
592
593 const auto id = SDL_GetDisplayForWindow(window);
594 if (id == 0)
595 return MODE_INVALID;
596
597 const auto cs = SDL_GetDisplayContentScale(id);
598 const auto ds = SDL_GetWindowDisplayScale(window);
599 const auto pd = SDL_GetWindowPixelDensity(window);
600
601 /* mac os x style, but no HighDPI display */
602 if ((cs == 1.0f) && (ds == 1.0f) && (pd == 1.0f))
603 return MODE_NONE;
604
605 /* mac os x style HighDPI */
606 if ((cs == 1.0f) && (ds > 1.0f) && (pd > 1.0f))
607 return MODE_MACOS;
608
609 /* rest is windows style */
610 return MODE_WINDOWS;
611}

◆ layoutPosition()

SDL_Point SdlWindow::layoutPosition ( ) const
nodiscard

Definition at line 170 of file SDL3/sdl_window.cpp.

171{
172 return _layoutPosition;
173}

◆ minimize()

void SdlWindow::minimize ( )

Definition at line 170 of file SDL2/sdl_window.cpp.

171{
172 SDL_MinimizeWindow(_window);
173}

◆ monitor()

rdpMonitor SdlWindow::monitor ( bool isPrimary) const
nodiscard

Definition at line 180 of file SDL3/sdl_window.cpp.

181{
182 auto m = _monitor;
183 if (isPrimary)
184 {
185 m.x = 0;
186 m.y = 0;
187 }
188 return m;
189}

◆ offsetX()

Sint32 SdlWindow::offsetX ( ) const
nodiscard

Definition at line 70 of file SDL2/sdl_window.cpp.

71{
72 return _offset_x;
73}

◆ offsetY()

Sint32 SdlWindow::offsetY ( ) const
nodiscard

Definition at line 85 of file SDL2/sdl_window.cpp.

86{
87 return _offset_y;
88}

◆ orientation()

SDL_DisplayOrientation SdlWindow::orientation ( ) const
nodiscard

Definition at line 201 of file SDL3/sdl_window.cpp.

202{
203 const auto did = displayIndex();
204 return SDL_GetCurrentDisplayOrientation(did);
205}

◆ paintResizeFrame()

bool SdlWindow::paintResizeFrame ( SDL_Surface * surface,
SDL_Point off,
bool contentChanged,
const SDL_Rect & inset = { 0, 0, 0, 0 },
bool fillRevealed = true,
bool dashedBorder = true )

Definition at line 795 of file SDL3/sdl_window.cpp.

797{
798 if (!_renderer || !surface)
799 return false;
800 ensureRenderTarget();
801
802 const int prevW = _gdiTextureW;
803 const int prevH = _gdiTextureH;
804 if (!ensureGdiTexture(surface))
805 return false;
806 /* A recreated texture is empty, so upload even when the content did not change. */
807 const bool recreated = (_gdiTextureW != prevW) || (_gdiTextureH != prevH);
808 if ((contentChanged || recreated) &&
809 !SDL_UpdateTexture(_gdiTexture, nullptr, surface->pixels, surface->pitch))
810 return false;
811
812 if (!SDL_SetRenderTarget(_renderer, _renderTarget))
813 return false;
814
815 int ww = 0;
816 int wh = 0;
817 SDL_GetWindowSizeInPixels(_window, &ww, &wh);
818 /* The visible frame; the ring outside it (the resize band) stays fully transparent. */
819 const SDL_FRect frame = { static_cast<float>(inset.x), static_cast<float>(inset.y),
820 static_cast<float>(ww - inset.x - inset.w),
821 static_cast<float>(wh - inset.y - inset.h) };
822
823 /* Translucent fill in revealed area awaiting server frame. */
824 constexpr Uint8 kFillAlpha = 0x80;
825 std::ignore = SDL_SetRenderDrawBlendMode(_renderer, SDL_BLENDMODE_NONE);
826 std::ignore = SDL_SetRenderDrawColor(_renderer, 0, 0, 0, 0);
827 std::ignore = SDL_RenderClear(_renderer);
828 if (fillRevealed)
829 {
830 std::ignore = SDL_SetRenderDrawColor(_renderer, 0x2B, 0x2B, 0x2B, kFillAlpha);
831 std::ignore = SDL_RenderFillRect(_renderer, &frame);
832 }
833 SDL_FRect fdst = { static_cast<float>(off.x), static_cast<float>(off.y),
834 static_cast<float>(surface->w), static_cast<float>(surface->h) };
835 /* Anchored frame clipped to visible bounds. */
836 const SDL_Rect clip = { inset.x, inset.y, ww - inset.x - inset.w, wh - inset.y - inset.h };
837 std::ignore = SDL_SetRenderClipRect(_renderer, &clip);
838 std::ignore = SDL_RenderTexture(_renderer, _gdiTexture, nullptr, &fdst);
839 std::ignore = SDL_SetRenderClipRect(_renderer, nullptr);
840
841 /* Dashed border indicating pending resize target. */
842 if (dashedBorder)
843 {
844 std::ignore = SDL_SetRenderDrawColor(_renderer, 0xC8, 0xC8, 0xC8, 0xFF);
845 const float dash = 8.0F;
846 const float gap = 5.0F;
847 const float fx2 = frame.x + frame.w;
848 const float fy2 = frame.y + frame.h;
849 const float lo = frame.y + 0.5F;
850 const float by = fy2 - 0.5F;
851 const float lx = frame.x + 0.5F;
852 const float rx = fx2 - 0.5F;
853 const float step = dash + gap;
854 const auto steps = [step](float len)
855 { return (len <= 0.0F) ? 0 : static_cast<int>(std::ceil(len / step)); };
856 for (int i = 0; i < steps(fx2 - frame.x); i++)
857 {
858 const float x = frame.x + (static_cast<float>(i) * step);
859 const float x2 = (x + dash < fx2) ? (x + dash) : rx;
860 std::ignore = SDL_RenderLine(_renderer, x, lo, x2, lo);
861 std::ignore = SDL_RenderLine(_renderer, x, by, x2, by);
862 }
863 for (int i = 0; i < steps(fy2 - frame.y); i++)
864 {
865 const float y = frame.y + (static_cast<float>(i) * step);
866 const float y2 = (y + dash < fy2) ? (y + dash) : by;
867 std::ignore = SDL_RenderLine(_renderer, lx, y, lx, y2);
868 std::ignore = SDL_RenderLine(_renderer, rx, y, rx, y2);
869 }
870 }
871
872 if (!SDL_SetRenderTarget(_renderer, nullptr))
873 return false;
874 std::ignore = SDL_RenderTexture(_renderer, _renderTarget, nullptr, nullptr);
875 drawVideoOverlays();
876 std::ignore = SDL_RenderPresent(_renderer);
877 return true;
878}

◆ pruneVideoOverlays()

void SdlWindow::pruneVideoOverlays ( const std::function< bool(Uint64)> & keep)

drops the overlays keep returns false for

Definition at line 764 of file SDL3/sdl_window.cpp.

765{
766 for (auto it = _videoOverlays.begin(); it != _videoOverlays.end();)
767 {
768 if (keep(it->first))
769 ++it;
770 else
771 {
772 SDL_DestroyTexture(it->second.texture);
773 it = _videoOverlays.erase(it);
774 }
775 }
776}

◆ query() [1/2]

rdpMonitor SdlWindow::query ( SDL_DisplayID id,
bool forceAsPrimary = false )
staticnodiscard

Definition at line 1000 of file SDL3/sdl_window.cpp.

1001{
1002 if (sdl::utils::isWaylandDriver() || sdl::utils::isX11Driver())
1003 return queryDisplay(id, forceAsPrimary);
1004
1005 std::unique_ptr<SDL_Window, void (*)(SDL_Window*)> window(createDummy(id), SDL_DestroyWindow);
1006 if (!window)
1007 return {};
1008
1009 std::unique_ptr<SDL_Renderer, void (*)(SDL_Renderer*)> renderer(
1010 SDL_CreateRenderer(window.get(), nullptr), SDL_DestroyRenderer);
1011
1012 if (!SDL_SyncWindow(window.get()))
1013 return {};
1014
1015 SDL_Event event{};
1016 while (SDL_PollEvent(&event))
1017 ;
1018
1019 return query(window.get(), id, forceAsPrimary);
1020}

◆ query() [2/2]

rdpMonitor SdlWindow::query ( SDL_Window * window,
SDL_DisplayID id,
bool forceAsPrimary = false )
staticnodiscardprotected

Definition at line 494 of file SDL3/sdl_window.cpp.

495{
496 if (!window)
497 return {};
498 return makeMonitor(id, rect(window, forceAsPrimary), SDL_GetWindowDisplayScale(window),
499 forceAsPrimary);
500}

◆ raise()

void SdlWindow::raise ( )

Definition at line 121 of file SDL2/sdl_window.cpp.

122{
123 SDL_RaiseWindow(_window);
124}

◆ rect() [1/3]

SDL_Rect SdlWindow::rect ( ) const
nodiscard

Definition at line 54 of file SDL2/sdl_window.cpp.

55{
56 SDL_Rect rect = {};
57 if (_window)
58 {
59 SDL_GetWindowPosition(_window, &rect.x, &rect.y);
60 SDL_GetWindowSize(_window, &rect.w, &rect.h);
61 }
62 return rect;
63}

◆ rect() [2/3]

SDL_Rect SdlWindow::rect ( SDL_DisplayID id,
bool forceAsPrimary = false )
staticnodiscardprotected

Definition at line 1022 of file SDL3/sdl_window.cpp.

1023{
1024 std::unique_ptr<SDL_Window, void (*)(SDL_Window*)> window(createDummy(id), SDL_DestroyWindow);
1025 if (!window)
1026 return {};
1027
1028 std::unique_ptr<SDL_Renderer, void (*)(SDL_Renderer*)> renderer(
1029 SDL_CreateRenderer(window.get(), nullptr), SDL_DestroyRenderer);
1030
1031 if (!SDL_SyncWindow(window.get()))
1032 return {};
1033
1034 SDL_Event event{};
1035 while (SDL_PollEvent(&event))
1036 ;
1037
1038 return rect(window.get(), forceAsPrimary);
1039}

◆ rect() [3/3]

SDL_Rect SdlWindow::rect ( SDL_Window * window,
bool forceAsPrimary = false )
staticnodiscardprotected

Definition at line 522 of file SDL3/sdl_window.cpp.

523{
524 SDL_Rect rect = {};
525 if (!window)
526 return {};
527
528 if (!forceAsPrimary)
529 {
530 if (!SDL_GetWindowPosition(window, &rect.x, &rect.y))
531 return {};
532 }
533
534 if (!SDL_GetWindowSizeInPixels(window, &rect.w, &rect.h))
535 return {};
536
537 const bool placeholder = (rect.w < 200) || (rect.h < 200);
538 if (placeholder && tryFallback(true))
539 {
540 /* On wlroots compositors (Sway, river, etc.), windows that are hidden/unmapped
541 * don't get their actual display dimensions. The dummy window returns its creation size
542 * (64x64) instead of the display size. This causes validation errors since we require >=
543 * 200px. Workaround: If we got dimensions that are too small, query the display directly.
544 */
545
546 const auto displayID = SDL_GetDisplayForWindow(window);
547 SDL_Rect displayBounds = {};
548 if (SDL_GetDisplayBounds(displayID, &displayBounds))
549 {
550 if (forceAsPrimary)
551 {
552 rect.x = 0;
553 rect.y = 0;
554 }
555 else
556 {
557 /* The dummy window is centered on the display by the compositor, so its
558 * position is not the display origin. Use the display bounds as for w/h. */
559 rect.x = displayBounds.x;
560 rect.y = displayBounds.y;
561 }
562 rect.w = displayBounds.w;
563 rect.h = displayBounds.h;
564
565 float factor = 1.0f;
566 const auto mode = SDL_GetCurrentDisplayMode(displayID);
567 if (mode && (mode->pixel_density > 1.0f))
568 factor = mode->pixel_density;
569 else
570 {
571 const float contentScale = SDL_GetDisplayContentScale(displayID);
572 if (contentScale > 1.0f)
573 factor = contentScale;
574 }
575 if (factor > 1.0f)
576 {
577 const auto fw = static_cast<float>(rect.w);
578 const auto fh = static_cast<float>(rect.h);
579 rect.w = static_cast<int>(std::roundf(fw * factor));
580 rect.h = static_cast<int>(std::roundf(fh * factor));
581 }
582 }
583 }
584
585 return rect;
586}

◆ render()

bool SdlWindow::render ( const SDL_Rect & src,
const SDL_Rect & dst )
nodiscard

Definition at line 661 of file SDL3/sdl_window.cpp.

662{
663 if (!_renderer || !_gdiTexture)
664 return false;
665
666 /* Render onto persistent render target to accumulate dirty rects */
667 if (!SDL_SetRenderTarget(_renderer, _renderTarget))
668 return false;
669
670 SDL_FRect fsrc = { static_cast<float>(srcRect.x), static_cast<float>(srcRect.y),
671 static_cast<float>(srcRect.w), static_cast<float>(srcRect.h) };
672 SDL_FRect fdst = { static_cast<float>(dstRect.x), static_cast<float>(dstRect.y),
673 static_cast<float>(dstRect.w), static_cast<float>(dstRect.h) };
674 if (!SDL_RenderTexture(_renderer, _gdiTexture, &fsrc, &fdst))
675 {
676 SDL_LogError(SDL_LOG_CATEGORY_RENDER, "SDL_RenderTexture: %s", SDL_GetError());
677 return false;
678 }
679 return true;
680}

◆ renderer()

SDL_Renderer * SdlWindow::renderer ( ) const
nodiscard

Definition at line 145 of file SDL3/sdl_window.cpp.

146{
147 return _renderer;
148}

◆ rendererName()

const char * SdlWindow::rendererName ( ) const
nodiscard

Definition at line 778 of file SDL3/sdl_window.cpp.

779{
780 return _renderer ? SDL_GetRendererName(_renderer) : nullptr;
781}

◆ resize()

bool SdlWindow::resize ( const SDL_Point & size)
nodiscard

Definition at line 298 of file SDL3/sdl_window.cpp.

299{
300 return SDL_SetWindowSize(_window, size.x, size.y);
301}

◆ resizeable()

void SdlWindow::resizeable ( bool use)

Definition at line 126 of file SDL2/sdl_window.cpp.

127{
128 SDL_SetWindowResizable(_window, use ? SDL_TRUE : SDL_FALSE);
129}

◆ resizeToScale()

bool SdlWindow::resizeToScale ( )
nodiscard

Definition at line 266 of file SDL3/sdl_window.cpp.

267{
268 if (!_window || _initialW <= 0 || _initialH <= 0)
269 return false;
270 if ((SDL_GetWindowFlags(_window) & SDL_WINDOW_FULLSCREEN) != 0)
271 return true;
272
273 float pd = SDL_GetWindowPixelDensity(_window);
274 if (pd <= 0.0f)
275 pd = 1.0f;
276
277 /* On macOS window sizes are in points, and a HighDPI display has more than one pixel
278 * per point. Keep the requested size in points, like the native mac client did, and let
279 * local scaling fill the window. Dividing by the pixel density would make the window a
280 * quarter of the expected area on a Retina display. */
281 if (isHighDPIWindowsMode(_window) == MODE_MACOS)
282 pd = 1.0f;
283
284 const int targetW = static_cast<int>(std::ceil(static_cast<float>(_initialW) / pd));
285 const int targetH = static_cast<int>(std::ceil(static_cast<float>(_initialH) / pd));
286
287 int curW = 0;
288 int curH = 0;
289 if (!SDL_GetWindowSize(_window, &curW, &curH))
290 return false;
291
292 if (curW == targetW && curH == targetH)
293 return true;
294
295 return resize({ targetW, targetH });
296}

◆ scale()

float SdlWindow::scale ( ) const
nodiscard

Definition at line 196 of file SDL3/sdl_window.cpp.

197{
198 return SDL_GetWindowDisplayScale(_window);
199}

◆ setBordered()

void SdlWindow::setBordered ( bool bordered)

Definition at line 115 of file SDL2/sdl_window.cpp.

116{
117 if (_window)
118 SDL_SetWindowBordered(_window, bordered ? SDL_TRUE : SDL_FALSE);
119}

◆ setLayoutPosition()

void SdlWindow::setLayoutPosition ( const SDL_Point & pos)

Definition at line 175 of file SDL3/sdl_window.cpp.

176{
177 _layoutPosition = pos;
178}

◆ setMonitor()

void SdlWindow::setMonitor ( rdpMonitor monitor)

Definition at line 191 of file SDL3/sdl_window.cpp.

192{
193 _monitor = monitor;
194}

◆ setOffsetX()

void SdlWindow::setOffsetX ( Sint32 x)

Definition at line 75 of file SDL2/sdl_window.cpp.

76{
77 _offset_x = x;
78}

◆ setOffsetY()

void SdlWindow::setOffsetY ( Sint32 y)

Definition at line 80 of file SDL2/sdl_window.cpp.

81{
82 _offset_y = y;
83}

◆ setVideoOverlay()

bool SdlWindow::setVideoOverlay ( Uint64 id,
const void * pixels,
int pitch,
int w,
int h,
const SDL_FRect & dst,
const std::vector< SDL_FRect > & clip,
bool visible )
nodiscard

redirected video id, drawn over the session at each present at dst (render coordinates), only in clip (render coordinates) when not empty: what the page draws over the video (player controls) stays visible. pixels (BGRA, wxh) is uploaded when not null

Definition at line 729 of file SDL3/sdl_window.cpp.

732{
733 if (!_renderer || (w <= 0) || (h <= 0))
734 return false;
735 auto& o = _videoOverlays[id];
736 if (!o.texture || (o.w != w) || (o.h != h))
737 {
738 if (o.texture)
739 SDL_DestroyTexture(o.texture);
740 o.texture =
741 SDL_CreateTexture(_renderer, SDL_PIXELFORMAT_BGRA32, SDL_TEXTUREACCESS_STREAMING, w, h);
742 if (!o.texture)
743 {
744 SDL_LogError(SDL_LOG_CATEGORY_RENDER, "SDL_CreateTexture: %s", SDL_GetError());
745 _videoOverlays.erase(id);
746 return false;
747 }
748 /* the video is opaque: its alpha is not for us to blend */
749 std::ignore = SDL_SetTextureBlendMode(o.texture, SDL_BLENDMODE_NONE);
750 o.w = w;
751 o.h = h;
752 }
753 if (pixels && !SDL_UpdateTexture(o.texture, nullptr, pixels, pitch))
754 {
755 SDL_LogError(SDL_LOG_CATEGORY_RENDER, "SDL_UpdateTexture: %s", SDL_GetError());
756 return false;
757 }
758 o.dst = dst;
759 o.clip = clip;
760 o.visible = visible;
761 return true;
762}

◆ tryFallback()

bool SdlWindow::tryFallback ( bool isFullscreen)
staticnodiscardprotected

Definition at line 1041 of file SDL3/sdl_window.cpp.

1042{
1043 /* If we define a custom env variable to use the wlroots hack
1044 * then enable/disable according to this setting only.
1045 */
1046 const auto wlroots_hack = SDL_getenv("FREERDP_WLROOTS_HACK");
1047 if (wlroots_hack != nullptr)
1048 {
1049 const auto enabled = strcmp(wlroots_hack, "0") != 0;
1050 if (strcmp(wlroots_hack, "force") == 0)
1051 isFullscreen = true;
1052 return enabled && isFullscreen;
1053 }
1054
1055 const auto platform = SDL_GetPlatform();
1056 if ((platform == nullptr) || (strcmp(platform, "Linux") != 0))
1057 return false;
1058
1059 const auto driver = SDL_GetCurrentVideoDriver();
1060 if ((driver == nullptr) || (strcmp(driver, "wayland") != 0))
1061 return false;
1062
1063 /* Check XDG_SESSION_DESKTOP and XDG_CURRENT_DESKTOP for wlroots-based
1064 * compositors. The original check only matched Sway, but other wlroots
1065 * compositors (Hyprland, river, etc.) have the same dummy-window sizing
1066 * behavior where hidden/unmapped windows return 64x64 instead of the
1067 * display size. Use strstr for substring matching since XDG_CURRENT_DESKTOP
1068 * can be a colon-separated list (e.g. "sway:wlroots", "Hyprland").
1069 */
1070 auto isWlrootsCompositor = [](const char* value) -> bool
1071 {
1072 if (!value)
1073 return false;
1074 if (strstr(value, "sway") || strstr(value, "Sway") || strstr(value, "Hyprland") ||
1075 strstr(value, "hyprland") || strstr(value, "river") || strstr(value, "wlroots"))
1076 return true;
1077 return false;
1078 };
1079
1080 const auto xdg_session = SDL_getenv("XDG_SESSION_DESKTOP");
1081 if (isWlrootsCompositor(xdg_session))
1082 return isFullscreen;
1083
1084 const auto xdg_desktop = SDL_getenv("XDG_CURRENT_DESKTOP");
1085 if (isWlrootsCompositor(xdg_desktop))
1086 return isFullscreen;
1087
1088 return false;
1089}

◆ updateSurface() [1/2]

void SdlWindow::updateSurface ( )

Definition at line 204 of file SDL2/sdl_window.cpp.

205{
206 SDL_UpdateWindowSurface(_window);
207}

◆ updateSurface() [2/2]

void SdlWindow::updateSurface ( const SdlOverlay * overlay = nullptr)

Present the picture, with the overlay over it when one is given and shown.

Definition at line 682 of file SDL3/sdl_window.cpp.

683{
684 if (!_renderer)
685 return;
686
687 ensureRenderTarget();
688
689 /* Copy accumulated render target to screen and present */
690 if (!SDL_SetRenderTarget(_renderer, nullptr))
691 return;
692 if (!SDL_RenderTexture(_renderer, _renderTarget, nullptr, nullptr))
693 return;
694 drawVideoOverlays();
695 /* over the picture, never into it */
696 if (overlay && overlay->visible())
697 drawOverlay(*overlay);
698 if (!SDL_RenderPresent(_renderer))
699 return;
700}

◆ upload()

bool SdlWindow::upload ( SDL_Surface * surface,
const SDL_Rect & src )
nodiscard

Definition at line 638 of file SDL3/sdl_window.cpp.

639{
640 if (!_renderer || !surface)
641 return false;
642
643 ensureRenderTarget();
644
645 if (!ensureGdiTexture(surface))
646 return false;
647
648 /* Upload only the dirty region */
649 const auto* details = SDL_GetPixelFormatDetails(surface->format);
650 const int bpp = details ? details->bytes_per_pixel : 4;
651 const auto* pixels = static_cast<const uint8_t*>(surface->pixels) +
652 (1ll * srcRect.y * surface->pitch) + (1ll * srcRect.x * bpp);
653 if (!SDL_UpdateTexture(_gdiTexture, &srcRect, pixels, surface->pitch))
654 {
655 SDL_LogError(SDL_LOG_CATEGORY_RENDER, "SDL_UpdateTexture: %s", SDL_GetError());
656 return false;
657 }
658 return true;
659}

◆ window()

SDL_Window * SdlWindow::window ( ) const
nodiscard

Definition at line 65 of file SDL2/sdl_window.cpp.

66{
67 return _window;
68}

The documentation for this class was generated from the following files: