forked from CalcProgrammer1/OpenRGB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResourceManager.cpp
793 lines (664 loc) · 30.3 KB
/
ResourceManager.cpp
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
/*-----------------------------------------*\
| ResourceManager.cpp |
| |
| OpenRGB Resource Manager controls access |
| to application components including |
| RGBControllers, I2C interfaces, and |
| network SDK components |
| |
| Adam Honse (CalcProgrammer1) 9/27/2020 |
\*-----------------------------------------*/
#include "ResourceManager.h"
#include "ProfileManager.h"
#define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING
#include <experimental/filesystem>
#include <stdlib.h>
#include <string>
#include <hidapi/hidapi.h>
std::unique_ptr<ResourceManager> ResourceManager::instance;
using namespace std::chrono_literals;
ResourceManager *ResourceManager::get()
{
if(!instance)
{
instance = std::make_unique<ResourceManager>();
}
return instance.get();
}
ResourceManager::ResourceManager()
{
/*-------------------------------------------------------------------------*\
| Initialize Detection Variables |
\*-------------------------------------------------------------------------*/
detection_enabled = true;
detection_percent = 100;
detection_string = "";
detection_is_required = false;
DetectDevicesThread = nullptr;
/*-------------------------------------------------------------------------*\
| Initialize Server Instance |
\*-------------------------------------------------------------------------*/
server = new NetworkServer(rgb_controllers_hw);
/*-------------------------------------------------------------------------*\
| Load sizes list from file |
\*-------------------------------------------------------------------------*/
profile_manager = new ProfileManager(GetConfigurationDirectory());
rgb_controllers_sizes = profile_manager->LoadProfileToList("sizes.ors");
/*-------------------------------------------------------------------------*\
| Load settings from file |
\*-------------------------------------------------------------------------*/
settings_manager = new SettingsManager();
settings_manager->LoadSettings(GetConfigurationDirectory() + "OpenRGB.json");
}
ResourceManager::~ResourceManager()
{
Cleanup();
}
void ResourceManager::RegisterI2CBus(i2c_smbus_interface *bus)
{
busses.push_back(bus);
}
std::vector<i2c_smbus_interface*> & ResourceManager::GetI2CBusses()
{
return busses;
}
void ResourceManager::RegisterRGBController(RGBController *rgb_controller)
{
rgb_controllers_hw.push_back(rgb_controller);
DeviceListChanged();
}
std::vector<RGBController*> & ResourceManager::GetRGBControllers()
{
return rgb_controllers;
}
void ResourceManager::RegisterI2CBusDetector(I2CBusDetectorFunction detector)
{
i2c_bus_detectors.push_back(detector);
}
void ResourceManager::RegisterI2CDeviceDetector(std::string name, I2CDeviceDetectorFunction detector)
{
i2c_device_detector_strings.push_back(name);
i2c_device_detectors.push_back(detector);
}
void ResourceManager::RegisterDeviceDetector(std::string name, DeviceDetectorFunction detector)
{
device_detector_strings.push_back(name);
device_detectors.push_back(detector);
}
void ResourceManager::RegisterHIDDeviceDetector(std::string name,
HIDDeviceDetectorFunction detector,
uint16_t vid,
uint16_t pid,
int interface,
int usage_page,
int usage)
{
HIDDeviceDetectorBlock block;
block.name = name;
block.address = (vid << 16) | pid;
block.function = detector;
block.interface = interface;
block.usage_page = usage_page;
block.usage = usage;
hid_device_detectors.push_back(block);
hid_device_detector_strings.push_back(name);
}
void ResourceManager::RegisterDeviceListChangeCallback(DeviceListChangeCallback new_callback, void * new_callback_arg)
{
DeviceListChangeCallbacks.push_back(new_callback);
DeviceListChangeCallbackArgs.push_back(new_callback_arg);
}
void ResourceManager::RegisterI2CBusListChangeCallback(I2CBusListChangeCallback new_callback, void * new_callback_arg)
{
I2CBusListChangeCallbacks.push_back(new_callback);
I2CBusListChangeCallbackArgs.push_back(new_callback_arg);
}
void ResourceManager::RegisterDetectionProgressCallback(DetectionProgressCallback new_callback, void *new_callback_arg)
{
DetectionProgressCallbacks.push_back(new_callback);
DetectionProgressCallbackArgs.push_back(new_callback_arg);
}
void ResourceManager::DeviceListChanged()
{
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]);
}
/*-------------------------------------------------*\
| Device list has changed, inform all clients |
| connected to this server |
\*-------------------------------------------------*/
server->DeviceListChanged();
DeviceListChangeMutex.unlock();
}
void ResourceManager::DetectionProgressChanged()
{
DetectionProgressMutex.lock();
/*-------------------------------------------------*\
| Detection progress has changed, call the callbacks|
\*-------------------------------------------------*/
for(unsigned int callback_idx = 0; callback_idx < DetectionProgressCallbacks.size(); callback_idx++)
{
DetectionProgressCallbacks[callback_idx](DetectionProgressCallbackArgs[callback_idx]);
}
DetectionProgressMutex.unlock();
}
void ResourceManager::I2CBusListChanged()
{
I2CBusListChangeMutex.lock();
/*-------------------------------------------------*\
| Detection progress has changed, call the callbacks|
\*-------------------------------------------------*/
for(unsigned int callback_idx = 0; callback_idx < I2CBusListChangeCallbacks.size(); callback_idx++)
{
I2CBusListChangeCallbacks[callback_idx](I2CBusListChangeCallbackArgs[callback_idx]);
}
I2CBusListChangeMutex.unlock();
}
std::string ResourceManager::GetConfigurationDirectory()
{
std::string config_dir = "";
const char* xdg_config_home = getenv("XDG_CONFIG_HOME");
const char* home = getenv("HOME");
const char* appdata = getenv("APPDATA");
/*-----------------------------------------------------*\
| Check both XDG_CONFIG_HOME and APPDATA environment |
| variables. If neither exist, use current directory |
\*-----------------------------------------------------*/
if(xdg_config_home != NULL)
{
config_dir = xdg_config_home;
}
else if(appdata != NULL)
{
config_dir = appdata;
}
else if(home != NULL)
{
config_dir = home;
config_dir = config_dir + "/.config";
}
/*-----------------------------------------------------*\
| If a configuration directory was found, append OpenRGB|
\*-----------------------------------------------------*/
if(config_dir != "")
{
config_dir = config_dir + "/OpenRGB/";
/*-------------------------------------------------------------------------*\
| Create OpenRGB configuration directory if it doesn't exist |
\*-------------------------------------------------------------------------*/
std::experimental::filesystem::create_directories(config_dir);
}
else
{
config_dir = "./";
}
return(config_dir);
}
NetworkServer* ResourceManager::GetServer()
{
return(server);
}
std::vector<NetworkClient*>& ResourceManager::GetClients()
{
return(clients);
}
ProfileManager* ResourceManager::GetProfileManager()
{
return(profile_manager);
}
SettingsManager* ResourceManager::GetSettingsManager()
{
return(settings_manager);
}
unsigned int ResourceManager::GetDetectionPercent()
{
return (detection_percent.load());
}
const char *ResourceManager::GetDetectionString()
{
return (detection_string);
}
void ResourceManager::SetConfigurationDirectory(std::string directory)
{
settings_manager->LoadSettings(directory + "OpenRGB.json");
profile_manager->SetConfigurationDirectory(directory);
}
void ResourceManager::Cleanup()
{
ResourceManager::get()->WaitForDeviceDetection();
std::vector<RGBController *> 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(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;
}
std::vector<i2c_smbus_interface *> busses_copy = busses;
busses.clear();
for(i2c_smbus_interface* bus : busses_copy)
{
delete bus;
}
if(DetectDevicesThread)
{
DetectDevicesThread->join();
delete DetectDevicesThread;
DetectDevicesThread = nullptr;
}
}
void ResourceManager::DetectDevices()
{
if(detection_enabled)
{
/*-------------------------------------------------*\
| Do nothing is it is already detecting devices |
\*-------------------------------------------------*/
if(detection_is_required.load())
{
return;
}
/*-------------------------------------------------*\
| If there's anything left from the last time, |
| we shall remove it first |
\*-------------------------------------------------*/
detection_percent = 0;
detection_string = "";
DetectionProgressChanged();
Cleanup();
DeviceListChanged();
/*-------------------------------------------------*\
| Start the device detection thread |
\*-------------------------------------------------*/
detection_is_required = true;
DetectDevicesThread = new std::thread(&ResourceManager::DetectDevicesThreadFunction, this);
/*-------------------------------------------------*\
| Release the current thread to allow detection |
| thread to start |
\*-------------------------------------------------*/
std::this_thread::sleep_for(1ms);
}
else
{
/*-------------------------------------------------*\
| Signal that detection is complete |
\*-------------------------------------------------*/
detection_percent = 100;
DetectionProgressChanged();
}
}
void ResourceManager::DisableDetection()
{
detection_enabled = false;
}
void ResourceManager::DetectDevicesThreadFunction()
{
DetectDeviceMutex.lock();
hid_device_info* current_hid_device;
float percent = 0.0f;
float percent_denominator = 0.0f;
json detector_settings;
unsigned int hid_device_count = 0;
hid_device_info* hid_devices = NULL;
bool hid_safe_mode = false;
unsigned int prev_count = 0;
bool save_settings = false;
std::vector<bool> size_used;
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 |
\*-------------------------------------------------*/
detector_settings = settings_manager->GetSettings("Detectors");
/*-------------------------------------------------*\
| Check HID safe mode setting |
\*-------------------------------------------------*/
if(detector_settings.contains("hid_safe_mode"))
{
hid_safe_mode = detector_settings["hid_safe_mode"];
}
/*-------------------------------------------------*\
| Calculate the percentage denominator by adding |
| the number of I2C and miscellaneous detectors and |
| the number of enumerated HID devices |
| |
| Start by iterating through all HID devices in |
| list to get a total count |
\*-------------------------------------------------*/
if(!hid_safe_mode)
{
hid_devices = hid_enumerate(0, 0);
}
current_hid_device = hid_devices;
while(current_hid_device)
{
hid_device_count++;
current_hid_device = current_hid_device->next;
}
percent_denominator = i2c_device_detectors.size() + device_detectors.size() + hid_device_count;
/*-------------------------------------------------*\
| Start at 0% detection progress |
\*-------------------------------------------------*/
detection_percent = 0;
/*-------------------------------------------------*\
| Detect i2c busses |
\*-------------------------------------------------*/
for(unsigned int i2c_bus_detector_idx = 0; i2c_bus_detector_idx < i2c_bus_detectors.size() && detection_is_required.load(); i2c_bus_detector_idx++)
{
i2c_bus_detectors[i2c_bus_detector_idx](busses);
I2CBusListChanged();
}
/*-------------------------------------------------*\
| Detect i2c devices |
\*-------------------------------------------------*/
for(unsigned int i2c_detector_idx = 0; i2c_detector_idx < i2c_device_detectors.size() && detection_is_required.load(); i2c_detector_idx++)
{
detection_string = i2c_device_detector_strings[i2c_detector_idx].c_str();
/*-------------------------------------------------*\
| Check if this detector is enabled or needs to be |
| added to the settings list |
\*-------------------------------------------------*/
bool this_device_enabled = true;
if(detector_settings.contains("detectors") && detector_settings["detectors"].contains(detection_string))
{
this_device_enabled = detector_settings["detectors"][detection_string];
}
else
{
detector_settings["detectors"][detection_string] = true;
save_settings = true;
}
if(this_device_enabled)
{
DetectionProgressChanged();
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; controller_size_idx < rgb_controllers_hw.size(); controller_size_idx++)
{
profile_manager->LoadDeviceFromListWithOptions(rgb_controllers_sizes, size_used, rgb_controllers_hw[controller_size_idx], true, false);
}
DeviceListChanged();
}
prev_count = rgb_controllers_hw.size();
/*-------------------------------------------------*\
| Update detection percent |
\*-------------------------------------------------*/
percent = (i2c_detector_idx + 1.0f) / percent_denominator;
detection_percent = percent * 100.0f;
}
/*-------------------------------------------------*\
| Detect HID devices |
| |
| Reset current device pointer to first device |
\*-------------------------------------------------*/
current_hid_device = hid_devices;
/*-------------------------------------------------*\
| Loop through all HID detectors and see if any |
| need to be saved to the settings |
\*-------------------------------------------------*/
for(unsigned int hid_detector_idx = 0; hid_detector_idx < hid_device_detectors.size(); hid_detector_idx++)
{
detection_string = hid_device_detectors[hid_detector_idx].name.c_str();
if(!(detector_settings.contains("detectors") && detector_settings["detectors"].contains(detection_string)))
{
detector_settings["detectors"][detection_string] = true;
save_settings = true;
}
}
if(hid_safe_mode)
{
/*-----------------------------------------------------------------------------*\
| Loop through all available detectors. If all required information matches, |
| run the detector |
\*-----------------------------------------------------------------------------*/
for(unsigned int hid_detector_idx = 0; hid_detector_idx < hid_device_detectors.size() && detection_is_required.load(); hid_detector_idx++)
{
hid_devices = hid_enumerate(hid_device_detectors[hid_detector_idx].address >> 16, hid_device_detectors[hid_detector_idx].address & 0x0000FFFF);
current_hid_device = hid_devices;
while(current_hid_device)
{
unsigned int addr = (current_hid_device->vendor_id << 16) | current_hid_device->product_id;
if(( hid_device_detectors[hid_detector_idx].address == addr )
&& ( ( hid_device_detectors[hid_detector_idx].interface == HID_INTERFACE_ANY )
|| ( hid_device_detectors[hid_detector_idx].interface == current_hid_device->interface_number ) )
#ifdef USE_HID_USAGE
&& ( ( hid_device_detectors[hid_detector_idx].usage_page == HID_USAGE_PAGE_ANY )
|| ( hid_device_detectors[hid_detector_idx].usage_page == current_hid_device->usage_page ) )
&& ( ( hid_device_detectors[hid_detector_idx].usage == HID_USAGE_ANY )
|| ( hid_device_detectors[hid_detector_idx].usage == current_hid_device->usage ) )
#endif
)
{
detection_string = hid_device_detectors[hid_detector_idx].name.c_str();
/*-------------------------------------------------*\
| Check if this detector is enabled or needs to be |
| added to the settings list |
\*-------------------------------------------------*/
bool this_device_enabled = true;
if(detector_settings.contains("detectors") && detector_settings["detectors"].contains(detection_string))
{
this_device_enabled = detector_settings["detectors"][detection_string];
}
if(this_device_enabled)
{
DetectionProgressChanged();
hid_device_detectors[hid_detector_idx].function(current_hid_device, hid_device_detectors[hid_detector_idx].name);
/*-------------------------------------------------*\
| 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; controller_size_idx < rgb_controllers_hw.size(); controller_size_idx++)
{
profile_manager->LoadDeviceFromListWithOptions(rgb_controllers_sizes, size_used, rgb_controllers_hw[controller_size_idx], true, false);
}
DeviceListChanged();
}
prev_count = rgb_controllers_hw.size();
}
}
current_hid_device = current_hid_device->next;
}
hid_free_enumeration(hid_devices);
}
}
else
{
/*-------------------------------------------------*\
| Iterate through all devices in list and run |
| detectors |
\*-------------------------------------------------*/
hid_device_count = 0;
while(current_hid_device)
{
detection_string = "";
DetectionProgressChanged();
unsigned int addr = (current_hid_device->vendor_id << 16) | current_hid_device->product_id;
/*-----------------------------------------------------------------------------*\
| Loop through all available detectors. If all required information matches, |
| run the detector |
\*-----------------------------------------------------------------------------*/
for(unsigned int hid_detector_idx = 0; hid_detector_idx < hid_device_detectors.size() && detection_is_required.load(); hid_detector_idx++)
{
if(( hid_device_detectors[hid_detector_idx].address == addr )
&& ( ( hid_device_detectors[hid_detector_idx].interface == HID_INTERFACE_ANY )
|| ( hid_device_detectors[hid_detector_idx].interface == current_hid_device->interface_number ) )
#ifdef USE_HID_USAGE
&& ( ( hid_device_detectors[hid_detector_idx].usage_page == HID_USAGE_PAGE_ANY )
|| ( hid_device_detectors[hid_detector_idx].usage_page == current_hid_device->usage_page ) )
&& ( ( hid_device_detectors[hid_detector_idx].usage == HID_USAGE_ANY )
|| ( hid_device_detectors[hid_detector_idx].usage == current_hid_device->usage ) )
#endif
)
{
detection_string = hid_device_detectors[hid_detector_idx].name.c_str();
/*-------------------------------------------------*\
| Check if this detector is enabled or needs to be |
| added to the settings list |
\*-------------------------------------------------*/
bool this_device_enabled = true;
if(detector_settings.contains("detectors") && detector_settings["detectors"].contains(detection_string))
{
this_device_enabled = detector_settings["detectors"][detection_string];
}
if(this_device_enabled)
{
DetectionProgressChanged();
hid_device_detectors[hid_detector_idx].function(current_hid_device, hid_device_detectors[hid_detector_idx].name);
}
}
}
/*-------------------------------------------------*\
| Update detection percent |
\*-------------------------------------------------*/
hid_device_count++;
percent = (i2c_device_detectors.size() + hid_device_count) / percent_denominator;
detection_percent = percent * 100.0f;
/*-------------------------------------------------*\
| Move on to the next HID device |
\*-------------------------------------------------*/
current_hid_device = current_hid_device->next;
}
/*-------------------------------------------------*\
| Done using the device list, free it |
\*-------------------------------------------------*/
hid_free_enumeration(hid_devices);
}
/*-------------------------------------------------*\
| Detect other devices |
\*-------------------------------------------------*/
for(unsigned int detector_idx = 0; detector_idx < device_detectors.size() && detection_is_required.load(); detector_idx++)
{
detection_string = device_detector_strings[detector_idx].c_str();
/*-------------------------------------------------*\
| Check if this detector is enabled or needs to be |
| added to the settings list |
\*-------------------------------------------------*/
bool this_device_enabled = true;
if(detector_settings.contains("detectors") && detector_settings["detectors"].contains(detection_string))
{
this_device_enabled = detector_settings["detectors"][detection_string];
}
else
{
detector_settings["detectors"][detection_string] = true;
save_settings = true;
}
if(this_device_enabled)
{
DetectionProgressChanged();
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; controller_size_idx < rgb_controllers_hw.size(); controller_size_idx++)
{
profile_manager->LoadDeviceFromListWithOptions(rgb_controllers_sizes, size_used, rgb_controllers_hw[controller_size_idx], true, false);
}
DeviceListChanged();
}
prev_count = rgb_controllers_hw.size();
/*-------------------------------------------------*\
| Update detection percent |
\*-------------------------------------------------*/
percent = (i2c_device_detectors.size() + hid_device_count + detector_idx + 1.0f) / percent_denominator;
detection_percent = percent * 100.0f;
}
/*-------------------------------------------------*\
| Make sure that when the detection is done, |
| progress bar is set to 100% |
\*-------------------------------------------------*/
detection_is_required = false;
detection_percent = 100;
detection_string = "";
DetectionProgressChanged();
DetectDeviceMutex.unlock();
if(save_settings)
{
settings_manager->SetSettings("Detectors", detector_settings);
settings_manager->SaveSettings();
}
}
void ResourceManager::StopDeviceDetection()
{
detection_is_required = false;
detection_percent = 100;
detection_string = "Stopping";
}
void ResourceManager::WaitForDeviceDetection()
{
DetectDeviceMutex.lock();
DetectDeviceMutex.unlock();
}