FreeRDP
Loading...
Searching...
No Matches
SDL3/sdl_window.cpp
1
20#include <limits>
21#include <sstream>
22#include <cmath>
23
24#include "sdl_window.hpp"
25#include "sdl_utils.hpp"
26#include "sdl_overlay.hpp"
27
28#include <freerdp/utils/string.h>
29
30SdlWindow::SdlWindow(SDL_DisplayID id, const std::string& title, const SDL_Rect& rect,
31 [[maybe_unused]] Uint32 flags)
32 : _initialW(rect.w), _initialH(rect.h), _displayID(id)
33{
34 float pd = SDL_GetDisplayContentScale(id);
35 if (pd <= 0.0f)
36 pd = 1.0f;
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));
39
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);
47
48 if (flags & SDL_WINDOW_HIGH_PIXEL_DENSITY)
49 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIGH_PIXEL_DENSITY_BOOLEAN, true);
50
51 if (flags & SDL_WINDOW_FULLSCREEN)
52 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_FULLSCREEN_BOOLEAN, true);
53
54 if (flags & SDL_WINDOW_BORDERLESS)
55 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_BORDERLESS_BOOLEAN, true);
56
57 if (flags & SDL_WINDOW_TRANSPARENT)
58 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_TRANSPARENT_BOOLEAN, true);
59
60 /* RAIL windows are created hidden until first paint to avoid black flash. */
61 if (flags & SDL_WINDOW_HIDDEN)
62 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIDDEN_BOOLEAN, true);
63
64 _window = SDL_CreateWindowWithProperties(props);
65 SDL_DestroyProperties(props);
66 SDL_SetHint(SDL_HINT_APP_NAME, "");
67 std::ignore = SDL_SyncWindow(_window);
68
69 _renderer = SDL_CreateRenderer(_window, nullptr);
70
71 std::ignore = resizeToScale();
72
73 _monitor = query(_window, id, true);
74}
75
76SdlWindow::SdlWindow(SdlWindow&& other) noexcept
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)
83{
84 other._videoOverlays.clear();
85 other._window = nullptr;
86 other._renderer = nullptr;
87 other._renderTarget = nullptr;
88 other._gdiTexture = nullptr;
89}
90
91SdlWindow::~SdlWindow()
92{
93 for (auto& o : _videoOverlays)
94 SDL_DestroyTexture(o.second.texture);
95 /* its text lives in the renderer */
96 _overlayPainter.reset();
97 if (_gdiTexture)
98 SDL_DestroyTexture(_gdiTexture);
99 if (_renderTarget)
100 SDL_DestroyTexture(_renderTarget);
101 if (_renderer)
102 SDL_DestroyRenderer(_renderer);
103 if (_window)
104 SDL_DestroyWindow(_window);
105}
106
107SDL_WindowID SdlWindow::id() const
108{
109 if (!_window)
110 return 0;
111 return SDL_GetWindowID(_window);
112}
113
114SDL_DisplayID SdlWindow::displayIndex() const
115{
116 if (!_window)
117 return 0;
118 return SDL_GetDisplayForWindow(_window);
119}
120
121SDL_Rect SdlWindow::rect() const
122{
123 return rect(_window);
124}
125
126SDL_Rect SdlWindow::bounds() const
127{
128 SDL_Rect rect = {};
129 if (_window)
130 {
131 if (!SDL_GetWindowPosition(_window, &rect.x, &rect.y))
132 return {};
133 if (!SDL_GetWindowSize(_window, &rect.w, &rect.h))
134 return {};
135 }
136 return rect;
137}
138
139SDL_Window* SdlWindow::window() const
140{
141 return _window;
142}
143
144SDL_Renderer* SdlWindow::renderer() const
145{
146 return _renderer;
147}
148
149Sint32 SdlWindow::offsetX() const
150{
151 return _offset_x;
152}
153
154void SdlWindow::setOffsetX(Sint32 x)
155{
156 _offset_x = x;
157}
158
159void SdlWindow::setOffsetY(Sint32 y)
160{
161 _offset_y = y;
162}
163
164Sint32 SdlWindow::offsetY() const
165{
166 return _offset_y;
167}
168
169rdpMonitor SdlWindow::monitor(bool isPrimary) const
170{
171 auto m = _monitor;
172 if (isPrimary)
173 {
174 m.x = 0;
175 m.y = 0;
176 }
177 return m;
178}
179
180void SdlWindow::setMonitor(rdpMonitor monitor)
181{
182 _monitor = monitor;
183}
184
185float SdlWindow::scale() const
186{
187 return SDL_GetWindowDisplayScale(_window);
188}
189
190SDL_DisplayOrientation SdlWindow::orientation() const
191{
192 const auto did = displayIndex();
193 return SDL_GetCurrentDisplayOrientation(did);
194}
195
196bool SdlWindow::grabKeyboard(bool enable)
197{
198 if (!_window)
199 return false;
200 SDL_SetWindowKeyboardGrab(_window, enable);
201 return true;
202}
203
204bool SdlWindow::grabMouse(bool enable)
205{
206 if (!_window)
207 return false;
208 SDL_SetWindowMouseGrab(_window, enable);
209 return true;
210}
211
212void SdlWindow::setBordered(bool bordered)
213{
214 if (_window)
215 SDL_SetWindowBordered(_window, bordered);
216 /* No SDL_SyncWindow: this runs for every window event, and on macOS each one waited for SDL's
217 * sync timeout (2.5 s) while a full screen change was pending. SDL ignores an unchanged border
218 * anyway. */
219}
220
221void SdlWindow::raise()
222{
223 SDL_RaiseWindow(_window);
224 std::ignore = SDL_SyncWindow(_window);
225}
226
227void SdlWindow::resizeable(bool use)
228{
229 SDL_SetWindowResizable(_window, use);
230 std::ignore = SDL_SyncWindow(_window);
231}
232
233void SdlWindow::fullscreen(bool enter, bool forceOriginalDisplay)
234{
235 if (enter && forceOriginalDisplay && _displayID != 0)
236 {
237 /* Move the window to the desired display. We should not wait
238 * for the window to be moved, because some backends can refuse
239 * the move. The intent of moving the window is enough for SDL
240 * to decide which display will be used for fullscreen. */
241 SDL_Rect rect = {};
242 std::ignore = SDL_GetDisplayBounds(_displayID, &rect);
243 std::ignore = SDL_SetWindowPosition(_window, rect.x, rect.y);
244 }
245 std::ignore = SDL_SetWindowFullscreen(_window, enter);
246 std::ignore = SDL_SyncWindow(_window);
247}
248
249void SdlWindow::minimize()
250{
251 SDL_MinimizeWindow(_window);
252 std::ignore = SDL_SyncWindow(_window);
253}
254
255bool SdlWindow::resizeToScale()
256{
257 if (!_window || _initialW <= 0 || _initialH <= 0)
258 return false;
259 if ((SDL_GetWindowFlags(_window) & SDL_WINDOW_FULLSCREEN) != 0)
260 return true;
261
262 float pd = SDL_GetWindowPixelDensity(_window);
263 if (pd <= 0.0f)
264 pd = 1.0f;
265
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));
268
269 int curW = 0;
270 int curH = 0;
271 if (!SDL_GetWindowSize(_window, &curW, &curH))
272 return false;
273
274 if (curW == targetW && curH == targetH)
275 return true;
276
277 return resize({ targetW, targetH });
278}
279
280bool SdlWindow::resize(const SDL_Point& size)
281{
282 return SDL_SetWindowSize(_window, size.x, size.y);
283}
284
285void SdlWindow::ensureRenderTarget()
286{
287 if (!_renderer)
288 return;
289
290 int w = 0;
291 int h = 0;
292 SDL_GetWindowSizeInPixels(_window, &w, &h);
293 if (w <= 0 || h <= 0)
294 return;
295
296 /* Recreate if missing or if window size changed */
297 if (_renderTarget)
298 {
299 float tw = 0;
300 float th = 0;
301 if (!SDL_GetTextureSize(_renderTarget, &tw, &th))
302 return;
303 if (static_cast<int>(tw) == w && static_cast<int>(th) == h)
304 return;
305 SDL_DestroyTexture(_renderTarget);
306 }
307
308 _renderTarget =
309 SDL_CreateTexture(_renderer, SDL_PIXELFORMAT_BGRA32, SDL_TEXTUREACCESS_TARGET, w, h);
310 if (!_renderTarget)
311 {
312 SDL_LogError(SDL_LOG_CATEGORY_RENDER, "SDL_CreateTexture (render target): %s",
313 SDL_GetError());
314 return;
315 }
316 /* Verbatim, never blended: a transparent window's alpha<0xFF regions would render black. */
317 std::ignore = SDL_SetTextureBlendMode(_renderTarget, SDL_BLENDMODE_NONE);
318
319 /* Clear once: transparent so windows never flash black before painting. */
320 if (SDL_SetRenderTarget(_renderer, _renderTarget))
321 {
322 std::ignore = SDL_SetRenderDrawColor(_renderer, 0x00, 0x00, 0x00, 0x00);
323 std::ignore = SDL_RenderClear(_renderer);
324 }
325}
326
327bool SdlWindow::drawRect(SDL_Surface* surface, SDL_Point offset, const SDL_Rect& srcRect)
328{
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);
332}
333
334bool SdlWindow::drawRects(SDL_Surface* surface, SDL_Point offset,
335 const std::vector<SDL_Rect>& rects)
336{
337 if (rects.empty())
338 {
339 return drawRect(surface, offset, { 0, 0, surface->w, surface->h });
340 }
341 for (auto& srcRect : rects)
342 {
343 if (!drawRect(surface, offset, srcRect))
344 return false;
345 }
346 return true;
347}
348
349static SDL_Rect scaledDstRect(const SDL_FPoint& scale, const SDL_Rect& srcRect)
350{
351 SDL_Rect dstRect = {};
352 float ix = 0.0f;
353 float iy = 0.0f;
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);
362 return dstRect;
363}
364
365bool SdlWindow::drawScaledRect(SDL_Surface* surface, const SDL_FPoint& scale,
366 const SDL_Rect& srcRect)
367{
368 auto dstRect = scaledDstRect(scale, srcRect);
369 return blit(surface, srcRect, dstRect);
370}
371
372bool SdlWindow::drawScaledRects(SDL_Surface* surface, const SDL_FPoint& scale,
373 const std::vector<SDL_Rect>& rects)
374{
375 if (rects.empty())
376 {
377 return drawScaledRect(surface, scale, { 0, 0, surface->w, surface->h });
378 }
379
380 /* Upload every dirty rect before drawing any of them: scaling filters linearly, so the
381 * edge of each drawn rect also samples the texels just outside it. Drawn one by one,
382 * those can be a neighbouring dirty rect that is not uploaded yet, and the old picture
383 * stays behind as a faint line along the edge until that area changes again. */
384 for (const auto& srcRect : rects)
385 {
386 if (!upload(surface, srcRect))
387 return false;
388 }
389 for (const auto& srcRect : rects)
390 {
391 if (!render(srcRect, scaledDstRect(scale, srcRect)))
392 return false;
393 }
394 return true;
395}
396
397bool SdlWindow::fill(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
398{
399 if (_renderer)
400 {
401 ensureRenderTarget();
402 if (!SDL_SetRenderTarget(_renderer, _renderTarget))
403 return false;
404 if (!SDL_SetRenderDrawColor(_renderer, r, g, b, a))
405 return false;
406 return SDL_RenderClear(_renderer);
407 }
408 return fill(_window, r, g, b, a);
409}
410
411bool SdlWindow::fill(SDL_Window* window, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
412{
413 auto surface = SDL_GetWindowSurface(window);
414 if (!surface)
415 return false;
416 SDL_Rect rect = { 0, 0, surface->w, surface->h };
417 auto color = SDL_MapSurfaceRGBA(surface, r, g, b, a);
418
419 return SDL_FillSurfaceRect(surface, &rect, color);
420}
421
422static rdpMonitor makeMonitor(SDL_DisplayID id, const SDL_Rect& r, float factor,
423 bool forceAsPrimary)
424{
425 const float dpi = std::roundf(factor * 100.0f);
426
427 /* A compositor without an output can report a display of 0x0 (Xwayland in a headless
428 * mutter), the caller skips such a display. */
429 if ((r.w <= 0) || (r.h <= 0))
430 {
431 SDL_LogWarn(SDL_LOG_CATEGORY_VIDEO, "display %" PRIu32 ": invalid size %dx%d", id, r.w,
432 r.h);
433 return {};
434 }
435
436 const auto primary = SDL_GetPrimaryDisplay();
437 const auto orientation = SDL_GetCurrentDisplayOrientation(id);
438 const auto rdp_orientation = sdl::utils::orientaion_to_rdp(orientation);
439
440 rdpMonitor monitor{};
441 monitor.orig_screen = id;
442 monitor.x = r.x;
443 monitor.y = r.y;
444 monitor.width = r.w;
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;
450 /* The physical size is in millimetres and ignored outside 10 to 10000 ([MS-RDPBCGR]
451 * 2.2.1.3.9.1, [MS-RDPEDISP] 2.2.2.2.1). SDL3 does not know a display's physical size, so
452 * leave it at 0 (not given) instead of sending the size in pixels as millimetres. */
453 monitor.attributes.physicalWidth = 0;
454 monitor.attributes.physicalHeight = 0;
455
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);
473 return monitor;
474}
475
476rdpMonitor SdlWindow::query(SDL_Window* window, SDL_DisplayID id, bool forceAsPrimary)
477{
478 if (!window)
479 return {};
480 return makeMonitor(id, rect(window, forceAsPrimary), SDL_GetWindowDisplayScale(window),
481 forceAsPrimary);
482}
483
484/* Dummy windows report wrong sizes on Wayland and X11, use the display. */
485static rdpMonitor queryDisplay(SDL_DisplayID id, bool forceAsPrimary)
486{
487 SDL_Rect r = {};
488 const SDL_DisplayMode* mode = SDL_GetDesktopDisplayMode(id);
489 if (!mode || !SDL_GetDisplayBounds(id, &r))
490 {
491 SDL_LogWarn(SDL_LOG_CATEGORY_VIDEO, "display %" PRIu32 ": %s", id, SDL_GetError());
492 return {};
493 }
494 if (forceAsPrimary)
495 {
496 r.x = 0;
497 r.y = 0;
498 }
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);
502}
503
504SDL_Rect SdlWindow::rect(SDL_Window* window, bool forceAsPrimary)
505{
506 SDL_Rect rect = {};
507 if (!window)
508 return {};
509
510 if (!forceAsPrimary)
511 {
512 if (!SDL_GetWindowPosition(window, &rect.x, &rect.y))
513 return {};
514 }
515
516 if (!SDL_GetWindowSizeInPixels(window, &rect.w, &rect.h))
517 return {};
518
519 const bool placeholder = (rect.w < 200) || (rect.h < 200);
520 if (placeholder && tryFallback(true))
521 {
522 /* On wlroots compositors (Sway, river, etc.), windows that are hidden/unmapped
523 * don't get their actual display dimensions. The dummy window returns its creation size
524 * (64x64) instead of the display size. This causes validation errors since we require >=
525 * 200px. Workaround: If we got dimensions that are too small, query the display directly.
526 */
527
528 const auto displayID = SDL_GetDisplayForWindow(window);
529 SDL_Rect displayBounds = {};
530 if (SDL_GetDisplayBounds(displayID, &displayBounds))
531 {
532 if (forceAsPrimary)
533 {
534 rect.x = 0;
535 rect.y = 0;
536 }
537 else
538 {
539 /* The dummy window is centered on the display by the compositor, so its
540 * position is not the display origin. Use the display bounds as for w/h. */
541 rect.x = displayBounds.x;
542 rect.y = displayBounds.y;
543 }
544 rect.w = displayBounds.w;
545 rect.h = displayBounds.h;
546
547 float factor = 1.0f;
548 const auto mode = SDL_GetCurrentDisplayMode(displayID);
549 if (mode && (mode->pixel_density > 1.0f))
550 factor = mode->pixel_density;
551 else
552 {
553 const float contentScale = SDL_GetDisplayContentScale(displayID);
554 if (contentScale > 1.0f)
555 factor = contentScale;
556 }
557 if (factor > 1.0f)
558 {
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));
563 }
564 }
565 }
566
567 return rect;
568}
569
570SdlWindow::HighDPIMode SdlWindow::isHighDPIWindowsMode(SDL_Window* window)
571{
572 if (!window)
573 return MODE_INVALID;
574
575 const auto id = SDL_GetDisplayForWindow(window);
576 if (id == 0)
577 return MODE_INVALID;
578
579 const auto cs = SDL_GetDisplayContentScale(id);
580 const auto ds = SDL_GetWindowDisplayScale(window);
581 const auto pd = SDL_GetWindowPixelDensity(window);
582
583 /* mac os x style, but no HighDPI display */
584 if ((cs == 1.0f) && (ds == 1.0f) && (pd == 1.0f))
585 return MODE_NONE;
586
587 /* mac os x style HighDPI */
588 if ((cs == 1.0f) && (ds > 1.0f) && (pd > 1.0f))
589 return MODE_MACOS;
590
591 /* rest is windows style */
592 return MODE_WINDOWS;
593}
594
595/* Lazily create or recreate the persistent streaming GDI texture to match `surface`. */
596bool SdlWindow::ensureGdiTexture(SDL_Surface* surface)
597{
598 if (_gdiTexture && (_gdiTextureW == surface->w) && (_gdiTextureH == surface->h))
599 return true;
600 if (_gdiTexture)
601 SDL_DestroyTexture(_gdiTexture);
602 _gdiTexture = SDL_CreateTexture(_renderer, surface->format, SDL_TEXTUREACCESS_STREAMING,
603 surface->w, surface->h);
604 if (!_gdiTexture)
605 {
606 SDL_LogError(SDL_LOG_CATEGORY_RENDER, "SDL_CreateTexture: %s", SDL_GetError());
607 return false;
608 }
609 std::ignore = SDL_SetTextureBlendMode(_gdiTexture, SDL_BLENDMODE_NONE);
610 _gdiTextureW = surface->w;
611 _gdiTextureH = surface->h;
612 return true;
613}
614
615bool SdlWindow::blit(SDL_Surface* surface, const SDL_Rect& srcRect, SDL_Rect& dstRect)
616{
617 return upload(surface, srcRect) && render(srcRect, dstRect);
618}
619
620bool SdlWindow::upload(SDL_Surface* surface, const SDL_Rect& srcRect)
621{
622 if (!_renderer || !surface)
623 return false;
624
625 ensureRenderTarget();
626
627 if (!ensureGdiTexture(surface))
628 return false;
629
630 /* Upload only the dirty region */
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))
636 {
637 SDL_LogError(SDL_LOG_CATEGORY_RENDER, "SDL_UpdateTexture: %s", SDL_GetError());
638 return false;
639 }
640 return true;
641}
642
643bool SdlWindow::render(const SDL_Rect& srcRect, const SDL_Rect& dstRect)
644{
645 if (!_renderer || !_gdiTexture)
646 return false;
647
648 /* Render onto persistent render target to accumulate dirty rects */
649 if (!SDL_SetRenderTarget(_renderer, _renderTarget))
650 return false;
651
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))
657 {
658 SDL_LogError(SDL_LOG_CATEGORY_RENDER, "SDL_RenderTexture: %s", SDL_GetError());
659 return false;
660 }
661 return true;
662}
663
664void SdlWindow::updateSurface(const SdlOverlay* overlay)
665{
666 if (!_renderer)
667 return;
668
669 ensureRenderTarget();
670
671 /* Copy accumulated render target to screen and present */
672 if (!SDL_SetRenderTarget(_renderer, nullptr))
673 return;
674 if (!SDL_RenderTexture(_renderer, _renderTarget, nullptr, nullptr))
675 return;
676 drawVideoOverlays();
677 /* over the picture, never into it */
678 if (overlay && overlay->visible())
679 drawOverlay(*overlay);
680 if (!SDL_RenderPresent(_renderer))
681 return;
682}
683
684void SdlWindow::drawVideoOverlays()
685{
686 for (const auto& entry : _videoOverlays)
687 {
688 const auto& o = entry.second;
689 if (!o.visible || !o.texture || (o.dst.w <= 0) || (o.dst.h <= 0))
690 continue;
691 if (o.clip.empty())
692 {
693 std::ignore = SDL_RenderTexture(_renderer, o.texture, nullptr, &o.dst);
694 continue;
695 }
696 /* each visible part, with the part of the texture it shows */
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)
700 {
701 SDL_FRect part{};
702 if (!SDL_GetRectIntersectionFloat(&c, &o.dst, &part))
703 continue;
704 const SDL_FRect src{ (part.x - o.dst.x) * sx, (part.y - o.dst.y) * sy, part.w * sx,
705 part.h * sy };
706 std::ignore = SDL_RenderTexture(_renderer, o.texture, &src, &part);
707 }
708 }
709}
710
711bool SdlWindow::setVideoOverlay(Uint64 id, const void* pixels, int pitch, int w, int h,
712 const SDL_FRect& dst, const std::vector<SDL_FRect>& clip,
713 bool visible)
714{
715 if (!_renderer || (w <= 0) || (h <= 0))
716 return false;
717 auto& o = _videoOverlays[id];
718 if (!o.texture || (o.w != w) || (o.h != h))
719 {
720 if (o.texture)
721 SDL_DestroyTexture(o.texture);
722 o.texture =
723 SDL_CreateTexture(_renderer, SDL_PIXELFORMAT_BGRA32, SDL_TEXTUREACCESS_STREAMING, w, h);
724 if (!o.texture)
725 {
726 SDL_LogError(SDL_LOG_CATEGORY_RENDER, "SDL_CreateTexture: %s", SDL_GetError());
727 _videoOverlays.erase(id);
728 return false;
729 }
730 /* the video is opaque: its alpha is not for us to blend */
731 std::ignore = SDL_SetTextureBlendMode(o.texture, SDL_BLENDMODE_NONE);
732 o.w = w;
733 o.h = h;
734 }
735 if (pixels && !SDL_UpdateTexture(o.texture, nullptr, pixels, pitch))
736 {
737 SDL_LogError(SDL_LOG_CATEGORY_RENDER, "SDL_UpdateTexture: %s", SDL_GetError());
738 return false;
739 }
740 o.dst = dst;
741 o.clip = clip;
742 o.visible = visible;
743 return true;
744}
745
746void SdlWindow::pruneVideoOverlays(const std::function<bool(Uint64)>& keep)
747{
748 for (auto it = _videoOverlays.begin(); it != _videoOverlays.end();)
749 {
750 if (keep(it->first))
751 ++it;
752 else
753 {
754 SDL_DestroyTexture(it->second.texture);
755 it = _videoOverlays.erase(it);
756 }
757 }
758}
759
760const char* SdlWindow::rendererName() const
761{
762 return _renderer ? SDL_GetRendererName(_renderer) : nullptr;
763}
764
765void SdlWindow::drawOverlay(const SdlOverlay& overlay)
766{
767 if (!_overlayPainter)
768 _overlayPainter = std::make_unique<SdlOverlayPainter>(_renderer);
769
770 int w = 0;
771 int h = 0;
772 if (!SDL_GetCurrentRenderOutputSize(_renderer, &w, &h))
773 return;
774 _overlayPainter->draw(overlay, w, h, SDL_GetWindowDisplayScale(_window));
775}
776
777bool SdlWindow::paintResizeFrame(SDL_Surface* surface, SDL_Point off, bool contentChanged,
778 const SDL_Rect& inset, bool fillRevealed, bool dashedBorder)
779{
780 if (!_renderer || !surface)
781 return false;
782 ensureRenderTarget();
783
784 const int prevW = _gdiTextureW;
785 const int prevH = _gdiTextureH;
786 if (!ensureGdiTexture(surface))
787 return false;
788 /* A recreated texture is empty, so upload even when the content did not change. */
789 const bool recreated = (_gdiTextureW != prevW) || (_gdiTextureH != prevH);
790 if ((contentChanged || recreated) &&
791 !SDL_UpdateTexture(_gdiTexture, nullptr, surface->pixels, surface->pitch))
792 return false;
793
794 if (!SDL_SetRenderTarget(_renderer, _renderTarget))
795 return false;
796
797 int ww = 0;
798 int wh = 0;
799 SDL_GetWindowSizeInPixels(_window, &ww, &wh);
800 /* The visible frame; the ring outside it (the resize band) stays fully transparent. */
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) };
804
805 /* Translucent fill in revealed area awaiting server frame. */
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);
810 if (fillRevealed)
811 {
812 std::ignore = SDL_SetRenderDrawColor(_renderer, 0x2B, 0x2B, 0x2B, kFillAlpha);
813 std::ignore = SDL_RenderFillRect(_renderer, &frame);
814 }
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) };
817 /* Anchored frame clipped to visible bounds. */
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);
822
823 /* Dashed border indicating pending resize target. */
824 if (dashedBorder)
825 {
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++)
839 {
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);
844 }
845 for (int i = 0; i < steps(fy2 - frame.y); i++)
846 {
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);
851 }
852 }
853
854 if (!SDL_SetRenderTarget(_renderer, nullptr))
855 return false;
856 std::ignore = SDL_RenderTexture(_renderer, _renderTarget, nullptr, nullptr);
857 drawVideoOverlays();
858 std::ignore = SDL_RenderPresent(_renderer);
859 return true;
860}
861
862SdlWindow SdlWindow::create(SDL_DisplayID id, const std::string& title, Uint32 flags, Uint32 width,
863 Uint32 height)
864{
865 flags |= SDL_WINDOW_HIGH_PIXEL_DENSITY;
866
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) };
870
871 if ((flags & SDL_WINDOW_FULLSCREEN) != 0)
872 {
873 std::ignore = SDL_GetDisplayBounds(id, &rect);
874 }
875
876 SdlWindow window{ id, title, rect, flags };
877
878 if ((flags & SDL_WINDOW_FULLSCREEN) != 0)
879 {
880 window.setOffsetX(rect.x);
881 window.setOffsetY(rect.y);
882 }
883
884 return window;
885}
886
887SdlWindow SdlWindow::create(SDL_DisplayID id, const std::string& title, Uint32 flags,
888 const SDL_Rect& rect)
889{
890 return SdlWindow{ id, title, rect, flags };
891}
892
893/* Popup constructor: positioned relative to the parent origin. */
894SdlWindow::SdlWindow(SDL_Window* parent, const SDL_Rect& rect, bool transparent, bool tooltip)
895 : _initialW(rect.w), _initialH(rect.h)
896{
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,
902 true);
903 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_FOCUSABLE_BOOLEAN, false);
904#if defined(SDL_PROP_WINDOW_CREATE_CONSTRAIN_POPUP_BOOLEAN)
905 /* The server places RemoteApp popups; a moved one would map clicks to other points. */
906 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_CONSTRAIN_POPUP_BOOLEAN, false);
907#endif
908 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_BORDERLESS_BOOLEAN, true);
909 /* Transparent so menus' genuine per-pixel alpha (corners/shadow) isn't rendered black. */
910 if (transparent)
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);
916 /* Hidden until first paint, like the app constructor. */
917 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIDDEN_BOOLEAN, true);
918
919 _window = SDL_CreateWindowWithProperties(props);
920 SDL_DestroyProperties(props);
921 if (_window)
922 {
923 std::ignore = SDL_SyncWindow(_window);
924 _renderer = SDL_CreateRenderer(_window, nullptr);
925 _displayID = SDL_GetDisplayForWindow(_window);
926 }
927}
928
929SdlWindow SdlWindow::createPopup(SDL_Window* parent, const SDL_Rect& rect, bool transparent,
930 bool tooltip)
931{
932 return SdlWindow{ parent, rect, transparent, tooltip };
933}
934
935static SDL_Window* createDummy(SDL_DisplayID id)
936{
937 const auto x = SDL_WINDOWPOS_CENTERED_DISPLAY(id);
938 const auto y = SDL_WINDOWPOS_CENTERED_DISPLAY(id);
939 const int w = 64;
940 const int h = 64;
941
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);
950
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);
955
956 auto window = SDL_CreateWindowWithProperties(props);
957 SDL_DestroyProperties(props);
958
959 /* Workaround: we need to properly position the window on the correct monitor
960 * before going fullscreen. Otherwise we will get the primary monitor details.
961 */
962 if (window)
963 {
964 SDL_Rect rect = {};
965 std::ignore = SDL_GetDisplayBounds(id, &rect);
966 std::ignore = SDL_SetWindowPosition(window, rect.x, rect.y);
967 std::ignore = SDL_SetWindowFullscreen(window, true);
968 }
969 return window;
970}
971
972rdpMonitor SdlWindow::query(SDL_DisplayID id, bool forceAsPrimary)
973{
974 if (sdl::utils::isWaylandDriver() || sdl::utils::isX11Driver())
975 return queryDisplay(id, forceAsPrimary);
976
977 std::unique_ptr<SDL_Window, void (*)(SDL_Window*)> window(createDummy(id), SDL_DestroyWindow);
978 if (!window)
979 return {};
980
981 std::unique_ptr<SDL_Renderer, void (*)(SDL_Renderer*)> renderer(
982 SDL_CreateRenderer(window.get(), nullptr), SDL_DestroyRenderer);
983
984 if (!SDL_SyncWindow(window.get()))
985 return {};
986
987 SDL_Event event{};
988 while (SDL_PollEvent(&event))
989 ;
990
991 return query(window.get(), id, forceAsPrimary);
992}
993
994SDL_Rect SdlWindow::rect(SDL_DisplayID id, bool forceAsPrimary)
995{
996 std::unique_ptr<SDL_Window, void (*)(SDL_Window*)> window(createDummy(id), SDL_DestroyWindow);
997 if (!window)
998 return {};
999
1000 std::unique_ptr<SDL_Renderer, void (*)(SDL_Renderer*)> renderer(
1001 SDL_CreateRenderer(window.get(), nullptr), SDL_DestroyRenderer);
1002
1003 if (!SDL_SyncWindow(window.get()))
1004 return {};
1005
1006 SDL_Event event{};
1007 while (SDL_PollEvent(&event))
1008 ;
1009
1010 return rect(window.get(), forceAsPrimary);
1011}
1012
1013bool SdlWindow::tryFallback(bool isFullscreen)
1014{
1015 /* If we define a custom env variable to use the wlroots hack
1016 * then enable/disable according to this setting only.
1017 */
1018 const auto wlroots_hack = SDL_getenv("FREERDP_WLROOTS_HACK");
1019 if (wlroots_hack != nullptr)
1020 {
1021 const auto enabled = strcmp(wlroots_hack, "0") != 0;
1022 if (strcmp(wlroots_hack, "force") == 0)
1023 isFullscreen = true;
1024 return enabled && isFullscreen;
1025 }
1026
1027 const auto platform = SDL_GetPlatform();
1028 if ((platform == nullptr) || (strcmp(platform, "Linux") != 0))
1029 return false;
1030
1031 const auto driver = SDL_GetCurrentVideoDriver();
1032 if ((driver == nullptr) || (strcmp(driver, "wayland") != 0))
1033 return false;
1034
1035 /* Check XDG_SESSION_DESKTOP and XDG_CURRENT_DESKTOP for wlroots-based
1036 * compositors. The original check only matched Sway, but other wlroots
1037 * compositors (Hyprland, river, etc.) have the same dummy-window sizing
1038 * behavior where hidden/unmapped windows return 64x64 instead of the
1039 * display size. Use strstr for substring matching since XDG_CURRENT_DESKTOP
1040 * can be a colon-separated list (e.g. "sway:wlroots", "Hyprland").
1041 */
1042 auto isWlrootsCompositor = [](const char* value) -> bool
1043 {
1044 if (!value)
1045 return false;
1046 if (strstr(value, "sway") || strstr(value, "Sway") || strstr(value, "Hyprland") ||
1047 strstr(value, "hyprland") || strstr(value, "river") || strstr(value, "wlroots"))
1048 return true;
1049 return false;
1050 };
1051
1052 const auto xdg_session = SDL_getenv("XDG_SESSION_DESKTOP");
1053 if (isWlrootsCompositor(xdg_session))
1054 return isFullscreen;
1055
1056 const auto xdg_desktop = SDL_getenv("XDG_CURRENT_DESKTOP");
1057 if (isWlrootsCompositor(xdg_desktop))
1058 return isFullscreen;
1059
1060 return false;
1061}
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)