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)
bool translateToServer (SDL_WindowID id, float &x, float &y)
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 45 of file sdl_rail.hpp.

Constructor & Destructor Documentation

◆ SdlRail()

SdlRail::SdlRail ( SdlContext * context)
explicit

Definition at line 49 of file sdl_rail.cpp.

49 : _context(context)
50{
51}

◆ ~SdlRail()

SdlRail::~SdlRail ( )

Definition at line 53 of file sdl_rail.cpp.

54{
55 railPlatformSetNativeLoopHandler({});
56}

Member Function Documentation

◆ completeLocalMoveIfPending()

void SdlRail::completeLocalMoveIfPending ( )

Definition at line 1608 of file sdl_rail.cpp.

1609{
1610 std::unique_lock lock(_windowsLock);
1611 if (_localMove.id == 0)
1612 return;
1613 /* Skip Wayland button-up finalize. */
1614 if (compositorDrag())
1615 return;
1616
1617 /* Snapshot and clear drag state atomically under lock. */
1618 const LocalMove drag = _localMove;
1619 _localMove = {};
1620
1621 auto appWindow = getWindow(drag.id);
1622 if (!appWindow || !appWindow->window() || appWindow->isDeleted())
1623 return; /* window gone mid-drag: nothing to release into or report for */
1624
1625 /* Send synthetic button release if WM grab consumed physical release. */
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");
1630
1631 /* Finalize WM resize before reading final size. */
1632 (void)SDL_SyncWindow(appWindow->window());
1633
1634 int x = 0;
1635 int y = 0;
1636 int w = 0;
1637 int h = 0;
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(); /* position frozen at drag start */
1642 /* Preserve server size during move; defer WM snap/tile size sync until loop END to avoid
1643 * server drag-restore conflict. */
1644 if (isMove)
1645 {
1646 if (!appWindow->effectivelyMaximized() && drag.wmSized &&
1647 !appWindow->localMoveSizeChanged())
1648 {
1649 /* Adopt WM size if window was resized during move. */
1650 w = drag.wmSize.x;
1651 h = drag.wmSize.y;
1652 appWindow->deferSnap(appWindow->serverRect({ x, y, w, h }));
1653 }
1654 else
1655 {
1656 w = start.w;
1657 h = start.h;
1658 }
1659 }
1660 /* Finalize modal loop unless server closed it early. */
1661 if (!drag.serverEnded)
1662 {
1663 int px = drag.anchor.x;
1664 int py = drag.anchor.y;
1665 if (isMove && appWindow->localMoveSizeChanged())
1666 {
1667 /* Measure delta from server anchor on drag-restore. */
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;
1672 }
1673 else if (!appWindow->effectivelyMaximized())
1674 {
1675 const RailEdges e = railEdges(drag.type);
1676 if (isMove || e.left)
1677 px += x - start.x;
1678 else if (e.right)
1679 px += (x + w) - (start.x + start.w);
1680 if (isMove || e.top)
1681 py += y - start.y;
1682 else if (e.bottom)
1683 py += (y + h) - (start.y + start.h);
1684 }
1685 (void)freerdp_client_send_button_event(_context->common(), FALSE, PTR_FLAGS_BUTTON1, px,
1686 py);
1687 /* Server loop unwinding: wait for explicit END order to gate deferred SC_MAXIMIZE. */
1688 appWindow->armLoopEnd();
1689
1690 /* Adopt local geometry without sending ClientWindowMove during mouse drags. */
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());
1695 /* Drag complete: apply pending server resize delayed during WM grab. */
1696 if (restoredMidDrag)
1697 appWindow->markGeometryDirty();
1698 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
1699 }
1700 else if (!isMove)
1701 {
1702 /* Sync final resize geometry if server closed loop early. */
1703 clampIntoDesktop(*appWindow, x, y, w, h);
1704 reportAndAdopt(appWindow, x, y, w, h);
1705 }
1706
1707 railPlatformEndDrag(appWindow->window());
1708}
#define TAG

◆ completeWaylandResize()

void SdlRail::completeWaylandResize ( bool definitive = false)

Definition at line 1563 of file sdl_rail.cpp.

