mirror of https://github.com/cutefishos/core
PowerManager: add dim display feature
parent
04697c1c3f
commit
bc6c46a02f
@ -0,0 +1,60 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2010 by Dario Freddi <drf@kde.org> *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
|
||||
***************************************************************************/
|
||||
|
||||
#include "action.h"
|
||||
#include "idlemanager.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
class Action::Private
|
||||
{
|
||||
public:
|
||||
Private() {}
|
||||
~Private() {}
|
||||
|
||||
QVector< int > registeredIdleTimeouts;
|
||||
};
|
||||
|
||||
Action::Action(QObject* parent)
|
||||
: QObject(parent)
|
||||
, d(new Private)
|
||||
{
|
||||
}
|
||||
|
||||
Action::~Action()
|
||||
{
|
||||
delete d;
|
||||
}
|
||||
|
||||
void Action::registerIdleTimeout(int msec)
|
||||
{
|
||||
d->registeredIdleTimeouts.append(msec);
|
||||
IdleManager::self()->registerActionTimeout(this, msec);
|
||||
}
|
||||
|
||||
bool Action::isSupported()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
void Action::setTimeout(int timeout)
|
||||
{
|
||||
Q_UNUSED(timeout)
|
||||
}
|
||||
|
||||
@ -0,0 +1,89 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2010 by Dario Freddi <drf@kde.org> *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#ifndef POWERDEVIL_POWERDEVILACTION_H
|
||||
#define POWERDEVIL_POWERDEVILACTION_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QVariantMap>
|
||||
|
||||
class IDleManager;
|
||||
class Q_DECL_EXPORT Action : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_DISABLE_COPY(Action)
|
||||
|
||||
public:
|
||||
/**
|
||||
* Default constructor
|
||||
*/
|
||||
explicit Action(QObject *parent);
|
||||
/**
|
||||
* Default destructor
|
||||
*/
|
||||
~Action() override;
|
||||
|
||||
/**
|
||||
* This function is meant to find out if this action is available on this system. Actions
|
||||
* CAN reimplement this function if they are dependent on specific hardware/software requirements.
|
||||
* By default, this function will always return true.
|
||||
*
|
||||
* Should this function return false, the core will delete and ignore the action right after creation.
|
||||
*
|
||||
* @returns Whether this action is supported or not by the current system
|
||||
*/
|
||||
virtual bool isSupported();
|
||||
|
||||
virtual void setTimeout(int timeout);
|
||||
|
||||
protected:
|
||||
/**
|
||||
* Registers an idle timeout for this action. Call this function and not KIdleTime directly to take advantage
|
||||
* of Action's automated handling of idle timeouts. Also, please reimplement onIdleTimeout instead of listening
|
||||
* to KIdleTime's signals to catch idle timeout events.
|
||||
*
|
||||
* @param msec The idle timeout to be registered in milliseconds.
|
||||
*/
|
||||
void registerIdleTimeout(int msec);
|
||||
|
||||
public Q_SLOTS:
|
||||
/**
|
||||
* This slot is triggered whenever an idle timeout registered with registerIdleTimeout is reached.
|
||||
*
|
||||
* @param msec The idle timeout reached in milliseconds
|
||||
*/
|
||||
virtual void onIdleTimeout(int msec) = 0;
|
||||
/**
|
||||
* This slot is triggered whenever the PC wakes up from an Idle state. It is @b always called after a registered
|
||||
* idle timeout has been reached.
|
||||
*/
|
||||
virtual void onWakeupFromIdle() = 0;
|
||||
|
||||
Q_SIGNALS:
|
||||
void actionTriggered(bool result, const QString &error = QString());
|
||||
|
||||
private:
|
||||
class Private;
|
||||
Private * const d;
|
||||
|
||||
friend class IDleManager;
|
||||
};
|
||||
|
||||
#endif // POWERDEVIL_POWERDEVILACTION_H
|
||||
@ -0,0 +1,8 @@
|
||||
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
|
||||
<node>
|
||||
<interface name="com.cutefish.PowerManager">
|
||||
<method name="setDimDisplayTimeout">
|
||||
<arg name="value" type="i" direction="in"/>
|
||||
</method>
|
||||
</interface>
|
||||
</node>
|
||||
@ -0,0 +1,79 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2021 by Reion Wong <reion@cutefishos.com> *
|
||||
* Copyright (C) 2010 by Dario Freddi <drf@kde.org> *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
|
||||
***************************************************************************/
|
||||
|
||||
#include "dimdisplayaction.h"
|
||||
#include <QSettings>
|
||||
#include <QTimer>
|
||||
#include <QDBusPendingCall>
|
||||
#include <QDebug>
|
||||
|
||||
DimDisplayAction::DimDisplayAction(QObject *parent)
|
||||
: Action(parent)
|
||||
, m_iface("com.cutefish.Settings",
|
||||
"/Brightness",
|
||||
"com.cutefish.Brightness", QDBusConnection::sessionBus())
|
||||
{
|
||||
}
|
||||
|
||||
void DimDisplayAction::onWakeupFromIdle()
|
||||
{
|
||||
if (!m_dimmed) {
|
||||
return;
|
||||
}
|
||||
|
||||
// An active inhibition may not let us restore the brightness.
|
||||
// We should wait a bit screen to wake-up from sleep
|
||||
QTimer::singleShot(0, this, [this]() {
|
||||
m_iface.asyncCall("setValue", QVariant::fromValue(m_oldScreenBrightness));
|
||||
});
|
||||
|
||||
m_dimmed = false;
|
||||
}
|
||||
|
||||
void DimDisplayAction::onIdleTimeout(int msec)
|
||||
{
|
||||
int sec = msec / 1000;
|
||||
|
||||
if (m_iface.property("brightness").toInt() == 0)
|
||||
return;
|
||||
|
||||
if (sec == m_dimOnIdleTime) {
|
||||
m_iface.asyncCall("setValue", QVariant::fromValue(0));
|
||||
} else if (sec == (m_dimOnIdleTime * 3 / 4)) {
|
||||
const int newBrightness = qRound(m_oldScreenBrightness / 8.0);
|
||||
m_iface.asyncCall("setValue", QVariant::fromValue(newBrightness));
|
||||
} else if (sec == (m_dimOnIdleTime * 1 / 2)) {
|
||||
m_oldScreenBrightness = m_iface.property("brightness").toInt();
|
||||
|
||||
const int newBrightness = qRound(m_oldScreenBrightness / 2.0);
|
||||
|
||||
m_iface.asyncCall("setValue", QVariant::fromValue(newBrightness));
|
||||
}
|
||||
|
||||
m_dimmed = true;
|
||||
}
|
||||
|
||||
void DimDisplayAction::setTimeout(int timeout)
|
||||
{
|
||||
m_dimOnIdleTime = timeout;
|
||||
registerIdleTimeout(m_dimOnIdleTime * 3 / 4);
|
||||
registerIdleTimeout(m_dimOnIdleTime / 2);
|
||||
registerIdleTimeout(m_dimOnIdleTime);
|
||||
}
|
||||
@ -0,0 +1,45 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2021 by Reion Wong <reion@cutefishos.com> *
|
||||
* Copyright (C) 2010 by Dario Freddi <drf@kde.org> *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef DIMDISPLAYACTION_H
|
||||
#define DIMDISPLAYACTION_H
|
||||
|
||||
#include "action.h"
|
||||
#include <QDBusInterface>
|
||||
|
||||
class DimDisplayAction : public Action
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit DimDisplayAction(QObject *parent = nullptr);
|
||||
|
||||
void onWakeupFromIdle() override;
|
||||
void onIdleTimeout(int msec) override;
|
||||
void setTimeout(int timeout) override;
|
||||
|
||||
private:
|
||||
QDBusInterface m_iface;
|
||||
int m_dimOnIdleTime = 0;
|
||||
int m_oldScreenBrightness = 0;
|
||||
bool m_dimmed = false;
|
||||
};
|
||||
|
||||
#endif // DIMDISPLAYACTION_H
|
||||
@ -0,0 +1,89 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2021 by Reion Wong <reion@cutefishos.com> *
|
||||
* Copyright (C) 2010 by Dario Freddi <drf@kde.org> *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
|
||||
***************************************************************************/
|
||||
|
||||
#include "idlemanager.h"
|
||||
|
||||
#include <KIdleTime>
|
||||
|
||||
IdleManager *IdleManager::self()
|
||||
{
|
||||
static IdleManager mgr;
|
||||
return &mgr;
|
||||
}
|
||||
|
||||
IdleManager::IdleManager(QObject *parent)
|
||||
: QObject(parent)
|
||||
{
|
||||
connect(KIdleTime::instance(), SIGNAL(timeoutReached(int,int)),
|
||||
this, SLOT(onKIdleTimeoutReached(int,int)));
|
||||
connect(KIdleTime::instance(), &KIdleTime::resumingFromIdle,
|
||||
this, &IdleManager::onResumingFromIdle);
|
||||
}
|
||||
|
||||
void IdleManager::registerActionTimeout(Action *action, int secs)
|
||||
{
|
||||
int identifier = KIdleTime::instance()->addIdleTimeout(secs * 1000);
|
||||
|
||||
QList<int> timeouts = m_registeredActionTimeouts[action];
|
||||
timeouts.append(identifier);
|
||||
|
||||
m_registeredActionTimeouts[action] = timeouts;
|
||||
}
|
||||
|
||||
void IdleManager::unregisterActionTimeouts(Action *action)
|
||||
{
|
||||
// Clear all timeouts from the action
|
||||
const QList< int > timeoutsToClean = m_registeredActionTimeouts[action];
|
||||
|
||||
for (int id : timeoutsToClean) {
|
||||
KIdleTime::instance()->removeIdleTimeout(id);
|
||||
}
|
||||
|
||||
m_registeredActionTimeouts.remove(action);
|
||||
}
|
||||
|
||||
void IdleManager::onKIdleTimeoutReached(int identifier, int timeout)
|
||||
{
|
||||
for (auto i = m_registeredActionTimeouts.constBegin(), end = m_registeredActionTimeouts.constEnd(); i != end; ++i) {
|
||||
if (i.value().contains(identifier)) {
|
||||
i.key()->onIdleTimeout(timeout);
|
||||
|
||||
// And it will need to be awaken
|
||||
m_pendingResumeFromIdleActions.insert(i.key());
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Catch the next resume event if some actions require it
|
||||
if (!m_pendingResumeFromIdleActions.isEmpty()) {
|
||||
KIdleTime::instance()->catchNextResumeEvent();
|
||||
}
|
||||
}
|
||||
|
||||
void IdleManager::onResumingFromIdle()
|
||||
{
|
||||
KIdleTime::instance()->simulateUserActivity();
|
||||
// Wake up the actions in which an idle action was triggered
|
||||
std::for_each(m_pendingResumeFromIdleActions.cbegin(), m_pendingResumeFromIdleActions.cend(),
|
||||
std::mem_fn(&Action::onWakeupFromIdle));
|
||||
|
||||
m_pendingResumeFromIdleActions.clear();
|
||||
}
|
||||
@ -0,0 +1,52 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2021 by Reion Wong <reion@cutefishos.com> *
|
||||
* Copyright (C) 2010 by Dario Freddi <drf@kde.org> *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef IDLEMANAGER_H
|
||||
#define IDLEMANAGER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QHash>
|
||||
#include <QSet>
|
||||
|
||||
#include "action.h"
|
||||
|
||||
class IdleManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
static IdleManager *self();
|
||||
explicit IdleManager(QObject *parent = nullptr);
|
||||
|
||||
void registerActionTimeout(Action *action, int secs);
|
||||
void unregisterActionTimeouts(Action *action);
|
||||
|
||||
private slots:
|
||||
void onKIdleTimeoutReached(int, int);
|
||||
void onResumingFromIdle();
|
||||
|
||||
private:
|
||||
friend class Action;
|
||||
|
||||
QHash<Action *, QList<int>> m_registeredActionTimeouts;
|
||||
QSet<Action *> m_pendingResumeFromIdleActions;
|
||||
};
|
||||
|
||||
#endif // IDLEMANAGER_H
|
||||
@ -0,0 +1,41 @@
|
||||
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
|
||||
<node>
|
||||
<interface name="org.freedesktop.ScreenSaver">
|
||||
<signal name="ActiveChanged">
|
||||
<arg type="b"/>
|
||||
</signal>
|
||||
<method name="Lock">
|
||||
</method>
|
||||
<method name="SimulateUserActivity">
|
||||
</method>
|
||||
<method name="GetActive">
|
||||
<arg type="b" direction="out"/>
|
||||
</method>
|
||||
<method name="GetActiveTime">
|
||||
<arg name="seconds" type="u" direction="out"/>
|
||||
</method>
|
||||
<method name="GetSessionIdleTime">
|
||||
<arg name="seconds" type="u" direction="out"/>
|
||||
</method>
|
||||
<method name="SetActive">
|
||||
<arg type="b" direction="out"/>
|
||||
<arg name="e" type="b" direction="in"/>
|
||||
</method>
|
||||
<method name="Inhibit">
|
||||
<arg name="application_name" type="s" direction="in"/>
|
||||
<arg name="reason_for_inhibit" type="s" direction="in"/>
|
||||
<arg name="cookie" type="u" direction="out"/>
|
||||
</method>
|
||||
<method name="UnInhibit">
|
||||
<arg name="cookie" type="u" direction="in"/>
|
||||
</method>
|
||||
<method name="Throttle">
|
||||
<arg name="application_name" type="s" direction="in"/>
|
||||
<arg name="reason_for_inhibit" type="s" direction="in"/>
|
||||
<arg name="cookie" type="u" direction="out"/>
|
||||
</method>
|
||||
<method name="UnThrottle">
|
||||
<arg name="cookie" type="u" direction="in"/>
|
||||
</method>
|
||||
</interface>
|
||||
</node>
|
||||
Loading…
Reference in New Issue