24#include "sdl_window.hpp"
25#include "sdl_utils.hpp"
26#include "sdl_overlay.hpp"
28#include <freerdp/utils/string.h>
31 [[maybe_unused]] Uint32 flags)
32 : _initialW(rect.w), _initialH(rect.h), _displayID(id)
34 float pd = SDL_GetDisplayContentScale(
id);
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));
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);
48 if (flags & SDL_WINDOW_HIGH_PIXEL_DENSITY)
49 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIGH_PIXEL_DENSITY_BOOLEAN,
true);
51 if (flags & SDL_WINDOW_FULLSCREEN)
52 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_FULLSCREEN_BOOLEAN,
true);
54 if (flags & SDL_WINDOW_BORDERLESS)
55 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_BORDERLESS_BOOLEAN,
true);
57 if (flags & SDL_WINDOW_TRANSPARENT)
58 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_TRANSPARENT_BOOLEAN,
true);
61 if (flags & SDL_WINDOW_HIDDEN)
62 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIDDEN_BOOLEAN,
true);
64 _window = SDL_CreateWindowWithProperties(props);
65 SDL_DestroyProperties(props);
66 SDL_SetHint(SDL_HINT_APP_NAME,
"");
67 std::ignore = SDL_SyncWindow(_window);
69 _renderer = SDL_CreateRenderer(_window,
nullptr);
71 std::ignore = resizeToScale();
73 _monitor = query(_window,
id,
true);
77 : _window(other._window), _videoOverlays(std::move(other._videoOverlays)),
78 _renderer(other._renderer), _renderTarget(other._renderTarget),
79 _gdiTexture(other._gdiTexture), _overlayPainter(std::move(other._overlayPainter)),
80 _gdiTextureW(other._gdiTextureW), _gdiTextureH(other._gdiTextureH),
81 _initialW(other._initialW), _initialH(other._initialH), _displayID(other._displayID),
82 _layoutPosition(other._layoutPosition), _offset_x(other._offset_x),
83 _offset_y(other._offset_y), _monitor(other._monitor)
85 other._videoOverlays.clear();
86 other._window =
nullptr;
87 other._renderer =
nullptr;
88 other._renderTarget =
nullptr;
89 other._gdiTexture =
nullptr;
92SdlWindow::~SdlWindow()
94 for (
auto& o : _videoOverlays)
95 SDL_DestroyTexture(o.second.texture);
97 _overlayPainter.reset();
99 SDL_DestroyTexture(_gdiTexture);
101 SDL_DestroyTexture(_renderTarget);
103 SDL_DestroyRenderer(_renderer);
105 SDL_DestroyWindow(_window);
108SDL_WindowID SdlWindow::id()
const
112 return SDL_GetWindowID(_window);
115SDL_DisplayID SdlWindow::displayIndex()
const
119 return SDL_GetDisplayForWindow(_window);
122SDL_Rect SdlWindow::rect()
const
124 return rect(_window);
127SDL_Rect SdlWindow::bounds()
const
132 if (!SDL_GetWindowPosition(_window, &rect.x, &rect.y))
134 if (!SDL_GetWindowSize(_window, &rect.w, &rect.h))
140SDL_Window* SdlWindow::window()
const
145SDL_Renderer* SdlWindow::renderer()
const
150Sint32 SdlWindow::offsetX()
const
155void SdlWindow::setOffsetX(Sint32 x)
160void SdlWindow::setOffsetY(Sint32 y)
165Sint32 SdlWindow::offsetY()
const
170SDL_Point SdlWindow::layoutPosition()
const
172 return _layoutPosition;
175void SdlWindow::setLayoutPosition(
const SDL_Point& pos)
177 _layoutPosition = pos;
180rdpMonitor SdlWindow::monitor(
bool isPrimary)
const
196float SdlWindow::scale()
const
198 return SDL_GetWindowDisplayScale(_window);
201SDL_DisplayOrientation SdlWindow::orientation()
const
203 const auto did = displayIndex();
204 return SDL_GetCurrentDisplayOrientation(did);
207bool SdlWindow::grabKeyboard(
bool enable)
211 SDL_SetWindowKeyboardGrab(_window, enable);
215bool SdlWindow::grabMouse(
bool enable)
219 SDL_SetWindowMouseGrab(_window, enable);
223void SdlWindow::setBordered(
bool bordered)
226 SDL_SetWindowBordered(_window, bordered);
232void SdlWindow::raise()
234 SDL_RaiseWindow(_window);
235 std::ignore = SDL_SyncWindow(_window);
238void SdlWindow::resizeable(
bool use)
240 SDL_SetWindowResizable(_window, use);
241 std::ignore = SDL_SyncWindow(_window);
244void SdlWindow::fullscreen(
bool enter,
bool forceOriginalDisplay)
246 if (enter && forceOriginalDisplay && _displayID != 0)
253 std::ignore = SDL_GetDisplayBounds(_displayID, &rect);
254 std::ignore = SDL_SetWindowPosition(_window, rect.x, rect.y);
256 std::ignore = SDL_SetWindowFullscreen(_window, enter);
257 std::ignore = SDL_SyncWindow(_window);
260void SdlWindow::minimize()
262 SDL_MinimizeWindow(_window);
263 std::ignore = SDL_SyncWindow(_window);
266bool SdlWindow::resizeToScale()
268 if (!_window || _initialW <= 0 || _initialH <= 0)
270 if ((SDL_GetWindowFlags(_window) & SDL_WINDOW_FULLSCREEN) != 0)
273 float pd = SDL_GetWindowPixelDensity(_window);
281 if (isHighDPIWindowsMode(_window) == MODE_MACOS)
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));
289 if (!SDL_GetWindowSize(_window, &curW, &curH))
292 if (curW == targetW && curH == targetH)
295 return resize({ targetW, targetH });
298bool SdlWindow::resize(
const SDL_Point& size)
300 return SDL_SetWindowSize(_window, size.x, size.y);
303void SdlWindow::ensureRenderTarget()
310 SDL_GetWindowSizeInPixels(_window, &w, &h);
311 if (w <= 0 || h <= 0)
319 if (!SDL_GetTextureSize(_renderTarget, &tw, &th))
321 if (
static_cast<int>(tw) == w &&
static_cast<int>(th) == h)
323 SDL_DestroyTexture(_renderTarget);
327 SDL_CreateTexture(_renderer, SDL_PIXELFORMAT_BGRA32, SDL_TEXTUREACCESS_TARGET, w, h);
330 SDL_LogError(SDL_LOG_CATEGORY_RENDER,
"SDL_CreateTexture (render target): %s",
335 std::ignore = SDL_SetTextureBlendMode(_renderTarget, SDL_BLENDMODE_NONE);
338 if (SDL_SetRenderTarget(_renderer, _renderTarget))
340 std::ignore = SDL_SetRenderDrawColor(_renderer, 0x00, 0x00, 0x00, 0x00);
341 std::ignore = SDL_RenderClear(_renderer);
345bool SdlWindow::drawRect(SDL_Surface* surface, SDL_Point offset,
const SDL_Rect& srcRect)
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);
352bool SdlWindow::drawRects(SDL_Surface* surface, SDL_Point offset,
353 const std::vector<SDL_Rect>& rects)
357 return drawRect(surface, offset, { 0, 0, surface->w, surface->h });
359 for (
auto& srcRect : rects)
361 if (!drawRect(surface, offset, srcRect))
367static SDL_Rect scaledDstRect(
const SDL_FPoint& scale,
const SDL_Rect& srcRect)
369 SDL_Rect dstRect = {};
372 const auto modx = std::modf(
static_cast<float>(srcRect.x) * scale.x, &ix);
373 const auto mody = std::modf(
static_cast<float>(srcRect.y) * scale.y, &iy);
374 auto sw = std::ceil(
static_cast<float>(srcRect.w) * scale.x) + std::ceil(modx);
375 auto sh = std::ceil(
static_cast<float>(srcRect.h) * scale.y) + std::ceil(mody);
376 dstRect.x =
static_cast<Sint32
>(ix);
377 dstRect.w =
static_cast<Sint32
>(sw);
378 dstRect.y =
static_cast<Sint32
>(iy);
379 dstRect.h =
static_cast<Sint32
>(sh);
383bool SdlWindow::drawScaledRect(SDL_Surface* surface,
const SDL_FPoint& scale,
384 const SDL_Rect& srcRect)
386 auto dstRect = scaledDstRect(scale, srcRect);
387 return blit(surface, srcRect, dstRect);
390bool SdlWindow::drawScaledRects(SDL_Surface* surface,
const SDL_FPoint& scale,
391 const std::vector<SDL_Rect>& rects)
395 return drawScaledRect(surface, scale, { 0, 0, surface->w, surface->h });
402 for (
const auto& srcRect : rects)
404 if (!upload(surface, srcRect))
407 for (
const auto& srcRect : rects)
409 if (!render(srcRect, scaledDstRect(scale, srcRect)))
415bool SdlWindow::fill(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
419 ensureRenderTarget();
420 if (!SDL_SetRenderTarget(_renderer, _renderTarget))
422 if (!SDL_SetRenderDrawColor(_renderer, r, g, b, a))
424 return SDL_RenderClear(_renderer);
426 return fill(_window, r, g, b, a);
429bool SdlWindow::fill(SDL_Window* window, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
431 auto surface = SDL_GetWindowSurface(window);
434 SDL_Rect rect = { 0, 0, surface->w, surface->h };
435 auto color = SDL_MapSurfaceRGBA(surface, r, g, b, a);
437 return SDL_FillSurfaceRect(surface, &rect, color);
440static rdpMonitor makeMonitor(SDL_DisplayID
id,
const SDL_Rect& r,
float factor,
443 const float dpi = std::roundf(factor * 100.0f);
447 if ((r.w <= 0) || (r.h <= 0))
449 SDL_LogWarn(SDL_LOG_CATEGORY_VIDEO,
"display %" PRIu32
": invalid size %dx%d",
id, r.w,
454 const auto primary = SDL_GetPrimaryDisplay();
455 const auto orientation = SDL_GetCurrentDisplayOrientation(
id);
456 const auto rdp_orientation = sdl::utils::orientaion_to_rdp(orientation);
459 monitor.orig_screen = id;
463 monitor.height = r.h;
464 monitor.is_primary = forceAsPrimary || (
id == primary);
465 monitor.attributes.desktopScaleFactor =
static_cast<UINT32
>(dpi);
466 monitor.attributes.deviceScaleFactor = 100;
467 monitor.attributes.orientation = rdp_orientation;
471 monitor.attributes.physicalWidth = 0;
472 monitor.attributes.physicalHeight = 0;
474 const auto cat = SDL_LOG_CATEGORY_APPLICATION;
475 SDL_LogDebug(cat,
"monitor.orig_screen %" PRIu32, monitor.orig_screen);
476 SDL_LogDebug(cat,
"monitor.x %" PRId32, monitor.x);
477 SDL_LogDebug(cat,
"monitor.y %" PRId32, monitor.y);
478 SDL_LogDebug(cat,
"monitor.width %" PRId32, monitor.width);
479 SDL_LogDebug(cat,
"monitor.height %" PRId32, monitor.height);
480 SDL_LogDebug(cat,
"monitor.is_primary %" PRIu32, monitor.is_primary);
481 SDL_LogDebug(cat,
"monitor.attributes.desktopScaleFactor %" PRIu32,
482 monitor.attributes.desktopScaleFactor);
483 SDL_LogDebug(cat,
"monitor.attributes.deviceScaleFactor %" PRIu32,
484 monitor.attributes.deviceScaleFactor);
485 SDL_LogDebug(cat,
"monitor.attributes.orientation %s",
486 freerdp_desktop_rotation_flags_to_string(monitor.attributes.orientation));
487 SDL_LogDebug(cat,
"monitor.attributes.physicalWidth %" PRIu32,
488 monitor.attributes.physicalWidth);
489 SDL_LogDebug(cat,
"monitor.attributes.physicalHeight %" PRIu32,
490 monitor.attributes.physicalHeight);
494rdpMonitor SdlWindow::query(SDL_Window* window, SDL_DisplayID
id,
bool forceAsPrimary)
498 return makeMonitor(
id, rect(window, forceAsPrimary), SDL_GetWindowDisplayScale(window),
503static rdpMonitor queryDisplay(SDL_DisplayID
id,
bool forceAsPrimary)
506 const SDL_DisplayMode* mode = SDL_GetDesktopDisplayMode(
id);
507 if (!mode || !SDL_GetDisplayBounds(
id, &r))
509 SDL_LogWarn(SDL_LOG_CATEGORY_VIDEO,
"display %" PRIu32
": %s",
id, SDL_GetError());
517 r.w =
static_cast<int>(std::roundf(
static_cast<float>(r.w) * mode->pixel_density));
518 r.h =
static_cast<int>(std::roundf(
static_cast<float>(r.h) * mode->pixel_density));
519 return makeMonitor(
id, r, mode->pixel_density * SDL_GetDisplayContentScale(
id), forceAsPrimary);
522SDL_Rect SdlWindow::rect(SDL_Window* window,
bool forceAsPrimary)
530 if (!SDL_GetWindowPosition(window, &rect.x, &rect.y))
534 if (!SDL_GetWindowSizeInPixels(window, &rect.w, &rect.h))
537 const bool placeholder = (rect.w < 200) || (rect.h < 200);
538 if (placeholder && tryFallback(
true))
546 const auto displayID = SDL_GetDisplayForWindow(window);
547 SDL_Rect displayBounds = {};
548 if (SDL_GetDisplayBounds(displayID, &displayBounds))
559 rect.x = displayBounds.x;
560 rect.y = displayBounds.y;
562 rect.w = displayBounds.w;
563 rect.h = displayBounds.h;
566 const auto mode = SDL_GetCurrentDisplayMode(displayID);
567 if (mode && (mode->pixel_density > 1.0f))
568 factor = mode->pixel_density;
571 const float contentScale = SDL_GetDisplayContentScale(displayID);
572 if (contentScale > 1.0f)
573 factor = contentScale;
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));
588SdlWindow::HighDPIMode SdlWindow::isHighDPIWindowsMode(SDL_Window* window)
593 const auto id = SDL_GetDisplayForWindow(window);
597 const auto cs = SDL_GetDisplayContentScale(
id);
598 const auto ds = SDL_GetWindowDisplayScale(window);
599 const auto pd = SDL_GetWindowPixelDensity(window);
602 if ((cs == 1.0f) && (ds == 1.0f) && (pd == 1.0f))
606 if ((cs == 1.0f) && (ds > 1.0f) && (pd > 1.0f))
614bool SdlWindow::ensureGdiTexture(SDL_Surface* surface)
616 if (_gdiTexture && (_gdiTextureW == surface->w) && (_gdiTextureH == surface->h))
619 SDL_DestroyTexture(_gdiTexture);
620 _gdiTexture = SDL_CreateTexture(_renderer, surface->format, SDL_TEXTUREACCESS_STREAMING,
621 surface->w, surface->h);
624 SDL_LogError(SDL_LOG_CATEGORY_RENDER,
"SDL_CreateTexture: %s", SDL_GetError());
627 std::ignore = SDL_SetTextureBlendMode(_gdiTexture, SDL_BLENDMODE_NONE);
628 _gdiTextureW = surface->w;
629 _gdiTextureH = surface->h;
633bool SdlWindow::blit(SDL_Surface* surface,
const SDL_Rect& srcRect, SDL_Rect& dstRect)
635 return upload(surface, srcRect) && render(srcRect, dstRect);
638bool SdlWindow::upload(SDL_Surface* surface,
const SDL_Rect& srcRect)
640 if (!_renderer || !surface)
643 ensureRenderTarget();
645 if (!ensureGdiTexture(surface))
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))
655 SDL_LogError(SDL_LOG_CATEGORY_RENDER,
"SDL_UpdateTexture: %s", SDL_GetError());
661bool SdlWindow::render(
const SDL_Rect& srcRect,
const SDL_Rect& dstRect)
663 if (!_renderer || !_gdiTexture)
667 if (!SDL_SetRenderTarget(_renderer, _renderTarget))
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))
676 SDL_LogError(SDL_LOG_CATEGORY_RENDER,
"SDL_RenderTexture: %s", SDL_GetError());
687 ensureRenderTarget();
690 if (!SDL_SetRenderTarget(_renderer,
nullptr))
692 if (!SDL_RenderTexture(_renderer, _renderTarget,
nullptr,
nullptr))
696 if (overlay && overlay->visible())
697 drawOverlay(*overlay);
698 if (!SDL_RenderPresent(_renderer))
702void SdlWindow::drawVideoOverlays()
704 for (
const auto& entry : _videoOverlays)
706 const auto& o = entry.second;
707 if (!o.visible || !o.texture || (o.dst.w <= 0) || (o.dst.h <= 0))
711 std::ignore = SDL_RenderTexture(_renderer, o.texture,
nullptr, &o.dst);
715 const float sx =
static_cast<float>(o.w) / o.dst.w;
716 const float sy =
static_cast<float>(o.h) / o.dst.h;
717 for (
const auto& c : o.clip)
720 if (!SDL_GetRectIntersectionFloat(&c, &o.dst, &part))
722 const SDL_FRect src{ (part.x - o.dst.x) * sx, (part.y - o.dst.y) * sy, part.w * sx,
724 std::ignore = SDL_RenderTexture(_renderer, o.texture, &src, &part);
730 const SDL_FRect& dst,
const std::vector<SDL_FRect>& clip,
733 if (!_renderer || (w <= 0) || (h <= 0))
735 auto& o = _videoOverlays[id];
736 if (!o.texture || (o.w != w) || (o.h != h))
739 SDL_DestroyTexture(o.texture);
741 SDL_CreateTexture(_renderer, SDL_PIXELFORMAT_BGRA32, SDL_TEXTUREACCESS_STREAMING, w, h);
744 SDL_LogError(SDL_LOG_CATEGORY_RENDER,
"SDL_CreateTexture: %s", SDL_GetError());
745 _videoOverlays.erase(
id);
749 std::ignore = SDL_SetTextureBlendMode(o.texture, SDL_BLENDMODE_NONE);
753 if (pixels && !SDL_UpdateTexture(o.texture,
nullptr, pixels, pitch))
755 SDL_LogError(SDL_LOG_CATEGORY_RENDER,
"SDL_UpdateTexture: %s", SDL_GetError());
766 for (
auto it = _videoOverlays.begin(); it != _videoOverlays.end();)
772 SDL_DestroyTexture(it->second.texture);
773 it = _videoOverlays.erase(it);
778const char* SdlWindow::rendererName()
const
780 return _renderer ? SDL_GetRendererName(_renderer) : nullptr;
783void SdlWindow::drawOverlay(
const SdlOverlay& overlay)
785 if (!_overlayPainter)
786 _overlayPainter = std::make_unique<SdlOverlayPainter>(_renderer);
790 if (!SDL_GetCurrentRenderOutputSize(_renderer, &w, &h))
792 _overlayPainter->draw(overlay, w, h, SDL_GetWindowDisplayScale(_window));
795bool SdlWindow::paintResizeFrame(SDL_Surface* surface, SDL_Point off,
bool contentChanged,
796 const SDL_Rect& inset,
bool fillRevealed,
bool dashedBorder)
798 if (!_renderer || !surface)
800 ensureRenderTarget();
802 const int prevW = _gdiTextureW;
803 const int prevH = _gdiTextureH;
804 if (!ensureGdiTexture(surface))
807 const bool recreated = (_gdiTextureW != prevW) || (_gdiTextureH != prevH);
808 if ((contentChanged || recreated) &&
809 !SDL_UpdateTexture(_gdiTexture,
nullptr, surface->pixels, surface->pitch))
812 if (!SDL_SetRenderTarget(_renderer, _renderTarget))
817 SDL_GetWindowSizeInPixels(_window, &ww, &wh);
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) };
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);
830 std::ignore = SDL_SetRenderDrawColor(_renderer, 0x2B, 0x2B, 0x2B, kFillAlpha);
831 std::ignore = SDL_RenderFillRect(_renderer, &frame);
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) };
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);
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++)
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);
863 for (
int i = 0; i < steps(fy2 - frame.y); i++)
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);
872 if (!SDL_SetRenderTarget(_renderer,
nullptr))
874 std::ignore = SDL_RenderTexture(_renderer, _renderTarget,
nullptr,
nullptr);
876 std::ignore = SDL_RenderPresent(_renderer);
880SdlWindow SdlWindow::create(SDL_DisplayID
id,
const std::string& title, Uint32 flags, Uint32 width,
883 flags |= SDL_WINDOW_HIGH_PIXEL_DENSITY;
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) };
889 if ((flags & SDL_WINDOW_FULLSCREEN) != 0)
891 std::ignore = SDL_GetDisplayBounds(
id, &rect);
894 SdlWindow window{ id, title, rect, flags };
896 if ((flags & SDL_WINDOW_FULLSCREEN) != 0)
898 window.setOffsetX(rect.x);
899 window.setOffsetY(rect.y);
905SdlWindow SdlWindow::create(SDL_DisplayID
id,
const std::string& title, Uint32 flags,
906 const SDL_Rect& rect)
908 return SdlWindow{ id, title, rect, flags };
913 : _initialW(rect.w), _initialH(rect.h)
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,
921 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_FOCUSABLE_BOOLEAN,
false);
922#if defined(SDL_PROP_WINDOW_CREATE_CONSTRAIN_POPUP_BOOLEAN)
924 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_CONSTRAIN_POPUP_BOOLEAN,
false);
926 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_BORDERLESS_BOOLEAN,
true);
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);
935 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIDDEN_BOOLEAN,
true);
937 auto isHighDpi =
true;
940 auto flags = SDL_GetWindowFlags(parent);
941 if ((flags & SDL_WINDOW_HIGH_PIXEL_DENSITY) != 0)
945 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIGH_PIXEL_DENSITY_BOOLEAN, isHighDpi);
947 _window = SDL_CreateWindowWithProperties(props);
948 SDL_DestroyProperties(props);
951 std::ignore = SDL_SyncWindow(_window);
952 _renderer = SDL_CreateRenderer(_window,
nullptr);
953 _displayID = SDL_GetDisplayForWindow(_window);
957SdlWindow SdlWindow::createPopup(SDL_Window* parent,
const SDL_Rect& rect,
bool transparent,
960 return SdlWindow{ parent, rect, transparent, tooltip };
963static SDL_Window* createDummy(SDL_DisplayID
id)
965 const auto x = SDL_WINDOWPOS_CENTERED_DISPLAY(
id);
966 const auto y = SDL_WINDOWPOS_CENTERED_DISPLAY(
id);
970 auto props = SDL_CreateProperties();
971 std::stringstream ss;
972 ss <<
"SdlWindow::query(" <<
id <<
")";
973 SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, ss.str().c_str());
974 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, x);
975 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, y);
976 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, w);
977 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, h);
979 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIGH_PIXEL_DENSITY_BOOLEAN,
true);
980 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_FULLSCREEN_BOOLEAN,
false);
981 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_BORDERLESS_BOOLEAN,
true);
982 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIDDEN_BOOLEAN,
false);
984 auto window = SDL_CreateWindowWithProperties(props);
985 SDL_DestroyProperties(props);
993 std::ignore = SDL_GetDisplayBounds(
id, &rect);
994 std::ignore = SDL_SetWindowPosition(window, rect.x, rect.y);
995 std::ignore = SDL_SetWindowFullscreen(window,
true);
1000rdpMonitor SdlWindow::query(SDL_DisplayID
id,
bool forceAsPrimary)
1002 if (sdl::utils::isWaylandDriver() || sdl::utils::isX11Driver())
1003 return queryDisplay(
id, forceAsPrimary);
1005 std::unique_ptr<SDL_Window, void (*)(SDL_Window*)> window(createDummy(
id), SDL_DestroyWindow);
1009 std::unique_ptr<SDL_Renderer, void (*)(SDL_Renderer*)> renderer(
1010 SDL_CreateRenderer(window.get(),
nullptr), SDL_DestroyRenderer);
1012 if (!SDL_SyncWindow(window.get()))
1016 while (SDL_PollEvent(&event))
1019 return query(window.get(),
id, forceAsPrimary);
1022SDL_Rect SdlWindow::rect(SDL_DisplayID
id,
bool forceAsPrimary)
1024 std::unique_ptr<SDL_Window, void (*)(SDL_Window*)> window(createDummy(
id), SDL_DestroyWindow);
1028 std::unique_ptr<SDL_Renderer, void (*)(SDL_Renderer*)> renderer(
1029 SDL_CreateRenderer(window.get(),
nullptr), SDL_DestroyRenderer);
1031 if (!SDL_SyncWindow(window.get()))
1035 while (SDL_PollEvent(&event))
1038 return rect(window.get(), forceAsPrimary);
1041bool SdlWindow::tryFallback(
bool isFullscreen)
1046 const auto wlroots_hack = SDL_getenv(
"FREERDP_WLROOTS_HACK");
1047 if (wlroots_hack !=
nullptr)
1049 const auto enabled = strcmp(wlroots_hack,
"0") != 0;
1050 if (strcmp(wlroots_hack,
"force") == 0)
1051 isFullscreen =
true;
1052 return enabled && isFullscreen;
1055 const auto platform = SDL_GetPlatform();
1056 if ((platform ==
nullptr) || (strcmp(platform,
"Linux") != 0))
1059 const auto driver = SDL_GetCurrentVideoDriver();
1060 if ((driver ==
nullptr) || (strcmp(driver,
"wayland") != 0))
1070 auto isWlrootsCompositor = [](
const char* value) ->
bool
1074 if (strstr(value,
"sway") || strstr(value,
"Sway") || strstr(value,
"Hyprland") ||
1075 strstr(value,
"hyprland") || strstr(value,
"river") || strstr(value,
"wlroots"))
1080 const auto xdg_session = SDL_getenv(
"XDG_SESSION_DESKTOP");
1081 if (isWlrootsCompositor(xdg_session))
1082 return isFullscreen;
1084 const auto xdg_desktop = SDL_getenv(
"XDG_CURRENT_DESKTOP");
1085 if (isWlrootsCompositor(xdg_desktop))
1086 return isFullscreen;
void pruneVideoOverlays(const std::function< bool(Uint64)> &keep)
drops the overlays keep returns false for
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),...
SdlWindow(const std::string &title, Sint32 startupX, Sint32 startupY, Sint32 width, Sint32 height, Uint32 flags)