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