-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
ResourceManager.cpp
1856 lines (1553 loc) · 73.8 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
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*---------------------------------------------------------*\
| ResourceManager.cpp |
| |
| 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 |
\*---------------------------------------------------------*/
#ifdef _WIN32
#include <codecvt>
#include <locale>
#endif
#include <stdlib.h>
#include <string>
#include <hidapi.h>
#include "cli.h"
#include "pci_ids/pci_ids.h"
#include "ResourceManager.h"
#include "ProfileManager.h"
#include "LogManager.h"
#include "SettingsManager.h"
#include "NetworkClient.h"
#include "NetworkServer.h"
#include "filesystem.h"
#include "StringUtils.h"
const hidapi_wrapper default_wrapper =
{
NULL,
(hidapi_wrapper_send_feature_report) hid_send_feature_report,
(hidapi_wrapper_get_feature_report) hid_get_feature_report,
(hidapi_wrapper_get_serial_number_string) hid_get_serial_number_string,
(hidapi_wrapper_open_path) hid_open_path,
(hidapi_wrapper_enumerate) hid_enumerate,
(hidapi_wrapper_free_enumeration) hid_free_enumeration,
(hidapi_wrapper_close) hid_close,
(hidapi_wrapper_error) hid_error
};
bool BasicHIDBlock::compare(hid_device_info* info)
{
return ( (vid == info->vendor_id)
&& (pid == info->product_id)
#ifdef USE_HID_USAGE
&& ( (usage_page == HID_USAGE_PAGE_ANY)
|| (usage_page == info->usage_page) )
&& ( (usage == HID_USAGE_ANY)
|| (usage == info->usage) )
&& ( (interface == HID_INTERFACE_ANY)
|| (interface == info->interface_number ) )
#else
&& ( (interface == HID_INTERFACE_ANY)
|| (interface == info->interface_number ) )
#endif
);
}
ResourceManager* ResourceManager::instance;
using namespace std::chrono_literals;
ResourceManager *ResourceManager::get()
{
if(!instance)
{
instance = new ResourceManager();
}
return instance;
}
ResourceManager::ResourceManager()
{
/*-------------------------------------------------------------------------*\
| Initialize Detection Variables |
\*-------------------------------------------------------------------------*/
detection_enabled = true;
detection_percent = 100;
detection_string = "";
detection_is_required = false;
InitThread = nullptr;
DetectDevicesThread = nullptr;
dynamic_detectors_processed = false;
init_finished = false;
SetupConfigurationDirectory();
/*-------------------------------------------------------------------------*\
| Load settings from file |
\*-------------------------------------------------------------------------*/
settings_manager = new SettingsManager();
settings_manager->LoadSettings(GetConfigurationDirectory() / "OpenRGB.json");
/*-------------------------------------------------------------------------*\
| Configure the log manager |
\*-------------------------------------------------------------------------*/
LogManager::get()->configure(settings_manager->GetSettings("LogManager"), GetConfigurationDirectory());
/*-------------------------------------------------------------------------*\
| Initialize Server Instance |
| If configured, pass through full controller list including clients |
| Otherwise, pass only local hardware controllers |
\*-------------------------------------------------------------------------*/
json server_settings = settings_manager->GetSettings("Server");
bool all_controllers = false;
if(server_settings.contains("all_controllers"))
{
all_controllers = server_settings["all_controllers"];
}
if(all_controllers)
{
server = new NetworkServer(rgb_controllers);
}
else
{
server = new NetworkServer(rgb_controllers_hw);
}
/*-------------------------------------------------------------------------*\
| Initialize Saved Client Connections |
\*-------------------------------------------------------------------------*/
json client_settings = settings_manager->GetSettings("Client");
if(client_settings.contains("clients"))
{
for(unsigned int client_idx = 0; client_idx < client_settings["clients"].size(); client_idx++)
{
NetworkClient * client = new NetworkClient(rgb_controllers);
std::string titleString = "OpenRGB ";
titleString.append(VERSION_STRING);
std::string client_ip = client_settings["clients"][client_idx]["ip"];
unsigned short client_port = client_settings["clients"][client_idx]["port"];
client->SetIP(client_ip.c_str());
client->SetName(titleString.c_str());
client->SetPort(client_port);
client->StartClient();
for(int timeout = 0; timeout < 100; timeout++)
{
if(client->GetConnected())
{
break;
}
std::this_thread::sleep_for(10ms);
}
clients.push_back(client);
}
}
/*-------------------------------------------------------------------------*\
| Load sizes list from file |
\*-------------------------------------------------------------------------*/
profile_manager = new ProfileManager(GetConfigurationDirectory());
server->SetProfileManager(profile_manager);
rgb_controllers_sizes = profile_manager->LoadProfileToList("sizes", true);
}
ResourceManager::~ResourceManager()
{
Cleanup();
if(InitThread)
{
DetectDevicesThread->join();
delete DetectDevicesThread;
DetectDevicesThread = nullptr;
}
}
void ResourceManager::RegisterI2CBus(i2c_smbus_interface *bus)
{
LOG_INFO("Registering I2C interface: %s Device %04X:%04X Subsystem: %04X:%04X", bus->device_name, bus->pci_vendor, bus->pci_device,bus->pci_subsystem_vendor,bus->pci_subsystem_device);
busses.push_back(bus);
}
std::vector<i2c_smbus_interface*> & ResourceManager::GetI2CBusses()
{
return busses;
}
void ResourceManager::RegisterRGBController(RGBController *rgb_controller)
{
LOG_INFO("[%s] Registering RGB controller", rgb_controller->name.c_str());
rgb_controllers_hw.push_back(rgb_controller);
/*-------------------------------------------------*\
| If the device list size has changed, call the |
| device list changed callbacks |
| |
| TODO: If all detection is reworked to use |
| RegisterRGBController, tracking of previous list |
| size can be removed and profile can be loaded per |
| controller before adding to list |
\*-------------------------------------------------*/
if(rgb_controllers_hw.size() != detection_prev_size)
{
/*-------------------------------------------------*\
| First, load sizes for the new controllers |
\*-------------------------------------------------*/
for(unsigned int controller_size_idx = detection_prev_size; controller_size_idx < rgb_controllers_hw.size(); controller_size_idx++)
{
profile_manager->LoadDeviceFromListWithOptions(rgb_controllers_sizes, detection_size_entry_used, rgb_controllers_hw[controller_size_idx], true, false);
}
UpdateDeviceList();
}
detection_prev_size = (unsigned int)rgb_controllers_hw.size();
UpdateDeviceList();
}
void ResourceManager::UnregisterRGBController(RGBController* rgb_controller)
{
LOG_INFO("[%s] Unregistering RGB controller", rgb_controller->name.c_str());
/*-------------------------------------------------------------------------*\
| Clear callbacks from the controller before removal |
\*-------------------------------------------------------------------------*/
rgb_controller->ClearCallbacks();
/*-------------------------------------------------------------------------*\
| Find the controller to remove and remove it from the hardware list |
\*-------------------------------------------------------------------------*/
std::vector<RGBController*>::iterator hw_it = std::find(rgb_controllers_hw.begin(), rgb_controllers_hw.end(), rgb_controller);
if (hw_it != rgb_controllers_hw.end())
{
rgb_controllers_hw.erase(hw_it);
}
/*-------------------------------------------------------------------------*\
| Find the controller to remove and remove it from the master list |
\*-------------------------------------------------------------------------*/
std::vector<RGBController*>::iterator rgb_it = std::find(rgb_controllers.begin(), rgb_controllers.end(), rgb_controller);
if (rgb_it != rgb_controllers.end())
{
rgb_controllers.erase(rgb_it);
}
UpdateDeviceList();
}
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::RegisterI2CDIMMDeviceDetector(std::string name, I2CDIMMDeviceDetectorFunction detector, uint16_t jedec_id, uint8_t dimm_type)
{
I2CDIMMDeviceDetectorBlock block;
block.name = name;
block.function = detector;
block.jedec_id = jedec_id;
block.dimm_type = dimm_type;
i2c_dimm_device_detectors.push_back(block);
}
void ResourceManager::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)
{
I2CPCIDeviceDetectorBlock block;
block.name = name;
block.function = detector;
block.ven_id = ven_id;
block.dev_id = dev_id;
block.subven_id = subven_id;
block.subdev_id = subdev_id;
block.i2c_addr = i2c_addr;
i2c_pci_device_detectors.push_back(block);
}
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.vid = vid;
block.pid = pid;
block.function = detector;
block.interface = interface;
block.usage_page = usage_page;
block.usage = usage;
hid_device_detectors.push_back(block);
}
void ResourceManager::RegisterHIDWrappedDeviceDetector(std::string name,
HIDWrappedDeviceDetectorFunction detector,
uint16_t vid,
uint16_t pid,
int interface,
int usage_page,
int usage)
{
HIDWrappedDeviceDetectorBlock block;
block.name = name;
block.vid = vid;
block.pid = pid;
block.function = detector;
block.interface = interface;
block.usage_page = usage_page;
block.usage = usage;
hid_wrapped_device_detectors.push_back(block);
}
void ResourceManager::RegisterDynamicDetector(std::string name, DynamicDetectorFunction detector)
{
dynamic_detector_strings.push_back(name);
dynamic_detectors.push_back(detector);
}
void ResourceManager::RegisterPreDetectionHook(PreDetectionHookFunction hook)
{
pre_detection_hooks.push_back(hook);
}
void ResourceManager::RegisterDeviceListChangeCallback(DeviceListChangeCallback new_callback, void * new_callback_arg)
{
DeviceListChangeCallbacks.push_back(new_callback);
DeviceListChangeCallbackArgs.push_back(new_callback_arg);
LOG_TRACE("[ResourceManager] Registered device list change callback. Total callbacks registered: %d", DeviceListChangeCallbacks.size());
}
void ResourceManager::UnregisterDeviceListChangeCallback(DeviceListChangeCallback callback, void * callback_arg)
{
for(size_t idx = 0; idx < DeviceListChangeCallbacks.size(); idx++)
{
if(DeviceListChangeCallbacks[idx] == callback && DeviceListChangeCallbackArgs[idx] == callback_arg)
{
DeviceListChangeCallbacks.erase(DeviceListChangeCallbacks.begin() + idx);
DeviceListChangeCallbackArgs.erase(DeviceListChangeCallbackArgs.begin() + idx);
}
}
LOG_TRACE("[ResourceManager] Unregistered device list change callback. Total callbacks registered: %d", DeviceListChangeCallbacks.size());
}
void ResourceManager::RegisterI2CBusListChangeCallback(I2CBusListChangeCallback new_callback, void * new_callback_arg)
{
I2CBusListChangeCallbacks.push_back(new_callback);
I2CBusListChangeCallbackArgs.push_back(new_callback_arg);
}
void ResourceManager::UnregisterI2CBusListChangeCallback(I2CBusListChangeCallback callback, void * callback_arg)
{
for(size_t idx = 0; idx < I2CBusListChangeCallbacks.size(); idx++)
{
if(I2CBusListChangeCallbacks[idx] == callback && I2CBusListChangeCallbackArgs[idx] == callback_arg)
{
I2CBusListChangeCallbacks.erase(I2CBusListChangeCallbacks.begin() + idx);
I2CBusListChangeCallbackArgs.erase(I2CBusListChangeCallbackArgs.begin() + idx);
}
}
}
void ResourceManager::RegisterDetectionProgressCallback(DetectionProgressCallback new_callback, void *new_callback_arg)
{
DetectionProgressCallbacks.push_back(new_callback);
DetectionProgressCallbackArgs.push_back(new_callback_arg);
LOG_TRACE("[ResourceManager] Registered detection progress callback. Total callbacks registered: %d", DetectionProgressCallbacks.size());
}
void ResourceManager::UnregisterDetectionProgressCallback(DetectionProgressCallback callback, void *callback_arg)
{
for(size_t idx = 0; idx < DetectionProgressCallbacks.size(); idx++)
{
if(DetectionProgressCallbacks[idx] == callback && DetectionProgressCallbackArgs[idx] == callback_arg)
{
DetectionProgressCallbacks.erase(DetectionProgressCallbacks.begin() + idx);
DetectionProgressCallbackArgs.erase(DetectionProgressCallbackArgs.begin() + idx);
}
}
LOG_TRACE("[ResourceManager] Unregistered detection progress callback. Total callbacks registered: %d", DetectionProgressCallbacks.size());
}
void ResourceManager::RegisterDetectionStartCallback(DetectionStartCallback new_callback, void *new_callback_arg)
{
DetectionStartCallbacks.push_back(new_callback);
DetectionStartCallbackArgs.push_back(new_callback_arg);
}
void ResourceManager::UnregisterDetectionStartCallback(DetectionStartCallback callback, void *callback_arg)
{
for(size_t idx = 0; idx < DetectionStartCallbacks.size(); idx++)
{
if(DetectionStartCallbacks[idx] == callback && DetectionStartCallbackArgs[idx] == callback_arg)
{
DetectionStartCallbacks.erase(DetectionStartCallbacks.begin() + idx);
DetectionStartCallbackArgs.erase(DetectionStartCallbackArgs.begin() + idx);
}
}
}
void ResourceManager::RegisterDetectionEndCallback(DetectionEndCallback new_callback, void *new_callback_arg)
{
DetectionEndCallbacks.push_back(new_callback);
DetectionEndCallbackArgs.push_back(new_callback_arg);
}
void ResourceManager::UnregisterDetectionEndCallback(DetectionEndCallback callback, void *callback_arg)
{
for(size_t idx = 0; idx < DetectionEndCallbacks.size(); idx++)
{
if(DetectionEndCallbacks[idx] == callback && DetectionEndCallbackArgs[idx] == callback_arg)
{
DetectionEndCallbacks.erase(DetectionEndCallbacks.begin() + idx);
DetectionEndCallbackArgs.erase(DetectionEndCallbackArgs.begin() + idx);
}
}
}
void ResourceManager::UpdateDeviceList()
{
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 |
\*-------------------------------------------------*/
DeviceListChanged();
/*-------------------------------------------------*\
| Device list has changed, inform all clients |
| connected to this server |
\*-------------------------------------------------*/
server->DeviceListChanged();
DeviceListChangeMutex.unlock();
}
void ResourceManager::DeviceListChanged()
{
/*-------------------------------------------------*\
| Device list has changed, call the callbacks |
\*-------------------------------------------------*/
LOG_TRACE("[ResourceManager] Calling device list change callbacks.");
for(std::size_t callback_idx = 0; callback_idx < (unsigned int)DeviceListChangeCallbacks.size(); callback_idx++)
{
ResourceManager::DeviceListChangeCallbacks[callback_idx](DeviceListChangeCallbackArgs[callback_idx]);
}
}
void ResourceManager::DetectionProgressChanged()
{
DetectionProgressMutex.lock();
/*-------------------------------------------------*\
| Detection progress has changed, call the callbacks|
\*-------------------------------------------------*/
LOG_TRACE("[ResourceManager] Calling detection progress callbacks.");
for(std::size_t callback_idx = 0; callback_idx < (unsigned int)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(std::size_t callback_idx = 0; callback_idx < (unsigned int)I2CBusListChangeCallbacks.size(); callback_idx++)
{
I2CBusListChangeCallbacks[callback_idx](I2CBusListChangeCallbackArgs[callback_idx]);
}
I2CBusListChangeMutex.unlock();
}
void ResourceManager::SetupConfigurationDirectory()
{
config_dir.clear();
#ifdef _WIN32
const wchar_t* appdata = _wgetenv(L"APPDATA");
if(appdata != NULL)
{
config_dir = appdata;
}
#else
const char* xdg_config_home = getenv("XDG_CONFIG_HOME");
const char* home = getenv("HOME");
/*-----------------------------------------------------*\
| 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(home != NULL)
{
config_dir = home;
config_dir /= ".config";
}
#endif
/*-----------------------------------------------------*\
| If a configuration directory was found, append OpenRGB|
\*-----------------------------------------------------*/
if(config_dir != "")
{
config_dir.append("OpenRGB");
/*-------------------------------------------------------------------------*\
| Create OpenRGB configuration directory if it doesn't exist |
\*-------------------------------------------------------------------------*/
filesystem::create_directories(config_dir);
}
else
{
config_dir = "./";
}
}
filesystem::path ResourceManager::GetConfigurationDirectory()
{
return(config_dir);
}
void ResourceManager::SetConfigurationDirectory(const filesystem::path &directory)
{
config_dir = directory;
settings_manager->LoadSettings(directory / "OpenRGB.json");
profile_manager->SetConfigurationDirectory(directory);
rgb_controllers_sizes.clear();
rgb_controllers_sizes = profile_manager->LoadProfileToList("sizes", true);
}
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<NetworkClient*>::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();
}
/******************************************************************************************\
* *
* AttemptLocalConnection *
* *
* Attempts an SDK connection to the local server. Returns true if success *
* *
\******************************************************************************************/
bool ResourceManager::AttemptLocalConnection()
{
detection_percent = 0;
detection_string = "Attempting local server connection...";
DetectionProgressChanged();
LOG_DEBUG("[ResourceManager] Attempting server connection...");
bool success = false;
NetworkClient * client = new NetworkClient(ResourceManager::get()->GetRGBControllers());
std::string titleString = "OpenRGB ";
titleString.append(VERSION_STRING);
client->SetName(titleString.c_str());
client->StartClient();
for(int timeout = 0; timeout < 10; timeout++)
{
if(client->GetConnected())
{
break;
}
std::this_thread::sleep_for(5ms);
}
if(!client->GetConnected())
{
LOG_TRACE("[main] Client failed to connect");
client->StopClient();
LOG_TRACE("[main] Client stopped");
delete client;
client = NULL;
}
else
{
ResourceManager::get()->RegisterNetworkClient(client);
LOG_TRACE("[main] Registered network client");
success = true;
/*-----------------------------------------------------*\
| Wait up to 5 seconds for the client connection to |
| retrieve all controllers |
\*-----------------------------------------------------*/
for(int timeout = 0; timeout < 1000; timeout++)
{
if(client->GetOnline())
{
break;
}
std::this_thread::sleep_for(5ms);
}
}
return success;
}
std::vector<NetworkClient*>& ResourceManager::GetClients()
{
return(clients);
}
ProfileManager* ResourceManager::GetProfileManager()
{
return(profile_manager);
}
SettingsManager* ResourceManager::GetSettingsManager()
{
return(settings_manager);
}
bool ResourceManager::GetDetectionEnabled()
{
return(detection_enabled);
}
unsigned int ResourceManager::GetDetectionPercent()
{
return (detection_percent.load());
}
const char *ResourceManager::GetDetectionString()
{
return (detection_string);
}
void ResourceManager::Cleanup()
{
ResourceManager::get()->WaitForDeviceDetection();
std::vector<RGBController *> rgb_controllers_hw_copy = rgb_controllers_hw;
for(std::size_t hw_controller_idx = 0; hw_controller_idx < rgb_controllers_hw.size(); hw_controller_idx++)
{
for(std::size_t 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;
}
}
}
/*-------------------------------------------------*\
| Clear the hardware controllers list and set the |
| previous hardware controllers list size to zero |
\*-------------------------------------------------*/
rgb_controllers_hw.clear();
detection_prev_size = 0;
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;
}
/*-------------------------------------------------*\
| Cleanup HID interface |
\*-------------------------------------------------*/
int hid_status = hid_exit();
LOG_DEBUG("Closing HID interfaces: %s", ((hid_status == 0) ? "Success" : "Failed"));
if(DetectDevicesThread)
{
DetectDevicesThread->join();
delete DetectDevicesThread;
DetectDevicesThread = nullptr;
}
}
void ResourceManager::ProcessPreDetectionHooks()
{
for(std::size_t hook_idx = 0; hook_idx < pre_detection_hooks.size(); hook_idx++)
{
pre_detection_hooks[hook_idx]();
}
}
void ResourceManager::ProcessDynamicDetectors()
{
for(std::size_t detector_idx = 0; detector_idx < dynamic_detectors.size(); detector_idx++)
{
dynamic_detectors[detector_idx]();
}
dynamic_detectors_processed = true;
}
/*-----------------------------------------------------*\
| Handle ALL pre-detection routines |
| The system should be ready to start a detection thread|
| (returns false if detection can not proceed) |
\*-----------------------------------------------------*/
bool ResourceManager::ProcessPreDetection()
{
/*-----------------------------------------------------*\
| Process pre-detection hooks |
\*-----------------------------------------------------*/
ProcessPreDetectionHooks();
/*-----------------------------------------------------*\
| Process Dynamic Detectors |
\*-----------------------------------------------------*/
if(!dynamic_detectors_processed)
{
ProcessDynamicDetectors();
}
/*-----------------------------------------------------*\
| Call detection start callbacks |
\*-----------------------------------------------------*/
LOG_TRACE("[ResourceManager] Calling detection start callbacks.");
for(std::size_t callback_idx = 0; callback_idx < DetectionStartCallbacks.size(); callback_idx++)
{
DetectionStartCallbacks[callback_idx](DetectionStartCallbackArgs[callback_idx]);
}
/*-----------------------------------------------------*\
| Update the detector settings |
\*-----------------------------------------------------*/
UpdateDetectorSettings();
if(detection_enabled)
{
/*-------------------------------------------------*\
| Do nothing is it is already detecting devices |
\*-------------------------------------------------*/
if(detection_is_required.load())
{
return false;
}
/*-------------------------------------------------*\
| If there's anything left from the last time, |
| we shall remove it first |
\*-------------------------------------------------*/
detection_percent = 0;
detection_string = "";
DetectionProgressChanged();
Cleanup();
UpdateDeviceList();
/*-------------------------------------------------*\
| Initialize HID interface for detection |
\*-------------------------------------------------*/
int hid_status = hid_init();
LOG_INFO("Initializing HID interfaces: %s", ((hid_status == 0) ? "Success" : "Failed"));
/*-------------------------------------------------*\
| Start the device detection thread |
\*-------------------------------------------------*/
detection_is_required = true;
return true;
}
return false;
}
void ResourceManager::DetectDevices()
{
if(ProcessPreDetection())
{
DetectDevicesThread = new std::thread(&ResourceManager::DetectDevicesThreadFunction, this);
/*-------------------------------------------------*\
| Release the current thread to allow detection |
| thread to start |
\*-------------------------------------------------*/
std::this_thread::sleep_for(1ms);
}
if(!detection_enabled)
{
ProcessPostDetection();
}
}
void ResourceManager::ProcessPostDetection()
{
/*-------------------------------------------------*\
| Signal that detection is complete |
\*-------------------------------------------------*/
detection_percent = 100;
DetectionProgressChanged();
LOG_INFO("[ResourceManager] Calling Post-detection callbacks");
/*-----------------------------------------------------*\
| Call detection end callbacks |
\*-----------------------------------------------------*/
for(std::size_t callback_idx = 0; callback_idx < DetectionEndCallbacks.size(); callback_idx++)
{
DetectionEndCallbacks[callback_idx](DetectionEndCallbackArgs[callback_idx]);
}
detection_is_required = false;
LOG_INFO("------------------------------------------------------");
LOG_INFO("| Detection completed |");
LOG_INFO("------------------------------------------------------");
}
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;
LOG_INFO("------------------------------------------------------");
LOG_INFO("| Start device detection |");
LOG_INFO("------------------------------------------------------");
/*-------------------------------------------------*\
| Reset the size entry used flags vector |
\*-------------------------------------------------*/
detection_size_entry_used.resize(rgb_controllers_sizes.size());
for(std::size_t size_idx = 0; size_idx < (unsigned int)detection_size_entry_used.size(); size_idx++)
{
detection_size_entry_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"];