2021-04-11 15:26:28 +02:00
|
|
|
/**
|
|
|
|
* SPDX-FileCopyrightText: 2019 (c) Matthieu Gallien <matthieu_gallien@yahoo.fr>
|
|
|
|
* SPDX-FileCopyrightText: 2021 Bart De Vries <bart@mogwai.be>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "powermanagementinterface.h"
|
2021-06-05 20:12:42 +02:00
|
|
|
#include "powermanagementinterfacelogging.h"
|
2021-04-11 15:26:28 +02:00
|
|
|
|
|
|
|
#include <KLocalizedString>
|
|
|
|
|
|
|
|
#if !defined Q_OS_ANDROID && !defined Q_OS_WIN
|
|
|
|
#include <QDBusConnection>
|
|
|
|
#include <QDBusPendingCallWatcher>
|
|
|
|
#include <QDBusPendingReply>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined Q_OS_WIN
|
2021-05-01 21:35:37 +02:00
|
|
|
#include <windows.h>
|
2021-04-11 15:26:28 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <QCoreApplication>
|
2021-05-01 21:35:37 +02:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QString>
|
2021-04-11 15:26:28 +02:00
|
|
|
|
2021-05-03 10:43:24 +02:00
|
|
|
#if !defined Q_OS_ANDROID && !defined Q_OS_WIN
|
2021-04-30 23:04:34 +02:00
|
|
|
#include "gnomesessioninterface.h"
|
2021-05-01 21:35:37 +02:00
|
|
|
#include "inhibitinterface.h"
|
2021-05-03 10:43:24 +02:00
|
|
|
#endif
|
2021-04-11 15:26:28 +02:00
|
|
|
|
|
|
|
class PowerManagementInterfacePrivate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool mPreventSleep = false;
|
|
|
|
|
|
|
|
bool mInhibitedSleep = false;
|
|
|
|
|
|
|
|
uint mInhibitSleepCookie = 0;
|
|
|
|
|
2021-04-30 23:04:34 +02:00
|
|
|
uint mGnomeSleepCookie = 0;
|
|
|
|
|
2021-04-11 15:26:28 +02:00
|
|
|
#if !defined Q_OS_ANDROID && !defined Q_OS_WIN
|
2021-05-01 21:35:37 +02:00
|
|
|
OrgFreedesktopPowerManagementInhibitInterface *mInhibitInterface;
|
|
|
|
OrgGnomeSessionManagerInterface *mGnomeInterface;
|
2021-04-11 15:26:28 +02:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2021-05-01 21:35:37 +02:00
|
|
|
PowerManagementInterface::PowerManagementInterface(QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, d(std::make_unique<PowerManagementInterfacePrivate>())
|
2021-04-11 15:26:28 +02:00
|
|
|
{
|
|
|
|
#if !defined Q_OS_ANDROID && !defined Q_OS_WIN
|
2021-04-30 23:04:34 +02:00
|
|
|
d->mInhibitInterface = new OrgFreedesktopPowerManagementInhibitInterface(QStringLiteral("org.freedesktop.PowerManagement.Inhibit"),
|
2021-05-01 21:35:37 +02:00
|
|
|
QStringLiteral("/org/freedesktop/PowerManagement/Inhibit"),
|
|
|
|
QDBusConnection::sessionBus(),
|
|
|
|
this);
|
2021-04-30 23:04:34 +02:00
|
|
|
|
|
|
|
d->mGnomeInterface = new OrgGnomeSessionManagerInterface(QStringLiteral("org.gnome.SessionManager"),
|
2021-05-01 21:35:37 +02:00
|
|
|
QStringLiteral("/org/gnome/SessionManager"),
|
|
|
|
QDBusConnection::sessionBus(),
|
|
|
|
this);
|
2021-04-11 15:26:28 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-04-30 23:04:34 +02:00
|
|
|
PowerManagementInterface::~PowerManagementInterface()
|
|
|
|
{
|
2021-05-01 21:21:09 +02:00
|
|
|
#if !defined Q_OS_ANDROID && !defined Q_OS_WIN
|
2021-04-30 23:04:34 +02:00
|
|
|
delete d->mInhibitInterface;
|
2021-05-01 21:21:09 +02:00
|
|
|
delete d->mGnomeInterface;
|
|
|
|
#endif
|
|
|
|
}
|
2021-04-11 15:26:28 +02:00
|
|
|
|
|
|
|
bool PowerManagementInterface::preventSleep() const
|
|
|
|
{
|
|
|
|
return d->mPreventSleep;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PowerManagementInterface::sleepInhibited() const
|
|
|
|
{
|
|
|
|
return d->mInhibitedSleep;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PowerManagementInterface::setPreventSleep(bool value)
|
|
|
|
{
|
|
|
|
if (d->mPreventSleep == value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value) {
|
|
|
|
inhibitSleepPlasmaWorkspace();
|
|
|
|
inhibitSleepGnomeWorkspace();
|
|
|
|
d->mPreventSleep = true;
|
|
|
|
} else {
|
|
|
|
uninhibitSleepPlasmaWorkspace();
|
|
|
|
uninhibitSleepGnomeWorkspace();
|
|
|
|
d->mPreventSleep = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Q_EMIT preventSleepChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PowerManagementInterface::retryInhibitingSleep()
|
|
|
|
{
|
|
|
|
if (d->mPreventSleep && !d->mInhibitedSleep) {
|
|
|
|
inhibitSleepPlasmaWorkspace();
|
|
|
|
inhibitSleepGnomeWorkspace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PowerManagementInterface::hostSleepInhibitChanged()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void PowerManagementInterface::inhibitDBusCallFinishedPlasmaWorkspace(QDBusPendingCallWatcher *aWatcher)
|
|
|
|
{
|
|
|
|
#if !defined Q_OS_ANDROID && !defined Q_OS_WIN
|
|
|
|
QDBusPendingReply<uint> reply = *aWatcher;
|
|
|
|
if (reply.isError()) {
|
|
|
|
} else {
|
|
|
|
d->mInhibitSleepCookie = reply.argumentAt<0>();
|
|
|
|
d->mInhibitedSleep = true;
|
|
|
|
|
|
|
|
Q_EMIT sleepInhibitedChanged();
|
|
|
|
}
|
|
|
|
aWatcher->deleteLater();
|
|
|
|
#else
|
|
|
|
Q_UNUSED(aWatcher)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void PowerManagementInterface::uninhibitDBusCallFinishedPlasmaWorkspace(QDBusPendingCallWatcher *aWatcher)
|
|
|
|
{
|
|
|
|
#if !defined Q_OS_ANDROID && !defined Q_OS_WIN
|
|
|
|
QDBusPendingReply<> reply = *aWatcher;
|
|
|
|
if (reply.isError()) {
|
2021-06-05 20:12:42 +02:00
|
|
|
qCDebug(kastsPowerManagementInterface) << "PowerManagementInterface::uninhibitDBusCallFinished" << reply.error();
|
2021-04-11 15:26:28 +02:00
|
|
|
} else {
|
|
|
|
d->mInhibitedSleep = false;
|
|
|
|
|
|
|
|
Q_EMIT sleepInhibitedChanged();
|
|
|
|
}
|
|
|
|
aWatcher->deleteLater();
|
|
|
|
#else
|
|
|
|
Q_UNUSED(aWatcher)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void PowerManagementInterface::inhibitDBusCallFinishedGnomeWorkspace(QDBusPendingCallWatcher *aWatcher)
|
|
|
|
{
|
|
|
|
#if !defined Q_OS_ANDROID && !defined Q_OS_WIN
|
|
|
|
QDBusPendingReply<uint> reply = *aWatcher;
|
|
|
|
if (reply.isError()) {
|
2021-06-05 20:12:42 +02:00
|
|
|
qCDebug(kastsPowerManagementInterface) << "PowerManagementInterface::inhibitDBusCallFinishedGnomeWorkspace" << reply.error();
|
2021-04-11 15:26:28 +02:00
|
|
|
} else {
|
2021-04-30 23:04:34 +02:00
|
|
|
d->mGnomeSleepCookie = reply.argumentAt<0>();
|
2021-04-11 15:26:28 +02:00
|
|
|
d->mInhibitedSleep = true;
|
|
|
|
|
|
|
|
Q_EMIT sleepInhibitedChanged();
|
|
|
|
}
|
|
|
|
aWatcher->deleteLater();
|
|
|
|
#else
|
|
|
|
Q_UNUSED(aWatcher)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void PowerManagementInterface::uninhibitDBusCallFinishedGnomeWorkspace(QDBusPendingCallWatcher *aWatcher)
|
|
|
|
{
|
|
|
|
#if !defined Q_OS_ANDROID && !defined Q_OS_WIN
|
|
|
|
QDBusPendingReply<> reply = *aWatcher;
|
|
|
|
if (reply.isError()) {
|
2021-06-05 20:12:42 +02:00
|
|
|
qCDebug(kastsPowerManagementInterface) << "PowerManagementInterface::uninhibitDBusCallFinished" << reply.error();
|
2021-04-11 15:26:28 +02:00
|
|
|
} else {
|
|
|
|
d->mInhibitedSleep = false;
|
|
|
|
|
|
|
|
Q_EMIT sleepInhibitedChanged();
|
|
|
|
}
|
|
|
|
aWatcher->deleteLater();
|
|
|
|
#else
|
|
|
|
Q_UNUSED(aWatcher)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void PowerManagementInterface::inhibitSleepPlasmaWorkspace()
|
|
|
|
{
|
|
|
|
#if !defined Q_OS_ANDROID && !defined Q_OS_WIN
|
2021-05-01 21:35:37 +02:00
|
|
|
auto asyncReply =
|
2021-05-28 22:16:14 +02:00
|
|
|
d->mInhibitInterface->Inhibit(QCoreApplication::applicationName(), i18nc("Explanation for sleep inhibit during play of music", "Playing Music"));
|
2021-04-11 15:26:28 +02:00
|
|
|
|
|
|
|
auto replyWatcher = new QDBusPendingCallWatcher(asyncReply, this);
|
|
|
|
|
2021-05-01 21:35:37 +02:00
|
|
|
QObject::connect(replyWatcher, &QDBusPendingCallWatcher::finished, this, &PowerManagementInterface::inhibitDBusCallFinishedPlasmaWorkspace);
|
2021-04-11 15:26:28 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void PowerManagementInterface::uninhibitSleepPlasmaWorkspace()
|
|
|
|
{
|
|
|
|
#if !defined Q_OS_ANDROID && !defined Q_OS_WIN
|
2021-04-30 23:04:34 +02:00
|
|
|
auto asyncReply = d->mInhibitInterface->UnInhibit(d->mInhibitSleepCookie);
|
2021-04-11 15:26:28 +02:00
|
|
|
|
|
|
|
auto replyWatcher = new QDBusPendingCallWatcher(asyncReply, this);
|
|
|
|
|
2021-05-01 21:35:37 +02:00
|
|
|
QObject::connect(replyWatcher, &QDBusPendingCallWatcher::finished, this, &PowerManagementInterface::uninhibitDBusCallFinishedPlasmaWorkspace);
|
2021-04-11 15:26:28 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void PowerManagementInterface::inhibitSleepGnomeWorkspace()
|
|
|
|
{
|
|
|
|
#if !defined Q_OS_ANDROID && !defined Q_OS_WIN
|
2021-06-15 11:27:49 +02:00
|
|
|
// flags are: 4 = "Inhibit suspending the session or computer"
|
|
|
|
// 8 = "Inhibit the session being marked as idle"
|
2021-04-30 23:04:34 +02:00
|
|
|
auto asyncReply = d->mGnomeInterface->Inhibit(QCoreApplication::applicationName(),
|
|
|
|
uint(0),
|
2021-05-28 22:16:14 +02:00
|
|
|
i18nc("Explanation for sleep inhibit during play of music", "Playing Music"),
|
2021-06-15 11:27:49 +02:00
|
|
|
uint(12));
|
2021-04-11 15:26:28 +02:00
|
|
|
|
|
|
|
auto replyWatcher = new QDBusPendingCallWatcher(asyncReply, this);
|
|
|
|
|
2021-05-01 21:35:37 +02:00
|
|
|
QObject::connect(replyWatcher, &QDBusPendingCallWatcher::finished, this, &PowerManagementInterface::inhibitDBusCallFinishedGnomeWorkspace);
|
2021-04-11 15:26:28 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void PowerManagementInterface::uninhibitSleepGnomeWorkspace()
|
|
|
|
{
|
|
|
|
#if !defined Q_OS_ANDROID && !defined Q_OS_WIN
|
2021-04-30 23:04:34 +02:00
|
|
|
auto asyncReply = d->mGnomeInterface->Uninhibit(d->mGnomeSleepCookie);
|
2021-04-11 15:26:28 +02:00
|
|
|
|
|
|
|
auto replyWatcher = new QDBusPendingCallWatcher(asyncReply, this);
|
|
|
|
|
2021-05-01 21:35:37 +02:00
|
|
|
QObject::connect(replyWatcher, &QDBusPendingCallWatcher::finished, this, &PowerManagementInterface::uninhibitDBusCallFinishedGnomeWorkspace);
|
2021-04-11 15:26:28 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void PowerManagementInterface::inhibitSleepWindowsWorkspace()
|
|
|
|
{
|
|
|
|
#if defined Q_OS_WIN
|
|
|
|
SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void PowerManagementInterface::uninhibitSleepWindowsWorkspace()
|
|
|
|
{
|
|
|
|
#if defined Q_OS_WIN
|
|
|
|
SetThreadExecutionState(ES_CONTINUOUS);
|
|
|
|
#endif
|
|
|
|
}
|