24#include "sdl_window.hpp"
25#include "sdl_utils.hpp"
27#include <freerdp/utils/string.h>
30 [[maybe_unused]] Uint32 flags)
33 auto props = SDL_CreateProperties();
34 SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, title.c_str());
35 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, rect.x);
36 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, rect.y);
37 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, rect.w);
38 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, rect.h);
40 if (flags & SDL_WINDOW_HIGH_PIXEL_DENSITY)
41 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIGH_PIXEL_DENSITY_BOOLEAN,
true);
43 if (flags & SDL_WINDOW_FULLSCREEN)
44 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_FULLSCREEN_BOOLEAN,
true);
46 if (flags & SDL_WINDOW_BORDERLESS)
47 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_BORDERLESS_BOOLEAN,
true);
49 _window = SDL_CreateWindowWithProperties(props);
50 SDL_DestroyProperties(props);
52 std::ignore = resizeToPixelSize({ rect.w, rect.h });
53 SDL_SetHint(SDL_HINT_APP_NAME,
"");
54 std::ignore = SDL_SyncWindow(_window);
56 _monitor = query(_window,
id,
true);
60 : _window(other._window), _displayID(other._displayID), _offset_x(other._offset_x),
61 _offset_y(other._offset_y), _monitor(other._monitor)
63 other._window =
nullptr;
66SdlWindow::~SdlWindow()
68 SDL_DestroyWindow(_window);
71SDL_WindowID SdlWindow::id()
const
75 return SDL_GetWindowID(_window);
78SDL_DisplayID SdlWindow::displayIndex()
const
82 return SDL_GetDisplayForWindow(_window);
85SDL_Rect SdlWindow::rect()
const
90SDL_Rect SdlWindow::bounds()
const
95 if (!SDL_GetWindowPosition(_window, &rect.x, &rect.y))
97 if (!SDL_GetWindowSize(_window, &rect.w, &rect.h))
103SDL_Window* SdlWindow::window()
const
108Sint32 SdlWindow::offsetX()
const
113void SdlWindow::setOffsetX(Sint32 x)
118void SdlWindow::setOffsetY(Sint32 y)
123Sint32 SdlWindow::offsetY()
const
128rdpMonitor SdlWindow::monitor(
bool isPrimary)
const
144float SdlWindow::scale()
const
146 return SDL_GetWindowDisplayScale(_window);
149SDL_DisplayOrientation SdlWindow::orientation()
const
151 const auto did = displayIndex();
152 return SDL_GetCurrentDisplayOrientation(did);
155bool SdlWindow::grabKeyboard(
bool enable)
159 SDL_SetWindowKeyboardGrab(_window, enable);
163bool SdlWindow::grabMouse(
bool enable)
167 SDL_SetWindowMouseGrab(_window, enable);
171void SdlWindow::setBordered(
bool bordered)
174 SDL_SetWindowBordered(_window, bordered);
175 std::ignore = SDL_SyncWindow(_window);
178void SdlWindow::raise()
180 SDL_RaiseWindow(_window);
181 std::ignore = SDL_SyncWindow(_window);
184void SdlWindow::resizeable(
bool use)
186 SDL_SetWindowResizable(_window, use);
187 std::ignore = SDL_SyncWindow(_window);
190void SdlWindow::fullscreen(
bool enter,
bool forceOriginalDisplay)
192 if (enter && forceOriginalDisplay && _displayID != 0)
199 std::ignore = SDL_GetDisplayBounds(_displayID, &rect);
200 std::ignore = SDL_SetWindowPosition(_window, rect.x, rect.y);
202 std::ignore = SDL_SetWindowFullscreen(_window, enter);
203 std::ignore = SDL_SyncWindow(_window);
206void SdlWindow::minimize()
208 SDL_MinimizeWindow(_window);
209 std::ignore = SDL_SyncWindow(_window);
212bool SdlWindow::resizeToPixelSize(
const SDL_Point& size)
215 const int iscale =
static_cast<int>(sc * 100.0f);
216 auto w = 100 * size.x / iscale;
217 auto h = 100 * size.y / iscale;
218 return resize({ w, h });
221bool SdlWindow::resize(
const SDL_Point& size)
223 return SDL_SetWindowSize(_window, size.x, size.y);
226bool SdlWindow::drawRect(SDL_Surface* surface, SDL_Point offset,
const SDL_Rect& srcRect)
228 WINPR_ASSERT(surface);
229 SDL_Rect dstRect = { offset.x + srcRect.x, offset.y + srcRect.y, srcRect.w, srcRect.h };
230 return blit(surface, srcRect, dstRect);
233bool SdlWindow::drawRects(SDL_Surface* surface, SDL_Point offset,
234 const std::vector<SDL_Rect>& rects)
238 return drawRect(surface, offset, { 0, 0, surface->w, surface->h });
240 for (
auto& srcRect : rects)
242 if (!drawRect(surface, offset, srcRect))
248bool SdlWindow::drawScaledRect(SDL_Surface* surface,
const SDL_FPoint& scale,
249 const SDL_Rect& srcRect)
251 SDL_Rect dstRect = srcRect;
252 dstRect.x =
static_cast<Sint32
>(
static_cast<float>(dstRect.x) * scale.x);
253 dstRect.w =
static_cast<Sint32
>(
static_cast<float>(dstRect.w) * scale.x);
254 dstRect.y =
static_cast<Sint32
>(
static_cast<float>(dstRect.y) * scale.y);
255 dstRect.h =
static_cast<Sint32
>(
static_cast<float>(dstRect.h) * scale.y);
256 return blit(surface, srcRect, dstRect);
259bool SdlWindow::drawScaledRects(SDL_Surface* surface,
const SDL_FPoint& scale,
260 const std::vector<SDL_Rect>& rects)
264 return drawScaledRect(surface, scale, { 0, 0, surface->w, surface->h });
266 for (
const auto& srcRect : rects)
268 if (!drawScaledRect(surface, scale, srcRect))
274bool SdlWindow::fill(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
276 return fill(_window, r, g, b, a);
279bool SdlWindow::fill(SDL_Window* window, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
281 auto surface = SDL_GetWindowSurface(window);
284 SDL_Rect rect = { 0, 0, surface->w, surface->h };
285 auto color = SDL_MapSurfaceRGBA(surface, r, g, b, a);
287 return SDL_FillSurfaceRect(surface, &rect, color);
290rdpMonitor SdlWindow::query(SDL_Window* window, SDL_DisplayID
id,
bool forceAsPrimary)
295 const auto& r = rect(window, forceAsPrimary);
296 const float factor = SDL_GetWindowDisplayScale(window);
297 const float dpi = std::roundf(factor * 100.0f);
299 WINPR_ASSERT(r.w > 0);
300 WINPR_ASSERT(r.h > 0);
302 const auto primary = SDL_GetPrimaryDisplay();
303 const auto orientation = SDL_GetCurrentDisplayOrientation(
id);
304 const auto rdp_orientation = sdl::utils::orientaion_to_rdp(orientation);
307 monitor.orig_screen = id;
311 monitor.height = r.h;
312 monitor.is_primary = forceAsPrimary || (
id == primary);
313 monitor.attributes.desktopScaleFactor =
static_cast<UINT32
>(dpi);
314 monitor.attributes.deviceScaleFactor = 100;
315 monitor.attributes.orientation = rdp_orientation;
316 monitor.attributes.physicalWidth = WINPR_ASSERTING_INT_CAST(uint32_t, r.w);
317 monitor.attributes.physicalHeight = WINPR_ASSERTING_INT_CAST(uint32_t, r.h);
319 const auto cat = SDL_LOG_CATEGORY_APPLICATION;
320 SDL_LogDebug(cat,
"monitor.orig_screen %" PRIu32, monitor.orig_screen);
321 SDL_LogDebug(cat,
"monitor.x %" PRId32, monitor.x);
322 SDL_LogDebug(cat,
"monitor.y %" PRId32, monitor.y);
323 SDL_LogDebug(cat,
"monitor.width %" PRId32, monitor.width);
324 SDL_LogDebug(cat,
"monitor.height %" PRId32, monitor.height);
325 SDL_LogDebug(cat,
"monitor.is_primary %" PRIu32, monitor.is_primary);
326 SDL_LogDebug(cat,
"monitor.attributes.desktopScaleFactor %" PRIu32,
327 monitor.attributes.desktopScaleFactor);
328 SDL_LogDebug(cat,
"monitor.attributes.deviceScaleFactor %" PRIu32,
329 monitor.attributes.deviceScaleFactor);
330 SDL_LogDebug(cat,
"monitor.attributes.orientation %s",
331 freerdp_desktop_rotation_flags_to_string(monitor.attributes.orientation));
332 SDL_LogDebug(cat,
"monitor.attributes.physicalWidth %" PRIu32,
333 monitor.attributes.physicalWidth);
334 SDL_LogDebug(cat,
"monitor.attributes.physicalHeight %" PRIu32,
335 monitor.attributes.physicalHeight);
339SDL_Rect SdlWindow::rect(SDL_Window* window,
bool forceAsPrimary)
347 if (!SDL_GetWindowPosition(window, &rect.x, &rect.y))
351 if (!SDL_GetWindowSizeInPixels(window, &rect.w, &rect.h))
354 const auto flags = SDL_GetWindowFlags(window);
355 const auto mask = SDL_WINDOW_FULLSCREEN;
356 const auto fs = (flags & mask) == mask;
365 const auto displayID = SDL_GetDisplayForWindow(window);
366 SDL_Rect displayBounds = {};
367 if (SDL_GetDisplayBounds(displayID, &displayBounds))
374 rect.w = displayBounds.w;
375 rect.h = displayBounds.h;
377 const float contentScale = SDL_GetDisplayContentScale(displayID);
378 if (contentScale > 1.0f)
380 const auto fw =
static_cast<float>(rect.w);
381 const auto fh =
static_cast<float>(rect.h);
382 rect.w =
static_cast<int>(std::roundf(fw * contentScale));
383 rect.h =
static_cast<int>(std::roundf(fh * contentScale));
391SdlWindow::HighDPIMode SdlWindow::isHighDPIWindowsMode(SDL_Window* window)
396 const auto id = SDL_GetDisplayForWindow(window);
400 const auto cs = SDL_GetDisplayContentScale(
id);
401 const auto ds = SDL_GetWindowDisplayScale(window);
402 const auto pd = SDL_GetWindowPixelDensity(window);
405 if ((cs == 1.0f) && (ds == 1.0f) && (pd == 1.0f))
409 if ((cs == 1.0f) && (ds > 1.0f) && (pd > 1.0f))
416bool SdlWindow::blit(SDL_Surface* surface,
const SDL_Rect& srcRect, SDL_Rect& dstRect)
418 auto screen = SDL_GetWindowSurface(_window);
419 if (!screen || !surface)
421 if (!SDL_SetSurfaceClipRect(surface, &srcRect))
423 if (!SDL_SetSurfaceClipRect(screen, &dstRect))
425 if (!SDL_BlitSurfaceScaled(surface, &srcRect, screen, &dstRect, SDL_SCALEMODE_LINEAR))
427 SDL_LogError(SDL_LOG_CATEGORY_RENDER,
"SDL_BlitScaled: %s", SDL_GetError());
433void SdlWindow::updateSurface()
435 SDL_UpdateWindowSurface(_window);
438SdlWindow SdlWindow::create(SDL_DisplayID
id,
const std::string& title, Uint32 flags, Uint32 width,
441 flags |= SDL_WINDOW_HIGH_PIXEL_DENSITY;
443 SDL_Rect rect = {
static_cast<int>(SDL_WINDOWPOS_CENTERED_DISPLAY(
id)),
444 static_cast<int>(SDL_WINDOWPOS_CENTERED_DISPLAY(
id)),
static_cast<int>(width),
445 static_cast<int>(height) };
447 if ((flags & SDL_WINDOW_FULLSCREEN) != 0)
449 std::ignore = SDL_GetDisplayBounds(
id, &rect);
452 SdlWindow window{ id, title, rect, flags };
454 if ((flags & (SDL_WINDOW_FULLSCREEN)) != 0)
456 window.setOffsetX(rect.x);
457 window.setOffsetY(rect.y);
463static SDL_Window* createDummy(SDL_DisplayID
id)
465 const auto x = SDL_WINDOWPOS_CENTERED_DISPLAY(
id);
466 const auto y = SDL_WINDOWPOS_CENTERED_DISPLAY(
id);
470 auto props = SDL_CreateProperties();
471 std::stringstream ss;
472 ss <<
"SdlWindow::query(" <<
id <<
")";
473 SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, ss.str().c_str());
474 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, x);
475 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, y);
476 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, w);
477 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, h);
479 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIGH_PIXEL_DENSITY_BOOLEAN,
true);
480 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_FULLSCREEN_BOOLEAN,
false);
481 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_BORDERLESS_BOOLEAN,
true);
482 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIDDEN_BOOLEAN,
false);
484 auto window = SDL_CreateWindowWithProperties(props);
485 SDL_DestroyProperties(props);
493 std::ignore = SDL_GetDisplayBounds(
id, &rect);
494 std::ignore = SDL_SetWindowPosition(window, rect.x, rect.y);
495 std::ignore = SDL_SetWindowFullscreen(window,
true);
500rdpMonitor SdlWindow::query(SDL_DisplayID
id,
bool forceAsPrimary)
502 std::unique_ptr<SDL_Window, void (*)(SDL_Window*)> window(createDummy(
id), SDL_DestroyWindow);
506 std::unique_ptr<SDL_Renderer, void (*)(SDL_Renderer*)> renderer(
507 SDL_CreateRenderer(window.get(),
nullptr), SDL_DestroyRenderer);
509 if (!SDL_SyncWindow(window.get()))
513 while (SDL_PollEvent(&event))
516 return query(window.get(),
id, forceAsPrimary);
519SDL_Rect SdlWindow::rect(SDL_DisplayID
id,
bool forceAsPrimary)
521 std::unique_ptr<SDL_Window, void (*)(SDL_Window*)> window(createDummy(
id), SDL_DestroyWindow);
525 std::unique_ptr<SDL_Renderer, void (*)(SDL_Renderer*)> renderer(
526 SDL_CreateRenderer(window.get(),
nullptr), SDL_DestroyRenderer);
528 if (!SDL_SyncWindow(window.get()))
532 while (SDL_PollEvent(&event))
535 return rect(window.get(), forceAsPrimary);
538bool SdlWindow::tryFallback(
bool isFullscreen)
543 const auto wlroots_hack = SDL_getenv(
"FREERDP_WLROOTS_HACK");
544 if (wlroots_hack !=
nullptr)
546 const auto enabled = strcmp(wlroots_hack,
"0") != 0;
547 if (strcmp(wlroots_hack,
"force") == 0)
549 return enabled && isFullscreen;
552 const auto platform = SDL_GetPlatform();
553 if ((platform ==
nullptr) || (strcmp(platform,
"Linux") != 0))
556 const auto driver = SDL_GetCurrentVideoDriver();
557 if ((driver ==
nullptr) || (strcmp(driver,
"wayland") != 0))
567 const auto xdg_session = SDL_getenv(
"XDG_SESSION_DESKTOP");
568 if (xdg_session !=
nullptr)
570 if (strcmp(xdg_session,
"sway") == 0)
581 const auto xdg_desktop = SDL_getenv(
"XDG_CURRENT_DESKTOP");
582 if (xdg_desktop !=
nullptr)
584 if (strcmp(xdg_desktop,
"sway:wlroots") == 0)
SdlWindow(const std::string &title, Sint32 startupX, Sint32 startupY, Sint32 width, Sint32 height, Uint32 flags)