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 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 ensureActive (SDL_WindowID id)
 
bool translateToServer (SDL_WindowID id, float &x, float &y)
 
void noteLeftPress (float x, float y)
 
void noteServerPointer (float x, float y)
 
bool suppressServerInput (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 handleWaylandResize (SDL_WindowID id)
 

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

44 : _context(context)
45{
46}

Member Function Documentation

◆ completeLocalMoveIfPending()

void SdlRail::completeLocalMoveIfPending ( )

Definition at line 1262 of file sdl_rail.cpp.

1263{
1264 std::unique_lock lock(_windowsLock);
1265 if (_localMove.id == 0)
1266 return;
1267 /* Skip Wayland button-up finalize. */
1268 if (_localMove.wayland)
1269 return;
1270
1271 /* Snapshot and clear drag state atomically under lock. */
1272 const LocalMove drag = _localMove;
1273 _localMove = {};
1274
1275 auto appWindow = getWindow(drag.id);
1276 if (!appWindow || !appWindow->window() || appWindow->isDeleted())
1277 return; /* window gone mid-drag: nothing to release into or report for */
1278
1279 /* Send synthetic button release if WM grab consumed physical release. */
1280 if ((SDL_GetMouseState(nullptr, nullptr) & SDL_BUTTON_LMASK) &&
1281 !(SDL_GetGlobalMouseState(nullptr, nullptr) & SDL_BUTTON_LMASK))
1282 (void)sdl_x11_send_left_button_release(appWindow->window());
1283
1284 /* Finalize WM resize before reading final size. */
1285 (void)SDL_SyncWindow(appWindow->window());
1286
1287 int x = 0;
1288 int y = 0;
1289 int w = 0;
1290 int h = 0;
1291 SDL_GetWindowSize(appWindow->window(), &w, &h);
1292 SDL_GetWindowPosition(appWindow->window(), &x, &y);
1293 const bool isMove = (drag.type == RAIL_WMSZ_MOVE);
1294 const SDL_Rect start = appWindow->outerRect(); /* position frozen at drag start */
1295 /* Preserve server size during move; defer WM snap/tile size sync until loop END to avoid
1296 * server drag-restore conflict. */
1297 if (isMove)
1298 {
1299 if (!appWindow->effectivelyMaximized() && drag.wmSized &&
1300 !appWindow->localMoveSizeChanged())
1301 {
1302 /* Adopt WM size if window was resized during move. */
1303 w = drag.wmSize.x;
1304 h = drag.wmSize.y;
1305 appWindow->deferSnap(appWindow->serverRect({ x, y, w, h }));
1306 }
1307 else
1308 {
1309 w = start.w;
1310 h = start.h;
1311 }
1312 }
1313 /* Finalize modal loop unless server closed it early. */
1314 if (!drag.serverEnded)
1315 {
1316 int px = drag.anchor.x;
1317 int py = drag.anchor.y;
1318 if (isMove && appWindow->localMoveSizeChanged())
1319 {
1320 /* Measure delta from server anchor on drag-restore. */
1321 const SDL_Point sp = appWindow->localMoveServerPos();
1322 const SDL_Rect ii = appWindow->insets();
1323 px = drag.pointer.x + (x + ii.x) - sp.x;
1324 py = drag.pointer.y + (y + ii.y) - sp.y;
1325 }
1326 else if (!appWindow->effectivelyMaximized())
1327 {
1328 const RailEdges e = railEdges(drag.type);
1329 if (isMove || e.left)
1330 px += x - start.x;
1331 else if (e.right)
1332 px += (x + w) - (start.x + start.w);
1333 if (isMove || e.top)
1334 py += y - start.y;
1335 else if (e.bottom)
1336 py += (y + h) - (start.y + start.h);
1337 }
1338 (void)freerdp_client_send_button_event(_context->common(), FALSE, PTR_FLAGS_BUTTON1, px,
1339 py);
1340 /* Server loop unwinding: wait for explicit END order to gate deferred SC_MAXIMIZE. */
1341 appWindow->armLoopEnd();
1342
1343 /* Adopt local geometry without sending ClientWindowMove during mouse drags. */
1344 const SDL_Rect rect = appWindow->serverRect({ x, y, w, h });
1345 const bool restoredMidDrag = appWindow->localMoveSizeChanged();
1346 appWindow->adoptLocalGeometry(rect);
1347 /* Drag complete: apply pending server resize delayed during WM grab. */
1348 if (restoredMidDrag)
1349 appWindow->markGeometryDirty();
1350 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
1351 }
1352 else if (!isMove)
1353 {
1354 /* Sync final resize geometry if server closed loop early. */
1355 clampIntoDesktop(x, y, w, h);
1356 reportAndAdopt(appWindow, x, y, w, h);
1357 }
1358
1359 (void)sdl_x11_set_bit_gravity(appWindow->window(), 0 /* reset to default */);
1360}

◆ completeWaylandResize()

void SdlRail::completeWaylandResize ( bool  definitive = false)

Definition at line 1217 of file sdl_rail.cpp.

1218{
1219 std::unique_lock lock(_windowsLock);
1220 if ((_localMove.id == 0) || !_localMove.wayland)
1221 return;
1222 /* Finalize drag resize on window enter if resize occurred. */
1223 if (!_localMove.sawResize)
1224 {
1225 if (definitive)
1226 {
1227 if (auto* appWindow = getWindow(_localMove.id))
1228 {
1229 appWindow->setLocalMoveActive(false);
1230 /* Repaint now: the dashed placeholder is on screen and an undamaged window would
1231 * otherwise keep it until the next server frame. */
1232 appWindow->invalidateAll();
1233 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
1234 }
1235 _localMove = {}; /* Snapshot and clear drag state under lock. */
1236 }
1237 return;
1238 }
1239
1240 /* Snapshot and clear drag state atomically under lock. */
1241 const LocalMove drag = _localMove;
1242 _localMove = {};
1243
1244 auto appWindow = getWindow(drag.id);
1245 if (!appWindow || !appWindow->window())
1246 return;
1247
1248 (void)SDL_SyncWindow(appWindow->window());
1249 int w = 0;
1250 int h = 0;
1251 SDL_GetWindowSize(appWindow->window(), &w, &h);
1252
1253 /* Derive Wayland origin from anchor edge. */
1254 const SDL_Rect start = appWindow->windowRect();
1255 const RailEdges e = railEdges(drag.type);
1256 int x = e.left ? (start.x + start.w - w) : start.x;
1257 int y = e.top ? (start.y + start.h - h) : start.y;
1258 clampIntoDesktop(x, y, w, h);
1259 reportAndAdopt(appWindow, x, y, w, h);
1260}

◆ enabled()

bool SdlRail::enabled ( ) const
inline

Definition at line 59 of file sdl_rail.hpp.

60 {
61 return _enabled;
62 }

◆ ensureActive()

void SdlRail::ensureActive ( SDL_WindowID  id)

Definition at line 347 of file sdl_rail.cpp.

348{
349 std::unique_lock lock(_windowsLock);
350 auto* appWindow = getWindowBySdlId(id);
351 if (!appWindow || (appWindow->isPopup() && !appWindow->isFullscreen()) || !appWindow->window())
352 return;
353 const auto wid = static_cast<uint32_t>(appWindow->id());
354 if (wid == _clientActiveId)
355 return;
356 sendClientActivate(wid, true);
357}

◆ handleClose()

void SdlRail::handleClose ( SDL_WindowID  id)

Definition at line 262 of file sdl_rail.cpp.

263{
264 std::unique_lock lock(_windowsLock);
265 auto* appWindow = getWindowBySdlId(id);
266 if (!appWindow || !appWindow->window())
267 return;
268 WLog_DBG(TAG, "local close id=0x%08" PRIx32 " -> SC_CLOSE",
269 static_cast<UINT32>(appWindow->id()));
270 sendSystemCommand(appWindow, SC_CLOSE);
271}

◆ handleFocus()

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

Definition at line 294 of file sdl_rail.cpp.

295{
296 std::unique_lock lock(_windowsLock);
297 auto appWindow = getWindowBySdlId(id);
298 if (!appWindow || (appWindow->isPopup() && !appWindow->isFullscreen()) || !appWindow->window())
299 return;
300
301 /* Ignore focus loss while dragging: mapping restore shadow frames can steal focus
302 * and cancel the server modal loop. */
303 if (!gained && (_localMove.id == static_cast<uint32_t>(appWindow->id())))
304 return;
305
306 /* Fallback parent for orphaned popups. */
307 if (gained)
308 _focusedAppId = appWindow->id();
309
310 /* Only report activation, never deactivation (see the X11 client, #13422).
311 * The server treats a deactivation of the owner as "no active window in
312 * this application" and closes popups/menus that have just been opened
313 * when the compositor moves focus to them. The activation that follows
314 * (or an explicit click, see ensureActive()) is enough to route input. */
315 if (!gained)
316 return;
317
318 /* A window owned by this one is currently active (e.g. a profile card or
319 * a transient dialog). On a real desktop the owner is only activated by a
320 * click; focus-follows-mouse compositors hand focus back on hover, which
321 * would make the server close the owned popup. Leave that to
322 * ensureActive() (explicit click). */
323 const auto* active = getWindow(_clientActiveId);
324 if (active && (active != appWindow) &&
325 (active->owner() == static_cast<uint32_t>(appWindow->id())))
326 return;
327
328 /* ClientActivate only. Do NOT SDL_RaiseWindow to avoid WM focus loops. */
329 const auto wid = static_cast<uint32_t>(appWindow->id());
330 sendClientActivate(wid, true);
331}

◆ handleLocalMoveRequested()

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

Definition at line 947 of file sdl_rail.cpp.

948{
949 const bool wayland = sdl::utils::isWaylandDriver();
950 const bool x11 = sdl::utils::isX11Driver();
951 if (!wayland && !x11)
952 return;
953
954 std::unique_lock lock(_windowsLock);
955 auto appWindow = getWindow(windowId);
956 if (!appWindow || !appWindow->window() || appWindow->isPopup())
957 return;
958
959 const bool isMove = (moveType == RAIL_WMSZ_MOVE);
960 WLog_DBG(TAG, "local move start id=0x%08" PRIx32 " driver=%s type=%" PRIu16 " %s", windowId,
961 wayland ? "wayland" : "x11", moveType, isMove ? "move" : "resize");
962 bool started = false;
963 if (wayland)
964 {
965 /* Wayland: positions unreadable, sizes readable. */
966 if (isMove)
967 started = sdl_wayland_begin_move(appWindow->window());
968 else
969 started = sdl_wayland_begin_resize(appWindow->window(), railToXdgEdge(moveType));
970 if (started && !isMove)
971 {
972 _localMove = {}; /* clear all per-drag state as a unit before latching this drag */
973 _localMove.id = windowId;
974 _localMove.wayland = true;
975 _localMove.type = moveType;
976 }
977 }
978 else
979 {
980 /* X11: hand resize to WM, suppress input. */
981 started = sdl_x11_begin_move_size(appWindow->window(), railToNetDirection(moveType));
982 if (started)
983 {
984 _localMove = {}; /* clear all per-drag state as a unit before latching this drag */
985 _localMove.id = windowId;
986 _localMove.type = moveType;
987 /* Latch drag anchors for release calculation. */
988 _localMove.anchor = _lastPressServer;
989 _localMove.pointer = _lastPointerServer;
990 /* Clear prior loop-end state on new drag. */
991 appWindow->clearLoopEnd();
992 /* Set bit gravity to fixed edge. */
993 const RailEdges e = railEdges(moveType);
994 const int row = e.top ? 2 : (e.bottom ? 0 : 1);
995 const int col = e.left ? 2 : (e.right ? 0 : 1);
996 (void)sdl_x11_set_bit_gravity(appWindow->window(), row * 3 + col + 1);
997 }
998 }
999 /* Drive local frame during X11/Wayland drag. */
1000 if (started && (!wayland || !isMove))
1001 {
1002 appWindow->setLocalMoveActive(true);
1003 if (!isMove)
1004 {
1005 /* Anchor the stale frame to the fixed edge (opposite the dragged one). */
1006 const RailEdges e = railEdges(moveType);
1007 appWindow->setResizeAnchor(e.left, e.top);
1008 }
1009 }
1010 else if (!started)
1011 {
1012 WLog_WARN(TAG, "WM move failed for RAIL window 0x%08" PRIx32, windowId);
1013 }
1014}

◆ handleMaximized()

void SdlRail::handleMaximized ( SDL_WindowID  id)

Definition at line 215 of file sdl_rail.cpp.

216{
217 std::unique_lock lock(_windowsLock);
218 auto* appWindow = getWindowBySdlId(id);
219 if (!appWindow || appWindow->isPopup() || !appWindow->window())
220 return;
221 /* Skip the echo of reconcile's own SDL_MaximizeWindow (railMaximized already set). */
222 if (appWindow->railMaximized())
223 {
224 WLog_DBG(TAG, "local maximize skipped id=0x%08" PRIx32 " (already rail-maximized)",
225 static_cast<UINT32>(appWindow->id()));
226 return;
227 }
228 appWindow->setRailMaximized(true);
229 /* Force full repaint (skips dirty-rect). */
230 appWindow->invalidateAll();
231 /* Defer SC_MAXIMIZE if modal move loop is still open. */
232 if (appWindow->loopEndPending())
233 {
234 appWindow->deferMaximize();
235 WLog_DBG(TAG, "local maximize deferred id=0x%08" PRIx32 " (modal loop open)",
236 static_cast<UINT32>(appWindow->id()));
237 }
238 else
239 {
240 WLog_DBG(TAG, "local maximize id=0x%08" PRIx32 " -> SC_MAXIMIZE",
241 static_cast<UINT32>(appWindow->id()));
242 sendSystemCommand(appWindow, SC_MAXIMIZE);
243 }
244 lock.unlock();
245 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
246}

◆ handleMinimized()

void SdlRail::handleMinimized ( SDL_WindowID  id)

Definition at line 248 of file sdl_rail.cpp.

249{
250 std::unique_lock lock(_windowsLock);
251 auto* appWindow = getWindowBySdlId(id);
252 if (!appWindow || appWindow->isPopup() || !appWindow->window())
253 return;
254 if (appWindow->railMinimized())
255 return; /* echo of reconcile's own SDL_MinimizeWindow */
256 appWindow->setRailMinimized(true);
257 WLog_DBG(TAG, "local minimize id=0x%08" PRIx32 " -> SC_MINIMIZE",
258 static_cast<UINT32>(appWindow->id()));
259 sendSystemCommand(appWindow, SC_MINIMIZE);
260}

◆ handleRestored()

void SdlRail::handleRestored ( SDL_WindowID  id)

Definition at line 273 of file sdl_rail.cpp.

274{
275 std::unique_lock lock(_windowsLock);
276 auto* appWindow = getWindowBySdlId(id);
277 if (!appWindow || appWindow->isPopup() || !appWindow->window())
278 return;
279 /* Resolve which state a RESTORED event ends; both clear = echo of our own restore, skip. */
280 if (appWindow->railMinimized())
281 appWindow->setRailMinimized(false);
282 else if (appWindow->railMaximized())
283 appWindow->setRailMaximized(false);
284 else
285 return;
286 WLog_DBG(TAG, "local restore id=0x%08" PRIx32 " -> SC_RESTORE",
287 static_cast<UINT32>(appWindow->id()));
288 appWindow->invalidateAll();
289 sendSystemCommand(appWindow, SC_RESTORE);
290 lock.unlock();
291 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
292}

◆ handleWaylandResize()

void SdlRail::handleWaylandResize ( SDL_WindowID  id)

Definition at line 1185 of file sdl_rail.cpp.

1186{
1187 if (!sdl::utils::isWaylandDriver())
1188 return;
1189 std::unique_lock lock(_windowsLock);
1190 auto* appWindow = getWindowBySdlId(id);
1191 if (!appWindow || appWindow->isPopup() || !appWindow->window())
1192 return;
1193 /* Track resize for active dragged window. */
1194 if (static_cast<uint32_t>(appWindow->id()) == _localMove.id)
1195 {
1196 if (_localMove.wayland)
1197 _localMove.sawResize = true;
1198 return;
1199 }
1200
1201 int w = 0;
1202 int h = 0;
1203 SDL_GetWindowSize(appWindow->window(), &w, &h);
1204 if ((w <= 0) || (h <= 0))
1205 return;
1206
1207 /* Local window size is the OUTER frame (rect + insets; identical off the inset paths). */
1208 const SDL_Rect vis = appWindow->outerRect();
1209 if ((w == vis.w) && (h == vis.h))
1210 return; /* echo of a size we already reported/applied - nothing new */
1211
1212 /* Compositor snap/tile: report via the debounced sync (never from transitional state). */
1213 lock.unlock();
1214 syncGeometry(id);
1215}

◆ init()

bool SdlRail::init ( RailClientContext *  rail)

Definition at line 666 of file sdl_rail.cpp.

667{
668 _rail = rail;
669 if (!rail)
670 return false;
671
672 registerUpdateCallbacks(_context->context()->update);
673
674 {
675 std::unique_lock lock(_windowsLock);
676 /* Move previous windows to deadWindows for main-thread teardown. */
677 for (auto it = _windows.begin(); it != _windows.end();)
678 {
679 auto cur = it++;
680 _deadWindows.insert(_windows.extract(cur));
681 }
682 auto* settings = _context->context()->settings;
683 const uint32_t caches =
684 freerdp_settings_get_uint32(settings, FreeRDP_RemoteAppNumIconCaches);
685 _iconCacheEntries =
686 freerdp_settings_get_uint32(settings, FreeRDP_RemoteAppNumIconCacheEntries);
687 _iconCache.assign(static_cast<size_t>(caches) * _iconCacheEntries, {});
688 }
689
690 rail->custom = this;
691 rail->ServerExecuteResult = SdlRail::server_execute_result;
692 /* ServerSystemParam not implemented: the server only sends screensaver state here. */
693 rail->ServerLocalMoveSize = SdlRail::server_local_move_size;
694 rail->ServerMinMaxInfo = SdlRail::server_min_max_info;
695 /* Keep default ServerHandshake. */
696
697 WLog_WARN(TAG, "RemoteApp/RAIL support in the SDL client is experimental");
698 const RailPlatformCaps& caps = railPlatformCaps();
699 WLog_DBG(TAG, "RAIL channel initialized: driver=%s positionsReadable=%d transparentWindows=%d",
700 sdl::utils::isWaylandDriver() ? "wayland"
701 : (sdl::utils::isX11Driver() ? "x11" : "other"),
702 caps.positionsReadable ? 1 : 0, caps.supportsTransparentWindows ? 1 : 0);
703#if SDL_VERSION_ATLEAST(3, 4, 10)
704 /* Synchronize WM-driven X11 resizes via _NET_WM_SYNC_REQUEST to prevent torn frames. */
705 if (sdl::utils::isX11Driver())
706 SDL_SetHint(SDL_HINT_VIDEO_X11_ENABLE_XSYNC_EXT, "1");
707#endif
708 return true;
709}
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 175 of file sdl_rail.cpp.

176{
177 std::unique_lock lock(_windowsLock);
178 auto* appWindow = getWindowBySdlId(id);
179 if (!appWindow)
180 return;
181 appWindow->invalidateAll();
182 lock.unlock();
183 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
184}

◆ noteDragResize()

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

Definition at line 1063 of file sdl_rail.cpp.

1064{
1065 std::unique_lock lock(_windowsLock);
1066 if ((_localMove.id == 0) || _localMove.wayland || (_localMove.type != RAIL_WMSZ_MOVE))
1067 return;
1068 auto* appWindow = getWindowBySdlId(id);
1069 if (!appWindow || (static_cast<uint32_t>(appWindow->id()) != _localMove.id))
1070 return;
1071 /* Filter out reconcile echo from server drag-restore. */
1072 if (appWindow->localMoveSizeChanged())
1073 {
1074 const SDL_Rect outer = appWindow->outerRect();
1075 if ((w == outer.w) && (h == outer.h))
1076 return;
1077 }
1078 _localMove.wmSized = true;
1079 _localMove.wmSize = { w, h };
1080}

◆ noteLeftPress()

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

Definition at line 91 of file sdl_rail.hpp.

92 {
93 _lastPressServer = { static_cast<int>(x), static_cast<int>(y) };
94 _lastPointerServer = _lastPressServer;
95 }

◆ noteResizeGrab()

void SdlRail::noteResizeGrab ( SDL_WindowID  id)

Definition at line 1173 of file sdl_rail.cpp.

1174{
1175 std::unique_lock lock(_windowsLock);
1176 if (_localMove.id == 0)
1177 return;
1178 auto* appWindow = getWindowBySdlId(id);
1179 /* Confirm compositor pointer grab on mouse leave during active drag resize. */
1180 if (appWindow && (static_cast<uint32_t>(appWindow->id()) == _localMove.id) &&
1181 _localMove.wayland)
1182 _localMove.sawResize = true;
1183}

◆ noteServerPointer()

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

Definition at line 97 of file sdl_rail.hpp.

98 {
99 _lastPointerServer = { static_cast<int>(x), static_cast<int>(y) };
100 }

◆ ownsWindow()

bool SdlRail::ownsWindow ( SDL_WindowID  id)

Definition at line 169 of file sdl_rail.cpp.

170{
171 std::unique_lock lock(_windowsLock);
172 return getWindowBySdlId(id) != nullptr;
173}

◆ paint()

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

Definition at line 396 of file sdl_rail.cpp.

398{
399 if (!_enabled)
400 return true;
401
402 std::unique_lock lock(_windowsLock);
403
404 /* Report workarea once (avoids maximizing under local panels). */
405 if (_sentWorkArea.w == 0)
406 {
407 SDL_Rect usable{};
408 if (SDL_GetDisplayUsableBounds(SDL_GetPrimaryDisplay(), &usable))
409 sendWorkArea(usable);
410 }
411
412 /* Cascade deletion to child popups whose SDL parent was deleted. */
413 for (auto& [wid, win] : _windows)
414 {
415 if (win.isDeleted() || !win.window())
416 continue;
417 for (SDL_Window* p = SDL_GetWindowParent(win.window()); p; p = SDL_GetWindowParent(p))
418 {
419 auto* owner = getWindowBySdlId(SDL_GetWindowID(p));
420 if (!owner || owner->isDeleted())
421 {
422 win.markDeleted();
423 break;
424 }
425 }
426 }
427
428 /* Erase deleted windows deepest child first to avoid SDL child double-free. */
429 std::vector<std::pair<size_t, uint64_t>> dead;
430 for (auto& [wid, win] : _windows)
431 {
432 if (!win.isDeleted())
433 continue;
434 size_t depth = 0;
435 for (SDL_Window* p = win.window() ? SDL_GetWindowParent(win.window()) : nullptr; p;
436 p = SDL_GetWindowParent(p))
437 depth++;
438 dead.emplace_back(depth, wid);
439 }
440 std::sort(dead.begin(), dead.end(),
441 [](const auto& a, const auto& b) { return a.first > b.first; });
442 for (const auto& [depth, wid] : dead)
443 _windows.erase(wid);
444 /* Windows displaced by a same-id recreate (addWindow) also die here, on the main thread. */
445 _deadWindows.clear();
446
447 /* App windows first so popup parents exist before their popups. */
448 for (auto& it : _windows)
449 {
450 auto& win = it.second;
451 if (win.isPopup())
452 continue;
453 /* Parent owned dialogs to owner. */
454 SDL_Window* parent = nullptr;
455 SDL_Rect parentRect{};
456 auto* owner = resolveParent(win.owner());
457 if (owner && (owner != &win))
458 {
459 parent = owner->window();
460 parentRect = owner->outerRect(); /* the SDL window's on-screen geometry */
461 }
462 win.paint(primary, fallbackFormat, damage, parent, parentRect);
463 /* Adopt WM-refused geometry and update server. */
464 SDL_Rect refusedOuter{};
465 if (win.takeWmOverride(refusedOuter))
466 {
467 const SDL_Rect rect = win.serverRect(refusedOuter);
468 const SDL_Rect cur = win.windowRect();
469 WLog_DBG(TAG,
470 "wm-refused adopt id=0x%08" PRIx32
471 " outer=%dx%d -> srv=%d,%d %dx%d (was %dx%d)",
472 static_cast<uint32_t>(win.id()), refusedOuter.w, refusedOuter.h, rect.x,
473 rect.y, rect.w, rect.h, cur.w, cur.h);
474 if (!SDL_RectsEqual(&rect, &cur))
475 sendClientWindowMove(&win, rect);
476 win.adoptLocalGeometry(rect);
477 }
478 }
479
480 /* Request full repaint after first window is realized (fixes reconnect blank windows). */
481 if (!_refreshSent)
482 {
483 for (auto& it : _windows)
484 {
485 if (it.second.isPopup() || !it.second.window())
486 continue;
487 auto* ctx = &_context->common()->context;
488 const auto dw = static_cast<UINT16>(
489 freerdp_settings_get_uint32(ctx->settings, FreeRDP_DesktopWidth));
490 const auto dh = static_cast<UINT16>(
491 freerdp_settings_get_uint32(ctx->settings, FreeRDP_DesktopHeight));
492 const RECTANGLE_16 all = { 0, 0, dw, dh };
493 if (ctx->update && ctx->update->RefreshRect)
494 (void)ctx->update->RefreshRect(ctx, 1, &all);
495 _refreshSent = true;
496 WLog_DBG(TAG, "refresh-rect sent (first app window realized)");
497 break;
498 }
499 }
500
501 for (auto& it : _windows)
502 {
503 auto& popup = it.second;
504 if (!popup.isPopup())
505 continue;
506
507 /* Latch shadow frame classification once identified. */
508 if (!popup.isFrame() && isShadowFrame(popup))
509 popup.setFrame(true);
510
511 /* Anchor drop shadow to adjoining visible popup. */
512 if (popup.isLayered())
513 {
514 constexpr int reach = 48; /* shadow offset+blur spread from its popup, server px */
515 const SDL_Rect sr = popup.windowRect();
516 bool anchored = false;
517 for (auto& other : _windows)
518 {
519 auto& cand = other.second;
520 if ((&cand == &popup) || !cand.isPopup() || cand.isLayered() || !cand.window() ||
521 ((SDL_GetWindowFlags(cand.window()) & SDL_WINDOW_HIDDEN) != 0))
522 continue;
523 SDL_Rect zone = cand.windowRect();
524 zone.x -= reach;
525 zone.y -= reach;
526 zone.w += 2 * reach;
527 zone.h += 2 * reach;
528 /* Shadow must be contained within reach zone to distinguish popup shadows from
529 * app frame edge shadows. */
530 const bool inside = (sr.x >= zone.x) && (sr.y >= zone.y) &&
531 (sr.x + sr.w <= zone.x + zone.w) &&
532 (sr.y + sr.h <= zone.y + zone.h);
533 if (inside)
534 {
535 anchored = true;
536 break;
537 }
538 }
539 popup.setShadowAnchored(anchored);
540 }
541
542 SdlRailWindow* chosen = nullptr;
543 /* Existing popups have an immutable SDL parent; position against it. */
544 if (auto* pw = popup.window())
545 {
546 SDL_Window* p = SDL_GetWindowParent(pw);
547 chosen = p ? getWindowBySdlId(SDL_GetWindowID(p)) : nullptr;
548 }
549 else
550 {
551 chosen = resolvePopupParent(popup);
552 if (!chosen)
553 WLog_DBG(TAG, "popup id=0x%08" PRIx32 " has no parent app window",
554 static_cast<UINT32>(popup.id()));
555 }
556
557 SDL_Window* parent = nullptr;
558 SDL_Rect parentRect{};
559 if (chosen)
560 {
561 parent = chosen->window();
562 parentRect = chosen->outerRect(); /* the SDL window's on-screen geometry */
563 }
564 popup.paint(primary, fallbackFormat, damage, parent, parentRect);
565 }
566
567 /* All live windows now have real X11 handles: realize the server's z-order. */
568 applyZOrder();
569 return true;
570}

◆ suppressServerInput()

bool SdlRail::suppressServerInput ( SDL_WindowID  id)

Definition at line 864 of file sdl_rail.cpp.

865{
866 std::unique_lock lock(_windowsLock);
867 if (_localMove.id == 0)
868 return false;
869 auto* appWindow = getWindowBySdlId(id);
870 return appWindow && appWindow->localMoveActive() && !_localMove.wayland;
871}

◆ suppressServerMotion()

bool SdlRail::suppressServerMotion ( SDL_WindowID  id)

Definition at line 873 of file sdl_rail.cpp.

874{
875 std::unique_lock lock(_windowsLock);
876 if (_localMove.id == 0)
877 return false;
878 auto* appWindow = getWindowBySdlId(id);
879 /* Suppress motion events during active local move. */
880 return appWindow && appWindow->localMoveActive();
881}

◆ syncGeometry()

void SdlRail::syncGeometry ( SDL_WindowID  id)

Definition at line 1106 of file sdl_rail.cpp.

1107{
1108 std::unique_lock lock(_windowsLock);
1109 auto* w = getWindowBySdlId(id);
1110 if (!w || !w->window() || w->isDeleted() || w->isPopup())
1111 return;
1112
1113 /* Geometry is server/WM-owned when maximized or minimized. */
1114 if (w->effectivelyMaximized())
1115 {
1116 reportMaximizedWorkArea(w);
1117 return;
1118 }
1119 if (w->railMinimized())
1120 return;
1121
1122 const auto wid = static_cast<uint32_t>(w->id());
1123 /* Skip geometry sync while awaiting server restored rect. */
1124 if (w->localMoveActive() || (_localMove.id == wid) || w->loopEndPending() ||
1125 w->stateTransitionPending() || w->awaitingRestoreRect())
1126 return;
1127
1128 int lw = 0;
1129 int lh = 0;
1130 SDL_GetWindowSize(w->window(), &lw, &lh);
1131 if ((lw <= 0) || (lh <= 0))
1132 return;
1133
1134 /* Ignore invalid zero-content window size updates. */
1135 const SDL_Rect ins = w->insets();
1136 if (((lw - ins.x - ins.w) <= 0) || ((lh - ins.y - ins.h) <= 0))
1137 {
1138 WLog_DBG(TAG, "geometry sync id=0x%08" PRIx32 " %dx%d dropped (content collapsed)", wid, lw,
1139 lh);
1140 return;
1141 }
1142
1143 const SDL_Rect vis = w->outerRect();
1144 const bool posKnown = railPlatformCaps().positionsReadable;
1145 int lx = vis.x;
1146 int ly = vis.y;
1147 if (posKnown)
1148 SDL_GetWindowPosition(w->window(), &lx, &ly);
1149 else
1150 clampIntoDesktop(lx, ly, lw, lh);
1151
1152 /* Check if window settled at applied target. */
1153 if ((lw == vis.w) && (lh == vis.h) && (!posKnown || ((lx == vis.x) && (ly == vis.y))))
1154 {
1155 w->clearGeomApplyPending();
1156 return; /* Ignore self-echo when geometry converged. */
1157 }
1158
1159 /* Drop transient echo events while client geometry application is in flight. */
1160 if (w->geomApplyPending())
1161 {
1162 /* Allow live Wayland compositor resize to advance beyond in-flight apply. */
1163 const bool overtaken = !posKnown && ((lw > vis.w) || (lh > vis.h));
1164 if (!overtaken)
1165 return;
1166 w->clearGeomApplyPending();
1167 }
1168
1169 WLog_DBG(TAG, "geometry sync id=0x%08" PRIx32 " local=%d,%d %dx%d (sync)", wid, lx, ly, lw, lh);
1170 reportAndAdopt(w, lx, ly, lw, lh);
1171}

◆ translateToServer()

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

Definition at line 359 of file sdl_rail.cpp.

360{
361 std::unique_lock lock(_windowsLock);
362 auto* appWindow = getWindowBySdlId(id);
363 if (!appWindow)
364 return false;
365 SDL_FPoint rpos = { x, y };
366 if (auto* renderer = appWindow->renderer())
367 (void)SDL_RenderCoordinatesFromWindow(renderer, x, y, &rpos.x, &rpos.y);
368 /* Window-local to server-absolute, aligned with surface blit anchor. */
369 const SDL_Point origin = appWindow->serverOrigin();
370 x = rpos.x + static_cast<float>(origin.x);
371 y = rpos.y + static_cast<float>(origin.y);
372 return true;
373}

◆ uninit()

bool SdlRail::uninit ( RailClientContext *  rail)

Definition at line 711 of file sdl_rail.cpp.

712{
713 WINPR_ASSERT(rail);
714 /* Leave rail->custom set: the server callbacks only assert it, so clearing it would crash a
715 * late order in a release build. _enabled gates them instead. */
716 std::unique_lock lock(_windowsLock);
717 _refreshSent = false;
718 /* Reset session and move state on reconnect. */
719 _localMove = {};
720 _sessionMargins = {};
721 _sentWorkArea = {};
722 _clientActiveId = 0;
723 _focusedAppId = 0;
724 _zOrder.clear();
725 _appliedZOrder.clear();
726 _zOrderDirty = false;
727 _iconCache.clear();
728 _iconScratch = {};
729 /* Mark windows for main-thread teardown. */
730 WLog_DBG(TAG, "RAIL channel uninit, marking %zu windows for main-thread teardown",
731 _windows.size());
732 for (auto& kv : _windows)
733 kv.second.markDeleted();
734 _rail = nullptr;
735 _enabled = false;
736 lock.unlock();
737 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
738 return true;
739}

◆ updateWindowFromSurface()

UINT SdlRail::updateWindowFromSurface ( gdiGfxSurface *  surface)

Definition at line 619 of file sdl_rail.cpp.

620{
621 if (!surface)
622 return CHANNEL_RC_OK;
623
624 std::unique_lock lock(_windowsLock);
625 auto appWindow = getWindow(surface->windowId);
626 if (!appWindow)
627 {
628 /* Drop GFX surface for unknown windows (repaints later). */
629 WLog_VRB(TAG, "gfx surface for untracked id=0x%08" PRIx64, surface->windowId);
630 return CHANNEL_RC_OK;
631 }
632
633 /* Clamp mapped dimensions to allocated surface bounds. */
634 const uint32_t w =
635 std::min(surface->mappedWidth ? surface->mappedWidth : surface->width, surface->width);
636 const uint32_t h =
637 std::min(surface->mappedHeight ? surface->mappedHeight : surface->height, surface->height);
638
639 /* Consume per-frame damage. */
640 UINT32 nbRects = 0;
641 const RECTANGLE_16* rects = region16_rects(&surface->invalidRegion, &nbRects);
642 /* Skip undamaged surfaces during EndFrame. */
643 const bool remapped = appWindow->takeSurfaceChange(surface->surfaceId);
644 if ((nbRects == 0) && !remapped)
645 return CHANNEL_RC_OK;
646 appWindow->updateGfxSurface(surface->data, surface->scanline, w, h, rects, nbRects,
647 surface->format);
648 region16_clear(&surface->invalidRegion);
649
650 (void)sdl_push_user_event(SDL_EVENT_USER_UPDATE);
651 return CHANNEL_RC_OK;
652}

◆ UpdateWindowFromSurface()

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

Definition at line 654 of file sdl_rail.cpp.

655{
656 WINPR_ASSERT(context);
657 auto gdi = static_cast<rdpGdi*>(context->custom);
658 if (!gdi || !gdi->context)
659 return CHANNEL_RC_OK;
660 auto sdl = get_context(gdi->context);
661 if (!sdl)
662 return CHANNEL_RC_OK;
663 return sdl->getRailChannelContext().updateWindowFromSurface(surface);
664}

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