Files
bananarchy/overlay/rootfs/usr/bin/sxmo_state.sh
T
Luxferre 2857ef4374 Make 2720 Flip lid switching instant and route closed-flip events to the cover
The lid daemon re-derived "observed" from a stale EV_SW mask on every 100 ms
iteration and only read the raw hall lines every 200 ms, so the candidate
oscillated and the 80 ms debounce almost never completed. A controlled hall
transition on the live phone took over 2.2 s to blank and lock the main panel.
The hall reading is now sticky, every EV_SW edge forces an immediate hall
re-read, the loop and debounce are 20 ms, and the daemon writes the lid state,
lock flag, Sxmo state and both backlights synchronously in C before spawning
hooks. A direct xset DPMS off/on stops or resumes Xorg's SPI flushes at once.
bananarchy-lid-state.sh guards the asynchronous hooks by lid state and a
transition generation counter, so a late screenoff cannot re-lock a flip that
was just opened.

bananarchy-lid-gate.sh now gates sxmo_wm.sh display on, the screen-wake
helper, the backlight udev helper, sxmo_state.sh set unlock (dwm calls it
directly) and both input handler copies. While the flip is closed it blocks
the main panel and keypad backlight, forces brightness/kbd/DPMS off against
stray writes, and forwards a WAKE datagram to the cover display. Ring,
notification and call-state hooks publish to the cover, and the cover daemon
gained WAKE, event wakes, immunity to repeated LID CLOSED rebroadcasts and a
bounded retry for a DPMS-deferred draw.

Live verification with controlled hall transitions and uinput switch/key
injection: close blanks and locks in ~45-90 ms (hall-poll fallback
~100-220 ms), open restores brightness, keypad backlight, unlock and DPMS in
~10-130 ms with no stale-hook re-lock. All three external keys and an
incoming call leave brightness=0, kbd_backlight=0 and DPMS=Off while the
cover reports backend=spidev visible=1; opening returns mux=open.

Rebuilt the a1108/dejavu nokia-beatles image and pinned the verified hash
0f33aa6e9fc70022574b453aaafb9307c4d185431d3be272782c8403dd0fb291 in
check-rebuild.sh. e2fsck -fn passed on both partitions, debugfs confirms
twelve updated rootfs files byte-for-byte, and the bootfs sync, boot image
check, artifact sync, host tests and rebuild preflight all pass.

This is an intermediate checkpoint: eyes-on cover illumination and the
remaining polarity/CS fixes are still required.
2026-11-10 20:23:06 +02:00

191 lines
3.8 KiB
Bash
Executable File

