From 38d773ca46c5f9af9bb0b09da456591378941f4a Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Mon, 17 Jul 2023 23:12:03 +0800 Subject: [PATCH 1/5] Change default power management to Gnome Session Manager As seen on https://www.freedesktop.org/wiki/Specifications/power-management-spec/, the `org.freedesktop.PowerManagement` is obsolete. --- src/gui/powermanagement/powermanagement_x11.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/gui/powermanagement/powermanagement_x11.cpp b/src/gui/powermanagement/powermanagement_x11.cpp index bfdb9da0b..c50b99bc3 100644 --- a/src/gui/powermanagement/powermanagement_x11.cpp +++ b/src/gui/powermanagement/powermanagement_x11.cpp @@ -48,7 +48,7 @@ PowerManagementInhibitor::PowerManagementInhibitor(QObject *parent) m_intendedState = Idle; m_cookie = 0; - m_useGSM = false; + m_useGSM = true; } void PowerManagementInhibitor::requestIdle() @@ -135,9 +135,9 @@ void PowerManagementInhibitor::onAsyncReply(QDBusPendingCallWatcher *call) if (reply.isError()) { qDebug("D-Bus: Reply: Error: %s", qUtf8Printable(reply.error().message())); - if (!m_useGSM) { - qDebug("D-Bus: Falling back to org.gnome.SessionManager"); - m_useGSM = true; + if (m_useGSM) { + qDebug("D-Bus: Falling back to org.freedesktop.PowerManagement"); + m_useGSM = false; m_state = Idle; if (m_intendedState == Busy) requestBusy(); From 79afa0b84d91468bdc663642b9d4ed27c1badc8f Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Mon, 17 Jul 2023 23:18:26 +0800 Subject: [PATCH 2/5] Clean up coding style --- src/gui/powermanagement/powermanagement.h | 2 +- .../powermanagement/powermanagement_x11.cpp | 54 ++++++++++--------- src/gui/powermanagement/powermanagement_x11.h | 10 ++-- 3 files changed, 36 insertions(+), 30 deletions(-) diff --git a/src/gui/powermanagement/powermanagement.h b/src/gui/powermanagement/powermanagement.h index d8019d79c..6fc42c679 100644 --- a/src/gui/powermanagement/powermanagement.h +++ b/src/gui/powermanagement/powermanagement.h @@ -61,6 +61,6 @@ private: PowerManagementInhibitor *m_inhibitor = nullptr; #endif #ifdef Q_OS_MACOS - IOPMAssertionID m_assertionID; + IOPMAssertionID m_assertionID {}; #endif }; diff --git a/src/gui/powermanagement/powermanagement_x11.cpp b/src/gui/powermanagement/powermanagement_x11.cpp index c50b99bc3..94f4dc8ea 100644 --- a/src/gui/powermanagement/powermanagement_x11.cpp +++ b/src/gui/powermanagement/powermanagement_x11.cpp @@ -38,17 +38,15 @@ PowerManagementInhibitor::PowerManagementInhibitor(QObject *parent) : QObject(parent) { - if (!QDBusConnection::sessionBus().isConnected()) { + if (!QDBusConnection::sessionBus().isConnected()) + { qDebug("D-Bus: Could not connect to session bus"); m_state = Error; } - else { + else + { m_state = Idle; } - - m_intendedState = Idle; - m_cookie = 0; - m_useGSM = true; } void PowerManagementInhibitor::requestIdle() @@ -73,12 +71,11 @@ void PowerManagementInhibitor::requestIdle() u"UnInhibit"_s); call.setArguments({m_cookie}); - QDBusPendingCall pcall = QDBusConnection::sessionBus().asyncCall(call, 1000); - auto *watcher = new QDBusPendingCallWatcher(pcall, this); + const QDBusPendingCall pcall = QDBusConnection::sessionBus().asyncCall(call, 1000); + const auto *watcher = new QDBusPendingCallWatcher(pcall, this); connect(watcher, &QDBusPendingCallWatcher::finished, this, &PowerManagementInhibitor::onAsyncReply); } - void PowerManagementInhibitor::requestBusy() { m_intendedState = Busy; @@ -108,45 +105,55 @@ void PowerManagementInhibitor::requestBusy() args << 4u; call.setArguments(args); - QDBusPendingCall pcall = QDBusConnection::sessionBus().asyncCall(call, 1000); - auto *watcher = new QDBusPendingCallWatcher(pcall, this); + const QDBusPendingCall pcall = QDBusConnection::sessionBus().asyncCall(call, 1000); + const auto *watcher = new QDBusPendingCallWatcher(pcall, this); connect(watcher, &QDBusPendingCallWatcher::finished, this, &PowerManagementInhibitor::onAsyncReply); } void PowerManagementInhibitor::onAsyncReply(QDBusPendingCallWatcher *call) { - if (m_state == RequestIdle) { - QDBusPendingReply<> reply = *call; + call->deleteLater(); - if (reply.isError()) { + if (m_state == RequestIdle) + { + const QDBusPendingReply reply = *call; + + if (reply.isError()) + { qDebug("D-Bus: Reply: Error: %s", qUtf8Printable(reply.error().message())); m_state = Error; } - else { + else + { m_state = Idle; qDebug("D-Bus: PowerManagementInhibitor: Request successful"); if (m_intendedState == Busy) requestBusy(); } } - else if (m_state == RequestBusy) { - QDBusPendingReply reply = *call; + else if (m_state == RequestBusy) + { + const QDBusPendingReply reply = *call; - if (reply.isError()) { + if (reply.isError()) + { qDebug("D-Bus: Reply: Error: %s", qUtf8Printable(reply.error().message())); - if (m_useGSM) { + if (m_useGSM) + { qDebug("D-Bus: Falling back to org.freedesktop.PowerManagement"); m_useGSM = false; m_state = Idle; if (m_intendedState == Busy) requestBusy(); } - else { + else + { m_state = Error; } } - else { + else + { m_state = Busy; m_cookie = reply.value(); qDebug("D-Bus: PowerManagementInhibitor: Request successful, cookie is %d", m_cookie); @@ -154,10 +161,9 @@ void PowerManagementInhibitor::onAsyncReply(QDBusPendingCallWatcher *call) requestIdle(); } } - else { + else + { qDebug("D-Bus: Unexpected reply in state %d", m_state); m_state = Error; } - - call->deleteLater(); } diff --git a/src/gui/powermanagement/powermanagement_x11.h b/src/gui/powermanagement/powermanagement_x11.h index 5b9b6a88d..c40502e20 100644 --- a/src/gui/powermanagement/powermanagement_x11.h +++ b/src/gui/powermanagement/powermanagement_x11.h @@ -32,7 +32,7 @@ class QDBusPendingCallWatcher; -class PowerManagementInhibitor : public QObject +class PowerManagementInhibitor final : public QObject { Q_OBJECT Q_DISABLE_COPY_MOVE(PowerManagementInhibitor) @@ -57,9 +57,9 @@ private: RequestIdle }; - enum State m_state; - enum State m_intendedState; - unsigned int m_cookie; + enum State m_state = Error; + enum State m_intendedState = Idle; + quint32 m_cookie = 0; - bool m_useGSM; + bool m_useGSM = true; }; From 64c3845a7c709687fba9f0a385bb356738924729 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Tue, 18 Jul 2023 15:34:32 +0800 Subject: [PATCH 3/5] Detect D-Bus interface --- .../powermanagement/powermanagement_x11.cpp | 85 +++++++------------ src/gui/powermanagement/powermanagement_x11.h | 12 ++- 2 files changed, 39 insertions(+), 58 deletions(-) diff --git a/src/gui/powermanagement/powermanagement_x11.cpp b/src/gui/powermanagement/powermanagement_x11.cpp index 94f4dc8ea..ee4a85c79 100644 --- a/src/gui/powermanagement/powermanagement_x11.cpp +++ b/src/gui/powermanagement/powermanagement_x11.cpp @@ -29,7 +29,7 @@ #include "powermanagement_x11.h" #include -#include +#include #include #include @@ -37,16 +37,27 @@ PowerManagementInhibitor::PowerManagementInhibitor(QObject *parent) : QObject(parent) + , m_busInterface {new QDBusInterface(u"org.gnome.SessionManager"_s, u"/org/gnome/SessionManager"_s + , u"org.gnome.SessionManager"_s, QDBusConnection::sessionBus(), this)} { - if (!QDBusConnection::sessionBus().isConnected()) + if (!m_busInterface->isValid()) { - qDebug("D-Bus: Could not connect to session bus"); - m_state = Error; - } - else - { - m_state = Idle; + delete m_busInterface; + + m_busInterface = new QDBusInterface(u"org.freedesktop.PowerManagement"_s, u"/org/freedesktop/PowerManagement/Inhibit"_s + , u"org.freedesktop.PowerManagement.Inhibit"_s, QDBusConnection::sessionBus(), this); + m_manager = ManagerType::Freedesktop; + if (!m_busInterface->isValid()) + { + delete m_busInterface; + m_busInterface = nullptr; + + m_state = Error; + return; + } } + + m_busInterface->setTimeout(1000); } void PowerManagementInhibitor::requestIdle() @@ -58,20 +69,10 @@ void PowerManagementInhibitor::requestIdle() m_state = RequestIdle; qDebug("D-Bus: PowerManagementInhibitor: Requesting idle"); - QDBusMessage call = m_useGSM - ? QDBusMessage::createMethodCall( - u"org.gnome.SessionManager"_s, - u"/org/gnome/SessionManager"_s, - u"org.gnome.SessionManager"_s, - u"Uninhibit"_s) - : QDBusMessage::createMethodCall( - u"org.freedesktop.PowerManagement"_s, - u"/org/freedesktop/PowerManagement/Inhibit"_s, - u"org.freedesktop.PowerManagement.Inhibit"_s, - u"UnInhibit"_s); - call.setArguments({m_cookie}); - - const QDBusPendingCall pcall = QDBusConnection::sessionBus().asyncCall(call, 1000); + const QString method = (m_manager == ManagerType::Gnome) + ? u"Uninhibit"_s + : u"UnInhibit"_s; + const QDBusPendingCall pcall = m_busInterface->asyncCall(method, m_cookie); const auto *watcher = new QDBusPendingCallWatcher(pcall, this); connect(watcher, &QDBusPendingCallWatcher::finished, this, &PowerManagementInhibitor::onAsyncReply); } @@ -85,27 +86,11 @@ void PowerManagementInhibitor::requestBusy() m_state = RequestBusy; qDebug("D-Bus: PowerManagementInhibitor: Requesting busy"); - QDBusMessage call = m_useGSM - ? QDBusMessage::createMethodCall( - u"org.gnome.SessionManager"_s, - u"/org/gnome/SessionManager"_s, - u"org.gnome.SessionManager"_s, - u"Inhibit"_s) - : QDBusMessage::createMethodCall( - u"org.freedesktop.PowerManagement"_s, - u"/org/freedesktop/PowerManagement/Inhibit"_s, - u"org.freedesktop.PowerManagement.Inhibit"_s, - u"Inhibit"_s); - - QList args = {u"qBittorrent"_s}; - if (m_useGSM) - args << 0u; - args << u"Active torrents are presented"_s; - if (m_useGSM) - args << 4u; - call.setArguments(args); - - const QDBusPendingCall pcall = QDBusConnection::sessionBus().asyncCall(call, 1000); + const QString message = u"Active torrents are presented"_s; + const auto args = (m_manager == ManagerType::Gnome) + ? QList {u"qBittorrent"_s, 0u, message, 4u} + : QList {u"qBittorrent"_s, message}; + const QDBusPendingCall pcall = m_busInterface->asyncCallWithArgumentList(u"Inhibit"_s, args); const auto *watcher = new QDBusPendingCallWatcher(pcall, this); connect(watcher, &QDBusPendingCallWatcher::finished, this, &PowerManagementInhibitor::onAsyncReply); } @@ -138,19 +123,7 @@ void PowerManagementInhibitor::onAsyncReply(QDBusPendingCallWatcher *call) if (reply.isError()) { qDebug("D-Bus: Reply: Error: %s", qUtf8Printable(reply.error().message())); - - if (m_useGSM) - { - qDebug("D-Bus: Falling back to org.freedesktop.PowerManagement"); - m_useGSM = false; - m_state = Idle; - if (m_intendedState == Busy) - requestBusy(); - } - else - { - m_state = Error; - } + m_state = Error; } else { diff --git a/src/gui/powermanagement/powermanagement_x11.h b/src/gui/powermanagement/powermanagement_x11.h index c40502e20..02c93d089 100644 --- a/src/gui/powermanagement/powermanagement_x11.h +++ b/src/gui/powermanagement/powermanagement_x11.h @@ -30,6 +30,7 @@ #include +class QDBusInterface; class QDBusPendingCallWatcher; class PowerManagementInhibitor final : public QObject @@ -57,9 +58,16 @@ private: RequestIdle }; + enum class ManagerType + { + Freedesktop, // https://www.freedesktop.org/wiki/Specifications/power-management-spec/ + Gnome // https://github.com/GNOME/gnome-settings-daemon/blob/master/gnome-settings-daemon/org.gnome.SessionManager.xml + }; + + QDBusInterface *m_busInterface = nullptr; + ManagerType m_manager = ManagerType::Gnome; + enum State m_state = Error; enum State m_intendedState = Idle; quint32 m_cookie = 0; - - bool m_useGSM = true; }; From d569eaa991c6d629517f4221662616c306a4bf32 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Tue, 18 Jul 2023 15:44:13 +0800 Subject: [PATCH 4/5] Revise message --- src/gui/powermanagement/powermanagement_x11.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gui/powermanagement/powermanagement_x11.cpp b/src/gui/powermanagement/powermanagement_x11.cpp index ee4a85c79..c7989f9bd 100644 --- a/src/gui/powermanagement/powermanagement_x11.cpp +++ b/src/gui/powermanagement/powermanagement_x11.cpp @@ -86,7 +86,7 @@ void PowerManagementInhibitor::requestBusy() m_state = RequestBusy; qDebug("D-Bus: PowerManagementInhibitor: Requesting busy"); - const QString message = u"Active torrents are presented"_s; + const QString message = u"Active torrents are currently present"_s; const auto args = (m_manager == ManagerType::Gnome) ? QList {u"qBittorrent"_s, 0u, message, 4u} : QList {u"qBittorrent"_s, message}; From 15b46259f3ac9b99e0a8b2c91a4c262facb397bc Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Tue, 18 Jul 2023 22:34:59 +0800 Subject: [PATCH 5/5] Add logging --- .../powermanagement/powermanagement_x11.cpp | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/gui/powermanagement/powermanagement_x11.cpp b/src/gui/powermanagement/powermanagement_x11.cpp index c7989f9bd..c555dd32d 100644 --- a/src/gui/powermanagement/powermanagement_x11.cpp +++ b/src/gui/powermanagement/powermanagement_x11.cpp @@ -34,6 +34,7 @@ #include #include "base/global.h" +#include "base/logger.h" PowerManagementInhibitor::PowerManagementInhibitor(QObject *parent) : QObject(parent) @@ -53,11 +54,18 @@ PowerManagementInhibitor::PowerManagementInhibitor(QObject *parent) m_busInterface = nullptr; m_state = Error; - return; } } - m_busInterface->setTimeout(1000); + if (m_busInterface) + { + m_busInterface->setTimeout(1000); + LogMsg(tr("Power management found suitable D-Bus interface. Interface: %1").arg(m_busInterface->interface())); + } + else + { + LogMsg(tr("Power management error. Did not found suitable D-Bus interface."), Log::WARNING); + } } void PowerManagementInhibitor::requestIdle() @@ -106,6 +114,8 @@ void PowerManagementInhibitor::onAsyncReply(QDBusPendingCallWatcher *call) if (reply.isError()) { qDebug("D-Bus: Reply: Error: %s", qUtf8Printable(reply.error().message())); + LogMsg(tr("Power management error. Action: %1. Error: %2").arg(u"RequestIdle"_s + , reply.error().message()), Log::WARNING); m_state = Error; } else @@ -123,6 +133,8 @@ void PowerManagementInhibitor::onAsyncReply(QDBusPendingCallWatcher *call) if (reply.isError()) { qDebug("D-Bus: Reply: Error: %s", qUtf8Printable(reply.error().message())); + LogMsg(tr("Power management error. Action: %1. Error: %2").arg(u"RequestBusy"_s + , reply.error().message()), Log::WARNING); m_state = Error; } else @@ -136,7 +148,15 @@ void PowerManagementInhibitor::onAsyncReply(QDBusPendingCallWatcher *call) } else { + const QDBusPendingReply reply = *call; + const QDBusError error = reply.error(); + qDebug("D-Bus: Unexpected reply in state %d", m_state); + if (error.isValid()) + { + LogMsg(tr("Power management unexpected error. State: %1. Error: %2").arg(QString::number(m_state) + , error.message()), Log::WARNING); + } m_state = Error; } }