-
-
Notifications
You must be signed in to change notification settings - Fork 378
Expand file tree
/
Copy pathOpenRGBPluginAPI.cpp
More file actions
242 lines (202 loc) · 8.93 KB
/
Copy pathOpenRGBPluginAPI.cpp
File metadata and controls
242 lines (202 loc) · 8.93 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
/*---------------------------------------------------------*\
| 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_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));
}
/*---------------------------------------------------------*\
| 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());
}
/*---------------------------------------------------------*\
| 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);
}