diff --git a/CMakeLists.txt b/CMakeLists.txt index 7bcb168..8a8087e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/powerman/CMakeLists.txt b/powerman/CMakeLists.txt deleted file mode 100644 index 8d30123..0000000 --- a/powerman/CMakeLists.txt +++ /dev/null @@ -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}) diff --git a/powerman/application.cpp b/powerman/application.cpp deleted file mode 100644 index 05e9701..0000000 --- a/powerman/application.cpp +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (C) 2021 CutefishOS Team. - * - * Author: Reion Wong - * - * 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 . - */ - -#include "application.h" -#include "powermanageradaptor.h" - -#include -#include - -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); -} diff --git a/powerman/application.h b/powerman/application.h deleted file mode 100644 index 2e6a102..0000000 --- a/powerman/application.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (C) 2021 CutefishOS Team. - * - * Author: Reion Wong - * - * 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 . - */ - -#ifndef APPLICATION_H -#define APPLICATION_H - -#include -#include -#include -#include - -#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 diff --git a/powerman/lidwatcher.cpp b/powerman/lidwatcher.cpp deleted file mode 100644 index c4b5d8d..0000000 --- a/powerman/lidwatcher.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include "lidwatcher.h" -#include - -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(); - } -} diff --git a/powerman/lidwatcher.h b/powerman/lidwatcher.h deleted file mode 100644 index 27ecb21..0000000 --- a/powerman/lidwatcher.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef LIDWATCHER_H -#define LIDWATCHER_H - -#include -#include - -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 diff --git a/powerman/main.cpp b/powerman/main.cpp deleted file mode 100644 index 409cbd2..0000000 --- a/powerman/main.cpp +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (C) 2021 CutefishOS Team. - * - * Author: Reion Wong - * - * 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 . - */ - -#include -#include "application.h" - -int main(int argc, char *argv[]) -{ - QApplication a(argc, argv); - Application app; - a.setQuitOnLastWindowClosed(false); - return a.exec(); -} diff --git a/session/processmanager.cpp b/session/processmanager.cpp index 752b112..70529b0 100644 --- a/session/processmanager.cpp +++ b/session/processmanager.cpp @@ -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. diff --git a/settings-daemon/CMakeLists.txt b/settings-daemon/CMakeLists.txt index e30caa6..1d9fabb 100644 --- a/settings-daemon/CMakeLists.txt +++ b/settings-daemon/CMakeLists.txt @@ -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) diff --git a/settings-daemon/application.cpp b/settings-daemon/application.cpp index 271aa74..35bf563 100644 --- a/settings-daemon/application.cpp +++ b/settings-daemon/application.cpp @@ -19,6 +19,7 @@ #include "application.h" #include "dbusadaptor.h" +#include "powermanageradaptor.h" #include #include #include @@ -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; diff --git a/settings-daemon/application.h b/settings-daemon/application.h index 3c31440..87f86ee 100644 --- a/settings-daemon/application.h +++ b/settings-daemon/application.h @@ -28,6 +28,8 @@ #include "mouse/mousemanager.h" #include "touchpad/touchpadmanager.h" #include "defaultapplications.h" +#include "power/powermanager.h" +#include "power/cpumanagement.h" #include @@ -49,6 +51,8 @@ private: Mouse *m_mouse; TouchpadManager *m_touchpad; DefaultApplications *m_defaultApps; + CPUManagement *m_cpuManagement; + PowerManager *m_powerManager; }; #endif // APPLICATION_H diff --git a/powerman/action.cpp b/settings-daemon/power/action.cpp similarity index 100% rename from powerman/action.cpp rename to settings-daemon/power/action.cpp diff --git a/powerman/action.h b/settings-daemon/power/action.h similarity index 100% rename from powerman/action.h rename to settings-daemon/power/action.h diff --git a/powerman/cpu/com.cutefish.CPUManagement.xml b/settings-daemon/power/com.cutefish.CPUManagement.xml similarity index 100% rename from powerman/cpu/com.cutefish.CPUManagement.xml rename to settings-daemon/power/com.cutefish.CPUManagement.xml diff --git a/powerman/com.cutefish.PowerManager.xml b/settings-daemon/power/com.cutefish.PowerManager.xml similarity index 54% rename from powerman/com.cutefish.PowerManager.xml rename to settings-daemon/power/com.cutefish.PowerManager.xml index d92e1d4..fe56222 100644 --- a/powerman/com.cutefish.PowerManager.xml +++ b/settings-daemon/power/com.cutefish.PowerManager.xml @@ -1,16 +1,21 @@ - + + + + + + + - + - - - \ No newline at end of file + diff --git a/powerman/cpu/cpuitem.cpp b/settings-daemon/power/cpuitem.cpp similarity index 100% rename from powerman/cpu/cpuitem.cpp rename to settings-daemon/power/cpuitem.cpp diff --git a/powerman/cpu/cpuitem.h b/settings-daemon/power/cpuitem.h similarity index 100% rename from powerman/cpu/cpuitem.h rename to settings-daemon/power/cpuitem.h diff --git a/powerman/cpu/cpumanagement.cpp b/settings-daemon/power/cpumanagement.cpp similarity index 100% rename from powerman/cpu/cpumanagement.cpp rename to settings-daemon/power/cpumanagement.cpp diff --git a/powerman/cpu/cpumanagement.h b/settings-daemon/power/cpumanagement.h similarity index 100% rename from powerman/cpu/cpumanagement.h rename to settings-daemon/power/cpumanagement.h diff --git a/powerman/dimdisplayaction.cpp b/settings-daemon/power/dimdisplayaction.cpp similarity index 100% rename from powerman/dimdisplayaction.cpp rename to settings-daemon/power/dimdisplayaction.cpp diff --git a/powerman/dimdisplayaction.h b/settings-daemon/power/dimdisplayaction.h similarity index 100% rename from powerman/dimdisplayaction.h rename to settings-daemon/power/dimdisplayaction.h diff --git a/powerman/idlemanager.cpp b/settings-daemon/power/idlemanager.cpp similarity index 100% rename from powerman/idlemanager.cpp rename to settings-daemon/power/idlemanager.cpp diff --git a/powerman/idlemanager.h b/settings-daemon/power/idlemanager.h similarity index 100% rename from powerman/idlemanager.h rename to settings-daemon/power/idlemanager.h diff --git a/powerman/org.freedesktop.ScreenSaver.xml b/settings-daemon/power/org.freedesktop.ScreenSaver.xml similarity index 100% rename from powerman/org.freedesktop.ScreenSaver.xml rename to settings-daemon/power/org.freedesktop.ScreenSaver.xml diff --git a/settings-daemon/power/powermanager.cpp b/settings-daemon/power/powermanager.cpp new file mode 100644 index 0000000..acb5877 --- /dev/null +++ b/settings-daemon/power/powermanager.cpp @@ -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(); +} diff --git a/settings-daemon/power/powermanager.h b/settings-daemon/power/powermanager.h new file mode 100644 index 0000000..d074296 --- /dev/null +++ b/settings-daemon/power/powermanager.h @@ -0,0 +1,44 @@ +#ifndef CUTEFISH_SETTINGS_POWERMANAGER_H +#define CUTEFISH_SETTINGS_POWERMANAGER_H + +#include +#include +#include + +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