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 if (flags & SDL_WINDOW_TRANSPARENT)
57 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_TRANSPARENT_BOOLEAN,
true);
60 if (flags & SDL_WINDOW_HIDDEN)
61 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIDDEN_BOOLEAN,
true);
63 _window = SDL_CreateWindowWithProperties(props);
64 SDL_DestroyProperties(props);
65 SDL_SetHint(SDL_HINT_APP_NAME,
"");
66 std::ignore = SDL_SyncWindow(_window);
68 _renderer = SDL_CreateRenderer(_window,
nullptr);
70 std::ignore = resizeToScale();
72 _monitor = query(_window,
id,
true);
76 : _window(other._window), _renderer(other._renderer), _renderTarget(other._renderTarget),
77 _gdiTexture(other._gdiTexture), _gdiTextureW(other._gdiTextureW),
78 _gdiTextureH(other._gdiTextureH), _initialW(other._initialW), _initialH(other._initialH),
79 _displayID(other._displayID), _offset_x(other._offset_x), _offset_y(other._offset_y),
80 _monitor(other._monitor)
82 other._window =
nullptr;
83 other._renderer =
nullptr;
84 other._renderTarget =
nullptr;
85 other._gdiTexture =
nullptr;
88SdlWindow::~SdlWindow()
91 SDL_DestroyTexture(_gdiTexture);
93 SDL_DestroyTexture(_renderTarget);
95 SDL_DestroyRenderer(_renderer);
97 SDL_DestroyWindow(_window);
100SDL_WindowID SdlWindow::id()
const
104 return SDL_GetWindowID(_window);
107SDL_DisplayID SdlWindow::displayIndex()
const
111 return SDL_GetDisplayForWindow(_window);
114SDL_Rect SdlWindow::rect()
const
116 return rect(_window);
119SDL_Rect SdlWindow::bounds()
const
124 if (!SDL_GetWindowPosition(_window, &rect.x, &rect.y))
126 if (!SDL_GetWindowSize(_window, &rect.w, &rect.h))
132SDL_Window* SdlWindow::window()
const
137SDL_Renderer* SdlWindow::renderer()
const
142Sint32 SdlWindow::offsetX()
const
147void SdlWindow::setOffsetX(Sint32 x)
152void SdlWindow::setOffsetY(Sint32 y)
157Sint32 SdlWindow::offsetY()
const
162rdpMonitor SdlWindow::monitor(
bool isPrimary)
const
178float SdlWindow::scale()
const
180 return SDL_GetWindowDisplayScale(_window);
183SDL_DisplayOrientation SdlWindow::orientation()
const
185 const auto did = displayIndex();
186 return SDL_GetCurrentDisplayOrientation(did);
189bool SdlWindow::grabKeyboard(
bool enable)
193 SDL_SetWindowKeyboardGrab(_window, enable);
197bool SdlWindow::grabMouse(
bool enable)
201 SDL_SetWindowMouseGrab(_window, enable);
205void SdlWindow::setBordered(
bool bordered)
208 SDL_SetWindowBordered(_window, bordered);
214void SdlWindow::raise()
216 SDL_RaiseWindow(_window);
217 std::ignore = SDL_SyncWindow(_window);
220void SdlWindow::resizeable(
bool use)
222 SDL_SetWindowResizable(_window, use);
223 std::ignore = SDL_SyncWindow(_window);
226void SdlWindow::fullscreen(
bool enter,
bool forceOriginalDisplay)
228 if (enter && forceOriginalDisplay && _displayID != 0)
235 std::ignore = SDL_GetDisplayBounds(_displayID, &rect);
236 std::ignore = SDL_SetWindowPosition(_window, rect.x, rect.y);
238 std::ignore = SDL_SetWindowFullscreen(_window, enter);
239 std::ignore = SDL_SyncWindow(_window);
242void SdlWindow::minimize()
244 SDL_MinimizeWindow(_window);
245 std::ignore = SDL_SyncWindow(_window);
248bool SdlWindow::resizeToScale()
250 if (!_window || _initialW <= 0 || _initialH <= 0)
252 if ((SDL_GetWindowFlags(_window) & SDL_WINDOW_FULLSCREEN) != 0)
255 float pd = SDL_GetWindowPixelDensity(_window);
259 const int targetW =
static_cast<int>(std::ceil(
static_cast<float>(_initialW) / pd));
260 const int targetH =
static_cast<int>(std::ceil(
static_cast<float>(_initialH) / pd));
264 if (!SDL_GetWindowSize(_window, &curW, &curH))
267 if (curW == targetW && curH == targetH)
270 return resize({ targetW, targetH });
273bool SdlWindow::resize(
const SDL_Point& size)
275 return SDL_SetWindowSize(_window, size.x, size.y);
278void SdlWindow::ensureRenderTarget()
285 SDL_GetWindowSizeInPixels(_window, &w, &h);
286 if (w <= 0 || h <= 0)
294 if (!SDL_GetTextureSize(_renderTarget, &tw, &th))
296 if (
static_cast<int>(tw) == w &&
static_cast<int>(th) == h)
298 SDL_DestroyTexture(_renderTarget);
302 SDL_CreateTexture(_renderer, SDL_PIXELFORMAT_BGRA32, SDL_TEXTUREACCESS_TARGET, w, h);
305 SDL_LogError(SDL_LOG_CATEGORY_RENDER,
"SDL_CreateTexture (render target): %s",
310 std::ignore = SDL_SetTextureBlendMode(_renderTarget, SDL_BLENDMODE_NONE);
313 if (SDL_SetRenderTarget(_renderer, _renderTarget))
315 std::ignore = SDL_SetRenderDrawColor(_renderer, 0x00, 0x00, 0x00, 0x00);
316 std::ignore = SDL_RenderClear(_renderer);
320bool SdlWindow::drawRect(SDL_Surface* surface, SDL_Point offset,
const SDL_Rect& srcRect)
322 WINPR_ASSERT(surface);
323 SDL_Rect dstRect = { offset.x + srcRect.x, offset.y + srcRect.y, srcRect.w, srcRect.h };
324 return blit(surface, srcRect, dstRect);
327bool SdlWindow::drawRects(SDL_Surface* surface, SDL_Point offset,
328 const std::vector<SDL_Rect>& rects)
332 return drawRect(surface, offset, { 0, 0, surface->w, surface->h });
334 for (
auto& srcRect : rects)
336 if (!drawRect(surface, offset, srcRect))
342bool SdlWindow::drawScaledRect(SDL_Surface* surface,
const SDL_FPoint& scale,
343 const SDL_Rect& srcRect)
345 SDL_Rect dstRect = {};
348 const auto modx = std::modf(
static_cast<float>(srcRect.x) * scale.x, &ix);
349 const auto mody = std::modf(
static_cast<float>(srcRect.y) * scale.y, &iy);
350 auto sw = std::ceil(
static_cast<float>(srcRect.w) * scale.x) + std::ceil(modx);
351 auto sh = std::ceil(
static_cast<float>(srcRect.h) * scale.y) + std::ceil(mody);
352 dstRect.x =
static_cast<Sint32
>(ix);
353 dstRect.w =
static_cast<Sint32
>(sw);
354 dstRect.y =
static_cast<Sint32
>(iy);
355 dstRect.h =
static_cast<Sint32
>(sh);
356 return blit(surface, srcRect, dstRect);
359bool SdlWindow::drawScaledRects(SDL_Surface* surface,
const SDL_FPoint& scale,
360 const std::vector<SDL_Rect>& rects)
364 return drawScaledRect(surface, scale, { 0, 0, surface->w, surface->h });
366 for (
const auto& srcRect : rects)
368 if (!drawScaledRect(surface, scale, srcRect))
374bool SdlWindow::fill(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
378 ensureRenderTarget();
379 if (!SDL_SetRenderTarget(_renderer, _renderTarget))
381 if (!SDL_SetRenderDrawColor(_renderer, r, g, b, a))
383 return SDL_RenderClear(_renderer);
385 return fill(_window, r, g, b, a);
388bool SdlWindow::fill(SDL_Window* window, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
390 auto surface = SDL_GetWindowSurface(window);
393 SDL_Rect rect = { 0, 0, surface->w, surface->h };
394 auto color = SDL_MapSurfaceRGBA(surface, r, g, b, a);
396 return SDL_FillSurfaceRect(surface, &rect, color);
399static rdpMonitor makeMonitor(SDL_DisplayID
id,
const SDL_Rect& r,
float factor,
402 const float dpi = std::roundf(factor * 100.0f);
404 WINPR_ASSERT(r.w > 0);
405 WINPR_ASSERT(r.h > 0);
407 const auto primary = SDL_GetPrimaryDisplay();
408 const auto orientation = SDL_GetCurrentDisplayOrientation(
id);
409 const auto rdp_orientation = sdl::utils::orientaion_to_rdp(orientation);
412 monitor.orig_screen = id;
416 monitor.height = r.h;
417 monitor.is_primary = forceAsPrimary || (
id == primary);
418 monitor.attributes.desktopScaleFactor =
static_cast<UINT32
>(dpi);
419 monitor.attributes.deviceScaleFactor = 100;
420 monitor.attributes.orientation = rdp_orientation;
424 monitor.attributes.physicalWidth = 0;
425 monitor.attributes.physicalHeight = 0;
427 const auto cat = SDL_LOG_CATEGORY_APPLICATION;
428 SDL_LogDebug(cat,
"monitor.orig_screen %" PRIu32, monitor.orig_screen);
429 SDL_LogDebug(cat,
"monitor.x %" PRId32, monitor.x);
430 SDL_LogDebug(cat,
"monitor.y %" PRId32, monitor.y);
431 SDL_LogDebug(cat,
"monitor.width %" PRId32, monitor.width);
432 SDL_LogDebug(cat,
"monitor.height %" PRId32, monitor.height);
433 SDL_LogDebug(cat,
"monitor.is_primary %" PRIu32, monitor.is_primary);
434 SDL_LogDebug(cat,
"monitor.attributes.desktopScaleFactor %" PRIu32,
435 monitor.attributes.desktopScaleFactor);
436 SDL_LogDebug(cat,
"monitor.attributes.deviceScaleFactor %" PRIu32,
437 monitor.attributes.deviceScaleFactor);
438 SDL_LogDebug(cat,
"monitor.attributes.orientation %s",
439 freerdp_desktop_rotation_flags_to_string(monitor.attributes.orientation));
440 SDL_LogDebug(cat,
"monitor.attributes.physicalWidth %" PRIu32,
441 monitor.attributes.physicalWidth);
442 SDL_LogDebug(cat,
"monitor.attributes.physicalHeight %" PRIu32,
443 monitor.attributes.physicalHeight);
447rdpMonitor SdlWindow::query(SDL_Window* window, SDL_DisplayID
id,
bool forceAsPrimary)
451 return makeMonitor(
id, rect(window, forceAsPrimary), SDL_GetWindowDisplayScale(window),
456static rdpMonitor queryDisplay(SDL_DisplayID
id,
bool forceAsPrimary)
459 const SDL_DisplayMode* mode = SDL_GetDesktopDisplayMode(
id);
460 if (!mode || !SDL_GetDisplayBounds(
id, &r))
462 SDL_LogWarn(SDL_LOG_CATEGORY_VIDEO,
"display %" PRIu32
": %s",
id, SDL_GetError());
470 r.w =
static_cast<int>(std::roundf(
static_cast<float>(r.w) * mode->pixel_density));
471 r.h =
static_cast<int>(std::roundf(
static_cast<float>(r.h) * mode->pixel_density));
472 return makeMonitor(
id, r, mode->pixel_density * SDL_GetDisplayContentScale(
id), forceAsPrimary);
475SDL_Rect SdlWindow::rect(SDL_Window* window,
bool forceAsPrimary)
483 if (!SDL_GetWindowPosition(window, &rect.x, &rect.y))
487 if (!SDL_GetWindowSizeInPixels(window, &rect.w, &rect.h))
490 const auto flags = SDL_GetWindowFlags(window);
491 const auto mask = SDL_WINDOW_FULLSCREEN;
492 const auto fs = (flags & mask) == mask;
501 const auto displayID = SDL_GetDisplayForWindow(window);
502 SDL_Rect displayBounds = {};
503 if (SDL_GetDisplayBounds(displayID, &displayBounds))
514 rect.x = displayBounds.x;
515 rect.y = displayBounds.y;
517 rect.w = displayBounds.w;
518 rect.h = displayBounds.h;
520 const float contentScale = SDL_GetDisplayContentScale(displayID);
521 if (contentScale > 1.0f)
523 const auto fw =
static_cast<float>(rect.w);
524 const auto fh =
static_cast<float>(rect.h);
525 rect.w =
static_cast<int>(std::roundf(fw * contentScale));
526 rect.h =
static_cast<int>(std::roundf(fh * contentScale));
534SdlWindow::HighDPIMode SdlWindow::isHighDPIWindowsMode(SDL_Window* window)
539 const auto id = SDL_GetDisplayForWindow(window);
543 const auto cs = SDL_GetDisplayContentScale(
id);
544 const auto ds = SDL_GetWindowDisplayScale(window);
545 const auto pd = SDL_GetWindowPixelDensity(window);
548 if ((cs == 1.0f) && (ds == 1.0f) && (pd == 1.0f))
552 if ((cs == 1.0f) && (ds > 1.0f) && (pd > 1.0f))
560bool SdlWindow::ensureGdiTexture(SDL_Surface* surface)
562 if (_gdiTexture && (_gdiTextureW == surface->w) && (_gdiTextureH == surface->h))
565 SDL_DestroyTexture(_gdiTexture);
566 _gdiTexture = SDL_CreateTexture(_renderer, surface->format, SDL_TEXTUREACCESS_STREAMING,
567 surface->w, surface->h);
570 SDL_LogError(SDL_LOG_CATEGORY_RENDER,
"SDL_CreateTexture: %s", SDL_GetError());
573 std::ignore = SDL_SetTextureBlendMode(_gdiTexture, SDL_BLENDMODE_NONE);
574 _gdiTextureW = surface->w;
575 _gdiTextureH = surface->h;
579bool SdlWindow::blit(SDL_Surface* surface,
const SDL_Rect& srcRect, SDL_Rect& dstRect)
581 if (!_renderer || !surface)
584 ensureRenderTarget();
586 if (!ensureGdiTexture(surface))
590 const auto* details = SDL_GetPixelFormatDetails(surface->format);
591 const int bpp = details ? details->bytes_per_pixel : 4;
592 const auto* pixels =
static_cast<const uint8_t*
>(surface->pixels) +
593 (1ll * srcRect.y * surface->pitch) + (1ll * srcRect.x * bpp);
594 if (!SDL_UpdateTexture(_gdiTexture, &srcRect, pixels, surface->pitch))
596 SDL_LogError(SDL_LOG_CATEGORY_RENDER,
"SDL_UpdateTexture: %s", SDL_GetError());
601 if (!SDL_SetRenderTarget(_renderer, _renderTarget))
604 SDL_FRect fsrc = {
static_cast<float>(srcRect.x),
static_cast<float>(srcRect.y),
605 static_cast<float>(srcRect.w),
static_cast<float>(srcRect.h) };
606 SDL_FRect fdst = {
static_cast<float>(dstRect.x),
static_cast<float>(dstRect.y),
607 static_cast<float>(dstRect.w),
static_cast<float>(dstRect.h) };
608 if (!SDL_RenderTexture(_renderer, _gdiTexture, &fsrc, &fdst))
610 SDL_LogError(SDL_LOG_CATEGORY_RENDER,
"SDL_RenderTexture: %s", SDL_GetError());
616void SdlWindow::updateSurface()
621 ensureRenderTarget();
624 if (!SDL_SetRenderTarget(_renderer,
nullptr))
626 if (!SDL_RenderTexture(_renderer, _renderTarget,
nullptr,
nullptr))
628 if (!SDL_RenderPresent(_renderer))
632bool SdlWindow::paintResizeFrame(SDL_Surface* surface, SDL_Point off,
bool contentChanged,
633 const SDL_Rect& inset,
bool fillRevealed,
bool dashedBorder)
635 if (!_renderer || !surface)
637 ensureRenderTarget();
639 const int prevW = _gdiTextureW;
640 const int prevH = _gdiTextureH;
641 if (!ensureGdiTexture(surface))
644 const bool recreated = (_gdiTextureW != prevW) || (_gdiTextureH != prevH);
645 if ((contentChanged || recreated) &&
646 !SDL_UpdateTexture(_gdiTexture,
nullptr, surface->pixels, surface->pitch))
649 if (!SDL_SetRenderTarget(_renderer, _renderTarget))
654 SDL_GetWindowSizeInPixels(_window, &ww, &wh);
656 const SDL_FRect frame = {
static_cast<float>(inset.x),
static_cast<float>(inset.y),
657 static_cast<float>(ww - inset.x - inset.w),
658 static_cast<float>(wh - inset.y - inset.h) };
661 constexpr Uint8 kFillAlpha = 0x80;
662 std::ignore = SDL_SetRenderDrawBlendMode(_renderer, SDL_BLENDMODE_NONE);
663 std::ignore = SDL_SetRenderDrawColor(_renderer, 0, 0, 0, 0);
664 std::ignore = SDL_RenderClear(_renderer);
667 std::ignore = SDL_SetRenderDrawColor(_renderer, 0x2B, 0x2B, 0x2B, kFillAlpha);
668 std::ignore = SDL_RenderFillRect(_renderer, &frame);
670 SDL_FRect fdst = {
static_cast<float>(off.x),
static_cast<float>(off.y),
671 static_cast<float>(surface->w),
static_cast<float>(surface->h) };
673 const SDL_Rect clip = { inset.x, inset.y, ww - inset.x - inset.w, wh - inset.y - inset.h };
674 std::ignore = SDL_SetRenderClipRect(_renderer, &clip);
675 std::ignore = SDL_RenderTexture(_renderer, _gdiTexture,
nullptr, &fdst);
676 std::ignore = SDL_SetRenderClipRect(_renderer,
nullptr);
681 std::ignore = SDL_SetRenderDrawColor(_renderer, 0xC8, 0xC8, 0xC8, 0xFF);
682 const float dash = 8.0F;
683 const float gap = 5.0F;
684 const float fx2 = frame.x + frame.w;
685 const float fy2 = frame.y + frame.h;
686 const float lo = frame.y + 0.5F;
687 const float by = fy2 - 0.5F;
688 const float lx = frame.x + 0.5F;
689 const float rx = fx2 - 0.5F;
690 const float step = dash + gap;
691 const auto steps = [step](
float len)
692 {
return (len <= 0.0F) ? 0 :
static_cast<int>(std::ceil(len / step)); };
693 for (
int i = 0; i < steps(fx2 - frame.x); i++)
695 const float x = frame.x + (
static_cast<float>(i) * step);
696 const float x2 = (x + dash < fx2) ? (x + dash) : rx;
697 std::ignore = SDL_RenderLine(_renderer, x, lo, x2, lo);
698 std::ignore = SDL_RenderLine(_renderer, x, by, x2, by);
700 for (
int i = 0; i < steps(fy2 - frame.y); i++)
702 const float y = frame.y + (
static_cast<float>(i) * step);
703 const float y2 = (y + dash < fy2) ? (y + dash) : by;
704 std::ignore = SDL_RenderLine(_renderer, lx, y, lx, y2);
705 std::ignore = SDL_RenderLine(_renderer, rx, y, rx, y2);
709 if (!SDL_SetRenderTarget(_renderer,
nullptr))
711 std::ignore = SDL_RenderTexture(_renderer, _renderTarget,
nullptr,
nullptr);
712 std::ignore = SDL_RenderPresent(_renderer);
716SdlWindow SdlWindow::create(SDL_DisplayID
id,
const std::string& title, Uint32 flags, Uint32 width,
719 flags |= SDL_WINDOW_HIGH_PIXEL_DENSITY;
721 SDL_Rect rect = {
static_cast<int>(SDL_WINDOWPOS_CENTERED_DISPLAY(
id)),
722 static_cast<int>(SDL_WINDOWPOS_CENTERED_DISPLAY(
id)),
static_cast<int>(width),
723 static_cast<int>(height) };
725 if ((flags & SDL_WINDOW_FULLSCREEN) != 0)
727 std::ignore = SDL_GetDisplayBounds(
id, &rect);
730 SdlWindow window{ id, title, rect, flags };
732 if ((flags & SDL_WINDOW_FULLSCREEN) != 0)
734 window.setOffsetX(rect.x);
735 window.setOffsetY(rect.y);
741SdlWindow SdlWindow::create(SDL_DisplayID
id,
const std::string& title, Uint32 flags,
742 const SDL_Rect& rect)
744 return SdlWindow{ id, title, rect, flags };
749 : _initialW(rect.w), _initialH(rect.h)
751 auto props = SDL_CreateProperties();
752 SDL_SetPointerProperty(props, SDL_PROP_WINDOW_CREATE_PARENT_POINTER, parent);
753 SDL_SetBooleanProperty(props,
754 tooltip ? SDL_PROP_WINDOW_CREATE_TOOLTIP_BOOLEAN
755 : SDL_PROP_WINDOW_CREATE_MENU_BOOLEAN,
757 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_FOCUSABLE_BOOLEAN,
false);
758#if defined(SDL_PROP_WINDOW_CREATE_CONSTRAIN_POPUP_BOOLEAN)
760 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_CONSTRAIN_POPUP_BOOLEAN,
false);
762 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_BORDERLESS_BOOLEAN,
true);
765 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_TRANSPARENT_BOOLEAN,
true);
766 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, rect.x);
767 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, rect.y);
768 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, rect.w);
769 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, rect.h);
771 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIDDEN_BOOLEAN,
true);
773 _window = SDL_CreateWindowWithProperties(props);
774 SDL_DestroyProperties(props);
777 std::ignore = SDL_SyncWindow(_window);
778 _renderer = SDL_CreateRenderer(_window,
nullptr);
779 _displayID = SDL_GetDisplayForWindow(_window);
783SdlWindow SdlWindow::createPopup(SDL_Window* parent,
const SDL_Rect& rect,
bool transparent,
786 return SdlWindow{ parent, rect, transparent, tooltip };
789static SDL_Window* createDummy(SDL_DisplayID
id)
791 const auto x = SDL_WINDOWPOS_CENTERED_DISPLAY(
id);
792 const auto y = SDL_WINDOWPOS_CENTERED_DISPLAY(
id);
796 auto props = SDL_CreateProperties();
797 std::stringstream ss;
798 ss <<
"SdlWindow::query(" <<
id <<
")";
799 SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, ss.str().c_str());
800 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, x);
801 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, y);
802 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, w);
803 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, h);
805 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIGH_PIXEL_DENSITY_BOOLEAN,
true);
806 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_FULLSCREEN_BOOLEAN,
false);
807 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_BORDERLESS_BOOLEAN,
true);
808 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIDDEN_BOOLEAN,
false);
810 auto window = SDL_CreateWindowWithProperties(props);
811 SDL_DestroyProperties(props);
819 std::ignore = SDL_GetDisplayBounds(
id, &rect);
820 std::ignore = SDL_SetWindowPosition(window, rect.x, rect.y);
821 std::ignore = SDL_SetWindowFullscreen(window,
true);
826rdpMonitor SdlWindow::query(SDL_DisplayID
id,
bool forceAsPrimary)
828 if (sdl::utils::isWaylandDriver() || sdl::utils::isX11Driver())
829 return queryDisplay(
id, forceAsPrimary);
831 std::unique_ptr<SDL_Window, void (*)(SDL_Window*)> window(createDummy(
id), SDL_DestroyWindow);
835 std::unique_ptr<SDL_Renderer, void (*)(SDL_Renderer*)> renderer(
836 SDL_CreateRenderer(window.get(),
nullptr), SDL_DestroyRenderer);
838 if (!SDL_SyncWindow(window.get()))
842 while (SDL_PollEvent(&event))
845 return query(window.get(),
id, forceAsPrimary);
848SDL_Rect SdlWindow::rect(SDL_DisplayID
id,
bool forceAsPrimary)
850 std::unique_ptr<SDL_Window, void (*)(SDL_Window*)> window(createDummy(
id), SDL_DestroyWindow);
854 std::unique_ptr<SDL_Renderer, void (*)(SDL_Renderer*)> renderer(
855 SDL_CreateRenderer(window.get(),
nullptr), SDL_DestroyRenderer);
857 if (!SDL_SyncWindow(window.get()))
861 while (SDL_PollEvent(&event))
864 return rect(window.get(), forceAsPrimary);
867bool SdlWindow::tryFallback(
bool isFullscreen)
872 const auto wlroots_hack = SDL_getenv(
"FREERDP_WLROOTS_HACK");
873 if (wlroots_hack !=
nullptr)
875 const auto enabled = strcmp(wlroots_hack,
"0") != 0;
876 if (strcmp(wlroots_hack,
"force") == 0)
878 return enabled && isFullscreen;
881 const auto platform = SDL_GetPlatform();
882 if ((platform ==
nullptr) || (strcmp(platform,
"Linux") != 0))
885 const auto driver = SDL_GetCurrentVideoDriver();
886 if ((driver ==
nullptr) || (strcmp(driver,
"wayland") != 0))
896 auto isWlrootsCompositor = [](
const char* value) ->
bool
900 if (strstr(value,
"sway") || strstr(value,
"Sway") || strstr(value,
"Hyprland") ||
901 strstr(value,
"hyprland") || strstr(value,
"river") || strstr(value,
"wlroots"))
906 const auto xdg_session = SDL_getenv(
"XDG_SESSION_DESKTOP");
907 if (isWlrootsCompositor(xdg_session))
910 const auto xdg_desktop = SDL_getenv(
"XDG_CURRENT_DESKTOP");
911 if (isWlrootsCompositor(xdg_desktop))
SdlWindow(const std::string &title, Sint32 startupX, Sint32 startupY, Sint32 width, Sint32 height, Uint32 flags)