From a422c813675c6f2eb69257fb429b0416a5c301cc Mon Sep 17 00:00:00 2001 From: Marcelo Alcantara Date: Fri, 25 Sep 2026 19:04:25 +1000 Subject: [PATCH 1/2] Add the Apple Silicon battery charge limit to omarchy-mac and restore it at boot --- packages/omarchy-mac/ORIGINS.md | 1 + packages/omarchy-mac/README.md | 6 + .../bin/omarchy-battery-charge-limit | 72 +++++++++ .../lib/battery-charge-limit-restore | 21 +++ packages/omarchy-mac/test/battery-test.sh | 152 ++++++++++++++++++ .../94-omarchy-mac-battery-charge-limit.rules | 2 + 6 files changed, 254 insertions(+) create mode 100755 packages/omarchy-mac/bin/omarchy-battery-charge-limit create mode 100755 packages/omarchy-mac/lib/battery-charge-limit-restore create mode 100644 packages/omarchy-mac/test/battery-test.sh create mode 100644 packages/omarchy-mac/vendor/udev/rules.d/94-omarchy-mac-battery-charge-limit.rules diff --git a/packages/omarchy-mac/ORIGINS.md b/packages/omarchy-mac/ORIGINS.md index 8ebb99da6ef2..923299e94f33 100644 --- a/packages/omarchy-mac/ORIGINS.md +++ b/packages/omarchy-mac/ORIGINS.md @@ -8,3 +8,4 @@ The network backend default follows Marcelo Alcantara's Apple Silicon integratio - Microphone mapper, user service, headset priority and behavioral tests: Scott Jones, `1d2f4af12cb4b65731bafa1305d1d5c9ac5507a8` (Map the Asahi mic array and install the protected audio stack). The mapper is extracted unchanged, including gain/mute persistence, device choice protection, graph rollback, and event-based supervision. - Notch module default: Scott Jones, `cfe1d37e8f0fa27811938872325119e3eeca9354` (Floor the top bar at the Apple Silicon camera cutout), extracted from `install/hardware/apple/enable-notch.sh` at the baseline above; only the existing `appledrm show_notch=1` default moves to the vendor directory. +- Battery charge limit command and behavioral tests: Naeem Malik, `00a2d31019e4d5ea5a3e5e9cb45a75aa90d07ee2` (Add an Apple Silicon battery charge limit command, #497). The driver semantics and readback check are retained; the platform gate, saved limit and boot-time restore are new. diff --git a/packages/omarchy-mac/README.md b/packages/omarchy-mac/README.md index a88f18993843..5da3e8ce1f3c 100644 --- a/packages/omarchy-mac/README.md +++ b/packages/omarchy-mac/README.md @@ -18,6 +18,12 @@ Run `omarchy-mac-setup-user` as each target user with their HOME/XDG directories Vendor defaults use NetworkManager's `/usr/lib/NetworkManager/conf.d`, systemd's `/usr/lib/systemd`, modprobe's `/usr/lib/modprobe.d`, and WirePlumber's `/usr/share/wireplumber/wireplumber.conf.d`. Same-name `/etc` or user fragments retain precedence. Setup reports effective live NetworkManager/module configuration and systemd fragments. Review those reports and any drop-ins when diagnosing overrides; custom policy is never normalized to the package default. +## Battery charge limit + +`omarchy battery charge limit [80|100]` shows or sets the `macsmc-battery` charge thresholds and refuses unless `omarchy-hw-platform` reports `apple-silicon`. It writes only the end threshold through `sudo`, since the driver derives the start threshold (80 restarts charging at 75; 100 restores full charging), and checks the SMC's readback. Full charging stays the default. + +A set limit is saved as `CHARGE_CONTROL_END_THRESHOLD=` in `/etc/udev/macsmc-battery.conf`, the file asahi-scripts' own rule restores from, so both agree on one value. The package's udev rule runs `/usr/lib/omarchy-mac/battery-charge-limit-restore` when the SMC battery appears at boot; it reapplies a saved 80 or 100 on Apple Silicon and otherwise leaves the SMC alone. Nothing needs enabling. + ## Candidate qualification The mocked behavioral tests cover Wi-Fi health, disabled radio, journal cursor/backstop recovery, unload/load failures and chipset gates; microphone gain/mute, device choices, missing endpoints, rollback and daemon loss; setup covers fresh/upgrade/repeated/interrupted operations, two users, first-session activation, masks and overrides. The desktop retains migration and audio restart integration coverage. diff --git a/packages/omarchy-mac/bin/omarchy-battery-charge-limit b/packages/omarchy-mac/bin/omarchy-battery-charge-limit new file mode 100755 index 000000000000..9dd08edd8a96 --- /dev/null +++ b/packages/omarchy-mac/bin/omarchy-battery-charge-limit @@ -0,0 +1,72 @@ +#!/bin/bash + +# omarchy:summary=Set or show the Apple Silicon battery charge limit +# omarchy:args=[80|100] +# omarchy:examples=omarchy battery charge limit | omarchy battery charge limit 80 | omarchy battery charge limit 100 + +set -euo pipefail + +battery_path="/sys/class/power_supply/macsmc-battery" +start_file="$battery_path/charge_control_start_threshold" +end_file="$battery_path/charge_control_end_threshold" +# asahi-scripts saves and restores the limit through this same file, so both +# mechanisms agree on one saved value. +saved_file="/etc/udev/macsmc-battery.conf" + +usage() { + echo "Usage: omarchy battery charge limit [80|100]" >&2 + exit 2 +} + +write_as_root() { + if (( EUID == 0 )); then + /usr/bin/tee "$1" >/dev/null + else + sudo /usr/bin/tee "$1" >/dev/null + fi +} + +(( $# <= 1 )) || usage +limit="${1:-status}" +case "$limit" in + status | 80 | 100) ;; + *) usage ;; +esac + +if ! platform=$(omarchy-hw-platform) || [[ $platform != "apple-silicon" ]]; then + echo "The battery charge limit is only available on Apple Silicon Macs." >&2 + exit 1 +fi + +if [[ ! -r $start_file || ! -r $end_file ]]; then + echo "Apple Silicon charge thresholds are not available on this system." >&2 + exit 1 +fi + +if [[ $limit == "status" ]]; then + printf 'Charge limit: %s%% (restart charging at %s%%)\n' "$(<"$end_file")" "$(<"$start_file")" +else + if [[ $limit == "80" ]]; then + expected_start=75 + else + expected_start=100 + fi + + # The driver derives the start threshold from the end threshold and ignores + # writes to the start threshold. + printf '%s\n' "$limit" | write_as_root "$end_file" + + actual_start=$(<"$start_file") + actual_end=$(<"$end_file") + if [[ $actual_start != "$expected_start" || $actual_end != "$limit" ]]; then + echo "Charge threshold update did not stick; the SMC reports ${actual_start}%/${actual_end}%." >&2 + exit 1 + fi + + if ! printf 'CHARGE_CONTROL_END_THRESHOLD=%s\n' "$limit" | write_as_root "$saved_file"; then + echo "Charge limit set to ${limit}%, but it could not be saved for the next boot." >&2 + exit 1 + fi + + printf 'Charge limit set to %s%% (restart charging at %s%%).\n' "$actual_end" "$actual_start" +fi diff --git a/packages/omarchy-mac/lib/battery-charge-limit-restore b/packages/omarchy-mac/lib/battery-charge-limit-restore new file mode 100755 index 000000000000..0dd80229ee48 --- /dev/null +++ b/packages/omarchy-mac/lib/battery-charge-limit-restore @@ -0,0 +1,21 @@ +#!/bin/bash +# udev runs this as the SMC battery appears, so a saved charge limit survives +# reboots. It never fails the event: anything unexpected leaves the SMC alone. +set -euo pipefail + +saved_file="/etc/udev/macsmc-battery.conf" +end_file="/sys/class/power_supply/macsmc-battery/charge_control_end_threshold" + +[[ $(omarchy-hw-platform 2>/dev/null) == "apple-silicon" ]] || exit 0 +[[ -r $saved_file && -w $end_file ]] || exit 0 + +limit="" +while IFS= read -r line || [[ -n $line ]]; do + if [[ $line == CHARGE_CONTROL_END_THRESHOLD=* ]]; then + limit=${line#*=} + fi +done <"$saved_file" + +if [[ $limit == "80" || $limit == "100" ]]; then + printf '%s\n' "$limit" >"$end_file" || true +fi diff --git a/packages/omarchy-mac/test/battery-test.sh b/packages/omarchy-mac/test/battery-test.sh new file mode 100644 index 000000000000..727b0cd5b17b --- /dev/null +++ b/packages/omarchy-mac/test/battery-test.sh @@ -0,0 +1,152 @@ +#!/bin/bash +set -euo pipefail +source "$(dirname "$0")/base-test.sh" +work=$(mktemp -d) +trap 'rm -rf "$work"' EXIT +stage="$work/stage" +"$ROOT/install" "$stage" +rule="$stage/usr/lib/udev/rules.d/94-omarchy-mac-battery-charge-limit.rules" +[[ -x $stage/usr/bin/omarchy-battery-charge-limit && -x $stage/usr/lib/omarchy-mac/battery-charge-limit-restore && -f $rule ]] || fail 'stages the command, restore helper and udev rule' +grep -Fxq 'ACTION=="add", SUBSYSTEM=="power_supply", KERNEL=="macsmc-battery", RUN+="/usr/lib/omarchy-mac/battery-charge-limit-restore"' "$rule" || fail 'rule runs the staged helper only for the SMC battery' +if command -v udevadm >/dev/null && udevadm verify --help >/dev/null 2>&1; then + udevadm verify --resolve-names=never "$rule" >/dev/null || fail 'udev rule verifies' +fi +pass 'stages a udev rule that restores the limit for the SMC battery only' + +battery="$work/sys/macsmc-battery" +saved="$work/etc/udev/macsmc-battery.conf" +mkdir -p "$battery" "${saved%/*}" "$work/bin" +export BATTERY="$battery" SAVED="$saved" SUDO_LOG="$work/sudo.log" + +# Exercise the terminal sudo path even when the suite runs as root, against a +# fixture battery and saved-limit file. +fixture() { + sed -e "s|/sys/class/power_supply/macsmc-battery|$battery|" \ + -e "s|/etc/udev/macsmc-battery.conf|$saved|" \ + -e 's/(( EUID == 0 ))/(( 1 == 0 ))/' "$1" >"$2" + chmod +x "$2" +} +command="$work/bin/omarchy-battery-charge-limit" +restore="$work/battery-charge-limit-restore" +fixture "$stage/usr/bin/omarchy-battery-charge-limit" "$command" +fixture "$stage/usr/lib/omarchy-mac/battery-charge-limit-restore" "$restore" + +cat >"$work/bin/omarchy-hw-platform" <<'STUB' +#!/bin/bash +if [[ ${PLATFORM:-apple-silicon} == "contradiction" ]]; then + echo "Error: contradictory platform identity" >&2 + exit 1 +fi +echo "${PLATFORM:-apple-silicon}" +STUB +# The SMC derives the start threshold from the end threshold. +cat >"$work/bin/sudo" <<'STUB' +#!/bin/bash +(( $# == 2 )) && [[ $1 == "/usr/bin/tee" ]] || exit 1 +printf '%s\n' "$2" >>"$SUDO_LOG" +value=$(cat) +if [[ $2 == "$BATTERY/charge_control_end_threshold" ]]; then + [[ ${SUDO_FAIL:-} != 1 ]] || exit 1 + [[ ${SUDO_IGNORE:-} != 1 ]] || exit 0 + printf '%s\n' "$value" >"$2" + if [[ $value == "80" ]]; then + printf '75\n' >"$BATTERY/charge_control_start_threshold" + else + printf '100\n' >"$BATTERY/charge_control_start_threshold" + fi +elif [[ $2 == "$SAVED" ]]; then + [[ ${SAVE_FAIL:-} != 1 ]] || exit 1 + printf '%s\n' "$value" >"$2" +else + exit 1 +fi +STUB +chmod +x "$work/bin/"* +export PATH="$work/bin:$PATH" + +smc() { + printf '%s\n' "$1" >"$battery/charge_control_start_threshold" + printf '%s\n' "$2" >"$battery/charge_control_end_threshold" +} +reads() { + [[ $(<"$battery/charge_control_start_threshold") == "$1" && $(<"$battery/charge_control_end_threshold") == "$2" ]] +} +smc 100 100 + +[[ $("$command") == "Charge limit: 100% (restart charging at 100%)" ]] || fail 'reports the active limit' +[[ ! -e $SUDO_LOG && ! -e $saved ]] || fail 'status needs no privilege and saves nothing' +pass 'reports the active Apple Silicon charge thresholds' + +[[ $("$command" 80) == "Charge limit set to 80% (restart charging at 75%)." ]] || fail 'reports the 80% cap' +reads 75 80 || fail 'sets the 80% cap and five point hysteresis' +[[ $(<"$saved") == "CHARGE_CONTROL_END_THRESHOLD=80" ]] || fail 'saves the 80% cap for the next boot' +"$command" 100 >/dev/null +reads 100 100 || fail 'restores full charging' +[[ $(<"$saved") == "CHARGE_CONTROL_END_THRESHOLD=100" ]] || fail 'saves full charging for the next boot' +[[ $(grep -c charge_control_end_threshold "$SUDO_LOG") == 2 && $(grep -c -v charge_control_end_threshold "$SUDO_LOG") == 2 ]] || fail 'writes only the end threshold and the saved limit once per change' +pass 'sets and clears the limit and saves it for the next boot' + +if SUDO_FAIL=1 "$command" 80 >/dev/null 2>&1; then fail 'reports a failed threshold update'; fi +reads 100 100 && [[ $(<"$saved") == "CHARGE_CONTROL_END_THRESHOLD=100" ]] || fail 'a failed write changes nothing' +if SUDO_IGNORE=1 "$command" 80 >/dev/null 2>&1; then fail 'reports an update that did not stick'; fi +[[ $(<"$saved") == "CHARGE_CONTROL_END_THRESHOLD=100" ]] || fail 'an update that did not stick is not saved' +if SAVE_FAIL=1 "$command" 80 >/dev/null 2>"$work/err"; then fail 'reports a limit that could not be saved'; fi +grep -Fq 'could not be saved' "$work/err" || fail 'says the limit was not saved' +"$command" 100 >/dev/null +pass 'checks the driver readback and reports failed writes and saves' + +: >"$SUDO_LOG" +for args in 85 0 '80 100'; do + # shellcheck disable=SC2086 + if "$command" $args >/dev/null 2>&1; then fail "rejects $args"; fi +done +reads 100 100 && [[ ! -s $SUDO_LOG ]] || fail 'invalid input leaves the limit unchanged' +pass 'rejects unsupported limits without changing the device' + +for platform in qualcomm generic-aarch64 generic contradiction; do + for args in '' 80 100; do + # shellcheck disable=SC2086 + if PLATFORM=$platform "$command" $args >/dev/null 2>&1; then fail "$platform refuses ${args:-status}"; fi + done +done +reads 100 100 && [[ ! -s $SUDO_LOG && $(<"$saved") == "CHARGE_CONTROL_END_THRESHOLD=100" ]] || fail 'non-Apple platforms leave the battery and saved limit alone' +rm "$battery/charge_control_start_threshold" +if "$command" >/dev/null 2>"$work/err"; then fail 'reports missing thresholds'; fi +grep -Fq 'not available' "$work/err" || fail 'explains missing thresholds' +smc 100 100 +pass 'refuses on non-Apple platforms and on kernels without thresholds' + +# A reboot resets the fixture SMC to full charging; the udev helper reapplies +# the saved limit. +"$command" 80 >/dev/null +smc 100 100 +"$restore" || fail 'restore succeeds' +[[ $(<"$battery/charge_control_end_threshold") == 80 ]] || fail 'restores the saved 80% cap after a reboot' +"$command" 100 >/dev/null +printf 'sentinel\n' >"$battery/charge_control_end_threshold" +"$restore" +[[ $(<"$battery/charge_control_end_threshold") == 100 ]] || fail 'restores saved full charging' +printf 'CHARGE_CONTROL_END_THRESHOLD=100\nCHARGE_CONTROL_END_THRESHOLD=80' >"$saved" +"$restore" +[[ $(<"$battery/charge_control_end_threshold") == 80 ]] || fail 'the last saved value wins, with or without a final newline' +pass 'reapplies the saved limit, including one saved by asahi-scripts' + +untouched() { + printf 'sentinel\n' >"$battery/charge_control_end_threshold" + "$@" || fail 'restore never fails the udev event' + [[ $(<"$battery/charge_control_end_threshold") == "sentinel" ]] +} +rm "$saved" +untouched "$restore" || fail 'no saved limit leaves the SMC alone' +for content in 'CHARGE_CONTROL_END_THRESHOLD=85' 'CHARGE_CONTROL_END_THRESHOLD=60' 'CHARGE_CONTROL_END_THRESHOLD="80"' 'CHARGE_CONTROL_END_THRESHOLD=80 ' 'OTHER=80' ''; do + printf '%s\n' "$content" >"$saved" + untouched "$restore" || fail "ignores saved content: $content" +done +printf 'CHARGE_CONTROL_END_THRESHOLD=80\n' >"$saved" +for platform in qualcomm generic-aarch64 generic contradiction; do + PLATFORM=$platform untouched "$restore" || fail "$platform leaves the battery alone" +done +rm "$battery/charge_control_end_threshold" +"$restore" || fail 'restore without a battery succeeds' +[[ ! -e $battery/charge_control_end_threshold ]] || fail 'restore without a battery writes nothing' +pass 'restore ignores unsupported saved values, missing batteries and non-Apple platforms' diff --git a/packages/omarchy-mac/vendor/udev/rules.d/94-omarchy-mac-battery-charge-limit.rules b/packages/omarchy-mac/vendor/udev/rules.d/94-omarchy-mac-battery-charge-limit.rules new file mode 100644 index 000000000000..1be0b0d3c846 --- /dev/null +++ b/packages/omarchy-mac/vendor/udev/rules.d/94-omarchy-mac-battery-charge-limit.rules @@ -0,0 +1,2 @@ +# Reapply the charge limit saved by omarchy-battery-charge-limit when the Apple SMC battery appears. +ACTION=="add", SUBSYSTEM=="power_supply", KERNEL=="macsmc-battery", RUN+="/usr/lib/omarchy-mac/battery-charge-limit-restore" From 1fd3033db2aef52790dc51a3d01e4d6b58880da5 Mon Sep 17 00:00:00 2001 From: Marcelo Alcantara Date: Fri, 25 Sep 2026 19:10:22 +1000 Subject: [PATCH 2/2] Cover the root path of the charge limit command and follow the conditional style --- .../bin/omarchy-battery-charge-limit | 2 +- packages/omarchy-mac/test/battery-test.sh | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/omarchy-mac/bin/omarchy-battery-charge-limit b/packages/omarchy-mac/bin/omarchy-battery-charge-limit index 9dd08edd8a96..8f97f367a340 100755 --- a/packages/omarchy-mac/bin/omarchy-battery-charge-limit +++ b/packages/omarchy-mac/bin/omarchy-battery-charge-limit @@ -58,7 +58,7 @@ else actual_start=$(<"$start_file") actual_end=$(<"$end_file") - if [[ $actual_start != "$expected_start" || $actual_end != "$limit" ]]; then + if [[ $actual_start != $expected_start || $actual_end != $limit ]]; then echo "Charge threshold update did not stick; the SMC reports ${actual_start}%/${actual_end}%." >&2 exit 1 fi diff --git a/packages/omarchy-mac/test/battery-test.sh b/packages/omarchy-mac/test/battery-test.sh index 727b0cd5b17b..e6c518f68278 100644 --- a/packages/omarchy-mac/test/battery-test.sh +++ b/packages/omarchy-mac/test/battery-test.sh @@ -45,7 +45,7 @@ cat >"$work/bin/sudo" <<'STUB' (( $# == 2 )) && [[ $1 == "/usr/bin/tee" ]] || exit 1 printf '%s\n' "$2" >>"$SUDO_LOG" value=$(cat) -if [[ $2 == "$BATTERY/charge_control_end_threshold" ]]; then +if [[ $2 == $BATTERY/charge_control_end_threshold ]]; then [[ ${SUDO_FAIL:-} != 1 ]] || exit 1 [[ ${SUDO_IGNORE:-} != 1 ]] || exit 0 printf '%s\n' "$value" >"$2" @@ -54,7 +54,7 @@ if [[ $2 == "$BATTERY/charge_control_end_threshold" ]]; then else printf '100\n' >"$BATTERY/charge_control_start_threshold" fi -elif [[ $2 == "$SAVED" ]]; then +elif [[ $2 == $SAVED ]]; then [[ ${SAVE_FAIL:-} != 1 ]] || exit 1 printf '%s\n' "$value" >"$2" else @@ -69,7 +69,7 @@ smc() { printf '%s\n' "$2" >"$battery/charge_control_end_threshold" } reads() { - [[ $(<"$battery/charge_control_start_threshold") == "$1" && $(<"$battery/charge_control_end_threshold") == "$2" ]] + [[ $(<"$battery/charge_control_start_threshold") == $1 && $(<"$battery/charge_control_end_threshold") == $2 ]] } smc 100 100 @@ -83,7 +83,7 @@ reads 75 80 || fail 'sets the 80% cap and five point hysteresis' "$command" 100 >/dev/null reads 100 100 || fail 'restores full charging' [[ $(<"$saved") == "CHARGE_CONTROL_END_THRESHOLD=100" ]] || fail 'saves full charging for the next boot' -[[ $(grep -c charge_control_end_threshold "$SUDO_LOG") == 2 && $(grep -c -v charge_control_end_threshold "$SUDO_LOG") == 2 ]] || fail 'writes only the end threshold and the saved limit once per change' +(( $(grep -c charge_control_end_threshold "$SUDO_LOG") == 2 && $(grep -c -v charge_control_end_threshold "$SUDO_LOG") == 2 )) || fail 'writes only the end threshold and the saved limit once per change' pass 'sets and clears the limit and saves it for the next boot' if SUDO_FAIL=1 "$command" 80 >/dev/null 2>&1; then fail 'reports a failed threshold update'; fi @@ -95,6 +95,16 @@ grep -Fq 'could not be saved' "$work/err" || fail 'says the limit was not saved' "$command" 100 >/dev/null pass 'checks the driver readback and reports failed writes and saves' +# As root the command writes both files itself, without sudo. +sed 's/(( 1 == 0 ))/(( 0 == 0 ))/' "$command" >"$work/root-command" +chmod +x "$work/root-command" +: >"$SUDO_LOG" +smc 75 100 +"$work/root-command" 80 >/dev/null || fail 'root sets the limit' +reads 75 80 && [[ $(<"$saved") == "CHARGE_CONTROL_END_THRESHOLD=80" && ! -s $SUDO_LOG ]] || fail 'root writes the threshold and saved limit without sudo' +"$command" 100 >/dev/null +pass 'root sets and saves the limit without sudo' + : >"$SUDO_LOG" for args in 85 0 '80 100'; do # shellcheck disable=SC2086