FreeRDP
Loading...
Searching...
No Matches
SdlRail Class Reference

#include <sdl_rail.hpp>

Public Member Functions

 SdlRail (SdlContext *context)
 SdlRail (const SdlRail &)=delete
 SdlRail (SdlRail &&)=delete
SdlRail & operator= (const SdlRail &)=delete
SdlRail & operator= (SdlRail &&)=delete
bool init (RailClientContext *rail)
bool uninit (RailClientContext *rail)
bool enabled () const
bool desktopMayShow ()
void noteLogonPrompt ()
bool paint (SDL_Surface *primary, SDL_PixelFormat fallbackFormat, const std::vector< SDL_Rect > &damage)
UINT updateWindowFromSurface (gdiGfxSurface *surface)
bool ownsWindow (SDL_WindowID id)
void invalidateWindow (SDL_WindowID id)
void handleMaximized (SDL_WindowID id)
void handleMinimized (SDL_WindowID id)
void handleRestored (SDL_WindowID id)
void handleClose (SDL_WindowID id)
void handleFocus (SDL_WindowID id, bool gained)
void handleDisplayChanged (SDL_WindowID id, SDL_DisplayID display)
void noteUsableBoundsChanged (SDL_DisplayID display)
void updateCoordinates ()
void ensureActive (SDL_WindowID id)
void inputWindows (SDL_WindowID id, std::vector< SdlInputWindow > &windows, std::optional< size_t > &source)
void notePopupMoved (SDL_WindowID id)
void noteLeftPress (float x, float y)
void noteServerPointer (float x, float y)
uint32_t pointerEpoch () const
bool dragActive (SDL_WindowID id)
bool suppressServerMotion (SDL_WindowID id)
void handleLocalMoveRequested (uint32_t windowId, uint16_t moveType)
void noteDragResize (SDL_WindowID id, int w, int h)
void syncGeometry (SDL_WindowID id)
void completeLocalMoveIfPending ()
void completeWaylandResize (bool definitive=false)
void noteResizeGrab (SDL_WindowID id)
void settleWaylandResize ()
void pollDragRelease ()
bool windowGeometry (uint64_t id, SDL_Window *&window, int &width, int &height)

Static Public Member Functions

static UINT UpdateWindowFromSurface (RdpgfxClientContext *context, gdiGfxSurface *surface)

Detailed Description

RAIL channel handler for the SDL3 client.

Server-authoritative: the server drives window geometry via WINDOW_ORDER and the local borderless windows follow (the RemoteApp frame is server-drawn content). Interactive move/resize is handed to the local WM only on a server request (ServerLocalMoveSize).

Definition at line 47 of file sdl_rail.hpp.

Constructor & Destructor Documentation

◆ SdlRail()

SdlRail::SdlRail ( SdlContext * context)
explicit

Definition at line 50 of file sdl_rail.cpp.

50 : _context(context)
51{
52}

◆ ~SdlRail()

SdlRail::~SdlRail ( )

Definition at line 54 of file sdl_rail.cpp.

55{
56 railPlatformSetNativeLoopHandler({});
57}

Member Function Documentation

◆ completeLocalMoveIfPending()

void SdlRail::completeLocalMoveIfPending ( )

Definition at line 1654 of file sdl_rail.cpp.

1655{
1656 std::unique_lock lock(_windowsLock);
1657 if (_localMove.id == 0)
1658 return;
1659 /* Skip Wayland button-up finalize. */
1660 if (compositorDrag())
1661 return;
1662
1663 /* Snapshot and clear drag state atomically under lock. */
1664 const LocalMove drag = _localMove;
1665 _localMove = {};
1666
1667 auto appWindow = getWindow(drag.id);
1668 if (!appWindow || !appWindow->window() || appWindow->isDeleted())
1669 return; /* window gone mid-drag: nothing to release into or report for */
1670
1671 /* Send synthetic button release if WM grab consumed physical release. */
1672 if ((SDL_GetMouseState(nullptr, nullptr) & SDL_BUTTON_LMASK) &&
1673 !(SDL_GetGlobalMouseState(nullptr, nullptr) & SDL_BUTTON_LMASK))
1674 if (!railPlatformReleaseButton(appWindow->window()))
1675 WLog_WARN(TAG, "synthetic button release failed");
1676
1677 /* Finalize WM resize before reading final size. */
1678 (void)SDL_SyncWindow(appWindow->window());
1679
1680 int x = 0;
1681 int y = 0;
1682 int w = 0;
1683 int h = 0;
1684 SDL_GetWindowSize(appWindow->window(), &w, &h);
1685 SDL_GetWindowPosition(appWindow->window(), &x, &y);
1686 const bool isMove = (drag.type == RAIL_WMSZ_MOVE);
1687 const SDL_Rect start = appWindow->outerRect(); /* position frozen at drag start */
1688 /* Preserve server size during move; defer WM snap/tile size sync until loop END to avoid
1689 * server drag-restore conflict. */
1690 if (isMove)
1691 {
1692 if (!appWindow->effectivelyMaximized() && drag.wmSized &&
1693 !appWindow->localMoveSizeChanged())
1694 {
1695 /* Adopt WM size if window was resized during move. */
1696 w = drag.wmSize.x;
1697 h = drag.wmSize.y;
1698 appWindow->deferSnap(appWindow->serverRect({ x, y, w, h }));
1699 }
1700 else
1701 {
1702 w = start.w;
1703 h = start.h;
1704 }
1705 }
1706 /* Finalize modal loop unless server closed it early. */
1707 if (!drag.serverEnded)
1708 {
1709 int px = drag.anchor.x;
1710 int py = drag.anchor.y;
1711 if (isMove && appWindow->localMoveSizeChanged())
1712 {
1713 /* Measure delta from server anchor on drag-restore. */
1714 const SDL_Point sp = appWindow->localMoveServerPos();
1715 const SDL_Rect now = appWindow->serverRect({ x, y, w, h });
1716 px = drag.pointer.x + now.x - sp.x;
1717 py = drag.pointer.y + now.y - sp.y;
1718 }
1719 else if (!appWindow->effectivelyMaximized())
1720 {
1721 const RailEdges e = railEdges(drag.type);
1722 if (isMove || e.left)
1723 px += x - start.x;
1724 else if (e.right)
1725 px += (x + w) - (start.x + start.w);
1726 if (isMove || e.top)
1727 py += y - start.y;
1728 else if (e.bottom)
1729 py += (y + h) - (start.y + start.h);
1730 }
1731 (void)freerdp_client_send_button_event(_context->common(), FALSE, PTR_FLAGS_BUTTON1, px,
1732 py);
1733 /* Server loop unwinding: wait for explicit END order to gate deferred SC_MAXIMIZE. */
1734 appWindow->armLoopEnd();
1735
1736 /* Adopt local geometry without sending ClientWindowMove during mouse drags. */
1737 const SDL_Rect rect = appWindow->serverRect({ x, y, w, h });
1738 const bool restoredMidDrag = appWindow->localMoveSizeChanged();
1739 appWindow->adoptLocalGeometry(rect);
1740 _context->railWindowInfoChanged(appWindow->id());
1741 /* Drag complete: apply pending server resize delayed during WM grab. */
1742 if (restoredMidDrag)
1743 appWindow->markGeometryDirty();
1744 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
1745 }
1746 else if (!isMove)
1747 {
1748 /* Sync final resize geometry if server closed loop early. */
1749 clampIntoDesktop(*appWindow, x, y, w, h);
1750 reportAndAdopt(appWindow, x, y, w, h);
1751 }
1752
1753 railPlatformEndDrag(appWindow->window());
1754}
#define TAG

