FreeRDP
Loading...
Searching...
No Matches
sdl_rail_window.hpp
1
19#pragma once
20
21#include <atomic>
22#include <cstdint>
23#include <memory>
24#include <mutex>
25#include <optional>
26#include <string>
27#include <vector>
28
29#include <SDL3/SDL.h>
30
31#include <freerdp/types.h>
32
33class SdlWindow;
34
35/* A decoded RAIL window icon (BGRA32 pixels). */
37{
38 uint32_t w = 0;
39 uint32_t h = 0;
40 std::vector<uint8_t> bgra;
41};
42
49class SdlRailWindow
50{
51 public:
52 SdlRailWindow(uint64_t id, const SDL_Rect& rect);
53 SdlRailWindow(SdlRailWindow&&) = delete;
54 SdlRailWindow(const SdlRailWindow&) = delete;
55 SdlRailWindow& operator=(const SdlRailWindow&) = delete;
56 SdlRailWindow& operator=(SdlRailWindow&&) = delete;
57 ~SdlRailWindow();
58
59 [[nodiscard]] uint64_t id() const;
60 [[nodiscard]] SDL_WindowID sdlId() const;
61 [[nodiscard]] SDL_Window* window() const;
62 [[nodiscard]] SDL_Renderer* renderer() const;
63
64 /* --- state setters, safe to call from the RDP thread (no SDL window calls) --- */
65
66 /* Server-driven geometry (from WINDOW_ORDER). */
67 void updateWindowRect(const SDL_Rect& rect);
68 /* Server-reported window rect. */
69 [[nodiscard]] SDL_Rect windowRect() const;
70
71 /* Local move/resize state. */
72 void setLocalMoveActive(bool active);
73 [[nodiscard]] bool localMoveActive() const;
74 /* True if the server resized the window during the current/last local move (drag-restore). */
75 [[nodiscard]] bool localMoveSizeChanged() const;
76 /* Where the server re-anchored its window mid-move (valid when localMoveSizeChanged()). */
77 [[nodiscard]] SDL_Point localMoveServerPos() const;
78 void adoptLocalGeometry(const SDL_Rect& rect);
79 /* Resize anchor corner during local resize. */
80 void setResizeAnchor(bool right, bool bottom);
81
82 /* Deferred destroy: mark here (RDP thread), erased on the main thread in SdlRail::paint. */
83 void markDeleted();
84 /* Drop the local SDL window only (its SDL parent is going away); the server state is kept
85 * and the next reconcile re-creates the window under a new parent. Main thread. */
86 void releaseWindow();
87 [[nodiscard]] bool isDeleted() const;
88
89 /* In-flight local state application flag. */
90 [[nodiscard]] bool geomApplyPending() const;
91 void clearGeomApplyPending();
92
93 /* Deferred completion of server modal move/size loop. */
94 void armLoopEnd(); /* completion: now awaiting the END order */
95 [[nodiscard]] bool loopEndPending() const;
96 void deferMaximize(); /* WM snap-to-top mid-drag: SC_MAXIMIZE on close */
97 void deferSnap(const SDL_Rect& serverRect); /* WM snap/tile: resend rect on close */
98 void clearLoopEnd(); /* a fresh drag of this window supersedes the pending close */
100 {
101 bool maximize = false;
102 bool snap = false;
103 SDL_Rect snapRect = { 0, 0, 0, 0 };
104 };
105 [[nodiscard]] LoopEndActions takeLoopEnd(); /* END order: drain queued actions and reset */
106
107 /* Adopt WM-refused geometry. */
108 [[nodiscard]] bool takeWmOverride(SDL_Rect& outer);
109
110 /* Visible sub-rects (window-relative); only these are painted so windows on top don't bleed. */
111 void setVisibilityRects(std::vector<SDL_Rect> rects);
112 /* Server visible-region origin offset. */
113 void setVisibleOffset(SDL_Point offset);
114
115 /* Server min/max tracking size (RAIL MinMaxInfo); applied in reconcile. */
116 void setMinMaxSize(SDL_Point minSize, SDL_Point maxSize);
117
118 /* Server outside resize margins. */
119 void setResizeMargins(int left, int top, int right, int bottom);
120 [[nodiscard]] SDL_Rect resizeMargins() const; /* x=left y=top w=right h=bottom */
121 /* Raw server frame margins. */
122 void setFrameMargins(const SDL_Rect& m);
123 [[nodiscard]] SDL_Rect frameMargins() const;
124 [[nodiscard]] bool takeUnconfirmedReport(SDL_Rect& rect);
125 void noteDisplayChange();
126 [[nodiscard]] bool takeDisplayChange(bool now);
127 [[nodiscard]] bool deferMaximizeUntilEcho();
128 [[nodiscard]] bool takeMaximizeAfterEcho();
129
130 /* Owning window id (WINDOW_ORDER_FIELD_OWNER); popups position relative to it. */
131 void setOwner(uint64_t ownerId);
132 [[nodiscard]] uint64_t owner() const;
133 /* Popup = caption-less transient (menu/dropdown/tooltip); created as an SDL popup. */
134 [[nodiscard]] bool isPopup() const;
135 /* Full-screen popup treated as a top-level window. */
136 [[nodiscard]] bool isFullscreen() const;
137 /* App window or fullscreen popup without WS_EX_NOACTIVATE (e.g. on-screen keyboards). */
138 [[nodiscard]] bool activatable() const;
139 /* Layered decoration (drop shadow); realized only while anchored to a visible popup. */
140 [[nodiscard]] bool isLayered() const;
141 /* Mark shadow anchored to an active popup. */
142 void setShadowAnchored(bool anchored);
143 void setFrame(bool frame);
144 [[nodiscard]] bool isFrame() const;
145 /* Local window insets for resize bands. */
146 [[nodiscard]] SDL_Rect insets() const;
147 /* Server rect inflated by insets() = the local SDL window's on-screen geometry. */
148 [[nodiscard]] SDL_Rect outerRect() const;
149 /* Server coordinate for window-local (0,0), aligned with blit anchor. */
150 [[nodiscard]] SDL_Point serverOrigin() const;
151 /* Wayland popup placement, see notePlaced(). */
152 void notePlaced();
153 [[nodiscard]] bool placedRel(SDL_Point& rel) const;
154 void placePopup(const SDL_Rect& vis, const SDL_Rect& parentRect);
155 void commitPlacement();
156 /* Inverse of outerRect(): strip insets() off a local outer rect to recover the server rect. */
157 [[nodiscard]] SDL_Rect serverRect(const SDL_Rect& outer) const;
158 void setLocalShift(const SDL_Point& shift);
159
160 void setStyle(uint32_t style, uint32_t exStyle);
161 void setTitle(const std::string& title);
162 /* Application ID reported by the server (RAIL Get AppID); used as the window's app_id. */
163 void setAppId(const std::string& appId);
164 void setVisible(bool visible);
165 /* Window icon (WindowIcon/WindowCachedIcon orders); applied in reconcile. */
166 void setIcon(const SdlRailIcon& icon);
167
168 /* --- main thread only (SDL window ops) --- */
169
170 /* Window-mapped GFX surface (RDP thread): deep-copies the damaged gdi pixels for paint(). */
171 [[nodiscard]] bool takeSurfaceChange(uint32_t surfaceId);
172 void updateGfxSurface(const void* data, uint32_t stride, uint32_t width, uint32_t height,
173 const RECTANGLE_16* damage, uint32_t nbDamage, uint32_t format);
174 /* Mark the whole window dirty for re-blit (e.g. on expose). */
175 void invalidateAll();
176
177 /* Maximize/minimize sync: local state (rail) vs server-reported state (RDP thread). */
178 [[nodiscard]] bool railMaximized() const
179 {
180 return _maxState.rail;
181 }
182 void setRailMaximized(bool m)
183 {
184 std::unique_lock lock(_gfxLock);
185 _maxState.rail = m;
186 }
187 void setServerMaximized(bool m);
188 [[nodiscard]] bool railMinimized() const
189 {
190 return _minState.rail;
191 }
192 void setRailMinimized(bool m)
193 {
194 std::unique_lock lock(_gfxLock);
195 _minState.rail = m;
196 }
197 void setServerMinimized(bool m);
198 void noteLocalRestore();
199 [[nodiscard]] bool takeRestoreRetry();
200 /* Max or min pins the geometry (server/WM owns it): the client geometry apply is skipped. */
201 /* Re-arm geometry apply after server drag-restore. */
202 void markGeometryDirty()
203 {
204 std::unique_lock lock(_gfxLock);
205 _geometryDirty = true;
206 }
207
208 [[nodiscard]] bool geometryFrozen() const
209 {
210 return _maxState.rail || _maxState.server || _minState.rail || _minState.server;
211 }
212 /* A server restore has been applied but its rect has not arrived: geometry is unknown. */
213 [[nodiscard]] bool awaitingRestoreRect() const
214 {
215 std::unique_lock lock(_gfxLock);
216 return restoreRectPending();
217 }
218 /* Insets have not yet been updated for the current maximized state. */
219 [[nodiscard]] bool insetsStale() const;
220 /* Local or live-WM maximized (railMaximized may lag the SDL flag during a snap). */
221 [[nodiscard]] bool effectivelyMaximized() const;
222 /* Transition in flight (local intent vs server state). WM resizes only trusted outside. */
223 [[nodiscard]] bool stateTransitionPending() const
224 {
225 /* _maxState/_minState are written under _gfxLock on the RDP thread. */
226 std::unique_lock lock(_gfxLock);
227 return _maxState.dirty || _minState.dirty || (_maxState.rail != _maxState.server) ||
228 (_minState.rail != _minState.server);
229 }
230
231 /* Create/move/show the local SDL window to match pending state; popups use parent+rect. */
232 bool reconcile(SDL_Window* parent, const SDL_Rect& parentRect);
233 /* Render: GFX surface if mapped, else the shared desktop region. `damage` = updated rects. */
234 bool paint(SDL_Surface* primary, SDL_PixelFormat fallbackFormat,
235 const std::vector<SDL_Rect>& damage, SDL_Window* parent, const SDL_Rect& parentRect);
236
237 private:
238 /* One maximize/minimize state pair: local (sent to server), server-reported, pending apply. */
239 struct StateSync
240 {
241 /* Atomic flags accessed lock-free by railMaximized()/geometryFrozen(). */
242 std::atomic<bool> rail{ false };
243 std::atomic<bool> server{ false };
244 bool dirty = false;
245 };
246 void setServerState(StateSync& s, bool m);
247 /* Caller holds _gfxLock and checked _win. True when it left the state (restore branch). */
248 bool applyServerState(StateSync& s, const char* what, bool (*enter)(SDL_Window*));
249 /* Caller holds _gfxLock. */
250 [[nodiscard]] bool restoreRectPending() const;
251 void setAppliedInsets(const SDL_Rect& insets); /* caller holds _gfxLock */
252 [[nodiscard]] bool styleResizable() const; /* caller holds _gfxLock */
253 /* Popups whose GFX surface may carry per-pixel alpha (paintGfx). */
254 [[nodiscard]] bool honorsAlpha() const
255 {
256 return _isPopup && !_layered;
257 }
258 /* Window role for logging. */
259 [[nodiscard]] const char* role() const
260 {
261 if (_overlay)
262 return _layered ? "overlay-layered" : "overlay";
263 return _layered ? "layered" : (_isPopup ? "popup" : "app");
264 }
265 [[nodiscard]] bool isFullDisplaySize() const; /* caller holds _gfxLock */
266 /* Band eligibility margins. */
267 [[nodiscard]] SDL_Rect bandMargins() const;
268 [[nodiscard]] SDL_Rect bandInsets() const; /* caller holds _gfxLock */
269 [[nodiscard]] SDL_Point blitOffset() const; /* caller holds _gfxLock */
270 [[nodiscard]] SDL_Rect targetOuterRect() const; /* caller holds _gfxLock */
271 bool create(SDL_Window* parent, const SDL_Rect& parentRect);
272 bool paintGfx(SDL_PixelFormat format);
273 bool paintLegacy(SDL_Surface* primary, const std::vector<SDL_Rect>& damage);
274 void placeRequested();
275 [[nodiscard]] bool holdForPlacement();
276
277 uint64_t _id;
278 std::unique_ptr<SdlWindow> _win; /* local window+renderer (lazy, main thread) */
279 SDL_Rect _windowRect; /* server offset/size */
280 SDL_Point _localShift = { 0, 0 }; /* Offset applied to local window coordinates */
281 uint32_t _style = 0;
282 uint32_t _exStyle = 0;
283 /* The shadow-rule suppression report in reconcile is once per window. */
284 bool _shadowSuppressLogged = false;
285 bool _everResizable = false; /* latched: ever had a resizable style */
286 uint64_t _ownerId = 0;
287 SDL_Rect _resizeMargins = { 0, 0, 0, 0 }; /* x=left y=top w=right h=bottom */
288 SDL_Rect _frameMargins = { 0, 0, 0, 0 }; /* raw server margins, for ClientWindowMove */
289 /* Insets applied to active SDL window; stripped to recover server coordinates. */
290 SDL_Rect _appliedInsets = { 0, 0, 0, 0 };
291 bool _wasMaximized = false; /* refresh insets on the maximize rising edge */
292 SDL_Point _minSize = { 0, 0 };
293 SDL_Point _maxSize = { 0, 0 };
294 bool _minMaxDirty = false;
295 bool _isPopup = false;
296 bool _fullscreen = false; /* full-display popup as fullscreen toplevel (Wayland) */
297 bool _layered = false; /* caption-less WS_EX_LAYERED decoration (drop shadow) */
298 bool _overlay = false; /* layered tool window: drag image / drag feedback */
299 bool _popupClassified = false; /* isPopup/_layered frozen after the first (creation) style */
300 bool _shadowAnchored = false; /* a visible popup adjoins this shadow (see setShadowAnchored) */
301 bool _frame = false; /* companion shadow frame */
302 bool _parentApplied = false; /* transient-for owner applied (owned non-popup dialogs) */
303 StateSync _maxState; /* maximize sync */
304 StateSync _minState; /* minimize sync */
305 std::string _title = "RdpRailWindow";
306 SdlRailIcon _icon;
307 bool _iconDirty = false;
308 bool _visible = false;
309 bool _geometryDirty = true;
310 bool _titleDirty = false;
311 std::string _appId;
312 bool _appIdDirty = false;
313 bool _topmost = false;
314 bool _topmostDirty = false;
315 bool _styleDirty = false; /* resizability needs re-applying (style change) */
316 bool _painted = false; /* full copy done; afterwards only damage regions are re-copied */
317 bool _gfxPresented = false; /* presented own GFX content once: gate the first map on it */
318 bool _mapped = false; /* shown at least once (one-shot show+raise) */
319 bool _localMoveActive = false; /* WM move/resize in progress: ignore server geometry + input */
320 bool _localMoveIsResize = false; /* local move is a resize vs a move */
321 bool _localMoveSizeChanged = false; /* server resized mid-move (drag-restore) */
322 SDL_Point _localMoveServerPos = { 0, 0 }; /* the re-anchored server origin (see above) */
323 /* Deferred server-modal-loop close for THIS window (see armLoopEnd()). */
324 struct
325 {
326 bool pending = false;
327 bool maximize = false;
328 bool snap = false;
329 SDL_Rect snapRect = { 0, 0, 0, 0 };
330 } _loopEnd;
331 bool _geomApplyPending = false; /* a client-issued geometry/state apply is still settling */
332 std::vector<SDL_Rect> _reported; /* rects sent to the server and not echoed yet */
333 uint64_t _reportCheckAt = 0; /* when to resend */
334 uint64_t _displayCheckAt = 0; /* when to follow the display */
335 bool _maximizeAfterEcho = false; /* SC_MAXIMIZE once _reported is answered */
336 bool _wmRefused = false; /* WM refused resize; adopt its geometry (takeWmOverride) */
337 SDL_Rect _wmRefusedOuter = { 0, 0, 0, 0 };
338 bool _resizeAnchorRight = false; /* anchor stale frame to the right edge (left-side resize) */
339 bool _resizeAnchorBottom = false; /* anchor stale frame to the bottom edge (top-side resize) */
340 /* Timeout deadline for anchored frames during resize completion. */
341 uint64_t _awaitingFrameUntil = 0;
342 /* Deadline for the server's post-restore rect; 0 when none is awaited. */
343 uint64_t _awaitRestoreUntil = 0;
344 /* A server minimize before this is stale. */
345 uint64_t _localRestoreUntil = 0;
346 bool _restoreRetried = false;
347 bool _restoreRetry = false;
348 /* Content origin offset inside current GFX surface. */
349 SDL_Point _gfxVisOrigin = { 0, 0 };
350 SDL_Rect _extentsApplied = { 0, 0, 0, 0 }; /* X11 extents sent to the WM */
351
352 /* Guard for state shared between RDP and main threads. */
353 mutable std::mutex _gfxLock;
354 std::vector<SDL_Rect> _visRects;
355 SDL_Point _visOffset = { 0, 0 }; /* server-absolute origin of _visRects */
356 bool _visOffsetSet = false; /* until VIS_OFFSET arrives, rects are window-relative */
357 bool _visDirty = false; /* rects/offset changed: wipe + full reclip (layered app) */
358 bool _deleted = false;
359 std::vector<uint8_t> _gfxBuffer; /* owned deep-copy of the gdi surface (avoids UAF) */
360 uint32_t _gfxStride = 0;
361 uint32_t _surfaceId = 0;
362 uint32_t _gfxW = 0;
363 uint32_t _gfxH = 0;
364 bool _hasGfx = false;
365 /* Popup surface has real per-pixel alpha (blend); all-zero undefined alpha stays opaque. */
366 bool _gfxHasAlpha = false;
367 /* Regions changed since last paint. Guarded by _gfxLock. */
368 std::vector<SDL_Rect> _gfxDamage;
369 /* Pixel dimensions at last presentation. */
370 int _lastWinW = 0;
371 int _lastWinH = 0;
372 int _lastGfxW = 0;
373 int _lastGfxH = 0;
374 bool _needsFullBlit = true;
375 SDL_Point _requestedRel = { 0, 0 };
376 Uint64 _requestedAt = 0;
377 std::optional<SDL_Point> _pendingRel; /* configured, shown with our next commit */
378 std::optional<SDL_Point> _placedRel; /* committed, input is mapped with it */
379 bool _placeInFlight = false;
380};