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

+42 -10
View File
@@ -1046,6 +1046,18 @@ static int test_value(const char *mode)
return 0;
}
/* A closed flip lights the outer panel only. Status updates (NETWORK,
* BATTERY) refresh an already visible cover but never wake it; real events
* (BUTTON, WAKE, CALL, NOTIFY, SMS) do. */
static void cover_wake(struct state *state, int *redraw)
{
if(strcmp(state->lid, "closed") == 0 && state->enabled && !state->test) {
state->visible = 1;
state->last_input_ms = now_ms();
*redraw = 1;
}
}
static void handle_command(char *buffer, struct state *state,
struct cover_hw *hw, int *redraw)
{
@@ -1058,19 +1070,23 @@ static void handle_command(char *buffer, struct state *state,
upper_string(command);
if(strcmp(command, "LID") == 0) {
char *value = strtok_r(NULL, " \t\r\n", &save);
int new_closed = value && strcasecmp(value, "CLOSED") == 0;
int old_closed = strcmp(state->lid, "closed") == 0;
safe_copy(state->lid, sizeof(state->lid),
value && strcasecmp(value, "CLOSED") == 0 ? "closed" : "open");
/* An open flip is always fully off; a closed flip waits for a button. */
state->visible = 0;
state->last_input_ms = now_ms();
*redraw = 1;
} else if(strcmp(command, "BUTTON") == 0) {
/* Trusted external side-button event from bananarchy-volume-keys. */
if(strcmp(state->lid, "closed") == 0 && state->enabled && !state->test) {
state->visible = 1;
new_closed ? "closed" : "open");
/* The lid daemon rebroadcasts its state every 2 s. Only a real
* transition blanks the cover; a repeating LID CLOSED must not undo a
* WAKE from an incoming call or an external side button. */
if(new_closed != old_closed) {
state->visible = 0;
state->last_input_ms = now_ms();
*redraw = 1;
}
} else if(strcmp(command, "BUTTON") == 0 ||
strcmp(command, "WAKE") == 0) {
/* Trusted external side button, or a main-panel wake request that the
* closed-lid gate forwarded to the cover display. */
cover_wake(state, redraw);
} else if(strcmp(command, "NETWORK") == 0) {
char *rest = save;
while(rest && (*rest == ' ' || *rest == '\t'))
@@ -1085,6 +1101,7 @@ static void handle_command(char *buffer, struct state *state,
rest++;
safe_copy(state->message, sizeof(state->message), rest ? rest : "");
trim(state->message);
cover_wake(state, redraw);
if(state->visible)
*redraw = 1;
} else if(strcmp(command, "CALL") == 0) {
@@ -1092,11 +1109,14 @@ static void handle_command(char *buffer, struct state *state,
if(value) {
safe_copy(state->call, sizeof(state->call), value);
lower_string(state->call);
if(strcmp(state->call, "none") != 0)
cover_wake(state, redraw);
}
} else if(strcmp(command, "SMS") == 0) {
char *value = strtok_r(NULL, " \t\r\n", &save);
if(value)
state->sms = (int)strtol(value, NULL, 10);
cover_wake(state, redraw);
} else if(strcmp(command, "BATTERY") == 0) {
char *value = strtok_r(NULL, " \t\r\n", &save);
if(value)
@@ -1201,6 +1221,7 @@ int main(int argc, char **argv)
struct state state;
struct cover_hw hw;
long long last_draw = 0;
long long pending_until = 0;
int redraw = 1;
if(lock_fd < 0)
return 1;
@@ -1263,13 +1284,24 @@ int main(int argc, char **argv)
set_backlight(&state, cover_is_lit(&state));
snprintf(state.backend, sizeof(state.backend), "%s",
cover_draw(&hw, fb_path, &state));
/* The lid daemon forces DPMS off asynchronously. Retry a pending draw
* for a bounded window so the cover lights even if the first draw ran
* before Xorg stopped flushing the main panel. */
pending_until = strcmp(state.backend, "pending") == 0 ?
now + 3000 : 0;
last_draw = now;
redraw = 0;
}
if(pending_until > now)
timeout_ms = 50;
pfd.fd = server;
pfd.events = POLLIN;
pfd.revents = 0;
if(poll(&pfd, 1, timeout_ms) > 0 && (pfd.revents & POLLIN)) {
if(poll(&pfd, 1, timeout_ms) == 0 && pending_until > now &&
strcmp(state.backend, "pending") == 0) {
redraw = 1;
}
if(pfd.revents & POLLIN) {
nread = recvfrom(server, buffer, sizeof(buffer) - 1, 0,
(struct sockaddr *)&peer, &peer_len);
if(nread > 0) {
+149 -27
View File
@@ -1,7 +1,16 @@
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
/* Compiled flip-lid handler for nokia-beatles. No Python in the idle path. */
/* Compiled flip-lid handler for nokia-beatles. No Python in the idle path.
*
* A closed flip must blank and lock the main panel immediately, and an open
* flip must hand the shared QUP5 bus back to the main panel immediately.
* The old loop re-derived "observed" from a stale EV_SW mask on every 100 ms
* iteration and only consulted the real hall lines every 200 ms, so the
* candidate oscillated open/closed and the 80 ms debounce almost never
* completed: a close could take seconds. The hall reading is now sticky and
* any EV_SW edge forces an immediate hall re-read, with a 20 ms debounce.
*/
#include <poll.h>
#include <sys/mount.h>
#include <sys/socket.h>
@@ -11,10 +20,20 @@
#define LID_LOCK "/run/bananarchy-lid.lock"
#define LID_STATE_FILE "/run/bananarchy/lid-state"
#define LID_GENERATION_FILE "/run/bananarchy/lid-generation"
#define LOCK_FLAG "/tmp/bananarchy_screen_locked"
#define LOCKER_FIFO "/tmp/dwm_plugin_locker.fifo"
#define LID_STABLE_MS 80
#define HALL_POLL_MS 200
#define SXMO_STATE_FILE "/run/user/10000/sxmo.state"
#define BRIGHTNESS "/sys/class/backlight/backlight/brightness"
#define BL_POWER "/sys/class/backlight/backlight/bl_power"
#define KBD_BACKLIGHT "/sys/class/leds/white:kbd_backlight/brightness"
#define BRIGHTNESS_SAVE "/tmp/sxmo_saved_brightness"
#define KBD_SAVE "/tmp/sxmo_saved_kbd_brightness"
#define LID_DEBOUNCE_MS 20
#define HALL_POLL_MS 100
#define HALL_EDGE_MS 20
#define LID_LOOP_MS 20
#define LID_MIN_GAP_MS 50
#define GPIO_FILE "/sys/kernel/debug/gpio"
#define COVER_SOCKET "/run/bananarchy/cover.sock"
#define LID_CLIENT_SOCKET "/run/bananarchy/lid-client.sock"
@@ -35,14 +54,71 @@ static char *const session_env[] = {
NULL
};
static void run_sxmo_state(const char *action)
/* Hooks are asynchronous. Each spawn carries the transition generation and
* the expected lid state; the wrapper refuses to run if either changed, so a
* late screenoff spawn can never re-lock a flip that was just opened. */
static unsigned long lid_generation = 0;
static void run_sxmo_state(const char *action, const char *expected_lid)
{
char generation[32];
char *const argv[] = {
(char *)"/usr/bin/bananarchy-lid-state.sh", (char *)expected_lid,
(char *)action, generation, NULL
};
snprintf(generation, sizeof(generation), "%lu", lid_generation);
daemon_spawn_env(argv, (char *const *)session_env);
}
/* DPMS is the gate the cover daemon uses before taking the shared SPI bus.
* The udev backlight helper is too slow on its own, so force it directly. */
static void run_dpms(const char *mode)
{
char *const argv[] = {
(char *)"/usr/bin/sxmo_state.sh", (char *)"set", (char *)action, NULL
(char *)"/usr/bin/xset", (char *)"dpms", (char *)"force",
(char *)mode, NULL
};
daemon_spawn_env(argv, (char *const *)session_env);
}
/* Files the user session also writes must stay owned by the session user. */
static void write_session_file(const char *path, const char *value)
{
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
if(fd < 0)
return;
(void)!write(fd, value, strlen(value));
fchown(fd, SESSION_UID, SESSION_GID);
fchmod(fd, 0644);
close(fd);
}
static int read_int_file(const char *path, int fallback)
{
FILE *handle = fopen(path, "r");
int value = fallback;
if(!handle)
return fallback;
if(fscanf(handle, "%d", &value) != 1)
value = fallback;
fclose(handle);
return value;
}
static void write_int_file(const char *path, int value)
{
char buf[32];
snprintf(buf, sizeof(buf), "%d", value);
write_session_file(path, buf);
}
static void write_sysfs_int(const char *path, int value)
{
char buf[32];
snprintf(buf, sizeof(buf), "%d", value);
write_text_file(path, buf);
}
static void write_lid_state(int closed)
{
const char *value = closed ? "closed\n" : "open\n";
@@ -95,24 +171,55 @@ static void locker_send(const char *payload)
close(fd);
}
static const char *sxmo_state_path(void)
{
const char *path = getenv("SXMO_STATE");
return path && *path ? path : SXMO_STATE_FILE;
}
/* Everything user-visible happens synchronously here: no shell, no fork and
* no scheduler wait before the main panel is dark and the screen is locked.
* The Sxmo state hooks are spawned afterwards and only repeat these writes. */
static void on_transition(int closed)
{
const char *state = sxmo_state_path();
char generation[32];
lid_generation++;
snprintf(generation, sizeof(generation), "%lu", lid_generation);
write_session_file(LID_GENERATION_FILE, generation);
/* Publish first: every main-panel wake path is gated on this file. */
write_lid_state(closed);
if(closed) {
write_text_file("/sys/class/backlight/backlight/brightness", "0");
write_text_file("/sys/class/backlight/backlight/bl_power", "4");
write_text_file("/sys/class/leds/white:kbd_backlight/brightness", "0");
int saved_bl = read_int_file(BRIGHTNESS, -1);
int saved_kbd = read_int_file(KBD_BACKLIGHT, -1);
write_session_file(LOCK_FLAG, "1");
write_session_file(state, "screenoff");
if(saved_bl > 0)
write_int_file(BRIGHTNESS_SAVE, saved_bl);
if(saved_kbd > 0)
write_int_file(KBD_SAVE, saved_kbd);
write_text_file(KBD_BACKLIGHT, "0");
/* The brightness uevent drives bananarchy-display-power-sync (DPMS off),
* which is also the gate the cover daemon uses to take the SPI bus. */
write_text_file(BRIGHTNESS, "0");
write_text_file(BL_POWER, "4");
run_dpms("off");
cover_send("CLOSED");
locker_send("SCREENOFF\n");
run_sxmo_state("screenoff");
run_sxmo_state("screenoff", "closed");
} else {
unlink(LOCK_FLAG);
write_text_file("/sys/class/backlight/backlight/bl_power", "0");
write_text_file("/sys/class/backlight/backlight/brightness", "10");
write_text_file("/sys/class/leds/white:kbd_backlight/brightness", "1");
int bl = read_int_file(BRIGHTNESS_SAVE, 10);
int kbd = read_int_file(KBD_SAVE, 1);
/* Release the cover's shared SPI bus before relighting the main panel. */
cover_send("OPEN");
unlink(LOCK_FLAG);
write_session_file(state, "unlock");
write_text_file(BL_POWER, "0");
write_sysfs_int(BRIGHTNESS, bl);
write_sysfs_int(KBD_BACKLIGHT, kbd);
run_dpms("on");
locker_send("CLOSE\n");
run_sxmo_state("unlock");
run_sxmo_state("unlock", "open");
}
}
@@ -214,7 +321,7 @@ static int current_switch_mask(int *found)
}
static void read_ready(struct watch_list *list, struct pollfd *fds, int nfds,
int *switch_mask)
int *switch_mask, int *edge)
{
int i;
for(i = 0; i < nfds; i++) {
@@ -233,6 +340,7 @@ static void read_ready(struct watch_list *list, struct pollfd *fds, int nfds,
bit = switch_bit(ev.code);
if(!bit)
continue;
*edge = 1;
if(ev.value)
*switch_mask |= (int)bit;
else
@@ -255,17 +363,22 @@ int main(void)
long long last_transition = 0;
long long last_broadcast = 0;
long long candidate_since;
long long fast_until = 0;
int evdev_found = 0;
int switch_mask;
int stable_closed;
int candidate;
int hall;
int hall_observed;
int edge;
int observed;
if(lock_fd < 0)
return 1;
install_reaper();
memset(&list, 0, sizeof(list));
switch_mask = current_switch_mask(&evdev_found);
hall = hall_state();
hall_observed = hall;
if(hall >= 0)
stable_closed = hall;
else if(evdev_found)
@@ -282,8 +395,6 @@ int main(void)
struct pollfd fds[WATCH_MAX];
int nfds = 0;
int i;
int observed;
now = now_ms();
if(now - last_scan > 2000) {
scan_devices(&list, WATCH_SWITCHES);
last_scan = now;
@@ -297,28 +408,39 @@ int main(void)
fds[nfds].revents = 0;
nfds++;
}
if(poll(fds, (nfds_t)nfds, 100) < 0 && errno != EINTR) {
if(poll(fds, (nfds_t)nfds, LID_LOOP_MS) < 0 && errno != EINTR) {
for(i = 0; i < list.count; i++)
drop_device(&list, list.devs[i].fd);
}
now = now_ms();
edge = 0;
if(nfds > 0)
read_ready(&list, fds, nfds, &switch_mask);
observed = (switch_mask & (int)CLOSED_MASK) != 0;
/* EV_SW can miss edges or report a stale mask on the 2720 hall sensors.
* Reconcile with the raw lines even while evdev remains available. */
if(now - last_hall_poll >= HALL_POLL_MS) {
read_ready(&list, fds, nfds, &switch_mask, &edge);
/* An EV_SW edge is the hardware interrupt: read the raw hall lines at
* once instead of waiting for the next reconciliation tick. The result
* is sticky, so a stale EV_SW mask cannot flip the candidate back. */
if(edge) {
hall = hall_state();
hall_observed = hall >= 0 ? hall :
((switch_mask & (int)CLOSED_MASK) != 0);
last_hall_poll = now;
fast_until = now + 120;
} else if(now - last_hall_poll >=
(now < fast_until ? HALL_EDGE_MS : HALL_POLL_MS)) {
hall = hall_state();
if(hall >= 0)
observed = hall;
hall_observed = hall;
last_hall_poll = now;
}
observed = hall_observed >= 0 ? hall_observed :
((switch_mask & (int)CLOSED_MASK) != 0);
if(observed != candidate) {
candidate = observed;
candidate_since = now;
}
if(candidate >= 0 && candidate != stable_closed &&
now - candidate_since >= LID_STABLE_MS &&
now - last_transition >= 150) {
if(candidate != stable_closed &&
now - candidate_since >= LID_DEBOUNCE_MS &&
now - last_transition >= LID_MIN_GAP_MS) {
stable_closed = candidate;
last_transition = now;
on_transition(stable_closed);