1564{
1565 std::unique_lock lock(_windowsLock);
1566 if ((_localMove.id == 0) || !compositorDrag())
1567 return;
1568 /* Finalize drag resize on window enter if resize occurred. */
1569 if (!_localMove.sawResize)
1570 {
1571 if (definitive)
1572 {
1573 if (auto* appWindow = getWindow(_localMove.id))
1574 {
1575 appWindow->setLocalMoveActive(false);
1576 /* Repaint now: the dashed placeholder is on screen and an undamaged window would
1577 * otherwise keep it until the next server frame. */
1578 appWindow->invalidateAll();
1579 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
1580 }
1581 _localMove = {}; /* Snapshot and clear drag state under lock. */
1582 }
1583 return;
1584 }
1585
1586 /* Snapshot and clear drag state atomically under lock. */
1587 const LocalMove drag = _localMove;
1588 _localMove = {};
1589
1590 auto appWindow = getWindow(drag.id);
1591 if (!appWindow || !appWindow->window())
1592 return;
1593
1594 (void)SDL_SyncWindow(appWindow->window());
1595 int w = 0;
1596 int h = 0;
1597 SDL_GetWindowSize(appWindow->window(), &w, &h);
1598
1599 /* Derive Wayland origin from anchor edge. */
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);
1606}

◆ desktopMayShow()

bool SdlRail::desktopMayShow ( )
nodiscard

Definition at line 513 of file sdl_rail.cpp.

514{
515 if (_desktopReleased)
516 return true;
517 const uint64_t now = SDL_GetTicks();
518 if (_desktopHoldUntil == 0)
519 {
520 _desktopHoldUntil = now + kDesktopHoldMs;
521 /* Frames may stop coming while the logon waits; wake up to re-check. */
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");
525 return false;
526 }
527 if (now < _desktopHoldUntil)
528 return false;
529 WLog_DBG(TAG, "RemoteApp not started after %" PRIu32 " ms, showing the desktop",
530 kDesktopHoldMs);
531 _desktopReleased = true;
532 return true;
533}

◆ dragActive()

bool SdlRail::dragActive ( SDL_WindowID id)

Definition at line 1139 of file sdl_rail.cpp.

1140{
1141 return !compositorDrag() && suppressServerMotion(id);
1142}

◆ enabled()

bool SdlRail::enabled ( ) const
inlinenodiscard

Definition at line 59 of file sdl_rail.hpp.

60 {
61 return _enabled;
62 }

◆ ensureActive()

void SdlRail::ensureActive ( SDL_WindowID id)

Definition at line 404 of file sdl_rail.cpp.

405{
406 std::unique_lock lock(_windowsLock);
407 auto* appWindow = getWindowBySdlId(id);
408 if (!appWindow || !appWindow->activatable() || !appWindow->window())
409 return;
410 const auto wid = static_cast<uint32_t>(appWindow->id());
411 if (wid == _clientActiveId)
412 return;
413 sendClientActivate(wid, true);
414}

◆ handleClose()

void SdlRail::handleClose ( SDL_WindowID id)

Definition at line 312 of file sdl_rail.cpp.

313{
314 std::unique_lock lock(_windowsLock);
315 auto* appWindow = getWindowBySdlId(id);
316 if (!appWindow || !appWindow->window())
317 return;
318 WLog_DBG(TAG, "local close id=0x%08" PRIx32 " -> SC_CLOSE",
319 static_cast<UINT32>(appWindow->id()));
320 sendSystemCommand(appWindow, SC_CLOSE);
321}

◆ handleDisplayChanged()

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

Definition at line 1290 of file sdl_rail.cpp.

1291{
1292 if (railPlatformCaps().supportsWindowPositions)
1293 return;
1294 std::unique_lock lock(_windowsLock);
1295 auto* appWindow = getWindowBySdlId(id);
1296 if (!appWindow || appWindow->isPopup())
1297 return;
1298 WLog_DBG(TAG, "display changed id=0x%08" PRIx32 " display %" PRIu32,
1299 static_cast<uint32_t>(appWindow->id()), display);
1300 appWindow->noteDisplayChange();
1301}

◆ handleFocus()

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

Definition at line 351 of file sdl_rail.cpp.

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

◆ handleLocalMoveRequested()

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

Definition at line 1199 of file sdl_rail.cpp.

