refactor: move Screensaver inhibate call for xwindows from arch to platform
This commit is contained in:
@ -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<QString, 2> services = {"org.freedesktop.ScreenSaver", "org.gnome.SessionManager"};
|
||||
static const std::array<QString, 2> paths = {"/org/freedesktop/ScreenSaver", "/org/gnome/SessionManager"};
|
||||
static std::array<uint, 2> cookies;
|
||||
|
||||
auto serviceNum = static_cast<uint8_t>(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<uint> 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
|
||||
|
||||
@ -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
|
||||
};
|
||||
|
||||
@ -8,14 +8,20 @@
|
||||
#include "XWindowsPowerManager.h"
|
||||
#include "arch/Arch.h"
|
||||
#include "base/Log.h"
|
||||
#include <common/constants.h>
|
||||
|
||||
#include <QDBusConnection>
|
||||
#include <QDBusError>
|
||||
#include <QDBusInterface>
|
||||
#include <QDBusReply>
|
||||
|
||||
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<QString, 2> services = {"org.freedesktop.ScreenSaver", "org.gnome.SessionManager"};
|
||||
static const std::array<QString, 2> paths = {"/org/freedesktop/ScreenSaver", "/org/gnome/SessionManager"};
|
||||
static std::array<uint, 2> cookies;
|
||||
|
||||
auto serviceNum = static_cast<uint8_t>(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<uint> 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;
|
||||
}
|
||||
|
||||
@ -7,6 +7,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
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);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user