◆ completeWaylandResize()

void SdlRail::completeWaylandResize ( bool definitive = false)

Definition at line 1609 of file sdl_rail.cpp.

1610{
1611 std::unique_lock lock(_windowsLock);
1612 if ((_localMove.id == 0) || !compositorDrag())
1613 return;
1614 /* Finalize drag resize on window enter if resize occurred. */
1615 if (!_localMove.sawResize)
1616 {
1617 if (definitive)
1618 {
1619 if (auto* appWindow = getWindow(_localMove.id))
1620 {
1621 appWindow->setLocalMoveActive(false);
1622 /* Repaint now: the dashed placeholder is on screen and an undamaged window would
1623 * otherwise keep it until the next server frame. */
1624 appWindow->invalidateAll();
1625 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
1626 }
1627 _localMove = {}; /* Snapshot and clear drag state under lock. */
1628 }
1629 return;
1630 }
1631
1632 /* Snapshot and clear drag state atomically under lock. */
1633 const LocalMove drag = _localMove;
1634 _localMove = {};
1635
1636 auto appWindow = getWindow(drag.id);
1637 if (!appWindow || !appWindow->window())
1638 return;
1639
1640 (void)SDL_SyncWindow(appWindow->window());
1641 int w = 0;
1642 int h = 0;
1643 SDL_GetWindowSize(appWindow->window(), &w, &h);
1644
1645 /* Derive Wayland origin from anchor edge. */
1646 const SDL_Rect start = appWindow->outerRect();
1647 const RailEdges e = railEdges(drag.type);
1648 int x = e.left ? (start.x + start.w - w) : start.x;
1649 int y = e.top ? (start.y + start.h - h) : start.y;
1650 clampIntoDesktop(*appWindow, x, y, w, h);
1651 reportAndAdopt(appWindow, x, y, w, h);
1652}

◆ desktopMayShow()

bool SdlRail::desktopMayShow ( )
nodiscard

Definition at line 529 of file sdl_rail.cpp.

530{
531 if (_desktopReleased)
532 return true;
533 const uint64_t now = SDL_GetTicks();
534 if (_desktopHoldUntil == 0)
535 {
536 _desktopHoldUntil = now + kDesktopHoldMs;
537 /* Frames may stop coming while the logon waits; wake up to re-check. */
538 if (!sdl_push_update_after(kDesktopHoldMs + 1))
539 WLog_WARN(TAG, "SDL_AddTimer failed: %s", SDL_GetError());
540 WLog_DBG(TAG, "RemoteApp starting: holding the logon desktop back");
541 return false;
542 }
543 if (now < _desktopHoldUntil)
544 return false;
545 WLog_DBG(TAG, "RemoteApp not started after %" PRIu32 " ms, showing the desktop",
546 kDesktopHoldMs);
547 _desktopReleased = true;
548 return true;
549}

◆ dragActive()

bool SdlRail::dragActive ( SDL_WindowID id)

Definition at line 1174 of file sdl_rail.cpp.

1175{
1176 return !compositorDrag() && suppressServerMotion(id);
1177}

◆ enabled()

bool SdlRail::enabled ( ) const
inlinenodiscard

Definition at line 61 of file sdl_rail.hpp.

62 {
63 return _enabled;
64 }

◆ ensureActive()

void SdlRail::ensureActive ( SDL_WindowID id)

Definition at line 417 of file sdl_rail.cpp.

418{
419 std::unique_lock lock(_windowsLock);
420 auto* appWindow = getWindowBySdlId(id);
421 if (!appWindow || !appWindow->activatable() || !appWindow->window())
422 return;
423 const auto wid = static_cast<uint32_t>(appWindow->id());
424 if (wid == _clientActiveId)
425 return;
426 sendClientActivate(wid, true);
427}

◆ handleClose()

void SdlRail::handleClose ( SDL_WindowID id)

Definition at line 325 of file sdl_rail.cpp.

326{
327 std::unique_lock lock(_windowsLock);
328 auto* appWindow = getWindowBySdlId(id);
329 if (!appWindow || !appWindow->window())
330 return;
331 WLog_DBG(TAG, "local close id=0x%08" PRIx32 " -> SC_CLOSE",
332 static_cast<UINT32>(appWindow->id()));
333 sendSystemCommand(appWindow, SC_CLOSE);
334}

