From 0afa06583a2db35def640a583bf4e132262ec66c Mon Sep 17 00:00:00 2001 From: Lef Date: Tue, 10 Nov 2026 23:02:17 +0200 Subject: [PATCH 1/2] omamobile-keys: put the brightness back only after the panel is off A Power press on the unlocked desktop set the brightness to 0, locked, asked Hyprland to switch the display off, and put the saved brightness back straight away. Hyprland reports the display off at once, but the panel only goes off when the compositor commits it, 0.3 s later on the Pixel 8 Pro, and bl_power is not writable by the user there, so the panel showed the lock screen at full brightness for a moment before it went black. Wait, at most 2 s, until the panel itself reads as off (bl_power, or the internal connector's dpms) before the brightness goes back; Hyprland's answer is used only on a machine with neither file. Test: the stand-in flips Hyprland's state at once and the panel 0.4 s later, and the brightness must come back after that; a panel that never goes off still ends the wait. --- packages/omamobile-shell/bin/omamobile-keys | 39 ++++++++++++- packages/omamobile-shell/test/keys.sh | 64 ++++++++++++++++++++- 2 files changed, 100 insertions(+), 3 deletions(-) diff --git a/packages/omamobile-shell/bin/omamobile-keys b/packages/omamobile-shell/bin/omamobile-keys index 6aece0fd0..964c5c30d 100644 --- a/packages/omamobile-shell/bin/omamobile-keys +++ b/packages/omamobile-shell/bin/omamobile-keys @@ -174,6 +174,42 @@ long_press() { fi } +# omarchy-brightness-display off only asks Hyprland to switch the display off. +# Hyprland reports it off at once (dpmsStatus), but the panel itself goes off +# only when the compositor commits it, 0.3 s later on the Pixel 8 Pro +# (measured: dpmsStatus false at +0.09 s, bl_power 4 and the DRM connector's +# dpms Off at +0.35 s), and where bl_power is not writable by the user the panel +# follows only then. Put the brightness back before that and the panel shows +# the lock screen for an instant at full brightness, then goes black. +# So wait for what the panel reports: the backlight's bl_power, or a connected +# internal connector's dpms; Hyprland's answer only on a machine with neither. +# panel_dark takes the backlight device name (may be empty). +panel_dark() { + local sys=${OMAMOBILE_KEYS_SYSFS:-/sys} device=$1 f source= + if [[ -n $device && -r $sys/class/backlight/$device/bl_power ]]; then + source=1 + [[ $(<"$sys/class/backlight/$device/bl_power") != 0 ]] && return 0 + fi + for f in "$sys"/class/drm/card*-{DSI,eDP,LVDS}-*; do + [[ -r $f/dpms && -r $f/status && $(<"$f/status") == connected ]] || continue + source=1 + [[ $(<"$f/dpms") == Off ]] && return 0 + done + [[ -z $source ]] && screen_off +} + +# At most 2 s by the clock, so a compositor that never answers cannot hold the +# key handler. +wait_screen_off() { + local end + end=$(( ${EPOCHREALTIME/./} + 2000000 )) + while (( ${EPOCHREALTIME/./} < end )); do + panel_dark "$1" && return 0 + sleep 0.05 + done + return 0 +} + short_press() { # Debounce: a second press within 0.8 s of the first, while it was still # locking and powering the panel off, read the screen as on and saved a @@ -220,7 +256,8 @@ short_press() { lock_dark omarchy-brightness-display off # The brightness the next wake lights up with: the saved level, or 40 % when - # the press caught the panel at 0. + # the press caught the panel at 0. Only once the panel is really off. + wait_screen_off "$device" if [[ -n $device ]]; then [[ -n $level ]] && brightnessctl -q -d "$device" set "$level" || brightnessctl -q -d "$device" set 40% fi diff --git a/packages/omamobile-shell/test/keys.sh b/packages/omamobile-shell/test/keys.sh index c13f42c54..68570074b 100755 --- a/packages/omamobile-shell/test/keys.sh +++ b/packages/omamobile-shell/test/keys.sh @@ -26,7 +26,11 @@ cat >"$t/bin/omarchy-shell" <"$t/sys/class/backlight/panel/bl_power"; echo On >"$t/sys/class/drm/card0-DSI-1/dpms"; echo connected >"$t/sys/class/drm/card0-DSI-1/status" +export PATH="$t/bin:$PATH" XDG_RUNTIME_DIR="$t/run" OMAMOBILE_KEYS_SYSFS="$t/sys" keys() { bash "$here/bin/omamobile-keys" "$@"; } fail() { printf 'not ok - %s\n' "$1" >&2; cat "$log" >&2 2>/dev/null; exit 1; } reset() { : >"$log"; rm -rf "$t/run/omamobile-keys"; } @@ -47,7 +51,8 @@ echo "ok - volume keys: camera shutter in front, volume otherwise" # The power key's short press, by lock state and screen state. hyprctl answers # both the window class and the monitors' dpmsStatus; the backlight tools are -# stubs, and bl_power is not consulted (no backlight device on this machine). +# stubs, and with no backlight device name (omarchy-hw-display answers nothing) +# bl_power is not consulted; the flash block below gives the panel a fake one. cat >"$t/bin/hyprctl" <"$t/locked"; echo true >"$t/dpms" press; grep -q "omarchy-system-lock" "$log" || fail "unlocked and lit: a press locks" before "omarchy-system-lock" "omarchy-shell lock dark" && before "omarchy-shell lock dark" "omarchy-brightness-display off" \ || fail "unlocked and lit: the new lock hears it goes dark, before the panel goes off" + +# The flash: Hyprland reports the display off at once (dpmsStatus) but the panel +# goes off only when the compositor commits it, 0.3 s later on the Pixel 8 Pro +# (measured), so the saved brightness must not go back before the panel itself +# reads as off (bl_power 4 or the connector's dpms Off), or the lock screen shows +# at full brightness for an instant. Here the stand-in flips hyprctl's dpmsStatus +# when "omarchy-brightness-display off" is called and the panel 0.4 s later, +# writing "panel-off" to the log when it does; with a real device name, a saved +# level of 2083, and a second case where the panel never goes off. +mkdir -p "$t/orig"; cp "$t"/bin/omarchy-hw-display "$t"/bin/brightnessctl "$t"/bin/omarchy-brightness-display "$t/orig/" +cat >"$t/bin/omarchy-hw-display" <"$t/bin/brightnessctl" <>"$log" +STUB +cat >"$t/bin/omarchy-brightness-display" <>"$log" +if [[ \$1 == off && -f "$t/async-off" ]]; then + echo false >"$t/dpms" + ( sleep 0.4; echo 4 >"$t/sys/class/backlight/panel/bl_power"; echo Off >"$t/sys/class/drm/card0-DSI-1/dpms"; echo "panel-off" >>"$log" ) >/dev/null 2>&1 & +fi +STUB +chmod +x "$t"/bin/* +panel_on() { echo true >"$t/dpms"; mkdir -p "$t/sys/class/backlight/panel" "$t/sys/class/drm/card0-DSI-1"; echo 0 >"$t/sys/class/backlight/panel/bl_power"; echo On >"$t/sys/class/drm/card0-DSI-1/dpms"; echo connected >"$t/sys/class/drm/card0-DSI-1/status"; } +echo false >"$t/locked"; panel_on; : >"$t/async-off" +reset; keys power-down; keys power-up; sleep 1.5 +before "brightnessctl -q -d panel set 0" "omarchy-system-lock" || fail "flash: the panel goes dark by the backlight before the lock is built" +before "panel-off" "brightnessctl -q -d panel set 2083" || fail "flash: the saved brightness goes back only after the panel is off" +before "panel-off" "omamobile-sleepd now" || fail "flash: sleep is asked for after the panel is off" +# The panel never goes off: the key handler still puts the brightness back and +# asks for sleep, after at most about 2 s. +rm -f "$t/async-off"; panel_on +reset; start=$(date +%s%N); keys power-down; keys power-up +for _ in $(seq 60); do grep -q "brightnessctl -q -d panel set 2083" "$log" && break; sleep 0.1; done +took=$(( ($(date +%s%N) - start) / 1000000 )) +grep -q "brightnessctl -q -d panel set 2083" "$log" || fail "flash: with a panel that never goes off the brightness still goes back" +(( took >= 1800 && took < 3500 )) || fail "flash: with a panel that never goes off the wait lasts about 2 s ($took ms)" +# No panel files at all (another machine): Hyprland's answer is all there is. +rm -rf "$t/sys/class/drm/card0-DSI-1" "$t/sys/class/backlight/panel"; : >"$t/async-off" +reset; start=$(date +%s%N); keys power-down; keys power-up +took=$(( ($(date +%s%N) - start) / 1000000 )); sleep 1 +grep -q "brightnessctl -q -d panel set 2083" "$log" || fail "flash: with no panel files the brightness goes back by Hyprland's word" +(( took < 1500 )) || fail "flash: with no panel files Hyprland's word ends the wait at once, not at the bound ($took ms)" +# A connector that is not connected (a second, unused one) says Off and must not end the wait. +panel_on; mkdir -p "$t/sys/class/drm/card0-DSI-2"; echo Off >"$t/sys/class/drm/card0-DSI-2/dpms"; echo disconnected >"$t/sys/class/drm/card0-DSI-2/status"; : >"$t/async-off" +reset; keys power-down; keys power-up; sleep 1 +before "panel-off" "brightnessctl -q -d panel set 2083" || fail "flash: an unconnected connector's Off does not end the wait" +rm -rf "$t/sys/class/drm/card0-DSI-2"; panel_on +cp "$t"/orig/omarchy-hw-display "$t"/orig/brightnessctl "$t"/orig/omarchy-brightness-display "$t/bin/" +echo "ok - power press unlocked and lit: the brightness comes back only after the panel is off (and still after 2 s if it never is)" echo false >"$t/locked"; echo false >"$t/dpms" press; grep -q "omarchy-brightness-display on" "$log" || fail "unlocked and dark: a press lights the screen" grep -q "omarchy-system-lock" "$log" && fail "unlocked and dark: a press does not lock" From 03b693912455b000fb6d3d5fbb3e5fb23d533927 Mon Sep 17 00:00:00 2001 From: Lef Date: Wed, 11 Nov 2026 07:19:01 +0200 Subject: [PATCH 2/2] omamobile-keys: bound the panel wait by date, not by the locale's clock The 2 s limit read bash's microsecond clock variable, which is written with the locale's decimal separator; under a comma locale the arithmetic turned into the comma operator and the limit became random, with an error in the journal. Use now (milliseconds from date) like the rest of the script, and shorten the limit to 1 s: the panel took 0.35 s on the Pixel 8 Pro. The test also checks that the script no longer reads that variable. --- packages/omamobile-shell/bin/omamobile-keys | 10 ++++++---- packages/omamobile-shell/test/keys.sh | 8 +++++--- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/omamobile-shell/bin/omamobile-keys b/packages/omamobile-shell/bin/omamobile-keys index 964c5c30d..1a6baf9dc 100644 --- a/packages/omamobile-shell/bin/omamobile-keys +++ b/packages/omamobile-shell/bin/omamobile-keys @@ -198,12 +198,14 @@ panel_dark() { [[ -z $source ]] && screen_off } -# At most 2 s by the clock, so a compositor that never answers cannot hold the -# key handler. +# At most 1 s by the clock (the panel took 0.35 s on the Pixel 8 Pro), so a +# compositor that never answers cannot hold the key handler. Milliseconds from +# `now`, not bash's microsecond clock variable: that one is written with the +# locale's decimal separator, and under a comma locale the arithmetic breaks. wait_screen_off() { local end - end=$(( ${EPOCHREALTIME/./} + 2000000 )) - while (( ${EPOCHREALTIME/./} < end )); do + end=$(( $(now) + 1000 )) + while (( $(now) < end )); do panel_dark "$1" && return 0 sleep 0.05 done diff --git a/packages/omamobile-shell/test/keys.sh b/packages/omamobile-shell/test/keys.sh index 68570074b..8ff39f522 100755 --- a/packages/omamobile-shell/test/keys.sh +++ b/packages/omamobile-shell/test/keys.sh @@ -128,13 +128,15 @@ before "brightnessctl -q -d panel set 0" "omarchy-system-lock" || fail "flash: t before "panel-off" "brightnessctl -q -d panel set 2083" || fail "flash: the saved brightness goes back only after the panel is off" before "panel-off" "omamobile-sleepd now" || fail "flash: sleep is asked for after the panel is off" # The panel never goes off: the key handler still puts the brightness back and -# asks for sleep, after at most about 2 s. +# asks for sleep, after at most about 1 s. rm -f "$t/async-off"; panel_on reset; start=$(date +%s%N); keys power-down; keys power-up for _ in $(seq 60); do grep -q "brightnessctl -q -d panel set 2083" "$log" && break; sleep 0.1; done +# The limit is read from `date`, never from EPOCHREALTIME (a comma locale writes it with a comma). +grep -q EPOCHREALTIME "$here/bin/omamobile-keys" && fail "flash: the wait's clock doesn't depend on the locale" took=$(( ($(date +%s%N) - start) / 1000000 )) grep -q "brightnessctl -q -d panel set 2083" "$log" || fail "flash: with a panel that never goes off the brightness still goes back" -(( took >= 1800 && took < 3500 )) || fail "flash: with a panel that never goes off the wait lasts about 2 s ($took ms)" +(( took >= 900 && took < 2500 )) || fail "flash: with a panel that never goes off the wait lasts about 1 s ($took ms)" # No panel files at all (another machine): Hyprland's answer is all there is. rm -rf "$t/sys/class/drm/card0-DSI-1" "$t/sys/class/backlight/panel"; : >"$t/async-off" reset; start=$(date +%s%N); keys power-down; keys power-up @@ -147,7 +149,7 @@ reset; keys power-down; keys power-up; sleep 1 before "panel-off" "brightnessctl -q -d panel set 2083" || fail "flash: an unconnected connector's Off does not end the wait" rm -rf "$t/sys/class/drm/card0-DSI-2"; panel_on cp "$t"/orig/omarchy-hw-display "$t"/orig/brightnessctl "$t"/orig/omarchy-brightness-display "$t/bin/" -echo "ok - power press unlocked and lit: the brightness comes back only after the panel is off (and still after 2 s if it never is)" +echo "ok - power press unlocked and lit: the brightness comes back only after the panel is off (and still after 1 s if it never is)" echo false >"$t/locked"; echo false >"$t/dpms" press; grep -q "omarchy-brightness-display on" "$log" || fail "unlocked and dark: a press lights the screen" grep -q "omarchy-system-lock" "$log" && fail "unlocked and dark: a press does not lock"