Remember power profile choices per power source across reboots
Explicit profile selections from the power panel or menu are now saved under an ac/battery key in ~/.local/state/omarchy/powerprofiles and reapplied on boot and on plug/unplug. Only successful, user-made selections are persisted, so the performance/balanced defaults still apply when nothing has been chosen. All entry points key off the same power signal, UPower's OnBattery: the shell reads it natively and omarchy-powerprofiles-set autodetect queries it via busctl. This replaces the sysfs online checks, which disagreed with the shell on desktops without power_supply entries and lagged behind plug events on some USB-C laptops. Plug/unplug switching moves from the udev rule into the shell's battery service, which already receives UPower's debounced state changes, so the udev rule and its settle-sleep workaround are gone. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
6ba0e9c234
commit
0f2f3f11cb
@@ -1,45 +1,63 @@
|
||||
#!/bin/bash
|
||||
|
||||
# omarchy:summary=Set the power profile to the requested level, falling back to balanced
|
||||
# omarchy:args=[autodetect|ac|battery]
|
||||
# omarchy:summary=Set and remember the power profile for AC or battery use
|
||||
# omarchy:args=[autodetect|ac|battery] [power-saver|balanced|performance]
|
||||
|
||||
action="${1-}"
|
||||
action="${1:-autodetect}"
|
||||
requested_profile="${2:-}"
|
||||
|
||||
# Auto-detect when called with no argument: treat any Mains or USB
|
||||
# power-supply device reporting online=1 as "on AC". This handles
|
||||
# USB-C only laptops where the legacy AC device may not fire udev
|
||||
# events, and also avoids false negatives from per-port USB-C devices
|
||||
# that are present-but-empty (online=0) while another port supplies power.
|
||||
if [[ -z $action || $action == "autodetect" ]]; then
|
||||
# On plug/unplug, udev fires the rule before sysfs `online` is updated
|
||||
# on some laptops (notably Lenovo Yoga Pro 7 with USB-C charging). A
|
||||
# short settle delay lets the kernel update before we read state.
|
||||
sleep 0.3
|
||||
usage() {
|
||||
echo "Usage: omarchy-powerprofiles-set [autodetect|ac|battery] [power-saver|balanced|performance]" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
action=battery
|
||||
for ps in /sys/class/power_supply/*; do
|
||||
[[ -r $ps/online && -r $ps/type ]] || continue
|
||||
type=$(cat "$ps/type")
|
||||
[[ $type == "Mains" || $type == "USB" ]] || continue
|
||||
if [[ $(cat "$ps/online") == "1" ]]; then
|
||||
(( $# <= 2 )) || usage
|
||||
|
||||
case "$action" in
|
||||
autodetect)
|
||||
# Use the same signal as the shell (UPower.onBattery) so every entry point
|
||||
# saves and restores the profile under the same ac/battery key.
|
||||
if [[ $(busctl get-property org.freedesktop.UPower /org/freedesktop/UPower org.freedesktop.UPower OnBattery 2>/dev/null) == "b true" ]]; then
|
||||
action=battery
|
||||
else
|
||||
action=ac
|
||||
break
|
||||
fi
|
||||
done
|
||||
fi
|
||||
;;
|
||||
ac | battery) ;;
|
||||
*) usage ;;
|
||||
esac
|
||||
|
||||
mapfile -t profiles < <(powerprofilesctl list | awk '/^\s*[* ]\s*[a-zA-Z0-9\-]+:$/ { gsub(/^[*[:space:]]+|:$/,""); print }')
|
||||
|
||||
case "$action" in
|
||||
ac)
|
||||
# Prefer performance, fall back to balanced
|
||||
if [[ " ${profiles[*]} " == *" performance "* ]]; then
|
||||
powerprofilesctl set performance
|
||||
else
|
||||
powerprofilesctl set balanced
|
||||
fi
|
||||
;;
|
||||
battery)
|
||||
powerprofilesctl set balanced
|
||||
;;
|
||||
esac
|
||||
profile_available() {
|
||||
[[ " ${profiles[*]} " == *" $1 "* ]]
|
||||
}
|
||||
|
||||
state_dir="${OMARCHY_POWERPROFILES_STATE_DIR:-${XDG_STATE_HOME:-$HOME/.local/state}/omarchy/powerprofiles}"
|
||||
state_file="$state_dir/$action"
|
||||
|
||||
if [[ -n $requested_profile ]]; then
|
||||
profile_available "$requested_profile" || {
|
||||
echo "Power profile is not available: $requested_profile" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
profile="$requested_profile"
|
||||
elif [[ -r $state_file ]]; then
|
||||
profile=$(<"$state_file")
|
||||
fi
|
||||
|
||||
if [[ -z ${profile:-} ]] || ! profile_available "$profile"; then
|
||||
if [[ $action == "ac" ]] && profile_available performance; then
|
||||
profile=performance
|
||||
else
|
||||
profile=balanced
|
||||
fi
|
||||
fi
|
||||
|
||||
powerprofilesctl set "$profile" || exit
|
||||
|
||||
if [[ -n $requested_profile ]]; then
|
||||
mkdir -p "$state_dir"
|
||||
printf '%s\n' "$requested_profile" >"$state_file"
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user