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
|
/*---------------------------------------------------------*\
| PluginManager.cpp |
| |
| OpenRGB plugin manager |
| |
| This file is part of the OpenRGB project |
| SPDX-License-Identifier: GPL-2.0-or-later |
\*---------------------------------------------------------*/
#include "LogManager.h"
#include "filesystem.h"
#include "PluginManager.h"
#include "OpenRGBThemeManager.h"
#include "SettingsManager.h"
#include "ResourceManager.h"
#ifdef _WIN32
#include <Windows.h>
#endif
PluginManager::PluginManager()
{
/*---------------------------------------------------------*\
| Initialize plugin manager class variables |
\*---------------------------------------------------------*/
AddPluginCallbackVal = nullptr;
AddPluginCallbackArg = nullptr;
RemovePluginCallbackVal = nullptr;
RemovePluginCallbackArg = nullptr;
/*-------------------------------------------------------------------------*\
| Create OpenRGB plugins directory |
\*-------------------------------------------------------------------------*/
filesystem::path plugins_dir = ResourceManager::get()->GetConfigurationDirectory() / plugins_path;
filesystem::create_directories(plugins_dir);
}
void PluginManager::RegisterAddPluginCallback(AddPluginCallback new_callback, void * new_callback_arg)
{
AddPluginCallbackVal = new_callback;
AddPluginCallbackArg = new_callback_arg;
}
void PluginManager::RegisterRemovePluginCallback(RemovePluginCallback new_callback, void * new_callback_arg)
{
RemovePluginCallbackVal = new_callback;
RemovePluginCallbackArg = new_callback_arg;
}
void PluginManager::ScanAndLoadPlugins()
{
/*---------------------------------------------------------*\
| Get the user plugins directory |
| |
| The user plugins directory is a directory named "plugins" |
| in the configuration directory |
\*---------------------------------------------------------*/
filesystem::path plugins_dir = ResourceManager::get()->GetConfigurationDirectory() / plugins_path;
ScanAndLoadPluginsFrom(plugins_dir, false);
#ifdef OPENRGB_SYSTEM_PLUGIN_DIRECTORY
/*---------------------------------------------------------*\
| Get the system plugins directory |
| |
| The system plugin directory can be set during build time, |
| e.g. by the package maintainer to load plugins installed |
| via package manager |
\*---------------------------------------------------------*/
ScanAndLoadPluginsFrom(OPENRGB_SYSTEM_PLUGIN_DIRECTORY, true);
#endif
#ifdef _WIN32
/*---------------------------------------------------------*\
| Get the exe folder plugins directory (Windows) |
| |
| On Windows, system plugins are located in a folder called |
| "plugins" inside the folder where the OpenRGB.exe file is |
| installed. Typically, C:\Program Files\OpenRGB but other |
| install paths are allowed. |
\*---------------------------------------------------------*/
char path[MAX_PATH];
GetModuleFileName(NULL, path, MAX_PATH);
filesystem::path exe_dir(path);
exe_dir = exe_dir.remove_filename() / plugins_path;
ScanAndLoadPluginsFrom(exe_dir, true);
#endif
}
void PluginManager::ScanAndLoadPluginsFrom(const filesystem::path & plugins_dir, bool is_system)
{
if(is_system)
{
LOG_TRACE("[PluginManager] Scanning system plugin directory: %s", plugins_dir.generic_u8string().c_str());
}
else
{
LOG_TRACE("[PluginManager] Scanning user plugin directory: %s", plugins_dir.generic_u8string().c_str());
}
if(!filesystem::is_directory(plugins_dir))
{
return;
}
/*---------------------------------------------------------*\
| Get a list of all files in the plugins directory |
\*---------------------------------------------------------*/
for(const filesystem::directory_entry& entry: filesystem::directory_iterator(plugins_dir))
{
if(filesystem::is_directory(entry.path()))
{
continue;
}
filesystem::path plugin_path = entry.path();
LOG_TRACE("[PluginManager] Found plugin file %s", plugin_path.filename().generic_u8string().c_str());
AddPlugin(plugin_path, is_system);
}
}
void PluginManager::AddPlugin(const filesystem::path& path, bool is_system)
{
OpenRGBPluginInterface* plugin = nullptr;
unsigned int plugin_idx;
/*---------------------------------------------------------------------*\
| Open plugin settings |
\*---------------------------------------------------------------------*/
json plugin_settings = ResourceManager::get()->GetSettingsManager()->GetSettings("Plugins");
/*---------------------------------------------------------------------*\
| Check if this plugin is on the remove list |
\*---------------------------------------------------------------------*/
if(plugin_settings.contains("plugins_remove"))
{
for(unsigned int plugin_remove_idx = 0; plugin_remove_idx < plugin_settings["plugins_remove"].size(); plugin_remove_idx++)
{
LOG_WARNING("[PluginManager] Checking remove %d, %s", plugin_remove_idx, to_string(plugin_settings["plugins_remove"][plugin_remove_idx]).c_str());
if(plugin_settings["plugins_remove"][plugin_remove_idx] == path.generic_u8string())
{
/*---------------------------------------------------------*\
| Delete the plugin file |
\*---------------------------------------------------------*/
filesystem::remove(path);
}
/*-----------------------------------------------------------------*\
| Erase the plugin from the remove list |
\*-----------------------------------------------------------------*/
plugin_settings["plugins_remove"].erase(plugin_remove_idx);
ResourceManager::get()->GetSettingsManager()->SetSettings("Plugins", plugin_settings);
ResourceManager::get()->GetSettingsManager()->SaveSettings();
}
}
/*---------------------------------------------------------------------*\
| Search active plugins to see if this path already exists |
\*---------------------------------------------------------------------*/
for(plugin_idx = 0; plugin_idx < ActivePlugins.size(); plugin_idx++)
{
if(path == ActivePlugins[plugin_idx].path)
{
break;
}
}
/*---------------------------------------------------------------------*\
| If the path does not match an existing entry, create a new entry |
\*---------------------------------------------------------------------*/
if(plugin_idx == ActivePlugins.size())
{
/*-----------------------------------------------------------------*\
| Create a QPluginLoader and load the plugin |
\*-----------------------------------------------------------------*/
std::string path_string = path.generic_u8string();
QPluginLoader* loader = new QPluginLoader(QString::fromStdString(path_string));
QObject* instance = loader->instance();
if(!loader->isLoaded())
{
LOG_WARNING("[PluginManager] Plugin %s cannot be loaded: %s", path.c_str(), loader->errorString().toStdString().c_str());
}
/*-----------------------------------------------------------------*\
| Check that the plugin is valid, then check the API version |
\*-----------------------------------------------------------------*/
if(instance)
{
plugin = qobject_cast<OpenRGBPluginInterface*>(instance);
if(plugin)
{
if(plugin->GetPluginAPIVersion() == OPENRGB_PLUGIN_API_VERSION)
{
LOG_TRACE("[PluginManager] Plugin %s has a compatible API version", path.c_str());
/*-----------------------------------------------------*\
| Get the plugin information |
\*-----------------------------------------------------*/
OpenRGBPluginInfo info = plugin->GetPluginInfo();
/*-----------------------------------------------------*\
| Search the settings to see if it is enabled |
\*-----------------------------------------------------*/
std::string name = "";
std::string description = "";
bool enabled = true;
bool found = false;
unsigned int plugin_ct = 0;
if(plugin_settings.contains("plugins"))
{
plugin_ct = (unsigned int)plugin_settings["plugins"].size();
for(unsigned int plugin_settings_idx = 0; plugin_settings_idx < plugin_settings["plugins"].size(); plugin_settings_idx++)
{
if(plugin_settings["plugins"][plugin_settings_idx].contains("name"))
{
name = plugin_settings["plugins"][plugin_settings_idx]["name"];
}
if(plugin_settings["plugins"][plugin_settings_idx].contains("description"))
{
description = plugin_settings["plugins"][plugin_settings_idx]["description"];
}
if(plugin_settings["plugins"][plugin_settings_idx].contains("enabled"))
{
enabled = plugin_settings["plugins"][plugin_settings_idx]["enabled"];
}
if((info.Name == name)
&&(info.Description == description))
{
found = true;
break;
}
}
}
/*-----------------------------------------------------*\
| If the plugin was not in the list, add it to the list |
| and default it to enabled, then save the settings |
\*-----------------------------------------------------*/
if(!found)
{
plugin_settings["plugins"][plugin_ct]["name"] = info.Name;
plugin_settings["plugins"][plugin_ct]["description"] = info.Description;
plugin_settings["plugins"][plugin_ct]["enabled"] = enabled;
ResourceManager::get()->GetSettingsManager()->SetSettings("Plugins", plugin_settings);
ResourceManager::get()->GetSettingsManager()->SaveSettings();
}
LOG_VERBOSE("[PluginManager] Loaded plugin %s", info.Name.c_str());
/*-----------------------------------------------------*\
| Add the plugin to the PluginManager active plugins |
\*-----------------------------------------------------*/
OpenRGBPluginEntry entry;
entry.info = info;
entry.plugin = plugin;
entry.loader = loader;
entry.path = path_string;
entry.enabled = enabled;
entry.widget = nullptr;
entry.incompatible = false;
entry.api_version = plugin->GetPluginAPIVersion();
entry.is_system = is_system;
loader->unload();
ActivePlugins.push_back(entry);
if(entry.enabled)
{
LoadPlugin(&ActivePlugins.back());
}
}
else
{
/*-----------------------------------------------------*\
| Fill in a plugin information object with text showing |
| the plugin is incompatible |
\*-----------------------------------------------------*/
OpenRGBPluginInfo info;
info.Name = "Incompatible Plugin";
info.Description = "This plugin is not compatible with this version of OpenRGB.";
/*-----------------------------------------------------*\
| Add the plugin to the PluginManager active plugins |
| but mark it as incompatible |
\*-----------------------------------------------------*/
OpenRGBPluginEntry entry;
entry.info = info;
entry.plugin = plugin;
entry.loader = loader;
entry.path = path_string;
entry.enabled = false;
entry.widget = nullptr;
entry.incompatible = true;
entry.api_version = plugin->GetPluginAPIVersion();
entry.is_system = is_system;
loader->unload();
PluginManager::ActivePlugins.push_back(entry);
bool unloaded = loader->unload();
LOG_WARNING("[PluginManager] Plugin %s has an incompatible API version", path.c_str());
if(!unloaded)
{
LOG_WARNING("[PluginManager] Plugin %s cannot be unloaded", path.c_str());
}
}
}
else
{
LOG_WARNING("[PluginManager] Plugin %s cannot be casted to OpenRGBPluginInterface", path.c_str());
}
}
else
{
LOG_WARNING("[PluginManager] Plugin %s cannot be instantiated.", path.c_str());
}
}
}
void PluginManager::RemovePlugin(const filesystem::path& path)
{
unsigned int plugin_idx;
LOG_TRACE("[PluginManager] Attempting to remove plugin %s", path.c_str());
/*---------------------------------------------------------------------*\
| Search active plugins to see if this path already exists |
\*---------------------------------------------------------------------*/
for(plugin_idx = 0; plugin_idx < ActivePlugins.size(); plugin_idx++)
{
if(path == ActivePlugins[plugin_idx].path)
{
break;
}
}
/*---------------------------------------------------------------------*\
| If the plugin path does not exist in the active plugins list, return |
\*---------------------------------------------------------------------*/
if(plugin_idx == ActivePlugins.size())
{
LOG_TRACE("[PluginManager] Plugin %s not active", path.c_str());
return;
}
/*---------------------------------------------------------------------*\
| If the selected plugin is in the list and loaded, unload it |
\*---------------------------------------------------------------------*/
if(ActivePlugins[plugin_idx].loader->isLoaded())
{
LOG_TRACE("[PluginManager] Plugin %s is active, unloading", path.c_str());
UnloadPlugin(&ActivePlugins[plugin_idx]);
}
/*---------------------------------------------------------------------*\
| Remove the plugin from the active plugins list |
\*---------------------------------------------------------------------*/
ActivePlugins.erase(ActivePlugins.begin() + plugin_idx);
}
void PluginManager::EnablePlugin(const filesystem::path& path)
{
unsigned int plugin_idx;
/*---------------------------------------------------------------------*\
| Search active plugins to see if this path already exists |
\*---------------------------------------------------------------------*/
for(plugin_idx = 0; plugin_idx < ActivePlugins.size(); plugin_idx++)
{
if(path == ActivePlugins[plugin_idx].path)
{
break;
}
}
/*---------------------------------------------------------------------*\
| If the plugin path does not exist in the active plugins list, return |
\*---------------------------------------------------------------------*/
if(plugin_idx == ActivePlugins.size())
{
return;
}
ActivePlugins[plugin_idx].enabled = true;
LoadPlugin(&ActivePlugins[plugin_idx]);
}
void PluginManager::LoadPlugin(OpenRGBPluginEntry* plugin_entry)
{
/*---------------------------------------------------------------------*\
| If the plugin is in the list but is incompatible, return |
\*---------------------------------------------------------------------*/
if(plugin_entry->incompatible)
{
return;
}
/*---------------------------------------------------------------------*\
| If the selected plugin is in the list but not loaded, load it |
\*---------------------------------------------------------------------*/
if(!plugin_entry->loader->isLoaded())
{
plugin_entry->loader->load();
QObject* instance = plugin_entry->loader->instance();
if(instance)
{
OpenRGBPluginInterface* plugin = qobject_cast<OpenRGBPluginInterface*>(instance);
if(plugin)
{
if(plugin->GetPluginAPIVersion() == OPENRGB_PLUGIN_API_VERSION)
{
plugin_entry->plugin = plugin;
plugin->Load(ResourceManager::get());
/*-------------------------------------------------*\
| Call the Add Plugin callback |
\*-------------------------------------------------*/
if(AddPluginCallbackArg != nullptr)
{
AddPluginCallbackVal(AddPluginCallbackArg, plugin_entry);
}
}
}
}
}
}
void PluginManager::DisablePlugin(const filesystem::path& path)
{
unsigned int plugin_idx;
/*---------------------------------------------------------------------*\
| Search active plugins to see if this path already exists |
\*---------------------------------------------------------------------*/
for(plugin_idx = 0; plugin_idx < ActivePlugins.size(); plugin_idx++)
{
if(path == ActivePlugins[plugin_idx].path)
{
break;
}
}
/*---------------------------------------------------------------------*\
| If the plugin path does not exist in the active plugins list, return |
\*---------------------------------------------------------------------*/
if(plugin_idx == ActivePlugins.size())
{
return;
}
ActivePlugins[plugin_idx].enabled = false;
UnloadPlugin(&ActivePlugins[plugin_idx]);
}
void PluginManager::UnloadPlugin(OpenRGBPluginEntry* plugin_entry)
{
/*---------------------------------------------------------------------*\
| If the selected plugin is in the list and loaded, unload it |
\*---------------------------------------------------------------------*/
if(plugin_entry->loader->isLoaded())
{
/*-------------------------------------------------*\
| Call plugin's Unload function before GUI removal |
\*-------------------------------------------------*/
plugin_entry->plugin->Unload();
/*-------------------------------------------------*\
| Call the Remove Plugin callback |
\*-------------------------------------------------*/
if(RemovePluginCallbackVal != nullptr)
{
RemovePluginCallbackVal(RemovePluginCallbackArg, plugin_entry);
}
bool unloaded = plugin_entry->loader->unload();
if(!unloaded)
{
LOG_WARNING("[PluginManager] Plugin %s cannot be unloaded", plugin_entry->path.c_str());
}
else
{
LOG_TRACE("[PluginManager] Plugin %s successfully unloaded", plugin_entry->path.c_str());
}
}
else
{
LOG_TRACE("[PluginManager] Plugin %s was already unloaded", plugin_entry->path.c_str());
}
}
void PluginManager::LoadPlugins()
{
for(OpenRGBPluginEntry& plugin_entry: ActivePlugins)
{
if(plugin_entry.enabled)
{
LoadPlugin(&plugin_entry);
}
}
}
void PluginManager::UnloadPlugins()
{
for(OpenRGBPluginEntry& plugin_entry: ActivePlugins)
{
UnloadPlugin(&plugin_entry);
}
}
|