From 7898e9b95dac9c354d17aa67b584ade844fb16fa Mon Sep 17 00:00:00 2001 From: Adam Honse Date: Thu, 6 Aug 2020 08:43:42 -0500 Subject: [PATCH] Initial prototype of asynchronous detection. DMI information broken --- ResourceManager.cpp | 60 +++++++++++++++++++++++++++++++++++++++- ResourceManager.h | 15 ++++++++++ dependencies/dmiinfo.cpp | 2 +- qt/OpenRGBDialog2.cpp | 10 +++++++ wmi/wmi.cpp | 4 +-- 5 files changed, 87 insertions(+), 4 deletions(-) diff --git a/ResourceManager.cpp b/ResourceManager.cpp index 6e2c8340..2c0e9e34 100644 --- a/ResourceManager.cpp +++ b/ResourceManager.cpp @@ -40,6 +40,7 @@ std::vector & ResourceManager::GetI2CBusses() void ResourceManager::RegisterRGBController(RGBController *rgb_controller) { rgb_controllers.push_back(rgb_controller); + DeviceListChanged(); } std::vector & ResourceManager::GetRGBControllers() @@ -57,15 +58,72 @@ void ResourceManager::RegisterDeviceDetector(DeviceDetectorFunction detector) device_detectors.push_back(detector); } +void ResourceManager::RegisterDeviceListChangeCallback(ResourceManagerCallback new_callback, void * new_callback_arg) +{ + DeviceListChangeCallbacks.push_back(new_callback); + DeviceListChangeCallbackArgs.push_back(new_callback_arg); +} + +void ResourceManager::DeviceListChanged() +{ + DeviceListChangeMutex.lock(); + + /*-------------------------------------------------*\ + | Client info has changed, call the callbacks | + \*-------------------------------------------------*/ + for(unsigned int callback_idx = 0; callback_idx < DeviceListChangeCallbacks.size(); callback_idx++) + { + DeviceListChangeCallbacks[callback_idx](DeviceListChangeCallbackArgs[callback_idx]); + } + + DeviceListChangeMutex.unlock(); +} + void ResourceManager::DetectDevices() { + /*-------------------------------------------------*\ + | Start the device detection thread | + \*-------------------------------------------------*/ + DetectDevicesThread = new std::thread(&ResourceManager::DetectDevicesThreadFunction, this); +} + +void ResourceManager::DetectDevicesThreadFunction() +{ + unsigned int prev_count = 0; + + /*-------------------------------------------------*\ + | Detect i2c devices | + \*-------------------------------------------------*/ for(int i2c_detector_idx = 0; i2c_detector_idx < i2c_device_detectors.size(); i2c_detector_idx++) { i2c_device_detectors[i2c_detector_idx](busses, rgb_controllers); + + /*-------------------------------------------------*\ + | If the device list size has changed, call the | + | device list changed callbacks | + \*-------------------------------------------------*/ + if(rgb_controllers.size() != prev_count) + { + DeviceListChanged(); + } + prev_count = rgb_controllers.size(); } + /*-------------------------------------------------*\ + | Detect other devices | + \*-------------------------------------------------*/ for(int detector_idx = 0; detector_idx < device_detectors.size(); detector_idx++) { device_detectors[detector_idx](rgb_controllers); + + /*-------------------------------------------------*\ + | If the device list size has changed, call the | + | device list changed callbacks | + \*-------------------------------------------------*/ + if(rgb_controllers.size() != prev_count) + { + DeviceListChanged(); + } + prev_count = rgb_controllers.size(); } -} \ No newline at end of file +} diff --git a/ResourceManager.h b/ResourceManager.h index 20a3fa60..c9277362 100644 --- a/ResourceManager.h +++ b/ResourceManager.h @@ -3,6 +3,7 @@ #include #include #include +#include #include "i2c_smbus.h" #include "RGBController.h" @@ -10,6 +11,8 @@ typedef std::function&)> DeviceDetectorFunction; typedef std::function&, std::vector&)> I2CDeviceDetectorFunction; +typedef void (*ResourceManagerCallback)(void *); + class ResourceManager { public: @@ -27,8 +30,14 @@ public: void RegisterDeviceDetector (DeviceDetectorFunction detector); void RegisterI2CDeviceDetector (I2CDeviceDetectorFunction detector); + void RegisterDeviceListChangeCallback(ResourceManagerCallback new_callback, void * new_callback_arg); + + void DeviceListChanged(); + void DetectDevices(); + void DetectDevicesThreadFunction(); + private: static std::unique_ptr instance; @@ -36,4 +45,10 @@ private: std::vector rgb_controllers; std::vector device_detectors; std::vector i2c_device_detectors; + + std::thread * DetectDevicesThread; + + std::mutex DeviceListChangeMutex; + std::vector DeviceListChangeCallbacks; + std::vector DeviceListChangeCallbackArgs; }; diff --git a/dependencies/dmiinfo.cpp b/dependencies/dmiinfo.cpp index 8e210d43..ee65b567 100644 --- a/dependencies/dmiinfo.cpp +++ b/dependencies/dmiinfo.cpp @@ -32,7 +32,7 @@ DMIInfo::DMIInfo() // Query WMI for Win32_PnPSignedDriver entries with names matching "SMBUS" or "SM BUS" // These devices may be browsed under Device Manager -> System Devices std::vector q_res_BaseBoard; - hres = wmi.query("SELECT * FROM Win32_BaseBoard", q_res_BaseBoard); + //hres = wmi.query("SELECT * FROM Win32_BaseBoard", q_res_BaseBoard); if (hres) { diff --git a/qt/OpenRGBDialog2.cpp b/qt/OpenRGBDialog2.cpp index ced45308..683e860b 100644 --- a/qt/OpenRGBDialog2.cpp +++ b/qt/OpenRGBDialog2.cpp @@ -3,6 +3,7 @@ #include "OpenRGBDeviceInfoPage.h" #include "OpenRGBServerInfoPage.h" #include "OpenRGBProfileSaveDialog.h" +#include "ResourceManager.h" #include #include #include @@ -53,6 +54,13 @@ static QString GetIconString(device_type type) } } +static void UpdateInfoCallback(void * this_ptr) +{ + OpenRGBDialog2 * this_obj = (OpenRGBDialog2 *)this_ptr; + + QMetaObject::invokeMethod(this_obj, "on_ClientListUpdated", Qt::QueuedConnection); +} + OpenRGBDialog2::OpenRGBDialog2(std::vector& bus, std::vector& control, ProfileManager* manager, QWidget *parent) : QMainWindow(parent), busses(bus), controllers(control), profile_manager(manager), ui(new OpenRGBDialog2Ui) { ui->setupUi(this); @@ -70,6 +78,8 @@ OpenRGBDialog2::OpenRGBDialog2(std::vector& bus, std::vec SMBusToolsPage = NULL; SoftInfoPage = NULL; + ResourceManager::get()->RegisterDeviceListChangeCallback(UpdateInfoCallback, this); + /*-----------------------------------------------------*\ | Set up tray icon menu | \*-----------------------------------------------------*/ diff --git a/wmi/wmi.cpp b/wmi/wmi.cpp index 1ff143ac..4a6a18ec 100644 --- a/wmi/wmi.cpp +++ b/wmi/wmi.cpp @@ -39,8 +39,8 @@ Wmi::Wmi() : pLoc(nullptr), pSvc(nullptr) Wmi::~Wmi() { - pSvc->Release(); - pLoc->Release(); + //pSvc->Release(); + //pLoc->Release(); CoUninitialize(); }