25#include <freerdp/codec/color.h>
26#include <freerdp/log.h>
27#include <freerdp/window.h>
29#include "sdl_rail_platform.hpp"
30#include "sdl_rail_window.hpp"
31#include "sdl_rail_window_style.hpp"
32#include "sdl_utils.hpp"
33#include "sdl_wayland.hpp"
34#include "sdl_window.hpp"
36#define TAG CLIENT_TAG("sdl.rail.window")
39static constexpr uint64_t kAwaitServerFrameMs = 1000;
42static constexpr uint64_t kAwaitRestoreRectMs = 500;
45static constexpr size_t kMaxReported = 16;
48static constexpr uint64_t kReportConfirmMs = 1000;
50static void armDeadline(uint64_t& until, uint64_t ms)
52 until = SDL_GetTicks() + ms;
53 if (!sdl_push_update_after(WINPR_ASSERTING_INT_CAST(Uint32, ms + 1)))
54 WLog_WARN(TAG,
"SDL_AddTimer failed: %s", SDL_GetError());
58static constexpr uint64_t kDisplaySettleMs = 500;
61static constexpr uint64_t kMoveActiveMs = 250;
64static constexpr uint64_t kPlaceWaitMs = 50;
67static bool regionHasAlpha(
const uint8_t* base, uint32_t stride,
const SDL_Rect& r)
69 for (
int y = r.y; y < r.y + r.h; y++)
71 const uint8_t* row = base +
static_cast<size_t>(y) * stride;
72 for (
int x = r.x; x < r.x + r.w; x++)
73 if (row[
static_cast<size_t>(x) * 4 + 3] != 0)
79static bool regionIsBlank(
const uint8_t* base, uint32_t stride,
const SDL_Rect& r)
81 for (
int y = r.y; y < r.y + r.h; y++)
83 const auto* row =
reinterpret_cast<const uint32_t*
>(base +
static_cast<size_t>(y) * stride);
84 for (
int x = r.x; x < r.x + r.w; x++)
91SdlRailWindow::SdlRailWindow(uint64_t
id,
const SDL_Rect& rect) : _id(id), _windowRect(rect)
96SdlRailWindow::~SdlRailWindow() =
default;
98uint64_t SdlRailWindow::id()
const
103SDL_WindowID SdlRailWindow::sdlId()
const
105 return _win ? _win->id() : 0;
108SDL_Window* SdlRailWindow::window()
const
110 return _win ? _win->window() :
nullptr;
113SDL_Renderer* SdlRailWindow::renderer()
const
115 return _win ? _win->renderer() :
nullptr;
118void SdlRailWindow::updateWindowRect(
const SDL_Rect& rect)
120 std::unique_lock lock(_gfxLock);
122 if (_minState.server)
124 _awaitRestoreUntil = 0;
125 if (_localMoveActive)
128 if (!_localMoveIsResize && ((rect.w != _windowRect.w) || (rect.h != _windowRect.h)))
130 WLog_DBG(
TAG,
"size adopted mid-move id=0x%08" PRIx32
" %dx%d -> %dx%d at %d,%d",
131 static_cast<uint32_t
>(_id), _windowRect.w, _windowRect.h, rect.w, rect.h,
133 _windowRect.w = rect.w;
134 _windowRect.h = rect.h;
136 _localMoveServerPos = { rect.x, rect.y };
137 _localMoveSizeChanged =
true;
143 if (SDL_RectsEqual(&rect, &_windowRect))
149 const auto echo = std::find_if(_reported.begin(), _reported.end(),
150 [&](
const SDL_Rect& r) { return SDL_RectsEqual(&r, &rect); });
151 if (echo != _reported.end())
153 WLog_DBG(
TAG,
"late echo id=0x%08" PRIx32
" %dx%d ignored",
static_cast<uint32_t
>(_id),
155 _reported.erase(_reported.begin(), echo + 1);
160 if ((rect.w != _windowRect.w) || (rect.h != _windowRect.h))
164 _geometryDirty =
true;
167void SdlRailWindow::setLocalMoveActive(
bool active)
169 std::unique_lock lock(_gfxLock);
170 _localMoveActive = active;
173 _localMoveIsResize =
false;
174 _localMoveSizeChanged =
false;
178void SdlRailWindow::setResizeAnchor(
bool right,
bool bottom)
180 std::unique_lock lock(_gfxLock);
181 _resizeAnchorRight = right;
182 _resizeAnchorBottom = bottom;
183 _localMoveIsResize =
true;
186bool SdlRailWindow::localMoveActive()
const
188 std::unique_lock lock(_gfxLock);
189 return _localMoveActive;
192bool SdlRailWindow::localMoveSizeChanged()
const
194 std::unique_lock lock(_gfxLock);
195 return _localMoveSizeChanged;
198SDL_Point SdlRailWindow::localMoveServerPos()
const
200 std::unique_lock lock(_gfxLock);
201 return _localMoveServerPos;
204void SdlRailWindow::adoptLocalGeometry(
const SDL_Rect& rect)
207 std::unique_lock lock(_gfxLock);
209 _geomApplyPending =
false;
213 _visOffset.x += rect.x - _windowRect.x;
214 _visOffset.y += rect.y - _windowRect.y;
216 if (!SDL_RectsEqual(&rect, &_windowRect))
218 if (_reported.size() >= kMaxReported)
219 _reported.erase(_reported.begin());
220 _reported.push_back(rect);
221 armDeadline(_reportCheckAt, kReportConfirmMs);
224 _geometryDirty =
false;
226 _awaitingFrameUntil =
227 (_localMoveActive && _localMoveIsResize) ? (SDL_GetTicks() + kAwaitServerFrameMs) : 0;
228 _localMoveActive =
false;
229 _needsFullBlit =
true;
232 _gfxDamage.assign(1, SDL_Rect{ 0, 0,
static_cast<int>(_gfxW),
static_cast<int>(_gfxH) });
235SDL_Rect SdlRailWindow::windowRect()
const
237 std::unique_lock lock(_gfxLock);
241void SdlRailWindow::markDeleted()
243 std::unique_lock lock(_gfxLock);
247void SdlRailWindow::releaseWindow()
249 std::unique_lock lock(_gfxLock);
252 WLog_DBG(
TAG,
"release id=0x%08" PRIx32
" %s%s",
static_cast<uint32_t
>(_id), role(),
253 _deleted ?
"" :
" (SDL parent gone, re-created on next pass)");
255 _appIdDirty = !_appId.empty();
259 _gfxPresented =
false;
260 _needsFullBlit =
true;
261 _geometryDirty =
true;
262 _parentApplied =
false;
264 _appliedInsets = { 0, 0, 0, 0 };
265 _extentsApplied = { 0, 0, 0, 0 };
266 _lastWinW = _lastWinH = _lastGfxW = _lastGfxH = 0;
269bool SdlRailWindow::isDeleted()
const
271 std::unique_lock lock(_gfxLock);
275bool SdlRailWindow::geomApplyPending()
const
277 std::unique_lock lock(_gfxLock);
278 return _geomApplyPending;
281void SdlRailWindow::clearGeomApplyPending()
283 std::unique_lock lock(_gfxLock);
284 _geomApplyPending =
false;
287void SdlRailWindow::armLoopEnd()
289 std::unique_lock lock(_gfxLock);
290 _loopEnd.pending =
true;
293bool SdlRailWindow::loopEndPending()
const
295 std::unique_lock lock(_gfxLock);
296 return _loopEnd.pending;
299void SdlRailWindow::deferMaximize()
301 std::unique_lock lock(_gfxLock);
302 _loopEnd.maximize =
true;
305void SdlRailWindow::deferSnap(
const SDL_Rect& serverRect)
307 std::unique_lock lock(_gfxLock);
308 _loopEnd.snap =
true;
309 _loopEnd.snapRect = serverRect;
312void SdlRailWindow::clearLoopEnd()
314 std::unique_lock lock(_gfxLock);
320 std::unique_lock lock(_gfxLock);
321 const LoopEndActions actions{ _loopEnd.maximize, _loopEnd.snap, _loopEnd.snapRect };
326bool SdlRailWindow::takeWmOverride(SDL_Rect& outer)
328 std::unique_lock lock(_gfxLock);
331 outer = _wmRefusedOuter;
339static bool rectsAreDisjoint(
const std::vector<SDL_Rect>& rects)
345 const SDL_Rect* rect;
347 std::vector<Edge> edges;
348 edges.reserve(rects.size() * 2);
349 for (
const auto& r : rects)
351 if (SDL_RectEmpty(&r))
353 edges.push_back({ r.y,
true, &r });
354 edges.push_back({ r.y + r.h,
false, &r });
357 std::sort(edges.begin(), edges.end(), [](
const Edge& a,
const Edge& b)
358 { return (a.y != b.y) ? (a.y < b.y) : (!a.top && b.top); });
360 std::map<int, int> row;
361 for (
const auto& e : edges)
363 const int left = e.rect->x;
364 const int right = e.rect->x + e.rect->w;
370 const auto next = row.lower_bound(left);
371 if ((next != row.end()) && (next->first < right))
373 if ((next != row.begin()) && (std::prev(next)->second > left))
375 row.emplace_hint(next, left, right);
380void SdlRailWindow::setVisibilityRects(std::vector<SDL_Rect> rects)
384 if (!rectsAreDisjoint(rects))
387 for (
const auto& r : rects)
390 if (SDL_GetRectUnion(&extents, &r, &u))
393 rects.assign(1, extents);
395 std::unique_lock lock(_gfxLock);
396 _visRects = std::move(rects);
400void SdlRailWindow::setVisibleOffset(SDL_Point offset)
402 std::unique_lock lock(_gfxLock);
404 if (_localMoveActive)
406 if (_visOffsetSet && (offset.x == _visOffset.x) && (offset.y == _visOffset.y))
409 _visOffsetSet =
true;
413void SdlRailWindow::setMinMaxSize(SDL_Point minSize, SDL_Point maxSize)
415 std::unique_lock lock(_gfxLock);
416 _minSize = { std::max(0, minSize.x), std::max(0, minSize.y) };
417 _maxSize = { std::max(0, maxSize.x), std::max(0, maxSize.y) };
420 _geometryDirty =
true;
423void SdlRailWindow::setResizeMargins(
int left,
int top,
int right,
int bottom)
425 std::unique_lock lock(_gfxLock);
426 const SDL_Rect m = { left, top, right, bottom };
427 if (SDL_RectsEqual(&m, &_resizeMargins))
431 if (!railMaximized())
432 _geometryDirty =
true;
435SDL_Rect SdlRailWindow::resizeMargins()
const
437 std::unique_lock lock(_gfxLock);
438 return _resizeMargins;
441void SdlRailWindow::setFrameMargins(
const SDL_Rect& m)
443 std::unique_lock lock(_gfxLock);
447SDL_Rect SdlRailWindow::frameMargins()
const
449 std::unique_lock lock(_gfxLock);
450 return _frameMargins;
453bool SdlRailWindow::takeUnconfirmedReport(SDL_Rect& rect)
455 std::unique_lock lock(_gfxLock);
456 if ((_reportCheckAt == 0) || (SDL_GetTicks() < _reportCheckAt))
459 if (_reported.empty())
461 rect = _reported.back();
466void SdlRailWindow::noteDisplayChange()
468 std::unique_lock lock(_gfxLock);
469 armDeadline(_displayCheckAt, kDisplaySettleMs);
472bool SdlRailWindow::takeDisplayChange(
bool now)
474 std::unique_lock lock(_gfxLock);
475 if ((_displayCheckAt == 0) || (!now && (SDL_GetTicks() < _displayCheckAt)))
481bool SdlRailWindow::deferMaximizeUntilEcho()
483 std::unique_lock lock(_gfxLock);
484 if (_reported.empty())
486 _maximizeAfterEcho =
true;
490bool SdlRailWindow::takeMaximizeAfterEcho()
492 std::unique_lock lock(_gfxLock);
493 if (!_maximizeAfterEcho || !_reported.empty())
495 _maximizeAfterEcho =
false;
499void SdlRailWindow::setOwner(uint64_t ownerId)
501 std::unique_lock lock(_gfxLock);
505uint64_t SdlRailWindow::owner()
const
507 std::unique_lock lock(_gfxLock);
511bool SdlRailWindow::isPopup()
const
513 std::unique_lock lock(_gfxLock);
517bool SdlRailWindow::isFullscreen()
const
519 std::unique_lock lock(_gfxLock);
523bool SdlRailWindow::activatable()
const
525 std::unique_lock lock(_gfxLock);
526 return (!_isPopup || _fullscreen) && ((_exStyle & WS_EX_NOACTIVATE) == 0);
529bool SdlRailWindow::isLayered()
const
531 std::unique_lock lock(_gfxLock);
535void SdlRailWindow::setShadowAnchored(
bool anchored)
537 std::unique_lock lock(_gfxLock);
538 _shadowAnchored = anchored;
541void SdlRailWindow::setFrame(
bool frame)
543 std::unique_lock lock(_gfxLock);
547bool SdlRailWindow::isFrame()
const
549 std::unique_lock lock(_gfxLock);
553bool SdlRailWindow::styleResizable()
const
556 return _everResizable;
560static constexpr int kMinGrip = 4;
562SDL_Rect SdlRailWindow::bandMargins()
const
564 if (!_visible || _isPopup || _layered || !styleResizable() || effectivelyMaximized())
565 return { 0, 0, 0, 0 };
567 const SDL_Rect& m = _resizeMargins;
568 return { std::max(m.x, kMinGrip), std::max(m.y, kMinGrip), std::max(m.w, kMinGrip),
569 std::max(m.h, kMinGrip) };
572SDL_Rect SdlRailWindow::bandInsets()
const
575 const auto band = railPlatformCaps().band;
576 if ((band == RailBandMechanism::None) || (band == RailBandMechanism::NativeFrame))
577 return { 0, 0, 0, 0 };
578 return bandMargins();
582static SDL_Rect addInsets(
const SDL_Rect& r,
const SDL_Rect& i)
584 return { r.x - i.x, r.y - i.y, r.w + i.x + i.w, r.h + i.y + i.h };
589static SDL_Rect stripInsets(
const SDL_Rect& r,
const SDL_Rect& i)
591 return { r.x + i.x, r.y + i.y, r.w - i.x - i.w, r.h - i.y - i.h };
595bool SdlRailWindow::isFullDisplaySize()
const
598 return SDL_GetDisplayBounds(SDL_GetPrimaryDisplay(), &disp) && (_windowRect.w >= disp.w) &&
599 (_windowRect.h >= disp.h);
604SDL_Rect SdlRailWindow::targetOuterRect()
const
606 SDL_Rect r = addInsets(_windowRect, bandInsets());
607 r.x += _localShift.x;
608 r.y += _localShift.y;
613SDL_Rect SdlRailWindow::insets()
const
615 std::unique_lock lock(_gfxLock);
616 return _appliedInsets;
620SDL_Rect SdlRailWindow::outerRect()
const
622 std::unique_lock lock(_gfxLock);
623 SDL_Rect r = addInsets(_windowRect, _appliedInsets);
624 r.x += _localShift.x;
625 r.y += _localShift.y;
629SDL_Rect SdlRailWindow::serverRect(
const SDL_Rect& outer)
const
631 SDL_Rect r = stripInsets(outer, insets());
632 r.x -= _localShift.x;
633 r.y -= _localShift.y;
637void SdlRailWindow::setLocalShift(
const SDL_Point& shift)
639 std::unique_lock lock(_gfxLock);
644SDL_Point SdlRailWindow::blitOffset()
const
646 if (!effectivelyMaximized())
647 return { _appliedInsets.x, _appliedInsets.y };
649 const bool frameInSurface =
650 ((_frameMargins.x > 0) || (_frameMargins.y > 0)) &&
651 (
static_cast<int>(_gfxW) == _windowRect.w + _frameMargins.x + _frameMargins.w) &&
652 (
static_cast<int>(_gfxH) == _windowRect.h + _frameMargins.y + _frameMargins.h);
653 SDL_Point content = { 0, 0 };
655 content = { _frameMargins.x, _frameMargins.y };
656 else if (_visOffsetSet)
657 content = { _visOffset.x - _windowRect.x, _visOffset.y - _windowRect.y };
658 return { -std::clamp(content.x, 0,
static_cast<int>(_gfxW)),
659 -std::clamp(content.y, 0,
static_cast<int>(_gfxH)) };
663static Uint32 placeWaitTimer([[maybe_unused]]
void* userdata, [[maybe_unused]] SDL_TimerID timerID,
664 [[maybe_unused]] Uint32 interval)
666 if (!sdl_push_user_event(SDL_EVENT_USER_UPDATE))
667 WLog_WARN(TAG,
"could not wake the paint loop for a held popup");
673void SdlRailWindow::notePlaced()
675 std::unique_lock lock(_gfxLock);
680void SdlRailWindow::placeRequested()
682 _pendingRel = _requestedRel;
683 _placeInFlight =
false;
687bool SdlRailWindow::holdForPlacement()
691 if (SDL_GetTicks() - _requestedAt < kPlaceWaitMs)
698void SdlRailWindow::placePopup(
const SDL_Rect& vis,
const SDL_Rect& parentRect)
700 const SDL_Point rel = { vis.x - parentRect.x, vis.y - parentRect.y };
701 const bool wayland = !railPlatformCaps().supportsWindowPositions;
702 const bool moved = (rel.x != _requestedRel.x) || (rel.y != _requestedRel.y);
706 _requestedAt = SDL_GetTicks();
708 if (!SDL_SetWindowPosition(_win->window(), rel.x, rel.y))
710 WLog_VRB(
TAG,
"popup reposition unsupported id=0x%08" PRIx32
": %s",
711 static_cast<uint32_t
>(_id), SDL_GetError());
716 _needsFullBlit =
true;
717 if (!moved || !_mapped)
719 _placeInFlight =
true;
720 if (SDL_AddTimer(
static_cast<Uint32
>(kPlaceWaitMs + 1), placeWaitTimer,
nullptr) == 0)
721 WLog_WARN(
TAG,
"popup placement timer failed: %s", SDL_GetError());
725void SdlRailWindow::commitPlacement()
729 _placedRel = _pendingRel;
733bool SdlRailWindow::placedRel(SDL_Point& rel)
const
735 std::unique_lock lock(_gfxLock);
737 if (!_placedRel || (SDL_GetTicks() - _requestedAt >= kMoveActiveMs))
743SDL_Point SdlRailWindow::serverOrigin()
const
745 std::unique_lock lock(_gfxLock);
747 const SDL_Point dst = blitOffset();
748 return { _windowRect.x - dst.x, _windowRect.y - dst.y };
751void SdlRailWindow::setStyle(uint32_t style, uint32_t exStyle)
753 std::unique_lock lock(_gfxLock);
754 const bool wasTopmost = _topmost;
757 _topmost = (exStyle & WS_EX_TOPMOST) != 0;
758 if (_topmost != wasTopmost)
759 _topmostDirty =
true;
760 if (!_everResizable && ((style & (WS_THICKFRAME | WS_MAXIMIZEBOX)) != 0))
762 _everResizable =
true;
766 if (!_popupClassified)
768 const auto classification = classifySdlRailWindowStyle(style, exStyle, _ownerId != 0);
769 _isPopup = classification.popup;
770 _layered = classification.layered;
771 _overlay = classification.overlay;
772 _popupClassified =
true;
776void SdlRailWindow::setTitle(
const std::string& title)
778 std::unique_lock lock(_gfxLock);
783void SdlRailWindow::setAppId(
const std::string& appId)
785 std::unique_lock lock(_gfxLock);
787 _appIdDirty = !appId.empty();
790void SdlRailWindow::setVisible(
bool visible)
792 std::unique_lock lock(_gfxLock);
796void SdlRailWindow::setIcon(
const SdlRailIcon& icon)
798 std::unique_lock lock(_gfxLock);
803bool SdlRailWindow::create(SDL_Window* parent,
const SDL_Rect& parentRect)
808 const RailPlatformCaps& caps = railPlatformCaps();
811 const SDL_Rect vis = targetOuterRect();
812 _appliedInsets = bandInsets();
814 const bool fullscreen = _isPopup && !caps.supportsWindowPositions && isFullDisplaySize();
815 if (_isPopup && parent && !fullscreen)
818 const SDL_Rect rel = { vis.x - parentRect.x, vis.y - parentRect.y, vis.w, vis.h };
819 _requestedRel = { rel.x, rel.y };
820 _win = std::make_unique<SdlWindow>(
821 SdlWindow::createPopup(parent, rel, caps.supportsTransparentWindows, _overlay));
823 else if (_isPopup && !caps.supportsWindowPositions && !fullscreen)
826 WLog_VRB(
TAG,
"popup create deferred id=0x%08" PRIx32
": no parent yet",
827 static_cast<uint32_t
>(_id));
833 Uint32 flags = SDL_WINDOW_HIDDEN | SDL_WINDOW_HIGH_PIXEL_DENSITY;
834 if (_isPopup || (caps.band != RailBandMechanism::NativeFrame))
835 flags |= SDL_WINDOW_BORDERLESS;
836 if (caps.supportsTransparentWindows)
837 flags |= SDL_WINDOW_TRANSPARENT;
838 if ((_exStyle & WS_EX_NOACTIVATE) != 0)
839 flags |= SDL_WINDOW_NOT_FOCUSABLE;
842 const char* hint = SDL_GetHint(SDL_HINT_APP_ID);
843 const std::string previous = hint ? hint :
"";
845 SDL_SetHint(SDL_HINT_APP_ID, _appId.c_str());
846 _win = std::make_unique<SdlWindow>(
847 SdlWindow::create(SDL_GetPrimaryDisplay(), _title, flags, vis));
851 SDL_SetHint(SDL_HINT_APP_ID, previous.c_str());
853 SDL_ResetHint(SDL_HINT_APP_ID);
856 if (!_win || !_win->window() || !_win->renderer())
859 WLog_WARN(
TAG,
"create failed id=0x%08" PRIx32
" %s",
static_cast<uint32_t
>(_id), role());
862 _win->resizeable(styleResizable());
867 SDL_SetWindowFullscreen(_win->window(),
true);
868 WLog_DBG(
TAG,
"fullscreen id=0x%08" PRIx32
" %dx%d (WS_POPUP spans display)",
869 static_cast<uint32_t
>(_id), _windowRect.w, _windowRect.h);
876 railPlatformPrepareWindow(_win->window());
877 std::ignore = railPlatformSetWindowRect(_win->window(), vis);
881 "create id=0x%08" PRIx32
" sdl=%" PRIu32
" %s vis=%dx%d+%d+%d margins=L%d,T%d,R%d,B%d "
883 static_cast<uint32_t
>(_id),
static_cast<uint32_t
>(_win->id()), role(), vis.w, vis.h,
884 vis.x, vis.y, _resizeMargins.x, _resizeMargins.y, _resizeMargins.w, _resizeMargins.h,
885 caps.supportsTransparentWindows ? 1 : 0);
889bool SdlRailWindow::reconcile(SDL_Window* parent,
const SDL_Rect& parentRect)
891 std::unique_lock lock(_gfxLock);
895 const bool popupReady = !_isPopup || _hasGfx;
897 const bool unplaceable =
898 _frame || (_overlay && !railPlatformCaps().supportsWindowPositions && isFullDisplaySize());
900 const bool drawable =
901 _visible && (_windowRect.w > 0) && (_windowRect.h > 0) && popupReady && !unplaceable &&
902 (!_layered || (_hasGfx && !_visRects.empty() && (_shadowAnchored || _overlay)));
907 if (_layered && _hasGfx && !_visRects.empty() && !_shadowAnchored && !_overlay &&
908 !_shadowSuppressLogged)
910 _shadowSuppressLogged =
true;
912 "shadow-rule suppressed id=0x%08" PRIx32
" %dx%d style=0x%08" PRIx32
914 static_cast<uint32_t
>(_id), _windowRect.w, _windowRect.h, _style, _exStyle);
918 if ((SDL_GetWindowFlags(_win->window()) & SDL_WINDOW_HIDDEN) == 0)
920 WLog_DBG(
TAG,
"hide id=0x%08" PRIx32
" %s",
static_cast<uint32_t
>(_id), role());
921 SDL_HideWindow(_win->window());
922 _appIdDirty = !_appId.empty();
926 if (_isPopup && !_visible)
928 _gfxPresented =
false;
936 if (!create(parent, parentRect))
938 _geometryDirty =
false;
945 _topmostDirty =
false;
947 if (!_isPopup && !_layered)
948 std::ignore = SDL_SetWindowAlwaysOnTop(_win->window(), _topmost);
954 _win->resizeable(styleResizable());
961 if (applyServerState(_maxState,
"maximize", SDL_MaximizeWindow))
962 armDeadline(_awaitRestoreUntil, kAwaitRestoreRectMs);
964 if ((_localRestoreUntil != 0) && (SDL_GetTicks() >= _localRestoreUntil))
966 _localRestoreUntil = 0;
967 if (_minState.server && !_minState.rail && !_restoreRetried)
969 _restoreRetried =
true;
970 _restoreRetry =
true;
971 armDeadline(_localRestoreUntil, kAwaitRestoreRectMs);
973 else if (_minState.server != _minState.rail)
974 _minState.dirty =
true;
976 std::ignore = applyServerState(_minState,
"minimize", SDL_MinimizeWindow);
977 const bool restorePending = restoreRectPending();
980 const bool maxed = effectivelyMaximized();
981 const SDL_Rect band = bandInsets();
982 if ((maxed != _wasMaximized) && !_localMoveActive && !restorePending)
984 setAppliedInsets(band);
986 _geometryDirty =
true;
987 _wasMaximized = maxed;
991 if ((railPlatformCaps().band == RailBandMechanism::FrameExtents) && !restorePending &&
992 !SDL_RectsEqual(&band, &_extentsApplied) && railPlatformSetBand(_win->window(), band))
994 _extentsApplied = band;
996 const bool wmOwnsGeometry =
997 _localMoveActive || geometryFrozen() || _fullscreen || restorePending;
1000 if ((_minMaxDirty || _geometryDirty) && !wmOwnsGeometry)
1002 const SDL_Rect vis = targetOuterRect();
1003 const int bandW = band.x + band.w;
1004 const int bandH = band.y + band.h;
1006 const SDL_Rect& m = _resizeMargins;
1007 const int padW = bandW - m.x - m.w;
1008 const int padH = bandH - m.y - m.h;
1009 const auto toOuter = [](
int track,
int pad)
1010 {
return (track > 0) ? std::max(1, track + pad) : 0; };
1011 const int minW = toOuter(_minSize.x, padW);
1012 const int minH = toOuter(_minSize.y, padH);
1013 SDL_SetWindowMinimumSize(_win->window(), std::clamp(minW, 0, vis.w),
1014 std::clamp(minH, 0, vis.h));
1015 int maxW = toOuter(_maxSize.x, padW);
1016 int maxH = toOuter(_maxSize.y, padH);
1019 if (!railPlatformCaps().supportsWindowPositions &&
1020 SDL_GetDisplayUsableBounds(SDL_GetPrimaryDisplay(), &usable))
1022 const int capW = usable.w + bandW;
1023 const int capH = usable.h + bandH;
1024 if ((maxW == 0) || (maxW > capW))
1026 if ((maxH == 0) || (maxH > capH))
1029 SDL_SetWindowMaximumSize(_win->window(), maxW, maxH);
1030 _minMaxDirty =
false;
1034 if (_geometryDirty && !wmOwnsGeometry)
1036 const SDL_Rect vis = targetOuterRect();
1038 setAppliedInsets(band);
1041 SDL_GetWindowSize(_win->window(), &cw, &ch);
1042 bool applied =
false;
1043 bool placed =
false;
1045 if (_isPopup && parent)
1047 placePopup(vis, parentRect);
1050 else if (railPlatformCaps().supportsWindowPositions &&
1051 !(_isPopup && SDL_GetWindowParent(_win->window())))
1055 SDL_GetWindowPosition(_win->window(), &cx, &cy);
1056 if ((cx != vis.x) || (cy != vis.y))
1058 placed = railPlatformSetWindowRect(_win->window(), vis);
1060 SDL_SetWindowPosition(_win->window(), vis.x, vis.y);
1064 if ((cw != vis.w) || (ch != vis.h))
1066 if (!placed && !railPlatformSetWindowRect(_win->window(), vis))
1067 std::ignore = _win->resize({ vis.w, vis.h });
1071 if (railPlatformCaps().supportsWindowPositions && !_isPopup && !_localMoveActive &&
1074 const bool settled = SDL_SyncWindow(_win->window());
1077 SDL_GetWindowSize(_win->window(), &aw, &ah);
1078 if (settled && ((aw != vis.w) || (ah != vis.h)))
1081 _wmRefusedOuter = { vis.x, vis.y, aw, ah };
1087 _geometryDirty =
false;
1089 _geomApplyPending = applied && !_isPopup;
1093 if (!_isPopup && parent && !_parentApplied &&
1094 ((SDL_GetWindowFlags(parent) & SDL_WINDOW_HIDDEN) == 0))
1096 if (SDL_SetWindowParent(_win->window(), parent))
1098 _parentApplied =
true;
1100 if (((SDL_GetWindowFlags(_win->window()) & SDL_WINDOW_HIDDEN) == 0) &&
1101 !railPlatformRaiseOwned(_win->window()))
1102 WLog_WARN(
TAG,
"raising dialog id=0x%08" PRIx32
" failed",
1103 static_cast<uint32_t
>(_id));
1109 SDL_SetWindowTitle(_win->window(), _title.c_str());
1110 _titleDirty =
false;
1116 if ((_isPopup && !_fullscreen) || !sdl::utils::isWaylandDriver() ||
1117 sdl_wayland_set_app_id(_win->window(), _appId.c_str()))
1118 _appIdDirty =
false;
1122 if (!_isPopup && !_icon.bgra.empty())
1124 SDL_Surface* s = SDL_CreateSurfaceFrom(
1125 static_cast<int>(_icon.w),
static_cast<int>(_icon.h), SDL_PIXELFORMAT_BGRA32,
1126 _icon.bgra.data(),
static_cast<int>(_icon.w * 4));
1129 if (!SDL_SetWindowIcon(_win->window(), s))
1130 WLog_WARN(
TAG,
"SDL_SetWindowIcon failed for window 0x%08" PRIx32
": %s",
1131 static_cast<uint32_t
>(_id), SDL_GetError());
1132 SDL_DestroySurface(s);
1139 if (!_minState.rail && _gfxPresented &&
1140 (SDL_GetWindowFlags(_win->window()) & SDL_WINDOW_HIDDEN))
1143 SDL_Window* owner = _isPopup ? nullptr : SDL_GetWindowParent(_win->window());
1144 if (owner && ((SDL_GetWindowFlags(owner) & SDL_WINDOW_HIDDEN) != 0))
1146 if (SDL_SetWindowParent(_win->window(),
nullptr))
1147 _parentApplied =
false;
1149 WLog_WARN(
TAG,
"detaching dialog id=0x%08" PRIx32
" failed: %s",
1150 static_cast<uint32_t
>(_id), SDL_GetError());
1152 SDL_ShowWindow(_win->window());
1157bool SdlRailWindow::takeSurfaceChange(uint32_t surfaceId)
1159 std::unique_lock lock(_gfxLock);
1160 if (_surfaceId == surfaceId)
1162 _surfaceId = surfaceId;
1166void SdlRailWindow::updateGfxSurface(
const void* data, uint32_t stride, uint32_t width,
1167 uint32_t height,
const RECTANGLE_16* damage, uint32_t nbDamage,
1168 WINPR_ATTR_UNUSED uint32_t format)
1170 std::unique_lock lock(_gfxLock);
1172 WINPR_ASSERT((format == PIXEL_FORMAT_BGRA32) || (format == PIXEL_FORMAT_BGRX32));
1174 const size_t bytes =
static_cast<size_t>(stride) * height;
1175 if (!data || (bytes == 0) || (width == 0) || (height == 0))
1178 WLog_DBG(
TAG,
"gfx cleared id=0x%08" PRIx32
" (surface unmapped)",
1179 static_cast<uint32_t
>(_id));
1183 _gfxStride = stride;
1186 _gfxVisOrigin = { 0, 0 };
1190 const auto* src =
static_cast<const uint8_t*
>(data);
1192 const bool full = !_hasGfx || (_gfxBuffer.size() != bytes) || (_gfxStride != stride) ||
1193 (_gfxW != width) || (_gfxH != height) || (nbDamage == 0);
1196 const SDL_Rect bounds{ 0, 0,
static_cast<int>(width),
static_cast<int>(height) };
1198 if (!_hasGfx && regionIsBlank(src, stride, bounds))
1200 _gfxBuffer.assign(src, src + bytes);
1201 _gfxDamage.assign(1, SDL_Rect{ 0, 0,
static_cast<int>(width),
static_cast<int>(height) });
1202 _needsFullBlit =
true;
1204 _gfxHasAlpha = honorsAlpha() &&
1205 regionHasAlpha(_gfxBuffer.data(), stride,
1206 { 0, 0, static_cast<int>(width), static_cast<int>(height) });
1210 const SDL_Rect bounds{ 0, 0,
static_cast<int>(width),
static_cast<int>(height) };
1211 for (uint32_t i = 0; i < nbDamage; i++)
1213 const SDL_Rect r{ damage[i].left, damage[i].top, damage[i].right - damage[i].left,
1214 damage[i].bottom - damage[i].top };
1216 if (!SDL_GetRectIntersection(&r, &bounds, &clip))
1218 std::ignore = freerdp_image_copy_no_overlap(
1219 _gfxBuffer.data(), PIXEL_FORMAT_BGRA32, stride,
static_cast<UINT32
>(clip.x),
1220 static_cast<UINT32
>(clip.y),
static_cast<UINT32
>(clip.w),
1221 static_cast<UINT32
>(clip.h), src, PIXEL_FORMAT_BGRA32, stride,
1222 static_cast<UINT32
>(clip.x),
static_cast<UINT32
>(clip.y),
nullptr,
1224 _gfxDamage.push_back(clip);
1226 if (honorsAlpha() && !_gfxHasAlpha && regionHasAlpha(_gfxBuffer.data(), stride, clip))
1227 _gfxHasAlpha =
true;
1230 if (_gfxDamage.size() > 32)
1231 _gfxDamage.assign(1,
1232 SDL_Rect{ 0, 0,
static_cast<int>(width),
static_cast<int>(height) });
1234 if ((_gfxW != width) || (_gfxH != height))
1236 _resizeAnchorRight =
false;
1237 _resizeAnchorBottom =
false;
1238 _needsFullBlit =
true;
1240 _gfxVisOrigin = { 0, 0 };
1243 _gfxStride = stride;
1246 WLog_VRB(
TAG,
"gfx id=0x%08" PRIx32
" %ux%u full=%d nDamage=%u",
static_cast<uint32_t
>(_id),
1247 width, height, full ? 1 : 0, nbDamage);
1250void SdlRailWindow::invalidateAll()
1252 std::unique_lock lock(_gfxLock);
1253 _needsFullBlit =
true;
1255 _gfxDamage.assign(1, SDL_Rect{ 0, 0,
static_cast<int>(_gfxW),
static_cast<int>(_gfxH) });
1258void SdlRailWindow::setServerState(StateSync& s,
bool m)
1260 std::unique_lock lock(_gfxLock);
1268void SdlRailWindow::setServerMaximized(
bool m)
1270 setServerState(_maxState, m);
1273void SdlRailWindow::setServerMinimized(
bool m)
1276 std::unique_lock lock(_gfxLock);
1278 if (m && (_localRestoreUntil != 0) && (SDL_GetTicks() < _localRestoreUntil))
1280 _minState.server =
true;
1284 _localRestoreUntil = 0;
1286 setServerState(_minState, m);
1289void SdlRailWindow::noteLocalRestore()
1291 std::unique_lock lock(_gfxLock);
1292 _restoreRetried =
false;
1293 armDeadline(_localRestoreUntil, kAwaitRestoreRectMs);
1296bool SdlRailWindow::takeRestoreRetry()
1298 std::unique_lock lock(_gfxLock);
1299 return std::exchange(_restoreRetry,
false);
1302bool SdlRailWindow::applyServerState(StateSync& s,
const char* what,
bool (*enter)(SDL_Window*))
1306 bool restored =
false;
1307 if (s.server && !s.rail)
1311 WLog_DBG(
TAG,
"%s id=0x%08" PRIx32
"", what,
static_cast<uint32_t
>(_id));
1312 enter(_win->window());
1314 else if (!s.server && s.rail)
1318 WLog_DBG(
TAG,
"restore id=0x%08" PRIx32
" (%s)",
static_cast<uint32_t
>(_id), what);
1319 SDL_RestoreWindow(_win->window());
1327 (void)SDL_SyncWindow(_win->window());
1333void SdlRailWindow::setAppliedInsets(
const SDL_Rect& insets)
1335 if (SDL_RectsEqual(&insets, &_appliedInsets))
1337 _appliedInsets = insets;
1338 _needsFullBlit =
true;
1341bool SdlRailWindow::insetsStale()
const
1343 std::unique_lock lock(_gfxLock);
1344 return _wasMaximized != effectivelyMaximized();
1348bool SdlRailWindow::restoreRectPending()
const
1350 return (_awaitRestoreUntil != 0) && (SDL_GetTicks() < _awaitRestoreUntil);
1353bool SdlRailWindow::effectivelyMaximized()
const
1355 return _maxState.rail ||
1356 (_win && (SDL_GetWindowFlags(_win->window()) & SDL_WINDOW_MAXIMIZED) != 0);
1359bool SdlRailWindow::paint(SDL_Surface* primary, SDL_PixelFormat fallbackFormat,
1360 const std::vector<SDL_Rect>& damage, SDL_Window* parent,
1361 const SDL_Rect& parentRect)
1363 if (!reconcile(parent, parentRect))
1367 auto legacy =
false;
1369 std::unique_lock lock(_gfxLock);
1371 ok = paintGfx(fallbackFormat);
1376 ok = paintLegacy(primary, damage);
1379 if (_gfxPresented && !_mapped && !_minState.rail)
1382 WLog_DBG(
TAG,
"map id=0x%08" PRIx32
" %s",
static_cast<uint32_t
>(_id), role());
1383 SDL_ShowWindow(_win->window());
1385 if (_isPopup && parent)
1387 std::unique_lock lock(_gfxLock);
1388 const SDL_Rect vis = targetOuterRect();
1389 placePopup(vis, parentRect);
1391 if (((!_isPopup && !_layered) || _fullscreen) && ((_exStyle & WS_EX_NOACTIVATE) == 0))
1398bool SdlRailWindow::paintGfx(SDL_PixelFormat format)
1400 if (holdForPlacement())
1403 const SDL_Rect bi = _appliedInsets;
1406 SDL_GetWindowSizeInPixels(_win->window(), &ww, &wh);
1407 const int cw = ww - bi.x - bi.w;
1408 const int ch = wh - bi.y - bi.h;
1410 static_cast<int>(_gfxW);
1411 const int gh =
static_cast<int>(_gfxH);
1413 if ((_visRects.size() == 1) && (_visRects.at(0).w == gw) && (_visRects.at(0).h == gh))
1414 _gfxVisOrigin = { _visRects.at(0).x, _visRects.at(0).y };
1416 if (!_localMoveActive && (cw == gw) && (ch == gh))
1418 _resizeAnchorRight =
false;
1419 _resizeAnchorBottom =
false;
1420 _awaitingFrameUntil = 0;
1423 if ((_awaitingFrameUntil != 0) && (SDL_GetTicks() > _awaitingFrameUntil))
1425 WLog_DBG(
TAG,
"resize frame timeout id=0x%08" PRIx32
" win=%dx%d gfx=%dx%d",
1426 static_cast<uint32_t
>(_id), cw, ch, gw, gh);
1427 _awaitingFrameUntil = 0;
1429 if ((cw != gw) || (ch != gh))
1431 if (_resizeAnchorRight)
1432 _windowRect.x += _windowRect.w - gw;
1433 if (_resizeAnchorBottom)
1434 _windowRect.y += _windowRect.h - gh;
1437 _resizeAnchorRight =
false;
1438 _resizeAnchorBottom =
false;
1439 _geometryDirty =
true;
1440 _needsFullBlit =
true;
1445 const bool awaitingServerResize = (_awaitingFrameUntil != 0) && ((cw != gw) || (ch != gh));
1446 const bool localResize = awaitingServerResize ||
1447 (_localMoveActive && (_localMoveIsResize || (cw != gw) || (ch != gh)));
1450 const bool winResized = (ww != _lastWinW) || (wh != _lastWinH);
1451 const bool gfxResized = (gw != _lastGfxW) || (gh != _lastGfxH);
1452 const bool serverResize =
1453 !_localMoveActive && !awaitingServerResize &&
1454 (winResized || gfxResized || _needsFullBlit || _pendingRel.has_value());
1457 if (!localResize && !serverResize && _gfxDamage.empty() && !(_layered && _visDirty))
1459 WLog_VRB(
TAG,
"paintGfx skip id=0x%08" PRIx32
" no-damage no-resize",
1460 static_cast<uint32_t
>(_id));
1465 if (_isPopup && !_gfxPresented && (_gfxW > 0) && (_gfxH > 0))
1467 const uint32_t first = *
reinterpret_cast<const uint32_t*
>(_gfxBuffer.data());
1469 bool uniform = (first & 0x00FFFFFFu) == 0;
1470 for (uint32_t y = 0; uniform && (y < _gfxH); y++)
1472 const auto* row =
reinterpret_cast<const uint32_t*
>(
1473 _gfxBuffer.data() +
static_cast<size_t>(y) * _gfxStride);
1474 for (uint32_t x = 0; x < _gfxW; x++)
1475 if (row[x] != first)
1485 _needsFullBlit =
false;
1495 SDL_PixelFormat contentFormat = format;
1496 if (format == SDL_PIXELFORMAT_BGRA32)
1498 const bool blend = railPlatformCaps().supportsTransparentWindows;
1499 if (!blend || (_isPopup && !_layered && !_gfxHasAlpha))
1500 contentFormat = SDL_PIXELFORMAT_BGRX32;
1503 SDL_Surface* s = SDL_CreateSurfaceFrom(gw, gh, contentFormat, _gfxBuffer.data(),
1504 static_cast<int>(_gfxStride));
1507 WLog_WARN(
TAG,
"paintGfx id=0x%08" PRIx32
" SDL_CreateSurfaceFrom failed: %s",
1508 static_cast<uint32_t
>(_id), SDL_GetError());
1516 const SDL_Point crop = { std::clamp(_gfxVisOrigin.x, 0, gw),
1517 std::clamp(_gfxVisOrigin.y, 0, gh) };
1519 const SDL_Point off = { (_resizeAnchorRight ? (ww - bi.w - gw) : bi.x) - crop.x,
1520 (_resizeAnchorBottom ? (wh - bi.h - gh) : bi.y) - crop.y };
1522 const bool showDashes = _localMoveActive && _localMoveIsResize;
1524 const bool fillRevealed = showDashes || awaitingServerResize;
1526 _win->paintResizeFrame(s, off, !_gfxDamage.empty(), bi, fillRevealed, showDashes);
1531 const SDL_Rect full = { 0, 0, gw, gh };
1532 const SDL_Point dst = blitOffset();
1533 const bool maxed = effectivelyMaximized();
1534 if (_layered && !_visRects.empty() && !maxed)
1537 const bool logClip = _visDirty;
1539 if (_visDirty || serverResize)
1542 std::ignore = _win->fill(
static_cast<Uint8
>(0), 0, 0, 0);
1543 _gfxDamage.assign(1, full);
1545 _needsFullBlit =
false;
1547 const SDL_Point off = { _visOffsetSet ? (_visOffset.x - _windowRect.x) : 0,
1548 _visOffsetSet ? (_visOffset.y - _windowRect.y) : 0 };
1551 "clip id=0x%08" PRIx32
" off=%d,%d nVis=%zu vis0=%dx%d+%d+%d win=%dx%d "
1553 static_cast<uint32_t
>(_id), off.x, off.y, _visRects.size(),
1554 _visRects.at(0).w, _visRects.at(0).h, _visRects.at(0).x, _visRects.at(0).y,
1555 ww, wh, _gfxW, _gfxH);
1556 const std::vector<SDL_Rect>& damageRects = _gfxDamage;
1557 std::vector<SDL_Rect> draw;
1558 draw.reserve(damageRects.size() * _visRects.size());
1559 for (
const auto& d : damageRects)
1561 for (
auto v : _visRects)
1566 if (SDL_GetRectIntersection(&d, &v, &part))
1567 draw.push_back(part);
1571 std::ignore = _win->drawRects(s, dst, draw);
1573 else if (_gfxDamage.empty() || serverResize)
1578 const bool transparentWin =
1579 (SDL_GetWindowFlags(_win->window()) & SDL_WINDOW_TRANSPARENT) != 0;
1581 std::ignore = _win->fill(
static_cast<Uint8
>(0), 0, 0,
1582 static_cast<Uint8
>(transparentWin ? 0x00 : 0xFF));
1583 _needsFullBlit =
false;
1585 std::ignore = _win->drawRects(s, dst, { full });
1589 std::ignore = _win->drawRects(s, dst, _gfxDamage);
1592 _win->updateSurface();
1593 _gfxPresented =
true;
1596 SDL_DestroySurface(s);
1597 WLog_VRB(
TAG,
"paintGfx id=0x%08" PRIx32
" mode=%s win=%dx%d dmg=%zu",
1598 static_cast<uint32_t
>(_id),
1599 localResize ?
"resize" : (serverResize ?
"server-resize" :
"gfx"), ww, wh,
1613bool SdlRailWindow::paintLegacy(SDL_Surface* primary,
const std::vector<SDL_Rect>& damage)
1617 std::vector<SDL_Rect> vis;
1620 std::unique_lock lock(_gfxLock);
1622 if (!primary || _layered || (_windowRect.w <= 0) || (_windowRect.h <= 0) ||
1627 full = !_painted || _pendingRel.has_value();
1628 if (!full && damage.empty())
1630 WLog_VRB(
TAG,
"paintLegacy skip id=0x%08" PRIx32
" no-damage",
1631 static_cast<uint32_t
>(_id));
1636 bi = _appliedInsets;
1641 vis.push_back({ 0, 0, rect.w, rect.h });
1644 const SDL_Rect bounds = { 0, 0, primary->w, primary->h };
1645 bool blitted =
false;
1646 for (
const auto& v : vis)
1648 SDL_Rect src = { rect.x + v.x, rect.y + v.y, v.w, v.h };
1650 if (!SDL_GetRectIntersection(&src, &bounds, &clipped))
1655 SDL_Rect dst = { clipped.x - rect.x + bi.x, clipped.y - rect.y + bi.y, clipped.w,
1657 if (_win->blit(primary, clipped, dst))
1661 for (
const auto& d : damage)
1664 if (!SDL_GetRectIntersection(&clipped, &d, &part))
1666 SDL_Rect dst = { part.x - rect.x + bi.x, part.y - rect.y + bi.y, part.w, part.h };
1667 if (_win->blit(primary, part, dst))
1674 std::unique_lock lock(_gfxLock);
1677 _win->updateSurface();
1679 std::unique_lock lock(_gfxLock);
1682 WLog_VRB(
TAG,
"paintLegacy id=0x%08" PRIx32
" full=%d visRects=%zu",
1683 static_cast<uint32_t
>(_id), full ? 1 : 0, vis.size());