Skip to content

Commit bd3cc94

Browse files
zachdeibertCalcProgrammer1
authored andcommitted
Add support for loading profiles on suspend/resume
1 parent 7d96b27 commit bd3cc94

17 files changed

+608
-87
lines changed

Diff for: OpenRGB.pro

+15-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ INCLUDEPATH +=
122122
AutoStart/ \
123123
KeyboardLayoutManager/ \
124124
RGBController/ \
125-
qt/
125+
qt/ \
126+
SuspendResume/
126127

127128
HEADERS += \
128129
$$GUI_H \
@@ -156,6 +157,7 @@ HEADERS +=
156157
serial_port/serial_port.h \
157158
StringUtils.h \
158159
super_io/super_io.h \
160+
SuspendResume/SuspendResume.h \
159161
AutoStart/AutoStart.h \
160162
KeyboardLayoutManager/KeyboardLayoutManager.h \
161163
RGBController/RGBController.h \
@@ -388,6 +390,7 @@ win32:SOURCES +=
388390
i2c_smbus/i2c_smbus_piix4.cpp \
389391
scsiapi/scsiapi_windows.c \
390392
serial_port/find_usb_serial_port_win.cpp \
393+
SuspendResume/SuspendResume_Windows.cpp \
391394
wmi/wmi.cpp \
392395
AutoStart/AutoStart-Windows.cpp \
393396

@@ -405,6 +408,7 @@ win32:HEADERS +=
405408
i2c_smbus/i2c_smbus_piix4.h \
406409
wmi/wmi.h \
407410
AutoStart/AutoStart-Windows.h \
411+
SuspendResume/SuspendResume_Windows.h \
408412

