From 276c7e66e9fa17d48a1bc7cae5735a582f8ef680 Mon Sep 17 00:00:00 2001 From: Adam Honse Date: Fri, 9 Apr 2021 18:27:58 -0500 Subject: [PATCH] Update the detector settings list before running the detectors, so that the list is populated even if a detector crashes --- ResourceManager.cpp | 116 ++++++++++++++++++++++++++++---------------- ResourceManager.h | 1 + 2 files changed, 76 insertions(+), 41 deletions(-) diff --git a/ResourceManager.cpp b/ResourceManager.cpp index 484465cf..a4eab8cd 100644 --- a/ResourceManager.cpp +++ b/ResourceManager.cpp @@ -375,6 +375,11 @@ void ResourceManager::Cleanup() void ResourceManager::DetectDevices() { + /*-----------------------------------------------------*\ + | Update the detector settings | + \*-----------------------------------------------------*/ + UpdateDetectorSettings(); + if(detection_enabled) { /*-------------------------------------------------*\ @@ -437,7 +442,6 @@ void ResourceManager::DetectDevicesThreadFunction() hid_device_info* hid_devices = NULL; bool hid_safe_mode = false; unsigned int prev_count = 0; - bool save_settings = false; std::vector size_used; LOG_NOTICE("Detection started"); @@ -520,19 +524,13 @@ void ResourceManager::DetectDevicesThreadFunction() detection_string = i2c_device_detector_strings[i2c_detector_idx].c_str(); /*-------------------------------------------------*\ - | Check if this detector is enabled or needs to be | - | added to the settings list | + | Check if this detector is enabled | \*-------------------------------------------------*/ bool this_device_enabled = true; if(detector_settings.contains("detectors") && detector_settings["detectors"].contains(detection_string)) { this_device_enabled = detector_settings["detectors"][detection_string]; } - else - { - detector_settings["detectors"][detection_string] = true; - save_settings = true; - } if(this_device_enabled) { @@ -574,21 +572,6 @@ void ResourceManager::DetectDevicesThreadFunction() \*-------------------------------------------------*/ current_hid_device = hid_devices; - /*-------------------------------------------------*\ - | Loop through all HID detectors and see if any | - | need to be saved to the settings | - \*-------------------------------------------------*/ - for(unsigned int hid_detector_idx = 0; hid_detector_idx < hid_device_detectors.size(); hid_detector_idx++) - { - detection_string = hid_device_detectors[hid_detector_idx].name.c_str(); - - if(!(detector_settings.contains("detectors") && detector_settings["detectors"].contains(detection_string))) - { - detector_settings["detectors"][detection_string] = true; - save_settings = true; - } - } - if(hid_safe_mode) { LOG_NOTICE("Detecting HID devices in safe mode"); @@ -765,26 +748,20 @@ void ResourceManager::DetectDevicesThreadFunction() detection_string = device_detector_strings[detector_idx].c_str(); /*-------------------------------------------------*\ - | Check if this detector is enabled or needs to be | - | added to the settings list | + | Check if this detector is enabled | \*-------------------------------------------------*/ bool this_device_enabled = true; if(detector_settings.contains("detectors") && detector_settings["detectors"].contains(detection_string)) { this_device_enabled = detector_settings["detectors"][detection_string]; } - else - { - detector_settings["detectors"][detection_string] = true; - save_settings = true; - } if(this_device_enabled) - { + { DetectionProgressChanged(); device_detectors[detector_idx](rgb_controllers_hw); - } + } /*-------------------------------------------------*\ | If the device list size has changed, call the | @@ -824,15 +801,6 @@ void ResourceManager::DetectDevicesThreadFunction() DetectDeviceMutex.unlock(); - if(save_settings) - { - LOG_NOTICE("Saving detector settings"); - - settings_manager->SetSettings("Detectors", detector_settings); - - settings_manager->SaveSettings(); - } - LOG_NOTICE("Detection completed"); } @@ -844,6 +812,72 @@ void ResourceManager::StopDeviceDetection() detection_string = "Stopping"; } +void ResourceManager::UpdateDetectorSettings() +{ + json detector_settings; + bool save_settings = false; + + /*-------------------------------------------------*\ + | Open device disable list and read in disabled | + | device strings | + \*-------------------------------------------------*/ + detector_settings = settings_manager->GetSettings("Detectors"); + + /*-------------------------------------------------*\ + | Loop through all I2C detectors and see if any | + | need to be saved to the settings | + \*-------------------------------------------------*/ + for(unsigned int i2c_detector_idx = 0; i2c_detector_idx < i2c_device_detectors.size(); i2c_detector_idx++) + { + detection_string = i2c_device_detector_strings[i2c_detector_idx].c_str(); + + if(!(detector_settings.contains("detectors") && detector_settings["detectors"].contains(detection_string))) + { + detector_settings["detectors"][detection_string] = true; + save_settings = true; + } + } + + /*-------------------------------------------------*\ + | Loop through all HID detectors and see if any | + | need to be saved to the settings | + \*-------------------------------------------------*/ + for(unsigned int hid_detector_idx = 0; hid_detector_idx < hid_device_detectors.size(); hid_detector_idx++) + { + detection_string = hid_device_detectors[hid_detector_idx].name.c_str(); + + if(!(detector_settings.contains("detectors") && detector_settings["detectors"].contains(detection_string))) + { + detector_settings["detectors"][detection_string] = true; + save_settings = true; + } + } + + /*-------------------------------------------------*\ + | Loop through remaining detectors and see if any | + | need to be saved to the settings | + \*-------------------------------------------------*/ + for(unsigned int detector_idx = 0; detector_idx < device_detectors.size(); detector_idx++) + { + detection_string = device_detector_strings[detector_idx].c_str(); + + if(!(detector_settings.contains("detectors") && detector_settings["detectors"].contains(detection_string))) + { + detector_settings["detectors"][detection_string] = true; + save_settings = true; + } + } + + if(save_settings) + { + LOG_NOTICE("Saving detector settings"); + + settings_manager->SetSettings("Detectors", detector_settings); + + settings_manager->SaveSettings(); + } +} + void ResourceManager::WaitForDeviceDetection() { DetectDeviceMutex.lock(); diff --git a/ResourceManager.h b/ResourceManager.h index d6f1bed1..06ff9751 100644 --- a/ResourceManager.h +++ b/ResourceManager.h @@ -139,6 +139,7 @@ public: private: void DetectDevicesThreadFunction(); + void UpdateDetectorSettings(); /*-------------------------------------------------------------------------------------*\ | Static pointer to shared instance of ResourceManager |