#ifndef _GNU_SOURCE #define _GNU_SOURCE #endif /* 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 #include #include #include #include "common.h" #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 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" #define LID_BROADCAST_MS 2000 #define EV_SW 0x05 #define SW_LID 0x00 #define SW_KEYPAD_SLIDE 0x0a #define CLOSED_MASK ((1u << SW_LID) | (1u << SW_KEYPAD_SLIDE)) #define BA_EVIOCGSW(len) BA_IOC(2U, 'E', 0x1bU, (unsigned int)(len)) static char *const session_env[] = { (char *)"PATH=/home/user/.local/bin:/home/user/.config/sxmo/hooks:/usr/share/sxmo/default_hooks:/usr/local/bin:/usr/bin:/bin:/sbin:/usr/sbin", (char *)"HOME=/home/user", (char *)"USER=user", (char *)"DISPLAY=:0", (char *)"XDG_RUNTIME_DIR=/run/user/10000", (char *)"SHELL=/bin/sh", NULL }; /* 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/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"; int fd; mkdir("/run/bananarchy", 0755); fd = open(LID_STATE_FILE, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644); if(fd < 0) return; if(write(fd, value, strlen(value)) < 0) { close(fd); return; } close(fd); chmod(LID_STATE_FILE, 0644); } static void cover_send(const char *state) { int fd = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0); struct sockaddr_un local; struct sockaddr_un remote; char payload[64]; if(fd < 0) return; mkdir("/run/bananarchy", 0755); unlink(LID_CLIENT_SOCKET); memset(&local, 0, sizeof(local)); local.sun_family = AF_UNIX; snprintf(local.sun_path, sizeof(local.sun_path), "%s", LID_CLIENT_SOCKET); if(bind(fd, (struct sockaddr *)&local, sizeof(local)) < 0) { close(fd); return; } memset(&remote, 0, sizeof(remote)); remote.sun_family = AF_UNIX; snprintf(remote.sun_path, sizeof(remote.sun_path), "%s", COVER_SOCKET); snprintf(payload, sizeof(payload), "LID %s", state); sendto(fd, payload, strlen(payload), 0, (struct sockaddr *)&remote, sizeof(remote)); close(fd); unlink(LID_CLIENT_SOCKET); } static void locker_send(const char *payload) { int fd = open(LOCKER_FIFO, O_WRONLY | O_NONBLOCK | O_CLOEXEC); if(fd < 0) return; (void)write(fd, payload, strlen(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) { 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", "closed"); } else { 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", "open"); } } static int hall_line_closed(const char *line) { char buf[256]; char *save = NULL; char *token; char *name = NULL; char *level = NULL; int index = 0; safe_copy(buf, sizeof(buf), line); token = strtok_r(buf, " \t\r\n", &save); while(token) { if(index == 0) name = token; if(index == 3) level = token; index++; token = strtok_r(NULL, " \t\r\n", &save); } if(!name || !level) return 0; if(strcmp(name, "gpio36") != 0 && strcmp(name, "gpio92") != 0) return 0; return strcmp(level, "low") == 0; } static int hall_state(void) { const char *override = getenv("BANANARCHY_LID_GPIO_FILE"); const char *path = override ? override : GPIO_FILE; char line[256]; FILE *handle; int closed = 0; if(access(path, R_OK) < 0) { mount("none", "/sys/kernel/debug", "debugfs", 0, NULL); } handle = fopen(path, "r"); if(!handle) return -1; while(fgets(line, sizeof(line), handle)) { if(hall_line_closed(line)) { closed = 1; break; } } fclose(handle); return closed; } static unsigned int switch_bit(unsigned int code) { if(code == SW_LID || code == SW_KEYPAD_SLIDE) return 1u << code; return 0; } static int device_switch_mask(int fd) { unsigned char bits[16]; unsigned int mask = 0; memset(bits, 0, sizeof(bits)); if(ioctl(fd, BA_EVIOCGSW(sizeof(bits)), bits) < 0) return -1; if(bits[SW_LID / 8] & (1u << (SW_LID % 8))) mask |= switch_bit(SW_LID); if(bits[SW_KEYPAD_SLIDE / 8] & (1u << (SW_KEYPAD_SLIDE % 8))) mask |= switch_bit(SW_KEYPAD_SLIDE); return (int)mask; } static int current_switch_mask(int *found) { glob_t gl; size_t i; int mask = 0; *found = 0; if(glob("/dev/input/event*", 0, NULL, &gl) != 0) return 0; for(i = 0; i < gl.gl_pathc; i++) { int fd = open(gl.gl_pathv[i], O_RDONLY | O_NONBLOCK | O_CLOEXEC); int devmask; if(fd < 0) continue; if(!wanted_device(fd, WATCH_SWITCHES)) { close(fd); continue; } devmask = device_switch_mask(fd); if(devmask >= 0) { *found = 1; mask |= devmask; } close(fd); } globfree(&gl); return mask & (int)CLOSED_MASK; } static void read_ready(struct watch_list *list, struct pollfd *fds, int nfds, int *switch_mask, int *edge) { int i; for(i = 0; i < nfds; i++) { struct input_event ev; ssize_t nread = 0; if(fds[i].revents & (POLLHUP | POLLERR | POLLNVAL)) { drop_device(list, fds[i].fd); continue; } if(!(fds[i].revents & POLLIN)) continue; while((nread = read(fds[i].fd, &ev, sizeof(ev))) == (ssize_t)sizeof(ev)) { unsigned int bit; if(ev.type != EV_SW) continue; bit = switch_bit(ev.code); if(!bit) continue; *edge = 1; if(ev.value) *switch_mask |= (int)bit; else *switch_mask &= ~(int)bit; } if(nread < 0 && errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) { drop_device(list, fds[i].fd); } } } int main(void) { int lock_fd = lock_singleton(LID_LOCK); struct watch_list list; long long now; long long last_scan = 0; long long last_hall_poll = 0; 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) stable_closed = (switch_mask & (int)CLOSED_MASK) != 0; else stable_closed = 0; candidate = stable_closed; now = now_ms(); candidate_since = now; write_lid_state(stable_closed); cover_send(stable_closed ? "CLOSED" : "OPEN"); last_broadcast = now; while(1) { struct pollfd fds[WATCH_MAX]; int nfds = 0; int i; if(now - last_scan > 2000) { scan_devices(&list, WATCH_SWITCHES); last_scan = now; if(list.count > 0 && !evdev_found) { switch_mask = current_switch_mask(&evdev_found); } } for(i = 0; i < list.count; i++) { fds[nfds].fd = list.devs[i].fd; fds[nfds].events = POLLIN; fds[nfds].revents = 0; nfds++; } 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, &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) 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 != 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); } if(now - last_broadcast >= LID_BROADCAST_MS) { write_lid_state(stable_closed); cover_send(stable_closed ? "CLOSED" : "OPEN"); last_broadcast = now; } } return 0; }