From 15163eea0dbb599480b55d67ee0729fbba2ec78d Mon Sep 17 00:00:00 2001 From: k1-801 Date: Thu, 12 Nov 2020 04:25:08 +0400 Subject: [PATCH] Improved the ResourceManager device list management Commit amended for code style by Adam Honse --- NetworkClient.cpp | 96 +++++++--- NetworkClient.h | 37 ++-- ProfileManager.cpp | 2 + ResourceManager.cpp | 353 +++++++++++++++++++++++------------ ResourceManager.h | 56 ++++-- cli.cpp | 100 +++++----- main.cpp | 15 +- qt/OpenRGBClientInfoPage.cpp | 4 +- qt/OpenRGBClientInfoPage.h | 4 +- qt/OpenRGBDevicePage.cpp | 6 +- qt/OpenRGBDialog2.cpp | 162 +++++----------- qt/OpenRGBDialog2.h | 12 +- 12 files changed, 497 insertions(+), 350 deletions(-) diff --git a/NetworkClient.cpp b/NetworkClient.cpp index cd24dab80..7a7c734b3 100644 --- a/NetworkClient.cpp +++ b/NetworkClient.cpp @@ -27,7 +27,7 @@ using namespace std::chrono_literals; -NetworkClient::NetworkClient(std::vector& control) : controllers(control) +NetworkClient::NetworkClient() { strcpy(port_ip, "127.0.0.1"); port_num = OPENRGB_SDK_PORT; @@ -55,13 +55,28 @@ void NetworkClient::ClientInfoChanged() \*-------------------------------------------------*/ for(unsigned int callback_idx = 0; callback_idx < ClientInfoChangeCallbacks.size(); callback_idx++) { - ClientInfoChangeCallbacks[callback_idx](ClientInfoChangeCallbackArgs[callback_idx]); + NetClientClientInfoChangeCallbackBlock block = ClientInfoChangeCallbacks[callback_idx]; + block.callback(block.receiver); } ControllerListMutex.unlock(); ClientInfoChangeMutex.unlock(); } +void NetworkClient::DeviceListChanged(RGBController* controller, bool removed) +{ + ControllerListMutex.lock(); + /*-------------------------------------------------*\ + | Controller list has changed, call the callbacks | + \*-------------------------------------------------*/ + for(unsigned int callback_idx = 0; callback_idx < DeviceListChangeCallbacks.size(); callback_idx++) + { + NetClientDeviceListChangeCallbackBlock block = DeviceListChangeCallbacks[callback_idx]; + block.callback(block.receiver, this, controller, removed); + } + ControllerListMutex.unlock(); +} + const char * NetworkClient::GetIP() { return(port_ip); @@ -82,10 +97,48 @@ bool NetworkClient::GetOnline() return(server_connected && server_initialized); } -void NetworkClient::RegisterClientInfoChangeCallback(NetClientCallback new_callback, void * new_callback_arg) +void NetworkClient::RegisterClientInfoChangeCallback(NetClientClientInfoChangeCallback callback, void * receiver) { - ClientInfoChangeCallbacks.push_back(new_callback); - ClientInfoChangeCallbackArgs.push_back(new_callback_arg); + NetClientClientInfoChangeCallbackBlock block; + + block.callback = callback; + block.receiver = receiver; + + ClientInfoChangeCallbacks.push_back(block); +} + +void NetworkClient::UnregisterClientInfoChangedCallback(NetClientClientInfoChangeCallback callback, void* receiver) +{ + for(size_t idx = 0; idx < ClientInfoChangeCallbacks.size(); ++idx) + { + if(ClientInfoChangeCallbacks[idx].callback == callback && ClientInfoChangeCallbacks[idx].receiver == receiver) + { + ClientInfoChangeCallbacks.erase(ClientInfoChangeCallbacks.begin()); + break; + } + } +} + +void NetworkClient::RegisterDeviceListChangeCallback(NetClientDeviceListChangeCallback callback, void* receiver) +{ + NetClientDeviceListChangeCallbackBlock block; + + block.callback = callback; + block.receiver = receiver; + + DeviceListChangeCallbacks.push_back(block); +} + +void NetworkClient::UnregisterDeviceListChangeCallback(NetClientDeviceListChangeCallback callback, void* receiver) +{ + for(size_t idx = 0; idx < DeviceListChangeCallbacks.size(); ++idx) + { + if(DeviceListChangeCallbacks[idx].callback == callback && DeviceListChangeCallbacks[idx].receiver == receiver) + { + DeviceListChangeCallbacks.erase(DeviceListChangeCallbacks.begin()); + break; + } + } } void NetworkClient::SetIP(const char *new_ip) @@ -247,7 +300,7 @@ void NetworkClient::ConnectionThreadFunction() printf("Client: All controllers received, adding them to master list\r\n"); for(std::size_t controller_idx = 0; controller_idx < server_controllers.size(); controller_idx++) { - controllers.push_back(server_controllers[controller_idx]); + DeviceListChanged(server_controllers[controller_idx], false); } ControllerListMutex.unlock(); @@ -427,25 +480,17 @@ listen_done: ControllerListMutex.lock(); - for(size_t server_controller_idx = 0; server_controller_idx < server_controllers.size(); server_controller_idx++) + for(size_t controller_idx = 0; controller_idx < server_controllers.size(); controller_idx++) { - for(size_t controller_idx = 0; controller_idx < controllers.size(); controller_idx++) - { - if(controllers[controller_idx] == server_controllers[server_controller_idx]) - { - controllers.erase(controllers.begin() + controller_idx); - break; - } - } + DeviceListChanged(server_controllers[controller_idx], true); } - std::vector server_controllers_copy = server_controllers; + std::vector server_controllers_copy; + server_controllers.swap(server_controllers_copy); - server_controllers.clear(); - - for(size_t server_controller_idx = 0; server_controller_idx < server_controllers_copy.size(); server_controller_idx++) + for(size_t controller_idx = 0; controller_idx < server_controllers_copy.size(); controller_idx++) { - delete server_controllers_copy[server_controller_idx]; + delete server_controllers_copy[controller_idx]; } ControllerListMutex.unlock(); @@ -508,16 +553,9 @@ void NetworkClient::ProcessRequest_DeviceListChanged() ControllerListMutex.lock(); - for(size_t server_controller_idx = 0; server_controller_idx < server_controllers.size(); server_controller_idx++) + for(size_t idx = 0; idx < server_controllers.size(); idx++) { - for(size_t controller_idx = 0; controller_idx < controllers.size(); controller_idx++) - { - if(controllers[controller_idx] == server_controllers[server_controller_idx]) - { - controllers.erase(controllers.begin() + controller_idx); - break; - } - } + DeviceListChanged(server_controllers[idx], true); } std::vector server_controllers_copy = server_controllers; diff --git a/NetworkClient.h b/NetworkClient.h index 109675097..1bcd6059d 100644 --- a/NetworkClient.h +++ b/NetworkClient.h @@ -15,22 +15,41 @@ #pragma once -typedef void (*NetClientCallback)(void *); +class NetworkClient; + +typedef void (*NetClientClientInfoChangeCallback)(void *); +typedef void (*NetClientDeviceListChangeCallback)(void* receiver, NetworkClient* client, RGBController* controller, bool removed); + +typedef struct +{ + NetClientClientInfoChangeCallback callback; + void* receiver; +} NetClientClientInfoChangeCallbackBlock; + +typedef struct +{ + NetClientDeviceListChangeCallback callback; + void* receiver; +} NetClientDeviceListChangeCallbackBlock; class NetworkClient { public: - NetworkClient(std::vector& control); + NetworkClient(); ~NetworkClient(); void ClientInfoChanged(); + void DeviceListChanged(RGBController *controller, bool removed); bool GetConnected(); const char * GetIP(); unsigned short GetPort(); bool GetOnline(); - void RegisterClientInfoChangeCallback(NetClientCallback new_callback, void * new_callback_arg); + void RegisterClientInfoChangeCallback(NetClientClientInfoChangeCallback callback, void* receiver); + void UnregisterClientInfoChangedCallback(NetClientClientInfoChangeCallback callback, void* receiver); + void RegisterDeviceListChangeCallback(NetClientDeviceListChangeCallback callback, void* receiver); + void UnregisterDeviceListChangeCallback(NetClientDeviceListChangeCallback callback, void* receiver); void SetIP(const char *new_ip); void SetName(const char *new_name); @@ -65,11 +84,7 @@ public: std::vector server_controllers; - std::mutex ControllerListMutex; - -protected: - std::vector& controllers; - + std::mutex ControllerListMutex; private: SOCKET client_sock; @@ -88,9 +103,9 @@ private: std::thread * ConnectionThread; std::thread * ListenThread; - std::mutex ClientInfoChangeMutex; - std::vector ClientInfoChangeCallbacks; - std::vector ClientInfoChangeCallbackArgs; + std::mutex ClientInfoChangeMutex; + std::vector ClientInfoChangeCallbacks; + std::vector DeviceListChangeCallbacks; int recv_select(SOCKET s, char *buf, int len, int flags); }; diff --git a/ProfileManager.cpp b/ProfileManager.cpp index a6770b33a..a91ee7a6f 100644 --- a/ProfileManager.cpp +++ b/ProfileManager.cpp @@ -156,6 +156,8 @@ bool ProfileManager::LoadDeviceFromListWithOptions bool load_settings ) { + temp_controller_used.resize(temp_controllers.size(), false); // Failsafe + for(std::size_t temp_index = 0; temp_index < temp_controllers.size(); temp_index++) { RGBController *temp_controller = temp_controllers[temp_index]; diff --git a/ResourceManager.cpp b/ResourceManager.cpp index e4320bd7c..67978e901 100644 --- a/ResourceManager.cpp +++ b/ResourceManager.cpp @@ -27,7 +27,7 @@ ResourceManager *ResourceManager::get() { instance = std::make_unique(); } - + return instance.get(); } @@ -49,8 +49,8 @@ ResourceManager::ResourceManager() /*-------------------------------------------------------------------------*\ | Load sizes list from file | \*-------------------------------------------------------------------------*/ - profile_manager = new ProfileManager(rgb_controllers, GetConfigurationDirectory()); - rgb_controllers_sizes = profile_manager->LoadProfileToList("sizes.ors"); + profile_manager = new ProfileManager(rgb_controllers_remote, GetConfigurationDirectory()); + rgb_controllers_hw_sizes = profile_manager->LoadProfileToList("sizes.ors"); /*-------------------------------------------------------------------------*\ | Load settings from file | @@ -74,15 +74,142 @@ std::vector & ResourceManager::GetI2CBusses() return busses; } -void ResourceManager::RegisterRGBController(RGBController *rgb_controller) +void ResourceManager::RegisterRGBController(RGBController *rgb_controller, ControllerList list) { - rgb_controllers.push_back(rgb_controller); - DeviceListChanged(); + switch(list) + { + case CONTROLLER_LIST_HW: + rgb_controllers_hw.push_back(rgb_controller); + profile_manager->LoadDeviceFromListWithOptions(rgb_controllers_hw_sizes, rgb_controllers_hw_sizes_used, rgb_controller, true, false); + DeviceListChanged(rgb_controllers_hw.size() - 1, list, false); + break; + + case CONTROLLER_LIST_REMOTE: + rgb_controllers_remote.push_back(rgb_controller); + DeviceListChanged(rgb_controllers_remote.size() - 1, list, false); + break; + + default: + break; + } } -std::vector & ResourceManager::GetRGBControllers() +bool ResourceManager::RemoveRGBController(RGBController* rgb_controller, ControllerList list) { - return rgb_controllers; + switch(list) + { + case CONTROLLER_LIST_HW: + for(size_t idx = 0; idx < rgb_controllers_hw.size(); ++idx) + { + if(rgb_controllers_hw[idx] == rgb_controller) + { + RGBController * controller_to_delete = rgb_controllers_hw[idx]; + rgb_controllers_hw.erase(rgb_controllers_hw.begin() + idx); + DeviceListChanged(idx, CONTROLLER_LIST_HW, true); + delete controller_to_delete; + return true; + } + } + return false; + + case CONTROLLER_LIST_REMOTE: + for(size_t idx = 0; idx < rgb_controllers_remote.size(); ++idx) + { + if(rgb_controllers_remote[idx] == rgb_controller) + { + RGBController * controller_to_delete = rgb_controllers_remote[idx]; + rgb_controllers_remote.erase(rgb_controllers_remote.begin() + idx); + DeviceListChanged(idx, CONTROLLER_LIST_REMOTE, true); + delete controller_to_delete; + return true; + } + } + return false; + + /*-------------------------------------------------------------------------*\ + | If the list ID is invalid, try all of the lists | + \*-------------------------------------------------------------------------*/ + default: + return RemoveRGBController(rgb_controller, CONTROLLER_LIST_HW) | + RemoveRGBController(rgb_controller, CONTROLLER_LIST_REMOTE); + } +} + +unsigned int ResourceManager::GetControllerCount(ControllerList list) +{ + switch(list) + { + case CONTROLLER_LIST_UI: + return rgb_controllers_hw.size() + rgb_controllers_remote.size(); + + case CONTROLLER_LIST_HW: + return rgb_controllers_hw.size(); + + case CONTROLLER_LIST_REMOTE: + return rgb_controllers_remote.size(); + } + return 0; +} + +RGBController* ResourceManager::GetController(size_t id, ControllerList list) +{ + switch(list) + { + case CONTROLLER_LIST_UI: + /*-------------------------------------------------------------------------*\ + | If the ID is within the hardware list, return the controller | + \*-------------------------------------------------------------------------*/ + if(id < rgb_controllers_hw.size()) + { + return rgb_controllers_hw[id]; + } + + /*-------------------------------------------------------------------------*\ + | Otherwise, offset the ID and check the remote list | + \*-------------------------------------------------------------------------*/ + id -= rgb_controllers_hw.size(); + + if(id < rgb_controllers_remote.size()) + { + return rgb_controllers_remote[id]; + } + + /*-------------------------------------------------------------------------*\ + | If the ID is not in either list, return null pointer | + \*-------------------------------------------------------------------------*/ + return nullptr; + + case CONTROLLER_LIST_HW: + if(id < rgb_controllers_hw.size()) + { + return rgb_controllers_hw[id]; + } + return nullptr; + + case CONTROLLER_LIST_REMOTE: + if(id < rgb_controllers_remote.size()) + { + return rgb_controllers_remote[id]; + } + return nullptr; + } + + return nullptr; +} + +int ResourceManager::GetUIListIndex(size_t id, ControllerList list) +{ + switch(list) + { + case CONTROLLER_LIST_REMOTE: + id += rgb_controllers_hw.size(); + break; + + default: + break; + } + + return id; } void ResourceManager::RegisterI2CBusDetector(I2CBusDetectorFunction detector) @@ -102,72 +229,78 @@ void ResourceManager::RegisterDeviceDetector(std::string name, DeviceDetectorFun device_detectors.push_back(detector); } -void ResourceManager::RegisterDeviceListChangeCallback(DeviceListChangeCallback new_callback, void * new_callback_arg) +void ResourceManager::RegisterDeviceListChangeCallback(DeviceListChangeCallback callback, void * receiver) { - DeviceListChangeCallbacks.push_back(new_callback); - DeviceListChangeCallbackArgs.push_back(new_callback_arg); + DeviceListChangeCallbackBlock block; + + block.callback = callback; + block.receiver = receiver; + + DeviceListChangeCallbacks.push_back(block); } -void ResourceManager::RegisterDetectionProgressCallback(DetectionProgressCallback new_callback, void *new_callback_arg) +void ResourceManager::RegisterDetectionProgressCallback(DetectionProgressCallback callback, void * receiver) { - DetectionProgressCallbacks.push_back(new_callback); - DetectionProgressCallbackArgs.push_back(new_callback_arg); + DetectionProgressCallbackBlock block; + + block.callback = callback; + block.receiver = receiver; + + DetectionProgressCallbacks.push_back(block); } -void ResourceManager::DeviceListChanged() +void ResourceManager::UnregisterDeviceListChangeCallback(DeviceListChangeCallback callback, void * receiver) +{ + /*-------------------------------------------------------------------------*\ + | Loop through the Device List Change Callbacks list and remove the given | + | entry | + \*-------------------------------------------------------------------------*/ + for(unsigned int callback_idx = 0; callback_idx < DeviceListChangeCallbacks.size(); callback_idx++) + { + if(DeviceListChangeCallbacks[callback_idx].callback == callback && DeviceListChangeCallbacks[callback_idx].receiver == receiver) + { + DeviceListChangeCallbacks.erase(DeviceListChangeCallbacks.begin() + callback_idx); + break; + } + } +} + +void ResourceManager::UnregisterDetectionProgressCallback(DetectionProgressCallback callback, void * receiver) +{ + /*-------------------------------------------------------------------------*\ + | Loop through the Detection Progress Callbacks list and remove the given | + | entry | + \*-------------------------------------------------------------------------*/ + for(unsigned int callback_idx = 0; callback_idx < DetectionProgressCallbacks.size(); callback_idx++) + { + if(DetectionProgressCallbacks[callback_idx].callback == callback && DetectionProgressCallbacks[callback_idx].receiver == receiver) + { + DetectionProgressCallbacks.erase(DetectionProgressCallbacks.begin() + callback_idx); + break; + } + } +} + +void ResourceManager::DeviceListChanged(int id, ControllerList list, bool removed) { 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 | \*-------------------------------------------------*/ for(unsigned int callback_idx = 0; callback_idx < DeviceListChangeCallbacks.size(); callback_idx++) { - DeviceListChangeCallbacks[callback_idx](DeviceListChangeCallbackArgs[callback_idx]); + DeviceListChangeCallbacks[callback_idx].callback(DeviceListChangeCallbacks[callback_idx].receiver, id, list, removed); } /*-------------------------------------------------*\ | Device list has changed, inform all clients | | connected to this server | \*-------------------------------------------------*/ - server->DeviceListChanged(); + if(server) + { + server->DeviceListChanged(); + } DeviceListChangeMutex.unlock(); } @@ -181,7 +314,7 @@ void ResourceManager::DetectionProgressChanged() \*-------------------------------------------------*/ for(unsigned int callback_idx = 0; callback_idx < DetectionProgressCallbacks.size(); callback_idx++) { - DetectionProgressCallbacks[callback_idx](DetectionProgressCallbackArgs[callback_idx]); + DetectionProgressCallbacks[callback_idx].callback(DetectionProgressCallbacks[callback_idx].receiver); } DetectionProgressMutex.unlock(); @@ -237,6 +370,43 @@ NetworkServer* ResourceManager::GetServer() return(server); } +unsigned int ResourceManager::GetClientCount() +{ + return clients.size(); +} + +static void OnNetworkClientDeviceListChange(void*, NetworkClient*, RGBController* controller, bool removed) +{ + if(removed) + { + ResourceManager::get()->RemoveRGBController(controller, CONTROLLER_LIST_REMOTE); + } + else + { + ResourceManager::get()->RegisterRGBController(controller, CONTROLLER_LIST_REMOTE); + } +} + +void ResourceManager::RegisterClient(NetworkClient* client) +{ + client->RegisterDeviceListChangeCallback(OnNetworkClientDeviceListChange, NULL); + clients.push_back(client); + // Notify about client list change? +} + +NetworkClient* ResourceManager::GetClient(size_t id) +{ + /*-------------------------------------------------------------------------*\ + | Verify ID is valid, return null pointer if it is out of range | + \*-------------------------------------------------------------------------*/ + if(id >= clients.size()) + { + return nullptr; + } + + return clients[id]; +} + std::vector& ResourceManager::GetClients() { return(clients); @@ -266,25 +436,9 @@ void ResourceManager::Cleanup() { ResourceManager::get()->WaitForDeviceDetection(); - std::vector rgb_controllers_hw_copy = rgb_controllers_hw; - - for(unsigned int hw_controller_idx = 0; hw_controller_idx < rgb_controllers_hw.size(); hw_controller_idx++) + for(int controller_idx = GetControllerCount() - 1; controller_idx >= 0; --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; - } - } - } - - rgb_controllers_hw.clear(); - - for(RGBController* rgb_controller : rgb_controllers_hw_copy) - { - delete rgb_controller; + RemoveRGBController(GetController(controller_idx)); } std::vector busses_copy = busses; @@ -325,8 +479,6 @@ void ResourceManager::DetectDevices() Cleanup(); - DeviceListChanged(); - /*-------------------------------------------------*\ | Start the device detection thread | \*-------------------------------------------------*/ @@ -344,19 +496,10 @@ void ResourceManager::DetectDevicesThreadFunction() { DetectDeviceMutex.lock(); - unsigned int prev_count = 0; float percent = 0.0f; - std::vector size_used; json detector_settings; bool save_settings = false; - size_used.resize(rgb_controllers_sizes.size()); - - for(unsigned int size_idx = 0; size_idx < size_used.size(); size_idx++) - { - size_used[size_idx] = false; - } - /*-------------------------------------------------*\ | Open device disable list and read in disabled | | device strings | @@ -397,26 +540,13 @@ void ResourceManager::DetectDevicesThreadFunction() if(this_device_enabled) { - 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_hw.size() != prev_count) - { - /*-------------------------------------------------*\ - | First, load sizes for the new controllers | - \*-------------------------------------------------*/ - for(unsigned int controller_size_idx = prev_count - 1; controller_size_idx < rgb_controllers_hw.size(); controller_size_idx++) + std::vector tempVector; // To be removed + i2c_device_detectors[i2c_detector_idx](busses, tempVector); + for(size_t id = 0; id < tempVector.size(); ++id) { - profile_manager->LoadDeviceFromListWithOptions(rgb_controllers_sizes, size_used, rgb_controllers_hw[controller_size_idx], true, false); + RegisterRGBController(tempVector[id]); } - - DeviceListChanged(); } - prev_count = rgb_controllers_hw.size(); percent = (i2c_detector_idx + 1.0f) / (i2c_device_detectors.size() + device_detectors.size()); @@ -443,27 +573,14 @@ void ResourceManager::DetectDevicesThreadFunction() } if(this_device_enabled) - { - device_detectors[detector_idx](rgb_controllers_hw); - } - - /*-------------------------------------------------*\ - | If the device list size has changed, call the | - | device list changed callbacks | - \*-------------------------------------------------*/ - if(rgb_controllers_hw.size() != prev_count) { - /*-------------------------------------------------*\ - | First, load sizes for the new controllers | - \*-------------------------------------------------*/ - for(unsigned int controller_size_idx = prev_count - 1; controller_size_idx < rgb_controllers_hw.size(); controller_size_idx++) + std::vector tempVector; // To be removed + device_detectors[detector_idx](tempVector); + for(size_t id = 0; id < tempVector.size(); ++id) { - profile_manager->LoadDeviceFromListWithOptions(rgb_controllers_sizes, size_used, rgb_controllers_hw[controller_size_idx], true, false); + RegisterRGBController(tempVector[id]); } - - DeviceListChanged(); } - 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 fd007b236..36200be90 100644 --- a/ResourceManager.h +++ b/ResourceManager.h @@ -28,8 +28,27 @@ typedef std::function&)> typedef std::function&)> DeviceDetectorFunction; typedef std::function&, std::vector&)> I2CDeviceDetectorFunction; -typedef void (*DeviceListChangeCallback)(void *); -typedef void (*DetectionProgressCallback)(void *); +enum ControllerList +{ + CONTROLLER_LIST_UI = 0, // Merges all lists + CONTROLLER_LIST_HW, + CONTROLLER_LIST_REMOTE +}; + +typedef void (*DeviceListChangeCallback)(void* receiver, int index, ControllerList list, bool removed); +typedef void (*DetectionProgressCallback)(void* receiver); + +typedef struct +{ + DeviceListChangeCallback callback; + void* receiver; +} DeviceListChangeCallbackBlock; + +typedef struct +{ + DetectionProgressCallback callback; + void* receiver; + } DetectionProgressCallbackBlock; class ResourceManager { @@ -42,28 +61,38 @@ public: void RegisterI2CBus(i2c_smbus_interface *); std::vector & GetI2CBusses(); - void RegisterRGBController(RGBController *); - std::vector & GetRGBControllers(); + void RegisterRGBController(RGBController*, ControllerList list = CONTROLLER_LIST_HW); + bool RemoveRGBController(RGBController*, ControllerList list = CONTROLLER_LIST_UI); + + unsigned int GetControllerCount(ControllerList list = CONTROLLER_LIST_UI); + RGBController* GetController(size_t, ControllerList list = CONTROLLER_LIST_UI); + + int GetUIListIndex(size_t, ControllerList list); void RegisterI2CBusDetector (I2CBusDetectorFunction detector); void RegisterDeviceDetector (std::string name, DeviceDetectorFunction detector); void RegisterI2CDeviceDetector (std::string name, I2CDeviceDetectorFunction detector); - void RegisterDeviceListChangeCallback(DeviceListChangeCallback new_callback, void * new_callback_arg); - void RegisterDetectionProgressCallback(DetectionProgressCallback new_callback, void * new_callback_arg); + void RegisterDeviceListChangeCallback(DeviceListChangeCallback, void * receiver); + void RegisterDetectionProgressCallback(DetectionProgressCallback callback, void* receiver); + void UnregisterDeviceListChangeCallback(DeviceListChangeCallback, void* receiver); + void UnregisterDetectionProgressCallback(DetectionProgressCallback callback, void* receiver); unsigned int GetDetectionPercent(); const char* GetDetectionString(); std::string GetConfigurationDirectory(); - std::vector& GetClients(); + void RegisterClient(NetworkClient*); + unsigned int GetClientCount(); + NetworkClient* GetClient(size_t id); + std::vector& GetClients(); // TODO: hide the vector; do NOT push in here! Use RegisterClient instead NetworkServer* GetServer(); ProfileManager* GetProfileManager(); SettingsManager* GetSettingsManager(); - void DeviceListChanged(); + void DeviceListChanged(int id, ControllerList list, bool removed); void DetectionProgressChanged(); void Cleanup(); @@ -97,9 +126,10 @@ private: /*-------------------------------------------------------------------------------------*\ | RGBControllers | \*-------------------------------------------------------------------------------------*/ - std::vector rgb_controllers_sizes; + std::vector rgb_controllers_hw_sizes; + std::vector rgb_controllers_hw_sizes_used; std::vector rgb_controllers_hw; - std::vector rgb_controllers; + std::vector rgb_controllers_remote; /*-------------------------------------------------------------------------------------*\ | Network Server | @@ -134,13 +164,11 @@ private: | Device List Changed Callback | \*-------------------------------------------------------------------------------------*/ std::mutex DeviceListChangeMutex; - std::vector DeviceListChangeCallbacks; - std::vector DeviceListChangeCallbackArgs; + std::vector DeviceListChangeCallbacks; /*-------------------------------------------------------------------------------------*\ | Detection Progress Callback | \*-------------------------------------------------------------------------------------*/ std::mutex DetectionProgressMutex; - std::vector DetectionProgressCallbacks; - std::vector DetectionProgressCallbackArgs; + std::vector DetectionProgressCallbacks; }; diff --git a/cli.cpp b/cli.cpp index e357517c4..71ff5b7db 100644 --- a/cli.cpp +++ b/cli.cpp @@ -280,8 +280,9 @@ bool ParseColors(std::string colors_string, DeviceOptions *options) return options->colors.size() > 0; } -unsigned int ParseMode(DeviceOptions& options, std::vector &rgb_controllers) +unsigned int ParseMode(DeviceOptions& options) { + ResourceManager* manager = ResourceManager::get(); // no need to check if --mode wasn't passed if (options.mode.size() == 0) { @@ -292,15 +293,16 @@ unsigned int ParseMode(DeviceOptions& options, std::vector &rgb | Search through all of the device modes and see if there is| | a match. If no match is found, print an error message. | \*---------------------------------------------------------*/ - for(unsigned int mode_idx = 0; mode_idx < rgb_controllers[options.device]->modes.size(); mode_idx++) + RGBController* device = manager->GetController(options.device); + for(unsigned int mode_idx = 0; mode_idx < device->modes.size(); mode_idx++) { - if (strcasecmp(rgb_controllers[options.device]->modes[mode_idx].name.c_str(), options.mode.c_str()) == 0) + if (strcasecmp(device->modes[mode_idx].name.c_str(), options.mode.c_str()) == 0) { return mode_idx; } } - std::cout << "Error: Mode '" + options.mode + "' not available for device '" + rgb_controllers[options.device]->name + "'" << std::endl; + std::cout << "Error: Mode '" + options.mode + "' not available for device '" + device->name + "'" << std::endl; return false; } @@ -396,13 +398,15 @@ void OptionVersion() std::cout << version_text << std::endl; } -void OptionListDevices(std::vector &rgb_controllers) +void OptionListDevices() { - ResourceManager::get()->WaitForDeviceDetection(); + ResourceManager* manager = ResourceManager::get(); + manager->WaitForDeviceDetection(); + size_t count = manager->GetControllerCount(); - for(std::size_t controller_idx = 0; controller_idx < rgb_controllers.size(); controller_idx++) + for(std::size_t controller_idx = 0; controller_idx < count; controller_idx++) { - RGBController *controller = rgb_controllers[controller_idx]; + RGBController *controller = manager->GetController(controller_idx); /*---------------------------------------------------------*\ | Print device name | @@ -499,15 +503,17 @@ void OptionListDevices(std::vector &rgb_controllers) } } -bool OptionDevice(int *current_device, std::string argument, Options *options, std::vector &rgb_controllers) +bool OptionDevice(int *current_device, std::string argument, Options *options) { - ResourceManager::get()->WaitForDeviceDetection(); + ResourceManager* manager = ResourceManager::get(); + manager->WaitForDeviceDetection(); + size_t count = manager->GetControllerCount(); try { *current_device = std::stoi(argument); - if((*current_device >= static_cast(rgb_controllers.size())) || (*current_device < 0)) + if((*current_device >= int(count)) || (*current_device < 0)) { throw nullptr; } @@ -531,17 +537,19 @@ 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) +bool OptionZone(int *current_device, int *current_zone, std::string argument, Options */*options*/) { - ResourceManager::get()->WaitForDeviceDetection(); + ResourceManager* manager = ResourceManager::get(); + manager->WaitForDeviceDetection(); + size_t count = manager->GetControllerCount(); try { *current_zone = std::stoi(argument); - if(*current_device >= static_cast(rgb_controllers.size())) + if(*current_device >= int(count)) { - if(*current_zone >= static_cast(rgb_controllers[*current_device]->zones.size())) + if(*current_zone >= int(manager->GetController(*current_device)->zones.size())) { throw nullptr; } @@ -586,26 +594,29 @@ bool OptionMode(int *currentDev, std::string argument, Options *options) return true; } -bool OptionSize(int *current_device, int *current_zone, std::string argument, Options */*options*/, std::vector &rgb_controllers) +bool OptionSize(int *current_device, int *current_zone, std::string argument, Options */*options*/) { const unsigned int new_size = std::stoi(argument); - ResourceManager::get()->WaitForDeviceDetection(); + ResourceManager* manager = ResourceManager::get(); + manager->WaitForDeviceDetection(); + size_t count = manager->GetControllerCount(); /*---------------------------------------------------------*\ | Fail out if device, zone, or size are out of range | \*---------------------------------------------------------*/ - if((*current_device >= static_cast(rgb_controllers.size())) || (*current_device < 0)) + if((*current_device >= int(count)) || (*current_device < 0)) { std::cout << "Error: Device is out of range" << std::endl; return false; } - else if((*current_zone >= static_cast(rgb_controllers[*current_device]->zones.size())) || (*current_zone < 0)) + RGBController* rgb_controller = manager->GetController(*current_device); + if((*current_zone >= int(rgb_controller->zones.size())) || (*current_zone < 0)) { std::cout << "Error: Zone is out of range" << std::endl; return false; } - else if((new_size < rgb_controllers[*current_device]->zones[*current_zone].leds_min) || (new_size > rgb_controllers[*current_device]->zones[*current_zone].leds_max)) + if((new_size < rgb_controller->zones[*current_zone].leds_min) || (new_size > rgb_controller->zones[*current_zone].leds_max)) { std::cout << "Error: New size is out of range" << std::endl; } @@ -613,7 +624,7 @@ bool OptionSize(int *current_device, int *current_zone, std::string argument, Op /*---------------------------------------------------------*\ | Resize the zone | \*---------------------------------------------------------*/ - rgb_controllers[*current_device]->ResizeZone(*current_zone, new_size); + rgb_controller->ResizeZone(*current_zone, new_size); /*---------------------------------------------------------*\ | Save the profile | @@ -623,9 +634,11 @@ bool OptionSize(int *current_device, int *current_zone, std::string argument, Op return true; } -bool OptionProfile(std::string argument, std::vector &rgb_controllers) +bool OptionProfile(std::string argument) { - ResourceManager::get()->WaitForDeviceDetection(); + ResourceManager* manager = ResourceManager::get(); + manager->WaitForDeviceDetection(); + size_t count = manager->GetControllerCount(); /*---------------------------------------------------------*\ | Attempt to load profile | @@ -635,9 +648,9 @@ bool OptionProfile(std::string argument, std::vector &rgb_contr /*-----------------------------------------------------*\ | Change device mode if profile loading was successful | \*-----------------------------------------------------*/ - for(std::size_t controller_idx = 0; controller_idx < rgb_controllers.size(); controller_idx++) + for(std::size_t controller_idx = 0; controller_idx < count; controller_idx++) { - RGBController* device = rgb_controllers[controller_idx]; + RGBController* device = manager->GetController(controller_idx); device->SetMode(device->active_mode); @@ -666,7 +679,7 @@ bool OptionSaveProfile(std::string argument) return(true); } -int ProcessOptions(int argc, char *argv[], Options *options, std::vector &rgb_controllers) +int ProcessOptions(int argc, char *argv[], Options *options) { unsigned int ret_flags = 0; int arg_index = 1; @@ -693,7 +706,7 @@ int ProcessOptions(int argc, char *argv[], Options *options, std::vectorGetClients().push_back(client); + ResourceManager::get()->RegisterClient(client); arg_index++; } @@ -822,7 +835,7 @@ int ProcessOptions(int argc, char *argv[], Options *options, std::vector &rgb_controllers) +void ApplyOptions(DeviceOptions& options) { - RGBController *device = rgb_controllers[options.device]; + ResourceManager* manager = ResourceManager::get(); + RGBController *device = manager->GetController(options.device); /*---------------------------------------------------------*\ | Set mode first, in case it's 'direct' (which affects | | SetLED below) | \*---------------------------------------------------------*/ - unsigned int mode = ParseMode(options, rgb_controllers); + unsigned int mode = ParseMode(options); /*---------------------------------------------------------*\ | Determine which color mode this mode uses and update | @@ -1029,7 +1043,7 @@ void WaitWhileServerOnline(NetworkServer* srv) }; } -unsigned int cli_main(int argc, char *argv[], std::vector &rgb_controllers, ProfileManager* profile_manager_in) +unsigned int cli_main(int argc, char *argv[], ProfileManager* profile_manager_in) { profile_manager = profile_manager_in; @@ -1037,7 +1051,7 @@ unsigned int cli_main(int argc, char *argv[], std::vector &rgb_ | Process the argument options | \*---------------------------------------------------------*/ Options options; - unsigned int ret_flags = ProcessOptions(argc, argv, &options, rgb_controllers); + unsigned int ret_flags = ProcessOptions(argc, argv, &options); /*---------------------------------------------------------*\ | If the server was told to start, start it before returning| @@ -1085,7 +1099,9 @@ unsigned int cli_main(int argc, char *argv[], std::vector &rgb_ break; } - ResourceManager::get()->WaitForDeviceDetection(); + ResourceManager* manager = ResourceManager::get(); + manager->WaitForDeviceDetection(); + size_t count = manager->GetControllerCount(); /*---------------------------------------------------------*\ | If the options has one or more specific devices, loop | @@ -1096,15 +1112,15 @@ unsigned int cli_main(int argc, char *argv[], std::vector &rgb_ { for(unsigned int device_idx = 0; device_idx < options.devices.size(); device_idx++) { - ApplyOptions(options.devices[device_idx], rgb_controllers); + ApplyOptions(options.devices[device_idx]); } } else { - for (unsigned int device_idx = 0; device_idx < rgb_controllers.size(); device_idx++) + for (unsigned int device_idx = 0; device_idx < count; device_idx++) { options.allDeviceOptions.device = device_idx; - ApplyOptions(options.allDeviceOptions, rgb_controllers); + ApplyOptions(options.allDeviceOptions); } } diff --git a/main.cpp b/main.cpp index 503c0b756..3c326ef57 100644 --- a/main.cpp +++ b/main.cpp @@ -25,7 +25,7 @@ using namespace std::chrono_literals; /*-------------------------------------------------------------*\ | Command line functionality and return flags | \*-------------------------------------------------------------*/ -extern unsigned int cli_main(int argc, char *argv[], std::vector &rgb_controllers, ProfileManager* profile_manager_in); +extern unsigned int cli_main(int argc, char *argv[], ProfileManager* profile_manager_in); enum { @@ -80,11 +80,11 @@ void InitializeTimerResolutionThreadFunction() * * \******************************************************************************************/ -bool AttemptLocalConnection(std::vector &rgb_controllers) +bool AttemptLocalConnection() { bool success = false; - NetworkClient * client = new NetworkClient(rgb_controllers); + NetworkClient * client = new NetworkClient(); client->StartClient(); for(int timeout = 0; timeout < 10; timeout++) @@ -106,7 +106,7 @@ bool AttemptLocalConnection(std::vector &rgb_controllers) } else { - ResourceManager::get()->GetClients().push_back(client); + ResourceManager::get()->RegisterClient(client); success = true; } @@ -145,9 +145,8 @@ int main(int argc, char* argv[]) #endif std::vector &busses = ResourceManager::get()->GetI2CBusses(); - std::vector &rgb_controllers = ResourceManager::get()->GetRGBControllers(); - if(!AttemptLocalConnection(rgb_controllers)) + if(!AttemptLocalConnection()) { ResourceManager::get()->DetectDevices(); } @@ -158,7 +157,7 @@ int main(int argc, char* argv[]) unsigned int ret_flags = RET_FLAG_START_GUI; if(argc > 1) { - ret_flags = cli_main(argc, argv, rgb_controllers, ResourceManager::get()->GetProfileManager()); + ret_flags = cli_main(argc, argv, ResourceManager::get()->GetProfileManager()); } /*---------------------------------------------------------*\ @@ -171,7 +170,7 @@ int main(int argc, char* argv[]) QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QApplication a(argc, argv); - Ui::OpenRGBDialog2 dlg(busses, rgb_controllers); + Ui::OpenRGBDialog2 dlg(busses); if(ret_flags & RET_FLAG_I2C_TOOLS) { diff --git a/qt/OpenRGBClientInfoPage.cpp b/qt/OpenRGBClientInfoPage.cpp index 294f59a25..b0c9c8918 100644 --- a/qt/OpenRGBClientInfoPage.cpp +++ b/qt/OpenRGBClientInfoPage.cpp @@ -17,7 +17,7 @@ public: NetworkClient * net_client; }; -OpenRGBClientInfoPage::OpenRGBClientInfoPage(std::vector& control, QWidget *parent) : +OpenRGBClientInfoPage::OpenRGBClientInfoPage(QWidget *parent) : QFrame(parent), ui(new Ui::OpenRGBClientInfoPageUi) { @@ -177,7 +177,7 @@ void Ui::OpenRGBClientInfoPage::on_ClientConnectButton_clicked() /*-----------------------------------------------------*\ | Create a new client and set name, IP, and port values | \*-----------------------------------------------------*/ - NetworkClient * rgb_client = new NetworkClient(ResourceManager::get()->GetRGBControllers()); + NetworkClient * rgb_client = new NetworkClient(); std::string titleString = "OpenRGB "; titleString.append(VERSION_STRING); diff --git a/qt/OpenRGBClientInfoPage.h b/qt/OpenRGBClientInfoPage.h index d133237f7..d2b3d42e8 100644 --- a/qt/OpenRGBClientInfoPage.h +++ b/qt/OpenRGBClientInfoPage.h @@ -15,10 +15,10 @@ class Ui::OpenRGBClientInfoPage : public QFrame Q_OBJECT public: - explicit OpenRGBClientInfoPage(std::vector& control, QWidget *parent = nullptr); + explicit OpenRGBClientInfoPage(QWidget *parent = nullptr); ~OpenRGBClientInfoPage(); - void AddClient(NetworkClient* new_client); + void AddClient(NetworkClient* new_client); // Deprecated and possibly unused public slots: void UpdateInfo(); diff --git a/qt/OpenRGBDevicePage.cpp b/qt/OpenRGBDevicePage.cpp index 137dfabc3..2548e30ce 100644 --- a/qt/OpenRGBDevicePage.cpp +++ b/qt/OpenRGBDevicePage.cpp @@ -109,9 +109,11 @@ OpenRGBDevicePage::~OpenRGBDevicePage() | Unregister update callback from the controller if the | | controller still exists | \*-----------------------------------------------------*/ - for(unsigned int controller_idx = 0; controller_idx < ResourceManager::get()->GetRGBControllers().size(); controller_idx++) + ResourceManager* manager = ResourceManager::get(); + size_t count = manager->GetControllerCount(); + for(unsigned int controller_idx = 0; controller_idx < count; controller_idx++) { - if(ResourceManager::get()->GetRGBControllers()[controller_idx] == device) + if(manager->GetController(controller_idx) == device) { device->UnregisterUpdateCallback(this); break; diff --git a/qt/OpenRGBDialog2.cpp b/qt/OpenRGBDialog2.cpp index ab5ecbbfb..852a02807 100644 --- a/qt/OpenRGBDialog2.cpp +++ b/qt/OpenRGBDialog2.cpp @@ -72,11 +72,11 @@ static QString GetIconString(device_type type, bool dark) return filename; } -static void UpdateDeviceListCallback(void * this_ptr) +static void UpdateDeviceListCallback(void* this_ptr, int index, ControllerList list, bool removed) { OpenRGBDialog2 * this_obj = (OpenRGBDialog2 *)this_ptr; - QMetaObject::invokeMethod(this_obj, "onDeviceListUpdated", Qt::QueuedConnection); + QMetaObject::invokeMethod(this_obj, "onDeviceListUpdated", Qt::QueuedConnection, Q_ARG(int, index), Q_ARG(int, list), Q_ARG(bool, removed)); } static void UpdateDetectionProgressCallback(void * this_ptr) @@ -86,7 +86,7 @@ static void UpdateDetectionProgressCallback(void * this_ptr) QMetaObject::invokeMethod(this_obj, "onDetectionProgressUpdated", Qt::QueuedConnection); } -OpenRGBDialog2::OpenRGBDialog2(std::vector& bus, std::vector& control, QWidget *parent) : QMainWindow(parent), busses(bus), controllers(control), ui(new OpenRGBDialog2Ui) +OpenRGBDialog2::OpenRGBDialog2(std::vector& bus, QWidget *parent) : QMainWindow(parent), busses(bus), ui(new OpenRGBDialog2Ui) { ui->setupUi(this); @@ -100,8 +100,9 @@ OpenRGBDialog2::OpenRGBDialog2(std::vector& bus, std::vec | Register detection progress callback with resource | | manager | \*-----------------------------------------------------*/ - ResourceManager::get()->RegisterDetectionProgressCallback(UpdateDetectionProgressCallback, this); - ResourceManager::get()->RegisterDeviceListChangeCallback(UpdateDeviceListCallback, this); + ResourceManager* manager = ResourceManager::get(); + manager->RegisterDetectionProgressCallback(UpdateDetectionProgressCallback, this); + manager->RegisterDeviceListChangeCallback(UpdateDeviceListCallback, this); /*-----------------------------------------------------*\ | Initialize page pointers | @@ -245,7 +246,7 @@ OpenRGBDialog2::OpenRGBDialog2(std::vector& bus, std::vec | Update the device list and make sure the | | ProgressBar gets a proper value | \*-----------------------------------------------------*/ - UpdateDevicesList(); + LoadDevicesList(); /*-----------------------------------------------------*\ | Add Server Tab | @@ -338,7 +339,7 @@ void OpenRGBDialog2::AddClientTab() \*-----------------------------------------------------*/ if(ClientInfoPage == NULL) { - ClientInfoPage = new OpenRGBClientInfoPage(controllers); + ClientInfoPage = new OpenRGBClientInfoPage(); ui->MainTabBar->addTab(ClientInfoPage, "SDK Client"); /*-----------------------------------------------------*\ @@ -386,42 +387,37 @@ void OpenRGBDialog2::ClearDevicesList() ui->InformationTabBar->clear(); } -void OpenRGBDialog2::UpdateDevicesList() +void OpenRGBDialog2::LoadDevicesList() { - /*-----------------------------------------------------*\ - | Loop through each controller in the list. | - \*-----------------------------------------------------*/ - for(unsigned int controller_idx = 0; controller_idx < controllers.size(); controller_idx++) + ResourceManager* manager = ResourceManager::get(); + size_t count = manager->GetControllerCount(); + for(size_t idx = 0; idx < count; ++idx) { - /*-----------------------------------------------------*\ - | Loop through each tab in the devices tab bar | - \*-----------------------------------------------------*/ - bool found = false; + UpdateDevicesList(idx, false); + } +} - for(unsigned int tab_idx = 0; tab_idx < ui->DevicesTabBar->count(); tab_idx++) - { - OpenRGBDevicePage* page = (OpenRGBDevicePage*) ui->DevicesTabBar->widget(tab_idx); - - /*-----------------------------------------------------*\ - | If the current tab matches the current controller, | - | move the tab to the correct position | - \*-----------------------------------------------------*/ - if(controllers[controller_idx] == page->GetController()) - { - found = true; - ui->DevicesTabBar->tabBar()->moveTab(tab_idx, controller_idx); - break; - } - } - - if(!found) +void OpenRGBDialog2::UpdateDevicesList(int index, bool remove) +{ + ResourceManager* manager = ResourceManager::get(); + if(remove) + { + QWidget* device_page = ui->DevicesTabBar->widget(index); + QWidget* info_page = ui->InformationTabBar->widget(index); + ui->DevicesTabBar->removeTab(index); + ui->InformationTabBar->removeTab(index); + delete device_page; + delete info_page; + } + else + { + RGBController* device = manager->GetController(index); { /*-----------------------------------------------------*\ - | The controller does not have a tab already created | | Create a new tab and move it to the correct position | \*-----------------------------------------------------*/ - OpenRGBDevicePage *NewPage = new OpenRGBDevicePage(controllers[controller_idx]); - ui->DevicesTabBar->addTab(NewPage, ""); + OpenRGBDevicePage *NewPage = new OpenRGBDevicePage(device); + ui->DevicesTabBar->insertTab(index, NewPage, ""); /*-----------------------------------------------------*\ | Connect the page's Set All button to the Set All slot | @@ -445,58 +441,23 @@ void OpenRGBDialog2::UpdateDevicesList() | type and append device name string. | \*-----------------------------------------------------*/ QString NewLabelString = "
" + QString::fromStdString(controllers[controller_idx]->name) + "
"; + NewLabelString += GetIconString(device->type, darkTheme); + NewLabelString += "' height='16' width='16'>" + QString::fromStdString(device->name) + ""; QLabel *NewTabLabel = new QLabel(); NewTabLabel->setText(NewLabelString); NewTabLabel->setIndent(20); NewTabLabel->setGeometry(0, 0, 200, 20); - ui->DevicesTabBar->tabBar()->setTabButton(ui->DevicesTabBar->count() - 1, QTabBar::LeftSide, NewTabLabel); - - /*-----------------------------------------------------*\ - | Now move the new tab to the correct position | - \*-----------------------------------------------------*/ - ui->DevicesTabBar->tabBar()->moveTab(ui->DevicesTabBar->count() - 1, controller_idx); + ui->DevicesTabBar->tabBar()->setTabButton(index, QTabBar::LeftSide, NewTabLabel); } - - /*-----------------------------------------------------*\ - | Loop through each tab in the information tab bar | - \*-----------------------------------------------------*/ - found = false; - - for(unsigned int tab_idx = 0; tab_idx < ui->InformationTabBar->count(); tab_idx++) - { - /*-----------------------------------------------------*\ - | If type is a device info page, check it | - \*-----------------------------------------------------*/ - std::string type_str = ui->InformationTabBar->widget(tab_idx)->metaObject()->className(); - if(type_str == "Ui::OpenRGBDeviceInfoPage") - { - OpenRGBDeviceInfoPage* page = (OpenRGBDeviceInfoPage*) ui->InformationTabBar->widget(tab_idx); - - /*-----------------------------------------------------*\ - | If the current tab matches the current controller, | - | move the tab to the correct position | - \*-----------------------------------------------------*/ - if(controllers[controller_idx] == page->GetController()) - { - found = true; - ui->InformationTabBar->tabBar()->moveTab(tab_idx, controller_idx); - break; - } - } - } - - if(!found) { /*-----------------------------------------------------*\ | The controller does not have a tab already created | | Create a new tab and move it to the correct position | \*-----------------------------------------------------*/ - OpenRGBDeviceInfoPage *NewPage = new OpenRGBDeviceInfoPage(controllers[controller_idx]); - ui->InformationTabBar->addTab(NewPage, ""); + OpenRGBDeviceInfoPage *NewPage = new OpenRGBDeviceInfoPage(device); + ui->InformationTabBar->insertTab(index, NewPage, ""); /*-----------------------------------------------------*\ | Use Qt's HTML capabilities to display both icon and | @@ -504,50 +465,15 @@ void OpenRGBDialog2::UpdateDevicesList() | type and append device name string. | \*-----------------------------------------------------*/ QString NewLabelString = "
" + QString::fromStdString(controllers[controller_idx]->name) + "
"; + NewLabelString += GetIconString(device->type, darkTheme); + NewLabelString += "' height='16' width='16'>" + QString::fromStdString(device->name) + ""; QLabel *NewTabLabel = new QLabel(); NewTabLabel->setText(NewLabelString); NewTabLabel->setIndent(20); NewTabLabel->setGeometry(0, 0, 200, 20); - ui->InformationTabBar->tabBar()->setTabButton(ui->InformationTabBar->count() - 1, QTabBar::LeftSide, NewTabLabel); - - /*-----------------------------------------------------*\ - | Now move the new tab to the correct position | - \*-----------------------------------------------------*/ - ui->InformationTabBar->tabBar()->moveTab(ui->InformationTabBar->count() - 1, controller_idx); - } - } - - /*-----------------------------------------------------*\ - | Remove all remaining device tabs | - \*-----------------------------------------------------*/ - unsigned int tab_count = ui->DevicesTabBar->count(); - for(unsigned int tab_idx = controllers.size(); tab_idx < tab_count; tab_idx++) - { - ui->DevicesTabBar->removeTab(ui->DevicesTabBar->count() - 1); - } - - bool found = true; - while(found) - { - found = false; - - /*-----------------------------------------------------*\ - | Remove all remaining device information tabs, leaving | - | other information tabs alone | - \*-----------------------------------------------------*/ - for(unsigned int tab_idx = controllers.size(); tab_idx < ui->InformationTabBar->count(); tab_idx++) - { - std::string type_str = ui->InformationTabBar->widget(tab_idx)->metaObject()->className(); - if(type_str == "Ui::OpenRGBDeviceInfoPage") - { - found = true; - ui->InformationTabBar->removeTab(tab_idx); - break; - } + ui->InformationTabBar->tabBar()->setTabButton(index, QTabBar::LeftSide, NewTabLabel); } } } @@ -630,12 +556,14 @@ void OpenRGBDialog2::on_QuickWhite() void OpenRGBDialog2::on_ClientListUpdated() { - UpdateDevicesList(); + //UpdateDevicesList(); } -void OpenRGBDialog2::onDeviceListUpdated() +void OpenRGBDialog2::onDeviceListUpdated(int index, int list, bool removed) { - UpdateDevicesList(); + ResourceManager* manager = ResourceManager::get(); + int idx = manager->GetUIListIndex(index, ControllerList(list)); + UpdateDevicesList(idx, removed); } void OpenRGBDialog2::onDetectionProgressUpdated() diff --git a/qt/OpenRGBDialog2.h b/qt/OpenRGBDialog2.h index 33a137fbc..44c0b6ca9 100644 --- a/qt/OpenRGBDialog2.h +++ b/qt/OpenRGBDialog2.h @@ -13,6 +13,7 @@ #include "ProfileManager.h" #include "NetworkClient.h" #include "NetworkServer.h" +#include "ResourceManager.h" #include #include @@ -29,10 +30,10 @@ class Ui::OpenRGBDialog2 : public QMainWindow Q_OBJECT public: - explicit OpenRGBDialog2(std::vector& bus, std::vector& control, QWidget *parent = 0); + explicit OpenRGBDialog2(std::vector& bus, QWidget *parent = 0); ~OpenRGBDialog2(); - void AddClient(NetworkClient* new_client); + void AddClient(NetworkClient* new_client); // Deprecated and possibly unused void AddClientTab(); void AddI2CToolsPage(); void AddServerTab(); @@ -41,7 +42,6 @@ public: protected: std::vector& busses; - std::vector& controllers; private: /*-------------------------------------*\ @@ -67,7 +67,9 @@ private: void AddSoftwareInfoPage(); void ClearDevicesList(); - void UpdateDevicesList(); + //void UpdateDevicesList(); // Deprecated + void LoadDevicesList(); + void UpdateDevicesList(int index, bool remove); void UpdateProfileList(); void closeEvent(QCloseEvent *event); @@ -87,7 +89,7 @@ private slots: void on_QuickMagenta(); void on_QuickWhite(); void on_ClientListUpdated(); - void onDeviceListUpdated(); + void onDeviceListUpdated(int index, int list, bool removed); void onDetectionProgressUpdated(); void on_SetAllDevices(unsigned char red, unsigned char green, unsigned char blue); void on_SaveSizeProfile();