-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathResourceManager.h
371 lines (301 loc) · 20.3 KB
/
ResourceManager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*---------------------------------------------------------*\
| ResourceManager.h |
| |
| OpenRGB Resource Manager controls access to application |
| components including RGBControllers, I2C interfaces, |
| and network SDK components |
| |
| Adam Honse (CalcProgrammer1) 27 Sep 2020 |
| |
| This file is part of the OpenRGB project |
| SPDX-License-Identifier: GPL-2.0-only |
\*---------------------------------------------------------*/
#pragma once
#include <memory>
#include <vector>
#include <functional>
#include <thread>
#include <string>
#include <vector>
#include "SPDWrapper.h"
#include "hidapi_wrapper.h"
#include "i2c_smbus.h"
#include "filesystem.h"
#include "json.hpp"
using json = nlohmann::json;
#define HID_INTERFACE_ANY -1
#define HID_USAGE_ANY -1
#define HID_USAGE_PAGE_ANY -1L
#define CONTROLLER_LIST_HID 0
struct hid_device_info;
class NetworkClient;
class NetworkServer;
class ProfileManager;
class RGBController;
class SettingsManager;
typedef std::function<bool()> I2CBusDetectorFunction;
typedef std::function<void()> DeviceDetectorFunction;
typedef std::function<void(std::vector<i2c_smbus_interface*>&)> I2CDeviceDetectorFunction;
typedef std::function<void(i2c_smbus_interface*, std::vector<SPDWrapper*>&)> I2CDIMMDeviceDetectorFunction;
typedef std::function<void(i2c_smbus_interface*, uint8_t, const std::string&)> I2CPCIDeviceDetectorFunction;
typedef std::function<void(hid_device_info*, const std::string&)> HIDDeviceDetectorFunction;
typedef std::function<void(hidapi_wrapper wrapper, hid_device_info*, const std::string&)> HIDWrappedDeviceDetectorFunction;
typedef std::function<void()> DynamicDetectorFunction;
typedef std::function<void()> PreDetectionHookFunction;
class BasicHIDBlock
{
public:
std::string name;
uint16_t vid;
uint16_t pid;
int interface;
int usage_page;
int usage;
bool compare(hid_device_info* info);
};
class HIDDeviceDetectorBlock : public BasicHIDBlock
{
public:
HIDDeviceDetectorFunction function;
};
class HIDWrappedDeviceDetectorBlock : public BasicHIDBlock
{
public:
HIDWrappedDeviceDetectorFunction function;
};
typedef struct
{
std::string name;
I2CPCIDeviceDetectorFunction function;
uint16_t ven_id;
uint16_t dev_id;
uint16_t subven_id;
uint16_t subdev_id;
uint8_t i2c_addr;
} I2CPCIDeviceDetectorBlock;
typedef struct
{
std::string name;
I2CDIMMDeviceDetectorFunction function;
uint16_t jedec_id;
uint8_t dimm_type;
} I2CDIMMDeviceDetectorBlock;
typedef void (*DeviceListChangeCallback)(void *);
typedef void (*DetectionProgressCallback)(void *);
typedef void (*DetectionStartCallback)(void *);
typedef void (*DetectionEndCallback)(void *);
typedef void (*I2CBusListChangeCallback)(void *);
class ResourceManagerInterface
{
public:
virtual std::vector<i2c_smbus_interface*> & GetI2CBusses() = 0;
virtual void RegisterRGBController(RGBController *rgb_controller) = 0;
virtual void UnregisterRGBController(RGBController *rgb_controller) = 0;
virtual void RegisterDeviceListChangeCallback(DeviceListChangeCallback new_callback, void * new_callback_arg) = 0;
virtual void RegisterDetectionProgressCallback(DetectionProgressCallback new_callback, void * new_callback_arg) = 0;
virtual void RegisterDetectionStartCallback(DetectionStartCallback new_callback, void * new_callback_arg) = 0;
virtual void RegisterDetectionEndCallback(DetectionEndCallback new_callback, void * new_callback_arg) = 0;
virtual void RegisterI2CBusListChangeCallback(I2CBusListChangeCallback new_callback, void * new_callback_arg) = 0;
virtual void UnregisterDeviceListChangeCallback(DeviceListChangeCallback callback, void * callback_arg) = 0;
virtual void UnregisterDetectionProgressCallback(DetectionProgressCallback callback, void *callback_arg) = 0;
virtual void UnregisterDetectionStartCallback(DetectionStartCallback callback, void *callback_arg) = 0;
virtual void UnregisterDetectionEndCallback(DetectionEndCallback callback, void *callback_arg) = 0;
virtual void UnregisterI2CBusListChangeCallback(I2CBusListChangeCallback callback, void * callback_arg) = 0;
virtual std::vector<RGBController*> & GetRGBControllers() = 0;
virtual unsigned int GetDetectionPercent() = 0;
virtual filesystem::path GetConfigurationDirectory() = 0;
virtual std::vector<NetworkClient*>& GetClients() = 0;
virtual NetworkServer* GetServer() = 0;
virtual ProfileManager* GetProfileManager() = 0;
virtual SettingsManager* GetSettingsManager() = 0;
virtual void UpdateDeviceList() = 0;
virtual void WaitForDeviceDetection() = 0;
protected:
virtual ~ResourceManagerInterface() {};
};
class ResourceManager: public ResourceManagerInterface
{
public:
static ResourceManager *get();
ResourceManager();
~ResourceManager();
void RegisterI2CBus(i2c_smbus_interface *);
std::vector<i2c_smbus_interface*> & GetI2CBusses();
void RegisterRGBController(RGBController *rgb_controller);
void UnregisterRGBController(RGBController *rgb_controller);
std::vector<RGBController*> & GetRGBControllers();
void RegisterI2CBusDetector (I2CBusDetectorFunction detector);
void RegisterDeviceDetector (std::string name, DeviceDetectorFunction detector);
void RegisterI2CDeviceDetector (std::string name, I2CDeviceDetectorFunction detector);
void RegisterI2CDIMMDeviceDetector (std::string name, I2CDIMMDeviceDetectorFunction detector, uint16_t jedec_id, uint8_t dimm_type);
void RegisterI2CPCIDeviceDetector (std::string name, I2CPCIDeviceDetectorFunction detector, uint16_t ven_id, uint16_t dev_id, uint16_t subven_id, uint16_t subdev_id, uint8_t i2c_addr);
void RegisterHIDDeviceDetector (std::string name,
HIDDeviceDetectorFunction detector,
uint16_t vid,
uint16_t pid,
int interface = HID_INTERFACE_ANY,
int usage_page = HID_USAGE_PAGE_ANY,
int usage = HID_USAGE_ANY);
void RegisterHIDWrappedDeviceDetector (std::string name,
HIDWrappedDeviceDetectorFunction detector,
uint16_t vid,
uint16_t pid,
int interface = HID_INTERFACE_ANY,
int usage_page = HID_USAGE_PAGE_ANY,
int usage = HID_USAGE_ANY);
void RegisterDynamicDetector (std::string name, DynamicDetectorFunction detector);
void RegisterPreDetectionHook (PreDetectionHookFunction hook);
void RegisterDeviceListChangeCallback(DeviceListChangeCallback new_callback, void * new_callback_arg);
void RegisterDetectionProgressCallback(DetectionProgressCallback new_callback, void * new_callback_arg);
void RegisterDetectionStartCallback(DetectionStartCallback new_callback, void * new_callback_arg);
void RegisterDetectionEndCallback(DetectionEndCallback new_callback, void * new_callback_arg);
void RegisterI2CBusListChangeCallback(I2CBusListChangeCallback new_callback, void * new_callback_arg);
void UnregisterDeviceListChangeCallback(DeviceListChangeCallback callback, void * callback_arg);
void UnregisterDetectionProgressCallback(DetectionProgressCallback callback, void *callback_arg);
void UnregisterDetectionStartCallback(DetectionStartCallback callback, void *callback_arg);
void UnregisterDetectionEndCallback(DetectionEndCallback callback, void *callback_arg);
void UnregisterI2CBusListChangeCallback(I2CBusListChangeCallback callback, void * callback_arg);
bool GetDetectionEnabled();
unsigned int GetDetectionPercent();
const char* GetDetectionString();
filesystem::path GetConfigurationDirectory();
void RegisterNetworkClient(NetworkClient* new_client);
void UnregisterNetworkClient(NetworkClient* network_client);
std::vector<NetworkClient*>& GetClients();
NetworkServer* GetServer();
ProfileManager* GetProfileManager();
SettingsManager* GetSettingsManager();
void SetConfigurationDirectory(const filesystem::path &directory);
void ProcessPreDetectionHooks(); // Consider making private
void ProcessDynamicDetectors(); // Consider making private
void UpdateDeviceList();
void DeviceListChanged();
void DetectionProgressChanged();
void I2CBusListChanged();
void Initialize(bool tryConnect, bool detectDevices, bool startServer, bool applyPostOptions);
void Cleanup();
void DetectDevices();
void DisableDetection();
void StopDeviceDetection();
void WaitForInitialization();
void WaitForDeviceDetection();
private:
void UpdateDetectorSettings();
void SetupConfigurationDirectory();
bool AttemptLocalConnection();
bool ProcessPreDetection();
void ProcessPostDetection();
bool IsAnyDimmDetectorEnabled(json &detector_settings);
void RunInBackgroundThread(std::function<void()>);
void BackgroundThreadFunction();
/*-------------------------------------------------------------------------------------*\
| Functions that must be run in the background thread |
| These are not related to STL coroutines, yet this name is the most convenient |
\*-------------------------------------------------------------------------------------*/
void InitCoroutine();
void DetectDevicesCoroutine();
void HidExitCoroutine();
/*-------------------------------------------------------------------------------------*\
| Static pointer to shared instance of ResourceManager |
\*-------------------------------------------------------------------------------------*/
static ResourceManager* instance;
/*-------------------------------------------------------------------------------------*\
| Auto connection permitting flag |
\*-------------------------------------------------------------------------------------*/
bool tryAutoConnect;
/*-------------------------------------------------------------------------------------*\
| Detection enabled flag |
\*-------------------------------------------------------------------------------------*/
bool detection_enabled;
/*-------------------------------------------------------------------------------------*\
| Auto connection permitting flag |
\*-------------------------------------------------------------------------------------*/
bool start_server;
/*-------------------------------------------------------------------------------------*\
| Auto connection permitting flag |
\*-------------------------------------------------------------------------------------*/
bool apply_post_options;
/*-------------------------------------------------------------------------------------*\
| Initialization completion flag |
\*-------------------------------------------------------------------------------------*/
std::atomic<bool> init_finished;
/*-------------------------------------------------------------------------------------*\
| Profile Manager |
\*-------------------------------------------------------------------------------------*/
ProfileManager* profile_manager;
/*-------------------------------------------------------------------------------------*\
| Settings Manager |
\*-------------------------------------------------------------------------------------*/
SettingsManager* settings_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;
std::vector<I2CDIMMDeviceDetectorBlock> i2c_dimm_device_detectors;
std::vector<I2CPCIDeviceDetectorBlock> i2c_pci_device_detectors;
std::vector<HIDDeviceDetectorBlock> hid_device_detectors;
std::vector<HIDWrappedDeviceDetectorBlock> hid_wrapped_device_detectors;
std::vector<DynamicDetectorFunction> dynamic_detectors;
std::vector<std::string> dynamic_detector_strings;
std::vector<PreDetectionHookFunction> pre_detection_hooks;
bool dynamic_detectors_processed;
/*-------------------------------------------------------------------------------------*\
| Detection Thread and Detection State |
\*-------------------------------------------------------------------------------------*/
std::thread * DetectDevicesThread;
std::mutex DetectDeviceMutex;
std::function<void()> ScheduledBackgroundFunction;
std::mutex BackgroundThreadStateMutex;
std::condition_variable BackgroundFunctionStartTrigger; // NOTE: wakes up the background detection thread
std::atomic<bool> background_thread_running;
std::atomic<bool> detection_is_required;
std::atomic<unsigned int> detection_percent;
std::atomic<unsigned int> detection_prev_size;
std::vector<bool> detection_size_entry_used;
const char* detection_string;
/*-------------------------------------------------------------------------------------*\
| Device List Changed Callback |
\*-------------------------------------------------------------------------------------*/
std::mutex DeviceListChangeMutex;
std::vector<DeviceListChangeCallback> DeviceListChangeCallbacks;
std::vector<void *> DeviceListChangeCallbackArgs;
/*-------------------------------------------------------------------------------------*\
| Detection Progress, Start, and End Callbacks |
\*-------------------------------------------------------------------------------------*/
std::mutex DetectionProgressMutex;
std::vector<DetectionProgressCallback> DetectionProgressCallbacks;
std::vector<void *> DetectionProgressCallbackArgs;
std::vector<DetectionStartCallback> DetectionStartCallbacks;
std::vector<void *> DetectionStartCallbackArgs;
std::vector<DetectionEndCallback> DetectionEndCallbacks;
std::vector<void *> DetectionEndCallbackArgs;
/*-------------------------------------------------------------------------------------*\
| I2C/SMBus Adapter List Changed Callback |
\*-------------------------------------------------------------------------------------*/
std::mutex I2CBusListChangeMutex;
std::vector<I2CBusListChangeCallback> I2CBusListChangeCallbacks;
std::vector<void *> I2CBusListChangeCallbackArgs;
filesystem::path config_dir;
};