◆ handleDisplayChanged()

void SdlRail::handleDisplayChanged ( SDL_WindowID id,
SDL_DisplayID display )

Definition at line 1325 of file sdl_rail.cpp.

1326{
1327 if (railPlatformCaps().supportsWindowPositions)
1328 return;
1329 std::unique_lock lock(_windowsLock);
1330 auto* appWindow = getWindowBySdlId(id);
1331 if (!appWindow || appWindow->isPopup())
1332 return;
1333 WLog_DBG(TAG, "display changed id=0x%08" PRIx32 " display %" PRIu32,
1334 static_cast<uint32_t>(appWindow->id()), display);
1335 appWindow->noteDisplayChange();
1336}

◆ handleFocus()

void SdlRail::handleFocus ( SDL_WindowID id,
bool gained )

Definition at line 364 of file sdl_rail.cpp.

365{
366 std::unique_lock lock(_windowsLock);
367 auto appWindow = getWindowBySdlId(id);
368 if (!appWindow || !appWindow->activatable() || !appWindow->window())
369 return;
370
371 /* Ignore focus loss while dragging: mapping restore shadow frames can steal focus
372 * and cancel the server modal loop. */
373 if (!gained && (_localMove.id == static_cast<uint32_t>(appWindow->id())))
374 return;
375
376 /* Fallback parent for orphaned popups. */
377 if (gained)
378 _focusedAppId = appWindow->id();
379
380 /* Only report activation, never deactivation (see the X11 client, #13422).
381 * The server treats a deactivation of the owner as "no active window in
382 * this application" and closes popups/menus that have just been opened
383 * when the compositor moves focus to them. The activation that follows
384 * (or an explicit click, see ensureActive()) is enough to route input. */
385 if (!gained)
386 return;
387
388 /* A window owned by this one is currently active (e.g. a profile card or
389 * a transient dialog). On a real desktop the owner is only activated by a
390 * click; focus-follows-mouse compositors hand focus back on hover, which
391 * would make the server close the owned popup. Leave that to
392 * ensureActive() (explicit click). */
393 const auto* active = getWindow(_clientActiveId);
394 if (active && (active != appWindow) &&
395 (active->owner() == static_cast<uint32_t>(appWindow->id())))
396 return;
397
398 /* ClientActivate only. Do NOT SDL_RaiseWindow to avoid WM focus loops. */
399 const auto wid = static_cast<uint32_t>(appWindow->id());
400 sendClientActivate(wid, true);
401}

◆ handleLocalMoveRequested()

void SdlRail::handleLocalMoveRequested ( uint32_t windowId,
uint16_t moveType )

Definition at line 1234 of file sdl_rail.cpp.

1235{
1236 /* Native modal loop: runs unlocked until the drag ends. */
1237 SDL_Window* sdlWindow = beginLocalMove(windowId, moveType);
1238 if (!sdlWindow)
1239 return;
1240 _sizeMoveRedraws = 0;
1241 railPlatformRunModalDrag(sdlWindow, moveType);
1242 WLog_DBG(TAG, "modal drag end id=0x%08" PRIx32 " redraws=%" PRIu32, windowId, _sizeMoveRedraws);
1243 completeLocalMoveIfPending();
1244}

◆ handleMaximized()

void SdlRail::handleMaximized ( SDL_WindowID id)

Definition at line 267 of file sdl_rail.cpp.

268{
269 std::unique_lock lock(_windowsLock);
270 auto* appWindow = getWindowBySdlId(id);
271 if (!appWindow || appWindow->isPopup() || !appWindow->window())
272 return;
273 /* Skip echo of programmatic maximize, but trigger reconcile to update insets. */
274 if (appWindow->railMaximized())
275 {
276 WLog_DBG(TAG, "local maximize skipped id=0x%08" PRIx32 " (already rail-maximized)",
277 static_cast<UINT32>(appWindow->id()));
278
279 if (!sdl_push_user_event(SDL_EVENT_USER_UPDATE))
280 WLog_WARN(TAG, "sdl_push_user_event failed");
281 return;
282 }
283 /* Move first: the server maximizes on its window's monitor. */
284 followDisplay(appWindow, true);
285 appWindow->setRailMaximized(true);
286 /* Force full repaint (skips dirty-rect). */
287 appWindow->invalidateAll();
288 /* Defer SC_MAXIMIZE if modal move loop is still open. */
289 if (appWindow->loopEndPending())
290 {
291 appWindow->deferMaximize();
292 WLog_DBG(TAG, "local maximize deferred id=0x%08" PRIx32 " (modal loop open)",
293 static_cast<UINT32>(appWindow->id()));
294 }
295 else if (appWindow->deferMaximizeUntilEcho())
296 {
297 /* Right after a move, Windows can keep the restored size. */
298 WLog_DBG(TAG, "local maximize deferred id=0x%08" PRIx32 " (move not confirmed)",
299 static_cast<UINT32>(appWindow->id()));
300 }
301 else
302 {
303 WLog_DBG(TAG, "local maximize id=0x%08" PRIx32 " -> SC_MAXIMIZE",
304 static_cast<UINT32>(appWindow->id()));
305 sendSystemCommand(appWindow, SC_MAXIMIZE);
306 }
307
308 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
309}

◆ handleMinimized()

void SdlRail::handleMinimized ( SDL_WindowID id)

Definition at line 311 of file sdl_rail.cpp.

312{
313 std::unique_lock lock(_windowsLock);
314 auto* appWindow = getWindowBySdlId(id);
315 if (!appWindow || appWindow->isPopup() || !appWindow->window())
316 return;
317 if (appWindow->railMinimized())
318 return; /* echo of reconcile's own SDL_MinimizeWindow */
319 appWindow->setRailMinimized(true);
320 WLog_DBG(TAG, "local minimize id=0x%08" PRIx32 " -> SC_MINIMIZE",
321 static_cast<UINT32>(appWindow->id()));
322 sendSystemCommand(appWindow, SC_MINIMIZE);
323}