1200{
1201 /* Native modal loop: runs unlocked until the drag ends. */
1202 SDL_Window* sdlWindow = beginLocalMove(windowId, moveType);
1203 if (!sdlWindow)
1204 return;
1205 _sizeMoveRedraws = 0;
1206 railPlatformRunModalDrag(sdlWindow, moveType);
1207 WLog_DBG(TAG, "modal drag end id=0x%08" PRIx32 " redraws=%" PRIu32, windowId, _sizeMoveRedraws);
1208 completeLocalMoveIfPending();
1209}

◆ handleMaximized()

void SdlRail::handleMaximized ( SDL_WindowID id)

Definition at line 254 of file sdl_rail.cpp.

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

◆ handleMinimized()

void SdlRail::handleMinimized ( SDL_WindowID id)

Definition at line 298 of file sdl_rail.cpp.

299{
300 std::unique_lock lock(_windowsLock);
301 auto* appWindow = getWindowBySdlId(id);
302 if (!appWindow || appWindow->isPopup() || !appWindow->window())
303 return;
304 if (appWindow->railMinimized())
305 return; /* echo of reconcile's own SDL_MinimizeWindow */
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);
310}

◆ handleRestored()

void SdlRail::handleRestored ( SDL_WindowID id)

Definition at line 323 of file sdl_rail.cpp.

324{
325 std::unique_lock lock(_windowsLock);
326 auto* appWindow = getWindowBySdlId(id);
327 if (!appWindow || appWindow->isPopup() || !appWindow->window())
328 return;
329 /* Resolve restored state; trigger reconcile in case WM cleared flags asynchronously. */
330 if (appWindow->railMinimized())
331 {
332 appWindow->setRailMinimized(false);
333 appWindow->noteLocalRestore();
334 }
335 else if (appWindow->railMaximized())
336 appWindow->setRailMaximized(false);
337 else
338 {
339 if (!sdl_push_user_event(SDL_EVENT_USER_UPDATE))
340 WLog_WARN(TAG, "sdl_push_user_event failed");
341 return;
342 }
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);
347
348 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
349}

◆ init()

bool SdlRail::init ( RailClientContext * rail)

Definition at line 868 of file sdl_rail.cpp.

869{
870 _rail = rail;
871 if (!rail)
872 return false;
873
874 registerUpdateCallbacks(_context->context()->update);
875
876 {
877 std::unique_lock lock(_windowsLock);
878 /* Move previous windows to deadWindows for main-thread teardown. */
879 for (auto it = _windows.begin(); it != _windows.end();)
880 {
881 auto cur = it++;
882 _deadWindows.insert(_windows.extract(cur));
883 }
884 auto* settings = _context->context()->settings;
885 const uint32_t caches =
886 freerdp_settings_get_uint32(settings, FreeRDP_RemoteAppNumIconCaches);
887 _iconCacheEntries =
888 freerdp_settings_get_uint32(settings, FreeRDP_RemoteAppNumIconCacheEntries);
889 _iconCache.assign(static_cast<size_t>(caches) * _iconCacheEntries, {});
890 }
891
892 rail->custom = this;
893 rail->ServerExecuteResult = SdlRail::server_execute_result;
894 rail->ServerGetAppIdResponse = SdlRail::server_app_id;
895 /* ServerSystemParam not implemented: the server only sends screensaver state here. */
896 rail->ServerLocalMoveSize = SdlRail::server_local_move_size;
897 rail->ServerMinMaxInfo = SdlRail::server_min_max_info;
898 rail->ServerPowerDisplayRequest = SdlRail::server_power_display_request;
899 /* Keep default ServerHandshake. */
900
901 WLog_WARN(TAG, "RemoteApp/RAIL support in the SDL client is experimental");
902 {
903 std::unique_lock lock(_windowsLock);
904 updateCoordinates();
905 }
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)
912 /* Synchronize WM-driven X11 resizes via _NET_WM_SYNC_REQUEST to prevent torn frames. */
913 if (sdl::utils::isX11Driver())
914 {
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",
917 SDL_GetError());
918 }
919#endif
920 /* Like Windows, the click that activates a window also reaches the app. */
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());
923 return true;
924}
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.

◆ invalidateWindow()

void SdlRail::invalidateWindow ( SDL_WindowID id)

Definition at line 196 of file sdl_rail.cpp.