409413
win32:contains(QMAKE_TARGET.arch, x86_64) {
410414
LIBS += \
@@ -499,6 +503,7 @@ contains(QMAKE_PLATFORM, linux) {
499503
dependencies/NVFC/nvapi.h \
500504
i2c_smbus/i2c_smbus_linux.h \
501505
AutoStart/AutoStart-Linux.h \
506+
SuspendResume/SuspendResume_Linux_FreeBSD.h \
502507

503508
INCLUDEPATH += \
504509
dependencies/NVFC \
@@ -516,6 +521,8 @@ contains(QMAKE_PLATFORM, linux) {
516521
LIBS += -lstdc++fs
517522
}
518523

524+
QT += dbus
525+
519526
QMAKE_CXXFLAGS += -Wno-implicit-fallthrough -Wno-psabi
520527

521528
#-------------------------------------------------------------------------------------------#
@@ -550,6 +557,7 @@ contains(QMAKE_PLATFORM, linux) {
550557
scsiapi/scsiapi_linux.c \
551558
serial_port/find_usb_serial_port_linux.cpp \
552559
AutoStart/AutoStart-Linux.cpp \
560+
SuspendResume/SuspendResume_Linux_FreeBSD.cpp \
553561

554562
#-------------------------------------------------------------------------------------------#
555563
# Set up install paths #
@@ -622,6 +630,7 @@ contains(QMAKE_PLATFORM, freebsd) {
622630

623631
HEADERS += \
624632
AutoStart/AutoStart-FreeBSD.h \
633+
SuspendResume/SuspendResume_Linux_FreeBSD.h \
625634

626635
HEADERS -= \
627636
Controllers/SeagateController/RGBController_Seagate.h \
@@ -639,6 +648,8 @@ contains(QMAKE_PLATFORM, freebsd) {
639648
LIBS += -lstdc++fs
640649
}
641650

651+
QT += dbus
652+
642653
#-------------------------------------------------------------------------------------------#
643654
# Determine which hidapi to use based on availability #
644655
# Prefer hidraw backend, then libusb #
@@ -667,6 +678,7 @@ contains(QMAKE_PLATFORM, freebsd) {
667678
dependencies/hueplusplus-1.1.0/src/LinHttpHandler.cpp \
668679
serial_port/find_usb_serial_port_linux.cpp \
669680
AutoStart/AutoStart-FreeBSD.cpp \
681+
SuspendResume/SuspendResume_Linux_FreeBSD.cpp \
670682

671683
SOURCES -= \
672684
Controllers/SeagateController/RGBController_Seagate.cpp \
@@ -726,6 +738,7 @@ macx {
726738
HEADERS += \
727739
AutoStart/AutoStart-MacOS.h \
728740
qt/macutils.h \
741+
SuspendResume/SuspendResume_MacOS.h \
729742

730743
HEADERS += $$CONTROLLER_H_MACOS
731744

@@ -734,6 +747,7 @@ macx {
734747
serial_port/find_usb_serial_port_macos.cpp \
735748
AutoStart/AutoStart-MacOS.cpp \
736749
qt/macutils.mm \
750+
SuspendResume/SuspendResume_MacOS.cpp \
737751

738752
SOURCES += $$CONTROLLER_CPP_MACOS
739753

Diff for: PluginManager.cpp

+41-21
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ void PluginManager::AddPlugin(const filesystem::path& path, bool is_system)
232232

233233
if(entry.enabled)
234234
{
235-
LoadPlugin(path);
235+
LoadPlugin(&ActivePlugins.back());
236236
}
237237
}
238238
else
@@ -319,7 +319,7 @@ void PluginManager::RemovePlugin(const filesystem::path& path)
319319
if(ActivePlugins[plugin_idx].loader->isLoaded())
320320
{
321321
LOG_TRACE("[PluginManager] Plugin %s is active, unloading", path.c_str());
322-
UnloadPlugin(path);
322+
UnloadPlugin(&ActivePlugins[plugin_idx]);
323323
}
324324

325325
/*---------------------------------------------------------------------*\
@@ -328,7 +328,7 @@ void PluginManager::RemovePlugin(const filesystem::path& path)
328328
ActivePlugins.erase(ActivePlugins.begin() + plugin_idx);
329329
}
330330

331-
void PluginManager::LoadPlugin(const filesystem::path& path)
331+
void PluginManager::EnablePlugin(const filesystem::path& path)
332332
{
333333
unsigned int plugin_idx;
334334

@@ -351,22 +351,28 @@ void PluginManager::LoadPlugin(const filesystem::path& path)
351351
return;
352352
}
353353

354+
ActivePlugins[plugin_idx].enabled = true;
355+
LoadPlugin(&ActivePlugins[plugin_idx]);
356+
}
357+
358+
void PluginManager::LoadPlugin(OpenRGBPluginEntry* plugin_entry)
359+
{
354360
/*---------------------------------------------------------------------*\
355361
| If the plugin is in the list but is incompatible, return |
356362
\*---------------------------------------------------------------------*/
357-
if(ActivePlugins[plugin_idx].incompatible)
363+
if(plugin_entry->incompatible)
358364
{
359365
return;
360366
}
361367

362368
/*---------------------------------------------------------------------*\
363369
| If the selected plugin is in the list but not loaded, load it |
364370
\*---------------------------------------------------------------------*/
365-
if(!ActivePlugins[plugin_idx].loader->isLoaded())
371+
if(!plugin_entry->loader->isLoaded())
366372
{
367-
ActivePlugins[plugin_idx].loader->load();
373+
plugin_entry->loader->load();
368374

369-
QObject* instance = ActivePlugins[plugin_idx].loader->instance();
375+
QObject* instance = plugin_entry->loader->instance();
370376

371377
bool dark_theme = OpenRGBThemeManager::IsDarkTheme();
372378

@@ -378,7 +384,7 @@ void PluginManager::LoadPlugin(const filesystem::path& path)
378384
{
379385
if(plugin->GetPluginAPIVersion() == OPENRGB_PLUGIN_API_VERSION)
380386
{
381-
ActivePlugins[plugin_idx].plugin = plugin;
387+
plugin_entry->plugin = plugin;
382388

383389
plugin->Load(dark_theme, ResourceManager::get());
384390

@@ -387,15 +393,15 @@ void PluginManager::LoadPlugin(const filesystem::path& path)
387393
\*-------------------------------------------------*/
388394
if(AddPluginCallbackArg != nullptr)
389395
{
390-
AddPluginCallbackVal(AddPluginCallbackArg, &ActivePlugins[plugin_idx]);
396+
AddPluginCallbackVal(AddPluginCallbackArg, plugin_entry);
391397
}
392398
}
393399
}
394400
}
395401
}
396402
}
397403

398-
void PluginManager::UnloadPlugin(const filesystem::path& path)
404+
void PluginManager::DisablePlugin(const filesystem::path& path)
399405
{
400406
unsigned int plugin_idx;
401407

@@ -418,48 +424,62 @@ void PluginManager::UnloadPlugin(const filesystem::path& path)
418424
return;
419425
}
420426

427+
ActivePlugins[plugin_idx].enabled = false;
428+
UnloadPlugin(&ActivePlugins[plugin_idx]);
429+
}
430+
431+
void PluginManager::UnloadPlugin(OpenRGBPluginEntry* plugin_entry)
432+
{
421433
/*---------------------------------------------------------------------*\
422434
| If the selected plugin is in the list and loaded, unload it |
423435
\*---------------------------------------------------------------------*/
424-
if(ActivePlugins[plugin_idx].loader->isLoaded())
436+
if(plugin_entry->loader->isLoaded())
425437
{
426438
/*-------------------------------------------------*\
427439
| Call plugin's Unload function before GUI removal |
428440
\*-------------------------------------------------*/
429-
ActivePlugins[plugin_idx].plugin->Unload();
441+
plugin_entry->plugin->Unload();
430442

431443
/*-------------------------------------------------*\
432444
| Call the Remove Plugin callback |
433445
\*-------------------------------------------------*/
434446
if(RemovePluginCallbackVal != nullptr)
435447
{
436-
RemovePluginCallbackVal(RemovePluginCallbackArg, &ActivePlugins[plugin_idx]);
448+
RemovePluginCallbackVal(RemovePluginCallbackArg, plugin_entry);
437449
}
438450

439-
bool unloaded = ActivePlugins[plugin_idx].loader->unload();
451+
bool unloaded = plugin_entry->loader->unload();
440452

441453
if(!unloaded)
442454
{
443-
LOG_WARNING("[PluginManager] Plugin %s cannot be unloaded", path.c_str());
455+
LOG_WARNING("[PluginManager] Plugin %s cannot be unloaded", plugin_entry->path.c_str());
444456
}
445457
else
446458
{
447-
LOG_TRACE("[PluginManager] Plugin %s successfully unloaded", path.c_str());
459+
LOG_TRACE("[PluginManager] Plugin %s successfully unloaded", plugin_entry->path.c_str());
448460
}
449461
}
450462
else
451463
{
452-
LOG_TRACE("[PluginManager] Plugin %s was already unloaded", path.c_str());
464+
LOG_TRACE("[PluginManager] Plugin %s was already unloaded", plugin_entry->path.c_str());
453465
}
454466
}
455467

456-
void PluginManager::UnloadPlugins()
468+
void PluginManager::LoadPlugins()
457469
{
458-
for(const OpenRGBPluginEntry& plugin_entry: ActivePlugins)
470+
for(OpenRGBPluginEntry& plugin_entry: ActivePlugins)
459471
{
460-
if(plugin_entry.loader->isLoaded())
472+
if(plugin_entry.enabled)
461473
{
462-
plugin_entry.plugin->Unload();
474+
LoadPlugin(&plugin_entry);
463475
}
464476
}
465477
}
478+
479+
void PluginManager::UnloadPlugins()
480+
{
481+
for(OpenRGBPluginEntry& plugin_entry: ActivePlugins)
482+
{
483+
UnloadPlugin(&plugin_entry);
484+
}
485+
}

Diff for: PluginManager.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,18 @@ class PluginManager
4747
void AddPlugin(const filesystem::path& path, bool is_system);
4848
void RemovePlugin(const filesystem::path& path);
4949

50-
void LoadPlugin(const filesystem::path& path);
51-
void UnloadPlugin(const filesystem::path& path);
50+
void EnablePlugin(const filesystem::path& path);
51+
void DisablePlugin(const filesystem::path& path);
5252

53+
void LoadPlugins();
5354
void UnloadPlugins();
5455

5556
std::vector<OpenRGBPluginEntry> ActivePlugins;
5657

5758
private:
59+
void LoadPlugin(OpenRGBPluginEntry* plugin_entry);
60+
void UnloadPlugin(OpenRGBPluginEntry* plugin_entry);
61+
5862
void ScanAndLoadPluginsFrom(const filesystem::path & plugins_dir, bool is_system);
5963

6064
AddPluginCallback AddPluginCallbackVal;

Diff for: SuspendResume/SuspendResume.h

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*---------------------------------------------------------*\
2+
| SuspendResume.h |
3+
| |
4+
| Suspend/resume common implementation |
5+
| |
6+
| Zach Deibert (zachdeibert) 12 Nov 2024 |
7+
| |
8+
| This file is part of the OpenRGB project |
9+
| SPDX-License-Identifier: GPL-2.0-only |
10+
\*---------------------------------------------------------*/
11+
12+
#pragma once
13+
14+
class SuspendResumeListenerBase
15+
{
16+
protected:
17+
virtual void OnSuspend() = 0;
18+
virtual void OnResume() = 0;
19+
};
20+
21+
#ifdef _WIN32
22+
#include "SuspendResume_Windows.h"
23+
#endif
24+
25+
#ifdef __APPLE__
26+
#include "SuspendResume_MacOS.h"
27+
#endif
28+
29+
#if defined(__linux__) || defined(__FreeBSD__)
30+
#include "SuspendResume_Linux_FreeBSD.h"
31+
#endif

Diff for: SuspendResume/SuspendResume_Linux_FreeBSD.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*---------------------------------------------------------*\
2+
| SuspendResume_Linux_FreeBSD.cpp |
3+
| |
4+
| Suspend/resume Linux/FreeBSD implementation |
5+
| |
6+
| Zach Deibert (zachdeibert) 12 Nov 2024 |
7+
| |
8+
| This file is part of the OpenRGB project |
9+
| SPDX-License-Identifier: GPL-2.0-only |
10+
\*---------------------------------------------------------*/
11+
12+
#include <QDBusConnection>
13+
#include "SuspendResume.h"
14+
15+
SuspendResumeLoginManager::SuspendResumeLoginManager(SuspendResumeListener *srl) : srl(srl), bus(QDBusConnection::systemBus())
16+
{
17+
bus.connect("org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager", "PrepareForSleep", this, SLOT(PrepareForSleep()));
18+
}
19+
20+
SuspendResumeLoginManager::~SuspendResumeLoginManager()
21+
{
22+
bus.disconnect("org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager", "PrepareForSleep", this, SLOT(PrepareForSleep()));
23+
}
24+
25+
void SuspendResumeLoginManager::PrepareForSleep(bool mode)
26+
{
27+
if(mode)
28+
{
29+
srl->OnSuspend();
30+
}
31+
else
32+
{
33+
srl->OnResume();
34+
}
35+
}
36+
37+
SuspendResumeListener::SuspendResumeListener() : login_manager(this)
38+
{
39+
}

Diff for: SuspendResume/SuspendResume_Linux_FreeBSD.h

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*---------------------------------------------------------*\
2+
| SuspendResume_Linux_FreeBSD.h |
3+
| |
4+
| Suspend/resume Linux/FreeBSD implementation |
5+
| |
6+
| Zach Deibert (zachdeibert) 12 Nov 2024 |
7+
| |
8+
| This file is part of the OpenRGB project |
9+
| SPDX-License-Identifier: GPL-2.0-only |
10+
\*---------------------------------------------------------*/
11+
12+
#pragma once
13+
14+
#include <QDBusConnection>
15+
#include <QObject>
16+
#include "SuspendResume.h"
17+
18+
class SuspendResumeListener;
19+
20+
class SuspendResumeLoginManager : public QObject
21+
{
22+
Q_OBJECT
23+
24+
public:
25+
SuspendResumeLoginManager(SuspendResumeListener *srl);
26+
~SuspendResumeLoginManager();
27+
28+
public slots:
29+
void PrepareForSleep(bool mode);
30+
31+
private:
32+
SuspendResumeListener *srl;
33+
QDBusConnection bus;
34+
};
35+
36+
class SuspendResumeListener : public SuspendResumeListenerBase
37+
{
38+
friend class SuspendResumeLoginManager;
39+
40+
protected:
41+
SuspendResumeListener();
42+
43+
private:
44+
SuspendResumeLoginManager login_manager;
45+
};

0 commit comments

Comments
 (0)