diff --git a/src/lib/arch/unix/ArchSystemUnix.cpp b/src/lib/arch/unix/ArchSystemUnix.cpp index 31d3d351a..9b16eb95e 100644 --- a/src/lib/arch/unix/ArchSystemUnix.cpp +++ b/src/lib/arch/unix/ArchSystemUnix.cpp @@ -50,56 +50,3 @@ std::string ArchSystemUnix::getLibsUsed(void) const { return "not implemented.\nuse lsof on shell"; } - -#ifndef __APPLE__ -bool ArchSystemUnix::DBusInhibitScreenCall(InhibitScreenServices serviceID, bool state, std::string &error) -{ - error = ""; - static const std::array services = {"org.freedesktop.ScreenSaver", "org.gnome.SessionManager"}; - static const std::array paths = {"/org/freedesktop/ScreenSaver", "/org/gnome/SessionManager"}; - static std::array cookies; - - auto serviceNum = static_cast(serviceID); - - QDBusConnection bus = QDBusConnection::sessionBus(); - if (!bus.isConnected()) { - error = "bus failed to connect"; - return false; - } - - QDBusInterface screenSaverInterface(services[serviceNum], paths[serviceNum], services[serviceNum], bus); - - if (!screenSaverInterface.isValid()) { - error = "screen saver interface failed to initialize"; - return false; - } - - QDBusReply reply; - if (state) { - if (cookies[serviceNum]) { - error = "coockies are not empty"; - return false; - } - - QString msg = "Sleep is manually prevented by the %1 preferences"; - reply = screenSaverInterface.call("Inhibit", kAppName, msg.arg(kAppName)); - if (reply.isValid()) - cookies[serviceNum] = reply.value(); - } else { - if (!cookies[serviceNum]) { - error = "coockies are empty"; - return false; - } - reply = screenSaverInterface.call("UnInhibit", cookies[serviceNum]); - cookies[serviceNum] = 0; - } - - if (!reply.isValid()) { - QDBusError qerror = reply.error(); - error = qerror.name().toStdString() + " : " + qerror.message().toStdString(); - return false; - } - - return true; -} -#endif diff --git a/src/lib/arch/unix/ArchSystemUnix.h b/src/lib/arch/unix/ArchSystemUnix.h index e363b82fa..bf56cb352 100644 --- a/src/lib/arch/unix/ArchSystemUnix.h +++ b/src/lib/arch/unix/ArchSystemUnix.h @@ -23,13 +23,4 @@ public: virtual void setting(const std::string &, const std::string &) const; virtual std::string getLibsUsed(void) const; virtual void clearSettings() const; - -#ifndef __APPLE__ - enum class InhibitScreenServices - { - kScreenSaver, - kSessionManager - }; - static bool DBusInhibitScreenCall(InhibitScreenServices serviceID, bool state, std::string &error); -#endif }; diff --git a/src/lib/platform/XWindowsPowerManager.cpp b/src/lib/platform/XWindowsPowerManager.cpp index b3dae1f65..cf10bc302 100644 --- a/src/lib/platform/XWindowsPowerManager.cpp +++ b/src/lib/platform/XWindowsPowerManager.cpp @@ -8,14 +8,20 @@ #include "XWindowsPowerManager.h" #include "arch/Arch.h" #include "base/Log.h" +#include + +#include +#include +#include +#include namespace { -bool sleepInhibitCall(bool state, ArchSystemUnix::InhibitScreenServices serviceID) +bool sleepInhibitCall(bool state, XWindowsPowerManager::InhibitScreenServices serviceID) { std::string error; - if (!ArchSystemUnix::DBusInhibitScreenCall(serviceID, state, error)) { + if (!XWindowsPowerManager::inhibitScreenCall(serviceID, state, error)) { LOG((CLOG_DEBUG "dbus inhibit error %s", error.c_str())); return false; } @@ -32,16 +38,67 @@ XWindowsPowerManager::~XWindowsPowerManager() void XWindowsPowerManager::disableSleep() const { - if (!sleepInhibitCall(true, ArchSystemUnix::InhibitScreenServices::kScreenSaver) && - !sleepInhibitCall(true, ArchSystemUnix::InhibitScreenServices::kSessionManager)) { + if (!sleepInhibitCall(true, XWindowsPowerManager::InhibitScreenServices::kScreenSaver) && + !sleepInhibitCall(true, XWindowsPowerManager::InhibitScreenServices::kSessionManager)) { LOG((CLOG_WARN "failed to prevent system from going to sleep")); } } void XWindowsPowerManager::enableSleep() const { - if (!sleepInhibitCall(false, ArchSystemUnix::InhibitScreenServices::kScreenSaver) && - !sleepInhibitCall(false, ArchSystemUnix::InhibitScreenServices::kSessionManager)) { + if (!sleepInhibitCall(false, XWindowsPowerManager::InhibitScreenServices::kScreenSaver) && + !sleepInhibitCall(false, XWindowsPowerManager::InhibitScreenServices::kSessionManager)) { LOG((CLOG_WARN "failed to enable system idle sleep")); } } + +bool XWindowsPowerManager::inhibitScreenCall(InhibitScreenServices serviceID, bool state, std::string &error) +{ + error = ""; + static const std::array services = {"org.freedesktop.ScreenSaver", "org.gnome.SessionManager"}; + static const std::array paths = {"/org/freedesktop/ScreenSaver", "/org/gnome/SessionManager"}; + static std::array cookies; + + auto serviceNum = static_cast(serviceID); + + QDBusConnection bus = QDBusConnection::sessionBus(); + if (!bus.isConnected()) { + error = "bus failed to connect"; + return false; + } + + QDBusInterface screenSaverInterface(services[serviceNum], paths[serviceNum], services[serviceNum], bus); + + if (!screenSaverInterface.isValid()) { + error = "screen saver interface failed to initialize"; + return false; + } + + QDBusReply reply; + if (state) { + if (cookies[serviceNum]) { + error = "cookies are not empty"; + return false; + } + + QString msg = "Sleep is manually prevented by the %1 preferences"; + reply = screenSaverInterface.call("Inhibit", kAppName, msg.arg(kAppName)); + if (reply.isValid()) + cookies[serviceNum] = reply.value(); + } else { + if (!cookies[serviceNum]) { + error = "cookies are empty"; + return false; + } + reply = screenSaverInterface.call("UnInhibit", cookies[serviceNum]); + cookies[serviceNum] = 0; + } + + if (!reply.isValid()) { + QDBusError qerror = reply.error(); + error = qerror.name().toStdString() + " : " + qerror.message().toStdString(); + return false; + } + + return true; +} diff --git a/src/lib/platform/XWindowsPowerManager.h b/src/lib/platform/XWindowsPowerManager.h index 9b26eeb8f..fc526fa86 100644 --- a/src/lib/platform/XWindowsPowerManager.h +++ b/src/lib/platform/XWindowsPowerManager.h @@ -7,6 +7,8 @@ #pragma once +#include + class XWindowsPowerManager { public: @@ -25,4 +27,11 @@ public: XWindowsPowerManager(const XWindowsPowerManager &) = delete; XWindowsPowerManager &operator=(const XWindowsPowerManager &) = delete; + + enum class InhibitScreenServices + { + kScreenSaver, + kSessionManager + }; + static bool inhibitScreenCall(InhibitScreenServices serviceID, bool state, std::string &error); };