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
24 changes: 16 additions & 8 deletions bin/omarchy-battery-status
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,23 @@ power_rate_raw=$(awk '/energy-rate/ { print $2; exit }' <<<"$battery_info")
native_path=$(awk '/native-path/ { print $2; exit }' <<<"$battery_info")
battery_path="$power_supply_path/$native_path"

# A sysfs reading as a number, or nothing. Firmware that doesn't know the
# present rate leaves current_now in place but fails every read of it (ENODEV),
# so a readable file is no promise of a value.
read_sysfs_number() {
local value
{ value=$(<"$1"); } 2>/dev/null || return 1
[[ $value =~ ^-?[0-9]+$ ]] || return 1
echo "$value"
}

# UPower's energy-rate can lag the kernel telemetry by tens of seconds. Use
# the instantaneous sysfs reading when available so the open panel stays live.
if [[ -r $battery_path/power_now ]]; then
power_rate_raw=$(awk -v microwatts="$(<"$battery_path/power_now")" 'BEGIN { print microwatts / 1000000 }')
elif [[ -r $battery_path/current_now && -r $battery_path/voltage_now ]]; then
power_rate_raw=$(awk \
-v microamps="$(<"$battery_path/current_now")" \
-v microvolts="$(<"$battery_path/voltage_now")" \
'BEGIN { print microamps * microvolts / 1000000000000 }')
# the instantaneous sysfs reading when there is one so the open panel stays
# live; without one, UPower's rate, worked out from the falling charge, stands.
if microwatts=$(read_sysfs_number "$battery_path/power_now"); then
power_rate_raw=$(awk -v microwatts="$microwatts" 'BEGIN { print microwatts / 1000000 }')
elif microamps=$(read_sysfs_number "$battery_path/current_now") && microvolts=$(read_sysfs_number "$battery_path/voltage_now"); then
power_rate_raw=$(awk -v microamps="$microamps" -v microvolts="$microvolts" 'BEGIN { print microamps * microvolts / 1000000000000 }')
fi

# Some kernels sign power_now by direction (negative while discharging on
Expand Down
9 changes: 9 additions & 0 deletions test/shell.d/battery-status-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ grep -Fx $'rate\t7.3W' <<<"$generic_output" >/dev/null || fail "battery status a
grep -Fx $'time\t3h 53m' <<<"$generic_output" >/dev/null || fail "battery status time left follows the live draw"
pass "battery status supports Apple Silicon and arbitrary native battery paths"

# Firmware that doesn't know the present rate leaves current_now in place but
# gives nothing on read; UPower's rate, from the falling charge, stands.
rm "$tmp_dir/power/CMB0/power_now"
: >"$tmp_dir/power/CMB0/current_now"
printf '12900000\n' >"$tmp_dir/power/CMB0/voltage_now"
unknown_rate_output=$(OMARCHY_TEST_NATIVE_PATH=CMB0 OMARCHY_POWER_SUPPLY_PATH="$tmp_dir/power" PATH="$tmp_dir/bin:$PATH" "$ROOT/bin/omarchy-battery-status" --shell)
grep -Fx $'rate\t7.3W' <<<"$unknown_rate_output" >/dev/null || fail "an unreadable current_now leaves UPower's rate in place" "$unknown_rate_output"
pass "battery status keeps UPower's rate when the battery has no live reading"

# An 80% hold threshold doesn't trip while the battery is still below it.
hold_dir=$(mktemp -d)
trap 'rm -rf "$tmp_dir" "$hold_dir"' EXIT
Expand Down
Loading