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 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);
39 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_RESIZABLE_BOOLEAN,
true);
41 if (flags & SDL_WINDOW_HIGH_PIXEL_DENSITY)
42 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIGH_PIXEL_DENSITY_BOOLEAN,
true);
44 if (flags & SDL_WINDOW_FULLSCREEN)
45 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_FULLSCREEN_BOOLEAN,
true);
47 if (flags & SDL_WINDOW_BORDERLESS)
48 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_BORDERLESS_BOOLEAN,
true);
50 _window = SDL_CreateWindowWithProperties(props);
51 SDL_DestroyProperties(props);
52 SDL_SetHint(SDL_HINT_APP_NAME,
"");
53 std::ignore = SDL_SyncWindow(_window);
55 _renderer = SDL_CreateRenderer(_window,
nullptr);
57 std::ignore = resizeToScale();
59 _monitor = query(_window,
id,
true);
63 : _window(other._window), _renderer(other._renderer), _renderTarget(other._renderTarget),
64 _gdiTexture(other._gdiTexture), _gdiTextureW(other._gdiTextureW),
65 _gdiTextureH(other._gdiTextureH), _initialW(other._initialW), _initialH(other._initialH),
66 _displayID(other._displayID), _offset_x(other._offset_x), _offset_y(other._offset_y),
67 _monitor(other._monitor)
69 other._window =
nullptr;
70 other._renderer =
nullptr;
71 other._renderTarget =
nullptr;
72 other._gdiTexture =
nullptr;
75SdlWindow::~SdlWindow()
78 SDL_DestroyTexture(_gdiTexture);
80 SDL_DestroyTexture(_renderTarget);
82 SDL_DestroyRenderer(_renderer);
84 SDL_DestroyWindow(_window);
87SDL_WindowID SdlWindow::id()
const
91 return SDL_GetWindowID(_window);
94SDL_DisplayID SdlWindow::displayIndex()
const
98 return SDL_GetDisplayForWindow(_window);
101SDL_Rect SdlWindow::rect()
const
103 return rect(_window);
106SDL_Rect SdlWindow::bounds()
const
111 if (!SDL_GetWindowPosition(_window, &rect.x, &rect.y))
113 if (!SDL_GetWindowSize(_window, &rect.w, &rect.h))
119SDL_Window* SdlWindow::window()
const
124SDL_Renderer* SdlWindow::renderer()
const
129Sint32 SdlWindow::offsetX()
const
134void SdlWindow::setOffsetX(Sint32 x)
139void SdlWindow::setOffsetY(Sint32 y)
144Sint32 SdlWindow::offsetY()
const
149rdpMonitor SdlWindow::monitor(
bool isPrimary)
const
165float SdlWindow::scale()
const
167 return SDL_GetWindowDisplayScale(_window);
170SDL_DisplayOrientation SdlWindow::orientation()
const
172 const auto did = displayIndex();
173 return SDL_GetCurrentDisplayOrientation(did);
176bool SdlWindow::grabKeyboard(
bool enable)
180 SDL_SetWindowKeyboardGrab(_window, enable);
184bool SdlWindow::grabMouse(
bool enable)
188 SDL_SetWindowMouseGrab(_window, enable);
192void SdlWindow::setBordered(
bool bordered)
195 SDL_SetWindowBordered(_window, bordered);
196 std::ignore = SDL_SyncWindow(_window);
199void SdlWindow::raise()
201 SDL_RaiseWindow(_window);
202 std::ignore = SDL_SyncWindow(_window);
205void SdlWindow::resizeable(
bool use)
207 SDL_SetWindowResizable(_window, use);
208 std::ignore = SDL_SyncWindow(_window);
211void SdlWindow::fullscreen(
bool enter,
bool forceOriginalDisplay)
213 if (enter && forceOriginalDisplay && _displayID != 0)
220 std::ignore = SDL_GetDisplayBounds(_displayID, &rect);
221 std::ignore = SDL_SetWindowPosition(_window, rect.x, rect.y);
223 std::ignore = SDL_SetWindowFullscreen(_window, enter);
224 std::ignore = SDL_SyncWindow(_window);
227void SdlWindow::minimize()
229 SDL_MinimizeWindow(_window);
230 std::ignore = SDL_SyncWindow(_window);
233bool SdlWindow::resizeToScale()
235 if (!_window || _initialW <= 0 || _initialH <= 0)
237 if ((SDL_GetWindowFlags(_window) & SDL_WINDOW_FULLSCREEN) != 0)
240 float pd = SDL_GetWindowPixelDensity(_window);
244 const int targetW =
static_cast<int>(std::ceil(
static_cast<float>(_initialW) / pd));
245 const int targetH =
static_cast<int>(std::ceil(
static_cast<float>(_initialH) / pd));
249 if (!SDL_GetWindowSize(_window, &curW, &curH))
252 if (curW == targetW && curH == targetH)
255 return resize({ targetW, targetH });
258bool SdlWindow::resize(
const SDL_Point& size)
260 return SDL_SetWindowSize(_window, size.x, size.y);
263void SdlWindow::ensureRenderTarget()
270 SDL_GetWindowSizeInPixels(_window, &w, &h);
271 if (w <= 0 || h <= 0)
279 if (!SDL_GetTextureSize(_renderTarget, &tw, &th))
281 if (
static_cast<int>(tw) == w &&
static_cast<int>(th) == h)
283 SDL_DestroyTexture(_renderTarget);
287 SDL_CreateTexture(_renderer, SDL_PIXELFORMAT_BGRA32, SDL_TEXTUREACCESS_TARGET, w, h);
289 SDL_LogError(SDL_LOG_CATEGORY_RENDER,
"SDL_CreateTexture (render target): %s",
293bool SdlWindow::drawRect(SDL_Surface* surface, SDL_Point offset,
const SDL_Rect& srcRect)
295 WINPR_ASSERT(surface);
296 SDL_Rect dstRect = { offset.x + srcRect.x, offset.y + srcRect.y, srcRect.w, srcRect.h };
297 return blit(surface, srcRect, dstRect);
300bool SdlWindow::drawRects(SDL_Surface* surface, SDL_Point offset,
301 const std::vector<SDL_Rect>& rects)
305 return drawRect(surface, offset, { 0, 0, surface->w, surface->h });
307 for (
auto& srcRect : rects)
309 if (!drawRect(surface, offset, srcRect))
315bool SdlWindow::drawScaledRect(SDL_Surface* surface,
const SDL_FPoint& scale,
316 const SDL_Rect& srcRect)
318 SDL_Rect dstRect = srcRect;
319 dstRect.x =
static_cast<Sint32
>(
static_cast<float>(dstRect.x) * scale.x);
320 dstRect.w =
static_cast<Sint32
>(
static_cast<float>(dstRect.w) * scale.x);
321 dstRect.y =
static_cast<Sint32
>(
static_cast<float>(dstRect.y) * scale.y);
322 dstRect.h =
static_cast<Sint32
>(
static_cast<float>(dstRect.h) * scale.y);
323 return blit(surface, srcRect, dstRect);
326bool SdlWindow::drawScaledRects(SDL_Surface* surface,
const SDL_FPoint& scale,
327 const std::vector<SDL_Rect>& rects)
331 return drawScaledRect(surface, scale, { 0, 0, surface->w, surface->h });
333 for (
const auto& srcRect : rects)
335 if (!drawScaledRect(surface, scale, srcRect))
341bool SdlWindow::fill(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
345 ensureRenderTarget();
346 if (!SDL_SetRenderTarget(_renderer, _renderTarget))
348 if (!SDL_SetRenderDrawColor(_renderer, r, g, b, a))
350 return SDL_RenderClear(_renderer);
352 return fill(_window, r, g, b, a);
355bool SdlWindow::fill(SDL_Window* window, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
357 auto surface = SDL_GetWindowSurface(window);
360 SDL_Rect rect = { 0, 0, surface->w, surface->h };
361 auto color = SDL_MapSurfaceRGBA(surface, r, g, b, a);
363 return SDL_FillSurfaceRect(surface, &rect, color);
366rdpMonitor SdlWindow::query(SDL_Window* window, SDL_DisplayID
id,
bool forceAsPrimary)
371 const auto& r = rect(window, forceAsPrimary);
372 const float factor = SDL_GetWindowDisplayScale(window);
373 const float dpi = std::roundf(factor * 100.0f);
375 WINPR_ASSERT(r.w > 0);
376 WINPR_ASSERT(r.h > 0);
378 const auto primary = SDL_GetPrimaryDisplay();
379 const auto orientation = SDL_GetCurrentDisplayOrientation(
id);
380 const auto rdp_orientation = sdl::utils::orientaion_to_rdp(orientation);
383 monitor.orig_screen = id;
387 monitor.height = r.h;
388 monitor.is_primary = forceAsPrimary || (
id == primary);
389 monitor.attributes.desktopScaleFactor =
static_cast<UINT32
>(dpi);
390 monitor.attributes.deviceScaleFactor = 100;
391 monitor.attributes.orientation = rdp_orientation;
392 monitor.attributes.physicalWidth = WINPR_ASSERTING_INT_CAST(uint32_t, r.w);
393 monitor.attributes.physicalHeight = WINPR_ASSERTING_INT_CAST(uint32_t, r.h);
395 const auto cat = SDL_LOG_CATEGORY_APPLICATION;
396 SDL_LogDebug(cat,
"monitor.orig_screen %" PRIu32, monitor.orig_screen);
397 SDL_LogDebug(cat,
"monitor.x %" PRId32, monitor.x);
398 SDL_LogDebug(cat,
"monitor.y %" PRId32, monitor.y);
399 SDL_LogDebug(cat,
"monitor.width %" PRId32, monitor.width);
400 SDL_LogDebug(cat,
"monitor.height %" PRId32, monitor.height);
401 SDL_LogDebug(cat,
"monitor.is_primary %" PRIu32, monitor.is_primary);
402 SDL_LogDebug(cat,
"monitor.attributes.desktopScaleFactor %" PRIu32,
403 monitor.attributes.desktopScaleFactor);
404 SDL_LogDebug(cat,
"monitor.attributes.deviceScaleFactor %" PRIu32,
405 monitor.attributes.deviceScaleFactor);
406 SDL_LogDebug(cat,
"monitor.attributes.orientation %s",
407 freerdp_desktop_rotation_flags_to_string(monitor.attributes.orientation));
408 SDL_LogDebug(cat,
"monitor.attributes.physicalWidth %" PRIu32,
409 monitor.attributes.physicalWidth);
410 SDL_LogDebug(cat,
"monitor.attributes.physicalHeight %" PRIu32,
411 monitor.attributes.physicalHeight);
415SDL_Rect SdlWindow::rect(SDL_Window* window,
bool forceAsPrimary)
423 if (!SDL_GetWindowPosition(window, &rect.x, &rect.y))
427 if (!SDL_GetWindowSizeInPixels(window, &rect.w, &rect.h))
430 const auto flags = SDL_GetWindowFlags(window);
431 const auto mask = SDL_WINDOW_FULLSCREEN;
432 const auto fs = (flags & mask) == mask;
441 const auto displayID = SDL_GetDisplayForWindow(window);
442 SDL_Rect displayBounds = {};
443 if (SDL_GetDisplayBounds(displayID, &displayBounds))
450 rect.w = displayBounds.w;
451 rect.h = displayBounds.h;
453 const float contentScale = SDL_GetDisplayContentScale(displayID);
454 if (contentScale > 1.0f)
456 const auto fw =
static_cast<float>(rect.w);
457 const auto fh =
static_cast<float>(rect.h);
458 rect.w =
static_cast<int>(std::roundf(fw * contentScale));
459 rect.h =
static_cast<int>(std::roundf(fh * contentScale));
467SdlWindow::HighDPIMode SdlWindow::isHighDPIWindowsMode(SDL_Window* window)
472 const auto id = SDL_GetDisplayForWindow(window);
476 const auto cs = SDL_GetDisplayContentScale(
id);
477 const auto ds = SDL_GetWindowDisplayScale(window);
478 const auto pd = SDL_GetWindowPixelDensity(window);
481 if ((cs == 1.0f) && (ds == 1.0f) && (pd == 1.0f))
485 if ((cs == 1.0f) && (ds > 1.0f) && (pd > 1.0f))
492bool SdlWindow::blit(SDL_Surface* surface,
const SDL_Rect& srcRect, SDL_Rect& dstRect)
494 if (!_renderer || !surface)
498 if (!_gdiTexture || _gdiTextureW != surface->w || _gdiTextureH != surface->h)
501 SDL_DestroyTexture(_gdiTexture);
502 _gdiTexture = SDL_CreateTexture(_renderer, surface->format, SDL_TEXTUREACCESS_STREAMING,
503 surface->w, surface->h);
506 SDL_LogError(SDL_LOG_CATEGORY_RENDER,
"SDL_CreateTexture: %s", SDL_GetError());
509 _gdiTextureW = surface->w;
510 _gdiTextureH = surface->h;
514 const auto* details = SDL_GetPixelFormatDetails(surface->format);
515 const int bpp = details ? details->bytes_per_pixel : 4;
516 const auto* pixels =
static_cast<const uint8_t*
>(surface->pixels) +
517 (1ll * srcRect.y * surface->pitch) + (1ll * srcRect.x * bpp);
518 if (!SDL_UpdateTexture(_gdiTexture, &srcRect, pixels, surface->pitch))
520 SDL_LogError(SDL_LOG_CATEGORY_RENDER,
"SDL_UpdateTexture: %s", SDL_GetError());
525 if (!SDL_SetRenderTarget(_renderer, _renderTarget))
528 SDL_FRect fsrc = {
static_cast<float>(srcRect.x),
static_cast<float>(srcRect.y),
529 static_cast<float>(srcRect.w),
static_cast<float>(srcRect.h) };
530 SDL_FRect fdst = {
static_cast<float>(dstRect.x),
static_cast<float>(dstRect.y),
531 static_cast<float>(dstRect.w),
static_cast<float>(dstRect.h) };
532 if (!SDL_RenderTexture(_renderer, _gdiTexture, &fsrc, &fdst))
534 SDL_LogError(SDL_LOG_CATEGORY_RENDER,
"SDL_RenderTexture: %s", SDL_GetError());
540void SdlWindow::updateSurface()
545 ensureRenderTarget();
548 if (!SDL_SetRenderTarget(_renderer,
nullptr))
550 if (!SDL_RenderTexture(_renderer, _renderTarget,
nullptr,
nullptr))
552 if (!SDL_RenderPresent(_renderer))
556SdlWindow SdlWindow::create(SDL_DisplayID
id,
const std::string& title, Uint32 flags, Uint32 width,
559 flags |= SDL_WINDOW_HIGH_PIXEL_DENSITY;
561 SDL_Rect rect = {
static_cast<int>(SDL_WINDOWPOS_CENTERED_DISPLAY(
id)),
562 static_cast<int>(SDL_WINDOWPOS_CENTERED_DISPLAY(
id)),
static_cast<int>(width),
563 static_cast<int>(height) };
565 if ((flags & SDL_WINDOW_FULLSCREEN) != 0)
567 std::ignore = SDL_GetDisplayBounds(
id, &rect);
570 SdlWindow window{ id, title, rect, flags };
572 if ((flags & SDL_WINDOW_FULLSCREEN) != 0)
574 window.setOffsetX(rect.x);
575 window.setOffsetY(rect.y);
581static SDL_Window* createDummy(SDL_DisplayID
id)
583 const auto x = SDL_WINDOWPOS_CENTERED_DISPLAY(
id);
584 const auto y = SDL_WINDOWPOS_CENTERED_DISPLAY(
id);
588 auto props = SDL_CreateProperties();
589 std::stringstream ss;
590 ss <<
"SdlWindow::query(" <<
id <<
")";
591 SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, ss.str().c_str());
592 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, x);
593 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, y);
594 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, w);
595 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, h);
597 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIGH_PIXEL_DENSITY_BOOLEAN,
true);
598 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_FULLSCREEN_BOOLEAN,
false);
599 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_BORDERLESS_BOOLEAN,
true);
600 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIDDEN_BOOLEAN,
false);
602 auto window = SDL_CreateWindowWithProperties(props);
603 SDL_DestroyProperties(props);
611 std::ignore = SDL_GetDisplayBounds(
id, &rect);
612 std::ignore = SDL_SetWindowPosition(window, rect.x, rect.y);
613 std::ignore = SDL_SetWindowFullscreen(window,
true);
618rdpMonitor SdlWindow::query(SDL_DisplayID
id,
bool forceAsPrimary)
620 std::unique_ptr<SDL_Window, void (*)(SDL_Window*)> window(createDummy(
id), SDL_DestroyWindow);
624 std::unique_ptr<SDL_Renderer, void (*)(SDL_Renderer*)> renderer(
625 SDL_CreateRenderer(window.get(),
nullptr), SDL_DestroyRenderer);
627 if (!SDL_SyncWindow(window.get()))
631 while (SDL_PollEvent(&event))
634 return query(window.get(),
id, forceAsPrimary);
637SDL_Rect SdlWindow::rect(SDL_DisplayID
id,
bool forceAsPrimary)
639 std::unique_ptr<SDL_Window, void (*)(SDL_Window*)> window(createDummy(
id), SDL_DestroyWindow);
643 std::unique_ptr<SDL_Renderer, void (*)(SDL_Renderer*)> renderer(
644 SDL_CreateRenderer(window.get(),
nullptr), SDL_DestroyRenderer);
646 if (!SDL_SyncWindow(window.get()))
650 while (SDL_PollEvent(&event))
653 return rect(window.get(), forceAsPrimary);
656bool SdlWindow::tryFallback(
bool isFullscreen)
661 const auto wlroots_hack = SDL_getenv(
"FREERDP_WLROOTS_HACK");
662 if (wlroots_hack !=
nullptr)
664 const auto enabled = strcmp(wlroots_hack,
"0") != 0;
665 if (strcmp(wlroots_hack,
"force") == 0)
667 return enabled && isFullscreen;
670 const auto platform = SDL_GetPlatform();
671 if ((platform ==
nullptr) || (strcmp(platform,
"Linux") != 0))
674 const auto driver = SDL_GetCurrentVideoDriver();
675 if ((driver ==
nullptr) || (strcmp(driver,
"wayland") != 0))
685 auto isWlrootsCompositor = [](
const char* value) ->
bool
689 if (strstr(value,
"sway") || strstr(value,
"Sway") || strstr(value,
"Hyprland") ||
690 strstr(value,
"hyprland") || strstr(value,
"river") || strstr(value,
"wlroots"))
695 const auto xdg_session = SDL_getenv(
"XDG_SESSION_DESKTOP");
696 if (isWlrootsCompositor(xdg_session))
699 const auto xdg_desktop = SDL_getenv(
"XDG_CURRENT_DESKTOP");
700 if (isWlrootsCompositor(xdg_desktop))
SdlWindow(const std::string &title, Sint32 startupX, Sint32 startupY, Sint32 width, Sint32 height, Uint32 flags)