197{
198 std::unique_lock lock(_windowsLock);
199 auto* appWindow = getWindowBySdlId(id);
200 if (!appWindow)
201 return;
202 appWindow->invalidateAll();
203
204 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
205}

◆ noteDragResize()

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

Definition at line 1411 of file sdl_rail.cpp.

1412{
1413 std::unique_lock lock(_windowsLock);
1414 if ((_localMove.id == 0) || compositorDrag() || (_localMove.type != RAIL_WMSZ_MOVE))
1415 return;
1416 auto* appWindow = getWindowBySdlId(id);
1417 if (!appWindow || (static_cast<uint32_t>(appWindow->id()) != _localMove.id))
1418 return;
1419 /* Filter out reconcile echo from server drag-restore. */
1420 if (appWindow->localMoveSizeChanged())
1421 {
1422 const SDL_Rect outer = appWindow->outerRect();
1423 if ((w == outer.w) && (h == outer.h))
1424 return;
1425 }
1426 _localMove.wmSized = true;
1427 _localMove.wmSize = { w, h };
1428}

◆ noteLeftPress()

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

Definition at line 101 of file sdl_rail.hpp.

102 {
103 _lastPressServer = { static_cast<int>(x), static_cast<int>(y) };
104 _lastPointerServer = _lastPressServer;
105 }

◆ noteLogonPrompt()

void SdlRail::noteLogonPrompt ( )

Definition at line 535 of file sdl_rail.cpp.

536{
537 if (_desktopReleased.exchange(true))
538 return;
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");
542}

◆ notePopupMoved()

void SdlRail::notePopupMoved ( SDL_WindowID id)

Definition at line 447 of file sdl_rail.cpp.

448{
449 /* X11 input already matches the server rect; only Wayland lags behind it. */
450 if (railPlatformCaps().supportsWindowPositions)
451 return;
452 std::unique_lock lock(_windowsLock);
453 auto* w = getWindowBySdlId(id);
454 if (!w || !w->isPopup())
455 return;
456 w->notePlaced();
457 /* Show the held frame now: that commit applies the move. */
458 if (!sdl_push_user_event(SDL_EVENT_USER_UPDATE))
459 WLog_WARN(TAG, "could not wake the paint loop for a moved popup");
460}

◆ noteResizeGrab()

void SdlRail::noteResizeGrab ( SDL_WindowID id)

Definition at line 1547 of file sdl_rail.cpp.

1548{
1549 std::unique_lock lock(_windowsLock);
1550 if (_localMove.id == 0)
1551 return;
1552 auto* appWindow = getWindowBySdlId(id);
1553 /* Mouse leave or a configure during the drag: the compositor resize is under way. */
1554 if (!appWindow || (static_cast<uint32_t>(appWindow->id()) != _localMove.id) ||
1555 !compositorDrag())
1556 return;
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());
1561}

◆ noteServerPointer()

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

Definition at line 107 of file sdl_rail.hpp.

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

◆ noteUsableBoundsChanged()

void SdlRail::noteUsableBoundsChanged ( SDL_DisplayID display)

Definition at line 244 of file sdl_rail.cpp.

245{
246 if (display != SDL_GetPrimaryDisplay())
247 return;
248 std::unique_lock lock(_windowsLock);
249 _sentWorkArea = {};
250 if (!sdl_push_user_event(SDL_EVENT_USER_UPDATE))
251 WLog_WARN(TAG, "sdl_push_user_event failed");
252}

◆ ownsWindow()

bool SdlRail::ownsWindow ( SDL_WindowID id)
nodiscard

Definition at line 190 of file sdl_rail.cpp.

191{
192 std::unique_lock lock(_windowsLock);
193 return getWindowBySdlId(id) != nullptr;
194}

◆ paint()

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

Definition at line 544 of file sdl_rail.cpp.

