From 62f69808525bc621cecde566509fe841edb5282c Mon Sep 17 00:00:00 2001 From: Mikko Nyman Date: Sat, 2 May 2026 12:47:02 +0300 Subject: [PATCH] Fix power profile auto-switching on USB-C only machines (#5444) * Fix power profile auto-switching on USB-C only machines * Small nits * Stick with single unit name --------- Co-authored-by: David Heinemeier Hansson --- bin/omarchy-powerprofiles-set | 26 +++++++++++++++++++++--- install/config/powerprofilesctl-rules.sh | 8 ++++++-- migrations/1777098818.sh | 3 +++ 3 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 migrations/1777098818.sh diff --git a/bin/omarchy-powerprofiles-set b/bin/omarchy-powerprofiles-set index a137c4d4..a7dd623d 100755 --- a/bin/omarchy-powerprofiles-set +++ b/bin/omarchy-powerprofiles-set @@ -1,11 +1,31 @@ #!/bin/bash # omarchy:summary=Set the power profile to the requested level, falling back to balanced -# omarchy:args= +# omarchy:args=[autodetect|ac|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 || $action == "autodetect" ]]; 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 @@ -17,4 +37,4 @@ case "$1" in battery) powerprofilesctl set balanced ;; -esac +esac \ No newline at end of file diff --git a/install/config/powerprofilesctl-rules.sh b/install/config/powerprofilesctl-rules.sh index 9b5a71a6..01fa5da6 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 00000000..a9de7e1b --- /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"