Browse Source

Merge pull request #19344 from Chocobo1/systemd

Add support for systemd power management
adaptive-webui-19844
Chocobo1 1 year ago committed by GitHub
parent
commit
6bd56478fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 67
      src/gui/powermanagement/powermanagement_x11.cpp
  2. 7
      src/gui/powermanagement/powermanagement_x11.h

67
src/gui/powermanagement/powermanagement_x11.cpp

@ -41,6 +41,13 @@ PowerManagementInhibitor::PowerManagementInhibitor(QObject *parent)
, m_busInterface {new QDBusInterface(u"org.gnome.SessionManager"_s, u"/org/gnome/SessionManager"_s , m_busInterface {new QDBusInterface(u"org.gnome.SessionManager"_s, u"/org/gnome/SessionManager"_s
, u"org.gnome.SessionManager"_s, QDBusConnection::sessionBus(), this)} , u"org.gnome.SessionManager"_s, QDBusConnection::sessionBus(), this)}
{ {
if (!m_busInterface->isValid())
{
delete m_busInterface;
m_busInterface = new QDBusInterface(u"org.freedesktop.login1"_s, u"/org/freedesktop/login1"_s
, u"org.freedesktop.login1.Manager"_s, QDBusConnection::systemBus(), this);
m_manager = ManagerType::Systemd;
if (!m_busInterface->isValid()) if (!m_busInterface->isValid())
{ {
delete m_busInterface; delete m_busInterface;
@ -52,14 +59,13 @@ PowerManagementInhibitor::PowerManagementInhibitor(QObject *parent)
{ {
delete m_busInterface; delete m_busInterface;
m_busInterface = nullptr; m_busInterface = nullptr;
}
m_state = Error;
} }
} }
if (m_busInterface) if (m_busInterface)
{ {
m_busInterface->setTimeout(1000); m_state = Idle;
LogMsg(tr("Power management found suitable D-Bus interface. Interface: %1").arg(m_busInterface->interface())); LogMsg(tr("Power management found suitable D-Bus interface. Interface: %1").arg(m_busInterface->interface()));
} }
else else
@ -75,7 +81,13 @@ void PowerManagementInhibitor::requestIdle()
return; return;
m_state = RequestIdle; m_state = RequestIdle;
qDebug("D-Bus: PowerManagementInhibitor: Requesting idle");
if (m_manager == ManagerType::Systemd)
{
QDBusUnixFileDescriptor dummy;
m_fd.swap(dummy);
return;
}
const QString method = (m_manager == ManagerType::Gnome) const QString method = (m_manager == ManagerType::Gnome)
? u"Uninhibit"_s ? u"Uninhibit"_s
@ -92,12 +104,23 @@ void PowerManagementInhibitor::requestBusy()
return; return;
m_state = RequestBusy; m_state = RequestBusy;
qDebug("D-Bus: PowerManagementInhibitor: Requesting busy");
const QString message = u"Active torrents are currently present"_s; const QString message = u"Active torrents are currently present"_s;
const auto args = (m_manager == ManagerType::Gnome)
? QList<QVariant> {u"qBittorrent"_s, 0u, message, 4u} QList<QVariant> args;
: QList<QVariant> {u"qBittorrent"_s, message}; switch (m_manager)
{
case ManagerType::Freedesktop:
args = {u"qBittorrent"_s, message};
break;
case ManagerType::Gnome:
args = {u"qBittorrent"_s, 0u, message, 4u};
break;
case ManagerType::Systemd:
args = {u"sleep"_s, u"qBittorrent"_s, message, u"block"_s};
break;
}
const QDBusPendingCall pcall = m_busInterface->asyncCallWithArgumentList(u"Inhibit"_s, args); const QDBusPendingCall pcall = m_busInterface->asyncCallWithArgumentList(u"Inhibit"_s, args);
const auto *watcher = new QDBusPendingCallWatcher(pcall, this); const auto *watcher = new QDBusPendingCallWatcher(pcall, this);
connect(watcher, &QDBusPendingCallWatcher::finished, this, &PowerManagementInhibitor::onAsyncReply); connect(watcher, &QDBusPendingCallWatcher::finished, this, &PowerManagementInhibitor::onAsyncReply);
@ -113,7 +136,6 @@ void PowerManagementInhibitor::onAsyncReply(QDBusPendingCallWatcher *call)
if (reply.isError()) 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 LogMsg(tr("Power management error. Action: %1. Error: %2").arg(u"RequestIdle"_s
, reply.error().message()), Log::WARNING); , reply.error().message()), Log::WARNING);
m_state = Error; m_state = Error;
@ -121,18 +143,36 @@ void PowerManagementInhibitor::onAsyncReply(QDBusPendingCallWatcher *call)
else else
{ {
m_state = Idle; m_state = Idle;
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)
{ {
const QDBusPendingReply<quint32> reply = *call; if (m_manager == ManagerType::Systemd)
{
const QDBusPendingReply<QDBusUnixFileDescriptor> reply = *call;
if (reply.isError())
{
LogMsg(tr("Power management error. Action: %1. Error: %2").arg(u"RequestBusy"_s
, reply.error().message()), Log::WARNING);
m_state = Error;
}
else
{
m_state = Busy;
m_fd = reply.value();
if (m_intendedState == Idle)
requestIdle();
}
}
else
{
const QDBusPendingReply<uint> reply = *call;
if (reply.isError()) 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 LogMsg(tr("Power management error. Action: %1. Error: %2").arg(u"RequestBusy"_s
, reply.error().message()), Log::WARNING); , reply.error().message()), Log::WARNING);
m_state = Error; m_state = Error;
@ -141,17 +181,16 @@ void PowerManagementInhibitor::onAsyncReply(QDBusPendingCallWatcher *call)
{ {
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);
if (m_intendedState == Idle) if (m_intendedState == Idle)
requestIdle(); requestIdle();
} }
} }
}
else else
{ {
const QDBusPendingReply reply = *call; const QDBusPendingReply reply = *call;
const QDBusError error = reply.error(); const QDBusError error = reply.error();
qDebug("D-Bus: Unexpected reply in state %d", m_state);
if (error.isValid()) if (error.isValid())
{ {
LogMsg(tr("Power management unexpected error. State: %1. Error: %2").arg(QString::number(m_state) LogMsg(tr("Power management unexpected error. State: %1. Error: %2").arg(QString::number(m_state)

7
src/gui/powermanagement/powermanagement_x11.h

@ -28,6 +28,7 @@
#pragma once #pragma once
#include <QDBusUnixFileDescriptor>
#include <QObject> #include <QObject>
class QDBusInterface; class QDBusInterface;
@ -61,7 +62,8 @@ private:
enum class ManagerType enum class ManagerType
{ {
Freedesktop, // https://www.freedesktop.org/wiki/Specifications/power-management-spec/ 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 Gnome, // https://github.com/GNOME/gnome-settings-daemon/blob/master/gnome-settings-daemon/org.gnome.SessionManager.xml
Systemd // https://www.freedesktop.org/software/systemd/man/org.freedesktop.login1.html
}; };
QDBusInterface *m_busInterface = nullptr; QDBusInterface *m_busInterface = nullptr;
@ -69,5 +71,6 @@ private:
enum State m_state = Error; enum State m_state = Error;
enum State m_intendedState = Idle; enum State m_intendedState = Idle;
quint32 m_cookie = 0; uint m_cookie = 0;
QDBusUnixFileDescriptor m_fd;
}; };

Loading…
Cancel
Save