Skip to content

Commit 949991f

Browse files
Aytaç KayadelenCalcProgrammer1
authored andcommitted
Razer Mutex on Windows
1 parent b9402d8 commit 949991f

File tree

5 files changed

+118
-9
lines changed

5 files changed

+118
-9
lines changed

Controllers/RazerController/RazerController/RGBController_Razer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ RGBController_Razer::RGBController_Razer(RazerController* controller_ptr)
4040
if(type == DEVICE_TYPE_KEYBOARD)
4141
{
4242
LOG_DEBUG("[%s] Checking Keyboard Layout", name.c_str());
43-
std::string layout = controller->GetKeyboardLayoutName();
43+
std::string layout = controller->GetKeyboardLayoutString();
4444

4545
LOG_DEBUG("[%s] returned: %s", name.c_str(), layout.c_str());
4646
description.append(", ");

Controllers/RazerController/RazerController/RazerController.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,19 @@
1313
#include "RazerController.h"
1414
#include "RazerDevices.h"
1515
#include "LogManager.h"
16+
#include "RazerDeviceGuard.h"
1617

1718
using namespace std::chrono_literals;
1819

1920
RazerController::RazerController(hid_device* dev_handle, hid_device* dev_argb_handle, const char* path, unsigned short pid, std::string dev_name)
2021
{
21-
dev = dev_handle;
22-
dev_argb = dev_argb_handle;
23-
dev_pid = pid;
24-
location = path;
25-
name = dev_name;
26-
device_index = 0;
22+
dev = dev_handle;
23+
dev_argb = dev_argb_handle;
24+
dev_pid = pid;
25+
location = path;
26+
name = dev_name;
27+
device_index = 0;
28+
guard_manager_ptr = new DeviceGuardManager(new RazerDeviceGuard());
2729

2830
/*-----------------------------------------------------------------*\
2931
| 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
168170
RazerController::~RazerController()
169171
{
170172
hid_close(dev);
173+
delete guard_manager_ptr;
171174
}
172175

173176
std::string RazerController::GetName()
@@ -1074,7 +1077,7 @@ unsigned char RazerController::GetKeyboardLayoutType()
10741077
}
10751078
}
10761079

1077-
std::string RazerController::GetKeyboardLayoutName()
1080+
std::string RazerController::GetKeyboardLayoutString()
10781081
{
10791082
unsigned char layout;
10801083
unsigned char variant;
@@ -1802,10 +1805,12 @@ int RazerController::razer_usb_send(razer_report* report)
18021805
{
18031806
report->crc = razer_calculate_crc(report);
18041807

1808+
DeviceGuardLock _ = guard_manager_ptr->AwaitExclusiveAccess();
18051809
return hid_send_feature_report(dev, (unsigned char*)report, sizeof(*report));
18061810
}
18071811

18081812
int RazerController::razer_usb_send_argb(razer_argb_report* report)
18091813
{
1814+
DeviceGuardLock _ = guard_manager_ptr->AwaitExclusiveAccess();
18101815
return hid_send_feature_report(dev_argb, (unsigned char*)report, sizeof(*report));
18111816
}

Controllers/RazerController/RazerController/RazerController.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <string>
1515
#include <hidapi/hidapi.h>
1616
#include "RGBController.h"
17+
#include "DeviceGuardManager.h"
1718

1819
/*---------------------------------------------------------*\
1920
| Struct packing macro for GCC and MSVC |
@@ -228,7 +229,7 @@ class RazerController
228229
unsigned char GetMaxBrightness();
229230

230231
unsigned char GetKeyboardLayoutType();
231-
std::string GetKeyboardLayoutName();
232+
std::string GetKeyboardLayoutString();
232233
std::string GetVariantName();
233234

234235
void SetBrightness(unsigned char brightness);
@@ -283,6 +284,11 @@ class RazerController
283284
\*---------------------------------------------------------*/
284285
unsigned char matrix_type;
285286

287+
/*---------------------------------------------------------*\
288+
| Mutex lock to sync with other softwares |
289+
\*---------------------------------------------------------*/
290+
DeviceGuardManager* guard_manager_ptr;
291+
286292
/*---------------------------------------------------------*\
287293
| Private functions based on OpenRazer |
288294
\*---------------------------------------------------------*/
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*---------------------------------------------------------*\
2+
| RazerDeviceGuard.cpp |
3+
| |
4+
| DeviceGuard for Razer devices |
5+
| |
6+
| Aytac Kayadelen 18 May 2024 |
7+
| |
8+
| This file is part of the OpenRGB project |
9+
| SPDX-License-Identifier: GPL-2.0-only |
10+
\*---------------------------------------------------------*/
11+
12+
#include "RazerDeviceGuard.h"
13+
14+
RazerDeviceGuard::RazerDeviceGuard() : DeviceGuard()
15+
{
16+
#ifdef _WIN32
17+
mutex_handle = CreateWindowsMutex();
18+
#endif
19+
}
20+
21+
void RazerDeviceGuard::Acquire()
22+
{
23+
#ifdef _WIN32
24+
while(true)
25+
{
26+
DWORD result = WaitForSingleObject(mutex_handle, INFINITE);
27+
28+
if(result == WAIT_OBJECT_0)
29+
{
30+
break;
31+
}
32+
33+
if(result == WAIT_ABANDONED)
34+
{
35+
ReleaseMutex(mutex_handle);
36+
}
37+
}
38+
#endif
39+
}
40+
41+
void RazerDeviceGuard::Release()
42+
{
43+
#ifdef _WIN32
44+
ReleaseMutex(mutex_handle);
45+
#endif
46+
}
47+
48+
#ifdef _WIN32
49+
50+
HANDLE RazerDeviceGuard::CreateWindowsMutex()
51+
{
52+
SECURITY_DESCRIPTOR sd;
53+
InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
54+
SetSecurityDescriptorDacl(&sd, TRUE, NULL, FALSE);
55+
56+
SECURITY_ATTRIBUTES sa;
57+
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
58+
sa.lpSecurityDescriptor = &sd;
59+
sa.bInheritHandle = FALSE;
60+
61+
return CreateMutex(&sa, FALSE, "Global\\RazerLinkReadWriteGuardMutex");
62+
}
63+
64+
#endif
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*---------------------------------------------------------*\
2+
| RazerDeviceGuard.h |
3+
| |
4+
| DeviceGuard for Razer devices |
5+
| |
6+
| Aytac Kayadelen 18 May 2024 |
7+
| |
8+
| This file is part of the OpenRGB project |
9+
| SPDX-License-Identifier: GPL-2.0-only |
10+
\*---------------------------------------------------------*/
11+
12+
#pragma once
13+
14+
#include "DeviceGuard.h"
15+
16+
#ifdef _WIN32
17+
#include <Windows.h>
18+
#endif
19+
20+
class RazerDeviceGuard : public DeviceGuard
21+
{
22+
public:
23+
RazerDeviceGuard();
24+
25+
void Acquire() override;
26+
void Release() override;
27+
28+
private:
29+
#ifdef _WIN32
30+
HANDLE mutex_handle;
31+
32+
HANDLE CreateWindowsMutex();
33+
#endif
34+
};

0 commit comments

Comments
 (0)