Persist Framework power-button lighting across reboots
This commit is contained in:
1 parent
ddb912d4ba
commit
dc2372ef3f
10 files changed
+142
-5
No files matched your search
@@ -2,6 +2,7 @@
|
||||
#include "hardware.h"
|
||||
#include "fan.h"
|
||||
#include "cpu.h"
|
||||
#include "lighting.h"
|
||||
#include <KAuth/ActionReply>
|
||||
#include <KAuth/HelperSupport>
|
||||
#include <QDir>
|
||||
@@ -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);
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
#include "lighting.h"
|
||||
#include <QFile>
|
||||
#include <QJsonDocument>
|
||||
#include <cmath>
|
||||
|
||||
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{};
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
#pragma once
|
||||
#include <QVariantMap>
|
||||
|
||||
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);
|
||||
@@ -0,0 +1,17 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
#include "hardware.h"
|
||||
#include "lighting.h"
|
||||
#include <QCoreApplication>
|
||||
#include <QTextStream>
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user