@@ -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
2436LogitechGLightsyncController::~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
102128void 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+ }
0 commit comments