◆ handleRestored()

void SdlRail::handleRestored ( SDL_WindowID id)

Definition at line 336 of file sdl_rail.cpp.

337{
338 std::unique_lock lock(_windowsLock);
339 auto* appWindow = getWindowBySdlId(id);
340 if (!appWindow || appWindow->isPopup() || !appWindow->window())
341 return;
342 /* Resolve restored state; trigger reconcile in case WM cleared flags asynchronously. */
343 if (appWindow->railMinimized())
344 {
345 appWindow->setRailMinimized(false);
346 appWindow->noteLocalRestore();
347 }
348 else if (appWindow->railMaximized())
349 appWindow->setRailMaximized(false);
350 else
351 {
352 if (!sdl_push_user_event(SDL_EVENT_USER_UPDATE))
353 WLog_WARN(TAG, "sdl_push_user_event failed");
354 return;
355 }
356 WLog_DBG(TAG, "local restore id=0x%08" PRIx32 " -> SC_RESTORE",
357 static_cast<UINT32>(appWindow->id()));
358 appWindow->invalidateAll();
359 sendSystemCommand(appWindow, SC_RESTORE);
360
361 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
362}

◆ init()

bool SdlRail::init ( RailClientContext * rail)

Definition at line 903 of file sdl_rail.cpp.

