24#include <freerdp/config.h>
26#if defined(__has_include)
27#if __has_include(<version>)
36#if defined(__cpp_lib_source_location) && (__cpp_lib_source_location >= 201907L)
37#include <source_location>
40#include <freerdp/constants.h>
41#include <freerdp/freerdp.h>
42#include <freerdp/gdi/gdi.h>
43#include <freerdp/streamdump.h>
44#include <freerdp/utils/signal.h>
46#include <freerdp/channels/channels.h>
47#include <freerdp/client/channels.h>
48#include <freerdp/client/cliprdr.h>
49#include <freerdp/client/cmdline.h>
50#include <freerdp/client/file.h>
52#include <freerdp/log.h>
53#include <winpr/assert.h>
54#include <winpr/config.h>
56#include <winpr/synch.h>
59#if !defined(__MINGW32__)
60#include <SDL3/SDL_main.h>
62#include <SDL3/SDL_hints.h>
63#include <SDL3/SDL_oldnames.h>
64#include <SDL3/SDL_video.h>
66#include <sdl_config.hpp>
68#include "dialogs/sdl_connection_dialog_hider.hpp"
69#include "dialogs/sdl_dialogs.hpp"
70#include "scoped_guard.hpp"
71#include "sdl_channels.hpp"
72#include "sdl_freerdp.hpp"
73#include "sdl_context.hpp"
74#include "sdl_monitor.hpp"
75#include "sdl_pointer.hpp"
76#include "sdl_prefs.hpp"
78#include "sdl_win32_console.hpp"
80#include "sdl_utils.hpp"
83#define SDL_TAG CLIENT_TAG("SDL")
86class ErrorMsg :
public std::exception
89 ErrorMsg(
int rc, Uint32 type,
const std::string& msg,
90 const std::string& sdlError = SDL_GetError()
91#
if defined(__cpp_lib_source_location) && (__cpp_lib_source_location >= 201907L)
93 std::source_location loc = std::source_location::current()
94#elif defined(__GNUC__)
96 const std::string& file = __builtin_FILE(),
97 const std::string& fkt = __builtin_FUNCTION(),
size_t line = __builtin_LINE(),
99 size_t column = __builtin_COLUMN()
106 [[nodiscard]]
int rc()
const;
107 [[nodiscard]]
const char* what() const noexcept override;
113 std::
string _sdlError;
121const
char* ErrorMsg::what() const noexcept
123 return _buffer.c_str();
126int ErrorMsg::rc()
const
131ErrorMsg::ErrorMsg(
int rc, Uint32 type,
const std::string& msg,
const std::string& sdlError
132#
if defined(__cpp_lib_source_location) && (__cpp_lib_source_location >= 201907L)
134 std::source_location loc
135#elif defined(__GNUC__)
137 const std::string& file,
const std::string& fkt,
size_t line,
size_t column
140 : _rc(rc), _type(type), _msg(msg), _sdlError(sdlError)
141#if defined(__cpp_lib_source_location) && (__cpp_lib_source_location >= 201907L)
143 _file(loc.file_name()), _fkt(loc.function_name()), _line(loc.line()), _column(loc.column())
144#elif defined(__GNUC__)
146 _file(file), _fkt(fkt), _line(line), _column(column)
149 std::stringstream ss;
150 ss << _msg <<
" {" << sdl::utils::toString(_type) <<
"}{SDL:" << sdlError <<
"}";
151 ss <<
"{" << _fkt <<
" [" << _file <<
":" << _line <<
"-" << _column <<
"]}";
155static void sdl_term_handler([[maybe_unused]]
int signum, [[maybe_unused]]
const char* signame,
156 [[maybe_unused]]
void* context)
158 std::ignore = sdl_push_quit();
168 while (!
sdl->shallAbort())
170 SDL_Event windowEvent = {};
171 while (!
sdl->shallAbort() && SDL_WaitEventTimeout(
nullptr, 1000))
176 const int prc = SDL_PeepEvents(&windowEvent, 1, SDL_GETEVENT, SDL_EVENT_FIRST,
177 SDL_EVENT_USER_RETRY_DIALOG);
180 if (sdl_log_error(prc,
sdl->getWLog(),
"SDL_PeepEvents"))
184 WLog_Print(
sdl->getWLog(), WLOG_TRACE,
"got event %s [0x%08" PRIx32
"]",
185 sdl::utils::toString(windowEvent.type).c_str(), windowEvent.type);
188 if (
sdl->getDialog().handleEvent(windowEvent))
192 if (
sdl->shallAbort(
true) && (windowEvent.type != SDL_EVENT_QUIT))
195 if (!
sdl->handleEvent(windowEvent))
196 throw ErrorMsg{ -1, windowEvent.type,
"sdl->handleEvent" };
198 switch (windowEvent.type)
201 std::ignore =
sdl->abortConnect();
203 case SDL_EVENT_USER_CERT_DIALOG:
206 auto title =
static_cast<const char*
>(windowEvent.user.data1);
207 auto msg =
static_cast<const char*
>(windowEvent.user.data2);
208 if (!sdl_cert_dialog_show(title, msg))
209 throw ErrorMsg{ -1, windowEvent.type,
"sdl_cert_dialog_show" };
212 case SDL_EVENT_USER_SHOW_DIALOG:
215 auto title =
static_cast<const char*
>(windowEvent.user.data1);
216 auto msg =
static_cast<const char*
>(windowEvent.user.data2);
217 if (!sdl_message_dialog_show(title, msg, windowEvent.user.code))
218 throw ErrorMsg{ -1, windowEvent.type,
"sdl_message_dialog_show" };
221 case SDL_EVENT_USER_SCARD_DIALOG:
224 auto title =
static_cast<const char*
>(windowEvent.user.data1);
225 auto msg =
static_cast<const char**
>(windowEvent.user.data2);
226 if (!sdl_scard_dialog_show(title, windowEvent.user.code, msg))
227 throw ErrorMsg{ -1, windowEvent.type,
"sdl_scard_dialog_show" };
230 case SDL_EVENT_USER_AUTH_DIALOG:
233 if (!sdl_auth_dialog_show(
235 throw ErrorMsg{ -1, windowEvent.type,
"sdl_auth_dialog_show" };
238 case SDL_EVENT_USER_UPDATE:
240 std::vector<SDL_Rect> rectangles;
241 std::vector<SDL_Rect> cur;
245 rectangles.insert(rectangles.end(), cur.begin(), cur.end());
246 }
while (!cur.empty());
248 if (!
sdl->drawToWindows(rectangles))
249 throw ErrorMsg{ -1, windowEvent.type,
"sdl->drawToWindows" };
253 if (
sdl->hasRenderers() && !
sdl->presentRenderers())
254 throw ErrorMsg{ -1, windowEvent.type,
"sdl->presentRenderers" };
257 case SDL_EVENT_USER_WINDOW_INFO:
258 sdl->flushWindowInfo();
260 case SDL_EVENT_USER_RENDERER:
261 if (!
sdl->presentRenderers())
262 throw ErrorMsg{ -1, windowEvent.type,
"sdl->presentRenderers" };
264 case SDL_EVENT_USER_OVERLAY:
265 if (!
sdl->updateOverlay())
266 throw ErrorMsg{ -1, windowEvent.type,
"sdl->updateOverlay" };
268 case SDL_EVENT_USER_CREATE_WINDOWS:
270 auto ctx =
static_cast<SdlContext*
>(windowEvent.user.data1);
271 if (!ctx->createWindows())
272 throw ErrorMsg{ -1, windowEvent.type,
"sdl->createWindows" };
275 case SDL_EVENT_USER_WINDOW_RESIZEABLE:
277 auto window =
static_cast<SdlWindow*
>(windowEvent.user.data1);
278 const bool use = windowEvent.user.code != 0;
280 window->resizeable(use);
283 case SDL_EVENT_USER_WINDOW_FULLSCREEN:
285 auto window =
static_cast<SdlWindow*
>(windowEvent.user.data1);
286 const bool enter = windowEvent.user.code != 0;
287 const bool forceOriginalDisplay = windowEvent.user.data2 !=
nullptr;
289 window->fullscreen(enter, forceOriginalDisplay);
292 case SDL_EVENT_USER_WINDOW_MINIMIZE:
293 if (!
sdl->minimizeAllWindows())
294 throw ErrorMsg{ -1, windowEvent.type,
"sdl->minimizeAllWindows" };
296 case SDL_EVENT_USER_POINTER_NULL:
297 if (!
sdl->setCursor(SdlContext::CURSOR_NULL))
298 throw ErrorMsg{ -1, windowEvent.type,
"sdl->setCursor" };
300 case SDL_EVENT_USER_POINTER_DEFAULT:
301 if (!
sdl->setCursor(SdlContext::CURSOR_DEFAULT))
302 throw ErrorMsg{ -1, windowEvent.type,
"sdl->setCursor" };
304 case SDL_EVENT_USER_POINTER_POSITION:
307 static_cast<INT32
>(
reinterpret_cast<uintptr_t
>(windowEvent.user.data1));
309 static_cast<INT32
>(
reinterpret_cast<uintptr_t
>(windowEvent.user.data2));
310 if (!
sdl->moveMouseTo(
311 { static_cast<float>(x) * 1.0f, static_cast<float>(y) * 1.0f }))
312 throw ErrorMsg{ -1, windowEvent.type,
"sdl->moveMouseTo" };
315 case SDL_EVENT_USER_POINTER_SET:
316 if (!
sdl->setCursor(
static_cast<rdpPointer*
>(windowEvent.user.data1)))
317 throw ErrorMsg{ -1, windowEvent.type,
"sdl->setCursor" };
319 case SDL_EVENT_USER_RAIL_MOVE:
321 const auto id =
static_cast<uint32_t
>(
322 reinterpret_cast<uintptr_t
>(windowEvent.user.data1));
323 const auto moveType =
static_cast<uint16_t
>(windowEvent.user.code);
324 sdl->getRailChannelContext().handleLocalMoveRequested(
id, moveType);
327 case SDL_EVENT_USER_QUIT:
335 catch (ErrorMsg& msg)
337 WLog_Print(
sdl->getWLog(), WLOG_ERROR,
"[exception] %s", msg.what());
347[[nodiscard]]
static BOOL sdl_client_global_init()
351 const DWORD wVersionRequested = MAKEWORD(1, 1);
352 const int rc = WSAStartup(wVersionRequested, &wsaData);
355 WLog_ERR(SDL_TAG,
"WSAStartup failed with %s [%d]", gai_strerrorA(rc), rc);
360 return (freerdp_handle_signals() == 0);
364static void sdl_client_global_uninit()
371[[nodiscard]]
static BOOL sdl_client_new(freerdp* instance, rdpContext* context)
375 if (!instance || !context)
379 return sdl->sdl !=
nullptr;
382static void sdl_client_free([[maybe_unused]] freerdp* instance, rdpContext* context)
392[[nodiscard]]
static int sdl_client_start(rdpContext* context)
394 auto sdl = get_context(context);
399[[nodiscard]]
static int sdl_client_stop(rdpContext* context)
401 const int rc = freerdp_client_common_stop(context);
402 auto sdl = get_context(context);
404 const int rc2 =
sdl->join();
410static void RdpClientEntry(RDP_CLIENT_ENTRY_POINTS* pEntryPoints)
412 WINPR_ASSERT(pEntryPoints);
414 ZeroMemory(pEntryPoints,
sizeof(RDP_CLIENT_ENTRY_POINTS));
415 pEntryPoints->Version = RDP_CLIENT_INTERFACE_VERSION;
416 pEntryPoints->Size =
sizeof(RDP_CLIENT_ENTRY_POINTS_V1);
417 pEntryPoints->GlobalInit = sdl_client_global_init;
418 pEntryPoints->GlobalUninit = sdl_client_global_uninit;
420 pEntryPoints->ClientNew = sdl_client_new;
421 pEntryPoints->ClientFree = sdl_client_free;
422 pEntryPoints->ClientStart = sdl_client_start;
423 pEntryPoints->ClientStop = sdl_client_stop;
429 freerdp_client_context_free(&
sdl->common.context);
432[[nodiscard]]
static const char* category2str(
int category)
436 case SDL_LOG_CATEGORY_APPLICATION:
437 return "SDL_LOG_CATEGORY_APPLICATION";
438 case SDL_LOG_CATEGORY_ERROR:
439 return "SDL_LOG_CATEGORY_ERROR";
440 case SDL_LOG_CATEGORY_ASSERT:
441 return "SDL_LOG_CATEGORY_ASSERT";
442 case SDL_LOG_CATEGORY_SYSTEM:
443 return "SDL_LOG_CATEGORY_SYSTEM";
444 case SDL_LOG_CATEGORY_AUDIO:
445 return "SDL_LOG_CATEGORY_AUDIO";
446 case SDL_LOG_CATEGORY_VIDEO:
447 return "SDL_LOG_CATEGORY_VIDEO";
448 case SDL_LOG_CATEGORY_RENDER:
449 return "SDL_LOG_CATEGORY_RENDER";
450 case SDL_LOG_CATEGORY_INPUT:
451 return "SDL_LOG_CATEGORY_INPUT";
452 case SDL_LOG_CATEGORY_TEST:
453 return "SDL_LOG_CATEGORY_TEST";
454 case SDL_LOG_CATEGORY_GPU:
455 return "SDL_LOG_CATEGORY_GPU";
456 case SDL_LOG_CATEGORY_RESERVED2:
457 return "SDL_LOG_CATEGORY_RESERVED2";
458 case SDL_LOG_CATEGORY_RESERVED3:
459 return "SDL_LOG_CATEGORY_RESERVED3";
460 case SDL_LOG_CATEGORY_RESERVED4:
461 return "SDL_LOG_CATEGORY_RESERVED4";
462 case SDL_LOG_CATEGORY_RESERVED5:
463 return "SDL_LOG_CATEGORY_RESERVED5";
464 case SDL_LOG_CATEGORY_RESERVED6:
465 return "SDL_LOG_CATEGORY_RESERVED6";
466 case SDL_LOG_CATEGORY_RESERVED7:
467 return "SDL_LOG_CATEGORY_RESERVED7";
468 case SDL_LOG_CATEGORY_RESERVED8:
469 return "SDL_LOG_CATEGORY_RESERVED8";
470 case SDL_LOG_CATEGORY_RESERVED9:
471 return "SDL_LOG_CATEGORY_RESERVED9";
472 case SDL_LOG_CATEGORY_RESERVED10:
473 return "SDL_LOG_CATEGORY_RESERVED10";
474 case SDL_LOG_CATEGORY_CUSTOM:
476 return "SDL_LOG_CATEGORY_CUSTOM";
480[[nodiscard]]
static SDL_LogPriority wloglevel2dl(DWORD level)
485 return SDL_LOG_PRIORITY_VERBOSE;
487 return SDL_LOG_PRIORITY_DEBUG;
489 return SDL_LOG_PRIORITY_INFO;
491 return SDL_LOG_PRIORITY_WARN;
493 return SDL_LOG_PRIORITY_ERROR;
495 return SDL_LOG_PRIORITY_CRITICAL;
498 return SDL_LOG_PRIORITY_VERBOSE;
502[[nodiscard]]
static DWORD sdlpriority2wlog(SDL_LogPriority priority)
504 DWORD level = WLOG_OFF;
507 case SDL_LOG_PRIORITY_VERBOSE:
510 case SDL_LOG_PRIORITY_DEBUG:
513 case SDL_LOG_PRIORITY_INFO:
516 case SDL_LOG_PRIORITY_WARN:
519 case SDL_LOG_PRIORITY_ERROR:
522 case SDL_LOG_PRIORITY_CRITICAL:
532static void SDLCALL winpr_LogOutputFunction(
void* userdata,
int category, SDL_LogPriority priority,
538 const DWORD level = sdlpriority2wlog(priority);
539 auto log =
sdl->getWLog();
540 if (!WLog_IsLevelActive(log, level))
543 WLog_PrintTextMessage(log, level, __LINE__, __FILE__, __func__,
"[%s] %s",
544 category2str(category), message);
547static void sdl_quit()
549 const auto cat = SDL_LOG_CATEGORY_APPLICATION;
551 ev.type = SDL_EVENT_QUIT;
552 if (!SDL_PushEvent(&ev))
554 SDL_LogError(cat,
"An error occurred: %s", SDL_GetError());
558static void SDLCALL rdp_file_cb(
void* userdata,
const char*
const* filelist,
559 WINPR_ATTR_UNUSED
int filter)
561 auto rdp =
static_cast<std::string*
>(userdata);
563 const auto cat = SDL_LOG_CATEGORY_APPLICATION;
566 SDL_LogError(cat,
"An error occurred: %s", SDL_GetError());
572 SDL_LogError(cat,
"The user did not select any file.");
573 SDL_LogError(cat,
"Most likely, the dialog was canceled.");
580 SDL_LogError(cat,
"Full path to selected file: '%s'", *filelist);
588[[nodiscard]]
static std::string getRdpFile()
590 const auto flags = SDL_INIT_VIDEO | SDL_INIT_EVENTS;
591 SDL_DialogFileFilter filters[] = { {
"RDP files",
"rdp;rdpw" } };
595 if (!SDL_Init(flags))
597 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"SDL_Init failed: %s", SDL_GetError());
601 auto props = SDL_CreateProperties();
602 SDL_SetStringProperty(props, SDL_PROP_FILE_DIALOG_TITLE_STRING,
603 "SDL Freerdp - Open a RDP file");
604 SDL_SetBooleanProperty(props, SDL_PROP_FILE_DIALOG_MANY_BOOLEAN,
false);
605 SDL_SetPointerProperty(props, SDL_PROP_FILE_DIALOG_FILTERS_POINTER, filters);
606 SDL_SetNumberProperty(props, SDL_PROP_FILE_DIALOG_NFILTERS_NUMBER, ARRAYSIZE(filters));
607 SDL_ShowFileDialogWithProperties(SDL_FILEDIALOG_OPENFILE, rdp_file_cb, &rdp, props);
608 SDL_DestroyProperties(props);
612 SDL_Event
event = {};
613 std::ignore = SDL_PollEvent(&event);
628int main(
int argc,
char* argv[])
631 sdl::win32::release_transient_console();
635 RDP_CLIENT_ENTRY_POINTS clientEntryPoints = {};
638 RdpClientEntry(&clientEntryPoints);
640 reinterpret_cast<sdl_rdp_context*
>(freerdp_client_context_new(&clientEntryPoints)),
645 auto sdl = sdl_rdp->sdl;
647 auto settings =
sdl->context()->settings;
648 WINPR_ASSERT(settings);
650 std::string rdp_file;
651 std::vector<char*> args;
652 args.reserve(WINPR_ASSERTING_INT_CAST(
size_t, argc));
653 for (
auto x = 0; x < argc; x++)
654 args.push_back(argv[x]);
658 rdp_file = getRdpFile();
659 if (!rdp_file.empty())
661 args.push_back(rdp_file.data());
665 auto status = freerdp_client_settings_parse_command_line_ex(
666 settings, WINPR_ASSERTING_INT_CAST(
int, args.size()), args.data(), FALSE,
sdl->args(),
667 sdl->argsCount(), &SdlContext::argumentHandler,
sdl);
668 sdl_rdp->sdl->setMetadata();
671 rc = freerdp_client_settings_command_line_status_print_ex(settings, status, argc, argv,
675 if (sdl_list_monitors(
sdl) != 0)
682 case COMMAND_LINE_STATUS_PRINT:
683 case COMMAND_LINE_STATUS_PRINT_VERSION:
684 case COMMAND_LINE_STATUS_PRINT_BUILDCONFIG:
686 case COMMAND_LINE_STATUS_PRINT_HELP:
687 SdlPref::print_config_file_help(3);
696 if (!SDL_SetHint(SDL_HINT_ALLOW_ALT_TAB_WHILE_GRABBED,
"0"))
698 if (!SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR,
"0"))
700 if (!SDL_SetHint(SDL_HINT_PEN_MOUSE_EVENTS,
"0"))
702 if (!SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS,
"0"))
704 if (!SDL_SetHint(SDL_HINT_PEN_TOUCH_EVENTS,
"1"))
706 if (!SDL_SetHint(SDL_HINT_TRACKPAD_IS_TOUCH_ONLY,
"1"))
708 if (!SDL_SetHint(SDL_HINT_SCREENSAVER_INHIBIT_ACTIVITY_NAME,
"RDP session running"))
710#if SDL_VERSION_ATLEAST(3, 4, 0)
711 if (!SDL_SetHint(SDL_HINT_MOUSE_DPI_SCALE_CURSORS,
"1"))
717 std::ignore = SDL_SetHint(SDL_HINT_VIDEO_MAC_FULLSCREEN_MENU_VISIBILITY,
"1");
722 !SDL_SetHint(SDL_HINT_RENDER_VSYNC,
"1"))
727 if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS))
729 WLog_Print(
sdl->getWLog(), WLOG_ERROR,
"SDL_Init failed: %s", SDL_GetError());
733#if defined(WITH_SDL_AUDIO)
736 if (!SDL_InitSubSystem(SDL_INIT_AUDIO))
737 WLog_Print(
sdl->getWLog(), WLOG_WARN,
"SDL_InitSubSystem(SDL_INIT_AUDIO): %s",
741#if defined(__APPLE__)
744 SDL_SetModState(SDL_GetModState() | SDL_KMOD_NUM);
748 SDL_SetLogOutputFunction(winpr_LogOutputFunction,
sdl);
749 auto level = WLog_GetLogLevel(
sdl->getWLog());
750 SDL_SetLogPriorities(wloglevel2dl(level));
752 auto backend = SDL_GetCurrentVideoDriver();
753 WLog_Print(
sdl->getWLog(), WLOG_DEBUG,
"client is using backend '%s'", backend);
761 freerdp_del_signal_cleanup_handler(
sdl->context(), sdl_term_handler);
762 sdl_dialogs_uninit();
765 if (!freerdp_add_signal_cleanup_handler(
sdl->context(), sdl_term_handler))
767 if (!
sdl->detectDisplays())
771 auto context =
sdl->context();
772 WINPR_ASSERT(context);
774 if (!stream_dump_register_handlers(context, CONNECTION_STATE_MCS_CREATE_REQUEST, FALSE))
777 if (freerdp_client_start(context) != 0)
782 if (freerdp_client_stop(context) != 0)
785 if (
sdl->exitCode() != 0)
786 rc =
sdl->exitCode();
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.