546{
547 if (!_enabled)
548 return true;
549
550 std::unique_lock lock(_windowsLock);
551
552 /* Report workarea (avoids maximizing under local panels). */
553 if (_sentWorkArea.w == 0)
554 {
555 SDL_Rect usable{};
556 if (SDL_GetDisplayUsableBounds(SDL_GetPrimaryDisplay(), &usable))
557 sendWorkArea(usable);
558 }
559
560 /* Windows whose SDL parent is going away (deleted by the server, or displaced by a same-id
561 * recreate, see addWindow). Popups owned by that window die with it (the server deletes
562 * them too), and so do non-popup windows, as before. Any other popup keeps its entry: the
563 * server still owns it. A popup without an owner (e.g. a tray notification) is parented to
564 * the focused app window as a fallback; deleting it locally when that window closes
565 * desynchronizes us from the server, which keeps sending orders for it. It only drops its
566 * local SDL window here and the next pass re-creates it under a new parent.
567 * SDL destroys child windows together with their parent, so every window that is about to
568 * lose its SDL window (deleted, displaced, or under one of those) releases it here, deepest
569 * child first, before any entry is destroyed below. */
570 std::vector<std::pair<size_t, SdlRailWindow*>> release;
571 for (auto& [wid, win] : _windows)
572 {
573 if (!win.window())
574 continue;
575 size_t depth = 0;
576 bool orphan = false;
577 bool ownerDeleted = false;
578 for (SDL_Window* p = SDL_GetWindowParent(win.window()); p; p = SDL_GetWindowParent(p))
579 {
580 depth++;
581 auto* owner = getWindowBySdlId(SDL_GetWindowID(p));
582 if (!owner)
583 orphan = true;
584 else if (owner->isDeleted())
585 {
586 orphan = true;
587 if (owner->id() == win.owner())
588 ownerDeleted = true;
589 }
590 }
591 if (orphan && !win.isDeleted() && (ownerDeleted || !win.isPopup()))
592 win.markDeleted();
593 if (orphan || win.isDeleted())
594 release.emplace_back(depth, &win);
595 }
596 for (auto& [wid, win] : _deadWindows)
597 {
598 if (!win.window())
599 continue;
600 size_t depth = 0;
601 for (SDL_Window* p = SDL_GetWindowParent(win.window()); p; p = SDL_GetWindowParent(p))
602 depth++;
603 release.emplace_back(depth, &win);
604 }
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();
609
610 /* Erase deleted windows; their SDL windows are gone already. */
611 for (auto it = _windows.begin(); it != _windows.end();)
612 it = it->second.isDeleted() ? _windows.erase(it) : std::next(it);
613 /* Windows displaced by a same-id recreate (addWindow) also die here, on the main thread. */
614 _deadWindows.clear();
615
616 /* App windows first so popup parents exist before their popups. */
617 for (auto& it : _windows)
618 {
619 auto& win = it.second;
620 if (win.isPopup())
621 continue;
622 /* Parent owned dialogs to owner. */
623 SDL_Window* parent = nullptr;
624 SDL_Rect parentRect{};
625 auto* owner = resolveParent(win.owner());
626 if (owner && (owner != &win))
627 {
628 parent = owner->window();
629 parentRect = owner->outerRect(); /* the SDL window's on-screen geometry */
630 }
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());
635 /* Adopt WM-refused geometry and update server. */
636 SDL_Rect refusedOuter{};
637 if (win.takeWmOverride(refusedOuter))
638 {
639 const SDL_Rect rect = win.serverRect(refusedOuter);
640 const SDL_Rect cur = win.windowRect();
641 WLog_DBG(TAG,
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());
650 }
651 /* Resend a size the server left unanswered. */
652 SDL_Rect unconfirmed{};
653 if (win.takeUnconfirmedReport(unconfirmed) && !win.railMinimized() &&
654 !win.effectivelyMaximized())
655 {
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);
659 }
660 if (!win.effectivelyMaximized())
661 followDisplay(&win, false);
662 if (win.takeMaximizeAfterEcho() && win.effectivelyMaximized())
663 sendSystemCommand(&win, SC_MAXIMIZE);
664 if (win.takeRestoreRetry())
665 {
666 WLog_DBG(TAG, "restore resent id=0x%08" PRIx32, static_cast<uint32_t>(win.id()));
667 sendSystemCommand(&win, SC_RESTORE);
668 }
669 }
670
671 /* Request full repaint after first window is realized (fixes reconnect blank windows). */
672 if (!_refreshSent)
673 {
674 for (auto& it : _windows)
675 {
676 if (it.second.isPopup() || !it.second.window())
677 continue;
678 auto* ctx = &_context->common()->context;
679 const auto dw = static_cast<UINT16>(
680 freerdp_settings_get_uint32(ctx->settings, FreeRDP_DesktopWidth));
681 const auto dh = static_cast<UINT16>(
682 freerdp_settings_get_uint32(ctx->settings, FreeRDP_DesktopHeight));
683 const RECTANGLE_16 all = { 0, 0, dw, dh };
684 if (ctx->update && ctx->update->RefreshRect)
685 (void)ctx->update->RefreshRect(ctx, 1, &all);
686 _refreshSent = true;
687 WLog_DBG(TAG, "refresh-rect sent (first app window realized)");
688 break;
689 }
690 }
691
692 for (auto& it : _windows)
693 {
694 auto& popup = it.second;
695 if (!popup.isPopup())
696 continue;
697
698 /* Latch shadow frame classification once identified. */
699 if (!popup.isFrame() && isShadowFrame(popup))
700 popup.setFrame(true);
701
702 /* Anchor drop shadow to adjoining visible popup. */
703 if (popup.isLayered())
704 {
705 constexpr int reach = 48; /* shadow offset+blur spread from its popup, server px */
706 const SDL_Rect sr = popup.windowRect();
707 bool anchored = false;
708 for (auto& other : _windows)
709 {
710 auto& cand = other.second;
711 if ((&cand == &popup) || !cand.isPopup() || cand.isLayered() || !cand.window() ||
712 ((SDL_GetWindowFlags(cand.window()) & SDL_WINDOW_HIDDEN) != 0))
713 continue;
714 SDL_Rect zone = cand.windowRect();
715 zone.x -= reach;
716 zone.y -= reach;
717 zone.w += 2 * reach;
718 zone.h += 2 * reach;
719 /* Shadow must be contained within reach zone to distinguish popup shadows from
720 * app frame edge shadows. */
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);
724 if (inside)
725 {
726 anchored = true;
727 break;
728 }
729 }
730 popup.setShadowAnchored(anchored);
731 }
732
733 SdlRailWindow* chosen = nullptr;
734 /* Existing popups have an immutable SDL parent; position against it. */
735 if (auto* pw = popup.window())
736 {
737 SDL_Window* p = SDL_GetWindowParent(pw);
738 chosen = p ? getWindowBySdlId(SDL_GetWindowID(p)) : nullptr;
739 }
740 else
741 {
742 chosen = resolvePopupParent(popup);
743 if (!chosen)
744 WLog_DBG(TAG, "popup id=0x%08" PRIx32 " has no parent app window",
745 static_cast<UINT32>(popup.id()));
746 }
747
748 SDL_Window* parent = nullptr;
749 SDL_Rect parentRect{};
750 if (chosen)
751 {
752 parent = chosen->window();
753 parentRect = chosen->outerRect(); /* the SDL window's on-screen geometry */
754 }
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());
759 }
760
761 /* All live windows now have real X11 handles: realize the server's z-order. */
762 applyZOrder();
763 return true;
764}

