2021-07-23 15:22:28 +00:00
|
|
|
/*
|
2024-09-17 19:00:25 +00:00
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
2026-06-08 20:37:23 +00:00
|
|
|
* SPDX-FileCopyrightText: (C) 2012 - 2016 Synergy App Ltd
|
2025-01-24 00:11:17 +00:00
|
|
|
* SPDX-FileCopyrightText: (C) 2002 Chris Schoeneman
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
2021-07-23 15:22:28 +00:00
|
|
|
*/
|
2025-01-24 00:11:17 +00:00
|
|
|
|
2025-09-12 15:22:42 +00:00
|
|
|
#include "XDGPowerManager.h"
|
2021-07-23 15:22:28 +00:00
|
|
|
#include "base/Log.h"
|
2025-03-19 22:14:27 +00:00
|
|
|
#include "common/Constants.h"
|
2025-03-14 22:16:34 +00:00
|
|
|
|
|
|
|
|
#include <QDBusConnection>
|
|
|
|
|
#include <QDBusError>
|
|
|
|
|
#include <QDBusInterface>
|
|
|
|
|
#include <QDBusReply>
|
2021-07-23 15:22:28 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
namespace {
|
2021-07-23 15:22:28 +00:00
|
|
|
|
2025-09-12 15:22:42 +00:00
|
|
|
bool sleepInhibitCall(bool state, XDGPowerManager::InhibitScreenServices serviceID)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2025-09-12 15:22:42 +00:00
|
|
|
if (std::string error; !XDGPowerManager::inhibitScreenCall(serviceID, state, error)) {
|
2025-08-06 11:54:59 +00:00
|
|
|
LOG_DEBUG("dbus inhibit error %s", error.c_str());
|
2024-07-02 19:07:06 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2021-07-23 15:22:28 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
return true;
|
2021-07-23 15:22:28 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
} // namespace
|
2021-07-23 15:22:28 +00:00
|
|
|
|
2025-09-12 15:22:42 +00:00
|
|
|
XDGPowerManager::~XDGPowerManager()
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
|
|
|
|
enableSleep();
|
|
|
|
|
}
|
2021-07-23 15:22:28 +00:00
|
|
|
|
2025-09-12 15:22:42 +00:00
|
|
|
void XDGPowerManager::disableSleep() const
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2025-09-12 15:22:42 +00:00
|
|
|
if (!sleepInhibitCall(true, XDGPowerManager::InhibitScreenServices::kScreenSaver) &&
|
|
|
|
|
!sleepInhibitCall(true, XDGPowerManager::InhibitScreenServices::kSessionManager)) {
|
2025-08-06 11:54:59 +00:00
|
|
|
LOG_WARN("failed to prevent system from going to sleep");
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
2021-07-23 15:22:28 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-12 15:22:42 +00:00
|
|
|
void XDGPowerManager::enableSleep() const
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2025-09-12 15:22:42 +00:00
|
|
|
if (!sleepInhibitCall(false, XDGPowerManager::InhibitScreenServices::kScreenSaver) &&
|
|
|
|
|
!sleepInhibitCall(false, XDGPowerManager::InhibitScreenServices::kSessionManager)) {
|
2025-08-06 11:54:59 +00:00
|
|
|
LOG_WARN("failed to enable system idle sleep");
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
2021-07-23 15:22:28 +00:00
|
|
|
}
|
2025-03-14 22:16:34 +00:00
|
|
|
|
2025-09-12 15:22:42 +00:00
|
|
|
bool XDGPowerManager::inhibitScreenCall(InhibitScreenServices serviceID, bool state, std::string &error)
|
2025-03-14 22:16:34 +00:00
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|