904{
905 _rail = rail;
906 if (!rail)
907 return false;
908
909 registerUpdateCallbacks(_context->context()->update);
910
911 {
912 std::unique_lock lock(_windowsLock);
913 /* Move previous windows to deadWindows for main-thread teardown. */
914 for (auto it = _windows.begin(); it != _windows.end();)
915 {
916 auto cur = it++;
917 _deadWindows.insert(_windows.extract(cur));
918 }
919 auto* settings = _context->context()->settings;
920 const uint32_t caches =
921 freerdp_settings_get_uint32(settings, FreeRDP_RemoteAppNumIconCaches);
922 _iconCacheEntries =
923 freerdp_settings_get_uint32(settings, FreeRDP_RemoteAppNumIconCacheEntries);
924 _iconCache.assign(static_cast<size_t>(caches) * _iconCacheEntries, {});
925 }
926
927 rail->custom = this;
928 rail->ServerExecuteResult = SdlRail::server_execute_result;
929 rail->ServerGetAppIdResponse = SdlRail::server_app_id;
930 /* ServerSystemParam not implemented: the server only sends screensaver state here. */
931 rail->ServerLocalMoveSize = SdlRail::server_local_move_size;
932 rail->ServerMinMaxInfo = SdlRail::server_min_max_info;
933 rail->ServerPowerDisplayRequest = SdlRail::server_power_display_request;
934 /* Keep default ServerHandshake. */
935
936 WLog_WARN(TAG, "RemoteApp/RAIL support in the SDL client is experimental");
937 {
938 std::unique_lock lock(_windowsLock);
939 updateCoordinates();
940 }
941 const RailPlatformCaps& caps = railPlatformCaps();
942 railPlatformSetNativeLoopHandler({ this, redrawDuringSizeMove, nativeResize });
943 WLog_DBG(TAG, "RAIL channel initialized: driver=%s windowPositions=%d transparentWindows=%d",
944 SDL_GetCurrentVideoDriver(), caps.supportsWindowPositions ? 1 : 0,
945 caps.supportsTransparentWindows ? 1 : 0);
946#if SDL_VERSION_ATLEAST(3, 4, 10)
947 /* Synchronize WM-driven X11 resizes via _NET_WM_SYNC_REQUEST to prevent torn frames. */
948 if (sdl::utils::isX11Driver())
949 {
950 if (!SDL_SetHint(SDL_HINT_VIDEO_X11_ENABLE_XSYNC_EXT, "1"))
951 WLog_WARN(TAG, "SDL_SetHint(SDL_HINT_VIDEO_X11_ENABLE_XSYNC_EXT) failed: %s",
952 SDL_GetError());
953 }
954#endif
955 /* Like Windows, the click that activates a window also reaches the app. */
956 if (!SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1"))
957 WLog_WARN(TAG, "SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH) failed: %s", SDL_GetError());
958 return true;
959}
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.

◆ inputWindows()

void SdlRail::inputWindows ( SDL_WindowID id,
std::vector< SdlInputWindow > & windows,
std::optional< size_t > & source )

Definition at line 448 of file sdl_rail.cpp.

450{
451 std::unique_lock lock(_windowsLock);
452 for (auto& entry : _windows)
453 {
454 auto& appWindow = entry.second;
455 auto* window = appWindow.window();
456 if (!window)
457 continue;
458 const auto isSource = (SDL_GetWindowID(window) == id);
459 if (!isSource && ((SDL_GetWindowFlags(window) & SDL_WINDOW_HIDDEN) != 0))
460 continue;
461 /* Input is mapped with the server rect: the source window follows its display first. */
462 if (isSource && !appWindow.effectivelyMaximized())
463 followDisplay(&appWindow, true);
464
465 /* RemoteApp windows are drawn 1:1, without local scaling. */
466 auto input = SdlContext::inputWindowGeometry(window, appWindow.renderer());
467 input.layout = inputLayoutPosition(&appWindow);
468 if (isSource)
469 source = windows.size();
470 windows.push_back(input);
471 }
472}

◆ invalidateWindow()

void SdlRail::invalidateWindow ( SDL_WindowID id)

Definition at line 209 of file sdl_rail.cpp.

210{
211 std::unique_lock lock(_windowsLock);
212 auto* appWindow = getWindowBySdlId(id);
213 if (!appWindow)
214 return;
215 appWindow->invalidateAll();
216
217 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
218}

◆ noteDragResize()

void SdlRail::noteDragResize ( SDL_WindowID id,
int w,
int h )

Definition at line 1457 of file sdl_rail.cpp.

1458{
1459 std::unique_lock lock(_windowsLock);
1460 if ((_localMove.id == 0) || compositorDrag() || (_localMove.type != RAIL_WMSZ_MOVE))
1461 return;
1462 auto* appWindow = getWindowBySdlId(id);
1463 if (!appWindow || (static_cast<uint32_t>(appWindow->id()) != _localMove.id))
1464 return;
1465 /* Filter out reconcile echo from server drag-restore. */
1466 if (appWindow->localMoveSizeChanged())
1467 {
1468 const SDL_Rect outer = appWindow->outerRect();
1469 if ((w == outer.w) && (h == outer.h))
1470 return;
1471 }
1472 _localMove.wmSized = true;
1473 _localMove.wmSize = { w, h };
1474}

◆ noteLeftPress()

void SdlRail::noteLeftPress ( float x,
float y )
inline

Definition at line 105 of file sdl_rail.hpp.

106 {
107 _lastPressServer = { static_cast<int>(x), static_cast<int>(y) };
108 _lastPointerServer = _lastPressServer;
109 }

◆ noteLogonPrompt()

void SdlRail::noteLogonPrompt ( )

Definition at line 551 of file sdl_rail.cpp.

552{
553 if (_desktopReleased.exchange(true))
554 return;
555 WLog_DBG(TAG, "logon dialog on the server, showing the desktop");
556 if (!sdl_push_user_event(SDL_EVENT_USER_UPDATE))
557 WLog_WARN(TAG, "sdl_push_user_event failed");
558}

◆ notePopupMoved()

void SdlRail::notePopupMoved ( SDL_WindowID id)

Definition at line 474 of file sdl_rail.cpp.

475{
476 /* X11 input already matches the server rect; only Wayland lags behind it. */
477 if (railPlatformCaps().supportsWindowPositions)
478 return;
479 std::unique_lock lock(_windowsLock);
480 auto* w = getWindowBySdlId(id);
481 if (!w || !w->isPopup())
482 return;
483 w->notePlaced();
484 /* Show the held frame now: that commit applies the move. */
485 if (!sdl_push_user_event(SDL_EVENT_USER_UPDATE))
486 WLog_WARN(TAG, "could not wake the paint loop for a moved popup");
487}

◆ noteResizeGrab()

void SdlRail::noteResizeGrab ( SDL_WindowID id)

Definition at line 1593 of file sdl_rail.cpp.

1594{
1595 std::unique_lock lock(_windowsLock);
1596 if (_localMove.id == 0)
1597 return;
1598 auto* appWindow = getWindowBySdlId(id);
1599 /* Mouse leave or a configure during the drag: the compositor resize is under way. */
1600 if (!appWindow || (static_cast<uint32_t>(appWindow->id()) != _localMove.id) ||
1601 !compositorDrag())
1602 return;
1603 _localMove.sawResize = true;
1604 _localMove.lastResize = SDL_GetTicks();
1605 if (SDL_AddTimer(kResizeSettleMs, resizeSettleTimer, this) == 0)
1606 WLog_WARN(TAG, "resize settle timer failed: %s", SDL_GetError());
1607}

◆ noteServerPointer()

void SdlRail::noteServerPointer ( float x,
float y )
inline

Definition at line 111 of file sdl_rail.hpp.

112 {
113 _lastPointerServer = { static_cast<int>(x), static_cast<int>(y) };
114 }

◆ noteUsableBoundsChanged()

void SdlRail::noteUsableBoundsChanged ( SDL_DisplayID display)

Definition at line 257 of file sdl_rail.cpp.

258{
259 if (display != SDL_GetPrimaryDisplay())
260 return;
261 std::unique_lock lock(_windowsLock);
262 _sentWorkArea = {};
263 if (!sdl_push_user_event(SDL_EVENT_USER_UPDATE))
264 WLog_WARN(TAG, "sdl_push_user_event failed");
265}

◆ ownsWindow()

bool SdlRail::ownsWindow ( SDL_WindowID id)
nodiscard

Definition at line 203 of file sdl_rail.cpp.

204{
205 std::unique_lock lock(_windowsLock);
206 return getWindowBySdlId(id) != nullptr;
207}

◆ paint()

bool SdlRail::paint ( SDL_Surface * primary,
SDL_PixelFormat fallbackFormat,
const std::vector< SDL_Rect > & damage )

Definition at line 580 of file sdl_rail.cpp.

582{
583 if (!_enabled)
584 return true;
585
586 std::unique_lock lock(_windowsLock);
587
588 /* Report workarea (avoids maximizing under local panels). */
589 if (_sentWorkArea.w == 0)
590 {
591 SDL_Rect usable{};
592 if (SDL_GetDisplayUsableBounds(SDL_GetPrimaryDisplay(), &usable))
593 sendWorkArea(usable);
594 }
595
596 /* Windows whose SDL parent is going away (deleted by the server, or displaced by a same-id
597 * recreate, see addWindow). Popups owned by that window die with it (the server deletes
598 * them too), and so do non-popup windows, as before. Any other popup keeps its entry: the
599 * server still owns it. A popup without an owner (e.g. a tray notification) is parented to
600 * the focused app window as a fallback; deleting it locally when that window closes
601 * desynchronizes us from the server, which keeps sending orders for it. It only drops its
602 * local SDL window here and the next pass re-creates it under a new parent.
603 * SDL destroys child windows together with their parent, so every window that is about to
604 * lose its SDL window (deleted, displaced, or under one of those) releases it here, deepest
605 * child first, before any entry is destroyed below. */
606 std::vector<std::pair<size_t, SdlRailWindow*>> release;
607 for (auto& [wid, win] : _windows)
608 {
609 if (!win.window())
610 continue;
611 size_t depth = 0;
612 bool orphan = false;
613 bool ownerDeleted = false;
614 for (SDL_Window* p = SDL_GetWindowParent(win.window()); p; p = SDL_GetWindowParent(p))
615 {
616 depth++;
617 auto* owner = getWindowBySdlId(SDL_GetWindowID(p));
618 if (!owner)
619 orphan = true;
620 else if (owner->isDeleted())
621 {
622 orphan = true;
623 if (owner->id() == win.owner())
624 ownerDeleted = true;
625 }
626 }
627 if (orphan && !win.isDeleted() && (ownerDeleted || !win.isPopup()))
628 win.markDeleted();
629 if (orphan || win.isDeleted())
630 release.emplace_back(depth, &win);
631 }
632 for (auto& [wid, win] : _deadWindows)
633 {
634 if (!win.window())
635 continue;
636 size_t depth = 0;
637 for (SDL_Window* p = SDL_GetWindowParent(win.window()); p; p = SDL_GetWindowParent(p))
638 depth++;
639 release.emplace_back(depth, &win);
640 }
641 std::sort(release.begin(), release.end(),
642 [](const auto& a, const auto& b) { return a.first > b.first; });
643 for (auto& [depth, win] : release)
644 win->releaseWindow();
645
646 /* Erase deleted windows; their SDL windows are gone already. */
647 for (auto it = _windows.begin(); it != _windows.end();)
648 it = it->second.isDeleted() ? _windows.erase(it) : std::next(it);
649 /* Windows displaced by a same-id recreate (addWindow) also die here, on the main thread. */
650 _deadWindows.clear();
651
652 /* App windows first so popup parents exist before their popups. */
653 for (auto& it : _windows)
654 {
655 auto& win = it.second;
656 if (win.isPopup())
657 continue;
658 /* Parent owned dialogs to owner. */
659 SDL_Window* parent = nullptr;
660 SDL_Rect parentRect{};
661 /* The owner is chosen by the server: never parent a window to itself or to one of its
662 * own SDL descendants (windows owning each other). SDL does not reject that and the
663 * parent chain walked above becomes a loop. */
664 auto* owner = resolveParent(win.owner());
665 if (owner && !descendsFrom(*owner, win))
666 {
667 parent = owner->window();
668 parentRect = owner->outerRect(); /* the SDL window's on-screen geometry */
669 }
670 const bool realized = win.window() != nullptr;
671 win.paint(primary, fallbackFormat, damage, parent, parentRect);
672 if (!realized && win.window())
673 _context->railWindowInfoChanged(win.id());
674 /* Adopt WM-refused geometry and update server. */
675 SDL_Rect refusedOuter{};
676 if (win.takeWmOverride(refusedOuter))
677 {
678 const SDL_Rect rect = win.serverRect(refusedOuter);
679 const SDL_Rect cur = win.windowRect();
680 WLog_DBG(TAG,
681 "wm-refused adopt id=0x%08" PRIx32
682 " outer=%dx%d -> srv=%d,%d %dx%d (was %dx%d)",
683 static_cast<uint32_t>(win.id()), refusedOuter.w, refusedOuter.h, rect.x,
684 rect.y, rect.w, rect.h, cur.w, cur.h);
685 if (!SDL_RectsEqual(&rect, &cur))
686 sendClientWindowMove(&win, rect);
687 win.adoptLocalGeometry(rect);
688 _context->railWindowInfoChanged(win.id());
689 }
690 /* Resend a size the server left unanswered. */
691 SDL_Rect unconfirmed{};
692 if (win.takeUnconfirmedReport(unconfirmed) && !win.railMinimized() &&
693 !win.effectivelyMaximized())
694 {
695 WLog_DBG(TAG, "size resent id=0x%08" PRIx32 " %dx%d (not confirmed)",
696 static_cast<uint32_t>(win.id()), unconfirmed.w, unconfirmed.h);
697 sendClientWindowMove(&win, unconfirmed);
698 }
699 if (!win.effectivelyMaximized())
700 followDisplay(&win, false);
701 if (win.takeMaximizeAfterEcho() && win.effectivelyMaximized())
702 sendSystemCommand(&win, SC_MAXIMIZE);
703 if (win.takeRestoreRetry())
704 {
705 WLog_DBG(TAG, "restore resent id=0x%08" PRIx32, static_cast<uint32_t>(win.id()));
706 sendSystemCommand(&win, SC_RESTORE);
707 }
708 }
709
710 /* Request full repaint after first window is realized (fixes reconnect blank windows). */
711 if (!_refreshSent)
712 {
713 for (auto& it : _windows)
714 {
715 if (it.second.isPopup() || !it.second.window())
716 continue;
717 auto* ctx = &_context->common()->context;
718 /* Not sent from here (main thread), the RDP thread does it as soon as it can. */
719 if (freerdp_timer_add(ctx, 1, refreshRectTimer, nullptr, true) == 0)
720 WLog_WARN(TAG, "could not queue the refresh-rect");
721 _refreshSent = true;
722 WLog_DBG(TAG, "refresh-rect queued (first app window realized)");
723 break;
724 }
725 }
726
727 for (auto& it : _windows)
728 {
729 auto& popup = it.second;
730 if (!popup.isPopup())
731 continue;
732
733 /* Latch shadow frame classification once identified. */
734 if (!popup.isFrame() && isShadowFrame(popup))
735 popup.setFrame(true);
736
737 /* Anchor drop shadow to adjoining visible popup. */
738 if (popup.isLayered())
739 {
740 constexpr int reach = 48; /* shadow offset+blur spread from its popup, server px */
741 const SDL_Rect sr = popup.windowRect();
742 bool anchored = false;
743 for (auto& other : _windows)
744 {
745 auto& cand = other.second;
746 if ((&cand == &popup) || !cand.isPopup() || cand.isLayered() || !cand.window() ||
747 ((SDL_GetWindowFlags(cand.window()) & SDL_WINDOW_HIDDEN) != 0))
748 continue;
749 SDL_Rect zone = cand.windowRect();
750 zone.x -= reach;
751 zone.y -= reach;
752 zone.w += 2 * reach;
753 zone.h += 2 * reach;
754 /* Shadow must be contained within reach zone to distinguish popup shadows from
755 * app frame edge shadows. */
756 const bool inside = (sr.x >= zone.x) && (sr.y >= zone.y) &&
757 (sr.x + sr.w <= zone.x + zone.w) &&
758 (sr.y + sr.h <= zone.y + zone.h);
759 if (inside)
760 {
761 anchored = true;
762 break;
763 }
764 }
765 popup.setShadowAnchored(anchored);
766 }
767
768 SdlRailWindow* chosen = nullptr;
769 /* Existing popups have an immutable SDL parent; position against it. */
770 if (auto* pw = popup.window())
771 {
772 SDL_Window* p = SDL_GetWindowParent(pw);
773 chosen = p ? getWindowBySdlId(SDL_GetWindowID(p)) : nullptr;
774 }
775 else
776 {
777 chosen = resolvePopupParent(popup);
778 if (!chosen)
779 WLog_DBG(TAG, "popup id=0x%08" PRIx32 " has no parent app window",
780 static_cast<UINT32>(popup.id()));
781 }
782
783 SDL_Window* parent = nullptr;
784 SDL_Rect parentRect{};
785 if (chosen)
786 {
787 parent = chosen->window();
788 parentRect = chosen->outerRect(); /* the SDL window's on-screen geometry */
789 }
790 const bool realized = popup.window() != nullptr;
791 popup.paint(primary, fallbackFormat, damage, parent, parentRect);
792 if (!realized && popup.window())
793 _context->railWindowInfoChanged(popup.id());
794 }
795
796 /* All live windows now have real X11 handles: realize the server's z-order. */
797 applyZOrder();
798 return true;
799}

◆ pointerEpoch()

uint32_t SdlRail::pointerEpoch ( ) const
inlinenodiscard

Definition at line 116 of file sdl_rail.hpp.

117 {
118 return _pointerEpoch;
119 }

◆ pollDragRelease()

void SdlRail::pollDragRelease ( )

Definition at line 1258 of file sdl_rail.cpp.

1259{
1260 {
1261 std::unique_lock lock(_windowsLock);
1262 if ((_localMove.id == 0) || compositorDrag())
1263 return;
1264 }
1265 if (SDL_GetGlobalMouseState(nullptr, nullptr) & SDL_BUTTON_LMASK)
1266 {
1267 if (SDL_AddTimer(kReleasePollMs, releasePollTimer, this) == 0)
1268 WLog_WARN(TAG, "drag release timer failed: %s", SDL_GetError());
1269 return;
1270 }
1271 completeLocalMoveIfPending();
1272}

◆ settleWaylandResize()

void SdlRail::settleWaylandResize ( )

Definition at line 1582 of file sdl_rail.cpp.

1583{
1584 {
1585 std::unique_lock lock(_windowsLock);
1586 if ((_localMove.id == 0) || !compositorDrag() || !_localMove.sawResize ||
1587 (SDL_GetTicks() - _localMove.lastResize < kResizeSettleMs))
1588 return;
1589 }
1590 completeWaylandResize();
1591}

◆ suppressServerMotion()

bool SdlRail::suppressServerMotion ( SDL_WindowID id)

Definition at line 1179 of file sdl_rail.cpp.

1180{
1181 std::unique_lock lock(_windowsLock);
1182 if (_localMove.id == 0)
1183 return false;
1184 auto* appWindow = getWindowBySdlId(id);
1185 /* Suppress motion events during active local move. */
1186 return appWindow && appWindow->localMoveActive();
1187}

◆ syncGeometry()

void SdlRail::syncGeometry ( SDL_WindowID id)

Definition at line 1503 of file sdl_rail.cpp.

1504{
1505 std::unique_lock lock(_windowsLock);
1506 auto* w = getWindowBySdlId(id);
1507 if (!w || !w->window() || w->isDeleted() || w->isPopup())
1508 return;
1509
1510 /* Geometry is server/WM-owned when maximized or minimized. */
1511 if (w->effectivelyMaximized())
1512 {
1513 reportMaximizedWorkArea(w);
1514 return;
1515 }
1516 if (w->railMinimized())
1517 return;
1518
1519 const auto wid = static_cast<uint32_t>(w->id());
1520 /* Skip geometry sync while awaiting server restored rect. */
1521 if (w->localMoveActive() || (_localMove.id == wid) || w->loopEndPending() ||
1522 w->stateTransitionPending() || w->awaitingRestoreRect() || w->insetsStale())
1523 return;
1524
1525 int lw = 0;
1526 int lh = 0;
1527 SDL_GetWindowSize(w->window(), &lw, &lh);
1528 if ((lw <= 0) || (lh <= 0))
1529 return;
1530
1531 /* Ignore invalid zero-content window size updates. */
1532 const SDL_Rect ins = w->insets();
1533 if (((lw - ins.x - ins.w) <= 0) || ((lh - ins.y - ins.h) <= 0))
1534 {
1535 WLog_DBG(TAG, "geometry sync id=0x%08" PRIx32 " %dx%d dropped (content collapsed)", wid, lw,
1536 lh);
1537 return;
1538 }
1539
1540 const SDL_Rect vis = w->outerRect();
1541 const bool posKnown = railPlatformCaps().supportsWindowPositions;
1542 int lx = vis.x;
1543 int ly = vis.y;
1544 if (posKnown)
1545 SDL_GetWindowPosition(w->window(), &lx, &ly);
1546 else
1547 clampIntoDesktop(*w, lx, ly, lw, lh);
1548
1549 /* Check if window settled at applied target. */
1550 if ((lw == vis.w) && (lh == vis.h) && (!posKnown || ((lx == vis.x) && (ly == vis.y))))
1551 {
1552 w->clearGeomApplyPending();
1553 return; /* Ignore self-echo when geometry converged. */
1554 }
1555
1556 /* Drop transient echo events while client geometry application is in flight. */
1557 if (w->geomApplyPending())
1558 {
1559 /* Allow live Wayland compositor resize to advance beyond in-flight apply. */
1560 const bool overtaken = !posKnown && ((lw > vis.w) || (lh > vis.h));
1561 if (!overtaken)
1562 return;
1563 w->clearGeomApplyPending();
1564 }
1565
1566 WLog_DBG(TAG, "geometry sync id=0x%08" PRIx32 " local=%d,%d %dx%d (sync)", wid, lx, ly, lw, lh);
1567 reportAndAdopt(w, lx, ly, lw, lh);
1568}

◆ uninit()

bool SdlRail::uninit ( RailClientContext * rail)

Definition at line 977 of file sdl_rail.cpp.

978{
979 WINPR_ASSERT(rail);
980 /* The caller clears rail->custom next; server callbacks return early on nullptr. */
981 std::unique_lock lock(_windowsLock);
982 _refreshSent = false;
983 _appStarted = false;
984 /* Reset session and move state on reconnect. */
985 _localMove = {};
986 _sessionMargins = {};
987 _sentWorkArea = {};
988 _clientActiveId = 0;
989 _focusedAppId = 0;
990 _zOrder.clear();
991 _appliedZOrder.clear();
992 _zOrderDirty = false;
993 _iconCache.clear();
994 _iconScratch = {};
995 /* Mark windows for main-thread teardown. */
996 WLog_DBG(TAG, "RAIL channel uninit, marking %zu windows for main-thread teardown",
997 _windows.size());
998 for (auto& kv : _windows)
999 kv.second.markDeleted();
1000 _rail = nullptr;
1001 _enabled = false;
1002 /* The next connection logs on again: hold its logon desktop back too. */
1003 _desktopReleased = false;
1004 _desktopHoldUntil = 0;
1005 _displayRequested = false;
1006 queueScreenSaverUpdate();
1007
1008 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
1009 return true;
1010}

◆ updateCoordinates()

void SdlRail::updateCoordinates ( )

Definition at line 507 of file sdl_rail.cpp.

508{
509 const auto* settings = _context->common()->context.settings;
510 _displayShift = { freerdp_settings_get_int32(settings, FreeRDP_MonitorLocalShiftX),
511 freerdp_settings_get_int32(settings, FreeRDP_MonitorLocalShiftY) };
512 _localShift = railPlatformCaps().supportsWindowPositions ? _displayShift : SDL_Point{ 0, 0 };
513 const auto origin = _context->inputOrigin();
514 WLog_DBG(TAG, "coordinates: display shift %d,%d window shift %d,%d input origin %d,%d",
515 _displayShift.x, _displayShift.y, _localShift.x, _localShift.y, origin.x, origin.y);
516}
WINPR_ATTR_NODISCARD FREERDP_API INT32 freerdp_settings_get_int32(const rdpSettings *settings, FreeRDP_Settings_Keys_Int32 id)
Returns a INT32 settings value.

◆ UpdateWindowFromSurface()

UINT SdlRail::UpdateWindowFromSurface ( RdpgfxClientContext * context,
gdiGfxSurface * surface )
static

Definition at line 891 of file sdl_rail.cpp.

892{
893 WINPR_ASSERT(context);
894 auto gdi = static_cast<rdpGdi*>(context->custom);
895 if (!gdi || !gdi->context)
896 return CHANNEL_RC_OK;
897 auto sdl = get_context(gdi->context);
898 if (!sdl)
899 return CHANNEL_RC_OK;
900 return sdl->getRailChannelContext().updateWindowFromSurface(surface);
901}

◆ updateWindowFromSurface()

UINT SdlRail::updateWindowFromSurface ( gdiGfxSurface * surface)

Definition at line 856 of file sdl_rail.cpp.

857{
858 if (!surface)
859 return CHANNEL_RC_OK;
860
861 std::unique_lock lock(_windowsLock);
862 auto appWindow = getWindow(surface->windowId);
863 if (!appWindow)
864 {
865 /* Drop GFX surface for unknown windows (repaints later). */
866 WLog_VRB(TAG, "gfx surface for untracked id=0x%08" PRIx64, surface->windowId);
867 return CHANNEL_RC_OK;
868 }
869
870 /* Clamp mapped dimensions to allocated surface bounds. */
871 const uint32_t w =
872 std::min(surface->mappedWidth ? surface->mappedWidth : surface->width, surface->width);
873 const uint32_t h =
874 std::min(surface->mappedHeight ? surface->mappedHeight : surface->height, surface->height);
875
876 /* Consume per-frame damage. */
877 UINT32 nbRects = 0;
878 const RECTANGLE_16* rects = region16_rects(&surface->invalidRegion, &nbRects);
879 /* Skip undamaged surfaces during EndFrame. */
880 const bool remapped = appWindow->takeSurfaceChange(surface->surfaceId);
881 if ((nbRects == 0) && !remapped)
882 return CHANNEL_RC_OK;
883 appWindow->updateGfxSurface(surface->data, surface->scanline, w, h, rects, nbRects,
884 surface->format);
885 region16_clear(&surface->invalidRegion);
886
887 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
888 return CHANNEL_RC_OK;
889}

◆ windowGeometry()

bool SdlRail::windowGeometry ( uint64_t id,
SDL_Window *& window,
int & width,
int & height )
nodiscard

main thread: the SDL window and client size (server coordinates, 1:1) of the RemoteApp window id; false if it doesn't exist or isn't realized yet

Definition at line 67 of file sdl_rail.cpp.

68{
69 std::scoped_lock lock(_windowsLock);
70 auto w = getWindow(id);
71 if (!w || w->isDeleted() || !w->window())
72 return false;
73 const SDL_Rect r = w->windowRect();
74 window = w->window();
75 width = r.w;
76 height = r.h;
77 return true;
78}

The documentation for this class was generated from the following files: