diff --git a/packages/omamobile-shell/bin/omamobile-keys b/packages/omamobile-shell/bin/omamobile-keys index 861436235..6aece0fd0 100644 --- a/packages/omamobile-shell/bin/omamobile-keys +++ b/packages/omamobile-shell/bin/omamobile-keys @@ -180,7 +180,7 @@ short_press() { # brightness of 0, which left the phone dark on the next wake. A press a # few seconds later must still count: it is the wake. local now last - now=$(date +%s%3N); last=$(cat "$state/last-press" 2>/dev/null || echo 0) + now=$1; last=$(cat "$state/last-press" 2>/dev/null || echo 0) echo "$now" >"$state/last-press" (( now - last < 800 )) && return 0 # The press that woke the phone from sleep, if it gets here at all: @@ -312,7 +312,17 @@ case "${1:-}" in elif [[ -f $state/long ]]; then rm -f "$state/long" else - short_press + # Let a second press observe the finished off transition before it + # decides whether to wake. Keep the lock out of background helpers. + released_at=$(now) + exec {short_lock}>"$state/short.lock" + # A stuck action must not queue every later press forever. Drop this + # press after five seconds rather than overlap display transitions. + if ! flock -w 5 "$short_lock"; then + echo 'omamobile-keys: short-press lock wait failed; dropping press' >&2 + exit 0 + fi + ( short_press "$released_at" ) {short_lock}>&- fi ;; wake) diff --git a/packages/omamobile-shell/test/all b/packages/omamobile-shell/test/all index 6746e3fc7..bac547012 100755 --- a/packages/omamobile-shell/test/all +++ b/packages/omamobile-shell/test/all @@ -1166,3 +1166,4 @@ elif [[ $rc != 0 ]]; then else pass "Back threshold feedback: once per gesture, both edges and retreat" fi +bash test/power-overlap.sh || fail "overlapping power-key presses" diff --git a/packages/omamobile-shell/test/power-overlap.sh b/packages/omamobile-shell/test/power-overlap.sh new file mode 100644 index 000000000..1b24a40da --- /dev/null +++ b/packages/omamobile-shell/test/power-overlap.sh @@ -0,0 +1,100 @@ +#!/bin/bash +# SPDX-License-Identifier: MIT +# Two presses during a slow lock must leave the phone awake at its old brightness. +set -euo pipefail +here=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd) +t=$(mktemp -d); trap 'rm -rf "$t"' EXIT +mkdir -p "$t/bin" "$t/run" +export XDG_RUNTIME_DIR="$t/run" T="$t" +echo 512 >"$t/brightness"; echo true >"$t/dpms" +cat >"$t/bin/hyprctl" <<'STUB' +#!/bin/bash +printf '[{"dpmsStatus":%s}]\n' "$(cat "$T/dpms")" +STUB +cat >"$t/bin/omarchy-shell" <<'STUB' +#!/bin/bash +[[ $2 == isLocked ]] && echo false +exit 0 +STUB +cat >"$t/bin/omarchy-hw-display" <<'STUB' +#!/bin/bash +echo fake-panel +STUB +cat >"$t/bin/brightnessctl" <<'STUB' +#!/bin/bash +if [[ $* == *get* ]]; then cat "$T/brightness"; else echo "${@: -1}" >"$T/brightness"; fi +STUB +cat >"$t/bin/omarchy-brightness-display" <<'STUB' +#!/bin/bash +case $1 in off) echo false >"$T/dpms";; on) echo true >"$T/dpms";; esac +STUB +cat >"$t/bin/omarchy-system-lock" <<'STUB' +#!/bin/bash +for fd in /proc/$$/fd/*; do + [[ $(readlink "$fd" 2>/dev/null) == "$T/run/omamobile-keys/short.lock" ]] && touch "$T/inherited-lock" +done +touch "$T/locking" +sleep "${LOCK_DELAY:-1.5}" +STUB +cat >"$t/bin/pgrep" <<'STUB' +#!/bin/bash +exit 1 +STUB +chmod +x "$t"/bin/* +# Only these host tools are reachable; unstubbed desktop commands must fail. +for tool in bash cat readlink touch sleep mkdir rm seq grep jq flock date stat timeout; do + ln -s "$(command -v "$tool")" "$t/bin/$tool" +done +export PATH="$t/bin" +keys() { bash "$here/bin/omamobile-keys" "$@"; } +run_case() { + local delay=$1 expected=$2 first second + rm -rf "$t/run/omamobile-keys" + echo 512 >"$t/brightness"; echo true >"$t/dpms" + keys power-down; sleep 0.1; keys power-up & first=$! + sleep "$delay"; keys power-down; sleep 0.1; keys power-up & second=$! + wait "$first"; wait "$second" + [[ ! -e $t/inherited-lock ]] || { echo 'not ok - helper inherited the power-key lock' >&2; exit 1; } + [[ $(cat "$t/dpms") == "$expected" && $(cat "$t/brightness") == 512 ]] || { + echo "not ok - delay $delay produced wrong screen state or brightness" >&2 + exit 1 + } +} +run_case 1 true +run_case 0.2 false +echo 'ok - overlapping presses wake after one second and debounce at 0.2 seconds' + +# A hung lock helper must not queue later presses indefinitely. The second +# press is dropped after five seconds without changing debounce or display. +rm -rf "$t/run/omamobile-keys" +rm -f "$t/locking" +echo 512 >"$t/brightness"; echo true >"$t/dpms" +export LOCK_DELAY=8 +keys power-down; sleep 0.1; keys power-up & first=$! +for _ in {1..100}; do + [[ -e $t/locking ]] && break + sleep 0.05 +done +[[ -e $t/locking ]] || { echo 'not ok - slow action never started' >&2; exit 1; } +last=$(cat "$t/run/omamobile-keys/last-press") +keys power-down; sleep 0.1 +start=$(date +%s%3N) +rc=0 +timeout 7 bash "$here/bin/omamobile-keys" power-up 2>"$t/timeout-log" || rc=$? +elapsed=$(( $(date +%s%3N) - start )) +if [[ $rc != 0 || $elapsed -lt 4500 || $elapsed -ge 7000 ]] || + ! grep -q 'short-press lock wait failed; dropping press' "$t/timeout-log" || + [[ $(cat "$t/run/omamobile-keys/last-press") != "$last" || + $(cat "$t/dpms") != true || $(cat "$t/brightness") != 0 ]]; then + wait "$first" + echo 'not ok - blocked press did not time out without acting' >&2 + exit 1 +fi +wait "$first" +unset LOCK_DELAY +# Once the slow action releases the lock, a fresh press still wakes normally. +keys power-down; sleep 0.1; keys power-up +[[ $(cat "$t/dpms") == true && $(cat "$t/brightness") == 512 ]] || { + echo 'not ok - power key failed to recover after lock release' >&2; exit 1; +} +echo 'ok - blocked press is dropped after five seconds; next press works after release' diff --git a/packages/omamobile-shell/version b/packages/omamobile-shell/version index f1245817e..71b776cdf 100644 --- a/packages/omamobile-shell/version +++ b/packages/omamobile-shell/version @@ -1 +1 @@ -0.2.111 +0.2.112