23#include <freerdp/codec/color.h>
24#include <freerdp/log.h>
25#include <freerdp/window.h>
27#include "sdl_rail_platform.hpp"
28#include "sdl_rail_window.hpp"
29#include "sdl_rail_window_style.hpp"
30#include "sdl_utils.hpp"
31#include "sdl_wayland.hpp"
32#include "sdl_window.hpp"
34#define TAG CLIENT_TAG("sdl.rail.window")
37static constexpr uint64_t kAwaitServerFrameMs = 1000;
40static constexpr uint64_t kAwaitRestoreRectMs = 500;
43static constexpr size_t kMaxReported = 16;
46static constexpr uint64_t kReportConfirmMs = 1000;
48static void armDeadline(uint64_t& until, uint64_t ms)
50 until = SDL_GetTicks() + ms;
51 if (!sdl_push_update_after(WINPR_ASSERTING_INT_CAST(Uint32, ms + 1)))
52 WLog_WARN(TAG,
"SDL_AddTimer failed: %s", SDL_GetError());
56static constexpr uint64_t kDisplaySettleMs = 500;
59static constexpr uint64_t kMoveActiveMs = 250;
62static constexpr uint64_t kPlaceWaitMs = 50;
65static bool regionHasAlpha(
const uint8_t* base, uint32_t stride,
const SDL_Rect& r)
67 for (
int y = r.y; y < r.y + r.h; y++)
69 const uint8_t* row = base +
static_cast<size_t>(y) * stride;
70 for (
int x = r.x; x < r.x + r.w; x++)
71 if (row[
static_cast<size_t>(x) * 4 + 3] != 0)
77static bool regionIsBlank(
const uint8_t* base, uint32_t stride,
const SDL_Rect& r)
79 for (
int y = r.y; y < r.y + r.h; y++)
81 const auto* row =
reinterpret_cast<const uint32_t*
>(base +
static_cast<size_t>(y) * stride);
82 for (
int x = r.x; x < r.x + r.w; x++)
89SdlRailWindow::SdlRailWindow(uint64_t
id,
const SDL_Rect& rect) : _id(id), _windowRect(rect)
94SdlRailWindow::~SdlRailWindow() =
default;
96uint64_t SdlRailWindow::id()
const
101SDL_WindowID SdlRailWindow::sdlId()
const
103 return _win ? _win->id() : 0;
106SDL_Window* SdlRailWindow::window()
const
108 return _win ? _win->window() :
nullptr;
111SDL_Renderer* SdlRailWindow::renderer()
const
113 return _win ? _win->renderer() :
nullptr;
116void SdlRailWindow::updateWindowRect(
const SDL_Rect& rect)
118 std::unique_lock lock(_gfxLock);
120 if (_minState.server)
122 _awaitRestoreUntil = 0;
123 if (_localMoveActive)
126 if (!_localMoveIsResize && ((rect.w != _windowRect.w) || (rect.h != _windowRect.h)))
128 WLog_DBG(
TAG,
"size adopted mid-move id=0x%08" PRIx32
" %dx%d -> %dx%d at %d,%d",
129 static_cast<uint32_t
>(_id), _windowRect.w, _windowRect.h, rect.w, rect.h,
131 _windowRect.w = rect.w;
132 _windowRect.h = rect.h;
134 _localMoveServerPos = { rect.x, rect.y };
135 _localMoveSizeChanged =
true;
141 if (SDL_RectsEqual(&rect, &_windowRect))
147 const auto echo = std::find_if(_reported.begin(), _reported.end(),
148 [&](
const SDL_Rect& r) { return SDL_RectsEqual(&r, &rect); });
149 if (echo != _reported.end())
151 WLog_DBG(
TAG,
"late echo id=0x%08" PRIx32
" %dx%d ignored",
static_cast<uint32_t
>(_id),
153 _reported.erase(_reported.begin(), echo + 1);
158 if ((rect.w != _windowRect.w) || (rect.h != _windowRect.h))
162 _geometryDirty =
true;
165void SdlRailWindow::setLocalMoveActive(
bool active)
167 std::unique_lock lock(_gfxLock);
168 _localMoveActive = active;
171 _localMoveIsResize =
false;
172 _localMoveSizeChanged =
false;
176void SdlRailWindow::setResizeAnchor(
bool right,
bool bottom)
178 std::unique_lock lock(_gfxLock);
179 _resizeAnchorRight = right;
180 _resizeAnchorBottom = bottom;
181 _localMoveIsResize =
true;
184bool SdlRailWindow::localMoveActive()
const
186 std::unique_lock lock(_gfxLock);
187 return _localMoveActive;
190bool SdlRailWindow::localMoveSizeChanged()
const
192 std::unique_lock lock(_gfxLock);
193 return _localMoveSizeChanged;
196SDL_Point SdlRailWindow::localMoveServerPos()
const
198 std::unique_lock lock(_gfxLock);
199 return _localMoveServerPos;
202void SdlRailWindow::adoptLocalGeometry(
const SDL_Rect& rect)
205 std::unique_lock lock(_gfxLock);
207 _geomApplyPending =
false;
211 _visOffset.x += rect.x - _windowRect.x;
212 _visOffset.y += rect.y - _windowRect.y;
214 if (!SDL_RectsEqual(&rect, &_windowRect))
216 if (_reported.size() >= kMaxReported)
217 _reported.erase(_reported.begin());
218 _reported.push_back(rect);
219 armDeadline(_reportCheckAt, kReportConfirmMs);
222 _geometryDirty =
false;
224 _awaitingFrameUntil =
225 (_localMoveActive && _localMoveIsResize) ? (SDL_GetTicks() + kAwaitServerFrameMs) : 0;
226 _localMoveActive =
false;
227 _needsFullBlit =
true;
230 _gfxDamage.assign(1, SDL_Rect{ 0, 0,
static_cast<int>(_gfxW),
static_cast<int>(_gfxH) });
233SDL_Rect SdlRailWindow::windowRect()
const
235 std::unique_lock lock(_gfxLock);
239void SdlRailWindow::markDeleted()
241 std::unique_lock lock(_gfxLock);
245void SdlRailWindow::releaseWindow()
247 std::unique_lock lock(_gfxLock);
250 WLog_DBG(
TAG,
"release id=0x%08" PRIx32
" %s%s",
static_cast<uint32_t
>(_id), role(),
251 _deleted ?
"" :
" (SDL parent gone, re-created on next pass)");
253 _appIdDirty = !_appId.empty();
257 _gfxPresented =
false;
258 _needsFullBlit =
true;
259 _geometryDirty =
true;
260 _parentApplied =
false;
262 _appliedInsets = { 0, 0, 0, 0 };
263 _extentsApplied = { 0, 0, 0, 0 };
264 _lastWinW = _lastWinH = _lastGfxW = _lastGfxH = 0;
267bool SdlRailWindow::isDeleted()
const
269 std::unique_lock lock(_gfxLock);
273bool SdlRailWindow::geomApplyPending()
const
275 std::unique_lock lock(_gfxLock);
276 return _geomApplyPending;
279void SdlRailWindow::clearGeomApplyPending()
281 std::unique_lock lock(_gfxLock);
282 _geomApplyPending =
false;
285void SdlRailWindow::armLoopEnd()
287 std::unique_lock lock(_gfxLock);
288 _loopEnd.pending =
true;
291bool SdlRailWindow::loopEndPending()
const
293 std::unique_lock lock(_gfxLock);
294 return _loopEnd.pending;
297void SdlRailWindow::deferMaximize()
299 std::unique_lock lock(_gfxLock);
300 _loopEnd.maximize =
true;
303void SdlRailWindow::deferSnap(
const SDL_Rect& serverRect)
305 std::unique_lock lock(_gfxLock);
306 _loopEnd.snap =
true;
307 _loopEnd.snapRect = serverRect;
310void SdlRailWindow::clearLoopEnd()
312 std::unique_lock lock(_gfxLock);
318 std::unique_lock lock(_gfxLock);
319 const LoopEndActions actions{ _loopEnd.maximize, _loopEnd.snap, _loopEnd.snapRect };
324bool SdlRailWindow::takeWmOverride(SDL_Rect& outer)
326 std::unique_lock lock(_gfxLock);
329 outer = _wmRefusedOuter;
334void SdlRailWindow::setVisibilityRects(std::vector<SDL_Rect> rects)
336 std::unique_lock lock(_gfxLock);
337 _visRects = std::move(rects);
341void SdlRailWindow::setVisibleOffset(SDL_Point offset)
343 std::unique_lock lock(_gfxLock);
345 if (_localMoveActive)
347 if (_visOffsetSet && (offset.x == _visOffset.x) && (offset.y == _visOffset.y))
350 _visOffsetSet =
true;
354void SdlRailWindow::setMinMaxSize(SDL_Point minSize, SDL_Point maxSize)
356 std::unique_lock lock(_gfxLock);
357 _minSize = { std::max(0, minSize.x), std::max(0, minSize.y) };
358 _maxSize = { std::max(0, maxSize.x), std::max(0, maxSize.y) };
361 _geometryDirty =
true;
364void SdlRailWindow::setResizeMargins(
int left,
int top,
int right,
int bottom)
366 std::unique_lock lock(_gfxLock);
367 const SDL_Rect m = { left, top, right, bottom };
368 if (SDL_RectsEqual(&m, &_resizeMargins))
372 if (!railMaximized())
373 _geometryDirty =
true;
376SDL_Rect SdlRailWindow::resizeMargins()
const
378 std::unique_lock lock(_gfxLock);
379 return _resizeMargins;
382void SdlRailWindow::setFrameMargins(
const SDL_Rect& m)
384 std::unique_lock lock(_gfxLock);
388SDL_Rect SdlRailWindow::frameMargins()
const
390 std::unique_lock lock(_gfxLock);
391 return _frameMargins;
394bool SdlRailWindow::takeUnconfirmedReport(SDL_Rect& rect)
396 std::unique_lock lock(_gfxLock);
397 if ((_reportCheckAt == 0) || (SDL_GetTicks() < _reportCheckAt))
400 if (_reported.empty())
402 rect = _reported.back();
407void SdlRailWindow::noteDisplayChange()
409 std::unique_lock lock(_gfxLock);
410 armDeadline(_displayCheckAt, kDisplaySettleMs);
413bool SdlRailWindow::takeDisplayChange(
bool now)
415 std::unique_lock lock(_gfxLock);
416 if ((_displayCheckAt == 0) || (!now && (SDL_GetTicks() < _displayCheckAt)))
422bool SdlRailWindow::deferMaximizeUntilEcho()
424 std::unique_lock lock(_gfxLock);
425 if (_reported.empty())
427 _maximizeAfterEcho =
true;
431bool SdlRailWindow::takeMaximizeAfterEcho()
433 std::unique_lock lock(_gfxLock);
434 if (!_maximizeAfterEcho || !_reported.empty())
436 _maximizeAfterEcho =
false;
440void SdlRailWindow::setOwner(uint64_t ownerId)
442 std::unique_lock lock(_gfxLock);
446uint64_t SdlRailWindow::owner()
const
448 std::unique_lock lock(_gfxLock);
452bool SdlRailWindow::isPopup()
const
454 std::unique_lock lock(_gfxLock);
458bool SdlRailWindow::isFullscreen()
const
460 std::unique_lock lock(_gfxLock);
464bool SdlRailWindow::activatable()
const
466 std::unique_lock lock(_gfxLock);
467 return (!_isPopup || _fullscreen) && ((_exStyle & WS_EX_NOACTIVATE) == 0);
470bool SdlRailWindow::isLayered()
const
472 std::unique_lock lock(_gfxLock);
476void SdlRailWindow::setShadowAnchored(
bool anchored)
478 std::unique_lock lock(_gfxLock);
479 _shadowAnchored = anchored;
482void SdlRailWindow::setFrame(
bool frame)
484 std::unique_lock lock(_gfxLock);
488bool SdlRailWindow::isFrame()
const
490 std::unique_lock lock(_gfxLock);
494bool SdlRailWindow::styleResizable()
const
497 return _everResizable;
501static constexpr int kMinGrip = 4;
503SDL_Rect SdlRailWindow::bandMargins()
const
505 if (!_visible || _isPopup || _layered || !styleResizable() || effectivelyMaximized())
506 return { 0, 0, 0, 0 };
508 const SDL_Rect& m = _resizeMargins;
509 return { std::max(m.x, kMinGrip), std::max(m.y, kMinGrip), std::max(m.w, kMinGrip),
510 std::max(m.h, kMinGrip) };
513SDL_Rect SdlRailWindow::bandInsets()
const
516 const auto band = railPlatformCaps().band;
517 if ((band == RailBandMechanism::None) || (band == RailBandMechanism::NativeFrame))
518 return { 0, 0, 0, 0 };
519 return bandMargins();
523static SDL_Rect addInsets(
const SDL_Rect& r,
const SDL_Rect& i)
525 return { r.x - i.x, r.y - i.y, r.w + i.x + i.w, r.h + i.y + i.h };
530static SDL_Rect stripInsets(
const SDL_Rect& r,
const SDL_Rect& i)
532 return { r.x + i.x, r.y + i.y, r.w - i.x - i.w, r.h - i.y - i.h };
536bool SdlRailWindow::isFullDisplaySize()
const
539 return SDL_GetDisplayBounds(SDL_GetPrimaryDisplay(), &disp) && (_windowRect.w >= disp.w) &&
540 (_windowRect.h >= disp.h);
545SDL_Rect SdlRailWindow::targetOuterRect()
const
547 SDL_Rect r = addInsets(_windowRect, bandInsets());
548 r.x += _localShift.x;
549 r.y += _localShift.y;
554SDL_Rect SdlRailWindow::insets()
const
556 std::unique_lock lock(_gfxLock);
557 return _appliedInsets;
561SDL_Rect SdlRailWindow::outerRect()
const
563 std::unique_lock lock(_gfxLock);
564 SDL_Rect r = addInsets(_windowRect, _appliedInsets);
565 r.x += _localShift.x;
566 r.y += _localShift.y;
570SDL_Rect SdlRailWindow::serverRect(
const SDL_Rect& outer)
const
572 SDL_Rect r = stripInsets(outer, insets());
573 r.x -= _localShift.x;
574 r.y -= _localShift.y;
578void SdlRailWindow::setLocalShift(
const SDL_Point& shift)
580 std::unique_lock lock(_gfxLock);
585SDL_Point SdlRailWindow::blitOffset()
const
587 if (!effectivelyMaximized())
588 return { _appliedInsets.x, _appliedInsets.y };
590 const bool frameInSurface =
591 ((_frameMargins.x > 0) || (_frameMargins.y > 0)) &&
592 (
static_cast<int>(_gfxW) == _windowRect.w + _frameMargins.x + _frameMargins.w) &&
593 (
static_cast<int>(_gfxH) == _windowRect.h + _frameMargins.y + _frameMargins.h);
594 SDL_Point content = { 0, 0 };
596 content = { _frameMargins.x, _frameMargins.y };
597 else if (_visOffsetSet)
598 content = { _visOffset.x - _windowRect.x, _visOffset.y - _windowRect.y };
599 return { -std::clamp(content.x, 0,
static_cast<int>(_gfxW)),
600 -std::clamp(content.y, 0,
static_cast<int>(_gfxH)) };
604static Uint32 placeWaitTimer([[maybe_unused]]
void* userdata, [[maybe_unused]] SDL_TimerID timerID,
605 [[maybe_unused]] Uint32 interval)
607 if (!sdl_push_user_event(SDL_EVENT_USER_UPDATE))
608 WLog_WARN(TAG,
"could not wake the paint loop for a held popup");
614void SdlRailWindow::notePlaced()
616 std::unique_lock lock(_gfxLock);
621void SdlRailWindow::placeRequested()
623 _pendingRel = _requestedRel;
624 _placeInFlight =
false;
628bool SdlRailWindow::holdForPlacement()
632 if (SDL_GetTicks() - _requestedAt < kPlaceWaitMs)
639void SdlRailWindow::placePopup(
const SDL_Rect& vis,
const SDL_Rect& parentRect)
641 const SDL_Point rel = { vis.x - parentRect.x, vis.y - parentRect.y };
642 const bool wayland = !railPlatformCaps().supportsWindowPositions;
643 const bool moved = (rel.x != _requestedRel.x) || (rel.y != _requestedRel.y);
647 _requestedAt = SDL_GetTicks();
649 if (!SDL_SetWindowPosition(_win->window(), rel.x, rel.y))
651 WLog_VRB(
TAG,
"popup reposition unsupported id=0x%08" PRIx32
": %s",
652 static_cast<uint32_t
>(_id), SDL_GetError());
657 _needsFullBlit =
true;
658 if (!moved || !_mapped)
660 _placeInFlight =
true;
661 if (SDL_AddTimer(
static_cast<Uint32
>(kPlaceWaitMs + 1), placeWaitTimer,
nullptr) == 0)
662 WLog_WARN(
TAG,
"popup placement timer failed: %s", SDL_GetError());
666void SdlRailWindow::commitPlacement()
670 _placedRel = _pendingRel;
674bool SdlRailWindow::placedRel(SDL_Point& rel)
const
676 std::unique_lock lock(_gfxLock);
678 if (!_placedRel || (SDL_GetTicks() - _requestedAt >= kMoveActiveMs))
684SDL_Point SdlRailWindow::serverOrigin()
const
686 std::unique_lock lock(_gfxLock);
688 const SDL_Point dst = blitOffset();
689 return { _windowRect.x - dst.x, _windowRect.y - dst.y };
692void SdlRailWindow::setStyle(uint32_t style, uint32_t exStyle)
694 std::unique_lock lock(_gfxLock);
695 const bool wasTopmost = _topmost;
698 _topmost = (exStyle & WS_EX_TOPMOST) != 0;
699 if (_topmost != wasTopmost)
700 _topmostDirty =
true;
701 if (!_everResizable && ((style & (WS_THICKFRAME | WS_MAXIMIZEBOX)) != 0))
703 _everResizable =
true;
707 if (!_popupClassified)
709 const auto classification = classifySdlRailWindowStyle(style, exStyle, _ownerId != 0);
710 _isPopup = classification.popup;
711 _layered = classification.layered;
712 _overlay = classification.overlay;
713 _popupClassified =
true;
717void SdlRailWindow::setTitle(
const std::string& title)
719 std::unique_lock lock(_gfxLock);
724void SdlRailWindow::setAppId(
const std::string& appId)
726 std::unique_lock lock(_gfxLock);
728 _appIdDirty = !appId.empty();
731void SdlRailWindow::setVisible(
bool visible)
733 std::unique_lock lock(_gfxLock);
737void SdlRailWindow::setIcon(
const SdlRailIcon& icon)
739 std::unique_lock lock(_gfxLock);
744bool SdlRailWindow::create(SDL_Window* parent,
const SDL_Rect& parentRect)
749 const RailPlatformCaps& caps = railPlatformCaps();
752 const SDL_Rect vis = targetOuterRect();
753 _appliedInsets = bandInsets();
755 const bool fullscreen = _isPopup && !caps.supportsWindowPositions && isFullDisplaySize();
756 if (_isPopup && parent && !fullscreen)
759 const SDL_Rect rel = { vis.x - parentRect.x, vis.y - parentRect.y, vis.w, vis.h };
760 _requestedRel = { rel.x, rel.y };
761 _win = std::make_unique<SdlWindow>(
762 SdlWindow::createPopup(parent, rel, caps.supportsTransparentWindows, _overlay));
764 else if (_isPopup && !caps.supportsWindowPositions && !fullscreen)
767 WLog_VRB(
TAG,
"popup create deferred id=0x%08" PRIx32
": no parent yet",
768 static_cast<uint32_t
>(_id));
774 Uint32 flags = SDL_WINDOW_HIDDEN;
775 if (_isPopup || (caps.band != RailBandMechanism::NativeFrame))
776 flags |= SDL_WINDOW_BORDERLESS;
777 if (caps.supportsTransparentWindows)
778 flags |= SDL_WINDOW_TRANSPARENT;
779 if ((_exStyle & WS_EX_NOACTIVATE) != 0)
780 flags |= SDL_WINDOW_NOT_FOCUSABLE;
783 const char* hint = SDL_GetHint(SDL_HINT_APP_ID);
784 const std::string previous = hint ? hint :
"";
786 SDL_SetHint(SDL_HINT_APP_ID, _appId.c_str());
787 _win = std::make_unique<SdlWindow>(
788 SdlWindow::create(SDL_GetPrimaryDisplay(), _title, flags, vis));
792 SDL_SetHint(SDL_HINT_APP_ID, previous.c_str());
794 SDL_ResetHint(SDL_HINT_APP_ID);
797 if (!_win || !_win->window() || !_win->renderer())
800 WLog_WARN(
TAG,
"create failed id=0x%08" PRIx32
" %s",
static_cast<uint32_t
>(_id), role());
803 _win->resizeable(styleResizable());
808 SDL_SetWindowFullscreen(_win->window(),
true);
809 WLog_DBG(
TAG,
"fullscreen id=0x%08" PRIx32
" %dx%d (WS_POPUP spans display)",
810 static_cast<uint32_t
>(_id), _windowRect.w, _windowRect.h);
817 railPlatformPrepareWindow(_win->window());
818 std::ignore = railPlatformSetWindowRect(_win->window(), vis);
822 "create id=0x%08" PRIx32
" sdl=%" PRIu32
" %s vis=%dx%d+%d+%d margins=L%d,T%d,R%d,B%d "
824 static_cast<uint32_t
>(_id),
static_cast<uint32_t
>(_win->id()), role(), vis.w, vis.h,
825 vis.x, vis.y, _resizeMargins.x, _resizeMargins.y, _resizeMargins.w, _resizeMargins.h,
826 caps.supportsTransparentWindows ? 1 : 0);
830bool SdlRailWindow::reconcile(SDL_Window* parent,
const SDL_Rect& parentRect)
832 std::unique_lock lock(_gfxLock);
836 const bool popupReady = !_isPopup || _hasGfx;
838 const bool unplaceable =
839 _frame || (_overlay && !railPlatformCaps().supportsWindowPositions && isFullDisplaySize());
841 const bool drawable =
842 _visible && (_windowRect.w > 0) && (_windowRect.h > 0) && popupReady && !unplaceable &&
843 (!_layered || (_hasGfx && !_visRects.empty() && (_shadowAnchored || _overlay)));
848 if (_layered && _hasGfx && !_visRects.empty() && !_shadowAnchored && !_overlay &&
849 !_shadowSuppressLogged)
851 _shadowSuppressLogged =
true;
853 "shadow-rule suppressed id=0x%08" PRIx32
" %dx%d style=0x%08" PRIx32
855 static_cast<uint32_t
>(_id), _windowRect.w, _windowRect.h, _style, _exStyle);
859 if ((SDL_GetWindowFlags(_win->window()) & SDL_WINDOW_HIDDEN) == 0)
861 WLog_DBG(
TAG,
"hide id=0x%08" PRIx32
" %s",
static_cast<uint32_t
>(_id), role());
862 SDL_HideWindow(_win->window());
863 _appIdDirty = !_appId.empty();
867 if (_isPopup && !_visible)
869 _gfxPresented =
false;
877 if (!create(parent, parentRect))
879 _geometryDirty =
false;
886 _topmostDirty =
false;
888 if (!_isPopup && !_layered)
889 std::ignore = SDL_SetWindowAlwaysOnTop(_win->window(), _topmost);
895 _win->resizeable(styleResizable());
902 if (applyServerState(_maxState,
"maximize", SDL_MaximizeWindow))
903 armDeadline(_awaitRestoreUntil, kAwaitRestoreRectMs);
905 if ((_localRestoreUntil != 0) && (SDL_GetTicks() >= _localRestoreUntil))
907 _localRestoreUntil = 0;
908 if (_minState.server && !_minState.rail && !_restoreRetried)
910 _restoreRetried =
true;
911 _restoreRetry =
true;
912 armDeadline(_localRestoreUntil, kAwaitRestoreRectMs);
914 else if (_minState.server != _minState.rail)
915 _minState.dirty =
true;
917 std::ignore = applyServerState(_minState,
"minimize", SDL_MinimizeWindow);
918 const bool restorePending = restoreRectPending();
921 const bool maxed = effectivelyMaximized();
922 const SDL_Rect band = bandInsets();
923 if ((maxed != _wasMaximized) && !_localMoveActive && !restorePending)
925 setAppliedInsets(band);
927 _geometryDirty =
true;
928 _wasMaximized = maxed;
932 if ((railPlatformCaps().band == RailBandMechanism::FrameExtents) && !restorePending &&
933 !SDL_RectsEqual(&band, &_extentsApplied) && railPlatformSetBand(_win->window(), band))
935 _extentsApplied = band;
937 const bool wmOwnsGeometry =
938 _localMoveActive || geometryFrozen() || _fullscreen || restorePending;
941 if ((_minMaxDirty || _geometryDirty) && !wmOwnsGeometry)
943 const SDL_Rect vis = targetOuterRect();
944 const int bandW = band.x + band.w;
945 const int bandH = band.y + band.h;
947 const SDL_Rect& m = _resizeMargins;
948 const int padW = bandW - m.x - m.w;
949 const int padH = bandH - m.y - m.h;
950 const auto toOuter = [](
int track,
int pad)
951 {
return (track > 0) ? std::max(1, track + pad) : 0; };
952 const int minW = toOuter(_minSize.x, padW);
953 const int minH = toOuter(_minSize.y, padH);
954 SDL_SetWindowMinimumSize(_win->window(), std::clamp(minW, 0, vis.w),
955 std::clamp(minH, 0, vis.h));
956 int maxW = toOuter(_maxSize.x, padW);
957 int maxH = toOuter(_maxSize.y, padH);
960 if (!railPlatformCaps().supportsWindowPositions &&
961 SDL_GetDisplayUsableBounds(SDL_GetPrimaryDisplay(), &usable))
963 const int capW = usable.w + bandW;
964 const int capH = usable.h + bandH;
965 if ((maxW == 0) || (maxW > capW))
967 if ((maxH == 0) || (maxH > capH))
970 SDL_SetWindowMaximumSize(_win->window(), maxW, maxH);
971 _minMaxDirty =
false;
975 if (_geometryDirty && !wmOwnsGeometry)
977 const SDL_Rect vis = targetOuterRect();
979 setAppliedInsets(band);
982 SDL_GetWindowSize(_win->window(), &cw, &ch);
983 bool applied =
false;
986 if (_isPopup && parent)
988 placePopup(vis, parentRect);
991 else if (railPlatformCaps().supportsWindowPositions &&
992 !(_isPopup && SDL_GetWindowParent(_win->window())))
996 SDL_GetWindowPosition(_win->window(), &cx, &cy);
997 if ((cx != vis.x) || (cy != vis.y))
999 placed = railPlatformSetWindowRect(_win->window(), vis);
1001 SDL_SetWindowPosition(_win->window(), vis.x, vis.y);
1005 if ((cw != vis.w) || (ch != vis.h))
1007 if (!placed && !railPlatformSetWindowRect(_win->window(), vis))
1008 std::ignore = _win->resize({ vis.w, vis.h });
1012 if (railPlatformCaps().supportsWindowPositions && !_isPopup && !_localMoveActive &&
1015 const bool settled = SDL_SyncWindow(_win->window());
1018 SDL_GetWindowSize(_win->window(), &aw, &ah);
1019 if (settled && ((aw != vis.w) || (ah != vis.h)))
1022 _wmRefusedOuter = { vis.x, vis.y, aw, ah };
1028 _geometryDirty =
false;
1030 _geomApplyPending = applied && !_isPopup;
1034 if (!_isPopup && parent && !_parentApplied &&
1035 ((SDL_GetWindowFlags(parent) & SDL_WINDOW_HIDDEN) == 0))
1037 if (SDL_SetWindowParent(_win->window(), parent))
1039 _parentApplied =
true;
1041 if (((SDL_GetWindowFlags(_win->window()) & SDL_WINDOW_HIDDEN) == 0) &&
1042 !railPlatformRaiseOwned(_win->window()))
1043 WLog_WARN(
TAG,
"raising dialog id=0x%08" PRIx32
" failed",
1044 static_cast<uint32_t
>(_id));
1050 SDL_SetWindowTitle(_win->window(), _title.c_str());
1051 _titleDirty =
false;
1057 if ((_isPopup && !_fullscreen) || !sdl::utils::isWaylandDriver() ||
1058 sdl_wayland_set_app_id(_win->window(), _appId.c_str()))
1059 _appIdDirty =
false;
1063 if (!_isPopup && !_icon.bgra.empty())
1065 SDL_Surface* s = SDL_CreateSurfaceFrom(
1066 static_cast<int>(_icon.w),
static_cast<int>(_icon.h), SDL_PIXELFORMAT_BGRA32,
1067 _icon.bgra.data(),
static_cast<int>(_icon.w * 4));
1070 if (!SDL_SetWindowIcon(_win->window(), s))
1071 WLog_WARN(
TAG,
"SDL_SetWindowIcon failed for window 0x%08" PRIx32
": %s",
1072 static_cast<uint32_t
>(_id), SDL_GetError());
1073 SDL_DestroySurface(s);
1080 if (!_minState.rail && _gfxPresented &&
1081 (SDL_GetWindowFlags(_win->window()) & SDL_WINDOW_HIDDEN))
1084 SDL_Window* owner = _isPopup ? nullptr : SDL_GetWindowParent(_win->window());
1085 if (owner && ((SDL_GetWindowFlags(owner) & SDL_WINDOW_HIDDEN) != 0))
1087 if (SDL_SetWindowParent(_win->window(),
nullptr))
1088 _parentApplied =
false;
1090 WLog_WARN(
TAG,
"detaching dialog id=0x%08" PRIx32
" failed: %s",
1091 static_cast<uint32_t
>(_id), SDL_GetError());
1093 SDL_ShowWindow(_win->window());
1098bool SdlRailWindow::takeSurfaceChange(uint32_t surfaceId)
1100 std::unique_lock lock(_gfxLock);
1101 if (_surfaceId == surfaceId)
1103 _surfaceId = surfaceId;
1107void SdlRailWindow::updateGfxSurface(
const void* data, uint32_t stride, uint32_t width,
1108 uint32_t height,
const RECTANGLE_16* damage, uint32_t nbDamage,
1109 WINPR_ATTR_UNUSED uint32_t format)
1111 std::unique_lock lock(_gfxLock);
1113 WINPR_ASSERT((format == PIXEL_FORMAT_BGRA32) || (format == PIXEL_FORMAT_BGRX32));
1115 const size_t bytes =
static_cast<size_t>(stride) * height;
1116 if (!data || (bytes == 0) || (width == 0) || (height == 0))
1119 WLog_DBG(
TAG,
"gfx cleared id=0x%08" PRIx32
" (surface unmapped)",
1120 static_cast<uint32_t
>(_id));
1124 _gfxStride = stride;
1127 _gfxVisOrigin = { 0, 0 };
1131 const auto* src =
static_cast<const uint8_t*
>(data);
1133 const bool full = !_hasGfx || (_gfxBuffer.size() != bytes) || (_gfxStride != stride) ||
1134 (_gfxW != width) || (_gfxH != height) || (nbDamage == 0);
1137 const SDL_Rect bounds{ 0, 0,
static_cast<int>(width),
static_cast<int>(height) };
1139 if (!_hasGfx && regionIsBlank(src, stride, bounds))
1141 _gfxBuffer.assign(src, src + bytes);
1142 _gfxDamage.assign(1, SDL_Rect{ 0, 0,
static_cast<int>(width),
static_cast<int>(height) });
1143 _needsFullBlit =
true;
1145 _gfxHasAlpha = honorsAlpha() &&
1146 regionHasAlpha(_gfxBuffer.data(), stride,
1147 { 0, 0, static_cast<int>(width), static_cast<int>(height) });
1151 const SDL_Rect bounds{ 0, 0,
static_cast<int>(width),
static_cast<int>(height) };
1152 for (uint32_t i = 0; i < nbDamage; i++)
1154 const SDL_Rect r{ damage[i].left, damage[i].top, damage[i].right - damage[i].left,
1155 damage[i].bottom - damage[i].top };
1157 if (!SDL_GetRectIntersection(&r, &bounds, &clip))
1159 std::ignore = freerdp_image_copy_no_overlap(
1160 _gfxBuffer.data(), PIXEL_FORMAT_BGRA32, stride,
static_cast<UINT32
>(clip.x),
1161 static_cast<UINT32
>(clip.y),
static_cast<UINT32
>(clip.w),
1162 static_cast<UINT32
>(clip.h), src, PIXEL_FORMAT_BGRA32, stride,
1163 static_cast<UINT32
>(clip.x),
static_cast<UINT32
>(clip.y),
nullptr,
1165 _gfxDamage.push_back(clip);
1167 if (honorsAlpha() && !_gfxHasAlpha && regionHasAlpha(_gfxBuffer.data(), stride, clip))
1168 _gfxHasAlpha =
true;
1171 if (_gfxDamage.size() > 32)
1172 _gfxDamage.assign(1,
1173 SDL_Rect{ 0, 0,
static_cast<int>(width),
static_cast<int>(height) });
1175 if ((_gfxW != width) || (_gfxH != height))
1177 _resizeAnchorRight =
false;
1178 _resizeAnchorBottom =
false;
1179 _needsFullBlit =
true;
1181 _gfxVisOrigin = { 0, 0 };
1184 _gfxStride = stride;
1187 WLog_VRB(
TAG,
"gfx id=0x%08" PRIx32
" %ux%u full=%d nDamage=%u",
static_cast<uint32_t
>(_id),
1188 width, height, full ? 1 : 0, nbDamage);
1191void SdlRailWindow::invalidateAll()
1193 std::unique_lock lock(_gfxLock);
1194 _needsFullBlit =
true;
1196 _gfxDamage.assign(1, SDL_Rect{ 0, 0,
static_cast<int>(_gfxW),
static_cast<int>(_gfxH) });
1199void SdlRailWindow::setServerState(StateSync& s,
bool m)
1201 std::unique_lock lock(_gfxLock);
1209void SdlRailWindow::setServerMaximized(
bool m)
1211 setServerState(_maxState, m);
1214void SdlRailWindow::setServerMinimized(
bool m)
1217 std::unique_lock lock(_gfxLock);
1219 if (m && (_localRestoreUntil != 0) && (SDL_GetTicks() < _localRestoreUntil))
1221 _minState.server =
true;
1225 _localRestoreUntil = 0;
1227 setServerState(_minState, m);
1230void SdlRailWindow::noteLocalRestore()
1232 std::unique_lock lock(_gfxLock);
1233 _restoreRetried =
false;
1234 armDeadline(_localRestoreUntil, kAwaitRestoreRectMs);
1237bool SdlRailWindow::takeRestoreRetry()
1239 std::unique_lock lock(_gfxLock);
1240 return std::exchange(_restoreRetry,
false);
1243bool SdlRailWindow::applyServerState(StateSync& s,
const char* what,
bool (*enter)(SDL_Window*))
1247 bool restored =
false;
1248 if (s.server && !s.rail)
1252 WLog_DBG(
TAG,
"%s id=0x%08" PRIx32
"", what,
static_cast<uint32_t
>(_id));
1253 enter(_win->window());
1255 else if (!s.server && s.rail)
1259 WLog_DBG(
TAG,
"restore id=0x%08" PRIx32
" (%s)",
static_cast<uint32_t
>(_id), what);
1260 SDL_RestoreWindow(_win->window());
1268 (void)SDL_SyncWindow(_win->window());
1274void SdlRailWindow::setAppliedInsets(
const SDL_Rect& insets)
1276 if (SDL_RectsEqual(&insets, &_appliedInsets))
1278 _appliedInsets = insets;
1279 _needsFullBlit =
true;
1282bool SdlRailWindow::insetsStale()
const
1284 std::unique_lock lock(_gfxLock);
1285 return _wasMaximized != effectivelyMaximized();
1289bool SdlRailWindow::restoreRectPending()
const
1291 return (_awaitRestoreUntil != 0) && (SDL_GetTicks() < _awaitRestoreUntil);
1294bool SdlRailWindow::effectivelyMaximized()
const
1296 return _maxState.rail ||
1297 (_win && (SDL_GetWindowFlags(_win->window()) & SDL_WINDOW_MAXIMIZED) != 0);
1300bool SdlRailWindow::paint(SDL_Surface* primary, SDL_PixelFormat fallbackFormat,
1301 const std::vector<SDL_Rect>& damage, SDL_Window* parent,
1302 const SDL_Rect& parentRect)
1304 if (!reconcile(parent, parentRect))
1308 auto legacy =
false;
1310 std::unique_lock lock(_gfxLock);
1312 ok = paintGfx(fallbackFormat);
1317 ok = paintLegacy(primary, damage);
1320 if (_gfxPresented && !_mapped && !_minState.rail)
1323 WLog_DBG(
TAG,
"map id=0x%08" PRIx32
" %s",
static_cast<uint32_t
>(_id), role());
1324 SDL_ShowWindow(_win->window());
1326 if (_isPopup && parent)
1328 std::unique_lock lock(_gfxLock);
1329 const SDL_Rect vis = targetOuterRect();
1330 placePopup(vis, parentRect);
1332 if (((!_isPopup && !_layered) || _fullscreen) && ((_exStyle & WS_EX_NOACTIVATE) == 0))
1339bool SdlRailWindow::paintGfx(SDL_PixelFormat format)
1341 if (holdForPlacement())
1344 const SDL_Rect bi = _appliedInsets;
1347 SDL_GetWindowSizeInPixels(_win->window(), &ww, &wh);
1348 const int cw = ww - bi.x - bi.w;
1349 const int ch = wh - bi.y - bi.h;
1351 static_cast<int>(_gfxW);
1352 const int gh =
static_cast<int>(_gfxH);
1354 if ((_visRects.size() == 1) && (_visRects.at(0).w == gw) && (_visRects.at(0).h == gh))
1355 _gfxVisOrigin = { _visRects.at(0).x, _visRects.at(0).y };
1357 if (!_localMoveActive && (cw == gw) && (ch == gh))
1359 _resizeAnchorRight =
false;
1360 _resizeAnchorBottom =
false;
1361 _awaitingFrameUntil = 0;
1364 if ((_awaitingFrameUntil != 0) && (SDL_GetTicks() > _awaitingFrameUntil))
1366 WLog_DBG(
TAG,
"resize frame timeout id=0x%08" PRIx32
" win=%dx%d gfx=%dx%d",
1367 static_cast<uint32_t
>(_id), cw, ch, gw, gh);
1368 _awaitingFrameUntil = 0;
1370 if ((cw != gw) || (ch != gh))
1372 if (_resizeAnchorRight)
1373 _windowRect.x += _windowRect.w - gw;
1374 if (_resizeAnchorBottom)
1375 _windowRect.y += _windowRect.h - gh;
1378 _resizeAnchorRight =
false;
1379 _resizeAnchorBottom =
false;
1380 _geometryDirty =
true;
1381 _needsFullBlit =
true;
1386 const bool awaitingServerResize = (_awaitingFrameUntil != 0) && ((cw != gw) || (ch != gh));
1387 const bool localResize = awaitingServerResize ||
1388 (_localMoveActive && (_localMoveIsResize || (cw != gw) || (ch != gh)));
1391 const bool winResized = (ww != _lastWinW) || (wh != _lastWinH);
1392 const bool gfxResized = (gw != _lastGfxW) || (gh != _lastGfxH);
1393 const bool serverResize =
1394 !_localMoveActive && !awaitingServerResize &&
1395 (winResized || gfxResized || _needsFullBlit || _pendingRel.has_value());
1398 if (!localResize && !serverResize && _gfxDamage.empty() && !(_layered && _visDirty))
1400 WLog_VRB(
TAG,
"paintGfx skip id=0x%08" PRIx32
" no-damage no-resize",
1401 static_cast<uint32_t
>(_id));
1406 if (_isPopup && !_gfxPresented && (_gfxW > 0) && (_gfxH > 0))
1408 const uint32_t first = *
reinterpret_cast<const uint32_t*
>(_gfxBuffer.data());
1410 bool uniform = (first & 0x00FFFFFFu) == 0;
1411 for (uint32_t y = 0; uniform && (y < _gfxH); y++)
1413 const auto* row =
reinterpret_cast<const uint32_t*
>(
1414 _gfxBuffer.data() +
static_cast<size_t>(y) * _gfxStride);
1415 for (uint32_t x = 0; x < _gfxW; x++)
1416 if (row[x] != first)
1426 _needsFullBlit =
false;
1436 SDL_PixelFormat contentFormat = format;
1437 if (format == SDL_PIXELFORMAT_BGRA32)
1439 const bool blend = railPlatformCaps().supportsTransparentWindows;
1440 if (!blend || (_isPopup && !_layered && !_gfxHasAlpha))
1441 contentFormat = SDL_PIXELFORMAT_BGRX32;
1444 SDL_Surface* s = SDL_CreateSurfaceFrom(gw, gh, contentFormat, _gfxBuffer.data(),
1445 static_cast<int>(_gfxStride));
1448 WLog_WARN(
TAG,
"paintGfx id=0x%08" PRIx32
" SDL_CreateSurfaceFrom failed: %s",
1449 static_cast<uint32_t
>(_id), SDL_GetError());
1457 const SDL_Point crop = { std::clamp(_gfxVisOrigin.x, 0, gw),
1458 std::clamp(_gfxVisOrigin.y, 0, gh) };
1460 const SDL_Point off = { (_resizeAnchorRight ? (ww - bi.w - gw) : bi.x) - crop.x,
1461 (_resizeAnchorBottom ? (wh - bi.h - gh) : bi.y) - crop.y };
1463 const bool showDashes = _localMoveActive && _localMoveIsResize;
1465 const bool fillRevealed = showDashes || awaitingServerResize;
1467 _win->paintResizeFrame(s, off, !_gfxDamage.empty(), bi, fillRevealed, showDashes);
1472 const SDL_Rect full = { 0, 0, gw, gh };
1473 const SDL_Point dst = blitOffset();
1474 const bool maxed = effectivelyMaximized();
1475 if (_layered && !_visRects.empty() && !maxed)
1478 const bool logClip = _visDirty;
1480 if (_visDirty || serverResize)
1483 std::ignore = _win->fill(
static_cast<Uint8
>(0), 0, 0, 0);
1484 _gfxDamage.assign(1, full);
1486 _needsFullBlit =
false;
1488 const SDL_Point off = { _visOffsetSet ? (_visOffset.x - _windowRect.x) : 0,
1489 _visOffsetSet ? (_visOffset.y - _windowRect.y) : 0 };
1492 "clip id=0x%08" PRIx32
" off=%d,%d nVis=%zu vis0=%dx%d+%d+%d win=%dx%d "
1494 static_cast<uint32_t
>(_id), off.x, off.y, _visRects.size(),
1495 _visRects.at(0).w, _visRects.at(0).h, _visRects.at(0).x, _visRects.at(0).y,
1496 ww, wh, _gfxW, _gfxH);
1497 const std::vector<SDL_Rect>& damageRects = _gfxDamage;
1498 std::vector<SDL_Rect> draw;
1499 draw.reserve(damageRects.size() * _visRects.size());
1500 for (
const auto& d : damageRects)
1502 for (
auto v : _visRects)
1507 if (SDL_GetRectIntersection(&d, &v, &part))
1508 draw.push_back(part);
1512 std::ignore = _win->drawRects(s, dst, draw);
1514 else if (_gfxDamage.empty() || serverResize)
1519 const bool transparentWin =
1520 (SDL_GetWindowFlags(_win->window()) & SDL_WINDOW_TRANSPARENT) != 0;
1522 std::ignore = _win->fill(
static_cast<Uint8
>(0), 0, 0,
1523 static_cast<Uint8
>(transparentWin ? 0x00 : 0xFF));
1524 _needsFullBlit =
false;
1526 std::ignore = _win->drawRects(s, dst, { full });
1530 std::ignore = _win->drawRects(s, dst, _gfxDamage);
1533 _win->updateSurface();
1534 _gfxPresented =
true;
1537 SDL_DestroySurface(s);
1538 WLog_VRB(
TAG,
"paintGfx id=0x%08" PRIx32
" mode=%s win=%dx%d dmg=%zu",
1539 static_cast<uint32_t
>(_id),
1540 localResize ?
"resize" : (serverResize ?
"server-resize" :
"gfx"), ww, wh,
1554bool SdlRailWindow::paintLegacy(SDL_Surface* primary,
const std::vector<SDL_Rect>& damage)
1558 std::vector<SDL_Rect> vis;
1561 std::unique_lock lock(_gfxLock);
1563 if (!primary || _layered || (_windowRect.w <= 0) || (_windowRect.h <= 0) ||
1568 full = !_painted || _pendingRel.has_value();
1569 if (!full && damage.empty())
1571 WLog_VRB(
TAG,
"paintLegacy skip id=0x%08" PRIx32
" no-damage",
1572 static_cast<uint32_t
>(_id));
1577 bi = _appliedInsets;
1582 vis.push_back({ 0, 0, rect.w, rect.h });
1585 const SDL_Rect bounds = { 0, 0, primary->w, primary->h };
1586 bool blitted =
false;
1587 for (
const auto& v : vis)
1589 SDL_Rect src = { rect.x + v.x, rect.y + v.y, v.w, v.h };
1591 if (!SDL_GetRectIntersection(&src, &bounds, &clipped))
1596 SDL_Rect dst = { clipped.x - rect.x + bi.x, clipped.y - rect.y + bi.y, clipped.w,
1598 if (_win->blit(primary, clipped, dst))
1602 for (
const auto& d : damage)
1605 if (!SDL_GetRectIntersection(&clipped, &d, &part))
1607 SDL_Rect dst = { part.x - rect.x + bi.x, part.y - rect.y + bi.y, part.w, part.h };
1608 if (_win->blit(primary, part, dst))
1615 std::unique_lock lock(_gfxLock);
1618 _win->updateSurface();
1620 std::unique_lock lock(_gfxLock);
1623 WLog_VRB(
TAG,
"paintLegacy id=0x%08" PRIx32
" full=%d visRects=%zu",
1624 static_cast<uint32_t
>(_id), full ? 1 : 0, vis.size());