feat(settings-daemon): merge power policy into settings daemon

main
Reion Wong 3 weeks ago
parent 684161f21a
commit 450ff00a9b

@ -27,7 +27,6 @@ add_subdirectory(screen-brightness)
add_subdirectory(session)
add_subdirectory(settings-daemon)
add_subdirectory(shutdown-ui)
add_subdirectory(powerman)
add_subdirectory(cpufreq)
add_subdirectory(sddm-helper)
add_subdirectory(clipboard)

@ -1,38 +0,0 @@
project(cutefish-powerman)
set(TARGET cutefish-powerman)
find_package(KF6IdleTime)
find_package(KWayland REQUIRED)
set(SOURCES
main.cpp
application.cpp
lidwatcher.cpp
action.cpp
idlemanager.cpp
dimdisplayaction.cpp
cpu/cpuitem.cpp
cpu/cpumanagement.cpp
)
qt6_add_dbus_adaptor(DBUS_SOURCES
cpu/com.cutefish.CPUManagement.xml
cpu/cpumanagement.h CPUManagement)
qt6_add_dbus_adaptor(DBUS_SOURCES
com.cutefish.PowerManager.xml
application.h Application)
qt_add_dbus_interface(DBUS_SOURCES org.freedesktop.ScreenSaver.xml screenlocker_interface)
set_source_files_properties(${DBUS_SOURCES} PROPERTIES SKIP_AUTOGEN ON)
add_executable(${TARGET} ${SOURCES} ${DBUS_SOURCES})
target_link_libraries(${TARGET}
Qt6::Core
Qt6::Gui
Qt6::Widgets
Qt6::Quick
Qt6::DBus
KF6::IdleTime
Plasma::KWaylandClient
)
install(TARGETS ${TARGET} DESTINATION ${CMAKE_INSTALL_BINDIR})

@ -1,73 +0,0 @@
/*
* Copyright (C) 2021 CutefishOS Team.
*
* Author: Reion Wong <reionwong@gmail.com>
*
* 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 3 of the License, or
* 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, see <http://www.gnu.org/licenses/>.
*/
#include "application.h"
#include "powermanageradaptor.h"
#include <QDBusConnection>
#include <QDebug>
Application::Application(QObject *parent)
: QObject(parent)
, m_lidWatcher(new LidWatcher)
, m_cpuManagement(new CPUManagement)
, m_settings("cutefishos", "power")
, m_dimDisplayAction(new DimDisplayAction)
{
m_closeScreenTimeout = m_settings.value("CloseScreenTimeout", 600).toInt();
m_sleepWhenClosedScreen = m_settings.value("SleepWhenClosedScreen", false).toBool();
m_lockWhenClosedScreen = m_settings.value("LockWhenClosedScreen", true).toBool();
// Live
if (QFile("/run/live/medium/live/filesystem.squashfs").exists()) {
m_closeScreenTimeout = -1;
}
m_dimDisplayAction->setTimeout(m_closeScreenTimeout);
m_dimDisplayAction->setSleep(m_sleepWhenClosedScreen);
m_dimDisplayAction->setLock(m_lockWhenClosedScreen);
QDBusConnection::sessionBus().registerService(QStringLiteral("com.cutefish.PowerManager"));
QDBusConnection::sessionBus().registerObject(QStringLiteral("/PowerManager"), this);
new PowerManagerAdaptor(this);
}
void Application::setDimDisplayTimeout(int timeout)
{
m_closeScreenTimeout = timeout;
m_dimDisplayAction->setTimeout(timeout);
m_settings.setValue("CloseScreenTimeout", timeout);
}
void Application::setSleepWhenClosedScreen(bool enabled)
{
m_sleepWhenClosedScreen = enabled;
m_dimDisplayAction->setSleep(enabled);
m_settings.setValue("SleepWhenClosedScreen", enabled);
}
void Application::setLockWhenClosedScreen(bool lock)
{
m_lockWhenClosedScreen = lock;
m_dimDisplayAction->setLock(lock);
m_settings.setValue("LockWhenClosedScreen", lock);
}