◆ pointerEpoch()

uint32_t SdlRail::pointerEpoch ( ) const
inlinenodiscard

Definition at line 112 of file sdl_rail.hpp.

113 {
114 return _pointerEpoch;
115 }

◆ pollDragRelease()

void SdlRail::pollDragRelease ( )

Definition at line 1223 of file sdl_rail.cpp.

1224{
1225 {
1226 std::unique_lock lock(_windowsLock);
1227 if ((_localMove.id == 0) || compositorDrag())
1228 return;
1229 }
1230 if (SDL_GetGlobalMouseState(nullptr, nullptr) & SDL_BUTTON_LMASK)
1231 {
1232 if (SDL_AddTimer(kReleasePollMs, releasePollTimer, this) == 0)
1233 WLog_WARN(TAG, "drag release timer failed: %s", SDL_GetError());
1234 return;
1235 }
1236 completeLocalMoveIfPending();
1237}

◆ settleWaylandResize()

void SdlRail::settleWaylandResize ( )

Definition at line 1536 of file sdl_rail.cpp.

1537{
1538 {
1539 std::unique_lock lock(_windowsLock);
1540 if ((_localMove.id == 0) || !compositorDrag() || !_localMove.sawResize ||
1541 (SDL_GetTicks() - _localMove.lastResize < kResizeSettleMs))
1542 return;
1543 }
1544 completeWaylandResize();
1545}

◆ suppressServerMotion()

