diff --git a/NetworkClient.cpp b/NetworkClient.cpp index ea8e307e..a46c6ea8 100644 --- a/NetworkClient.cpp +++ b/NetworkClient.cpp @@ -45,6 +45,12 @@ NetworkClient::~NetworkClient() StopClient(); } +void NetworkClient::ClearCallbacks() +{ + ClientInfoChangeCallbacks.clear(); + ClientInfoChangeCallbackArgs.clear(); +} + void NetworkClient::ClientInfoChanged() { ClientInfoChangeMutex.lock(); diff --git a/NetworkClient.h b/NetworkClient.h index 1870d7ed..3499b00d 100644 --- a/NetworkClient.h +++ b/NetworkClient.h @@ -31,6 +31,7 @@ public: unsigned int GetProtocolVersion(); bool GetOnline(); + void ClearCallbacks(); void RegisterClientInfoChangeCallback(NetClientCallback new_callback, void * new_callback_arg); void SetIP(const char *new_ip); diff --git a/ResourceManager.cpp b/ResourceManager.cpp index 471bb0e1..45b6da77 100644 --- a/ResourceManager.cpp +++ b/ResourceManager.cpp @@ -88,10 +88,10 @@ void ResourceManager::RegisterRGBController(RGBController *rgb_controller) LOG_NOTICE("Registering RGB controller: %s", rgb_controller->name.c_str()); rgb_controllers_hw.push_back(rgb_controller); - DeviceListChanged(); + UpdateDeviceList(); } -void ResourceManager::UnregisterRGBController(RGBController *rgb_controller) +void ResourceManager::UnregisterRGBController(RGBController* rgb_controller) { LOG_NOTICE("Unregistering RGB controller: %s", rgb_controller->name.c_str()); @@ -120,7 +120,7 @@ void ResourceManager::UnregisterRGBController(RGBController *rgb_controller) rgb_controllers.erase(rgb_it); } - DeviceListChanged(); + UpdateDeviceList(); } std::vector & ResourceManager::GetRGBControllers() @@ -190,7 +190,7 @@ void ResourceManager::RegisterDetectionProgressCallback(DetectionProgressCallbac DetectionProgressCallbackArgs.push_back(new_callback_arg); } -void ResourceManager::DeviceListChanged() +void ResourceManager::UpdateDeviceList() { DeviceListChangeMutex.lock(); @@ -239,10 +239,7 @@ void ResourceManager::DeviceListChanged() /*-------------------------------------------------*\ | 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]); - } + DeviceListChanged(); /*-------------------------------------------------*\ | Device list has changed, inform all clients | @@ -253,6 +250,17 @@ void ResourceManager::DeviceListChanged() DeviceListChangeMutex.unlock(); } +void ResourceManager::DeviceListChanged() +{ + /*-------------------------------------------------*\ + | Device list has changed, call the callbacks | + \*-------------------------------------------------*/ + for(unsigned int callback_idx = 0; callback_idx < DeviceListChangeCallbacks.size(); callback_idx++) + { + ResourceManager::DeviceListChangeCallbacks[callback_idx](DeviceListChangeCallbackArgs[callback_idx]); + } +} + void ResourceManager::DetectionProgressChanged() { DetectionProgressMutex.lock(); @@ -340,6 +348,50 @@ NetworkServer* ResourceManager::GetServer() return(server); } +static void NetworkClientInfoChangeCallback(void* this_ptr) +{ + ResourceManager* this_obj = (ResourceManager*)this_ptr; + + this_obj->DeviceListChanged(); +} + +void ResourceManager::RegisterNetworkClient(NetworkClient* new_client) +{ + new_client->RegisterClientInfoChangeCallback(NetworkClientInfoChangeCallback, this); + + clients.push_back(new_client); +} + +void ResourceManager::UnregisterNetworkClient(NetworkClient* network_client) +{ + /*-------------------------------------------------------------------------*\ + | Stop the disconnecting client | + \*-------------------------------------------------------------------------*/ + network_client->StopClient(); + + /*-------------------------------------------------------------------------*\ + | Clear callbacks from the client before removal | + \*-------------------------------------------------------------------------*/ + network_client->ClearCallbacks(); + + /*-------------------------------------------------------------------------*\ + | Find the client to remove and remove it from the clients list | + \*-------------------------------------------------------------------------*/ + std::vector::iterator client_it = std::find(clients.begin(), clients.end(), network_client); + + if(client_it != clients.end()) + { + clients.erase(client_it); + } + + /*-------------------------------------------------------------------------*\ + | Delete the client | + \*-------------------------------------------------------------------------*/ + delete network_client; + + UpdateDeviceList(); +} + std::vector& ResourceManager::GetClients() { return(clients); @@ -451,7 +503,7 @@ void ResourceManager::DetectDevices() Cleanup(); - DeviceListChanged(); + UpdateDeviceList(); /*-------------------------------------------------*\ | Start the device detection thread | @@ -603,7 +655,7 @@ void ResourceManager::DetectDevicesThreadFunction() profile_manager->LoadDeviceFromListWithOptions(rgb_controllers_sizes, size_used, rgb_controllers_hw[controller_size_idx], true, false); } - DeviceListChanged(); + UpdateDeviceList(); } prev_count = rgb_controllers_hw.size(); @@ -690,7 +742,7 @@ void ResourceManager::DetectDevicesThreadFunction() profile_manager->LoadDeviceFromListWithOptions(rgb_controllers_sizes, size_used, rgb_controllers_hw[controller_size_idx], true, false); } - DeviceListChanged(); + UpdateDeviceList(); } prev_count = rgb_controllers_hw.size(); } @@ -827,7 +879,7 @@ void ResourceManager::DetectDevicesThreadFunction() profile_manager->LoadDeviceFromListWithOptions(rgb_controllers_sizes, size_used, rgb_controllers_hw[controller_size_idx], true, false); } - DeviceListChanged(); + UpdateDeviceList(); } prev_count = rgb_controllers_hw.size(); diff --git a/ResourceManager.h b/ResourceManager.h index 7d7e048f..566d0845 100644 --- a/ResourceManager.h +++ b/ResourceManager.h @@ -76,7 +76,7 @@ public: virtual ProfileManager* GetProfileManager() = 0; virtual SettingsManager* GetSettingsManager() = 0; - virtual void DeviceListChanged() = 0; + virtual void UpdateDeviceList() = 0; virtual void WaitForDeviceDetection() = 0; protected: @@ -90,7 +90,7 @@ public: ResourceManager(); ~ResourceManager(); - + void RegisterI2CBus(i2c_smbus_interface *); std::vector & GetI2CBusses(); @@ -120,6 +120,9 @@ public: std::string GetConfigurationDirectory(); + void RegisterNetworkClient(NetworkClient* new_client); + void UnregisterNetworkClient(NetworkClient* network_client); + std::vector& GetClients(); NetworkServer* GetServer(); @@ -128,6 +131,7 @@ public: void SetConfigurationDirectory(std::string directory); + void UpdateDeviceList(); void DeviceListChanged(); void DetectionProgressChanged(); void I2CBusListChanged(); diff --git a/qt/OpenRGBClientInfoPage.cpp b/qt/OpenRGBClientInfoPage.cpp index 491ab62d..cfda20f5 100644 --- a/qt/OpenRGBClientInfoPage.cpp +++ b/qt/OpenRGBClientInfoPage.cpp @@ -161,11 +161,6 @@ void OpenRGBClientInfoPage::UpdateInfo() } } } - - /*-----------------------------------------------------*\ - | Emit client information updated signal | - \*-----------------------------------------------------*/ - emit ClientListUpdated(); } void Ui::OpenRGBClientInfoPage::on_ClientConnectButton_clicked() @@ -193,7 +188,7 @@ void Ui::OpenRGBClientInfoPage::on_ClientConnectButton_clicked() /*-----------------------------------------------------*\ | Add new client to list and register update callback | \*-----------------------------------------------------*/ - ResourceManager::get()->GetClients().push_back(rgb_client); + ResourceManager::get()->RegisterNetworkClient(rgb_client); rgb_client->RegisterClientInfoChangeCallback(UpdateInfoCallback, this); } @@ -206,24 +201,8 @@ void Ui::OpenRGBClientInfoPage::onClientDisconnectButton_clicked(QObject * arg) NetworkClient * disconnect_client = ((NetworkClientPointer *)arg)->net_client; /*-----------------------------------------------------*\ - | Stop the disconnecting client | + | Remove the client from the resource manager, which | + | deletes the client | \*-----------------------------------------------------*/ - disconnect_client->StopClient(); - - /*-----------------------------------------------------*\ - | Remove disconnecting client from list | - \*-----------------------------------------------------*/ - for(unsigned int client_idx = 0; client_idx < ResourceManager::get()->GetClients().size(); client_idx++) - { - if(disconnect_client == ResourceManager::get()->GetClients()[client_idx]) - { - ResourceManager::get()->GetClients().erase(ResourceManager::get()->GetClients().begin() + client_idx); - break; - } - } - - /*-----------------------------------------------------*\ - | Delete the disconnecting client | - \*-----------------------------------------------------*/ - delete disconnect_client; + ResourceManager::get()->UnregisterNetworkClient(disconnect_client); } diff --git a/qt/OpenRGBClientInfoPage.h b/qt/OpenRGBClientInfoPage.h index f084998b..11e1ef78 100644 --- a/qt/OpenRGBClientInfoPage.h +++ b/qt/OpenRGBClientInfoPage.h @@ -29,9 +29,6 @@ private slots: private: Ui::OpenRGBClientInfoPageUi *ui; - -signals: - void ClientListUpdated(); }; #endif // OPENRGBCLIENTINFOPAGE_H diff --git a/qt/OpenRGBDialog2.cpp b/qt/OpenRGBDialog2.cpp index a020c55c..9280c89b 100644 --- a/qt/OpenRGBDialog2.cpp +++ b/qt/OpenRGBDialog2.cpp @@ -667,14 +667,6 @@ void OpenRGBDialog2::AddClientTab() { ClientInfoPage = new OpenRGBClientInfoPage(); ui->MainTabBar->insertTab(2, ClientInfoPage, "SDK Client"); - - /*-----------------------------------------------------*\ - | Connect the page's Set All button to the Set All slot | - \*-----------------------------------------------------*/ - connect(ClientInfoPage, - SIGNAL(ClientListUpdated()), - this, - SLOT(on_ClientListUpdated())); } } @@ -1002,11 +994,6 @@ void OpenRGBDialog2::on_QuickWhite() on_SetAllDevices(0xFF, 0xFF, 0xFF); } -void OpenRGBDialog2::on_ClientListUpdated() -{ - UpdateDevicesList(); -} - void OpenRGBDialog2::onDeviceListUpdated() { UpdateDevicesList(); diff --git a/qt/OpenRGBDialog2.h b/qt/OpenRGBDialog2.h index 1aee4263..a677fe44 100644 --- a/qt/OpenRGBDialog2.h +++ b/qt/OpenRGBDialog2.h @@ -95,7 +95,6 @@ private slots: void on_QuickBlue(); void on_QuickMagenta(); void on_QuickWhite(); - void on_ClientListUpdated(); void onDeviceListUpdated(); void onDetectionProgressUpdated(); void on_SetAllDevices(unsigned char red, unsigned char green, unsigned char blue);