FreeRDP
Loading...
Searching...
No Matches
SDL3/sdl_freerdp.cpp
1
19
20#include <iostream>
21#include <memory>
22#include <mutex>
23
24#include <freerdp/config.h>
25
26#if defined(__has_include)
27#if __has_include(<version>)
28#include <version>
29#endif
30#endif
31
32#include <cerrno>
33#include <cstdio>
34#include <cstring>
35
36#if defined(__cpp_lib_source_location) && (__cpp_lib_source_location >= 201907L)
37#include <source_location>
38#endif
39
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>
45
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>
51
52#include <freerdp/log.h>
53#include <winpr/assert.h>
54#include <winpr/config.h>
55#include <winpr/crt.h>
56#include <winpr/synch.h>
57
58#include <SDL3/SDL.h>
59#if !defined(__MINGW32__)
60#include <SDL3/SDL_main.h>
61#endif
62#include <SDL3/SDL_hints.h>
63#include <SDL3/SDL_oldnames.h>
64#include <SDL3/SDL_video.h>
65
66#include <sdl_config.hpp>
67
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"
77#if defined(_WIN32)
78#include "sdl_win32_console.hpp"
79#endif
80#include "sdl_utils.hpp"
81
82#if defined(_WIN32)
83#define SDL_TAG CLIENT_TAG("SDL")
84#endif
85
86class ErrorMsg : public std::exception
87{
88 public:
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)
92 ,
93 std::source_location loc = std::source_location::current()
94#elif defined(__GNUC__)
95 ,
96 const std::string& file = __builtin_FILE(),
97 const std::string& fkt = __builtin_FUNCTION(), size_t line = __builtin_LINE(),
98#if defined(__clang__)
99 size_t column = __builtin_COLUMN()
100#else
101 size_t column = 0
102#endif
103#endif
104 );
105
106 [[nodiscard]] int rc() const;
107 [[nodiscard]] const char* what() const noexcept override;
108
109 private:
110 int _rc;
111 Uint32 _type;
112 std::string _msg;
113 std::string _sdlError;
114 std::string _buffer;
115
116 std::string _file;
117 std::string _fkt;
118 size_t _line = 0;
119 size_t _column = 0;
120};
121const char* ErrorMsg::what() const noexcept
122{
123 return _buffer.c_str();
124}
125
126int ErrorMsg::rc() const
127{
128 return _rc;
129}
130
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)
133 ,
134 std::source_location loc
135#elif defined(__GNUC__)
136 ,
137 const std::string& file, const std::string& fkt, size_t line, size_t column
138#endif
139 )
140 : _rc(rc), _type(type), _msg(msg), _sdlError(sdlError)
141#if defined(__cpp_lib_source_location) && (__cpp_lib_source_location >= 201907L)
142 ,
143 _file(loc.file_name()), _fkt(loc.function_name()), _line(loc.line()), _column(loc.column())
144#elif defined(__GNUC__)
145 ,
146 _file(file), _fkt(fkt), _line(line), _column(column)
147#endif
148{
149 std::stringstream ss;
150 ss << _msg << " {" << sdl::utils::toString(_type) << "}{SDL:" << sdlError << "}";
151 ss << "{" << _fkt << " [" << _file << ":" << _line << "-" << _column << "]}";
152 _buffer = ss.str();
153}
154
155static void sdl_term_handler([[maybe_unused]] int signum, [[maybe_unused]] const char* signame,
156 [[maybe_unused]] void* context)
157{
158 std::ignore = sdl_push_quit();
159}
160
161[[nodiscard]] static int sdl_run(SdlContext* sdl)
162{
163 int rc = -1;
164 WINPR_ASSERT(sdl);
165
166 try
167 {
168 while (!sdl->shallAbort())
169 {
170 SDL_Event windowEvent = {};
171 while (!sdl->shallAbort() && SDL_WaitEventTimeout(nullptr, 1000))
172 {
173 /* Only poll standard SDL events and SDL_EVENT_USERS meant to create
174 * dialogs. do not process the dialog return value events here.
175 */
176 const int prc = SDL_PeepEvents(&windowEvent, 1, SDL_GETEVENT, SDL_EVENT_FIRST,
177 SDL_EVENT_USER_RETRY_DIALOG);
178 if (prc < 0)
179 {
180 if (sdl_log_error(prc, sdl->getWLog(), "SDL_PeepEvents"))
181 continue;
182 }
183
184 WLog_Print(sdl->getWLog(), WLOG_TRACE, "got event %s [0x%08" PRIx32 "]",
185 sdl::utils::toString(windowEvent.type).c_str(), windowEvent.type);
186 /* The connection dialog paints on window events, so let it see them even
187 * after the session is gone: it may still show an error message. */
188 if (sdl->getDialog().handleEvent(windowEvent))
189 continue;
190
191 /* Still handle a quit, it cancels the session */
192 if (sdl->shallAbort(true) && (windowEvent.type != SDL_EVENT_QUIT))
193 continue;
194
195 if (!sdl->handleEvent(windowEvent))
196 throw ErrorMsg{ -1, windowEvent.type, "sdl->handleEvent" };
197
198 switch (windowEvent.type)
199 {
200 case SDL_EVENT_QUIT:
201 std::ignore = sdl->abortConnect();
202 break;
203 case SDL_EVENT_USER_CERT_DIALOG:
204 {
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" };
210 }
211 break;
212 case SDL_EVENT_USER_SHOW_DIALOG:
213 {
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" };
219 }
220 break;
221 case SDL_EVENT_USER_SCARD_DIALOG:
222 {
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" };
228 }
229 break;
230 case SDL_EVENT_USER_AUTH_DIALOG:
231 {
233 if (!sdl_auth_dialog_show(
234 reinterpret_cast<const SDL_UserAuthArg*>(windowEvent.padding)))
235 throw ErrorMsg{ -1, windowEvent.type, "sdl_auth_dialog_show" };
236 }
237 break;
238 case SDL_EVENT_USER_UPDATE:
239 {
240 std::vector<SDL_Rect> rectangles;
241 std::vector<SDL_Rect> cur;
242 do
243 {
244 cur = sdl->pop();
245 rectangles.insert(rectangles.end(), cur.begin(), cur.end());
246 } while (!cur.empty());
247
248 if (!sdl->drawToWindows(rectangles))
249 throw ErrorMsg{ -1, windowEvent.type, "sdl->drawToWindows" };
250
251 /* geometry updates come with the session updates, not with the
252 * renderers' frames */
253 if (sdl->hasRenderers() && !sdl->presentRenderers())
254 throw ErrorMsg{ -1, windowEvent.type, "sdl->presentRenderers" };
255 }
256 break;
257 case SDL_EVENT_USER_WINDOW_INFO:
258 sdl->flushWindowInfo();
259 break;
260 case SDL_EVENT_USER_RENDERER:
261 if (!sdl->presentRenderers())
262 throw ErrorMsg{ -1, windowEvent.type, "sdl->presentRenderers" };
263 break;
264 case SDL_EVENT_USER_OVERLAY:
265 if (!sdl->updateOverlay())
266 throw ErrorMsg{ -1, windowEvent.type, "sdl->updateOverlay" };
267 break;
268 case SDL_EVENT_USER_CREATE_WINDOWS:
269 {
270 auto ctx = static_cast<SdlContext*>(windowEvent.user.data1);
271 if (!ctx->createWindows())
272 throw ErrorMsg{ -1, windowEvent.type, "sdl->createWindows" };
273 }
274 break;
275 case SDL_EVENT_USER_WINDOW_RESIZEABLE:
276 {
277 auto window = static_cast<SdlWindow*>(windowEvent.user.data1);
278 const bool use = windowEvent.user.code != 0;
279 if (window)
280 window->resizeable(use);
281 }
282 break;
283 case SDL_EVENT_USER_WINDOW_FULLSCREEN:
284 {
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;
288 if (window)
289 window->fullscreen(enter, forceOriginalDisplay);
290 }
291 break;
292 case SDL_EVENT_USER_WINDOW_MINIMIZE:
293 if (!sdl->minimizeAllWindows())
294 throw ErrorMsg{ -1, windowEvent.type, "sdl->minimizeAllWindows" };
295 break;
296 case SDL_EVENT_USER_POINTER_NULL:
297 if (!sdl->setCursor(SdlContext::CURSOR_NULL))
298 throw ErrorMsg{ -1, windowEvent.type, "sdl->setCursor" };
299 break;
300 case SDL_EVENT_USER_POINTER_DEFAULT:
301 if (!sdl->setCursor(SdlContext::CURSOR_DEFAULT))
302 throw ErrorMsg{ -1, windowEvent.type, "sdl->setCursor" };
303 break;
304 case SDL_EVENT_USER_POINTER_POSITION:
305 {
306 const auto x =
307 static_cast<INT32>(reinterpret_cast<uintptr_t>(windowEvent.user.data1));
308 const auto y =
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" };
313 }
314 break;
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" };
318 break;
319 case SDL_EVENT_USER_RAIL_MOVE:
320 {
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);
325 }
326 break;
327 case SDL_EVENT_USER_QUIT:
328 default:
329 break;
330 }
331 }
332 }
333 rc = 1;
334 }
335 catch (ErrorMsg& msg)
336 {
337 WLog_Print(sdl->getWLog(), WLOG_ERROR, "[exception] %s", msg.what());
338 rc = msg.rc();
339 }
340
341 return rc;
342}
343
344/* Optional global initializer.
345 * Here we just register a signal handler to print out stack traces
346 * if available. */
347[[nodiscard]] static BOOL sdl_client_global_init()
348{
349#if defined(_WIN32)
350 WSADATA wsaData = {};
351 const DWORD wVersionRequested = MAKEWORD(1, 1);
352 const int rc = WSAStartup(wVersionRequested, &wsaData);
353 if (rc != 0)
354 {
355 WLog_ERR(SDL_TAG, "WSAStartup failed with %s [%d]", gai_strerrorA(rc), rc);
356 return FALSE;
357 }
358#endif
359
360 return (freerdp_handle_signals() == 0);
361}
362
363/* Optional global tear down */
364static void sdl_client_global_uninit()
365{
366#if defined(_WIN32)
367 WSACleanup();
368#endif
369}
370
371[[nodiscard]] static BOOL sdl_client_new(freerdp* instance, rdpContext* context)
372{
373 auto sdl = reinterpret_cast<sdl_rdp_context*>(context);
374
375 if (!instance || !context)
376 return FALSE;
377
378 sdl->sdl = new SdlContext(context);
379 return sdl->sdl != nullptr;
380}
381
382static void sdl_client_free([[maybe_unused]] freerdp* instance, rdpContext* context)
383{
384 auto sdl = reinterpret_cast<sdl_rdp_context*>(context);
385
386 if (!context)
387 return;
388
389 delete sdl->sdl;
390}
391
392[[nodiscard]] static int sdl_client_start(rdpContext* context)
393{
394 auto sdl = get_context(context);
395 WINPR_ASSERT(sdl);
396 return sdl->start();
397}
398
399[[nodiscard]] static int sdl_client_stop(rdpContext* context)
400{
401 const int rc = freerdp_client_common_stop(context);
402 auto sdl = get_context(context);
403 WINPR_ASSERT(sdl);
404 const int rc2 = sdl->join();
405 if (rc < 0)
406 return rc;
407 return rc2;
408}
409
410static void RdpClientEntry(RDP_CLIENT_ENTRY_POINTS* pEntryPoints)
411{
412 WINPR_ASSERT(pEntryPoints);
413
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;
419 pEntryPoints->ContextSize = sizeof(sdl_rdp_context);
420 pEntryPoints->ClientNew = sdl_client_new;
421 pEntryPoints->ClientFree = sdl_client_free;
422 pEntryPoints->ClientStart = sdl_client_start;
423 pEntryPoints->ClientStop = sdl_client_stop;
424}
425
426static void context_free(sdl_rdp_context* sdl)
427{
428 if (sdl)
429 freerdp_client_context_free(&sdl->common.context);
430}
431
432[[nodiscard]] static const char* category2str(int category)
433{
434 switch (category)
435 {
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:
475 default:
476 return "SDL_LOG_CATEGORY_CUSTOM";
477 }
478}
479
480[[nodiscard]] static SDL_LogPriority wloglevel2dl(DWORD level)
481{
482 switch (level)
483 {
484 case WLOG_TRACE:
485 return SDL_LOG_PRIORITY_VERBOSE;
486 case WLOG_DEBUG:
487 return SDL_LOG_PRIORITY_DEBUG;
488 case WLOG_INFO:
489 return SDL_LOG_PRIORITY_INFO;
490 case WLOG_WARN:
491 return SDL_LOG_PRIORITY_WARN;
492 case WLOG_ERROR:
493 return SDL_LOG_PRIORITY_ERROR;
494 case WLOG_FATAL:
495 return SDL_LOG_PRIORITY_CRITICAL;
496 case WLOG_OFF:
497 default:
498 return SDL_LOG_PRIORITY_VERBOSE;
499 }
500}
501
502[[nodiscard]] static DWORD sdlpriority2wlog(SDL_LogPriority priority)
503{
504 DWORD level = WLOG_OFF;
505 switch (priority)
506 {
507 case SDL_LOG_PRIORITY_VERBOSE:
508 level = WLOG_TRACE;
509 break;
510 case SDL_LOG_PRIORITY_DEBUG:
511 level = WLOG_DEBUG;
512 break;
513 case SDL_LOG_PRIORITY_INFO:
514 level = WLOG_INFO;
515 break;
516 case SDL_LOG_PRIORITY_WARN:
517 level = WLOG_WARN;
518 break;
519 case SDL_LOG_PRIORITY_ERROR:
520 level = WLOG_ERROR;
521 break;
522 case SDL_LOG_PRIORITY_CRITICAL:
523 level = WLOG_FATAL;
524 break;
525 default:
526 break;
527 }
528
529 return level;
530}
531
532static void SDLCALL winpr_LogOutputFunction(void* userdata, int category, SDL_LogPriority priority,
533 const char* message)
534{
535 auto sdl = static_cast<SdlContext*>(userdata);
536 WINPR_ASSERT(sdl);
537
538 const DWORD level = sdlpriority2wlog(priority);
539 auto log = sdl->getWLog();
540 if (!WLog_IsLevelActive(log, level))
541 return;
542
543 WLog_PrintTextMessage(log, level, __LINE__, __FILE__, __func__, "[%s] %s",
544 category2str(category), message);
545}
546
547static void sdl_quit()
548{
549 const auto cat = SDL_LOG_CATEGORY_APPLICATION;
550 SDL_Event ev = {};
551 ev.type = SDL_EVENT_QUIT;
552 if (!SDL_PushEvent(&ev))
553 {
554 SDL_LogError(cat, "An error occurred: %s", SDL_GetError());
555 }
556}
557
558static void SDLCALL rdp_file_cb(void* userdata, const char* const* filelist,
559 WINPR_ATTR_UNUSED int filter)
560{
561 auto rdp = static_cast<std::string*>(userdata);
562
563 const auto cat = SDL_LOG_CATEGORY_APPLICATION;
564 if (!filelist)
565 {
566 SDL_LogError(cat, "An error occurred: %s", SDL_GetError());
567 sdl_quit();
568 return;
569 }
570 else if (!*filelist)
571 {
572 SDL_LogError(cat, "The user did not select any file.");
573 SDL_LogError(cat, "Most likely, the dialog was canceled.");
574 sdl_quit();
575 return;
576 }
577
578 while (*filelist)
579 {
580 SDL_LogError(cat, "Full path to selected file: '%s'", *filelist);
581 *rdp = *filelist;
582 filelist++;
583 }
584
585 sdl_quit();
586}
587
588[[nodiscard]] static std::string getRdpFile()
589{
590 const auto flags = SDL_INIT_VIDEO | SDL_INIT_EVENTS;
591 SDL_DialogFileFilter filters[] = { { "RDP files", "rdp;rdpw" } };
592 std::string rdp;
593
594 bool running = true;
595 if (!SDL_Init(flags))
596 {
597 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init failed: %s", SDL_GetError());
598 return {};
599 }
600
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);
609
610 do
611 {
612 SDL_Event event = {};
613 std::ignore = SDL_PollEvent(&event);
614
615 switch (event.type)
616 {
617 case SDL_EVENT_QUIT:
618 running = false;
619 break;
620 default:
621 break;
622 }
623 } while (running);
624 SDL_Quit();
625 return rdp;
626}
627
628int main(int argc, char* argv[])
629{
630#if defined(_WIN32)
631 sdl::win32::release_transient_console();
632#endif
633
634 int rc = -1;
635 RDP_CLIENT_ENTRY_POINTS clientEntryPoints = {};
636
637 /* Allocate the RDP context first, we need to pass it to SDL */
638 RdpClientEntry(&clientEntryPoints);
639 std::unique_ptr<sdl_rdp_context, void (*)(sdl_rdp_context*)> sdl_rdp(
640 reinterpret_cast<sdl_rdp_context*>(freerdp_client_context_new(&clientEntryPoints)),
641 context_free);
642
643 if (!sdl_rdp)
644 return -1;
645 auto sdl = sdl_rdp->sdl;
646
647 auto settings = sdl->context()->settings;
648 WINPR_ASSERT(settings);
649
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]);
655
656 if (argc == 1)
657 {
658 rdp_file = getRdpFile();
659 if (!rdp_file.empty())
660 {
661 args.push_back(rdp_file.data());
662 }
663 }
664
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();
669 if (status)
670 {
671 rc = freerdp_client_settings_command_line_status_print_ex(settings, status, argc, argv,
672 sdl->args());
673 if (freerdp_settings_get_bool(settings, FreeRDP_ListMonitors))
674 {
675 if (sdl_list_monitors(sdl) != 0)
676 return -1;
677 }
678 else
679 {
680 switch (status)
681 {
682 case COMMAND_LINE_STATUS_PRINT:
683 case COMMAND_LINE_STATUS_PRINT_VERSION:
684 case COMMAND_LINE_STATUS_PRINT_BUILDCONFIG:
685 break;
686 case COMMAND_LINE_STATUS_PRINT_HELP:
687 SdlPref::print_config_file_help(3);
688 break;
689 default:
690 break;
691 }
692 }
693 return rc;
694 }
695
696 if (!SDL_SetHint(SDL_HINT_ALLOW_ALT_TAB_WHILE_GRABBED, "0"))
697 return -1;
698 if (!SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0"))
699 return -1;
700 if (!SDL_SetHint(SDL_HINT_PEN_MOUSE_EVENTS, "0"))
701 return -1;
702 if (!SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS, "0"))
703 return -1;
704 if (!SDL_SetHint(SDL_HINT_PEN_TOUCH_EVENTS, "1"))
705 return -1;
706 if (!SDL_SetHint(SDL_HINT_TRACKPAD_IS_TOUCH_ONLY, "1"))
707 return -1;
708 if (!SDL_SetHint(SDL_HINT_SCREENSAVER_INHIBIT_ACTIVITY_NAME, "RDP session running"))
709 return -1;
710#if SDL_VERSION_ATLEAST(3, 4, 0)
711 if (!SDL_SetHint(SDL_HINT_MOUSE_DPI_SCALE_CURSORS, "1"))
712 return -1;
713#endif
714 /* /f enters fullscreen with SDL_SetWindowFullscreen, and SDL's "auto" then hides the macOS menu
715 * bar for good: no menu, no title bar, no way out. Fails if the environment variable is set,
716 * which then wins, so the result is ignored. */
717 std::ignore = SDL_SetHint(SDL_HINT_VIDEO_MAC_FULLSCREEN_MENU_VISIBILITY, "1");
718
719#if defined(_WIN32)
720 /* Without vsync D3D11 drops a frame while one is still queued (DXGI_PRESENT_DO_NOT_WAIT). */
721 if (freerdp_settings_get_bool(settings, FreeRDP_RemoteApplicationMode) &&
722 !SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1"))
723 return -1;
724#endif
725
726 /* Basic SDL initialization */
727 if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS))
728 {
729 WLog_Print(sdl->getWLog(), WLOG_ERROR, "SDL_Init failed: %s", SDL_GetError());
730 return -1;
731 }
732
733#if defined(WITH_SDL_AUDIO)
734 /* Keeps the audio channels from initializing or quitting SDL audio
735 * while this thread pumps events. */
736 if (!SDL_InitSubSystem(SDL_INIT_AUDIO))
737 WLog_Print(sdl->getWLog(), WLOG_WARN, "SDL_InitSubSystem(SDL_INIT_AUDIO): %s",
738 SDL_GetError());
739#endif
740
741#if defined(__APPLE__)
742 /* macOS has no num lock state, SDL_KMOD_NUM always starts off and we would turn num lock
743 * off on the server with every sync. Start with it on, the server LEDs take over from there. */
744 SDL_SetModState(SDL_GetModState() | SDL_KMOD_NUM);
745#endif
746
747 /* Redirect SDL log messages to wLog */
748 SDL_SetLogOutputFunction(winpr_LogOutputFunction, sdl);
749 auto level = WLog_GetLogLevel(sdl->getWLog());
750 SDL_SetLogPriorities(wloglevel2dl(level));
751
752 auto backend = SDL_GetCurrentVideoDriver();
753 WLog_Print(sdl->getWLog(), WLOG_DEBUG, "client is using backend '%s'", backend);
754 sdl_dialogs_init();
755
756 /* SDL cleanup code if the client exits */
757 ScopeGuard guard(
758 [&]()
759 {
760 sdl->cleanup();
761 freerdp_del_signal_cleanup_handler(sdl->context(), sdl_term_handler);
762 sdl_dialogs_uninit();
763 SDL_Quit();
764 });
765 if (!freerdp_add_signal_cleanup_handler(sdl->context(), sdl_term_handler))
766 return -1;
767 if (!sdl->detectDisplays())
768 return -1;
769
770 /* Initialize RDP */
771 auto context = sdl->context();
772 WINPR_ASSERT(context);
773
774 if (!stream_dump_register_handlers(context, CONNECTION_STATE_MCS_CREATE_REQUEST, FALSE))
775 return -1;
776
777 if (freerdp_client_start(context) != 0)
778 return -1;
779
780 rc = sdl_run(sdl);
781
782 if (freerdp_client_stop(context) != 0)
783 return -1;
784
785 if (sdl->exitCode() != 0)
786 rc = sdl->exitCode();
787
788 return rc;
789}
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.