Implement 2720 Flip cover display behaviour

This commit is contained in:
Luxferre committed 2026-11-10 13:23:03 +02:00
1 parent a50c01b416
commit ec55fb1457
20 files changed
+630 -49

No files matched your search

+196 -44
View File
@@ -1,7 +1,17 @@
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
/* Compiled idle cover IPC/renderer for nokia-beatles. No Python in the idle path. */
/* Compiled idle cover IPC/renderer for nokia-beatles. No Python in the idle path.
*
* Behaviour contract for the 2720 Flip external panel:
* - flip open: the panel is fully off (black frame and backlight 0);
* - flip closed: the panel stays off until an external side button is pressed;
* - BUTTON shows the clock, date and the compact network/battery indicators;
* - the same Screen blank timeout as dwm (~/.config/sxmo/idle_blank_timeout,
* default 300 s, 0 = never) blanks the cover again when no button arrives;
* - all other events (network, SMS, battery, calls, theme) only update the
* frame while the cover is visible, so an idle daemon stays at 0% CPU.
*/
#include <ctype.h>
#include <poll.h>
#include <strings.h>
@@ -13,6 +23,9 @@
#define COVER_LOCK "/run/bananarchy-cover-idle.lock"
#define DEFAULT_SOCKET "/run/bananarchy/cover.sock"
#define DEFAULT_FB "/dev/fb1"
#define MAIN_BLANK_CFG "/home/user/.config/sxmo/idle_blank_timeout"
#define DEFAULT_BLANK_SECS 300
#define CLOCK_REDRAW_MS 30000
struct glyph {
char ch;
@@ -40,6 +53,7 @@ static const struct glyph font[] = {
{'8', {0x36, 0x49, 0x49, 0x49, 0x36}}, {'9', {0x06, 0x49, 0x49, 0x29, 0x1e}},
{':', {0x00, 0x36, 0x36, 0x00, 0x00}}, {'-', {0x08, 0x08, 0x08, 0x08, 0x08}},
{'.', {0x00, 0x60, 0x60, 0x00, 0x00}}, {'/', {0x20, 0x10, 0x08, 0x04, 0x02}},
{'%', {0x23, 0x13, 0x08, 0x64, 0x62}}, {'|', {0x00, 0x00, 0x7f, 0x00, 0x00}},
{' ', {0x00, 0x00, 0x00, 0x00, 0x00}},
};
@@ -143,6 +157,12 @@ static int fb_geometry(const char *fb_path, unsigned int *width,
FILE *handle;
const char *p = strstr(fb_path, "fb");
unsigned int bytes_per_pixel;
const char *sim = getenv("BANANARCHY_COVER_FB_GEOM");
if(sim && sscanf(sim, "%u,%u,%u", width, height, bpp) == 3 &&
*width > 0 && *height > 0 && (*bpp == 16 || *bpp == 32)) {
*stride = *width * (*bpp / 8);
return 1;
}
if(p)
index = atoi(p + 2);
snprintf(sysfs, sizeof(sysfs),
@@ -185,7 +205,44 @@ static int write_all(int fd, const unsigned char *buffer, size_t length)
return 1;
}
static int draw_frame(const char *fb_path, const char *message, int test)
struct state {
char lid[16];
char backend[16];
char call[64];
char message[128];
char network[96];
int enabled;
int battery;
int sms;
int test;
int visible;
long long last_input_ms;
long long blank_ms;
char backlight_path[128];
int backlight_level;
};
/* Fully off means both a black framebuffer and a zeroed backlight. A missing
* backlight node is normal while the cover panel DT overlay is hardware-gated;
* the black frame still leaves nothing visible on the panel. */
static void set_backlight(const struct state *state, int on)
{
char value[32];
int fd;
if(!state->backlight_path[0])
return;
snprintf(value, sizeof(value), "%d", on ? state->backlight_level : 0);
/* O_TRUNC matters for regular files (host tests and future sysfs shims):
* a shorter "0" must not leave the tail of the previous "160" behind. */
fd = open(state->backlight_path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC,
0644);
if(fd < 0)
return;
(void)!write(fd, value, strlen(value));
close(fd);
}
static int draw_frame(const char *fb_path, const struct state *state)
{
int fd = open(fb_path, O_RDWR | O_CLOEXEC);
unsigned int width = 0;
@@ -197,6 +254,7 @@ static int draw_frame(const char *fb_path, const char *message, int test)
struct frame fb;
char clock[16];
char date[32];
char battery[32];
time_t now = time(NULL);
struct tm tm;
if(fd < 0)
@@ -219,7 +277,7 @@ static int draw_frame(const char *fb_path, const char *message, int test)
localtime_r(&now, &tm);
strftime(clock, sizeof(clock), "%H:%M", &tm);
strftime(date, sizeof(date), "%d.%m.%Y", &tm);
if(test == 2) {
if(state->test == 2) {
frame_rect(&fb, 0, 0, (int)width / 4, (int)height,
rgb888(250, 40, 40));
frame_rect(&fb, (int)width / 4, 0, (int)width / 2, (int)height,
@@ -228,10 +286,11 @@ static int draw_frame(const char *fb_path, const char *message, int test)
rgb888(60, 110, 250));
frame_rect(&fb, (int)width * 3 / 4, 0, (int)width, (int)height,
rgb888(245, 245, 245));
} else if(test == 3) {
} else if(state->test == 3) {
frame_rect(&fb, 0, 0, (int)width, (int)height,
rgb888(245, 245, 245));
} else if(test == 4) {
} else if(state->test == 4 ||
strcmp(state->lid, "open") == 0 || !state->visible) {
frame_rect(&fb, 0, 0, (int)width, (int)height, rgb888(0, 0, 0));
} else {
frame_rect(&fb, 0, 0, (int)width, (int)height, rgb888(8, 12, 26));
@@ -241,8 +300,17 @@ static int draw_frame(const char *fb_path, const char *message, int test)
width >= 220 ? 5 : 3, rgb888(235, 240, 248));
frame_text(&fb, date, 30, (int)height / 2 + 40, 2,
rgb888(90, 150, 240));
if(message && message[0])
frame_text(&fb, message, 6, (int)height - 30, 2,
/* Compact network / battery indicators, pushed by the event bridge. */
if(state->network[0])
frame_text(&fb, state->network, 6, (int)height - 44, 1,
rgb888(120, 230, 160));
if(state->battery >= 0) {
snprintf(battery, sizeof(battery), "BAT %d%%", state->battery);
frame_text(&fb, battery, 6, (int)height - 26, 1,
rgb888(250, 210, 60));
}
if(state->message[0])
frame_text(&fb, state->message, 6, (int)height - 12, 1,
rgb888(235, 240, 248));
}
if(lseek(fd, 0, SEEK_SET) < 0 || !write_all(fd, mem, size)) {
@@ -282,15 +350,50 @@ static void trim(char *value)
}
}
static long long blank_timeout_ms(const char *configured)
{
const char *path = getenv("BANANARCHY_COVER_MAIN_BLANK_CFG");
char buf[32];
long secs = DEFAULT_BLANK_SECS;
long value;
if(!path || !path[0])
path = MAIN_BLANK_CFG;
if(configured && strcasecmp(configured, "auto") != 0 && configured[0]) {
value = strtol(configured, NULL, 10);
if(value < 0)
value = 0;
if(value > 86400)
value = 86400;
return (long long)value * 1000LL;
}
/* "auto" (the default) follows Config -> Screen blank exactly. */
FILE *handle = fopen(path, "r");
if(handle) {
if(fscanf(handle, "%31s", buf) == 1) {
value = strtol(buf, NULL, 10);
if(value >= 0 && value <= 86400)
secs = value;
}
fclose(handle);
}
return (long long)secs * 1000LL;
}
static void read_config(const char *path, char *socket_path, size_t splen,
char *fb_path, size_t fblen)
char *fb_path, size_t fblen, struct state *state)
{
char line[256];
char blank[32];
FILE *handle = fopen(path, "r");
safe_copy(socket_path, splen, DEFAULT_SOCKET);
safe_copy(fb_path, fblen, DEFAULT_FB);
if(!handle)
snprintf(blank, sizeof(blank), "auto");
state->backlight_path[0] = '\0';
state->backlight_level = 160;
if(!handle) {
state->blank_ms = blank_timeout_ms(blank);
return;
}
while(fgets(line, sizeof(line), handle)) {
char *comment = strchr(line, '#');
char *eq;
@@ -310,8 +413,17 @@ static void read_config(const char *path, char *socket_path, size_t splen,
safe_copy(socket_path, splen, value);
else if(strcmp(key, "fb_device") == 0)
safe_copy(fb_path, fblen, value);
else if(strcmp(key, "backlight_path") == 0)
safe_copy(state->backlight_path, sizeof(state->backlight_path), value);
else if(strcmp(key, "brightness") == 0) {
long v = strtol(value, NULL, 10);
if(v > 0 && v <= 255)
state->backlight_level = (int)v;
} else if(strcmp(key, "blank_timeout") == 0)
safe_copy(blank, sizeof(blank), value);
}
fclose(handle);
state->blank_ms = blank_timeout_ms(blank);
}
static void lower_string(char *value)
@@ -330,17 +442,6 @@ static void upper_string(char *value)
}
}
struct state {
char lid[16];
char backend[16];
char call[64];
char message[128];
int enabled;
int battery;
int sms;
int test;
};
static int test_value(const char *mode)
{
if(!mode)
@@ -369,14 +470,33 @@ static void handle_command(char *buffer, struct state *state, int *redraw)
char *value = strtok_r(NULL, " \t\r\n", &save);
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;
state->last_input_ms = now_ms();
*redraw = 1;
}
} else if(strcmp(command, "NETWORK") == 0) {
char *rest = save;
while(rest && (*rest == ' ' || *rest == '\t'))
rest++;
safe_copy(state->network, sizeof(state->network), rest ? rest : "");
trim(state->network);
if(state->visible)
*redraw = 1;
} else if(strcmp(command, "NOTIFY") == 0) {
char *rest = save;
while(rest && (*rest == ' ' || *rest == '\t'))
rest++;
safe_copy(state->message, sizeof(state->message), rest ? rest : "");
trim(state->message);
*redraw = 1;
if(state->visible)
*redraw = 1;
} else if(strcmp(command, "CALL") == 0) {
char *value = strtok_r(NULL, " \t\r\n", &save);
if(value) {
@@ -391,6 +511,8 @@ static void handle_command(char *buffer, struct state *state, int *redraw)
char *value = strtok_r(NULL, " \t\r\n", &save);
if(value)
state->battery = (int)strtol(value, NULL, 10);
if(state->visible)
*redraw = 1;
} else if(strcmp(command, "SET") == 0) {
char *key = strtok_r(NULL, " \t\r\n", &save);
char *value = strtok_r(NULL, " \t\r\n", &save);
@@ -403,14 +525,18 @@ static void handle_command(char *buffer, struct state *state, int *redraw)
state->test = test_value(mode);
*redraw = 1;
} else if(strcmp(command, "BLANK") == 0) {
state->test = 4;
state->visible = 0;
*redraw = 1;
} else if(strcmp(command, "UNBLANK") == 0) {
state->test = 0;
if(strcmp(state->lid, "closed") == 0) {
state->visible = 1;
state->last_input_ms = now_ms();
}
*redraw = 1;
} else if(strcmp(command, "REFRESH") == 0 ||
strcmp(command, "RELOAD_THEME") == 0) {
*redraw = 1;
if(state->visible)
*redraw = 1;
}
}
@@ -430,9 +556,11 @@ static void reply_for(char *buffer, const struct state *state,
snprintf(reply, len, "OK pong\n");
} else if(strcmp(command, "STATUS") == 0) {
snprintf(reply, len,
"OK lid=%s backend=%s enabled=%d battery=%d call=%s sms=%d\n",
state->lid, state->backend, state->enabled, state->battery,
state->call, state->sms);
"OK lid=%s backend=%s enabled=%d visible=%d blank_ms=%lld "
"battery=%d call=%s sms=%d network=%s\n",
state->lid, state->backend, state->enabled, state->visible,
state->blank_ms, state->battery, state->call, state->sms,
state->network);
} else if(strcmp(command, "LID") == 0) {
snprintf(reply, len, "OK lid=%s\n", state->lid);
} else if(strcmp(command, "TEST") == 0) {
@@ -452,13 +580,22 @@ int main(int argc, char **argv)
struct sockaddr_un local;
int server;
struct state state;
time_t last_draw = 0;
long long last_draw = 0;
int redraw = 1;
if(lock_fd < 0)
return 1;
(void)lock_fd;
install_reaper();
if(argc > 2 && strcmp(argv[1], "--config") == 0)
config = argv[2];
memset(&state, 0, sizeof(state));
snprintf(state.lid, sizeof(state.lid), "open");
snprintf(state.backend, sizeof(state.backend), "none");
snprintf(state.call, sizeof(state.call), "none");
state.enabled = 1;
state.battery = -1;
read_config(config, socket_path, sizeof(socket_path),
fb_path, sizeof(fb_path));
fb_path, sizeof(fb_path), &state);
mkdir_p(socket_path);
unlink(socket_path);
server = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
@@ -473,12 +610,6 @@ int main(int argc, char **argv)
return 1;
chmod(socket_path, 0660);
chown(socket_path, 0, SESSION_GID);
memset(&state, 0, sizeof(state));
snprintf(state.lid, sizeof(state.lid), "open");
snprintf(state.backend, sizeof(state.backend), "none");
snprintf(state.call, sizeof(state.call), "none");
state.enabled = 1;
state.battery = -1;
while(1) {
struct pollfd pfd;
char buffer[512];
@@ -487,34 +618,55 @@ int main(int argc, char **argv)
struct sockaddr_un peer;
socklen_t peer_len = sizeof(peer);
ssize_t nread;
long long now = now_ms();
/* With the cover off there is no timer at all: poll blocks until IPC. */
int timeout_ms = -1;
if(state.visible) {
long long next = CLOCK_REDRAW_MS - (now - last_draw);
if(next < 1)
next = 1;
if(state.blank_ms > 0) {
long long remaining = state.blank_ms - (now - state.last_input_ms);
if(remaining <= 0) {
state.visible = 0;
redraw = 1;
} else if(remaining < next) {
next = remaining;
}
}
timeout_ms = (int)(next > 1000000 ? 1000000 : next);
if(state.visible && now - last_draw >= CLOCK_REDRAW_MS)
redraw = 1;
}
if(redraw) {
set_backlight(&state, state.visible || state.test == 2 ||
state.test == 3 || state.test == 1);
snprintf(state.backend, sizeof(state.backend), "%s",
draw_frame(fb_path, &state) ? "fb" : "none");
last_draw = now;
redraw = 0;
}
pfd.fd = server;
pfd.events = POLLIN;
pfd.revents = 0;
if(poll(&pfd, 1, 1000) > 0 && (pfd.revents & POLLIN)) {
if(poll(&pfd, 1, timeout_ms) > 0 && (pfd.revents & POLLIN)) {
nread = recvfrom(server, buffer, sizeof(buffer) - 1, 0,
(struct sockaddr *)&peer, &peer_len);
if(nread > 0) {
int redraw = 0;
buffer[nread] = '\0';
snprintf(original, sizeof(original), "%s", buffer);
handle_command(buffer, &state, &redraw);
reply_for(original, &state, reply, sizeof(reply));
sendto(server, reply, strlen(reply), 0,
(struct sockaddr *)&peer, peer_len);
if(redraw)
last_draw = 0;
if(strncasecmp(original, "QUIT", 4) == 0) {
set_backlight(&state, 0);
close(server);
unlink(socket_path);
return 0;
}
}
}
if(state.enabled && time(NULL) - last_draw > 30) {
last_draw = time(NULL);
snprintf(state.backend, sizeof(state.backend), "%s",
draw_frame(fb_path, state.message, state.test) ? "fb" : "none");
}
}
return 0;
}
@@ -3,10 +3,15 @@
#endif
/* Compiled side-volume-key reader for nokia-beatles. No Python in the idle path. */
#include <poll.h>
#include <sys/socket.h>
#include <sys/un.h>
#include "common.h"
#define VOLUME_LOCK "/run/bananarchy-volume-keys.lock"
#define LID_STATE_FILE "/run/bananarchy/lid-state"
#define LID_CLIENT_SOCKET "/run/bananarchy/volume-lid-client.sock"
#define COVER_SOCKET "/run/bananarchy/cover.sock"
#ifndef EV_KEY
#define EV_KEY 0x01
@@ -18,6 +23,10 @@
#define KEY_VOLUMEUP 115
#endif
#ifndef KEY_POWER
#define KEY_POWER 116
#endif
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",
@@ -28,6 +37,45 @@ static char *const session_env[] = {
NULL
};
static int lid_is_closed(void)
{
char buf[32] = "";
FILE *handle = fopen(LID_STATE_FILE, "r");
if(!handle)
return 0;
if(!fgets(buf, sizeof(buf), handle))
buf[0] = '\0';
fclose(handle);
return strncmp(buf, "closed", 6) == 0;
}
/* While the flip is closed the external side keys are cover-display wake
* buttons, not volume controls. The cover daemon is a 0%-CPU datagram
* server; a failed send just means it is not running on this target. */
static void cover_button(void)
{
int fd = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
struct sockaddr_un local;
struct sockaddr_un remote;
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);
sendto(fd, "BUTTON\n", 7, 0, (struct sockaddr *)&remote, sizeof(remote));
close(fd);
unlink(LID_CLIENT_SOCKET);
}
static void apply_volume(const char *action)
{
char *const argv[] = {
@@ -58,6 +106,13 @@ static void read_ready(struct watch_list *list, struct pollfd *fds, int nfds)
while((nread = read(fds[i].fd, &ev, sizeof(ev))) == (ssize_t)sizeof(ev)) {
if(ev.type != EV_KEY || ev.value != 1)
continue;
if(ev.code != KEY_VOLUMEUP && ev.code != KEY_VOLUMEDOWN &&
ev.code != KEY_POWER)
continue;
if(lid_is_closed()) {
cover_button();
continue;
}
if(ev.code == KEY_VOLUMEUP)
apply_volume("up");
else if(ev.code == KEY_VOLUMEDOWN)