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
27#include <freerdp/utils/string.h>
28
29SdlWindow::SdlWindow(SDL_DisplayID id, const std::string& title, const SDL_Rect& rect,
30 [[maybe_unused]] Uint32 flags)
31 : _initialW(rect.w), _initialH(rect.h), _displayID(id)
32{
33 float pd = SDL_GetDisplayContentScale(id);
34 if (pd <= 0.0f)
35 pd = 1.0f;
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));
38
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);
46
47 if (flags & SDL_WINDOW_HIGH_PIXEL_DENSITY)
48 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIGH_PIXEL_DENSITY_BOOLEAN, true);
49
50 if (flags & SDL_WINDOW_FULLSCREEN)
51 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_FULLSCREEN_BOOLEAN, true);
52
53 if (flags & SDL_WINDOW_BORDERLESS)
54 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_BORDERLESS_BOOLEAN, true);
55
56 if (flags & SDL_WINDOW_TRANSPARENT)
57 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_TRANSPARENT_BOOLEAN, true);
58
59 /* RAIL windows are created hidden until first paint to avoid black flash. */
60 if (flags & SDL_WINDOW_HIDDEN)
61 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIDDEN_BOOLEAN, true);
62
63 _window = SDL_CreateWindowWithProperties(props);
64 SDL_DestroyProperties(props);
65 SDL_SetHint(SDL_HINT_APP_NAME, "");
66 std::ignore = SDL_SyncWindow(_window);
67
68 _renderer = SDL_CreateRenderer(_window, nullptr);
69
70 std::ignore = resizeToScale();
71
72 _monitor = query(_window, id, true);
73}
74
75SdlWindow::SdlWindow(SdlWindow&& other) noexcept
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)
81{
82 other._window = nullptr;
83 other._renderer = nullptr;
84 other._renderTarget = nullptr;
85 other._gdiTexture = nullptr;
86}
87
88SdlWindow::~SdlWindow()
89{
90 if (_gdiTexture)
91 SDL_DestroyTexture(_gdiTexture);
92 if (_renderTarget)
93 SDL_DestroyTexture(_renderTarget);
94 if (_renderer)
95 SDL_DestroyRenderer(_renderer);
96 if (_window)
97 SDL_DestroyWindow(_window);
98}
99
100SDL_WindowID SdlWindow::id() const
101{
102 if (!_window)
103 return 0;
104 return SDL_GetWindowID(_window);
105}
106
107SDL_DisplayID SdlWindow::displayIndex() const
108{
109 if (!_window)
110 return 0;
111 return SDL_GetDisplayForWindow(_window);
112}
113
114SDL_Rect SdlWindow::rect() const
115{
116 return rect(_window);
117}
118
119SDL_Rect SdlWindow::bounds() const
120{
121 SDL_Rect rect = {};
122 if (_window)
123 {
124 if (!SDL_GetWindowPosition(_window, &rect.x, &rect.y))
125 return {};
126 if (!SDL_GetWindowSize(_window, &rect.w, &rect.h))
127 return {};
128 }
129 return rect;
130}
131
132SDL_Window* SdlWindow::window() const
133{
134 return _window;
135}
136
137SDL_Renderer* SdlWindow::renderer() const
138{
139 return _renderer;
140}
141
142Sint32 SdlWindow::offsetX() const
143{
144 return _offset_x;
145}
146
147void SdlWindow::setOffsetX(Sint32 x)
148{
149 _offset_x = x;
150}
151
152void SdlWindow::setOffsetY(Sint32 y)
153{
154 _offset_y = y;
155}
156
157Sint32 SdlWindow::offsetY() const
158{
159 return _offset_y;
160}
161
162rdpMonitor SdlWindow::monitor(bool isPrimary) const
163{
164 auto m = _monitor;
165 if (isPrimary)
166 {
167 m.x = 0;
168 m.y = 0;
169 }
170 return m;
171}
172
173void SdlWindow::setMonitor(rdpMonitor monitor)
174{
175 _monitor = monitor;
176}
177
178float SdlWindow::scale() const
179{
180 return SDL_GetWindowDisplayScale(_window);
181}
182
183SDL_DisplayOrientation SdlWindow::orientation() const
184{
185 const auto did = displayIndex();
186 return SDL_GetCurrentDisplayOrientation(did);
187}
188
189bool SdlWindow::grabKeyboard(bool enable)
190{
191 if (!_window)
192 return false;
193 SDL_SetWindowKeyboardGrab(_window, enable);
194 return true;
195}
196
197bool SdlWindow::grabMouse(bool enable)
198{
199 if (!_window)
200 return false;
201 SDL_SetWindowMouseGrab(_window, enable);
202 return true;
203}
204
205void SdlWindow::setBordered(bool bordered)
206{
207 if (_window)
208 SDL_SetWindowBordered(_window, bordered);
209 std::ignore = SDL_SyncWindow(_window);
210}
211
212void SdlWindow::raise()
213{
214 SDL_RaiseWindow(_window);
215 std::ignore = SDL_SyncWindow(_window);
216}
217
218void SdlWindow::resizeable(bool use)
219{
220 SDL_SetWindowResizable(_window, use);
221 std::ignore = SDL_SyncWindow(_window);
222}
223
224void SdlWindow::fullscreen(bool enter, bool forceOriginalDisplay)
225{
226 if (enter && forceOriginalDisplay && _displayID != 0)
227 {
228 /* Move the window to the desired display. We should not wait
229 * for the window to be moved, because some backends can refuse
230 * the move. The intent of moving the window is enough for SDL
231 * to decide which display will be used for fullscreen. */
232 SDL_Rect rect = {};
233 std::ignore = SDL_GetDisplayBounds(_displayID, &rect);
234 std::ignore = SDL_SetWindowPosition(_window, rect.x, rect.y);
235 }
236 std::ignore = SDL_SetWindowFullscreen(_window, enter);
237 std::ignore = SDL_SyncWindow(_window);
238}
239
240void SdlWindow::minimize()
241{
242 SDL_MinimizeWindow(_window);
243 std::ignore = SDL_SyncWindow(_window);
244}
245
246bool SdlWindow::resizeToScale()
247{
248 if (!_window || _initialW <= 0 || _initialH <= 0)
249 return false;
250 if ((SDL_GetWindowFlags(_window) & SDL_WINDOW_FULLSCREEN) != 0)
251 return true;
252
253 float pd = SDL_GetWindowPixelDensity(_window);
254 if (pd <= 0.0f)
255 pd = 1.0f;
256
257 const int targetW = static_cast<int>(std::ceil(static_cast<float>(_initialW) / pd));
258 const int targetH = static_cast<int>(std::ceil(static_cast<float>(_initialH) / pd));
259
260 int curW = 0;
261 int curH = 0;
262 if (!SDL_GetWindowSize(_window, &curW, &curH))
263 return false;
264
265 if (curW == targetW && curH == targetH)
266 return true;
267
268 return resize({ targetW, targetH });
269}
270
271bool SdlWindow::resize(const SDL_Point& size)
272{
273 return SDL_SetWindowSize(_window, size.x, size.y);
274}
275
276void SdlWindow::ensureRenderTarget()
277{
278 if (!_renderer)
279 return;
280
281 int w = 0;
282 int h = 0;
283 SDL_GetWindowSizeInPixels(_window, &w, &h);
284 if (w <= 0 || h <= 0)
285 return;
286
287 /* Recreate if missing or if window size changed */
288 if (_renderTarget)
289 {
290 float tw = 0;
291 float th = 0;
292 if (!SDL_GetTextureSize(_renderTarget, &tw, &th))
293 return;
294 if (static_cast<int>(tw) == w && static_cast<int>(th) == h)
295 return;
296 SDL_DestroyTexture(_renderTarget);
297 }
298
299 _renderTarget =
300 SDL_CreateTexture(_renderer, SDL_PIXELFORMAT_BGRA32, SDL_TEXTUREACCESS_TARGET, w, h);
301 if (!_renderTarget)
302 {
303 SDL_LogError(SDL_LOG_CATEGORY_RENDER, "SDL_CreateTexture (render target): %s",
304 SDL_GetError());
305 return;
306 }
307 /* Verbatim, never blended: a transparent window's alpha<0xFF regions would render black. */
308 std::ignore = SDL_SetTextureBlendMode(_renderTarget, SDL_BLENDMODE_NONE);
309
310 /* Clear once: transparent so windows never flash black before painting. */
311 if (SDL_SetRenderTarget(_renderer, _renderTarget))
312 {
313 std::ignore = SDL_SetRenderDrawColor(_renderer, 0x00, 0x00, 0x00, 0x00);
314 std::ignore = SDL_RenderClear(_renderer);
315 }
316}
317
318bool SdlWindow::drawRect(SDL_Surface* surface, SDL_Point offset, const SDL_Rect& srcRect)
319{
320 WINPR_ASSERT(surface);
321 SDL_Rect dstRect = { offset.x + srcRect.x, offset.y + srcRect.y, srcRect.w, srcRect.h };
322 return blit(surface, srcRect, dstRect);
323}
324
325bool SdlWindow::drawRects(SDL_Surface* surface, SDL_Point offset,
326 const std::vector<SDL_Rect>& rects)
327{
328 if (rects.empty())
329 {
330 return drawRect(surface, offset, { 0, 0, surface->w, surface->h });
331 }
332 for (auto& srcRect : rects)
333 {
334 if (!drawRect(surface, offset, srcRect))
335 return false;
336 }
337 return true;
338}
339
340bool SdlWindow::drawScaledRect(SDL_Surface* surface, const SDL_FPoint& scale,
341 const SDL_Rect& srcRect)
342{
343 SDL_Rect dstRect = {};
344 float ix = 0.0f;
345 float iy = 0.0f;
346 const auto modx = std::modf(static_cast<float>(srcRect.x) * scale.x, &ix);
347 const auto mody = std::modf(static_cast<float>(srcRect.y) * scale.y, &iy);
348 auto sw = std::ceil(static_cast<float>(srcRect.w) * scale.x) + std::ceil(modx);
349 auto sh = std::ceil(static_cast<float>(srcRect.h) * scale.y) + std::ceil(mody);
350 dstRect.x = static_cast<Sint32>(ix);
351 dstRect.w = static_cast<Sint32>(sw);
352 dstRect.y = static_cast<Sint32>(iy);
353 dstRect.h = static_cast<Sint32>(sh);
354 return blit(surface, srcRect, dstRect);
355}
356
357bool SdlWindow::drawScaledRects(SDL_Surface* surface, const SDL_FPoint& scale,
358 const std::vector<SDL_Rect>& rects)
359{
360 if (rects.empty())
361 {
362 return drawScaledRect(surface, scale, { 0, 0, surface->w, surface->h });
363 }
364 for (const auto& srcRect : rects)
365 {
366 if (!drawScaledRect(surface, scale, srcRect))
367 return false;
368 }
369 return true;
370}
371
372bool SdlWindow::fill(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
373{
374 if (_renderer)
375 {
376 ensureRenderTarget();
377 if (!SDL_SetRenderTarget(_renderer, _renderTarget))
378 return false;
379 if (!SDL_SetRenderDrawColor(_renderer, r, g, b, a))
380 return false;
381 return SDL_RenderClear(_renderer);
382 }
383 return fill(_window, r, g, b, a);
384}
385
386bool SdlWindow::fill(SDL_Window* window, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
387{
388 auto surface = SDL_GetWindowSurface(window);
389 if (!surface)
390 return false;
391 SDL_Rect rect = { 0, 0, surface->w, surface->h };
392 auto color = SDL_MapSurfaceRGBA(surface, r, g, b, a);
393
394 return SDL_FillSurfaceRect(surface, &rect, color);
395}
396
397rdpMonitor SdlWindow::query(SDL_Window* window, SDL_DisplayID id, bool forceAsPrimary)
398{
399 if (!window)
400 return {};
401
402 const auto& r = rect(window, forceAsPrimary);
403 const float factor = SDL_GetWindowDisplayScale(window);
404 const float dpi = std::roundf(factor * 100.0f);
405
406 WINPR_ASSERT(r.w > 0);
407 WINPR_ASSERT(r.h > 0);
408
409 const auto primary = SDL_GetPrimaryDisplay();
410 const auto orientation = SDL_GetCurrentDisplayOrientation(id);
411 const auto rdp_orientation = sdl::utils::orientaion_to_rdp(orientation);
412
413 rdpMonitor monitor{};
414 monitor.orig_screen = id;
415 monitor.x = r.x;
416 monitor.y = r.y;
417 monitor.width = r.w;
418 monitor.height = r.h;
419 monitor.is_primary = forceAsPrimary || (id == primary);
420 monitor.attributes.desktopScaleFactor = static_cast<UINT32>(dpi);
421 monitor.attributes.deviceScaleFactor = 100;
422 monitor.attributes.orientation = rdp_orientation;
423 monitor.attributes.physicalWidth = WINPR_ASSERTING_INT_CAST(uint32_t, r.w);
424 monitor.attributes.physicalHeight = WINPR_ASSERTING_INT_CAST(uint32_t, r.h);
425
426 const auto cat = SDL_LOG_CATEGORY_APPLICATION;
427 SDL_LogDebug(cat, "monitor.orig_screen %" PRIu32, monitor.orig_screen);
428 SDL_LogDebug(cat, "monitor.x %" PRId32, monitor.x);
429 SDL_LogDebug(cat, "monitor.y %" PRId32, monitor.y);
430 SDL_LogDebug(cat, "monitor.width %" PRId32, monitor.width);
431 SDL_LogDebug(cat, "monitor.height %" PRId32, monitor.height);
432 SDL_LogDebug(cat, "monitor.is_primary %" PRIu32, monitor.is_primary);
433 SDL_LogDebug(cat, "monitor.attributes.desktopScaleFactor %" PRIu32,
434 monitor.attributes.desktopScaleFactor);
435 SDL_LogDebug(cat, "monitor.attributes.deviceScaleFactor %" PRIu32,
436 monitor.attributes.deviceScaleFactor);
437 SDL_LogDebug(cat, "monitor.attributes.orientation %s",
438 freerdp_desktop_rotation_flags_to_string(monitor.attributes.orientation));
439 SDL_LogDebug(cat, "monitor.attributes.physicalWidth %" PRIu32,
440 monitor.attributes.physicalWidth);
441 SDL_LogDebug(cat, "monitor.attributes.physicalHeight %" PRIu32,
442 monitor.attributes.physicalHeight);
443 return monitor;
444}
445
446SDL_Rect SdlWindow::rect(SDL_Window* window, bool forceAsPrimary)
447{
448 SDL_Rect rect = {};
449 if (!window)
450 return {};
451
452 if (!forceAsPrimary)
453 {
454 if (!SDL_GetWindowPosition(window, &rect.x, &rect.y))
455 return {};
456 }
457
458 if (!SDL_GetWindowSizeInPixels(window, &rect.w, &rect.h))
459 return {};
460
461 const auto flags = SDL_GetWindowFlags(window);
462 const auto mask = SDL_WINDOW_FULLSCREEN;
463 const auto fs = (flags & mask) == mask;
464 if (tryFallback(fs))
465 {
466 /* On wlroots compositors (Sway, river, etc.), windows that are hidden/unmapped
467 * don't get their actual display dimensions. The dummy window returns its creation size
468 * (64x64) instead of the display size. This causes validation errors since we require >=
469 * 200px. Workaround: If we got dimensions that are too small, query the display directly.
470 */
471
472 const auto displayID = SDL_GetDisplayForWindow(window);
473 SDL_Rect displayBounds = {};
474 if (SDL_GetDisplayBounds(displayID, &displayBounds))
475 {
476 if (forceAsPrimary)
477 {
478 rect.x = 0;
479 rect.y = 0;
480 }
481 rect.w = displayBounds.w;
482 rect.h = displayBounds.h;
483
484 const float contentScale = SDL_GetDisplayContentScale(displayID);
485 if (contentScale > 1.0f)
486 {
487 const auto fw = static_cast<float>(rect.w);
488 const auto fh = static_cast<float>(rect.h);
489 rect.w = static_cast<int>(std::roundf(fw * contentScale));
490 rect.h = static_cast<int>(std::roundf(fh * contentScale));
491 }
492 }
493 }
494
495 return rect;
496}
497
498SdlWindow::HighDPIMode SdlWindow::isHighDPIWindowsMode(SDL_Window* window)
499{
500 if (!window)
501 return MODE_INVALID;
502
503 const auto id = SDL_GetDisplayForWindow(window);
504 if (id == 0)
505 return MODE_INVALID;
506
507 const auto cs = SDL_GetDisplayContentScale(id);
508 const auto ds = SDL_GetWindowDisplayScale(window);
509 const auto pd = SDL_GetWindowPixelDensity(window);
510
511 /* mac os x style, but no HighDPI display */
512 if ((cs == 1.0f) && (ds == 1.0f) && (pd == 1.0f))
513 return MODE_NONE;
514
515 /* mac os x style HighDPI */
516 if ((cs == 1.0f) && (ds > 1.0f) && (pd > 1.0f))
517 return MODE_MACOS;
518
519 /* rest is windows style */
520 return MODE_WINDOWS;
521}
522
523/* Lazily create or recreate the persistent streaming GDI texture to match `surface`. */
524bool SdlWindow::ensureGdiTexture(SDL_Surface* surface)
525{
526 if (_gdiTexture && (_gdiTextureW == surface->w) && (_gdiTextureH == surface->h))
527 return true;
528 if (_gdiTexture)
529 SDL_DestroyTexture(_gdiTexture);
530 _gdiTexture = SDL_CreateTexture(_renderer, surface->format, SDL_TEXTUREACCESS_STREAMING,
531 surface->w, surface->h);
532 if (!_gdiTexture)
533 {
534 SDL_LogError(SDL_LOG_CATEGORY_RENDER, "SDL_CreateTexture: %s", SDL_GetError());
535 return false;
536 }
537 std::ignore = SDL_SetTextureBlendMode(_gdiTexture, SDL_BLENDMODE_NONE);
538 _gdiTextureW = surface->w;
539 _gdiTextureH = surface->h;
540 return true;
541}
542
543bool SdlWindow::blit(SDL_Surface* surface, const SDL_Rect& srcRect, SDL_Rect& dstRect)
544{
545 if (!_renderer || !surface)
546 return false;
547
548 ensureRenderTarget();
549
550 if (!ensureGdiTexture(surface))
551 return false;
552
553 /* Upload only the dirty region */
554 const auto* details = SDL_GetPixelFormatDetails(surface->format);
555 const int bpp = details ? details->bytes_per_pixel : 4;
556 const auto* pixels = static_cast<const uint8_t*>(surface->pixels) +
557 (1ll * srcRect.y * surface->pitch) + (1ll * srcRect.x * bpp);
558 if (!SDL_UpdateTexture(_gdiTexture, &srcRect, pixels, surface->pitch))
559 {
560 SDL_LogError(SDL_LOG_CATEGORY_RENDER, "SDL_UpdateTexture: %s", SDL_GetError());
561 return false;
562 }
563
564 /* Render onto persistent render target to accumulate dirty rects */
565 if (!SDL_SetRenderTarget(_renderer, _renderTarget))
566 return false;
567
568 SDL_FRect fsrc = { static_cast<float>(srcRect.x), static_cast<float>(srcRect.y),
569 static_cast<float>(srcRect.w), static_cast<float>(srcRect.h) };
570 SDL_FRect fdst = { static_cast<float>(dstRect.x), static_cast<float>(dstRect.y),
571 static_cast<float>(dstRect.w), static_cast<float>(dstRect.h) };
572 if (!SDL_RenderTexture(_renderer, _gdiTexture, &fsrc, &fdst))
573 {
574 SDL_LogError(SDL_LOG_CATEGORY_RENDER, "SDL_RenderTexture: %s", SDL_GetError());
575 return false;
576 }
577 return true;
578}
579
580void SdlWindow::updateSurface()
581{
582 if (!_renderer)
583 return;
584
585 ensureRenderTarget();
586
587 /* Copy accumulated render target to screen and present */
588 if (!SDL_SetRenderTarget(_renderer, nullptr))
589 return;
590 if (!SDL_RenderTexture(_renderer, _renderTarget, nullptr, nullptr))
591 return;
592 if (!SDL_RenderPresent(_renderer))
593 return;
594}
595
596bool SdlWindow::paintResizeFrame(SDL_Surface* surface, SDL_Point off, bool contentChanged,
597 const SDL_Rect& inset, bool fillRevealed, bool dashedBorder)
598{
599 if (!_renderer || !surface)
600 return false;
601 ensureRenderTarget();
602
603 const int prevW = _gdiTextureW;
604 const int prevH = _gdiTextureH;
605 if (!ensureGdiTexture(surface))
606 return false;
607 /* A recreated texture is empty, so upload even when the content did not change. */
608 const bool recreated = (_gdiTextureW != prevW) || (_gdiTextureH != prevH);
609 if ((contentChanged || recreated) &&
610 !SDL_UpdateTexture(_gdiTexture, nullptr, surface->pixels, surface->pitch))
611 return false;
612
613 if (!SDL_SetRenderTarget(_renderer, _renderTarget))
614 return false;
615
616 int ww = 0;
617 int wh = 0;
618 SDL_GetWindowSizeInPixels(_window, &ww, &wh);
619 /* The visible frame; the ring outside it (the resize band) stays fully transparent. */
620 const SDL_FRect frame = { static_cast<float>(inset.x), static_cast<float>(inset.y),
621 static_cast<float>(ww - inset.x - inset.w),
622 static_cast<float>(wh - inset.y - inset.h) };
623
624 /* Translucent fill in revealed area awaiting server frame. */
625 constexpr Uint8 kFillAlpha = 0x80;
626 std::ignore = SDL_SetRenderDrawBlendMode(_renderer, SDL_BLENDMODE_NONE);
627 std::ignore = SDL_SetRenderDrawColor(_renderer, 0, 0, 0, 0);
628 std::ignore = SDL_RenderClear(_renderer);
629 if (fillRevealed)
630 {
631 std::ignore = SDL_SetRenderDrawColor(_renderer, 0x2B, 0x2B, 0x2B, kFillAlpha);
632 std::ignore = SDL_RenderFillRect(_renderer, &frame);
633 }
634 SDL_FRect fdst = { static_cast<float>(off.x), static_cast<float>(off.y),
635 static_cast<float>(surface->w), static_cast<float>(surface->h) };
636 /* Anchored frame clipped to visible bounds. */
637 const SDL_Rect clip = { inset.x, inset.y, ww - inset.x - inset.w, wh - inset.y - inset.h };
638 std::ignore = SDL_SetRenderClipRect(_renderer, &clip);
639 std::ignore = SDL_RenderTexture(_renderer, _gdiTexture, nullptr, &fdst);
640 std::ignore = SDL_SetRenderClipRect(_renderer, nullptr);
641
642 /* Dashed border indicating pending resize target. */
643 if (dashedBorder)
644 {
645 std::ignore = SDL_SetRenderDrawColor(_renderer, 0xC8, 0xC8, 0xC8, 0xFF);
646 const float dash = 8.0F;
647 const float gap = 5.0F;
648 const float fx2 = frame.x + frame.w;
649 const float fy2 = frame.y + frame.h;
650 const float lo = frame.y + 0.5F;
651 const float by = fy2 - 0.5F;
652 const float lx = frame.x + 0.5F;
653 const float rx = fx2 - 0.5F;
654 const float step = dash + gap;
655 const auto steps = [step](float len)
656 { return (len <= 0.0F) ? 0 : static_cast<int>(std::ceil(len / step)); };
657 for (int i = 0; i < steps(fx2 - frame.x); i++)
658 {
659 const float x = frame.x + (static_cast<float>(i) * step);
660 const float x2 = (x + dash < fx2) ? (x + dash) : rx;
661 std::ignore = SDL_RenderLine(_renderer, x, lo, x2, lo);
662 std::ignore = SDL_RenderLine(_renderer, x, by, x2, by);
663 }
664 for (int i = 0; i < steps(fy2 - frame.y); i++)
665 {
666 const float y = frame.y + (static_cast<float>(i) * step);
667 const float y2 = (y + dash < fy2) ? (y + dash) : by;
668 std::ignore = SDL_RenderLine(_renderer, lx, y, lx, y2);
669 std::ignore = SDL_RenderLine(_renderer, rx, y, rx, y2);
670 }
671 }
672
673 if (!SDL_SetRenderTarget(_renderer, nullptr))
674 return false;
675 std::ignore = SDL_RenderTexture(_renderer, _renderTarget, nullptr, nullptr);
676 std::ignore = SDL_RenderPresent(_renderer);
677 return true;
678}
679
680SdlWindow SdlWindow::create(SDL_DisplayID id, const std::string& title, Uint32 flags, Uint32 width,
681 Uint32 height)
682{
683 flags |= SDL_WINDOW_HIGH_PIXEL_DENSITY;
684
685 SDL_Rect rect = { static_cast<int>(SDL_WINDOWPOS_CENTERED_DISPLAY(id)),
686 static_cast<int>(SDL_WINDOWPOS_CENTERED_DISPLAY(id)), static_cast<int>(width),
687 static_cast<int>(height) };
688
689 if ((flags & SDL_WINDOW_FULLSCREEN) != 0)
690 {
691 std::ignore = SDL_GetDisplayBounds(id, &rect);
692 }
693
694 SdlWindow window{ id, title, rect, flags };
695
696 if ((flags & SDL_WINDOW_FULLSCREEN) != 0)
697 {
698 window.setOffsetX(rect.x);
699 window.setOffsetY(rect.y);
700 }
701
702 return window;
703}
704
705SdlWindow SdlWindow::create(SDL_DisplayID id, const std::string& title, Uint32 flags,
706 const SDL_Rect& rect)
707{
708 return SdlWindow{ id, title, rect, flags };
709}
710
711/* Popup constructor: positioned relative to the parent origin. */
712SdlWindow::SdlWindow(SDL_Window* parent, const SDL_Rect& rect, bool transparent, bool tooltip)
713 : _initialW(rect.w), _initialH(rect.h)
714{
715 auto props = SDL_CreateProperties();
716 SDL_SetPointerProperty(props, SDL_PROP_WINDOW_CREATE_PARENT_POINTER, parent);
717 SDL_SetBooleanProperty(props,
718 tooltip ? SDL_PROP_WINDOW_CREATE_TOOLTIP_BOOLEAN
719 : SDL_PROP_WINDOW_CREATE_MENU_BOOLEAN,
720 true);
721 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_FOCUSABLE_BOOLEAN, false);
722 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_BORDERLESS_BOOLEAN, true);
723 /* Transparent so menus' genuine per-pixel alpha (corners/shadow) isn't rendered black. */
724 if (transparent)
725 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_TRANSPARENT_BOOLEAN, true);
726 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, rect.x);
727 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, rect.y);
728 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, rect.w);
729 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, rect.h);
730 /* Hidden until first paint, like the app constructor. */
731 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIDDEN_BOOLEAN, true);
732
733 _window = SDL_CreateWindowWithProperties(props);
734 SDL_DestroyProperties(props);
735 if (_window)
736 {
737 std::ignore = SDL_SyncWindow(_window);
738 _renderer = SDL_CreateRenderer(_window, nullptr);
739 _displayID = SDL_GetDisplayForWindow(_window);
740 }
741}
742
743SdlWindow SdlWindow::createPopup(SDL_Window* parent, const SDL_Rect& rect, bool transparent,
744 bool tooltip)
745{
746 return SdlWindow{ parent, rect, transparent, tooltip };
747}
748
749static SDL_Window* createDummy(SDL_DisplayID id)
750{
751 const auto x = SDL_WINDOWPOS_CENTERED_DISPLAY(id);
752 const auto y = SDL_WINDOWPOS_CENTERED_DISPLAY(id);
753 const int w = 64;
754 const int h = 64;
755
756 auto props = SDL_CreateProperties();
757 std::stringstream ss;
758 ss << "SdlWindow::query(" << id << ")";
759 SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, ss.str().c_str());
760 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, x);
761 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, y);
762 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, w);
763 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, h);
764
765 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIGH_PIXEL_DENSITY_BOOLEAN, true);
766 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_FULLSCREEN_BOOLEAN, false);
767 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_BORDERLESS_BOOLEAN, true);
768 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIDDEN_BOOLEAN, false);
769
770 auto window = SDL_CreateWindowWithProperties(props);
771 SDL_DestroyProperties(props);
772
773 /* Workaround: we need to properly position the window on the correct monitor
774 * before going fullscreen. Otherwise we will get the primary monitor details.
775 */
776 if (window)
777 {
778 SDL_Rect rect = {};
779 std::ignore = SDL_GetDisplayBounds(id, &rect);
780 std::ignore = SDL_SetWindowPosition(window, rect.x, rect.y);
781 std::ignore = SDL_SetWindowFullscreen(window, true);
782 }
783 return window;
784}
785
786rdpMonitor SdlWindow::query(SDL_DisplayID id, bool forceAsPrimary)
787{
788 std::unique_ptr<SDL_Window, void (*)(SDL_Window*)> window(createDummy(id), SDL_DestroyWindow);
789 if (!window)
790 return {};
791
792 std::unique_ptr<SDL_Renderer, void (*)(SDL_Renderer*)> renderer(
793 SDL_CreateRenderer(window.get(), nullptr), SDL_DestroyRenderer);
794
795 if (!SDL_SyncWindow(window.get()))
796 return {};
797
798 SDL_Event event{};
799 while (SDL_PollEvent(&event))
800 ;
801
802 return query(window.get(), id, forceAsPrimary);
803}
804
805SDL_Rect SdlWindow::rect(SDL_DisplayID id, bool forceAsPrimary)
806{
807 std::unique_ptr<SDL_Window, void (*)(SDL_Window*)> window(createDummy(id), SDL_DestroyWindow);
808 if (!window)
809 return {};
810
811 std::unique_ptr<SDL_Renderer, void (*)(SDL_Renderer*)> renderer(
812 SDL_CreateRenderer(window.get(), nullptr), SDL_DestroyRenderer);
813
814 if (!SDL_SyncWindow(window.get()))
815 return {};
816
817 SDL_Event event{};
818 while (SDL_PollEvent(&event))
819 ;
820
821 return rect(window.get(), forceAsPrimary);
822}
823
824bool SdlWindow::tryFallback(bool isFullscreen)
825{
826 /* If we define a custom env variable to use the wlroots hack
827 * then enable/disable according to this setting only.
828 */
829 const auto wlroots_hack = SDL_getenv("FREERDP_WLROOTS_HACK");
830 if (wlroots_hack != nullptr)
831 {
832 const auto enabled = strcmp(wlroots_hack, "0") != 0;
833 if (strcmp(wlroots_hack, "force") == 0)
834 isFullscreen = true;
835 return enabled && isFullscreen;
836 }
837
838 const auto platform = SDL_GetPlatform();
839 if ((platform == nullptr) || (strcmp(platform, "Linux") != 0))
840 return false;
841
842 const auto driver = SDL_GetCurrentVideoDriver();
843 if ((driver == nullptr) || (strcmp(driver, "wayland") != 0))
844 return false;
845
846 /* Check XDG_SESSION_DESKTOP and XDG_CURRENT_DESKTOP for wlroots-based
847 * compositors. The original check only matched Sway, but other wlroots
848 * compositors (Hyprland, river, etc.) have the same dummy-window sizing
849 * behavior where hidden/unmapped windows return 64x64 instead of the
850 * display size. Use strstr for substring matching since XDG_CURRENT_DESKTOP
851 * can be a colon-separated list (e.g. "sway:wlroots", "Hyprland").
852 */
853 auto isWlrootsCompositor = [](const char* value) -> bool
854 {
855 if (!value)
856 return false;
857 if (strstr(value, "sway") || strstr(value, "Sway") || strstr(value, "Hyprland") ||
858 strstr(value, "hyprland") || strstr(value, "river") || strstr(value, "wlroots"))
859 return true;
860 return false;
861 };
862
863 const auto xdg_session = SDL_getenv("XDG_SESSION_DESKTOP");
864 if (isWlrootsCompositor(xdg_session))
865 return isFullscreen;
866
867 const auto xdg_desktop = SDL_getenv("XDG_CURRENT_DESKTOP");
868 if (isWlrootsCompositor(xdg_desktop))
869 return isFullscreen;
870
871 return false;
872}
SdlWindow(const std::string &title, Sint32 startupX, Sint32 startupY, Sint32 width, Sint32 height, Uint32 flags)