From 24de16f77b4c811652369707a0ec97fb207e8443 Mon Sep 17 00:00:00 2001 From: Adam Honse Date: Tue, 6 Oct 2020 21:20:08 -0500 Subject: [PATCH] Keep a separate list of controllers detected from hardware and dynamically add/remove them from the master list when redetecting so client controllers aren't deleted during redetection --- ResourceManager.cpp | 69 ++++++++++++++++++++++++++++++++++++++------- ResourceManager.h | 2 +- 2 files changed, 60 insertions(+), 11 deletions(-) diff --git a/ResourceManager.cpp b/ResourceManager.cpp index 04c455e9..3a4b54ed 100644 --- a/ResourceManager.cpp +++ b/ResourceManager.cpp @@ -43,7 +43,7 @@ ResourceManager::ResourceManager() /*-------------------------------------------------------------------------*\ | Initialize Server Instance | \*-------------------------------------------------------------------------*/ - server = new NetworkServer(rgb_controllers); + server = new NetworkServer(rgb_controllers_hw); } ResourceManager::~ResourceManager() @@ -105,6 +105,43 @@ void ResourceManager::DeviceListChanged() { DeviceListChangeMutex.lock(); + /*-------------------------------------------------*\ + | Insert hardware controllers into controller list | + \*-------------------------------------------------*/ + for(unsigned int hw_controller_idx = 0; hw_controller_idx < rgb_controllers_hw.size(); hw_controller_idx++) + { + /*-------------------------------------------------*\ + | Check if the controller is already in the list | + | at the correct index | + \*-------------------------------------------------*/ + if(hw_controller_idx < rgb_controllers.size()) + { + if(rgb_controllers[hw_controller_idx] == rgb_controllers_hw[hw_controller_idx]) + { + continue; + } + } + + /*-------------------------------------------------*\ + | If not, check if the controller is already in the | + | list at a different index | + \*-------------------------------------------------*/ + for(unsigned int controller_idx = 0; controller_idx < rgb_controllers.size(); controller_idx++) + { + if(rgb_controllers[controller_idx] == rgb_controllers_hw[hw_controller_idx]) + { + rgb_controllers.erase(rgb_controllers.begin() + controller_idx); + rgb_controllers.insert(rgb_controllers.begin() + hw_controller_idx, rgb_controllers_hw[hw_controller_idx]); + break; + } + } + + /*-------------------------------------------------*\ + | If it still hasn't been found, add it to the list | + \*-------------------------------------------------*/ + rgb_controllers.insert(rgb_controllers.begin() + hw_controller_idx, rgb_controllers_hw[hw_controller_idx]); + } + /*-------------------------------------------------*\ | Device list has changed, call the callbacks | \*-------------------------------------------------*/ @@ -161,11 +198,23 @@ void ResourceManager::Cleanup() { ResourceManager::get()->WaitForDeviceDetection(); - std::vector rgb_controllers_copy = rgb_controllers; + std::vector rgb_controllers_hw_copy = rgb_controllers_hw; - rgb_controllers.clear(); + for(unsigned int hw_controller_idx = 0; hw_controller_idx < rgb_controllers_hw.size(); hw_controller_idx++) + { + for(unsigned int controller_idx = 0; controller_idx < rgb_controllers.size(); controller_idx++) + { + if(rgb_controllers[controller_idx] == rgb_controllers_hw[hw_controller_idx]) + { + rgb_controllers.erase(rgb_controllers.begin() + controller_idx); + break; + } + } + } - for(RGBController* rgb_controller : rgb_controllers_copy) + rgb_controllers_hw.clear(); + + for(RGBController* rgb_controller : rgb_controllers_hw_copy) { delete rgb_controller; } @@ -283,18 +332,18 @@ void ResourceManager::DetectDevicesThreadFunction() if(!this_device_disabled) { - i2c_device_detectors[i2c_detector_idx](busses, rgb_controllers); + i2c_device_detectors[i2c_detector_idx](busses, rgb_controllers_hw); } /*-------------------------------------------------*\ | If the device list size has changed, call the | | device list changed callbacks | \*-------------------------------------------------*/ - if(rgb_controllers.size() != prev_count) + if(rgb_controllers_hw.size() != prev_count) { DeviceListChanged(); } - prev_count = rgb_controllers.size(); + prev_count = rgb_controllers_hw.size(); percent = (i2c_detector_idx + 1.0f) / (i2c_device_detectors.size() + device_detectors.size()); @@ -321,18 +370,18 @@ void ResourceManager::DetectDevicesThreadFunction() if(!this_device_disabled) { - device_detectors[detector_idx](rgb_controllers); + device_detectors[detector_idx](rgb_controllers_hw); } /*-------------------------------------------------*\ | If the device list size has changed, call the | | device list changed callbacks | \*-------------------------------------------------*/ - if(rgb_controllers.size() != prev_count) + if(rgb_controllers_hw.size() != prev_count) { DeviceListChanged(); } - prev_count = rgb_controllers.size(); + prev_count = rgb_controllers_hw.size(); percent = (detector_idx + 1.0f + i2c_device_detectors.size()) / (i2c_device_detectors.size() + device_detectors.size()); diff --git a/ResourceManager.h b/ResourceManager.h index 321e54b5..c5b33278 100644 --- a/ResourceManager.h +++ b/ResourceManager.h @@ -80,7 +80,7 @@ private: /*-------------------------------------------------------------------------------------*\ | RGBControllers | \*-------------------------------------------------------------------------------------*/ - //std::vector rgb_controllers_hw; + std::vector rgb_controllers_hw; std::vector rgb_controllers; /*-------------------------------------------------------------------------------------*\