FreeRDP
Loading...
Searching...
No Matches
sdl_context.hpp
1
19#pragma once
20
21#include <map>
22#include <memory>
23#include <sstream>
24#include <vector>
25#include <mutex>
26#include <queue>
27#include <thread>
28#include <atomic>
29
30#include <freerdp/freerdp.h>
31
32#include <SDL3/SDL.h>
33
34#include <sdl_common_utils.hpp>
35
36#include "sdl_window.hpp"
37#include "sdl_disp.hpp"
38#include "sdl_clip.hpp"
39#include "sdl_input.hpp"
40
41#include "dialogs/sdl_connection_dialog_wrapper.hpp"
42
43class SdlContext
44{
45 public:
46 enum CursorType
47 {
48 CURSOR_NULL,
49 CURSOR_DEFAULT,
50 CURSOR_IMAGE
51 };
52
53 explicit SdlContext(rdpContext* context);
54 SdlContext(const SdlContext& other) = delete;
55 SdlContext(SdlContext&& other) = delete;
56 ~SdlContext() = default;
57
58 SdlContext& operator=(const SdlContext& other) = delete;
59 SdlContext& operator=(SdlContext&& other) = delete;
60
61 [[nodiscard]] bool redraw(bool suppress = false) const;
62
63 void setConnected(bool val);
64 [[nodiscard]] bool isConnected() const;
65 void cleanup();
66
67 [[nodiscard]] bool resizeable() const;
68 [[nodiscard]] bool toggleResizeable();
69 [[nodiscard]] bool setResizeable(bool enable);
70
71 [[nodiscard]] bool fullscreen() const;
72 [[nodiscard]] bool toggleFullscreen();
73 [[nodiscard]] bool setFullscreen(bool enter);
74
75 [[nodiscard]] bool setMinimized();
76
77 [[nodiscard]] bool grabMouse() const;
78 [[nodiscard]] bool toggleGrabMouse();
79 [[nodiscard]] bool setGrabMouse(bool enter);
80
81 [[nodiscard]] bool grabKeyboard() const;
82 [[nodiscard]] bool toggleGrabKeyboard();
83 [[nodiscard]] bool setGrabKeyboard(bool enter);
84
85 [[nodiscard]] rdpContext* context() const;
86 [[nodiscard]] rdpClientContext* common() const;
87
88 [[nodiscard]] bool setCursor(CursorType type);
89 [[nodiscard]] bool setCursor(rdpPointer* cursor);
90 [[nodiscard]] rdpPointer* cursor() const;
91 [[nodiscard]] bool restoreCursor();
92
93 void setMonitorIds(const std::vector<SDL_DisplayID>& ids);
94 [[nodiscard]] const std::vector<SDL_DisplayID>& monitorIds() const;
95 [[nodiscard]] int64_t monitorId(uint32_t index) const;
96
97 void push(std::vector<SDL_Rect>&& rects);
98 [[nodiscard]] std::vector<SDL_Rect> pop();
99
100 void setHasCursor(bool val);
101 [[nodiscard]] bool hasCursor() const;
102
103 void setMetadata();
104
105 [[nodiscard]] int start();
106 [[nodiscard]] int join();
107 [[nodiscard]] bool shallAbort(bool ignoreDialogs = false);
108
109 [[nodiscard]] bool createWindows();
110 [[nodiscard]] bool updateWindowList();
111
112 [[nodiscard]] bool drawToWindows(const std::vector<SDL_Rect>& rects = {});
113 [[nodiscard]] bool drawToWindow(SdlWindow& window, const std::vector<SDL_Rect>& rects = {});
114 [[nodiscard]] bool minimizeAllWindows();
115 [[nodiscard]] int exitCode() const;
116 [[nodiscard]] SDL_PixelFormat pixelFormat() const;
117
118 [[nodiscard]] const SdlWindow* getWindowForId(SDL_WindowID id) const;
119 [[nodiscard]] SdlWindow* getWindowForId(SDL_WindowID id);
120 [[nodiscard]] SdlWindow* getFirstWindow();
121
122 [[nodiscard]] bool addDisplayWindow(SDL_DisplayID id);
123 [[nodiscard]] bool removeDisplay(SDL_DisplayID id);
124
125 [[nodiscard]] sdlDispContext& getDisplayChannelContext();
126 [[nodiscard]] sdlInput& getInputChannelContext();
127 [[nodiscard]] sdlClip& getClipboardChannelContext();
128
129 [[nodiscard]] SdlConnectionDialogWrapper& getDialog();
130
131 [[nodiscard]] wLog* getWLog();
132
133 [[nodiscard]] bool moveMouseTo(const SDL_FPoint& pos);
134
135 [[nodiscard]] SDL_FPoint screenToPixel(SDL_WindowID id, const SDL_FPoint& pos);
136
137 [[nodiscard]] SDL_FPoint pixelToScreen(SDL_WindowID id, const SDL_FPoint& pos);
138 [[nodiscard]] SDL_FRect pixelToScreen(SDL_WindowID id, const SDL_FRect& pos);
139
140 [[nodiscard]] bool handleEvent(const SDL_Event& ev);
141
142 private:
143 [[nodiscard]] static BOOL preConnect(freerdp* instance);
144 [[nodiscard]] static BOOL postConnect(freerdp* instance);
145 static void postDisconnect(freerdp* instance);
146 static void postFinalDisconnect(freerdp* instance);
147 [[nodiscard]] static BOOL desktopResize(rdpContext* context);
148 [[nodiscard]] static BOOL playSound(rdpContext* context, const PLAY_SOUND_UPDATE* play_sound);
149 [[nodiscard]] static BOOL beginPaint(rdpContext* context);
150 [[nodiscard]] static BOOL endPaint(rdpContext* context);
151 [[nodiscard]] static DWORD WINAPI rdpThreadRun(SdlContext* sdl);
152
153 [[nodiscard]] bool eventToPixelCoordinates(SDL_WindowID id, SDL_Event& ev);
154
155 [[nodiscard]] SDL_FPoint applyLocalScaling(const SDL_FPoint& val) const;
156 void removeLocalScaling(float& x, float& y) const;
157
158 [[nodiscard]] bool handleEvent(const SDL_WindowEvent& ev);
159 [[nodiscard]] bool handleEvent(const SDL_DisplayEvent& ev);
160 [[nodiscard]] bool handleEvent(const SDL_MouseButtonEvent& ev);
161 [[nodiscard]] bool handleEvent(const SDL_MouseMotionEvent& ev);
162 [[nodiscard]] bool handleEvent(const SDL_MouseWheelEvent& ev);
163 [[nodiscard]] bool handleEvent(const SDL_TouchFingerEvent& ev);
164
165 [[nodiscard]] bool createPrimary();
166 [[nodiscard]] std::string windowTitle() const;
167 [[nodiscard]] bool waitForWindowsCreated();
168
169 void sdl_client_cleanup(int exit_code, const std::string& error_msg);
170 [[nodiscard]] int sdl_client_thread_connect(std::string& error_msg);
171 [[nodiscard]] int sdl_client_thread_run(std::string& error_msg);
172
173 [[nodiscard]] int error_info_to_error(DWORD* pcode, char** msg, size_t* len) const;
174
175 void applyMonitorOffset(SDL_WindowID window, float& x, float& y) const;
176
177 rdpContext* _context = nullptr;
178 wLog* _log = nullptr;
179
180 std::atomic<bool> _connected = false;
181 bool _cursor_visible = true;
182 rdpPointer* _cursor = nullptr;
183 CursorType _cursorType = CURSOR_NULL;
184 std::vector<SDL_DisplayID> _monitorIds;
185 std::mutex _queue_mux;
186 std::queue<std::vector<SDL_Rect>> _queue;
187 /* SDL */
188 bool _fullscreen = false;
189 bool _resizeable = false;
190 bool _grabMouse = false;
191 bool _grabKeyboard = false;
192 int _exitCode = -1;
193 std::atomic<bool> _rdpThreadRunning = false;
194 SDL_PixelFormat _sdlPixelFormat = SDL_PIXELFORMAT_UNKNOWN;
195
196 CriticalSection _critical;
197
198 using SDLSurfacePtr = std::unique_ptr<SDL_Surface, decltype(&SDL_DestroySurface)>;
199
200 SDLSurfacePtr _primary;
201 SDL_FPoint _localScale{ 1.0f, 1.0f };
202
203 sdlDispContext _disp;
204 sdlInput _input;
205 sdlClip _clip;
206
208
209 std::map<Uint32, SdlWindow> _windows;
210
211 WinPREvent _windowsCreatedEvent;
212 std::thread _thread;
213};
object that handles clipboard context for the SDL3 client
Definition sdl_clip.hpp:76