#!/bin/sh
# include common definitions
# shellcheck source=scripts/core/sxmo_common.sh
. sxmo_common.sh
SXMO_STATE="${SXMO_STATE:-$XDG_RUNTIME_DIR/sxmo.state}"
if [ -n "$SXMO_HAS_PEANUTBUTTER" ] && command -v peanutbutter > /dev/null && [ -n "$WAYLAND_DISPLAY" ]; then
SXMO_STATES="${SXMO_STATES:-unlock screenoff}"
elif [ -z "$WAYLAND_DISPLAY" ] && command -v smlock > /dev/null; then
SXMO_STATES="${SXMO_STATES:-unlock screenoff}"
else
SXMO_STATES="${SXMO_STATES:-unlock lock screenoff}"
fi
SXMO_SUSPENDABLE_STATES="${SXMO_SUSPENDABLE_STATES:-screenoff 3}"
transition_can_suspend() {
sxmo_jobs.sh stop idle_suspender 2>/dev/null || true
# U2: no infinite wakelock. Deep standby is guarded by
# /etc/bananarchy-no-deep-standby and the blocker list instead.
:
}
transition() {
state="$1"
if [ "$state" = "unlock" ] && [ -f /tmp/bananarchy_screen_locked ]; then
state="lock"
printf '%s\n' "$state" > "$SXMO_STATE"
sxmo_wm.sh display on >/dev/null 2>&1 &
xlocker wake >/dev/null 2>&1 &
return 0
fi
if [ "$state" = "lock" ]; then
printf '1\n' > /tmp/bananarchy_screen_locked
xlocker >/dev/null 2>&1 &
elif [ "$state" = "screenoff" ]; then
printf '1\n' > /tmp/bananarchy_screen_locked
xlocker screenoff >/dev/null 2>&1 &
fi
sxmo_log "transitioning to stage $state"
printf '%s\n' "$state" > "$SXMO_STATE"
lock_shared
(
# We need a subshell so we can close the lock fd, without
# releasing the lock
exec 3<&-
sxmo_hook_"$state".sh &
sxmo_hook_statusbar.sh state_change &
transition_can_suspend &
wait
)
}
click() {
count="${1:-1}"
# shellcheck disable=SC2086
set -- $SXMO_STATES
i=1
while [ $i -le $# ]; do
tmpstate=
prevstate=
eval "tmpstate=\$$i"
if [ "$tmpstate" = "$state" ]; then
if [ $i = 1 ]; then
eval "prevstate=\$$#"
else
eval "prevstate=\$$((i-1))"
fi
state="$prevstate"
break
fi
i=$((i+1))
done
if [ "$count" -gt 1 ]; then
click $((count-1))
else
transition "$state"
fi
flushstored
}
idle() {
sxmo_jobs.sh stop idle_locker 2>/dev/null || true
sxmo_jobs.sh stop idle_suspender 2>/dev/null || true
sxmo_log "idle: automatic idle locking is disabled"
}
store() {
storeid="$(tr -dc 'a-zA-Z0-9' < /dev/urandom 2>/dev/null | head -c 10)"
printf '%s\n' "$state" > "$SXMO_STATE.stored.$storeid"
printf %s "$storeid"
}
flushstored() {
rm -f "$SXMO_STATE.stored."* 2>/dev/null || true
}
restore() {
storeid="$1"
if [ -f "$SXMO_STATE.stored.$storeid" ]; then
state=""
read -r state < "$SXMO_STATE.stored.$storeid" 2>/dev/null || true
transition "$state"
flushstored
fi
}
read_state() {
state=""
if [ -r "$SXMO_STATE" ]; then
read -r state < "$SXMO_STATE" 2>/dev/null || true
fi
}
action="$1"
shift
case "$action" in
get)
read_state
if [ -f /tmp/bananarchy_screen_locked ] && [ "$state" = "unlock" ]; then
state="lock"
fi
printf %s "$state"
exit 0
;;
is_locked)
if [ -f /tmp/bananarchy_screen_locked ]; then
exit 0
fi
if [ -n "$SXMO_HAS_PEANUTBUTTER" ]; then
pidof peanutbutter swaylock waylock i3lock > /dev/null && exit 0
fi
read_state
if [ "$state" = "unlock" ]; then
exit 1
fi
exit 0
;;
esac
exec 3<> "$SXMO_STATE.lock"
lock_exclusive() {
flock -x 3
}
lock_shared() {
flock -s 3
}
case "$action" in
click|idle)
lock_exclusive
read_state
"$action" "$@" ;;
set)
# dwm calls "sxmo_state.sh set unlock" directly on every key press.
# A closed flip must stay locked: forward the request to the cover.
if [ "${1:-}" = "unlock" ] && \
! "${BANANARCHY_LID_GATE:-/usr/bin/bananarchy-lid-gate.sh}" wake; then
exit 0
fi
lock_exclusive
read_state
case " $SXMO_STATES " in
*" $1 "*)
transition "$1"
;;
esac
;;
store)
lock_exclusive
read_state
store
;;
restore)
lock_exclusive
restore "$1"
;;
flushstored)
lock_exclusive
flushstored
;;
esac