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 if (PubSub_SubscribeChannelConnected(instance->context->pubSub,
159 sdl_OnChannelConnectedEventHandler) < 0)
161 if (PubSub_SubscribeChannelDisconnected(instance->context->pubSub,
162 sdl_OnChannelDisconnectedEventHandler) < 0)
168 UINT32 maxHeight = 0;
170 if (!sdl_detect_monitors(sdl, &maxWidth, &maxHeight))
173 if ((maxWidth != 0) && (maxHeight != 0) &&
176 WLog_Print(sdl->getWLog(), WLOG_INFO,
"Update size to %ux%u", maxWidth, maxHeight);
191 if (sm && (sw > 0) && (sh > 0))
195 WLog_Print(sdl->getWLog(), WLOG_WARN,
196 "/smart-sizing and /multimon are currently not supported, ignoring "
215 WLog_Print(sdl->getWLog(), WLOG_INFO,
216 "auth-only, but no password set. Please provide one.");
223 WLog_Print(sdl->getWLog(), WLOG_INFO,
"Authentication only. Don't connect SDL.");
226 if (!sdl->getInputChannelContext().initialize())
241BOOL SdlContext::postConnect(freerdp* instance)
243 WINPR_ASSERT(instance);
245 auto context = instance->context;
246 WINPR_ASSERT(context);
248 auto sdl = get_context(context);
251 sdl->getDialog().show(
false);
258 WLog_Print(sdl->getWLog(), WLOG_INFO,
259 "auth-only, but no password set. Please provide one.");
263 WLog_Print(sdl->getWLog(), WLOG_INFO,
"Authentication only. Don't connect to X.");
267 if (!sdl->waitForWindowsCreated())
270 sdl->_sdlPixelFormat = SDL_PIXELFORMAT_BGRA32;
271 if (!gdi_init(instance, PIXEL_FORMAT_BGRA32))
274 if (!sdl->createPrimary())
277 if (!sdl_register_pointer(instance->context->graphics))
280 WINPR_ASSERT(context->update);
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;
289 if (!sdl->setResizeable(
false))
295 sdl->setConnected(
true);
302void SdlContext::postDisconnect(freerdp* instance)
307 if (!instance->context)
310 auto sdl = get_context(instance->context);
311 sdl->setConnected(
false);
316void SdlContext::postFinalDisconnect(freerdp* instance)
321 if (!instance->context)
324 PubSub_UnsubscribeChannelConnected(instance->context->pubSub,
325 sdl_OnChannelConnectedEventHandler);
326 PubSub_UnsubscribeChannelDisconnected(instance->context->pubSub,
327 sdl_OnChannelDisconnectedEventHandler);
331bool SdlContext::createPrimary()
333 auto gdi = context()->gdi;
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)),
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));
351bool SdlContext::createWindows()
353 auto settings = context()->settings;
354 const auto& title = windowTitle();
356 ScopeGuard guard1([&]() { _windowsCreatedEvent.set(); });
362 for (UINT32 x = 0; x < windowCount; x++)
364 auto id = monitorId(x);
369 freerdp_settings_get_pointer_array_writable(settings, FreeRDP_MonitorDefArray, x));
371 originX = std::min<Sint32>(monitor->x, originX);
372 originY = std::min<Sint32>(monitor->y, originY);
375 for (UINT32 x = 0; x < windowCount; x++)
377 auto id = monitorId(x);
382 freerdp_settings_get_pointer_array_writable(settings, FreeRDP_MonitorDefArray, x));
384 Uint32 w = WINPR_ASSERTING_INT_CAST(Uint32, monitor->width);
385 Uint32 h = WINPR_ASSERTING_INT_CAST(Uint32, monitor->height);
389 if (_windowWidth > 0)
394 if (_windowHeigth > 0)
400 Uint32 flags = SDL_WINDOW_HIGH_PIXEL_DENSITY;
405 flags |= SDL_WINDOW_FULLSCREEN;
410 flags |= SDL_WINDOW_BORDERLESS;
414 flags |= SDL_WINDOW_BORDERLESS;
416 auto did = WINPR_ASSERTING_INT_CAST(SDL_DisplayID,
id);
417 auto window = SdlWindow::create(did, title, flags, w, h);
421 window.setOffsetX(originX - monitor->x);
422 window.setOffsetY(originY - monitor->y);
425 _windows.insert({ window.id(), std::move(window) });
431bool SdlContext::updateWindowList()
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));
442bool SdlContext::updateWindow(SDL_WindowID
id)
448 auto& w = _windows.at(
id);
449 auto m = w.monitor(
true);
453 m.attributes.physicalWidth =
static_cast<UINT32
>(r.w);
454 m.attributes.physicalHeight =
static_cast<UINT32
>(r.h);
459std::string SdlContext::windowTitle()
const
461 const char* prefix =
"FreeRDP:";
469 const auto addPort = (port != 3389);
471 std::stringstream ss;
472 ss << prefix <<
" " << name;
480bool SdlContext::waitForWindowsCreated()
483 std::unique_lock<CriticalSection> lock(_critical);
484 _windowsCreatedEvent.clear();
485 if (!sdl_push_user_event(SDL_EVENT_USER_CREATE_WINDOWS,
this))
489 HANDLE handles[] = { _windowsCreatedEvent.handle(), freerdp_abort_event(context()) };
491 const DWORD rc = WaitForMultipleObjects(ARRAYSIZE(handles), handles, FALSE, INFINITE);
505BOOL SdlContext::endPaint(rdpContext* context)
507 auto sdl = get_context(context);
510 auto gdi = context->gdi;
512 WINPR_ASSERT(gdi->primary);
514 HGDI_DC hdc = gdi->primary->hdc;
520 WINPR_ASSERT(hwnd->invalid || (hwnd->ninvalid == 0));
522 if (hwnd->invalid->null)
525 WINPR_ASSERT(hwnd->invalid);
526 if (gdi->suppressOutput || hwnd->invalid->null)
529 const INT32 ninvalid = hwnd->ninvalid;
530 const GDI_RGN* cinvalid = hwnd->cinvalid;
535 std::vector<SDL_Rect> rects;
536 for (INT32 x = 0; x < ninvalid; x++)
538 auto& rgn = cinvalid[x];
539 rects.push_back({ rgn.x, rgn.y, rgn.w, rgn.h });
542 sdl->push(std::move(rects));
543 return sdl_push_user_event(SDL_EVENT_USER_UPDATE);
546void SdlContext::sdl_client_cleanup(
int exit_code,
const std::string& error_msg)
548 rdpSettings* settings = context()->settings;
549 WINPR_ASSERT(settings);
551 _rdpThreadRunning =
false;
552 bool showError =
false;
554 WLog_Print(getWLog(), WLOG_INFO,
"Authentication only, exit status %s [%" PRId32
"]",
555 sdl::error::exitCodeToTag(exit_code), exit_code);
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:
568 getDialog().showError(error_msg);
575 getDialog().show(
false);
577 _exitCode = exit_code;
578 std::ignore = sdl_push_user_event(SDL_EVENT_USER_QUIT);
582int SdlContext::sdl_client_thread_connect(std::string& error_msg)
584 auto instance = context()->instance;
585 WINPR_ASSERT(instance);
587 _rdpThreadRunning =
true;
588 BOOL rc = freerdp_connect(instance);
590 rdpSettings* settings = context()->settings;
591 WINPR_ASSERT(settings);
593 int exit_code = sdl::error::SUCCESS;
596 UINT32 error = freerdp_get_last_error(context());
597 exit_code = sdl::error::errorToExitCode(error);
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));
611 DWORD code = freerdp_error_info(instance);
612 if (exit_code == sdl::error::SUCCESS)
616 exit_code = error_info_to_error(&code, &msg, &len);
622 auto last = freerdp_get_last_error(context());
623 if (error_msg.empty())
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));
635 if (exit_code == sdl::error::SUCCESS)
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;
643 getDialog().show(
false);
649int SdlContext::sdl_client_thread_run(std::string& error_msg)
651 auto instance = context()->instance;
652 WINPR_ASSERT(instance);
654 int exit_code = sdl::error::SUCCESS;
655 while (!freerdp_shall_disconnect_context(context()))
657 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = {};
663 if (freerdp_focus_required(instance))
665 auto ctx = get_context(context());
668 auto& input = ctx->getInputChannelContext();
669 if (!input.keyboard_focus_in())
671 if (!input.keyboard_focus_in())
675 const DWORD nCount = freerdp_get_event_handles(context(), handles, ARRAYSIZE(handles));
679 WLog_Print(getWLog(), WLOG_ERROR,
"freerdp_get_event_handles failed");
683 const DWORD status = WaitForMultipleObjects(nCount, handles, FALSE, INFINITE);
685 if (status == WAIT_FAILED)
687 WLog_Print(getWLog(), WLOG_ERROR,
"WaitForMultipleObjects WAIT_FAILED");
691 if (!freerdp_check_event_handles(context()))
693 if (client_auto_reconnect(instance))
696 getDialog().show(
false);
705 if (freerdp_error_info(instance) == 0)
706 exit_code = sdl::error::CONN_FAILED;
709 if (freerdp_get_last_error(context()) == FREERDP_ERROR_SUCCESS)
710 WLog_Print(getWLog(), WLOG_ERROR,
"WaitForMultipleObjects failed with %" PRIu32
"",
712 if (freerdp_get_last_error(context()) == FREERDP_ERROR_SUCCESS)
713 WLog_Print(getWLog(), WLOG_ERROR,
"Failed to check FreeRDP event handles");
718 if (exit_code == sdl::error::SUCCESS)
722 char* emsg =
nullptr;
724 exit_code = error_info_to_error(&code, &emsg, &elen);
730 if ((code == ERRINFO_LOGOFF_BY_USER) &&
731 (freerdp_get_disconnect_ultimatum(context()) == Disconnect_Ultimatum_user_requested))
733 const char* msg =
"Error info says user did not initiate but disconnect ultimatum says "
734 "they did; treat this as a user logoff";
736 char* emsg =
nullptr;
738 winpr_asprintf(&emsg, &elen,
"%s", msg);
744 WLog_Print(getWLog(), WLOG_INFO,
"%s", msg);
745 exit_code = sdl::error::LOGOFF;
749 freerdp_disconnect(instance);
757DWORD SdlContext::rdpThreadRun(
SdlContext* sdl)
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);
767 return static_cast<DWORD
>(exit_code);
770int SdlContext::error_info_to_error(DWORD* pcode,
char** msg,
size_t* len)
const
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);
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);
785void SdlContext::applyMonitorOffset(SDL_WindowID window,
float& x,
float& y)
const
790 auto w = getWindowForId(window);
791 x -=
static_cast<float>(w->offsetX());
792 y -=
static_cast<float>(w->offsetY());
795static bool alignX(
const SDL_Rect& a,
const SDL_Rect& b)
797 if (a.x + a.w == b.x)
799 if (b.x + b.w == a.x)
804static bool alignY(
const SDL_Rect& a,
const SDL_Rect& b)
806 if (a.y + a.h == b.y)
808 if (b.y + b.h == a.y)
813std::vector<SDL_DisplayID>
814SdlContext::updateDisplayOffsetsForNeighbours(SDL_DisplayID
id,
815 const std::vector<SDL_DisplayID>& ignore)
817 auto first = _offsets.at(
id);
818 std::vector<SDL_DisplayID> neighbours;
820 for (
auto& entry : _offsets)
822 if (entry.first ==
id)
824 if (std::find(ignore.begin(), ignore.end(), entry.first) != ignore.end())
827 bool neighbor =
false;
828 if (alignX(entry.second.first, first.first))
830 if (entry.second.first.x < first.first.x)
831 entry.second.second.x = first.second.x - entry.second.second.w;
833 entry.second.second.x = first.second.x + first.second.w;
836 if (alignY(entry.second.first, first.first))
838 if (entry.second.first.y < first.first.y)
839 entry.second.second.y = first.second.y - entry.second.second.h;
841 entry.second.second.y = first.second.y + first.second.h;
846 neighbours.push_back(entry.first);
851void SdlContext::updateMonitorDataFromOffsets()
853 for (
auto& entry : _displays)
855 auto offsets = _offsets.at(entry.first);
856 entry.second.x = offsets.second.x;
857 entry.second.y = offsets.second.y;
860 for (
auto& entry : _windows)
862 const auto& monitor = _displays.at(entry.first);
863 entry.second.setMonitor(monitor);
867bool SdlContext::drawToWindow(
SdlWindow& window,
const std::vector<SDL_Rect>& rects)
872 auto gdi = context()->gdi;
875 auto size = window.rect();
877 std::unique_lock lock(_critical);
878 auto surface = _primary.get();
881 window.setOffsetX(0);
882 window.setOffsetY(0);
883 if (gdi->width < size.w)
885 window.setOffsetX((size.w - gdi->width) / 2);
887 if (gdi->height < size.h)
889 window.setOffsetY((size.h - gdi->height) / 2);
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))
899 SDL_Point offset{ 0, 0 };
901 offset = { window.offsetX(), window.offsetY() };
902 if (!window.drawRects(surface, offset, rects))
906 window.updateSurface();
910bool SdlContext::minimizeAllWindows()
912 for (
auto& w : _windows)
917int SdlContext::exitCode()
const
922SDL_PixelFormat SdlContext::pixelFormat()
const
924 return _sdlPixelFormat;
927bool SdlContext::addDisplayWindow(SDL_DisplayID
id)
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));
937bool SdlContext::removeDisplayWindow(SDL_DisplayID
id)
939 for (
auto& w : _windows)
941 if (w.second.displayIndex() ==
id)
942 _windows.erase(w.first);
947bool SdlContext::detectDisplays()
950 auto display = SDL_GetDisplays(&count);
953 for (
int x = 0; x < count; x++)
955 const auto id = display[x];
956 addOrUpdateDisplay(
id);
962rdpMonitor SdlContext::getDisplay(SDL_DisplayID
id)
const
964 return _displays.at(
id);
967std::vector<SDL_DisplayID> SdlContext::getDisplayIds()
const
969 std::vector<SDL_DisplayID> keys;
970 keys.reserve(_displays.size());
971 for (
const auto& entry : _displays)
973 keys.push_back(entry.first);
978const SdlWindow* SdlContext::getWindowForId(SDL_WindowID
id)
const
980 auto it = _windows.find(
id);
981 if (it == _windows.end())
986SdlWindow* SdlContext::getWindowForId(SDL_WindowID
id)
988 auto it = _windows.find(
id);
989 if (it == _windows.end())
996 if (_windows.empty())
998 return &_windows.begin()->second;
1006sdlInput& SdlContext::getInputChannelContext()
1011sdlClip& SdlContext::getClipboardChannelContext()
1021wLog* SdlContext::getWLog()
1026bool SdlContext::moveMouseTo(
const SDL_FPoint& pos)
1028 auto window = SDL_GetMouseFocus();
1032 const auto id = SDL_GetWindowID(window);
1033 const auto spos = pixelToScreen(
id, pos);
1034 SDL_WarpMouseInWindow(window, spos.x, spos.y);
1038bool SdlContext::handleEvent(
const SDL_MouseMotionEvent& ev)
1042 if (!eventToPixelCoordinates(ev.windowID, copy))
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);
1048 return SdlTouch::handleEvent(
this, copy.motion);
1051bool SdlContext::handleEvent(
const SDL_MouseWheelEvent& ev)
1055 if (!eventToPixelCoordinates(ev.windowID, copy))
1057 removeLocalScaling(copy.wheel.mouse_x, copy.wheel.mouse_y);
1058 return SdlTouch::handleEvent(
this, copy.wheel);
1061bool SdlContext::handleEvent(
const SDL_WindowEvent& ev)
1063 if (!getDisplayChannelContext().handleEvent(ev))
1066 auto window = getWindowForId(ev.windowID);
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());
1084 case SDL_EVENT_WINDOW_MOUSE_ENTER:
1085 return restoreCursor();
1086 case SDL_EVENT_WINDOW_DISPLAY_SCALE_CHANGED:
1089 if (!window->fill())
1091 if (!drawToWindow(*window))
1093 if (!restoreCursor())
1097 case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED:
1098 if (!window->fill())
1100 if (!drawToWindow(*window))
1102 if (!restoreCursor())
1105 case SDL_EVENT_WINDOW_MOVED:
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);
1112 case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
1114 SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION,
"Window closed, terminating RDP session...");
1115 freerdp_abort_connect_context(context());
1124bool SdlContext::handleEvent(
const SDL_DisplayEvent& ev)
1126 if (!getDisplayChannelContext().handleEvent(ev))
1131 case SDL_EVENT_DISPLAY_REMOVED:
1136 if (!SDL_GetDisplayBounds(ev.displayID, &r))
1138 const auto name = SDL_GetDisplayName(ev.displayID);
1141 const auto orientation = SDL_GetCurrentDisplayOrientation(ev.displayID);
1142 const auto scale = SDL_GetDisplayContentScale(ev.displayID);
1143 const auto mode = SDL_GetCurrentDisplayMode(ev.displayID);
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());
1158bool SdlContext::handleEvent(
const SDL_MouseButtonEvent& ev)
1160 SDL_Event copy = {};
1162 if (!eventToPixelCoordinates(ev.windowID, copy))
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);
1169bool SdlContext::handleEvent(
const SDL_TouchFingerEvent& ev)
1173 if (!eventToPixelCoordinates(ev.windowID, copy))
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);
1181void SdlContext::addOrUpdateDisplay(SDL_DisplayID
id)
1183 auto monitor = SdlWindow::query(
id,
false);
1184 _displays.emplace(
id, monitor);
1194 for (
auto& entry : _displays)
1197 std::ignore = SDL_GetDisplayBounds(entry.first, &bounds);
1200 pixel.w = entry.second.width;
1201 pixel.h = entry.second.height;
1202 _offsets.emplace(entry.first, std::pair{ bounds, pixel });
1209 const auto primary = SDL_GetPrimaryDisplay();
1210 std::vector<SDL_DisplayID> handled;
1211 handled.push_back(primary);
1213 auto neighbors = updateDisplayOffsetsForNeighbours(primary);
1214 while (!neighbors.empty())
1216 auto neighbor = *neighbors.begin();
1217 neighbors.pop_back();
1219 if (std::find(handled.begin(), handled.end(), neighbor) != handled.end())
1221 handled.push_back(neighbor);
1223 auto next = updateDisplayOffsetsForNeighbours(neighbor, handled);
1224 neighbors.insert(neighbors.end(), next.begin(), next.end());
1226 updateMonitorDataFromOffsets();
1229void SdlContext::deleteDisplay(SDL_DisplayID
id)
1231 _displays.erase(
id);
1234bool SdlContext::eventToPixelCoordinates(SDL_WindowID
id, SDL_Event& ev)
1236 auto w = getWindowForId(
id);
1241 auto renderer = SDL_GetRenderer(w->window());
1244 return SDL_ConvertEventToRenderCoordinates(renderer, &ev);
1247SDL_FPoint SdlContext::applyLocalScaling(
const SDL_FPoint& val)
const
1253 rval.x *= _localScale.x;
1254 rval.y *= _localScale.y;
1258void SdlContext::removeLocalScaling(
float& x,
float& y)
const
1266SDL_FPoint SdlContext::screenToPixel(SDL_WindowID
id,
const SDL_FPoint& pos)
1268 auto w = getWindowForId(
id);
1273 auto renderer = SDL_GetRenderer(w->window());
1278 if (!SDL_RenderCoordinatesFromWindow(renderer, pos.x, pos.y, &rpos.x, &rpos.y))
1280 removeLocalScaling(rpos.x, rpos.y);
1284SDL_FPoint SdlContext::pixelToScreen(SDL_WindowID
id,
const SDL_FPoint& pos)
1286 auto w = getWindowForId(
id);
1291 auto renderer = SDL_GetRenderer(w->window());
1296 if (!SDL_RenderCoordinatesToWindow(renderer, pos.x, pos.y, &rpos.x, &rpos.y))
1298 return applyLocalScaling(rpos);
1301SDL_FRect SdlContext::pixelToScreen(SDL_WindowID
id,
const SDL_FRect& pos)
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 };
1308bool SdlContext::handleEvent(
const SDL_Event& ev)
1310 if ((ev.type >= SDL_EVENT_DISPLAY_FIRST) && (ev.type <= SDL_EVENT_DISPLAY_LAST))
1312 const auto& dev = ev.display;
1313 return handleEvent(dev);
1315 if ((ev.type >= SDL_EVENT_WINDOW_FIRST) && (ev.type <= SDL_EVENT_WINDOW_LAST))
1317 const auto& wev = ev.window;
1318 return handleEvent(wev);
1322 case SDL_EVENT_FINGER_DOWN:
1323 case SDL_EVENT_FINGER_UP:
1324 case SDL_EVENT_FINGER_MOTION:
1326 const auto& cev = ev.tfinger;
1327 return handleEvent(cev);
1329 case SDL_EVENT_MOUSE_MOTION:
1331 const auto& cev = ev.motion;
1332 return handleEvent(cev);
1334 case SDL_EVENT_MOUSE_BUTTON_DOWN:
1335 case SDL_EVENT_MOUSE_BUTTON_UP:
1337 const auto& cev = ev.button;
1338 return handleEvent(cev);
1340 case SDL_EVENT_MOUSE_WHEEL:
1342 const auto& cev = ev.wheel;
1343 return handleEvent(cev);
1345 case SDL_EVENT_CLIPBOARD_UPDATE:
1347 const auto& cev = ev.clipboard;
1348 return getClipboardChannelContext().handleEvent(cev);
1350 case SDL_EVENT_KEY_DOWN:
1351 case SDL_EVENT_KEY_UP:
1353 const auto& cev = ev.key;
1354 return getInputChannelContext().handleEvent(cev);
1356 case SDL_EVENT_RENDER_TARGETS_RESET:
1357 case SDL_EVENT_RENDER_DEVICE_RESET:
1358 case SDL_EVENT_WILL_ENTER_FOREGROUND:
1365bool SdlContext::drawToWindows(
const std::vector<SDL_Rect>& rects)
1367 for (
auto& window : _windows)
1369 if (!drawToWindow(window.second, rects))
1376BOOL SdlContext::desktopResize(rdpContext* context)
1378 rdpGdi* gdi =
nullptr;
1379 rdpSettings* settings =
nullptr;
1380 auto sdl = get_context(context);
1383 WINPR_ASSERT(context);
1385 settings = context->settings;
1386 WINPR_ASSERT(settings);
1388 std::unique_lock lock(sdl->_critical);
1393 return sdl->createPrimary();
1397BOOL SdlContext::playSound(rdpContext* context,
const PLAY_SOUND_UPDATE* play_sound)
1400 WINPR_UNUSED(context);
1401 WINPR_UNUSED(play_sound);
1407BOOL SdlContext::beginPaint(rdpContext* context)
1409 auto gdi = context->gdi;
1411 WINPR_ASSERT(gdi->primary);
1413 HGDI_DC hdc = gdi->primary->hdc;
1419 WINPR_ASSERT(hwnd->invalid);
1420 hwnd->invalid->null = TRUE;
1426bool SdlContext::redraw(
bool suppress)
const
1431 auto gdi = context()->gdi;
1433 return gdi_send_suppress_output(gdi, suppress);
1436void SdlContext::setConnected(
bool val)
1441bool SdlContext::isConnected()
const
1446rdpContext* SdlContext::context()
const
1448 WINPR_ASSERT(_context);
1452rdpClientContext* SdlContext::common()
const
1454 return reinterpret_cast<rdpClientContext*
>(context());
1457bool SdlContext::setCursor(CursorType type)
1460 return restoreCursor();
1463bool SdlContext::setCursor(rdpPointer* cursor)
1466 return setCursor(CURSOR_IMAGE);
1469rdpPointer* SdlContext::cursor()
const
1474bool SdlContext::restoreCursor()
1476 WLog_Print(getWLog(), WLOG_DEBUG,
"restore cursor: %d", _cursorType);
1477 switch (_cursorType)
1480 if (!SDL_HideCursor())
1482 WLog_Print(getWLog(), WLOG_ERROR,
"SDL_HideCursor failed");
1486 setHasCursor(
false);
1489 case CURSOR_DEFAULT:
1491 auto def = SDL_GetDefaultCursor();
1492 if (!SDL_SetCursor(def))
1494 WLog_Print(getWLog(), WLOG_ERROR,
"SDL_SetCursor(default=%p) failed",
1495 static_cast<void*
>(def));
1498 if (!SDL_ShowCursor())
1500 WLog_Print(getWLog(), WLOG_ERROR,
"SDL_ShowCursor failed");
1508 return sdl_Pointer_Set_Process(
this);
1510 WLog_Print(getWLog(), WLOG_ERROR,
"Unknown cursorType %s",
1511 sdl::utils::toString(_cursorType).c_str());
1516void SdlContext::setMonitorIds(
const std::vector<SDL_DisplayID>& ids)
1518 _monitorIds.clear();
1521 _monitorIds.push_back(
id);
1525const std::vector<SDL_DisplayID>& SdlContext::monitorIds()
const
1530int64_t SdlContext::monitorId(uint32_t index)
const
1532 if (index >= _monitorIds.size())
1536 return _monitorIds.at(index);
1539void SdlContext::push(std::vector<SDL_Rect>&& rects)
1541 std::unique_lock lock(_queue_mux);
1542 _queue.emplace(std::move(rects));
1545std::vector<SDL_Rect> SdlContext::pop()
1547 std::unique_lock lock(_queue_mux);
1552 auto val = std::move(_queue.front());
1557bool SdlContext::setFullscreen(
bool enter,
bool forceOriginalDisplay)
1559 for (
const auto& window : _windows)
1561 if (!sdl_push_user_event(SDL_EVENT_USER_WINDOW_FULLSCREEN, &window.second, enter,
1562 forceOriginalDisplay))
1565 _fullscreen = enter;
1569bool SdlContext::setMinimized()
1571 return sdl_push_user_event(SDL_EVENT_USER_WINDOW_MINIMIZE);
1574bool SdlContext::grabMouse()
const
1579bool SdlContext::toggleGrabMouse()
1581 return setGrabMouse(!grabMouse());
1584bool SdlContext::setGrabMouse(
bool enter)
1590bool SdlContext::grabKeyboard()
const
1592 return _grabKeyboard;
1595bool SdlContext::toggleGrabKeyboard()
1597 return setGrabKeyboard(!grabKeyboard());
1600bool SdlContext::setGrabKeyboard(
bool enter)
1602 _grabKeyboard = enter;
1606bool SdlContext::setResizeable(
bool enable)
1608 const auto settings = context()->settings;
1611 bool use = (dyn && enable) || smart;
1613 for (
const auto& window : _windows)
1615 if (!sdl_push_user_event(SDL_EVENT_USER_WINDOW_RESIZEABLE, &window.second, use))
1623bool SdlContext::resizeable()
const
1628bool SdlContext::toggleResizeable()
1630 return setResizeable(!resizeable());
1633bool SdlContext::fullscreen()
const
1638bool SdlContext::toggleFullscreen()
1640 return setFullscreen(!fullscreen());
SdlContext(rdpContext *context)
object that handles clipboard context for the SDL3 client
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.