FreeRDP
Loading...
Searching...
No Matches
sdl_context.cpp
1
20#include <algorithm>
21
22#include "sdl_context.hpp"
23#include "sdl_config.hpp"
24#include "sdl_channels.hpp"
25#include "sdl_monitor.hpp"
26#include "sdl_pointer.hpp"
27#include "sdl_touch.hpp"
28
29#include <sdl_common_utils.hpp>
30#include <scoped_guard.hpp>
31
32#include "dialogs/sdl_dialogs.hpp"
33
34#if defined(WITH_WEBVIEW)
35#include <aad/sdl_webview.hpp>
36#endif
37
38SdlContext::SdlContext(rdpContext* context)
39 : _context(context), _log(WLog_Get(CLIENT_TAG("SDL"))), _rdpThreadRunning(false),
40 _primary(nullptr, SDL_DestroySurface), _disp(this), _input(this), _clip(this), _dialog(_log)
41{
42 WINPR_ASSERT(context);
43 setMetadata();
44
45 auto instance = _context->instance;
46 WINPR_ASSERT(instance);
47
48 instance->PreConnect = preConnect;
49 instance->PostConnect = postConnect;
50 instance->PostDisconnect = postDisconnect;
51 instance->PostFinalDisconnect = postFinalDisconnect;
52 instance->AuthenticateEx = sdl_authenticate_ex;
53 instance->VerifyCertificateEx = sdl_verify_certificate_ex;
54 instance->VerifyChangedCertificateEx = sdl_verify_changed_certificate_ex;
55 instance->LogonErrorInfo = sdl_logon_error_info;
56 instance->PresentGatewayMessage = sdl_present_gateway_message;
57 instance->ChooseSmartcard = sdl_choose_smartcard;
58 instance->RetryDialog = sdl_retry_dialog;
59
60#ifdef WITH_WEBVIEW
61 instance->GetAccessToken = sdl_webview_get_access_token;
62#else
63 instance->GetAccessToken = client_cli_get_access_token;
64#endif
65 /* TODO: Client display set up */
66}
67
68void SdlContext::setHasCursor(bool val)
69{
70 this->_cursor_visible = val;
71}
72
73bool SdlContext::hasCursor() const
74{
75 return _cursor_visible;
76}
77
78void SdlContext::setMetadata()
79{
80 auto wmclass = freerdp_settings_get_string(_context->settings, FreeRDP_WmClass);
81 if (!wmclass || (strlen(wmclass) == 0))
82 wmclass = SDL_CLIENT_UUID;
83
84 SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_IDENTIFIER_STRING, wmclass);
85 SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_NAME_STRING, SDL_CLIENT_NAME);
86 SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_VERSION_STRING, SDL_CLIENT_VERSION);
87 SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_CREATOR_STRING, SDL_CLIENT_VENDOR);
88 SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_COPYRIGHT_STRING, SDL_CLIENT_COPYRIGHT);
89 SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_URL_STRING, SDL_CLIENT_URL);
90 SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_TYPE_STRING, SDL_CLIENT_TYPE);
91}
92
93int SdlContext::start()
94{
95 _thread = std::thread(rdpThreadRun, this);
96 return 0;
97}
98
99int SdlContext::join()
100{
101 /* We do not want to use freerdp_abort_connect_context here.
102 * It would change the exit code and we do not want that. */
103 HANDLE event = freerdp_abort_event(context());
104 if (!SetEvent(event))
105 return -1;
106
107 _thread.join();
108 return 0;
109}
110
111void SdlContext::cleanup()
112{
113 std::unique_lock lock(_critical);
114 _windows.clear();
115 _dialog.destroy();
116 _primary.reset();
117}
118
119bool SdlContext::shallAbort(bool ignoreDialogs)
120{
121 std::unique_lock lock(_critical);
122 if (freerdp_shall_disconnect_context(context()))
123 {
124 if (ignoreDialogs)
125 return true;
126 if (_rdpThreadRunning)
127 return false;
128 return !getDialog().isRunning();
129 }
130 return false;
131}
132
133/* Called before a connection is established.
134 * Set all configuration options to support and load channels here. */
135BOOL SdlContext::preConnect(freerdp* instance)
136{
137 WINPR_ASSERT(instance);
138 WINPR_ASSERT(instance->context);
139
140 auto sdl = get_context(instance->context);
141
142 auto settings = instance->context->settings;
143 WINPR_ASSERT(settings);
144
145 if (!freerdp_settings_set_bool(settings, FreeRDP_CertificateCallbackPreferPEM, TRUE))
146 return FALSE;
147
148 /* Optional OS identifier sent to server */
149 if (!freerdp_settings_set_uint32(settings, FreeRDP_OsMajorType, OSMAJORTYPE_UNIX))
150 return FALSE;
151 if (!freerdp_settings_set_uint32(settings, FreeRDP_OsMinorType, OSMINORTYPE_NATIVE_SDL))
152 return FALSE;
153 /* OrderSupport is initialized at this point.
154 * Only override it if you plan to implement custom order
155 * callbacks or deactivate certain features. */
156 /* Register the channel listeners.
157 * They are required to set up / tear down channels if they are loaded. */
158 if (PubSub_SubscribeChannelConnected(instance->context->pubSub,
159 sdl_OnChannelConnectedEventHandler) < 0)
160 return FALSE;
161 if (PubSub_SubscribeChannelDisconnected(instance->context->pubSub,
162 sdl_OnChannelDisconnectedEventHandler) < 0)
163 return FALSE;
164
165 if (!freerdp_settings_get_bool(settings, FreeRDP_AuthenticationOnly))
166 {
167 UINT32 maxWidth = 0;
168 UINT32 maxHeight = 0;
169
170 if (!sdl_detect_monitors(sdl, &maxWidth, &maxHeight))
171 return FALSE;
172
173 if ((maxWidth != 0) && (maxHeight != 0) &&
174 !freerdp_settings_get_bool(settings, FreeRDP_SmartSizing))
175 {
176 WLog_Print(sdl->getWLog(), WLOG_INFO, "Update size to %ux%u", maxWidth, maxHeight);
177 if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopWidth, maxWidth))
178 return FALSE;
179 if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopHeight, maxHeight))
180 return FALSE;
181 }
182
188 const uint32_t sw = freerdp_settings_get_uint32(settings, FreeRDP_SmartSizingWidth);
189 const uint32_t sh = freerdp_settings_get_uint32(settings, FreeRDP_SmartSizingHeight);
190 const BOOL sm = freerdp_settings_get_bool(settings, FreeRDP_SmartSizing);
191 if (sm && (sw > 0) && (sh > 0))
192 {
193 const BOOL mm = freerdp_settings_get_bool(settings, FreeRDP_UseMultimon);
194 if (mm)
195 WLog_Print(sdl->getWLog(), WLOG_WARN,
196 "/smart-sizing and /multimon are currently not supported, ignoring "
197 "/smart-sizing!");
198 else
199 {
200 sdl->_windowWidth = freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth);
201 sdl->_windowHeigth = freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight);
202
203 if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopWidth, sw))
204 return FALSE;
205 if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopHeight, sh))
206 return FALSE;
207 }
208 }
209 }
210 else
211 {
212 /* Check +auth-only has a username and password. */
213 if (!freerdp_settings_get_string(settings, FreeRDP_Password))
214 {
215 WLog_Print(sdl->getWLog(), WLOG_INFO,
216 "auth-only, but no password set. Please provide one.");
217 return FALSE;
218 }
219
220 if (!freerdp_settings_set_bool(settings, FreeRDP_DeactivateClientDecoding, TRUE))
221 return FALSE;
222
223 WLog_Print(sdl->getWLog(), WLOG_INFO, "Authentication only. Don't connect SDL.");
224 }
225
226 if (!sdl->getInputChannelContext().initialize())
227 return FALSE;
228
229 /* TODO: Any code your client requires */
230 return TRUE;
231}
232
233/* Called after a RDP connection was successfully established.
234 * Settings might have changed during negotiation of client / server feature
235 * support.
236 *
237 * Set up local framebuffers and paing callbacks.
238 * If required, register pointer callbacks to change the local mouse cursor
239 * when hovering over the RDP window
240 */
241BOOL SdlContext::postConnect(freerdp* instance)
242{
243 WINPR_ASSERT(instance);
244
245 auto context = instance->context;
246 WINPR_ASSERT(context);
247
248 auto sdl = get_context(context);
249
250 // Retry was successful, discard dialog
251 sdl->getDialog().show(false);
252
253 if (freerdp_settings_get_bool(context->settings, FreeRDP_AuthenticationOnly))
254 {
255 /* Check +auth-only has a username and password. */
256 if (!freerdp_settings_get_string(context->settings, FreeRDP_Password))
257 {
258 WLog_Print(sdl->getWLog(), WLOG_INFO,
259 "auth-only, but no password set. Please provide one.");
260 return FALSE;
261 }
262
263 WLog_Print(sdl->getWLog(), WLOG_INFO, "Authentication only. Don't connect to X.");
264 return TRUE;
265 }
266
267 if (!sdl->waitForWindowsCreated())
268 return FALSE;
269
270 sdl->_sdlPixelFormat = SDL_PIXELFORMAT_BGRA32;
271 if (!gdi_init(instance, PIXEL_FORMAT_BGRA32))
272 return FALSE;
273
274 if (!sdl->createPrimary())
275 return FALSE;
276
277 if (!sdl_register_pointer(instance->context->graphics))
278 return FALSE;
279
280 WINPR_ASSERT(context->update);
281
282 context->update->BeginPaint = beginPaint;
283 context->update->EndPaint = endPaint;
284 context->update->PlaySound = playSound;
285 context->update->DesktopResize = desktopResize;
286 context->update->SetKeyboardIndicators = sdlInput::keyboard_set_indicators;
287 context->update->SetKeyboardImeStatus = sdlInput::keyboard_set_ime_status;
288
289 if (!sdl->setResizeable(false))
290 return FALSE;
291 if (!sdl->setFullscreen(freerdp_settings_get_bool(context->settings, FreeRDP_Fullscreen) ||
292 freerdp_settings_get_bool(context->settings, FreeRDP_UseMultimon),
293 true))
294 return FALSE;
295 sdl->setConnected(true);
296 return TRUE;
297}
298
299/* This function is called whether a session ends by failure or success.
300 * Clean up everything allocated by pre_connect and post_connect.
301 */
302void SdlContext::postDisconnect(freerdp* instance)
303{
304 if (!instance)
305 return;
306
307 if (!instance->context)
308 return;
309
310 auto sdl = get_context(instance->context);
311 sdl->setConnected(false);
312
313 gdi_free(instance);
314}
315
316void SdlContext::postFinalDisconnect(freerdp* instance)
317{
318 if (!instance)
319 return;
320
321 if (!instance->context)
322 return;
323
324 PubSub_UnsubscribeChannelConnected(instance->context->pubSub,
325 sdl_OnChannelConnectedEventHandler);
326 PubSub_UnsubscribeChannelDisconnected(instance->context->pubSub,
327 sdl_OnChannelDisconnectedEventHandler);
328}
329
330/* Create a SDL surface from the GDI buffer */
331bool SdlContext::createPrimary()
332{
333 auto gdi = context()->gdi;
334 WINPR_ASSERT(gdi);
335
336 _primary = SDLSurfacePtr(
337 SDL_CreateSurfaceFrom(static_cast<int>(gdi->width), static_cast<int>(gdi->height),
338 pixelFormat(), gdi->primary_buffer, static_cast<int>(gdi->stride)),
339 SDL_DestroySurface);
340 if (!_primary)
341 return false;
342
343 SDL_SetSurfaceBlendMode(_primary.get(), SDL_BLENDMODE_NONE);
344 SDL_Rect surfaceRect = { 0, 0, gdi->width, gdi->height };
345 SDL_FillSurfaceRect(_primary.get(), &surfaceRect,
346 SDL_MapSurfaceRGBA(_primary.get(), 0, 0, 0, 0xff));
347
348 return true;
349}
350
351bool SdlContext::createWindows()
352{
353 auto settings = context()->settings;
354 const auto& title = windowTitle();
355
356 ScopeGuard guard1([&]() { _windowsCreatedEvent.set(); });
357
358 UINT32 windowCount = freerdp_settings_get_uint32(settings, FreeRDP_MonitorCount);
359
360 Sint32 originX = 0;
361 Sint32 originY = 0;
362 for (UINT32 x = 0; x < windowCount; x++)
363 {
364 auto id = monitorId(x);
365 if (id < 0)
366 return false;
367
368 auto monitor = static_cast<rdpMonitor*>(
369 freerdp_settings_get_pointer_array_writable(settings, FreeRDP_MonitorDefArray, x));
370
371 originX = std::min<Sint32>(monitor->x, originX);
372 originY = std::min<Sint32>(monitor->y, originY);
373 }
374
375 for (UINT32 x = 0; x < windowCount; x++)
376 {
377 auto id = monitorId(x);
378 if (id < 0)
379 return false;
380
381 auto monitor = static_cast<rdpMonitor*>(
382 freerdp_settings_get_pointer_array_writable(settings, FreeRDP_MonitorDefArray, x));
383
384 Uint32 w = WINPR_ASSERTING_INT_CAST(Uint32, monitor->width);
385 Uint32 h = WINPR_ASSERTING_INT_CAST(Uint32, monitor->height);
386 if (!(freerdp_settings_get_bool(settings, FreeRDP_UseMultimon) ||
387 freerdp_settings_get_bool(settings, FreeRDP_Fullscreen)))
388 {
389 if (_windowWidth > 0)
390 w = _windowWidth;
391 else
392 w = freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth);
393
394 if (_windowHeigth > 0)
395 h = _windowHeigth;
396 else
397 h = freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight);
398 }
399
400 Uint32 flags = SDL_WINDOW_HIGH_PIXEL_DENSITY;
401
402 if (freerdp_settings_get_bool(settings, FreeRDP_Fullscreen) &&
403 !freerdp_settings_get_bool(settings, FreeRDP_UseMultimon))
404 {
405 flags |= SDL_WINDOW_FULLSCREEN;
406 }
407
408 if (freerdp_settings_get_bool(settings, FreeRDP_UseMultimon))
409 {
410 flags |= SDL_WINDOW_BORDERLESS;
411 }
412
413 if (!freerdp_settings_get_bool(settings, FreeRDP_Decorations))
414 flags |= SDL_WINDOW_BORDERLESS;
415
416 auto did = WINPR_ASSERTING_INT_CAST(SDL_DisplayID, id);
417 auto window = SdlWindow::create(did, title, flags, w, h);
418
419 if (freerdp_settings_get_bool(settings, FreeRDP_UseMultimon))
420 {
421 window.setOffsetX(originX - monitor->x);
422 window.setOffsetY(originY - monitor->y);
423 }
424
425 _windows.insert({ window.id(), std::move(window) });
426 }
427
428 return true;
429}
430
431bool SdlContext::updateWindowList()
432{
433 std::vector<rdpMonitor> list;
434 list.reserve(_windows.size());
435 for (const auto& win : _windows)
436 list.push_back(win.second.monitor(_windows.size() == 1));
437
438 return freerdp_settings_set_monitor_def_array_sorted(context()->settings, list.data(),
439 list.size());
440}
441
442bool SdlContext::updateWindow(SDL_WindowID id)
443{
444 if (freerdp_settings_get_bool(_context->settings, FreeRDP_Fullscreen) ||
445 freerdp_settings_get_bool(_context->settings, FreeRDP_UseMultimon))
446 return true;
447
448 auto& w = _windows.at(id);
449 auto m = w.monitor(true);
450 auto r = w.rect();
451 m.width = r.w;
452 m.height = r.h;
453 m.attributes.physicalWidth = static_cast<UINT32>(r.w);
454 m.attributes.physicalHeight = static_cast<UINT32>(r.h);
455 w.setMonitor(m);
456 return true;
457}
458
459std::string SdlContext::windowTitle() const
460{
461 const char* prefix = "FreeRDP:";
462
463 const auto windowTitle = freerdp_settings_get_string(context()->settings, FreeRDP_WindowTitle);
464 if (windowTitle)
465 return windowTitle;
466
467 const auto name = freerdp_settings_get_server_name(context()->settings);
468 const auto port = freerdp_settings_get_uint32(context()->settings, FreeRDP_ServerPort);
469 const auto addPort = (port != 3389);
470
471 std::stringstream ss;
472 ss << prefix << " " << name;
473
474 if (addPort)
475 ss << ":" << port;
476
477 return ss.str();
478}
479
480bool SdlContext::waitForWindowsCreated()
481{
482 {
483 std::unique_lock<CriticalSection> lock(_critical);
484 _windowsCreatedEvent.clear();
485 if (!sdl_push_user_event(SDL_EVENT_USER_CREATE_WINDOWS, this))
486 return false;
487 }
488
489 HANDLE handles[] = { _windowsCreatedEvent.handle(), freerdp_abort_event(context()) };
490
491 const DWORD rc = WaitForMultipleObjects(ARRAYSIZE(handles), handles, FALSE, INFINITE);
492 switch (rc)
493 {
494 case WAIT_OBJECT_0:
495 return true;
496 default:
497 return false;
498 }
499}
500
501/* This function is called when the library completed composing a new
502 * frame. Read out the changed areas and blit them to your output device.
503 * The image buffer will have the format specified by gdi_init
504 */
505BOOL SdlContext::endPaint(rdpContext* context)
506{
507 auto sdl = get_context(context);
508 WINPR_ASSERT(sdl);
509
510 auto gdi = context->gdi;
511 WINPR_ASSERT(gdi);
512 WINPR_ASSERT(gdi->primary);
513
514 HGDI_DC hdc = gdi->primary->hdc;
515 WINPR_ASSERT(hdc);
516 if (!hdc->hwnd)
517 return TRUE;
518
519 HGDI_WND hwnd = hdc->hwnd;
520 WINPR_ASSERT(hwnd->invalid || (hwnd->ninvalid == 0));
521
522 if (hwnd->invalid->null)
523 return TRUE;
524
525 WINPR_ASSERT(hwnd->invalid);
526 if (gdi->suppressOutput || hwnd->invalid->null)
527 return TRUE;
528
529 const INT32 ninvalid = hwnd->ninvalid;
530 const GDI_RGN* cinvalid = hwnd->cinvalid;
531
532 if (ninvalid < 1)
533 return TRUE;
534
535 std::vector<SDL_Rect> rects;
536 for (INT32 x = 0; x < ninvalid; x++)
537 {
538 auto& rgn = cinvalid[x];
539 rects.push_back({ rgn.x, rgn.y, rgn.w, rgn.h });
540 }
541
542 sdl->push(std::move(rects));
543 return sdl_push_user_event(SDL_EVENT_USER_UPDATE);
544}
545
546void SdlContext::sdl_client_cleanup(int exit_code, const std::string& error_msg)
547{
548 rdpSettings* settings = context()->settings;
549 WINPR_ASSERT(settings);
550
551 _rdpThreadRunning = false;
552 bool showError = false;
553 if (freerdp_settings_get_bool(settings, FreeRDP_AuthenticationOnly))
554 WLog_Print(getWLog(), WLOG_INFO, "Authentication only, exit status %s [%" PRId32 "]",
555 sdl::error::exitCodeToTag(exit_code), exit_code);
556 else
557 {
558 switch (exit_code)
559 {
560 case sdl::error::SUCCESS:
561 case sdl::error::DISCONNECT:
562 case sdl::error::LOGOFF:
563 case sdl::error::DISCONNECT_BY_USER:
564 case sdl::error::CONNECT_CANCELLED:
565 break;
566 default:
567 {
568 getDialog().showError(error_msg);
569 }
570 break;
571 }
572 }
573
574 if (!showError)
575 getDialog().show(false);
576
577 _exitCode = exit_code;
578 std::ignore = sdl_push_user_event(SDL_EVENT_USER_QUIT);
579 SDL_CleanupTLS();
580}
581
582int SdlContext::sdl_client_thread_connect(std::string& error_msg)
583{
584 auto instance = context()->instance;
585 WINPR_ASSERT(instance);
586
587 _rdpThreadRunning = true;
588 BOOL rc = freerdp_connect(instance);
589
590 rdpSettings* settings = context()->settings;
591 WINPR_ASSERT(settings);
592
593 int exit_code = sdl::error::SUCCESS;
594 if (!rc)
595 {
596 UINT32 error = freerdp_get_last_error(context());
597 exit_code = sdl::error::errorToExitCode(error);
598 }
599
600 if (freerdp_settings_get_bool(settings, FreeRDP_AuthenticationOnly))
601 {
602 DWORD code = freerdp_get_last_error(context());
603 freerdp_abort_connect_context(context());
604 WLog_Print(getWLog(), WLOG_ERROR, "Authentication only, %s [0x%08" PRIx32 "] %s",
605 freerdp_get_last_error_name(code), code, freerdp_get_last_error_string(code));
606 return exit_code;
607 }
608
609 if (!rc)
610 {
611 DWORD code = freerdp_error_info(instance);
612 if (exit_code == sdl::error::SUCCESS)
613 {
614 char* msg = nullptr;
615 size_t len = 0;
616 exit_code = error_info_to_error(&code, &msg, &len);
617 if (msg)
618 error_msg = msg;
619 free(msg);
620 }
621
622 auto last = freerdp_get_last_error(context());
623 if (error_msg.empty())
624 {
625 char* msg = nullptr;
626 size_t len = 0;
627 winpr_asprintf(&msg, &len, "%s [0x%08" PRIx32 "]\n%s",
628 freerdp_get_last_error_name(last), last,
629 freerdp_get_last_error_string(last));
630 if (msg)
631 error_msg = msg;
632 free(msg);
633 }
634
635 if (exit_code == sdl::error::SUCCESS)
636 {
637 if (last == FREERDP_ERROR_AUTHENTICATION_FAILED)
638 exit_code = sdl::error::AUTH_FAILURE;
639 else if (code == ERRINFO_SUCCESS)
640 exit_code = sdl::error::CONN_FAILED;
641 }
642
643 getDialog().show(false);
644 }
645
646 return exit_code;
647}
648
649int SdlContext::sdl_client_thread_run(std::string& error_msg)
650{
651 auto instance = context()->instance;
652 WINPR_ASSERT(instance);
653
654 int exit_code = sdl::error::SUCCESS;
655 while (!freerdp_shall_disconnect_context(context()))
656 {
657 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = {};
658 /*
659 * win8 and server 2k12 seem to have some timing issue/race condition
660 * when a initial sync request is send to sync the keyboard indicators
661 * sending the sync event twice fixed this problem
662 */
663 if (freerdp_focus_required(instance))
664 {
665 auto ctx = get_context(context());
666 WINPR_ASSERT(ctx);
667
668 auto& input = ctx->getInputChannelContext();
669 if (!input.keyboard_focus_in())
670 break;
671 if (!input.keyboard_focus_in())
672 break;
673 }
674
675 const DWORD nCount = freerdp_get_event_handles(context(), handles, ARRAYSIZE(handles));
676
677 if (nCount == 0)
678 {
679 WLog_Print(getWLog(), WLOG_ERROR, "freerdp_get_event_handles failed");
680 break;
681 }
682
683 const DWORD status = WaitForMultipleObjects(nCount, handles, FALSE, INFINITE);
684
685 if (status == WAIT_FAILED)
686 {
687 WLog_Print(getWLog(), WLOG_ERROR, "WaitForMultipleObjects WAIT_FAILED");
688 break;
689 }
690
691 if (!freerdp_check_event_handles(context()))
692 {
693 if (client_auto_reconnect(instance))
694 {
695 // Retry was successful, discard dialog
696 getDialog().show(false);
697 continue;
698 }
699 else
700 {
701 /*
702 * Indicate an unsuccessful connection attempt if reconnect
703 * did not succeed and no other error was specified.
704 */
705 if (freerdp_error_info(instance) == 0)
706 exit_code = sdl::error::CONN_FAILED;
707 }
708
709 if (freerdp_get_last_error(context()) == FREERDP_ERROR_SUCCESS)
710 WLog_Print(getWLog(), WLOG_ERROR, "WaitForMultipleObjects failed with %" PRIu32 "",
711 status);
712 if (freerdp_get_last_error(context()) == FREERDP_ERROR_SUCCESS)
713 WLog_Print(getWLog(), WLOG_ERROR, "Failed to check FreeRDP event handles");
714 break;
715 }
716 }
717
718 if (exit_code == sdl::error::SUCCESS)
719 {
720 DWORD code = 0;
721 {
722 char* emsg = nullptr;
723 size_t elen = 0;
724 exit_code = error_info_to_error(&code, &emsg, &elen);
725 if (emsg)
726 error_msg = emsg;
727 free(emsg);
728 }
729
730 if ((code == ERRINFO_LOGOFF_BY_USER) &&
731 (freerdp_get_disconnect_ultimatum(context()) == Disconnect_Ultimatum_user_requested))
732 {
733 const char* msg = "Error info says user did not initiate but disconnect ultimatum says "
734 "they did; treat this as a user logoff";
735
736 char* emsg = nullptr;
737 size_t elen = 0;
738 winpr_asprintf(&emsg, &elen, "%s", msg);
739 if (emsg)
740 error_msg = emsg;
741 free(emsg);
742
743 /* This situation might be limited to Windows XP. */
744 WLog_Print(getWLog(), WLOG_INFO, "%s", msg);
745 exit_code = sdl::error::LOGOFF;
746 }
747 }
748
749 freerdp_disconnect(instance);
750
751 return exit_code;
752}
753
754/* RDP main loop.
755 * Connects RDP, loops while running and handles event and dispatch, cleans up
756 * after the connection ends. */
757DWORD SdlContext::rdpThreadRun(SdlContext* sdl)
758{
759 WINPR_ASSERT(sdl);
760
761 std::string error_msg;
762 int exit_code = sdl->sdl_client_thread_connect(error_msg);
763 if (exit_code == sdl::error::SUCCESS)
764 exit_code = sdl->sdl_client_thread_run(error_msg);
765 sdl->sdl_client_cleanup(exit_code, error_msg);
766
767 return static_cast<DWORD>(exit_code);
768}
769
770int SdlContext::error_info_to_error(DWORD* pcode, char** msg, size_t* len) const
771{
772 const DWORD code = freerdp_error_info(context()->instance);
773 const char* name = freerdp_get_error_info_name(code);
774 const char* str = freerdp_get_error_info_string(code);
775 const int exit_code = sdl::error::errorToExitCode(code);
776
777 winpr_asprintf(msg, len, "Terminate with %s due to ERROR_INFO %s [0x%08" PRIx32 "]: %s",
778 sdl::error::errorToExitCodeTag(code), name, code, str);
779 SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "%s", *msg);
780 if (pcode)
781 *pcode = code;
782 return exit_code;
783}
784
785void SdlContext::applyMonitorOffset(SDL_WindowID window, float& x, float& y) const
786{
787 if (!freerdp_settings_get_bool(context()->settings, FreeRDP_UseMultimon))
788 return;
789
790 auto w = getWindowForId(window);
791 x -= static_cast<float>(w->offsetX());
792 y -= static_cast<float>(w->offsetY());
793}
794
795static bool alignX(const SDL_Rect& a, const SDL_Rect& b)
796{
797 if (a.x + a.w == b.x)
798 return true;
799 if (b.x + b.w == a.x)
800 return true;
801 return false;
802}
803
804static bool alignY(const SDL_Rect& a, const SDL_Rect& b)
805{
806 if (a.y + a.h == b.y)
807 return true;
808 if (b.y + b.h == a.y)
809 return true;
810 return false;
811}
812
813std::vector<SDL_DisplayID>
814SdlContext::updateDisplayOffsetsForNeighbours(SDL_DisplayID id,
815 const std::vector<SDL_DisplayID>& ignore)
816{
817 auto first = _offsets.at(id);
818 std::vector<SDL_DisplayID> neighbours;
819
820 for (auto& entry : _offsets)
821 {
822 if (entry.first == id)
823 continue;
824 if (std::find(ignore.begin(), ignore.end(), entry.first) != ignore.end())
825 continue;
826
827 bool neighbor = false;
828 if (alignX(entry.second.first, first.first))
829 {
830 if (entry.second.first.x < first.first.x)
831 entry.second.second.x = first.second.x - entry.second.second.w;
832 else
833 entry.second.second.x = first.second.x + first.second.w;
834 neighbor = true;
835 }
836 if (alignY(entry.second.first, first.first))
837 {
838 if (entry.second.first.y < first.first.y)
839 entry.second.second.y = first.second.y - entry.second.second.h;
840 else
841 entry.second.second.y = first.second.y + first.second.h;
842 neighbor = true;
843 }
844
845 if (neighbor)
846 neighbours.push_back(entry.first);
847 }
848 return neighbours;
849}
850
851void SdlContext::updateMonitorDataFromOffsets()
852{
853 for (auto& entry : _displays)
854 {
855 auto offsets = _offsets.at(entry.first);
856 entry.second.x = offsets.second.x;
857 entry.second.y = offsets.second.y;
858 }
859
860 for (auto& entry : _windows)
861 {
862 const auto& monitor = _displays.at(entry.first);
863 entry.second.setMonitor(monitor);
864 }
865}
866
867bool SdlContext::drawToWindow(SdlWindow& window, const std::vector<SDL_Rect>& rects)
868{
869 if (!isConnected())
870 return true;
871
872 auto gdi = context()->gdi;
873 WINPR_ASSERT(gdi);
874
875 auto size = window.rect();
876
877 std::unique_lock lock(_critical);
878 auto surface = _primary.get();
879 if (freerdp_settings_get_bool(context()->settings, FreeRDP_SmartSizing))
880 {
881 window.setOffsetX(0);
882 window.setOffsetY(0);
883 if (gdi->width < size.w)
884 {
885 window.setOffsetX((size.w - gdi->width) / 2);
886 }
887 if (gdi->height < size.h)
888 {
889 window.setOffsetY((size.h - gdi->height) / 2);
890 }
891
892 _localScale = { static_cast<float>(size.w) / static_cast<float>(gdi->width),
893 static_cast<float>(size.h) / static_cast<float>(gdi->height) };
894 if (!window.drawScaledRects(surface, _localScale, rects))
895 return false;
896 }
897 else
898 {
899 SDL_Point offset{ 0, 0 };
900 if (freerdp_settings_get_bool(context()->settings, FreeRDP_UseMultimon))
901 offset = { window.offsetX(), window.offsetY() };
902 if (!window.drawRects(surface, offset, rects))
903 return false;
904 }
905
906 window.updateSurface();
907 return true;
908}
909
910bool SdlContext::minimizeAllWindows()
911{
912 for (auto& w : _windows)
913 w.second.minimize();
914 return true;
915}
916
917int SdlContext::exitCode() const
918{
919 return _exitCode;
920}
921
922SDL_PixelFormat SdlContext::pixelFormat() const
923{
924 return _sdlPixelFormat;
925}
926
927bool SdlContext::addDisplayWindow(SDL_DisplayID id)
928{
929 const auto flags =
930 SDL_WINDOW_HIGH_PIXEL_DENSITY | SDL_WINDOW_FULLSCREEN | SDL_WINDOW_BORDERLESS;
931 auto title = sdl::utils::windowTitle(context()->settings);
932 auto w = SdlWindow::create(id, title, flags);
933 _windows.emplace(w.id(), std::move(w));
934 return true;
935}
936
937bool SdlContext::removeDisplayWindow(SDL_DisplayID id)
938{
939 for (auto& w : _windows)
940 {
941 if (w.second.displayIndex() == id)
942 _windows.erase(w.first);
943 }
944 return true;
945}
946
947bool SdlContext::detectDisplays()
948{
949 int count = 0;
950 auto display = SDL_GetDisplays(&count);
951 if (!display)
952 return false;
953 for (int x = 0; x < count; x++)
954 {
955 const auto id = display[x];
956 addOrUpdateDisplay(id);
957 }
958 SDL_free(display);
959 return true;
960}
961
962rdpMonitor SdlContext::getDisplay(SDL_DisplayID id) const
963{
964 return _displays.at(id);
965}
966
967std::vector<SDL_DisplayID> SdlContext::getDisplayIds() const
968{
969 std::vector<SDL_DisplayID> keys;
970 keys.reserve(_displays.size());
971 for (const auto& entry : _displays)
972 {
973 keys.push_back(entry.first);
974 }
975 return keys;
976}
977
978const SdlWindow* SdlContext::getWindowForId(SDL_WindowID id) const
979{
980 auto it = _windows.find(id);
981 if (it == _windows.end())
982 return nullptr;
983 return &it->second;
984}
985
986SdlWindow* SdlContext::getWindowForId(SDL_WindowID id)
987{
988 auto it = _windows.find(id);
989 if (it == _windows.end())
990 return nullptr;
991 return &it->second;
992}
993
994SdlWindow* SdlContext::getFirstWindow()
995{
996 if (_windows.empty())
997 return nullptr;
998 return &_windows.begin()->second;
999}
1000
1001sdlDispContext& SdlContext::getDisplayChannelContext()
1002{
1003 return _disp;
1004}
1005
1006sdlInput& SdlContext::getInputChannelContext()
1007{
1008 return _input;
1009}
1010
1011sdlClip& SdlContext::getClipboardChannelContext()
1012{
1013 return _clip;
1014}
1015
1016SdlConnectionDialogWrapper& SdlContext::getDialog()
1017{
1018 return _dialog;
1019}
1020
1021wLog* SdlContext::getWLog()
1022{
1023 return _log;
1024}
1025
1026bool SdlContext::moveMouseTo(const SDL_FPoint& pos)
1027{
1028 auto window = SDL_GetMouseFocus();
1029 if (!window)
1030 return true;
1031
1032 const auto id = SDL_GetWindowID(window);
1033 const auto spos = pixelToScreen(id, pos);
1034 SDL_WarpMouseInWindow(window, spos.x, spos.y);
1035 return true;
1036}
1037
1038bool SdlContext::handleEvent(const SDL_MouseMotionEvent& ev)
1039{
1040 SDL_Event copy{};
1041 copy.motion = ev;
1042 if (!eventToPixelCoordinates(ev.windowID, copy))
1043 return false;
1044 removeLocalScaling(copy.motion.x, copy.motion.y);
1045 removeLocalScaling(copy.motion.xrel, copy.motion.yrel);
1046 applyMonitorOffset(copy.motion.windowID, copy.motion.x, copy.motion.y);
1047
1048 return SdlTouch::handleEvent(this, copy.motion);
1049}
1050
1051bool SdlContext::handleEvent(const SDL_MouseWheelEvent& ev)
1052{
1053 SDL_Event copy{};
1054 copy.wheel = ev;
1055 if (!eventToPixelCoordinates(ev.windowID, copy))
1056 return false;
1057 removeLocalScaling(copy.wheel.mouse_x, copy.wheel.mouse_y);
1058 return SdlTouch::handleEvent(this, copy.wheel);
1059}
1060
1061bool SdlContext::handleEvent(const SDL_WindowEvent& ev)
1062{
1063 if (!getDisplayChannelContext().handleEvent(ev))
1064 return false;
1065
1066 auto window = getWindowForId(ev.windowID);
1067 if (!window)
1068 return true;
1069
1070 {
1071 const auto& r = window->rect();
1072 const auto& b = window->bounds();
1073 const auto& scale = window->scale();
1074 const auto& orientation = window->orientation();
1075 SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION,
1076 "%s: [%u] %dx%d-%dx%d {%dx%d-%dx%d}{scale=%f,orientation=%s}",
1077 sdl::utils::toString(ev.type).c_str(), ev.windowID, r.x, r.y, r.w, r.h, b.x,
1078 b.y, b.w, b.h, static_cast<double>(scale),
1079 sdl::utils::toString(orientation).c_str());
1080 }
1081
1082 switch (ev.type)
1083 {
1084 case SDL_EVENT_WINDOW_MOUSE_ENTER:
1085 return restoreCursor();
1086 case SDL_EVENT_WINDOW_DISPLAY_SCALE_CHANGED:
1087 if (isConnected())
1088 {
1089 if (!window->fill())
1090 return false;
1091 if (!drawToWindow(*window))
1092 return false;
1093 if (!restoreCursor())
1094 return false;
1095 }
1096 break;
1097 case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED:
1098 if (!window->fill())
1099 return false;
1100 if (!drawToWindow(*window))
1101 return false;
1102 if (!restoreCursor())
1103 return false;
1104 break;
1105 case SDL_EVENT_WINDOW_MOVED:
1106 {
1107 auto r = window->rect();
1108 auto id = window->id();
1109 SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "%u: %dx%d-%dx%d", id, r.x, r.y, r.w, r.h);
1110 }
1111 break;
1112 case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
1113 {
1114 SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "Window closed, terminating RDP session...");
1115 freerdp_abort_connect_context(context());
1116 }
1117 break;
1118 default:
1119 break;
1120 }
1121 return true;
1122}
1123
1124bool SdlContext::handleEvent(const SDL_DisplayEvent& ev)
1125{
1126 if (!getDisplayChannelContext().handleEvent(ev))
1127 return false;
1128
1129 switch (ev.type)
1130 {
1131 case SDL_EVENT_DISPLAY_REMOVED: // Can't show details for this one...
1132 break;
1133 default:
1134 {
1135 SDL_Rect r = {};
1136 if (!SDL_GetDisplayBounds(ev.displayID, &r))
1137 return false;
1138 const auto name = SDL_GetDisplayName(ev.displayID);
1139 if (!name)
1140 return false;
1141 const auto orientation = SDL_GetCurrentDisplayOrientation(ev.displayID);
1142 const auto scale = SDL_GetDisplayContentScale(ev.displayID);
1143 const auto mode = SDL_GetCurrentDisplayMode(ev.displayID);
1144 if (!mode)
1145 return false;
1146
1147 SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION,
1148 "%s: [%u, %s] %dx%d-%dx%d {orientation=%s, scale=%f}%s",
1149 sdl::utils::toString(ev.type).c_str(), ev.displayID, name, r.x, r.y, r.w,
1150 r.h, sdl::utils::toString(orientation).c_str(), static_cast<double>(scale),
1151 sdl::utils::toString(mode).c_str());
1152 }
1153 break;
1154 }
1155 return true;
1156}
1157
1158bool SdlContext::handleEvent(const SDL_MouseButtonEvent& ev)
1159{
1160 SDL_Event copy = {};
1161 copy.button = ev;
1162 if (!eventToPixelCoordinates(ev.windowID, copy))
1163 return false;
1164 removeLocalScaling(copy.button.x, copy.button.y);
1165 applyMonitorOffset(copy.button.windowID, copy.button.x, copy.button.y);
1166 return SdlTouch::handleEvent(this, copy.button);
1167}
1168
1169bool SdlContext::handleEvent(const SDL_TouchFingerEvent& ev)
1170{
1171 SDL_Event copy{};
1172 copy.tfinger = ev;
1173 if (!eventToPixelCoordinates(ev.windowID, copy))
1174 return false;
1175 removeLocalScaling(copy.tfinger.dx, copy.tfinger.dy);
1176 removeLocalScaling(copy.tfinger.x, copy.tfinger.y);
1177 applyMonitorOffset(copy.tfinger.windowID, copy.tfinger.x, copy.tfinger.y);
1178 return SdlTouch::handleEvent(this, copy.tfinger);
1179}
1180
1181void SdlContext::addOrUpdateDisplay(SDL_DisplayID id)
1182{
1183 auto monitor = SdlWindow::query(id, false);
1184 _displays.emplace(id, monitor);
1185
1186 /* Update actual display rectangles:
1187 *
1188 * 1. Get logical display bounds
1189 * 2. Use already known pixel width and height
1190 * 3. Iterate over each display and update the x and y offsets by adding all monitor
1191 * widths/heights from the primary
1192 */
1193 _offsets.clear();
1194 for (auto& entry : _displays)
1195 {
1196 SDL_Rect bounds{};
1197 std::ignore = SDL_GetDisplayBounds(entry.first, &bounds);
1198
1199 SDL_Rect pixel{};
1200 pixel.w = entry.second.width;
1201 pixel.h = entry.second.height;
1202 _offsets.emplace(entry.first, std::pair{ bounds, pixel });
1203 }
1204
1205 /* 1. Find primary and update all neighbors
1206 * 2. For each neighbor update all neighbors
1207 * 3. repeat until all displays updated.
1208 */
1209 const auto primary = SDL_GetPrimaryDisplay();
1210 std::vector<SDL_DisplayID> handled;
1211 handled.push_back(primary);
1212
1213 auto neighbors = updateDisplayOffsetsForNeighbours(primary);
1214 while (!neighbors.empty())
1215 {
1216 auto neighbor = *neighbors.begin();
1217 neighbors.pop_back();
1218
1219 if (std::find(handled.begin(), handled.end(), neighbor) != handled.end())
1220 continue;
1221 handled.push_back(neighbor);
1222
1223 auto next = updateDisplayOffsetsForNeighbours(neighbor, handled);
1224 neighbors.insert(neighbors.end(), next.begin(), next.end());
1225 }
1226 updateMonitorDataFromOffsets();
1227}
1228
1229void SdlContext::deleteDisplay(SDL_DisplayID id)
1230{
1231 _displays.erase(id);
1232}
1233
1234bool SdlContext::eventToPixelCoordinates(SDL_WindowID id, SDL_Event& ev)
1235{
1236 auto w = getWindowForId(id);
1237 if (!w)
1238 return false;
1239
1240 /* Ignore errors here, sometimes SDL has no renderer */
1241 auto renderer = SDL_GetRenderer(w->window());
1242 if (!renderer)
1243 return true;
1244 return SDL_ConvertEventToRenderCoordinates(renderer, &ev);
1245}
1246
1247SDL_FPoint SdlContext::applyLocalScaling(const SDL_FPoint& val) const
1248{
1249 if (!freerdp_settings_get_bool(context()->settings, FreeRDP_SmartSizing))
1250 return val;
1251
1252 auto rval = val;
1253 rval.x *= _localScale.x;
1254 rval.y *= _localScale.y;
1255 return rval;
1256}
1257
1258void SdlContext::removeLocalScaling(float& x, float& y) const
1259{
1260 if (!freerdp_settings_get_bool(context()->settings, FreeRDP_SmartSizing))
1261 return;
1262 x /= _localScale.x;
1263 y /= _localScale.y;
1264}
1265
1266SDL_FPoint SdlContext::screenToPixel(SDL_WindowID id, const SDL_FPoint& pos)
1267{
1268 auto w = getWindowForId(id);
1269 if (!w)
1270 return {};
1271
1272 /* Ignore errors here, sometimes SDL has no renderer */
1273 auto renderer = SDL_GetRenderer(w->window());
1274 if (!renderer)
1275 return pos;
1276
1277 SDL_FPoint rpos{};
1278 if (!SDL_RenderCoordinatesFromWindow(renderer, pos.x, pos.y, &rpos.x, &rpos.y))
1279 return {};
1280 removeLocalScaling(rpos.x, rpos.y);
1281 return rpos;
1282}
1283
1284SDL_FPoint SdlContext::pixelToScreen(SDL_WindowID id, const SDL_FPoint& pos)
1285{
1286 auto w = getWindowForId(id);
1287 if (!w)
1288 return {};
1289
1290 /* Ignore errors here, sometimes SDL has no renderer */
1291 auto renderer = SDL_GetRenderer(w->window());
1292 if (!renderer)
1293 return pos;
1294
1295 SDL_FPoint rpos{};
1296 if (!SDL_RenderCoordinatesToWindow(renderer, pos.x, pos.y, &rpos.x, &rpos.y))
1297 return {};
1298 return applyLocalScaling(rpos);
1299}
1300
1301SDL_FRect SdlContext::pixelToScreen(SDL_WindowID id, const SDL_FRect& pos)
1302{
1303 const auto fpos = pixelToScreen(id, SDL_FPoint{ pos.x, pos.y });
1304 const auto size = pixelToScreen(id, SDL_FPoint{ pos.w, pos.h });
1305 return { fpos.x, fpos.y, size.x, size.y };
1306}
1307
1308bool SdlContext::handleEvent(const SDL_Event& ev)
1309{
1310 if ((ev.type >= SDL_EVENT_DISPLAY_FIRST) && (ev.type <= SDL_EVENT_DISPLAY_LAST))
1311 {
1312 const auto& dev = ev.display;
1313 return handleEvent(dev);
1314 }
1315 if ((ev.type >= SDL_EVENT_WINDOW_FIRST) && (ev.type <= SDL_EVENT_WINDOW_LAST))
1316 {
1317 const auto& wev = ev.window;
1318 return handleEvent(wev);
1319 }
1320 switch (ev.type)
1321 {
1322 case SDL_EVENT_FINGER_DOWN:
1323 case SDL_EVENT_FINGER_UP:
1324 case SDL_EVENT_FINGER_MOTION:
1325 {
1326 const auto& cev = ev.tfinger;
1327 return handleEvent(cev);
1328 }
1329 case SDL_EVENT_MOUSE_MOTION:
1330 {
1331 const auto& cev = ev.motion;
1332 return handleEvent(cev);
1333 }
1334 case SDL_EVENT_MOUSE_BUTTON_DOWN:
1335 case SDL_EVENT_MOUSE_BUTTON_UP:
1336 {
1337 const auto& cev = ev.button;
1338 return handleEvent(cev);
1339 }
1340 case SDL_EVENT_MOUSE_WHEEL:
1341 {
1342 const auto& cev = ev.wheel;
1343 return handleEvent(cev);
1344 }
1345 case SDL_EVENT_CLIPBOARD_UPDATE:
1346 {
1347 const auto& cev = ev.clipboard;
1348 return getClipboardChannelContext().handleEvent(cev);
1349 }
1350 case SDL_EVENT_KEY_DOWN:
1351 case SDL_EVENT_KEY_UP:
1352 {
1353 const auto& cev = ev.key;
1354 return getInputChannelContext().handleEvent(cev);
1355 }
1356 case SDL_EVENT_RENDER_TARGETS_RESET:
1357 case SDL_EVENT_RENDER_DEVICE_RESET:
1358 case SDL_EVENT_WILL_ENTER_FOREGROUND:
1359 return redraw();
1360 default:
1361 return true;
1362 }
1363}
1364
1365bool SdlContext::drawToWindows(const std::vector<SDL_Rect>& rects)
1366{
1367 for (auto& window : _windows)
1368 {
1369 if (!drawToWindow(window.second, rects))
1370 return FALSE;
1371 }
1372
1373 return TRUE;
1374}
1375
1376BOOL SdlContext::desktopResize(rdpContext* context)
1377{
1378 rdpGdi* gdi = nullptr;
1379 rdpSettings* settings = nullptr;
1380 auto sdl = get_context(context);
1381
1382 WINPR_ASSERT(sdl);
1383 WINPR_ASSERT(context);
1384
1385 settings = context->settings;
1386 WINPR_ASSERT(settings);
1387
1388 std::unique_lock lock(sdl->_critical);
1389 gdi = context->gdi;
1390 if (!gdi_resize(gdi, freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth),
1391 freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight)))
1392 return FALSE;
1393 return sdl->createPrimary();
1394}
1395
1396/* This function is called to output a System BEEP */
1397BOOL SdlContext::playSound(rdpContext* context, const PLAY_SOUND_UPDATE* play_sound)
1398{
1399 /* TODO: Implement */
1400 WINPR_UNUSED(context);
1401 WINPR_UNUSED(play_sound);
1402 return TRUE;
1403}
1404
1405/* This function is called whenever a new frame starts.
1406 * It can be used to reset invalidated areas. */
1407BOOL SdlContext::beginPaint(rdpContext* context)
1408{
1409 auto gdi = context->gdi;
1410 WINPR_ASSERT(gdi);
1411 WINPR_ASSERT(gdi->primary);
1412
1413 HGDI_DC hdc = gdi->primary->hdc;
1414 WINPR_ASSERT(hdc);
1415 if (!hdc->hwnd)
1416 return TRUE;
1417
1418 HGDI_WND hwnd = hdc->hwnd;
1419 WINPR_ASSERT(hwnd->invalid);
1420 hwnd->invalid->null = TRUE;
1421 hwnd->ninvalid = 0;
1422
1423 return TRUE;
1424}
1425
1426bool SdlContext::redraw(bool suppress) const
1427{
1428 if (!_connected)
1429 return true;
1430
1431 auto gdi = context()->gdi;
1432 WINPR_ASSERT(gdi);
1433 return gdi_send_suppress_output(gdi, suppress);
1434}
1435
1436void SdlContext::setConnected(bool val)
1437{
1438 _connected = val;
1439}
1440
1441bool SdlContext::isConnected() const
1442{
1443 return _connected;
1444}
1445
1446rdpContext* SdlContext::context() const
1447{
1448 WINPR_ASSERT(_context);
1449 return _context;
1450}
1451
1452rdpClientContext* SdlContext::common() const
1453{
1454 return reinterpret_cast<rdpClientContext*>(context());
1455}
1456
1457bool SdlContext::setCursor(CursorType type)
1458{
1459 _cursorType = type;
1460 return restoreCursor();
1461}
1462
1463bool SdlContext::setCursor(rdpPointer* cursor)
1464{
1465 _cursor = cursor;
1466 return setCursor(CURSOR_IMAGE);
1467}
1468
1469rdpPointer* SdlContext::cursor() const
1470{
1471 return _cursor;
1472}
1473
1474bool SdlContext::restoreCursor()
1475{
1476 WLog_Print(getWLog(), WLOG_DEBUG, "restore cursor: %d", _cursorType);
1477 switch (_cursorType)
1478 {
1479 case CURSOR_NULL:
1480 if (!SDL_HideCursor())
1481 {
1482 WLog_Print(getWLog(), WLOG_ERROR, "SDL_HideCursor failed");
1483 return false;
1484 }
1485
1486 setHasCursor(false);
1487 return true;
1488
1489 case CURSOR_DEFAULT:
1490 {
1491 auto def = SDL_GetDefaultCursor();
1492 if (!SDL_SetCursor(def))
1493 {
1494 WLog_Print(getWLog(), WLOG_ERROR, "SDL_SetCursor(default=%p) failed",
1495 static_cast<void*>(def));
1496 return false;
1497 }
1498 if (!SDL_ShowCursor())
1499 {
1500 WLog_Print(getWLog(), WLOG_ERROR, "SDL_ShowCursor failed");
1501 return false;
1502 }
1503 setHasCursor(true);
1504 return true;
1505 }
1506 case CURSOR_IMAGE:
1507 setHasCursor(true);
1508 return sdl_Pointer_Set_Process(this);
1509 default:
1510 WLog_Print(getWLog(), WLOG_ERROR, "Unknown cursorType %s",
1511 sdl::utils::toString(_cursorType).c_str());
1512 return false;
1513 }
1514}
1515
1516void SdlContext::setMonitorIds(const std::vector<SDL_DisplayID>& ids)
1517{
1518 _monitorIds.clear();
1519 for (auto id : ids)
1520 {
1521 _monitorIds.push_back(id);
1522 }
1523}
1524
1525const std::vector<SDL_DisplayID>& SdlContext::monitorIds() const
1526{
1527 return _monitorIds;
1528}
1529
1530int64_t SdlContext::monitorId(uint32_t index) const
1531{
1532 if (index >= _monitorIds.size())
1533 {
1534 return -1;
1535 }
1536 return _monitorIds.at(index);
1537}
1538
1539void SdlContext::push(std::vector<SDL_Rect>&& rects)
1540{
1541 std::unique_lock lock(_queue_mux);
1542 _queue.emplace(std::move(rects));
1543}
1544
1545std::vector<SDL_Rect> SdlContext::pop()
1546{
1547 std::unique_lock lock(_queue_mux);
1548 if (_queue.empty())
1549 {
1550 return {};
1551 }
1552 auto val = std::move(_queue.front());
1553 _queue.pop();
1554 return val;
1555}
1556
1557bool SdlContext::setFullscreen(bool enter, bool forceOriginalDisplay)
1558{
1559 for (const auto& window : _windows)
1560 {
1561 if (!sdl_push_user_event(SDL_EVENT_USER_WINDOW_FULLSCREEN, &window.second, enter,
1562 forceOriginalDisplay))
1563 return false;
1564 }
1565 _fullscreen = enter;
1566 return true;
1567}
1568
1569bool SdlContext::setMinimized()
1570{
1571 return sdl_push_user_event(SDL_EVENT_USER_WINDOW_MINIMIZE);
1572}
1573
1574bool SdlContext::grabMouse() const
1575{
1576 return _grabMouse;
1577}
1578
1579bool SdlContext::toggleGrabMouse()
1580{
1581 return setGrabMouse(!grabMouse());
1582}
1583
1584bool SdlContext::setGrabMouse(bool enter)
1585{
1586 _grabMouse = enter;
1587 return true;
1588}
1589
1590bool SdlContext::grabKeyboard() const
1591{
1592 return _grabKeyboard;
1593}
1594
1595bool SdlContext::toggleGrabKeyboard()
1596{
1597 return setGrabKeyboard(!grabKeyboard());
1598}
1599
1600bool SdlContext::setGrabKeyboard(bool enter)
1601{
1602 _grabKeyboard = enter;
1603 return true;
1604}
1605
1606bool SdlContext::setResizeable(bool enable)
1607{
1608 const auto settings = context()->settings;
1609 const bool dyn = freerdp_settings_get_bool(settings, FreeRDP_DynamicResolutionUpdate);
1610 const bool smart = freerdp_settings_get_bool(settings, FreeRDP_SmartSizing);
1611 bool use = (dyn && enable) || smart;
1612
1613 for (const auto& window : _windows)
1614 {
1615 if (!sdl_push_user_event(SDL_EVENT_USER_WINDOW_RESIZEABLE, &window.second, use))
1616 return false;
1617 }
1618 _resizeable = use;
1619
1620 return true;
1621}
1622
1623bool SdlContext::resizeable() const
1624{
1625 return _resizeable;
1626}
1627
1628bool SdlContext::toggleResizeable()
1629{
1630 return setResizeable(!resizeable());
1631}
1632
1633bool SdlContext::fullscreen() const
1634{
1635 return _fullscreen;
1636}
1637
1638bool SdlContext::toggleFullscreen()
1639{
1640 return setFullscreen(!fullscreen());
1641}
SdlContext(rdpContext *context)
object that handles clipboard context for the SDL3 client
Definition sdl_clip.hpp:76
FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 val)
Sets a UINT32 settings value.
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 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 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.