-
-
Notifications
You must be signed in to change notification settings - Fork 391
Expand file tree
/
Copy pathOpenRGBPluginAPI.cpp
More file actions
329 lines (272 loc) · 11.7 KB
/
Copy pathOpenRGBPluginAPI.cpp
File metadata and controls
329 lines (272 loc) · 11.7 KB
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
/*---------------------------------------------------------*\
| OpenRGBPluginAPI.cpp |
| |
| Interface for OpenRGB plugins to call OpenRGB functions |
| |
| Adam Honse (CalcProgrammer1) 08 Feb 2026 |
| |
| This file is part of the OpenRGB project |
| SPDX-License-Identifier: GPL-2.0-or-later |
\*---------------------------------------------------------*/
#include "OpenRGBPluginAPI.h"
#include "RGBController_Dummy.h"
#include "RGBController_Virtual.h"
OpenRGBPluginAPI::OpenRGBPluginAPI()
{
log_manager = ResourceManager::get()->GetLogManager();
plugin_manager = ResourceManager::get()->GetPluginManager();
profile_manager = ResourceManager::get()->GetProfileManager();
resource_manager = ResourceManager::get();
settings_manager = ResourceManager::get()->GetSettingsManager();
}
/*---------------------------------------------------------*\
| LogManager APIs |
\*---------------------------------------------------------*/
void OpenRGBPluginAPI::LogEntry(const char* filename, int line, unsigned int level, const char* fmt, ...)
{
va_list va;
va_start(va, fmt);
log_manager->LogEntry_va(filename, line, level, fmt, va);
va_end(va);
}
/*---------------------------------------------------------*\
| PluginManager APIs |
\*---------------------------------------------------------*/
RGBControllerInterface* OpenRGBPluginAPI::CreateVirtualRGBController(RGBController_Setup* setup)
{
RGBController_Virtual* rgb_controller = new RGBController_Virtual(setup);
/*-----------------------------------------------------*\
| Add the new controller to the list of created |
| controllers |
\*-----------------------------------------------------*/
created_controllers.push_back((RGBController*)rgb_controller);
return(rgb_controller);
}
static void CallRegisterVirtualRGBController(OpenRGBPluginAPI* this_ptr, RGBControllerInterface* rgb_controller)
{
this_ptr->RegisterVirtualRGBController(rgb_controller);
}
void OpenRGBPluginAPI::RegisterVirtualRGBControllerInThread(RGBControllerInterface* rgb_controller)
{
/*-----------------------------------------------------*\
| To avoid deadlocks if this is called from a UI thread |
| run the register operation in a background thread. |
\*-----------------------------------------------------*/
std::thread register_thread(CallRegisterVirtualRGBController, this, rgb_controller);
register_thread.detach();
}
void OpenRGBPluginAPI::RegisterVirtualRGBController(RGBControllerInterface* rgb_controller)
{
LOG_INFO("[PluginManager] Registering RGB controller %s", rgb_controller->GetName().c_str());
/*-----------------------------------------------------*\
| Ensure the pointer given is a pointer to a valid |
| virtual controller |
\*-----------------------------------------------------*/
bool found = false;
for(std::size_t controller_idx = 0; controller_idx < created_controllers.size(); controller_idx++)
{
if(created_controllers[controller_idx] == rgb_controller)
{
found = true;
break;
}
}
if(!found)
{
LOG_ERROR("[PluginManager] Attempted to register an RGBController that was not created by this plugin API instance.");
return;
}
/*-----------------------------------------------------*\
| Mark this controller as locally owned |
\*-----------------------------------------------------*/
((RGBController*)rgb_controller)->flags &= ~CONTROLLER_FLAG_REMOTE;
((RGBController*)rgb_controller)->flags |= CONTROLLER_FLAG_LOCAL;
/*-----------------------------------------------------*\
| Add the new controller to the list |
\*-----------------------------------------------------*/
rgb_controllers.push_back((RGBController*)rgb_controller);
/*-----------------------------------------------------*\
| Signal device list update in ResourceManager |
\*-----------------------------------------------------*/
ResourceManager::get()->UpdateDeviceList();
}
static void CallUnregisterVirtualRGBController(OpenRGBPluginAPI* this_ptr, RGBControllerInterface* rgb_controller)
{
this_ptr->UnregisterVirtualRGBController(rgb_controller);
}
void OpenRGBPluginAPI::UnregisterVirtualRGBController(RGBControllerInterface* rgb_controller)
{
LOG_INFO("[PluginManager] Unregistering RGB controller %s", rgb_controller->GetName().c_str());
/*-----------------------------------------------------*\
| Clear callbacks from the controller before removal |
\*-----------------------------------------------------*/
rgb_controller->ClearCallbacks();
/*-----------------------------------------------------*\
| 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(), (RGBController*)rgb_controller);
if(rgb_it != rgb_controllers.end())
{
rgb_controllers.erase(rgb_it);
}
/*-----------------------------------------------------*\
| Signal device list update in ResourceManager |
\*-----------------------------------------------------*/
ResourceManager::get()->UpdateDeviceList();
}
void OpenRGBPluginAPI::UnregisterVirtualRGBControllerInThread(RGBControllerInterface* rgb_controller)
{
/*-----------------------------------------------------*\
| To avoid deadlocks if this is called from a UI thread |
| run the unregister operation in a background thread. |
\*-----------------------------------------------------*/
std::thread unregister_thread(CallUnregisterVirtualRGBController, this, rgb_controller);
unregister_thread.detach();
}
void OpenRGBPluginAPI::UpdateVirtualRGBController(RGBControllerInterface* rgb_controller, RGBController_Setup* setup)
{
if(rgb_controller)
{
((RGBController_Virtual*)rgb_controller)->UpdateVirtualController(setup);
}
}
void OpenRGBPluginAPI::DeleteVirtualRGBController(RGBControllerInterface* rgb_controller)
{
/*-----------------------------------------------------*\
| Remove the controller from the list of created |
| controllers |
\*-----------------------------------------------------*/
created_controllers.erase(std::remove(created_controllers.begin(), created_controllers.end(), (RGBController*)rgb_controller), created_controllers.end());
delete (RGBController*)rgb_controller;
}
/*---------------------------------------------------------*\
| ProfileManager APIs |
\*---------------------------------------------------------*/
void OpenRGBPluginAPI::ClearActiveProfile()
{
profile_manager->ClearActiveProfile();
}
std::vector<std::string> OpenRGBPluginAPI::GetProfileList()
{
return(profile_manager->GetProfileList());
}
bool OpenRGBPluginAPI::LoadProfile(std::string profile_name)
{
return(profile_manager->LoadProfile(profile_name));
}
bool OpenRGBPluginAPI::SaveProfileFromPlugin(std::string profile_name, std::string plugin_name, nlohmann::json plugin_data)
{
return(profile_manager->SaveProfileFromPlugin(profile_name, plugin_name, plugin_data));
}
/*---------------------------------------------------------*\
| ResourceManager APIs |
\*---------------------------------------------------------*/
filesystem::path OpenRGBPluginAPI::GetConfigurationDirectory()
{
return(resource_manager->GetConfigurationDirectory());
}
bool OpenRGBPluginAPI::GetDetectionEnabled()
{
return(resource_manager->GetDetectionEnabled());
}
unsigned int OpenRGBPluginAPI::GetDetectionPercent()
{
return(resource_manager->GetDetectionPercent());
}
std::string OpenRGBPluginAPI::GetDetectionString()
{
return(resource_manager->GetDetectionString());
}
void OpenRGBPluginAPI::RescanDevices()
{
resource_manager->RescanDevices();
}
void OpenRGBPluginAPI::WaitForDetection()
{
resource_manager->WaitForDetection();
}
std::vector<RGBControllerInterface*> OpenRGBPluginAPI::GetRGBControllers()
{
return(resource_manager->GetRGBControllerInterfaces());
}
/*---------------------------------------------------------*\
| RGBController APIs |
\*---------------------------------------------------------*/
nlohmann::json OpenRGBPluginAPI::GetDeviceDescriptionJSON(RGBControllerInterface* controller)
{
return(RGBController::GetDeviceDescriptionJSON((RGBController*)controller));
}
nlohmann::json OpenRGBPluginAPI::GetLEDDescriptionJSON(led led)
{
return(RGBController::GetLEDDescriptionJSON(led));
}
nlohmann::json OpenRGBPluginAPI::GetMatrixMapDescriptionJSON(matrix_map_type matrix_map)
{
return(RGBController::GetMatrixMapDescriptionJSON(matrix_map));
}
nlohmann::json OpenRGBPluginAPI::GetModeDescriptionJSON(mode mode)
{
return(RGBController::GetModeDescriptionJSON(mode));
}
nlohmann::json OpenRGBPluginAPI::GetSegmentDescriptionJSON(segment segment)
{
return(RGBController::GetSegmentDescriptionJSON(segment));
}
nlohmann::json OpenRGBPluginAPI::GetZoneDescriptionJSON(zone zone)
{
return(RGBController::GetZoneDescriptionJSON(zone));
}
RGBControllerInterface* OpenRGBPluginAPI::SetDeviceDescriptionJSON(nlohmann::json controller_json)
{
RGBController_Dummy* new_controller = new RGBController_Dummy();
RGBController::SetDeviceDescriptionJSON(controller_json, (RGBController*)new_controller);
return(new_controller);
}
led OpenRGBPluginAPI::SetLEDDescriptionJSON(nlohmann::json led_json)
{
return(RGBController::SetLEDDescriptionJSON(led_json));
}
matrix_map_type OpenRGBPluginAPI::SetMatrixMapDescriptionJSON(nlohmann::json matrix_map_json)
{
return(RGBController::SetMatrixMapDescriptionJSON(matrix_map_json));
}
mode OpenRGBPluginAPI::SetModeDescriptionJSON(nlohmann::json mode_json)
{
return(RGBController::SetModeDescriptionJSON(mode_json));
}
segment OpenRGBPluginAPI::SetSegmentDescriptionJSON(nlohmann::json segment_json)
{
return(RGBController::SetSegmentDescriptionJSON(segment_json));
}
zone OpenRGBPluginAPI::SetZoneDescriptionJSON(nlohmann::json zone_json)
{
return(RGBController::SetZoneDescriptionJSON(zone_json));
}
bool OpenRGBPluginAPI::CompareControllers(RGBControllerInterface* controller_1, RGBControllerInterface* controller_2)
{
return(RGBController::CompareControllers((RGBController*)controller_1, (RGBController*)controller_2));
}
std::string OpenRGBPluginAPI::DeviceTypeToString(device_type type)
{
return(RGBController::DeviceTypeToString(type));
}
bool OpenRGBPluginAPI::SetModeValuesFromMode(mode& destination, mode& source)
{
return(RGBController::SetModeValuesFromMode(destination, source));
}
/*---------------------------------------------------------*\
| SettingsManager APIs |
\*---------------------------------------------------------*/
nlohmann::json OpenRGBPluginAPI::GetSettings(std::string settings_key)
{
return(settings_manager->GetSettings(settings_key));
}
void OpenRGBPluginAPI::SaveSettings()
{
settings_manager->SaveSettings();
}
void OpenRGBPluginAPI::SetSettings(std::string settings_key, nlohmann::json new_settings)
{
settings_manager->SetSettings(settings_key, new_settings);
}