Browse Source

Merge pull request #19333 from Chocobo1/clean

Change default power management to Gnome Session Manager
adaptive-webui-19844
Chocobo1 1 year ago committed by GitHub
parent
commit
2c08dc9dad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      src/gui/powermanagement/powermanagement.h
  2. 139
      src/gui/powermanagement/powermanagement_x11.cpp
  3. 18
      src/gui/powermanagement/powermanagement_x11.h

2
src/gui/powermanagement/powermanagement.h

@ -61,6 +61,6 @@ private:
PowerManagementInhibitor *m_inhibitor = nullptr; PowerManagementInhibitor *m_inhibitor = nullptr;
#endif #endif
#ifdef Q_OS_MACOS #ifdef Q_OS_MACOS
IOPMAssertionID m_assertionID; IOPMAssertionID m_assertionID {};
#endif #endif
}; };

139
src/gui/powermanagement/powermanagement_x11.cpp

@ -29,26 +29,43 @@
#include "powermanagement_x11.h" #include "powermanagement_x11.h"
#include <QDBusConnection> #include <QDBusConnection>
#include <QDBusMessage> #include <QDBusInterface>
#include <QDBusPendingCall> #include <QDBusPendingCall>
#include <QDBusPendingReply> #include <QDBusPendingReply>
#include "base/global.h" #include "base/global.h"
#include "base/logger.h"
PowerManagementInhibitor::PowerManagementInhibitor(QObject *parent) PowerManagementInhibitor::PowerManagementInhibitor(QObject *parent)
: 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; delete m_busInterface;
}
else { m_busInterface = new QDBusInterface(u"org.freedesktop.PowerManagement"_s, u"/org/freedesktop/PowerManagement/Inhibit"_s
m_state = Idle; , 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;
}
} }
m_intendedState = Idle; if (m_busInterface)
m_cookie = 0; {
m_useGSM = false; 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() void PowerManagementInhibitor::requestIdle()
@ -60,25 +77,14 @@ void PowerManagementInhibitor::requestIdle()
m_state = RequestIdle; m_state = RequestIdle;
qDebug("D-Bus: PowerManagementInhibitor: Requesting idle"); qDebug("D-Bus: PowerManagementInhibitor: Requesting idle");
QDBusMessage call = m_useGSM const QString method = (m_manager == ManagerType::Gnome)
? QDBusMessage::createMethodCall( ? u"Uninhibit"_s
u"org.gnome.SessionManager"_s, : u"UnInhibit"_s;
u"/org/gnome/SessionManager"_s, const QDBusPendingCall pcall = m_busInterface->asyncCall(method, m_cookie);
u"org.gnome.SessionManager"_s, const auto *watcher = new QDBusPendingCallWatcher(pcall, this);
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});
QDBusPendingCall pcall = QDBusConnection::sessionBus().asyncCall(call, 1000);
auto *watcher = new QDBusPendingCallWatcher(pcall, this);
connect(watcher, &QDBusPendingCallWatcher::finished, this, &PowerManagementInhibitor::onAsyncReply); connect(watcher, &QDBusPendingCallWatcher::finished, this, &PowerManagementInhibitor::onAsyncReply);
} }
void PowerManagementInhibitor::requestBusy() void PowerManagementInhibitor::requestBusy()
{ {
m_intendedState = Busy; m_intendedState = Busy;
@ -88,65 +94,51 @@ void PowerManagementInhibitor::requestBusy()
m_state = RequestBusy; m_state = RequestBusy;
qDebug("D-Bus: PowerManagementInhibitor: Requesting busy"); qDebug("D-Bus: PowerManagementInhibitor: Requesting busy");
QDBusMessage call = m_useGSM const QString message = u"Active torrents are currently present"_s;
? QDBusMessage::createMethodCall( const auto args = (m_manager == ManagerType::Gnome)
u"org.gnome.SessionManager"_s, ? QList<QVariant> {u"qBittorrent"_s, 0u, message, 4u}
u"/org/gnome/SessionManager"_s, : QList<QVariant> {u"qBittorrent"_s, message};
u"org.gnome.SessionManager"_s, const QDBusPendingCall pcall = m_busInterface->asyncCallWithArgumentList(u"Inhibit"_s, args);
u"Inhibit"_s) const auto *watcher = new QDBusPendingCallWatcher(pcall, this);
: QDBusMessage::createMethodCall(
u"org.freedesktop.PowerManagement"_s,
u"/org/freedesktop/PowerManagement/Inhibit"_s,
u"org.freedesktop.PowerManagement.Inhibit"_s,
u"Inhibit"_s);
QList<QVariant> args = {u"qBittorrent"_s};
if (m_useGSM)
args << 0u;
args << u"Active torrents are presented"_s;
if (m_useGSM)
args << 4u;
call.setArguments(args);
QDBusPendingCall pcall = QDBusConnection::sessionBus().asyncCall(call, 1000);
auto *watcher = new QDBusPendingCallWatcher(pcall, this);
connect(watcher, &QDBusPendingCallWatcher::finished, this, &PowerManagementInhibitor::onAsyncReply); connect(watcher, &QDBusPendingCallWatcher::finished, this, &PowerManagementInhibitor::onAsyncReply);
} }
void PowerManagementInhibitor::onAsyncReply(QDBusPendingCallWatcher *call) void PowerManagementInhibitor::onAsyncReply(QDBusPendingCallWatcher *call)
{ {
if (m_state == RequestIdle) { call->deleteLater();
QDBusPendingReply<> reply = *call;
if (m_state == RequestIdle)
{
const QDBusPendingReply reply = *call;
if (reply.isError()) { if (reply.isError())
{
qDebug("D-Bus: Reply: Error: %s", qUtf8Printable(reply.error().message())); 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; m_state = Error;
} }
else { else
{
m_state = Idle; m_state = Idle;
qDebug("D-Bus: PowerManagementInhibitor: Request successful"); qDebug("D-Bus: PowerManagementInhibitor: Request successful");
if (m_intendedState == Busy) if (m_intendedState == Busy)
requestBusy(); requestBusy();
} }
} }
else if (m_state == RequestBusy) { else if (m_state == RequestBusy)
QDBusPendingReply<uint> reply = *call; {
const QDBusPendingReply<quint32> reply = *call;
if (reply.isError()) { if (reply.isError())
{
qDebug("D-Bus: Reply: Error: %s", qUtf8Printable(reply.error().message())); qDebug("D-Bus: Reply: Error: %s", qUtf8Printable(reply.error().message()));
LogMsg(tr("Power management error. Action: %1. Error: %2").arg(u"RequestBusy"_s
if (!m_useGSM) { , reply.error().message()), Log::WARNING);
qDebug("D-Bus: Falling back to org.gnome.SessionManager"); m_state = Error;
m_useGSM = true;
m_state = Idle;
if (m_intendedState == Busy)
requestBusy();
}
else {
m_state = Error;
}
} }
else { else
{
m_state = Busy; m_state = Busy;
m_cookie = reply.value(); m_cookie = reply.value();
qDebug("D-Bus: PowerManagementInhibitor: Request successful, cookie is %d", m_cookie); qDebug("D-Bus: PowerManagementInhibitor: Request successful, cookie is %d", m_cookie);
@ -154,10 +146,17 @@ void PowerManagementInhibitor::onAsyncReply(QDBusPendingCallWatcher *call)
requestIdle(); requestIdle();
} }
} }
else { else
{
const QDBusPendingReply reply = *call;
const QDBusError error = reply.error();
qDebug("D-Bus: Unexpected reply in state %d", m_state); 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; m_state = Error;
} }
call->deleteLater();
} }

18
src/gui/powermanagement/powermanagement_x11.h

@ -30,9 +30,10 @@
#include <QObject> #include <QObject>
class QDBusInterface;
class QDBusPendingCallWatcher; class QDBusPendingCallWatcher;
class PowerManagementInhibitor : public QObject class PowerManagementInhibitor final : public QObject
{ {
Q_OBJECT Q_OBJECT
Q_DISABLE_COPY_MOVE(PowerManagementInhibitor) Q_DISABLE_COPY_MOVE(PowerManagementInhibitor)
@ -57,9 +58,16 @@ private:
RequestIdle RequestIdle
}; };
enum State m_state; enum class ManagerType
enum State m_intendedState; {
unsigned int m_cookie; 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;
bool m_useGSM; enum State m_state = Error;
enum State m_intendedState = Idle;
quint32 m_cookie = 0;
}; };

Loading…
Cancel
Save