refactor: move Screensaver inhibate call for xwindows from arch to platform

This commit is contained in:
sithlord48 2025-03-14 18:16:34 -04:00 committed by Nick Bolton
parent 613f3651ea
commit 27451d3425
4 changed files with 72 additions and 68 deletions

View file

@ -50,56 +50,3 @@ std::string ArchSystemUnix::getLibsUsed(void) const
{ {
return "not implemented.\nuse lsof on shell"; 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

View file

@ -23,13 +23,4 @@ public:
virtual void setting(const std::string &, const std::string &) const; virtual void setting(const std::string &, const std::string &) const;
virtual std::string getLibsUsed(void) const; virtual std::string getLibsUsed(void) const;
virtual void clearSettings() const; virtual void clearSettings() const;
#ifndef __APPLE__
enum class InhibitScreenServices
{
kScreenSaver,
kSessionManager
};
static bool DBusInhibitScreenCall(InhibitScreenServices serviceID, bool state, std::string &error);
#endif
}; };

View file

@ -8,14 +8,20 @@
#include "XWindowsPowerManager.h" #include "XWindowsPowerManager.h"
#include "arch/Arch.h" #include "arch/Arch.h"
#include "base/Log.h" #include "base/Log.h"
#include <common/constants.h>
#include <QDBusConnection>
#include <QDBusError>
#include <QDBusInterface>
#include <QDBusReply>
namespace { namespace {
bool sleepInhibitCall(bool state, ArchSystemUnix::InhibitScreenServices serviceID) bool sleepInhibitCall(bool state, XWindowsPowerManager::InhibitScreenServices serviceID)
{ {
std::string error; 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())); LOG((CLOG_DEBUG "dbus inhibit error %s", error.c_str()));
return false; return false;
} }
@ -32,16 +38,67 @@ XWindowsPowerManager::~XWindowsPowerManager()
void XWindowsPowerManager::disableSleep() const void XWindowsPowerManager::disableSleep() const
{ {
if (!sleepInhibitCall(true, ArchSystemUnix::InhibitScreenServices::kScreenSaver) && if (!sleepInhibitCall(true, XWindowsPowerManager::InhibitScreenServices::kScreenSaver) &&
!sleepInhibitCall(true, ArchSystemUnix::InhibitScreenServices::kSessionManager)) { !sleepInhibitCall(true, XWindowsPowerManager::InhibitScreenServices::kSessionManager)) {
LOG((CLOG_WARN "failed to prevent system from going to sleep")); LOG((CLOG_WARN "failed to prevent system from going to sleep"));
} }
} }
void XWindowsPowerManager::enableSleep() const void XWindowsPowerManager::enableSleep() const
{ {
if (!sleepInhibitCall(false, ArchSystemUnix::InhibitScreenServices::kScreenSaver) && if (!sleepInhibitCall(false, XWindowsPowerManager::InhibitScreenServices::kScreenSaver) &&
!sleepInhibitCall(false, ArchSystemUnix::InhibitScreenServices::kSessionManager)) { !sleepInhibitCall(false, XWindowsPowerManager::InhibitScreenServices::kSessionManager)) {
LOG((CLOG_WARN "failed to enable system idle sleep")); 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;
}

View file

@ -7,6 +7,8 @@
#pragma once #pragma once
#include <string>
class XWindowsPowerManager class XWindowsPowerManager
{ {
public: public:
@ -25,4 +27,11 @@ public:
XWindowsPowerManager(const XWindowsPowerManager &) = delete; XWindowsPowerManager(const XWindowsPowerManager &) = delete;
XWindowsPowerManager &operator=(const XWindowsPowerManager &) = delete; XWindowsPowerManager &operator=(const XWindowsPowerManager &) = delete;
enum class InhibitScreenServices
{
kScreenSaver,
kSessionManager
};
static bool inhibitScreenCall(InhibitScreenServices serviceID, bool state, std::string &error);
}; };