From 3551459e2c83a706292eea0eb03b1f17973e5a3e Mon Sep 17 00:00:00 2001 From: akallabeth Date: Thu, 6 Mar 2025 16:21:32 +0100 Subject: [PATCH] sdl-common --- client/SDL/SDL2/sdl_utils.cpp | 49 ------------ client/SDL/SDL2/sdl_utils.hpp | 34 +------- client/SDL/SDL3/sdl_utils.cpp | 49 ------------ client/SDL/SDL3/sdl_utils.hpp | 36 +-------- client/SDL/common/CMakeLists.txt | 4 +- client/SDL/common/sdl_common_utils.cpp | 103 +++++++++++++++++++++++++ client/SDL/common/sdl_common_utils.hpp | 65 ++++++++++++++++ 7 files changed, 174 insertions(+), 166 deletions(-) create mode 100644 client/SDL/common/sdl_common_utils.cpp create mode 100644 client/SDL/common/sdl_common_utils.hpp diff --git a/client/SDL/SDL2/sdl_utils.cpp b/client/SDL/SDL2/sdl_utils.cpp index ce5f6a87d..9b24bb948 100644 --- a/client/SDL/SDL2/sdl_utils.cpp +++ b/client/SDL/SDL2/sdl_utils.cpp @@ -233,55 +233,6 @@ BOOL sdl_push_user_event(Uint32 type, ...) return SDL_PushEvent(&ev) == 1; } -CriticalSection::CriticalSection() -{ - InitializeCriticalSection(&_section); -} - -CriticalSection::~CriticalSection() -{ - DeleteCriticalSection(&_section); -} - -void CriticalSection::lock() -{ - EnterCriticalSection(&_section); -} - -void CriticalSection::unlock() -{ - LeaveCriticalSection(&_section); -} - -WinPREvent::WinPREvent(bool initial) : _handle(CreateEventA(nullptr, TRUE, initial, nullptr)) -{ -} - -WinPREvent::~WinPREvent() -{ - (void)CloseHandle(_handle); -} - -void WinPREvent::set() -{ - (void)SetEvent(_handle); -} - -void WinPREvent::clear() -{ - (void)ResetEvent(_handle); -} - -bool WinPREvent::isSet() const -{ - return WaitForSingleObject(_handle, 0) == WAIT_OBJECT_0; -} - -HANDLE WinPREvent::handle() const -{ - return _handle; -} - bool sdl_push_quit() { SDL_Event ev = {}; diff --git a/client/SDL/SDL2/sdl_utils.hpp b/client/SDL/SDL2/sdl_utils.hpp index df989db86..586d0e1b0 100644 --- a/client/SDL/SDL2/sdl_utils.hpp +++ b/client/SDL/SDL2/sdl_utils.hpp @@ -26,39 +26,7 @@ #include #include -class CriticalSection -{ - public: - CriticalSection(); - CriticalSection(const CriticalSection& other) = delete; - CriticalSection(CriticalSection&& other) = delete; - ~CriticalSection(); - - CriticalSection& operator=(const CriticalSection& other) = delete; - CriticalSection& operator=(const CriticalSection&& other) = delete; - - void lock(); - void unlock(); - - private: - CRITICAL_SECTION _section{}; -}; - -class WinPREvent -{ - public: - explicit WinPREvent(bool initial = false); - ~WinPREvent(); - - void set(); - void clear(); - [[nodiscard]] bool isSet() const; - - [[nodiscard]] HANDLE handle() const; - - private: - HANDLE _handle; -}; +#include enum { diff --git a/client/SDL/SDL3/sdl_utils.cpp b/client/SDL/SDL3/sdl_utils.cpp index 62a8d99b0..49945b4f7 100644 --- a/client/SDL/SDL3/sdl_utils.cpp +++ b/client/SDL/SDL3/sdl_utils.cpp @@ -280,55 +280,6 @@ BOOL sdl_push_user_event(Uint32 type, ...) return SDL_PushEvent(&ev) == 1; } -CriticalSection::CriticalSection() -{ - InitializeCriticalSection(&_section); -} - -CriticalSection::~CriticalSection() -{ - DeleteCriticalSection(&_section); -} - -void CriticalSection::lock() -{ - EnterCriticalSection(&_section); -} - -void CriticalSection::unlock() -{ - LeaveCriticalSection(&_section); -} - -WinPREvent::WinPREvent(bool initial) : _handle(CreateEventA(nullptr, TRUE, initial, nullptr)) -{ -} - -WinPREvent::~WinPREvent() -{ - (void)CloseHandle(_handle); -} - -void WinPREvent::set() -{ - (void)SetEvent(_handle); -} - -void WinPREvent::clear() -{ - (void)ResetEvent(_handle); -} - -bool WinPREvent::isSet() const -{ - return WaitForSingleObject(_handle, 0) == WAIT_OBJECT_0; -} - -HANDLE WinPREvent::handle() const -{ - return _handle; -} - bool sdl_push_quit() { SDL_Event ev = {}; diff --git a/client/SDL/SDL3/sdl_utils.hpp b/client/SDL/SDL3/sdl_utils.hpp index 54d1ed411..5984856d9 100644 --- a/client/SDL/SDL3/sdl_utils.hpp +++ b/client/SDL/SDL3/sdl_utils.hpp @@ -28,42 +28,10 @@ #include #include +#include + template using deleted_unique_ptr = std::unique_ptr>; -class CriticalSection -{ - public: - CriticalSection(); - CriticalSection(const CriticalSection& other) = delete; - CriticalSection(CriticalSection&& other) = delete; - ~CriticalSection(); - - CriticalSection& operator=(const CriticalSection& other) = delete; - CriticalSection& operator=(CriticalSection&& other) = delete; - - void lock(); - void unlock(); - - private: - CRITICAL_SECTION _section{}; -}; - -class WinPREvent -{ - public: - explicit WinPREvent(bool initial = false); - ~WinPREvent(); - - void set(); - void clear(); - [[nodiscard]] bool isSet() const; - - [[nodiscard]] HANDLE handle() const; - - private: - HANDLE _handle; -}; - enum { SDL_EVENT_USER_UPDATE = SDL_EVENT_USER + 1, diff --git a/client/SDL/common/CMakeLists.txt b/client/SDL/common/CMakeLists.txt index fb3e3ed89..b750db711 100644 --- a/client/SDL/common/CMakeLists.txt +++ b/client/SDL/common/CMakeLists.txt @@ -19,7 +19,9 @@ add_subdirectory(aad) add_subdirectory(res) -add_library(sdl-common-prefs STATIC sdl_prefs.hpp sdl_prefs.cpp scoped_guard.hpp) +add_library( + sdl-common-prefs STATIC sdl_prefs.hpp sdl_prefs.cpp scoped_guard.hpp sdl_common_utils.hpp sdl_common_utils.cpp +) target_link_libraries(sdl-common-prefs winpr freerdp) if(BUILD_TESTING_INTERNAL OR BUILD_TESTING) diff --git a/client/SDL/common/sdl_common_utils.cpp b/client/SDL/common/sdl_common_utils.cpp new file mode 100644 index 000000000..798c603f3 --- /dev/null +++ b/client/SDL/common/sdl_common_utils.cpp @@ -0,0 +1,103 @@ +/** + * FreeRDP: A Remote Desktop Protocol Implementation + * SDL common utilitis + * + * Copyright 2025 Armin Novak + * Copyright 2025 Thincast Technologies GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "sdl_common_utils.hpp" + +CriticalSection::CriticalSection() +{ + InitializeCriticalSection(&_section); +} + +CriticalSection::~CriticalSection() +{ + DeleteCriticalSection(&_section); +} + +void CriticalSection::lock() +{ + EnterCriticalSection(&_section); +} + +void CriticalSection::unlock() +{ + LeaveCriticalSection(&_section); +} + +WinPREvent::WinPREvent(bool initial) : _handle(CreateEventA(nullptr, TRUE, initial, nullptr)) +{ +} + +WinPREvent::~WinPREvent() +{ + (void)CloseHandle(_handle); +} + +void WinPREvent::set() +{ + (void)SetEvent(_handle); +} + +void WinPREvent::clear() +{ + (void)ResetEvent(_handle); +} + +bool WinPREvent::isSet() const +{ + return WaitForSingleObject(_handle, 0) == WAIT_OBJECT_0; +} + +HANDLE WinPREvent::handle() const +{ + return _handle; +} + +bool operator==(const rdpMonitor& l, const rdpMonitor& r) +{ + if (l.x != r.x) + return false; + if (l.y != r.y) + return false; + if (l.width != r.width) + return false; + if (l.height != r.height) + return false; + if (l.is_primary != r.is_primary) + return false; + if (l.orig_screen != r.orig_screen) + return false; + + return l.attributes == r.attributes; +} + +bool operator==(const MONITOR_ATTRIBUTES& l, const MONITOR_ATTRIBUTES& r) +{ + if (l.physicalWidth != r.physicalWidth) + return false; + if (l.physicalHeight != r.physicalHeight) + return false; + if (l.orientation != r.orientation) + return false; + if (l.desktopScaleFactor != r.desktopScaleFactor) + return false; + if (l.deviceScaleFactor != r.deviceScaleFactor) + return false; + return true; +} diff --git a/client/SDL/common/sdl_common_utils.hpp b/client/SDL/common/sdl_common_utils.hpp new file mode 100644 index 000000000..4b9d3810c --- /dev/null +++ b/client/SDL/common/sdl_common_utils.hpp @@ -0,0 +1,65 @@ +/** + * FreeRDP: A Remote Desktop Protocol Implementation + * SDL common utilitis + * + * Copyright 2025 Armin Novak + * Copyright 2025 Thincast Technologies GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include + +bool operator==(const MONITOR_ATTRIBUTES& l, const MONITOR_ATTRIBUTES& r); +bool operator==(const rdpMonitor& l, const rdpMonitor& r); + +class WinPREvent +{ + public: + explicit WinPREvent(bool initial = false); + WinPREvent(const WinPREvent& other) = delete; + WinPREvent(WinPREvent&& other) = delete; + + WinPREvent& operator=(const WinPREvent& other) = delete; + WinPREvent& operator=(WinPREvent&& other) = delete; + + ~WinPREvent(); + + void set(); + void clear(); + [[nodiscard]] bool isSet() const; + + [[nodiscard]] HANDLE handle() const; + + private: + HANDLE _handle; +}; + +class CriticalSection +{ + public: + CriticalSection(); + CriticalSection(const CriticalSection& other) = delete; + CriticalSection(CriticalSection&& other) = delete; + ~CriticalSection(); + + CriticalSection& operator=(const CriticalSection& other) = delete; + CriticalSection& operator=(const CriticalSection&& other) = delete; + + void lock(); + void unlock(); + + private: + CRITICAL_SECTION _section{}; +};