From 4287128fb4ed9ef34c3b36e1d506d7175ca80f44 Mon Sep 17 00:00:00 2001 From: Adam Honse Date: Mon, 24 May 2021 17:30:50 -0500 Subject: [PATCH] Attempt to implement !513 using shared mutex pointer for each detected mouse/mousemat combo --- .../LogitechControllerDetect.cpp | 10 +++- .../LogitechGLightsyncController.cpp | 53 +++++++++++++++++-- .../LogitechGLightsyncController.h | 24 ++++++--- 3 files changed, 73 insertions(+), 14 deletions(-) diff --git a/Controllers/LogitechController/LogitechControllerDetect.cpp b/Controllers/LogitechController/LogitechControllerDetect.cpp index f068af3d..16e26501 100644 --- a/Controllers/LogitechController/LogitechControllerDetect.cpp +++ b/Controllers/LogitechController/LogitechControllerDetect.cpp @@ -406,10 +406,16 @@ void DetectLogitechMouseGLS(hid_device_info* info, const std::string& name) if(dev) { + /*---------------------------------------------*\ + | Create mutex to prevent the two controllers | + | from interfering with each other | + \*---------------------------------------------*/ + std::shared_ptr logitech_mutex = std::make_shared(); + /*---------------------------------------------*\ | Add mouse | \*---------------------------------------------*/ - LogitechGLightsyncController* controller = new LogitechGLightsyncController(dev, dev, info->path, 0x01, 0x07, 0x3C); + LogitechGLightsyncController* controller = new LogitechGLightsyncController(dev, dev, info->path, 0x01, 0x07, 0x3C, logitech_mutex); RGBController_LogitechGLightsync* rgb_controller = new RGBController_LogitechGLightsync(controller); rgb_controller->name = name; ResourceManager::get()->RegisterRGBController(rgb_controller); @@ -417,7 +423,7 @@ void DetectLogitechMouseGLS(hid_device_info* info, const std::string& name) /*---------------------------------------------*\ | Add Powerplay mousemat | \*---------------------------------------------*/ - LogitechGLightsyncController* mousemat_controller = new LogitechGLightsyncController(dev, dev, info->path, 0x07, 0x0B, 0x3C); + LogitechGLightsyncController* mousemat_controller = new LogitechGLightsyncController(dev, dev, info->path, 0x07, 0x0B, 0x3C, logitech_mutex); RGBController_LogitechGPowerPlay* mousemat_rgb_controller = new RGBController_LogitechGPowerPlay(mousemat_controller); mousemat_rgb_controller->name = name; ResourceManager::get()->RegisterRGBController(mousemat_rgb_controller); diff --git a/Controllers/LogitechController/LogitechGLightsyncController.cpp b/Controllers/LogitechController/LogitechGLightsyncController.cpp index f5cbbd5c..3bf47d2e 100644 --- a/Controllers/LogitechController/LogitechGLightsyncController.cpp +++ b/Controllers/LogitechController/LogitechGLightsyncController.cpp @@ -19,6 +19,18 @@ LogitechGLightsyncController::LogitechGLightsyncController(hid_device* dev_cmd_h dev_index = hid_dev_index; feature_index = hid_feature_index; fctn_ase_id = hid_fctn_ase_id; + mutex = nullptr; +} + +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 mutex_ptr) +{ + dev = dev_handle; + cmd_dev = dev_cmd_handle; + location = path; + dev_index = hid_dev_index; + feature_index = hid_feature_index; + fctn_ase_id = hid_fctn_ase_id; + mutex = mutex_ptr; } LogitechGLightsyncController::~LogitechGLightsyncController() @@ -94,15 +106,30 @@ void LogitechGLightsyncController::UpdateMouseLED( /*-----------------------------------------------------*\ | Send packet | + | This code has to be protected to avoid crashes when | + | this is called at the same time to change a powerplay | + | mat and its paired wireless mouse leds. It will | + | happen when using effects engines with high framerate | \*-----------------------------------------------------*/ - hid_write(dev, usb_buf, 20); - hid_read(dev, usb_buf, 20); + if(mutex) + { + std::lock_guard guard(*mutex); + + hid_write(dev, usb_buf, 20); + hid_read(dev, usb_buf, 20); + } + else + { + hid_write(dev, usb_buf, 20); + hid_read(dev, usb_buf, 20); + } } void LogitechGLightsyncController::SetDirectMode(bool direct) { char cmd_buf[7]; char usb_buf[20]; + /*-----------------------------------------------------*\ | Zero out buffer | \*-----------------------------------------------------*/ @@ -117,6 +144,7 @@ void LogitechGLightsyncController::SetDirectMode(bool direct) cmd_buf[0x03] = 0x8A; cmd_buf[0x04] = 0x00; cmd_buf[0x05] = 0x00; + /*-----------------------------------------------------*\ | If direct, disable save to flash | \*-----------------------------------------------------*/ @@ -125,9 +153,24 @@ void LogitechGLightsyncController::SetDirectMode(bool direct) cmd_buf[0x04] = 0x01; cmd_buf[0x05] = 0x01; } + /*-----------------------------------------------------*\ | Send packet | + | This code has to be protected to avoid crashes when | + | this is called at the same time to change a powerplay | + | mat and its paired wireless mouse leds. It will | + | happen when using effects engines with high framerate | \*-----------------------------------------------------*/ - hid_write(cmd_dev, (unsigned char *)cmd_buf, 7); - hid_read(dev, (unsigned char *)usb_buf, 20); -} \ No newline at end of file + if(mutex) + { + std::lock_guard guard(*mutex); + + hid_write(cmd_dev, (unsigned char *)cmd_buf, 7); + hid_read(dev, (unsigned char *)usb_buf, 20); + } + else + { + hid_write(cmd_dev, (unsigned char *)cmd_buf, 7); + hid_read(dev, (unsigned char *)usb_buf, 20); + } +} diff --git a/Controllers/LogitechController/LogitechGLightsyncController.h b/Controllers/LogitechController/LogitechGLightsyncController.h index 813791fd..f041550e 100644 --- a/Controllers/LogitechController/LogitechGLightsyncController.h +++ b/Controllers/LogitechController/LogitechGLightsyncController.h @@ -43,6 +43,15 @@ public: unsigned char hid_dev_index, unsigned char hid_feature_index, unsigned char hid_fctn_ase_id); + 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 mutex_ptr); + ~LogitechGLightsyncController(); std::string GetDeviceLocation(); @@ -61,11 +70,12 @@ public: void SetDirectMode(bool direct); private: - hid_device* dev; - hid_device* cmd_dev; - std::string location; - unsigned char dev_index; - unsigned char feature_index; - unsigned char fctn_ase_id; - bool direct_state; + hid_device* dev; + hid_device* cmd_dev; + std::string location; + unsigned char dev_index; + unsigned char feature_index; + unsigned char fctn_ase_id; + bool direct_state; + std::shared_ptr mutex; };