32#include <freerdp/freerdp.h>
37#include <sdl_common_utils.hpp>
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"
48#include "dialogs/sdl_connection_dialog_wrapper.hpp"
60 explicit SdlContext(rdpContext* context);
61 SdlContext(
const SdlContext& other) =
delete;
63 SdlContext(SdlContext&& other) =
delete;
65 SdlContext& operator=(
const SdlContext& other) =
delete;
66 SdlContext& operator=(SdlContext&& other) =
delete;
68 [[nodiscard]]
bool redraw(
bool suppress =
false)
const;
70 void setConnected(
bool val);
71 [[nodiscard]]
bool isConnected()
const;
72 [[nodiscard]]
bool abortConnect()
const;
75 [[nodiscard]]
bool resizeable()
const;
76 [[nodiscard]]
bool toggleResizeable();
77 [[nodiscard]]
bool setResizeable(
bool enable);
79 [[nodiscard]]
bool fullscreen()
const;
80 [[nodiscard]]
bool toggleFullscreen();
81 [[nodiscard]]
bool setFullscreen(
bool enter,
bool forceOriginalDisplay =
false);
83 [[nodiscard]]
bool setMinimized();
87 [[nodiscard]]
bool showOverlay(
bool on);
88 [[nodiscard]]
bool updateOverlay();
90 [[nodiscard]]
bool grabMouse()
const;
91 [[nodiscard]]
bool toggleGrabMouse();
92 [[nodiscard]]
bool setGrabMouse(
bool enter);
94 [[nodiscard]]
bool grabKeyboard()
const;
95 [[nodiscard]]
bool toggleGrabKeyboard();
96 [[nodiscard]]
bool setGrabKeyboard(
bool enter);
98 [[nodiscard]] rdpContext* context()
const;
99 [[nodiscard]] rdpClientContext* common()
const;
101 [[nodiscard]]
bool setCursor(CursorType type);
102 [[nodiscard]]
bool setCursor(
const rdpPointer* cursor);
103 [[nodiscard]] rdpPointer* cursor()
const;
104 [[nodiscard]]
bool restoreCursor();
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;
110 void push(std::vector<SDL_Rect>&& rects);
111 [[nodiscard]] std::vector<SDL_Rect> pop();
113 void setHasCursor(
bool val);
114 [[nodiscard]] std::optional<bool> allowScreenSaver()
const;
115 [[nodiscard]]
bool hasCursor()
const;
119 [[nodiscard]]
int start();
120 [[nodiscard]]
int join();
121 [[nodiscard]]
bool shallAbort(
bool ignoreDialogs =
false);
123 [[nodiscard]]
bool createWindows();
137 [[nodiscard]]
bool hasRenderers();
140 [[nodiscard]] Uint64 rendererAdd(UINT64 mappingId, IWTSBitmapRendererCallback* callback);
141 [[nodiscard]]
bool rendererFrame(Uint64
id,
const BYTE* data, UINT32 width, UINT32 height,
143 void rendererRemove(Uint64
id);
145 [[nodiscard]]
bool updateWindowList();
146 [[nodiscard]]
bool updateWindow(SDL_WindowID
id);
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;
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();
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;
171 [[nodiscard]] sdlDispContext& getDisplayChannelContext();
172 [[nodiscard]] sdlInput& getInputChannelContext();
173 [[nodiscard]] sdlClip& getClipboardChannelContext();
174 [[nodiscard]] SdlRail& getRailChannelContext();
176 [[nodiscard]] SdlConnectionDialogWrapper& getDialog();
178 [[nodiscard]] wLog* getWLog();
180 [[nodiscard]]
bool moveMouseTo(
const SDL_FPoint& pos);
187 [[nodiscard]]
bool screenToRdp(SDL_WindowID
id,
const SDL_FPoint& pos, SDL_FPoint& rpos,
188 SDL_FPoint* delta =
nullptr);
191 [[nodiscard]] SDL_Point inputOrigin()
const;
195 [[nodiscard]] std::vector<SdlInputWindow> inputWindows(SDL_WindowID
id,
const SDL_Point& origin,
196 std::optional<size_t>& source);
198 [[nodiscard]]
static SdlInputWindow inputWindowGeometry(SDL_Window* window,
199 SDL_Renderer* renderer);
201 [[nodiscard]] SDL_FPoint pixelToScreen(SDL_WindowID
id,
const SDL_FPoint& pos);
202 [[nodiscard]] SDL_FRect pixelToScreen(SDL_WindowID
id,
const SDL_FRect& pos,
205 [[nodiscard]]
bool handleEvent(
const SDL_Event& ev);
207 [[nodiscard]] COMMAND_LINE_ARGUMENT_A* args();
208 [[nodiscard]]
size_t argsCount()
const;
210 [[nodiscard]]
static int argumentHandler(
const COMMAND_LINE_ARGUMENT_A* arg,
void* custom);
212 [[nodiscard]] CriticalSection& lock();
214 [[nodiscard]] std::vector<rdpPointer*>& pointers();
215 [[nodiscard]]
bool contains(
const rdpPointer* ptr)
const;
217 [[nodiscard]]
bool credentialsRead()
const;
218 void setCredentialsRead();
221 [[nodiscard]] SdlWheelAxis& wheelAxis(
bool horizontal);
225 [[nodiscard]] SdlWindow* overlayWindow();
226 [[nodiscard]]
bool resizeToScale(SdlWindow* window);
227 [[nodiscard]]
bool useLocalScale()
const;
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);
241 [[nodiscard]] SDL_FPoint applyLocalScaling(
const SDL_FPoint& val)
const;
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);
250 void addOrUpdateDisplay(SDL_DisplayID
id);
251 void deleteDisplay(SDL_DisplayID
id);
253 [[nodiscard]]
bool createPrimary();
254 [[nodiscard]] std::string windowTitle()
const;
255 [[nodiscard]]
bool waitForWindowsCreated();
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);
261 [[nodiscard]]
int error_info_to_error(DWORD* pcode,
char** msg,
size_t* len)
const;
263 void updateDisplayOffsets();
264 void updateMonitorDataFromOffsets();
266 rdpContext* _context =
nullptr;
267 wLog* _log =
nullptr;
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;
277 bool _fullscreen =
false;
278 bool _resizeable =
false;
279 bool _grabMouse =
false;
280 bool _grabKeyboard =
false;
285 SDL_WindowID window = 0;
286 SDL_FPoint local = {};
287 SDL_FPoint server = {};
292 SDL_WindowID window = 0;
296 std::atomic<bool> _rdpThreadRunning =
false;
297 SDL_PixelFormat _sdlPixelFormat = SDL_PIXELFORMAT_UNKNOWN;
299 CriticalSection _critical;
301 using SDLSurfacePtr = std::unique_ptr<SDL_Surface,
decltype(&SDL_DestroySurface)>;
303 SDLSurfacePtr _primary;
304 SDL_FPoint _localScale{ 1.0f, 1.0f };
306 sdlDispContext _disp;
311 SdlConnectionDialogWrapper _dialog;
313 void updateNotification();
314 std::mutex _notificationLock;
315 std::string _notification;
317 SDL_WindowID _notificationWindow = 0;
318 std::string _notificationSavedTitle;
320 std::map<SDL_DisplayID, rdpMonitor> _displays;
321 std::map<SDL_WindowID, SdlWindow> _windows;
324 struct MappedRenderer
326 UINT64 mappingId = 0;
327 std::vector<Uint8> pixels;
332 IWTSBitmapRendererCallback* callback =
nullptr;
335 void renderersChanged();
336 std::mutex _rendererLock;
337 std::map<Uint64, MappedRenderer> _renderers;
338 Uint64 _nextRenderer = 1;
339 std::atomic<bool> _renderersPending =
false;
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;
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;
353 uint32_t _windowWidth = 0;
354 uint32_t _windowHeight = 0;
355 WinPREvent _windowsCreatedEvent;
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;
bool setNotification(const std::string &message)
void railWindowInfoChanged(uint64_t remoteId)