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_window.hpp"
40#include "sdl_disp.hpp"
41#include "sdl_clip.hpp"
42#include "sdl_input.hpp"
43#include "sdl_rail.hpp"
44#include "sdl_overlay.hpp"
45#include "sdl_wheel.hpp"
46
47#include "dialogs/sdl_connection_dialog_wrapper.hpp"
48
49class SdlContext
50{
51 public:
52 enum CursorType
53 {
54 CURSOR_NULL,
55 CURSOR_DEFAULT,
56 CURSOR_IMAGE
57 };
58
59 explicit SdlContext(rdpContext* context);
60 SdlContext(const SdlContext& other) = delete;
61 ~SdlContext();
62 SdlContext(SdlContext&& other) = delete;
63
64 SdlContext& operator=(const SdlContext& other) = delete;
65 SdlContext& operator=(SdlContext&& other) = delete;
66
67 [[nodiscard]] bool redraw(bool suppress = false) const;
68
69 void setConnected(bool val);
70 [[nodiscard]] bool isConnected() const;
71 [[nodiscard]] bool abortConnect() const;
72 void cleanup();
73
74 [[nodiscard]] bool resizeable() const;
75 [[nodiscard]] bool toggleResizeable();
76 [[nodiscard]] bool setResizeable(bool enable);
77
78 [[nodiscard]] bool fullscreen() const;
79 [[nodiscard]] bool toggleFullscreen();
80 [[nodiscard]] bool setFullscreen(bool enter, bool forceOriginalDisplay = false);
81
82 [[nodiscard]] bool setMinimized();
83
85 [[nodiscard]] bool toggleOverlay();
86 [[nodiscard]] bool showOverlay(bool on);
87 [[nodiscard]] bool updateOverlay();
88
89 [[nodiscard]] bool grabMouse() const;
90 [[nodiscard]] bool toggleGrabMouse();
91 [[nodiscard]] bool setGrabMouse(bool enter);
92
93 [[nodiscard]] bool grabKeyboard() const;
94 [[nodiscard]] bool toggleGrabKeyboard();
95 [[nodiscard]] bool setGrabKeyboard(bool enter);
96
97 [[nodiscard]] rdpContext* context() const;
98 [[nodiscard]] rdpClientContext* common() const;
99
100 [[nodiscard]] bool setCursor(CursorType type);
101 [[nodiscard]] bool setCursor(const rdpPointer* cursor);
102 [[nodiscard]] rdpPointer* cursor() const;
103 [[nodiscard]] bool restoreCursor();
104
105 void setMonitorIds(const std::vector<SDL_DisplayID>& ids);
106 [[nodiscard]] const std::vector<SDL_DisplayID>& monitorIds() const;
107 [[nodiscard]] int64_t monitorId(uint32_t index) const;
108
109 void push(std::vector<SDL_Rect>&& rects);
110 [[nodiscard]] std::vector<SDL_Rect> pop();
111
112 void setHasCursor(bool val);
113 [[nodiscard]] std::optional<bool> allowScreenSaver() const;
114 [[nodiscard]] bool hasCursor() const;
115
116 void setMetadata();
117
118 [[nodiscard]] int start();
119 [[nodiscard]] int join();
120 [[nodiscard]] bool shallAbort(bool ignoreDialogs = false);
121
122 [[nodiscard]] bool createWindows();
123
126 void updateWindowInfo();
129 void railWindowInfoChanged(uint64_t remoteId);
131 void flushWindowInfo();
132
135 [[nodiscard]] bool presentRenderers();
136 [[nodiscard]] bool hasRenderers();
137
138 /* rdpContext::bitmapRenderService's renderers (sdl_render_service.cpp), any thread */
139 [[nodiscard]] Uint64 rendererAdd(UINT64 mappingId, IWTSBitmapRendererCallback* callback);
140 [[nodiscard]] bool rendererFrame(Uint64 id, const BYTE* data, UINT32 width, UINT32 height,
141 LONG stride);
142 void rendererRemove(Uint64 id);
143
144 [[nodiscard]] bool updateWindowList();
145 [[nodiscard]] bool updateWindow(SDL_WindowID id);
146
147 [[nodiscard]] bool drawToWindows(const std::vector<SDL_Rect>& rects = {});
148 [[nodiscard]] bool drawToWindow(SdlWindow& window, const std::vector<SDL_Rect>& rects = {});
149 [[nodiscard]] bool minimizeAllWindows();
150 [[nodiscard]] int exitCode() const;
151 [[nodiscard]] SDL_PixelFormat pixelFormat() const;
152
153 [[nodiscard]] const SdlWindow* getWindowForId(SDL_WindowID id) const;
154 [[nodiscard]] SdlWindow* getWindowForId(SDL_WindowID id);
155 [[nodiscard]] bool allWindowsHidden() const;
156 [[nodiscard]] SdlWindow* getFirstWindow();
157
158 [[nodiscard]] bool addDisplayWindow(SDL_DisplayID id);
159 [[nodiscard]] bool removeDisplayWindow(SDL_DisplayID id);
160 [[nodiscard]] bool detectDisplays();
161 [[nodiscard]] rdpMonitor getDisplay(SDL_DisplayID id) const;
162 [[nodiscard]] std::vector<SDL_DisplayID> getDisplayIds() const;
163
164 [[nodiscard]] sdlDispContext& getDisplayChannelContext();
165 [[nodiscard]] sdlInput& getInputChannelContext();
166 [[nodiscard]] sdlClip& getClipboardChannelContext();
167 [[nodiscard]] SdlRail& getRailChannelContext();
168
169 [[nodiscard]] SdlConnectionDialogWrapper& getDialog();
170
171 [[nodiscard]] wLog* getWLog();
172
173 [[nodiscard]] bool moveMouseTo(const SDL_FPoint& pos);
174
175 [[nodiscard]] SDL_FPoint screenToPixel(SDL_WindowID id, const SDL_FPoint& pos);
176
177 [[nodiscard]] SDL_FPoint pixelToScreen(SDL_WindowID id, const SDL_FPoint& pos);
178 [[nodiscard]] SDL_FRect pixelToScreen(SDL_WindowID id, const SDL_FRect& pos,
179 bool round = false);
180
181 [[nodiscard]] bool handleEvent(const SDL_Event& ev);
182
183 [[nodiscard]] COMMAND_LINE_ARGUMENT_A* args();
184 [[nodiscard]] size_t argsCount() const;
185
186 [[nodiscard]] static int argumentHandler(const COMMAND_LINE_ARGUMENT_A* arg, void* custom);
187
188 [[nodiscard]] CriticalSection& lock();
189
190 [[nodiscard]] std::vector<rdpPointer*>& pointers();
191 [[nodiscard]] bool contains(const rdpPointer* ptr) const;
192
193 [[nodiscard]] bool credentialsRead() const;
194 void setCredentialsRead();
195
196 /* the wheel rotation not yet sent, one axis each */
197 [[nodiscard]] SdlWheelAxis& wheelAxis(bool horizontal);
198
199 private:
201 [[nodiscard]] SdlWindow* overlayWindow();
202 [[nodiscard]] bool resizeToScale(SdlWindow* window);
203 [[nodiscard]] bool useLocalScale() const;
204
205 [[nodiscard]] static BOOL preConnect(freerdp* instance);
206 [[nodiscard]] static BOOL postConnect(freerdp* instance);
207 static void postDisconnect(freerdp* instance);
208 static void postFinalDisconnect(freerdp* instance);
209 [[nodiscard]] static BOOL getAccessToken(freerdp* instance, AccessTokenType tokenType,
210 char** token, size_t count, ...);
211 [[nodiscard]] static BOOL desktopResize(rdpContext* context);
212 [[nodiscard]] static BOOL playSound(rdpContext* context, const PLAY_SOUND_UPDATE* play_sound);
213 [[nodiscard]] static BOOL beginPaint(rdpContext* context);
214 [[nodiscard]] static BOOL endPaint(rdpContext* context);
215 [[nodiscard]] static DWORD WINAPI rdpThreadRun(SdlContext* sdl);
216
217 [[nodiscard]] bool eventToPixelCoordinates(SDL_WindowID id, SDL_Event& ev);
218
219 [[nodiscard]] SDL_FPoint applyLocalScaling(const SDL_FPoint& val) const;
220 void removeLocalScaling(float& x, float& y) const;
221
222 [[nodiscard]] bool handleEvent(const SDL_WindowEvent& ev);
223 [[nodiscard]] bool handleEvent(const SDL_DisplayEvent& ev);
224 [[nodiscard]] bool handleEvent(const SDL_MouseButtonEvent& ev);
225 [[nodiscard]] bool handleEvent(const SDL_MouseMotionEvent& ev);
226 [[nodiscard]] bool handleEvent(const SDL_MouseWheelEvent& ev);
227 [[nodiscard]] bool handleEvent(const SDL_TouchFingerEvent& ev);
228
229 void addOrUpdateDisplay(SDL_DisplayID id);
230 void deleteDisplay(SDL_DisplayID id);
231
232 [[nodiscard]] bool createPrimary();
233 [[nodiscard]] std::string windowTitle() const;
234 [[nodiscard]] bool waitForWindowsCreated();
235
236 void sdl_client_cleanup(int exit_code, const std::string& error_msg);
237 [[nodiscard]] int sdl_client_thread_connect(std::string& error_msg);
238 [[nodiscard]] int sdl_client_thread_run(std::string& error_msg);
239
240 [[nodiscard]] int error_info_to_error(DWORD* pcode, char** msg, size_t* len) const;
241
242 void applyMonitorOffset(SDL_WindowID window, float& x, float& y) const;
243
244 void updateDisplayOffsets();
245 void updateMonitorDataFromOffsets();
246
247 rdpContext* _context = nullptr;
248 wLog* _log = nullptr;
249
250 std::atomic<bool> _connected = false;
251 bool _cursor_visible = true;
252 std::unique_ptr<rdpPointer, void (*)(rdpPointer*)> _cursor;
253 CursorType _cursorType = CURSOR_NULL;
254 std::vector<SDL_DisplayID> _monitorIds;
255 std::mutex _queue_mux;
256 std::queue<std::vector<SDL_Rect>> _queue;
257 /* SDL */
258 bool _fullscreen = false;
259 bool _resizeable = false;
260 bool _grabMouse = false;
261 bool _grabKeyboard = false;
262
263 /* RemoteApp pointer state */
264 struct
265 {
266 SDL_WindowID window = 0;
267 SDL_FPoint local = {};
268 SDL_FPoint server = {};
269 uint32_t epoch = 0;
270 } _lastMotion; /* last motion forwarded to the server */
271 struct
272 {
273 SDL_WindowID window = 0;
274 Uint64 time = 0;
275 } _lastRelease;
276 int _exitCode = -1;
277 std::atomic<bool> _rdpThreadRunning = false;
278 SDL_PixelFormat _sdlPixelFormat = SDL_PIXELFORMAT_UNKNOWN;
279
280 CriticalSection _critical;
281
282 using SDLSurfacePtr = std::unique_ptr<SDL_Surface, decltype(&SDL_DestroySurface)>;
283
284 SDLSurfacePtr _primary;
285 SDL_FPoint _localScale{ 1.0f, 1.0f };
286
287 sdlDispContext _disp;
288 sdlInput _input;
289 sdlClip _clip;
290 SdlRail _rail;
291
292 SdlConnectionDialogWrapper _dialog;
293
294 std::map<SDL_DisplayID, rdpMonitor> _displays;
295 std::map<SDL_WindowID, SdlWindow> _windows;
296
297 /* the mapped renderers: what they were last given, under _rendererLock */
298 struct MappedRenderer
299 {
300 UINT64 mappingId = 0; /* MS-RDPEGT */
301 std::vector<Uint8> pixels;
302 int pitch = 0;
303 int width = 0;
304 int height = 0;
305 bool dirty = false;
306 IWTSBitmapRendererCallback* callback = nullptr; /* a reference */
307 SDL_Rect target{}; /* last OnTargetSizeChanged */
308 };
309 void renderersChanged();
310 std::mutex _rendererLock;
311 std::map<Uint64, MappedRenderer> _renderers;
312 Uint64 _nextRenderer = 1;
313 std::atomic<bool> _renderersPending = false;
314 /* rdpContext::windowInfoService: answered from these snapshots (channel threads), which
315 * the main thread refreshes. _windowInfoLock is a leaf lock. */
316 static HRESULT windowInfoGet(void* custom, UINT64 remoteHwnd, WTS_WINDOW_INFO* info);
317 static UINT32 windowInfoSessionType(void* custom);
318 std::mutex _windowInfoLock;
319 WTS_WINDOW_INFO _desktopWindowInfo{};
320 std::map<uint64_t, WTS_WINDOW_INFO> _railWindowInfo;
321 std::set<uint64_t> _railWindowInfoPending;
322
323 std::map<SDL_DisplayID, std::pair<SDL_Rect, SDL_Rect>> _offsets;
324 std::map<SDL_DisplayID, SDL_Rect> _displayBounds;
325 SDL_DisplayID _primaryDisplay = 0;
326
327 uint32_t _windowWidth = 0;
328 uint32_t _windowHeight = 0;
329 WinPREvent _windowsCreatedEvent;
330 std::thread _thread;
331 std::vector<COMMAND_LINE_ARGUMENT_A> _args;
332 std::optional<bool> _connectionDialog;
333 std::optional<bool> _allowScreenSaver;
334 SdlOverlay _overlay{ this };
335 std::vector<rdpPointer*> _valid_pointers;
336 bool _credentialsRead = false;
337 SdlWheelAxis _wheelX;
338 SdlWheelAxis _wheelY;
339};
void flushWindowInfo()
void railWindowInfoChanged(uint64_t remoteId)
void updateWindowInfo()
bool presentRenderers()
bool toggleOverlay()