Razer Mutex on Windows

This commit is contained in:
Aytaç Kayadelen 2024-07-07 22:10:03 +00:00 committed by Adam Honse
parent b9402d8e94
commit 949991f608
5 changed files with 118 additions and 9 deletions

View file

@ -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(", ");

View file

@ -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));
}

View file

@ -14,6 +14,7 @@
#include <string>
#include <hidapi/hidapi.h>
#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 |
\*---------------------------------------------------------*/

View file

@ -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

View file

@ -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 <Windows.h>
#endif
class RazerDeviceGuard : public DeviceGuard
{
public:
RazerDeviceGuard();
void Acquire() override;
void Release() override;
private:
#ifdef _WIN32
HANDLE mutex_handle;
HANDLE CreateWindowsMutex();
#endif
};