2026-11-09 22:50:51 +02:00
|
|
|
#ifndef _GNU_SOURCE
|
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
#endif
|
2026-11-10 13:23:03 +02:00
|
|
|
/* 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.
|
|
|
|
|
*/
|
2026-11-09 22:50:51 +02:00
|
|
|
#include <ctype.h>
|
|
|
|
|
#include <poll.h>
|
|
|
|
|
#include <strings.h>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
|
|
#define COVER_LOCK "/run/bananarchy-cover-idle.lock"
|
|
|
|
|
#define DEFAULT_SOCKET "/run/bananarchy/cover.sock"
|
|
|
|
|
#define DEFAULT_FB "/dev/fb1"
|
2026-11-10 13:23:03 +02:00
|
|
|
#define MAIN_BLANK_CFG "/home/user/.config/sxmo/idle_blank_timeout"
|
|
|
|
|
#define DEFAULT_BLANK_SECS 300
|
|
|
|
|
#define CLOCK_REDRAW_MS 30000
|
2026-11-09 22:50:51 +02:00
|
|
|
|
|
|
|
|
struct glyph {
|
|
|
|
|
char ch;
|
|
|
|
|
unsigned char col[5];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static const struct glyph font[] = {
|
|
|
|
|
{'A', {0x7e, 0x11, 0x11, 0x11, 0x7e}}, {'B', {0x7f, 0x49, 0x49, 0x49, 0x36}},
|
|
|
|
|
{'C', {0x3e, 0x41, 0x41, 0x41, 0x22}}, {'D', {0x7f, 0x41, 0x41, 0x22, 0x1c}},
|
|
|
|
|
{'E', {0x7f, 0x49, 0x49, 0x49, 0x41}}, {'F', {0x7f, 0x09, 0x09, 0x09, 0x01}},
|
|
|
|
|
{'G', {0x3e, 0x41, 0x49, 0x49, 0x7a}}, {'H', {0x7f, 0x08, 0x08, 0x08, 0x7f}},
|
|
|
|
|
{'I', {0x00, 0x41, 0x7f, 0x41, 0x00}}, {'J', {0x20, 0x40, 0x41, 0x3f, 0x01}},
|
|
|
|
|
{'K', {0x7f, 0x08, 0x14, 0x22, 0x41}}, {'L', {0x7f, 0x40, 0x40, 0x40, 0x40}},
|
|
|
|
|
{'M', {0x7f, 0x02, 0x0c, 0x02, 0x7f}}, {'N', {0x7f, 0x04, 0x08, 0x10, 0x7f}},
|
|
|
|
|
{'O', {0x3e, 0x41, 0x41, 0x41, 0x3e}}, {'P', {0x7f, 0x09, 0x09, 0x09, 0x06}},
|
|
|
|
|
{'Q', {0x3e, 0x41, 0x51, 0x21, 0x5e}}, {'R', {0x7f, 0x09, 0x19, 0x29, 0x46}},
|
|
|
|
|
{'S', {0x46, 0x49, 0x49, 0x49, 0x31}}, {'T', {0x01, 0x01, 0x7f, 0x01, 0x01}},
|
|
|
|
|
{'U', {0x3f, 0x40, 0x40, 0x40, 0x3f}}, {'V', {0x1f, 0x20, 0x40, 0x20, 0x1f}},
|
|
|
|
|
{'W', {0x3f, 0x40, 0x38, 0x40, 0x3f}}, {'X', {0x63, 0x14, 0x08, 0x14, 0x63}},
|
|
|
|
|
{'Y', {0x07, 0x08, 0x70, 0x08, 0x07}}, {'Z', {0x61, 0x51, 0x49, 0x45, 0x43}},
|
|
|
|
|
{'0', {0x3e, 0x51, 0x49, 0x45, 0x3e}}, {'1', {0x00, 0x42, 0x7f, 0x40, 0x00}},
|
|
|
|
|
{'2', {0x42, 0x61, 0x51, 0x49, 0x46}}, {'3', {0x21, 0x41, 0x45, 0x4b, 0x31}},
|
|
|
|
|
{'4', {0x18, 0x14, 0x12, 0x7f, 0x10}}, {'5', {0x27, 0x45, 0x45, 0x45, 0x39}},
|
|
|
|
|
{'6', {0x3c, 0x4a, 0x49, 0x49, 0x30}}, {'7', {0x01, 0x71, 0x09, 0x05, 0x03}},
|
|
|
|
|
{'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}},
|
2026-11-10 13:23:03 +02:00
|
|
|
{'%', {0x23, 0x13, 0x08, 0x64, 0x62}}, {'|', {0x00, 0x00, 0x7f, 0x00, 0x00}},
|
2026-11-09 22:50:51 +02:00
|
|
|
{' ', {0x00, 0x00, 0x00, 0x00, 0x00}},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static uint32_t rgb888(unsigned int r, unsigned int g, unsigned int b)
|
|
|
|
|
{
|
|
|
|
|
return ((uint32_t)(r & 0xff) << 16) |
|
|
|
|
|
((uint32_t)(g & 0xff) << 8) |
|
|
|
|
|
(uint32_t)(b & 0xff);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static uint16_t rgb565_from_rgb888(uint32_t color)
|
|
|
|
|
{
|
|
|
|
|
unsigned int r = (color >> 16) & 0xff;
|
|
|
|
|
unsigned int g = (color >> 8) & 0xff;
|
|
|
|
|
unsigned int b = color & 0xff;
|
|
|
|
|
return (uint16_t)(((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct frame {
|
|
|
|
|
unsigned char *pixels;
|
|
|
|
|
unsigned int width;
|
|
|
|
|
unsigned int height;
|
|
|
|
|
unsigned int stride;
|
|
|
|
|
unsigned int bpp;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void frame_pixel(struct frame *fb, int x, int y, uint32_t color)
|
|
|
|
|
{
|
|
|
|
|
size_t offset;
|
|
|
|
|
if(x < 0 || y < 0 || (unsigned int)x >= fb->width ||
|
|
|
|
|
(unsigned int)y >= fb->height)
|
|
|
|
|
return;
|
|
|
|
|
offset = (size_t)y * fb->stride + (size_t)x * (fb->bpp / 8);
|
|
|
|
|
if(fb->bpp == 32) {
|
|
|
|
|
memcpy(fb->pixels + offset, &color, 4);
|
|
|
|
|
} else {
|
|
|
|
|
uint16_t pixel = rgb565_from_rgb888(color);
|
|
|
|
|
memcpy(fb->pixels + offset, &pixel, 2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void frame_rect(struct frame *fb, int x0, int y0, int x1, int y1,
|
|
|
|
|
uint32_t color)
|
|
|
|
|
{
|
|
|
|
|
int x;
|
|
|
|
|
int y;
|
|
|
|
|
for(y = y0; y < y1; y++)
|
|
|
|
|
for(x = x0; x < x1; x++)
|
|
|
|
|
frame_pixel(fb, x, y, color);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const unsigned char *glyph_for(char ch)
|
|
|
|
|
{
|
|
|
|
|
size_t i;
|
|
|
|
|
ch = (char)toupper((unsigned char)ch);
|
|
|
|
|
for(i = 0; i < sizeof(font) / sizeof(font[0]); i++) {
|
|
|
|
|
if(font[i].ch == ch)
|
|
|
|
|
return font[i].col;
|
|
|
|
|
}
|
|
|
|
|
return font[sizeof(font) / sizeof(font[0]) - 1].col;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void frame_text(struct frame *fb, const char *text, int x, int y,
|
|
|
|
|
int scale, uint32_t color)
|
|
|
|
|
{
|
|
|
|
|
while(*text) {
|
|
|
|
|
const unsigned char *cols = glyph_for(*text++);
|
|
|
|
|
int cx;
|
|
|
|
|
int cy;
|
|
|
|
|
for(cx = 0; cx < 5; cx++) {
|
|
|
|
|
for(cy = 0; cy < 7; cy++) {
|
|
|
|
|
if(cols[cx] & (1u << cy)) {
|
|
|
|
|
frame_rect(fb, x + cx * scale, y + cy * scale,
|
|
|
|
|
x + (cx + 1) * scale, y + (cy + 1) * scale, color);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
x += 6 * scale;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int read_uint_file(const char *path, unsigned int *value)
|
|
|
|
|
{
|
|
|
|
|
FILE *handle = fopen(path, "r");
|
|
|
|
|
if(!handle)
|
|
|
|
|
return 0;
|
|
|
|
|
if(fscanf(handle, "%u", value) != 1) {
|
|
|
|
|
fclose(handle);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
fclose(handle);
|
|
|
|
|
return *value > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int fb_geometry(const char *fb_path, unsigned int *width,
|
|
|
|
|
unsigned int *height, unsigned int *bpp,
|
|
|
|
|
unsigned int *stride)
|
|
|
|
|
{
|
|
|
|
|
char sysfs[128];
|
|
|
|
|
int index = 0;
|
|
|
|
|
FILE *handle;
|
|
|
|
|
const char *p = strstr(fb_path, "fb");
|
|
|
|
|
unsigned int bytes_per_pixel;
|
2026-11-10 13:23:03 +02:00
|
|
|
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;
|
|
|
|
|
}
|
2026-11-09 22:50:51 +02:00
|
|
|
if(p)
|
|
|
|
|
index = atoi(p + 2);
|
|
|
|
|
snprintf(sysfs, sizeof(sysfs),
|
|
|
|
|
"/sys/class/graphics/fb%d/virtual_size", index);
|
|
|
|
|
handle = fopen(sysfs, "r");
|
|
|
|
|
if(!handle)
|
|
|
|
|
return 0;
|
|
|
|
|
if(fscanf(handle, "%u,%u", width, height) != 2) {
|
|
|
|
|
fclose(handle);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
fclose(handle);
|
|
|
|
|
snprintf(sysfs, sizeof(sysfs),
|
|
|
|
|
"/sys/class/graphics/fb%d/bits_per_pixel", index);
|
|
|
|
|
if(!read_uint_file(sysfs, bpp))
|
|
|
|
|
*bpp = 16;
|
|
|
|
|
if(*bpp != 16 && *bpp != 32)
|
|
|
|
|
return 0;
|
|
|
|
|
bytes_per_pixel = *bpp / 8;
|
|
|
|
|
snprintf(sysfs, sizeof(sysfs), "/sys/class/graphics/fb%d/stride", index);
|
|
|
|
|
if(!read_uint_file(sysfs, stride) || *stride < *width * bytes_per_pixel)
|
|
|
|
|
*stride = *width * bytes_per_pixel;
|
|
|
|
|
return *width > 0 && *height > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int write_all(int fd, const unsigned char *buffer, size_t length)
|
|
|
|
|
{
|
|
|
|
|
size_t written = 0;
|
|
|
|
|
while(written < length) {
|
|
|
|
|
ssize_t n = write(fd, buffer + written, length - written);
|
|
|
|
|
if(n < 0) {
|
|
|
|
|
if(errno == EINTR)
|
|
|
|
|
continue;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if(n == 0)
|
|
|
|
|
return 0;
|
|
|
|
|
written += (size_t)n;
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-10 13:23:03 +02:00
|
|
|
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)
|
2026-11-09 22:50:51 +02:00
|
|
|
{
|
|
|
|
|
int fd = open(fb_path, O_RDWR | O_CLOEXEC);
|
|
|
|
|
unsigned int width = 0;
|
|
|
|
|
unsigned int height = 0;
|
|
|
|
|
unsigned int bpp = 0;
|
|
|
|
|
unsigned int stride = 0;
|
|
|
|
|
size_t size;
|
|
|
|
|
unsigned char *mem;
|
|
|
|
|
struct frame fb;
|
|
|
|
|
char clock[16];
|
|
|
|
|
char date[32];
|
2026-11-10 13:23:03 +02:00
|
|
|
char battery[32];
|
2026-11-09 22:50:51 +02:00
|
|
|
time_t now = time(NULL);
|
|
|
|
|
struct tm tm;
|
|
|
|
|
if(fd < 0)
|
|
|
|
|
return 0;
|
|
|
|
|
if(!fb_geometry(fb_path, &width, &height, &bpp, &stride)) {
|
|
|
|
|
close(fd);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
size = (size_t)stride * height;
|
|
|
|
|
mem = calloc(1, size);
|
|
|
|
|
if(!mem) {
|
|
|
|
|
close(fd);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
fb.pixels = mem;
|
|
|
|
|
fb.width = width;
|
|
|
|
|
fb.height = height;
|
|
|
|
|
fb.stride = stride;
|
|
|
|
|
fb.bpp = bpp;
|
|
|
|
|
localtime_r(&now, &tm);
|
|
|
|
|
strftime(clock, sizeof(clock), "%H:%M", &tm);
|
|
|
|
|
strftime(date, sizeof(date), "%d.%m.%Y", &tm);
|
2026-11-10 13:23:03 +02:00
|
|
|
if(state->test == 2) {
|
2026-11-09 22:50:51 +02:00
|
|
|
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,
|
|
|
|
|
rgb888(40, 230, 80));
|
|
|
|
|
frame_rect(&fb, (int)width / 2, 0, (int)width * 3 / 4, (int)height,
|
|
|
|
|
rgb888(60, 110, 250));
|
|
|
|
|
frame_rect(&fb, (int)width * 3 / 4, 0, (int)width, (int)height,
|
|
|
|
|
rgb888(245, 245, 245));
|
2026-11-10 13:23:03 +02:00
|
|
|
} else if(state->test == 3) {
|
2026-11-09 22:50:51 +02:00
|
|
|
frame_rect(&fb, 0, 0, (int)width, (int)height,
|
|
|
|
|
rgb888(245, 245, 245));
|
2026-11-10 13:23:03 +02:00
|
|
|
} else if(state->test == 4 ||
|
|
|
|
|
strcmp(state->lid, "open") == 0 || !state->visible) {
|
2026-11-09 22:50:51 +02:00
|
|
|
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));
|
|
|
|
|
frame_rect(&fb, 0, 0, (int)width, 3, rgb888(250, 210, 60));
|
|
|
|
|
frame_text(&fb, "BANANARCHY", 12, 26, 2, rgb888(250, 210, 60));
|
|
|
|
|
frame_text(&fb, clock, 20, (int)height / 2 - 20,
|
|
|
|
|
width >= 220 ? 5 : 3, rgb888(235, 240, 248));
|
|
|
|
|
frame_text(&fb, date, 30, (int)height / 2 + 40, 2,
|
|
|
|
|
rgb888(90, 150, 240));
|
2026-11-10 13:23:03 +02:00
|
|
|
/* 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,
|
2026-11-09 22:50:51 +02:00
|
|
|
rgb888(235, 240, 248));
|
|
|
|
|
}
|
|
|
|
|
if(lseek(fd, 0, SEEK_SET) < 0 || !write_all(fd, mem, size)) {
|
|
|
|
|
free(mem);
|
|
|
|
|
close(fd);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
fsync(fd);
|
|
|
|
|
free(mem);
|
|
|
|
|
close(fd);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void mkdir_p(const char *path)
|
|
|
|
|
{
|
|
|
|
|
char buf[256];
|
|
|
|
|
char *p;
|
|
|
|
|
snprintf(buf, sizeof(buf), "%s", path);
|
|
|
|
|
for(p = buf + 1; *p; p++) {
|
|
|
|
|
if(*p != '/')
|
|
|
|
|
continue;
|
|
|
|
|
*p = '\0';
|
|
|
|
|
mkdir(buf, 0755);
|
|
|
|
|
*p = '/';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void trim(char *value)
|
|
|
|
|
{
|
|
|
|
|
size_t len;
|
|
|
|
|
while(*value == ' ' || *value == '\t')
|
|
|
|
|
memmove(value, value + 1, strlen(value));
|
|
|
|
|
len = strlen(value);
|
|
|
|
|
while(len > 0 && (value[len - 1] == ' ' || value[len - 1] == '\t' ||
|
|
|
|
|
value[len - 1] == '\r' || value[len - 1] == '\n')) {
|
|
|
|
|
value[--len] = '\0';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-10 13:23:03 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-09 22:50:51 +02:00
|
|
|
static void read_config(const char *path, char *socket_path, size_t splen,
|
2026-11-10 13:23:03 +02:00
|
|
|
char *fb_path, size_t fblen, struct state *state)
|
2026-11-09 22:50:51 +02:00
|
|
|
{
|
|
|
|
|
char line[256];
|
2026-11-10 13:23:03 +02:00
|
|
|
char blank[32];
|
2026-11-09 22:50:51 +02:00
|
|
|
FILE *handle = fopen(path, "r");
|
|
|
|
|
safe_copy(socket_path, splen, DEFAULT_SOCKET);
|
|
|
|
|
safe_copy(fb_path, fblen, DEFAULT_FB);
|
2026-11-10 13:23:03 +02:00
|
|
|
snprintf(blank, sizeof(blank), "auto");
|
|
|
|
|
state->backlight_path[0] = '\0';
|
|
|
|
|
state->backlight_level = 160;
|
|
|
|
|
if(!handle) {
|
|
|
|
|
state->blank_ms = blank_timeout_ms(blank);
|
2026-11-09 22:50:51 +02:00
|
|
|
return;
|
2026-11-10 13:23:03 +02:00
|
|
|
}
|
2026-11-09 22:50:51 +02:00
|
|
|
while(fgets(line, sizeof(line), handle)) {
|
|
|
|
|
char *comment = strchr(line, '#');
|
|
|
|
|
char *eq;
|
|
|
|
|
char *key;
|
|
|
|
|
char *value;
|
|
|
|
|
if(comment)
|
|
|
|
|
*comment = '\0';
|
|
|
|
|
eq = strchr(line, '=');
|
|
|
|
|
if(!eq)
|
|
|
|
|
continue;
|
|
|
|
|
*eq = '\0';
|
|
|
|
|
key = line;
|
|
|
|
|
value = eq + 1;
|
|
|
|
|
trim(key);
|
|
|
|
|
trim(value);
|
|
|
|
|
if(strcmp(key, "socket_path") == 0)
|
|
|
|
|
safe_copy(socket_path, splen, value);
|
|
|
|
|
else if(strcmp(key, "fb_device") == 0)
|
|
|
|
|
safe_copy(fb_path, fblen, value);
|
2026-11-10 13:23:03 +02:00
|
|
|
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);
|
2026-11-09 22:50:51 +02:00
|
|
|
}
|
|
|
|
|
fclose(handle);
|
2026-11-10 13:23:03 +02:00
|
|
|
state->blank_ms = blank_timeout_ms(blank);
|
2026-11-09 22:50:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void lower_string(char *value)
|
|
|
|
|
{
|
|
|
|
|
while(*value) {
|
|
|
|
|
*value = (char)tolower((unsigned char)*value);
|
|
|
|
|
value++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void upper_string(char *value)
|
|
|
|
|
{
|
|
|
|
|
while(*value) {
|
|
|
|
|
*value = (char)toupper((unsigned char)*value);
|
|
|
|
|
value++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int test_value(const char *mode)
|
|
|
|
|
{
|
|
|
|
|
if(!mode)
|
|
|
|
|
return 0;
|
|
|
|
|
if(strcmp(mode, "bars") == 0)
|
|
|
|
|
return 2;
|
|
|
|
|
if(strcmp(mode, "clock") == 0)
|
|
|
|
|
return 1;
|
|
|
|
|
if(strcmp(mode, "solid") == 0)
|
|
|
|
|
return 3;
|
|
|
|
|
if(strcmp(mode, "off") == 0)
|
|
|
|
|
return 4;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void handle_command(char *buffer, struct state *state, int *redraw)
|
|
|
|
|
{
|
|
|
|
|
char command[32];
|
|
|
|
|
char *save = NULL;
|
|
|
|
|
char *token = strtok_r(buffer, " \t\r\n", &save);
|
|
|
|
|
if(!token)
|
|
|
|
|
return;
|
|
|
|
|
safe_copy(command, sizeof(command), token);
|
|
|
|
|
upper_string(command);
|
|
|
|
|
if(strcmp(command, "LID") == 0) {
|
|
|
|
|
char *value = strtok_r(NULL, " \t\r\n", &save);
|
|
|
|
|
safe_copy(state->lid, sizeof(state->lid),
|
|
|
|
|
value && strcasecmp(value, "CLOSED") == 0 ? "closed" : "open");
|
2026-11-10 13:23:03 +02:00
|
|
|
/* An open flip is always fully off; a closed flip waits for a button. */
|
|
|
|
|
state->visible = 0;
|
|
|
|
|
state->last_input_ms = now_ms();
|
2026-11-09 22:50:51 +02:00
|
|
|
*redraw = 1;
|
2026-11-10 13:23:03 +02:00
|
|
|
} 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;
|
2026-11-09 22:50:51 +02:00
|
|
|
} 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);
|
2026-11-10 13:23:03 +02:00
|
|
|
if(state->visible)
|
|
|
|
|
*redraw = 1;
|
2026-11-09 22:50:51 +02:00
|
|
|
} else if(strcmp(command, "CALL") == 0) {
|
|
|
|
|
char *value = strtok_r(NULL, " \t\r\n", &save);
|
|
|
|
|
if(value) {
|
|
|
|
|
safe_copy(state->call, sizeof(state->call), value);
|
|
|
|
|
lower_string(state->call);
|
|
|
|
|
}
|
|
|
|
|
} else if(strcmp(command, "SMS") == 0) {
|
|
|
|
|
char *value = strtok_r(NULL, " \t\r\n", &save);
|
|
|
|
|
if(value)
|
|
|
|
|
state->sms = (int)strtol(value, NULL, 10);
|
|
|
|
|
} else if(strcmp(command, "BATTERY") == 0) {
|
|
|
|
|
char *value = strtok_r(NULL, " \t\r\n", &save);
|
|
|
|
|
if(value)
|
|
|
|
|
state->battery = (int)strtol(value, NULL, 10);
|
2026-11-10 13:23:03 +02:00
|
|
|
if(state->visible)
|
|
|
|
|
*redraw = 1;
|
2026-11-09 22:50:51 +02:00
|
|
|
} else if(strcmp(command, "SET") == 0) {
|
|
|
|
|
char *key = strtok_r(NULL, " \t\r\n", &save);
|
|
|
|
|
char *value = strtok_r(NULL, " \t\r\n", &save);
|
|
|
|
|
if(key && value && strcmp(key, "enabled") == 0)
|
|
|
|
|
state->enabled = (int)strtol(value, NULL, 10);
|
|
|
|
|
} else if(strcmp(command, "TEST") == 0) {
|
|
|
|
|
char *mode = strtok_r(NULL, " \t\r\n", &save);
|
|
|
|
|
if(mode)
|
|
|
|
|
lower_string(mode);
|
|
|
|
|
state->test = test_value(mode);
|
|
|
|
|
*redraw = 1;
|
|
|
|
|
} else if(strcmp(command, "BLANK") == 0) {
|
2026-11-10 13:23:03 +02:00
|
|
|
state->visible = 0;
|
2026-11-09 22:50:51 +02:00
|
|
|
*redraw = 1;
|
|
|
|
|
} else if(strcmp(command, "UNBLANK") == 0) {
|
2026-11-10 13:23:03 +02:00
|
|
|
if(strcmp(state->lid, "closed") == 0) {
|
|
|
|
|
state->visible = 1;
|
|
|
|
|
state->last_input_ms = now_ms();
|
|
|
|
|
}
|
2026-11-09 22:50:51 +02:00
|
|
|
*redraw = 1;
|
|
|
|
|
} else if(strcmp(command, "REFRESH") == 0 ||
|
|
|
|
|
strcmp(command, "RELOAD_THEME") == 0) {
|
2026-11-10 13:23:03 +02:00
|
|
|
if(state->visible)
|
|
|
|
|
*redraw = 1;
|
2026-11-09 22:50:51 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void reply_for(char *buffer, const struct state *state,
|
|
|
|
|
char *reply, size_t len)
|
|
|
|
|
{
|
|
|
|
|
char command[32];
|
|
|
|
|
char *save = NULL;
|
|
|
|
|
char *token = strtok_r(buffer, " \t\r\n", &save);
|
|
|
|
|
if(!token) {
|
|
|
|
|
snprintf(reply, len, "OK\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
safe_copy(command, sizeof(command), token);
|
|
|
|
|
upper_string(command);
|
|
|
|
|
if(strcmp(command, "PING") == 0) {
|
|
|
|
|
snprintf(reply, len, "OK pong\n");
|
|
|
|
|
} else if(strcmp(command, "STATUS") == 0) {
|
|
|
|
|
snprintf(reply, len,
|
2026-11-10 13:23:03 +02:00
|
|
|
"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);
|
2026-11-09 22:50:51 +02:00
|
|
|
} else if(strcmp(command, "LID") == 0) {
|
|
|
|
|
snprintf(reply, len, "OK lid=%s\n", state->lid);
|
|
|
|
|
} else if(strcmp(command, "TEST") == 0) {
|
|
|
|
|
snprintf(reply, len, "OK test=%d\n", state->test);
|
|
|
|
|
} else {
|
|
|
|
|
snprintf(reply, len, "OK\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
|
{
|
|
|
|
|
const char *lock_path = getenv("BANANARCHY_COVER_LOCK");
|
|
|
|
|
int lock_fd = lock_singleton(lock_path ? lock_path : COVER_LOCK);
|
|
|
|
|
const char *config = "/etc/bananarchy/cover-display.conf";
|
|
|
|
|
char socket_path[256];
|
|
|
|
|
char fb_path[128];
|
|
|
|
|
struct sockaddr_un local;
|
|
|
|
|
int server;
|
|
|
|
|
struct state state;
|
2026-11-10 13:23:03 +02:00
|
|
|
long long last_draw = 0;
|
|
|
|
|
int redraw = 1;
|
2026-11-09 22:50:51 +02:00
|
|
|
if(lock_fd < 0)
|
|
|
|
|
return 1;
|
2026-11-10 13:23:03 +02:00
|
|
|
(void)lock_fd;
|
|
|
|
|
install_reaper();
|
2026-11-09 22:50:51 +02:00
|
|
|
if(argc > 2 && strcmp(argv[1], "--config") == 0)
|
|
|
|
|
config = argv[2];
|
2026-11-10 13:23:03 +02:00
|
|
|
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;
|
2026-11-09 22:50:51 +02:00
|
|
|
read_config(config, socket_path, sizeof(socket_path),
|
2026-11-10 13:23:03 +02:00
|
|
|
fb_path, sizeof(fb_path), &state);
|
2026-11-09 22:50:51 +02:00
|
|
|
mkdir_p(socket_path);
|
|
|
|
|
unlink(socket_path);
|
|
|
|
|
server = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
|
|
|
|
|
if(server < 0)
|
|
|
|
|
return 1;
|
|
|
|
|
if(strlen(socket_path) >= sizeof(local.sun_path))
|
|
|
|
|
return 1;
|
|
|
|
|
memset(&local, 0, sizeof(local));
|
|
|
|
|
local.sun_family = AF_UNIX;
|
|
|
|
|
strncpy(local.sun_path, socket_path, sizeof(local.sun_path) - 1);
|
|
|
|
|
if(bind(server, (struct sockaddr *)&local, sizeof(local)) < 0)
|
|
|
|
|
return 1;
|
|
|
|
|
chmod(socket_path, 0660);
|
|
|
|
|
chown(socket_path, 0, SESSION_GID);
|
|
|
|
|
while(1) {
|
|
|
|
|
struct pollfd pfd;
|
|
|
|
|
char buffer[512];
|
|
|
|
|
char original[512];
|
|
|
|
|
char reply[256];
|
|
|
|
|
struct sockaddr_un peer;
|
|
|
|
|
socklen_t peer_len = sizeof(peer);
|
|
|
|
|
ssize_t nread;
|
2026-11-10 13:23:03 +02:00
|
|
|
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;
|
|
|
|
|
}
|
2026-11-09 22:50:51 +02:00
|
|
|
pfd.fd = server;
|
|
|
|
|
pfd.events = POLLIN;
|
|
|
|
|
pfd.revents = 0;
|
2026-11-10 13:23:03 +02:00
|
|
|
if(poll(&pfd, 1, timeout_ms) > 0 && (pfd.revents & POLLIN)) {
|
2026-11-09 22:50:51 +02:00
|
|
|
nread = recvfrom(server, buffer, sizeof(buffer) - 1, 0,
|
|
|
|
|
(struct sockaddr *)&peer, &peer_len);
|
|
|
|
|
if(nread > 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(strncasecmp(original, "QUIT", 4) == 0) {
|
2026-11-10 13:23:03 +02:00
|
|
|
set_backlight(&state, 0);
|
2026-11-09 22:50:51 +02:00
|
|
|
close(server);
|
|
|
|
|
unlink(socket_path);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|