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
25 changes: 25 additions & 0 deletions bin/omarchy-system-wake
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,31 @@
# omarchy:name=wake
# omarchy:examples=omarchy system wake

# On dual-GPU Macs, gmux resets the panel backlight (typically to 80%) when the
# panel powers down after suspend, leaving the kernel's requested brightness
# unchanged because apple-gmux is not tied to the GPU's DPMS power sequence.
# Re-apply the requested level on wake when the hardware level has diverged.
restore_gmux_backlight() {
local dir="${OMARCHY_BACKLIGHT_PATH:-/sys/class/backlight}/gmux_backlight"
local requested actual lock_fd

[[ -r $dir/brightness && -r $dir/actual_brightness ]] || return 0
read -r requested <"$dir/brightness" || return 0
read -r actual <"$dir/actual_brightness" || return 0
[[ $requested =~ ^[0-9]+$ && $actual =~ ^[0-9]+$ ]] || return 0
(( requested == actual )) && return 0

# Coordinate with omarchy-brightness-display and use a +0 delta so
# brightnessctl re-reads the current requested level instead of writing a
# stale value if a brightness key ran concurrently.
exec {lock_fd}>"${XDG_RUNTIME_DIR:-/tmp}/omarchy-brightness-display.lock"
if flock "$lock_fd"; then
brightnessctl -q -d gmux_backlight set +0 >/dev/null 2>&1 || true
fi
exec {lock_fd}>&-
}

omarchy-brightness-display on
restore_gmux_backlight
omarchy-brightness-keyboard restore
omarchy-hyprland-monitor-clamshell >/dev/null 2>&1 || true
53 changes: 53 additions & 0 deletions test/shell.d/brightness-display-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,56 @@ for conf in "" "ddc-require-connector-ddc extra" "ddc-future"; do
done
rm -f "$platform_root/displays.conf"
pass "every external monitor is probed without the directive"

# Wake restores gmux_backlight only when hardware has diverged from the
# kernel's requested level (e.g. post-suspend 80% reset on dual-GPU Macs).
wake_bin="$test_tmp/wake-bin"
mkdir -p "$wake_bin" "$test_tmp/backlight/gmux_backlight"
for helper in omarchy-brightness-display omarchy-brightness-keyboard omarchy-hyprland-monitor-clamshell; do
printf '#!/bin/bash\nexit 0\n' >"$wake_bin/$helper"
done
chmod +x "$wake_bin"/*

run_wake() {
CALL_LOG="$call_log" XDG_RUNTIME_DIR="$runtime_dir" \
OMARCHY_BACKLIGHT_PATH="$test_tmp/backlight" \
PATH="$wake_bin:$mock_bin:$PATH" "$ROOT/bin/omarchy-system-wake"
}

printf '9830\n' >"$test_tmp/backlight/gmux_backlight/brightness"
printf '52428\n' >"$test_tmp/backlight/gmux_backlight/actual_brightness"
: >"$call_log"
run_wake
grep -Fx 'brightnessctl -q -d gmux_backlight set +0' "$call_log" >/dev/null ||
fail "wake re-applies requested gmux_backlight level after hardware reset" "$(<"$call_log")"

: >"$call_log"
(
exec {held_fd}>"$runtime_dir/omarchy-brightness-display.lock"
flock "$held_fd"
touch "$test_tmp/wake-lock-held"
sleep 0.2
touch "$test_tmp/wake-lock-released"
) &
lock_holder=$!
while [[ ! -e $test_tmp/wake-lock-held ]]; do sleep 0.01; done
run_wake
waited_for_lock=0
[[ -e $test_tmp/wake-lock-released ]] && waited_for_lock=1
wait "$lock_holder"
(( waited_for_lock )) || fail "wake waits for an external brightness change to finish"
grep -Fx 'brightnessctl -q -d gmux_backlight set +0' "$call_log" >/dev/null ||
fail "wake restores gmux_backlight after an external brightness change releases the lock" "$(<"$call_log")"

printf '9830\n' >"$test_tmp/backlight/gmux_backlight/actual_brightness"
: >"$call_log"
run_wake
[[ ! -s $call_log ]] ||
fail "wake skips gmux_backlight write when hardware already matches" "$(<"$call_log")"

rm -rf "$test_tmp/backlight/gmux_backlight"
: >"$call_log"
run_wake
[[ ! -s $call_log ]] ||
fail "wake does nothing for backlight on machines without gmux_backlight" "$(<"$call_log")"
pass "wake restores gmux_backlight only when hardware diverges"