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 _offset_x(other._offset_x), _offset_y(other._offset_y), _monitor(other._monitor)
84 other._videoOverlays.clear();
85 other._window =
nullptr;
86 other._renderer =
nullptr;
87 other._renderTarget =
nullptr;
88 other._gdiTexture =
nullptr;
91SdlWindow::~SdlWindow()
93 for (
auto& o : _videoOverlays)
94 SDL_DestroyTexture(o.second.texture);
96 _overlayPainter.reset();
98 SDL_DestroyTexture(_gdiTexture);
100 SDL_DestroyTexture(_renderTarget);
102 SDL_DestroyRenderer(_renderer);
104 SDL_DestroyWindow(_window);
107SDL_WindowID SdlWindow::id()
const
111 return SDL_GetWindowID(_window);
114SDL_DisplayID SdlWindow::displayIndex()
const
118 return SDL_GetDisplayForWindow(_window);
121SDL_Rect SdlWindow::rect()
const
123 return rect(_window);
126SDL_Rect SdlWindow::bounds()
const
131 if (!SDL_GetWindowPosition(_window, &rect.x, &rect.y))
133 if (!SDL_GetWindowSize(_window, &rect.w, &rect.h))
139SDL_Window* SdlWindow::window()
const
144SDL_Renderer* SdlWindow::renderer()
const
149Sint32 SdlWindow::offsetX()
const
154void SdlWindow::setOffsetX(Sint32 x)
159void SdlWindow::setOffsetY(Sint32 y)
164Sint32 SdlWindow::offsetY()
const
169rdpMonitor SdlWindow::monitor(
bool isPrimary)
const
185float SdlWindow::scale()
const
187 return SDL_GetWindowDisplayScale(_window);
190SDL_DisplayOrientation SdlWindow::orientation()
const
192 const auto did = displayIndex();
193 return SDL_GetCurrentDisplayOrientation(did);
196bool SdlWindow::grabKeyboard(
bool enable)
200 SDL_SetWindowKeyboardGrab(_window, enable);
204bool SdlWindow::grabMouse(
bool enable)
208 SDL_SetWindowMouseGrab(_window, enable);
212void SdlWindow::setBordered(
bool bordered)
215 SDL_SetWindowBordered(_window, bordered);
221void SdlWindow::raise()
223 SDL_RaiseWindow(_window);
224 std::ignore = SDL_SyncWindow(_window);
227void SdlWindow::resizeable(
bool use)
229 SDL_SetWindowResizable(_window, use);
230 std::ignore = SDL_SyncWindow(_window);
233void SdlWindow::fullscreen(
bool enter,
bool forceOriginalDisplay)
235 if (enter && forceOriginalDisplay && _displayID != 0)
242 std::ignore = SDL_GetDisplayBounds(_displayID, &rect);
243 std::ignore = SDL_SetWindowPosition(_window, rect.x, rect.y);
245 std::ignore = SDL_SetWindowFullscreen(_window, enter);
246 std::ignore = SDL_SyncWindow(_window);
249void SdlWindow::minimize()
251 SDL_MinimizeWindow(_window);
252 std::ignore = SDL_SyncWindow(_window);
255bool SdlWindow::resizeToScale()
257 if (!_window || _initialW <= 0 || _initialH <= 0)
259 if ((SDL_GetWindowFlags(_window) & SDL_WINDOW_FULLSCREEN) != 0)
262 float pd = SDL_GetWindowPixelDensity(_window);
266 const int targetW =
static_cast<int>(std::ceil(
static_cast<float>(_initialW) / pd));
267 const int targetH =
static_cast<int>(std::ceil(
static_cast<float>(_initialH) / pd));
271 if (!SDL_GetWindowSize(_window, &curW, &curH))
274 if (curW == targetW && curH == targetH)
277 return resize({ targetW, targetH });
280bool SdlWindow::resize(
const SDL_Point& size)
282 return SDL_SetWindowSize(_window, size.x, size.y);
285void SdlWindow::ensureRenderTarget()
292 SDL_GetWindowSizeInPixels(_window, &w, &h);
293 if (w <= 0 || h <= 0)
301 if (!SDL_GetTextureSize(_renderTarget, &tw, &th))
303 if (
static_cast<int>(tw) == w &&
static_cast<int>(th) == h)
305 SDL_DestroyTexture(_renderTarget);
309 SDL_CreateTexture(_renderer, SDL_PIXELFORMAT_BGRA32, SDL_TEXTUREACCESS_TARGET, w, h);
312 SDL_LogError(SDL_LOG_CATEGORY_RENDER,
"SDL_CreateTexture (render target): %s",
317 std::ignore = SDL_SetTextureBlendMode(_renderTarget, SDL_BLENDMODE_NONE);
320 if (SDL_SetRenderTarget(_renderer, _renderTarget))
322 std::ignore = SDL_SetRenderDrawColor(_renderer, 0x00, 0x00, 0x00, 0x00);
323 std::ignore = SDL_RenderClear(_renderer);
327bool SdlWindow::drawRect(SDL_Surface* surface, SDL_Point offset,
const SDL_Rect& srcRect)
329 WINPR_ASSERT(surface);
330 SDL_Rect dstRect = { offset.x + srcRect.x, offset.y + srcRect.y, srcRect.w, srcRect.h };
331 return blit(surface, srcRect, dstRect);
334bool SdlWindow::drawRects(SDL_Surface* surface, SDL_Point offset,
335 const std::vector<SDL_Rect>& rects)
339 return drawRect(surface, offset, { 0, 0, surface->w, surface->h });
341 for (
auto& srcRect : rects)
343 if (!drawRect(surface, offset, srcRect))
349static SDL_Rect scaledDstRect(
const SDL_FPoint& scale,
const SDL_Rect& srcRect)
351 SDL_Rect dstRect = {};
354 const auto modx = std::modf(
static_cast<float>(srcRect.x) * scale.x, &ix);
355 const auto mody = std::modf(
static_cast<float>(srcRect.y) * scale.y, &iy);
356 auto sw = std::ceil(
static_cast<float>(srcRect.w) * scale.x) + std::ceil(modx);
357 auto sh = std::ceil(
static_cast<float>(srcRect.h) * scale.y) + std::ceil(mody);
358 dstRect.x =
static_cast<Sint32
>(ix);
359 dstRect.w =
static_cast<Sint32
>(sw);
360 dstRect.y =
static_cast<Sint32
>(iy);
361 dstRect.h =
static_cast<Sint32
>(sh);
365bool SdlWindow::drawScaledRect(SDL_Surface* surface,
const SDL_FPoint& scale,
366 const SDL_Rect& srcRect)
368 auto dstRect = scaledDstRect(scale, srcRect);
369 return blit(surface, srcRect, dstRect);
372bool SdlWindow::drawScaledRects(SDL_Surface* surface,
const SDL_FPoint& scale,
373 const std::vector<SDL_Rect>& rects)
377 return drawScaledRect(surface, scale, { 0, 0, surface->w, surface->h });
384 for (
const auto& srcRect : rects)
386 if (!upload(surface, srcRect))
389 for (
const auto& srcRect : rects)
391 if (!render(srcRect, scaledDstRect(scale, srcRect)))
397bool SdlWindow::fill(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
401 ensureRenderTarget();
402 if (!SDL_SetRenderTarget(_renderer, _renderTarget))
404 if (!SDL_SetRenderDrawColor(_renderer, r, g, b, a))
406 return SDL_RenderClear(_renderer);
408 return fill(_window, r, g, b, a);
411bool SdlWindow::fill(SDL_Window* window, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
413 auto surface = SDL_GetWindowSurface(window);
416 SDL_Rect rect = { 0, 0, surface->w, surface->h };
417 auto color = SDL_MapSurfaceRGBA(surface, r, g, b, a);
419 return SDL_FillSurfaceRect(surface, &rect, color);
422static rdpMonitor makeMonitor(SDL_DisplayID
id,
const SDL_Rect& r,
float factor,
425 const float dpi = std::roundf(factor * 100.0f);
429 if ((r.w <= 0) || (r.h <= 0))
431 SDL_LogWarn(SDL_LOG_CATEGORY_VIDEO,
"display %" PRIu32
": invalid size %dx%d",
id, r.w,
436 const auto primary = SDL_GetPrimaryDisplay();
437 const auto orientation = SDL_GetCurrentDisplayOrientation(
id);
438 const auto rdp_orientation = sdl::utils::orientaion_to_rdp(orientation);
441 monitor.orig_screen = id;
445 monitor.height = r.h;
446 monitor.is_primary = forceAsPrimary || (
id == primary);
447 monitor.attributes.desktopScaleFactor =
static_cast<UINT32
>(dpi);
448 monitor.attributes.deviceScaleFactor = 100;
449 monitor.attributes.orientation = rdp_orientation;
453 monitor.attributes.physicalWidth = 0;
454 monitor.attributes.physicalHeight = 0;
456 const auto cat = SDL_LOG_CATEGORY_APPLICATION;
457 SDL_LogDebug(cat,
"monitor.orig_screen %" PRIu32, monitor.orig_screen);
458 SDL_LogDebug(cat,
"monitor.x %" PRId32, monitor.x);
459 SDL_LogDebug(cat,
"monitor.y %" PRId32, monitor.y);
460 SDL_LogDebug(cat,
"monitor.width %" PRId32, monitor.width);
461 SDL_LogDebug(cat,
"monitor.height %" PRId32, monitor.height);
462 SDL_LogDebug(cat,
"monitor.is_primary %" PRIu32, monitor.is_primary);
463 SDL_LogDebug(cat,
"monitor.attributes.desktopScaleFactor %" PRIu32,
464 monitor.attributes.desktopScaleFactor);
465 SDL_LogDebug(cat,
"monitor.attributes.deviceScaleFactor %" PRIu32,
466 monitor.attributes.deviceScaleFactor);
467 SDL_LogDebug(cat,
"monitor.attributes.orientation %s",
468 freerdp_desktop_rotation_flags_to_string(monitor.attributes.orientation));
469 SDL_LogDebug(cat,
"monitor.attributes.physicalWidth %" PRIu32,
470 monitor.attributes.physicalWidth);
471 SDL_LogDebug(cat,
"monitor.attributes.physicalHeight %" PRIu32,
472 monitor.attributes.physicalHeight);
476rdpMonitor SdlWindow::query(SDL_Window* window, SDL_DisplayID
id,
bool forceAsPrimary)
480 return makeMonitor(
id, rect(window, forceAsPrimary), SDL_GetWindowDisplayScale(window),
485static rdpMonitor queryDisplay(SDL_DisplayID
id,
bool forceAsPrimary)
488 const SDL_DisplayMode* mode = SDL_GetDesktopDisplayMode(
id);
489 if (!mode || !SDL_GetDisplayBounds(
id, &r))
491 SDL_LogWarn(SDL_LOG_CATEGORY_VIDEO,
"display %" PRIu32
": %s",
id, SDL_GetError());
499 r.w =
static_cast<int>(std::roundf(
static_cast<float>(r.w) * mode->pixel_density));
500 r.h =
static_cast<int>(std::roundf(
static_cast<float>(r.h) * mode->pixel_density));
501 return makeMonitor(
id, r, mode->pixel_density * SDL_GetDisplayContentScale(
id), forceAsPrimary);
504SDL_Rect SdlWindow::rect(SDL_Window* window,
bool forceAsPrimary)
512 if (!SDL_GetWindowPosition(window, &rect.x, &rect.y))
516 if (!SDL_GetWindowSizeInPixels(window, &rect.w, &rect.h))
519 const bool placeholder = (rect.w < 200) || (rect.h < 200);
520 if (placeholder && tryFallback(
true))
528 const auto displayID = SDL_GetDisplayForWindow(window);
529 SDL_Rect displayBounds = {};
530 if (SDL_GetDisplayBounds(displayID, &displayBounds))
541 rect.x = displayBounds.x;
542 rect.y = displayBounds.y;
544 rect.w = displayBounds.w;
545 rect.h = displayBounds.h;
548 const auto mode = SDL_GetCurrentDisplayMode(displayID);
549 if (mode && (mode->pixel_density > 1.0f))
550 factor = mode->pixel_density;
553 const float contentScale = SDL_GetDisplayContentScale(displayID);
554 if (contentScale > 1.0f)
555 factor = contentScale;
559 const auto fw =
static_cast<float>(rect.w);
560 const auto fh =
static_cast<float>(rect.h);
561 rect.w =
static_cast<int>(std::roundf(fw * factor));
562 rect.h =
static_cast<int>(std::roundf(fh * factor));
570SdlWindow::HighDPIMode SdlWindow::isHighDPIWindowsMode(SDL_Window* window)
575 const auto id = SDL_GetDisplayForWindow(window);
579 const auto cs = SDL_GetDisplayContentScale(
id);
580 const auto ds = SDL_GetWindowDisplayScale(window);
581 const auto pd = SDL_GetWindowPixelDensity(window);
584 if ((cs == 1.0f) && (ds == 1.0f) && (pd == 1.0f))
588 if ((cs == 1.0f) && (ds > 1.0f) && (pd > 1.0f))
596bool SdlWindow::ensureGdiTexture(SDL_Surface* surface)
598 if (_gdiTexture && (_gdiTextureW == surface->w) && (_gdiTextureH == surface->h))
601 SDL_DestroyTexture(_gdiTexture);
602 _gdiTexture = SDL_CreateTexture(_renderer, surface->format, SDL_TEXTUREACCESS_STREAMING,
603 surface->w, surface->h);
606 SDL_LogError(SDL_LOG_CATEGORY_RENDER,
"SDL_CreateTexture: %s", SDL_GetError());
609 std::ignore = SDL_SetTextureBlendMode(_gdiTexture, SDL_BLENDMODE_NONE);
610 _gdiTextureW = surface->w;
611 _gdiTextureH = surface->h;
615bool SdlWindow::blit(SDL_Surface* surface,
const SDL_Rect& srcRect, SDL_Rect& dstRect)
617 return upload(surface, srcRect) && render(srcRect, dstRect);
620bool SdlWindow::upload(SDL_Surface* surface,
const SDL_Rect& srcRect)
622 if (!_renderer || !surface)
625 ensureRenderTarget();
627 if (!ensureGdiTexture(surface))
631 const auto* details = SDL_GetPixelFormatDetails(surface->format);
632 const int bpp = details ? details->bytes_per_pixel : 4;
633 const auto* pixels =
static_cast<const uint8_t*
>(surface->pixels) +
634 (1ll * srcRect.y * surface->pitch) + (1ll * srcRect.x * bpp);
635 if (!SDL_UpdateTexture(_gdiTexture, &srcRect, pixels, surface->pitch))
637 SDL_LogError(SDL_LOG_CATEGORY_RENDER,
"SDL_UpdateTexture: %s", SDL_GetError());
643bool SdlWindow::render(
const SDL_Rect& srcRect,
const SDL_Rect& dstRect)
645 if (!_renderer || !_gdiTexture)
649 if (!SDL_SetRenderTarget(_renderer, _renderTarget))
652 SDL_FRect fsrc = {
static_cast<float>(srcRect.x),
static_cast<float>(srcRect.y),
653 static_cast<float>(srcRect.w),
static_cast<float>(srcRect.h) };
654 SDL_FRect fdst = {
static_cast<float>(dstRect.x),
static_cast<float>(dstRect.y),
655 static_cast<float>(dstRect.w),
static_cast<float>(dstRect.h) };
656 if (!SDL_RenderTexture(_renderer, _gdiTexture, &fsrc, &fdst))
658 SDL_LogError(SDL_LOG_CATEGORY_RENDER,
"SDL_RenderTexture: %s", SDL_GetError());
669 ensureRenderTarget();
672 if (!SDL_SetRenderTarget(_renderer,
nullptr))
674 if (!SDL_RenderTexture(_renderer, _renderTarget,
nullptr,
nullptr))
678 if (overlay && overlay->visible())
679 drawOverlay(*overlay);
680 if (!SDL_RenderPresent(_renderer))
684void SdlWindow::drawVideoOverlays()
686 for (
const auto& entry : _videoOverlays)
688 const auto& o = entry.second;
689 if (!o.visible || !o.texture || (o.dst.w <= 0) || (o.dst.h <= 0))
693 std::ignore = SDL_RenderTexture(_renderer, o.texture,
nullptr, &o.dst);
697 const float sx =
static_cast<float>(o.w) / o.dst.w;
698 const float sy =
static_cast<float>(o.h) / o.dst.h;
699 for (
const auto& c : o.clip)
702 if (!SDL_GetRectIntersectionFloat(&c, &o.dst, &part))
704 const SDL_FRect src{ (part.x - o.dst.x) * sx, (part.y - o.dst.y) * sy, part.w * sx,
706 std::ignore = SDL_RenderTexture(_renderer, o.texture, &src, &part);
712 const SDL_FRect& dst,
const std::vector<SDL_FRect>& clip,
715 if (!_renderer || (w <= 0) || (h <= 0))
717 auto& o = _videoOverlays[id];
718 if (!o.texture || (o.w != w) || (o.h != h))
721 SDL_DestroyTexture(o.texture);
723 SDL_CreateTexture(_renderer, SDL_PIXELFORMAT_BGRA32, SDL_TEXTUREACCESS_STREAMING, w, h);
726 SDL_LogError(SDL_LOG_CATEGORY_RENDER,
"SDL_CreateTexture: %s", SDL_GetError());
727 _videoOverlays.erase(
id);
731 std::ignore = SDL_SetTextureBlendMode(o.texture, SDL_BLENDMODE_NONE);
735 if (pixels && !SDL_UpdateTexture(o.texture,
nullptr, pixels, pitch))
737 SDL_LogError(SDL_LOG_CATEGORY_RENDER,
"SDL_UpdateTexture: %s", SDL_GetError());
748 for (
auto it = _videoOverlays.begin(); it != _videoOverlays.end();)
754 SDL_DestroyTexture(it->second.texture);
755 it = _videoOverlays.erase(it);
760const char* SdlWindow::rendererName()
const
762 return _renderer ? SDL_GetRendererName(_renderer) : nullptr;
765void SdlWindow::drawOverlay(
const SdlOverlay& overlay)
767 if (!_overlayPainter)
768 _overlayPainter = std::make_unique<SdlOverlayPainter>(_renderer);
772 if (!SDL_GetCurrentRenderOutputSize(_renderer, &w, &h))
774 _overlayPainter->draw(overlay, w, h, SDL_GetWindowDisplayScale(_window));
777bool SdlWindow::paintResizeFrame(SDL_Surface* surface, SDL_Point off,
bool contentChanged,
778 const SDL_Rect& inset,
bool fillRevealed,
bool dashedBorder)
780 if (!_renderer || !surface)
782 ensureRenderTarget();
784 const int prevW = _gdiTextureW;
785 const int prevH = _gdiTextureH;
786 if (!ensureGdiTexture(surface))
789 const bool recreated = (_gdiTextureW != prevW) || (_gdiTextureH != prevH);
790 if ((contentChanged || recreated) &&
791 !SDL_UpdateTexture(_gdiTexture,
nullptr, surface->pixels, surface->pitch))
794 if (!SDL_SetRenderTarget(_renderer, _renderTarget))
799 SDL_GetWindowSizeInPixels(_window, &ww, &wh);
801 const SDL_FRect frame = {
static_cast<float>(inset.x),
static_cast<float>(inset.y),
802 static_cast<float>(ww - inset.x - inset.w),
803 static_cast<float>(wh - inset.y - inset.h) };
806 constexpr Uint8 kFillAlpha = 0x80;
807 std::ignore = SDL_SetRenderDrawBlendMode(_renderer, SDL_BLENDMODE_NONE);
808 std::ignore = SDL_SetRenderDrawColor(_renderer, 0, 0, 0, 0);
809 std::ignore = SDL_RenderClear(_renderer);
812 std::ignore = SDL_SetRenderDrawColor(_renderer, 0x2B, 0x2B, 0x2B, kFillAlpha);
813 std::ignore = SDL_RenderFillRect(_renderer, &frame);
815 SDL_FRect fdst = {
static_cast<float>(off.x),
static_cast<float>(off.y),
816 static_cast<float>(surface->w),
static_cast<float>(surface->h) };
818 const SDL_Rect clip = { inset.x, inset.y, ww - inset.x - inset.w, wh - inset.y - inset.h };
819 std::ignore = SDL_SetRenderClipRect(_renderer, &clip);
820 std::ignore = SDL_RenderTexture(_renderer, _gdiTexture,
nullptr, &fdst);
821 std::ignore = SDL_SetRenderClipRect(_renderer,
nullptr);
826 std::ignore = SDL_SetRenderDrawColor(_renderer, 0xC8, 0xC8, 0xC8, 0xFF);
827 const float dash = 8.0F;
828 const float gap = 5.0F;
829 const float fx2 = frame.x + frame.w;
830 const float fy2 = frame.y + frame.h;
831 const float lo = frame.y + 0.5F;
832 const float by = fy2 - 0.5F;
833 const float lx = frame.x + 0.5F;
834 const float rx = fx2 - 0.5F;
835 const float step = dash + gap;
836 const auto steps = [step](
float len)
837 {
return (len <= 0.0F) ? 0 :
static_cast<int>(std::ceil(len / step)); };
838 for (
int i = 0; i < steps(fx2 - frame.x); i++)
840 const float x = frame.x + (
static_cast<float>(i) * step);
841 const float x2 = (x + dash < fx2) ? (x + dash) : rx;
842 std::ignore = SDL_RenderLine(_renderer, x, lo, x2, lo);
843 std::ignore = SDL_RenderLine(_renderer, x, by, x2, by);
845 for (
int i = 0; i < steps(fy2 - frame.y); i++)
847 const float y = frame.y + (
static_cast<float>(i) * step);
848 const float y2 = (y + dash < fy2) ? (y + dash) : by;
849 std::ignore = SDL_RenderLine(_renderer, lx, y, lx, y2);
850 std::ignore = SDL_RenderLine(_renderer, rx, y, rx, y2);
854 if (!SDL_SetRenderTarget(_renderer,
nullptr))
856 std::ignore = SDL_RenderTexture(_renderer, _renderTarget,
nullptr,
nullptr);
858 std::ignore = SDL_RenderPresent(_renderer);
862SdlWindow SdlWindow::create(SDL_DisplayID
id,
const std::string& title, Uint32 flags, Uint32 width,
865 flags |= SDL_WINDOW_HIGH_PIXEL_DENSITY;
867 SDL_Rect rect = {
static_cast<int>(SDL_WINDOWPOS_CENTERED_DISPLAY(
id)),
868 static_cast<int>(SDL_WINDOWPOS_CENTERED_DISPLAY(
id)),
static_cast<int>(width),
869 static_cast<int>(height) };
871 if ((flags & SDL_WINDOW_FULLSCREEN) != 0)
873 std::ignore = SDL_GetDisplayBounds(
id, &rect);
876 SdlWindow window{ id, title, rect, flags };
878 if ((flags & SDL_WINDOW_FULLSCREEN) != 0)
880 window.setOffsetX(rect.x);
881 window.setOffsetY(rect.y);
887SdlWindow SdlWindow::create(SDL_DisplayID
id,
const std::string& title, Uint32 flags,
888 const SDL_Rect& rect)
890 return SdlWindow{ id, title, rect, flags };
895 : _initialW(rect.w), _initialH(rect.h)
897 auto props = SDL_CreateProperties();
898 SDL_SetPointerProperty(props, SDL_PROP_WINDOW_CREATE_PARENT_POINTER, parent);
899 SDL_SetBooleanProperty(props,
900 tooltip ? SDL_PROP_WINDOW_CREATE_TOOLTIP_BOOLEAN
901 : SDL_PROP_WINDOW_CREATE_MENU_BOOLEAN,
903 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_FOCUSABLE_BOOLEAN,
false);
904#if defined(SDL_PROP_WINDOW_CREATE_CONSTRAIN_POPUP_BOOLEAN)
906 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_CONSTRAIN_POPUP_BOOLEAN,
false);
908 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_BORDERLESS_BOOLEAN,
true);
911 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_TRANSPARENT_BOOLEAN,
true);
912 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, rect.x);
913 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, rect.y);
914 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, rect.w);
915 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, rect.h);
917 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIDDEN_BOOLEAN,
true);
919 _window = SDL_CreateWindowWithProperties(props);
920 SDL_DestroyProperties(props);
923 std::ignore = SDL_SyncWindow(_window);
924 _renderer = SDL_CreateRenderer(_window,
nullptr);
925 _displayID = SDL_GetDisplayForWindow(_window);
929SdlWindow SdlWindow::createPopup(SDL_Window* parent,
const SDL_Rect& rect,
bool transparent,
932 return SdlWindow{ parent, rect, transparent, tooltip };
935static SDL_Window* createDummy(SDL_DisplayID
id)
937 const auto x = SDL_WINDOWPOS_CENTERED_DISPLAY(
id);
938 const auto y = SDL_WINDOWPOS_CENTERED_DISPLAY(
id);
942 auto props = SDL_CreateProperties();
943 std::stringstream ss;
944 ss <<
"SdlWindow::query(" <<
id <<
")";
945 SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, ss.str().c_str());
946 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, x);
947 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, y);
948 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, w);
949 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, h);
951 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIGH_PIXEL_DENSITY_BOOLEAN,
true);
952 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_FULLSCREEN_BOOLEAN,
false);
953 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_BORDERLESS_BOOLEAN,
true);
954 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIDDEN_BOOLEAN,
false);
956 auto window = SDL_CreateWindowWithProperties(props);
957 SDL_DestroyProperties(props);
965 std::ignore = SDL_GetDisplayBounds(
id, &rect);
966 std::ignore = SDL_SetWindowPosition(window, rect.x, rect.y);
967 std::ignore = SDL_SetWindowFullscreen(window,
true);
972rdpMonitor SdlWindow::query(SDL_DisplayID
id,
bool forceAsPrimary)
974 if (sdl::utils::isWaylandDriver() || sdl::utils::isX11Driver())
975 return queryDisplay(
id, forceAsPrimary);
977 std::unique_ptr<SDL_Window, void (*)(SDL_Window*)> window(createDummy(
id), SDL_DestroyWindow);
981 std::unique_ptr<SDL_Renderer, void (*)(SDL_Renderer*)> renderer(
982 SDL_CreateRenderer(window.get(),
nullptr), SDL_DestroyRenderer);
984 if (!SDL_SyncWindow(window.get()))
988 while (SDL_PollEvent(&event))
991 return query(window.get(),
id, forceAsPrimary);
994SDL_Rect SdlWindow::rect(SDL_DisplayID
id,
bool forceAsPrimary)
996 std::unique_ptr<SDL_Window, void (*)(SDL_Window*)> window(createDummy(
id), SDL_DestroyWindow);
1000 std::unique_ptr<SDL_Renderer, void (*)(SDL_Renderer*)> renderer(
1001 SDL_CreateRenderer(window.get(),
nullptr), SDL_DestroyRenderer);
1003 if (!SDL_SyncWindow(window.get()))
1007 while (SDL_PollEvent(&event))
1010 return rect(window.get(), forceAsPrimary);
1013bool SdlWindow::tryFallback(
bool isFullscreen)
1018 const auto wlroots_hack = SDL_getenv(
"FREERDP_WLROOTS_HACK");
1019 if (wlroots_hack !=
nullptr)
1021 const auto enabled = strcmp(wlroots_hack,
"0") != 0;
1022 if (strcmp(wlroots_hack,
"force") == 0)
1023 isFullscreen =
true;
1024 return enabled && isFullscreen;
1027 const auto platform = SDL_GetPlatform();
1028 if ((platform ==
nullptr) || (strcmp(platform,
"Linux") != 0))
1031 const auto driver = SDL_GetCurrentVideoDriver();
1032 if ((driver ==
nullptr) || (strcmp(driver,
"wayland") != 0))
1042 auto isWlrootsCompositor = [](
const char* value) ->
bool
1046 if (strstr(value,
"sway") || strstr(value,
"Sway") || strstr(value,
"Hyprland") ||
1047 strstr(value,
"hyprland") || strstr(value,
"river") || strstr(value,
"wlroots"))
1052 const auto xdg_session = SDL_getenv(
"XDG_SESSION_DESKTOP");
1053 if (isWlrootsCompositor(xdg_session))
1054 return isFullscreen;
1056 const auto xdg_desktop = SDL_getenv(
"XDG_CURRENT_DESKTOP");
1057 if (isWlrootsCompositor(xdg_desktop))
1058 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)