diff --git a/Controllers/RazerController/RazerController/RGBController_Razer.cpp b/Controllers/RazerController/RazerController/RGBController_Razer.cpp index 67fae605..8fabce59 100644 --- a/Controllers/RazerController/RazerController/RGBController_Razer.cpp +++ b/Controllers/RazerController/RazerController/RGBController_Razer.cpp @@ -40,7 +40,7 @@ RGBController_Razer::RGBController_Razer(RazerController* controller_ptr) if(type == DEVICE_TYPE_KEYBOARD) { LOG_DEBUG("[%s] Checking Keyboard Layout", name.c_str()); - std::string layout = controller->GetKeyboardLayoutName(); + std::string layout = controller->GetKeyboardLayoutString(); LOG_DEBUG("[%s] returned: %s", name.c_str(), layout.c_str()); description.append(", "); diff --git a/Controllers/RazerController/RazerController/RazerController.cpp b/Controllers/RazerController/RazerController/RazerController.cpp index 101babc7..c1deea37 100644 --- a/Controllers/RazerController/RazerController/RazerController.cpp +++ b/Controllers/RazerController/RazerController/RazerController.cpp @@ -13,17 +13,19 @@ #include "RazerController.h" #include "RazerDevices.h" #include "LogManager.h" +#include "RazerDeviceGuard.h" using namespace std::chrono_literals; RazerController::RazerController(hid_device* dev_handle, hid_device* dev_argb_handle, const char* path, unsigned short pid, std::string dev_name) { - dev = dev_handle; - dev_argb = dev_argb_handle; - dev_pid = pid; - location = path; - name = dev_name; - device_index = 0; + dev = dev_handle; + dev_argb = dev_argb_handle; + dev_pid = pid; + location = path; + name = dev_name; + device_index = 0; + guard_manager_ptr = new DeviceGuardManager(new RazerDeviceGuard()); /*-----------------------------------------------------------------*\ | Loop through all known devices to look for a name match | @@ -168,6 +170,7 @@ RazerController::RazerController(hid_device* dev_handle, hid_device* dev_argb_ha RazerController::~RazerController() { hid_close(dev); + delete guard_manager_ptr; } std::string RazerController::GetName() @@ -1074,7 +1077,7 @@ unsigned char RazerController::GetKeyboardLayoutType() } } -std::string RazerController::GetKeyboardLayoutName() +std::string RazerController::GetKeyboardLayoutString() { unsigned char layout; unsigned char variant; @@ -1802,10 +1805,12 @@ int RazerController::razer_usb_send(razer_report* report) { report->crc = razer_calculate_crc(report); + DeviceGuardLock _ = guard_manager_ptr->AwaitExclusiveAccess(); return hid_send_feature_report(dev, (unsigned char*)report, sizeof(*report)); } int RazerController::razer_usb_send_argb(razer_argb_report* report) { + DeviceGuardLock _ = guard_manager_ptr->AwaitExclusiveAccess(); return hid_send_feature_report(dev_argb, (unsigned char*)report, sizeof(*report)); } diff --git a/Controllers/RazerController/RazerController/RazerController.h b/Controllers/RazerController/RazerController/RazerController.h index 04f122cd..e449a0c4 100644 --- a/Controllers/RazerController/RazerController/RazerController.h +++ b/Controllers/RazerController/RazerController/RazerController.h @@ -14,6 +14,7 @@ #include #include #include "RGBController.h" +#include "DeviceGuardManager.h" /*---------------------------------------------------------*\ | Struct packing macro for GCC and MSVC | @@ -228,7 +229,7 @@ public: unsigned char GetMaxBrightness(); unsigned char GetKeyboardLayoutType(); - std::string GetKeyboardLayoutName(); + std::string GetKeyboardLayoutString(); std::string GetVariantName(); void SetBrightness(unsigned char brightness); @@ -283,6 +284,11 @@ private: \*---------------------------------------------------------*/ unsigned char matrix_type; + /*---------------------------------------------------------*\ + | Mutex lock to sync with other softwares | + \*---------------------------------------------------------*/ + DeviceGuardManager* guard_manager_ptr; + /*---------------------------------------------------------*\ | Private functions based on OpenRazer | \*---------------------------------------------------------*/ diff --git a/Controllers/RazerController/RazerDeviceGuard.cpp b/Controllers/RazerController/RazerDeviceGuard.cpp new file mode 100644 index 00000000..8d0b28c8 --- /dev/null +++ b/Controllers/RazerController/RazerDeviceGuard.cpp @@ -0,0 +1,64 @@ +/*---------------------------------------------------------*\ +| RazerDeviceGuard.cpp | +| | +| DeviceGuard for Razer devices | +| | +| Aytac Kayadelen 18 May 2024 | +| | +| This file is part of the OpenRGB project | +| SPDX-License-Identifier: GPL-2.0-only | +\*---------------------------------------------------------*/ + +#include "RazerDeviceGuard.h" + +RazerDeviceGuard::RazerDeviceGuard() : DeviceGuard() +{ +#ifdef _WIN32 + mutex_handle = CreateWindowsMutex(); +#endif +} + +void RazerDeviceGuard::Acquire() +{ +#ifdef _WIN32 + while(true) + { + DWORD result = WaitForSingleObject(mutex_handle, INFINITE); + + if(result == WAIT_OBJECT_0) + { + break; + } + + if(result == WAIT_ABANDONED) + { + ReleaseMutex(mutex_handle); + } + } +#endif +} + +void RazerDeviceGuard::Release() +{ +#ifdef _WIN32 + ReleaseMutex(mutex_handle); +#endif +} + +#ifdef _WIN32 + +HANDLE RazerDeviceGuard::CreateWindowsMutex() +{ + SECURITY_DESCRIPTOR sd; + InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION); + SetSecurityDescriptorDacl(&sd, TRUE, NULL, FALSE); + + SECURITY_ATTRIBUTES sa; + sa.nLength = sizeof(SECURITY_ATTRIBUTES); + sa.lpSecurityDescriptor = &sd; + sa.bInheritHandle = FALSE; + + return CreateMutex(&sa, FALSE, "Global\\RazerLinkReadWriteGuardMutex"); +} + +#endif diff --git a/Controllers/RazerController/RazerDeviceGuard.h b/Controllers/RazerController/RazerDeviceGuard.h new file mode 100644 index 00000000..967037a0 --- /dev/null +++ b/Controllers/RazerController/RazerDeviceGuard.h @@ -0,0 +1,34 @@ +/*---------------------------------------------------------*\ +| RazerDeviceGuard.h | +| | +| DeviceGuard for Razer devices | +| | +| Aytac Kayadelen 18 May 2024 | +| | +| This file is part of the OpenRGB project | +| SPDX-License-Identifier: GPL-2.0-only | +\*---------------------------------------------------------*/ + +#pragma once + +#include "DeviceGuard.h" + +#ifdef _WIN32 +#include +#endif + +class RazerDeviceGuard : public DeviceGuard +{ +public: + RazerDeviceGuard(); + + void Acquire() override; + void Release() override; + +private: +#ifdef _WIN32 + HANDLE mutex_handle; + + HANDLE CreateWindowsMutex(); +#endif +};