@ -1,57 +0,0 @@
/*
* Copyright (C) 2021 CutefishOS Team.
*
* Author: Reion Wong <reionwong@gmail.com>
*
* 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 3 of the License, or
* 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, see <http://www.gnu.org/licenses/>.
*/
#ifndef APPLICATION_H
#define APPLICATION_H
#include <QObject>
#include <QSettings>
#include <QAction>
#include <QSet>
#include "lidwatcher.h"
#include "idlemanager.h"
#include "dimdisplayaction.h"
#include "cpu/cpumanagement.h"
class Application : public QObject
{
Q_OBJECT
public:
explicit Application(QObject *parent = nullptr);
void setDimDisplayTimeout(int timeout);
void setSleepWhenClosedScreen(bool enabled);
void setLockWhenClosedScreen(bool lock);
private:
LidWatcher *m_lidWatcher;
CPUManagement *m_cpuManagement;
QSettings m_settings;
int m_closeScreenTimeout;
bool m_sleepWhenClosedScreen;
bool m_lockWhenClosedScreen;
DimDisplayAction *m_dimDisplayAction;
};
#endif // APPLICATION_H

@ -1,43 +0,0 @@
#include "lidwatcher.h"
#include <QDebug>
LidWatcher::LidWatcher(QObject *parent)
: QObject(parent)
{
m_uPowerIface = new QDBusInterface("org.freedesktop.UPower",
"/org/freedesktop/UPower",
"org.freedesktop.UPower", QDBusConnection::systemBus(), this);
m_uPowerPropertiesIface = new QDBusInterface("org.freedesktop.UPower",
"/org/freedesktop/UPower",
"org.freedesktop.DBus.Properties",
QDBusConnection::systemBus(), this);
QDBusConnection::systemBus().connect("org.freedesktop.UPower",
"/org/freedesktop/UPower",
"org.freedesktop.DBus.Properties",
"PropertiesChanged",
this, SLOT(onPropertiesChanged()));
m_lidClosed = m_uPowerPropertiesIface->property("LidIsClosed").toBool();
}
bool LidWatcher::existLid() const
{
return m_uPowerIface->property("LidIsPresent").toBool();
}
bool LidWatcher::lidClosed() const
{
return m_lidClosed;
}
void LidWatcher::onPropertiesChanged()
{
bool isLidClosed = m_uPowerIface->property("LidIsClosed").toBool();
if (isLidClosed != m_lidClosed) {
m_lidClosed = isLidClosed;
emit lidClosedChanged();
}
}

@ -1,30 +0,0 @@
#ifndef LIDWATCHER_H
#define LIDWATCHER_H
#include <QObject>
#include <QDBusInterface>
class LidWatcher : public QObject
{
Q_OBJECT
public:
explicit LidWatcher(QObject *parent = nullptr);
bool existLid() const;
bool lidClosed() const;
signals:
void lidClosedChanged();
private slots:
void onPropertiesChanged();
private:
QDBusInterface *m_uPowerIface;
QDBusInterface *m_uPowerPropertiesIface;
bool m_lidClosed;
};
#endif // LIDWATCHER_H

@ -1,29 +0,0 @@
/*
* Copyright (C) 2021 CutefishOS Team.
*
* Author: Reion Wong <reionwong@gmail.com>
*
* 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 3 of the License, or
* 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, see <http://www.gnu.org/licenses/>.
*/
#include <QApplication>
#include "application.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Application app;
a.setQuitOnLastWindowClosed(false);
return a.exec();
}

@ -96,7 +96,6 @@ void ProcessManager::startDesktopProcess()
// Desktop components
// The status bar, dock, launcher, desktop and notifications are one process now.
list << qMakePair(QString("cutefish-shell"), QStringList());
list << qMakePair(QString("cutefish-powerman"), QStringList());
list << qMakePair(QString("cutefish-clipboard"), QStringList());
// For CutefishOS.

