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"
29#include <sdl_common_utils.hpp>
30#include <scoped_guard.hpp>
32#include "dialogs/sdl_dialogs.hpp"
34#if defined(WITH_WEBVIEW)
35#include <aad/sdl_webview.hpp>
39 : _context(context), _log(WLog_Get(CLIENT_TAG(
"SDL"))), _rdpThreadRunning(false),
40 _primary(nullptr, SDL_DestroySurface), _disp(this), _input(this), _clip(this), _dialog(_log)
42 WINPR_ASSERT(context);
45 auto instance = _context->instance;
46 WINPR_ASSERT(instance);
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;
61 instance->GetAccessToken = sdl_webview_get_access_token;
63 instance->GetAccessToken = client_cli_get_access_token;
68void SdlContext::setHasCursor(
bool val)
70 this->_cursor_visible = val;
73bool SdlContext::hasCursor()
const
75 return _cursor_visible;
78void SdlContext::setMetadata()
81 if (!wmclass || (strlen(wmclass) == 0))
82 wmclass = SDL_CLIENT_UUID;
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);
93int SdlContext::start()
95 _thread = std::thread(rdpThreadRun,
this);
103 HANDLE
event = freerdp_abort_event(context());
104 if (!SetEvent(event))
111void SdlContext::cleanup()
113 std::unique_lock lock(_critical);
119bool SdlContext::shallAbort(
bool ignoreDialogs)
121 std::unique_lock lock(_critical);
122 if (freerdp_shall_disconnect_context(context()))
126 if (_rdpThreadRunning)
128 return !getDialog().isRunning();
135BOOL SdlContext::preConnect(freerdp* instance)
137 WINPR_ASSERT(instance);
138 WINPR_ASSERT(instance->context);
140 auto sdl = get_context(instance->context);
142 auto settings = instance->context->settings;
143 WINPR_ASSERT(settings);
158 PubSub_SubscribeChannelConnected(instance->context->pubSub, sdl_OnChannelConnectedEventHandler);
159 PubSub_SubscribeChannelDisconnected(instance->context->pubSub,
160 sdl_OnChannelDisconnectedEventHandler);
165 UINT32 maxHeight = 0;
167 if (!sdl_detect_monitors(sdl, &maxWidth, &maxHeight))
170 if ((maxWidth != 0) && (maxHeight != 0) &&
173 WLog_Print(sdl->getWLog(), WLOG_INFO,
"Update size to %ux%u", maxWidth, maxHeight);
188 if (sm && (sw > 0) && (sh > 0))
192 WLog_Print(sdl->getWLog(), WLOG_WARN,
193 "/smart-sizing and /multimon are currently not supported, ignoring "
212 WLog_Print(sdl->getWLog(), WLOG_INFO,
213 "auth-only, but no password set. Please provide one.");
220 WLog_Print(sdl->getWLog(), WLOG_INFO,
"Authentication only. Don't connect SDL.");
223 if (!sdl->getInputChannelContext().initialize())
238BOOL SdlContext::postConnect(freerdp* instance)
240 WINPR_ASSERT(instance);
242 auto context = instance->context;
243 WINPR_ASSERT(context);
245 auto sdl = get_context(context);
249 const auto driver = SDL_GetCurrentVideoDriver();
253 if (strcmp(driver,
"wayland") == 0)
255 else if (strcmp(driver,
"x11") == 0)
257 auto env = SDL_GetEnvironment();
258 auto xdg = SDL_GetEnvironmentVariable(env,
"XDG_SESSION_TYPE");
259 auto qpa = SDL_GetEnvironmentVariable(env,
"QT_QPA_PLATFORM");
260 if (xdg && (strcmp(xdg,
"wayland") == 0))
262 else if (qpa && (strcmp(qpa,
"wayland") == 0))
269 const auto name = SDL_GetAppMetadataProperty(SDL_PROP_APP_METADATA_NAME_STRING);
271 WLog_Print(sdl->getWLog(), WLOG_WARN,
272 "%s is affected by wayland bug "
273 "https://gitlab.freedesktop.org/wayland/wayland-protocols/-/issues/179",
276 sdl->getWLog(), WLOG_WARN,
277 "you will not be able to properly use all monitors for FreeRDP unless this is "
278 "resolved and the SDL library you are using supports this.");
279 WLog_Print(sdl->getWLog(), WLOG_WARN,
280 "For the time being run %s from an X11 session or only use single monitor "
287 sdl->getDialog().show(
false);
294 WLog_Print(sdl->getWLog(), WLOG_INFO,
295 "auth-only, but no password set. Please provide one.");
299 WLog_Print(sdl->getWLog(), WLOG_INFO,
"Authentication only. Don't connect to X.");
303 if (!sdl->waitForWindowsCreated())
306 sdl->_sdlPixelFormat = SDL_PIXELFORMAT_BGRA32;
307 if (!gdi_init(instance, PIXEL_FORMAT_BGRA32))
310 if (!sdl->createPrimary())
313 if (!sdl_register_pointer(instance->context->graphics))
316 WINPR_ASSERT(context->update);
318 context->update->BeginPaint = beginPaint;
319 context->update->EndPaint = endPaint;
320 context->update->PlaySound = playSound;
321 context->update->DesktopResize = desktopResize;
322 context->update->SetKeyboardIndicators = sdlInput::keyboard_set_indicators;
323 context->update->SetKeyboardImeStatus = sdlInput::keyboard_set_ime_status;
325 if (!sdl->setResizeable(
false))
331 sdl->setConnected(
true);
338void SdlContext::postDisconnect(freerdp* instance)
343 if (!instance->context)
346 auto sdl = get_context(instance->context);
347 sdl->setConnected(
false);
352void SdlContext::postFinalDisconnect(freerdp* instance)
357 if (!instance->context)
360 PubSub_UnsubscribeChannelConnected(instance->context->pubSub,
361 sdl_OnChannelConnectedEventHandler);
362 PubSub_UnsubscribeChannelDisconnected(instance->context->pubSub,
363 sdl_OnChannelDisconnectedEventHandler);
367bool SdlContext::createPrimary()
369 auto gdi = context()->gdi;
372 _primary = SDLSurfacePtr(
373 SDL_CreateSurfaceFrom(
static_cast<int>(gdi->width),
static_cast<int>(gdi->height),
374 pixelFormat(), gdi->primary_buffer,
static_cast<int>(gdi->stride)),
379 SDL_SetSurfaceBlendMode(_primary.get(), SDL_BLENDMODE_NONE);
380 SDL_Rect surfaceRect = { 0, 0, gdi->width, gdi->height };
381 SDL_FillSurfaceRect(_primary.get(), &surfaceRect,
382 SDL_MapSurfaceRGBA(_primary.get(), 0, 0, 0, 0xff));
387bool SdlContext::createWindows()
389 auto settings = context()->settings;
390 const auto& title = windowTitle();
392 ScopeGuard guard1([&]() { _windowsCreatedEvent.set(); });
398 for (UINT32 x = 0; x < windowCount; x++)
400 auto id = monitorId(x);
405 freerdp_settings_get_pointer_array_writable(settings, FreeRDP_MonitorDefArray, x));
407 originX = std::min<Sint32>(monitor->x, originX);
408 originY = std::min<Sint32>(monitor->y, originY);
411 for (UINT32 x = 0; x < windowCount; x++)
413 auto id = monitorId(x);
418 freerdp_settings_get_pointer_array_writable(settings, FreeRDP_MonitorDefArray, x));
420 Uint32 w = WINPR_ASSERTING_INT_CAST(Uint32, monitor->width);
421 Uint32 h = WINPR_ASSERTING_INT_CAST(Uint32, monitor->height);
425 if (_windowWidth > 0)
430 if (_windowHeigth > 0)
436 Uint32 flags = SDL_WINDOW_HIGH_PIXEL_DENSITY;
441 flags |= SDL_WINDOW_FULLSCREEN;
446 flags |= SDL_WINDOW_BORDERLESS;
450 flags |= SDL_WINDOW_BORDERLESS;
452 auto did = WINPR_ASSERTING_INT_CAST(SDL_DisplayID,
id);
453 auto window = SdlWindow::create(did, title, flags, w, h);
457 window.setOffsetX(originX - monitor->x);
458 window.setOffsetY(originY - monitor->y);
461 _windows.insert({ window.id(), std::move(window) });
467bool SdlContext::updateWindowList()
469 std::vector<rdpMonitor> list;
470 list.reserve(_windows.size());
471 for (
const auto& win : _windows)
472 list.push_back(win.second.monitor(_windows.size() == 1));
478bool SdlContext::updateWindow(SDL_WindowID
id)
484 auto& w = _windows.at(
id);
485 auto m = w.monitor(
true);
489 m.attributes.physicalWidth =
static_cast<UINT32
>(r.w);
490 m.attributes.physicalHeight =
static_cast<UINT32
>(r.h);
495std::string SdlContext::windowTitle()
const
497 const char* prefix =
"FreeRDP:";
505 const auto addPort = (port != 3389);
507 std::stringstream ss;
508 ss << prefix <<
" " << name;
516bool SdlContext::waitForWindowsCreated()
519 std::unique_lock<CriticalSection> lock(_critical);
520 _windowsCreatedEvent.clear();
521 if (!sdl_push_user_event(SDL_EVENT_USER_CREATE_WINDOWS,
this))
525 HANDLE handles[] = { _windowsCreatedEvent.handle(), freerdp_abort_event(context()) };
527 const DWORD rc = WaitForMultipleObjects(ARRAYSIZE(handles), handles, FALSE, INFINITE);
541BOOL SdlContext::endPaint(rdpContext* context)
543 auto sdl = get_context(context);
546 auto gdi = context->gdi;
548 WINPR_ASSERT(gdi->primary);
550 HGDI_DC hdc = gdi->primary->hdc;
556 WINPR_ASSERT(hwnd->invalid || (hwnd->ninvalid == 0));
558 if (hwnd->invalid->null)
561 WINPR_ASSERT(hwnd->invalid);
562 if (gdi->suppressOutput || hwnd->invalid->null)
565 const INT32 ninvalid = hwnd->ninvalid;
566 const GDI_RGN* cinvalid = hwnd->cinvalid;
571 std::vector<SDL_Rect> rects;
572 for (INT32 x = 0; x < ninvalid; x++)
574 auto& rgn = cinvalid[x];
575 rects.push_back({ rgn.x, rgn.y, rgn.w, rgn.h });
578 sdl->push(std::move(rects));
579 return sdl_push_user_event(SDL_EVENT_USER_UPDATE);
582void SdlContext::sdl_client_cleanup(
int exit_code,
const std::string& error_msg)
584 rdpSettings* settings = context()->settings;
585 WINPR_ASSERT(settings);
587 _rdpThreadRunning =
false;
588 bool showError =
false;
590 WLog_Print(getWLog(), WLOG_INFO,
"Authentication only, exit status %s [%" PRId32
"]",
591 sdl::error::exitCodeToTag(exit_code), exit_code);
596 case sdl::error::SUCCESS:
597 case sdl::error::DISCONNECT:
598 case sdl::error::LOGOFF:
599 case sdl::error::DISCONNECT_BY_USER:
600 case sdl::error::CONNECT_CANCELLED:
604 getDialog().showError(error_msg);
611 getDialog().show(
false);
613 _exitCode = exit_code;
614 std::ignore = sdl_push_user_event(SDL_EVENT_USER_QUIT);
618int SdlContext::sdl_client_thread_connect(std::string& error_msg)
620 auto instance = context()->instance;
621 WINPR_ASSERT(instance);
623 _rdpThreadRunning =
true;
624 BOOL rc = freerdp_connect(instance);
626 rdpSettings* settings = context()->settings;
627 WINPR_ASSERT(settings);
629 int exit_code = sdl::error::SUCCESS;
632 UINT32 error = freerdp_get_last_error(context());
633 exit_code = sdl::error::errorToExitCode(error);
638 DWORD code = freerdp_get_last_error(context());
639 freerdp_abort_connect_context(context());
640 WLog_Print(getWLog(), WLOG_ERROR,
"Authentication only, %s [0x%08" PRIx32
"] %s",
641 freerdp_get_last_error_name(code), code, freerdp_get_last_error_string(code));
647 DWORD code = freerdp_error_info(instance);
648 if (exit_code == sdl::error::SUCCESS)
652 exit_code = error_info_to_error(&code, &msg, &len);
658 auto last = freerdp_get_last_error(context());
659 if (error_msg.empty())
663 winpr_asprintf(&msg, &len,
"%s [0x%08" PRIx32
"]\n%s",
664 freerdp_get_last_error_name(last), last,
665 freerdp_get_last_error_string(last));
671 if (exit_code == sdl::error::SUCCESS)
673 if (last == FREERDP_ERROR_AUTHENTICATION_FAILED)
674 exit_code = sdl::error::AUTH_FAILURE;
675 else if (code == ERRINFO_SUCCESS)
676 exit_code = sdl::error::CONN_FAILED;
679 getDialog().show(
false);
685int SdlContext::sdl_client_thread_run(std::string& error_msg)
687 auto instance = context()->instance;
688 WINPR_ASSERT(instance);
690 int exit_code = sdl::error::SUCCESS;
691 while (!freerdp_shall_disconnect_context(context()))
693 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = {};
699 if (freerdp_focus_required(instance))
701 auto ctx = get_context(context());
704 auto& input = ctx->getInputChannelContext();
705 if (!input.keyboard_focus_in())
707 if (!input.keyboard_focus_in())
711 const DWORD nCount = freerdp_get_event_handles(context(), handles, ARRAYSIZE(handles));
715 WLog_Print(getWLog(), WLOG_ERROR,
"freerdp_get_event_handles failed");
719 const DWORD status = WaitForMultipleObjects(nCount, handles, FALSE, INFINITE);
721 if (status == WAIT_FAILED)
723 WLog_Print(getWLog(), WLOG_ERROR,
"WaitForMultipleObjects WAIT_FAILED");
727 if (!freerdp_check_event_handles(context()))
729 if (client_auto_reconnect(instance))
732 getDialog().show(
false);
741 if (freerdp_error_info(instance) == 0)
742 exit_code = sdl::error::CONN_FAILED;
745 if (freerdp_get_last_error(context()) == FREERDP_ERROR_SUCCESS)
746 WLog_Print(getWLog(), WLOG_ERROR,
"WaitForMultipleObjects failed with %" PRIu32
"",
748 if (freerdp_get_last_error(context()) == FREERDP_ERROR_SUCCESS)
749 WLog_Print(getWLog(), WLOG_ERROR,
"Failed to check FreeRDP event handles");
754 if (exit_code == sdl::error::SUCCESS)
758 char* emsg =
nullptr;
760 exit_code = error_info_to_error(&code, &emsg, &elen);
766 if ((code == ERRINFO_LOGOFF_BY_USER) &&
767 (freerdp_get_disconnect_ultimatum(context()) == Disconnect_Ultimatum_user_requested))
769 const char* msg =
"Error info says user did not initiate but disconnect ultimatum says "
770 "they did; treat this as a user logoff";
772 char* emsg =
nullptr;
774 winpr_asprintf(&emsg, &elen,
"%s", msg);
780 WLog_Print(getWLog(), WLOG_INFO,
"%s", msg);
781 exit_code = sdl::error::LOGOFF;
785 freerdp_disconnect(instance);
793DWORD SdlContext::rdpThreadRun(
SdlContext* sdl)
797 std::string error_msg;
798 int exit_code = sdl->sdl_client_thread_connect(error_msg);
799 if (exit_code == sdl::error::SUCCESS)
800 exit_code = sdl->sdl_client_thread_run(error_msg);
801 sdl->sdl_client_cleanup(exit_code, error_msg);
803 return static_cast<DWORD
>(exit_code);
806int SdlContext::error_info_to_error(DWORD* pcode,
char** msg,
size_t* len)
const
808 const DWORD code = freerdp_error_info(context()->instance);
809 const char* name = freerdp_get_error_info_name(code);
810 const char* str = freerdp_get_error_info_string(code);
811 const int exit_code = sdl::error::errorToExitCode(code);
813 winpr_asprintf(msg, len,
"Terminate with %s due to ERROR_INFO %s [0x%08" PRIx32
"]: %s",
814 sdl::error::errorToExitCodeTag(code), name, code, str);
815 SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION,
"%s", *msg);
821void SdlContext::applyMonitorOffset(SDL_WindowID window,
float& x,
float& y)
const
826 auto w = getWindowForId(window);
827 x -=
static_cast<float>(w->offsetX());
828 y -=
static_cast<float>(w->offsetY());
831static bool alignX(
const SDL_Rect& a,
const SDL_Rect& b)
833 if (a.x + a.w == b.x)
835 if (b.x + b.w == a.x)
840static bool alignY(
const SDL_Rect& a,
const SDL_Rect& b)
842 if (a.y + a.h == b.y)
844 if (b.y + b.h == a.y)
849std::vector<SDL_DisplayID>
850SdlContext::updateDisplayOffsetsForNeighbours(SDL_DisplayID
id,
851 const std::vector<SDL_DisplayID>& ignore)
853 auto first = _offsets.at(
id);
854 std::vector<SDL_DisplayID> neighbours;
856 for (
auto& entry : _offsets)
858 if (entry.first ==
id)
860 if (std::find(ignore.begin(), ignore.end(), entry.first) != ignore.end())
863 bool neighbor =
false;
864 if (alignX(entry.second.first, first.first))
866 if (entry.second.first.x < first.first.x)
867 entry.second.second.x = first.second.x - entry.second.second.w;
869 entry.second.second.x = first.second.x + first.second.w;
872 if (alignY(entry.second.first, first.first))
874 if (entry.second.first.y < first.first.y)
875 entry.second.second.y = first.second.y - entry.second.second.h;
877 entry.second.second.y = first.second.y + first.second.h;
882 neighbours.push_back(entry.first);
887void SdlContext::updateMonitorDataFromOffsets()
889 for (
auto& entry : _displays)
891 auto offsets = _offsets.at(entry.first);
892 entry.second.x = offsets.second.x;
893 entry.second.y = offsets.second.y;
896 for (
auto& entry : _windows)
898 const auto& monitor = _displays.at(entry.first);
899 entry.second.setMonitor(monitor);
903bool SdlContext::drawToWindow(
SdlWindow& window,
const std::vector<SDL_Rect>& rects)
908 auto gdi = context()->gdi;
911 auto size = window.rect();
913 std::unique_lock lock(_critical);
914 auto surface = _primary.get();
917 window.setOffsetX(0);
918 window.setOffsetY(0);
919 if (gdi->width < size.w)
921 window.setOffsetX((size.w - gdi->width) / 2);
923 if (gdi->height < size.h)
925 window.setOffsetY((size.h - gdi->height) / 2);
928 _localScale = {
static_cast<float>(size.w) /
static_cast<float>(gdi->width),
929 static_cast<float>(size.h) /
static_cast<float>(gdi->height) };
930 if (!window.drawScaledRects(surface, _localScale, rects))
935 SDL_Point offset{ 0, 0 };
937 offset = { window.offsetX(), window.offsetY() };
938 if (!window.drawRects(surface, offset, rects))
942 window.updateSurface();
946bool SdlContext::minimizeAllWindows()
948 for (
auto& w : _windows)
953int SdlContext::exitCode()
const
958SDL_PixelFormat SdlContext::pixelFormat()
const
960 return _sdlPixelFormat;
963bool SdlContext::addDisplayWindow(SDL_DisplayID
id)
966 SDL_WINDOW_HIGH_PIXEL_DENSITY | SDL_WINDOW_FULLSCREEN | SDL_WINDOW_BORDERLESS;
967 auto title = sdl::utils::windowTitle(context()->settings);
968 auto w = SdlWindow::create(
id, title, flags);
969 _windows.emplace(w.id(), std::move(w));
973bool SdlContext::removeDisplayWindow(SDL_DisplayID
id)
975 for (
auto& w : _windows)
977 if (w.second.displayIndex() ==
id)
978 _windows.erase(w.first);
983bool SdlContext::detectDisplays()
986 auto display = SDL_GetDisplays(&count);
989 for (
int x = 0; x < count; x++)
991 const auto id = display[x];
992 addOrUpdateDisplay(
id);
998rdpMonitor SdlContext::getDisplay(SDL_DisplayID
id)
const
1000 return _displays.at(
id);
1003std::vector<SDL_DisplayID> SdlContext::getDisplayIds()
const
1005 std::vector<SDL_DisplayID> keys;
1006 keys.reserve(_displays.size());
1007 for (
const auto& entry : _displays)
1009 keys.push_back(entry.first);
1014const SdlWindow* SdlContext::getWindowForId(SDL_WindowID
id)
const
1016 auto it = _windows.find(
id);
1017 if (it == _windows.end())
1022SdlWindow* SdlContext::getWindowForId(SDL_WindowID
id)
1024 auto it = _windows.find(
id);
1025 if (it == _windows.end())
1032 if (_windows.empty())
1034 return &_windows.begin()->second;
1042sdlInput& SdlContext::getInputChannelContext()
1047sdlClip& SdlContext::getClipboardChannelContext()
1057wLog* SdlContext::getWLog()
1062bool SdlContext::moveMouseTo(
const SDL_FPoint& pos)
1064 auto window = SDL_GetMouseFocus();
1068 const auto id = SDL_GetWindowID(window);
1069 const auto spos = pixelToScreen(
id, pos);
1070 SDL_WarpMouseInWindow(window, spos.x, spos.y);
1074bool SdlContext::handleEvent(
const SDL_MouseMotionEvent& ev)
1078 if (!eventToPixelCoordinates(ev.windowID, copy))
1080 removeLocalScaling(copy.motion.x, copy.motion.y);
1081 removeLocalScaling(copy.motion.xrel, copy.motion.yrel);
1082 applyMonitorOffset(copy.motion.windowID, copy.motion.x, copy.motion.y);
1084 return SdlTouch::handleEvent(
this, copy.motion);
1087bool SdlContext::handleEvent(
const SDL_MouseWheelEvent& ev)
1091 if (!eventToPixelCoordinates(ev.windowID, copy))
1093 removeLocalScaling(copy.wheel.mouse_x, copy.wheel.mouse_y);
1094 return SdlTouch::handleEvent(
this, copy.wheel);
1097bool SdlContext::handleEvent(
const SDL_WindowEvent& ev)
1099 if (!getDisplayChannelContext().handleEvent(ev))
1102 auto window = getWindowForId(ev.windowID);
1107 const auto& r = window->rect();
1108 const auto& b = window->bounds();
1109 const auto& scale = window->scale();
1110 const auto& orientation = window->orientation();
1111 SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION,
1112 "%s: [%u] %dx%d-%dx%d {%dx%d-%dx%d}{scale=%f,orientation=%s}",
1113 sdl::utils::toString(ev.type).c_str(), ev.windowID, r.x, r.y, r.w, r.h, b.x,
1114 b.y, b.w, b.h,
static_cast<double>(scale),
1115 sdl::utils::toString(orientation).c_str());
1120 case SDL_EVENT_WINDOW_MOUSE_ENTER:
1121 return restoreCursor();
1122 case SDL_EVENT_WINDOW_DISPLAY_SCALE_CHANGED:
1125 if (!window->fill())
1127 if (!drawToWindow(*window))
1129 if (!restoreCursor())
1133 case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED:
1134 if (!window->fill())
1136 if (!drawToWindow(*window))
1138 if (!restoreCursor())
1141 case SDL_EVENT_WINDOW_MOVED:
1143 auto r = window->rect();
1144 auto id = window->id();
1145 SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION,
"%u: %dx%d-%dx%d",
id, r.x, r.y, r.w, r.h);
1148 case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
1150 SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION,
"Window closed, terminating RDP session...");
1151 freerdp_abort_connect_context(context());
1160bool SdlContext::handleEvent(
const SDL_DisplayEvent& ev)
1162 if (!getDisplayChannelContext().handleEvent(ev))
1167 case SDL_EVENT_DISPLAY_REMOVED:
1172 if (!SDL_GetDisplayBounds(ev.displayID, &r))
1174 const auto name = SDL_GetDisplayName(ev.displayID);
1177 const auto orientation = SDL_GetCurrentDisplayOrientation(ev.displayID);
1178 const auto scale = SDL_GetDisplayContentScale(ev.displayID);
1179 const auto mode = SDL_GetCurrentDisplayMode(ev.displayID);
1183 SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION,
1184 "%s: [%u, %s] %dx%d-%dx%d {orientation=%s, scale=%f}%s",
1185 sdl::utils::toString(ev.type).c_str(), ev.displayID, name, r.x, r.y, r.w,
1186 r.h, sdl::utils::toString(orientation).c_str(),
static_cast<double>(scale),
1187 sdl::utils::toString(mode).c_str());
1194bool SdlContext::handleEvent(
const SDL_MouseButtonEvent& ev)
1196 SDL_Event copy = {};
1198 if (!eventToPixelCoordinates(ev.windowID, copy))
1200 removeLocalScaling(copy.button.x, copy.button.y);
1201 applyMonitorOffset(copy.button.windowID, copy.button.x, copy.button.y);
1202 return SdlTouch::handleEvent(
this, copy.button);
1205bool SdlContext::handleEvent(
const SDL_TouchFingerEvent& ev)
1209 if (!eventToPixelCoordinates(ev.windowID, copy))
1211 removeLocalScaling(copy.tfinger.dx, copy.tfinger.dy);
1212 removeLocalScaling(copy.tfinger.x, copy.tfinger.y);
1213 applyMonitorOffset(copy.tfinger.windowID, copy.tfinger.x, copy.tfinger.y);
1214 return SdlTouch::handleEvent(
this, copy.tfinger);
1217void SdlContext::addOrUpdateDisplay(SDL_DisplayID
id)
1219 auto monitor = SdlWindow::query(
id,
false);
1220 _displays.emplace(
id, monitor);
1230 for (
auto& entry : _displays)
1233 std::ignore = SDL_GetDisplayBounds(entry.first, &bounds);
1236 pixel.w = entry.second.width;
1237 pixel.h = entry.second.height;
1238 _offsets.emplace(entry.first, std::pair{ bounds, pixel });
1245 const auto primary = SDL_GetPrimaryDisplay();
1246 std::vector<SDL_DisplayID> handled;
1247 handled.push_back(primary);
1249 auto neighbors = updateDisplayOffsetsForNeighbours(primary);
1250 while (!neighbors.empty())
1252 auto neighbor = *neighbors.begin();
1253 neighbors.pop_back();
1255 if (std::find(handled.begin(), handled.end(), neighbor) != handled.end())
1257 handled.push_back(neighbor);
1259 auto next = updateDisplayOffsetsForNeighbours(neighbor, handled);
1260 neighbors.insert(neighbors.end(), next.begin(), next.end());
1262 updateMonitorDataFromOffsets();
1265void SdlContext::deleteDisplay(SDL_DisplayID
id)
1267 _displays.erase(
id);
1270bool SdlContext::eventToPixelCoordinates(SDL_WindowID
id, SDL_Event& ev)
1272 auto w = getWindowForId(
id);
1277 auto renderer = SDL_GetRenderer(w->window());
1280 return SDL_ConvertEventToRenderCoordinates(renderer, &ev);
1283SDL_FPoint SdlContext::applyLocalScaling(
const SDL_FPoint& val)
const
1289 rval.x *= _localScale.x;
1290 rval.y *= _localScale.y;
1294void SdlContext::removeLocalScaling(
float& x,
float& y)
const
1302SDL_FPoint SdlContext::screenToPixel(SDL_WindowID
id,
const SDL_FPoint& pos)
1304 auto w = getWindowForId(
id);
1309 auto renderer = SDL_GetRenderer(w->window());
1314 if (!SDL_RenderCoordinatesFromWindow(renderer, pos.x, pos.y, &rpos.x, &rpos.y))
1316 removeLocalScaling(rpos.x, rpos.y);
1320SDL_FPoint SdlContext::pixelToScreen(SDL_WindowID
id,
const SDL_FPoint& pos)
1322 auto w = getWindowForId(
id);
1327 auto renderer = SDL_GetRenderer(w->window());
1332 if (!SDL_RenderCoordinatesToWindow(renderer, pos.x, pos.y, &rpos.x, &rpos.y))
1334 return applyLocalScaling(rpos);
1337SDL_FRect SdlContext::pixelToScreen(SDL_WindowID
id,
const SDL_FRect& pos)
1339 const auto fpos = pixelToScreen(
id, SDL_FPoint{ pos.x, pos.y });
1340 const auto size = pixelToScreen(
id, SDL_FPoint{ pos.w, pos.h });
1341 return { fpos.x, fpos.y, size.x, size.y };
1344bool SdlContext::handleEvent(
const SDL_Event& ev)
1346 if ((ev.type >= SDL_EVENT_DISPLAY_FIRST) && (ev.type <= SDL_EVENT_DISPLAY_LAST))
1348 const auto& dev = ev.display;
1349 return handleEvent(dev);
1351 if ((ev.type >= SDL_EVENT_WINDOW_FIRST) && (ev.type <= SDL_EVENT_WINDOW_LAST))
1353 const auto& wev = ev.window;
1354 return handleEvent(wev);
1358 case SDL_EVENT_FINGER_DOWN:
1359 case SDL_EVENT_FINGER_UP:
1360 case SDL_EVENT_FINGER_MOTION:
1362 const auto& cev = ev.tfinger;
1363 return handleEvent(cev);
1365 case SDL_EVENT_MOUSE_MOTION:
1367 const auto& cev = ev.motion;
1368 return handleEvent(cev);
1370 case SDL_EVENT_MOUSE_BUTTON_DOWN:
1371 case SDL_EVENT_MOUSE_BUTTON_UP:
1373 const auto& cev = ev.button;
1374 return handleEvent(cev);
1376 case SDL_EVENT_MOUSE_WHEEL:
1378 const auto& cev = ev.wheel;
1379 return handleEvent(cev);
1381 case SDL_EVENT_CLIPBOARD_UPDATE:
1383 const auto& cev = ev.clipboard;
1384 return getClipboardChannelContext().handleEvent(cev);
1386 case SDL_EVENT_KEY_DOWN:
1387 case SDL_EVENT_KEY_UP:
1389 const auto& cev = ev.key;
1390 return getInputChannelContext().handleEvent(cev);
1392 case SDL_EVENT_RENDER_TARGETS_RESET:
1393 case SDL_EVENT_RENDER_DEVICE_RESET:
1394 case SDL_EVENT_WILL_ENTER_FOREGROUND:
1401bool SdlContext::drawToWindows(
const std::vector<SDL_Rect>& rects)
1403 for (
auto& window : _windows)
1405 if (!drawToWindow(window.second, rects))
1412BOOL SdlContext::desktopResize(rdpContext* context)
1414 rdpGdi* gdi =
nullptr;
1415 rdpSettings* settings =
nullptr;
1416 auto sdl = get_context(context);
1419 WINPR_ASSERT(context);
1421 settings = context->settings;
1422 WINPR_ASSERT(settings);
1424 std::unique_lock lock(sdl->_critical);
1429 return sdl->createPrimary();
1433BOOL SdlContext::playSound(rdpContext* context,
const PLAY_SOUND_UPDATE* play_sound)
1436 WINPR_UNUSED(context);
1437 WINPR_UNUSED(play_sound);
1443BOOL SdlContext::beginPaint(rdpContext* context)
1445 auto gdi = context->gdi;
1447 WINPR_ASSERT(gdi->primary);
1449 HGDI_DC hdc = gdi->primary->hdc;
1455 WINPR_ASSERT(hwnd->invalid);
1456 hwnd->invalid->null = TRUE;
1462bool SdlContext::redraw(
bool suppress)
const
1467 auto gdi = context()->gdi;
1469 return gdi_send_suppress_output(gdi, suppress);
1472void SdlContext::setConnected(
bool val)
1477bool SdlContext::isConnected()
const
1482rdpContext* SdlContext::context()
const
1484 WINPR_ASSERT(_context);
1488rdpClientContext* SdlContext::common()
const
1490 return reinterpret_cast<rdpClientContext*
>(context());
1493bool SdlContext::setCursor(CursorType type)
1496 return restoreCursor();
1499bool SdlContext::setCursor(rdpPointer* cursor)
1502 return setCursor(CURSOR_IMAGE);
1505rdpPointer* SdlContext::cursor()
const
1510bool SdlContext::restoreCursor()
1512 WLog_Print(getWLog(), WLOG_DEBUG,
"restore cursor: %d", _cursorType);
1513 switch (_cursorType)
1516 if (!SDL_HideCursor())
1518 WLog_Print(getWLog(), WLOG_ERROR,
"SDL_HideCursor failed");
1522 setHasCursor(
false);
1525 case CURSOR_DEFAULT:
1527 auto def = SDL_GetDefaultCursor();
1528 if (!SDL_SetCursor(def))
1530 WLog_Print(getWLog(), WLOG_ERROR,
"SDL_SetCursor(default=%p) failed",
1531 static_cast<void*
>(def));
1534 if (!SDL_ShowCursor())
1536 WLog_Print(getWLog(), WLOG_ERROR,
"SDL_ShowCursor failed");
1544 return sdl_Pointer_Set_Process(
this);
1546 WLog_Print(getWLog(), WLOG_ERROR,
"Unknown cursorType %s",
1547 sdl::utils::toString(_cursorType).c_str());
1552void SdlContext::setMonitorIds(
const std::vector<SDL_DisplayID>& ids)
1554 _monitorIds.clear();
1557 _monitorIds.push_back(
id);
1561const std::vector<SDL_DisplayID>& SdlContext::monitorIds()
const
1566int64_t SdlContext::monitorId(uint32_t index)
const
1568 if (index >= _monitorIds.size())
1572 return _monitorIds.at(index);
1575void SdlContext::push(std::vector<SDL_Rect>&& rects)
1577 std::unique_lock lock(_queue_mux);
1578 _queue.emplace(std::move(rects));
1581std::vector<SDL_Rect> SdlContext::pop()
1583 std::unique_lock lock(_queue_mux);
1588 auto val = std::move(_queue.front());
1593bool SdlContext::setFullscreen(
bool enter,
bool forceOriginalDisplay)
1595 for (
const auto& window : _windows)
1597 if (!sdl_push_user_event(SDL_EVENT_USER_WINDOW_FULLSCREEN, &window.second, enter,
1598 forceOriginalDisplay))
1601 _fullscreen = enter;
1605bool SdlContext::setMinimized()
1607 return sdl_push_user_event(SDL_EVENT_USER_WINDOW_MINIMIZE);
1610bool SdlContext::grabMouse()
const
1615bool SdlContext::toggleGrabMouse()
1617 return setGrabMouse(!grabMouse());
1620bool SdlContext::setGrabMouse(
bool enter)
1626bool SdlContext::grabKeyboard()
const
1628 return _grabKeyboard;
1631bool SdlContext::toggleGrabKeyboard()
1633 return setGrabKeyboard(!grabKeyboard());
1636bool SdlContext::setGrabKeyboard(
bool enter)
1638 _grabKeyboard = enter;
1642bool SdlContext::setResizeable(
bool enable)
1644 const auto settings = context()->settings;
1647 bool use = (dyn && enable) || smart;
1649 for (
const auto& window : _windows)
1651 if (!sdl_push_user_event(SDL_EVENT_USER_WINDOW_RESIZEABLE, &window.second, use))
1659bool SdlContext::resizeable()
const
1664bool SdlContext::toggleResizeable()
1666 return setResizeable(!resizeable());
1669bool SdlContext::fullscreen()
const
1674bool SdlContext::toggleFullscreen()
1676 return setFullscreen(!fullscreen());
SdlContext(rdpContext *context)
object that handles clipboard context for the SDL3 client
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:
FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 param)
Sets a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
FREERDP_API BOOL freerdp_settings_set_bool(rdpSettings *settings, FreeRDP_Settings_Keys_Bool id, BOOL param)
Sets a BOOL settings value.