diff --git a/juk.h b/juk.h --- a/juk.h +++ b/juk.h @@ -96,6 +96,7 @@ QStringList m_filesToOpen; bool m_startDocked; bool m_shuttingDown; + uint m_pmToken; static JuK* m_instance; }; diff --git a/juk.cpp b/juk.cpp --- a/juk.cpp +++ b/juk.cpp @@ -17,6 +17,7 @@ #include "juk.h" +#include #include #include #include @@ -39,6 +40,9 @@ #include #include #include +#include +#include +#include #include "slideraction.h" #include "statuslabel.h" @@ -79,7 +83,8 @@ m_player(new PlayerManager), m_scrobbler(nullptr), m_filesToOpen(filesToOpen), - m_shuttingDown(false) + m_shuttingDown(false), + m_pmToken(0) { // Expect segfaults if you change this order. @@ -120,6 +125,38 @@ setupGlobalAccels(); activateScrobblerIfEnabled(); + QDBusInterface *pmInterface = new QDBusInterface(QStringLiteral("org.kde.Solid.PowerManagement"), + QStringLiteral("/org/freedesktop/PowerManagement/Inhibit"), + QStringLiteral("org.freedesktop.PowerManagement.Inhibit"), + QDBusConnection::sessionBus()); + + connect(m_player, &PlayerManager::signalPlay, [=] () { + QDBusReply reply; + if (pmInterface->isValid() && (m_pmToken == 0)) { + reply = pmInterface->call(QStringLiteral("Inhibit"), + KAboutData::applicationData().componentName(), + QStringLiteral("playing audio")); + if (reply.isValid()) { + m_pmToken = reply.value(); + qCDebug(JUK_LOG) << "Inhibiting power management"; + } + } + }); + + auto uninhibitPowerManagement = [=] () { + QDBusMessage reply; + if (pmInterface->isValid() && (m_pmToken != 0)) { + reply = pmInterface->call(QStringLiteral("UnInhibit"), m_pmToken); + if (reply.errorName().isEmpty()) { + m_pmToken = 0; + qCDebug(JUK_LOG) << "Uninhibiting power management"; + } + } + }; + + connect(m_player, &PlayerManager::signalPause, uninhibitPowerManagement); + connect(m_player, &PlayerManager::signalStop, uninhibitPowerManagement); + // The system tray quit command will go straight to qApp->quit without calling // our quit action, so make sure we save config changes no matter how quit is // called.