From dc2372ef3fff66fcd102935c3a44a8e982f5786a Mon Sep 17 00:00:00 2001 From: ajp_anton Date: Tue, 20 Oct 2026 13:30:03 +0000 Subject: [PATCH] Persist Framework power-button lighting across reboots --- framework-laptop-tools/CMakeLists.txt | 6 +++- framework-laptop-tools/README.md | 8 +++++ .../framework-laptop-tools-lighting.service | 22 +++++++++++++ .../framework-laptop-tools.spec | 9 ++++-- framework-laptop-tools/src/helper.cpp | 13 +++++++- framework-laptop-tools/src/lighting.cpp | 32 +++++++++++++++++++ framework-laptop-tools/src/lighting.h | 7 ++++ .../src/lightingservice.cpp | 17 ++++++++++ framework-laptop-tools/src/window.cpp | 2 +- .../tests/test-hardware.cpp | 31 ++++++++++++++++++ 10 files changed, 142 insertions(+), 5 deletions(-) create mode 100644 framework-laptop-tools/data/framework-laptop-tools-lighting.service create mode 100644 framework-laptop-tools/src/lighting.cpp create mode 100644 framework-laptop-tools/src/lighting.h create mode 100644 framework-laptop-tools/src/lightingservice.cpp diff --git a/framework-laptop-tools/CMakeLists.txt b/framework-laptop-tools/CMakeLists.txt index 6b8803b..f5ae147 100644 --- a/framework-laptop-tools/CMakeLists.txt +++ b/framework-laptop-tools/CMakeLists.txt @@ -13,7 +13,7 @@ find_package(KF6Auth REQUIRED) find_package(KF6Service REQUIRED) find_package(PkgConfig REQUIRED) pkg_check_modules(Systemd REQUIRED IMPORTED_TARGET libsystemd) -add_library(framework-hardware STATIC src/hardware.cpp src/fan.cpp src/cpu.cpp src/power.cpp) +add_library(framework-hardware STATIC src/hardware.cpp src/fan.cpp src/cpu.cpp src/power.cpp src/lighting.cpp) target_include_directories(framework-hardware PUBLIC src) target_link_libraries(framework-hardware PUBLIC Qt6::Core) set(gui_sources src/window.cpp src/chart.cpp src/legend.cpp src/tray.cpp src/traypage.cpp src/colorbutton.cpp src/cpupage.cpp src/valuecontrol.cpp src/tooltip.cpp src/processusage.cpp) @@ -25,16 +25,20 @@ add_executable(framework-laptop-tools-fan src/fanservice.cpp) target_link_libraries(framework-laptop-tools-fan PRIVATE framework-hardware PkgConfig::Systemd) add_executable(framework-laptop-tools-cpu src/cpuservice.cpp) target_link_libraries(framework-laptop-tools-cpu PRIVATE framework-hardware Qt6::DBus PkgConfig::Systemd) +add_executable(framework-laptop-tools-lighting src/lightingservice.cpp) +target_link_libraries(framework-laptop-tools-lighting PRIVATE framework-hardware) install(TARGETS framework-laptop-tools DESTINATION ${KDE_INSTALL_BINDIR}) install(TARGETS framework-laptop-tools-helper DESTINATION ${KAUTH_HELPER_INSTALL_DIR}) install(TARGETS framework-laptop-tools-fan DESTINATION libexec) install(TARGETS framework-laptop-tools-cpu DESTINATION libexec) +install(TARGETS framework-laptop-tools-lighting DESTINATION libexec) kauth_install_helper_files(framework-laptop-tools-helper se.ajpanton.frameworktools root) kauth_install_actions(se.ajpanton.frameworktools data/se.ajpanton.frameworktools.actions) install(FILES data/se.ajpanton.framework-laptop-tools.desktop DESTINATION ${KDE_INSTALL_APPDIR}) install(FILES data/framework-laptop-tools.json DESTINATION ${KDE_INSTALL_DATADIR}/fedora-tools/settings) install(FILES data/framework-laptop-tools-fan.service data/framework-laptop-tools-fan-resume.service DESTINATION /usr/lib/systemd/system) install(FILES data/framework-laptop-tools-cpu.service DESTINATION /usr/lib/systemd/system) +install(FILES data/framework-laptop-tools-lighting.service DESTINATION /usr/lib/systemd/system) if(BUILD_TESTING) find_package(Qt6 6.8 REQUIRED COMPONENTS Test) add_executable(test-hardware tests/test-hardware.cpp) diff --git a/framework-laptop-tools/README.md b/framework-laptop-tools/README.md index 056b544..6a650bd 100644 --- a/framework-laptop-tools/README.md +++ b/framework-laptop-tools/README.md @@ -139,10 +139,16 @@ the system tray to suppress that effect. - Keyboard brightness: 0–100%. **Use Fn+Space to leave or enter Auto mode.** The inspected 13 Pro firmware does not expose a host command to change that mode; its ambient-light logic overrides manual percentage changes in Auto. + The tool does not save or restore keyboard brightness itself; Fedora's + backlight service may restore it, while firmware Auto can override it. - Power-button brightness: 1–100%, or firmware Auto. This is the illuminated power button surrounding the fingerprint reader, not its authentication. The Automatic brightness checkbox disables the slider; both mode and brightness are staged until Save and Apply. Unchecking it allows a fixed brightness to be selected. + Saving writes `/etc/framework-laptop-tools/power-button.json` and enables a + one-shot service to restore the choice at boot, even without the GUI running. + Installation alone does not enable it. After upgrading from an older version, + change and save the power-button setting once to enable restoration. - Fan: firmware Auto, manual duty, or a four-point curve. Manual values range from 0–100%, including fan off. Manual speeds below 30% show a warning. Saving manual or curve speeds below 30% also requires explicit confirmation. @@ -165,6 +171,8 @@ the system tray to suppress that effect. vary with voltage and system/charger limits. Zero restores firmware defaults. This is **battery charging power**, not wall power or total laptop power. Charge current has no read-back command in the interface used here. + The charging-power limit is currently applied only for the running session; + it is not saved or restored by this tool. The C-rate used by some other utilities expresses current relative to battery capacity: 1 C means 4.64 A for a 4.64 Ah battery, not a fixed number of watts. - CPU: minimum/maximum frequency, governor and energy preference, with optional diff --git a/framework-laptop-tools/data/framework-laptop-tools-lighting.service b/framework-laptop-tools/data/framework-laptop-tools-lighting.service new file mode 100644 index 0000000..747a218 --- /dev/null +++ b/framework-laptop-tools/data/framework-laptop-tools-lighting.service @@ -0,0 +1,22 @@ +[Unit] +Description=Restore saved Framework power-button lighting +After=systemd-modules-load.service +ConditionPathExists=/etc/framework-laptop-tools/power-button.json +StartLimitIntervalSec=60 +StartLimitBurst=5 + +[Service] +Type=oneshot +ExecStart=/usr/libexec/framework-laptop-tools-lighting +# Allow briefly for the embedded-controller driver to become available at boot. +Restart=on-failure +RestartSec=2 +TimeoutStartSec=10 +NoNewPrivileges=yes +ProtectHome=yes +PrivateTmp=yes +ProtectSystem=strict +RestrictAddressFamilies=AF_UNIX + +[Install] +WantedBy=multi-user.target diff --git a/framework-laptop-tools/framework-laptop-tools.spec b/framework-laptop-tools/framework-laptop-tools.spec index 0efe38b..19da9b3 100644 --- a/framework-laptop-tools/framework-laptop-tools.spec +++ b/framework-laptop-tools/framework-laptop-tools.spec @@ -1,6 +1,6 @@ Name: framework-laptop-tools Version: 0.1.0 -Release: 28%{?dist} +Release: 29%{?dist} Summary: Hardware controls and monitoring for Framework Laptop 13 Pro License: MIT URL: https://git.ajpanton.se/ajp_anton/fedora-tools @@ -37,7 +37,7 @@ install -Dpm 0644 %{SOURCE1} %{buildroot}%{_licensedir}/%{name}/LICENSE %post %systemd_post framework-laptop-tools-fan.service framework-laptop-tools-fan-resume.service %preun -%systemd_preun framework-laptop-tools-fan.service framework-laptop-tools-fan-resume.service framework-laptop-tools-cpu.service +%systemd_preun framework-laptop-tools-fan.service framework-laptop-tools-fan-resume.service framework-laptop-tools-cpu.service framework-laptop-tools-lighting.service %postun %systemd_postun_with_restart framework-laptop-tools-fan.service framework-laptop-tools-cpu.service %files @@ -46,6 +46,7 @@ install -Dpm 0644 %{SOURCE1} %{buildroot}%{_licensedir}/%{name}/LICENSE %{_bindir}/framework-laptop-tools %{_libexecdir}/framework-laptop-tools-fan %{_libexecdir}/framework-laptop-tools-cpu +%{_libexecdir}/framework-laptop-tools-lighting %{_libexecdir}/kf6/kauth/framework-laptop-tools-helper %{_datadir}/applications/se.ajpanton.framework-laptop-tools.desktop %{_datadir}/fedora-tools/settings/framework-laptop-tools.json @@ -55,7 +56,11 @@ install -Dpm 0644 %{SOURCE1} %{buildroot}%{_licensedir}/%{name}/LICENSE %{_unitdir}/framework-laptop-tools-fan.service %{_unitdir}/framework-laptop-tools-fan-resume.service %{_unitdir}/framework-laptop-tools-cpu.service +%{_unitdir}/framework-laptop-tools-lighting.service %changelog +* Wed Sep 23 2026 fedora-tools contributors - 0.1.0-29 +- Save power-button brightness or Auto mode and restore it at boot + * Fri Sep 18 2026 fedora-tools contributors - 0.1.0-28 - Add an optional second numeric tray reading and P/E-core maximum frequencies diff --git a/framework-laptop-tools/src/helper.cpp b/framework-laptop-tools/src/helper.cpp index e4d7f21..d24de9e 100644 --- a/framework-laptop-tools/src/helper.cpp +++ b/framework-laptop-tools/src/helper.cpp @@ -2,6 +2,7 @@ #include "hardware.h" #include "fan.h" #include "cpu.h" +#include "lighting.h" #include #include #include @@ -122,7 +123,17 @@ public Q_SLOTS: error = saveConfig(savedFanConfigPath, args); if (error.isEmpty()) error = service("start"); } - } else if (operation == "powerAuto") error = setFirmwareValue(operation, 0); + } else if (operation == "powerAuto" || operation == "powerBrightness") { + const QVariantMap config{{"operation", operation}, {"value", operation == "powerAuto" ? 0 : value}}; + error = validatePowerButtonConfig(config); + if (!error.isEmpty()) return reply(error); + error = saveConfig(powerButtonConfigPath, config); + if (!error.isEmpty()) return reply(error); + const QString unit = "framework-laptop-tools-lighting.service"; + error = service("enable", unit); + if (error.isEmpty()) error = service("restart", unit); + if (!error.isEmpty()) error = "Power-button settings were saved, but applying them failed: " + error; + } else if (value >= 0) error = setFirmwareValue(operation, value); else error = "Unknown operation or invalid value."; return reply(error); diff --git a/framework-laptop-tools/src/lighting.cpp b/framework-laptop-tools/src/lighting.cpp new file mode 100644 index 0000000..3b3149d --- /dev/null +++ b/framework-laptop-tools/src/lighting.cpp @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: MIT +#include "lighting.h" +#include +#include +#include + +QString validatePowerButtonConfig(const QVariantMap &config) +{ + const auto operation = config.value("operation").toString(); + if (operation == "powerAuto") return {}; + bool valid; + const double value = config.value("value").toDouble(&valid); + if (operation == "powerBrightness" && valid && value >= 1 && value <= 100 && std::floor(value) == value) + return {}; + return "Invalid power-button setting: choose Auto or a whole-number brightness from 1 to 100%."; +} + +QVariantMap readPowerButtonConfig(QString &error, const QString &path) +{ + error.clear(); + QFile file(path); + if (!file.open(QIODevice::ReadOnly)) { error = file.errorString(); return {}; } + QJsonParseError parseError; + const auto document = QJsonDocument::fromJson(file.readAll(), &parseError); + if (parseError.error != QJsonParseError::NoError || !document.isObject()) { + error = "Invalid power-button settings file."; + return {}; + } + const auto config = document.toVariant().toMap(); + error = validatePowerButtonConfig(config); + return error.isEmpty() ? config : QVariantMap{}; +} diff --git a/framework-laptop-tools/src/lighting.h b/framework-laptop-tools/src/lighting.h new file mode 100644 index 0000000..f78ee3c --- /dev/null +++ b/framework-laptop-tools/src/lighting.h @@ -0,0 +1,7 @@ +// SPDX-License-Identifier: MIT +#pragma once +#include + +inline const QString powerButtonConfigPath = QStringLiteral("/etc/framework-laptop-tools/power-button.json"); +QString validatePowerButtonConfig(const QVariantMap &config); +QVariantMap readPowerButtonConfig(QString &error, const QString &path = powerButtonConfigPath); diff --git a/framework-laptop-tools/src/lightingservice.cpp b/framework-laptop-tools/src/lightingservice.cpp new file mode 100644 index 0000000..c8c1c9c --- /dev/null +++ b/framework-laptop-tools/src/lightingservice.cpp @@ -0,0 +1,17 @@ +// SPDX-License-Identifier: MIT +#include "hardware.h" +#include "lighting.h" +#include +#include + +int main(int argc, char **argv) +{ + QCoreApplication app(argc, argv); + QString error; + const auto config = readPowerButtonConfig(error); + if (error.isEmpty()) error = compatibilityError(); + if (error.isEmpty()) error = setFirmwareValue(config["operation"].toString(), config.value("value", 0).toInt()); + if (error.isEmpty()) return 0; + QTextStream(stderr) << "Could not restore power-button lighting: " << error << '\n'; + return 1; +} diff --git a/framework-laptop-tools/src/window.cpp b/framework-laptop-tools/src/window.cpp index 9a757c4..dcd5281 100644 --- a/framework-laptop-tools/src/window.cpp +++ b/framework-laptop-tools/src/window.cpp @@ -243,7 +243,7 @@ QWidget *Window::batteryPage() m_charge = spin(50, 100, "%"); m_charge->setValue(100); m_charge->setObjectName("chargeLimit"); form->addRow("Limit battery:", m_charge); m_watts = spin(0, 75, " W"); m_watts->setSpecialValueText("Firmware default"); m_watts->setObjectName("chargeWatts"); - m_watts->setToolTip("Zero restores firmware defaults. The current charging-power limit cannot be read back."); + m_watts->setToolTip("Applies only for the current session; not saved or restored after reboot. Zero restores firmware defaults. The current charging-power limit cannot be read back."); form->addRow("Limit charging power:", m_watts); form->addRow(note("Limiting charging power is approximate.")); layout->addWidget(group); layout->addStretch(); return page; diff --git a/framework-laptop-tools/tests/test-hardware.cpp b/framework-laptop-tools/tests/test-hardware.cpp index 9e5bda9..7773275 100644 --- a/framework-laptop-tools/tests/test-hardware.cpp +++ b/framework-laptop-tools/tests/test-hardware.cpp @@ -3,6 +3,7 @@ #include "fan.h" #include "cpu.h" #include "power.h" +#include "lighting.h" #include #include #include @@ -15,6 +16,36 @@ class HardwareTest : public QObject { QFile file(path); QVERIFY(file.open(QIODevice::WriteOnly)); QCOMPARE(file.write(value), value.size()); } private Q_SLOTS: + void powerButtonSettings() { + QVERIFY(validatePowerButtonConfig({{"operation", "powerAuto"}}).isEmpty()); + for (const int brightness : {1, 50, 100}) + QVERIFY(validatePowerButtonConfig({{"operation", "powerBrightness"}, {"value", brightness}}).isEmpty()); + for (const QVariant &value : {QVariant(-1), QVariant(0), QVariant(101), QVariant(2.5), QVariant("invalid"), QVariant()}) + QVERIFY(!validatePowerButtonConfig({{"operation", "powerBrightness"}, {"value", value}}).isEmpty()); + QVERIFY(!validatePowerButtonConfig({}).isEmpty()); + QVERIFY(!validatePowerButtonConfig({{"operation", "fan"}, {"value", 50}}).isEmpty()); + + QTemporaryDir directory; + const QString path = directory.filePath("power-button.json"); + QString error; + QVERIFY(readPowerButtonConfig(error, path).isEmpty()); + QVERIFY(!error.isEmpty()); // No saved setting must not turn into a default override. + for (const auto &data : {QByteArray("broken"), QByteArray("[]"), QByteArray("{}"), + QByteArray("{\"operation\":\"powerBrightness\",\"value\":0}"), QByteArray("{\"operation\":\"chargeLimit\",\"value\":80}")}) { + put(path, data); + QVERIFY(readPowerButtonConfig(error, path).isEmpty()); + QVERIFY(!error.isEmpty()); + } + put(path, "{\"operation\":\"powerBrightness\",\"value\":15}"); + const auto fixed = readPowerButtonConfig(error, path); + QVERIFY2(error.isEmpty(), qPrintable(error)); + QCOMPARE(fixed["operation"].toString(), QString("powerBrightness")); + QCOMPARE(fixed["value"].toInt(), 15); + put(path, "{\"operation\":\"powerAuto\",\"value\":0}"); + QCOMPARE(readPowerButtonConfig(error, path)["operation"].toString(), QString("powerAuto")); + QVERIFY(error.isEmpty()); + } + void coreGroupFrequencies() { QTemporaryDir root; const QString base = root.path() + "/devices/system/cpu/cpufreq/";