FreeRDP
Loading...
Searching...
No Matches
sdl_context.hpp
1
19#pragma once
20
21#include <map>
22#include <memory>
23#include <optional>
24#include <sstream>
25#include <vector>
26#include <set>
27#include <mutex>
28#include <queue>
29#include <thread>
30#include <atomic>
31
32#include <freerdp/freerdp.h>
34
35#include <SDL3/SDL.h>
36
37#include <sdl_common_utils.hpp>
38
39#include "sdl_input_mapping.hpp"
40#include "sdl_window.hpp"
41#include "sdl_disp.hpp"
42#include "sdl_clip.hpp"
43#include "sdl_input.hpp"
44#include "sdl_rail.hpp"
45#include "sdl_overlay.hpp"
46#include "sdl_wheel.hpp"
47
48#include "dialogs/sdl_connection_dialog_wrapper.hpp"
49
50class SdlContext
51{
52 public:
53 enum CursorType
54 {
55 CURSOR_NULL,
56 CURSOR_DEFAULT,
57 CURSOR_IMAGE
58 };
59
60 explicit SdlContext(rdpContext* context);
61 SdlContext(const SdlContext& other) = delete;
62 ~SdlContext();
63 SdlContext(SdlContext&& other) = delete;
64
65 SdlContext& operator=(const SdlContext& other) = delete;
66 SdlContext& operator=(SdlContext&& other) = delete;
67
68 [[nodiscard]] bool redraw(bool suppress = false) const;
69
70 void setConnected(bool val);
71 [[nodiscard]] bool isConnected() const;
72 [[nodiscard]] bool abortConnect() const;
73 void cleanup();
74
75 [[nodiscard]] bool resizeable() const;
76 [[nodiscard]] bool toggleResizeable();
77 [[nodiscard]] bool setResizeable(bool enable);
78
79 [[nodiscard]] bool fullscreen() const;
80 [[nodiscard]] bool toggleFullscreen();
81 [[nodiscard]] bool setFullscreen(bool enter, bool forceOriginalDisplay = false);
82
83 [[nodiscard]] bool setMinimized();
84
86 [[nodiscard]] bool toggleOverlay();
87 [[nodiscard]] bool showOverlay(bool on);
88 [[nodiscard]] bool updateOverlay();
89
90 [[nodiscard]] bool grabMouse() const;
91 [[nodiscard]] bool toggleGrabMouse();
92 [[nodiscard]] bool setGrabMouse(bool enter);
93
94 [[nodiscard]] bool grabKeyboard() const;
95 [[nodiscard]] bool toggleGrabKeyboard();
96 [[nodiscard]] bool setGrabKeyboard(bool enter);
97
98 [[nodiscard]] rdpContext* context() const;
99 [[nodiscard]] rdpClientContext* common() const;
100
101 [[nodiscard]] bool setCursor(CursorType type);
102 [[nodiscard]] bool setCursor(const rdpPointer* cursor);
103 [[nodiscard]] rdpPointer* cursor() const;
104 [[nodiscard]] bool restoreCursor();
105
106 void setMonitorIds(const std::vector<SDL_DisplayID>& ids);
107 [[nodiscard]] const std::vector<SDL_DisplayID>& monitorIds() const;
108 [[nodiscard]] int64_t monitorId(uint32_t index) const;
109
110 void push(std::vector<SDL_Rect>&& rects);
111 [[nodiscard]] std::vector<SDL_Rect> pop();
112
113 void setHasCursor(bool val);
114 [[nodiscard]] std::optional<bool> allowScreenSaver() const;
115 [[nodiscard]] bool hasCursor() const;
116
117 void setMetadata();
118
119 [[nodiscard]] int start();
120 [[nodiscard]] int join();
121 [[nodiscard]] bool shallAbort(bool ignoreDialogs = false);
122
123 [[nodiscard]] bool createWindows();
124
127 void updateWindowInfo();
130 void railWindowInfoChanged(uint64_t remoteId);
132 void flushWindowInfo();
133
136 [[nodiscard]] bool presentRenderers();
137 [[nodiscard]] bool hasRenderers();
138
139 /* rdpContext::bitmapRenderService's renderers (sdl_render_service.cpp), any thread */
140 [[nodiscard]] Uint64 rendererAdd(UINT64 mappingId, IWTSBitmapRendererCallback* callback);
141 [[nodiscard]] bool rendererFrame(Uint64 id, const BYTE* data, UINT32 width, UINT32 height,
142 LONG stride);
143 void rendererRemove(Uint64 id);
144
145 [[nodiscard]] bool updateWindowList();
146 [[nodiscard]] bool updateWindow(SDL_WindowID id);
147
148 [[nodiscard]] bool drawToWindows(const std::vector<SDL_Rect>& rects = {});
149 [[nodiscard]] bool drawToWindow(SdlWindow& window, const std::vector<SDL_Rect>& rects = {});
150 [[nodiscard]] bool minimizeAllWindows();
151 [[nodiscard]] int exitCode() const;
152 [[nodiscard]] SDL_PixelFormat pixelFormat() const;
153
154 [[nodiscard]] const SdlWindow* getWindowForId(SDL_WindowID id) const;
155 [[nodiscard]] SdlWindow* getWindowForId(SDL_WindowID id);
156 [[nodiscard]] bool allWindowsHidden() const;
157 [[nodiscard]] SdlWindow* getFirstWindow();
158
162 [[nodiscard]] bool setNotification(const std::string& message);
163
164 [[nodiscard]] bool addDisplayWindow(SDL_DisplayID id);
165 [[nodiscard]] bool removeDisplayWindow(SDL_DisplayID id);
166 [[nodiscard]] bool detectDisplays();
167 [[nodiscard]] rdpMonitor getDisplay(SDL_DisplayID id) const;
168 [[nodiscard]] std::vector<SDL_DisplayID> getDisplayIds() const;
169 void applyMonitorScaleOverride(rdpMonitor& monitor) const;
170
171 [[nodiscard]] sdlDispContext& getDisplayChannelContext();
172 [[nodiscard]] sdlInput& getInputChannelContext();
173 [[nodiscard]] sdlClip& getClipboardChannelContext();
174 [[nodiscard]] SdlRail& getRailChannelContext();
175
176 [[nodiscard]] SdlConnectionDialogWrapper& getDialog();
177
178 [[nodiscard]] wLog* getWLog();
179
180 [[nodiscard]] bool moveMouseTo(const SDL_FPoint& pos);
181
182 /* Map a position relative to window id, in points, to mouse input coordinates, the same way
183 * for desktop and RemoteApp windows; see sdl_window_to_input. During capture the window
184 * under the position is used, while the event keeps its window ID for input focus and
185 * capture ownership. delta, if given, is a relative motion, mapped with window id's scale.
186 * Returns false if id is not a session window. */
187 [[nodiscard]] bool screenToRdp(SDL_WindowID id, const SDL_FPoint& pos, SDL_FPoint& rpos,
188 SDL_FPoint* delta = nullptr);
189
190 /* Mouse input origin: top left of the bounding box of the monitor layout sent to the server. */
191 [[nodiscard]] SDL_Point inputOrigin() const;
192 /* The session's windows for pointer mapping: desktop and RemoteApp windows alike. source is
193 * set to the index of window id, or left empty if id is not a session window. Hidden windows
194 * other than id are left out. */
195 [[nodiscard]] std::vector<SdlInputWindow> inputWindows(SDL_WindowID id, const SDL_Point& origin,
196 std::optional<size_t>& source);
197 /* Local bounds and pixel density of an SDL window, for pointer mapping. */
198 [[nodiscard]] static SdlInputWindow inputWindowGeometry(SDL_Window* window,
199 SDL_Renderer* renderer);
200
201 [[nodiscard]] SDL_FPoint pixelToScreen(SDL_WindowID id, const SDL_FPoint& pos);
202 [[nodiscard]] SDL_FRect pixelToScreen(SDL_WindowID id, const SDL_FRect& pos,
203 bool round = false);
204
205 [[nodiscard]] bool handleEvent(const SDL_Event& ev);
206
207 [[nodiscard]] COMMAND_LINE_ARGUMENT_A* args();
208 [[nodiscard]] size_t argsCount() const;
209
210 [[nodiscard]] static int argumentHandler(const COMMAND_LINE_ARGUMENT_A* arg, void* custom);
211
212 [[nodiscard]] CriticalSection& lock();
213
214 [[nodiscard]] std::vector<rdpPointer*>& pointers();
215 [[nodiscard]] bool contains(const rdpPointer* ptr) const;
216
217 [[nodiscard]] bool credentialsRead() const;
218 void setCredentialsRead();
219
220 /* the wheel rotation not yet sent, one axis each */
221 [[nodiscard]] SdlWheelAxis& wheelAxis(bool horizontal);
222
223 private:
225 [[nodiscard]] SdlWindow* overlayWindow();
226 [[nodiscard]] bool resizeToScale(SdlWindow* window);
227 [[nodiscard]] bool useLocalScale() const;
228
229 [[nodiscard]] static BOOL preConnect(freerdp* instance);
230 [[nodiscard]] static BOOL postConnect(freerdp* instance);
231 static void postDisconnect(freerdp* instance);
232 static void postFinalDisconnect(freerdp* instance);
233 [[nodiscard]] static BOOL getAccessToken(freerdp* instance, AccessTokenType tokenType,
234 char** token, size_t count, ...);
235 [[nodiscard]] static BOOL desktopResize(rdpContext* context);
236 [[nodiscard]] static BOOL playSound(rdpContext* context, const PLAY_SOUND_UPDATE* play_sound);
237 [[nodiscard]] static BOOL beginPaint(rdpContext* context);
238 [[nodiscard]] static BOOL endPaint(rdpContext* context);
239 [[nodiscard]] static DWORD WINAPI rdpThreadRun(SdlContext* sdl);
240
241 [[nodiscard]] SDL_FPoint applyLocalScaling(const SDL_FPoint& val) const;
242
243 [[nodiscard]] bool handleEvent(const SDL_WindowEvent& ev);
244 [[nodiscard]] bool handleEvent(const SDL_DisplayEvent& ev);
245 [[nodiscard]] bool handleEvent(const SDL_MouseButtonEvent& ev);
246 [[nodiscard]] bool handleEvent(const SDL_MouseMotionEvent& ev);
247 [[nodiscard]] bool handleEvent(const SDL_MouseWheelEvent& ev);
248 [[nodiscard]] bool handleEvent(const SDL_TouchFingerEvent& ev);
249
250 void addOrUpdateDisplay(SDL_DisplayID id);
251 void deleteDisplay(SDL_DisplayID id);
252
253 [[nodiscard]] bool createPrimary();
254 [[nodiscard]] std::string windowTitle() const;
255 [[nodiscard]] bool waitForWindowsCreated();
256
257 void sdl_client_cleanup(int exit_code, const std::string& error_msg);
258 [[nodiscard]] int sdl_client_thread_connect(std::string& error_msg);
259 [[nodiscard]] int sdl_client_thread_run(std::string& error_msg);
260
261 [[nodiscard]] int error_info_to_error(DWORD* pcode, char** msg, size_t* len) const;
262
263 void updateDisplayOffsets();
264 void updateMonitorDataFromOffsets();
265
266 rdpContext* _context = nullptr;
267 wLog* _log = nullptr;
268
269 std::atomic<bool> _connected = false;
270 bool _cursor_visible = true;
271 std::unique_ptr<rdpPointer, void (*)(rdpPointer*)> _cursor;
272 CursorType _cursorType = CURSOR_NULL;
273 std::vector<SDL_DisplayID> _monitorIds;
274 std::mutex _queue_mux;
275 std::queue<std::vector<SDL_Rect>> _queue;
276 /* SDL */
277 bool _fullscreen = false;
278 bool _resizeable = false;
279 bool _grabMouse = false;
280 bool _grabKeyboard = false;
281
282 /* RemoteApp pointer state */
283 struct
284 {
285 SDL_WindowID window = 0;
286 SDL_FPoint local = {};
287 SDL_FPoint server = {};
288 uint32_t epoch = 0;
289 } _lastMotion; /* last motion forwarded to the server */
290 struct
291 {
292 SDL_WindowID window = 0;
293 Uint64 time = 0;
294 } _lastRelease;
295 int _exitCode = -1;
296 std::atomic<bool> _rdpThreadRunning = false;
297 SDL_PixelFormat _sdlPixelFormat = SDL_PIXELFORMAT_UNKNOWN;
298
299 CriticalSection _critical;
300
301 using SDLSurfacePtr = std::unique_ptr<SDL_Surface, decltype(&SDL_DestroySurface)>;
302
303 SDLSurfacePtr _primary;
304 SDL_FPoint _localScale{ 1.0f, 1.0f };
305
306 sdlDispContext _disp;
307 sdlInput _input;
308 sdlClip _clip;
309 SdlRail _rail;
310
311 SdlConnectionDialogWrapper _dialog;
312
313 void updateNotification();
314 std::mutex _notificationLock;
315 std::string _notification; /* under _notificationLock */
316 /* main thread only: the window showing the notification, and its title before that */
317 SDL_WindowID _notificationWindow = 0;
318 std::string _notificationSavedTitle;
319
320 std::map<SDL_DisplayID, rdpMonitor> _displays;
321 std::map<SDL_WindowID, SdlWindow> _windows;
322
323 /* the mapped renderers: what they were last given, under _rendererLock */
324 struct MappedRenderer
325 {
326 UINT64 mappingId = 0; /* MS-RDPEGT */
327 std::vector<Uint8> pixels;
328 int pitch = 0;
329 int width = 0;
330 int height = 0;
331 bool dirty = false;
332 IWTSBitmapRendererCallback* callback = nullptr; /* a reference */
333 SDL_Rect target{}; /* last OnTargetSizeChanged */
334 };
335 void renderersChanged();
336 std::mutex _rendererLock;
337 std::map<Uint64, MappedRenderer> _renderers;
338 Uint64 _nextRenderer = 1;
339 std::atomic<bool> _renderersPending = false;
340 /* rdpContext::windowInfoService: answered from these snapshots (channel threads), which
341 * the main thread refreshes. _windowInfoLock is a leaf lock. */
342 static HRESULT windowInfoGet(void* custom, UINT64 remoteHwnd, WTS_WINDOW_INFO* info);
343 static UINT32 windowInfoSessionType(void* custom);
344 std::mutex _windowInfoLock;
345 WTS_WINDOW_INFO _desktopWindowInfo{};
346 std::map<uint64_t, WTS_WINDOW_INFO> _railWindowInfo;
347 std::set<uint64_t> _railWindowInfoPending;
348
349 std::map<SDL_DisplayID, std::pair<SDL_Rect, SDL_Rect>> _offsets;
350 std::map<SDL_DisplayID, SDL_Rect> _displayBounds;
351 SDL_DisplayID _primaryDisplay = 0;
352
353 uint32_t _windowWidth = 0;
354 uint32_t _windowHeight = 0;
355 WinPREvent _windowsCreatedEvent;
356 std::thread _thread;
357 std::vector<COMMAND_LINE_ARGUMENT_A> _args;
358 std::optional<bool> _connectionDialog;
359 std::optional<bool> _allowScreenSaver;
360 SdlOverlay _overlay{ this };
361 std::vector<rdpPointer*> _valid_pointers;
362 bool _credentialsRead = false;
363 SdlWheelAxis _wheelX;
364 SdlWheelAxis _wheelY;
365};
void flushWindowInfo()
bool setNotification(const std::string &message)
void railWindowInfoChanged(uint64_t remoteId)
void updateWindowInfo()
bool presentRenderers()
bool toggleOverlay()