bool SdlRail::suppressServerMotion ( SDL_WindowID id)

Definition at line 1144 of file sdl_rail.cpp.

1145{
1146 std::unique_lock lock(_windowsLock);
1147 if (_localMove.id == 0)
1148 return false;
1149 auto* appWindow = getWindowBySdlId(id);
1150 /* Suppress motion events during active local move. */
1151 return appWindow && appWindow->localMoveActive();
1152}

◆ syncGeometry()

void SdlRail::syncGeometry ( SDL_WindowID id)

Definition at line 1457 of file sdl_rail.cpp.

1458{
1459 std::unique_lock lock(_windowsLock);
1460 auto* w = getWindowBySdlId(id);
1461 if (!w || !w->window() || w->isDeleted() || w->isPopup())
1462 return;
1463
1464 /* Geometry is server/WM-owned when maximized or minimized. */
1465 if (w->effectivelyMaximized())
1466 {
1467 reportMaximizedWorkArea(w);
1468 return;
1469 }
1470 if (w->railMinimized())
1471 return;
1472
1473 const auto wid = static_cast<uint32_t>(w->id());
1474 /* Skip geometry sync while awaiting server restored rect. */
1475 if (w->localMoveActive() || (_localMove.id == wid) || w->loopEndPending() ||
1476 w->stateTransitionPending() || w->awaitingRestoreRect() || w->insetsStale())
1477 return;
1478
1479 int lw = 0;
1480 int lh = 0;
1481 SDL_GetWindowSize(w->window(), &lw, &lh);
1482 if ((lw <= 0) || (lh <= 0))
1483 return;
1484
1485 /* Ignore invalid zero-content window size updates. */
1486 const SDL_Rect ins = w->insets();
1487 if (((lw - ins.x - ins.w) <= 0) || ((lh - ins.y - ins.h) <= 0))
1488 {
1489 WLog_DBG(TAG, "geometry sync id=0x%08" PRIx32 " %dx%d dropped (content collapsed)", wid, lw,
1490 lh);
1491 return;
1492 }
1493
1494 const SDL_Rect vis = w->outerRect();
1495 const bool posKnown = railPlatformCaps().supportsWindowPositions;
1496 int lx = vis.x;
1497 int ly = vis.y;
1498 if (posKnown)
1499 SDL_GetWindowPosition(w->window(), &lx, &ly);
1500 else
1501 clampIntoDesktop(*w, lx, ly, lw, lh);
1502
1503 /* Check if window settled at applied target. */
1504 if ((lw == vis.w) && (lh == vis.h) && (!posKnown || ((lx == vis.x) && (ly == vis.y))))
1505 {
1506 w->clearGeomApplyPending();
1507 return; /* Ignore self-echo when geometry converged. */
1508 }
1509
1510 /* Drop transient echo events while client geometry application is in flight. */
1511 if (w->geomApplyPending())
1512 {
1513 /* Allow live Wayland compositor resize to advance beyond in-flight apply. */
1514 const bool overtaken = !posKnown && ((lw > vis.w) || (lh > vis.h));
1515 if (!overtaken)
1516 return;
1517 w->clearGeomApplyPending();
1518 }
1519
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);
1522}

◆ translateToServer()

bool SdlRail::translateToServer ( SDL_WindowID id,
float & x,
float & y )

Definition at line 416 of file sdl_rail.cpp.

417{
418 std::unique_lock lock(_windowsLock);
419 auto* appWindow = getWindowBySdlId(id);
420 if (!appWindow)
421 return false;
422 /* Input is translated with the server rect: move first. */
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);
428 /* Window-local to server-absolute, aligned with surface blit anchor. */
429 SDL_Point origin = appWindow->serverOrigin();
430 SDL_Point rel{};
431 if (appWindow->isPopup() && !railPlatformCaps().supportsWindowPositions &&
432 appWindow->placedRel(rel))
433 {
434 auto* sdlParent = SDL_GetWindowParent(appWindow->window());
435 if (auto* parent = sdlParent ? getWindowBySdlId(SDL_GetWindowID(sdlParent)) : nullptr)
436 {
437 const SDL_Rect pr = parent->outerRect();
438 origin = { pr.x + rel.x, pr.y + rel.y };
439 }
440 }
441 /* Input is relative to the top left of the remote desktop, not to the primary monitor. */
442 x = rpos.x + static_cast<float>(origin.x - _inputOrigin.x);
443 y = rpos.y + static_cast<float>(origin.y - _inputOrigin.y);
444 return true;
445}

