Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
161 changes: 161 additions & 0 deletions bin/omarchy-battery-limit
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#!/bin/bash

# omarchy:summary=Manage the battery charge limit (80% cap, 100% once, or full)
# omarchy:subcommand=status [--shell] — Show the current battery charge limit state
# omarchy:subcommand=on — Enable the hardware battery charge limit
# omarchy:subcommand=off — Disable the charge limit and allow full charging
# omarchy:subcommand=once — Charge to 100% once, then re-enable the limit automatically
# omarchy:subcommand=toggle — Toggle the battery charge limit on or off

set -euo pipefail

state_dir="${OMARCHY_BATTERY_STATE_DIR:-${XDG_STATE_HOME:-$HOME/.local/state}/omarchy/battery}"
once_file="$state_dir/top-up-once"
power_supply_path="${OMARCHY_POWER_SUPPLY_PATH:-/sys/class/power_supply}"

usage() {
echo "Usage: omarchy-battery-limit [status [--shell]|on|off|once|toggle]" >&2
exit 2
}

action="${1:-status}"
shell_output=false
if [[ $action == "status" && ${2:-} == "--shell" ]]; then
shell_output=true
elif (( $# > 1 )); then
usage
fi

upower_prop() {
local path=$1 prop=$2 raw
raw=$(busctl get-property org.freedesktop.UPower "$path" org.freedesktop.UPower.Device "$prop" 2>/dev/null) || return 1
# busctl prints e.g. "b true", "u 80", "s \"BAT1\""
raw=${raw#* }
raw=${raw#\"}
raw=${raw%\"}
printf '%s\n' "$raw"
}

# Enumerate all present system batteries that support hardware charge limits.
mapfile -t battery_paths < <(
busctl --no-pager tree --list org.freedesktop.UPower 2>/dev/null | grep '/org/freedesktop/UPower/devices/battery_' || true
)

supported_batteries=()
for path in "${battery_paths[@]}"; do
[[ -n $path ]] || continue
[[ $(upower_prop "$path" PowerSupply || true) == "true" ]] || continue
[[ $(upower_prop "$path" ChargeThresholdSupported || true) == "true" ]] || continue
supported_batteries+=("$path")
done

effective_state() {
if (( ${#supported_batteries[@]} == 0 )); then
printf 'unsupported\n'
return 0
fi

local enabled_count=0 path enabled native sysfs_end
for path in "${supported_batteries[@]}"; do
enabled=$(upower_prop "$path" ChargeThresholdEnabled || echo "false")
if [[ $enabled == "true" ]]; then
(( enabled_count++ ))
else
native=$(upower_prop "$path" NativePath || true)
if [[ -n $native && -r $power_supply_path/$native/charge_control_end_threshold ]]; then
sysfs_end=$(<"$power_supply_path/$native/charge_control_end_threshold")
if [[ $sysfs_end =~ ^[0-9]+$ ]] && (( sysfs_end > 0 && sysfs_end < 100 )); then
(( enabled_count++ ))
fi
fi
fi
done

if (( enabled_count == ${#supported_batteries[@]} )); then
printf 'on\n'
elif (( enabled_count > 0 )); then
printf 'mixed\n'
elif [[ -f $once_file ]]; then
printf 'once\n'
else
printf 'off\n'
fi
}

apply_threshold() {
local want=$1 path actual
if (( ${#supported_batteries[@]} == 0 )); then
echo "Battery charge limit is not supported on this machine." >&2
exit 1
fi

for path in "${supported_batteries[@]}"; do
busctl call org.freedesktop.UPower "$path" org.freedesktop.UPower.Device EnableChargeThreshold b "$want" >/dev/null
actual=$(upower_prop "$path" ChargeThresholdEnabled || echo "")
if [[ $actual != "$want" ]]; then
echo "UPower did not apply charge threshold ($want) on $path (got: ${actual:-unknown})." >&2
exit 1
fi
done
}

notify_shell() {
omarchy-shell -q battery refreshChargeLimit >/dev/null 2>&1 || true
}

case "$action" in
status)
cur=$(effective_state)
if [[ $shell_output == "true" ]]; then
printf 'state\t%s\n' "$cur"
if (( ${#supported_batteries[@]} > 0 )); then
first=${supported_batteries[0]}
settings_mask=$(upower_prop "$first" ChargeThresholdSettingsSupported || echo "0")
end_val=$(upower_prop "$first" ChargeEndThreshold || true)
start_val=$(upower_prop "$first" ChargeStartThreshold || true)
printf 'supported\t1\n'
if (( settings_mask == 4 )); then
printf 'mode\tfirmware\n'
else
printf 'mode\tthreshold\n'
fi
[[ -n $end_val ]] && printf 'end\t%s\n' "$end_val"
[[ -n $start_val ]] && printf 'start\t%s\n' "$start_val"
else
printf 'supported\t0\n'
fi
else
printf '%s\n' "$cur"
fi
;;
on)
apply_threshold true
rm -f "$once_file"
notify_shell
;;
off)
apply_threshold false
rm -f "$once_file"
notify_shell
;;
once)
apply_threshold false
mkdir -p "$state_dir"
printf '%s\n' "$(date +%s)" >"$once_file"
notify_shell
;;
toggle)
cur=$(effective_state)
if [[ $cur == "on" || $cur == "once" ]]; then
apply_threshold false
rm -f "$once_file"
else
apply_threshold true
rm -f "$once_file"
fi
notify_shell
;;
*)
usage
;;
esac
142 changes: 134 additions & 8 deletions bin/omarchy-battery-status
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

shell_output=false
power_supply_path="${OMARCHY_POWER_SUPPLY_PATH:-/sys/class/power_supply}"
sleep_run_dir="${OMARCHY_SLEEP_RUN_DIR:-/run/omarchy}"
battery_state_dir="${OMARCHY_BATTERY_STATE_DIR:-${XDG_STATE_HOME:-$HOME/.local/state}/omarchy/battery}"

case "${1:-}" in
"")
Expand Down Expand Up @@ -56,6 +58,8 @@ percentage=$(awk '/percentage/ { print int($2); exit }' <<<"$battery_info")
capacity=$(awk '/energy-full:/ { printf "%d", $2; exit }' <<<"$battery_info")
energy=$(awk '/^ *energy:/ { print $2; exit }' <<<"$battery_info")
energy_full=$(awk '/energy-full:/ { print $2; exit }' <<<"$battery_info")
energy_full_design=$(awk '/energy-full-design:/ { print $2; exit }' <<<"$battery_info")
upower_capacity=$(awk '/^ *capacity:/ { gsub(/%/, "", $2); print $2; exit }' <<<"$battery_info")
upower_hours=$(awk '/time to (empty|full)/ { print ($5 ~ /^minute/ ? $4 / 60 : $4); exit }' <<<"$battery_info")
power_rate_raw=$(awk '/energy-rate/ { print $2; exit }' <<<"$battery_info")
native_path=$(awk '/native-path/ { print $2; exit }' <<<"$battery_info")
Expand Down Expand Up @@ -89,17 +93,47 @@ power_rate=$(awk -v rate="${power_rate_raw:-0}" 'BEGIN {
print rounded
}')
state=$(awk '/state/ { print $2; exit }' <<<"$battery_info")
threshold_start=$(awk '/charge-start-threshold:/ { gsub(/%/, "", $2); print int($2); exit }' <<<"$battery_info")
threshold_end=$(awk '/charge-end-threshold:/ { gsub(/%/, "", $2); print int($2); exit }' <<<"$battery_info")
upower_threshold_start=$(awk '/charge-start-threshold:/ { gsub(/%/, "", $2); print int($2); exit }' <<<"$battery_info")
upower_threshold_end=$(awk '/charge-end-threshold:/ { gsub(/%/, "", $2); print int($2); exit }' <<<"$battery_info")
upower_threshold_enabled=$(awk '/charge-threshold-enabled:/ { print tolower($2); exit }' <<<"$battery_info")
upower_threshold_supported=$(awk '/charge-threshold-supported:/ { print tolower($2); exit }' <<<"$battery_info")

sysfs_threshold_end=$(read_sysfs_number "$battery_path/charge_control_end_threshold" || true)
sysfs_threshold_start=$(read_sysfs_number "$battery_path/charge_control_start_threshold" || true)

# Sysfs charge_control_end_threshold is the kernel's ground truth: UPower 1.91+
# prints hwdb defaults (e.g. 75/80%) in `upower -i` even when ChargeThresholdEnabled
# is false and sysfs is at 100%. Only fall back to UPower's thresholds when no
# sysfs file exists (e.g. firmware-only conservation mode or test stubs).
threshold_start=""
threshold_end=""
if [[ -n $sysfs_threshold_end ]]; then
if (( sysfs_threshold_end > 0 && sysfs_threshold_end < 100 )); then
threshold_end=$sysfs_threshold_end
if [[ -n $sysfs_threshold_start ]] && (( sysfs_threshold_start > 0 && sysfs_threshold_start < threshold_end )); then
threshold_start=$sysfs_threshold_start
fi
fi
elif [[ $upower_threshold_enabled != "no" ]]; then
if [[ -n $upower_threshold_end ]] && (( upower_threshold_end > 0 && upower_threshold_end < 100 )); then
threshold_end=$upower_threshold_end
threshold_start=$upower_threshold_start
fi
fi

# Read the thresholds from the battery UPower resolved rather than a BAT* glob,
# which misses every battery with another name.
[[ -n $threshold_end || ! -r $battery_path/charge_control_end_threshold ]] || threshold_end=$(<"$battery_path/charge_control_end_threshold")
[[ -n $threshold_start || ! -r $battery_path/charge_control_start_threshold ]] || threshold_start=$(<"$battery_path/charge_control_start_threshold")
# The time to full counts to this battery's own limit, never another one's.
charge_limit=${threshold_end:-100}
[[ -n $threshold_end ]] || threshold_end=$(cat "$power_supply_path"/BAT*/charge_control_end_threshold 2>/dev/null | head -1)
[[ -n $threshold_start ]] || threshold_start=$(cat "$power_supply_path"/BAT*/charge_control_start_threshold 2>/dev/null | head -1)
if [[ -z $sysfs_threshold_end && $upower_threshold_enabled != "no" ]]; then
if [[ -z $threshold_end ]]; then
fallback_end=$(cat "$power_supply_path"/BAT*/charge_control_end_threshold 2>/dev/null | head -1)
if [[ $fallback_end =~ ^[0-9]+$ ]] && (( fallback_end > 0 && fallback_end < 100 )); then
threshold_end=$fallback_end
fi
fi
if [[ -z $threshold_start && -n $threshold_end ]]; then
threshold_start=$(cat "$power_supply_path"/BAT*/charge_control_start_threshold 2>/dev/null | head -1)
fi
fi

# Time left is the energy left over the draw shown beside it, so the two always
# agree; charging counts up to the charge limit. UPower's own estimate averages
Expand Down Expand Up @@ -146,6 +180,22 @@ if [[ $ac_online == "true" && -n $threshold_end ]]; then
fi
fi

# Battery health percentage (full vs design capacity, clamped to 1..100%).
compute_health() {
local full=$1 design=$2 cap=$3
awk -v full="$full" -v design="$design" -v cap="$cap" 'BEGIN {
pct = -1
if (full + 0 > 0 && design + 0 > 0) {
pct = (full / design) * 100
} else if (cap + 0 > 0) {
pct = cap + 0
}
if (pct <= 0) exit
if (pct > 100) pct = 100
printf "%d", int(pct + 0.5)
}'
}

if [[ $shell_output == "true" ]]; then
printf 'percentage\t%s\n' "${percentage}%"
if [[ $charge_holding == "true" ]]; then
Expand All @@ -157,6 +207,14 @@ if [[ $shell_output == "true" ]]; then
printf 'size\t%s\n' "${capacity}Wh"
printf 'time\t%s\n' "$time_remaining"

health=$(compute_health "$energy_full" "$energy_full_design" "$upower_capacity")
if [[ -z $health ]]; then
sysfs_full=$(read_sysfs_number "$battery_path/energy_full" || read_sysfs_number "$battery_path/charge_full" || true)
sysfs_design=$(read_sysfs_number "$battery_path/energy_full_design" || read_sysfs_number "$battery_path/charge_full_design" || true)
health=$(compute_health "$sysfs_full" "$sysfs_design" "")
fi
[[ -n $health ]] && printf 'health\t%s\n' "${health}%"

cycles=""
[[ ! -r $battery_path/cycle_count ]] || cycles=$(<"$battery_path/cycle_count")
[[ -n $cycles ]] || cycles=$(cat "$power_supply_path"/BAT*/cycle_count 2>/dev/null | head -1)
Expand All @@ -171,6 +229,74 @@ if [[ $shell_output == "true" ]]; then
fi
fi

# Charge limit control metadata for the power panel.
limit_supported=0
if [[ $upower_threshold_supported == "yes" || -r $battery_path/charge_control_end_threshold ]]; then
limit_supported=1
fi
printf 'limit_supported\t%s\n' "$limit_supported"

if (( limit_supported == 1 )); then
limit_mode="threshold"
if [[ $upower_threshold_supported == "yes" && ! -r $battery_path/charge_control_end_threshold && -z $upower_threshold_end ]]; then
limit_mode="firmware"
fi

limit_state="off"
if [[ $upower_threshold_enabled == "yes" ]] || { [[ -n $threshold_end ]] && (( threshold_end < 100 )); }; then
limit_state="on"
elif [[ $limit_mode == "firmware" ]] && command -v busctl >/dev/null 2>&1; then
if [[ $(busctl get-property org.freedesktop.UPower "$battery" org.freedesktop.UPower.Device ChargeThresholdEnabled 2>/dev/null || true) == "b true" ]]; then
limit_state="on"
elif [[ -f $battery_state_dir/top-up-once ]]; then
limit_state="once"
fi
elif [[ -f $battery_state_dir/top-up-once ]]; then
limit_state="once"
fi

limit_end=$upower_threshold_end
[[ -n $limit_end ]] || limit_end=$threshold_end
if [[ -z $limit_end && $limit_mode == "threshold" ]]; then
limit_end=80
fi

printf 'limit_state\t%s\n' "$limit_state"
printf 'limit_mode\t%s\n' "$limit_mode"
[[ -n $limit_end ]] && printf 'limit_end\t%s\n' "$limit_end"
[[ -n $upower_threshold_start ]] && printf 'limit_start\t%s\n' "$upower_threshold_start"
fi

# Last sleep drain telemetry written by /usr/lib/systemd/system-sleep/omarchy-sleep-stats.
last_sleep_file="$sleep_run_dir/last-sleep"
if [[ -r $last_sleep_file ]]; then
s_mode="" s_dur="" s_ac="0" s_dpct="" s_pct_hr="" s_w="" s_s0ix=""
while IFS=$'\t' read -r key val; do
case "$key" in
mode) s_mode=$val ;;
duration_s) s_dur=$val ;;
ac_plugged) s_ac=$val ;;
delta_pct) s_dpct=$val ;;
drain_pct_hr) s_pct_hr=$val ;;
drain_w) s_w=$val ;;
s0ix_pct) s_s0ix=$val ;;
esac
done <"$last_sleep_file"

if [[ $s_ac == "0" && $s_dur =~ ^[0-9]+$ ]] && (( s_dur >= 300 )); then
s_minutes=$(( (s_dur + 30) / 60 ))
s_formatted=$(format_duration "$s_minutes")
if [[ -n $s_formatted ]]; then
printf 'sleep_duration\t%s\n' "$s_formatted"
[[ -n $s_dpct ]] && printf 'sleep_drain_pct\t%s%%\n' "$s_dpct"
[[ -n $s_pct_hr ]] && printf 'sleep_drain_pct_hr\t%s%%/h\n' "$s_pct_hr"
[[ -n $s_w ]] && printf 'sleep_drain_w\t%sW\n' "$s_w"
[[ -n $s_s0ix ]] && printf 'sleep_s0ix\t%s%%\n' "$s_s0ix"
[[ -n $s_mode ]] && printf 'sleep_mode\t%s\n' "$s_mode"
fi
fi
fi

exit 0
fi

Expand Down
7 changes: 7 additions & 0 deletions bin/omarchy-debug
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ SYSTEM INFORMATION
=========================================
$(inxi -Farz)

=========================================
SLEEP & BATTERY
=========================================
Sleep mode: $(cat /sys/power/mem_sleep 2>/dev/null || echo "unknown")
$(omarchy-battery-status --shell 2>/dev/null || true)
$(if [[ -r /var/lib/omarchy/sleep-history.tsv ]]; then echo "Recent sleeps (ts mode dur_s ac delta_pct delta_wh pct_hr w s0ix):"; tail -n 10 /var/lib/omarchy/sleep-history.tsv; fi)

=========================================
DMESG
=========================================
Expand Down
10 changes: 10 additions & 0 deletions default/i18n/en/shell.json
Original file line number Diff line number Diff line change
Expand Up @@ -574,10 +574,14 @@
"polkit.unlockDrive": "Unlock %1",
"polkit.wrong": "Wrong",
"power.battery": "Battery",
"power.batteryHealth": "Battery health",
"power.batterySize": "Battery size",
"power.batteryState": "Battery state",
"power.cancelTopUpOnce": "Click to cancel one-time top-up",
"power.chargeCycles": "Charge cycles",
"power.chargeLimit": "Charge limit",
"power.chargeLimitFailed": "Could not update charge limit",
"power.chargeLimitSection": "CHARGE LIMIT",
"power.charging": "Charging",
"power.chargingPhrase1": "Pumping power",
"power.chargingPhrase2": "Injecting electrons",
Expand All @@ -600,14 +604,20 @@
"power.dischargingPhrase9": "Munching reserves",
"power.fullyCharged": "Fully charged",
"power.holding": "Holding",
"power.holdingAt": "Holding at %1",
"power.lastSleep": "Last sleep",
"power.onBattery": "On battery",
"power.powerProfile": "POWER PROFILE",
"power.profileBalanced": "Balanced",
"power.profilePerformance": "Performance",
"power.profilePowerSaver": "Power-saver",
"power.shallowSleep": "Last sleep stayed shallow (S0ix %1)",
"power.threshold": "Threshold",
"power.timeLeft": "Time left",
"power.timeToFull": "Time to full",
"power.topUpOnce": "Top up to 100% once",
"power.toppingUpNext": "Next charge goes to 100%",
"power.toppingUpOnce": "Charging to 100% once",
"reminders.invalidBody": "Enter the number of minutes",
"reminders.invalidTitle": "Invalid reminder",
"reminders.messagePrompt": "Reminder message...",
Expand Down
Loading