24#include "sdl_window.hpp"
25#include "sdl_utils.hpp"
27#include <freerdp/utils/string.h>
30 [[maybe_unused]] Uint32 flags)
31 : _initialW(rect.w), _initialH(rect.h), _displayID(id)
33 float pd = SDL_GetDisplayContentScale(
id);
36 const int createW =
static_cast<int>(std::ceil(
static_cast<float>(rect.w) / pd));
37 const int createH =
static_cast<int>(std::ceil(
static_cast<float>(rect.h) / pd));
39 auto props = SDL_CreateProperties();
40 SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, title.c_str());
41 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, rect.x);
42 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, rect.y);
43 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, createW);
44 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, createH);
45 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_RESIZABLE_BOOLEAN,
true);
47 if (flags & SDL_WINDOW_HIGH_PIXEL_DENSITY)
48 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIGH_PIXEL_DENSITY_BOOLEAN,
true);
50 if (flags & SDL_WINDOW_FULLSCREEN)
51 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_FULLSCREEN_BOOLEAN,
true);
53 if (flags & SDL_WINDOW_BORDERLESS)
54 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_BORDERLESS_BOOLEAN,
true);
56 _window = SDL_CreateWindowWithProperties(props);
57 SDL_DestroyProperties(props);
58 SDL_SetHint(SDL_HINT_APP_NAME,
"");
59 std::ignore = SDL_SyncWindow(_window);
61 _renderer = SDL_CreateRenderer(_window,
nullptr);
63 std::ignore = resizeToScale();
65 _monitor = query(_window,
id,
true);
69 : _window(other._window), _renderer(other._renderer), _renderTarget(other._renderTarget),
70 _gdiTexture(other._gdiTexture), _gdiTextureW(other._gdiTextureW),
71 _gdiTextureH(other._gdiTextureH), _initialW(other._initialW), _initialH(other._initialH),
72 _displayID(other._displayID), _offset_x(other._offset_x), _offset_y(other._offset_y),
73 _monitor(other._monitor)
75 other._window =
nullptr;
76 other._renderer =
nullptr;
77 other._renderTarget =
nullptr;
78 other._gdiTexture =
nullptr;
81SdlWindow::~SdlWindow()
84 SDL_DestroyTexture(_gdiTexture);
86 SDL_DestroyTexture(_renderTarget);
88 SDL_DestroyRenderer(_renderer);
90 SDL_DestroyWindow(_window);
93SDL_WindowID SdlWindow::id()
const
97 return SDL_GetWindowID(_window);
100SDL_DisplayID SdlWindow::displayIndex()
const
104 return SDL_GetDisplayForWindow(_window);
107SDL_Rect SdlWindow::rect()
const
109 return rect(_window);
112SDL_Rect SdlWindow::bounds()
const
117 if (!SDL_GetWindowPosition(_window, &rect.x, &rect.y))
119 if (!SDL_GetWindowSize(_window, &rect.w, &rect.h))
125SDL_Window* SdlWindow::window()
const
130SDL_Renderer* SdlWindow::renderer()
const
135Sint32 SdlWindow::offsetX()
const
140void SdlWindow::setOffsetX(Sint32 x)
145void SdlWindow::setOffsetY(Sint32 y)
150Sint32 SdlWindow::offsetY()
const
155rdpMonitor SdlWindow::monitor(
bool isPrimary)
const
171float SdlWindow::scale()
const
173 return SDL_GetWindowDisplayScale(_window);
176SDL_DisplayOrientation SdlWindow::orientation()
const
178 const auto did = displayIndex();
179 return SDL_GetCurrentDisplayOrientation(did);
182bool SdlWindow::grabKeyboard(
bool enable)
186 SDL_SetWindowKeyboardGrab(_window, enable);
190bool SdlWindow::grabMouse(
bool enable)
194 SDL_SetWindowMouseGrab(_window, enable);
198void SdlWindow::setBordered(
bool bordered)
201 SDL_SetWindowBordered(_window, bordered);
202 std::ignore = SDL_SyncWindow(_window);
205void SdlWindow::raise()
207 SDL_RaiseWindow(_window);
208 std::ignore = SDL_SyncWindow(_window);
211void SdlWindow::resizeable(
bool use)
213 SDL_SetWindowResizable(_window, use);
214 std::ignore = SDL_SyncWindow(_window);
217void SdlWindow::fullscreen(
bool enter,
bool forceOriginalDisplay)
219 if (enter && forceOriginalDisplay && _displayID != 0)
226 std::ignore = SDL_GetDisplayBounds(_displayID, &rect);
227 std::ignore = SDL_SetWindowPosition(_window, rect.x, rect.y);
229 std::ignore = SDL_SetWindowFullscreen(_window, enter);
230 std::ignore = SDL_SyncWindow(_window);
233void SdlWindow::minimize()
235 SDL_MinimizeWindow(_window);
236 std::ignore = SDL_SyncWindow(_window);
239bool SdlWindow::resizeToScale()
241 if (!_window || _initialW <= 0 || _initialH <= 0)
243 if ((SDL_GetWindowFlags(_window) & SDL_WINDOW_FULLSCREEN) != 0)
246 float pd = SDL_GetWindowPixelDensity(_window);
250 const int targetW =
static_cast<int>(std::ceil(
static_cast<float>(_initialW) / pd));
251 const int targetH =
static_cast<int>(std::ceil(
static_cast<float>(_initialH) / pd));
255 if (!SDL_GetWindowSize(_window, &curW, &curH))
258 if (curW == targetW && curH == targetH)
261 return resize({ targetW, targetH });
264bool SdlWindow::resize(
const SDL_Point& size)
266 return SDL_SetWindowSize(_window, size.x, size.y);
269void SdlWindow::ensureRenderTarget()
276 SDL_GetWindowSizeInPixels(_window, &w, &h);
277 if (w <= 0 || h <= 0)
285 if (!SDL_GetTextureSize(_renderTarget, &tw, &th))
287 if (
static_cast<int>(tw) == w &&
static_cast<int>(th) == h)
289 SDL_DestroyTexture(_renderTarget);
293 SDL_CreateTexture(_renderer, SDL_PIXELFORMAT_BGRA32, SDL_TEXTUREACCESS_TARGET, w, h);
295 SDL_LogError(SDL_LOG_CATEGORY_RENDER,
"SDL_CreateTexture (render target): %s",
299bool SdlWindow::drawRect(SDL_Surface* surface, SDL_Point offset,
const SDL_Rect& srcRect)
301 WINPR_ASSERT(surface);
302 SDL_Rect dstRect = { offset.x + srcRect.x, offset.y + srcRect.y, srcRect.w, srcRect.h };
303 return blit(surface, srcRect, dstRect);
306bool SdlWindow::drawRects(SDL_Surface* surface, SDL_Point offset,
307 const std::vector<SDL_Rect>& rects)
311 return drawRect(surface, offset, { 0, 0, surface->w, surface->h });
313 for (
auto& srcRect : rects)
315 if (!drawRect(surface, offset, srcRect))
321bool SdlWindow::drawScaledRect(SDL_Surface* surface,
const SDL_FPoint& scale,
322 const SDL_Rect& srcRect)
324 SDL_Rect dstRect = {};
327 const auto modx = std::modf(
static_cast<float>(srcRect.x) * scale.x, &ix);
328 const auto mody = std::modf(
static_cast<float>(srcRect.y) * scale.y, &iy);
329 auto sw = std::ceil(
static_cast<float>(srcRect.w) * scale.x) + std::ceil(modx);
330 auto sh = std::ceil(
static_cast<float>(srcRect.h) * scale.y) + std::ceil(mody);
331 dstRect.x =
static_cast<Sint32
>(ix);
332 dstRect.w =
static_cast<Sint32
>(sw);
333 dstRect.y =
static_cast<Sint32
>(iy);
334 dstRect.h =
static_cast<Sint32
>(sh);
335 return blit(surface, srcRect, dstRect);
338bool SdlWindow::drawScaledRects(SDL_Surface* surface,
const SDL_FPoint& scale,
339 const std::vector<SDL_Rect>& rects)
343 return drawScaledRect(surface, scale, { 0, 0, surface->w, surface->h });
345 for (
const auto& srcRect : rects)
347 if (!drawScaledRect(surface, scale, srcRect))
353bool SdlWindow::fill(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
357 ensureRenderTarget();
358 if (!SDL_SetRenderTarget(_renderer, _renderTarget))
360 if (!SDL_SetRenderDrawColor(_renderer, r, g, b, a))
362 return SDL_RenderClear(_renderer);
364 return fill(_window, r, g, b, a);
367bool SdlWindow::fill(SDL_Window* window, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
369 auto surface = SDL_GetWindowSurface(window);
372 SDL_Rect rect = { 0, 0, surface->w, surface->h };
373 auto color = SDL_MapSurfaceRGBA(surface, r, g, b, a);
375 return SDL_FillSurfaceRect(surface, &rect, color);
378rdpMonitor SdlWindow::query(SDL_Window* window, SDL_DisplayID
id,
bool forceAsPrimary)
383 const auto& r = rect(window, forceAsPrimary);
384 const float factor = SDL_GetWindowDisplayScale(window);
385 const float dpi = std::roundf(factor * 100.0f);
387 WINPR_ASSERT(r.w > 0);
388 WINPR_ASSERT(r.h > 0);
390 const auto primary = SDL_GetPrimaryDisplay();
391 const auto orientation = SDL_GetCurrentDisplayOrientation(
id);
392 const auto rdp_orientation = sdl::utils::orientaion_to_rdp(orientation);
395 monitor.orig_screen = id;
399 monitor.height = r.h;
400 monitor.is_primary = forceAsPrimary || (
id == primary);
401 monitor.attributes.desktopScaleFactor =
static_cast<UINT32
>(dpi);
402 monitor.attributes.deviceScaleFactor = 100;
403 monitor.attributes.orientation = rdp_orientation;
404 monitor.attributes.physicalWidth = WINPR_ASSERTING_INT_CAST(uint32_t, r.w);
405 monitor.attributes.physicalHeight = WINPR_ASSERTING_INT_CAST(uint32_t, r.h);
407 const auto cat = SDL_LOG_CATEGORY_APPLICATION;
408 SDL_LogDebug(cat,
"monitor.orig_screen %" PRIu32, monitor.orig_screen);
409 SDL_LogDebug(cat,
"monitor.x %" PRId32, monitor.x);
410 SDL_LogDebug(cat,
"monitor.y %" PRId32, monitor.y);
411 SDL_LogDebug(cat,
"monitor.width %" PRId32, monitor.width);
412 SDL_LogDebug(cat,
"monitor.height %" PRId32, monitor.height);
413 SDL_LogDebug(cat,
"monitor.is_primary %" PRIu32, monitor.is_primary);
414 SDL_LogDebug(cat,
"monitor.attributes.desktopScaleFactor %" PRIu32,
415 monitor.attributes.desktopScaleFactor);
416 SDL_LogDebug(cat,
"monitor.attributes.deviceScaleFactor %" PRIu32,
417 monitor.attributes.deviceScaleFactor);
418 SDL_LogDebug(cat,
"monitor.attributes.orientation %s",
419 freerdp_desktop_rotation_flags_to_string(monitor.attributes.orientation));
420 SDL_LogDebug(cat,
"monitor.attributes.physicalWidth %" PRIu32,
421 monitor.attributes.physicalWidth);
422 SDL_LogDebug(cat,
"monitor.attributes.physicalHeight %" PRIu32,
423 monitor.attributes.physicalHeight);
427SDL_Rect SdlWindow::rect(SDL_Window* window,
bool forceAsPrimary)
435 if (!SDL_GetWindowPosition(window, &rect.x, &rect.y))
439 if (!SDL_GetWindowSizeInPixels(window, &rect.w, &rect.h))
442 const auto flags = SDL_GetWindowFlags(window);
443 const auto mask = SDL_WINDOW_FULLSCREEN;
444 const auto fs = (flags & mask) == mask;
453 const auto displayID = SDL_GetDisplayForWindow(window);
454 SDL_Rect displayBounds = {};
455 if (SDL_GetDisplayBounds(displayID, &displayBounds))
462 rect.w = displayBounds.w;
463 rect.h = displayBounds.h;
465 const float contentScale = SDL_GetDisplayContentScale(displayID);
466 if (contentScale > 1.0f)
468 const auto fw =
static_cast<float>(rect.w);
469 const auto fh =
static_cast<float>(rect.h);
470 rect.w =
static_cast<int>(std::roundf(fw * contentScale));
471 rect.h =
static_cast<int>(std::roundf(fh * contentScale));
479SdlWindow::HighDPIMode SdlWindow::isHighDPIWindowsMode(SDL_Window* window)
484 const auto id = SDL_GetDisplayForWindow(window);
488 const auto cs = SDL_GetDisplayContentScale(
id);
489 const auto ds = SDL_GetWindowDisplayScale(window);
490 const auto pd = SDL_GetWindowPixelDensity(window);
493 if ((cs == 1.0f) && (ds == 1.0f) && (pd == 1.0f))
497 if ((cs == 1.0f) && (ds > 1.0f) && (pd > 1.0f))
504bool SdlWindow::blit(SDL_Surface* surface,
const SDL_Rect& srcRect, SDL_Rect& dstRect)
506 if (!_renderer || !surface)
510 if (!_gdiTexture || _gdiTextureW != surface->w || _gdiTextureH != surface->h)
513 SDL_DestroyTexture(_gdiTexture);
514 _gdiTexture = SDL_CreateTexture(_renderer, surface->format, SDL_TEXTUREACCESS_STREAMING,
515 surface->w, surface->h);
518 SDL_LogError(SDL_LOG_CATEGORY_RENDER,
"SDL_CreateTexture: %s", SDL_GetError());
521 _gdiTextureW = surface->w;
522 _gdiTextureH = surface->h;
526 const auto* details = SDL_GetPixelFormatDetails(surface->format);
527 const int bpp = details ? details->bytes_per_pixel : 4;
528 const auto* pixels =
static_cast<const uint8_t*
>(surface->pixels) +
529 (1ll * srcRect.y * surface->pitch) + (1ll * srcRect.x * bpp);
530 if (!SDL_UpdateTexture(_gdiTexture, &srcRect, pixels, surface->pitch))
532 SDL_LogError(SDL_LOG_CATEGORY_RENDER,
"SDL_UpdateTexture: %s", SDL_GetError());
537 if (!SDL_SetRenderTarget(_renderer, _renderTarget))
540 SDL_FRect fsrc = {
static_cast<float>(srcRect.x),
static_cast<float>(srcRect.y),
541 static_cast<float>(srcRect.w),
static_cast<float>(srcRect.h) };
542 SDL_FRect fdst = {
static_cast<float>(dstRect.x),
static_cast<float>(dstRect.y),
543 static_cast<float>(dstRect.w),
static_cast<float>(dstRect.h) };
544 if (!SDL_RenderTexture(_renderer, _gdiTexture, &fsrc, &fdst))
546 SDL_LogError(SDL_LOG_CATEGORY_RENDER,
"SDL_RenderTexture: %s", SDL_GetError());
552void SdlWindow::updateSurface()
557 ensureRenderTarget();
560 if (!SDL_SetRenderTarget(_renderer,
nullptr))
562 if (!SDL_RenderTexture(_renderer, _renderTarget,
nullptr,
nullptr))
564 if (!SDL_RenderPresent(_renderer))
568SdlWindow SdlWindow::create(SDL_DisplayID
id,
const std::string& title, Uint32 flags, Uint32 width,
571 flags |= SDL_WINDOW_HIGH_PIXEL_DENSITY;
573 SDL_Rect rect = {
static_cast<int>(SDL_WINDOWPOS_CENTERED_DISPLAY(
id)),
574 static_cast<int>(SDL_WINDOWPOS_CENTERED_DISPLAY(
id)),
static_cast<int>(width),
575 static_cast<int>(height) };
577 if ((flags & SDL_WINDOW_FULLSCREEN) != 0)
579 std::ignore = SDL_GetDisplayBounds(
id, &rect);
582 SdlWindow window{ id, title, rect, flags };
584 if ((flags & SDL_WINDOW_FULLSCREEN) != 0)
586 window.setOffsetX(rect.x);
587 window.setOffsetY(rect.y);
593static SDL_Window* createDummy(SDL_DisplayID
id)
595 const auto x = SDL_WINDOWPOS_CENTERED_DISPLAY(
id);
596 const auto y = SDL_WINDOWPOS_CENTERED_DISPLAY(
id);
600 auto props = SDL_CreateProperties();
601 std::stringstream ss;
602 ss <<
"SdlWindow::query(" <<
id <<
")";
603 SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, ss.str().c_str());
604 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, x);
605 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, y);
606 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, w);
607 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, h);
609 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIGH_PIXEL_DENSITY_BOOLEAN,
true);
610 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_FULLSCREEN_BOOLEAN,
false);
611 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_BORDERLESS_BOOLEAN,
true);
612 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIDDEN_BOOLEAN,
false);
614 auto window = SDL_CreateWindowWithProperties(props);
615 SDL_DestroyProperties(props);
623 std::ignore = SDL_GetDisplayBounds(
id, &rect);
624 std::ignore = SDL_SetWindowPosition(window, rect.x, rect.y);
625 std::ignore = SDL_SetWindowFullscreen(window,
true);
630rdpMonitor SdlWindow::query(SDL_DisplayID
id,
bool forceAsPrimary)
632 std::unique_ptr<SDL_Window, void (*)(SDL_Window*)> window(createDummy(
id), SDL_DestroyWindow);
636 std::unique_ptr<SDL_Renderer, void (*)(SDL_Renderer*)> renderer(
637 SDL_CreateRenderer(window.get(),
nullptr), SDL_DestroyRenderer);
639 if (!SDL_SyncWindow(window.get()))
643 while (SDL_PollEvent(&event))
646 return query(window.get(),
id, forceAsPrimary);
649SDL_Rect SdlWindow::rect(SDL_DisplayID
id,
bool forceAsPrimary)
651 std::unique_ptr<SDL_Window, void (*)(SDL_Window*)> window(createDummy(
id), SDL_DestroyWindow);
655 std::unique_ptr<SDL_Renderer, void (*)(SDL_Renderer*)> renderer(
656 SDL_CreateRenderer(window.get(),
nullptr), SDL_DestroyRenderer);
658 if (!SDL_SyncWindow(window.get()))
662 while (SDL_PollEvent(&event))
665 return rect(window.get(), forceAsPrimary);
668bool SdlWindow::tryFallback(
bool isFullscreen)
673 const auto wlroots_hack = SDL_getenv(
"FREERDP_WLROOTS_HACK");
674 if (wlroots_hack !=
nullptr)
676 const auto enabled = strcmp(wlroots_hack,
"0") != 0;
677 if (strcmp(wlroots_hack,
"force") == 0)
679 return enabled && isFullscreen;
682 const auto platform = SDL_GetPlatform();
683 if ((platform ==
nullptr) || (strcmp(platform,
"Linux") != 0))
686 const auto driver = SDL_GetCurrentVideoDriver();
687 if ((driver ==
nullptr) || (strcmp(driver,
"wayland") != 0))
697 auto isWlrootsCompositor = [](
const char* value) ->
bool
701 if (strstr(value,
"sway") || strstr(value,
"Sway") || strstr(value,
"Hyprland") ||
702 strstr(value,
"hyprland") || strstr(value,
"river") || strstr(value,
"wlroots"))
707 const auto xdg_session = SDL_getenv(
"XDG_SESSION_DESKTOP");
708 if (isWlrootsCompositor(xdg_session))
711 const auto xdg_desktop = SDL_getenv(
"XDG_CURRENT_DESKTOP");
712 if (isWlrootsCompositor(xdg_desktop))
SdlWindow(const std::string &title, Sint32 startupX, Sint32 startupY, Sint32 width, Sint32 height, Uint32 flags)