◆ uninit()

bool SdlRail::uninit ( RailClientContext * rail)

Definition at line 942 of file sdl_rail.cpp.

943{
944 WINPR_ASSERT(rail);
945 /* The caller clears rail->custom next; server callbacks return early on nullptr. */
946 std::unique_lock lock(_windowsLock);
947 _refreshSent = false;
948 _appStarted = false;
949 /* Reset session and move state on reconnect. */
950 _localMove = {};
951 _sessionMargins = {};
952 _sentWorkArea = {};
953 _clientActiveId = 0;
954 _focusedAppId = 0;
955 _zOrder.clear();
956 _appliedZOrder.clear();
957 _zOrderDirty = false;
958 _iconCache.clear();
959 _iconScratch = {};
960 /* Mark windows for main-thread teardown. */
961 WLog_DBG(TAG, "RAIL channel uninit, marking %zu windows for main-thread teardown",
962 _windows.size());
963 for (auto& kv : _windows)
964 kv.second.markDeleted();
965 _rail = nullptr;
966 _enabled = false;
967 /* The next connection logs on again: hold its logon desktop back too. */
968 _desktopReleased = false;
969 _desktopHoldUntil = 0;
970 _displayRequested = false;
971 queueScreenSaverUpdate();
972
973 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
974 return true;
975}

◆ updateCoordinates()

void SdlRail::updateCoordinates ( )

Definition at line 480 of file sdl_rail.cpp.

481{
482 const auto* settings = _context->common()->context.settings;
483 _displayShift = { freerdp_settings_get_int32(settings, FreeRDP_MonitorLocalShiftX),
484 freerdp_settings_get_int32(settings, FreeRDP_MonitorLocalShiftY) };
485 _localShift = railPlatformCaps().supportsWindowPositions ? _displayShift : SDL_Point{ 0, 0 };
486 _inputOrigin = { 0, 0 };
487 const UINT32 count = freerdp_settings_get_uint32(settings, FreeRDP_MonitorCount);
488 for (UINT32 x = 0; x < count; x++)
489 {
490 const auto* mon = static_cast<const rdpMonitor*>(
491 freerdp_settings_get_pointer_array(settings, FreeRDP_MonitorDefArray, x));
492 if (!mon)
493 continue;
494 _inputOrigin.x = std::min(_inputOrigin.x, mon->x);
495 _inputOrigin.y = std::min(_inputOrigin.y, mon->y);
496 }
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,
499 _inputOrigin.y);
500}
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 856 of file sdl_rail.cpp.

857{
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);
863 if (!sdl)
864 return CHANNEL_RC_OK;
865 return sdl->getRailChannelContext().updateWindowFromSurface(surface);
866}

◆ updateWindowFromSurface()

UINT SdlRail::updateWindowFromSurface ( gdiGfxSurface * surface)

Definition at line 821 of file sdl_rail.cpp.

822{
823 if (!surface)
824 return CHANNEL_RC_OK;
825
826 std::unique_lock lock(_windowsLock);
827 auto appWindow = getWindow(surface->windowId);
828 if (!appWindow)
829 {
830 /* Drop GFX surface for unknown windows (repaints later). */
831 WLog_VRB(TAG, "gfx surface for untracked id=0x%08" PRIx64, surface->windowId);
832 return CHANNEL_RC_OK;
833 }
834
835 /* Clamp mapped dimensions to allocated surface bounds. */
836 const uint32_t w =
837 std::min(surface->mappedWidth ? surface->mappedWidth : surface->width, surface->width);
838 const uint32_t h =
839 std::min(surface->mappedHeight ? surface->mappedHeight : surface->height, surface->height);
840
841 /* Consume per-frame damage. */
842 UINT32 nbRects = 0;
843 const RECTANGLE_16* rects = region16_rects(&surface->invalidRegion, &nbRects);
844 /* Skip undamaged surfaces during EndFrame. */
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,
849 surface->format);
850 region16_clear(&surface->invalidRegion);
851
852 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
853 return CHANNEL_RC_OK;
854}

◆ 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 66 of file sdl_rail.cpp.

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

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