FreeRDP
Loading...
Searching...
No Matches
sdl_rail.hpp
1
19#pragma once
20
21#include <cstdint>
22#include <map>
23#include <atomic>
24#include <mutex>
25#include <optional>
26#include <vector>
27
28#include <SDL3/SDL.h>
29
30#include <freerdp/client/rail.h>
31#include <freerdp/client/rdpgfx.h>
32#include <freerdp/gdi/gfx.h>
33#include <freerdp/update.h>
34
35#include "sdl_input_mapping.hpp"
36#include "sdl_rail_window.hpp"
37
38class SdlContext;
39
47class SdlRail
48{
49 public:
50 explicit SdlRail(SdlContext* context);
51 ~SdlRail();
52
53 SdlRail(const SdlRail&) = delete;
54 SdlRail(SdlRail&&) = delete;
55 SdlRail& operator=(const SdlRail&) = delete;
56 SdlRail& operator=(SdlRail&&) = delete;
57
58 bool init(RailClientContext* rail);
59 bool uninit(RailClientContext* rail);
60
61 [[nodiscard]] bool enabled() const
62 {
63 return _enabled;
64 }
65
66 /* Main thread: whether the desktop window may show; before the RemoteApp starts, only
67 * for a logon dialog or a slow start. */
68 [[nodiscard]] bool desktopMayShow();
69 /* Any thread: the server shows a logon dialog that needs the user (LogonErrorInfo). */
70 void noteLogonPrompt();
71
72 /* Paint all RAIL windows (window-mapped GFX surface, or legacy desktop region).
73 * `damage` = server-updated desktop rects for this cycle (empty = nothing new). */
74 bool paint(SDL_Surface* primary, SDL_PixelFormat fallbackFormat,
75 const std::vector<SDL_Rect>& damage);
76
77 /* Called from the GFX pipeline when a window-mapped surface updated (RDP thread). */
78 UINT updateWindowFromSurface(gdiGfxSurface* surface);
79
80 /* GFX callback: install as gfx->UpdateWindowFromSurface after gdi_graphics_pipeline_init. */
81 static UINT UpdateWindowFromSurface(RdpgfxClientContext* context, gdiGfxSurface* surface);
82
83 /* Input routing (main thread): true if the SDL window id is one of our RAIL windows. */
84 [[nodiscard]] bool ownsWindow(SDL_WindowID id);
85 /* Mark a RAIL window fully dirty and request a repaint (e.g. on expose). */
86 void invalidateWindow(SDL_WindowID id);
87 /* Local maximize/minimize/restore state handlers. */
88 void handleMaximized(SDL_WindowID id);
89 void handleMinimized(SDL_WindowID id);
90 void handleRestored(SDL_WindowID id);
91 void handleClose(SDL_WindowID id);
92 /* Local focus change: send ClientActivate, like xf FocusIn/FocusOut. */
93 void handleFocus(SDL_WindowID id, bool gained);
94 void handleDisplayChanged(SDL_WindowID id, SDL_DisplayID display);
95 void noteUsableBoundsChanged(SDL_DisplayID display);
96 void updateCoordinates();
97 /* Activate window on server if not already active. */
98 void ensureActive(SDL_WindowID id);
99 /* Append the visible RemoteApp windows for pointer mapping; see SdlContext::inputWindows.
100 * Window id follows its display first, so its server rect is current. */
101 void inputWindows(SDL_WindowID id, std::vector<SdlInputWindow>& windows,
102 std::optional<size_t>& source);
103 void notePopupMoved(SDL_WindowID id);
104 /* Record position of last left button-down. */
105 void noteLeftPress(float x, float y)
106 {
107 _lastPressServer = { static_cast<int>(x), static_cast<int>(y) };
108 _lastPointerServer = _lastPressServer;
109 }
110 /* Record pointer position on server. */
111 void noteServerPointer(float x, float y)
112 {
113 _lastPointerServer = { static_cast<int>(x), static_cast<int>(y) };
114 }
115 /* Changes when the client moves a server window. */
116 [[nodiscard]] uint32_t pointerEpoch() const
117 {
118 return _pointerEpoch;
119 }
120 /* Local move/resize under the WM grab: server input is suppressed meanwhile. */
121 bool dragActive(SDL_WindowID id);
122 /* Suppress motion events during active local move/resize. */
123 bool suppressServerMotion(SDL_WindowID id);
124
125 /* Start native interactive window move/resize. */
126 void handleLocalMoveRequested(uint32_t windowId, uint16_t moveType);
127 /* Track size change during local move. */
128 void noteDragResize(SDL_WindowID id, int w, int h);
129 /* Sync window geometry to server. */
130 void syncGeometry(SDL_WindowID id);
131 /* Finalize pending local move/resize. */
132 void completeLocalMoveIfPending();
133
134 /* Finalize Wayland resize. */
135 void completeWaylandResize(bool definitive = false);
136 /* Wayland: the compositor runs the interactive resize (mouse leave or configure). */
137 void noteResizeGrab(SDL_WindowID id);
138 void settleWaylandResize();
139 /* X11: end a WM drag once the button is up. */
140 void pollDragRelease();
141
144 [[nodiscard]] bool windowGeometry(uint64_t id, SDL_Window*& window, int& width, int& height);
145
146 private:
147 void queueScreenSaverUpdate();
148 void updateScreenSaver(); /* main thread */
149 /* Latch a local move/resize; returns the window for a native modal loop. */
150 [[nodiscard]] SDL_Window* beginLocalMove(uint32_t windowId, uint16_t moveType);
151 /* _windows helpers: callers must hold _windowsLock. */
152 [[nodiscard]] SdlRailWindow* getWindow(uint64_t id);
153 [[nodiscard]] SdlRailWindow* getWindowBySdlId(SDL_WindowID id);
154 /* Server coordinate for window-local (0,0), including Wayland popup placement. */
155 [[nodiscard]] SDL_Point inputLayoutPosition(SdlRailWindow* appWindow);
156 /* Send RAIL_ACTIVATE_ORDER and track _clientActiveId; caller holds _windowsLock. */
157 void sendClientActivate(uint32_t wid, bool enabled);
158 SdlRailWindow* addWindow(uint64_t id, const SDL_Rect& rect);
159 /* The window `ownerId` names, if it is a live non-popup app window (a valid popup/dialog
160 * parent); else nullptr. Caller holds _windowsLock. */
161 [[nodiscard]] SdlRailWindow* resolveParent(uint64_t ownerId);
162 [[nodiscard]] SdlRailWindow* resolvePopupParent(const SdlRailWindow& popup);
163 [[nodiscard]] bool isShadowFrame(const SdlRailWindow& popup);
164
165 void enableRemoteAppMode(bool enable);
166 /* Report a work area to the server (SPI_SET_WORK_AREA, server coords). Main thread. No-op if it
167 * matches the last one sent. */
168 void sendWorkArea(const SDL_Rect& local);
169 /* Report a maximized window's WM-given geometry as the work area. Caller holds _windowsLock. */
170 void reportMaximizedWorkArea(SdlRailWindow* appWindow);
171 /* Clamp an outer origin so its server rect stays on the desktop. */
172 void clampIntoDesktop(const SdlRailWindow& appWindow, int& x, int& y, int w, int h) const;
173 /* Send a RAIL_SYSCOMMAND_ORDER for the window. Caller holds _windowsLock. */
174 void sendSystemCommand(SdlRailWindow* appWindow, uint16_t command);
175 /* Realize the server's top-level z-order (X11 only, focus-neutral). Caller holds _windowsLock.
176 */
177 void applyZOrder();
178 /* Session-wide resize margins (guarded by _windowsLock). */
179 SDL_Rect _sessionMargins = { 0, 0, 0, 0 };
180 SDL_Point _lastPressServer = { 0, 0 }; /* last forwarded left press; main thread */
181 SDL_Point _lastPointerServer = { 0, 0 }; /* last forwarded pointer position; main thread */
182 /* Server window coordinates are relative to the primary monitor, input coordinates to the
183 * top left of the remote desktop. */
184 SDL_Point _displayShift = { 0, 0 }; /* local display minus server coordinates */
185 SDL_Point _localShift = { 0, 0 }; /* applied to local windows where positions work */
186
187 /* --- RAIL server callbacks (static, dispatched to the instance) --- */
188 static UINT server_app_id(RailClientContext* context,
189 const RAIL_GET_APPID_RESP_ORDER* response);
190 static UINT server_execute_result(RailClientContext* context,
191 const RAIL_EXEC_RESULT_ORDER* execResult);
192 static UINT server_local_move_size(RailClientContext* context,
193 const RAIL_LOCALMOVESIZE_ORDER* localMoveSize);
194 static UINT server_min_max_info(RailClientContext* context,
195 const RAIL_MINMAXINFO_ORDER* minMaxInfo);
196 static UINT server_power_display_request(RailClientContext* context,
197 const RAIL_POWER_DISPLAY_REQUEST* power);
198
199 /* --- window order callbacks (registered on rdpUpdate::window) --- */
200 void registerUpdateCallbacks(rdpUpdate* update);
201 static BOOL window_common(rdpContext* context, const WINDOW_ORDER_INFO* orderInfo,
202 const WINDOW_STATE_ORDER* windowState);
203 /* window_common field handlers (split out for readability). */
204 void updateMargins(SdlRailWindow* appWindow, const WINDOW_ORDER_INFO* orderInfo,
205 const WINDOW_STATE_ORDER* state);
206 static void updateShowState(SdlRailWindow* appWindow, const WINDOW_ORDER_INFO* orderInfo,
207 const WINDOW_STATE_ORDER* state);
208 static void updateVisRects(SdlRailWindow* appWindow, const WINDOW_ORDER_INFO* orderInfo,
209 const WINDOW_STATE_ORDER* state);
210 static BOOL window_icon(rdpContext* context, const WINDOW_ORDER_INFO* orderInfo,
211 const WINDOW_ICON_ORDER* windowIcon);
212 static BOOL window_cached_icon(rdpContext* context, const WINDOW_ORDER_INFO* orderInfo,
213 const WINDOW_CACHED_ICON_ORDER* windowCachedIcon);
214 static BOOL window_delete(rdpContext* context, const WINDOW_ORDER_INFO* orderInfo);
215 static BOOL monitored_desktop(rdpContext* context, const WINDOW_ORDER_INFO* orderInfo,
216 const MONITORED_DESKTOP_ORDER* monitoredDesktop);
217 static BOOL non_monitored_desktop(rdpContext* context, const WINDOW_ORDER_INFO* orderInfo);
218
219 static SdlRail* get(rdpContext* context);
220
221 /* Shared finalize tail (caller holds _windowsLock): report the final rect to the server and
222 * adopt it locally. Both the X11 and Wayland completion paths funnel here. */
223 void reportAndAdopt(SdlRailWindow* appWindow, int x, int y, int w, int h);
224 /* Send ClientWindowMove to server. */
225 void sendClientWindowMove(SdlRailWindow* appWindow, const SDL_Rect& serverRect);
226 /* Caller holds _windowsLock. */
227 void followDisplay(SdlRailWindow* appWindow, bool now);
228 void moveToDisplay(SdlRailWindow* appWindow, SDL_DisplayID display);
229 [[nodiscard]] bool monitorRect(SDL_DisplayID display, SDL_Rect& rect) const;
230
231 /* MS-RDPERP icon cache slot for cacheId:cacheEntry; nullptr if out of range. cacheId 0xFF =
232 * "do not cache" scratch slot (the spec says 0xFFFF but the field is one byte). */
233 [[nodiscard]] SdlRailIcon* iconCacheLookup(uint32_t cacheId, uint32_t cacheEntry);
234
235 private:
236 /* Win32: redraw while a native size/move loop blocks our loop. */
237 static void redrawDuringSizeMove(void* userdata);
238 /* Win32: a resize on the native frame drives the local resize frame like a server one. */
239 static void nativeResize(void* userdata, SDL_WindowID id, int moveType, bool end);
240 SdlContext* _context;
241 RailClientContext* _rail = nullptr;
242 /* Written on the RDP thread (RemoteApp mode toggles), read lock-free on the main thread. */
243 std::atomic<bool> _enabled = false;
244 std::atomic<uint32_t> _pointerEpoch = 0;
245 bool _refreshSent = false; /* one full RefreshRect per connection, at first window realize */
246 std::atomic<bool> _appStarted = false;
247 /* The desktop window may show: RAIL started, a logon prompt, or the hold ran out. */
248 std::atomic<bool> _desktopReleased = false;
249 std::atomic<uint64_t> _desktopHoldUntil = 0; /* end of the logon desktop hold (ticks) */
250 std::atomic<bool> _displayRequested = false;
251 uint32_t _sizeMoveRedraws = 0;
252 SDL_WindowID _nativeResizeId = 0; /* window resized on its native frame, 0 = none */
253 /* Last work area reported to the server (server coordinates). */
254 SDL_Rect _sentWorkArea = { 0, 0, 0, 0 };
255 /* Guards _windows (RDP thread mutates, main thread paints/iterates, GFX thread looks up).
256 * Erased on the main thread only, so SDL windows are destroyed there. */
257 mutable std::mutex _windowsLock;
258 std::map<uint64_t, SdlRailWindow> _windows;
259 /* Windows awaiting destruction on the main thread. */
260 std::multimap<uint64_t, SdlRailWindow> _deadWindows;
261 /* State of active interactive WM move/resize. */
262 struct LocalMove
263 {
264 uint32_t id = 0; /* windowId of the active move, 0 = none (RAIL ids are non-zero) */
265 uint16_t type = 0; /* RAIL_WMSZ_* of the active local move */
266 SDL_Point anchor = { 0, 0 }; /* modal-loop anchor (forwarded press) of this drag */
267 SDL_Point pointer = { 0, 0 }; /* server's frozen cursor (last forward) of this drag */
268 /* Wayland: a WINDOW_RESIZED arrived since the grab started, so the op really resized
269 * (guards against a bare pointer re-enter finalizing a no-op click). Main thread. */
270 bool sawResize = false;
271 Uint64 lastResize = 0; /* time of the last size step */
272 /* Server END order arrived while the drag was still pending (app closed its loop early):
273 * the completion must not send the synthetic button-up. Guarded by _windowsLock. */
274 bool serverEnded = false;
275 /* Last WM-imposed size while plain X11 move was active (self-echoes filtered out). */
276 bool wmSized = false;
277 SDL_Point wmSize = { 0, 0 };
278 };
279 LocalMove _localMove;
280 /* Server move/size loop in progress (_windowsLock). */
281 struct
282 {
283 uint64_t id = 0;
284 SDL_DisplayID display = 0;
285 } _moveLoop;
286 /* Last focused app window ID. */
287 uint64_t _focusedAppId = 0;
288 /* Server-driven z-order list. */
289 std::vector<uint32_t> _zOrder;
290 std::vector<uint32_t> _appliedZOrder;
291 uint32_t _clientActiveId = 0; /* last window we told the server to activate (ClientActivate) */
292 bool _zOrderDirty = false;
293 /* Icon cache (RDP thread): index = cacheId * entries + cacheEntry, sized from settings. */
294 std::vector<SdlRailIcon> _iconCache;
295 SdlRailIcon _iconScratch;
296 uint32_t _iconCacheEntries = 0;
297};
bool windowGeometry(uint64_t id, SDL_Window *&window, int &width, int &height)
Definition sdl_rail.cpp:67