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-10 19:41:57 +02:00
|
|
|
*
|
|
|
|
|
* The 2720's two GC9305 panels share one QUP5 SPI bus through a gpio13 mux.
|
|
|
|
|
* Registering both panels in the DT darkens the main panel, so the shipping
|
|
|
|
|
* DTB keeps one panel and this daemon drives the external panel from userspace
|
|
|
|
|
* on /dev/spidev0.1 ONLY while the flip is closed (the main panel is blanked
|
|
|
|
|
* and DPMS-off then). On open it restores the mux to the internal panel and
|
|
|
|
|
* releases every GPIO/SPIDEV handle before Xorg can flush another frame.
|
|
|
|
|
* If the opt-in two-panel DT is installed, /dev/fb1 takes precedence instead.
|
2026-11-10 13:23:03 +02:00
|
|
|
*/
|
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>
|
|
|
|
|
|
2026-11-10 14:11:00 +02:00
|
|
|
/* The armv7 cross sysroot does not always ship linux/fb.h. These UAPI
|
|
|
|
|
* values are stable; use the header when it exists, otherwise the numeric
|
|
|
|
|
* definitions from linux/fb.h (FBIOBLANK = _IO('F', 0x11) = 0x4611). */
|
|
|
|
|
#if defined(__has_include)
|
|
|
|
|
#if __has_include(<linux/fb.h>)
|
|
|
|
|
#include <linux/fb.h>
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
#ifndef FBIOBLANK
|
|
|
|
|
#define FBIOBLANK 0x4611
|
|
|
|
|
#endif
|
|
|
|
|
#ifndef FB_BLANK_UNBLANK
|
|
|
|
|
#define FB_BLANK_UNBLANK 0
|
|
|
|
|
#endif
|
|
|
|
|
#ifndef FB_BLANK_POWERDOWN
|
|
|
|
|
#define FB_BLANK_POWERDOWN 4
|
|
|
|
|
#endif
|
|
|
|
|
|
2026-11-09 22:50:51 +02:00
|
|
|
#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-10 23:20:13 +02:00
|
|
|
#define KERNEL_REDRAW_MS 1000
|
2026-11-10 19:41:57 +02:00
|
|
|
#define COVER_SPI_WIDTH 240
|
|
|
|
|
#define COVER_SPI_HEIGHT 240
|
|
|
|
|
#define COVER_SPI_CHUNK 2048
|
|
|
|
|
#define MAIN_DPMS_PATH "/sys/class/drm/card0-SPI-1/dpms"
|
|
|
|
|
|
|
|
|
|
/* Self-contained linux/gpio.h (v1 ABI) and linux/spi/spidev.h UAPI values.
|
|
|
|
|
* The armv7 cross sysroot does not always ship these headers. The layouts are
|
|
|
|
|
* fixed by the kernel ABI; a compile-time size check guards the cross build. */
|
|
|
|
|
#define BA_GPIO_IOC_MAGIC 0xb4u
|
|
|
|
|
#define BA_IOC_WR 1u
|
|
|
|
|
#define BA_IOC_RDWR 3u
|
|
|
|
|
#define BA_IOC(dir, type, nr, size) \
|
|
|
|
|
(((unsigned int)(dir) << 30) | ((unsigned int)(type) << 8) | \
|
|
|
|
|
((unsigned int)(nr) << 0) | ((unsigned int)(size) << 16))
|
|
|
|
|
#define BA_SPI_IOC_MESSAGE(n) \
|
|
|
|
|
BA_IOC(BA_IOC_WR, 'k', 0u, (unsigned int)(n) * 32u)
|
|
|
|
|
#define BA_SPI_IOC_WR_MODE BA_IOC(BA_IOC_WR, 'k', 1u, 1u)
|
|
|
|
|
#define BA_SPI_IOC_WR_BITS BA_IOC(BA_IOC_WR, 'k', 3u, 1u)
|
|
|
|
|
#define BA_SPI_IOC_WR_SPEED BA_IOC(BA_IOC_WR, 'k', 4u, 4u)
|
|
|
|
|
#define BA_GPIO_GET_LINEHANDLE BA_IOC(BA_IOC_RDWR, BA_GPIO_IOC_MAGIC, 3u, 364u)
|
|
|
|
|
#define BA_GPIO_GET_VALUES BA_IOC(BA_IOC_RDWR, BA_GPIO_IOC_MAGIC, 8u, 64u)
|
|
|
|
|
#define BA_GPIO_SET_VALUES BA_IOC(BA_IOC_RDWR, BA_GPIO_IOC_MAGIC, 9u, 64u)
|
|
|
|
|
#define BA_GPIOHANDLE_REQUEST_OUTPUT (1u << 1)
|
|
|
|
|
|
|
|
|
|
struct ba_gpiohandle_request {
|
|
|
|
|
uint32_t lineoffsets[64];
|
|
|
|
|
uint32_t flags;
|
|
|
|
|
uint8_t default_values[64];
|
|
|
|
|
char consumer_label[32];
|
|
|
|
|
uint32_t lines;
|
|
|
|
|
int32_t fd;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct ba_gpiohandle_data {
|
|
|
|
|
uint8_t values[64];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct ba_spi_ioc_transfer {
|
|
|
|
|
uint64_t tx_buf;
|
|
|
|
|
uint64_t rx_buf;
|
|
|
|
|
uint32_t len;
|
|
|
|
|
uint32_t speed_hz;
|
|
|
|
|
uint16_t delay_usecs;
|
|
|
|
|
uint8_t bits_per_word;
|
|
|
|
|
uint8_t cs_change;
|
|
|
|
|
uint8_t tx_nbits;
|
|
|
|
|
uint8_t rx_nbits;
|
|
|
|
|
uint8_t word_delay_usecs;
|
|
|
|
|
uint8_t pad;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_Static_assert(sizeof(struct ba_gpiohandle_request) == 364,
|
|
|
|
|
"gpiochip v1 linehandle ABI size");
|
|
|
|
|
_Static_assert(sizeof(struct ba_spi_ioc_transfer) == 32,
|
|
|
|
|
"spidev transfer ABI size");
|
|
|
|
|
|
2026-11-10 21:54:53 +02:00
|
|
|
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
|
|
|
|
|
};
|
|
|
|
|
|
2026-11-10 19:41:57 +02:00
|
|
|
struct cover_hw {
|
|
|
|
|
char backend[16];
|
|
|
|
|
char spi_path[128];
|
|
|
|
|
char gpio_path[128];
|
|
|
|
|
char firmware[128];
|
|
|
|
|
char sim_path[128];
|
|
|
|
|
unsigned int mux_gpio;
|
|
|
|
|
unsigned int dc_gpio;
|
|
|
|
|
unsigned int reset_gpio;
|
|
|
|
|
int mux_closed_level;
|
|
|
|
|
int mux_open_level;
|
|
|
|
|
int dc_command_level;
|
|
|
|
|
int dc_data_level;
|
|
|
|
|
int reset_assert_level;
|
|
|
|
|
int reset_idle_level;
|
|
|
|
|
unsigned int speed_hz;
|
2026-11-10 20:56:42 +02:00
|
|
|
int word_bits;
|
|
|
|
|
int panel_lines;
|
|
|
|
|
int probe_index;
|
|
|
|
|
char probe_desc[96];
|
2026-11-10 21:54:53 +02:00
|
|
|
char kernel_fb[128];
|
|
|
|
|
char kernel_dpms[128];
|
2026-11-11 07:57:38 +02:00
|
|
|
char kernel_sync[128];
|
2026-11-10 23:20:13 +02:00
|
|
|
char kernel_cmd[128];
|
|
|
|
|
char kernel_bl[128];
|
2026-11-11 07:57:38 +02:00
|
|
|
char kernel_bl_power[128];
|
2026-11-10 23:20:13 +02:00
|
|
|
char kernel_marker[128];
|
|
|
|
|
unsigned int kernel_bl_level;
|
2026-11-11 07:57:38 +02:00
|
|
|
int kernel_bl_invert;
|
2026-11-10 21:54:53 +02:00
|
|
|
int kernel_lit;
|
2026-11-10 23:20:13 +02:00
|
|
|
int kernel_owned;
|
2026-11-11 07:57:38 +02:00
|
|
|
int kernel_restored;
|
2026-11-10 19:41:57 +02:00
|
|
|
int sim;
|
|
|
|
|
int spi_enabled;
|
|
|
|
|
int fd_spi;
|
|
|
|
|
int fd_chip;
|
|
|
|
|
int fd_mux;
|
|
|
|
|
int fd_panel;
|
|
|
|
|
int fd_sim;
|
|
|
|
|
int mux_claimed;
|
|
|
|
|
int claimed;
|
|
|
|
|
int initialized;
|
|
|
|
|
};
|
2026-11-09 22:50:51 +02:00
|
|
|
|
2026-11-10 20:56:42 +02:00
|
|
|
/* Hardware sign-off cases for the external GC9305. The KaiOS downstream DTS
|
|
|
|
|
* calls gpio21 "TE", not D/C, and Qualcomm MDSS SPI sends D/C as the ninth
|
|
|
|
|
* bit, so both 8-bit (D/C on a GPIO) and 9-bit (D/C in bit 8) are plausible.
|
|
|
|
|
* PROBE walks these while drawing TEST bars; the working case is written to
|
|
|
|
|
* /etc/bananarchy/cover-display.conf afterwards. */
|
|
|
|
|
struct probe_case {
|
|
|
|
|
int bits;
|
|
|
|
|
int mux;
|
|
|
|
|
int reset;
|
|
|
|
|
int dc;
|
|
|
|
|
unsigned int speed;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static const struct probe_case probe_cases[] = {
|
|
|
|
|
/* 9-bit first: the downstream DTS labels gpio21 TE and MDSS SPI carries
|
|
|
|
|
* D/C in bit 8, so this is the best-supported wiring. */
|
|
|
|
|
{9, 1, 0, 0, 25000000}, {9, 1, 1, 0, 25000000},
|
|
|
|
|
{9, 0, 0, 0, 25000000}, {9, 0, 1, 0, 25000000},
|
|
|
|
|
{9, 1, 0, 0, 5000000}, {9, 0, 1, 0, 5000000},
|
|
|
|
|
{8, 1, 0, 0, 25000000}, {8, 1, 1, 0, 25000000},
|
|
|
|
|
{8, 1, 0, 1, 25000000}, {8, 1, 1, 1, 25000000},
|
|
|
|
|
{8, 0, 0, 0, 25000000}, {8, 1, 0, 0, 5000000},
|
|
|
|
|
};
|
|
|
|
|
|
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 23:20:13 +02:00
|
|
|
static void write_cover_text(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));
|
|
|
|
|
close(fd);
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
|
2026-11-10 14:11:00 +02:00
|
|
|
/* A lit cover needs a lit framebuffer, a lit backlight and an unblanked fbdev;
|
|
|
|
|
* everything else is "fully off". TEST modes are diagnostic overrides. */
|
|
|
|
|
static int cover_is_lit(const struct state *state)
|
|
|
|
|
{
|
|
|
|
|
if(state->test == 4)
|
|
|
|
|
return 0;
|
|
|
|
|
if(state->test == 1 || state->test == 2 || state->test == 3)
|
|
|
|
|
return 1;
|
|
|
|
|
return state->enabled && state->visible &&
|
|
|
|
|
strcmp(state->lid, "closed") == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Fully off means a black framebuffer, a zeroed backlight and an fbdev
|
|
|
|
|
* FB_BLANK_POWERDOWN. An empty backlight_path is normal on hardware without
|
|
|
|
|
* a separate cover backlight sysfs node; fbdev blanking still powers it down. */
|
2026-11-10 13:23:03 +02:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-10 19:41:57 +02:00
|
|
|
static void render_frame(struct frame *fb, const struct state *state)
|
|
|
|
|
{
|
|
|
|
|
char clock[16];
|
|
|
|
|
char date[32];
|
|
|
|
|
char battery[32];
|
|
|
|
|
time_t now = time(NULL);
|
|
|
|
|
struct tm tm;
|
|
|
|
|
unsigned int width = fb->width;
|
|
|
|
|
unsigned int height = fb->height;
|
|
|
|
|
localtime_r(&now, &tm);
|
|
|
|
|
strftime(clock, sizeof(clock), "%H:%M", &tm);
|
|
|
|
|
strftime(date, sizeof(date), "%d.%m.%Y", &tm);
|
|
|
|
|
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,
|
|
|
|
|
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));
|
|
|
|
|
} else if(state->test == 3) {
|
|
|
|
|
frame_rect(fb, 0, 0, (int)width, (int)height,
|
|
|
|
|
rgb888(245, 245, 245));
|
|
|
|
|
} else if(!cover_is_lit(state)) {
|
|
|
|
|
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));
|
|
|
|
|
/* 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));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-10 13:23:03 +02:00
|
|
|
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;
|
|
|
|
|
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;
|
2026-11-10 19:41:57 +02:00
|
|
|
render_frame(&fb, state);
|
2026-11-09 22:50:51 +02:00
|
|
|
if(lseek(fd, 0, SEEK_SET) < 0 || !write_all(fd, mem, size)) {
|
|
|
|
|
free(mem);
|
|
|
|
|
close(fd);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
fsync(fd);
|
2026-11-10 14:11:00 +02:00
|
|
|
/* DRM fbdev emulation turns FB_BLANK_POWERDOWN into a DPMS-off on the
|
|
|
|
|
* external connector, so the cover is electrically dark as well as black.
|
|
|
|
|
* Unblank before drawing a lit frame; blank after writing the black one.
|
|
|
|
|
* Regular files (host tests / sim backends) simply ignore the ioctl. */
|
|
|
|
|
(void)ioctl(fd, FBIOBLANK,
|
|
|
|
|
cover_is_lit(state) ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN);
|
2026-11-09 22:50:51 +02:00
|
|
|
free(mem);
|
|
|
|
|
close(fd);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-10 19:41:57 +02:00
|
|
|
/* --- Closed-flip-only userspace GC9305 backend (/dev/spidev0.1) --------- */
|
|
|
|
|
|
|
|
|
|
static void hw_event(struct cover_hw *hw, const char *line)
|
|
|
|
|
{
|
|
|
|
|
if(hw->fd_sim < 0)
|
|
|
|
|
return;
|
|
|
|
|
(void)!write(hw->fd_sim, line, strlen(line));
|
|
|
|
|
(void)!write(hw->fd_sim, "\n", 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int hw_gpio_set(struct cover_hw *hw, int fd,
|
|
|
|
|
const unsigned char *values, unsigned int count)
|
|
|
|
|
{
|
|
|
|
|
struct ba_gpiohandle_data data;
|
|
|
|
|
char line[64];
|
|
|
|
|
unsigned int i;
|
|
|
|
|
snprintf(line, sizeof(line), "gpio");
|
|
|
|
|
for(i = 0; i < count; i++)
|
|
|
|
|
snprintf(line + strlen(line), sizeof(line) - strlen(line),
|
|
|
|
|
" %u", values[i]);
|
|
|
|
|
hw_event(hw, line);
|
|
|
|
|
if(hw->sim)
|
|
|
|
|
return 1;
|
|
|
|
|
memset(&data, 0, sizeof(data));
|
|
|
|
|
for(i = 0; i < count && i < sizeof(data.values); i++)
|
|
|
|
|
data.values[i] = values[i];
|
|
|
|
|
return ioctl(fd, BA_GPIO_SET_VALUES, &data) >= 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int hw_gpio_request(struct cover_hw *hw, const unsigned int *offsets,
|
|
|
|
|
const unsigned char *defaults, unsigned int count)
|
|
|
|
|
{
|
|
|
|
|
struct ba_gpiohandle_request req;
|
|
|
|
|
unsigned int i;
|
|
|
|
|
if(hw->sim) {
|
|
|
|
|
hw_event(hw, "claim");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
if(hw->fd_chip < 0)
|
|
|
|
|
return -1;
|
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
|
for(i = 0; i < count && i < 64; i++) {
|
|
|
|
|
req.lineoffsets[i] = offsets[i];
|
|
|
|
|
req.default_values[i] = defaults[i];
|
|
|
|
|
}
|
|
|
|
|
req.flags = BA_GPIOHANDLE_REQUEST_OUTPUT;
|
|
|
|
|
snprintf(req.consumer_label, sizeof(req.consumer_label),
|
|
|
|
|
"bananarchy-cover");
|
|
|
|
|
req.lines = count;
|
|
|
|
|
req.fd = -1;
|
|
|
|
|
if(ioctl(hw->fd_chip, BA_GPIO_GET_LINEHANDLE, &req) < 0)
|
|
|
|
|
return -1;
|
|
|
|
|
hw_event(hw, "claim");
|
|
|
|
|
return req.fd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int hw_xfer(struct cover_hw *hw, const unsigned char *tx, size_t len,
|
|
|
|
|
int cs_change)
|
|
|
|
|
{
|
|
|
|
|
struct ba_spi_ioc_transfer tr;
|
|
|
|
|
if(hw->sim)
|
|
|
|
|
return 1;
|
|
|
|
|
memset(&tr, 0, sizeof(tr));
|
|
|
|
|
tr.tx_buf = (uint64_t)(uintptr_t)tx;
|
|
|
|
|
tr.len = (uint32_t)len;
|
|
|
|
|
tr.bits_per_word = 8;
|
|
|
|
|
tr.cs_change = (uint8_t)(cs_change ? 1 : 0);
|
|
|
|
|
return ioctl(hw->fd_spi, BA_SPI_IOC_MESSAGE(1), &tr) >= 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int hw_dc(struct cover_hw *hw, int level)
|
|
|
|
|
{
|
|
|
|
|
unsigned char values[2];
|
2026-11-10 20:56:42 +02:00
|
|
|
if(hw->word_bits == 9)
|
|
|
|
|
return 1;
|
2026-11-10 19:41:57 +02:00
|
|
|
values[0] = (unsigned char)level;
|
|
|
|
|
values[1] = (unsigned char)hw->reset_idle_level;
|
|
|
|
|
return hw_gpio_set(hw, hw->fd_panel, values, 2);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-10 20:56:42 +02:00
|
|
|
static int hw_reset_set(struct cover_hw *hw, int level)
|
|
|
|
|
{
|
|
|
|
|
unsigned char values[2];
|
|
|
|
|
if(hw->panel_lines == 1) {
|
|
|
|
|
values[0] = (unsigned char)level;
|
|
|
|
|
return hw_gpio_set(hw, hw->fd_panel, values, 1);
|
|
|
|
|
}
|
|
|
|
|
values[0] = (unsigned char)hw->dc_data_level;
|
|
|
|
|
values[1] = (unsigned char)level;
|
|
|
|
|
return hw_gpio_set(hw, hw->fd_panel, values, 2);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-10 19:41:57 +02:00
|
|
|
static int hw_command(struct cover_hw *hw, unsigned char cmd,
|
|
|
|
|
const unsigned char *params, size_t n)
|
|
|
|
|
{
|
|
|
|
|
char line[64];
|
2026-11-10 20:56:42 +02:00
|
|
|
snprintf(line, sizeof(line), "cmd %02x %zu bits=%d", cmd, n,
|
|
|
|
|
hw->word_bits);
|
2026-11-10 19:41:57 +02:00
|
|
|
hw_event(hw, line);
|
|
|
|
|
if(hw->sim)
|
|
|
|
|
return 1;
|
2026-11-10 20:56:42 +02:00
|
|
|
if(hw->word_bits == 9) {
|
|
|
|
|
/* MIPI DBI Type C: D/C is bit 8 of each little-endian 9-bit word. */
|
|
|
|
|
unsigned char buf[64];
|
|
|
|
|
size_t used = 0;
|
|
|
|
|
size_t i;
|
|
|
|
|
if((n + 1) * 2 > sizeof(buf))
|
|
|
|
|
return 0;
|
|
|
|
|
buf[used++] = cmd;
|
|
|
|
|
buf[used++] = 0;
|
|
|
|
|
for(i = 0; i < n; i++) {
|
|
|
|
|
buf[used++] = params[i];
|
|
|
|
|
buf[used++] = 1;
|
|
|
|
|
}
|
|
|
|
|
return hw_xfer(hw, buf, used, 1);
|
|
|
|
|
}
|
2026-11-10 19:41:57 +02:00
|
|
|
if(!hw_dc(hw, hw->dc_command_level))
|
|
|
|
|
return 0;
|
|
|
|
|
if(!hw_xfer(hw, &cmd, 1, n ? 0 : 1))
|
|
|
|
|
return 0;
|
|
|
|
|
if(!n)
|
|
|
|
|
return 1;
|
|
|
|
|
if(!hw_dc(hw, hw->dc_data_level))
|
|
|
|
|
return 0;
|
|
|
|
|
while(n) {
|
|
|
|
|
size_t chunk = n > COVER_SPI_CHUNK ? COVER_SPI_CHUNK : n;
|
|
|
|
|
if(!hw_xfer(hw, params, chunk, chunk == n))
|
|
|
|
|
return 0;
|
|
|
|
|
params += chunk;
|
|
|
|
|
n -= chunk;
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int hw_init_panel(struct cover_hw *hw)
|
|
|
|
|
{
|
|
|
|
|
unsigned char blob[4096];
|
|
|
|
|
unsigned char blank[2] = {0, 0};
|
|
|
|
|
size_t len = 0;
|
|
|
|
|
size_t p;
|
|
|
|
|
ssize_t got;
|
|
|
|
|
int fd = open(hw->firmware, O_RDONLY | O_CLOEXEC);
|
|
|
|
|
if(fd < 0)
|
|
|
|
|
return 0;
|
|
|
|
|
got = read(fd, blob, sizeof(blob));
|
|
|
|
|
close(fd);
|
|
|
|
|
if(got < 17)
|
|
|
|
|
return 0;
|
|
|
|
|
len = (size_t)got;
|
|
|
|
|
if(memcmp(blob, "MIPI DBI", 8) != 0)
|
|
|
|
|
return 0;
|
|
|
|
|
p = 16;
|
|
|
|
|
while(p + 2 <= len) {
|
|
|
|
|
unsigned char cmd = blob[p];
|
|
|
|
|
size_t n = blob[p + 1];
|
|
|
|
|
p += 2;
|
|
|
|
|
if(p + n > len)
|
|
|
|
|
break;
|
|
|
|
|
if(cmd == 0x00 && n == 1) {
|
|
|
|
|
/* Vendor "delay ms" record. Host simulation skips the wait. */
|
|
|
|
|
if(!hw->sim)
|
|
|
|
|
poll(NULL, 0, (int)blob[p] > 0 ? (int)blob[p] : 1);
|
|
|
|
|
} else if(cmd == 0x2c && n == 0) {
|
|
|
|
|
/* Memory-write marker: every command gets its own CS framing, so a
|
|
|
|
|
* bare 0x2c is not part of the init sequence. */
|
|
|
|
|
hw_event(hw, "skip 2c");
|
|
|
|
|
} else {
|
|
|
|
|
hw_command(hw, cmd, n ? blob + p : blank, n);
|
|
|
|
|
}
|
|
|
|
|
p += n;
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int hw_activate(struct cover_hw *hw)
|
|
|
|
|
{
|
|
|
|
|
unsigned int mux_offset[1];
|
|
|
|
|
unsigned int panel_offsets[2];
|
|
|
|
|
unsigned char mux_default[1];
|
|
|
|
|
unsigned char panel_default[2];
|
|
|
|
|
int fd;
|
|
|
|
|
if(!hw->spi_enabled)
|
|
|
|
|
return 0;
|
|
|
|
|
if(!hw->sim && hw->fd_chip < 0) {
|
|
|
|
|
hw->fd_chip = open(hw->gpio_path, O_RDWR | O_CLOEXEC);
|
|
|
|
|
if(hw->fd_chip < 0)
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if(!hw->mux_claimed) {
|
|
|
|
|
mux_offset[0] = hw->mux_gpio;
|
|
|
|
|
mux_default[0] = (unsigned char)hw->mux_open_level;
|
|
|
|
|
fd = hw_gpio_request(hw, mux_offset, mux_default, 1);
|
|
|
|
|
if(fd < 0)
|
|
|
|
|
return 0;
|
|
|
|
|
hw->fd_mux = fd;
|
|
|
|
|
hw->mux_claimed = 1;
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
unsigned char mux_value[1];
|
|
|
|
|
mux_value[0] = (unsigned char)hw->mux_closed_level;
|
|
|
|
|
hw_gpio_set(hw, hw->fd_mux, mux_value, 1);
|
|
|
|
|
}
|
|
|
|
|
if(!hw->claimed) {
|
2026-11-10 20:56:42 +02:00
|
|
|
hw->panel_lines = hw->word_bits == 9 ? 1 : 2;
|
|
|
|
|
if(hw->panel_lines == 1) {
|
|
|
|
|
panel_offsets[0] = hw->reset_gpio;
|
|
|
|
|
panel_default[0] = (unsigned char)hw->reset_idle_level;
|
|
|
|
|
} else {
|
|
|
|
|
panel_offsets[0] = hw->dc_gpio;
|
|
|
|
|
panel_offsets[1] = hw->reset_gpio;
|
|
|
|
|
panel_default[0] = (unsigned char)hw->dc_data_level;
|
|
|
|
|
panel_default[1] = (unsigned char)hw->reset_idle_level;
|
|
|
|
|
}
|
|
|
|
|
fd = hw_gpio_request(hw, panel_offsets, panel_default,
|
|
|
|
|
(unsigned int)hw->panel_lines);
|
2026-11-10 19:41:57 +02:00
|
|
|
if(fd < 0)
|
|
|
|
|
return 0;
|
|
|
|
|
hw->fd_panel = fd;
|
|
|
|
|
hw->claimed = 1;
|
|
|
|
|
hw->initialized = 0;
|
|
|
|
|
if(hw->fd_spi < 0 && !hw->sim) {
|
|
|
|
|
unsigned char mode = 0;
|
|
|
|
|
unsigned char bits = 8;
|
|
|
|
|
uint32_t speed = hw->speed_hz;
|
|
|
|
|
hw->fd_spi = open(hw->spi_path, O_RDWR | O_CLOEXEC);
|
|
|
|
|
if(hw->fd_spi < 0)
|
|
|
|
|
return 0;
|
2026-11-10 20:56:42 +02:00
|
|
|
bits = (unsigned char)hw->word_bits;
|
2026-11-10 19:41:57 +02:00
|
|
|
if(ioctl(hw->fd_spi, BA_SPI_IOC_WR_MODE, &mode) < 0 ||
|
|
|
|
|
ioctl(hw->fd_spi, BA_SPI_IOC_WR_BITS, &bits) < 0 ||
|
|
|
|
|
ioctl(hw->fd_spi, BA_SPI_IOC_WR_SPEED, &speed) < 0)
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(!hw->initialized) {
|
|
|
|
|
/* Reset pulse on the external panel, then replay its firmware stream. */
|
2026-11-10 20:56:42 +02:00
|
|
|
hw_reset_set(hw, hw->reset_assert_level);
|
2026-11-10 19:41:57 +02:00
|
|
|
if(!hw->sim)
|
|
|
|
|
poll(NULL, 0, 20);
|
2026-11-10 20:56:42 +02:00
|
|
|
hw_reset_set(hw, hw->reset_idle_level);
|
2026-11-10 19:41:57 +02:00
|
|
|
if(!hw->sim)
|
|
|
|
|
poll(NULL, 0, 150);
|
|
|
|
|
if(!hw_init_panel(hw))
|
|
|
|
|
return 0;
|
|
|
|
|
hw->initialized = 1;
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int hw_frame(struct cover_hw *hw, const struct frame *fb, int lit)
|
|
|
|
|
{
|
|
|
|
|
unsigned char cmd = 0x2c;
|
|
|
|
|
size_t size = (size_t)fb->stride * fb->height;
|
|
|
|
|
size_t left = size;
|
|
|
|
|
const unsigned char *pixels = fb->pixels;
|
|
|
|
|
char line[96];
|
|
|
|
|
snprintf(line, sizeof(line), "frame lit=%d size=%zu", lit, size);
|
|
|
|
|
hw_event(hw, line);
|
|
|
|
|
if(hw->sim)
|
|
|
|
|
return 1;
|
2026-11-10 20:56:42 +02:00
|
|
|
if(hw->word_bits == 9) {
|
|
|
|
|
unsigned char start[2];
|
|
|
|
|
unsigned char *words;
|
|
|
|
|
size_t i;
|
|
|
|
|
size_t left;
|
|
|
|
|
const unsigned char *p;
|
|
|
|
|
start[0] = 0x2c;
|
|
|
|
|
start[1] = 0;
|
|
|
|
|
if(!hw_xfer(hw, start, 2, 0))
|
|
|
|
|
return 0;
|
|
|
|
|
words = malloc(size * 2);
|
|
|
|
|
if(!words)
|
|
|
|
|
return 0;
|
|
|
|
|
for(i = 0; i < size; i++) {
|
|
|
|
|
words[i * 2] = fb->pixels[i];
|
|
|
|
|
words[i * 2 + 1] = 1;
|
|
|
|
|
}
|
|
|
|
|
p = words;
|
|
|
|
|
left = size * 2;
|
|
|
|
|
while(left) {
|
|
|
|
|
size_t chunk = left > COVER_SPI_CHUNK ? COVER_SPI_CHUNK : left;
|
|
|
|
|
if(!hw_xfer(hw, p, chunk, chunk == left)) {
|
|
|
|
|
free(words);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
p += chunk;
|
|
|
|
|
left -= chunk;
|
|
|
|
|
}
|
|
|
|
|
free(words);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2026-11-10 19:41:57 +02:00
|
|
|
if(!hw_dc(hw, hw->dc_command_level))
|
|
|
|
|
return 0;
|
|
|
|
|
if(!hw_xfer(hw, &cmd, 1, 0))
|
|
|
|
|
return 0;
|
|
|
|
|
if(!hw_dc(hw, hw->dc_data_level))
|
|
|
|
|
return 0;
|
|
|
|
|
while(left) {
|
|
|
|
|
size_t chunk = left > COVER_SPI_CHUNK ? COVER_SPI_CHUNK : left;
|
|
|
|
|
if(!hw_xfer(hw, pixels, chunk, chunk == left))
|
|
|
|
|
return 0;
|
|
|
|
|
pixels += chunk;
|
|
|
|
|
left -= chunk;
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int cover_spi_draw(struct cover_hw *hw, const struct state *state)
|
|
|
|
|
{
|
|
|
|
|
size_t size = COVER_SPI_WIDTH * COVER_SPI_HEIGHT * 2;
|
|
|
|
|
unsigned char *mem;
|
|
|
|
|
struct frame fb;
|
|
|
|
|
int lit = cover_is_lit(state);
|
|
|
|
|
if(!hw_activate(hw))
|
|
|
|
|
return 0;
|
|
|
|
|
mem = calloc(1, size);
|
|
|
|
|
if(!mem)
|
|
|
|
|
return 0;
|
|
|
|
|
fb.pixels = mem;
|
|
|
|
|
fb.width = COVER_SPI_WIDTH;
|
|
|
|
|
fb.height = COVER_SPI_HEIGHT;
|
|
|
|
|
fb.stride = COVER_SPI_WIDTH * 2;
|
|
|
|
|
fb.bpp = 16;
|
|
|
|
|
render_frame(&fb, state);
|
|
|
|
|
if(lit)
|
|
|
|
|
hw_command(hw, 0x29, NULL, 0);
|
|
|
|
|
if(!hw_frame(hw, &fb, lit)) {
|
|
|
|
|
free(mem);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if(!lit)
|
|
|
|
|
hw_command(hw, 0x28, NULL, 0);
|
|
|
|
|
free(mem);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-11 07:57:38 +02:00
|
|
|
static void cover_kernel_release(struct cover_hw *hw, int open);
|
2026-11-10 23:20:13 +02:00
|
|
|
|
2026-11-10 19:41:57 +02:00
|
|
|
/* On an open flip the daemon must not own the shared QUP5 bus at all. The
|
|
|
|
|
* mux line stays claimed for the daemon's lifetime (so it is always driven
|
|
|
|
|
* back to the internal panel), while the DC/reset handle is released. */
|
|
|
|
|
static void cover_hw_release(struct cover_hw *hw)
|
|
|
|
|
{
|
|
|
|
|
unsigned char mux_value[1];
|
2026-11-10 23:20:13 +02:00
|
|
|
if(hw->kernel_owned) {
|
2026-11-11 07:57:38 +02:00
|
|
|
cover_kernel_release(hw, 0);
|
2026-11-10 23:20:13 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2026-11-10 19:41:57 +02:00
|
|
|
if(!hw->mux_claimed)
|
|
|
|
|
return;
|
|
|
|
|
if(hw->initialized)
|
|
|
|
|
hw_command(hw, 0x28, NULL, 0);
|
|
|
|
|
mux_value[0] = (unsigned char)hw->mux_open_level;
|
|
|
|
|
hw_gpio_set(hw, hw->fd_mux, mux_value, 1);
|
|
|
|
|
hw_event(hw, "release");
|
|
|
|
|
if(hw->claimed && !hw->sim)
|
|
|
|
|
close(hw->fd_panel);
|
|
|
|
|
hw->fd_panel = -1;
|
|
|
|
|
hw->claimed = 0;
|
|
|
|
|
hw->initialized = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void cover_hw_shutdown(struct cover_hw *hw)
|
|
|
|
|
{
|
|
|
|
|
cover_hw_release(hw);
|
2026-11-11 07:57:38 +02:00
|
|
|
cover_kernel_release(hw, 0);
|
2026-11-10 19:41:57 +02:00
|
|
|
if(!hw->sim) {
|
|
|
|
|
if(hw->fd_mux >= 0)
|
|
|
|
|
close(hw->fd_mux);
|
|
|
|
|
if(hw->fd_spi >= 0)
|
|
|
|
|
close(hw->fd_spi);
|
|
|
|
|
if(hw->fd_chip >= 0)
|
|
|
|
|
close(hw->fd_chip);
|
|
|
|
|
}
|
|
|
|
|
hw->fd_mux = -1;
|
|
|
|
|
hw->fd_spi = -1;
|
|
|
|
|
hw->fd_chip = -1;
|
|
|
|
|
if(hw->fd_sim >= 0) {
|
|
|
|
|
hw_event(hw, "close");
|
|
|
|
|
close(hw->fd_sim);
|
|
|
|
|
hw->fd_sim = -1;
|
|
|
|
|
}
|
|
|
|
|
hw->mux_claimed = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* The cover may only own QUP5 while the main panel is DPMS-off. This is the
|
|
|
|
|
* real closed-flip state and also rejects a transient lid-state flap at lid
|
|
|
|
|
* daemon startup, when Xorg is still flushing frames to the internal panel. */
|
|
|
|
|
static int cover_main_panel_is_off(void)
|
|
|
|
|
{
|
|
|
|
|
const char *path = getenv("BANANARCHY_COVER_DPMS_PATH");
|
|
|
|
|
char buf[16];
|
|
|
|
|
ssize_t got;
|
|
|
|
|
int fd;
|
|
|
|
|
if(!path || !*path)
|
|
|
|
|
path = MAIN_DPMS_PATH;
|
|
|
|
|
fd = open(path, O_RDONLY | O_CLOEXEC);
|
|
|
|
|
if(fd < 0)
|
|
|
|
|
return 1;
|
|
|
|
|
got = read(fd, buf, sizeof(buf) - 1);
|
|
|
|
|
close(fd);
|
|
|
|
|
if(got <= 0)
|
|
|
|
|
return 1;
|
|
|
|
|
buf[got] = '\0';
|
|
|
|
|
return strncmp(buf, "Off", 3) == 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-10 21:54:53 +02:00
|
|
|
/* --- Kernel-owned cover path ----------------------------------------------
|
2026-11-11 07:57:38 +02:00
|
|
|
* The KaiOS 3.10 DTS shows that both GC9305s share CS0, gpio110 D/C and one
|
|
|
|
|
* PMIC MPP2 PWM backlight; gpio13 selects the panel, gpio23 is the external
|
|
|
|
|
* reset and gpio21 is TE. Live probing showed that the shared PWM is
|
|
|
|
|
* active-low for the cover backlight and active-high for the main backlight:
|
|
|
|
|
* duty 0 lights the cover, duty 14 lights the internal panel. The daemon
|
|
|
|
|
* therefore publishes a cover marker and owns the backlight state while the
|
|
|
|
|
* kernel driver owns CS0 and gpio110.
|
2026-11-10 21:54:53 +02:00
|
|
|
*/
|
2026-11-11 07:57:38 +02:00
|
|
|
static void cover_kernel_release(struct cover_hw *hw, int open);
|
|
|
|
|
|
2026-11-10 21:54:53 +02:00
|
|
|
static void hw_dpms(struct cover_hw *hw, const char *mode)
|
|
|
|
|
{
|
|
|
|
|
char line[64];
|
|
|
|
|
if(hw->sim) {
|
|
|
|
|
snprintf(line, sizeof(line), "dpms %s", mode);
|
|
|
|
|
hw_event(hw, line);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if(!hw->kernel_dpms[0])
|
|
|
|
|
return;
|
|
|
|
|
{
|
|
|
|
|
char *const argv[] = { hw->kernel_dpms, (char *)mode, NULL };
|
|
|
|
|
daemon_spawn_env(argv, (char *const *)session_env);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-11 07:57:38 +02:00
|
|
|
static void cover_kernel_power_sync(struct cover_hw *hw)
|
|
|
|
|
{
|
|
|
|
|
char line[64];
|
|
|
|
|
if(hw->sim) {
|
|
|
|
|
snprintf(line, sizeof(line), "kernel power sync");
|
|
|
|
|
hw_event(hw, line);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if(!hw->kernel_sync[0])
|
|
|
|
|
return;
|
|
|
|
|
{
|
2026-11-11 08:23:12 +02:00
|
|
|
char *const argv[] = { hw->kernel_sync, (char *)"force", NULL };
|
2026-11-11 07:57:38 +02:00
|
|
|
daemon_spawn_env(argv, (char *const *)session_env);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int cover_text_equals(const char *path, const char *value)
|
|
|
|
|
{
|
|
|
|
|
char buf[64];
|
|
|
|
|
ssize_t got;
|
|
|
|
|
size_t len;
|
|
|
|
|
int fd = open(path, O_RDONLY | O_CLOEXEC);
|
|
|
|
|
if(fd < 0)
|
|
|
|
|
return 0;
|
|
|
|
|
got = read(fd, buf, sizeof(buf) - 1);
|
|
|
|
|
close(fd);
|
|
|
|
|
if(got <= 0)
|
|
|
|
|
return 0;
|
|
|
|
|
buf[got] = '\0';
|
|
|
|
|
while(got > 0 && (buf[got - 1] == '\n' || buf[got - 1] == '\r' ||
|
|
|
|
|
buf[got - 1] == ' '))
|
|
|
|
|
buf[--got] = '\0';
|
|
|
|
|
len = strlen(value);
|
|
|
|
|
return strlen(buf) == len && memcmp(buf, value, len) == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void write_cover_text_once(const char *path, const char *value)
|
|
|
|
|
{
|
|
|
|
|
if(!cover_text_equals(path, value))
|
|
|
|
|
write_cover_text(path, value);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-10 23:20:13 +02:00
|
|
|
static int cover_kernel_command(struct cover_hw *hw, unsigned char cmd,
|
|
|
|
|
const unsigned char *params,
|
|
|
|
|
unsigned int count)
|
|
|
|
|
{
|
|
|
|
|
char line[256];
|
|
|
|
|
char event[320];
|
|
|
|
|
size_t used;
|
|
|
|
|
unsigned int i;
|
|
|
|
|
int flags = O_WRONLY | O_CLOEXEC;
|
|
|
|
|
int fd;
|
|
|
|
|
used = (size_t)snprintf(line, sizeof(line), "%02x", cmd);
|
|
|
|
|
for(i = 0; i < count && used + 4 < sizeof(line); i++)
|
|
|
|
|
used += (size_t)snprintf(line + used, sizeof(line) - used,
|
|
|
|
|
" %02x", params[i]);
|
|
|
|
|
snprintf(event, sizeof(event), "kernel cmd %s", line);
|
|
|
|
|
hw_event(hw, event);
|
|
|
|
|
if(hw->sim)
|
|
|
|
|
flags |= O_TRUNC;
|
|
|
|
|
fd = open(hw->kernel_cmd, flags);
|
|
|
|
|
if(fd < 0)
|
|
|
|
|
return hw->sim;
|
|
|
|
|
if(!write_all(fd, (const unsigned char *)line, strlen(line))) {
|
|
|
|
|
close(fd);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
close(fd);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int cover_kernel_replay_firmware(struct cover_hw *hw)
|
|
|
|
|
{
|
|
|
|
|
unsigned char buf[256];
|
|
|
|
|
size_t len;
|
|
|
|
|
size_t i = 16;
|
|
|
|
|
FILE *handle = fopen(hw->firmware, "rb");
|
|
|
|
|
if(!handle)
|
|
|
|
|
return 0;
|
|
|
|
|
len = fread(buf, 1, sizeof(buf), handle);
|
|
|
|
|
fclose(handle);
|
|
|
|
|
if(len < 18 || memcmp(buf, "MIPI DBI", 8) != 0 || buf[15] != 1)
|
|
|
|
|
return 0;
|
|
|
|
|
while(i + 1 < len) {
|
|
|
|
|
unsigned char cmd = buf[i++];
|
|
|
|
|
unsigned char count = buf[i++];
|
|
|
|
|
if(i + count > len)
|
|
|
|
|
return 0;
|
|
|
|
|
if(cmd == 0x00 && count == 1) {
|
|
|
|
|
char line[64];
|
|
|
|
|
snprintf(line, sizeof(line), "kernel delay %u",
|
|
|
|
|
(unsigned int)buf[i]);
|
|
|
|
|
hw_event(hw, line);
|
|
|
|
|
if(!hw->sim)
|
|
|
|
|
poll(NULL, 0, (int)buf[i]);
|
|
|
|
|
} else if(!cover_kernel_command(hw, cmd, buf + i, count)) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
i += count;
|
|
|
|
|
}
|
|
|
|
|
return i == len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void cover_kernel_reset(struct cover_hw *hw)
|
|
|
|
|
{
|
|
|
|
|
unsigned char values[2];
|
|
|
|
|
values[0] = (unsigned char)hw->mux_closed_level;
|
|
|
|
|
values[1] = (unsigned char)hw->reset_idle_level;
|
|
|
|
|
hw_gpio_set(hw, hw->fd_mux, values, 2);
|
|
|
|
|
if(!hw->sim)
|
|
|
|
|
poll(NULL, 0, 1);
|
|
|
|
|
values[1] = (unsigned char)hw->reset_assert_level;
|
|
|
|
|
hw_gpio_set(hw, hw->fd_mux, values, 2);
|
|
|
|
|
if(!hw->sim)
|
|
|
|
|
poll(NULL, 0, 10);
|
|
|
|
|
values[1] = (unsigned char)hw->reset_idle_level;
|
|
|
|
|
hw_gpio_set(hw, hw->fd_mux, values, 2);
|
|
|
|
|
if(!hw->sim)
|
|
|
|
|
poll(NULL, 0, 10);
|
|
|
|
|
hw_event(hw, "kernel reset");
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-11 07:57:38 +02:00
|
|
|
static void cover_kernel_marker(struct cover_hw *hw, const char *value)
|
|
|
|
|
{
|
|
|
|
|
char line[64];
|
|
|
|
|
if(value)
|
|
|
|
|
write_cover_text_once(hw->kernel_marker, value);
|
|
|
|
|
else
|
|
|
|
|
unlink(hw->kernel_marker);
|
|
|
|
|
snprintf(line, sizeof(line), "kernel marker %s",
|
|
|
|
|
value ? value : "absent");
|
|
|
|
|
hw_event(hw, line);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void cover_kernel_backlight(struct cover_hw *hw, int lit)
|
2026-11-10 23:20:13 +02:00
|
|
|
{
|
|
|
|
|
char value[32];
|
|
|
|
|
char line[64];
|
2026-11-11 07:57:38 +02:00
|
|
|
unsigned int level;
|
|
|
|
|
if(lit) {
|
|
|
|
|
level = hw->kernel_bl_invert ? 0 : hw->kernel_bl_level;
|
|
|
|
|
cover_kernel_marker(hw, "1");
|
|
|
|
|
} else {
|
|
|
|
|
level = hw->kernel_bl_invert ? hw->kernel_bl_level : 0;
|
|
|
|
|
cover_kernel_marker(hw, "0");
|
|
|
|
|
}
|
2026-11-10 23:20:13 +02:00
|
|
|
snprintf(value, sizeof(value), "%u", level);
|
2026-11-11 07:57:38 +02:00
|
|
|
/* Writing bl_power first and brightness unconditionally forces the PWM
|
|
|
|
|
* duty to update even when the numeric value is unchanged. The udev helper
|
|
|
|
|
* skips writes once both files already match, so this cannot loop. */
|
|
|
|
|
write_cover_text_once(hw->kernel_bl_power, "0");
|
2026-11-10 23:20:13 +02:00
|
|
|
write_cover_text(hw->kernel_bl, value);
|
2026-11-11 07:57:38 +02:00
|
|
|
snprintf(line, sizeof(line), "kernel backlight %u power=0", level);
|
2026-11-10 23:20:13 +02:00
|
|
|
hw_event(hw, line);
|
2026-11-11 07:57:38 +02:00
|
|
|
cover_kernel_power_sync(hw);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void cover_kernel_open_backlight(struct cover_hw *hw)
|
|
|
|
|
{
|
|
|
|
|
char value[32];
|
|
|
|
|
unlink(hw->kernel_marker);
|
|
|
|
|
snprintf(value, sizeof(value), "%u", hw->kernel_bl_level);
|
|
|
|
|
write_cover_text_once(hw->kernel_bl_power, "0");
|
|
|
|
|
write_cover_text(hw->kernel_bl, value);
|
|
|
|
|
hw_event(hw, "kernel restore internal");
|
|
|
|
|
cover_kernel_power_sync(hw);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int cover_kernel_mux(struct cover_hw *hw, int closed)
|
|
|
|
|
{
|
|
|
|
|
unsigned char values[2];
|
|
|
|
|
values[0] = (unsigned char)(closed ? hw->mux_closed_level :
|
|
|
|
|
hw->mux_open_level);
|
|
|
|
|
values[1] = (unsigned char)hw->reset_idle_level;
|
|
|
|
|
return hw_gpio_set(hw, hw->fd_mux, values, 2);
|
2026-11-10 23:20:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void cover_kernel_display_off(struct cover_hw *hw)
|
|
|
|
|
{
|
|
|
|
|
cover_kernel_command(hw, 0x28, NULL, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-10 21:54:53 +02:00
|
|
|
static int cover_kernel_claim(struct cover_hw *hw)
|
|
|
|
|
{
|
|
|
|
|
unsigned int offsets[2];
|
|
|
|
|
unsigned char defaults[2];
|
|
|
|
|
int fd;
|
|
|
|
|
if(hw->fd_mux >= 0 || hw->mux_claimed)
|
|
|
|
|
return 1;
|
|
|
|
|
offsets[0] = hw->mux_gpio;
|
|
|
|
|
offsets[1] = hw->reset_gpio;
|
|
|
|
|
defaults[0] = (unsigned char)hw->mux_open_level;
|
|
|
|
|
defaults[1] = (unsigned char)hw->reset_idle_level;
|
|
|
|
|
fd = hw_gpio_request(hw, offsets, defaults, 2);
|
|
|
|
|
if(fd < 0)
|
|
|
|
|
return 0;
|
|
|
|
|
hw->fd_mux = fd;
|
|
|
|
|
hw->mux_claimed = 1;
|
2026-11-10 23:20:13 +02:00
|
|
|
hw->kernel_owned = 1;
|
2026-11-11 07:57:38 +02:00
|
|
|
hw->kernel_restored = 0;
|
|
|
|
|
return cover_kernel_mux(hw, 1);
|
2026-11-10 21:54:53 +02:00
|
|
|
}
|
|
|
|
|
|
2026-11-11 07:57:38 +02:00
|
|
|
static void cover_kernel_release(struct cover_hw *hw, int open)
|
2026-11-10 21:54:53 +02:00
|
|
|
{
|
2026-11-10 23:20:13 +02:00
|
|
|
if(!hw->kernel_owned)
|
2026-11-10 21:54:53 +02:00
|
|
|
return;
|
2026-11-11 07:57:38 +02:00
|
|
|
if(!open)
|
|
|
|
|
cover_kernel_backlight(hw, 0);
|
|
|
|
|
cover_kernel_mux(hw, 0);
|
|
|
|
|
unlink(hw->kernel_marker);
|
|
|
|
|
if(open) {
|
|
|
|
|
hw_dpms(hw, "reinit");
|
|
|
|
|
cover_kernel_open_backlight(hw);
|
|
|
|
|
hw_dpms(hw, "on");
|
|
|
|
|
cover_kernel_power_sync(hw);
|
|
|
|
|
hw->kernel_restored = 1;
|
|
|
|
|
} else {
|
|
|
|
|
write_cover_text_once(hw->kernel_bl_power, "4");
|
|
|
|
|
write_cover_text(hw->kernel_bl, "0");
|
|
|
|
|
hw_dpms(hw, "off");
|
|
|
|
|
hw->kernel_restored = 0;
|
|
|
|
|
}
|
2026-11-10 21:54:53 +02:00
|
|
|
hw_event(hw, "release");
|
|
|
|
|
if(!hw->sim && hw->fd_mux >= 0)
|
|
|
|
|
close(hw->fd_mux);
|
|
|
|
|
hw->fd_mux = -1;
|
|
|
|
|
hw->mux_claimed = 0;
|
2026-11-10 23:20:13 +02:00
|
|
|
hw->kernel_owned = 0;
|
2026-11-10 21:54:53 +02:00
|
|
|
hw->kernel_lit = -1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-10 23:20:13 +02:00
|
|
|
static void cover_bus_release(struct cover_hw *hw)
|
|
|
|
|
{
|
|
|
|
|
if(hw->kernel_owned) {
|
2026-11-11 07:57:38 +02:00
|
|
|
cover_kernel_release(hw, 0);
|
2026-11-10 23:20:13 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if(!hw->mux_claimed)
|
|
|
|
|
return;
|
|
|
|
|
cover_hw_release(hw);
|
|
|
|
|
if(!hw->sim && hw->fd_mux >= 0)
|
|
|
|
|
close(hw->fd_mux);
|
|
|
|
|
hw->fd_mux = -1;
|
|
|
|
|
hw->mux_claimed = 0;
|
2026-11-11 07:57:38 +02:00
|
|
|
hw->kernel_restored = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void cover_kernel_restore_internal(struct cover_hw *hw)
|
|
|
|
|
{
|
|
|
|
|
unsigned int offsets[1];
|
|
|
|
|
unsigned char defaults[1];
|
|
|
|
|
unsigned char values[1];
|
|
|
|
|
int fd;
|
|
|
|
|
if(hw->kernel_restored)
|
|
|
|
|
return;
|
|
|
|
|
if(hw->kernel_owned) {
|
|
|
|
|
cover_kernel_release(hw, 1);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if(hw->mux_claimed || hw->fd_mux >= 0) {
|
|
|
|
|
cover_bus_release(hw);
|
|
|
|
|
} else {
|
|
|
|
|
offsets[0] = hw->mux_gpio;
|
|
|
|
|
defaults[0] = (unsigned char)hw->mux_open_level;
|
|
|
|
|
fd = hw_gpio_request(hw, offsets, defaults, 1);
|
|
|
|
|
if(fd >= 0) {
|
|
|
|
|
values[0] = (unsigned char)hw->mux_open_level;
|
|
|
|
|
hw_gpio_set(hw, fd, values, 1);
|
|
|
|
|
if(!hw->sim)
|
|
|
|
|
close(fd);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
hw_dpms(hw, "reinit");
|
|
|
|
|
cover_kernel_open_backlight(hw);
|
|
|
|
|
hw_dpms(hw, "on");
|
|
|
|
|
cover_kernel_power_sync(hw);
|
|
|
|
|
hw->kernel_restored = 1;
|
2026-11-10 23:20:13 +02:00
|
|
|
}
|
|
|
|
|
|
2026-11-10 21:54:53 +02:00
|
|
|
static int cover_kernel_frame(struct cover_hw *hw, const struct state *state)
|
|
|
|
|
{
|
|
|
|
|
unsigned int width = COVER_SPI_WIDTH;
|
|
|
|
|
unsigned int height = COVER_SPI_HEIGHT;
|
|
|
|
|
unsigned int stride = width * 4;
|
|
|
|
|
char sysfs[128];
|
|
|
|
|
unsigned int parsed = 0;
|
|
|
|
|
size_t size;
|
|
|
|
|
unsigned char *mem;
|
|
|
|
|
struct frame fb;
|
|
|
|
|
int fd;
|
|
|
|
|
char line[96];
|
|
|
|
|
snprintf(sysfs, sizeof(sysfs), "/sys/class/graphics/fb0/stride");
|
|
|
|
|
if(read_uint_file(sysfs, &parsed) && parsed >= width * 4)
|
|
|
|
|
stride = parsed;
|
|
|
|
|
size = (size_t)stride * height;
|
|
|
|
|
mem = calloc(1, size);
|
|
|
|
|
if(!mem)
|
|
|
|
|
return 0;
|
|
|
|
|
fb.pixels = mem;
|
|
|
|
|
fb.width = width;
|
|
|
|
|
fb.height = height;
|
|
|
|
|
fb.stride = stride;
|
|
|
|
|
fb.bpp = 32;
|
|
|
|
|
render_frame(&fb, state);
|
|
|
|
|
snprintf(line, sizeof(line), "kernel frame lit=%d size=%zu",
|
|
|
|
|
cover_is_lit(state), size);
|
|
|
|
|
hw_event(hw, line);
|
|
|
|
|
fd = open(hw->kernel_fb, O_RDWR | O_CLOEXEC);
|
|
|
|
|
if(fd < 0) {
|
|
|
|
|
free(mem);
|
|
|
|
|
return hw->sim;
|
|
|
|
|
}
|
|
|
|
|
if(lseek(fd, 0, SEEK_SET) < 0 || !write_all(fd, mem, size)) {
|
|
|
|
|
free(mem);
|
|
|
|
|
close(fd);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
fsync(fd);
|
|
|
|
|
close(fd);
|
|
|
|
|
free(mem);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const char *cover_kernel_draw(struct cover_hw *hw,
|
|
|
|
|
const struct state *state)
|
|
|
|
|
{
|
|
|
|
|
int lit = cover_is_lit(state);
|
|
|
|
|
if(!cover_kernel_claim(hw))
|
|
|
|
|
return "none";
|
|
|
|
|
if(lit != hw->kernel_lit) {
|
|
|
|
|
if(lit) {
|
2026-11-11 07:57:38 +02:00
|
|
|
cover_kernel_backlight(hw, 1);
|
|
|
|
|
hw_dpms(hw, "on");
|
2026-11-10 21:54:53 +02:00
|
|
|
hw_dpms(hw, "reinit");
|
|
|
|
|
if(!hw->sim)
|
|
|
|
|
poll(NULL, 0, 180);
|
2026-11-11 07:57:38 +02:00
|
|
|
cover_kernel_mux(hw, 1);
|
2026-11-10 23:20:13 +02:00
|
|
|
cover_kernel_reset(hw);
|
|
|
|
|
if(!cover_kernel_replay_firmware(hw))
|
|
|
|
|
return "none";
|
2026-11-11 07:57:38 +02:00
|
|
|
/* DPMS off/on can rewrite bl_power, so force the active-low duty again. */
|
2026-11-10 23:20:13 +02:00
|
|
|
cover_kernel_backlight(hw, 1);
|
2026-11-11 07:57:38 +02:00
|
|
|
hw_dpms(hw, "on");
|
|
|
|
|
cover_kernel_power_sync(hw);
|
2026-11-10 23:20:13 +02:00
|
|
|
if(!hw->sim)
|
|
|
|
|
poll(NULL, 0, 50);
|
2026-11-10 21:54:53 +02:00
|
|
|
} else {
|
2026-11-10 23:20:13 +02:00
|
|
|
if(hw->kernel_lit > 0)
|
|
|
|
|
cover_kernel_display_off(hw);
|
|
|
|
|
cover_kernel_backlight(hw, 0);
|
2026-11-11 07:57:38 +02:00
|
|
|
hw_dpms(hw, "reinit");
|
|
|
|
|
if(!hw->sim)
|
|
|
|
|
poll(NULL, 0, 180);
|
|
|
|
|
cover_kernel_display_off(hw);
|
|
|
|
|
cover_kernel_backlight(hw, 0);
|
|
|
|
|
hw_dpms(hw, "on");
|
|
|
|
|
cover_kernel_power_sync(hw);
|
2026-11-10 21:54:53 +02:00
|
|
|
}
|
|
|
|
|
hw->kernel_lit = lit;
|
|
|
|
|
}
|
|
|
|
|
if(lit && !cover_kernel_frame(hw, state))
|
|
|
|
|
return "none";
|
|
|
|
|
return lit ? "kernel" : "kernel-dark";
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-10 19:41:57 +02:00
|
|
|
static const char *cover_draw(struct cover_hw *hw, const char *fb_path,
|
|
|
|
|
const struct state *state)
|
|
|
|
|
{
|
|
|
|
|
int flip_open = strcmp(state->lid, "open") == 0;
|
|
|
|
|
int fb_present = access(fb_path, W_OK) == 0;
|
|
|
|
|
int prefer_fb = strcmp(hw->backend, "spidev") != 0 &&
|
|
|
|
|
strcmp(hw->backend, "none") != 0 && fb_present;
|
|
|
|
|
if(flip_open) {
|
2026-11-11 07:57:38 +02:00
|
|
|
if(strcmp(hw->backend, "kernel") == 0)
|
|
|
|
|
cover_kernel_restore_internal(hw);
|
|
|
|
|
else {
|
|
|
|
|
cover_hw_release(hw);
|
|
|
|
|
cover_kernel_release(hw, 0);
|
|
|
|
|
}
|
2026-11-10 19:41:57 +02:00
|
|
|
if(prefer_fb)
|
|
|
|
|
return draw_frame(fb_path, state) ? "fb" : "none";
|
|
|
|
|
return "idle";
|
|
|
|
|
}
|
|
|
|
|
if(strcmp(hw->backend, "none") == 0) {
|
2026-11-10 23:20:13 +02:00
|
|
|
cover_bus_release(hw);
|
2026-11-10 19:41:57 +02:00
|
|
|
return "none";
|
|
|
|
|
}
|
2026-11-10 21:54:53 +02:00
|
|
|
if(strcmp(hw->backend, "kernel") == 0) {
|
2026-11-10 23:20:13 +02:00
|
|
|
/* A runtime backend switch must hand over the spidev linehandle first. */
|
|
|
|
|
if(hw->mux_claimed && !hw->kernel_owned)
|
|
|
|
|
cover_bus_release(hw);
|
|
|
|
|
/* Take the shared bus only after the lid daemon has stopped Xorg's
|
|
|
|
|
* main-panel flushes. Once the cover owns it, DPMS is intentionally On
|
|
|
|
|
* so the kernel driver can send frames. */
|
|
|
|
|
if(hw->kernel_lit < 0 && !cover_main_panel_is_off()) {
|
2026-11-11 07:57:38 +02:00
|
|
|
cover_kernel_release(hw, 0);
|
2026-11-10 21:54:53 +02:00
|
|
|
return "pending";
|
|
|
|
|
}
|
|
|
|
|
return cover_kernel_draw(hw, state);
|
|
|
|
|
}
|
2026-11-10 19:41:57 +02:00
|
|
|
if(prefer_fb) {
|
|
|
|
|
cover_hw_release(hw);
|
|
|
|
|
return draw_frame(fb_path, state) ? "fb" : "none";
|
|
|
|
|
}
|
|
|
|
|
if(hw->spi_enabled) {
|
|
|
|
|
if(!cover_main_panel_is_off()) {
|
|
|
|
|
cover_hw_release(hw);
|
|
|
|
|
return "pending";
|
|
|
|
|
}
|
|
|
|
|
return cover_spi_draw(hw, state) ? "spidev" : "none";
|
|
|
|
|
}
|
|
|
|
|
cover_hw_release(hw);
|
|
|
|
|
return "none";
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-09 22:50:51 +02:00
|
|
|
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-10 19:41:57 +02:00
|
|
|
static int config_level(const char *value, int fallback)
|
|
|
|
|
{
|
|
|
|
|
long v = strtol(value, NULL, 10);
|
|
|
|
|
if(v < 0 || v > 1)
|
|
|
|
|
return fallback;
|
|
|
|
|
return (int)v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void cover_hw_init(struct cover_hw *hw)
|
|
|
|
|
{
|
|
|
|
|
const char *sim;
|
|
|
|
|
memset(hw, 0, sizeof(*hw));
|
|
|
|
|
safe_copy(hw->backend, sizeof(hw->backend), "auto");
|
|
|
|
|
hw->mux_gpio = 13;
|
|
|
|
|
hw->dc_gpio = 21;
|
|
|
|
|
hw->reset_gpio = 23;
|
|
|
|
|
/* The live phone holds gpio13 low while the 240x320 main panel is active,
|
|
|
|
|
* so low selects the internal panel and high must select the cover. */
|
|
|
|
|
hw->mux_open_level = 0;
|
|
|
|
|
hw->mux_closed_level = 1;
|
|
|
|
|
hw->dc_command_level = 0;
|
|
|
|
|
hw->dc_data_level = 1;
|
|
|
|
|
hw->reset_assert_level = 0;
|
|
|
|
|
hw->reset_idle_level = 1;
|
|
|
|
|
hw->speed_hz = 25000000;
|
2026-11-10 20:56:42 +02:00
|
|
|
hw->word_bits = 9;
|
|
|
|
|
hw->panel_lines = 1;
|
|
|
|
|
hw->probe_index = -1;
|
2026-11-10 21:54:53 +02:00
|
|
|
hw->kernel_lit = -1;
|
2026-11-11 07:57:38 +02:00
|
|
|
hw->kernel_restored = 0;
|
2026-11-10 23:20:13 +02:00
|
|
|
safe_copy(hw->spi_path, sizeof(hw->spi_path), "/dev/spidev0.1");
|
|
|
|
|
safe_copy(hw->gpio_path, sizeof(hw->gpio_path), "/dev/gpiochip0");
|
|
|
|
|
safe_copy(hw->firmware, sizeof(hw->firmware),
|
|
|
|
|
"/lib/firmware/nokia,beatles-gc9305-ext-panel.bin");
|
2026-11-10 21:54:53 +02:00
|
|
|
safe_copy(hw->kernel_fb, sizeof(hw->kernel_fb), "/dev/fb0");
|
|
|
|
|
safe_copy(hw->kernel_dpms, sizeof(hw->kernel_dpms),
|
|
|
|
|
"/usr/bin/bananarchy-cover-dpms.sh");
|
2026-11-11 07:57:38 +02:00
|
|
|
safe_copy(hw->kernel_sync, sizeof(hw->kernel_sync),
|
|
|
|
|
"/usr/bin/bananarchy-display-power-sync");
|
2026-11-10 23:20:13 +02:00
|
|
|
safe_copy(hw->kernel_cmd, sizeof(hw->kernel_cmd),
|
|
|
|
|
"/sys/kernel/debug/dri/0/command");
|
|
|
|
|
safe_copy(hw->kernel_bl, sizeof(hw->kernel_bl),
|
|
|
|
|
"/sys/class/backlight/backlight/brightness");
|
2026-11-11 07:57:38 +02:00
|
|
|
safe_copy(hw->kernel_bl_power, sizeof(hw->kernel_bl_power),
|
|
|
|
|
"/sys/class/backlight/backlight/bl_power");
|
2026-11-10 23:20:13 +02:00
|
|
|
safe_copy(hw->kernel_marker, sizeof(hw->kernel_marker),
|
|
|
|
|
"/run/bananarchy/cover-backlight");
|
|
|
|
|
hw->kernel_bl_level = 14;
|
2026-11-11 07:57:38 +02:00
|
|
|
hw->kernel_bl_invert = 1;
|
2026-11-10 20:56:42 +02:00
|
|
|
safe_copy(hw->probe_desc, sizeof(hw->probe_desc), "none");
|
2026-11-10 19:41:57 +02:00
|
|
|
hw->fd_spi = -1;
|
|
|
|
|
hw->fd_chip = -1;
|
|
|
|
|
hw->fd_mux = -1;
|
|
|
|
|
hw->fd_panel = -1;
|
|
|
|
|
hw->fd_sim = -1;
|
2026-11-10 21:54:53 +02:00
|
|
|
{
|
|
|
|
|
const char *fb = getenv("BANANARCHY_COVER_MAIN_FB");
|
|
|
|
|
const char *dp = getenv("BANANARCHY_COVER_DPMS_HELPER");
|
2026-11-11 07:57:38 +02:00
|
|
|
const char *sync = getenv("BANANARCHY_COVER_KERNEL_POWER_SYNC");
|
2026-11-10 23:20:13 +02:00
|
|
|
const char *cmd = getenv("BANANARCHY_COVER_KERNEL_CMD");
|
|
|
|
|
const char *bl = getenv("BANANARCHY_COVER_KERNEL_BL");
|
2026-11-11 07:57:38 +02:00
|
|
|
const char *power = getenv("BANANARCHY_COVER_KERNEL_BL_POWER");
|
2026-11-10 23:20:13 +02:00
|
|
|
const char *marker = getenv("BANANARCHY_COVER_KERNEL_MARKER");
|
|
|
|
|
const char *level = getenv("BANANARCHY_COVER_KERNEL_BL_LEVEL");
|
2026-11-11 07:57:38 +02:00
|
|
|
const char *invert = getenv("BANANARCHY_COVER_KERNEL_BL_INVERT");
|
2026-11-10 21:54:53 +02:00
|
|
|
if(fb && *fb)
|
|
|
|
|
safe_copy(hw->kernel_fb, sizeof(hw->kernel_fb), fb);
|
|
|
|
|
if(dp && *dp)
|
|
|
|
|
safe_copy(hw->kernel_dpms, sizeof(hw->kernel_dpms), dp);
|
2026-11-11 07:57:38 +02:00
|
|
|
if(sync && *sync)
|
|
|
|
|
safe_copy(hw->kernel_sync, sizeof(hw->kernel_sync), sync);
|
2026-11-10 23:20:13 +02:00
|
|
|
if(cmd && *cmd)
|
|
|
|
|
safe_copy(hw->kernel_cmd, sizeof(hw->kernel_cmd), cmd);
|
|
|
|
|
if(bl && *bl)
|
|
|
|
|
safe_copy(hw->kernel_bl, sizeof(hw->kernel_bl), bl);
|
2026-11-11 07:57:38 +02:00
|
|
|
if(power && *power)
|
|
|
|
|
safe_copy(hw->kernel_bl_power, sizeof(hw->kernel_bl_power), power);
|
2026-11-10 23:20:13 +02:00
|
|
|
if(marker && *marker)
|
|
|
|
|
safe_copy(hw->kernel_marker, sizeof(hw->kernel_marker), marker);
|
|
|
|
|
if(level && *level) {
|
|
|
|
|
unsigned long parsed = strtoul(level, NULL, 10);
|
|
|
|
|
if(parsed > 0 && parsed <= 255)
|
|
|
|
|
hw->kernel_bl_level = (unsigned int)parsed;
|
|
|
|
|
}
|
2026-11-11 07:57:38 +02:00
|
|
|
if(invert && *invert)
|
|
|
|
|
hw->kernel_bl_invert = strtol(invert, NULL, 10) != 0;
|
2026-11-10 21:54:53 +02:00
|
|
|
}
|
2026-11-10 19:41:57 +02:00
|
|
|
sim = getenv("BANANARCHY_COVER_SPI_SIM");
|
|
|
|
|
if(sim && *sim) {
|
|
|
|
|
safe_copy(hw->sim_path, sizeof(hw->sim_path), sim);
|
|
|
|
|
hw->fd_sim = open(sim, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
|
|
|
|
|
if(hw->fd_sim >= 0)
|
|
|
|
|
hw->sim = 1;
|
|
|
|
|
else
|
|
|
|
|
hw->sim_path[0] = '\0';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-09 22:50:51 +02:00
|
|
|
static void read_config(const char *path, char *socket_path, size_t splen,
|
2026-11-10 19:41:57 +02:00
|
|
|
char *fb_path, size_t fblen, struct state *state,
|
|
|
|
|
struct cover_hw *hw)
|
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;
|
2026-11-10 19:41:57 +02:00
|
|
|
long v;
|
2026-11-09 22:50:51 +02:00
|
|
|
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) {
|
2026-11-10 19:41:57 +02:00
|
|
|
v = strtol(value, NULL, 10);
|
2026-11-10 13:23:03 +02:00
|
|
|
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-10 19:41:57 +02:00
|
|
|
else if(strcmp(key, "backend") == 0)
|
|
|
|
|
safe_copy(hw->backend, sizeof(hw->backend), value);
|
|
|
|
|
else if(strcmp(key, "spi_device") == 0)
|
|
|
|
|
safe_copy(hw->spi_path, sizeof(hw->spi_path), value);
|
2026-11-10 21:54:53 +02:00
|
|
|
else if(strcmp(key, "main_fb_device") == 0)
|
|
|
|
|
safe_copy(hw->kernel_fb, sizeof(hw->kernel_fb), value);
|
|
|
|
|
else if(strcmp(key, "kernel_dpms_helper") == 0)
|
|
|
|
|
safe_copy(hw->kernel_dpms, sizeof(hw->kernel_dpms), value);
|
2026-11-11 07:57:38 +02:00
|
|
|
else if(strcmp(key, "kernel_power_sync_helper") == 0)
|
|
|
|
|
safe_copy(hw->kernel_sync, sizeof(hw->kernel_sync), value);
|
2026-11-10 23:20:13 +02:00
|
|
|
else if(strcmp(key, "kernel_command_path") == 0)
|
|
|
|
|
safe_copy(hw->kernel_cmd, sizeof(hw->kernel_cmd), value);
|
|
|
|
|
else if(strcmp(key, "kernel_backlight_path") == 0)
|
|
|
|
|
safe_copy(hw->kernel_bl, sizeof(hw->kernel_bl), value);
|
2026-11-11 07:57:38 +02:00
|
|
|
else if(strcmp(key, "kernel_backlight_power_path") == 0)
|
|
|
|
|
safe_copy(hw->kernel_bl_power, sizeof(hw->kernel_bl_power), value);
|
2026-11-10 23:20:13 +02:00
|
|
|
else if(strcmp(key, "kernel_backlight_marker") == 0)
|
|
|
|
|
safe_copy(hw->kernel_marker, sizeof(hw->kernel_marker), value);
|
|
|
|
|
else if(strcmp(key, "kernel_backlight_level") == 0) {
|
|
|
|
|
v = strtol(value, NULL, 10);
|
|
|
|
|
if(v > 0 && v <= 255)
|
|
|
|
|
hw->kernel_bl_level = (unsigned int)v;
|
2026-11-11 07:57:38 +02:00
|
|
|
} else if(strcmp(key, "kernel_backlight_invert") == 0) {
|
|
|
|
|
v = strtol(value, NULL, 10);
|
|
|
|
|
if(v == 0 || v == 1)
|
|
|
|
|
hw->kernel_bl_invert = (int)v;
|
2026-11-10 23:20:13 +02:00
|
|
|
}
|
2026-11-10 19:41:57 +02:00
|
|
|
else if(strcmp(key, "gpiochip_device") == 0)
|
|
|
|
|
safe_copy(hw->gpio_path, sizeof(hw->gpio_path), value);
|
|
|
|
|
else if(strcmp(key, "panel_firmware") == 0)
|
|
|
|
|
safe_copy(hw->firmware, sizeof(hw->firmware), value);
|
|
|
|
|
else if(strcmp(key, "spi_mux_gpio") == 0) {
|
|
|
|
|
v = strtol(value, NULL, 10);
|
|
|
|
|
if(v >= 0 && v < 1024)
|
|
|
|
|
hw->mux_gpio = (unsigned int)v;
|
|
|
|
|
} else if(strcmp(key, "spi_dc_gpio") == 0) {
|
|
|
|
|
v = strtol(value, NULL, 10);
|
|
|
|
|
if(v >= 0 && v < 1024)
|
|
|
|
|
hw->dc_gpio = (unsigned int)v;
|
|
|
|
|
} else if(strcmp(key, "spi_reset_gpio") == 0) {
|
|
|
|
|
v = strtol(value, NULL, 10);
|
|
|
|
|
if(v >= 0 && v < 1024)
|
|
|
|
|
hw->reset_gpio = (unsigned int)v;
|
|
|
|
|
} else if(strcmp(key, "mux_closed_level") == 0)
|
|
|
|
|
hw->mux_closed_level = config_level(value, hw->mux_closed_level);
|
|
|
|
|
else if(strcmp(key, "mux_open_level") == 0)
|
|
|
|
|
hw->mux_open_level = config_level(value, hw->mux_open_level);
|
|
|
|
|
else if(strcmp(key, "dc_command_level") == 0)
|
|
|
|
|
hw->dc_command_level = config_level(value, hw->dc_command_level);
|
|
|
|
|
else if(strcmp(key, "dc_data_level") == 0)
|
|
|
|
|
hw->dc_data_level = config_level(value, hw->dc_data_level);
|
|
|
|
|
else if(strcmp(key, "reset_assert_level") == 0)
|
|
|
|
|
hw->reset_assert_level = config_level(value, hw->reset_assert_level);
|
|
|
|
|
else if(strcmp(key, "reset_idle_level") == 0)
|
|
|
|
|
hw->reset_idle_level = config_level(value, hw->reset_idle_level);
|
|
|
|
|
else if(strcmp(key, "spi_speed_hz") == 0) {
|
|
|
|
|
v = strtol(value, NULL, 10);
|
|
|
|
|
if(v >= 1000000 && v <= 50000000)
|
|
|
|
|
hw->speed_hz = (unsigned int)v;
|
2026-11-10 20:56:42 +02:00
|
|
|
} else if(strcmp(key, "spi_word_bits") == 0) {
|
|
|
|
|
v = strtol(value, NULL, 10);
|
|
|
|
|
if(v == 8 || v == 9)
|
|
|
|
|
hw->word_bits = (int)v;
|
2026-11-10 19:41:57 +02:00
|
|
|
}
|
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-10 19:41:57 +02:00
|
|
|
if(strcmp(hw->backend, "none") == 0 || strcmp(hw->backend, "fb") == 0 ||
|
|
|
|
|
!hw->spi_path[0] || !hw->gpio_path[0] || !hw->firmware[0])
|
|
|
|
|
return;
|
|
|
|
|
if(!hw->sim) {
|
|
|
|
|
hw->fd_chip = open(hw->gpio_path, O_RDWR | O_CLOEXEC);
|
|
|
|
|
if(hw->fd_chip < 0)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
hw->spi_enabled = 1;
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-10 20:23:06 +02:00
|
|
|
/* 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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-10 19:41:57 +02:00
|
|
|
static void handle_command(char *buffer, struct state *state,
|
|
|
|
|
struct cover_hw *hw, int *redraw)
|
2026-11-09 22:50:51 +02:00
|
|
|
{
|
|
|
|
|
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);
|
2026-11-10 20:23:06 +02:00
|
|
|
int new_closed = value && strcasecmp(value, "CLOSED") == 0;
|
|
|
|
|
int old_closed = strcmp(state->lid, "closed") == 0;
|
2026-11-09 22:50:51 +02:00
|
|
|
safe_copy(state->lid, sizeof(state->lid),
|
2026-11-10 20:23:06 +02:00
|
|
|
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;
|
2026-11-10 13:23:03 +02:00
|
|
|
state->last_input_ms = now_ms();
|
|
|
|
|
*redraw = 1;
|
|
|
|
|
}
|
2026-11-10 20:23:06 +02:00
|
|
|
} 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);
|
2026-11-10 13:23:03 +02:00
|
|
|
} 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 20:23:06 +02:00
|
|
|
cover_wake(state, redraw);
|
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);
|
2026-11-10 20:23:06 +02:00
|
|
|
if(strcmp(state->call, "none") != 0)
|
|
|
|
|
cover_wake(state, redraw);
|
2026-11-09 22:50:51 +02:00
|
|
|
}
|
|
|
|
|
} else if(strcmp(command, "SMS") == 0) {
|
|
|
|
|
char *value = strtok_r(NULL, " \t\r\n", &save);
|
|
|
|
|
if(value)
|
|
|
|
|
state->sms = (int)strtol(value, NULL, 10);
|
2026-11-10 20:23:06 +02:00
|
|
|
cover_wake(state, redraw);
|
2026-11-09 22:50:51 +02:00
|
|
|
} 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);
|
2026-11-10 19:41:57 +02:00
|
|
|
if(!key || !value)
|
|
|
|
|
return;
|
|
|
|
|
if(strcmp(key, "enabled") == 0) {
|
2026-11-09 22:50:51 +02:00
|
|
|
state->enabled = (int)strtol(value, NULL, 10);
|
2026-11-10 19:41:57 +02:00
|
|
|
*redraw = 1;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
/* Runtime polarity knobs and a one-command rollback for the physical
|
|
|
|
|
* sign-off. Releasing forces the next draw to re-init the panel. */
|
|
|
|
|
if(strcmp(key, "backend") == 0) {
|
|
|
|
|
safe_copy(hw->backend, sizeof(hw->backend), value);
|
|
|
|
|
hw->spi_enabled =
|
|
|
|
|
strcmp(hw->backend, "none") != 0 && strcmp(hw->backend, "fb") != 0 &&
|
|
|
|
|
hw->spi_path[0] && hw->gpio_path[0] && hw->firmware[0];
|
|
|
|
|
} else if(strcmp(key, "mux_closed_level") == 0)
|
|
|
|
|
hw->mux_closed_level = config_level(value, hw->mux_closed_level);
|
|
|
|
|
else if(strcmp(key, "mux_open_level") == 0)
|
|
|
|
|
hw->mux_open_level = config_level(value, hw->mux_open_level);
|
|
|
|
|
else if(strcmp(key, "dc_command_level") == 0)
|
|
|
|
|
hw->dc_command_level = config_level(value, hw->dc_command_level);
|
|
|
|
|
else if(strcmp(key, "dc_data_level") == 0)
|
|
|
|
|
hw->dc_data_level = config_level(value, hw->dc_data_level);
|
|
|
|
|
else if(strcmp(key, "reset_assert_level") == 0)
|
|
|
|
|
hw->reset_assert_level = config_level(value, hw->reset_assert_level);
|
|
|
|
|
else if(strcmp(key, "reset_idle_level") == 0)
|
|
|
|
|
hw->reset_idle_level = config_level(value, hw->reset_idle_level);
|
2026-11-10 20:56:42 +02:00
|
|
|
else if(strcmp(key, "spi_word_bits") == 0) {
|
|
|
|
|
long bits = strtol(value, NULL, 10);
|
|
|
|
|
if(bits == 8 || bits == 9)
|
|
|
|
|
hw->word_bits = (int)bits;
|
|
|
|
|
else
|
|
|
|
|
return;
|
2026-11-10 23:20:13 +02:00
|
|
|
} else if(strcmp(key, "kernel_backlight_level") == 0) {
|
|
|
|
|
long level = strtol(value, NULL, 10);
|
|
|
|
|
if(level > 0 && level <= 255)
|
|
|
|
|
hw->kernel_bl_level = (unsigned int)level;
|
|
|
|
|
else
|
|
|
|
|
return;
|
2026-11-11 07:57:38 +02:00
|
|
|
} else if(strcmp(key, "kernel_backlight_invert") == 0) {
|
|
|
|
|
long invert = strtol(value, NULL, 10);
|
|
|
|
|
if(invert == 0 || invert == 1)
|
|
|
|
|
hw->kernel_bl_invert = (int)invert;
|
|
|
|
|
else
|
|
|
|
|
return;
|
2026-11-10 20:56:42 +02:00
|
|
|
} else
|
2026-11-10 19:41:57 +02:00
|
|
|
return;
|
2026-11-10 23:20:13 +02:00
|
|
|
cover_bus_release(hw);
|
2026-11-11 07:57:38 +02:00
|
|
|
hw->kernel_restored = 0;
|
2026-11-10 19:41:57 +02:00
|
|
|
*redraw = 1;
|
2026-11-10 20:56:42 +02:00
|
|
|
} else if(strcmp(command, "PROBE") == 0) {
|
|
|
|
|
const struct probe_case *p;
|
|
|
|
|
hw->probe_index = (hw->probe_index + 1) %
|
|
|
|
|
(int)(sizeof(probe_cases) / sizeof(probe_cases[0]));
|
|
|
|
|
p = &probe_cases[hw->probe_index];
|
|
|
|
|
hw->word_bits = p->bits;
|
|
|
|
|
hw->mux_closed_level = p->mux;
|
|
|
|
|
hw->mux_open_level = p->mux ? 0 : 1;
|
|
|
|
|
hw->reset_assert_level = p->reset;
|
|
|
|
|
hw->reset_idle_level = p->reset ? 0 : 1;
|
|
|
|
|
hw->dc_command_level = p->dc;
|
|
|
|
|
hw->dc_data_level = p->dc ? 0 : 1;
|
|
|
|
|
hw->speed_hz = p->speed;
|
|
|
|
|
snprintf(hw->probe_desc, sizeof(hw->probe_desc),
|
|
|
|
|
"case %d/%d bits=%d mux=%d reset=%d dc=%d speed=%u",
|
|
|
|
|
hw->probe_index + 1,
|
|
|
|
|
(int)(sizeof(probe_cases) / sizeof(probe_cases[0])),
|
|
|
|
|
p->bits, p->mux, p->reset, p->dc, p->speed);
|
2026-11-10 23:20:13 +02:00
|
|
|
/* PROBE is the legacy userspace sign-off path; it owns its own D/C. */
|
|
|
|
|
safe_copy(hw->backend, sizeof(hw->backend), "spidev");
|
|
|
|
|
hw->spi_enabled = hw->spi_path[0] && hw->gpio_path[0] &&
|
|
|
|
|
hw->firmware[0];
|
|
|
|
|
cover_bus_release(hw);
|
2026-11-10 20:56:42 +02:00
|
|
|
state->test = 2;
|
|
|
|
|
state->visible = 1;
|
|
|
|
|
state->last_input_ms = now_ms();
|
|
|
|
|
*redraw = 1;
|
2026-11-09 22:50:51 +02:00
|
|
|
} 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,
|
2026-11-10 19:41:57 +02:00
|
|
|
const struct cover_hw *hw, char *reply, size_t len)
|
2026-11-09 22:50:51 +02:00
|
|
|
{
|
|
|
|
|
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) {
|
2026-11-10 21:54:53 +02:00
|
|
|
const char *mux = "free";
|
|
|
|
|
if(hw->mux_claimed)
|
|
|
|
|
mux = strcmp(hw->backend, "kernel") == 0 ? "cover" :
|
|
|
|
|
(hw->claimed ? "closed" : "open");
|
2026-11-09 22:50:51 +02:00
|
|
|
snprintf(reply, len,
|
2026-11-10 13:23:03 +02:00
|
|
|
"OK lid=%s backend=%s enabled=%d visible=%d blank_ms=%lld "
|
2026-11-10 20:56:42 +02:00
|
|
|
"battery=%d call=%s sms=%d network=%s spi=%d mux=%s "
|
|
|
|
|
"bits=%d probe=%s\n",
|
2026-11-10 13:23:03 +02:00
|
|
|
state->lid, state->backend, state->enabled, state->visible,
|
|
|
|
|
state->blank_ms, state->battery, state->call, state->sms,
|
2026-11-10 21:54:53 +02:00
|
|
|
state->network, hw->spi_enabled, mux,
|
2026-11-10 20:56:42 +02:00
|
|
|
hw->word_bits, hw->probe_desc);
|
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 19:41:57 +02:00
|
|
|
struct cover_hw hw;
|
2026-11-10 13:23:03 +02:00
|
|
|
long long last_draw = 0;
|
2026-11-10 20:23:06 +02:00
|
|
|
long long pending_until = 0;
|
2026-11-10 13:23:03 +02:00
|
|
|
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-10 19:41:57 +02:00
|
|
|
cover_hw_init(&hw);
|
2026-11-09 22:50:51 +02:00
|
|
|
read_config(config, socket_path, sizeof(socket_path),
|
2026-11-10 19:41:57 +02:00
|
|
|
fb_path, sizeof(fb_path), &state, &hw);
|
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];
|
2026-11-10 19:41:57 +02:00
|
|
|
char reply[512];
|
2026-11-09 22:50:51 +02:00
|
|
|
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) {
|
2026-11-10 23:20:13 +02:00
|
|
|
long long interval = strcmp(hw.backend, "kernel") == 0 ?
|
|
|
|
|
KERNEL_REDRAW_MS : CLOCK_REDRAW_MS;
|
|
|
|
|
long long next = interval - (now - last_draw);
|
2026-11-10 13:23:03 +02:00
|
|
|
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);
|
2026-11-10 23:20:13 +02:00
|
|
|
if(state.visible && now - last_draw >= interval)
|
2026-11-10 13:23:03 +02:00
|
|
|
redraw = 1;
|
|
|
|
|
}
|
|
|
|
|
if(redraw) {
|
2026-11-10 14:11:00 +02:00
|
|
|
set_backlight(&state, cover_is_lit(&state));
|
2026-11-10 13:23:03 +02:00
|
|
|
snprintf(state.backend, sizeof(state.backend), "%s",
|
2026-11-10 19:41:57 +02:00
|
|
|
cover_draw(&hw, fb_path, &state));
|
2026-11-10 20:23:06 +02:00
|
|
|
/* 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;
|
2026-11-10 13:23:03 +02:00
|
|
|
last_draw = now;
|
|
|
|
|
redraw = 0;
|
|
|
|
|
}
|
2026-11-10 20:23:06 +02:00
|
|
|
if(pending_until > now)
|
|
|
|
|
timeout_ms = 50;
|
2026-11-09 22:50:51 +02:00
|
|
|
pfd.fd = server;
|
|
|
|
|
pfd.events = POLLIN;
|
|
|
|
|
pfd.revents = 0;
|
2026-11-10 20:23:06 +02:00
|
|
|
if(poll(&pfd, 1, timeout_ms) == 0 && pending_until > now &&
|
|
|
|
|
strcmp(state.backend, "pending") == 0) {
|
|
|
|
|
redraw = 1;
|
|
|
|
|
}
|
|
|
|
|
if(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);
|
2026-11-10 19:41:57 +02:00
|
|
|
handle_command(buffer, &state, &hw, &redraw);
|
|
|
|
|
reply_for(original, &state, &hw, reply, sizeof(reply));
|
2026-11-09 22:50:51 +02:00
|
|
|
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-10 19:41:57 +02:00
|
|
|
cover_hw_shutdown(&hw);
|
2026-11-09 22:50:51 +02:00
|
|
|
close(server);
|
|
|
|
|
unlink(socket_path);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|