Files
bananarchy/src/bananarchy-daemons/bananarchy-cover-idle.c
T

1325 lines
40 KiB
C
Raw Normal View History

#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.
*
* 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
*/
#include <ctype.h>
#include <poll.h>
#include <strings.h>
#include <sys/socket.h>
#include <sys/un.h>
/* 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
#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
#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");
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;
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;
};
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}},
{' ', {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;
}
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;
};
/* 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);
}
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)
{
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;
render_frame(&fb, state);
if(lseek(fd, 0, SEEK_SET) < 0 || !write_all(fd, mem, size)) {
free(mem);
close(fd);
return 0;
}
fsync(fd);
/* 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);
free(mem);
close(fd);
return 1;
}
/* --- 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];
values[0] = (unsigned char)level;
values[1] = (unsigned char)hw->reset_idle_level;
return hw_gpio_set(hw, hw->fd_panel, values, 2);
}
static int hw_command(struct cover_hw *hw, unsigned char cmd,
const unsigned char *params, size_t n)
{
char line[64];
snprintf(line, sizeof(line), "cmd %02x %zu", cmd, n);
hw_event(hw, line);
if(hw->sim)
return 1;
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];
unsigned char values[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) {
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, 2);
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;
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. */
values[0] = (unsigned char)hw->dc_data_level;
values[1] = (unsigned char)hw->reset_assert_level;
hw_gpio_set(hw, hw->fd_panel, values, 2);
if(!hw->sim)
poll(NULL, 0, 20);
values[1] = (unsigned char)hw->reset_idle_level;
hw_gpio_set(hw, hw->fd_panel, values, 2);
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;
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;
}
/* 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];
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);
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;
}
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) {
cover_hw_release(hw);
if(prefer_fb)
return draw_frame(fb_path, state) ? "fb" : "none";
return "idle";
}
if(strcmp(hw->backend, "none") == 0) {
cover_hw_release(hw);
return "none";
}
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";
}
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;
}
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;
hw->fd_spi = -1;
hw->fd_chip = -1;
hw->fd_mux = -1;
hw->fd_panel = -1;
hw->fd_sim = -1;
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';
}
}
static void read_config(const char *path, char *socket_path, size_t splen,
char *fb_path, size_t fblen, struct state *state,
struct cover_hw *hw)
{
char line[256];
2026-11-10 13:23:03 +02:00
char blank[32];
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);
return;
2026-11-10 13:23:03 +02:00
}
while(fgets(line, sizeof(line), handle)) {
char *comment = strchr(line, '#');
char *eq;
char *key;
char *value;
long v;
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) {
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);
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);
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;
}
}
fclose(handle);
2026-11-10 13:23:03 +02:00
state->blank_ms = blank_timeout_ms(blank);
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;
}
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;
}
/* A closed flip lights the outer panel only. Status updates (NETWORK,
* BATTERY) refresh an already visible cover but never wake it; real events
* (BUTTON, WAKE, CALL, NOTIFY, SMS) do. */
static void cover_wake(struct state *state, int *redraw)
{
if(strcmp(state->lid, "closed") == 0 && state->enabled && !state->test) {
state->visible = 1;
state->last_input_ms = now_ms();
*redraw = 1;
}
}
static void handle_command(char *buffer, struct state *state,
struct cover_hw *hw, int *redraw)
{
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);
int new_closed = value && strcasecmp(value, "CLOSED") == 0;
int old_closed = strcmp(state->lid, "closed") == 0;
safe_copy(state->lid, sizeof(state->lid),
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;
}
} 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;
} 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);
cover_wake(state, redraw);
2026-11-10 13:23:03 +02:00
if(state->visible)
*redraw = 1;
} 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);
if(strcmp(state->call, "none") != 0)
cover_wake(state, redraw);
}
} else if(strcmp(command, "SMS") == 0) {
char *value = strtok_r(NULL, " \t\r\n", &save);
if(value)
state->sms = (int)strtol(value, NULL, 10);
cover_wake(state, redraw);
} else if(strcmp(command, "BATTERY") == 0) {
char *value = strtok_r(NULL, " \t\r\n", &save);
if(value)
state->battery = (int)strtol(value, NULL, 10);
2026-11-10 13:23:03 +02:00
if(state->visible)
*redraw = 1;
} else if(strcmp(command, "SET") == 0) {
char *key = strtok_r(NULL, " \t\r\n", &save);
char *value = strtok_r(NULL, " \t\r\n", &save);
if(!key || !value)
return;
if(strcmp(key, "enabled") == 0) {
state->enabled = (int)strtol(value, NULL, 10);
*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);
else
return;
cover_hw_release(hw);
*redraw = 1;
} 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;
*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();
}
*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;
}
}
static void reply_for(char *buffer, const struct state *state,
const struct cover_hw *hw, 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 spi=%d mux=%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,
state->network, hw->spi_enabled,
hw->mux_claimed ? (hw->claimed ? "closed" : "open") : "free");
} 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;
struct cover_hw hw;
2026-11-10 13:23:03 +02:00
long long last_draw = 0;
long long pending_until = 0;
2026-11-10 13:23:03 +02:00
int redraw = 1;
if(lock_fd < 0)
return 1;
2026-11-10 13:23:03 +02:00
(void)lock_fd;
install_reaper();
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;
cover_hw_init(&hw);
read_config(config, socket_path, sizeof(socket_path),
fb_path, sizeof(fb_path), &state, &hw);
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[512];
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, cover_is_lit(&state));
2026-11-10 13:23:03 +02:00
snprintf(state.backend, sizeof(state.backend), "%s",
cover_draw(&hw, fb_path, &state));
/* The lid daemon forces DPMS off asynchronously. Retry a pending draw
* for a bounded window so the cover lights even if the first draw ran
* before Xorg stopped flushing the main panel. */
pending_until = strcmp(state.backend, "pending") == 0 ?
now + 3000 : 0;
2026-11-10 13:23:03 +02:00
last_draw = now;
redraw = 0;
}
if(pending_until > now)
timeout_ms = 50;
pfd.fd = server;
pfd.events = POLLIN;
pfd.revents = 0;
if(poll(&pfd, 1, timeout_ms) == 0 && pending_until > now &&
strcmp(state.backend, "pending") == 0) {
redraw = 1;
}
if(pfd.revents & POLLIN) {
nread = recvfrom(server, buffer, sizeof(buffer) - 1, 0,
(struct sockaddr *)&peer, &peer_len);
if(nread > 0) {
buffer[nread] = '\0';
snprintf(original, sizeof(original), "%s", buffer);
handle_command(buffer, &state, &hw, &redraw);
reply_for(original, &state, &hw, 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);
cover_hw_shutdown(&hw);
close(server);
unlink(socket_path);
return 0;
}
}
}
}
return 0;
}