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.
This commit is contained in:
Luxferre committed 2026-11-10 20:23:06 +02:00
1 parent 40a840b49b
commit 2857ef4374
29 files changed
+567 -49

No files matched your search

+156
View File
@@ -0,0 +1,156 @@
#!/usr/bin/env python3
"""Host test for the 2720 Flip instant lid handling and closed-lid gate.
Checks the compiled lid daemon's event-driven structure, that every main-panel
wake path is gated on /run/bananarchy/lid-state, and that the gate forwards a
blocked wake to the cover display instead of the main panel.
"""
import os
import pathlib
import subprocess
import sys
import tempfile
BASE = pathlib.Path(__file__).resolve().parent.parent
LID_SRC = BASE / "src/bananarchy-daemons/bananarchy-lid.c"
COVER_SRC = BASE / "src/bananarchy-daemons/bananarchy-cover-idle.c"
GATE = BASE / "overlay/rootfs/usr/bin/bananarchy-lid-gate.sh"
LID_WRAPPER = BASE / "overlay/rootfs/usr/bin/bananarchy-lid-state.sh"
WM = BASE / "overlay/rootfs/usr/bin/sxmo_wm.sh"
STATE_SH = BASE / "overlay/rootfs/usr/bin/sxmo_state.sh"
WAKE = BASE / "overlay/rootfs/usr/bin/bananarchy-screen-wake.sh"
DPMS_SYNC = BASE / "overlay/rootfs/usr/bin/bananarchy-display-power-sync"
INPUT_HOOKS = (
BASE / "overlay/rootfs/usr/share/sxmo/default_hooks/sxmo_hook_inputhandler.sh",
BASE / "overlay/rootfs/etc/skel/.config/sxmo/hooks/sxmo_hook_inputhandler.sh",
)
EVENT = BASE / "overlay/rootfs/usr/bin/bananarchy-cover-event.sh"
HOOKS = (
BASE / "overlay/rootfs/usr/share/sxmo/default_hooks/sxmo_hook_ring.sh",
BASE / "overlay/rootfs/usr/share/sxmo/default_hooks/sxmo_hook_notification.sh",
BASE / "overlay/rootfs/usr/share/sxmo/default_hooks/sxmo_hook_missed_call.sh",
BASE / "overlay/rootfs/usr/share/sxmo/default_hooks/sxmo_hook_pickup.sh",
BASE / "overlay/rootfs/usr/share/sxmo/default_hooks/sxmo_hook_hangup.sh",
)
def fail(message):
sys.stderr.write("FAIL: %s\n" % message)
return 1
def run_gate(profile, state, arg="", stub=None):
env = dict(os.environ)
env["BANANARCHY_LID_PROFILE"] = str(profile)
env["BANANARCHY_LID_STATE"] = str(state)
if stub:
env["BANANARCHY_COVERCTL"] = str(stub)
return subprocess.run([str(GATE)] + ([arg] if arg else []),
env=env, capture_output=True, text=True)
def main():
paths = [LID_SRC, COVER_SRC, GATE, LID_WRAPPER, WM, STATE_SH, WAKE,
DPMS_SYNC, EVENT, *INPUT_HOOKS, *HOOKS]
for path in paths:
if not path.is_file():
return fail("missing %s" % path.relative_to(BASE))
lid = LID_SRC.read_text(encoding="utf-8")
for token in ("LID_DEBOUNCE_MS 20", "HALL_EDGE_MS 20", "LID_LOOP_MS 20",
"hall_observed", "fast_until", "write_session_file",
"write_lid_state(closed)", 'write_session_file(state, "screenoff")',
'write_session_file(state, "unlock")', 'cover_send("OPEN")',
"read_ready(&list, fds, nfds, &switch_mask, &edge)",
"LID_GENERATION_FILE", "run_dpms",
"bananarchy-lid-state.sh"):
if token not in lid:
return fail("lid daemon lost instant handling: %s" % token)
# The cover daemon must be told to release the bus before the panel is lit.
if lid.index('cover_send("OPEN")') > lid.index('write_sysfs_int(BRIGHTNESS, bl)'):
return fail("the SPI bus is released after the main panel is relit")
lid_close = lid[lid.index('if(closed) {'):lid.index(' } else {')]
if lid_close.index('write_session_file(LOCK_FLAG, "1")') > \
lid_close.index('write_text_file(BRIGHTNESS, "0")'):
return fail("the screen is locked after the panel is blanked")
print("[ok] lid daemon: sticky hall state, edge wakeup and synchronous lock")
scratch = pathlib.Path("/tmp/bantam/9064bb701d9e9f7c6412f0ea6846d88a")
scratch.mkdir(parents=True, exist_ok=True)
build = subprocess.run(
["gcc", "-std=c11", "-O2", "-Wall", "-Wextra", "-Werror",
"-I" + str(LID_SRC.parent), str(LID_SRC), "-o",
str(scratch / "lid-gate-host")],
capture_output=True, text=True)
if build.returncode:
sys.stderr.write(build.stderr)
return fail("lid daemon does not compile on the host")
print("[ok] host build of the compiled lid daemon")
wrapper = LID_WRAPPER.read_text(encoding="utf-8")
if "lid-generation" not in wrapper or "lid-state" not in wrapper:
return fail("the lid hook wrapper is not guarded by lid state/generation")
for path in (GATE, LID_WRAPPER, WM, STATE_SH, WAKE, DPMS_SYNC, EVENT,
*INPUT_HOOKS, *HOOKS):
if subprocess.run(["sh", "-n", str(path)], capture_output=True).returncode:
return fail("shell syntax: %s" % path.relative_to(BASE))
for path in (WM, STATE_SH, WAKE, DPMS_SYNC, *INPUT_HOOKS):
if "bananarchy-lid-gate.sh" not in path.read_text(encoding="utf-8"):
return fail("%s is not gated on the flip lid" % path.relative_to(BASE))
if "bananarchy-cover-event.sh ring" not in HOOKS[0].read_text():
return fail("ring hook does not publish the call to the cover")
if "bananarchy-cover-event.sh notification" not in HOOKS[1].read_text():
return fail("notification hook does not publish to the cover")
event = EVENT.read_text(encoding="utf-8")
if " wake)" not in event or "coverctl WAKE" not in event:
return fail("cover event bridge lost the wake forwarding case")
cover = COVER_SRC.read_text(encoding="utf-8")
if "cover_wake" not in cover or 'strcmp(command, "WAKE")' not in cover:
return fail("cover daemon lost the WAKE command")
print("[ok] every main-panel wake path is gated and forwarded to the cover")
with tempfile.TemporaryDirectory(prefix="bananarchy-lid-gate-") as tmp:
root = pathlib.Path(tmp)
profile = root / "lid-profile"
state = root / "lid-state"
calls = root / "coverctl.calls"
stub = root / "coverctl"
stub.write_text('#!/bin/sh\nprintf "%s\\n" "$*" >> "%s"\n' %
("%s", calls))
stub.chmod(0o755)
profile.write_text("bar\n")
state.write_text("closed\n")
if run_gate(profile, state, stub=stub).returncode != 0:
return fail("a non-flip device was blocked by the lid gate")
print("[ok] non-flip devices are unaffected")
profile.write_text("flip\n")
state.write_text("open\n")
if run_gate(profile, state, "wake", stub).returncode != 0:
return fail("an open flip was blocked by the lid gate")
if calls.exists() and calls.read_text().strip():
return fail("an open flip forwarded a wake to the cover")
print("[ok] open flip: main panel allowed, no cover wake")
state.write_text("closed\n")
if run_gate(profile, state, stub=stub).returncode != 1:
return fail("a closed flip was allowed to light the main panel")
if calls.exists() and calls.read_text().strip():
return fail("a non-wake gate call forwarded to the cover")
if run_gate(profile, state, "wake", stub).returncode != 1:
return fail("a closed flip wake was not blocked")
if "WAKE" not in calls.read_text():
return fail("a blocked main-panel wake was not forwarded to the cover")
print("[ok] closed flip: main panel blocked, WAKE forwarded to cover")
state.write_text("unknown\n")
if run_gate(profile, state, "wake", stub).returncode != 0:
return fail("a missing/unknown lid state blocked the main panel")
print("[ok] unknown lid state fails open for the main panel")
print("instant flip lid handling: all checks passed")
return 0
if __name__ == "__main__":
sys.exit(main())