@@ -7,12 +7,11 @@ namespace srilakshmikanthanp::clipbirdesk {
77PowerHandler::PowerHandler (controller::ClipBird *controller) : controller(controller) {
88#if defined(__linux__)
99 this ->registerPowerManagementListener ();
10+ this ->acquireInhibitLock ();
1011#endif
1112}
1213
13- bool PowerHandler::nativeEventFilter (
14- const QByteArray &eventType, void *message, qintptr *result
15- ) {
14+ bool PowerHandler::nativeEventFilter (const QByteArray &eventType, void *message, qintptr *result) {
1615 constexpr const char *WIN_MSG = " windows_generic_MSG" ;
1716
1817#if defined(_WIN32) || defined(_WIN64)
@@ -25,33 +24,6 @@ bool PowerHandler::nativeEventFilter(
2524}
2625
2726#if defined(__linux__)
28- bool PowerHandler::acquireInhibitLock () {
29- QDBusInterface iface (service, path, interface, QDBusConnection::systemBus ());
30- QDBusReply<QDBusUnixFileDescriptor> reply = iface.call (
31- " Inhibit" , // method name
32- " sleep" , // mode
33- constants::getAppName (), // app name
34- " Preparing for suspend" , // reason
35- " delay" // flags
36- );
37-
38- if (reply.isValid ()) {
39- (inhibitLock = new QFile ())->open (reply.value ().fileDescriptor (), QIODevice::ReadWrite);
40- return true ;
41- } else {
42- qWarning () << " Failed to acquire delay inhibitor:" << reply.error ().message ();
43- return false ;
44- }
45- }
46-
47- void PowerHandler::releaseInhibitLock () {
48- if (inhibitLock) {
49- inhibitLock->close ();
50- delete inhibitLock;
51- inhibitLock = nullptr ;
52- }
53- }
54-
5527void PowerHandler::registerPowerManagementListener () {
5628 QDBusConnection systemBus = QDBusConnection::systemBus ();
5729
@@ -74,14 +46,61 @@ void PowerHandler::registerPowerManagementListener() {
7446 }
7547}
7648
77- void PowerHandler::PrepareForSleep (bool suspending) {
78- if (suspending && acquireInhibitLock ()) {
49+ bool PowerHandler::acquireInhibitLock () {
50+ QDBusInterface iface (service, path, interface, QDBusConnection::systemBus ());
51+ QDBusReply<QDBusUnixFileDescriptor> reply = iface.call (
52+ " Inhibit" , // method name
53+ " sleep" , // mode
54+ constants::getAppName (), // app name
55+ " Preparing for suspend" , // reason
56+ " delay" // flags
57+ );
58+
59+ if (!reply.isValid ()) {
60+ qWarning () << " Failed to acquire delay inhibitor:" << reply.error ().message ();
61+ return false ;
62+ }
63+
64+ inhibitLock = std::make_unique<QFile>();
65+
66+ if (!inhibitLock->open (reply.value ().fileDescriptor (), QIODevice::ReadWrite)) {
67+ qWarning () << " Failed to open inhibit lock file descriptor" ;
68+ return false ;
69+ } else {
70+ qInfo () << " Inhibit lock acquired successfully" ;
71+ return true ;
72+ }
73+ }
74+
75+ void PowerHandler::releaseInhibitLock () {
76+ if (inhibitLock && inhibitLock->isOpen ()) {
77+ inhibitLock->close ();
78+ inhibitLock.reset ();
79+ qInfo () << " Block inhibitor released." ;
80+ } else {
81+ qWarning () << " No inhibit lock to release." ;
82+ }
83+ }
84+
85+ void PowerHandler::handleLinuxSuspendEvent () {
86+ if (inhibitLock && inhibitLock->isOpen ()) {
7987 handleSleepEvent ();
8088 releaseInhibitLock ();
8189 }
90+ }
91+
92+ void PowerHandler::handleLinuxResumeEvent () {
93+ handleWakeUpEvent ();
94+ acquireInhibitLock ();
95+ }
8296
83- if (!suspending) {
84- handleWakeUpEvent ();
97+ void PowerHandler::PrepareForSleep (bool suspending) {
98+ if (suspending) {
99+ qInfo () << " Received suspend signal, preparing for sleep." ;
100+ handleLinuxSuspendEvent ();
101+ } else {
102+ qInfo () << " Received resume signal, waking up." ;
103+ handleLinuxResumeEvent ();
85104 }
86105}
87106#endif
0 commit comments