24#include <freerdp/config.h>
30#include <freerdp/constants.h>
31#include <freerdp/freerdp.h>
32#include <freerdp/gdi/gdi.h>
33#include <freerdp/streamdump.h>
34#include <freerdp/utils/signal.h>
36#include <freerdp/channels/channels.h>
37#include <freerdp/client/channels.h>
38#include <freerdp/client/cliprdr.h>
39#include <freerdp/client/cmdline.h>
40#include <freerdp/client/file.h>
42#include <freerdp/log.h>
43#include <winpr/assert.h>
44#include <winpr/config.h>
46#include <winpr/synch.h>
49#if !defined(__MINGW32__)
50#include <SDL3/SDL_main.h>
52#include <SDL3/SDL_hints.h>
53#include <SDL3/SDL_oldnames.h>
54#include <SDL3/SDL_video.h>
56#include <sdl_config.hpp>
58#include "dialogs/sdl_connection_dialog_hider.hpp"
59#include "dialogs/sdl_dialogs.hpp"
60#include "scoped_guard.hpp"
61#include "sdl_channels.hpp"
62#include "sdl_disp.hpp"
63#include "sdl_freerdp.hpp"
65#include "sdl_monitor.hpp"
66#include "sdl_pointer.hpp"
67#include "sdl_prefs.hpp"
68#include "sdl_touch.hpp"
69#include "sdl_utils.hpp"
71#if defined(WITH_WEBVIEW)
72#include <aad/sdl_webview.hpp>
75#define SDL_TAG CLIENT_TAG("SDL")
81 SDL_EXIT_DISCONNECT = 1,
83 SDL_EXIT_IDLE_TIMEOUT = 3,
84 SDL_EXIT_LOGON_TIMEOUT = 4,
85 SDL_EXIT_CONN_REPLACED = 5,
86 SDL_EXIT_OUT_OF_MEMORY = 6,
87 SDL_EXIT_CONN_DENIED = 7,
88 SDL_EXIT_CONN_DENIED_FIPS = 8,
89 SDL_EXIT_USER_PRIVILEGES = 9,
90 SDL_EXIT_FRESH_CREDENTIALS_REQUIRED = 10,
91 SDL_EXIT_DISCONNECT_BY_USER = 11,
94 SDL_EXIT_LICENSE_INTERNAL = 16,
95 SDL_EXIT_LICENSE_NO_LICENSE_SERVER = 17,
96 SDL_EXIT_LICENSE_NO_LICENSE = 18,
97 SDL_EXIT_LICENSE_BAD_CLIENT_MSG = 19,
98 SDL_EXIT_LICENSE_HWID_DOESNT_MATCH = 20,
99 SDL_EXIT_LICENSE_BAD_CLIENT = 21,
100 SDL_EXIT_LICENSE_CANT_FINISH_PROTOCOL = 22,
101 SDL_EXIT_LICENSE_CLIENT_ENDED_PROTOCOL = 23,
102 SDL_EXIT_LICENSE_BAD_CLIENT_ENCRYPTION = 24,
103 SDL_EXIT_LICENSE_CANT_UPGRADE = 25,
104 SDL_EXIT_LICENSE_NO_REMOTE_CONNECTIONS = 26,
110 SDL_EXIT_PARSE_ARGUMENTS = 128,
111 SDL_EXIT_MEMORY = 129,
112 SDL_EXIT_PROTOCOL = 130,
113 SDL_EXIT_CONN_FAILED = 131,
114 SDL_EXIT_AUTH_FAILURE = 132,
115 SDL_EXIT_NEGO_FAILURE = 133,
116 SDL_EXIT_LOGON_FAILURE = 134,
117 SDL_EXIT_ACCOUNT_LOCKED_OUT = 135,
118 SDL_EXIT_PRE_CONNECT_FAILED = 136,
119 SDL_EXIT_CONNECT_UNDEFINED = 137,
120 SDL_EXIT_POST_CONNECT_FAILED = 138,
121 SDL_EXIT_DNS_ERROR = 139,
122 SDL_EXIT_DNS_NAME_NOT_FOUND = 140,
123 SDL_EXIT_CONNECT_FAILED = 141,
124 SDL_EXIT_MCS_CONNECT_INITIAL_ERROR = 142,
125 SDL_EXIT_TLS_CONNECT_FAILED = 143,
126 SDL_EXIT_INSUFFICIENT_PRIVILEGES = 144,
127 SDL_EXIT_CONNECT_CANCELLED = 145,
129 SDL_EXIT_CONNECT_TRANSPORT_FAILED = 147,
130 SDL_EXIT_CONNECT_PASSWORD_EXPIRED = 148,
131 SDL_EXIT_CONNECT_PASSWORD_MUST_CHANGE = 149,
132 SDL_EXIT_CONNECT_KDC_UNREACHABLE = 150,
133 SDL_EXIT_CONNECT_ACCOUNT_DISABLED = 151,
134 SDL_EXIT_CONNECT_PASSWORD_CERTAINLY_EXPIRED = 152,
135 SDL_EXIT_CONNECT_CLIENT_REVOKED = 153,
136 SDL_EXIT_CONNECT_WRONG_PASSWORD = 154,
137 SDL_EXIT_CONNECT_ACCESS_DENIED = 155,
138 SDL_EXIT_CONNECT_ACCOUNT_RESTRICTION = 156,
139 SDL_EXIT_CONNECT_ACCOUNT_EXPIRED = 157,
140 SDL_EXIT_CONNECT_LOGON_TYPE_NOT_GRANTED = 158,
141 SDL_EXIT_CONNECT_NO_OR_MISSING_CREDENTIALS = 159,
142 SDL_EXIT_CONNECT_TARGET_BOOTING = 160,
144 SDL_EXIT_UNKNOWN = 255,
147struct sdl_exit_code_map_t
151 const char* code_tag;
154#define ENTRY(x, y) { x, y, #y }
155static const struct sdl_exit_code_map_t sdl_exit_code_map[] = {
156 ENTRY(FREERDP_ERROR_SUCCESS, SDL_EXIT_SUCCESS), ENTRY(FREERDP_ERROR_NONE, SDL_EXIT_DISCONNECT),
157 ENTRY(FREERDP_ERROR_NONE, SDL_EXIT_LOGOFF), ENTRY(FREERDP_ERROR_NONE, SDL_EXIT_IDLE_TIMEOUT),
158 ENTRY(FREERDP_ERROR_NONE, SDL_EXIT_LOGON_TIMEOUT),
159 ENTRY(FREERDP_ERROR_NONE, SDL_EXIT_CONN_REPLACED),
160 ENTRY(FREERDP_ERROR_NONE, SDL_EXIT_OUT_OF_MEMORY),
161 ENTRY(FREERDP_ERROR_NONE, SDL_EXIT_CONN_DENIED),
162 ENTRY(FREERDP_ERROR_NONE, SDL_EXIT_CONN_DENIED_FIPS),
163 ENTRY(FREERDP_ERROR_NONE, SDL_EXIT_USER_PRIVILEGES),
164 ENTRY(FREERDP_ERROR_NONE, SDL_EXIT_FRESH_CREDENTIALS_REQUIRED),
165 ENTRY(ERRINFO_LOGOFF_BY_USER, SDL_EXIT_DISCONNECT_BY_USER),
166 ENTRY(FREERDP_ERROR_NONE, SDL_EXIT_UNKNOWN),
169 ENTRY(FREERDP_ERROR_NONE, SDL_EXIT_LICENSE_INTERNAL),
170 ENTRY(FREERDP_ERROR_NONE, SDL_EXIT_LICENSE_NO_LICENSE_SERVER),
171 ENTRY(FREERDP_ERROR_NONE, SDL_EXIT_LICENSE_NO_LICENSE),
172 ENTRY(FREERDP_ERROR_NONE, SDL_EXIT_LICENSE_BAD_CLIENT_MSG),
173 ENTRY(FREERDP_ERROR_NONE, SDL_EXIT_LICENSE_HWID_DOESNT_MATCH),
174 ENTRY(FREERDP_ERROR_NONE, SDL_EXIT_LICENSE_BAD_CLIENT),
175 ENTRY(FREERDP_ERROR_NONE, SDL_EXIT_LICENSE_CANT_FINISH_PROTOCOL),
176 ENTRY(FREERDP_ERROR_NONE, SDL_EXIT_LICENSE_CLIENT_ENDED_PROTOCOL),
177 ENTRY(FREERDP_ERROR_NONE, SDL_EXIT_LICENSE_BAD_CLIENT_ENCRYPTION),
178 ENTRY(FREERDP_ERROR_NONE, SDL_EXIT_LICENSE_CANT_UPGRADE),
179 ENTRY(FREERDP_ERROR_NONE, SDL_EXIT_LICENSE_NO_REMOTE_CONNECTIONS),
180 ENTRY(FREERDP_ERROR_NONE, SDL_EXIT_LICENSE_CANT_UPGRADE),
183 ENTRY(FREERDP_ERROR_NONE, SDL_EXIT_RDP),
186 ENTRY(FREERDP_ERROR_NONE, SDL_EXIT_PARSE_ARGUMENTS), ENTRY(FREERDP_ERROR_NONE, SDL_EXIT_MEMORY),
187 ENTRY(FREERDP_ERROR_NONE, SDL_EXIT_PROTOCOL), ENTRY(FREERDP_ERROR_NONE, SDL_EXIT_CONN_FAILED),
189 ENTRY(FREERDP_ERROR_AUTHENTICATION_FAILED, SDL_EXIT_AUTH_FAILURE),
190 ENTRY(FREERDP_ERROR_SECURITY_NEGO_CONNECT_FAILED, SDL_EXIT_NEGO_FAILURE),
191 ENTRY(FREERDP_ERROR_CONNECT_LOGON_FAILURE, SDL_EXIT_LOGON_FAILURE),
192 ENTRY(FREERDP_ERROR_CONNECT_TARGET_BOOTING, SDL_EXIT_CONNECT_TARGET_BOOTING),
193 ENTRY(FREERDP_ERROR_CONNECT_ACCOUNT_LOCKED_OUT, SDL_EXIT_ACCOUNT_LOCKED_OUT),
194 ENTRY(FREERDP_ERROR_PRE_CONNECT_FAILED, SDL_EXIT_PRE_CONNECT_FAILED),
195 ENTRY(FREERDP_ERROR_CONNECT_UNDEFINED, SDL_EXIT_CONNECT_UNDEFINED),
196 ENTRY(FREERDP_ERROR_POST_CONNECT_FAILED, SDL_EXIT_POST_CONNECT_FAILED),
197 ENTRY(FREERDP_ERROR_DNS_ERROR, SDL_EXIT_DNS_ERROR),
198 ENTRY(FREERDP_ERROR_DNS_NAME_NOT_FOUND, SDL_EXIT_DNS_NAME_NOT_FOUND),
199 ENTRY(FREERDP_ERROR_CONNECT_FAILED, SDL_EXIT_CONNECT_FAILED),
200 ENTRY(FREERDP_ERROR_MCS_CONNECT_INITIAL_ERROR, SDL_EXIT_MCS_CONNECT_INITIAL_ERROR),
201 ENTRY(FREERDP_ERROR_TLS_CONNECT_FAILED, SDL_EXIT_TLS_CONNECT_FAILED),
202 ENTRY(FREERDP_ERROR_INSUFFICIENT_PRIVILEGES, SDL_EXIT_INSUFFICIENT_PRIVILEGES),
203 ENTRY(FREERDP_ERROR_CONNECT_CANCELLED, SDL_EXIT_CONNECT_CANCELLED),
204 ENTRY(FREERDP_ERROR_CONNECT_TRANSPORT_FAILED, SDL_EXIT_CONNECT_TRANSPORT_FAILED),
205 ENTRY(FREERDP_ERROR_CONNECT_PASSWORD_EXPIRED, SDL_EXIT_CONNECT_PASSWORD_EXPIRED),
206 ENTRY(FREERDP_ERROR_CONNECT_PASSWORD_MUST_CHANGE, SDL_EXIT_CONNECT_PASSWORD_MUST_CHANGE),
207 ENTRY(FREERDP_ERROR_CONNECT_KDC_UNREACHABLE, SDL_EXIT_CONNECT_KDC_UNREACHABLE),
208 ENTRY(FREERDP_ERROR_CONNECT_ACCOUNT_DISABLED, SDL_EXIT_CONNECT_ACCOUNT_DISABLED),
209 ENTRY(FREERDP_ERROR_CONNECT_PASSWORD_CERTAINLY_EXPIRED,
210 SDL_EXIT_CONNECT_PASSWORD_CERTAINLY_EXPIRED),
211 ENTRY(FREERDP_ERROR_CONNECT_CLIENT_REVOKED, SDL_EXIT_CONNECT_CLIENT_REVOKED),
212 ENTRY(FREERDP_ERROR_CONNECT_WRONG_PASSWORD, SDL_EXIT_CONNECT_WRONG_PASSWORD),
213 ENTRY(FREERDP_ERROR_CONNECT_ACCESS_DENIED, SDL_EXIT_CONNECT_ACCESS_DENIED),
214 ENTRY(FREERDP_ERROR_CONNECT_ACCOUNT_RESTRICTION, SDL_EXIT_CONNECT_ACCOUNT_RESTRICTION),
215 ENTRY(FREERDP_ERROR_CONNECT_ACCOUNT_EXPIRED, SDL_EXIT_CONNECT_ACCOUNT_EXPIRED),
216 ENTRY(FREERDP_ERROR_CONNECT_LOGON_TYPE_NOT_GRANTED, SDL_EXIT_CONNECT_LOGON_TYPE_NOT_GRANTED),
217 ENTRY(FREERDP_ERROR_CONNECT_NO_OR_MISSING_CREDENTIALS,
218 SDL_EXIT_CONNECT_NO_OR_MISSING_CREDENTIALS)
221static const struct sdl_exit_code_map_t* sdl_map_entry_by_code(
int exit_code)
223 for (
const auto& x : sdl_exit_code_map)
225 const struct sdl_exit_code_map_t* cur = &x;
226 if (cur->code == exit_code)
232static const struct sdl_exit_code_map_t* sdl_map_entry_by_error(UINT32 error)
234 for (
const auto& x : sdl_exit_code_map)
236 const struct sdl_exit_code_map_t* cur = &x;
237 if (cur->error == error)
243static int sdl_map_error_to_exit_code(DWORD error)
245 const struct sdl_exit_code_map_t* entry = sdl_map_entry_by_error(error);
249 return SDL_EXIT_CONN_FAILED;
252static const char* sdl_map_error_to_code_tag(UINT32 error)
254 const struct sdl_exit_code_map_t* entry = sdl_map_entry_by_error(error);
256 return entry->code_tag;
260static const char* sdl_map_to_code_tag(
int code)
262 const struct sdl_exit_code_map_t* entry = sdl_map_entry_by_code(code);
264 return entry->code_tag;
268static int error_info_to_error(freerdp* instance, DWORD* pcode,
char** msg,
size_t* len)
270 const DWORD code = freerdp_error_info(instance);
271 const char* name = freerdp_get_error_info_name(code);
272 const char* str = freerdp_get_error_info_string(code);
273 const int exit_code = sdl_map_error_to_exit_code(code);
275 winpr_asprintf(msg, len,
"Terminate with %s due to ERROR_INFO %s [0x%08" PRIx32
"]: %s",
276 sdl_map_error_to_code_tag(code), name, code, str);
277 SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION,
"%s", *msg);
285static BOOL sdl_begin_paint(rdpContext* context)
287 auto gdi = context->gdi;
289 WINPR_ASSERT(gdi->primary);
291 HGDI_DC hdc = gdi->primary->hdc;
297 WINPR_ASSERT(hwnd->invalid);
298 hwnd->invalid->null = TRUE;
305 SDL_Surface* surface, SDL_Point offset,
const SDL_Rect& srcRect)
307 WINPR_ASSERT(surface);
308 SDL_Rect dstRect = { offset.x + srcRect.x, offset.y + srcRect.y, srcRect.w, srcRect.h };
309 return window.blit(surface, srcRect, dstRect);
312static bool sdl_draw_to_window_rect(
SdlContext* sdl,
SdlWindow& window, SDL_Surface* surface,
313 SDL_Point offset,
const std::vector<SDL_Rect>& rects = {})
317 return sdl_draw_to_window_rect(sdl, window, surface, offset,
318 { 0, 0, surface->w, surface->h });
320 for (
auto& srcRect : rects)
322 if (!sdl_draw_to_window_rect(sdl, window, surface, offset, srcRect))
328static bool sdl_draw_to_window_scaled_rect(
SdlContext* sdl,
SdlWindow& window, SDL_Surface* surface,
329 const SDL_Rect& srcRect)
331 SDL_Rect dstRect = srcRect;
332 sdl_scale_coordinates(sdl, window.id(), &dstRect.x, &dstRect.y, FALSE, TRUE);
333 sdl_scale_coordinates(sdl, window.id(), &dstRect.w, &dstRect.h, FALSE, TRUE);
334 return window.blit(surface, srcRect, dstRect);
337static BOOL sdl_draw_to_window_scaled_rect(
SdlContext* sdl,
SdlWindow& window, SDL_Surface* surface,
338 const std::vector<SDL_Rect>& rects = {})
342 return sdl_draw_to_window_scaled_rect(sdl, window, surface,
343 { 0, 0, surface->w, surface->h });
345 for (
const auto& srcRect : rects)
347 if (!sdl_draw_to_window_scaled_rect(sdl, window, surface, srcRect))
354 const std::vector<SDL_Rect>& rects = {})
358 if (!sdl->isConnected())
361 auto context = sdl->context();
362 auto gdi = context->gdi;
365 auto size = window.rect();
367 std::unique_lock lock(sdl->critical);
368 auto surface = sdl->primary.get();
371 window.setOffsetX(0);
372 window.setOffsetY(0);
373 if (gdi->width < size.w)
375 window.setOffsetX((size.w - gdi->width) / 2);
377 if (gdi->height < size.h)
379 window.setOffsetY((size.h - gdi->height) / 2);
381 if (!sdl_draw_to_window_scaled_rect(sdl, window, surface, rects))
387 if (!sdl_draw_to_window_rect(sdl, window, surface, { window.offsetX(), window.offsetY() },
392 window.updateSurface();
396static BOOL sdl_draw_to_window(
SdlContext* sdl, std::map<Uint32, SdlWindow>& windows,
397 const std::vector<SDL_Rect>& rects = {})
399 for (
auto& window : windows)
401 if (!sdl_draw_to_window(sdl, window.second, rects))
412static BOOL sdl_end_paint(rdpContext* context)
414 auto sdl = get_context(context);
417 auto gdi = context->gdi;
419 WINPR_ASSERT(gdi->primary);
421 HGDI_DC hdc = gdi->primary->hdc;
427 WINPR_ASSERT(hwnd->invalid || (hwnd->ninvalid == 0));
429 if (hwnd->invalid->null)
432 WINPR_ASSERT(hwnd->invalid);
433 if (gdi->suppressOutput || hwnd->invalid->null)
436 const INT32 ninvalid = hwnd->ninvalid;
437 const GDI_RGN* cinvalid = hwnd->cinvalid;
442 std::vector<SDL_Rect> rects;
443 for (INT32 x = 0; x < ninvalid; x++)
445 auto& rgn = cinvalid[x];
446 rects.push_back({ rgn.x, rgn.y, rgn.w, rgn.h });
449 sdl->push(std::move(rects));
450 return sdl_push_user_event(SDL_EVENT_USER_UPDATE);
453static void sdl_destroy_primary(
SdlContext* sdl)
457 sdl->primary.reset();
461static BOOL sdl_create_primary(
SdlContext* sdl)
463 rdpGdi* gdi =
nullptr;
467 gdi = sdl->context()->gdi;
470 sdl_destroy_primary(sdl);
472 SDLSurfacePtr(SDL_CreateSurfaceFrom(
static_cast<int>(gdi->width),
473 static_cast<int>(gdi->height), sdl->sdl_pixel_format,
474 gdi->primary_buffer,
static_cast<int>(gdi->stride)),
479 SDL_SetSurfaceBlendMode(sdl->primary.get(), SDL_BLENDMODE_NONE);
480 SDL_Rect surfaceRect = { 0, 0, gdi->width, gdi->height };
481 SDL_FillSurfaceRect(sdl->primary.get(), &surfaceRect,
482 SDL_MapSurfaceRGBA(sdl->primary.get(), 0, 0, 0, 0xff));
487static BOOL sdl_desktop_resize(rdpContext* context)
489 rdpGdi* gdi =
nullptr;
490 rdpSettings* settings =
nullptr;
491 auto sdl = get_context(context);
494 WINPR_ASSERT(context);
496 settings = context->settings;
497 WINPR_ASSERT(settings);
499 std::unique_lock lock(sdl->critical);
504 return sdl_create_primary(sdl);
508static BOOL sdl_play_sound(rdpContext* context,
const PLAY_SOUND_UPDATE* play_sound)
511 WINPR_UNUSED(context);
512 WINPR_UNUSED(play_sound);
519 sdl->initialize.set();
521 HANDLE handles[] = { sdl->initialized.handle(), freerdp_abort_event(sdl->context()) };
523 const DWORD rc = WaitForMultipleObjects(ARRAYSIZE(handles), handles, FALSE, INFINITE);
535static BOOL sdl_pre_connect(freerdp* instance)
537 WINPR_ASSERT(instance);
538 WINPR_ASSERT(instance->context);
540 auto sdl = get_context(instance->context);
542 auto settings = instance->context->settings;
543 WINPR_ASSERT(settings);
558 PubSub_SubscribeChannelConnected(instance->context->pubSub, sdl_OnChannelConnectedEventHandler);
559 PubSub_SubscribeChannelDisconnected(instance->context->pubSub,
560 sdl_OnChannelDisconnectedEventHandler);
565 UINT32 maxHeight = 0;
567 if (!sdl_wait_for_init(sdl))
570 if (!sdl_detect_monitors(sdl, &maxWidth, &maxHeight))
573 if ((maxWidth != 0) && (maxHeight != 0) &&
576 WLog_Print(sdl->log, WLOG_INFO,
"Update size to %ux%u", maxWidth, maxHeight);
588 WLog_Print(sdl->log, WLOG_INFO,
"auth-only, but no password set. Please provide one.");
595 WLog_Print(sdl->log, WLOG_INFO,
"Authentication only. Don't connect SDL.");
598 if (!sdl->input.initialize())
605static const char* sdl_window_get_title(rdpSettings* settings)
607 const char* windowTitle =
nullptr;
610 const char* name =
nullptr;
611 const char* prefix =
"FreeRDP:";
623 addPort = (port != 3389);
625 char buffer[MAX_PATH + 64] = {};
628 (void)sprintf_s(buffer,
sizeof(buffer),
"%s %s", prefix, name);
630 (
void)sprintf_s(buffer,
sizeof(buffer),
"%s %s:%" PRIu32, prefix, name, port);
637static void sdl_term_handler([[maybe_unused]]
int signum, [[maybe_unused]]
const char* signame,
638 [[maybe_unused]]
void* context)
648 std::unique_lock lock(sdl->critical);
649 sdl->windows.clear();
650 sdl->dialog.destroy();
652 sdl_destroy_primary(sdl);
654 freerdp_del_signal_cleanup_handler(sdl->context(), sdl_term_handler);
655 sdl_dialogs_uninit();
659static BOOL sdl_create_windows(
SdlContext* sdl)
663 auto settings = sdl->context()->settings;
664 auto title = sdl_window_get_title(settings);
666 ScopeGuard guard1([&]() { sdl->windows_created.set(); });
672 for (UINT32 x = 0; x < windowCount; x++)
674 auto id = sdl->monitorId(x);
679 freerdp_settings_get_pointer_array_writable(settings, FreeRDP_MonitorDefArray, x));
681 originX = std::min<Sint32>(monitor->x, originX);
682 originY = std::min<Sint32>(monitor->y, originY);
685 for (UINT32 x = 0; x < windowCount; x++)
687 auto id = sdl->monitorId(x);
692 freerdp_settings_get_pointer_array_writable(settings, FreeRDP_MonitorDefArray, x));
694 auto w = WINPR_ASSERTING_INT_CAST(Uint32, monitor->width);
695 auto h = WINPR_ASSERTING_INT_CAST(Uint32, monitor->height);
703 Uint32 flags = SDL_WINDOW_HIGH_PIXEL_DENSITY;
704 auto startupX = SDL_WINDOWPOS_CENTERED_DISPLAY(
id);
705 auto startupY = SDL_WINDOWPOS_CENTERED_DISPLAY(
id);
710 flags |= SDL_WINDOW_FULLSCREEN;
715 flags |= SDL_WINDOW_BORDERLESS;
719 flags |= SDL_WINDOW_BORDERLESS;
721 char buffer[MAX_PATH + 64] = {};
722 (void)sprintf_s(buffer,
sizeof(buffer),
"%s:%" PRIu32, title, x);
724 static_cast<int>(startupX),
725 static_cast<int>(startupY),
729 if (!window.window())
734 window.setOffsetX(originX - monitor->x);
735 window.setOffsetY(originY - monitor->y);
738 sdl->windows.insert({ window.id(), std::move(window) });
744static BOOL sdl_wait_create_windows(
SdlContext* sdl)
747 std::unique_lock<CriticalSection> lock(sdl->critical);
748 sdl->windows_created.clear();
749 if (!sdl_push_user_event(SDL_EVENT_USER_CREATE_WINDOWS, sdl))
753 HANDLE handles[] = { sdl->windows_created.handle(), freerdp_abort_event(sdl->context()) };
755 const DWORD rc = WaitForMultipleObjects(ARRAYSIZE(handles), handles, FALSE, INFINITE);
767 std::unique_lock lock(sdl->critical);
768 if (freerdp_shall_disconnect_context(sdl->context()))
770 if (sdl->rdp_thread_running)
772 return !sdl->dialog.isRunning();
782 HANDLE handles[] = { sdl->initialize.handle(), freerdp_abort_event(sdl->context()) };
783 const DWORD status = WaitForMultipleObjects(ARRAYSIZE(handles), handles, FALSE, INFINITE);
792 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
793 auto backend = SDL_GetCurrentVideoDriver();
794 WLog_Print(sdl->log, WLOG_DEBUG,
"client is using backend '%s'", backend);
797 SDL_SetHint(SDL_HINT_ALLOW_ALT_TAB_WHILE_GRABBED,
"0");
798 SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR,
"0");
799 SDL_SetHint(SDL_HINT_PEN_MOUSE_EVENTS,
"0");
800 SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS,
"0");
801 SDL_SetHint(SDL_HINT_PEN_TOUCH_EVENTS,
"1");
802 SDL_SetHint(SDL_HINT_TRACKPAD_IS_TOUCH_ONLY,
"1");
804 freerdp_add_signal_cleanup_handler(sdl->context(), sdl_term_handler);
805 sdl->dialog.create(sdl->context());
806 sdl->dialog.setTitle(
"Connecting to '%s'",
808 sdl->dialog.showInfo(
"The connection is being established\n\nPlease wait...");
811 sdl->dialog.show(
true);
814 sdl->initialized.set();
816 while (!shall_abort(sdl))
818 SDL_Event windowEvent = {};
819 while (!shall_abort(sdl) && SDL_WaitEventTimeout(
nullptr, 1000))
824 const int prc = SDL_PeepEvents(&windowEvent, 1, SDL_GETEVENT, SDL_EVENT_FIRST,
825 SDL_EVENT_USER_RETRY_DIALOG);
828 if (sdl_log_error(prc, sdl->log,
"SDL_PeepEvents"))
832#if defined(WITH_DEBUG_SDL_EVENTS)
833 SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION,
"got event %s [0x%08" PRIx32
"]",
834 sdl_event_type_str(windowEvent.type), windowEvent.type);
837 std::unique_lock lock(sdl->critical);
840 if (freerdp_shall_disconnect_context(sdl->context()))
844 if (sdl->dialog.handleEvent(windowEvent))
849 auto point2pix = [](Uint32 win_id,
float& x,
float& y)
851 auto win = SDL_GetWindowFromID(win_id);
854 auto scale = SDL_GetWindowDisplayScale(win);
861 switch (windowEvent.type)
864 freerdp_abort_connect_context(sdl->context());
866 case SDL_EVENT_KEY_DOWN:
867 case SDL_EVENT_KEY_UP:
869 const SDL_KeyboardEvent* ev = &windowEvent.key;
870 sdl->input.keyboard_handle_event(ev);
873 case SDL_EVENT_KEYMAP_CHANGED:
877 case SDL_EVENT_MOUSE_MOTION:
879 SDL_MouseMotionEvent& ev = windowEvent.motion;
880 point2pix(ev.windowID, ev.x, ev.y);
881 point2pix(ev.windowID, ev.xrel, ev.yrel);
882 sdl_handle_mouse_motion(sdl, &ev);
885 case SDL_EVENT_MOUSE_BUTTON_DOWN:
886 case SDL_EVENT_MOUSE_BUTTON_UP:
888 SDL_MouseButtonEvent& ev = windowEvent.button;
889 point2pix(ev.windowID, ev.x, ev.y);
890 sdl_handle_mouse_button(sdl, &ev);
893 case SDL_EVENT_MOUSE_WHEEL:
895 const SDL_MouseWheelEvent* ev = &windowEvent.wheel;
896 sdl_handle_mouse_wheel(sdl, ev);
899 case SDL_EVENT_FINGER_DOWN:
901 const SDL_TouchFingerEvent* ev = &windowEvent.tfinger;
902 sdl_handle_touch_down(sdl, ev);
905 case SDL_EVENT_FINGER_UP:
907 const SDL_TouchFingerEvent* ev = &windowEvent.tfinger;
908 sdl_handle_touch_up(sdl, ev);
911 case SDL_EVENT_FINGER_MOTION:
913 const SDL_TouchFingerEvent* ev = &windowEvent.tfinger;
914 sdl_handle_touch_motion(sdl, ev);
918 case SDL_EVENT_RENDER_TARGETS_RESET:
921 case SDL_EVENT_RENDER_DEVICE_RESET:
924 case SDL_EVENT_WILL_ENTER_FOREGROUND:
927 case SDL_EVENT_USER_CERT_DIALOG:
930 auto title =
static_cast<const char*
>(windowEvent.user.data1);
931 auto msg =
static_cast<const char*
>(windowEvent.user.data2);
932 sdl_cert_dialog_show(title, msg);
935 case SDL_EVENT_USER_SHOW_DIALOG:
938 auto title =
static_cast<const char*
>(windowEvent.user.data1);
939 auto msg =
static_cast<const char*
>(windowEvent.user.data2);
940 sdl_message_dialog_show(title, msg, windowEvent.user.code);
943 case SDL_EVENT_USER_SCARD_DIALOG:
946 auto title =
static_cast<const char*
>(windowEvent.user.data1);
947 auto msg =
static_cast<const char**
>(windowEvent.user.data2);
948 sdl_scard_dialog_show(title, windowEvent.user.code, msg);
951 case SDL_EVENT_USER_AUTH_DIALOG:
954 sdl_auth_dialog_show(
958 case SDL_EVENT_USER_UPDATE:
960 std::vector<SDL_Rect> rectangles;
963 rectangles = sdl->pop();
964 sdl_draw_to_window(sdl, sdl->windows, rectangles);
965 }
while (!rectangles.empty());
968 case SDL_EVENT_USER_CREATE_WINDOWS:
970 auto ctx =
static_cast<SdlContext*
>(windowEvent.user.data1);
971 sdl_create_windows(ctx);
974 case SDL_EVENT_USER_WINDOW_RESIZEABLE:
976 auto window =
static_cast<SdlWindow*
>(windowEvent.user.data1);
977 const bool use = windowEvent.user.code != 0;
979 window->resizeable(use);
982 case SDL_EVENT_USER_WINDOW_FULLSCREEN:
984 auto window =
static_cast<SdlWindow*
>(windowEvent.user.data1);
985 const bool enter = windowEvent.user.code != 0;
987 window->fullscreen(enter);
990 case SDL_EVENT_USER_WINDOW_MINIMIZE:
991 for (
auto& window : sdl->windows)
993 window.second.minimize();
996 case SDL_EVENT_USER_POINTER_NULL:
998 sdl->setCursor(
nullptr);
999 sdl->setHasCursor(
false);
1001 case SDL_EVENT_USER_POINTER_DEFAULT:
1003 SDL_Cursor* def = SDL_GetDefaultCursor();
1006 sdl->setCursor(
nullptr);
1007 sdl->setHasCursor(
true);
1010 case SDL_EVENT_USER_POINTER_POSITION:
1013 static_cast<INT32
>(
reinterpret_cast<uintptr_t
>(windowEvent.user.data1));
1015 static_cast<INT32
>(
reinterpret_cast<uintptr_t
>(windowEvent.user.data2));
1017 SDL_Window* window = SDL_GetMouseFocus();
1020 const Uint32
id = SDL_GetWindowID(window);
1024 if (sdl_scale_coordinates(sdl,
id, &sx, &sy, FALSE, FALSE))
1025 SDL_WarpMouseInWindow(window,
static_cast<float>(sx),
1026 static_cast<float>(sy));
1030 case SDL_EVENT_USER_POINTER_SET:
1031 sdl->setCursor(
static_cast<rdpPointer*
>(windowEvent.user.data1));
1032 sdl_Pointer_Set_Process(sdl);
1034 case SDL_EVENT_CLIPBOARD_UPDATE:
1035 sdl->clip.handle_update(windowEvent.clipboard);
1037 case SDL_EVENT_USER_QUIT:
1039 if ((windowEvent.type >= SDL_EVENT_DISPLAY_FIRST) &&
1040 (windowEvent.type <= SDL_EVENT_DISPLAY_LAST))
1042 const SDL_DisplayEvent* ev = &windowEvent.display;
1043 (void)sdl->disp.handle_display_event(ev);
1045 else if ((windowEvent.type >= SDL_EVENT_WINDOW_FIRST) &&
1046 (windowEvent.type <= SDL_EVENT_WINDOW_LAST))
1048 const SDL_WindowEvent* ev = &windowEvent.window;
1049 auto window = sdl->windows.find(ev->windowID);
1050 if (window != sdl->windows.end())
1052 (void)sdl->disp.handle_window_event(ev);
1056 case SDL_EVENT_WINDOW_DISPLAY_SCALE_CHANGED:
1057 if (sdl->isConnected())
1059 sdl_Pointer_Set_Process(sdl);
1061 sdl->context()->settings,
1062 FreeRDP_DynamicResolutionUpdate))
1066 auto win = window->second.window();
1069 [[maybe_unused]]
auto rcpix =
1070 SDL_GetWindowSizeInPixels(win, &w_pix, &h_pix);
1072 auto scale = SDL_GetWindowDisplayScale(win);
1073 if (scale <= SDL_FLT_EPSILON)
1075 auto err = SDL_GetError();
1077 SDL_LOG_CATEGORY_APPLICATION,
1078 "SDL_GetWindowDisplayScale() failed with %s", err);
1082 assert(SDL_isnanf(scale) == 0);
1083 assert(SDL_isinff(scale) == 0);
1084 assert(scale > SDL_FLT_EPSILON);
1085 auto w_gdi = sdl->context()->gdi->width;
1086 auto h_gdi = sdl->context()->gdi->height;
1087 auto pix2point = [=](
int pix)
1089 return static_cast<int>(
static_cast<float>(pix) /
1092 if (w_pix != w_gdi || h_pix != h_gdi)
1094 const auto ssws = SDL_SetWindowSize(
1095 win, pix2point(w_gdi), pix2point(h_gdi));
1098 auto err = SDL_GetError();
1100 SDL_LOG_CATEGORY_APPLICATION,
1101 "SDL_SetWindowSize() failed with %s", err);
1107 case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED:
1108 window->second.fill();
1109 sdl_draw_to_window(sdl, window->second);
1110 sdl_Pointer_Set_Process(sdl);
1112 case SDL_EVENT_WINDOW_MOVED:
1114 auto r = window->second.rect();
1115 auto id = window->second.id();
1116 SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION,
"%u: %dx%d-%dx%d",
1117 id, r.x, r.y, r.w, r.h);
1120 case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
1122 SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION,
1123 "Window closed, terminating RDP session...");
1124 freerdp_abort_connect_context(sdl->context());
1139 sdl_cleanup_sdl(sdl);
1151static BOOL sdl_post_connect(freerdp* instance)
1153 WINPR_ASSERT(instance);
1155 auto context = instance->context;
1156 WINPR_ASSERT(context);
1158 auto sdl = get_context(context);
1161 sdl->dialog.show(
false);
1168 WLog_Print(sdl->log, WLOG_INFO,
"auth-only, but no password set. Please provide one.");
1172 WLog_Print(sdl->log, WLOG_INFO,
"Authentication only. Don't connect to X.");
1176 if (!sdl_wait_create_windows(sdl))
1179 sdl->sdl_pixel_format = SDL_PIXELFORMAT_BGRA32;
1180 if (!gdi_init(instance, PIXEL_FORMAT_BGRA32))
1183 if (!sdl_create_primary(sdl))
1186 if (!sdl_register_pointer(instance->context->graphics))
1189 WINPR_ASSERT(context->update);
1191 context->update->BeginPaint = sdl_begin_paint;
1192 context->update->EndPaint = sdl_end_paint;
1193 context->update->PlaySound = sdl_play_sound;
1194 context->update->DesktopResize = sdl_desktop_resize;
1195 context->update->SetKeyboardIndicators = sdlInput::keyboard_set_indicators;
1196 context->update->SetKeyboardImeStatus = sdlInput::keyboard_set_ime_status;
1198 if (!sdl->update_resizeable(
false))
1203 sdl->setConnected(
true);
1210static void sdl_post_disconnect(freerdp* instance)
1215 if (!instance->context)
1218 auto sdl = get_context(instance->context);
1219 sdl->setConnected(
false);
1224static void sdl_post_final_disconnect(freerdp* instance)
1229 if (!instance->context)
1232 PubSub_UnsubscribeChannelConnected(instance->context->pubSub,
1233 sdl_OnChannelConnectedEventHandler);
1234 PubSub_UnsubscribeChannelDisconnected(instance->context->pubSub,
1235 sdl_OnChannelDisconnectedEventHandler);
1238static void sdl_client_cleanup(
SdlContext* sdl,
int exit_code,
const std::string& error_msg)
1242 rdpContext* context = sdl->context();
1243 WINPR_ASSERT(context);
1244 rdpSettings* settings = context->settings;
1245 WINPR_ASSERT(settings);
1247 sdl->rdp_thread_running =
false;
1248 bool showError =
false;
1250 WLog_Print(sdl->log, WLOG_INFO,
"Authentication only, exit status %s [%" PRId32
"]",
1251 sdl_map_to_code_tag(exit_code), exit_code);
1256 case SDL_EXIT_SUCCESS:
1257 case SDL_EXIT_DISCONNECT:
1258 case SDL_EXIT_LOGOFF:
1259 case SDL_EXIT_DISCONNECT_BY_USER:
1260 case SDL_EXIT_CONNECT_CANCELLED:
1264 sdl->dialog.showError(error_msg);
1271 sdl->dialog.show(
false);
1273 sdl->exit_code = exit_code;
1274 sdl_push_user_event(SDL_EVENT_USER_QUIT);
1278static int sdl_client_thread_connect(
SdlContext* sdl, std::string& error_msg)
1282 auto instance = sdl->context()->instance;
1283 WINPR_ASSERT(instance);
1285 sdl->rdp_thread_running =
true;
1286 BOOL rc = freerdp_connect(instance);
1288 rdpContext* context = sdl->context();
1289 rdpSettings* settings = context->settings;
1290 WINPR_ASSERT(settings);
1292 int exit_code = SDL_EXIT_SUCCESS;
1295 UINT32 error = freerdp_get_last_error(context);
1296 exit_code = sdl_map_error_to_exit_code(error);
1301 DWORD code = freerdp_get_last_error(context);
1302 freerdp_abort_connect_context(context);
1303 WLog_Print(sdl->log, WLOG_ERROR,
"Authentication only, %s [0x%08" PRIx32
"] %s",
1304 freerdp_get_last_error_name(code), code, freerdp_get_last_error_string(code));
1310 DWORD code = freerdp_error_info(instance);
1311 if (exit_code == SDL_EXIT_SUCCESS)
1313 char* msg =
nullptr;
1315 exit_code = error_info_to_error(instance, &code, &msg, &len);
1321 auto last = freerdp_get_last_error(context);
1322 if (error_msg.empty())
1324 char* msg =
nullptr;
1326 winpr_asprintf(&msg, &len,
"%s [0x%08" PRIx32
"]\n%s",
1327 freerdp_get_last_error_name(last), last,
1328 freerdp_get_last_error_string(last));
1334 if (exit_code == SDL_EXIT_SUCCESS)
1336 if (last == FREERDP_ERROR_AUTHENTICATION_FAILED)
1337 exit_code = SDL_EXIT_AUTH_FAILURE;
1338 else if (code == ERRINFO_SUCCESS)
1339 exit_code = SDL_EXIT_CONN_FAILED;
1342 sdl->dialog.show(
false);
1348static int sdl_client_thread_run(
SdlContext* sdl, std::string& error_msg)
1352 auto context = sdl->context();
1353 WINPR_ASSERT(context);
1355 auto instance = context->instance;
1356 WINPR_ASSERT(instance);
1358 int exit_code = SDL_EXIT_SUCCESS;
1359 while (!freerdp_shall_disconnect_context(context))
1361 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = {};
1367 if (freerdp_focus_required(instance))
1369 auto ctx = get_context(context);
1371 if (!ctx->input.keyboard_focus_in())
1373 if (!ctx->input.keyboard_focus_in())
1377 const DWORD nCount = freerdp_get_event_handles(context, handles, ARRAYSIZE(handles));
1381 WLog_Print(sdl->log, WLOG_ERROR,
"freerdp_get_event_handles failed");
1385 const DWORD status = WaitForMultipleObjects(nCount, handles, FALSE, INFINITE);
1387 if (status == WAIT_FAILED)
1390 if (!freerdp_check_event_handles(context))
1392 if (client_auto_reconnect(instance))
1395 sdl->dialog.show(
false);
1404 if (freerdp_error_info(instance) == 0)
1405 exit_code = SDL_EXIT_CONN_FAILED;
1408 if (freerdp_get_last_error(context) == FREERDP_ERROR_SUCCESS)
1409 WLog_Print(sdl->log, WLOG_ERROR,
"WaitForMultipleObjects failed with %" PRIu32
"",
1411 if (freerdp_get_last_error(context) == FREERDP_ERROR_SUCCESS)
1412 WLog_Print(sdl->log, WLOG_ERROR,
"Failed to check FreeRDP event handles");
1417 if (exit_code == SDL_EXIT_SUCCESS)
1421 char* emsg =
nullptr;
1423 exit_code = error_info_to_error(instance, &code, &emsg, &elen);
1429 if ((code == ERRINFO_LOGOFF_BY_USER) &&
1430 (freerdp_get_disconnect_ultimatum(context) == Disconnect_Ultimatum_user_requested))
1432 const char* msg =
"Error info says user did not initiate but disconnect ultimatum says "
1433 "they did; treat this as a user logoff";
1435 char* emsg =
nullptr;
1437 winpr_asprintf(&emsg, &elen,
"%s", msg);
1443 WLog_Print(sdl->log, WLOG_INFO,
"%s", msg);
1444 exit_code = SDL_EXIT_LOGOFF;
1448 freerdp_disconnect(instance);
1456static DWORD WINAPI sdl_client_thread_proc(
SdlContext* sdl)
1460 std::string error_msg;
1461 int exit_code = sdl_client_thread_connect(sdl, error_msg);
1462 if (exit_code == SDL_EXIT_SUCCESS)
1463 exit_code = sdl_client_thread_run(sdl, error_msg);
1464 sdl_client_cleanup(sdl, exit_code, error_msg);
1466 return static_cast<DWORD
>(exit_code);
1472static BOOL sdl_client_global_init()
1476 const DWORD wVersionRequested = MAKEWORD(1, 1);
1477 const int rc = WSAStartup(wVersionRequested, &wsaData);
1480 WLog_ERR(SDL_TAG,
"WSAStartup failed with %s [%d]", gai_strerrorA(rc), rc);
1485 return (freerdp_handle_signals() == 0);
1489static void sdl_client_global_uninit()
1496static BOOL sdl_client_new(freerdp* instance, rdpContext* context)
1500 if (!instance || !context)
1507 instance->PreConnect = sdl_pre_connect;
1508 instance->PostConnect = sdl_post_connect;
1509 instance->PostDisconnect = sdl_post_disconnect;
1510 instance->PostFinalDisconnect = sdl_post_final_disconnect;
1511 instance->AuthenticateEx = sdl_authenticate_ex;
1512 instance->VerifyCertificateEx = sdl_verify_certificate_ex;
1513 instance->VerifyChangedCertificateEx = sdl_verify_changed_certificate_ex;
1514 instance->LogonErrorInfo = sdl_logon_error_info;
1515 instance->PresentGatewayMessage = sdl_present_gateway_message;
1516 instance->ChooseSmartcard = sdl_choose_smartcard;
1517 instance->RetryDialog = sdl_retry_dialog;
1520 instance->GetAccessToken = sdl_webview_get_access_token;
1522 instance->GetAccessToken = client_cli_get_access_token;
1529static void sdl_client_free([[maybe_unused]] freerdp* instance, rdpContext* context)
1539static int sdl_client_start(rdpContext* context)
1541 auto sdl = get_context(context);
1544 sdl->thread = std::thread(sdl_client_thread_proc, sdl);
1548static int sdl_client_stop(rdpContext* context)
1550 auto sdl = get_context(context);
1555 HANDLE
event = freerdp_abort_event(context);
1556 if (!SetEvent(event))
1563static int RdpClientEntry(RDP_CLIENT_ENTRY_POINTS* pEntryPoints)
1565 WINPR_ASSERT(pEntryPoints);
1567 ZeroMemory(pEntryPoints,
sizeof(RDP_CLIENT_ENTRY_POINTS));
1568 pEntryPoints->Version = RDP_CLIENT_INTERFACE_VERSION;
1569 pEntryPoints->Size =
sizeof(RDP_CLIENT_ENTRY_POINTS_V1);
1570 pEntryPoints->GlobalInit = sdl_client_global_init;
1571 pEntryPoints->GlobalUninit = sdl_client_global_uninit;
1573 pEntryPoints->ClientNew = sdl_client_new;
1574 pEntryPoints->ClientFree = sdl_client_free;
1575 pEntryPoints->ClientStart = sdl_client_start;
1576 pEntryPoints->ClientStop = sdl_client_stop;
1583 freerdp_client_context_free(&sdl->common.context);
1586static const char* category2str(
int category)
1590 case SDL_LOG_CATEGORY_APPLICATION:
1591 return "SDL_LOG_CATEGORY_APPLICATION";
1592 case SDL_LOG_CATEGORY_ERROR:
1593 return "SDL_LOG_CATEGORY_ERROR";
1594 case SDL_LOG_CATEGORY_ASSERT:
1595 return "SDL_LOG_CATEGORY_ASSERT";
1596 case SDL_LOG_CATEGORY_SYSTEM:
1597 return "SDL_LOG_CATEGORY_SYSTEM";
1598 case SDL_LOG_CATEGORY_AUDIO:
1599 return "SDL_LOG_CATEGORY_AUDIO";
1600 case SDL_LOG_CATEGORY_VIDEO:
1601 return "SDL_LOG_CATEGORY_VIDEO";
1602 case SDL_LOG_CATEGORY_RENDER:
1603 return "SDL_LOG_CATEGORY_RENDER";
1604 case SDL_LOG_CATEGORY_INPUT:
1605 return "SDL_LOG_CATEGORY_INPUT";
1606 case SDL_LOG_CATEGORY_TEST:
1607 return "SDL_LOG_CATEGORY_TEST";
1608 case SDL_LOG_CATEGORY_GPU:
1609 return "SDL_LOG_CATEGORY_GPU";
1610 case SDL_LOG_CATEGORY_RESERVED2:
1611 return "SDL_LOG_CATEGORY_RESERVED2";
1612 case SDL_LOG_CATEGORY_RESERVED3:
1613 return "SDL_LOG_CATEGORY_RESERVED3";
1614 case SDL_LOG_CATEGORY_RESERVED4:
1615 return "SDL_LOG_CATEGORY_RESERVED4";
1616 case SDL_LOG_CATEGORY_RESERVED5:
1617 return "SDL_LOG_CATEGORY_RESERVED5";
1618 case SDL_LOG_CATEGORY_RESERVED6:
1619 return "SDL_LOG_CATEGORY_RESERVED6";
1620 case SDL_LOG_CATEGORY_RESERVED7:
1621 return "SDL_LOG_CATEGORY_RESERVED7";
1622 case SDL_LOG_CATEGORY_RESERVED8:
1623 return "SDL_LOG_CATEGORY_RESERVED8";
1624 case SDL_LOG_CATEGORY_RESERVED9:
1625 return "SDL_LOG_CATEGORY_RESERVED9";
1626 case SDL_LOG_CATEGORY_RESERVED10:
1627 return "SDL_LOG_CATEGORY_RESERVED10";
1628 case SDL_LOG_CATEGORY_CUSTOM:
1630 return "SDL_LOG_CATEGORY_CUSTOM";
1634static SDL_LogPriority wloglevel2dl(DWORD level)
1639 return SDL_LOG_PRIORITY_VERBOSE;
1641 return SDL_LOG_PRIORITY_DEBUG;
1643 return SDL_LOG_PRIORITY_INFO;
1645 return SDL_LOG_PRIORITY_WARN;
1647 return SDL_LOG_PRIORITY_ERROR;
1649 return SDL_LOG_PRIORITY_CRITICAL;
1652 return SDL_LOG_PRIORITY_VERBOSE;
1656static DWORD sdlpriority2wlog(SDL_LogPriority priority)
1658 DWORD level = WLOG_OFF;
1661 case SDL_LOG_PRIORITY_VERBOSE:
1664 case SDL_LOG_PRIORITY_DEBUG:
1667 case SDL_LOG_PRIORITY_INFO:
1670 case SDL_LOG_PRIORITY_WARN:
1673 case SDL_LOG_PRIORITY_ERROR:
1676 case SDL_LOG_PRIORITY_CRITICAL:
1686static void SDLCALL winpr_LogOutputFunction(
void* userdata,
int category, SDL_LogPriority priority,
1687 const char* message)
1689 auto sdl =
static_cast<SdlContext*
>(userdata);
1692 const DWORD level = sdlpriority2wlog(priority);
1693 auto log = sdl->log;
1694 if (!WLog_IsLevelActive(log, level))
1697 WLog_PrintTextMessage(log, level, __LINE__, __FILE__, __func__,
"[%s] %s",
1698 category2str(category), message);
1701static void sdl_quit()
1704 ev.type = SDL_EVENT_QUIT;
1705 if (!SDL_PushEvent(&ev))
1707 SDL_Log(
"An error occurred: %s", SDL_GetError());
1711static void SDLCALL rdp_file_cb(
void* userdata,
const char*
const* filelist,
1712 WINPR_ATTR_UNUSED
int filter)
1714 auto rdp =
static_cast<std::string*
>(userdata);
1718 SDL_Log(
"An error occurred: %s", SDL_GetError());
1722 else if (!*filelist)
1724 SDL_Log(
"The user did not select any file.");
1725 SDL_Log(
"Most likely, the dialog was canceled.");
1732 SDL_Log(
"Full path to selected file: '%s'", *filelist);
1740static std::string getRdpFile()
1742 const auto flags = SDL_INIT_VIDEO | SDL_INIT_EVENTS;
1743 SDL_DialogFileFilter filters[] = { {
"RDP files",
"rdp;rdpw" } };
1746 bool running =
true;
1747 if (!SDL_Init(flags))
1750 auto props = SDL_CreateProperties();
1751 SDL_SetStringProperty(props, SDL_PROP_FILE_DIALOG_TITLE_STRING,
1752 "SDL Freerdp - Open a RDP file");
1753 SDL_SetBooleanProperty(props, SDL_PROP_FILE_DIALOG_MANY_BOOLEAN,
false);
1754 SDL_SetPointerProperty(props, SDL_PROP_FILE_DIALOG_FILTERS_POINTER, filters);
1755 SDL_SetNumberProperty(props, SDL_PROP_FILE_DIALOG_NFILTERS_NUMBER, ARRAYSIZE(filters));
1756 SDL_ShowFileDialogWithProperties(SDL_FILEDIALOG_OPENFILE, rdp_file_cb, &rdp, props);
1757 SDL_DestroyProperties(props);
1761 SDL_Event
event = {};
1762 (void)SDL_PollEvent(&event);
1766 case SDL_EVENT_QUIT:
1777int main(
int argc,
char* argv[])
1781 RDP_CLIENT_ENTRY_POINTS clientEntryPoints = {};
1783 RdpClientEntry(&clientEntryPoints);
1785 reinterpret_cast<sdl_rdp_context*
>(freerdp_client_context_new(&clientEntryPoints)),
1790 auto sdl = sdl_rdp->sdl;
1792 auto settings = sdl->context()->settings;
1793 WINPR_ASSERT(settings);
1795 std::string rdp_file;
1796 std::vector<char*> args;
1797 args.reserve(WINPR_ASSERTING_INT_CAST(
size_t, argc));
1798 for (
auto x = 0; x < argc; x++)
1799 args.push_back(argv[x]);
1803 rdp_file = getRdpFile();
1804 if (!rdp_file.empty())
1806 args.push_back(rdp_file.data());
1810 status = freerdp_client_settings_parse_command_line(
1811 settings, WINPR_ASSERTING_INT_CAST(
int, args.size()), args.data(), FALSE);
1812 sdl_rdp->sdl->setMetadata();
1815 rc = freerdp_client_settings_command_line_status_print(settings, status, argc, argv);
1817 sdl_list_monitors(sdl);
1822 case COMMAND_LINE_STATUS_PRINT:
1823 case COMMAND_LINE_STATUS_PRINT_VERSION:
1824 case COMMAND_LINE_STATUS_PRINT_BUILDCONFIG:
1826 case COMMAND_LINE_STATUS_PRINT_HELP:
1828 SdlPref::print_config_file_help(3);
1835 SDL_SetLogOutputFunction(winpr_LogOutputFunction, sdl);
1836 auto level = WLog_GetLogLevel(sdl->log);
1837 SDL_SetLogPriorities(wloglevel2dl(level));
1839 auto context = sdl->context();
1840 WINPR_ASSERT(context);
1842 if (!stream_dump_register_handlers(context, CONNECTION_STATE_MCS_CREATE_REQUEST, FALSE))
1845 if (freerdp_client_start(context) != 0)
1850 if (freerdp_client_stop(context) != 0)
1853 if (sdl->exit_code != 0)
1854 rc = sdl->exit_code;
1859bool SdlContext::update_fullscreen(
bool enter)
1861 for (
const auto& window : windows)
1863 if (!sdl_push_user_event(SDL_EVENT_USER_WINDOW_FULLSCREEN, &window.second, enter))
1870bool SdlContext::update_minimize()
1872 return sdl_push_user_event(SDL_EVENT_USER_WINDOW_MINIMIZE);
1875bool SdlContext::update_resizeable(
bool enable)
1877 const auto settings = context()->settings;
1880 bool use = (dyn && enable) || smart;
1882 for (
const auto& window : windows)
1884 if (!sdl_push_user_event(SDL_EVENT_USER_WINDOW_RESIZEABLE, &window.second, use))
1892SdlContext::SdlContext(rdpContext* context)
1893 : _context(context), log(WLog_Get(SDL_TAG)), disp(this), input(this), clip(this),
1894 primary(nullptr, SDL_DestroySurface), rdp_thread_running(false), dialog(log)
1896 WINPR_ASSERT(context);
1900void SdlContext::setHasCursor(
bool val)
1902 this->_cursor_visible = val;
1905bool SdlContext::hasCursor()
const
1907 return _cursor_visible;
1910void SdlContext::setMetadata()
1913 if (!wmclass || (strlen(wmclass) == 0))
1914 wmclass = SDL_CLIENT_UUID;
1916 SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_IDENTIFIER_STRING, wmclass);
1917 SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_NAME_STRING, SDL_CLIENT_NAME);
1918 SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_VERSION_STRING, SDL_CLIENT_VERSION);
1919 SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_CREATOR_STRING, SDL_CLIENT_VENDOR);
1920 SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_COPYRIGHT_STRING, SDL_CLIENT_COPYRIGHT);
1921 SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_URL_STRING, SDL_CLIENT_URL);
1922 SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_TYPE_STRING, SDL_CLIENT_TYPE);
1925bool SdlContext::redraw(
bool suppress)
const
1930 auto gdi = context()->gdi;
1932 return gdi_send_suppress_output(gdi, suppress);
1935void SdlContext::setConnected(
bool val)
1940bool SdlContext::isConnected()
const
1945rdpContext* SdlContext::context()
const
1947 WINPR_ASSERT(_context);
1951rdpClientContext* SdlContext::common()
const
1953 return reinterpret_cast<rdpClientContext*
>(context());
1956void SdlContext::setCursor(rdpPointer* cursor)
1961rdpPointer* SdlContext::cursor()
const
1966void SdlContext::setMonitorIds(
const std::vector<SDL_DisplayID>& ids)
1968 _monitorIds.clear();
1971 _monitorIds.push_back(
id);
1975const std::vector<SDL_DisplayID>& SdlContext::monitorIds()
const
1980int64_t SdlContext::monitorId(uint32_t index)
const
1982 if (index >= _monitorIds.size())
1986 return _monitorIds[index];
1989void SdlContext::push(std::vector<SDL_Rect>&& rects)
1991 std::unique_lock lock(_queue_mux);
1992 _queue.emplace(std::move(rects));
1995std::vector<SDL_Rect> SdlContext::pop()
1997 std::unique_lock lock(_queue_mux);
2002 auto val = std::move(_queue.front());
FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
FREERDP_API BOOL freerdp_settings_set_string(rdpSettings *settings, FreeRDP_Settings_Keys_String id, const char *param)
Sets a string settings value. The param is copied.
FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
FREERDP_API const char * freerdp_settings_get_server_name(const rdpSettings *settings)
A helper function to return the correct server name.
FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 param)
Sets a UINT32 settings value.
FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.
FREERDP_API BOOL freerdp_settings_set_bool(rdpSettings *settings, FreeRDP_Settings_Keys_Bool id, BOOL param)
Sets a BOOL settings value.