diff --git a/packages/omamobile-device-husky/README.md b/packages/omamobile-device-husky/README.md index 79749e243..645ef820b 100644 --- a/packages/omamobile-device-husky/README.md +++ b/packages/omamobile-device-husky/README.md @@ -18,6 +18,7 @@ Omarchy Mobile's first userspace package for the Google Pixel 8 Pro (codename hu | `vendor/modprobe.d/omamobile-max77759.conf` | `/usr/lib/modprobe.d` | Keeps max77759_charger, gpio_max77759, nvmem_max77759 and tcpci_maxim from autoloading: binding the PMIC creates the charger cell, which programs charge limits from the battery node. Blacklist only, so a runbook can still load them by hand. The fuel gauge and the MFD core are not blacklisted | | `vendor/systemd/system/plymouth-quit.service.d/10-omamobile-retain-splash.conf` | `/usr/lib/systemd/system/plymouth-quit.service.d` | Makes `plymouth quit` keep its last frame on screen (`--retain-splash`) until Hyprland draws, instead of a black screen. The same three unit lines as cheetah and raven (the comments differ): gs-decon has no fbdev console to hand back, and on raven a plain quit hung for 27 s and held the session back. It only does anything with `splash` on the kernel command line, which husky's descriptor gets from #278; without it Plymouth never runs and the drop-in is inert. Booted from the package on a Pixel 8 Pro (2026-11-02, kernel 7.3.0-rc6-omamobile-push1-g5899fec2bd28): Plymouth started at 3.30 s and its quit, with --retain-splash, finished at 4.94 s without a hang | | `bin/omamobile-bt-addr` + `vendor/systemd/system/omamobile-bt-addr@.service` + `vendor/udev/rules.d/80-omamobile-bt-addr.rules` | `/usr/bin`, `/usr/lib/systemd/system`, `/usr/lib/udev/rules.d` | Gives the BCM4398 its real Bluetooth address from the bootloader's device tree (`/chosen/config/bt_addr`) when the controller appears, powering it on once first, as on the Pixel 9a. The 9a's script, rule and unit with the chip named; the test checks. Without it the controller keeps its placeholder (43:98:D0:..), the same on every 8 Pro. The address is read at run time and never stored in the package. Bluetooth then comes up powered at every boot | +| `charge-limit.conf` + `vendor/udev/rules.d/75-omamobile-charge-limit.rules` + `vendor/systemd/system/omamobile-charge-limit-apply.service` | `/usr/share/omamobile/charge-limit.conf`, `/usr/lib/udev/rules.d`, `/usr/lib/systemd/system` (not enabled) | Declares that this phone has a charge limit on `max77759-charger` (`supply=max77759-charger`). The Settings app's Battery page and its root helper (omamobile-settings) need this file and do nothing without it, so no other phone gets the page whatever its kernel. When the charger appears, udev pulls in the apply unit, which runs the helper without an argument to put the saved `/etc/omamobile/charge-limit` back on the charger; no saved file, nothing is touched. Since 0.1.16. Inert while `omamobile-max77759.conf` blacklists `max77759_charger`: there is no charger power supply, and the Battery row stays hidden | | `etc/omamobile-mirrorlist` | `/etc/pacman.d/omamobile-mirrorlist` | Where pacman finds the `[omamobile]` repository. Same file as the Pixel 7 Pro package | | `omamobile-device-husky.install` | pacman hook | Adds `[omamobile]` to `pacman.conf` before `[omarchy]`, once. Same script as the Pixel 7 Pro package | | `src/reboot-to.c` | `/usr/bin/reboot-to` | Reboot with a reason the bootloader understands. Same bytes as the Pixel 7 Pro package; the test checks | diff --git a/packages/omamobile-device-husky/charge-limit.conf b/packages/omamobile-device-husky/charge-limit.conf new file mode 100644 index 000000000..12b68bfc5 --- /dev/null +++ b/packages/omamobile-device-husky/charge-limit.conf @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: MIT +# Declares that this phone has a charge limit, and on which power supply. The Settings app's Battery page +# (omamobile-settings: backends/charge.sh and the root helper charge-limit) does nothing on a phone without this +# file. Husky's max77759-charger has charge_control_end_threshold and charge_control_start_threshold on a kernel +# with derekross/linux-omarchy-mobile#266, once this package stops blacklisting max77759_charger. +supply=max77759-charger diff --git a/packages/omamobile-device-husky/install b/packages/omamobile-device-husky/install index a551f6982..790758918 100755 --- a/packages/omamobile-device-husky/install +++ b/packages/omamobile-device-husky/install @@ -55,6 +55,12 @@ ln -sfn ../omamobile-husky-aoc.service \ "$destination/usr/lib/systemd/system/multi-user.target.wants/omamobile-husky-aoc.service" install -Dm644 vendor/modprobe.d/omamobile-husky-aoc.conf \ "$destination/usr/lib/modprobe.d/omamobile-husky-aoc.conf" +install -Dm644 charge-limit.conf \ + "$destination/usr/share/omamobile/charge-limit.conf" +install -Dm644 vendor/udev/rules.d/75-omamobile-charge-limit.rules \ + "$destination/usr/lib/udev/rules.d/75-omamobile-charge-limit.rules" +install -Dm644 vendor/systemd/system/omamobile-charge-limit-apply.service \ + "$destination/usr/lib/systemd/system/omamobile-charge-limit-apply.service" install -Dm644 device.conf \ "$destination/usr/share/omamobile/device.conf" install -Dm644 etc/omamobile-mirrorlist \ diff --git a/packages/omamobile-device-husky/test/all b/packages/omamobile-device-husky/test/all index c7b3d10b6..1760d8b79 100755 --- a/packages/omamobile-device-husky/test/all +++ b/packages/omamobile-device-husky/test/all @@ -86,6 +86,9 @@ for f in usr/bin/reboot-to \ usr/bin/omamobile-bt-addr usr/lib/systemd/system/omamobile-bt-addr@.service \ usr/lib/udev/rules.d/80-omamobile-bt-addr.rules \ usr/share/omamobile/device.conf \ + usr/share/omamobile/charge-limit.conf \ + usr/lib/udev/rules.d/75-omamobile-charge-limit.rules \ + usr/lib/systemd/system/omamobile-charge-limit-apply.service \ usr/share/omamobile/device-husky/version \ usr/share/licenses/omamobile-device-husky/LICENSE \ etc/pacman.d/omamobile-mirrorlist; do @@ -173,6 +176,11 @@ pass "install stages the udev rule, the USB link, the mirrorlist and the display [[ ! -e $tmp/root/usr/lib/systemd/logind.conf.d/10-omamobile-lid.conf ]] \ || fail "husky has no lid: no lid drop-in" pass "no lid drop-in" +grep -qx 'supply=max77759-charger' "$tmp/root/usr/share/omamobile/charge-limit.conf" \ + || fail "husky declares its charge limit on max77759-charger" +[[ ! -e $tmp/root/usr/lib/systemd/system/multi-user.target.wants/omamobile-charge-limit-apply.service ]] \ + || fail "the apply unit is pulled in by udev, not enabled" +pass "the charge limit declaration, its udev rule and the apply unit are staged; the unit is not enabled" # --- the AoC start: hang marker, nothing without the node ------------------ diff --git a/packages/omamobile-device-husky/vendor/systemd/system/omamobile-charge-limit-apply.service b/packages/omamobile-device-husky/vendor/systemd/system/omamobile-charge-limit-apply.service new file mode 100644 index 000000000..9b68fd111 --- /dev/null +++ b/packages/omamobile-device-husky/vendor/systemd/system/omamobile-charge-limit-apply.service @@ -0,0 +1,15 @@ +# SPDX-License-Identifier: MIT +# Applies the saved charge limit (/etc/omamobile/charge-limit) to the charger this package declares +# (/usr/share/omamobile/charge-limit.conf). Pulled in by udev when the charger appears +# (75-omamobile-charge-limit.rules); without a saved file the helper does nothing. The helper is omamobile-settings'. +# Not enabled. +[Unit] +Description=Apply the saved charge limit +StartLimitIntervalSec=0 +After=local-fs.target +ConditionFileIsExecutable=/usr/lib/omamobile-settings/charge-limit + +[Service] +Type=oneshot +ExecStart=/usr/lib/omamobile-settings/charge-limit +TimeoutStartSec=15 diff --git a/packages/omamobile-device-husky/vendor/udev/rules.d/75-omamobile-charge-limit.rules b/packages/omamobile-device-husky/vendor/udev/rules.d/75-omamobile-charge-limit.rules new file mode 100644 index 000000000..2555686e8 --- /dev/null +++ b/packages/omamobile-device-husky/vendor/udev/rules.d/75-omamobile-charge-limit.rules @@ -0,0 +1,7 @@ +# SPDX-License-Identifier: MIT +# Apply the saved charge limit (/etc/omamobile/charge-limit) when the MAX77759 charger appears, at every boot, +# through omamobile-charge-limit-apply.service rather than inside the udev worker. The helper acts only on the +# supply this package declares in /usr/share/omamobile/charge-limit.conf; no saved file: nothing is touched. +# No ACTION filter: with ACTION=="add" the device loses its systemd tag on the charger's next change event, and +# the unit did not start at boot on husky (2026-11-16); without it, it does. +SUBSYSTEM=="power_supply", KERNEL=="max77759-charger", TAG+="systemd", ENV{SYSTEMD_WANTS}+="omamobile-charge-limit-apply.service" diff --git a/packages/omamobile-device-husky/version b/packages/omamobile-device-husky/version index c34958a47..e8e277f2f 100644 --- a/packages/omamobile-device-husky/version +++ b/packages/omamobile-device-husky/version @@ -1 +1 @@ -0.1.15 +0.1.16 diff --git a/packages/omamobile-settings/PKGBUILD b/packages/omamobile-settings/PKGBUILD index 28ab9ed7f..cb9f9b41c 100644 --- a/packages/omamobile-settings/PKGBUILD +++ b/packages/omamobile-settings/PKGBUILD @@ -6,7 +6,7 @@ pkgdesc="Native mobile Settings framework and device information" arch=(aarch64 x86_64) url="https://github.com/derekross/omarchy-mobile" license=(MIT) -depends=(omarchy bash brightnessctl python qt6-base qt6-declarative qt6-svg) +depends=(omarchy bash coreutils systemd brightnessctl polkit python qt6-base qt6-declarative qt6-svg) makedepends=(gcc make) checkdepends=(dbus python) diff --git a/packages/omamobile-settings/README.md b/packages/omamobile-settings/README.md index 4b4b88960..3cdd006cd 100644 --- a/packages/omamobile-settings/README.md +++ b/packages/omamobile-settings/README.md @@ -12,6 +12,30 @@ refresh rate changes the running session only: turning the phone makes auto-rotate apply the monitor's preferred mode again, and a session restart or a reload of Hyprland's config does too). The page runs `backends/display.sh` through a fixed-argument runner, with no shell string. +A second page, Battery, sets the charge limit (80, 85, 90, 95 or 100 %), only +on a phone whose device package declares one in +`/usr/share/omamobile/charge-limit.conf` (a line `supply=NAME`; +omamobile-device-husky ships it for `max77759-charger`) and only while that +supply has `charge_control_end_threshold`. Everywhere else the Battery row is +hidden (the catalog row's `"requires": "charge"`: Settings runs +`backends/charge.sh get` at start and shows the row only on +`{"available":true}`), and the backend and the helper write nothing. The row +appears only if the charger was there when Settings started: a charger that +shows up later needs Settings to be started again. The page +runs `backends/charge.sh`, which starts `omamobile-charge-limit@N.service`; the +polkit rule `50-omamobile-charge-limit.rules` lets the active local user start +those five instances only. The service runs `libexec/charge-limit` as root, +which checks the declaration, writes the charger (start threshold 0, then the +end threshold) and only then saves the value to `/etc/omamobile/charge-limit`. +The device package that declares the limit also ships the udev rule and +`omamobile-charge-limit-apply.service`, which run the same helper without an +argument when the charger appears, to apply the saved value at boot. The +Pixel 9a's limit (`omamobile-device-tegu`, from `/etc/omamobile/charge-limit` +by its own script) is not declared, so Settings leaves it alone. On husky the +row stays hidden today: omamobile-device-husky blacklists `max77759_charger` +(`omamobile-max77759.conf`), so there is no `max77759-charger` power supply +until that blacklist is lifted on a kernel with +derekross/linux-omarchy-mobile#266. Omarchy's existing tools continue to provide the other settings changes. Network, wallpaper, app management and recording controls are separate future contributions with their own runtime contracts and tests; this catalog does not offer those routes. diff --git a/packages/omamobile-settings/app/BatteryPage.qml b/packages/omamobile-settings/app/BatteryPage.qml new file mode 100644 index 000000000..db3a93a6e --- /dev/null +++ b/packages/omamobile-settings/app/BatteryPage.qml @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: MIT +import QtQuick +import QtQuick.Layouts +import "." +ColumnLayout { + id: page + spacing: 14 + property int limit: -1 + property bool available: false + property string status: qsTr("Reading the charger...") + property bool busy: true + property int reqId: -1 + readonly property var levels: [80, 85, 90, 95, 100] + function apply(r) { + busy = false + if (r.error) { status = r.error; return } + available = r.available === true + if (!available) { status = qsTr("Settings cannot set a charge limit on this phone."); return } + if (r.limit === undefined) { status = qsTr("The charger backend gave no value."); return } + limit = r.limit + status = qsTr("Stops charging at this level; it resumes 5 % lower. 100 % charges fully.") + } + Connections { target: actions; function onFinished(tool, r, id) { if (tool === "charge" && id === page.reqId) page.apply(r) } } + Component.onCompleted: reqId = actions.run("charge", ["get"]) + SettingsLabel { + visible: page.available + text: page.limit > 0 ? qsTr("Charge limit: %1 %").arg(page.limit) : qsTr("Charge limit") + font.weight: Font.DemiBold; color: backend.foreground; Layout.fillWidth: true + } + Flow { + visible: page.available + Layout.fillWidth: true; spacing: 8 + Repeater { + model: page.levels + delegate: SettingsButton { + required property int modelData + text: modelData + "%"; highlighted: page.limit === modelData; enabled: !page.busy + onClicked: { page.busy = true; page.reqId = actions.run("charge", ["set", "limit", String(modelData)]) } + } + } + } + SettingsLabel { text: page.status; color: backend.muted; font.pixelSize: SettingsStyle.fontPx(13); wrapMode: Text.Wrap; Layout.fillWidth: true } + Item { Layout.fillHeight: true } +} diff --git a/packages/omamobile-settings/app/Main.qml b/packages/omamobile-settings/app/Main.qml index 25a26417f..3d4ccd66b 100644 --- a/packages/omamobile-settings/app/Main.qml +++ b/packages/omamobile-settings/app/Main.qml @@ -24,7 +24,7 @@ ApplicationWindow { if (backend.searching) search.clear() backend.back() } - readonly property var customPages: ({ display: "DisplayPage.qml" }) + readonly property var customPages: ({ display: "DisplayPage.qml", battery: "BatteryPage.qml" }) readonly property string customSource: backend.searching ? "" : (customPages[backend.page] || "") readonly property bool showingAbout: backend.page === "about" && !backend.searching Connections { diff --git a/packages/omamobile-settings/app/actionrunner.cpp b/packages/omamobile-settings/app/actionrunner.cpp index a118095f2..83822926e 100644 --- a/packages/omamobile-settings/app/actionrunner.cpp +++ b/packages/omamobile-settings/app/actionrunner.cpp @@ -16,12 +16,11 @@ void ActionRunner::post(const QString &tool, const QVariantMap &result, int id) int ActionRunner::run(const QString &tool, const QStringList &args) { static const QRegularExpression word("\\A[a-z0-9]{1,16}\\z"); - static const QStringList tools = {"display"}; + static const QStringList tools = {"display", "charge"}; const int id = ++m_next; auto fail = [this, &tool, id](const QString &why) { post(tool, {{"error", why}}, id); return id; }; if (!tools.contains(tool) || args.size() > 3) return fail("unknown action"); for (const auto &a : args) if (!word.match(a).hasMatch()) return fail("bad argument"); - const QString dir = "/usr/lib/omamobile-settings/backends"; auto *p = new QProcess(this); // bash would run a startup file named by these auto env = QProcessEnvironment::systemEnvironment(); @@ -29,7 +28,7 @@ int ActionRunner::run(const QString &tool, const QStringList &args) { env.remove("ENV"); p->setProcessEnvironment(env); p->setProgram("/usr/bin/bash"); - p->setArguments(QStringList{dir + "/" + tool + ".sh"} + args); + p->setArguments(QStringList{m_dir + "/" + tool + ".sh"} + args); connect(p, &QProcess::finished, this, [this, p, tool, id](int code, QProcess::ExitStatus st) { const auto raw = p->readAllStandardOutput().left(4096); p->readAllStandardError(); diff --git a/packages/omamobile-settings/app/actionrunner.h b/packages/omamobile-settings/app/actionrunner.h index e56a52234..3ebe43868 100644 --- a/packages/omamobile-settings/app/actionrunner.h +++ b/packages/omamobile-settings/app/actionrunner.h @@ -4,7 +4,7 @@ #include #include -// Runs the source-owned backend scripts (display) with a fixed argument list and no shell. Pages call run(); +// Runs the source-owned backend scripts (display, charge) with a fixed argument list and no shell. Pages call run(); // the JSON the script prints comes back through finished(), always from the event loop and never before run() // has returned its id. class ActionRunner : public QObject { @@ -12,9 +12,12 @@ class ActionRunner : public QObject { public: explicit ActionRunner(QObject *parent = nullptr) : QObject(parent) {} Q_INVOKABLE int run(const QString &tool, const QStringList &args); + // Where the backend scripts live; tests point it at stubs so they don't depend on the host's install. + void setBackendDir(const QString &dir) { m_dir = dir; } signals: void finished(const QString &tool, const QVariantMap &result, int id); private: void post(const QString &tool, const QVariantMap &result, int id); int m_next = 0; + QString m_dir = QStringLiteral("/usr/lib/omamobile-settings/backends"); }; diff --git a/packages/omamobile-settings/app/backend.cpp b/packages/omamobile-settings/app/backend.cpp index 8722e2ea9..3a617d9c5 100644 --- a/packages/omamobile-settings/app/backend.cpp +++ b/packages/omamobile-settings/app/backend.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -30,8 +31,40 @@ Backend::Backend(QObject *parent) : QObject(parent) { }); reloadTheme(); rebuild(); + QTimer::singleShot(0, this, [this] { probe("charge"); }); +} +void Backend::probe(const QString &tool) { + // The same fixed-argument call the page makes (actionrunner.cpp): bash, no startup file, one `get`. + auto *p = &m_chargeProbe; + connect(p, &QProcess::finished, this, [this, p, tool](int code, QProcess::ExitStatus status) { + const auto raw = p->readAllStandardOutput().left(4096); + p->readAllStandardError(); + const auto obj = QJsonDocument::fromJson(raw).object(); + setToolAvailable(tool, code == 0 && status == QProcess::NormalExit && obj["available"].toBool()); + }); + auto env = QProcessEnvironment::systemEnvironment(); + env.remove("BASH_ENV"); + env.remove("ENV"); + p->setProcessEnvironment(env); + p->start("/usr/bin/bash", {m_backendDir + "/" + tool + ".sh", "get"}); + p->closeWriteChannel(); + QTimer::singleShot(5000, p, [p] { if (p->state() != QProcess::NotRunning) p->kill(); }); +} +void Backend::setToolAvailable(const QString &tool, bool available) { + if (available == m_availableTools.contains(tool)) return; + if (available) m_availableTools.insert(tool); else m_availableTools.remove(tool); + rebuild(); +} +bool Backend::shown(const QJsonObject &row) const { + const auto tool = row["requires"].toString(); + return tool.isEmpty() || m_availableTools.contains(tool); } Backend::~Backend() { + m_chargeProbe.disconnect(this); + if (m_chargeProbe.state() != QProcess::NotRunning) { + m_chargeProbe.kill(); + m_chargeProbe.waitForFinished(1000); + } m_roundingQuery.disconnect(this); if (m_roundingQuery.state() != QProcess::NotRunning) { m_roundingQuery.kill(); @@ -80,12 +113,12 @@ void Backend::rebuild() { m_rows.clear(); if (!searching()) { for (const auto &row : m_catalog[m_page].toObject()["rows"].toArray()) - m_rows.append(row.toObject().toVariantMap()); + if (shown(row.toObject())) m_rows.append(row.toObject().toVariantMap()); } else { for (const auto &page : m_catalog) { for (const auto &value : page.toObject()["rows"].toArray()) { const auto row = value.toObject(); - if ((row["title"].toString() + " " + row["detail"].toString()).contains(m_query, Qt::CaseInsensitive)) + if (shown(row) && (row["title"].toString() + " " + row["detail"].toString()).contains(m_query, Qt::CaseInsensitive)) m_rows.append(row.toVariantMap()); } } diff --git a/packages/omamobile-settings/app/backend.h b/packages/omamobile-settings/app/backend.h index c05da8f61..45b45e576 100644 --- a/packages/omamobile-settings/app/backend.h +++ b/packages/omamobile-settings/app/backend.h @@ -7,6 +7,7 @@ #include #include #include +#include #include "themetokens.h" class Backend : public QObject { @@ -46,6 +47,12 @@ class Backend : public QObject { void setHomeCommand(const QStringList &command) { m_homeCommand = command; } Q_INVOKABLE QString icon(const QString &name) const; Q_INVOKABLE QStringList catalogProblems() const; + // A row with "requires": TOOL is shown only once that backend's `get` has said {"available":true} (the Battery + // row: only on a phone whose device package declares a charge limit). The probe calls this; so do the tests. + void setToolAvailable(const QString &tool, bool available); + // Where the probe finds the backend scripts; tests point it at stubs so they don't depend on the host's + // install. The probe starts from the event loop, so set this right after construction. + void setBackendDir(const QString &dir) { m_backendDir = dir; } Q_SCRIPTABLE bool Open(const QString &page); signals: void rowsChanged(); @@ -55,6 +62,8 @@ class Backend : public QObject { void raiseRequested(); private: void rebuild(); + bool shown(const QJsonObject &row) const; + void probe(const QString &tool); void reloadTheme(); QJsonObject m_catalog; QVariantList m_rows; @@ -63,6 +72,9 @@ class Backend : public QObject { QStringList m_homeCommand{"omamobile-home"}; QFileSystemWatcher m_watcher; QProcess m_roundingQuery; + QProcess m_chargeProbe; + QSet m_availableTools; + QString m_backendDir = QStringLiteral("/usr/lib/omamobile-settings/backends"); quint64 m_roundingGeneration = 0; int m_radius = 0; QVariantMap m_themeTokens = SettingsTheme::tokens({}, {}); diff --git a/packages/omamobile-settings/app/catalog.json b/packages/omamobile-settings/app/catalog.json index 23efec22b..35b266a24 100644 --- a/packages/omamobile-settings/app/catalog.json +++ b/packages/omamobile-settings/app/catalog.json @@ -9,6 +9,14 @@ "icon": "display", "page": "display" }, + { + "id": "battery", + "title": "Battery", + "detail": "Charge limit", + "icon": "battery", + "page": "battery", + "requires": "charge" + }, { "id": "about", "title": "About this device", @@ -25,5 +33,9 @@ "display": { "title": "Display", "rows": [] + }, + "battery": { + "title": "Battery", + "rows": [] } } diff --git a/packages/omamobile-settings/app/icons/battery.svg b/packages/omamobile-settings/app/icons/battery.svg new file mode 100644 index 000000000..b300a8215 --- /dev/null +++ b/packages/omamobile-settings/app/icons/battery.svg @@ -0,0 +1 @@ + diff --git a/packages/omamobile-settings/app/main.cpp b/packages/omamobile-settings/app/main.cpp index ab0826a12..68f0f1edb 100644 --- a/packages/omamobile-settings/app/main.cpp +++ b/packages/omamobile-settings/app/main.cpp @@ -39,7 +39,11 @@ int main(int argc, char **argv) { QQuickStyle::setStyle("Basic"); Backend backend; #ifdef OMAMOBILE_SETTINGS_TEST_DEVICE_INFO - // Only the separately built test entrypoint fixes read-only model data. + // Only the separately built test entrypoint fixes read-only model data, + // and it never runs the backends installed on the machine + const QString testBackends = qEnvironmentVariable("OMAMOBILE_SETTINGS_TEST_BACKENDS", + QStringLiteral("/nonexistent/omamobile-settings-test")); + backend.setBackendDir(testBackends); DeviceInfoController deviceInfo(true); #else DeviceInfoController deviceInfo(preview); @@ -64,6 +68,9 @@ int main(int argc, char **argv) { | QDBusConnection::ExportScriptableInvokables)) return 1; } ActionRunner actions; +#ifdef OMAMOBILE_SETTINGS_TEST_DEVICE_INFO + actions.setBackendDir(testBackends); +#endif QQmlApplicationEngine engine; engine.rootContext()->setContextProperty("backend", &backend); engine.rootContext()->setContextProperty("deviceInfo", &deviceInfo); diff --git a/packages/omamobile-settings/app/resources.qrc b/packages/omamobile-settings/app/resources.qrc index 0f18d70dc..27e14cec7 100644 --- a/packages/omamobile-settings/app/resources.qrc +++ b/packages/omamobile-settings/app/resources.qrc @@ -4,6 +4,8 @@ Main.qml AboutPage.qml DisplayPage.qml + BatteryPage.qml + icons/battery.svg icons/display.svg catalog.json icons/system.svg diff --git a/packages/omamobile-settings/backends/charge.sh b/packages/omamobile-settings/backends/charge.sh new file mode 100755 index 000000000..739dc1d12 --- /dev/null +++ b/packages/omamobile-settings/backends/charge.sh @@ -0,0 +1,51 @@ +#!/bin/bash +# SPDX-License-Identifier: MIT +# Charge limit for the Settings app's Battery page. Prints one line of JSON. +# charge.sh get {"available":true,"limit":80} (read from the charger), or {"available":false} +# charge.sh set limit N N is 80, 85, 90, 95 or 100; saved and applied as root by +# omamobile-charge-limit@N.service, which the polkit rule lets the active local user start +# Available only on a phone whose device package declares its charge limit in /usr/share/omamobile/charge-limit.conf +# (a line supply=NAME; omamobile-device-husky ships it for max77759-charger), and only while that supply has +# charge_control_end_threshold. Anywhere else: {"available":false}, and set changes nothing. +# The test runs a copy of this script with `fake` set to a scratch root: the threshold is read there and +# `set` writes it directly, no service is started. Nothing in the environment sets it. +set -euo pipefail +set +x +verb=${1:-}; key=${2:-}; value=${3:-} +fail() { printf '{"error":"%s"}\n' "$1"; exit 2; } +fake='' +[[ -z $fake || -d $fake ]] || fail "cannot read the test root" +decl=$fake/usr/share/omamobile/charge-limit.conf +supply=$(sed -n 's/^supply=//p' "$decl" 2>/dev/null | head -n 1) || supply='' +[[ $supply =~ ^[a-z0-9][a-z0-9-]{0,31}$ ]] || supply='' +end=$fake/sys/class/power_supply/$supply/charge_control_end_threshold + +read_limit() { + [[ -n $supply && -r $end ]] || return 1 + local v; v=$(tr -d ' \t\n' < "$end") + [[ $v =~ ^[0-9]{1,3}$ ]] || return 2 + echo $((10#$v)) +} +get() { + local v rc=0 + v=$(read_limit) || rc=$? + case $rc in + 0) printf '{"available":true,"limit":%s}\n' "$v" ;; + 1) printf '{"available":false}\n' ;; + *) fail "cannot read the charger" ;; + esac +} + +case $verb in +get) get ;; +set) + [[ $key == limit ]] || fail "unknown key" + case $value in 80|85|90|95|100) ;; *) fail "bad value" ;; esac + [[ -n $supply && -e $end ]] || fail "this phone has no charge limit" + if [[ -n $fake ]]; then echo "$value" > "$end" + else + systemctl start --no-ask-password "omamobile-charge-limit@$value.service" >/dev/null 2>&1 || fail "could not set the charge limit" + fi + get ;; +*) fail "usage: get|set" ;; +esac diff --git a/packages/omamobile-settings/install b/packages/omamobile-settings/install index 2963f0a8b..6847708ce 100755 --- a/packages/omamobile-settings/install +++ b/packages/omamobile-settings/install @@ -11,3 +11,7 @@ install -Dm644 "$source_dir/org.omamobile.Settings.desktop" "$dest/usr/share/app install -Dm644 "$source_dir/org.omamobile.Settings.svg" "$dest/usr/share/icons/hicolor/scalable/apps/org.omamobile.Settings.svg" install -Dm644 "$source_dir/LICENSE" "$dest/usr/share/licenses/omamobile-settings/LICENSE" install -Dm755 "$source_dir/backends/display.sh" "$dest/usr/lib/omamobile-settings/backends/display.sh" +install -Dm755 "$source_dir/backends/charge.sh" "$dest/usr/lib/omamobile-settings/backends/charge.sh" +install -Dm755 "$source_dir/libexec/charge-limit" "$dest/usr/lib/omamobile-settings/charge-limit" +install -Dm644 "$source_dir/vendor/systemd/system/omamobile-charge-limit@.service" "$dest/usr/lib/systemd/system/omamobile-charge-limit@.service" +install -Dm644 "$source_dir/vendor/polkit/50-omamobile-charge-limit.rules" "$dest/usr/share/polkit-1/rules.d/50-omamobile-charge-limit.rules" diff --git a/packages/omamobile-settings/libexec/charge-limit b/packages/omamobile-settings/libexec/charge-limit new file mode 100755 index 000000000..f93ba421c --- /dev/null +++ b/packages/omamobile-settings/libexec/charge-limit @@ -0,0 +1,52 @@ +#!/bin/bash +# SPDX-License-Identifier: MIT +# Charge limit on the power supply the device package declares in /usr/share/omamobile/charge-limit.conf (a line +# supply=NAME; omamobile-device-husky ships it for max77759-charger). Without that file, or with a bad name, both +# paths below do nothing and write nothing. Runs as root, from omamobile-charge-limit@N.service (the Settings app's Battery +# page, through the polkit rule 50-omamobile-charge-limit.rules) and, with no argument, from +# omamobile-charge-limit-apply.service, which udev pulls in when the charger appears (the device package's +# 75-omamobile-charge-limit.rules). +# charge-limit N N is 80, 85, 90, 95 or 100: written to the charger, then saved to /etc/omamobile/charge-limit; +# no declaration: exit 1; no writable end threshold, or a write that fails: exit 1, nothing saved +# charge-limit the boot path: applies the saved value; no declaration or no saved file: nothing is touched +# The start threshold is written first, as 0, which the driver takes as "resume 5 below the end". +# /etc/omamobile/charge-limit is the same file omamobile-device-tegu's charge limit reads. +set -uo pipefail +root=${OMAMOBILE_CHARGE_ROOT:-} +if [[ -n $root ]]; then [[ $root == /* && $root != / && -d $root ]] || exit 2; fi +file=$root/etc/omamobile/charge-limit +decl=$root/usr/share/omamobile/charge-limit.conf +supply=$(sed -n 's/^supply=//p' "$decl" 2>/dev/null | head -n 1) || supply='' +dir=$root/sys/class/power_supply/$supply +end=$dir/charge_control_end_threshold start=$dir/charge_control_start_threshold +log() { echo "omamobile-charge-limit: $*"; } +valid() { case $1 in 80|85|90|95|100) return 0 ;; *) return 1 ;; esac; } +save() { + mkdir -p "${file%/*}" && printf '%s\n' "$1" > "$file.new" && mv -f "$file.new" "$file" || { log "cannot save $file"; exit 1; } +} + +if [[ ! $supply =~ ^[a-z0-9][a-z0-9-]{0,31}$ ]]; then + log "this phone declares no charge limit ($decl)" + (( $# > 0 )) && exit 1 + exit 0 +fi +if (( $# > 0 )); then + valid "$1" || { log "refusing $1: only 80, 85, 90, 95 or 100"; exit 2; } + limit=$1 +else + [[ -s $file ]] || exit 0 + limit=$(tr -d ' \t\n' < "$file") + valid "$limit" || { log "$file holds '$limit', not 80, 85, 90, 95 or 100; charger left alone"; exit 1; } +fi +if [[ ! -f $end || ! -w $end ]]; then + log "no charger with a writable end threshold" + (( $# > 0 )) && exit 1 + exit 0 +fi +if [[ -e $start ]]; then + echo 0 > "$start" || { log "the charger refused start threshold 0"; exit 1; } +fi +echo "$limit" > "$end" || { log "the charger refused $limit"; exit 1; } +(( $# > 0 )) && save "$limit" +log "charge limit $(tr -d ' \t\n' < "$end") %" +exit 0 diff --git a/packages/omamobile-settings/test/all b/packages/omamobile-settings/test/all index d0c6618eb..a8623d42a 100755 --- a/packages/omamobile-settings/test/all +++ b/packages/omamobile-settings/test/all @@ -5,6 +5,7 @@ source_dir=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd) build_dir=${OMAMOBILE_SETTINGS_TEST_BUILD_DIR:-"$source_dir/build/tests"} "$source_dir/bin/build" "$source_dir/test/display-backend.sh" +"$source_dir/test/charge-backend.sh" mkdir -p "$build_dir" fixture=$(mktemp -d) trap 'rm -rf -- "$fixture"' EXIT HUP INT TERM diff --git a/packages/omamobile-settings/test/charge-backend.sh b/packages/omamobile-settings/test/charge-backend.sh new file mode 100755 index 000000000..4ba8f4e93 --- /dev/null +++ b/packages/omamobile-settings/test/charge-backend.sh @@ -0,0 +1,148 @@ +#!/bin/bash +# SPDX-License-Identifier: MIT +# backends/charge.sh against a scratch root (a copy of the script with `fake` set), and libexec/charge-limit, +# the root helper, in the same scratch root: no service is started and nothing real is written. +set -euo pipefail +src=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd) +tmp=$(mktemp -d); trap 'rm -rf -- "$tmp"' EXIT +root=$tmp/root +dir=$root/sys/class/power_supply/max77759-charger +end=$dir/charge_control_end_threshold start=$dir/charge_control_start_threshold +sed "s|^fake=.*|fake=$root|" "$src/backends/charge.sh" > "$tmp/charge.sh" +grep -q "^fake=$root" "$tmp/charge.sh" +run() { bash "$tmp/charge.sh" "$@" || true; } +eq() { [[ $1 == "$2" ]] || { echo "FAIL: got $1, want $2" >&2; exit 1; }; } + +decl=$root/usr/share/omamobile/charge-limit.conf +declare_husky() { mkdir -p "${decl%/*}"; printf '# test\nsupply=max77759-charger\n' > "$decl"; } +# no charger: not available, and set refuses +mkdir -p "$root" +eq "$(run get)" '{"available":false}' +eq "$(run set limit 80)" '{"error":"this phone has no charge limit"}' + +# a charger with the threshold but no declaration (the Pixel 7 Pro on a kernel with #266): not available, no write +mkdir -p "$dir"; echo 100 > "$end"; echo 7 > "$start" +eq "$(run get)" '{"available":false}' +eq "$(run set limit 80)" '{"error":"this phone has no charge limit"}' +eq "$(cat "$end")" 100 +for bad in 'supply=../../x' 'supply=' 'supply=Max' 'supply=../power_supply/max77759-charger'; do + mkdir -p "${decl%/*}"; echo "$bad" > "$decl"; eq "$(run get)" '{"available":false}' +done +declare_husky +eq "$(run get)" '{"available":true,"limit":100}' +eq "$(run set limit 80)" '{"available":true,"limit":80}' +eq "$(run set limit 75)" '{"error":"bad value"}' +eq "$(run set limit 101)" '{"error":"bad value"}' +eq "$(run set limit abc)" '{"error":"bad value"}' +eq "$(run set level 80)" '{"error":"unknown key"}' +eq "$(run nonsense)" '{"error":"usage: get|set"}' +echo junk > "$end" +eq "$(run get)" '{"error":"cannot read the charger"}' + +# the root helper +helper=$src/libexec/charge-limit +export OMAMOBILE_CHARGE_ROOT=$root +file=$root/etc/omamobile/charge-limit +no() { if bash "$helper" "$@" >/dev/null 2>&1; then echo "FAIL: accepted: $*" >&2; exit 1; fi; } +echo 100 > "$end" +bash "$helper" >/dev/null || { echo "FAIL: no saved file is an error" >&2; exit 1; } +eq "$(cat "$end")" 100 +[[ ! -e $file ]] || { echo "FAIL: a file appeared" >&2; exit 1; } +echo 7 > "$start" +bash "$helper" 85 >/dev/null +eq "$(cat "$file")" 85; eq "$(cat "$end")" 85; eq "$(cat "$start")" 0 +for bad in 0 79 101 abc 8.5 '80;x'; do no "$bad"; done +eq "$(cat "$file")" 85; eq "$(cat "$end")" 85 +# boot: udev runs it with no argument, the saved value goes to the charger +echo 100 > "$end"; echo 90 > "$file" +bash "$helper" >/dev/null; eq "$(cat "$end")" 90 +echo 42 > "$file"; no; eq "$(cat "$end")" 90 +# no declaration (another phone): both paths touch nothing, and N is refused without saving +echo 80 > "$file"; rm "$decl" +bash "$helper" >/dev/null || { echo "FAIL: boot without a declaration is an error" >&2; exit 1; } +eq "$(cat "$end")" 90 +no 95; eq "$(cat "$end")" 90; eq "$(cat "$file")" 80 +echo 'supply=../max77759-charger' > "$decl"; no 95; bash "$helper" >/dev/null; eq "$(cat "$end")" 90 +# a name with a path in it that still reaches the real charger: only the name check refuses it +echo 'supply=../power_supply/max77759-charger' > "$decl" +no 95; eq "$(cat "$end")" 90; eq "$(cat "$file")" 80 +bash "$helper" >/dev/null; eq "$(cat "$end")" 90 +declare_husky +# a failed write saves nothing: a directory in place of the end file cannot be written +echo 85 > "$file" +mv "$end" "$tmp/end.bak"; mkdir "$end"; chmod 755 "$end" +no 95; eq "$(cat "$file")" 85 +rmdir "$end"; mv "$tmp/end.bak" "$end" +# a start threshold that cannot be written stops before the end is touched +mv "$start" "$tmp/start.bak"; mkdir "$start" +echo 90 > "$end"; no 80; eq "$(cat "$end")" 90; eq "$(cat "$file")" 85 +rmdir "$start"; mv "$tmp/start.bak" "$start" +# an end threshold that is not writable (read-only file; skipped as root, which can write anyway): refused, nothing saved +if (( EUID != 0 )); then + chmod 444 "$end"; no 95; eq "$(cat "$file")" 85; chmod 644 "$end" +fi +# no charger: refused and nothing saved; the boot path just does nothing +rm -rf "$dir" +no 95; eq "$(cat "$file")" 85 +bash "$helper" >/dev/null || { echo "FAIL: boot with no charger is an error" >&2; exit 1; } +OMAMOBILE_CHARGE_ROOT=relative bash "$helper" 80 && { echo "FAIL: relative root accepted" >&2; exit 1; } || true + +# the polkit rule, the unit and the udev rule +v=$src/vendor +grep -qF '/^omamobile-charge-limit@(80|85|90|95|100)\.service$/' "$v/polkit/50-omamobile-charge-limit.rules" && + grep -qF 'action.lookup("verb") == "start"' "$v/polkit/50-omamobile-charge-limit.rules" && + grep -qF 'subject.local && subject.active' "$v/polkit/50-omamobile-charge-limit.rules" || + { echo "FAIL: polkit: the five charge-limit units, start only" >&2; exit 1; } +u=$v/systemd/system/omamobile-charge-limit@.service +grep -qx 'ExecStart=/usr/lib/omamobile-settings/charge-limit %i' "$u" && grep -qx 'Type=oneshot' "$u" && + grep -qx 'StartLimitIntervalSec=0' "$u" && ! grep -q '^\[Install\]' "$u" || + { echo "FAIL: unit: oneshot helper with the instance, no start limit, not enabled" >&2; exit 1; } +h=$src/../omamobile-device-husky +if [[ -d $h ]]; then + r=$h/vendor/udev/rules.d/75-omamobile-charge-limit.rules + [[ $(grep -v '^#' "$r" | grep -c .) == 1 ]] && + grep -qxF 'SUBSYSTEM=="power_supply", KERNEL=="max77759-charger", TAG+="systemd", ENV{SYSTEMD_WANTS}+="omamobile-charge-limit-apply.service"' "$r" && + ! grep -v '^#' "$r" | grep -c -e RUN -e ACTION >/dev/null || + { echo "FAIL: udev: one rule, no ACTION filter, pulls in the apply unit" >&2; exit 1; } + a=$h/vendor/systemd/system/omamobile-charge-limit-apply.service + grep -qx 'ExecStart=/usr/lib/omamobile-settings/charge-limit' "$a" && grep -qx 'Type=oneshot' "$a" && + grep -qx 'StartLimitIntervalSec=0' "$a" && ! grep -q '^\[Install\]' "$a" || + { echo "FAIL: apply unit: oneshot helper with no argument, not enabled" >&2; exit 1; } + grep -qx 'supply=max77759-charger' "$h/charge-limit.conf" || { echo "FAIL: husky declares max77759-charger" >&2; exit 1; } +fi +[[ ! -e $v/udev/rules.d/75-omamobile-charge-limit.rules && ! -e $v/systemd/system/omamobile-charge-limit-apply.service ]] || + { echo "FAIL: the udev rule and the apply unit belong to the device package" >&2; exit 1; } +grep -q 'systemctl start --no-ask-password "omamobile-charge-limit@$value.service"' "$src/backends/charge.sh" || + { echo "FAIL: charge.sh starts the unit without asking" >&2; exit 1; } + +# the real branch of charge.sh: a stand-in systemctl on PATH records its command line +mkdir -p "$dir" "$tmp/bin"; echo 100 > "$end" +cat > "$tmp/bin/systemctl" <> "$tmp/systemctl.log" +EOT +chmod +x "$tmp/bin/systemctl" +sed -e "s|^decl=.*|decl=$decl|" -e "s|^end=.*|end=$end|" "$src/backends/charge.sh" > "$tmp/charge-real.sh" +grep -q "^fake=''$" "$tmp/charge-real.sh" +eq "$(PATH=$tmp/bin:$PATH bash "$tmp/charge-real.sh" set limit 80)" '{"available":true,"limit":100}' +eq "$(cat "$tmp/systemctl.log")" 'systemctl start --no-ask-password omamobile-charge-limit@80.service' +printf '#!/bin/bash\nexit 1\n' > "$tmp/bin/systemctl" +eq "$(PATH=$tmp/bin:$PATH bash "$tmp/charge-real.sh" set limit 80 || true)" '{"error":"could not set the charge limit"}' + +# installed paths and modes (the binary is a stand-in: only the staging is checked) +stage=$tmp/stage; mkdir -p "$tmp/build"; : > "$tmp/build/omamobile-settings" +OMAMOBILE_SETTINGS_BUILD_DIR=$tmp/build "$src/install" "$stage" +while read -r mode f; do + [[ -f $stage/$f ]] || { echo "FAIL: install stages $f" >&2; exit 1; } + eq "$(stat -c %a "$stage/$f")" "$mode" +done <<'EOT' +755 usr/lib/omamobile-settings/backends/charge.sh +755 usr/lib/omamobile-settings/charge-limit +644 usr/lib/systemd/system/omamobile-charge-limit@.service +644 usr/share/polkit-1/rules.d/50-omamobile-charge-limit.rules +EOT +for f in usr/lib/systemd/system/omamobile-charge-limit-apply.service usr/lib/udev/rules.d/75-omamobile-charge-limit.rules \ + usr/share/omamobile/charge-limit.conf; do + [[ ! -e $stage/$f ]] || { echo "FAIL: Settings stages $f, which is the device package's" >&2; exit 1; } +done +echo "charge backend: ok" diff --git a/packages/omamobile-settings/tests/core_tests.cpp b/packages/omamobile-settings/tests/core_tests.cpp index e8e0a2e77..b87a721ce 100644 --- a/packages/omamobile-settings/tests/core_tests.cpp +++ b/packages/omamobile-settings/tests/core_tests.cpp @@ -2,7 +2,10 @@ // Copyright (c) 2026 Marsel Nenaj #include "backend.h" #include "deviceinfocontroller.h" +#include "actionrunner.h" #include +#include +#include #include #include #include @@ -14,10 +17,35 @@ class CoreTests : public QObject { Q_OBJECT private slots: - void initTestCase() { QCoreApplication::setApplicationVersion("0.1.0"); } + void initTestCase() { + QCoreApplication::setApplicationVersion("0.1.0"); + QVERIFY(m_stubs.isValid()); + const QMap answers{{"on", R"({"available":true,"limit":90})"}, {"off", R"({"available":false})"}}; + for (auto it = answers.begin(); it != answers.end(); ++it) { + QVERIFY(QDir(m_stubs.path()).mkpath(it.key())); + QFile f(stubs(it.key()) + "/charge.sh"); + QVERIFY(f.open(QIODevice::WriteOnly)); + f.write("printf '%s\\n' '" + it.value() + "'\n"); + } + } + void probeShowsTheBatteryRowOnlyWhenTheBackendSaysAvailable() { + Backend on; + on.setBackendDir(stubs("on")); + QCOMPARE(on.rows().size(), 2); + QTRY_COMPARE(on.rows().size(), 3); + Backend off; + off.setBackendDir(stubs("off")); + off.setToolAvailable("charge", true); + QTRY_COMPARE(off.rows().size(), 2); // the probe's answer clears it + } void navigationRejectsUnknownAndHiddenActions() { Backend backend; + backend.setBackendDir(stubs("off")); QVERIFY(backend.catalogProblems().isEmpty()); + QCOMPARE(backend.rows().size(), 2); // the Battery row waits for the charge backend, absent here + backend.setToolAvailable("charge", true); + QCOMPARE(backend.rows().size(), 3); + backend.setToolAvailable("charge", false); QCOMPARE(backend.rows().size(), 2); QVERIFY(!backend.navigate("recording")); backend.activate("arbitrary-command"); @@ -31,13 +59,84 @@ private slots: } void displayPageGoesBackToRoot() { Backend backend; + backend.setBackendDir(stubs("off")); QVERIFY(backend.navigate("display")); QCOMPARE(backend.page(), QString("display")); backend.back(); QCOMPARE(backend.page(), QString("root")); } + void batteryPageGoesBackToRoot() { + Backend backend; + backend.setBackendDir(stubs("off")); + QVERIFY(backend.navigate("battery")); + QCOMPARE(backend.page(), QString("battery")); + backend.back(); + QCOMPARE(backend.page(), QString("root")); + backend.search("charge limit"); + QCOMPARE(backend.rows().size(), 0); // hidden from search too while the backend reports no limit + backend.setToolAvailable("charge", true); + backend.search("charge limit"); + QCOMPARE(backend.rows().size(), 1); + QCOMPARE(backend.rows().first().toMap()["id"].toString(), QString("battery")); + } + void runnerKnowsChargeAndRejectsOthers() { + ActionRunner runner; + runner.setBackendDir(stubs("off")); + QSignalSpy done(&runner, &ActionRunner::finished); + const int bad = runner.run("nope", {"get"}); + const int badArg = runner.run("charge", {"set", "limit", "80;x"}); + const int charge = runner.run("charge", {"get"}); + QTRY_COMPARE(done.count(), 3); + QMap errors; + QMap answers; + for (const auto &call : done) { + errors[call.at(2).toInt()] = call.at(1).toMap()["error"].toString(); + answers[call.at(2).toInt()] = call.at(1).toMap(); + } + QCOMPARE(errors[bad], QString("unknown action")); + QCOMPARE(errors[badArg], QString("bad argument")); + // the stub's own answer, so the runner ran the stub and not an installed charge.sh + QCOMPARE(errors[charge], QString()); + QCOMPARE(answers[charge].value("available"), QVariant(false)); + } + void batteryPageLoadsAndShowsTheBackendAnswer_data() { + QTest::addColumn("stub"); + QTest::newRow("available") << "on"; + QTest::newRow("not available") << "off"; + } + void batteryPageLoadsAndShowsTheBackendAnswer() { + QFETCH(QString, stub); + ActionRunner runner; + runner.setBackendDir(stubs(stub)); + Backend backend; // outlives the engine: the style singleton reads it until the engine goes + backend.setBackendDir(stubs(stub)); + QQmlEngine engine; + engine.rootContext()->setContextProperty("backend", &backend); + engine.rootContext()->setContextProperty("actions", &runner); + QQmlComponent component(&engine, QUrl("qrc:/BatteryPage.qml")); + std::unique_ptr page(component.create()); + QVERIFY2(page, qPrintable(component.errorString())); + QCOMPARE(page->property("levels").toList().size(), 5); + // the page runs `charge get` through the runner and leaves "busy" with the stub's answer + QTRY_VERIFY(!page->property("busy").toBool()); + if (stub == "on") { + QVERIFY(page->property("available").toBool()); + QCOMPARE(page->property("limit").toInt(), 90); + QVERIFY(page->property("status").toString().startsWith("Stops charging at this level")); + } else { + QVERIFY(!page->property("available").toBool()); + QCOMPARE(page->property("status").toString(), QString("Settings cannot set a charge limit on this phone.")); + } + QMetaObject::invokeMethod(page.get(), "apply", QVariant(QVariantMap{{"available", true}, {"limit", 85}})); + QVERIFY(page->property("available").toBool()); + QCOMPARE(page->property("limit").toInt(), 85); + QVERIFY(page->property("status").toString().startsWith("Stops charging at this level")); + QMetaObject::invokeMethod(page.get(), "apply", QVariant(QVariantMap{{"available", false}})); + QCOMPARE(page->property("status").toString(), QString("Settings cannot set a charge limit on this phone.")); + } void searchHasOnlyImplementedRoutesAndBackClearsIt() { Backend backend; + backend.setBackendDir(stubs("off")); backend.search("STORAGE"); QCOMPARE(backend.rows().size(), 1); backend.search("brightness"); @@ -67,6 +166,7 @@ private slots: } QVERIFY(QFile::setPermissions(command, QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner)); Backend backend; + backend.setBackendDir(stubs("off")); backend.setHomeCommand({command, marker}); QSignalSpy home(&backend, &Backend::homeRequested); // A page and a search on it: Back leaves the search, then the page. @@ -108,6 +208,7 @@ private slots: } void iconNamesCannotEscapeResources() { Backend backend; + backend.setBackendDir(stubs("off")); QVERIFY(backend.icon("../catalog").isEmpty()); QVERIFY(backend.icon("unknown").isEmpty()); QVERIFY(backend.icon("system").startsWith("data:image/svg+xml;base64,")); @@ -122,6 +223,7 @@ private slots: }; QVERIFY(save("background = '#112233'\nforeground = '#eeeeee'\naccent = '#66aa88'\n")); Backend backend; + backend.setBackendDir(stubs("off")); QCOMPARE(backend.background(), QColor("#112233")); QVERIFY(save("background = '#ddeeff'\nforeground = '#112233'\naccent = '#557799'\n")); QTRY_COMPARE(backend.background(), QColor("#ddeeff")); @@ -140,6 +242,7 @@ private slots: QVERIFY(save(directory+"/colors.toml","background='#112233'\nforeground='#eeeeee'\naccent='#66aa88'\nmuted='#778899'\n")); QVERIFY(save(directory+"/shell.toml","[font]\nbase-size=12\n[controls]\nnormal-fill-alpha=0.04\n")); Backend backend; + backend.setBackendDir(stubs("off")); QCOMPARE(backend.themeTokens()["fontScale"].toDouble(),1.0); QCOMPARE(backend.muted(),QColor("#778899")); const auto page=backend.page(); @@ -165,6 +268,7 @@ private slots: } void actualWindowAndReadOnlyAbout() { Backend backend; + backend.setBackendDir(stubs("off")); backend.setHomeCommand({}); DeviceInfoController info(true); QQmlApplicationEngine engine; @@ -190,6 +294,11 @@ private slots: QTRY_COMPARE(info.info()["model"].toString(), QString("Mobile device")); } +private: + // Stand-ins for backends/charge.sh, so no test reads the backend installed on the host: "on" answers like a + // phone with a declared charge limit, "off" like any other. + QString stubs(const QString &name) const { return m_stubs.filePath(name); } + QTemporaryDir m_stubs; }; QTEST_MAIN(CoreTests) #include "core_tests.moc" diff --git a/packages/omamobile-settings/tests/core_tests.pro b/packages/omamobile-settings/tests/core_tests.pro index 0b1d6ace6..228e867ff 100644 --- a/packages/omamobile-settings/tests/core_tests.pro +++ b/packages/omamobile-settings/tests/core_tests.pro @@ -4,6 +4,6 @@ CONFIG += testcase c++17 console no_include_pwd TARGET = settings-core-tests TEMPLATE = app INCLUDEPATH += ../app -SOURCES += core_tests.cpp ../app/backend.cpp ../app/deviceinfocontroller.cpp -HEADERS += ../app/backend.h ../app/deviceinfocontroller.h +SOURCES += core_tests.cpp ../app/backend.cpp ../app/deviceinfocontroller.cpp ../app/actionrunner.cpp +HEADERS += ../app/backend.h ../app/deviceinfocontroller.h ../app/actionrunner.h RESOURCES += ../app/resources.qrc diff --git a/packages/omamobile-settings/vendor/polkit/50-omamobile-charge-limit.rules b/packages/omamobile-settings/vendor/polkit/50-omamobile-charge-limit.rules new file mode 100644 index 000000000..1c8dbc559 --- /dev/null +++ b/packages/omamobile-settings/vendor/polkit/50-omamobile-charge-limit.rules @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: MIT +// The Settings app's Battery page: let the active local user start +// omamobile-charge-limit@80|85|90|95|100.service, which runs +// "/usr/lib/omamobile-settings/charge-limit N" as root. Nothing else. +polkit.addRule(function(action, subject) { + if (action.id == "org.freedesktop.systemd1.manage-units" && + subject.local && subject.active && + /^omamobile-charge-limit@(80|85|90|95|100)\.service$/.test(action.lookup("unit")) && + action.lookup("verb") == "start") { + return polkit.Result.YES; + } +}); diff --git a/packages/omamobile-settings/vendor/systemd/system/omamobile-charge-limit@.service b/packages/omamobile-settings/vendor/systemd/system/omamobile-charge-limit@.service new file mode 100644 index 000000000..4f3911662 --- /dev/null +++ b/packages/omamobile-settings/vendor/systemd/system/omamobile-charge-limit@.service @@ -0,0 +1,12 @@ +# SPDX-License-Identifier: MIT +# The Settings app's charge limit: saves %i (80, 85, 90, 95 or 100) to /etc/omamobile/charge-limit and writes it +# to the charger's charge_control_end_threshold. The polkit rule 50-omamobile-charge-limit.rules lets the active +# local user start these instances and nothing else; the script refuses any other value. Not enabled. +[Unit] +Description=Charge limit %i %% +StartLimitIntervalSec=0 + +[Service] +Type=oneshot +ExecStart=/usr/lib/omamobile-settings/charge-limit %i +TimeoutStartSec=15 diff --git a/packages/omamobile-settings/version b/packages/omamobile-settings/version index d917d3e26..b1e80bb24 100644 --- a/packages/omamobile-settings/version +++ b/packages/omamobile-settings/version @@ -1 +1 @@ -0.1.2 +0.1.3