@ -1,6 +1,9 @@
project(cutefish-settings-daemon)
set(TARGET cutefish-settings-daemon)
find_package(KF6IdleTime REQUIRED)
find_package(KWayland REQUIRED)
file (GLOB_RECURSE SRCS
"*.cpp"
"*.h"
@ -47,6 +50,13 @@ qt6_add_dbus_adaptor(DBUS_SOURCES
qt6_add_dbus_adaptor(DBUS_SOURCES
touchpad/com.cutefish.Touchpad.xml
touchpad/touchpadmanager.h TouchpadManager)
qt6_add_dbus_adaptor(DBUS_SOURCES
power/com.cutefish.PowerManager.xml
power/powermanager.h PowerManager)
qt6_add_dbus_adaptor(DBUS_SOURCES
power/com.cutefish.CPUManagement.xml
power/cpumanagement.h CPUManagement)
qt_add_dbus_interface(DBUS_SOURCES power/org.freedesktop.ScreenSaver.xml screenlocker_interface)
set_source_files_properties(${DBUS_SOURCES} PROPERTIES SKIP_AUTOGEN ON)
add_executable(${TARGET} ${SOURCES} ${DBUS_SOURCES} ${HEADERS} ${FORMS} ${RESOURCES})
@ -57,6 +67,8 @@ target_link_libraries(${TARGET}
Qt6::DBus
Qt6::Xml
KF6::Screen
KF6::IdleTime
Plasma::KWaylandClient
)
file(GLOB TS_FILES translations/*.ts)

@ -19,6 +19,7 @@
#include "application.h"
#include "dbusadaptor.h"
#include "powermanageradaptor.h"
#include <QStandardPaths>
#include <QProcess>
#include <QTimer>
@ -45,12 +46,17 @@ Application::Application(int &argc, char **argv)
, m_mouse(new Mouse(this))
, m_touchpad(new TouchpadManager(this))
, m_defaultApps(new DefaultApplications)
, m_cpuManagement(new CPUManagement(this))
, m_powerManager(new PowerManager(m_upowerManager, this))
{
initTrash();
new DBusAdaptor(this);
// connect to D-Bus and register as an object:
QDBusConnection::sessionBus().registerService(QStringLiteral("com.cutefish.Settings"));
QDBusConnection::sessionBus().registerService(QStringLiteral("com.cutefish.PowerManager"));
QDBusConnection::sessionBus().registerObject(QStringLiteral("/PowerManager"), m_powerManager);
new PowerManagerAdaptor(m_powerManager);
// Translations
QLocale locale;

@ -28,6 +28,8 @@
#include "mouse/mousemanager.h"
#include "touchpad/touchpadmanager.h"
#include "defaultapplications.h"
#include "power/powermanager.h"
#include "power/cpumanagement.h"
#include <QTimer>
@ -49,6 +51,8 @@ private:
Mouse *m_mouse;
TouchpadManager *m_touchpad;
DefaultApplications *m_defaultApps;
CPUManagement *m_cpuManagement;
PowerManager *m_powerManager;
};
#endif // APPLICATION_H

@ -1,16 +1,21 @@
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<!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="setBatteryScreenOff">
<arg name="value" type="i" direction="in"/>
</method>
<method name="setACScreenOff">
<arg name="value" type="i" direction="in"/>
</method>
<method name="setDimDisplayTimeout">
<arg name="value" type="i" direction="in"/>
<arg name="value" type="i" direction="in"/>
</method>
<method name="setSleepWhenClosedScreen">
<arg name="value" type="b" direction="in"/>
</method>
<method name="setLockWhenClosedScreen">
<arg name="value" type="b" direction="in"/>
</method>
</interface>
</node>
</node>

@ -0,0 +1,130 @@
#include "powermanager.h"
#include "../battery/upowermanager.h"
#include "dimdisplayaction.h"
namespace
{
constexpr auto s_powerGroup = "Power";
int normalizedTimeout(int timeout)
{
return timeout < 0 ? -1 : qMax(1, timeout);
}
int readTimeout(QSettings &settings, const QString &key, const QString &legacyKey, int fallback)
{
settings.beginGroup(QLatin1String(s_powerGroup));
const bool hasValue = settings.contains(key);
const int value = hasValue ? settings.value(key).toInt() : fallback;
settings.endGroup();
if (hasValue)
return normalizedTimeout(value);
if (!legacyKey.isEmpty() && settings.contains(legacyKey))
return normalizedTimeout(settings.value(legacyKey).toInt());
return fallback;
}
bool readBool(QSettings &settings, const QString &key, const QString &legacyKey, bool fallback)
{
settings.beginGroup(QLatin1String(s_powerGroup));
const bool hasValue = settings.contains(key);
const bool value = hasValue ? settings.value(key).toBool() : fallback;
settings.endGroup();
if (hasValue)
return value;
return settings.value(legacyKey, fallback).toBool();
}
}
PowerManager::PowerManager(UPowerManager *upowerManager, QObject *parent)
: QObject(parent)
, m_settings(QStringLiteral("cutefishos"), QStringLiteral("power"))
, m_upowerManager(upowerManager)
, m_dimDisplayAction(new DimDisplayAction(this))
{
loadSettings();
m_onBattery = m_upowerManager && m_upowerManager->onBattery();
if (m_upowerManager) {
connect(m_upowerManager, &UPowerManager::onBatteryChanged,
this, &PowerManager::onBatteryChanged);
}
applyPolicy();
}
void PowerManager::loadSettings()
{
m_batteryScreenOff = readTimeout(m_settings, QStringLiteral("BatteryScreenOff"),
QStringLiteral("CloseScreenTimeout"), 300);
m_acScreenOff = readTimeout(m_settings, QStringLiteral("ACScreenOff"),
QStringLiteral("CloseScreenTimeout"), 1200);
m_sleepWhenClosedScreen = readBool(m_settings, QStringLiteral("SleepWhenClosedScreen"),
QStringLiteral("SleepWhenClosedScreen"), false);
m_lockWhenClosedScreen = readBool(m_settings, QStringLiteral("LockWhenClosedScreen"),
QStringLiteral("LockWhenClosedScreen"), true);
}
void PowerManager::applyPolicy()
{
const int timeout = m_onBattery ? m_batteryScreenOff : m_acScreenOff;
m_dimDisplayAction->setTimeout(timeout);
m_dimDisplayAction->setSleep(m_sleepWhenClosedScreen);
m_dimDisplayAction->setLock(m_lockWhenClosedScreen);
}
void PowerManager::writePowerSetting(const QString &key, const QVariant &value)
{
m_settings.beginGroup(QLatin1String(s_powerGroup));
m_settings.setValue(key, value);
m_settings.endGroup();
m_settings.sync();
}
void PowerManager::setBatteryScreenOff(int timeout)
{
m_batteryScreenOff = normalizedTimeout(timeout);
writePowerSetting(QStringLiteral("BatteryScreenOff"), m_batteryScreenOff);
applyPolicy();
}
void PowerManager::setACScreenOff(int timeout)
{
m_acScreenOff = normalizedTimeout(timeout);
writePowerSetting(QStringLiteral("ACScreenOff"), m_acScreenOff);
applyPolicy();
}
void PowerManager::setDimDisplayTimeout(int timeout)
{
setBatteryScreenOff(timeout);
setACScreenOff(timeout);
}
void PowerManager::setSleepWhenClosedScreen(bool enabled)
{
m_sleepWhenClosedScreen = enabled;
writePowerSetting(QStringLiteral("SleepWhenClosedScreen"), enabled);
applyPolicy();
}
void PowerManager::setLockWhenClosedScreen(bool enabled)
{
m_lockWhenClosedScreen = enabled;
writePowerSetting(QStringLiteral("LockWhenClosedScreen"), enabled);
applyPolicy();
}
void PowerManager::onBatteryChanged()
{
m_onBattery = m_upowerManager && m_upowerManager->onBattery();
applyPolicy();
}

@ -0,0 +1,44 @@
#ifndef CUTEFISH_SETTINGS_POWERMANAGER_H
#define CUTEFISH_SETTINGS_POWERMANAGER_H
#include <QDBusInterface>
#include <QObject>
#include <QSettings>
class DimDisplayAction;
class UPowerManager;
class PowerManager : public QObject
{
Q_OBJECT
public:
explicit PowerManager(UPowerManager *upowerManager, QObject *parent = nullptr);
public slots:
void setBatteryScreenOff(int timeout);
void setACScreenOff(int timeout);
void setDimDisplayTimeout(int timeout);
void setSleepWhenClosedScreen(bool enabled);
void setLockWhenClosedScreen(bool enabled);
private slots:
void onBatteryChanged();
private:
void loadSettings();
void applyPolicy();
void writePowerSetting(const QString &key, const QVariant &value);
QSettings m_settings;
UPowerManager *m_upowerManager;
DimDisplayAction *m_dimDisplayAction;
int m_batteryScreenOff = 300;
int m_acScreenOff = 1200;
bool m_sleepWhenClosedScreen = false;
bool m_lockWhenClosedScreen = true;
bool m_onBattery = false;
};
#endif
Loading…
Cancel
Save