23#include "sdl_context.hpp"
24#include "sdl_config.hpp"
25#include "sdl_channels.hpp"
26#include "sdl_monitor.hpp"
27#include "sdl_pointer.hpp"
28#include "sdl_touch.hpp"
30#include <sdl_common_utils.hpp>
31#include <scoped_guard.hpp>
33#include "dialogs/sdl_dialogs.hpp"
35#if defined(WITH_WEBVIEW)
36#include <aad/sdl_webview.hpp>
40 : _context(context), _log(WLog_Get(CLIENT_TAG(
"SDL"))), _cursor(nullptr, sdl_Pointer_FreeCopy),
41 _rdpThreadRunning(false), _primary(nullptr, SDL_DestroySurface), _disp(this), _input(this),
42 _clip(this), _dialog(_log)
44 WINPR_ASSERT(context);
47 auto instance = _context->instance;
48 WINPR_ASSERT(instance);
50 instance->PreConnect = preConnect;
51 instance->PostConnect = postConnect;
52 instance->PostDisconnect = postDisconnect;
53 instance->PostFinalDisconnect = postFinalDisconnect;
54 instance->AuthenticateEx = sdl_authenticate_ex;
55 instance->VerifyCertificateEx = sdl_verify_certificate_ex;
56 instance->VerifyChangedCertificateEx = sdl_verify_changed_certificate_ex;
57 instance->LogonErrorInfo = sdl_logon_error_info;
58 instance->PresentGatewayMessage = sdl_present_gateway_message;
59 instance->ChooseSmartcard = sdl_choose_smartcard;
60 instance->RetryDialog = sdl_retry_dialog;
63 instance->GetAccessToken = sdl_webview_get_access_token;
65 instance->GetAccessToken = client_cli_get_access_token;
70void SdlContext::setHasCursor(
bool val)
72 this->_cursor_visible = val;
75bool SdlContext::hasCursor()
const
77 return _cursor_visible;
80void SdlContext::setMetadata()
83 if (!wmclass || (strlen(wmclass) == 0))
84 wmclass = SDL_CLIENT_UUID;
86 SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_IDENTIFIER_STRING, wmclass);
87 SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_NAME_STRING, SDL_CLIENT_NAME);
88 SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_VERSION_STRING, SDL_CLIENT_VERSION);
89 SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_CREATOR_STRING, SDL_CLIENT_VENDOR);
90 SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_COPYRIGHT_STRING, SDL_CLIENT_COPYRIGHT);
91 SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_URL_STRING, SDL_CLIENT_URL);
92 SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_TYPE_STRING, SDL_CLIENT_TYPE);
95int SdlContext::start()
97 _thread = std::thread(rdpThreadRun,
this);
101int SdlContext::join()
105 HANDLE
event = freerdp_abort_event(context());
106 if (!SetEvent(event))
113void SdlContext::cleanup()
115 std::unique_lock lock(_critical);
121bool SdlContext::shallAbort(
bool ignoreDialogs)
123 std::unique_lock lock(_critical);
124 if (freerdp_shall_disconnect_context(context()))
128 if (_rdpThreadRunning)
130 return !getDialog().isRunning();
137BOOL SdlContext::preConnect(freerdp* instance)
139 WINPR_ASSERT(instance);
140 WINPR_ASSERT(instance->context);
142 auto sdl = get_context(instance->context);
144 auto settings = instance->context->settings;
145 WINPR_ASSERT(settings);
160 if (PubSub_SubscribeChannelConnected(instance->context->pubSub,
161 sdl_OnChannelConnectedEventHandler) < 0)
163 if (PubSub_SubscribeChannelDisconnected(instance->context->pubSub,
164 sdl_OnChannelDisconnectedEventHandler) < 0)
170 UINT32 maxHeight = 0;
172 if (!sdl_detect_monitors(sdl, &maxWidth, &maxHeight))
175 if ((maxWidth != 0) && (maxHeight != 0) &&
178 WLog_Print(sdl->getWLog(), WLOG_INFO,
"Update size to %ux%u", maxWidth, maxHeight);
193 if (sm && (sw > 0) && (sh > 0))
197 WLog_Print(sdl->getWLog(), WLOG_WARN,
198 "/smart-sizing and /multimon are currently not supported, ignoring "
217 WLog_Print(sdl->getWLog(), WLOG_INFO,
218 "auth-only, but no password set. Please provide one.");
225 WLog_Print(sdl->getWLog(), WLOG_INFO,
"Authentication only. Don't connect SDL.");
228 if (!sdl->getInputChannelContext().initialize())
243BOOL SdlContext::postConnect(freerdp* instance)
245 WINPR_ASSERT(instance);
247 auto context = instance->context;
248 WINPR_ASSERT(context);
250 auto sdl = get_context(context);
253 sdl->getDialog().show(
false);
260 WLog_Print(sdl->getWLog(), WLOG_INFO,
261 "auth-only, but no password set. Please provide one.");
265 WLog_Print(sdl->getWLog(), WLOG_INFO,
"Authentication only. Don't connect to X.");
269 if (!sdl->waitForWindowsCreated())
272 sdl->_sdlPixelFormat = SDL_PIXELFORMAT_BGRA32;
273 if (!gdi_init(instance, PIXEL_FORMAT_BGRA32))
276 if (!sdl->createPrimary())
279 if (!sdl_register_pointer(instance->context->graphics))
282 WINPR_ASSERT(context->update);
284 context->update->BeginPaint = beginPaint;
285 context->update->EndPaint = endPaint;
286 context->update->PlaySound = playSound;
287 context->update->DesktopResize = desktopResize;
288 context->update->SetKeyboardIndicators = sdlInput::keyboard_set_indicators;
289 context->update->SetKeyboardImeStatus = sdlInput::keyboard_set_ime_status;
291 if (!sdl->setResizeable(
false))
297 sdl->setConnected(
true);
304void SdlContext::postDisconnect(freerdp* instance)
309 if (!instance->context)
312 auto sdl = get_context(instance->context);
313 sdl->setConnected(
false);
318void SdlContext::postFinalDisconnect(freerdp* instance)
323 if (!instance->context)
326 PubSub_UnsubscribeChannelConnected(instance->context->pubSub,
327 sdl_OnChannelConnectedEventHandler);
328 PubSub_UnsubscribeChannelDisconnected(instance->context->pubSub,
329 sdl_OnChannelDisconnectedEventHandler);
333bool SdlContext::createPrimary()
335 auto gdi = context()->gdi;
338 _primary = SDLSurfacePtr(
339 SDL_CreateSurfaceFrom(
static_cast<int>(gdi->width),
static_cast<int>(gdi->height),
340 pixelFormat(), gdi->primary_buffer,
static_cast<int>(gdi->stride)),
345 SDL_SetSurfaceBlendMode(_primary.get(), SDL_BLENDMODE_NONE);
346 SDL_Rect surfaceRect = { 0, 0, gdi->width, gdi->height };
347 SDL_FillSurfaceRect(_primary.get(), &surfaceRect,
348 SDL_MapSurfaceRGBA(_primary.get(), 0, 0, 0, 0xff));
353bool SdlContext::createWindows()
355 auto settings = context()->settings;
356 const auto& title = windowTitle();
358 ScopeGuard guard1([&]() { _windowsCreatedEvent.set(); });
364 for (UINT32 x = 0; x < windowCount; x++)
366 auto id = monitorId(x);
371 freerdp_settings_get_pointer_array_writable(settings, FreeRDP_MonitorDefArray, x));
373 originX = std::min<Sint32>(monitor->x, originX);
374 originY = std::min<Sint32>(monitor->y, originY);
377 for (UINT32 x = 0; x < windowCount; x++)
379 auto id = monitorId(x);
384 freerdp_settings_get_pointer_array_writable(settings, FreeRDP_MonitorDefArray, x));
386 Uint32 w = WINPR_ASSERTING_INT_CAST(Uint32, monitor->width);
387 Uint32 h = WINPR_ASSERTING_INT_CAST(Uint32, monitor->height);
391 if (_windowWidth > 0)
396 if (_windowHeigth > 0)
402 Uint32 flags = SDL_WINDOW_HIGH_PIXEL_DENSITY;
407 flags |= SDL_WINDOW_FULLSCREEN;
412 flags |= SDL_WINDOW_BORDERLESS;
416 flags |= SDL_WINDOW_BORDERLESS;
418 auto did = WINPR_ASSERTING_INT_CAST(SDL_DisplayID,
id);
419 auto window = SdlWindow::create(did, title, flags, w, h);
423 window.setOffsetX(originX - monitor->x);
424 window.setOffsetY(originY - monitor->y);
427 _windows.insert({ window.id(), std::move(window) });
433bool SdlContext::updateWindowList()
435 std::vector<rdpMonitor> list;
436 list.reserve(_windows.size());
437 for (
const auto& win : _windows)
438 list.push_back(win.second.monitor(_windows.size() == 1));
444bool SdlContext::updateWindow(SDL_WindowID
id)
450 auto& w = _windows.at(
id);
451 auto m = w.monitor(
true);
455 m.attributes.physicalWidth =
static_cast<UINT32
>(r.w);
456 m.attributes.physicalHeight =
static_cast<UINT32
>(r.h);
461std::string SdlContext::windowTitle()
const
463 const char* prefix =
"FreeRDP:";
471 const auto addPort = (port != 3389);
473 std::stringstream ss;
474 ss << prefix <<
" " << name;
482bool SdlContext::waitForWindowsCreated()
485 std::unique_lock<CriticalSection> lock(_critical);
486 _windowsCreatedEvent.clear();
487 if (!sdl_push_user_event(SDL_EVENT_USER_CREATE_WINDOWS,
this))
491 HANDLE handles[] = { _windowsCreatedEvent.handle(), freerdp_abort_event(context()) };
493 const DWORD rc = WaitForMultipleObjects(ARRAYSIZE(handles), handles, FALSE, INFINITE);
507BOOL SdlContext::endPaint(rdpContext* context)
509 auto sdl = get_context(context);
512 auto gdi = context->gdi;
514 WINPR_ASSERT(gdi->primary);
516 HGDI_DC hdc = gdi->primary->hdc;
522 WINPR_ASSERT(hwnd->invalid || (hwnd->ninvalid == 0));
524 if (hwnd->invalid->null)
527 WINPR_ASSERT(hwnd->invalid);
528 if (gdi->suppressOutput || hwnd->invalid->null)
531 const INT32 ninvalid = hwnd->ninvalid;
532 const GDI_RGN* cinvalid = hwnd->cinvalid;
537 std::vector<SDL_Rect> rects;
538 for (INT32 x = 0; x < ninvalid; x++)
540 auto& rgn = cinvalid[x];
541 rects.push_back({ rgn.x, rgn.y, rgn.w, rgn.h });
544 sdl->push(std::move(rects));
545 return sdl_push_user_event(SDL_EVENT_USER_UPDATE);
548void SdlContext::sdl_client_cleanup(
int exit_code,
const std::string& error_msg)
550 rdpSettings* settings = context()->settings;
551 WINPR_ASSERT(settings);
553 _rdpThreadRunning =
false;
554 bool showError =
false;
556 WLog_Print(getWLog(), WLOG_INFO,
"Authentication only, exit status %s [%" PRId32
"]",
557 sdl::error::exitCodeToTag(exit_code), exit_code);
562 case sdl::error::SUCCESS:
563 case sdl::error::DISCONNECT:
564 case sdl::error::LOGOFF:
565 case sdl::error::DISCONNECT_BY_USER:
566 case sdl::error::CONNECT_CANCELLED:
570 getDialog().showError(error_msg);
577 getDialog().show(
false);
579 _exitCode = exit_code;
580 std::ignore = sdl_push_user_event(SDL_EVENT_USER_QUIT);
584int SdlContext::sdl_client_thread_connect(std::string& error_msg)
586 auto instance = context()->instance;
587 WINPR_ASSERT(instance);
589 _rdpThreadRunning =
true;
590 BOOL rc = freerdp_connect(instance);
592 rdpSettings* settings = context()->settings;
593 WINPR_ASSERT(settings);
595 int exit_code = sdl::error::SUCCESS;
598 UINT32 error = freerdp_get_last_error(context());
599 exit_code = sdl::error::errorToExitCode(error);
604 DWORD code = freerdp_get_last_error(context());
605 freerdp_abort_connect_context(context());
606 WLog_Print(getWLog(), WLOG_ERROR,
"Authentication only, %s [0x%08" PRIx32
"] %s",
607 freerdp_get_last_error_name(code), code, freerdp_get_last_error_string(code));
613 DWORD code = freerdp_error_info(instance);
614 if (exit_code == sdl::error::SUCCESS)
618 exit_code = error_info_to_error(&code, &msg, &len);
624 auto last = freerdp_get_last_error(context());
625 if (error_msg.empty())
629 winpr_asprintf(&msg, &len,
"%s [0x%08" PRIx32
"]\n%s",
630 freerdp_get_last_error_name(last), last,
631 freerdp_get_last_error_string(last));
637 if (exit_code == sdl::error::SUCCESS)
639 if (last == FREERDP_ERROR_AUTHENTICATION_FAILED)
640 exit_code = sdl::error::AUTH_FAILURE;
641 else if (code == ERRINFO_SUCCESS)
642 exit_code = sdl::error::CONN_FAILED;
645 getDialog().show(
false);
651int SdlContext::sdl_client_thread_run(std::string& error_msg)
653 auto instance = context()->instance;
654 WINPR_ASSERT(instance);
656 int exit_code = sdl::error::SUCCESS;
657 while (!freerdp_shall_disconnect_context(context()))
659 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = {};
665 if (freerdp_focus_required(instance))
667 auto ctx = get_context(context());
670 auto& input = ctx->getInputChannelContext();
671 if (!input.keyboard_focus_in())
673 if (!input.keyboard_focus_in())
677 const DWORD nCount = freerdp_get_event_handles(context(), handles, ARRAYSIZE(handles));
681 WLog_Print(getWLog(), WLOG_ERROR,
"freerdp_get_event_handles failed");
685 const DWORD status = WaitForMultipleObjects(nCount, handles, FALSE, INFINITE);
687 if (status == WAIT_FAILED)
689 WLog_Print(getWLog(), WLOG_ERROR,
"WaitForMultipleObjects WAIT_FAILED");
693 if (!freerdp_check_event_handles(context()))
695 if (client_auto_reconnect(instance))
698 getDialog().show(
false);
707 if (freerdp_error_info(instance) == 0)
708 exit_code = sdl::error::CONN_FAILED;
711 if (freerdp_get_last_error(context()) == FREERDP_ERROR_SUCCESS)
712 WLog_Print(getWLog(), WLOG_ERROR,
"WaitForMultipleObjects failed with %" PRIu32
"",
714 if (freerdp_get_last_error(context()) == FREERDP_ERROR_SUCCESS)
715 WLog_Print(getWLog(), WLOG_ERROR,
"Failed to check FreeRDP event handles");
720 if (exit_code == sdl::error::SUCCESS)
724 char* emsg =
nullptr;
726 exit_code = error_info_to_error(&code, &emsg, &elen);
732 if ((code == ERRINFO_LOGOFF_BY_USER) &&
733 (freerdp_get_disconnect_ultimatum(context()) == Disconnect_Ultimatum_user_requested))
735 const char* msg =
"Error info says user did not initiate but disconnect ultimatum says "
736 "they did; treat this as a user logoff";
738 char* emsg =
nullptr;
740 winpr_asprintf(&emsg, &elen,
"%s", msg);
746 WLog_Print(getWLog(), WLOG_INFO,
"%s", msg);
747 exit_code = sdl::error::LOGOFF;
751 freerdp_disconnect(instance);
759DWORD SdlContext::rdpThreadRun(
SdlContext* sdl)
763 std::string error_msg;
764 int exit_code = sdl->sdl_client_thread_connect(error_msg);
765 if (exit_code == sdl::error::SUCCESS)
766 exit_code = sdl->sdl_client_thread_run(error_msg);
767 sdl->sdl_client_cleanup(exit_code, error_msg);
769 return static_cast<DWORD
>(exit_code);
772int SdlContext::error_info_to_error(DWORD* pcode,
char** msg,
size_t* len)
const
774 const DWORD code = freerdp_error_info(context()->instance);
775 const char* name = freerdp_get_error_info_name(code);
776 const char* str = freerdp_get_error_info_string(code);
777 const int exit_code = sdl::error::errorToExitCode(code);
779 winpr_asprintf(msg, len,
"Terminate with %s due to ERROR_INFO %s [0x%08" PRIx32
"]: %s",
780 sdl::error::errorToExitCodeTag(code), name, code, str);
781 SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION,
"%s", *msg);
787void SdlContext::applyMonitorOffset(SDL_WindowID window,
float& x,
float& y)
const
792 auto w = getWindowForId(window);
793 x -=
static_cast<float>(w->offsetX());
794 y -=
static_cast<float>(w->offsetY());
797static bool alignX(
const SDL_Rect& a,
const SDL_Rect& b)
799 if (a.x + a.w == b.x)
801 if (b.x + b.w == a.x)
806static bool alignY(
const SDL_Rect& a,
const SDL_Rect& b)
808 if (a.y + a.h == b.y)
810 if (b.y + b.h == a.y)
815std::vector<SDL_DisplayID>
816SdlContext::updateDisplayOffsetsForNeighbours(SDL_DisplayID
id,
817 const std::vector<SDL_DisplayID>& ignore)
819 auto first = _offsets.at(
id);
820 std::vector<SDL_DisplayID> neighbours;
822 for (
auto& entry : _offsets)
824 if (entry.first ==
id)
826 if (std::find(ignore.begin(), ignore.end(), entry.first) != ignore.end())
829 bool neighbor =
false;
830 if (alignX(entry.second.first, first.first))
832 if (entry.second.first.x < first.first.x)
833 entry.second.second.x = first.second.x - entry.second.second.w;
835 entry.second.second.x = first.second.x + first.second.w;
838 if (alignY(entry.second.first, first.first))
840 if (entry.second.first.y < first.first.y)
841 entry.second.second.y = first.second.y - entry.second.second.h;
843 entry.second.second.y = first.second.y + first.second.h;
848 neighbours.push_back(entry.first);
853void SdlContext::updateMonitorDataFromOffsets()
855 for (
auto& entry : _displays)
857 auto offsets = _offsets.at(entry.first);
858 entry.second.x = offsets.second.x;
859 entry.second.y = offsets.second.y;
862 for (
auto& entry : _windows)
864 const auto& monitor = _displays.at(entry.first);
865 entry.second.setMonitor(monitor);
869bool SdlContext::drawToWindow(
SdlWindow& window,
const std::vector<SDL_Rect>& rects)
874 auto gdi = context()->gdi;
877 auto size = window.rect();
879 std::unique_lock lock(_critical);
880 auto surface = _primary.get();
884 window.setOffsetX(0);
885 window.setOffsetY(0);
886 if (gdi->width < size.w)
888 window.setOffsetX((size.w - gdi->width) / 2);
890 if (gdi->height < size.h)
892 window.setOffsetY((size.h - gdi->height) / 2);
895 _localScale = {
static_cast<float>(size.w) /
static_cast<float>(gdi->width),
896 static_cast<float>(size.h) /
static_cast<float>(gdi->height) };
897 if (!window.drawScaledRects(surface, _localScale, rects))
902 SDL_Point offset{ 0, 0 };
904 offset = { window.offsetX(), window.offsetY() };
905 if (!window.drawRects(surface, offset, rects))
909 window.updateSurface();
913bool SdlContext::minimizeAllWindows()
915 for (
auto& w : _windows)
920int SdlContext::exitCode()
const
925SDL_PixelFormat SdlContext::pixelFormat()
const
927 return _sdlPixelFormat;
930bool SdlContext::addDisplayWindow(SDL_DisplayID
id)
933 SDL_WINDOW_HIGH_PIXEL_DENSITY | SDL_WINDOW_FULLSCREEN | SDL_WINDOW_BORDERLESS;
934 auto title = sdl::utils::windowTitle(context()->settings);
935 auto w = SdlWindow::create(
id, title, flags);
936 _windows.emplace(w.id(), std::move(w));
940bool SdlContext::removeDisplayWindow(SDL_DisplayID
id)
942 for (
auto& w : _windows)
944 if (w.second.displayIndex() ==
id)
945 _windows.erase(w.first);
950bool SdlContext::detectDisplays()
953 auto display = SDL_GetDisplays(&count);
956 for (
int x = 0; x < count; x++)
958 const auto id = display[x];
959 addOrUpdateDisplay(
id);
965rdpMonitor SdlContext::getDisplay(SDL_DisplayID
id)
const
967 return _displays.at(
id);
970std::vector<SDL_DisplayID> SdlContext::getDisplayIds()
const
972 std::vector<SDL_DisplayID> keys;
973 keys.reserve(_displays.size());
974 for (
const auto& entry : _displays)
976 keys.push_back(entry.first);
981const SdlWindow* SdlContext::getWindowForId(SDL_WindowID
id)
const
983 auto it = _windows.find(
id);
984 if (it == _windows.end())
989SdlWindow* SdlContext::getWindowForId(SDL_WindowID
id)
991 auto it = _windows.find(
id);
992 if (it == _windows.end())
999 if (_windows.empty())
1001 return &_windows.begin()->second;
1009sdlInput& SdlContext::getInputChannelContext()
1014sdlClip& SdlContext::getClipboardChannelContext()
1024wLog* SdlContext::getWLog()
1029bool SdlContext::moveMouseTo(
const SDL_FPoint& pos)
1031 auto window = SDL_GetMouseFocus();
1035 const auto id = SDL_GetWindowID(window);
1036 const auto spos = pixelToScreen(
id, pos);
1037 SDL_WarpMouseInWindow(window, spos.x, spos.y);
1041bool SdlContext::handleEvent(
const SDL_MouseMotionEvent& ev)
1045 if (!eventToPixelCoordinates(ev.windowID, copy))
1047 removeLocalScaling(copy.motion.x, copy.motion.y);
1048 removeLocalScaling(copy.motion.xrel, copy.motion.yrel);
1049 applyMonitorOffset(copy.motion.windowID, copy.motion.x, copy.motion.y);
1051 return SdlTouch::handleEvent(
this, copy.motion);
1054bool SdlContext::handleEvent(
const SDL_MouseWheelEvent& ev)
1058 if (!eventToPixelCoordinates(ev.windowID, copy))
1060 removeLocalScaling(copy.wheel.mouse_x, copy.wheel.mouse_y);
1061 return SdlTouch::handleEvent(
this, copy.wheel);
1064bool SdlContext::handleEvent(
const SDL_WindowEvent& ev)
1066 if (!getDisplayChannelContext().handleEvent(ev))
1069 auto window = getWindowForId(ev.windowID);
1074 const auto& r = window->rect();
1075 const auto& b = window->bounds();
1076 const auto& scale = window->scale();
1077 const auto& orientation = window->orientation();
1078 SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION,
1079 "%s: [%u] %dx%d-%dx%d {%dx%d-%dx%d}{scale=%f,orientation=%s}",
1080 sdl::utils::toString(ev.type).c_str(), ev.windowID, r.x, r.y, r.w, r.h, b.x,
1081 b.y, b.w, b.h,
static_cast<double>(scale),
1082 sdl::utils::toString(orientation).c_str());
1087 case SDL_EVENT_WINDOW_MOUSE_ENTER:
1088 return restoreCursor();
1089 case SDL_EVENT_WINDOW_DISPLAY_SCALE_CHANGED:
1092 if (!window->fill())
1094 if (!drawToWindow(*window))
1096 if (!restoreCursor())
1100 case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED:
1101 if (!window->fill())
1103 if (!drawToWindow(*window))
1105 if (!restoreCursor())
1108 case SDL_EVENT_WINDOW_MOVED:
1110 auto r = window->rect();
1111 auto id = window->id();
1112 SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION,
"%u: %dx%d-%dx%d",
id, r.x, r.y, r.w, r.h);
1115 case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
1117 SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION,
"Window closed, terminating RDP session...");
1118 freerdp_abort_connect_context(context());
1127bool SdlContext::handleEvent(
const SDL_DisplayEvent& ev)
1129 if (!getDisplayChannelContext().handleEvent(ev))
1134 case SDL_EVENT_DISPLAY_REMOVED:
1139 if (!SDL_GetDisplayBounds(ev.displayID, &r))
1141 const auto name = SDL_GetDisplayName(ev.displayID);
1144 const auto orientation = SDL_GetCurrentDisplayOrientation(ev.displayID);
1145 const auto scale = SDL_GetDisplayContentScale(ev.displayID);
1146 const auto mode = SDL_GetCurrentDisplayMode(ev.displayID);
1150 SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION,
1151 "%s: [%u, %s] %dx%d-%dx%d {orientation=%s, scale=%f}%s",
1152 sdl::utils::toString(ev.type).c_str(), ev.displayID, name, r.x, r.y, r.w,
1153 r.h, sdl::utils::toString(orientation).c_str(),
static_cast<double>(scale),
1154 sdl::utils::toString(mode).c_str());
1161bool SdlContext::handleEvent(
const SDL_MouseButtonEvent& ev)
1163 SDL_Event copy = {};
1165 if (!eventToPixelCoordinates(ev.windowID, copy))
1167 removeLocalScaling(copy.button.x, copy.button.y);
1168 applyMonitorOffset(copy.button.windowID, copy.button.x, copy.button.y);
1169 return SdlTouch::handleEvent(
this, copy.button);
1172bool SdlContext::handleEvent(
const SDL_TouchFingerEvent& ev)
1176 if (!eventToPixelCoordinates(ev.windowID, copy))
1178 removeLocalScaling(copy.tfinger.dx, copy.tfinger.dy);
1179 removeLocalScaling(copy.tfinger.x, copy.tfinger.y);
1180 applyMonitorOffset(copy.tfinger.windowID, copy.tfinger.x, copy.tfinger.y);
1181 return SdlTouch::handleEvent(
this, copy.tfinger);
1184void SdlContext::addOrUpdateDisplay(SDL_DisplayID
id)
1186 auto monitor = SdlWindow::query(
id,
false);
1187 _displays.emplace(
id, monitor);
1197 for (
auto& entry : _displays)
1200 std::ignore = SDL_GetDisplayBounds(entry.first, &bounds);
1203 pixel.w = entry.second.width;
1204 pixel.h = entry.second.height;
1205 _offsets.emplace(entry.first, std::pair{ bounds, pixel });
1212 const auto primary = SDL_GetPrimaryDisplay();
1213 std::vector<SDL_DisplayID> handled;
1214 handled.push_back(primary);
1216 auto neighbors = updateDisplayOffsetsForNeighbours(primary);
1217 while (!neighbors.empty())
1219 auto neighbor = *neighbors.begin();
1220 neighbors.pop_back();
1222 if (std::find(handled.begin(), handled.end(), neighbor) != handled.end())
1224 handled.push_back(neighbor);
1226 auto next = updateDisplayOffsetsForNeighbours(neighbor, handled);
1227 neighbors.insert(neighbors.end(), next.begin(), next.end());
1229 updateMonitorDataFromOffsets();
1232void SdlContext::deleteDisplay(SDL_DisplayID
id)
1234 _displays.erase(
id);
1237bool SdlContext::eventToPixelCoordinates(SDL_WindowID
id, SDL_Event& ev)
1239 auto w = getWindowForId(
id);
1244 auto renderer = SDL_GetRenderer(w->window());
1247 return SDL_ConvertEventToRenderCoordinates(renderer, &ev);
1250SDL_FPoint SdlContext::applyLocalScaling(
const SDL_FPoint& val)
const
1252 if (!useLocalScale())
1256 rval.x *= _localScale.x;
1257 rval.y *= _localScale.y;
1261void SdlContext::removeLocalScaling(
float& x,
float& y)
const
1263 if (!useLocalScale())
1269SDL_FPoint SdlContext::screenToPixel(SDL_WindowID
id,
const SDL_FPoint& pos)
1271 auto w = getWindowForId(
id);
1276 auto renderer = SDL_GetRenderer(w->window());
1281 if (!SDL_RenderCoordinatesFromWindow(renderer, pos.x, pos.y, &rpos.x, &rpos.y))
1283 removeLocalScaling(rpos.x, rpos.y);
1287SDL_FPoint SdlContext::pixelToScreen(SDL_WindowID
id,
const SDL_FPoint& pos)
1289 auto w = getWindowForId(
id);
1294 auto renderer = SDL_GetRenderer(w->window());
1299 if (!SDL_RenderCoordinatesToWindow(renderer, pos.x, pos.y, &rpos.x, &rpos.y))
1301 return applyLocalScaling(rpos);
1304SDL_FRect SdlContext::pixelToScreen(SDL_WindowID
id,
const SDL_FRect& pos,
bool round)
1306 const auto fpos = pixelToScreen(
id, SDL_FPoint{ pos.x, pos.y });
1307 const auto size = pixelToScreen(
id, SDL_FPoint{ pos.w, pos.h });
1308 SDL_FRect r{ fpos.x, fpos.y, size.x, size.y };
1311 r.w = std::ceil(r.w);
1312 r.h = std::ceil(r.h);
1313 r.x = std::floor(r.x);
1314 r.y = std::floor(r.y);
1319bool SdlContext::handleEvent(
const SDL_Event& ev)
1321 if ((ev.type >= SDL_EVENT_DISPLAY_FIRST) && (ev.type <= SDL_EVENT_DISPLAY_LAST))
1323 const auto& dev = ev.display;
1324 return handleEvent(dev);
1326 if ((ev.type >= SDL_EVENT_WINDOW_FIRST) && (ev.type <= SDL_EVENT_WINDOW_LAST))
1328 const auto& wev = ev.window;
1329 return handleEvent(wev);
1333 case SDL_EVENT_RENDER_TARGETS_RESET:
1334 case SDL_EVENT_RENDER_DEVICE_RESET:
1335 case SDL_EVENT_WILL_ENTER_FOREGROUND:
1346 case SDL_EVENT_FINGER_DOWN:
1347 case SDL_EVENT_FINGER_UP:
1348 case SDL_EVENT_FINGER_MOTION:
1350 const auto& cev = ev.tfinger;
1351 return handleEvent(cev);
1353 case SDL_EVENT_MOUSE_MOTION:
1356 const auto& cev = ev.motion;
1357 return handleEvent(cev);
1359 case SDL_EVENT_MOUSE_BUTTON_DOWN:
1360 case SDL_EVENT_MOUSE_BUTTON_UP:
1362 const auto& cev = ev.button;
1363 return handleEvent(cev);
1365 case SDL_EVENT_MOUSE_WHEEL:
1367 const auto& cev = ev.wheel;
1368 return handleEvent(cev);
1370 case SDL_EVENT_CLIPBOARD_UPDATE:
1372 const auto& cev = ev.clipboard;
1373 return getClipboardChannelContext().handleEvent(cev);
1375 case SDL_EVENT_KEY_DOWN:
1376 case SDL_EVENT_KEY_UP:
1378 const auto& cev = ev.key;
1379 return getInputChannelContext().handleEvent(cev);
1386bool SdlContext::useLocalScale()
const
1391 const auto dynResize =
1395 return !dynResize && !fs && !multimon;
1398bool SdlContext::drawToWindows(
const std::vector<SDL_Rect>& rects)
1400 for (
auto& window : _windows)
1402 if (!drawToWindow(window.second, rects))
1409BOOL SdlContext::desktopResize(rdpContext* context)
1411 rdpGdi* gdi =
nullptr;
1412 rdpSettings* settings =
nullptr;
1413 auto sdl = get_context(context);
1416 WINPR_ASSERT(context);
1418 settings = context->settings;
1419 WINPR_ASSERT(settings);
1421 std::unique_lock lock(sdl->_critical);
1426 return sdl->createPrimary();
1430BOOL SdlContext::playSound(rdpContext* context,
const PLAY_SOUND_UPDATE* play_sound)
1433 WINPR_UNUSED(context);
1434 WINPR_UNUSED(play_sound);
1440BOOL SdlContext::beginPaint(rdpContext* context)
1442 auto gdi = context->gdi;
1444 WINPR_ASSERT(gdi->primary);
1446 HGDI_DC hdc = gdi->primary->hdc;
1452 WINPR_ASSERT(hwnd->invalid);
1453 hwnd->invalid->null = TRUE;
1459bool SdlContext::redraw(
bool suppress)
const
1464 auto gdi = context()->gdi;
1466 return gdi_send_suppress_output(gdi, suppress);
1469void SdlContext::setConnected(
bool val)
1474bool SdlContext::isConnected()
const
1479rdpContext* SdlContext::context()
const
1481 WINPR_ASSERT(_context);
1485rdpClientContext* SdlContext::common()
const
1487 return reinterpret_cast<rdpClientContext*
>(context());
1490bool SdlContext::setCursor(CursorType type)
1493 return restoreCursor();
1496bool SdlContext::setCursor(
const rdpPointer* cursor)
1498 _cursor = { sdl_Pointer_Copy(cursor), sdl_Pointer_FreeCopy };
1499 return setCursor(CURSOR_IMAGE);
1502rdpPointer* SdlContext::cursor()
const
1504 return _cursor.get();
1507bool SdlContext::restoreCursor()
1509 WLog_Print(getWLog(), WLOG_DEBUG,
"restore cursor: %d", _cursorType);
1510 switch (_cursorType)
1513 if (!SDL_HideCursor())
1515 WLog_Print(getWLog(), WLOG_ERROR,
"SDL_HideCursor failed");
1519 setHasCursor(
false);
1522 case CURSOR_DEFAULT:
1524 auto def = SDL_GetDefaultCursor();
1525 if (!SDL_SetCursor(def))
1527 WLog_Print(getWLog(), WLOG_ERROR,
"SDL_SetCursor(default=%p) failed",
1528 static_cast<void*
>(def));
1531 if (!SDL_ShowCursor())
1533 WLog_Print(getWLog(), WLOG_ERROR,
"SDL_ShowCursor failed");
1541 return sdl_Pointer_Set_Process(
this);
1543 WLog_Print(getWLog(), WLOG_ERROR,
"Unknown cursorType %s",
1544 sdl::utils::toString(_cursorType).c_str());
1549void SdlContext::setMonitorIds(
const std::vector<SDL_DisplayID>& ids)
1551 _monitorIds.clear();
1554 _monitorIds.push_back(
id);
1558const std::vector<SDL_DisplayID>& SdlContext::monitorIds()
const
1563int64_t SdlContext::monitorId(uint32_t index)
const
1565 if (index >= _monitorIds.size())
1569 return _monitorIds.at(index);
1572void SdlContext::push(std::vector<SDL_Rect>&& rects)
1574 std::unique_lock lock(_queue_mux);
1575 _queue.emplace(std::move(rects));
1578std::vector<SDL_Rect> SdlContext::pop()
1580 std::unique_lock lock(_queue_mux);
1585 auto val = std::move(_queue.front());
1590bool SdlContext::setFullscreen(
bool enter,
bool forceOriginalDisplay)
1592 for (
const auto& window : _windows)
1594 if (!sdl_push_user_event(SDL_EVENT_USER_WINDOW_FULLSCREEN, &window.second, enter,
1595 forceOriginalDisplay))
1598 _fullscreen = enter;
1602bool SdlContext::setMinimized()
1604 return sdl_push_user_event(SDL_EVENT_USER_WINDOW_MINIMIZE);
1607bool SdlContext::grabMouse()
const
1612bool SdlContext::toggleGrabMouse()
1614 return setGrabMouse(!grabMouse());
1617bool SdlContext::setGrabMouse(
bool enter)
1623bool SdlContext::grabKeyboard()
const
1625 return _grabKeyboard;
1628bool SdlContext::toggleGrabKeyboard()
1630 return setGrabKeyboard(!grabKeyboard());
1633bool SdlContext::setGrabKeyboard(
bool enter)
1635 _grabKeyboard = enter;
1639bool SdlContext::setResizeable(
bool enable)
1641 const auto settings = context()->settings;
1644 bool use = (dyn && enable) || smart;
1646 for (
const auto& window : _windows)
1648 if (!sdl_push_user_event(SDL_EVENT_USER_WINDOW_RESIZEABLE, &window.second, use))
1656bool SdlContext::resizeable()
const
1661bool SdlContext::toggleResizeable()
1663 return setResizeable(!resizeable());
1666bool SdlContext::fullscreen()
const
1671bool SdlContext::toggleFullscreen()
1673 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 BOOL freerdp_settings_set_bool(rdpSettings *settings, FreeRDP_Settings_Keys_Bool id, BOOL val)
Sets a BOOL settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 val)
Sets a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_monitor_def_array_sorted(rdpSettings *settings, const rdpMonitor *monitors, size_t count)
Sort monitor array according to:
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.