Files
OpenRGB/ResourceManager.h

138 lines
6.5 KiB
C++

/*-----------------------------------------*\
| ResourceManager.h |
| |
| OpenRGB Resource Manager controls access |
| to application components including |
| RGBControllers, I2C interfaces, and |
| network SDK components |
| |
| Adam Honse (CalcProgrammer1) 9/27/2020 |
\*-----------------------------------------*/
#pragma once
#include <memory>
#include <vector>
#include <functional>
#include <thread>
#include <string>
#include "i2c_smbus.h"
#include "NetworkClient.h"
#include "NetworkServer.h"
#include "ProfileManager.h"
#include "RGBController.h"
typedef std::function<void(std::vector<i2c_smbus_interface*>&)> I2CBusDetectorFunction;
typedef std::function<void(std::vector<RGBController*>&)> DeviceDetectorFunction;
typedef std::function<void(std::vector<i2c_smbus_interface*>&, std::vector<RGBController*>&)> I2CDeviceDetectorFunction;
typedef void (*DeviceListChangeCallback)(void *);
typedef void (*DetectionProgressCallback)(void *);
class ResourceManager
{
public:
static ResourceManager *get();
ResourceManager();
~ResourceManager();
void RegisterI2CBus(i2c_smbus_interface *);
std::vector<i2c_smbus_interface*> & GetI2CBusses();
void RegisterRGBController(RGBController *);
std::vector<RGBController*> & GetRGBControllers();
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);
unsigned int GetDetectionPercent();
const char* GetDetectionString();
std::vector<NetworkClient*>& GetClients();
NetworkServer* GetServer();
ProfileManager* GetProfileManager();
void DeviceListChanged();
void DetectionProgressChanged();
void Cleanup();
void DetectDevices();
void DetectDevicesThreadFunction();
void StopDeviceDetection();
void WaitForDeviceDetection();
private:
static std::unique_ptr<ResourceManager> instance;
/*-------------------------------------------------------------------------------------*\
| Profile Manager |
\*-------------------------------------------------------------------------------------*/
ProfileManager* profile_manager;
/*-------------------------------------------------------------------------------------*\
| I2C/SMBus Interfaces |
\*-------------------------------------------------------------------------------------*/
std::vector<i2c_smbus_interface*> busses;
/*-------------------------------------------------------------------------------------*\
| RGBControllers |
\*-------------------------------------------------------------------------------------*/
std::vector<RGBController*> rgb_controllers_sizes;
std::vector<RGBController*> rgb_controllers_hw;
std::vector<RGBController*> rgb_controllers;
/*-------------------------------------------------------------------------------------*\
| Network Server |
\*-------------------------------------------------------------------------------------*/
NetworkServer* server;
/*-------------------------------------------------------------------------------------*\
| Network Clients |
\*-------------------------------------------------------------------------------------*/
std::vector<NetworkClient*> clients;
/*-------------------------------------------------------------------------------------*\
| Detectors |
\*-------------------------------------------------------------------------------------*/
std::vector<DeviceDetectorFunction> device_detectors;
std::vector<std::string> device_detector_strings;
std::vector<I2CBusDetectorFunction> i2c_bus_detectors;
std::vector<I2CDeviceDetectorFunction> i2c_device_detectors;
std::vector<std::string> i2c_device_detector_strings;
/*-------------------------------------------------------------------------------------*\
| Detection Thread and Detection State |
\*-------------------------------------------------------------------------------------*/
std::thread * DetectDevicesThread;
std::mutex DetectDeviceMutex;
std::atomic<bool> detection_is_required;
std::atomic<unsigned int> detection_percent;
const char* detection_string;
/*-------------------------------------------------------------------------------------*\
| Device List Changed Callback |
\*-------------------------------------------------------------------------------------*/
std::mutex DeviceListChangeMutex;
std::vector<DeviceListChangeCallback> DeviceListChangeCallbacks;
std::vector<void *> DeviceListChangeCallbackArgs;
/*-------------------------------------------------------------------------------------*\
| Detection Progress Callback |
\*-------------------------------------------------------------------------------------*/
std::mutex DetectionProgressMutex;
std::vector<DeviceListChangeCallback> DetectionProgressCallbacks;
std::vector<void *> DetectionProgressCallbackArgs;
};