Skip to content

Commit 4287128

Browse files
Attempt to implement !513 using shared mutex pointer for each detected mouse/mousemat combo
1 parent a2a93da commit 4287128

3 files changed

Lines changed: 73 additions & 14 deletions

File tree

Controllers/LogitechController/LogitechControllerDetect.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,18 +406,24 @@ void DetectLogitechMouseGLS(hid_device_info* info, const std::string& name)
406406

407407
if(dev)
408408
{
409+
/*---------------------------------------------*\
410+
| Create mutex to prevent the two controllers |
411+
| from interfering with each other |
412+
\*---------------------------------------------*/
413+
std::shared_ptr<std::mutex> logitech_mutex = std::make_shared<std::mutex>();
414+
409415
/*---------------------------------------------*\
410416
| Add mouse |
411417
\*---------------------------------------------*/
412-
LogitechGLightsyncController* controller = new LogitechGLightsyncController(dev, dev, info->path, 0x01, 0x07, 0x3C);
418+
LogitechGLightsyncController* controller = new LogitechGLightsyncController(dev, dev, info->path, 0x01, 0x07, 0x3C, logitech_mutex);
413419
RGBController_LogitechGLightsync* rgb_controller = new RGBController_LogitechGLightsync(controller);
414420
rgb_controller->name = name;
415421
ResourceManager::get()->RegisterRGBController(rgb_controller);
416422

417423
/*---------------------------------------------*\
418424
| Add Powerplay mousemat |
419425
\*---------------------------------------------*/
420-
LogitechGLightsyncController* mousemat_controller = new LogitechGLightsyncController(dev, dev, info->path, 0x07, 0x0B, 0x3C);
426+
LogitechGLightsyncController* mousemat_controller = new LogitechGLightsyncController(dev, dev, info->path, 0x07, 0x0B, 0x3C, logitech_mutex);
421427
RGBController_LogitechGPowerPlay* mousemat_rgb_controller = new RGBController_LogitechGPowerPlay(mousemat_controller);
422428
mousemat_rgb_controller->name = name;
423429
ResourceManager::get()->RegisterRGBController(mousemat_rgb_controller);

Controllers/LogitechController/LogitechGLightsyncController.cpp

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ LogitechGLightsyncController::LogitechGLightsyncController(hid_device* dev_cmd_h
1919
dev_index = hid_dev_index;
2020
feature_index = hid_feature_index;
2121
fctn_ase_id = hid_fctn_ase_id;
22+
mutex = nullptr;
23+
}
24+
25+
LogitechGLightsyncController::LogitechGLightsyncController(hid_device* dev_cmd_handle, hid_device *dev_handle, const char *path, unsigned char hid_dev_index, unsigned char hid_feature_index, unsigned char hid_fctn_ase_id, std::shared_ptr<std::mutex> mutex_ptr)
26+
{
27+
dev = dev_handle;
28+
cmd_dev = dev_cmd_handle;
29+
location = path;
30+
dev_index = hid_dev_index;
31+
feature_index = hid_feature_index;
32+
fctn_ase_id = hid_fctn_ase_id;
33+
mutex = mutex_ptr;
2234
}
2335

2436
LogitechGLightsyncController::~LogitechGLightsyncController()
@@ -94,15 +106,30 @@ void LogitechGLightsyncController::UpdateMouseLED(
94106

95107
/*-----------------------------------------------------*\
96108
| Send packet |
109+
| This code has to be protected to avoid crashes when |
110+
| this is called at the same time to change a powerplay |
111+
| mat and its paired wireless mouse leds. It will |
112+
| happen when using effects engines with high framerate |
97113
\*-----------------------------------------------------*/
98-
hid_write(dev, usb_buf, 20);
99-
hid_read(dev, usb_buf, 20);
114+
if(mutex)
115+
{
116+
std::lock_guard<std::mutex> guard(*mutex);
117+
118+
hid_write(dev, usb_buf, 20);
119+
hid_read(dev, usb_buf, 20);
120+
}
121+
else
122+
{
123+
hid_write(dev, usb_buf, 20);
124+
hid_read(dev, usb_buf, 20);
125+
}
100126
}
101127

102128
void LogitechGLightsyncController::SetDirectMode(bool direct)
103129
{
104130
char cmd_buf[7];
105131
char usb_buf[20];
132+
106133
/*-----------------------------------------------------*\
107134
| Zero out buffer |
108135
\*-----------------------------------------------------*/
@@ -117,6 +144,7 @@ void LogitechGLightsyncController::SetDirectMode(bool direct)
117144
cmd_buf[0x03] = 0x8A;
118145
cmd_buf[0x04] = 0x00;
119146
cmd_buf[0x05] = 0x00;
147+
120148
/*-----------------------------------------------------*\
121149
| If direct, disable save to flash |
122150
\*-----------------------------------------------------*/
@@ -125,9 +153,24 @@ void LogitechGLightsyncController::SetDirectMode(bool direct)
125153
cmd_buf[0x04] = 0x01;
126154
cmd_buf[0x05] = 0x01;
127155
}
156+
128157
/*-----------------------------------------------------*\
129158
| Send packet |
159+
| This code has to be protected to avoid crashes when |
160+
| this is called at the same time to change a powerplay |
161+
| mat and its paired wireless mouse leds. It will |
162+
| happen when using effects engines with high framerate |
130163
\*-----------------------------------------------------*/
131-
hid_write(cmd_dev, (unsigned char *)cmd_buf, 7);
132-
hid_read(dev, (unsigned char *)usb_buf, 20);
133-
}
164+
if(mutex)
165+
{
166+
std::lock_guard<std::mutex> guard(*mutex);
167+
168+
hid_write(cmd_dev, (unsigned char *)cmd_buf, 7);
169+
hid_read(dev, (unsigned char *)usb_buf, 20);
170+
}
171+
else
172+
{
173+
hid_write(cmd_dev, (unsigned char *)cmd_buf, 7);
174+
hid_read(dev, (unsigned char *)usb_buf, 20);
175+
}
176+
}

Controllers/LogitechController/LogitechGLightsyncController.h

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ class LogitechGLightsyncController
4343
unsigned char hid_dev_index,
4444
unsigned char hid_feature_index,
4545
unsigned char hid_fctn_ase_id);
46+
LogitechGLightsyncController(
47+
hid_device* dev_cmd_handle,
48+
hid_device* dev_handle,
49+
const char* path,
50+
unsigned char hid_dev_index,
51+
unsigned char hid_feature_index,
52+
unsigned char hid_fctn_ase_id,
53+
std::shared_ptr<std::mutex> mutex_ptr);
54+
4655
~LogitechGLightsyncController();
4756

4857
std::string GetDeviceLocation();
@@ -61,11 +70,12 @@ class LogitechGLightsyncController
6170
void SetDirectMode(bool direct);
6271

6372
private:
64-
hid_device* dev;
65-
hid_device* cmd_dev;
66-
std::string location;
67-
unsigned char dev_index;
68-
unsigned char feature_index;
69-
unsigned char fctn_ase_id;
70-
bool direct_state;
73+
hid_device* dev;
74+
hid_device* cmd_dev;
75+
std::string location;
76+
unsigned char dev_index;
77+
unsigned char feature_index;
78+
unsigned char fctn_ase_id;
79+
bool direct_state;
80+
std::shared_ptr<std::mutex> mutex;
7181
};

0 commit comments

Comments
 (0)