FreeRDP
Loading...
Searching...
No Matches
sdl_context.cpp
1
19
20#include <algorithm>
21#include <array>
22#include <cmath>
23#include <cfloat>
24
25#include "sdl_context.hpp"
26#include "sdl_config.hpp"
27#include "sdl_channels.hpp"
28#include "sdl_monitor.hpp"
29#include "sdl_display_offsets.hpp"
30#include "sdl_pointer.hpp"
31#include "sdl_touch.hpp"
32#include "sdl_prefs.hpp"
33
34#include <sdl_common_utils.hpp>
35#include <scoped_guard.hpp>
36
37#include "dialogs/sdl_dialogs.hpp"
38#include "sdl_render_service.hpp"
39
40#include <freerdp/client/aad_helper.h>
41#include <freerdp/client/geometry.h>
42#include <freerdp/gdi/gdi.h>
43
44static constexpr auto sdl_allow_screensaver = "sdl-allow-screensaver";
45static constexpr auto sdl_connection_dialog = "sdl-connection-dialog";
46static constexpr auto sdl_info = "sdl-info";
47
48SdlContext::SdlContext(rdpContext* context)
49 : _context(context), _log(WLog_Get(CLIENT_TAG("SDL"))), _cursor(nullptr, sdl_Pointer_FreeCopy),
50 _rdpThreadRunning(false), _primary(nullptr, SDL_DestroySurface), _disp(this), _input(this),
51 _clip(this), _rail(this), _dialog(_log)
52{
53 WINPR_ASSERT(context);
54 setMetadata();
55
56 auto instance = _context->instance;
57 WINPR_ASSERT(instance);
58
59 instance->PreConnect = preConnect;
60 instance->PostConnect = postConnect;
61 instance->PostDisconnect = postDisconnect;
62 instance->PostFinalDisconnect = postFinalDisconnect;
63 instance->AuthenticateEx = sdl_authenticate_ex;
64 instance->VerifyCertificateEx = sdl_verify_certificate_ex;
65 instance->VerifyChangedCertificateEx = sdl_verify_changed_certificate_ex;
66 instance->LogonErrorInfo = sdl_logon_error_info;
67 instance->PresentGatewayMessage = sdl_present_gateway_message;
68 instance->ChooseSmartcard = sdl_choose_smartcard;
69 instance->RetryDialog = sdl_retry_dialog;
70 instance->GetAccessToken = client_failsafe_get_access_token;
71
72 /* TODO: Client display set up */
73
74 _args.push_back({ sdl_allow_screensaver, COMMAND_LINE_VALUE_BOOL, nullptr, BoolValueFalse,
75 nullptr, -1, nullptr,
76 "Allow local screensaver to activate (default: on for RemoteApp)" });
77 /* BoolValueTrue does not compile in C++, any non null default means on */
78 _args.push_back({ sdl_connection_dialog, COMMAND_LINE_VALUE_BOOL, nullptr, "on", nullptr, -1,
79 nullptr, "the connection dialog with progress and errors" });
80 _args.push_back({ sdl_info, COMMAND_LINE_VALUE_BOOL, nullptr, BoolValueFalse, nullptr, -1,
81 nullptr,
82 "the stats and info overlay from the start (its hotkey SDL_Info, default "
83 "<modifier>+I, shows and hides it)" });
84
85 /* Push a null element used as abort when iterating the array */
86 _args.push_back({ nullptr, 0, nullptr, nullptr, nullptr, -1, nullptr, nullptr });
87
88 const WINDOW_INFO_SOURCE source = { this, windowInfoGet, windowInfoSessionType };
89 _context->windowInfoService = freerdp_client_window_info_service_new(&source);
90 if (!_context->windowInfoService)
91 WLog_Print(_log, WLOG_WARN, "no window info service: channels won't know our windows");
92 _context->bitmapRenderService = sdl_render_service_new(this);
93 if (!_context->bitmapRenderService)
94 WLog_Print(_log, WLOG_WARN, "no bitmap render service: no redirected video");
95}
96
97SdlContext::~SdlContext()
98{
99 /* the channels, freed after us, may still hold the services */
100 if (_context->windowInfoService)
101 {
102 freerdp_client_window_info_service_detach(_context->windowInfoService);
103 _context->windowInfoService->Release();
104 _context->windowInfoService = nullptr;
105 }
106 if (_context->bitmapRenderService)
107 {
108 sdl_render_service_detach(_context->bitmapRenderService);
109 _context->bitmapRenderService->Release();
110 _context->bitmapRenderService = nullptr;
111 }
112 /* renderers the channels didn't remove */
113 std::scoped_lock lock(_rendererLock);
114 for (auto& kv : _renderers)
115 {
116 if (kv.second.callback)
117 kv.second.callback->Release();
118 }
119 _renderers.clear();
120}
121
122std::optional<bool> SdlContext::allowScreenSaver() const
123{
124 return _allowScreenSaver;
125}
126
127void SdlContext::setHasCursor(bool val)
128{
129 this->_cursor_visible = val;
130}
131
132bool SdlContext::hasCursor() const
133{
134 return _cursor_visible;
135}
136
137void SdlContext::setMetadata()
138{
139 auto wmclass = freerdp_settings_get_string(_context->settings, FreeRDP_WmClass);
140 if (!wmclass || (strlen(wmclass) == 0))
141 wmclass = SDL_CLIENT_UUID;
142
143 SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_IDENTIFIER_STRING, wmclass);
144 SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_NAME_STRING, SDL_CLIENT_NAME);
145 SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_VERSION_STRING, SDL_CLIENT_VERSION);
146 SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_CREATOR_STRING, SDL_CLIENT_VENDOR);
147 SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_COPYRIGHT_STRING, SDL_CLIENT_COPYRIGHT);
148 SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_URL_STRING, SDL_CLIENT_URL);
149 SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_TYPE_STRING, SDL_CLIENT_TYPE);
150}
151
152int SdlContext::start()
153{
154 _thread = std::thread(rdpThreadRun, this);
155 return 0;
156}
157
158int SdlContext::join()
159{
160 /* We do not want to use freerdp_abort_connect_context here.
161 * It would change the exit code and we do not want that. */
162 HANDLE event = freerdp_abort_event(context());
163 if (!SetEvent(event))
164 return -1;
165
166 _thread.join();
167 return 0;
168}
169
170void SdlContext::cleanup()
171{
172 std::unique_lock lock(_critical);
173 _windows.clear();
174 _dialog.destroy();
175 _primary.reset();
176}
177
178bool SdlContext::shallAbort(bool ignoreDialogs)
179{
180 std::unique_lock lock(_critical);
181 if (freerdp_shall_disconnect_context(context()))
182 {
183 if (ignoreDialogs)
184 return true;
185 if (_rdpThreadRunning)
186 return false;
187 return !getDialog().isRunning();
188 }
189 return false;
190}
191
192/* Called before a connection is established.
193 * Set all configuration options to support and load channels here. */
194BOOL SdlContext::preConnect(freerdp* instance)
195{
196 WINPR_ASSERT(instance);
197 WINPR_ASSERT(instance->context);
198
199 auto sdl = get_context(instance->context);
200
201 auto settings = instance->context->settings;
202 WINPR_ASSERT(settings);
203
204 if (!freerdp_settings_set_bool(settings, FreeRDP_CertificateCallbackPreferPEM, TRUE))
205 return FALSE;
206
207 /* Optional OS identifier sent to server */
208 if (!freerdp_settings_set_uint32(settings, FreeRDP_OsMajorType, OSMAJORTYPE_UNIX))
209 return FALSE;
210 if (!freerdp_settings_set_uint32(settings, FreeRDP_OsMinorType, OSMINORTYPE_NATIVE_SDL))
211 return FALSE;
212 /* OrderSupport is initialized at this point.
213 * Only override it if you plan to implement custom order
214 * callbacks or deactivate certain features. */
215 /* Register the channel listeners.
216 * They are required to set up / tear down channels if they are loaded. */
217 if (PubSub_SubscribeChannelConnected(instance->context->pubSub,
218 sdl_OnChannelConnectedEventHandler) < 0)
219 return FALSE;
220 if (PubSub_SubscribeChannelDisconnected(instance->context->pubSub,
221 sdl_OnChannelDisconnectedEventHandler) < 0)
222 return FALSE;
223 if (PubSub_SubscribeUserNotification(instance->context->pubSub,
224 sdl_OnUserNotificationEventHandler) < 0)
225 return FALSE;
226
227 if (!freerdp_settings_get_bool(settings, FreeRDP_AuthenticationOnly))
228 {
229 UINT32 maxWidth = 0;
230 UINT32 maxHeight = 0;
231
232 if (!sdl_detect_monitors(sdl, &maxWidth, &maxHeight))
233 return FALSE;
234
235 /* The server takes the scale of a single monitor from the core data only, so put the
236 * display scale there too. Otherwise the session starts at 100% and is redone once the
237 * display channel sends the layout. */
238 const auto overrides = freerdp_settings_get_uint64(settings, FreeRDP_MonitorOverrideFlags);
239 if (((overrides & FREERDP_MONITOR_OVERRIDE_DESKTOP_SCALE) == 0) &&
240 !freerdp_settings_get_bool(settings, FreeRDP_SmartSizing))
241 {
242 UINT32 scale = 0;
243 for (UINT32 x = 0; x < freerdp_settings_get_uint32(settings, FreeRDP_MonitorCount); x++)
244 {
245 auto monitor = static_cast<const rdpMonitor*>(
246 freerdp_settings_get_pointer_array(settings, FreeRDP_MonitorDefArray, x));
247 if (monitor && monitor->is_primary)
248 scale = monitor->attributes.desktopScaleFactor;
249 }
250 if ((scale == 0) && !sdl->monitorIds().empty())
251 scale = sdl->getDisplay(sdl->monitorIds().front()).attributes.desktopScaleFactor;
252 if ((scale >= 100) && (scale <= 500))
253 {
254 if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopScaleFactor, scale))
255 return FALSE;
256 }
257
258 /* With /multimon the server also takes each monitor's own scale from the monitor
259 * attributes ([MS-RDPBCGR] 2.2.1.3.10), unless an override forces one value on all. */
260 if ((overrides == 0) &&
261 !freerdp_settings_set_bool(settings, FreeRDP_HasMonitorAttributes, TRUE))
262 return FALSE;
263 }
264
265 if ((maxWidth != 0) && (maxHeight != 0) &&
266 !freerdp_settings_get_bool(settings, FreeRDP_SmartSizing))
267 {
268 WLog_Print(sdl->getWLog(), WLOG_INFO, "Update size to %ux%u", maxWidth, maxHeight);
269 if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopWidth, maxWidth))
270 return FALSE;
271 if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopHeight, maxHeight))
272 return FALSE;
273 }
274
279
280 const uint32_t sw = freerdp_settings_get_uint32(settings, FreeRDP_SmartSizingWidth);
281 const uint32_t sh = freerdp_settings_get_uint32(settings, FreeRDP_SmartSizingHeight);
282 const BOOL sm = freerdp_settings_get_bool(settings, FreeRDP_SmartSizing);
283 if (sm && (sw > 0) && (sh > 0))
284 {
285 const BOOL mm = freerdp_settings_get_bool(settings, FreeRDP_UseMultimon);
286 if (mm)
287 WLog_Print(sdl->getWLog(), WLOG_WARN,
288 "/smart-sizing and /multimon are currently not supported, ignoring "
289 "/smart-sizing!");
290 else
291 {
292 sdl->_windowWidth = freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth);
293 sdl->_windowHeight = freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight);
294
295 if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopWidth, sw))
296 return FALSE;
297 if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopHeight, sh))
298 return FALSE;
299 }
300 }
301 }
302 else
303 {
304 /* Check +auth-only has a username and password. */
305 if (!freerdp_settings_get_string(settings, FreeRDP_Password))
306 {
307 WLog_Print(sdl->getWLog(), WLOG_INFO,
308 "auth-only, but no password set. Please provide one.");
309 return FALSE;
310 }
311
312 if (!freerdp_settings_set_bool(settings, FreeRDP_DeactivateClientDecoding, TRUE))
313 return FALSE;
314
315 WLog_Print(sdl->getWLog(), WLOG_INFO, "Authentication only. Don't connect SDL.");
316 }
317
318 if (!sdl->getInputChannelContext().initialize())
319 return FALSE;
320
321 sdl->_credentialsRead = false;
322
323 /* Settings are consistent at this point, the dialog can be created. */
324 auto prefs = SdlPref::instance();
325 auto dialog = prefs->get_bool("SDL_ConnectionDialog", true);
326 if (sdl->_connectionDialog && prefs->is_commandline_override_allowed())
327 dialog = *sdl->_connectionDialog;
328 if (dialog && !freerdp_settings_get_bool(settings, FreeRDP_AuthenticationOnly))
329 {
330 sdl->getDialog().create(instance->context);
331 sdl->getDialog().setTitle("Connecting to '%s'", freerdp_settings_get_server_name(settings));
332 sdl->getDialog().showInfo("The connection is being established\n\nPlease wait...");
333 }
334
335 /* TODO: Any code your client requires */
336 return TRUE;
337}
338
339/* Called after a RDP connection was successfully established.
340 * Settings might have changed during negotiation of client / server feature
341 * support.
342 *
343 * Set up local framebuffers and paing callbacks.
344 * If required, register pointer callbacks to change the local mouse cursor
345 * when hovering over the RDP window
346 */
347BOOL SdlContext::postConnect(freerdp* instance)
348{
349 WINPR_ASSERT(instance);
350
351 auto context = instance->context;
352 WINPR_ASSERT(context);
353
354 auto sdl = get_context(context);
355
356 // Retry was successful, discard dialog
357 sdl->getDialog().show(false);
358
359 if (freerdp_settings_get_bool(context->settings, FreeRDP_AuthenticationOnly))
360 {
361 /* Check +auth-only has a username and password. */
362 if (!freerdp_settings_get_string(context->settings, FreeRDP_Password))
363 {
364 WLog_Print(sdl->getWLog(), WLOG_INFO,
365 "auth-only, but no password set. Please provide one.");
366 return FALSE;
367 }
368
369 WLog_Print(sdl->getWLog(), WLOG_INFO, "Authentication only. Don't connect to X.");
370 return TRUE;
371 }
372
373 if (!sdl->waitForWindowsCreated())
374 return FALSE;
375
376 sdl->_sdlPixelFormat = SDL_PIXELFORMAT_BGRA32;
377 if (!gdi_init(instance, PIXEL_FORMAT_BGRA32))
378 return FALSE;
379
380 if (!sdl->createPrimary())
381 return FALSE;
382
383 if (!sdl_register_pointer(instance->context->graphics))
384 return FALSE;
385
386 WINPR_ASSERT(context->update);
387
388 context->update->BeginPaint = beginPaint;
389 context->update->EndPaint = endPaint;
390 context->update->PlaySound = playSound;
391 context->update->DesktopResize = desktopResize;
392 context->update->SetKeyboardIndicators = sdlInput::keyboard_set_indicators;
393 context->update->SetKeyboardImeStatus = sdlInput::keyboard_set_ime_status;
394
395 if (!sdl->setResizeable(false))
396 return FALSE;
397 if (!sdl->setFullscreen(freerdp_settings_get_bool(context->settings, FreeRDP_Fullscreen) ||
398 freerdp_settings_get_bool(context->settings, FreeRDP_UseMultimon),
399 true))
400 return FALSE;
401 sdl->setConnected(true);
402 /* also after a reconnect: the overlay's rates and totals are this connection's */
403 return sdl->_overlay.connected();
404}
405
406/* This function is called whether a session ends by failure or success.
407 * Clean up everything allocated by pre_connect and post_connect.
408 */
409void SdlContext::postDisconnect(freerdp* instance)
410{
411 if (!instance)
412 return;
413
414 if (!instance->context)
415 return;
416
417 auto sdl = get_context(instance->context);
418 sdl->setConnected(false);
419
420 gdi_free(instance);
421}
422
423void SdlContext::postFinalDisconnect(freerdp* instance)
424{
425 if (!instance)
426 return;
427
428 if (!instance->context)
429 return;
430
431 PubSub_UnsubscribeChannelConnected(instance->context->pubSub,
432 sdl_OnChannelConnectedEventHandler);
433 PubSub_UnsubscribeChannelDisconnected(instance->context->pubSub,
434 sdl_OnChannelDisconnectedEventHandler);
435 PubSub_UnsubscribeUserNotification(instance->context->pubSub,
436 sdl_OnUserNotificationEventHandler);
437}
438
439/* Create a SDL surface from the GDI buffer */
440bool SdlContext::createPrimary()
441{
442 auto gdi = context()->gdi;
443 WINPR_ASSERT(gdi);
444
445 _primary = SDLSurfacePtr(
446 SDL_CreateSurfaceFrom(static_cast<int>(gdi->width), static_cast<int>(gdi->height),
447 pixelFormat(), gdi->primary_buffer, static_cast<int>(gdi->stride)),
448 SDL_DestroySurface);
449 if (!_primary)
450 return false;
451
452 SDL_SetSurfaceBlendMode(_primary.get(), SDL_BLENDMODE_NONE);
453 SDL_Rect surfaceRect = { 0, 0, gdi->width, gdi->height };
454 SDL_FillSurfaceRect(_primary.get(), &surfaceRect,
455 SDL_MapSurfaceRGBA(_primary.get(), 0, 0, 0, 0xff));
456
457 return true;
458}
459
460bool SdlContext::createWindows()
461{
462 auto settings = context()->settings;
463 const auto& title = windowTitle();
464
465 ScopeGuard guard1([&]() { _windowsCreatedEvent.set(); });
466
467 UINT32 windowCount = freerdp_settings_get_uint32(settings, FreeRDP_MonitorCount);
468
469 /* The monitor definitions are sorted (primary first), so they need not be in the same
470 * order as the monitor IDs. Match them by display, fall back to the index. */
471 auto monitorFor = [&](UINT32 index, int64_t id)
472 {
473 for (UINT32 y = 0; y < windowCount; y++)
474 {
475 auto cur = static_cast<rdpMonitor*>(
476 freerdp_settings_get_pointer_array_writable(settings, FreeRDP_MonitorDefArray, y));
477 if (cur && (static_cast<int64_t>(cur->orig_screen) == id))
478 return cur;
479 }
480 return static_cast<rdpMonitor*>(
481 freerdp_settings_get_pointer_array_writable(settings, FreeRDP_MonitorDefArray, index));
482 };
483
484 Sint32 originX = 0;
485 Sint32 originY = 0;
486 for (UINT32 x = 0; x < windowCount; x++)
487 {
488 auto id = monitorId(x);
489 if (id < 0)
490 return false;
491
492 auto monitor = monitorFor(x, id);
493
494 originX = std::min<Sint32>(monitor->x, originX);
495 originY = std::min<Sint32>(monitor->y, originY);
496 }
497
498 for (UINT32 x = 0; x < windowCount; x++)
499 {
500 auto id = monitorId(x);
501 if (id < 0)
502 return false;
503
504 auto monitor = monitorFor(x, id);
505
506 Uint32 w = WINPR_ASSERTING_INT_CAST(Uint32, monitor->width);
507 Uint32 h = WINPR_ASSERTING_INT_CAST(Uint32, monitor->height);
508 if (!(freerdp_settings_get_bool(settings, FreeRDP_UseMultimon) ||
509 freerdp_settings_get_bool(settings, FreeRDP_Fullscreen)))
510 {
511 if (_windowWidth > 0)
512 w = _windowWidth;
513 else
514 w = freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth);
515
516 if (_windowHeight > 0)
517 h = _windowHeight;
518 else
519 h = freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight);
520 }
521
522 Uint32 flags = SDL_WINDOW_HIGH_PIXEL_DENSITY;
523
524 if (freerdp_settings_get_bool(settings, FreeRDP_Fullscreen) &&
525 !freerdp_settings_get_bool(settings, FreeRDP_UseMultimon))
526 {
527 flags |= SDL_WINDOW_FULLSCREEN;
528 }
529
530 if (freerdp_settings_get_bool(settings, FreeRDP_UseMultimon))
531 {
532 flags |= SDL_WINDOW_BORDERLESS;
533 }
534
535 if (!freerdp_settings_get_bool(settings, FreeRDP_Decorations))
536 flags |= SDL_WINDOW_BORDERLESS;
537
538 /* RemoteApp: the desktop only shows when needed (drawToWindows). */
539 if (freerdp_settings_get_bool(settings, FreeRDP_RemoteApplicationMode))
540 flags |= SDL_WINDOW_HIDDEN;
541
542 auto did = WINPR_ASSERTING_INT_CAST(SDL_DisplayID, id);
543 auto window = SdlWindow::create(did, title, flags, w, h);
544
545 if (freerdp_settings_get_bool(settings, FreeRDP_UseMultimon))
546 {
547 window.setOffsetX(originX - monitor->x);
548 window.setOffsetY(originY - monitor->y);
549 }
550
551 _windows.insert({ window.id(), std::move(window) });
552 }
553
555 return true;
556}
557
558#if defined(__EMSCRIPTEN__)
559/* in sdl_monitors_emscripten.js */
560extern "C" int freerdp_web_monitors_js(int32_t* edges, int max);
561
562/* A browser has one canvas, so one window. The page may show it across several screens and
563 * split it into monitors (sdl_monitors_emscripten.js): the server then places its windows,
564 * panels and maximized windows on each, as on separate physical screens. canvas is the
565 * window's monitor; returns the monitors it is split into, or none. */
566static std::vector<rdpMonitor> sdl_web_monitors(const rdpMonitor& canvas)
567{
568 constexpr int max = 16;
569 std::array<int32_t, 5 * max> edges{};
570 const auto n = freerdp_web_monitors_js(edges.data(), max);
571 if ((n < 2) || (n > max))
572 return {};
573
574 const auto at = [](int32_t edge, int32_t size)
575 { return static_cast<INT32>(static_cast<int64_t>(edge) * size / 10000); };
576 std::vector<rdpMonitor> list;
577 for (int i = 0; i < n; i++)
578 {
579 const int32_t* e = &edges.at(5 * static_cast<size_t>(i));
580 const auto left = at(e[0], canvas.width);
581 const auto top = at(e[1], canvas.height);
582 const auto right = at(e[2], canvas.width);
583 const auto bottom = at(e[3], canvas.height);
584 if ((right <= left) || (bottom <= top))
585 return {};
586
587 rdpMonitor m = canvas;
588 m.x = left;
589 m.y = top;
590 m.width = right - left;
591 m.height = bottom - top;
592 m.is_primary = (e[4] != 0);
593 m.attributes.physicalWidth =
594 static_cast<UINT32>(static_cast<uint64_t>(canvas.attributes.physicalWidth) *
595 static_cast<UINT32>(m.width) / static_cast<UINT32>(canvas.width));
596 m.attributes.physicalHeight =
597 static_cast<UINT32>(static_cast<uint64_t>(canvas.attributes.physicalHeight) *
598 static_cast<UINT32>(m.height) / static_cast<UINT32>(canvas.height));
599 list.push_back(m);
600 }
601 return list;
602}
603#endif
604
605bool SdlContext::updateWindowList()
606{
607 /* The windows only know their own size: keep the layout from connect. */
608 if (freerdp_settings_get_bool(context()->settings, FreeRDP_UseMultimon))
609 return true;
610
611 std::vector<rdpMonitor> list;
612 list.reserve(_windows.size());
613 for (const auto& win : _windows)
614 list.push_back(win.second.monitor(_windows.size() == 1));
615
616#if defined(__EMSCRIPTEN__)
617 if ((list.size() == 1) && (list.at(0).width > 0) && (list.at(0).height > 0))
618 {
619 auto split = sdl_web_monitors(list.at(0));
620 if (!split.empty())
621 list = std::move(split);
622 }
623#endif
624
625 // /monitors: subset may exclude the SDL primary. The library requires
626 // the array to mark one monitor as primary, so promote the first when
627 // none of the kept windows cover the original primary.
628 if (!list.empty() &&
629 std::none_of(list.cbegin(), list.cend(), [](const rdpMonitor& m) { return m.is_primary; }))
630 list.at(0).is_primary = true;
631
632 return freerdp_settings_set_monitor_def_array_sorted(context()->settings, list.data(),
633 list.size());
634}
635
636bool SdlContext::updateWindow(SDL_WindowID id)
637{
638 /* In fullscreen the monitor kept the size measured with the probe window at startup and was
639 * never updated, so a session the server started at another size stayed that way. Only the
640 * window knows its real size, so ask it in fullscreen as well. */
641 if (freerdp_settings_get_bool(_context->settings, FreeRDP_UseMultimon))
642 return true;
643
644 auto& w = _windows.at(id);
645 auto m = w.monitor(true);
646 auto r = w.rect();
647 m.width = r.w;
648 m.height = r.h;
649 /* The window may be on another display than the one it was created on. */
650 m.attributes.desktopScaleFactor = static_cast<UINT32>(std::roundf(w.scale() * 100.0f));
651 w.setMonitor(m);
652 return true;
653}
654
655std::string SdlContext::windowTitle() const
656{
657 const char* prefix = "FreeRDP:";
658
659 const auto windowTitle = freerdp_settings_get_string(context()->settings, FreeRDP_WindowTitle);
660 if (windowTitle)
661 return windowTitle;
662
663 const auto name = freerdp_settings_get_server_name(context()->settings);
664 const auto port = freerdp_settings_get_uint32(context()->settings, FreeRDP_ServerPort);
665 const auto addPort = (port != 3389);
666
667 std::stringstream ss;
668 ss << prefix << " " << name;
669
670 if (addPort)
671 ss << ":" << port;
672
673 return ss.str();
674}
675
676bool SdlContext::waitForWindowsCreated()
677{
678 {
679 std::unique_lock<CriticalSection> lock(_critical);
680 _windowsCreatedEvent.clear();
681 if (!sdl_push_user_event(SDL_EVENT_USER_CREATE_WINDOWS, this))
682 return false;
683 }
684
685 HANDLE handles[] = { _windowsCreatedEvent.handle(), freerdp_abort_event(context()) };
686
687 const DWORD rc = WaitForMultipleObjects(ARRAYSIZE(handles), handles, FALSE, INFINITE);
688 switch (rc)
689 {
690 case WAIT_OBJECT_0:
691 return true;
692 default:
693 return false;
694 }
695}
696
697/* This function is called when the library completed composing a new
698 * frame. Read out the changed areas and blit them to your output device.
699 * The image buffer will have the format specified by gdi_init
700 */
701BOOL SdlContext::endPaint(rdpContext* context)
702{
703 auto sdl = get_context(context);
704 WINPR_ASSERT(sdl);
705
706 auto gdi = context->gdi;
707 WINPR_ASSERT(gdi);
708 WINPR_ASSERT(gdi->primary);
709
710 HGDI_DC hdc = gdi->primary->hdc;
711 WINPR_ASSERT(hdc);
712 if (!hdc->hwnd)
713 return TRUE;
714
715 HGDI_WND hwnd = hdc->hwnd;
716 WINPR_ASSERT(hwnd->invalid || (hwnd->ninvalid == 0));
717
718 if (hwnd->invalid->null)
719 return TRUE;
720
721 WINPR_ASSERT(hwnd->invalid);
722 if (gdi->suppressOutput || hwnd->invalid->null)
723 return TRUE;
724
725 const INT32 ninvalid = hwnd->ninvalid;
726 const GDI_RGN* cinvalid = hwnd->cinvalid;
727
728 if (ninvalid < 1)
729 return TRUE;
730
731 std::vector<SDL_Rect> rects;
732 for (INT32 x = 0; x < ninvalid; x++)
733 {
734 auto& rgn = cinvalid[x];
735 rects.push_back({ rgn.x, rgn.y, rgn.w, rgn.h });
736 }
737
738 sdl->push(std::move(rects));
739 return sdl_push_user_event(SDL_EVENT_USER_UPDATE);
740}
741
742void SdlContext::sdl_client_cleanup(int exit_code, const std::string& error_msg)
743{
744 rdpSettings* settings = context()->settings;
745 WINPR_ASSERT(settings);
746
747 _rdpThreadRunning = false;
748 bool showError = false;
749 if (freerdp_settings_get_bool(settings, FreeRDP_AuthenticationOnly))
750 WLog_Print(getWLog(), WLOG_INFO, "Authentication only, exit status %s [%" PRId32 "]",
751 sdl::error::exitCodeToTag(exit_code), exit_code);
752 else
753 {
754 switch (exit_code)
755 {
756 case sdl::error::SUCCESS:
757 case sdl::error::DISCONNECT:
758 case sdl::error::LOGOFF:
759 case sdl::error::DISCONNECT_BY_USER:
760 case sdl::error::CONNECT_CANCELLED:
761 break;
762 default:
763 {
764 showError = true;
765 getDialog().showError(error_msg);
766 }
767 break;
768 }
769 }
770
771 if (!showError)
772 getDialog().show(false);
773
774 _exitCode = exit_code;
775 std::ignore = sdl_push_user_event(SDL_EVENT_USER_QUIT);
776 SDL_CleanupTLS();
777}
778
779int SdlContext::sdl_client_thread_connect(std::string& error_msg)
780{
781 auto instance = context()->instance;
782 WINPR_ASSERT(instance);
783
784 _rdpThreadRunning = true;
785 BOOL rc = freerdp_connect(instance);
786
787 rdpSettings* settings = context()->settings;
788 WINPR_ASSERT(settings);
789
790 int exit_code = sdl::error::SUCCESS;
791 if (!rc)
792 {
793 UINT32 error = freerdp_get_last_error(context());
794 exit_code = sdl::error::errorToExitCode(error);
795 }
796
797 if (freerdp_settings_get_bool(settings, FreeRDP_AuthenticationOnly))
798 {
799 DWORD code = freerdp_get_last_error(context());
800 freerdp_abort_connect_context(context());
801 WLog_Print(getWLog(), WLOG_ERROR, "Authentication only, %s [0x%08" PRIx32 "] %s",
802 freerdp_get_last_error_name(code), code, freerdp_get_last_error_string(code));
803 return exit_code;
804 }
805
806 if (!rc)
807 {
808 DWORD code = freerdp_error_info(instance);
809 if (exit_code == sdl::error::SUCCESS)
810 {
811 char* msg = nullptr;
812 size_t len = 0;
813 exit_code = error_info_to_error(&code, &msg, &len);
814 if (msg)
815 error_msg = msg;
816 free(msg);
817 }
818
819 auto last = freerdp_get_last_error(context());
820 if (error_msg.empty())
821 {
822 char* msg = nullptr;
823 size_t len = 0;
824 winpr_asprintf(&msg, &len, "%s [0x%08" PRIx32 "]\n%s",
825 freerdp_get_last_error_name(last), last,
826 freerdp_get_last_error_string(last));
827 if (msg)
828 error_msg = msg;
829 free(msg);
830 }
831
832 if (exit_code == sdl::error::SUCCESS)
833 {
834 if (last == FREERDP_ERROR_AUTHENTICATION_FAILED)
835 exit_code = sdl::error::AUTH_FAILURE;
836 else if (code == ERRINFO_SUCCESS)
837 exit_code = sdl::error::CONN_FAILED;
838 }
839
840 getDialog().show(false);
841 }
842
843 return exit_code;
844}
845
846int SdlContext::sdl_client_thread_run(std::string& error_msg)
847{
848 auto instance = context()->instance;
849 WINPR_ASSERT(instance);
850
851 int exit_code = sdl::error::SUCCESS;
852 while (!freerdp_shall_disconnect_context(context()))
853 {
854 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = {};
855 /*
856 * win8 and server 2k12 seem to have some timing issue/race condition
857 * when a initial sync request is send to sync the keyboard indicators
858 * sending the sync event twice fixed this problem
859 */
860 if (freerdp_focus_required(instance))
861 {
862 auto ctx = get_context(context());
863 WINPR_ASSERT(ctx);
864
865 auto& input = ctx->getInputChannelContext();
866 if (!input.keyboard_focus_in())
867 break;
868 if (!input.keyboard_focus_in())
869 break;
870 }
871
872 const DWORD nCount = freerdp_get_event_handles(context(), handles, ARRAYSIZE(handles));
873
874 if (nCount == 0)
875 {
876 WLog_Print(getWLog(), WLOG_ERROR, "freerdp_get_event_handles failed");
877 break;
878 }
879
880 const DWORD status = WaitForMultipleObjects(nCount, handles, FALSE, INFINITE);
881
882 if (status == WAIT_FAILED)
883 {
884 WLog_Print(getWLog(), WLOG_ERROR, "WaitForMultipleObjects WAIT_FAILED");
885 break;
886 }
887
888 if (!freerdp_check_event_handles(context()))
889 {
890 if (client_auto_reconnect(instance))
891 {
892 // Retry was successful, discard dialog
893 getDialog().show(false);
894 continue;
895 }
896 else
897 {
898 /*
899 * Indicate an unsuccessful connection attempt if reconnect
900 * did not succeed and no other error was specified.
901 */
902 if (freerdp_error_info(instance) == 0)
903 exit_code = sdl::error::errorToExitCode(freerdp_get_last_error(context()));
904 }
905
906 if (freerdp_get_last_error(context()) == FREERDP_ERROR_SUCCESS)
907 WLog_Print(getWLog(), WLOG_ERROR, "WaitForMultipleObjects failed with %" PRIu32 "",
908 status);
909 if (freerdp_get_last_error(context()) == FREERDP_ERROR_SUCCESS)
910 WLog_Print(getWLog(), WLOG_ERROR, "Failed to check FreeRDP event handles");
911 break;
912 }
913 }
914
915 if (exit_code == sdl::error::SUCCESS)
916 {
917 DWORD code = 0;
918 {
919 char* emsg = nullptr;
920 size_t elen = 0;
921 exit_code = error_info_to_error(&code, &emsg, &elen);
922 if (emsg)
923 error_msg = emsg;
924 free(emsg);
925 }
926
927 if ((code == ERRINFO_LOGOFF_BY_USER) &&
928 (freerdp_get_disconnect_ultimatum(context()) == Disconnect_Ultimatum_user_requested))
929 {
930 const char* msg = "Error info says user did not initiate but disconnect ultimatum says "
931 "they did; treat this as a user logoff";
932
933 char* emsg = nullptr;
934 size_t elen = 0;
935 winpr_asprintf(&emsg, &elen, "%s", msg);
936 if (emsg)
937 error_msg = emsg;
938 free(emsg);
939
940 /* This situation might be limited to Windows XP. */
941 WLog_Print(getWLog(), WLOG_INFO, "%s", msg);
942 exit_code = sdl::error::LOGOFF;
943 }
944 }
945
946 if ((exit_code != sdl::error::SUCCESS) && error_msg.empty())
947 {
948 const auto last = freerdp_get_last_error(context());
949 char* msg = nullptr;
950 size_t len = 0;
951 winpr_asprintf(&msg, &len, "%s [0x%08" PRIx32 "]\n%s", freerdp_get_last_error_name(last),
952 last, freerdp_get_last_error_string(last));
953 if (msg)
954 error_msg = msg;
955 free(msg);
956 }
957
958 freerdp_disconnect(instance);
959
960 return exit_code;
961}
962
963/* RDP main loop.
964 * Connects RDP, loops while running and handles event and dispatch, cleans up
965 * after the connection ends. */
966DWORD SdlContext::rdpThreadRun(SdlContext* sdl)
967{
968 WINPR_ASSERT(sdl);
969
970 std::string error_msg;
971 int exit_code = sdl->sdl_client_thread_connect(error_msg);
972 if (exit_code == sdl::error::SUCCESS)
973 exit_code = sdl->sdl_client_thread_run(error_msg);
974 sdl->sdl_client_cleanup(exit_code, error_msg);
975
976 return static_cast<DWORD>(exit_code);
977}
978
979int SdlContext::error_info_to_error(DWORD* pcode, char** msg, size_t* len) const
980{
981 const DWORD code = freerdp_error_info(context()->instance);
982 const char* name = freerdp_get_error_info_name(code);
983 const char* str = freerdp_get_error_info_string(code);
984 const int exit_code = sdl::error::errorToExitCode(code);
985
986 winpr_asprintf(msg, len, "Terminate with %s due to ERROR_INFO %s [0x%08" PRIx32 "]: %s",
987 sdl::error::errorToExitCodeTag(code), name, code, str);
988 SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "%s", *msg);
989 if (pcode)
990 *pcode = code;
991 return exit_code;
992}
993
994void SdlContext::applyMonitorOffset(SDL_WindowID window, float& x, float& y) const
995{
996 if (!freerdp_settings_get_bool(context()->settings, FreeRDP_UseMultimon))
997 return;
998
999 auto w = getWindowForId(window);
1000 x -= static_cast<float>(w->offsetX());
1001 y -= static_cast<float>(w->offsetY());
1002}
1003
1004void SdlContext::updateMonitorDataFromOffsets()
1005{
1006 for (auto& entry : _windows)
1007 {
1008 const auto& monitor = _displays.at(entry.first);
1009 entry.second.setMonitor(monitor);
1010 }
1011}
1012
1013bool SdlContext::drawToWindow(SdlWindow& window, const std::vector<SDL_Rect>& rects)
1014{
1015 if (!isConnected())
1016 return true;
1017
1018 auto gdi = context()->gdi;
1019 WINPR_ASSERT(gdi);
1020
1021 auto size = window.rect();
1022
1023 std::unique_lock lock(_critical);
1024 auto surface = _primary.get();
1025
1026 if (useLocalScale())
1027 {
1028 window.setOffsetX(0);
1029 window.setOffsetY(0);
1030 if (gdi->width < size.w)
1031 {
1032 window.setOffsetX((size.w - gdi->width) / 2);
1033 }
1034 if (gdi->height < size.h)
1035 {
1036 window.setOffsetY((size.h - gdi->height) / 2);
1037 }
1038
1039 _localScale = { static_cast<float>(size.w) / static_cast<float>(gdi->width),
1040 static_cast<float>(size.h) / static_cast<float>(gdi->height) };
1041 if (!window.drawScaledRects(surface, _localScale, rects))
1042 return false;
1043 }
1044 else
1045 {
1046 SDL_Point offset{ 0, 0 };
1047 if (freerdp_settings_get_bool(context()->settings, FreeRDP_UseMultimon))
1048 offset = { window.offsetX(), window.offsetY() };
1049 if (!window.drawRects(surface, offset, rects))
1050 return false;
1051 }
1052
1053 window.updateSurface((&window == overlayWindow()) ? &_overlay : nullptr);
1054 return true;
1055}
1056
1057SdlWindow* SdlContext::overlayWindow()
1058{
1059 /* RemoteApp windows are the server's, so v1 has no overlay there */
1060 if (_rail.enabled() || _windows.empty())
1061 return nullptr;
1062 return &_windows.begin()->second;
1063}
1064
1066{
1067 return showOverlay(!_overlay.visible());
1068}
1069
1070bool SdlContext::showOverlay(bool on)
1071{
1072 if (!_overlay.show(on))
1073 return false;
1074 if (on || !isConnected())
1075 return true;
1076
1077 /* hidden: present the picture once without it */
1078 auto window = overlayWindow();
1079 if (window)
1080 window->updateSurface();
1081 return true;
1082}
1083
1084bool SdlContext::updateOverlay()
1085{
1086 auto window = overlayWindow();
1087 if (!window || !isConnected())
1088 return true;
1089
1090 int w = 0;
1091 int h = 0;
1092 if (!SDL_GetWindowSizeInPixels(window->window(), &w, &h))
1093 return true;
1094 if (_overlay.update(w, h, window->rendererName()))
1095 window->updateSurface(&_overlay);
1096 return true;
1097}
1098
1099bool SdlContext::minimizeAllWindows()
1100{
1101 for (auto& w : _windows)
1102 w.second.minimize();
1103 return true;
1104}
1105
1106int SdlContext::exitCode() const
1107{
1108 return _exitCode;
1109}
1110
1111SDL_PixelFormat SdlContext::pixelFormat() const
1112{
1113 return _sdlPixelFormat;
1114}
1115
1116bool SdlContext::addDisplayWindow(SDL_DisplayID id)
1117{
1118 const auto flags =
1119 SDL_WINDOW_HIGH_PIXEL_DENSITY | SDL_WINDOW_FULLSCREEN | SDL_WINDOW_BORDERLESS;
1120 auto title = sdl::utils::windowTitle(context()->settings);
1121 auto w = SdlWindow::create(id, title, flags);
1122 _windows.emplace(w.id(), std::move(w));
1123 return true;
1124}
1125
1126bool SdlContext::removeDisplayWindow(SDL_DisplayID id)
1127{
1128 for (auto& w : _windows)
1129 {
1130 if (w.second.displayIndex() == id)
1131 _windows.erase(w.first);
1132 }
1133 return true;
1134}
1135
1136bool SdlContext::detectDisplays()
1137{
1138 int count = 0;
1139 auto display = SDL_GetDisplays(&count);
1140 if (!display)
1141 {
1142 WLog_Print(getWLog(), WLOG_ERROR, "SDL_GetDisplays failed: %s", SDL_GetError());
1143 return false;
1144 }
1145 for (int x = 0; x < count; x++)
1146 {
1147 const auto id = display[x];
1148 addOrUpdateDisplay(id);
1149 }
1150 SDL_free(display);
1151
1152 /* The monitor setup needs at least one display. */
1153 if (_displays.empty())
1154 {
1155 WLog_Print(getWLog(), WLOG_ERROR, "none of the %d displays has a usable size", count);
1156 return false;
1157 }
1158 return true;
1159}
1160
1161rdpMonitor SdlContext::getDisplay(SDL_DisplayID id) const
1162{
1163 return _displays.at(id);
1164}
1165
1166std::vector<SDL_DisplayID> SdlContext::getDisplayIds() const
1167{
1168 std::vector<SDL_DisplayID> keys;
1169 keys.reserve(_displays.size());
1170 for (const auto& entry : _displays)
1171 {
1172 keys.push_back(entry.first);
1173 }
1174 return keys;
1175}
1176
1177const SdlWindow* SdlContext::getWindowForId(SDL_WindowID id) const
1178{
1179 auto it = _windows.find(id);
1180 if (it == _windows.end())
1181 return nullptr;
1182 return &it->second;
1183}
1184
1185SdlWindow* SdlContext::getWindowForId(SDL_WindowID id)
1186{
1187 auto it = _windows.find(id);
1188 if (it == _windows.end())
1189 return nullptr;
1190 return &it->second;
1191}
1192
1193bool SdlContext::allWindowsHidden() const
1194{
1195 return std::all_of(_windows.begin(), _windows.end(),
1196 [](const auto& entry)
1197 {
1198 const auto flags = SDL_GetWindowFlags(entry.second.window());
1199 return (flags & (SDL_WINDOW_HIDDEN | SDL_WINDOW_MINIMIZED)) != 0;
1200 });
1201}
1202
1203SdlWindow* SdlContext::getFirstWindow()
1204{
1205 if (_windows.empty())
1206 return nullptr;
1207 return &_windows.begin()->second;
1208}
1209
1210sdlDispContext& SdlContext::getDisplayChannelContext()
1211{
1212 return _disp;
1213}
1214
1215sdlInput& SdlContext::getInputChannelContext()
1216{
1217 return _input;
1218}
1219
1220sdlClip& SdlContext::getClipboardChannelContext()
1221{
1222 return _clip;
1223}
1224
1225SdlRail& SdlContext::getRailChannelContext()
1226{
1227 return _rail;
1228}
1229
1230SdlConnectionDialogWrapper& SdlContext::getDialog()
1231{
1232 return _dialog;
1233}
1234
1235wLog* SdlContext::getWLog()
1236{
1237 return _log;
1238}
1239
1240bool SdlContext::moveMouseTo(const SDL_FPoint& pos)
1241{
1242 auto window = SDL_GetMouseFocus();
1243 if (!window)
1244 return true;
1245
1246 const auto id = SDL_GetWindowID(window);
1247 const auto spos = pixelToScreen(id, pos);
1248 SDL_WarpMouseInWindow(window, spos.x, spos.y);
1249 return true;
1250}
1251
1252static bool onWindowEdge(SDL_WindowID id, float x, float y)
1253{
1254 int w = 0;
1255 int h = 0;
1256 auto* window = SDL_GetWindowFromID(id);
1257 if (!window || !SDL_GetWindowSize(window, &w, &h))
1258 return false;
1259 return (x <= 0.0f) || (y <= 0.0f) || (x >= static_cast<float>(w - 1)) ||
1260 (y >= static_cast<float>(h - 1));
1261}
1262
1263bool SdlContext::handleEvent(const SDL_MouseMotionEvent& ev)
1264{
1265 SDL_Event copy{};
1266 copy.motion = ev;
1267 /* SDL follows a release outside the window with a motion clamped to its edge: drop it. */
1268 if (_rail.enabled() && (ev.state == 0) && (ev.windowID == _lastRelease.window) &&
1269 (ev.timestamp - _lastRelease.time < SDL_MS_TO_NS(2)) &&
1270 onWindowEdge(ev.windowID, ev.x, ev.y))
1271 return true;
1272 /* WM owns the drag (#12447); backstop: button released but button-up swallowed by grab. */
1273 if (_rail.enabled() && _rail.suppressServerMotion(ev.windowID))
1274 {
1275 if (!(SDL_GetGlobalMouseState(nullptr, nullptr) & SDL_BUTTON_LMASK))
1276 _rail.completeLocalMoveIfPending();
1277 return true;
1278 }
1279 if (_rail.enabled() && _rail.translateToServer(ev.windowID, copy.motion.x, copy.motion.y))
1280 {
1281 _lastMotion = {
1282 ev.windowID, { ev.x, ev.y }, { copy.motion.x, copy.motion.y }, _rail.pointerEpoch()
1283 };
1284 _rail.noteServerPointer(copy.motion.x, copy.motion.y);
1285 return SdlTouch::handleEvent(this, copy.motion);
1286 }
1287
1288 if (!getWindowForId(ev.windowID))
1289 return true; /* Event for an untracked window (e.g. closed dialog) */
1290 if (!eventToPixelCoordinates(ev.windowID, copy))
1291 return true;
1292 removeLocalScaling(copy.motion.x, copy.motion.y);
1293 removeLocalScaling(copy.motion.xrel, copy.motion.yrel);
1294 applyMonitorOffset(copy.motion.windowID, copy.motion.x, copy.motion.y);
1295
1296 return SdlTouch::handleEvent(this, copy.motion);
1297}
1298
1299bool SdlContext::handleEvent(const SDL_MouseWheelEvent& ev)
1300{
1301 SDL_Event copy{};
1302 copy.wheel = ev;
1303 if (_rail.enabled() &&
1304 _rail.translateToServer(ev.windowID, copy.wheel.mouse_x, copy.wheel.mouse_y))
1305 return SdlTouch::handleEvent(this, copy.wheel);
1306
1307 if (!getWindowForId(ev.windowID))
1308 return true;
1309 if (!eventToPixelCoordinates(ev.windowID, copy))
1310 return true;
1311 removeLocalScaling(copy.wheel.mouse_x, copy.wheel.mouse_y);
1312 return SdlTouch::handleEvent(this, copy.wheel);
1313}
1314
1315bool SdlContext::handleEvent(const SDL_WindowEvent& ev)
1316{
1317 if (!getDisplayChannelContext().handleEvent(ev))
1318 return false;
1319
1320 auto window = getWindowForId(ev.windowID);
1321 if (!window)
1322 {
1323 /* RAIL windows aren't in _windows; handle their events here. */
1324 if (_rail.enabled() && _rail.ownsWindow(ev.windowID))
1325 {
1326 switch (ev.type)
1327 {
1328 case SDL_EVENT_WINDOW_MOUSE_ENTER:
1329 /* Re-enter fires on move-grab end. */
1330 if (!(SDL_GetGlobalMouseState(nullptr, nullptr) & SDL_BUTTON_LMASK))
1331 _rail.completeLocalMoveIfPending(); /* X11 */
1332 _rail.completeWaylandResize(); /* Wayland: compositor grab ended */
1333 /* Restore the cursor or the pointer stays hidden over RemoteApp windows. */
1334 return restoreCursor();
1335 case SDL_EVENT_WINDOW_MOUSE_LEAVE:
1336 /* Compositor took the pointer for the pending Wayland resize grab. */
1337 _rail.noteResizeGrab(ev.windowID);
1338 return true;
1339 case SDL_EVENT_WINDOW_EXPOSED:
1340 /* Force repaint for exposed windows; skip during local drag to avoid redundant
1341 * frames. */
1342 if (!_rail.dragActive(ev.windowID))
1343 _rail.invalidateWindow(ev.windowID);
1344 return true;
1345 case SDL_EVENT_WINDOW_MAXIMIZED:
1346 /* Maximize must complete move first. */
1347 _rail.completeLocalMoveIfPending();
1348 _rail.handleMaximized(ev.windowID);
1349 return true;
1350 case SDL_EVENT_WINDOW_MINIMIZED:
1351 _rail.handleMinimized(ev.windowID);
1352 return true;
1353 case SDL_EVENT_WINDOW_RESTORED:
1354 _rail.handleRestored(ev.windowID);
1355 return true;
1356 case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
1357 /* Send SC_CLOSE, don't abort the session. */
1358 _rail.handleClose(ev.windowID);
1359 return true;
1360 case SDL_EVENT_WINDOW_FOCUS_GAINED:
1361 _rail.handleFocus(ev.windowID, true);
1362 return true;
1363 case SDL_EVENT_WINDOW_FOCUS_LOST:
1364 _rail.handleFocus(ev.windowID, false);
1365 return true;
1366 case SDL_EVENT_WINDOW_DISPLAY_CHANGED:
1367 _rail.handleDisplayChanged(ev.windowID, static_cast<SDL_DisplayID>(ev.data1));
1368 return true;
1369 case SDL_EVENT_WINDOW_MOVED:
1370 _rail.notePopupMoved(ev.windowID);
1371 if (!_rail.dragActive(ev.windowID))
1372 _rail.syncGeometry(ev.windowID);
1373 return true;
1374 case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED:
1375 /* No server frame during the drag: repaint synchronously each configure step;
1376 * also report the new size to catch a compositor snap/tile. */
1377 _rail.noteDragResize(ev.windowID, ev.data1, ev.data2);
1378 _rail.syncGeometry(ev.windowID);
1379 _rail.noteResizeGrab(ev.windowID);
1380 std::ignore = drawToWindows({});
1381 return true;
1382 default:
1383 break;
1384 }
1385 }
1386 return true;
1387 }
1388
1389 {
1390 const auto& r = window->rect();
1391 const auto& b = window->bounds();
1392 const auto& scale = window->scale();
1393 const auto& orientation = window->orientation();
1394 SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION,
1395 "%s: [%u] %dx%d-%dx%d {%dx%d-%dx%d}{scale=%f,orientation=%s}",
1396 sdl::utils::toString(ev.type).c_str(), ev.windowID, r.x, r.y, r.w, r.h, b.x,
1397 b.y, b.w, b.h, static_cast<double>(scale),
1398 sdl::utils::toString(orientation).c_str());
1399 }
1400
1401 switch (ev.type)
1402 {
1403 case SDL_EVENT_WINDOW_MOUSE_ENTER:
1404 return restoreCursor();
1405 case SDL_EVENT_WINDOW_DISPLAY_SCALE_CHANGED:
1406 if (!resizeToScale(window))
1407 return false;
1409 if (isConnected())
1410 {
1411 if (!window->fill())
1412 return false;
1413 if (!drawToWindow(*window))
1414 return false;
1415 if (!restoreCursor())
1416 return false;
1417 }
1418 break;
1419 case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED:
1420 if (!resizeToScale(window))
1421 return false;
1422 if (!window->fill())
1423 return false;
1424 if (!drawToWindow(*window))
1425 return false;
1426 if (!restoreCursor())
1427 return false;
1429 break;
1430 case SDL_EVENT_WINDOW_MOVED:
1431 {
1432 auto r = window->rect();
1433 auto id = window->id();
1434 SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "%u: %dx%d-%dx%d", id, r.x, r.y, r.w, r.h);
1435 }
1436 break;
1437 case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
1438 {
1439 SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "Window closed, terminating RDP session...");
1440 std::ignore = abortConnect();
1441 }
1442 break;
1443 default:
1444 break;
1445 }
1446 return true;
1447}
1448
1449bool SdlContext::handleEvent(const SDL_DisplayEvent& ev)
1450{
1451 if (!getDisplayChannelContext().handleEvent(ev))
1452 return false;
1453#if SDL_VERSION_ATLEAST(3, 3, 2)
1454 if (ev.type == SDL_EVENT_DISPLAY_USABLE_BOUNDS_CHANGED)
1455 _rail.noteUsableBoundsChanged(ev.displayID);
1456#endif
1457
1458 switch (ev.type)
1459 {
1460 case SDL_EVENT_DISPLAY_REMOVED: // Can't show details for this one...
1461 break;
1462 default:
1463 {
1464 SDL_Rect r = {};
1465 if (!SDL_GetDisplayBounds(ev.displayID, &r))
1466 return false;
1467 const auto name = SDL_GetDisplayName(ev.displayID);
1468 if (!name)
1469 return false;
1470 const auto orientation = SDL_GetCurrentDisplayOrientation(ev.displayID);
1471 const auto scale = SDL_GetDisplayContentScale(ev.displayID);
1472 const auto mode = SDL_GetCurrentDisplayMode(ev.displayID);
1473 if (!mode)
1474 return false;
1475
1476 SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION,
1477 "%s: [%u, %s] %dx%d-%dx%d {orientation=%s, scale=%f}%s",
1478 sdl::utils::toString(ev.type).c_str(), ev.displayID, name, r.x, r.y, r.w,
1479 r.h, sdl::utils::toString(orientation).c_str(), static_cast<double>(scale),
1480 sdl::utils::toString(mode).c_str());
1481 }
1482 break;
1483 }
1484 return true;
1485}
1486
1487bool SdlContext::handleEvent(const SDL_MouseButtonEvent& ev)
1488{
1489 SDL_Event copy = {};
1490 copy.button = ev;
1491 if (_rail.enabled())
1492 {
1493 /* Suppress X11 raw button during WM op. */
1494 const bool suppress = _rail.dragActive(ev.windowID);
1495 if (ev.type == SDL_EVENT_MOUSE_BUTTON_UP)
1496 _rail.completeLocalMoveIfPending();
1497 /* Backstop for a Wayland resize whose MOUSE_ENTER completion never arrived; a fresh press
1498 * proves the old grab is over, so a no-op click grab is cancelled here too. */
1499 if (ev.type == SDL_EVENT_MOUSE_BUTTON_DOWN)
1500 _rail.completeWaylandResize(true);
1501 /* activate the clicked window before forwarding, so the click routes to it. */
1502 if (ev.type == SDL_EVENT_MOUSE_BUTTON_DOWN)
1503 _rail.ensureActive(ev.windowID);
1504 if (suppress)
1505 return true;
1506 if (ev.type == SDL_EVENT_MOUSE_BUTTON_UP)
1507 {
1508 _lastRelease = { ev.windowID, ev.timestamp };
1509 }
1510 if (_rail.translateToServer(ev.windowID, copy.button.x, copy.button.y))
1511 {
1512 /* The window may have moved since the last motion: keep the server's position. */
1513 if ((ev.windowID == _lastMotion.window) &&
1514 (_lastMotion.epoch == _rail.pointerEpoch()) &&
1515 (fabsf(ev.x - _lastMotion.local.x) < FLT_EPSILON) &&
1516 (fabsf(ev.y - _lastMotion.local.y) < FLT_EPSILON))
1517 {
1518 copy.button.x = _lastMotion.server.x;
1519 copy.button.y = _lastMotion.server.y;
1520 }
1521 /* Record the forwarded left press: it is the anchor of any server modal move/size
1522 * loop this press starts (the loop moves the window by release - anchor). */
1523 if ((ev.type == SDL_EVENT_MOUSE_BUTTON_DOWN) && (ev.button == SDL_BUTTON_LEFT))
1524 _rail.noteLeftPress(copy.button.x, copy.button.y);
1525 return SdlTouch::handleEvent(this, copy.button);
1526 }
1527 }
1528
1529 if (!getWindowForId(ev.windowID))
1530 return true;
1531 if (!eventToPixelCoordinates(ev.windowID, copy))
1532 return true;
1533 removeLocalScaling(copy.button.x, copy.button.y);
1534 applyMonitorOffset(copy.button.windowID, copy.button.x, copy.button.y);
1535 return SdlTouch::handleEvent(this, copy.button);
1536}
1537
1538bool SdlContext::handleEvent(const SDL_TouchFingerEvent& ev)
1539{
1540 if (!getWindowForId(ev.windowID))
1541 return true;
1542 SDL_Event copy{};
1543 copy.tfinger = ev;
1544 if (!eventToPixelCoordinates(ev.windowID, copy))
1545 return true;
1546 removeLocalScaling(copy.tfinger.dx, copy.tfinger.dy);
1547 removeLocalScaling(copy.tfinger.x, copy.tfinger.y);
1548 applyMonitorOffset(copy.tfinger.windowID, copy.tfinger.x, copy.tfinger.y);
1549 return SdlTouch::handleEvent(this, copy.tfinger);
1550}
1551
1552void SdlContext::addOrUpdateDisplay(SDL_DisplayID id)
1553{
1554 auto monitor = SdlWindow::query(id, false);
1555 if ((monitor.width <= 0) || (monitor.height <= 0))
1556 {
1557 WLog_Print(getWLog(), WLOG_WARN, "display %" PRIu32 " has no usable size, skipped", id);
1558 return;
1559 }
1560 _displays.emplace(id, monitor);
1561
1562 /* The logical bounds and the primary are read here, on the thread SDL display functions
1563 * must be called from; updateDisplayOffsets() also runs when the selection is set during
1564 * connect, on another thread. */
1565 _displayBounds.clear();
1566 for (const auto& entry : _displays)
1567 {
1568 SDL_Rect bounds{};
1569 std::ignore = SDL_GetDisplayBounds(entry.first, &bounds);
1570 _displayBounds.emplace(entry.first, bounds);
1571 }
1572 _primaryDisplay = SDL_GetPrimaryDisplay();
1573 updateDisplayOffsets();
1574 updateMonitorDataFromOffsets();
1575}
1576
1577void SdlContext::updateDisplayOffsets()
1578{
1579 /* Update actual display rectangles from the logical display bounds and the known pixel
1580 * width and height of each display. */
1581 _offsets.clear();
1582 for (const auto& entry : _displays)
1583 {
1584 SDL_Rect bounds{};
1585 const auto found = _displayBounds.find(entry.first);
1586 if (found != _displayBounds.end())
1587 bounds = found->second;
1588
1589 SDL_Rect pixel{};
1590 pixel.w = entry.second.width;
1591 pixel.h = entry.second.height;
1592 _offsets.emplace(entry.first, std::pair{ bounds, pixel });
1593 }
1594
1595 if (_offsets.empty())
1596 return;
1597 sdl_display_offsets::update(_offsets, _primaryDisplay, _monitorIds);
1598 for (auto& entry : _displays)
1599 {
1600 const auto& pixel = _offsets.at(entry.first).second;
1601 entry.second.x = pixel.x;
1602 entry.second.y = pixel.y;
1603 }
1604}
1605
1606void SdlContext::deleteDisplay(SDL_DisplayID id)
1607{
1608 _displays.erase(id);
1609}
1610
1611bool SdlContext::eventToPixelCoordinates(SDL_WindowID id, SDL_Event& ev)
1612{
1613 auto w = getWindowForId(id);
1614 if (!w)
1615 return false;
1616
1617 /* Ignore errors here, sometimes SDL has no renderer */
1618 auto renderer = w->renderer();
1619 if (!renderer)
1620 return true;
1621 return SDL_ConvertEventToRenderCoordinates(renderer, &ev);
1622}
1623
1624SDL_FPoint SdlContext::applyLocalScaling(const SDL_FPoint& val) const
1625{
1626 if (!useLocalScale())
1627 return val;
1628
1629 auto rval = val;
1630 rval.x *= _localScale.x;
1631 rval.y *= _localScale.y;
1632 return rval;
1633}
1634
1635void SdlContext::removeLocalScaling(float& x, float& y) const
1636{
1637 if (!useLocalScale())
1638 return;
1639 x /= _localScale.x;
1640 y /= _localScale.y;
1641}
1642
1643SDL_FPoint SdlContext::screenToPixel(SDL_WindowID id, const SDL_FPoint& pos)
1644{
1645 auto w = getWindowForId(id);
1646 if (!w)
1647 {
1648 /* RAIL windows are server-authoritative 1:1 (no local scaling) and not in _windows. */
1649 if (_rail.enabled() && _rail.ownsWindow(id))
1650 return pos;
1651 return {};
1652 }
1653
1654 /* Ignore errors here, sometimes SDL has no renderer */
1655 auto renderer = w->renderer();
1656 if (!renderer)
1657 return pos;
1658
1659 SDL_FPoint rpos{};
1660 if (!SDL_RenderCoordinatesFromWindow(renderer, pos.x, pos.y, &rpos.x, &rpos.y))
1661 return {};
1662 removeLocalScaling(rpos.x, rpos.y);
1663 return rpos;
1664}
1665
1666SDL_FPoint SdlContext::pixelToScreen(SDL_WindowID id, const SDL_FPoint& pos)
1667{
1668 auto w = getWindowForId(id);
1669 if (!w)
1670 {
1671 if (_rail.enabled() && _rail.ownsWindow(id))
1672 return pos;
1673 return {};
1674 }
1675
1676 /* Ignore errors here, sometimes SDL has no renderer */
1677 auto renderer = w->renderer();
1678 if (!renderer)
1679 return pos;
1680
1681 SDL_FPoint rpos{};
1682 if (!SDL_RenderCoordinatesToWindow(renderer, pos.x, pos.y, &rpos.x, &rpos.y))
1683 return {};
1684 return applyLocalScaling(rpos);
1685}
1686
1687SDL_FRect SdlContext::pixelToScreen(SDL_WindowID id, const SDL_FRect& pos, bool round)
1688{
1689 const auto fpos = pixelToScreen(id, SDL_FPoint{ pos.x, pos.y });
1690 const auto size = pixelToScreen(id, SDL_FPoint{ pos.w, pos.h });
1691 SDL_FRect r{ fpos.x, fpos.y, size.x, size.y };
1692 if (round)
1693 {
1694 r.w = std::ceil(r.w);
1695 r.h = std::ceil(r.h);
1696 r.x = std::floor(r.x);
1697 r.y = std::floor(r.y);
1698 }
1699 return r;
1700}
1701
1702bool SdlContext::handleEvent(const SDL_Event& ev)
1703{
1704 if ((ev.type >= SDL_EVENT_DISPLAY_FIRST) && (ev.type <= SDL_EVENT_DISPLAY_LAST))
1705 {
1706 const auto& dev = ev.display;
1707 return handleEvent(dev);
1708 }
1709 if ((ev.type >= SDL_EVENT_WINDOW_FIRST) && (ev.type <= SDL_EVENT_WINDOW_LAST))
1710 {
1711 const auto& wev = ev.window;
1712 return handleEvent(wev);
1713 }
1714 switch (ev.type)
1715 {
1716 case SDL_EVENT_RENDER_TARGETS_RESET:
1717 case SDL_EVENT_RENDER_DEVICE_RESET:
1718 case SDL_EVENT_WILL_ENTER_FOREGROUND:
1719 return redraw();
1720 default:
1721 break;
1722 }
1723
1724 if (!isConnected())
1725 return true;
1726
1727 switch (ev.type)
1728 {
1729 case SDL_EVENT_FINGER_DOWN:
1730 case SDL_EVENT_FINGER_UP:
1731 case SDL_EVENT_FINGER_MOTION:
1732 {
1733 const auto& cev = ev.tfinger;
1734 return handleEvent(cev);
1735 }
1736 case SDL_EVENT_MOUSE_MOTION:
1737
1738 {
1739 const auto& cev = ev.motion;
1740 return handleEvent(cev);
1741 }
1742 case SDL_EVENT_MOUSE_BUTTON_DOWN:
1743 case SDL_EVENT_MOUSE_BUTTON_UP:
1744 {
1745 const auto& cev = ev.button;
1746 return handleEvent(cev);
1747 }
1748 case SDL_EVENT_MOUSE_WHEEL:
1749 {
1750 const auto& cev = ev.wheel;
1751 return handleEvent(cev);
1752 }
1753 case SDL_EVENT_CLIPBOARD_UPDATE:
1754 {
1755 const auto& cev = ev.clipboard;
1756 return getClipboardChannelContext().handleEvent(cev);
1757 }
1758 case SDL_EVENT_KEY_DOWN:
1759 case SDL_EVENT_KEY_UP:
1760 {
1761 const auto& cev = ev.key;
1762 return getInputChannelContext().handleEvent(cev);
1763 }
1764 default:
1765 return true;
1766 }
1767}
1768
1769COMMAND_LINE_ARGUMENT_A* SdlContext::args()
1770{
1771 return _args.data();
1772}
1773
1774size_t SdlContext::argsCount() const
1775{
1776 if (_args.size() <= 1)
1777 return 0;
1778 return _args.size() - 1;
1779}
1780
1781int SdlContext::argumentHandler(const COMMAND_LINE_ARGUMENT_A* arg, void* custom)
1782{
1783 auto sdl = static_cast<SdlContext*>(custom);
1784 if (!sdl)
1785 return -1;
1786
1787 if (arg->Name)
1788 {
1789 if (strcmp(arg->Name, sdl_allow_screensaver) == 0)
1790 {
1791 sdl->_allowScreenSaver = (arg->Value != nullptr);
1792 if (arg->Value != nullptr)
1793 {
1794 if (!SDL_SetHint(SDL_HINT_VIDEO_ALLOW_SCREENSAVER, "1"))
1795 {
1796 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
1797 "SDL_SetHint(SDL_HINT_VIDEO_ALLOW_SCREENSAVER) failed with %s",
1798 SDL_GetError());
1799 return -2;
1800 }
1801 }
1802 }
1803 else if (strcmp(arg->Name, sdl_connection_dialog) == 0)
1804 sdl->_connectionDialog = (arg->Value != nullptr);
1805 else if (strcmp(arg->Name, sdl_info) == 0)
1806 {
1807 if (!sdl->_overlay.show(arg->Value != nullptr))
1808 return -2;
1809 }
1810 }
1811 return 0;
1812}
1813
1814CriticalSection& SdlContext::lock()
1815{
1816 return _critical;
1817}
1818
1819std::vector<rdpPointer*>& SdlContext::pointers()
1820{
1821 return _valid_pointers;
1822}
1823
1824bool SdlContext::contains(const rdpPointer* ptr) const
1825{
1826 for (const auto& cur : _valid_pointers)
1827 {
1828 if (cur == ptr)
1829 return true;
1830 }
1831 return false;
1832}
1833
1834bool SdlContext::credentialsRead() const
1835{
1836 return _credentialsRead;
1837}
1838
1839void SdlContext::setCredentialsRead()
1840{
1841 _credentialsRead = true;
1842}
1843
1844SdlWheelAxis& SdlContext::wheelAxis(bool horizontal)
1845{
1846 return horizontal ? _wheelX : _wheelY;
1847}
1848
1849bool SdlContext::resizeToScale(SdlWindow* window)
1850{
1851 if (freerdp_settings_get_bool(context()->settings, FreeRDP_SmartSizing))
1852 return true;
1853 if (!useLocalScale())
1854 return true;
1855 if (!window)
1856 return false;
1857 return window->resizeToScale();
1858}
1859
1860bool SdlContext::useLocalScale() const
1861{
1862 const auto ssize = freerdp_settings_get_bool(context()->settings, FreeRDP_SmartSizing);
1863 if (ssize)
1864 return true;
1865 const auto dynResize =
1866 freerdp_settings_get_bool(context()->settings, FreeRDP_DynamicResolutionUpdate);
1867 const auto fs = freerdp_settings_get_bool(context()->settings, FreeRDP_Fullscreen);
1868 const auto multimon = freerdp_settings_get_bool(context()->settings, FreeRDP_UseMultimon);
1869 return !dynResize && !fs && !multimon;
1870}
1871
1872bool SdlContext::drawToWindows(const std::vector<SDL_Rect>& rects)
1873{
1874 /* RAIL damage is per-window (_gfxDamage), not in the rects queue: repaint every tick. */
1875 if (_rail.enabled())
1876 {
1877 for (auto& window : _windows)
1878 {
1879 if ((SDL_GetWindowFlags(window.second.window()) & SDL_WINDOW_HIDDEN) == 0)
1880 SDL_HideWindow(window.second.window());
1881 }
1882
1883 std::unique_lock lock(_critical);
1884 _rail.paint(_primary.get(), pixelFormat(), rects);
1885 return true;
1886 }
1887
1888 /* Non-RAIL mode (e.g. Non-Monitored Desktop during UAC / Consent UI): show the desktop window.
1889 * Before the RemoteApp starts, only when the logon needs the user. */
1890 if (freerdp_settings_get_bool(context()->settings, FreeRDP_RemoteApplicationMode) &&
1891 !_rail.desktopMayShow())
1892 return true;
1893 bool firstShow = false;
1894 for (auto& window : _windows)
1895 {
1896 if (SDL_GetWindowFlags(window.second.window()) & SDL_WINDOW_HIDDEN)
1897 {
1898 SDL_ShowWindow(window.second.window());
1899 SDL_RaiseWindow(window.second.window());
1900 firstShow = true;
1901 }
1902 }
1903
1904 if (rects.empty() && !firstShow)
1905 return true;
1906
1907 std::vector<SDL_Rect> drawRects = rects;
1908 {
1909 std::unique_lock lock(_critical);
1910 if ((drawRects.empty() || firstShow) && _primary)
1911 drawRects = { { 0, 0, _primary->w, _primary->h } };
1912 }
1913
1914 for (auto& window : _windows)
1915 {
1916 if (!drawToWindow(window.second, drawRects))
1917 return false;
1918 }
1919
1920 return true;
1921}
1922
1923BOOL SdlContext::desktopResize(rdpContext* context)
1924{
1925 rdpGdi* gdi = nullptr;
1926 rdpSettings* settings = nullptr;
1927 auto sdl = get_context(context);
1928
1929 WINPR_ASSERT(sdl);
1930 WINPR_ASSERT(context);
1931
1932 settings = context->settings;
1933 WINPR_ASSERT(settings);
1934
1935 std::unique_lock lock(sdl->_critical);
1936 gdi = context->gdi;
1937 if (!gdi_resize(gdi, freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth),
1938 freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight)))
1939 return FALSE;
1940 return sdl->createPrimary();
1941}
1942
1943/* This function is called to output a System BEEP */
1944BOOL SdlContext::playSound(rdpContext* context, const PLAY_SOUND_UPDATE* play_sound)
1945{
1946 /* TODO: Implement */
1947 WINPR_UNUSED(context);
1948 WINPR_UNUSED(play_sound);
1949 return TRUE;
1950}
1951
1952/* This function is called whenever a new frame starts.
1953 * It can be used to reset invalidated areas. */
1954BOOL SdlContext::beginPaint(rdpContext* context)
1955{
1956 auto gdi = context->gdi;
1957 WINPR_ASSERT(gdi);
1958 WINPR_ASSERT(gdi->primary);
1959
1960 HGDI_DC hdc = gdi->primary->hdc;
1961 WINPR_ASSERT(hdc);
1962 if (!hdc->hwnd)
1963 return TRUE;
1964
1965 HGDI_WND hwnd = hdc->hwnd;
1966 WINPR_ASSERT(hwnd->invalid);
1967 hwnd->invalid->null = TRUE;
1968 hwnd->ninvalid = 0;
1969
1970 return TRUE;
1971}
1972
1973bool SdlContext::redraw(bool suppress) const
1974{
1975 if (!_connected)
1976 return true;
1977
1978 /* RemoteApp desktop windows are placeholders, hiding one must not stop server output. */
1979 if (suppress && freerdp_settings_get_bool(context()->settings, FreeRDP_RemoteApplicationMode))
1980 return true;
1981
1982 auto gdi = context()->gdi;
1983 WINPR_ASSERT(gdi);
1984 return gdi_send_suppress_output(gdi, suppress);
1985}
1986
1987void SdlContext::setConnected(bool val)
1988{
1989 _connected = val;
1990}
1991
1992bool SdlContext::isConnected() const
1993{
1994 return _connected;
1995}
1996
1997/* Stop requested by the user. Overwrite any earlier error with CONNECT_CANCELLED,
1998 * otherwise an auto reconnect ignores the stop. */
1999bool SdlContext::abortConnect() const
2000{
2001 freerdp_set_last_error(context(), FREERDP_ERROR_CONNECT_CANCELLED);
2002 return freerdp_abort_connect_context(context());
2003}
2004
2005rdpContext* SdlContext::context() const
2006{
2007 WINPR_ASSERT(_context);
2008 return _context;
2009}
2010
2011rdpClientContext* SdlContext::common() const
2012{
2013 return reinterpret_cast<rdpClientContext*>(context());
2014}
2015
2016bool SdlContext::setCursor(CursorType type)
2017{
2018 _cursorType = type;
2019 return restoreCursor();
2020}
2021
2022bool SdlContext::setCursor(const rdpPointer* cursor)
2023{
2024 std::unique_lock lock(_critical);
2025 if (!contains(cursor))
2026 return true;
2027
2028 _cursor = { sdl_Pointer_Copy(cursor), sdl_PointerFreeCopyAll };
2029 return setCursor(CURSOR_IMAGE);
2030}
2031
2032rdpPointer* SdlContext::cursor() const
2033{
2034 return _cursor.get();
2035}
2036
2037bool SdlContext::restoreCursor()
2038{
2039 WLog_Print(getWLog(), WLOG_TRACE, "restore cursor: %d", _cursorType);
2040 switch (_cursorType)
2041 {
2042 case CURSOR_NULL:
2043 if (!SDL_HideCursor())
2044 {
2045 WLog_Print(getWLog(), WLOG_ERROR, "SDL_HideCursor failed");
2046 return false;
2047 }
2048
2049 setHasCursor(false);
2050 return true;
2051
2052 case CURSOR_DEFAULT:
2053 {
2054 auto def = SDL_GetDefaultCursor();
2055 if (!SDL_SetCursor(def))
2056 {
2057 WLog_Print(getWLog(), WLOG_ERROR, "SDL_SetCursor(default=%p) failed",
2058 static_cast<void*>(def));
2059 return false;
2060 }
2061 if (!SDL_ShowCursor())
2062 {
2063 WLog_Print(getWLog(), WLOG_ERROR, "SDL_ShowCursor failed");
2064 return false;
2065 }
2066 setHasCursor(true);
2067 return true;
2068 }
2069 case CURSOR_IMAGE:
2070 setHasCursor(true);
2071 return sdl_Pointer_Set_Process(this);
2072 default:
2073 WLog_Print(getWLog(), WLOG_ERROR, "Unknown cursorType %s",
2074 sdl::utils::toString(_cursorType).c_str());
2075 return false;
2076 }
2077}
2078
2079void SdlContext::setMonitorIds(const std::vector<SDL_DisplayID>& ids)
2080{
2081 _monitorIds.clear();
2082 for (auto id : ids)
2083 {
2084 _monitorIds.push_back(id);
2085 }
2086 /* Only the selected displays are sent, so only they need to fit together. This runs during
2087 * connect, outside the thread that owns the windows; they are not touched here and are
2088 * created from the updated displays. */
2089 updateDisplayOffsets();
2090}
2091
2092const std::vector<SDL_DisplayID>& SdlContext::monitorIds() const
2093{
2094 return _monitorIds;
2095}
2096
2097int64_t SdlContext::monitorId(uint32_t index) const
2098{
2099 if (index >= _monitorIds.size())
2100 {
2101 return -1;
2102 }
2103 return _monitorIds.at(index);
2104}
2105
2106void SdlContext::push(std::vector<SDL_Rect>&& rects)
2107{
2108 std::unique_lock lock(_queue_mux);
2109 _queue.emplace(std::move(rects));
2110}
2111
2112std::vector<SDL_Rect> SdlContext::pop()
2113{
2114 std::unique_lock lock(_queue_mux);
2115 if (_queue.empty())
2116 {
2117 return {};
2118 }
2119 auto val = std::move(_queue.front());
2120 _queue.pop();
2121 return val;
2122}
2123
2124bool SdlContext::setFullscreen(bool enter, bool forceOriginalDisplay)
2125{
2126 for (const auto& window : _windows)
2127 {
2128 if (!sdl_push_user_event(SDL_EVENT_USER_WINDOW_FULLSCREEN, &window.second, enter,
2129 forceOriginalDisplay))
2130 return false;
2131 }
2132 _fullscreen = enter;
2133 return true;
2134}
2135
2136bool SdlContext::setMinimized()
2137{
2138 return sdl_push_user_event(SDL_EVENT_USER_WINDOW_MINIMIZE);
2139}
2140
2141bool SdlContext::grabMouse() const
2142{
2143 return _grabMouse;
2144}
2145
2146bool SdlContext::toggleGrabMouse()
2147{
2148 return setGrabMouse(!grabMouse());
2149}
2150
2151bool SdlContext::setGrabMouse(bool enter)
2152{
2153 _grabMouse = enter;
2154 return true;
2155}
2156
2157bool SdlContext::grabKeyboard() const
2158{
2159 return _grabKeyboard;
2160}
2161
2162bool SdlContext::toggleGrabKeyboard()
2163{
2164 return setGrabKeyboard(!grabKeyboard());
2165}
2166
2167bool SdlContext::setGrabKeyboard(bool enter)
2168{
2169 _grabKeyboard = enter;
2170 return true;
2171}
2172
2173bool SdlContext::setResizeable(bool enable)
2174{
2175 const auto settings = context()->settings;
2176 const bool dyn = freerdp_settings_get_bool(settings, FreeRDP_DynamicResolutionUpdate);
2177 const bool smart = freerdp_settings_get_bool(settings, FreeRDP_SmartSizing);
2178 bool use = (dyn && enable) || smart;
2179
2180 for (const auto& window : _windows)
2181 {
2182 if (!sdl_push_user_event(SDL_EVENT_USER_WINDOW_RESIZEABLE, &window.second, use))
2183 return false;
2184 }
2185 _resizeable = use;
2186
2187 return true;
2188}
2189
2190bool SdlContext::resizeable() const
2191{
2192 return _resizeable;
2193}
2194
2195bool SdlContext::toggleResizeable()
2196{
2197 return setResizeable(!resizeable());
2198}
2199
2200bool SdlContext::fullscreen() const
2201{
2202 return _fullscreen;
2203}
2204
2205bool SdlContext::toggleFullscreen()
2206{
2207 return setFullscreen(!fullscreen());
2208}
2209
2210/* ---- rdpContext::windowInfoService ---- */
2211
2212static bool window_info_equal(const WTS_WINDOW_INFO& a, const WTS_WINDOW_INFO& b)
2213{
2214 const bool scale = fabsf(a.scale - b.scale) <= FLT_EPSILON;
2215 return (a.hwnd == b.hwnd) && (a.remoteWidth == b.remoteWidth) &&
2216 (a.remoteHeight == b.remoteHeight) && (a.viewWidth == b.viewWidth) &&
2217 (a.viewHeight == b.viewHeight) && (a.scrollX == b.scrollX) && (a.scrollY == b.scrollY) &&
2218 scale;
2219}
2220
2222static HWND native_window(SDL_Window* window)
2223{
2224 if (!window)
2225 return nullptr;
2226 auto props = SDL_GetWindowProperties(window);
2227#if defined(_WIN32)
2228 return static_cast<HWND>(
2229 SDL_GetPointerProperty(props, SDL_PROP_WINDOW_WIN32_HWND_POINTER, nullptr));
2230#else
2231 return reinterpret_cast<HWND>(
2232 static_cast<uintptr_t>(SDL_GetNumberProperty(props, SDL_PROP_WINDOW_X11_WINDOW_NUMBER, 0)));
2233#endif
2234}
2235
2237{
2238 WTS_WINDOW_INFO info{};
2239 auto window = getFirstWindow();
2240 if (window && window->window())
2241 {
2242 auto settings = _context->settings;
2243 info.hwnd = native_window(window->window());
2244 info.remoteWidth =
2245 static_cast<INT32>(freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth));
2246 info.remoteHeight =
2247 static_cast<INT32>(freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight));
2248 int w = 0;
2249 int h = 0;
2250 if (SDL_GetWindowSizeInPixels(window->window(), &w, &h))
2251 {
2252 info.viewWidth = w;
2253 info.viewHeight = h;
2254 }
2255 /* session pixels to native pixels */
2256 const float density = SDL_GetWindowPixelDensity(window->window());
2257 const auto p0 = pixelToScreen(window->id(), SDL_FPoint{ 0, 0 });
2258 const auto p1 = pixelToScreen(window->id(), SDL_FPoint{ 1000, 1000 });
2259 const float scale = (p1.x - p0.x) * density / 1000.0f;
2260 info.scale = (scale > 0.0f) ? scale : 1.0f;
2261 }
2262
2263 {
2264 std::scoped_lock lock(_windowInfoLock);
2265 if (window_info_equal(info, _desktopWindowInfo))
2266 return;
2267 _desktopWindowInfo = info;
2268 }
2269 if (!_rail.enabled())
2270 freerdp_client_window_info_service_changed(_context->windowInfoService, 0);
2271}
2272
2274{
2275 bool first = false;
2276 {
2277 std::scoped_lock lock(_windowInfoLock);
2278 first = _railWindowInfoPending.empty();
2279 _railWindowInfoPending.insert(remoteId);
2280 }
2281 if (first)
2282 std::ignore = sdl_push_user_event(SDL_EVENT_USER_WINDOW_INFO);
2283}
2284
2286{
2287 std::set<uint64_t> pending;
2288 {
2289 std::scoped_lock lock(_windowInfoLock);
2290 pending.swap(_railWindowInfoPending);
2291 }
2292
2293 for (const auto id : pending)
2294 {
2295 WTS_WINDOW_INFO info{};
2296 SDL_Window* window = nullptr;
2297 /* RemoteApp windows are drawn 1:1: the same remote and view size, scale left at 0 */
2298 if (_rail.windowGeometry(id, window, info.viewWidth, info.viewHeight))
2299 {
2300 info.hwnd = native_window(window);
2301 info.remoteWidth = info.viewWidth;
2302 info.remoteHeight = info.viewHeight;
2303 }
2304 {
2305 std::scoped_lock lock(_windowInfoLock);
2306 if (info.viewWidth > 0)
2307 _railWindowInfo[id] = info;
2308 else
2309 _railWindowInfo.erase(id);
2310 }
2311 if (info.viewWidth > 0)
2312 freerdp_client_window_info_service_changed(_context->windowInfoService, id);
2313 }
2314}
2315
2316HRESULT SdlContext::windowInfoGet(void* custom, UINT64 remoteHwnd, WTS_WINDOW_INFO* info)
2317{
2318 auto sdl = static_cast<SdlContext*>(custom);
2319 WINPR_ASSERT(sdl);
2320 WINPR_ASSERT(info);
2321
2322 std::scoped_lock lock(sdl->_windowInfoLock);
2323 if (!sdl->_rail.enabled())
2324 {
2325 /* remoteHwnd only means something in RemoteApp */
2326 if (!sdl->_desktopWindowInfo.hwnd && (sdl->_desktopWindowInfo.viewWidth == 0))
2327 return E_UNEXPECTED; /* no window yet */
2328 *info = sdl->_desktopWindowInfo;
2329 return S_OK;
2330 }
2331 auto it = sdl->_railWindowInfo.find(remoteHwnd);
2332 if (it == sdl->_railWindowInfo.end())
2333 return E_INVALIDARG;
2334 *info = it->second;
2335 return S_OK;
2336}
2337
2338UINT32 SdlContext::windowInfoSessionType(void* custom)
2339{
2340 auto sdl = static_cast<SdlContext*>(custom);
2341 WINPR_ASSERT(sdl);
2342 return sdl->_rail.enabled() ? WTS_RDP_SESSION_TYPE_REMOTEAPP : WTS_RDP_SESSION_TYPE_DESKTOP;
2343}
2344
2345/* ---- mapped renderers (rdpContext::bitmapRenderService): the channels' calls come from their
2346 * threads, the textures are the main thread's ---- */
2347
2348Uint64 SdlContext::rendererAdd(UINT64 mappingId, IWTSBitmapRendererCallback* callback)
2349{
2350 std::scoped_lock lock(_rendererLock);
2351 const auto id = _nextRenderer++;
2352 auto& r = _renderers[id];
2353 r.mappingId = mappingId;
2354 r.callback = callback;
2355 if (callback)
2356 callback->AddRef();
2357 return id;
2358}
2359
2360bool SdlContext::rendererFrame(Uint64 id, const BYTE* data, UINT32 width, UINT32 height,
2361 LONG stride)
2362{
2363 if (stride <= 0)
2364 return false;
2365
2366 {
2367 std::scoped_lock lock(_rendererLock);
2368 auto it = _renderers.find(id);
2369 if (it == _renderers.end())
2370 return false;
2371 auto& r = it->second;
2372 /* Render's buffer is the caller's for the call only */
2373 const size_t row = static_cast<size_t>(width) * 4;
2374 r.pixels.resize(row * height);
2375 for (UINT32 y = 0; y < height; y++)
2376 {
2377 const auto offset = row * y;
2378 const auto end = offset + row;
2379 const auto dataoffset = 1ull * y * static_cast<size_t>(stride);
2380
2381 try
2382 {
2383 auto& ptr = r.pixels.at(offset);
2384 std::ignore = r.pixels.at(end);
2385 memcpy(&ptr, &data[dataoffset], row);
2386 }
2387 catch (std::out_of_range& ex)
2388 {
2389 SDL_Log("failed to render: %s", ex.what());
2390 return false;
2391 }
2392 }
2393 r.pitch = static_cast<int>(row);
2394 r.width = static_cast<int>(width);
2395 r.height = static_cast<int>(height);
2396 r.dirty = true;
2397 }
2398 renderersChanged();
2399 return true;
2400}
2401
2402void SdlContext::rendererRemove(Uint64 id)
2403{
2404 IWTSBitmapRendererCallback* callback = nullptr;
2405 {
2406 std::scoped_lock lock(_rendererLock);
2407 auto it = _renderers.find(id);
2408 if (it == _renderers.end())
2409 return;
2410 callback = it->second.callback;
2411 _renderers.erase(it);
2412 }
2413 if (callback)
2414 callback->Release();
2415 renderersChanged();
2416}
2417
2418bool SdlContext::hasRenderers()
2419{
2420 std::scoped_lock lock(_rendererLock);
2421 return !_renderers.empty();
2422}
2423
2424void SdlContext::renderersChanged()
2425{
2426 /* one pending event at a time: frames coming faster than we draw are coalesced */
2427 if (!_renderersPending.exchange(true))
2428 std::ignore = sdl_push_user_event(SDL_EVENT_USER_RENDERER);
2429}
2430
2432{
2433 _renderersPending = false;
2434 if (!isConnected())
2435 return true;
2436
2437 /* OnTargetSizeChanged() calls, made once _rendererLock is released */
2438 std::vector<std::pair<IWTSBitmapRendererCallback*, WTS_RECT>> targets;
2439 {
2440 std::scoped_lock lock(_rendererLock);
2441
2442 /* a renderer is placed by its mapping's geometry (MS-RDPEGT), hidden without one */
2443 struct Placement
2444 {
2445 bool visible = false;
2446 SDL_Rect rect{};
2447 std::vector<SDL_Rect> region; /* the visible parts, empty: all of it */
2448 };
2449 std::map<Uint64, Placement> placements;
2450 rdpGdi* gdi = _context->gdi;
2451 GeometryClientContext* geometry = gdi ? gdi->geometry : nullptr;
2452 if (geometry && geometry->geometries)
2453 {
2454 HashTable_Lock(geometry->geometries);
2455 for (auto& kv : _renderers)
2456 {
2457 auto g = static_cast<const MAPPED_GEOMETRY*>(
2458 HashTable_GetItemValue(geometry->geometries, &kv.second.mappingId));
2459 if (!g)
2460 continue;
2461 Placement p;
2462 p.rect = { static_cast<int>(g->topLevelLeft + g->left),
2463 static_cast<int>(g->topLevelTop + g->top),
2464 static_cast<int>(std::max<INT64>(0, g->right - g->left)),
2465 static_cast<int>(std::max<INT64>(0, g->bottom - g->top)) };
2466 p.visible = (p.rect.w > 0) && (p.rect.h > 0);
2467 /* the region's rectangles are relative to the tracked rectangle */
2468 INT64 area = 0;
2469 for (UINT32 i = 0; g->geometry.rects && (i < g->geometry.nRectCount); i++)
2470 {
2471 const RDP_RECT& r = g->geometry.rects[i];
2472 const int left = std::max(0, static_cast<int>(r.x));
2473 const int top = std::max(0, static_cast<int>(r.y));
2474 const int right = std::min(p.rect.w, r.x + r.width);
2475 const int bottom = std::min(p.rect.h, r.y + r.height);
2476 if ((right <= left) || (bottom <= top))
2477 continue;
2478 area += static_cast<INT64>(right - left) * (bottom - top);
2479 p.region.push_back(
2480 { p.rect.x + left, p.rect.y + top, right - left, bottom - top });
2481 }
2482 if (area >= static_cast<INT64>(p.rect.w) * p.rect.h)
2483 p.region.clear();
2484 placements[kv.first] = std::move(p);
2485 }
2486 HashTable_Unlock(geometry->geometries);
2487 }
2488
2489 for (auto& kv : _renderers)
2490 {
2491 auto& r = kv.second;
2492 const auto& p = placements[kv.first];
2493 if (p.visible && r.callback && !SDL_RectsEqual(&p.rect, &r.target))
2494 {
2495 r.target = p.rect;
2496 r.callback->AddRef();
2497 targets.emplace_back(r.callback, WTS_RECT{ p.rect.x, p.rect.y, p.rect.x + p.rect.w,
2498 p.rect.y + p.rect.h });
2499 }
2500 }
2501
2502 for (auto& w : _windows)
2503 {
2504 auto& window = w.second;
2505 window.pruneVideoOverlays([this](Uint64 id) { return _renderers.count(id) != 0; });
2506 for (auto& kv : _renderers)
2507 {
2508 auto& r = kv.second;
2509 const auto& p = placements[kv.first];
2510 /* where the session is drawn in the window: as drawToWindow() does */
2511 SDL_Point offset{ 0, 0 };
2512 if (!useLocalScale() &&
2513 freerdp_settings_get_bool(context()->settings, FreeRDP_UseMultimon))
2514 offset = { window.offsetX(), window.offsetY() };
2515 auto toWindow = [&](const SDL_Rect& rc)
2516 {
2517 if (useLocalScale())
2518 return SDL_FRect{ static_cast<float>(rc.x) * _localScale.x,
2519 static_cast<float>(rc.y) * _localScale.y,
2520 static_cast<float>(rc.w) * _localScale.x,
2521 static_cast<float>(rc.h) * _localScale.y };
2522 return SDL_FRect{ static_cast<float>(rc.x + offset.x),
2523 static_cast<float>(rc.y + offset.y), static_cast<float>(rc.w),
2524 static_cast<float>(rc.h) };
2525 };
2526 const SDL_FRect dst = toWindow(p.rect);
2527 std::vector<SDL_FRect> clip;
2528 clip.reserve(p.region.size());
2529 for (const auto& rc : p.region)
2530 clip.push_back(toWindow(rc));
2531 const bool visible = p.visible && !r.pixels.empty();
2532 if (!window.setVideoOverlay(kv.first, r.dirty ? r.pixels.data() : nullptr, r.pitch,
2533 r.width, r.height, dst, clip, visible))
2534 continue;
2535 }
2536 window.updateSurface();
2537 }
2538 for (auto& kv : _renderers)
2539 kv.second.dirty = false;
2540 }
2541
2542 for (auto& t : targets)
2543 {
2544 std::ignore = t.first->OnTargetSizeChanged(t.second);
2545 t.first->Release();
2546 }
2547 return true;
2548}
void flushWindowInfo()
void railWindowInfoChanged(uint64_t remoteId)
void updateWindowInfo()
bool presentRenderers()
bool toggleOverlay()
bool show(bool on)
object that handles clipboard context for the SDL3 client
Definition sdl_clip.hpp:78
WINPR_ATTR_NODISCARD FREERDP_API const char * freerdp_settings_get_server_name(const rdpSettings *settings)
A helper function to return the correct server name.
WINPR_ATTR_NODISCARD FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_bool(rdpSettings *settings, FreeRDP_Settings_Keys_Bool id, BOOL val)
Sets a BOOL settings value.
WINPR_ATTR_NODISCARD FREERDP_API UINT64 freerdp_settings_get_uint64(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt64 id)
Returns a UINT64 settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 val)
Sets a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_monitor_def_array_sorted(rdpSettings *settings, const rdpMonitor *monitors, size_t count)
Sort monitor array according to:
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
what the client gives the service to answer from
same layout as Win32's RECT, which winpr doesn't have on POSIX
40 bytes on x64