Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions bin/omarchy-battery-charge-limit
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/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"

usage() {
echo "Usage: omarchy battery charge limit [80|100]" >&2
exit 2
}

(( $# <= 1 )) || usage
limit="${1:-status}"
case "$limit" in
status | 80 | 100) ;;
*) usage ;;
esac

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")"
exit 0
fi

if [[ $limit == "80" ]]; then
expected_start=75
else
expected_start=100
fi

if (( EUID == 0 )); then
printf '%s\n' "$limit" | /usr/bin/tee "$end_file" >/dev/null
else
printf '%s\n' "$limit" | sudo /usr/bin/tee "$end_file" >/dev/null
fi

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

printf 'Charge limit set to %s%% (restart charging at %s%%).\n' "$actual_end" "$actual_start"
2 changes: 2 additions & 0 deletions manual/36-system-sleep.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ On a laptop, Omarchy remembers your power profile separately for plugged in and

You can see what your machine offers with `omarchy powerprofiles list`, and set the one you want for the state you're currently in with `omarchy powerprofiles set autodetect power-saver`. To set the other state without unplugging anything, name it directly: `omarchy powerprofiles set battery power-saver`. Whatever you pick is what you'll get back the next time you're in that state.

On supported Apple Silicon MacBooks, the battery charge controller can hold charging below full to reduce time spent at a high state of charge. Check the current setting with `omarchy battery charge limit`, set an 80% cap with `omarchy battery charge limit 80`, or restore full charging with `omarchy battery charge limit 100`. The 80% setting resumes charging at 75%. The default remains full charging.

### Toggle suspend

You toggle suspend by running `omarchy toggle suspend` from the terminal. That just reveals/hides the option under _System_ (or `Super + Esc`), and then you can see if it works consistently on your system. If not, you can hide it again with the same command.
Expand Down
80 changes: 80 additions & 0 deletions test/shell.d/battery-charge-limit-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/bin/bash

set -euo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
tmp_dir=$(mktemp -d)
trap 'rm -rf "$tmp_dir"' EXIT

fail() {
echo "FAIL: $*" >&2
exit 1
}

pass() {
echo "PASS: $*"
}

battery_path="$tmp_dir/macsmc-battery"
mkdir -p "$battery_path" "$tmp_dir/bin"
printf '100\n' >"$battery_path/charge_control_start_threshold"
printf '100\n' >"$battery_path/charge_control_end_threshold"

cat >"$tmp_dir/bin/sudo" <<'SH'
#!/bin/bash
[[ $1 == "/usr/bin/tee" && $2 == "$BATTERY_PATH/charge_control_end_threshold" ]] || exit 1
printf 'write\n' >>"$SUDO_LOG"
[[ ${SUDO_FAIL:-} != 1 ]] || exit 1
value=$(cat)
[[ ${SUDO_IGNORE:-} != 1 ]] || exit 0
printf '%s\n' "$value" >"$2"
if [[ $value == 80 ]]; then
printf '75\n' >"$BATTERY_PATH/charge_control_start_threshold"
else
printf '100\n' >"$BATTERY_PATH/charge_control_start_threshold"
fi
SH
chmod +x "$tmp_dir/bin/sudo"

export PATH="$tmp_dir/bin:$PATH"
export BATTERY_PATH="$battery_path" SUDO_LOG="$tmp_dir/sudo.log"
test_command="$tmp_dir/bin/omarchy-battery-charge-limit"
# Exercise the terminal sudo path even when the suite runs as root.
sed -e "s|/sys/class/power_supply/macsmc-battery|$battery_path|" \
-e 's/(( EUID == 0 ))/(( 1 == 0 ))/' \
"$ROOT/bin/omarchy-battery-charge-limit" >"$test_command"
chmod +x "$test_command"

status=$("$test_command")
[[ $status == "Charge limit: 100% (restart charging at 100%)" ]] || fail "reports the active limit"
pass "reports the active Apple Silicon charge thresholds"
[[ ! -e $SUDO_LOG ]] || fail "status does not request privilege"

"$test_command" 80
[[ $(<"$battery_path/charge_control_start_threshold") == 75 ]] || fail "sets the restart threshold to 75%"
[[ $(<"$battery_path/charge_control_end_threshold") == 80 ]] || fail "sets the end threshold to 80%"
pass "sets the 80% cap and five point hysteresis"

"$test_command" 100
[[ $(<"$battery_path/charge_control_start_threshold") == 100 ]] || fail "restores the restart threshold"
[[ $(<"$battery_path/charge_control_end_threshold") == 100 ]] || fail "restores the end threshold"
pass "restores full charging"

if SUDO_FAIL=1 "$test_command" 80 >/dev/null 2>&1; then
fail "reports a failed threshold update"
fi
[[ $(<"$battery_path/charge_control_start_threshold") == 100 ]] || fail "preserves the restart threshold after a failed update"
[[ $(<"$battery_path/charge_control_end_threshold") == 100 ]] || fail "preserves the end threshold after a failed update"
pass "reports a failed write without changing either threshold"

if SUDO_IGNORE=1 "$test_command" 80 >/dev/null 2>&1; then
fail "reports a threshold update that did not stick"
fi
pass "checks the driver readback after a successful write"

if "$test_command" 85 >/dev/null 2>&1; then
fail "rejects unsupported limits"
fi
[[ $(<"$battery_path/charge_control_end_threshold") == 100 ]] || fail "invalid input leaves the limit unchanged"
[[ $(wc -l <"$SUDO_LOG") == 4 ]] || fail "writes only the end threshold once per change"
pass "rejects unsupported limits without changing the device"
Loading