24#include <winpr/assert.h>
25#include <winpr/platform.h>
26#include <winpr/cast.h>
27#include <winpr/string.h>
29#include <freerdp/freerdp.h>
30#include <freerdp/log.h>
31#include <freerdp/codec/color.h>
32#include <freerdp/codec/region.h>
33#include <freerdp/client/rail.h>
35#include "sdl_rail.hpp"
36#include "sdl_context.hpp"
37#include "sdl_rail_platform.hpp"
38#include "sdl_types.hpp"
39#include "sdl_utils.hpp"
41#define TAG CLIENT_TAG("sdl.rail")
44[[nodiscard]]
static bool compositorDrag()
46 return railPlatformCaps().drag == RailDragModel::CompositorResize;
49SdlRail::SdlRail(
SdlContext* context) : _context(context)
55 railPlatformSetNativeLoopHandler({});
58SdlRail* SdlRail::get(rdpContext* context)
60 auto sdl = get_context(context);
63 return &sdl->getRailChannelContext();
68 std::scoped_lock lock(_windowsLock);
69 auto w = getWindow(
id);
70 if (!w || w->isDeleted() || !w->window())
72 const SDL_Rect r = w->windowRect();
81 auto it = _windows.find(
id);
82 return (it == _windows.end()) ? nullptr : &it->second;
89 for (
auto& it : _windows)
91 if (it.second.sdlId() ==
id)
98static bool adjoinsOutside(
const SDL_Rect& popup,
const SDL_Rect& win)
101 if (SDL_GetRectIntersection(&popup, &win, &hit))
103 const SDL_Rect grown = { win.x - 1, win.y - 1, win.w + 2, win.h + 2 };
104 return SDL_GetRectIntersection(&popup, &grown, &hit);
110 return (!w.isPopup() || w.isFullscreen()) && (w.window() !=
nullptr);
115 auto* owner = getWindow(ownerId);
116 if (owner && canParentPopup(*owner))
125 SdlRailWindow* chosen = resolveParent(popup.owner());
128 const SDL_Rect pr = popup.windowRect();
129 const SDL_Point origin = { pr.x, pr.y };
130 for (uint32_t
id : _zOrder)
132 auto* w = getWindow(
id);
133 if (!w || !canParentPopup(*w))
135 const SDL_Rect wr = w->windowRect();
136 if (SDL_PointInRect(&origin, &wr))
144 for (
auto& other : _windows)
146 auto& w = other.second;
147 if (!canParentPopup(w))
149 const SDL_Rect wr = w.windowRect();
150 if (SDL_PointInRect(&origin, &wr))
159 chosen = resolveParent(_focusedAppId);
162 for (
auto& other : _windows)
164 auto& w = other.second;
165 if (canParentPopup(w))
178 if (!popup.isLayered())
180 const SDL_Rect pr = popup.windowRect();
181 for (
auto& other : _windows)
183 auto& w = other.second;
184 if (canParentPopup(w) && adjoinsOutside(pr, w.windowRect()))
190bool SdlRail::ownsWindow(SDL_WindowID
id)
192 std::unique_lock lock(_windowsLock);
193 return getWindowBySdlId(
id) !=
nullptr;
196void SdlRail::invalidateWindow(SDL_WindowID
id)
198 std::unique_lock lock(_windowsLock);
199 auto* appWindow = getWindowBySdlId(
id);
202 appWindow->invalidateAll();
204 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
208void SdlRail::sendSystemCommand(
SdlRailWindow* appWindow, uint16_t command)
210 if (!_rail || !_rail->ClientSystemCommand)
212 RAIL_SYSCOMMAND_ORDER syscommand = {};
213 syscommand.windowId =
static_cast<UINT32
>(appWindow->id());
214 syscommand.command = command;
215 std::ignore = _rail->ClientSystemCommand(_rail, &syscommand);
218void SdlRail::sendWorkArea(
const SDL_Rect& local)
221 const SDL_Rect area = { local.x - _displayShift.x, local.y - _displayShift.y, local.w,
223 if (!_rail || !_rail->ClientSystemParam || (area.w <= 0) || (area.h <= 0))
225 if ((area.x < 0) || (area.y < 0))
227 WLog_WARN(
TAG,
"work area %d,%d is outside the primary monitor, not sent", area.x, area.y);
230 if (SDL_RectsEqual(&area, &_sentWorkArea))
233 RAIL_SYSPARAM_ORDER param = {};
235 param.params = SPI_MASK_SET_WORK_AREA;
236 param.workArea.left = WINPR_ASSERTING_INT_CAST(UINT16, area.x);
237 param.workArea.top = WINPR_ASSERTING_INT_CAST(UINT16, area.y);
238 param.workArea.right = WINPR_ASSERTING_INT_CAST(UINT16, area.x + area.w);
239 param.workArea.bottom = WINPR_ASSERTING_INT_CAST(UINT16, area.y + area.h);
240 if (_rail->ClientSystemParam(_rail, ¶m) == CHANNEL_RC_OK)
241 _sentWorkArea = area;
244void SdlRail::noteUsableBoundsChanged(SDL_DisplayID display)
246 if (display != SDL_GetPrimaryDisplay())
248 std::unique_lock lock(_windowsLock);
250 if (!sdl_push_user_event(SDL_EVENT_USER_UPDATE))
251 WLog_WARN(
TAG,
"sdl_push_user_event failed");
254void SdlRail::handleMaximized(SDL_WindowID
id)
256 std::unique_lock lock(_windowsLock);
257 auto* appWindow = getWindowBySdlId(
id);
258 if (!appWindow || appWindow->isPopup() || !appWindow->window())
261 if (appWindow->railMaximized())
263 WLog_DBG(
TAG,
"local maximize skipped id=0x%08" PRIx32
" (already rail-maximized)",
264 static_cast<UINT32
>(appWindow->id()));
266 if (!sdl_push_user_event(SDL_EVENT_USER_UPDATE))
267 WLog_WARN(
TAG,
"sdl_push_user_event failed");
271 followDisplay(appWindow,
true);
272 appWindow->setRailMaximized(
true);
274 appWindow->invalidateAll();
276 if (appWindow->loopEndPending())
278 appWindow->deferMaximize();
279 WLog_DBG(
TAG,
"local maximize deferred id=0x%08" PRIx32
" (modal loop open)",
280 static_cast<UINT32
>(appWindow->id()));
282 else if (appWindow->deferMaximizeUntilEcho())
285 WLog_DBG(
TAG,
"local maximize deferred id=0x%08" PRIx32
" (move not confirmed)",
286 static_cast<UINT32
>(appWindow->id()));
290 WLog_DBG(
TAG,
"local maximize id=0x%08" PRIx32
" -> SC_MAXIMIZE",
291 static_cast<UINT32
>(appWindow->id()));
292 sendSystemCommand(appWindow, SC_MAXIMIZE);
295 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
298void SdlRail::handleMinimized(SDL_WindowID
id)
300 std::unique_lock lock(_windowsLock);
301 auto* appWindow = getWindowBySdlId(
id);
302 if (!appWindow || appWindow->isPopup() || !appWindow->window())
304 if (appWindow->railMinimized())
306 appWindow->setRailMinimized(
true);
307 WLog_DBG(
TAG,
"local minimize id=0x%08" PRIx32
" -> SC_MINIMIZE",
308 static_cast<UINT32
>(appWindow->id()));
309 sendSystemCommand(appWindow, SC_MINIMIZE);
312void SdlRail::handleClose(SDL_WindowID
id)
314 std::unique_lock lock(_windowsLock);
315 auto* appWindow = getWindowBySdlId(
id);
316 if (!appWindow || !appWindow->window())
318 WLog_DBG(
TAG,
"local close id=0x%08" PRIx32
" -> SC_CLOSE",
319 static_cast<UINT32
>(appWindow->id()));
320 sendSystemCommand(appWindow, SC_CLOSE);
323void SdlRail::handleRestored(SDL_WindowID
id)
325 std::unique_lock lock(_windowsLock);
326 auto* appWindow = getWindowBySdlId(
id);
327 if (!appWindow || appWindow->isPopup() || !appWindow->window())
330 if (appWindow->railMinimized())
332 appWindow->setRailMinimized(
false);
333 appWindow->noteLocalRestore();
335 else if (appWindow->railMaximized())
336 appWindow->setRailMaximized(
false);
339 if (!sdl_push_user_event(SDL_EVENT_USER_UPDATE))
340 WLog_WARN(
TAG,
"sdl_push_user_event failed");
343 WLog_DBG(
TAG,
"local restore id=0x%08" PRIx32
" -> SC_RESTORE",
344 static_cast<UINT32
>(appWindow->id()));
345 appWindow->invalidateAll();
346 sendSystemCommand(appWindow, SC_RESTORE);
348 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
351void SdlRail::handleFocus(SDL_WindowID
id,
bool gained)
353 std::unique_lock lock(_windowsLock);
354 auto appWindow = getWindowBySdlId(
id);
355 if (!appWindow || !appWindow->activatable() || !appWindow->window())
360 if (!gained && (_localMove.id ==
static_cast<uint32_t
>(appWindow->id())))
365 _focusedAppId = appWindow->id();
380 const auto* active = getWindow(_clientActiveId);
381 if (active && (active != appWindow) &&
382 (active->owner() ==
static_cast<uint32_t
>(appWindow->id())))
386 const auto wid =
static_cast<uint32_t
>(appWindow->id());
387 sendClientActivate(wid,
true);
391void SdlRail::sendClientActivate(uint32_t wid,
bool enabled)
393 if (!_rail || !_rail->ClientActivate)
396 _clientActiveId = wid;
397 RAIL_ACTIVATE_ORDER activate = {};
398 activate.windowId = wid;
399 activate.enabled = enabled;
400 std::ignore = _rail->ClientActivate(_rail, &activate);
404void SdlRail::ensureActive(SDL_WindowID
id)
406 std::unique_lock lock(_windowsLock);
407 auto* appWindow = getWindowBySdlId(
id);
408 if (!appWindow || !appWindow->activatable() || !appWindow->window())
410 const auto wid =
static_cast<uint32_t
>(appWindow->id());
411 if (wid == _clientActiveId)
413 sendClientActivate(wid,
true);
416bool SdlRail::translateToServer(SDL_WindowID
id,
float& x,
float& y)
418 std::unique_lock lock(_windowsLock);
419 auto* appWindow = getWindowBySdlId(
id);
423 if (!appWindow->effectivelyMaximized())
424 followDisplay(appWindow,
true);
425 SDL_FPoint rpos = { x, y };
426 if (
auto* renderer = appWindow->renderer())
427 (void)SDL_RenderCoordinatesFromWindow(renderer, x, y, &rpos.x, &rpos.y);
429 SDL_Point origin = appWindow->serverOrigin();
431 if (appWindow->isPopup() && !railPlatformCaps().supportsWindowPositions &&
432 appWindow->placedRel(rel))
434 auto* sdlParent = SDL_GetWindowParent(appWindow->window());
435 if (
auto* parent = sdlParent ? getWindowBySdlId(SDL_GetWindowID(sdlParent)) :
nullptr)
437 const SDL_Rect pr = parent->outerRect();
438 origin = { pr.x + rel.x, pr.y + rel.y };
442 x = rpos.x +
static_cast<float>(origin.x - _inputOrigin.x);
443 y = rpos.y +
static_cast<float>(origin.y - _inputOrigin.y);
447void SdlRail::notePopupMoved(SDL_WindowID
id)
450 if (railPlatformCaps().supportsWindowPositions)
452 std::unique_lock lock(_windowsLock);
453 auto* w = getWindowBySdlId(
id);
454 if (!w || !w->isPopup())
458 if (!sdl_push_user_event(SDL_EVENT_USER_UPDATE))
459 WLog_WARN(
TAG,
"could not wake the paint loop for a moved popup");
462SdlRailWindow* SdlRail::addWindow(uint64_t
id,
const SDL_Rect& rect)
465 auto it = _windows.find(
id);
466 if ((it != _windows.end()) && it->second.isDeleted())
468 WLog_DBG(
TAG,
"window recreate id=0x%08" PRIx32
" reused a deleted entry",
469 static_cast<uint32_t
>(
id));
470 _deadWindows.insert(_windows.extract(it));
473 auto res = _windows.emplace(std::piecewise_construct, std::forward_as_tuple(
id),
474 std::forward_as_tuple(
id, rect));
475 res.first->second.setLocalShift(_localShift);
476 return &res.first->second;
480void SdlRail::updateCoordinates()
482 const auto* settings = _context->common()->context.settings;
485 _localShift = railPlatformCaps().supportsWindowPositions ? _displayShift : SDL_Point{ 0, 0 };
486 _inputOrigin = { 0, 0 };
488 for (UINT32 x = 0; x < count; x++)
490 const auto* mon =
static_cast<const rdpMonitor*
>(
491 freerdp_settings_get_pointer_array(settings, FreeRDP_MonitorDefArray, x));
494 _inputOrigin.x = std::min(_inputOrigin.x, mon->x);
495 _inputOrigin.y = std::min(_inputOrigin.y, mon->y);
497 WLog_DBG(
TAG,
"coordinates: display shift %d,%d window shift %d,%d input origin %d,%d",
498 _displayShift.x, _displayShift.y, _localShift.x, _localShift.y, _inputOrigin.x,
502void SdlRail::enableRemoteAppMode(
bool enable)
506 _desktopReleased =
true;
507 queueScreenSaverUpdate();
511static constexpr Uint32 kDesktopHoldMs = 10000;
513bool SdlRail::desktopMayShow()
515 if (_desktopReleased)
517 const uint64_t now = SDL_GetTicks();
518 if (_desktopHoldUntil == 0)
520 _desktopHoldUntil = now + kDesktopHoldMs;
522 if (!sdl_push_update_after(kDesktopHoldMs + 1))
523 WLog_WARN(
TAG,
"SDL_AddTimer failed: %s", SDL_GetError());
524 WLog_DBG(
TAG,
"RemoteApp starting: holding the logon desktop back");
527 if (now < _desktopHoldUntil)
529 WLog_DBG(
TAG,
"RemoteApp not started after %" PRIu32
" ms, showing the desktop",
531 _desktopReleased =
true;
535void SdlRail::noteLogonPrompt()
537 if (_desktopReleased.exchange(
true))
539 WLog_DBG(
TAG,
"logon dialog on the server, showing the desktop");
540 if (!sdl_push_user_event(SDL_EVENT_USER_UPDATE))
541 WLog_WARN(
TAG,
"sdl_push_user_event failed");
544bool SdlRail::paint(SDL_Surface* primary, SDL_PixelFormat fallbackFormat,
545 const std::vector<SDL_Rect>& damage)
550 std::unique_lock lock(_windowsLock);
553 if (_sentWorkArea.w == 0)
556 if (SDL_GetDisplayUsableBounds(SDL_GetPrimaryDisplay(), &usable))
557 sendWorkArea(usable);
570 std::vector<std::pair<size_t, SdlRailWindow*>> release;
571 for (
auto& [wid, win] : _windows)
577 bool ownerDeleted =
false;
578 for (SDL_Window* p = SDL_GetWindowParent(win.window()); p; p = SDL_GetWindowParent(p))
581 auto* owner = getWindowBySdlId(SDL_GetWindowID(p));
584 else if (owner->isDeleted())
587 if (owner->id() == win.owner())
591 if (orphan && !win.isDeleted() && (ownerDeleted || !win.isPopup()))
593 if (orphan || win.isDeleted())
594 release.emplace_back(depth, &win);
596 for (
auto& [wid, win] : _deadWindows)
601 for (SDL_Window* p = SDL_GetWindowParent(win.window()); p; p = SDL_GetWindowParent(p))
603 release.emplace_back(depth, &win);
605 std::sort(release.begin(), release.end(),
606 [](
const auto& a,
const auto& b) { return a.first > b.first; });
607 for (
auto& [depth, win] : release)
608 win->releaseWindow();
611 for (
auto it = _windows.begin(); it != _windows.end();)
612 it = it->second.isDeleted() ? _windows.erase(it) : std::next(it);
614 _deadWindows.clear();
617 for (
auto& it : _windows)
619 auto& win = it.second;
623 SDL_Window* parent =
nullptr;
624 SDL_Rect parentRect{};
625 auto* owner = resolveParent(win.owner());
626 if (owner && (owner != &win))
628 parent = owner->window();
629 parentRect = owner->outerRect();
631 const bool realized = win.window() !=
nullptr;
632 win.paint(primary, fallbackFormat, damage, parent, parentRect);
633 if (!realized && win.window())
634 _context->railWindowInfoChanged(win.id());
636 SDL_Rect refusedOuter{};
637 if (win.takeWmOverride(refusedOuter))
639 const SDL_Rect rect = win.serverRect(refusedOuter);
640 const SDL_Rect cur = win.windowRect();
642 "wm-refused adopt id=0x%08" PRIx32
643 " outer=%dx%d -> srv=%d,%d %dx%d (was %dx%d)",
644 static_cast<uint32_t
>(win.id()), refusedOuter.w, refusedOuter.h, rect.x,
645 rect.y, rect.w, rect.h, cur.w, cur.h);
646 if (!SDL_RectsEqual(&rect, &cur))
647 sendClientWindowMove(&win, rect);
648 win.adoptLocalGeometry(rect);
649 _context->railWindowInfoChanged(win.id());
652 SDL_Rect unconfirmed{};
653 if (win.takeUnconfirmedReport(unconfirmed) && !win.railMinimized() &&
654 !win.effectivelyMaximized())
656 WLog_DBG(
TAG,
"size resent id=0x%08" PRIx32
" %dx%d (not confirmed)",
657 static_cast<uint32_t
>(win.id()), unconfirmed.w, unconfirmed.h);
658 sendClientWindowMove(&win, unconfirmed);
660 if (!win.effectivelyMaximized())
661 followDisplay(&win,
false);
662 if (win.takeMaximizeAfterEcho() && win.effectivelyMaximized())
663 sendSystemCommand(&win, SC_MAXIMIZE);
664 if (win.takeRestoreRetry())
666 WLog_DBG(
TAG,
"restore resent id=0x%08" PRIx32,
static_cast<uint32_t
>(win.id()));
667 sendSystemCommand(&win, SC_RESTORE);
674 for (
auto& it : _windows)
676 if (it.second.isPopup() || !it.second.window())
678 auto* ctx = &_context->common()->context;
679 const auto dw =
static_cast<UINT16
>(
681 const auto dh =
static_cast<UINT16
>(
683 const RECTANGLE_16 all = { 0, 0, dw, dh };
684 if (ctx->update && ctx->update->RefreshRect)
685 (void)ctx->update->RefreshRect(ctx, 1, &all);
687 WLog_DBG(
TAG,
"refresh-rect sent (first app window realized)");
692 for (
auto& it : _windows)
694 auto& popup = it.second;
695 if (!popup.isPopup())
699 if (!popup.isFrame() && isShadowFrame(popup))
700 popup.setFrame(
true);
703 if (popup.isLayered())
705 constexpr int reach = 48;
706 const SDL_Rect sr = popup.windowRect();
707 bool anchored =
false;
708 for (
auto& other : _windows)
710 auto& cand = other.second;
711 if ((&cand == &popup) || !cand.isPopup() || cand.isLayered() || !cand.window() ||
712 ((SDL_GetWindowFlags(cand.window()) & SDL_WINDOW_HIDDEN) != 0))
714 SDL_Rect zone = cand.windowRect();
721 const bool inside = (sr.x >= zone.x) && (sr.y >= zone.y) &&
722 (sr.x + sr.w <= zone.x + zone.w) &&
723 (sr.y + sr.h <= zone.y + zone.h);
730 popup.setShadowAnchored(anchored);
733 SdlRailWindow* chosen =
nullptr;
735 if (
auto* pw = popup.window())
737 SDL_Window* p = SDL_GetWindowParent(pw);
738 chosen = p ? getWindowBySdlId(SDL_GetWindowID(p)) : nullptr;
742 chosen = resolvePopupParent(popup);
744 WLog_DBG(
TAG,
"popup id=0x%08" PRIx32
" has no parent app window",
745 static_cast<UINT32
>(popup.id()));
748 SDL_Window* parent =
nullptr;
749 SDL_Rect parentRect{};
752 parent = chosen->window();
753 parentRect = chosen->outerRect();
755 const bool realized = popup.window() !=
nullptr;
756 popup.paint(primary, fallbackFormat, damage, parent, parentRect);
757 if (!realized && popup.window())
758 _context->railWindowInfoChanged(popup.id());
767void SdlRail::applyZOrder()
772 if (!railPlatformCaps().supportsRestack)
774 _zOrderDirty =
false;
778 if (_localMove.id != 0)
780 WLog_VRB(
TAG,
"zorder apply deferred: local move 0x%08" PRIx32
" active", _localMove.id);
784 if (_nativeResizeId != 0)
786 const auto* resizing = getWindowBySdlId(_nativeResizeId);
787 if (resizing && !resizing->isDeleted())
792 std::vector<SDL_Window*> stack;
793 std::vector<uint32_t> applied;
794 stack.reserve(_zOrder.size());
795 applied.reserve(_zOrder.size());
796 for (uint32_t
id : _zOrder)
798 auto* w = getWindow(
id);
799 if (w && !w->isPopup() && w->window() &&
800 ((SDL_GetWindowFlags(w->window()) & SDL_WINDOW_HIDDEN) == 0))
802 stack.push_back(w->window());
803 applied.push_back(
id);
807 if (applied == _appliedZOrder)
809 _zOrderDirty =
false;
812 if (stack.size() >= 2)
814 std::ignore = railPlatformRestack(stack);
817 _appliedZOrder = std::move(applied);
818 _zOrderDirty =
false;
821UINT SdlRail::updateWindowFromSurface(gdiGfxSurface* surface)
824 return CHANNEL_RC_OK;
826 std::unique_lock lock(_windowsLock);
827 auto appWindow = getWindow(surface->windowId);
831 WLog_VRB(
TAG,
"gfx surface for untracked id=0x%08" PRIx64, surface->windowId);
832 return CHANNEL_RC_OK;
837 std::min(surface->mappedWidth ? surface->mappedWidth : surface->width, surface->width);
839 std::min(surface->mappedHeight ? surface->mappedHeight : surface->height, surface->height);
843 const RECTANGLE_16* rects = region16_rects(&surface->invalidRegion, &nbRects);
845 const bool remapped = appWindow->takeSurfaceChange(surface->surfaceId);
846 if ((nbRects == 0) && !remapped)
847 return CHANNEL_RC_OK;
848 appWindow->updateGfxSurface(surface->data, surface->scanline, w, h, rects, nbRects,
850 region16_clear(&surface->invalidRegion);
852 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
853 return CHANNEL_RC_OK;
856UINT SdlRail::UpdateWindowFromSurface(RdpgfxClientContext* context, gdiGfxSurface* surface)
858 WINPR_ASSERT(context);
859 auto gdi =
static_cast<rdpGdi*
>(context->custom);
860 if (!gdi || !gdi->context)
861 return CHANNEL_RC_OK;
862 auto sdl = get_context(gdi->context);
864 return CHANNEL_RC_OK;
865 return sdl->getRailChannelContext().updateWindowFromSurface(surface);
868bool SdlRail::init(RailClientContext* rail)
874 registerUpdateCallbacks(_context->context()->update);
877 std::unique_lock lock(_windowsLock);
879 for (
auto it = _windows.begin(); it != _windows.end();)
882 _deadWindows.insert(_windows.extract(cur));
884 auto* settings = _context->context()->settings;
885 const uint32_t caches =
889 _iconCache.assign(
static_cast<size_t>(caches) * _iconCacheEntries, {});
893 rail->ServerExecuteResult = SdlRail::server_execute_result;
894 rail->ServerGetAppIdResponse = SdlRail::server_app_id;
896 rail->ServerLocalMoveSize = SdlRail::server_local_move_size;
897 rail->ServerMinMaxInfo = SdlRail::server_min_max_info;
898 rail->ServerPowerDisplayRequest = SdlRail::server_power_display_request;
901 WLog_WARN(
TAG,
"RemoteApp/RAIL support in the SDL client is experimental");
903 std::unique_lock lock(_windowsLock);
906 const RailPlatformCaps& caps = railPlatformCaps();
907 railPlatformSetNativeLoopHandler({
this, redrawDuringSizeMove, nativeResize });
908 WLog_DBG(
TAG,
"RAIL channel initialized: driver=%s windowPositions=%d transparentWindows=%d",
909 SDL_GetCurrentVideoDriver(), caps.supportsWindowPositions ? 1 : 0,
910 caps.supportsTransparentWindows ? 1 : 0);
911#if SDL_VERSION_ATLEAST(3, 4, 10)
913 if (sdl::utils::isX11Driver())
915 if (!SDL_SetHint(SDL_HINT_VIDEO_X11_ENABLE_XSYNC_EXT,
"1"))
916 WLog_WARN(
TAG,
"SDL_SetHint(SDL_HINT_VIDEO_X11_ENABLE_XSYNC_EXT) failed: %s",
921 if (!SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH,
"1"))
922 WLog_WARN(
TAG,
"SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH) failed: %s", SDL_GetError());
927void SdlRail::updateScreenSaver()
929 const bool allowed = _context->allowScreenSaver().value_or(_enabled);
930 const bool on = allowed && !_displayRequested;
931 if (!(on ? SDL_EnableScreenSaver() : SDL_DisableScreenSaver()))
932 WLog_WARN(
TAG,
"changing the screen saver failed: %s", SDL_GetError());
935void SdlRail::queueScreenSaverUpdate()
937 if (!SDL_RunOnMainThread([](
void* rail) {
static_cast<SdlRail*
>(rail)->updateScreenSaver(); },
939 WLog_WARN(
TAG,
"could not queue the screen saver change: %s", SDL_GetError());
942bool SdlRail::uninit(WINPR_ATTR_UNUSED RailClientContext* rail)
946 std::unique_lock lock(_windowsLock);
947 _refreshSent =
false;
951 _sessionMargins = {};
956 _appliedZOrder.clear();
957 _zOrderDirty =
false;
961 WLog_DBG(
TAG,
"RAIL channel uninit, marking %zu windows for main-thread teardown",
963 for (
auto& kv : _windows)
964 kv.second.markDeleted();
968 _desktopReleased =
false;
969 _desktopHoldUntil = 0;
970 _displayRequested =
false;
971 queueScreenSaverUpdate();
973 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
977void SdlRail::registerUpdateCallbacks(rdpUpdate* update)
979 WINPR_ASSERT(update);
980 auto window = update->window;
981 WINPR_ASSERT(window);
983 window->WindowCreate = SdlRail::window_common;
984 window->WindowUpdate = SdlRail::window_common;
985 window->WindowIcon = SdlRail::window_icon;
986 window->WindowCachedIcon = SdlRail::window_cached_icon;
987 window->WindowDelete = SdlRail::window_delete;
988 window->MonitoredDesktop = SdlRail::monitored_desktop;
989 window->NonMonitoredDesktop = SdlRail::non_monitored_desktop;
996 WINPR_ASSERT(context);
997 WINPR_ASSERT(response);
998 auto rail =
static_cast<SdlRail*
>(context->custom);
1000 return CHANNEL_RC_OK;
1002 char* appId = ConvertWCharToUtf8Alloc(response->applicationId,
nullptr);
1004 return CHANNEL_RC_NO_MEMORY;
1006 std::unique_lock lock(rail->_windowsLock);
1007 if (
auto window = rail->getWindow(response->windowId))
1008 window->setAppId(appId);
1011 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
1012 return CHANNEL_RC_OK;
1015UINT SdlRail::server_execute_result(RailClientContext* context,
1018 WINPR_ASSERT(context);
1019 WINPR_ASSERT(execResult);
1020 auto rail =
static_cast<SdlRail*
>(context->custom);
1022 return CHANNEL_RC_OK;
1024 if (execResult->execResult != RAIL_EXEC_S_OK)
1026 WLog_ERR(
TAG,
"RAIL exec error: execResult=0x%04" PRIx16
" rawResult=0x%08" PRIx32,
1027 execResult->execResult, execResult->rawResult);
1029 FreeRDP_RemoteApplicationExecFromPipe))
1030 freerdp_abort_connect_context(rail->_context->context());
1034 WLog_DBG(
TAG,
"RemoteApp exec OK, enabling RAIL mode");
1035 rail->enableRemoteAppMode(
true);
1037 return CHANNEL_RC_OK;
1040UINT SdlRail::server_local_move_size(RailClientContext* context,
1043 WINPR_ASSERT(context);
1044 WINPR_ASSERT(localMoveSize);
1047 if (localMoveSize->isMoveSizeStart && (localMoveSize->moveSizeType >= RAIL_WMSZ_LEFT) &&
1048 (localMoveSize->moveSizeType <= RAIL_WMSZ_MOVE))
1050 WLog_DBG(
TAG,
"server move/size start id=0x%08" PRIx32
" type=%" PRIu16
" pos=%d,%d",
1051 localMoveSize->windowId, localMoveSize->moveSizeType, localMoveSize->posX,
1052 localMoveSize->posY);
1053 if (
auto rail =
static_cast<SdlRail*
>(context->custom))
1055 std::unique_lock lock(rail->_windowsLock);
1056 rail->_moveLoop = { localMoveSize->windowId, 0 };
1058 (void)sdl_push_user_event(SDL_EVENT_USER_RAIL_MOVE, localMoveSize->windowId,
1059 static_cast<int>(localMoveSize->moveSizeType));
1061 else if (!localMoveSize->isMoveSizeStart)
1063 WLog_DBG(
TAG,
"server move/size end id=0x%08" PRIx32, localMoveSize->windowId);
1065 SDL_WindowID sdlId = 0;
1067 auto rail =
static_cast<SdlRail*
>(context->custom);
1070 std::unique_lock lock(rail->_windowsLock);
1071 auto* appWindow = rail->getWindow(localMoveSize->windowId);
1073 const bool ownedByWaylandResize = compositorDrag() &&
1074 (rail->_localMove.id == localMoveSize->windowId) &&
1075 rail->_localMove.sawResize;
1076 if (!ownedByWaylandResize)
1079 appWindow->setLocalMoveActive(
false);
1080 if (rail->_localMove.id == localMoveSize->windowId)
1082 if (compositorDrag())
1085 rail->_localMove = {};
1087 appWindow->invalidateAll();
1088 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
1093 rail->_localMove.serverEnded =
true;
1097 if (rail->_moveLoop.id == localMoveSize->windowId)
1099 const SDL_DisplayID display = rail->_moveLoop.display;
1100 rail->_moveLoop = {};
1101 auto* moved = rail->getWindow(localMoveSize->windowId);
1102 if (moved && (display != 0) && !moved->effectivelyMaximized())
1103 rail->moveToDisplay(moved, display);
1106 if (appWindow && appWindow->loopEndPending())
1108 const auto actions = appWindow->takeLoopEnd();
1109 if (actions.maximize && appWindow->railMaximized())
1111 WLog_DBG(
TAG,
"deferred SC_MAXIMIZE id=0x%08" PRIx32, localMoveSize->windowId);
1112 rail->sendSystemCommand(appWindow, SC_MAXIMIZE);
1117 WLog_DBG(
TAG,
"deferred snap resize id=0x%08" PRIx32
" rect=%d,%d %dx%d",
1118 localMoveSize->windowId, actions.snapRect.x, actions.snapRect.y,
1119 actions.snapRect.w, actions.snapRect.h);
1120 rail->sendClientWindowMove(appWindow, actions.snapRect);
1125 if (appWindow->window() && !appWindow->effectivelyMaximized() &&
1126 !appWindow->localMoveSizeChanged())
1128 sdlId = appWindow->sdlId();
1133 rail->syncGeometry(sdlId);
1135 return CHANNEL_RC_OK;
1139bool SdlRail::dragActive(SDL_WindowID
id)
1141 return !compositorDrag() && suppressServerMotion(
id);
1144bool SdlRail::suppressServerMotion(SDL_WindowID
id)
1146 std::unique_lock lock(_windowsLock);
1147 if (_localMove.id == 0)
1149 auto* appWindow = getWindowBySdlId(
id);
1151 return appWindow && appWindow->localMoveActive();
1154void SdlRail::redrawDuringSizeMove(
void* userdata)
1156 auto* rail =
static_cast<SdlRail*
>(userdata);
1158 rail->_sizeMoveRedraws++;
1159 if (!rail->_context->drawToWindows())
1160 WLog_WARN(
TAG,
"redraw during the native size/move loop failed");
1163void SdlRail::nativeResize(
void* userdata, SDL_WindowID
id,
int moveType,
bool end)
1165 auto* rail =
static_cast<SdlRail*
>(userdata);
1167 std::unique_lock lock(rail->_windowsLock);
1171 if (rail->_localMove.id != 0)
1173 auto* appWindow = rail->getWindowBySdlId(
id);
1174 if (!appWindow || appWindow->isPopup() || appWindow->effectivelyMaximized())
1176 WLog_DBG(
TAG,
"native resize start id=0x%08" PRIx64
" type=%d", appWindow->id(), moveType);
1177 appWindow->setLocalMoveActive(
true);
1178 const RailEdges e = railEdges(
static_cast<uint16_t
>(moveType));
1179 appWindow->setResizeAnchor(e.left, e.top);
1180 rail->_nativeResizeId = id;
1183 if (rail->_nativeResizeId !=
id)
1185 rail->_nativeResizeId = 0;
1186 auto* appWindow = rail->getWindowBySdlId(
id);
1187 if (!appWindow || !appWindow->window() || appWindow->isDeleted())
1193 SDL_GetWindowSize(appWindow->window(), &w, &h);
1194 SDL_GetWindowPosition(appWindow->window(), &x, &y);
1195 WLog_DBG(
TAG,
"native resize end id=0x%08" PRIx64
" %d,%d %dx%d", appWindow->id(), x, y, w, h);
1196 rail->reportAndAdopt(appWindow, x, y, w, h);
1199void SdlRail::handleLocalMoveRequested(uint32_t windowId, uint16_t moveType)
1202 SDL_Window* sdlWindow = beginLocalMove(windowId, moveType);
1205 _sizeMoveRedraws = 0;
1206 railPlatformRunModalDrag(sdlWindow, moveType);
1207 WLog_DBG(
TAG,
"modal drag end id=0x%08" PRIx32
" redraws=%" PRIu32, windowId, _sizeMoveRedraws);
1208 completeLocalMoveIfPending();
1212static constexpr Uint32 kReleasePollMs = 100;
1214static Uint32 releasePollTimer(
void* userdata, [[maybe_unused]] SDL_TimerID timerID,
1215 [[maybe_unused]] Uint32 interval)
1217 if (!SDL_RunOnMainThread([](
void* rail) {
static_cast<SdlRail*
>(rail)->pollDragRelease(); },
1219 WLog_WARN(TAG,
"could not queue the drag release check: %s", SDL_GetError());
1223void SdlRail::pollDragRelease()
1226 std::unique_lock lock(_windowsLock);
1227 if ((_localMove.id == 0) || compositorDrag())
1230 if (SDL_GetGlobalMouseState(
nullptr,
nullptr) & SDL_BUTTON_LMASK)
1232 if (SDL_AddTimer(kReleasePollMs, releasePollTimer,
this) == 0)
1233 WLog_WARN(
TAG,
"drag release timer failed: %s", SDL_GetError());
1236 completeLocalMoveIfPending();
1239SDL_Window* SdlRail::beginLocalMove(uint32_t windowId, uint16_t moveType)
1241 const RailDragModel model = railPlatformCaps().drag;
1242 if (model == RailDragModel::None)
1244 const bool compositor = (model == RailDragModel::CompositorResize);
1246 std::unique_lock lock(_windowsLock);
1247 auto appWindow = getWindow(windowId);
1248 if (!appWindow || !appWindow->window() || appWindow->isPopup())
1251 const bool isMove = (moveType == RAIL_WMSZ_MOVE);
1252 WLog_DBG(
TAG,
"local move start id=0x%08" PRIx32
" driver=%s type=%" PRIu16
" %s", windowId,
1253 SDL_GetCurrentVideoDriver(), moveType, isMove ?
"move" :
"resize");
1254 const bool started = railPlatformBeginDrag(appWindow->window(), moveType);
1256 if (started && (!compositor || !isMove))
1259 _localMove.id = windowId;
1260 _localMove.type = moveType;
1264 _localMove.anchor = _lastPressServer;
1265 _localMove.pointer = _lastPointerServer;
1267 appWindow->clearLoopEnd();
1269 if ((model == RailDragModel::WmMoveResize) &&
1270 (SDL_GetGlobalMouseState(
nullptr,
nullptr) & SDL_BUTTON_LMASK) &&
1271 (SDL_AddTimer(kReleasePollMs, releasePollTimer,
this) == 0))
1272 WLog_WARN(
TAG,
"drag release timer failed: %s", SDL_GetError());
1274 appWindow->setLocalMoveActive(
true);
1278 const RailEdges e = railEdges(moveType);
1279 appWindow->setResizeAnchor(e.left, e.top);
1284 WLog_WARN(
TAG,
"WM move failed for RAIL window 0x%08" PRIx32, windowId);
1286 return (started && (model == RailDragModel::ModalLoop)) ? appWindow->window() :
nullptr;
1290void SdlRail::handleDisplayChanged(SDL_WindowID
id, SDL_DisplayID display)
1292 if (railPlatformCaps().supportsWindowPositions)
1294 std::unique_lock lock(_windowsLock);
1295 auto* appWindow = getWindowBySdlId(
id);
1296 if (!appWindow || appWindow->isPopup())
1298 WLog_DBG(
TAG,
"display changed id=0x%08" PRIx32
" display %" PRIu32,
1299 static_cast<uint32_t
>(appWindow->id()), display);
1300 appWindow->noteDisplayChange();
1303void SdlRail::followDisplay(
SdlRailWindow* appWindow,
bool now)
1305 if (!appWindow->takeDisplayChange(now))
1307 const SDL_DisplayID display = SDL_GetDisplayForWindow(appWindow->window());
1311 if (_moveLoop.id == appWindow->id())
1313 _moveLoop.display = display;
1316 moveToDisplay(appWindow, display);
1319bool SdlRail::monitorRect(SDL_DisplayID display, SDL_Rect& rect)
const
1321 const auto* settings = _context->common()->context.settings;
1323 for (UINT32 x = 0; x < count; x++)
1325 const auto* mon =
static_cast<const rdpMonitor*
>(
1326 freerdp_settings_get_pointer_array(settings, FreeRDP_MonitorDefArray, x));
1327 if (mon && (mon->orig_screen == display))
1329 rect = { mon->x, mon->y, mon->width, mon->height };
1336void SdlRail::moveToDisplay(
SdlRailWindow* appWindow, SDL_DisplayID display)
1339 if (!monitorRect(display, area))
1341 SDL_Rect rect = appWindow->windowRect();
1342 const SDL_Point center = { rect.x + rect.w / 2, rect.y + rect.h / 2 };
1343 if (SDL_PointInRect(¢er, &area))
1345 rect.x = area.x + std::max(0, (area.w - rect.w) / 2);
1346 rect.y = area.y + std::max(0, (area.h - rect.h) / 2);
1347 WLog_DBG(
TAG,
"window 0x%08" PRIx32
" now on display %" PRIu32
", server move to %d,%d",
1348 static_cast<uint32_t
>(appWindow->id()), display, rect.x, rect.y);
1349 sendClientWindowMove(appWindow, rect);
1350 appWindow->adoptLocalGeometry(rect);
1356void SdlRail::sendClientWindowMove(
SdlRailWindow* appWindow,
const SDL_Rect& serverRect)
1358 const SDL_Rect fm = appWindow->frameMargins();
1359 RAIL_WINDOW_MOVE_ORDER move = {};
1360 move.windowId =
static_cast<UINT32
>(appWindow->id());
1361 move.left = WINPR_ASSERTING_INT_CAST(INT16, serverRect.x - fm.x);
1362 move.top = WINPR_ASSERTING_INT_CAST(INT16, serverRect.y - fm.y);
1363 move.right = WINPR_ASSERTING_INT_CAST(INT16, serverRect.x + serverRect.w + fm.w);
1364 move.bottom = WINPR_ASSERTING_INT_CAST(INT16, serverRect.y + serverRect.h + fm.h);
1365 if (_rail && _rail->ClientWindowMove &&
1366 (_rail->ClientWindowMove(_rail, &move) != CHANNEL_RC_OK))
1367 WLog_WARN(
TAG,
"ClientWindowMove failed for RAIL window 0x%08" PRIx32, move.windowId);
1371void SdlRail::reportAndAdopt(
SdlRailWindow* appWindow,
int x,
int y,
int w,
int h)
1374 const SDL_Rect rect = appWindow->serverRect({ x, y, w, h });
1376 const bool maximized = appWindow->effectivelyMaximized();
1378 const SDL_Rect cur = appWindow->windowRect();
1379 const bool unchanged = SDL_RectsEqual(&rect, &cur);
1380 if (!maximized && !unchanged)
1381 sendClientWindowMove(appWindow, rect);
1385 appWindow->setLocalMoveActive(
false);
1387 appWindow->adoptLocalGeometry(rect);
1388 _context->railWindowInfoChanged(appWindow->id());
1390 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
1393void SdlRail::clampIntoDesktop(
const SdlRailWindow& appWindow,
int& x,
int& y,
int w,
int h)
const
1395 auto* settings = _context->context()->settings;
1399 const SDL_Rect s = appWindow.serverRect({ x, y, w, h });
1400 SDL_Rect area = { _inputOrigin.x, _inputOrigin.y, dw, dh };
1403 if (!railPlatformCaps().supportsWindowPositions &&
1404 monitorRect(SDL_GetDisplayForWindow(appWindow.window()), mon) && (s.w <= mon.w) &&
1407 x += std::clamp(s.x, area.x, area.x + std::max(0, area.w - s.w)) - s.x;
1408 y += std::clamp(s.y, area.y, area.y + std::max(0, area.h - s.h)) - s.y;
1411void SdlRail::noteDragResize(SDL_WindowID
id,
int w,
int h)
1413 std::unique_lock lock(_windowsLock);
1414 if ((_localMove.id == 0) || compositorDrag() || (_localMove.type != RAIL_WMSZ_MOVE))
1416 auto* appWindow = getWindowBySdlId(
id);
1417 if (!appWindow || (
static_cast<uint32_t
>(appWindow->id()) != _localMove.id))
1420 if (appWindow->localMoveSizeChanged())
1422 const SDL_Rect outer = appWindow->outerRect();
1423 if ((w == outer.w) && (h == outer.h))
1426 _localMove.wmSized =
true;
1427 _localMove.wmSize = { w, h };
1431void SdlRail::reportMaximizedWorkArea(
SdlRailWindow* appWindow)
1434 if ((SDL_GetWindowFlags(appWindow->window()) & SDL_WINDOW_MAXIMIZED) == 0)
1438 SDL_GetWindowSize(appWindow->window(), &w, &h);
1439 if ((w <= 0) || (h <= 0))
1443 if (!SDL_GetDisplayBounds(SDL_GetPrimaryDisplay(), &primary))
1448 if (railPlatformCaps().supportsWindowPositions)
1449 SDL_GetWindowPosition(appWindow->window(), &x, &y);
1450 const SDL_Rect area = { x, y, w, h };
1452 if (!SDL_GetRectIntersection(&area, &primary, &overlap) || !SDL_RectsEqual(&overlap, &area))
1457void SdlRail::syncGeometry(SDL_WindowID
id)
1459 std::unique_lock lock(_windowsLock);
1460 auto* w = getWindowBySdlId(
id);
1461 if (!w || !w->window() || w->isDeleted() || w->isPopup())
1465 if (w->effectivelyMaximized())
1467 reportMaximizedWorkArea(w);
1470 if (w->railMinimized())
1473 const auto wid =
static_cast<uint32_t
>(w->id());
1475 if (w->localMoveActive() || (_localMove.id == wid) || w->loopEndPending() ||
1476 w->stateTransitionPending() || w->awaitingRestoreRect() || w->insetsStale())
1481 SDL_GetWindowSize(w->window(), &lw, &lh);
1482 if ((lw <= 0) || (lh <= 0))
1486 const SDL_Rect ins = w->insets();
1487 if (((lw - ins.x - ins.w) <= 0) || ((lh - ins.y - ins.h) <= 0))
1489 WLog_DBG(
TAG,
"geometry sync id=0x%08" PRIx32
" %dx%d dropped (content collapsed)", wid, lw,
1494 const SDL_Rect vis = w->outerRect();
1495 const bool posKnown = railPlatformCaps().supportsWindowPositions;
1499 SDL_GetWindowPosition(w->window(), &lx, &ly);
1501 clampIntoDesktop(*w, lx, ly, lw, lh);
1504 if ((lw == vis.w) && (lh == vis.h) && (!posKnown || ((lx == vis.x) && (ly == vis.y))))
1506 w->clearGeomApplyPending();
1511 if (w->geomApplyPending())
1514 const bool overtaken = !posKnown && ((lw > vis.w) || (lh > vis.h));
1517 w->clearGeomApplyPending();
1520 WLog_DBG(
TAG,
"geometry sync id=0x%08" PRIx32
" local=%d,%d %dx%d (sync)", wid, lx, ly, lw, lh);
1521 reportAndAdopt(w, lx, ly, lw, lh);
1525static constexpr Uint32 kResizeSettleMs = 500;
1527static Uint32 resizeSettleTimer(
void* userdata, [[maybe_unused]] SDL_TimerID timerID,
1528 [[maybe_unused]] Uint32 interval)
1530 if (!SDL_RunOnMainThread([](
void* rail) {
static_cast<SdlRail*
>(rail)->settleWaylandResize(); },
1532 WLog_WARN(TAG,
"could not queue the resize settle check: %s", SDL_GetError());
1536void SdlRail::settleWaylandResize()
1539 std::unique_lock lock(_windowsLock);
1540 if ((_localMove.id == 0) || !compositorDrag() || !_localMove.sawResize ||
1541 (SDL_GetTicks() - _localMove.lastResize < kResizeSettleMs))
1544 completeWaylandResize();
1547void SdlRail::noteResizeGrab(SDL_WindowID
id)
1549 std::unique_lock lock(_windowsLock);
1550 if (_localMove.id == 0)
1552 auto* appWindow = getWindowBySdlId(
id);
1554 if (!appWindow || (
static_cast<uint32_t
>(appWindow->id()) != _localMove.id) ||
1557 _localMove.sawResize =
true;
1558 _localMove.lastResize = SDL_GetTicks();
1559 if (SDL_AddTimer(kResizeSettleMs, resizeSettleTimer,
this) == 0)
1560 WLog_WARN(
TAG,
"resize settle timer failed: %s", SDL_GetError());
1563void SdlRail::completeWaylandResize(
bool definitive)
1565 std::unique_lock lock(_windowsLock);
1566 if ((_localMove.id == 0) || !compositorDrag())
1569 if (!_localMove.sawResize)
1573 if (
auto* appWindow = getWindow(_localMove.id))
1575 appWindow->setLocalMoveActive(
false);
1578 appWindow->invalidateAll();
1579 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
1587 const LocalMove drag = _localMove;
1590 auto appWindow = getWindow(drag.id);
1591 if (!appWindow || !appWindow->window())
1594 (void)SDL_SyncWindow(appWindow->window());
1597 SDL_GetWindowSize(appWindow->window(), &w, &h);
1600 const SDL_Rect start = appWindow->outerRect();
1601 const RailEdges e = railEdges(drag.type);
1602 int x = e.left ? (start.x + start.w - w) : start.x;
1603 int y = e.top ? (start.y + start.h - h) : start.y;
1604 clampIntoDesktop(*appWindow, x, y, w, h);
1605 reportAndAdopt(appWindow, x, y, w, h);
1608void SdlRail::completeLocalMoveIfPending()
1610 std::unique_lock lock(_windowsLock);
1611 if (_localMove.id == 0)
1614 if (compositorDrag())
1618 const LocalMove drag = _localMove;
1621 auto appWindow = getWindow(drag.id);
1622 if (!appWindow || !appWindow->window() || appWindow->isDeleted())
1626 if ((SDL_GetMouseState(
nullptr,
nullptr) & SDL_BUTTON_LMASK) &&
1627 !(SDL_GetGlobalMouseState(
nullptr,
nullptr) & SDL_BUTTON_LMASK))
1628 if (!railPlatformReleaseButton(appWindow->window()))
1629 WLog_WARN(
TAG,
"synthetic button release failed");
1632 (void)SDL_SyncWindow(appWindow->window());
1638 SDL_GetWindowSize(appWindow->window(), &w, &h);
1639 SDL_GetWindowPosition(appWindow->window(), &x, &y);
1640 const bool isMove = (drag.type == RAIL_WMSZ_MOVE);
1641 const SDL_Rect start = appWindow->outerRect();
1646 if (!appWindow->effectivelyMaximized() && drag.wmSized &&
1647 !appWindow->localMoveSizeChanged())
1652 appWindow->deferSnap(appWindow->serverRect({ x, y, w, h }));
1661 if (!drag.serverEnded)
1663 int px = drag.anchor.x;
1664 int py = drag.anchor.y;
1665 if (isMove && appWindow->localMoveSizeChanged())
1668 const SDL_Point sp = appWindow->localMoveServerPos();
1669 const SDL_Rect now = appWindow->serverRect({ x, y, w, h });
1670 px = drag.pointer.x + now.x - sp.x;
1671 py = drag.pointer.y + now.y - sp.y;
1673 else if (!appWindow->effectivelyMaximized())
1675 const RailEdges e = railEdges(drag.type);
1676 if (isMove || e.left)
1679 px += (x + w) - (start.x + start.w);
1680 if (isMove || e.top)
1683 py += (y + h) - (start.y + start.h);
1685 (void)freerdp_client_send_button_event(_context->common(), FALSE, PTR_FLAGS_BUTTON1, px,
1688 appWindow->armLoopEnd();
1691 const SDL_Rect rect = appWindow->serverRect({ x, y, w, h });
1692 const bool restoredMidDrag = appWindow->localMoveSizeChanged();
1693 appWindow->adoptLocalGeometry(rect);
1694 _context->railWindowInfoChanged(appWindow->id());
1696 if (restoredMidDrag)
1697 appWindow->markGeometryDirty();
1698 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
1703 clampIntoDesktop(*appWindow, x, y, w, h);
1704 reportAndAdopt(appWindow, x, y, w, h);
1707 railPlatformEndDrag(appWindow->window());
1710UINT SdlRail::server_power_display_request(RailClientContext* context,
1713 WINPR_ASSERT(context);
1714 WINPR_ASSERT(power);
1715 auto rail =
static_cast<SdlRail*
>(context->custom);
1717 return CHANNEL_RC_OK;
1719 WLog_DBG(
TAG,
"power display request %s", power->active ?
"on" :
"off");
1720 rail->_displayRequested = (power->active != 0);
1721 rail->queueScreenSaverUpdate();
1722 return CHANNEL_RC_OK;
1725UINT SdlRail::server_min_max_info(RailClientContext* context,
1728 WINPR_ASSERT(context);
1729 WINPR_ASSERT(minMaxInfo);
1730 auto rail =
static_cast<SdlRail*
>(context->custom);
1732 return CHANNEL_RC_OK;
1734 std::unique_lock lock(rail->_windowsLock);
1735 auto appWindow = rail->getWindow(minMaxInfo->windowId);
1738 WLog_VRB(
TAG,
"server minmax id=0x%08" PRIx32
" min=%dx%d max=%dx%d", minMaxInfo->windowId,
1739 minMaxInfo->minTrackWidth, minMaxInfo->minTrackHeight, minMaxInfo->maxTrackWidth,
1740 minMaxInfo->maxTrackHeight);
1741 appWindow->setMinMaxSize({ minMaxInfo->minTrackWidth, minMaxInfo->minTrackHeight },
1742 { minMaxInfo->maxTrackWidth, minMaxInfo->maxTrackHeight });
1744 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
1746 return CHANNEL_RC_OK;
1752static bool marginsSet(
const SDL_Rect& m)
1754 return m.x || m.y || m.w || m.h;
1761 const UINT32 fieldFlags = orderInfo->fieldFlags;
1762 SDL_Rect m = appWindow->frameMargins();
1763 if (fieldFlags & WINDOW_ORDER_FIELD_RESIZE_MARGIN_X)
1765 m.x =
static_cast<int>(state->resizeMarginLeft);
1766 m.w =
static_cast<int>(state->resizeMarginRight);
1768 if (fieldFlags & WINDOW_ORDER_FIELD_RESIZE_MARGIN_Y)
1770 m.y =
static_cast<int>(state->resizeMarginTop);
1771 m.h =
static_cast<int>(state->resizeMarginBottom);
1774 appWindow->setFrameMargins(m);
1775 SDL_Rect& sm = _sessionMargins;
1776 if (marginsSet(m) && !marginsSet(sm))
1780 for (
auto& [
id, w] : _windows)
1781 if (!marginsSet(w.resizeMargins()))
1782 w.setResizeMargins(m.x, m.y, m.w, m.h);
1784 const SDL_Rect bandSrc = marginsSet(m) ? m : sm;
1785 appWindow->setResizeMargins(bandSrc.x, bandSrc.y, bandSrc.w, bandSrc.h);
1786 WLog_DBG(
TAG,
"margins id=0x%08" PRIx32
" raw L%d T%d R%d B%d", orderInfo->windowId, m.x, m.y,
1794 WLog_DBG(
TAG,
"server showState id=0x%08" PRIx32
" state=0x%02" PRIx32, orderInfo->windowId,
1796 appWindow->setVisible(state->showState != WINDOW_HIDE);
1797 switch (state->showState)
1799 case WINDOW_SHOW_MAXIMIZED:
1800 appWindow->setServerMaximized(
true);
1801 appWindow->setServerMinimized(
false);
1803 case WINDOW_SHOW_MINIMIZED:
1804 appWindow->setServerMinimized(
true);
1807 appWindow->setServerMaximized(
false);
1808 appWindow->setServerMinimized(
false);
1819 std::vector<SDL_Rect> rects;
1820 rects.reserve(state->numVisibilityRects);
1821 for (UINT32 i = 0; i < state->numVisibilityRects; i++)
1823 const RECTANGLE_16& r = state->visibilityRects[i];
1824 rects.push_back({ r.left, r.top, r.right - r.left, r.bottom - r.top });
1827 WLog_DBG(
TAG,
"visrects id=0x%08" PRIx32
" n=0", orderInfo->windowId);
1829 WLog_DBG(
TAG,
"visrects id=0x%08" PRIx32
" n=%zu first=%dx%d+%d+%d", orderInfo->windowId,
1830 rects.size(), rects.at(0).w, rects.at(0).h, rects.at(0).x, rects.at(0).y);
1831 appWindow->setVisibilityRects(std::move(rects));
1834BOOL SdlRail::window_common(rdpContext* context,
const WINDOW_ORDER_INFO* orderInfo,
1837 WINPR_ASSERT(orderInfo);
1838 WINPR_ASSERT(windowState);
1839 auto rail = SdlRail::get(context);
1843 const UINT32 fieldFlags = orderInfo->fieldFlags;
1844 std::unique_lock lock(rail->_windowsLock);
1845 auto appWindow = rail->getWindow(orderInfo->windowId);
1847 if (fieldFlags & WINDOW_ORDER_STATE_NEW)
1849 const SDL_Rect rect = {
static_cast<int>(windowState->windowOffsetX),
1850 static_cast<int>(windowState->windowOffsetY),
1851 static_cast<int>(windowState->windowWidth),
1852 static_cast<int>(windowState->windowHeight) };
1853 appWindow = rail->addWindow(orderInfo->windowId, rect);
1858 "window create id=0x%08" PRIx32
" %dx%d+%d+%d style=0x%08" PRIx32
1859 " ex=0x%08" PRIx32
" owner=0x%08" PRIx32,
1860 orderInfo->windowId, rect.w, rect.h, rect.x, rect.y, windowState->style,
1861 windowState->extendedStyle, windowState->ownerWindowId);
1863 const SDL_Rect& sm = rail->_sessionMargins;
1865 appWindow->setResizeMargins(sm.x, sm.y, sm.w, sm.h);
1869 if (rail->_rail && rail->_rail->ClientGetAppIdRequest)
1871 const RAIL_GET_APPID_REQ_ORDER request = { orderInfo->windowId };
1872 if (rail->_rail->ClientGetAppIdRequest(rail->_rail, &request) != CHANNEL_RC_OK)
1873 WLog_WARN(
TAG,
"Get AppID request failed for window 0x%08" PRIx32,
1874 orderInfo->windowId);
1885 WLog_WARN(
TAG,
"ignoring order flags=0x%08" PRIx32
" for unknown window id=0x%08" PRIx32,
1886 fieldFlags, orderInfo->windowId);
1891 if (fieldFlags & WINDOW_ORDER_FIELD_SHOW)
1892 updateShowState(appWindow, orderInfo, windowState);
1894 if (fieldFlags & (WINDOW_ORDER_FIELD_WND_OFFSET | WINDOW_ORDER_FIELD_WND_SIZE))
1896 SDL_Rect r = appWindow->windowRect();
1897 if (fieldFlags & WINDOW_ORDER_FIELD_WND_OFFSET)
1899 r.x =
static_cast<int>(windowState->windowOffsetX);
1900 r.y =
static_cast<int>(windowState->windowOffsetY);
1902 if (fieldFlags & WINDOW_ORDER_FIELD_WND_SIZE)
1904 r.w =
static_cast<int>(windowState->windowWidth);
1905 r.h =
static_cast<int>(windowState->windowHeight);
1907 WLog_VRB(
TAG,
"server geom id=0x%08" PRIx32
" rect=%d,%d %dx%d", orderInfo->windowId, r.x,
1909 appWindow->updateWindowRect(r);
1910 rail->_context->railWindowInfoChanged(orderInfo->windowId);
1912 if (fieldFlags & WINDOW_ORDER_FIELD_OWNER)
1913 appWindow->setOwner(windowState->ownerWindowId);
1914 if (fieldFlags & (WINDOW_ORDER_FIELD_RESIZE_MARGIN_X | WINDOW_ORDER_FIELD_RESIZE_MARGIN_Y))
1915 rail->updateMargins(appWindow, orderInfo, windowState);
1916 if (fieldFlags & WINDOW_ORDER_FIELD_STYLE)
1917 appWindow->setStyle(windowState->style, windowState->extendedStyle);
1918 if (fieldFlags & WINDOW_ORDER_FIELD_TITLE)
1920 char* title = rail_string_to_utf8_string(&windowState->titleInfo);
1923 appWindow->setTitle(title);
1928 if (fieldFlags & WINDOW_ORDER_FIELD_VIS_OFFSET)
1930 WLog_DBG(
TAG,
"visoff id=0x%08" PRIx32
" %d,%d", orderInfo->windowId,
1931 windowState->visibleOffsetX, windowState->visibleOffsetY);
1932 appWindow->setVisibleOffset({
static_cast<int>(windowState->visibleOffsetX),
1933 static_cast<int>(windowState->visibleOffsetY) });
1935 if (fieldFlags & WINDOW_ORDER_FIELD_VISIBILITY)
1936 updateVisRects(appWindow, orderInfo, windowState);
1939 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
1943SdlRailIcon* SdlRail::iconCacheLookup(uint32_t cacheId, uint32_t cacheEntry)
1945 if (cacheId == 0xFF)
1946 return &_iconScratch;
1947 const size_t idx =
static_cast<size_t>(cacheId) * _iconCacheEntries + cacheEntry;
1948 if ((_iconCacheEntries == 0) || (cacheEntry >= _iconCacheEntries) || (idx >= _iconCache.size()))
1950 return &_iconCache.at(idx);
1961 const size_t pixels = 1ULL * info->width * info->height;
1962 if (pixels > info->cbBitsColor)
1968 icon.w = info->width;
1969 icon.h = info->height;
1970 icon.bgra.assign(4ULL * pixels, 0);
1971 if (freerdp_image_copy_from_icon_data(
1972 icon.bgra.data(), PIXEL_FORMAT_BGRA32, 0, 0, 0,
1973 WINPR_ASSERTING_INT_CAST(UINT16, info->width),
1974 WINPR_ASSERTING_INT_CAST(UINT16, info->height), info->bitsColor,
1975 WINPR_ASSERTING_INT_CAST(UINT16, info->cbBitsColor), info->bitsMask,
1976 WINPR_ASSERTING_INT_CAST(UINT16, info->cbBitsMask), info->colorTable,
1977 WINPR_ASSERTING_INT_CAST(UINT16, info->cbColorTable), info->bpp) != TRUE)
1985BOOL SdlRail::window_icon(rdpContext* context,
const WINDOW_ORDER_INFO* orderInfo,
1988 WINPR_ASSERT(orderInfo);
1989 WINPR_ASSERT(windowIcon);
1990 WINPR_ASSERT(windowIcon->iconInfo);
1991 auto rail = SdlRail::get(context);
1995 std::unique_lock lock(rail->_windowsLock);
1996 auto appWindow = rail->getWindow(orderInfo->windowId);
2001 const ICON_INFO* info = windowIcon->iconInfo;
2002 auto* icon = rail->iconCacheLookup(info->cacheId, info->cacheEntry);
2003 if (!icon || !convertRailIcon(info, *icon))
2005 WLog_WARN(
TAG,
"failed to decode icon %02" PRIX32
":%04" PRIX32
" for window 0x%08" PRIx32,
2006 info->cacheId, info->cacheEntry, orderInfo->windowId);
2009 appWindow->setIcon(*icon);
2011 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
2015BOOL SdlRail::window_cached_icon(rdpContext* context,
const WINDOW_ORDER_INFO* orderInfo,
2018 WINPR_ASSERT(orderInfo);
2019 WINPR_ASSERT(windowCachedIcon);
2020 auto rail = SdlRail::get(context);
2024 std::unique_lock lock(rail->_windowsLock);
2025 auto appWindow = rail->getWindow(orderInfo->windowId);
2029 const CACHED_ICON_INFO& cached = windowCachedIcon->cachedIcon;
2030 auto* icon = rail->iconCacheLookup(cached.cacheId, cached.cacheEntry);
2031 if (!icon || icon->bgra.empty())
2034 "cached icon %02" PRIX32
":%04" PRIX32
" not in cache (window 0x%08" PRIx32
")",
2035 cached.cacheId, cached.cacheEntry, orderInfo->windowId);
2038 appWindow->setIcon(*icon);
2040 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
2044BOOL SdlRail::window_delete(rdpContext* context,
const WINDOW_ORDER_INFO* orderInfo)
2046 WINPR_ASSERT(orderInfo);
2047 auto rail = SdlRail::get(context);
2052 std::unique_lock lock(rail->_windowsLock);
2053 auto appWindow = rail->getWindow(orderInfo->windowId);
2056 WLog_DBG(
TAG,
"window delete id=0x%08" PRIx32, orderInfo->windowId);
2057 appWindow->markDeleted();
2058 rail->_context->railWindowInfoChanged(orderInfo->windowId);
2062 if (rail->_localMove.id == orderInfo->windowId)
2063 rail->_localMove = {};
2064 if (rail->_clientActiveId == orderInfo->windowId)
2065 rail->_clientActiveId = 0;
2066 if (rail->_focusedAppId == orderInfo->windowId)
2067 rail->_focusedAppId = 0;
2069 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
2073BOOL SdlRail::monitored_desktop(rdpContext* context,
const WINDOW_ORDER_INFO* orderInfo,
2076 WINPR_ASSERT(orderInfo);
2077 WINPR_ASSERT(monitoredDesktop);
2078 auto rail = SdlRail::get(context);
2083 if (orderInfo->fieldFlags & WINDOW_ORDER_FIELD_DESKTOP_ARC_COMPLETED)
2085 rail->enableRemoteAppMode(
true);
2089 if (app && (strnlen(app, 1) > 0) && !rail->_appStarted)
2091 WLog_DBG(
TAG,
"RAIL mode enabled (monitored desktop); launching '%s'", app);
2092 if (client_rail_server_start_cmd(rail->_rail) != CHANNEL_RC_OK)
2094 WLog_ERR(
TAG,
"client_rail_server_start_cmd failed for '%s'", app);
2097 rail->_appStarted =
true;
2102 if (orderInfo->fieldFlags &
2103 (WINDOW_ORDER_FIELD_DESKTOP_ZORDER | WINDOW_ORDER_FIELD_DESKTOP_ACTIVE_WND))
2105 std::unique_lock lock(rail->_windowsLock);
2106 if (orderInfo->fieldFlags & WINDOW_ORDER_FIELD_DESKTOP_ACTIVE_WND)
2110 rail->_clientActiveId = monitoredDesktop->activeWindowId;
2112 if (orderInfo->fieldFlags & WINDOW_ORDER_FIELD_DESKTOP_ZORDER)
2114 if (monitoredDesktop->windowIds && (monitoredDesktop->numWindowIds > 0))
2115 rail->_zOrder.assign(monitoredDesktop->windowIds,
2116 monitoredDesktop->windowIds + monitoredDesktop->numWindowIds);
2118 rail->_zOrder.clear();
2119 rail->_zOrderDirty =
true;
2120 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
2126BOOL SdlRail::non_monitored_desktop(rdpContext* context,
2129 WINPR_ASSERT(orderInfo);
2130 auto rail = SdlRail::get(context);
2133 WLog_DBG(
TAG,
"RAIL mode disabled (non-monitored desktop)");
2134 rail->enableRemoteAppMode(
false);
bool windowGeometry(uint64_t id, SDL_Window *&window, int &width, int &height)
WINPR_ATTR_NODISCARD FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.
WINPR_ATTR_NODISCARD FREERDP_API INT32 freerdp_settings_get_int32(const rdpSettings *settings, FreeRDP_Settings_Keys_Int32 id)
Returns a INT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.