Skip to content
Open
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
31 changes: 27 additions & 4 deletions bin/omarchy-powerprofiles-set
Original file line number Diff line number Diff line change
@@ -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 <ac|battery>
# 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
Expand Down
8 changes: 6 additions & 2 deletions install/config/powerprofilesctl-rules.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
if omarchy-battery-present; then
cat <<EOF | sudo tee "/etc/udev/rules.d/99-power-profile.rules"
SUBSYSTEM=="power_supply", ATTR{type}=="Mains", ATTR{online}=="0", RUN+="/usr/bin/systemd-run --no-block --collect --unit=omarchy-power-profile-battery --property=After=power-profiles-daemon.service $HOME/.local/share/omarchy/bin/omarchy-powerprofiles-set battery"
SUBSYSTEM=="power_supply", ATTR{type}=="Mains", ATTR{online}=="1", RUN+="/usr/bin/systemd-run --no-block --collect --unit=omarchy-power-profile-ac --property=After=power-profiles-daemon.service $HOME/.local/share/omarchy/bin/omarchy-powerprofiles-set ac"
SUBSYSTEM=="power_supply", ATTR{type}=="Mains", RUN+="/usr/bin/systemd-run --no-block --collect --unit=omarchy-power-profile-%E{SEQNUM} --property=After=power-profiles-daemon.service $HOME/.local/share/omarchy/bin/omarchy-powerprofiles-set"
SUBSYSTEM=="power_supply", ATTR{type}=="USB", RUN+="/usr/bin/systemd-run --no-block --collect --unit=omarchy-power-profile-%E{SEQNUM} --property=After=power-profiles-daemon.service $HOME/.local/share/omarchy/bin/omarchy-powerprofiles-set"
EOF

# Ensure the daemon is running and starts on boot. The arch package preset
# leaves it disabled, which would silently break profile switching.
sudo systemctl enable --now power-profiles-daemon

sudo udevadm control --reload 2>/dev/null
sudo udevadm trigger --subsystem-match=power_supply 2>/dev/null
fi
3 changes: 3 additions & 0 deletions migrations/1777098818.sh
Original file line number Diff line number Diff line change
@@ -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"