diff --git a/bin/omarchy-powerprofiles-set b/bin/omarchy-powerprofiles-set index 86c3ac0cea..2cbfda3d53 100755 --- a/bin/omarchy-powerprofiles-set +++ b/bin/omarchy-powerprofiles-set @@ -1,13 +1,36 @@ #!/bin/bash -# Set the power profile to the requested level, falling back to balanced -# if the requested profile isn't available on this machine. +# Set the power profile based on AC/battery state, falling back to balanced +# if the desired profile isn't available on this machine. # -# Usage: omarchy-powerprofiles-set +# Usage: +# omarchy-powerprofiles-set # auto-detect from power-supply state +# omarchy-powerprofiles-set ac +# omarchy-powerprofiles-set battery + +action="${1-}" + +# 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 ]]; then + 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 + action=ac + break + fi + done +fi mapfile -t profiles < <(powerprofilesctl list | awk '/^\s*[* ]\s*[a-zA-Z0-9\-]+:$/ { gsub(/^[*[:space:]]+|:$/,""); print }') -case "$1" in +case "$action" in ac) # Prefer performance, fall back to balanced if [[ " ${profiles[*]} " == *" performance "* ]]; then diff --git a/install/config/powerprofilesctl-rules.sh b/install/config/powerprofilesctl-rules.sh index 9b5a71a637..60ea436520 100644 --- a/install/config/powerprofilesctl-rules.sh +++ b/install/config/powerprofilesctl-rules.sh @@ -1,9 +1,13 @@ if omarchy-battery-present; then cat </dev/null sudo udevadm trigger --subsystem-match=power_supply 2>/dev/null fi diff --git a/migrations/1777098818.sh b/migrations/1777098818.sh new file mode 100644 index 0000000000..a9de7e1b86 --- /dev/null +++ b/migrations/1777098818.sh @@ -0,0 +1,3 @@ +echo "Fix power profile auto-switching on USB-C only machines and ensure power-profiles-daemon is enabled" + +source "$OMARCHY_PATH/install/config/powerprofilesctl-rules.sh"