diff --git a/ResourceManager.cpp b/ResourceManager.cpp index d532309f..69a68821 100644 --- a/ResourceManager.cpp +++ b/ResourceManager.cpp @@ -3,6 +3,7 @@ std::unique_ptr ResourceManager::instance; +using namespace std::chrono_literals; ResourceManager *ResourceManager::get() { @@ -22,15 +23,17 @@ ResourceManager::ResourceManager() ResourceManager::~ResourceManager() { - for(i2c_smbus_interface* bus : busses) - { - delete bus; - } + ResourceManager::get()->WaitForDeviceDetection(); for(RGBController* rgb_controller : rgb_controllers) { delete rgb_controller; } + + for(i2c_smbus_interface* bus : busses) + { + //delete bus; + } } void ResourceManager::RegisterI2CBus(i2c_smbus_interface *bus) @@ -108,6 +111,12 @@ void ResourceManager::DetectDevices() | Start the device detection thread | \*-------------------------------------------------*/ DetectDevicesThread = new std::thread(&ResourceManager::DetectDevicesThreadFunction, this); + + /*-------------------------------------------------*\ + | Release the current thread to allow detection | + | thread to start | + \*-------------------------------------------------*/ + std::this_thread::sleep_for(1ms); } void ResourceManager::DetectDevicesThreadFunction() diff --git a/cli.cpp b/cli.cpp index 872ffc8a..78d6b755 100644 --- a/cli.cpp +++ b/cli.cpp @@ -5,6 +5,7 @@ #include #include "OpenRGB.h" #include "ProfileManager.h" +#include "ResourceManager.h" #include "RGBController.h" #include "i2c_smbus.h" #include "NetworkClient.h" @@ -398,6 +399,8 @@ void OptionVersion() void OptionListDevices(std::vector &rgb_controllers) { + ResourceManager::get()->WaitForDeviceDetection(); + for(std::size_t controller_idx = 0; controller_idx < rgb_controllers.size(); controller_idx++) { RGBController *controller = rgb_controllers[controller_idx]; @@ -499,6 +502,8 @@ void OptionListDevices(std::vector &rgb_controllers) bool OptionDevice(int *current_device, std::string argument, Options *options, std::vector &rgb_controllers) { + ResourceManager::get()->WaitForDeviceDetection(); + try { *current_device = std::stoi(argument); @@ -529,6 +534,8 @@ bool OptionDevice(int *current_device, std::string argument, Options *options, s bool OptionZone(int *current_device, int *current_zone, std::string argument, Options *options, std::vector &rgb_controllers) { + ResourceManager::get()->WaitForDeviceDetection(); + try { *current_zone = std::stoi(argument); @@ -584,6 +591,8 @@ bool OptionSize(int *current_device, int *current_zone, std::string argument, Op { const unsigned int new_size = std::stoi(argument); + ResourceManager::get()->WaitForDeviceDetection(); + /*---------------------------------------------------------*\ | Fail out if device, zone, or size are out of range | \*---------------------------------------------------------*/ @@ -617,6 +626,8 @@ bool OptionSize(int *current_device, int *current_zone, std::string argument, Op bool OptionProfile(std::string argument, std::vector &rgb_controllers) { + ResourceManager::get()->WaitForDeviceDetection(); + /*---------------------------------------------------------*\ | Attempt to load profile | \*---------------------------------------------------------*/ @@ -1068,6 +1079,8 @@ unsigned int cli_main(int argc, char *argv[], std::vector &rgb_ break; } + ResourceManager::get()->WaitForDeviceDetection(); + /*---------------------------------------------------------*\ | If the options has one or more specific devices, loop | | through all of the specific devices and apply settings. |