From ec77c658f201cb598bd343c1a3d60beaa80cc3f7 Mon Sep 17 00:00:00 2001 From: k1-801 Date: Tue, 25 Aug 2020 03:45:56 +0400 Subject: [PATCH] Rudimentary rescanning implemented but button for it not added due to SDK conflicts. Stop detection button. Commit amended by Adam Honse --- ResourceManager.cpp | 91 ++++++++++++++++++++++++++++++++----------- ResourceManager.h | 11 ++++-- qt/OpenRGBDialog2.cpp | 30 +++++++++++++- qt/OpenRGBDialog2.h | 1 + qt/OpenRGBDialog2.ui | 7 ++++ 5 files changed, 112 insertions(+), 28 deletions(-) diff --git a/ResourceManager.cpp b/ResourceManager.cpp index 9068ebde..7a1cbe44 100644 --- a/ResourceManager.cpp +++ b/ResourceManager.cpp @@ -23,24 +23,13 @@ ResourceManager::ResourceManager() { detection_percent = 100; detection_string = ""; + detection_is_required = false; + DetectDevicesThread = nullptr; } ResourceManager::~ResourceManager() { - ResourceManager::get()->WaitForDeviceDetection(); - - for(RGBController* rgb_controller : rgb_controllers) - { - delete rgb_controller; - } - - for(i2c_smbus_interface* bus : busses) - { - delete bus; - } - - DetectDevicesThread->join(); - delete DetectDevicesThread; + Cleanup(); } void ResourceManager::RegisterI2CBus(i2c_smbus_interface *bus) @@ -104,19 +93,59 @@ void ResourceManager::DeviceListChanged() unsigned int ResourceManager::GetDetectionPercent() { - return(detection_percent); + return (detection_percent.load()); } -std::string ResourceManager::GetDetectionString() +const char *ResourceManager::GetDetectionString() { - return(detection_string); + return (detection_string); +} + +void ResourceManager::Cleanup() +{ + ResourceManager::get()->WaitForDeviceDetection(); + + for(RGBController* rgb_controller : rgb_controllers) + { + delete rgb_controller; + } + rgb_controllers.clear(); + + for(i2c_smbus_interface* bus : busses) + { + delete bus; + } + busses.clear(); + + if(DetectDevicesThread) + { + DetectDevicesThread->join(); + delete DetectDevicesThread; + DetectDevicesThread = nullptr; + } } void ResourceManager::DetectDevices() { + /*-------------------------------------------------*\ + | Do nothing is it is already detecting devices | + \*-------------------------------------------------*/ + if(detection_is_required.load()) + { + return; + } + + /*-------------------------------------------------*\ + | If there's anything left from the last time, | + | we shall remove it first | + \*-------------------------------------------------*/ + detection_percent = 0; + Cleanup(); + /*-------------------------------------------------*\ | Start the device detection thread | \*-------------------------------------------------*/ + detection_is_required = true; DetectDevicesThread = new std::thread(&ResourceManager::DetectDevicesThreadFunction, this); /*-------------------------------------------------*\ @@ -161,7 +190,7 @@ void ResourceManager::DetectDevicesThreadFunction() /*-------------------------------------------------*\ | Detect i2c busses | \*-------------------------------------------------*/ - for(unsigned int i2c_bus_detector_idx = 0; i2c_bus_detector_idx < i2c_bus_detectors.size(); i2c_bus_detector_idx++) + for(unsigned int i2c_bus_detector_idx = 0; i2c_bus_detector_idx < i2c_bus_detectors.size() && detection_is_required.load(); i2c_bus_detector_idx++) { i2c_bus_detectors[i2c_bus_detector_idx](busses); } @@ -169,9 +198,9 @@ void ResourceManager::DetectDevicesThreadFunction() /*-------------------------------------------------*\ | Detect i2c devices | \*-------------------------------------------------*/ - for(unsigned int i2c_detector_idx = 0; i2c_detector_idx < i2c_device_detectors.size(); i2c_detector_idx++) + for(unsigned int i2c_detector_idx = 0; i2c_detector_idx < i2c_device_detectors.size() && detection_is_required.load(); i2c_detector_idx++) { - detection_string = i2c_device_detector_strings[i2c_detector_idx]; + detection_string = i2c_device_detector_strings[i2c_detector_idx].c_str(); DeviceListChanged(); bool this_device_disabled = false; @@ -207,9 +236,9 @@ void ResourceManager::DetectDevicesThreadFunction() /*-------------------------------------------------*\ | Detect other devices | \*-------------------------------------------------*/ - for(unsigned int detector_idx = 0; detector_idx < device_detectors.size(); detector_idx++) + for(unsigned int detector_idx = 0; detector_idx < device_detectors.size() && detection_is_required.load(); detector_idx++) { - detection_string = device_detector_strings[detector_idx]; + detection_string = device_detector_strings[detector_idx].c_str(); DeviceListChanged(); bool this_device_disabled = false; @@ -243,10 +272,26 @@ void ResourceManager::DetectDevicesThreadFunction() } profile_manager.LoadSizeFromProfile("sizes.ors"); - + + /*-------------------------------------------------*\ + | Make sure that when the detection is done, | + | progress bar is set to 100% | + \*-------------------------------------------------*/ + + detection_is_required = false; + detection_percent = 100; + detection_string = ""; + DetectDeviceMutex.unlock(); } +void ResourceManager::StopDeviceDetection() +{ + detection_is_required = false; + detection_percent = 100; + detection_string = "Stopping"; +} + void ResourceManager::WaitForDeviceDetection() { DetectDeviceMutex.lock(); diff --git a/ResourceManager.h b/ResourceManager.h index 6ffd310c..c19de62a 100644 --- a/ResourceManager.h +++ b/ResourceManager.h @@ -36,21 +36,26 @@ public: void RegisterDeviceListChangeCallback(ResourceManagerCallback new_callback, void * new_callback_arg); unsigned int GetDetectionPercent(); - std::string GetDetectionString(); + const char* GetDetectionString(); void DeviceListChanged(); + void Cleanup(); + void DetectDevices(); void DetectDevicesThreadFunction(); + void StopDeviceDetection(); + void WaitForDeviceDetection(); private: static std::unique_ptr instance; - unsigned int detection_percent; - std::string detection_string; + std::atomic detection_is_required; + std::atomic detection_percent; + const char* detection_string; std::vector busses; std::vector rgb_controllers; diff --git a/qt/OpenRGBDialog2.cpp b/qt/OpenRGBDialog2.cpp index 7ec22a49..0825f49f 100644 --- a/qt/OpenRGBDialog2.cpp +++ b/qt/OpenRGBDialog2.cpp @@ -167,7 +167,8 @@ OpenRGBDialog2::OpenRGBDialog2(std::vector& bus, std::vec UpdateProfileList(); /*-----------------------------------------------------*\ - | Update the device list | + | Update the device list and make sure the | + | ProgressBar gets a proper value | \*-----------------------------------------------------*/ UpdateDevicesList(); } @@ -292,6 +293,11 @@ void OpenRGBDialog2::ClearDevicesList() void OpenRGBDialog2::UpdateDevicesList() { + /*-----------------------------------------------------*\ + | Clear on each update | + \*-----------------------------------------------------*/ + ClearDevicesList(); + /*-----------------------------------------------------*\ | Set up list of devices | \*-----------------------------------------------------*/ @@ -452,7 +458,6 @@ void OpenRGBDialog2::on_QuickWhite() void OpenRGBDialog2::on_ClientListUpdated() { - ClearDevicesList(); UpdateDevicesList(); ui->DetectionProgressBar->setValue(ResourceManager::get()->GetDetectionPercent()); @@ -462,6 +467,7 @@ void OpenRGBDialog2::on_ClientListUpdated() { ui->DetectionProgressBar->setVisible(false); ui->DetectionProgressLabel->setVisible(false); + ui->ButtonStopDetection->setVisible(false); ui->ButtonToggleDeviceView->setVisible(true); ui->ButtonLoadProfile->setVisible(true); @@ -618,3 +624,23 @@ void Ui::OpenRGBDialog2::on_ButtonToggleDeviceView_clicked() device_view_showing = true; } } + +void Ui::OpenRGBDialog2::on_ButtonStopDetection_clicked() +{ + /*---------------------------------------------------------*\ + | Notify the detection thread that it has to die | + \*---------------------------------------------------------*/ + ResourceManager::get()->StopDeviceDetection(); + + /*---------------------------------------------------------*\ + | Pretend we're done already by hiding the progress bar | + \*---------------------------------------------------------*/ + ui->DetectionProgressBar->setVisible(false); + ui->DetectionProgressLabel->setVisible(false); + ui->ButtonStopDetection->setVisible(false); + + ui->ButtonLoadProfile->setVisible(true); + ui->ButtonSaveProfile->setVisible(true); + ui->ButtonDeleteProfile->setVisible(true); + ui->ProfileBox->setVisible(true); +} diff --git a/qt/OpenRGBDialog2.h b/qt/OpenRGBDialog2.h index 38ecdf00..462142d4 100644 --- a/qt/OpenRGBDialog2.h +++ b/qt/OpenRGBDialog2.h @@ -94,6 +94,7 @@ private slots: void on_ButtonLoadProfile_clicked(); void on_ButtonDeleteProfile_clicked(); void on_ButtonToggleDeviceView_clicked(); + void on_ButtonStopDetection_clicked(); }; #endif // OPENRGBDIALOG2_H diff --git a/qt/OpenRGBDialog2.ui b/qt/OpenRGBDialog2.ui index decf790f..7ae9bac9 100644 --- a/qt/OpenRGBDialog2.ui +++ b/qt/OpenRGBDialog2.ui @@ -116,6 +116,13 @@ + + + + Cancel + + +