From 68995ddffb02eeb41578a346803886e645a96789 Mon Sep 17 00:00:00 2001 From: Armin Novak Date: Tue, 30 May 2023 21:04:53 +0200 Subject: [PATCH] [client,sdl] wrap WinPR event in class --- client/SDL/sdl_freerdp.cpp | 51 ++++++++++++++++---------------------- client/SDL/sdl_freerdp.hpp | 8 +++--- client/SDL/sdl_utils.cpp | 30 ++++++++++++++++++++++ client/SDL/sdl_utils.hpp | 2 +- 4 files changed, 56 insertions(+), 35 deletions(-) diff --git a/client/SDL/sdl_freerdp.cpp b/client/SDL/sdl_freerdp.cpp index c960230fb..54fe116ac 100644 --- a/client/SDL/sdl_freerdp.cpp +++ b/client/SDL/sdl_freerdp.cpp @@ -278,7 +278,7 @@ static BOOL sdl_begin_paint(rdpContext* context) WINPR_ASSERT(sdl); - HANDLE handles[] = { sdl->update_complete, freerdp_abort_event(context) }; + HANDLE handles[] = { sdl->update_complete->handle(), freerdp_abort_event(context) }; const DWORD status = WaitForMultipleObjects(ARRAYSIZE(handles), handles, FALSE, INFINITE); switch (status) { @@ -287,8 +287,7 @@ static BOOL sdl_begin_paint(rdpContext* context) default: return FALSE; } - if (!ResetEvent(sdl->update_complete)) - return FALSE; + sdl->update_complete->clear(); gdi = context->gdi; WINPR_ASSERT(gdi); @@ -321,9 +320,10 @@ class SdlEventUpdateTriggerGuard } ~SdlEventUpdateTriggerGuard() { - SetEvent(_sdl->update_complete); + _sdl->update_complete->set(); } }; + static BOOL sdl_end_paint_process(rdpContext* context) { rdpGdi* gdi; @@ -483,10 +483,10 @@ static BOOL sdl_play_sound(rdpContext* context, const PLAY_SOUND_UPDATE* play_so static BOOL sdl_wait_for_init(sdlContext* sdl) { WINPR_ASSERT(sdl); - if (!SetEvent(sdl->initialize)) - return FALSE; + WINPR_ASSERT(sdl->initialize); + sdl->initialize->set(); - HANDLE handles[] = { sdl->initialized, freerdp_abort_event(&sdl->common.context) }; + HANDLE handles[] = { sdl->initialized->handle(), freerdp_abort_event(&sdl->common.context) }; const DWORD rc = WaitForMultipleObjects(ARRAYSIZE(handles), handles, FALSE, INFINITE); switch (rc) @@ -653,20 +653,18 @@ static BOOL sdl_create_windows(sdlContext* sdl) rc = TRUE; fail: - if (!SetEvent(sdl->windows_created)) - rc = FALSE; + sdl->windows_created->set(); return rc; } static BOOL sdl_wait_create_windows(sdlContext* sdl) { std::lock_guard lock(*sdl->critical); - if (!ResetEvent(sdl->windows_created)) - return FALSE; + sdl->windows_created->clear(); if (!sdl_push_user_event(SDL_USEREVENT_CREATE_WINDOWS, sdl)) return FALSE; - HANDLE handles[] = { sdl->initialized, freerdp_abort_event(&sdl->common.context) }; + HANDLE handles[] = { sdl->initialized->handle(), freerdp_abort_event(&sdl->common.context) }; const DWORD rc = WaitForMultipleObjects(ARRAYSIZE(handles), handles, FALSE, INFINITE); switch (rc) @@ -683,7 +681,7 @@ static int sdl_run(sdlContext* sdl) int rc = -1; WINPR_ASSERT(sdl); - HANDLE handles[] = { sdl->initialize, freerdp_abort_event(&sdl->common.context) }; + HANDLE handles[] = { sdl->initialize->handle(), freerdp_abort_event(&sdl->common.context) }; const DWORD status = WaitForMultipleObjects(ARRAYSIZE(handles), handles, FALSE, INFINITE); switch (status) { @@ -694,8 +692,7 @@ static int sdl_run(sdlContext* sdl) } SDL_Init(SDL_INIT_VIDEO); - if (!SetEvent(sdl->initialized)) - goto fail; + sdl->initialized->set(); while (!freerdp_shall_disconnect_context(&sdl->common.context)) { @@ -1176,11 +1173,11 @@ static BOOL sdl_client_new(freerdp* instance, rdpContext* context) #endif /* TODO: Client display set up */ - sdl->critical.reset(new CriticalSection); - sdl->initialize = CreateEventA(nullptr, TRUE, FALSE, nullptr); - sdl->initialized = CreateEventA(nullptr, TRUE, FALSE, nullptr); - sdl->update_complete = CreateEventA(nullptr, TRUE, TRUE, nullptr); - sdl->windows_created = CreateEventA(nullptr, TRUE, FALSE, nullptr); + sdl->critical = std::make_unique(); + sdl->initialize = std::make_unique(); + sdl->initialized = std::make_unique(); + sdl->update_complete = std::make_unique(true); + sdl->windows_created = std::make_unique(); return sdl->initialize && sdl->initialized && sdl->update_complete && sdl->windows_created; } @@ -1191,16 +1188,10 @@ static void sdl_client_free(freerdp* instance, rdpContext* context) if (!context) return; - CloseHandle(sdl->initialize); - CloseHandle(sdl->initialized); - CloseHandle(sdl->update_complete); - CloseHandle(sdl->windows_created); - - sdl->initialize = nullptr; - sdl->initialized = nullptr; - sdl->update_complete = nullptr; - sdl->windows_created = nullptr; - + sdl->initialize.reset(); + sdl->initialized.reset(); + sdl->update_complete.reset(); + sdl->windows_created.reset(); sdl->critical.reset(); } diff --git a/client/SDL/sdl_freerdp.hpp b/client/SDL/sdl_freerdp.hpp index 7b4b12164..88c7adf38 100644 --- a/client/SDL/sdl_freerdp.hpp +++ b/client/SDL/sdl_freerdp.hpp @@ -59,10 +59,10 @@ struct sdl_context std::unique_ptr critical; std::thread thread; - HANDLE initialize; - HANDLE initialized; - HANDLE update_complete; - HANDLE windows_created; + std::unique_ptr initialize; + std::unique_ptr initialized; + std::unique_ptr update_complete; + std::unique_ptr windows_created; int exit_code; SDL_Surface* primary; diff --git a/client/SDL/sdl_utils.cpp b/client/SDL/sdl_utils.cpp index ad95644fe..9aecfe629 100644 --- a/client/SDL/sdl_utils.cpp +++ b/client/SDL/sdl_utils.cpp @@ -194,3 +194,33 @@ void CriticalSection::unlock() { LeaveCriticalSection(&_section); } + +WinPREvent::WinPREvent(bool initial) + : _handle(CreateEventA(nullptr, TRUE, initial ? TRUE : FALSE, nullptr)) +{ +} + +WinPREvent::~WinPREvent() +{ + CloseHandle(_handle); +} + +void WinPREvent::set() +{ + SetEvent(_handle); +} + +void WinPREvent::clear() +{ + ResetEvent(_handle); +} + +bool WinPREvent::isSet() const +{ + return WaitForSingleObject(_handle, 0) == WAIT_OBJECT_0; +} + +HANDLE WinPREvent::handle() const +{ + return _handle; +} diff --git a/client/SDL/sdl_utils.hpp b/client/SDL/sdl_utils.hpp index f1df081d8..75d4ced89 100644 --- a/client/SDL/sdl_utils.hpp +++ b/client/SDL/sdl_utils.hpp @@ -45,7 +45,7 @@ class WinPREvent ~WinPREvent(); void set(); - void reset(); + void clear(); bool isSet() const; HANDLE handle() const;