Finalize verified 2720 Flip release source and untrack image outputs

This commit is contained in:
Luxferre committed 2026-11-09 23:04:13 +02:00
1 parent 2a3078e42c
commit 1c011b360a
121 files changed
+8467 -686

No files matched your search

@@ -0,0 +1,520 @@
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
/* Compiled idle cover IPC/renderer for nokia-beatles. No Python in the idle path. */
#include <ctype.h>
#include <poll.h>
#include <strings.h>
#include <sys/socket.h>
#include <sys/un.h>
#include "common.h"
#define COVER_LOCK "/run/bananarchy-cover-idle.lock"
#define DEFAULT_SOCKET "/run/bananarchy/cover.sock"
#define DEFAULT_FB "/dev/fb1"
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}},
{' ', {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;
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;
}
static int draw_frame(const char *fb_path, const char *message, int test)
{
int fd = open(fb_path, O_RDWR | O_CLOEXEC);
unsigned int width = 0;
unsigned int height = 0;
unsigned int bpp = 0;
unsigned int stride = 0;
size_t size;
unsigned char *mem;
struct frame fb;
char clock[16];
char date[32];
time_t now = time(NULL);
struct tm tm;
if(fd < 0)
return 0;
if(!fb_geometry(fb_path, &width, &height, &bpp, &stride)) {
close(fd);
return 0;
}
size = (size_t)stride * height;
mem = calloc(1, size);
if(!mem) {
close(fd);
return 0;
}
fb.pixels = mem;
fb.width = width;
fb.height = height;
fb.stride = stride;
fb.bpp = bpp;
localtime_r(&now, &tm);
strftime(clock, sizeof(clock), "%H:%M", &tm);
strftime(date, sizeof(date), "%d.%m.%Y", &tm);
if(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(test == 3) {
frame_rect(&fb, 0, 0, (int)width, (int)height,
rgb888(245, 245, 245));
} else if(test == 4) {
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));
if(message && message[0])
frame_text(&fb, message, 6, (int)height - 30, 2,
rgb888(235, 240, 248));
}
if(lseek(fd, 0, SEEK_SET) < 0 || !write_all(fd, mem, size)) {
free(mem);
close(fd);
return 0;
}
fsync(fd);
free(mem);
close(fd);
return 1;
}
static void mkdir_p(const char *path)
{
char buf[256];
char *p;
snprintf(buf, sizeof(buf), "%s", path);
for(p = buf + 1; *p; p++) {
if(*p != '/')
continue;
*p = '\0';
mkdir(buf, 0755);
*p = '/';
}
}
static void trim(char *value)
{
size_t len;
while(*value == ' ' || *value == '\t')
memmove(value, value + 1, strlen(value));
len = strlen(value);
while(len > 0 && (value[len - 1] == ' ' || value[len - 1] == '\t' ||
value[len - 1] == '\r' || value[len - 1] == '\n')) {
value[--len] = '\0';
}
}
static void read_config(const char *path, char *socket_path, size_t splen,
char *fb_path, size_t fblen)
{
char line[256];
FILE *handle = fopen(path, "r");
safe_copy(socket_path, splen, DEFAULT_SOCKET);
safe_copy(fb_path, fblen, DEFAULT_FB);
if(!handle)
return;
while(fgets(line, sizeof(line), handle)) {
char *comment = strchr(line, '#');
char *eq;
char *key;
char *value;
if(comment)
*comment = '\0';
eq = strchr(line, '=');
if(!eq)
continue;
*eq = '\0';
key = line;
value = eq + 1;
trim(key);
trim(value);
if(strcmp(key, "socket_path") == 0)
safe_copy(socket_path, splen, value);
else if(strcmp(key, "fb_device") == 0)
safe_copy(fb_path, fblen, value);
}
fclose(handle);
}
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++;
}
}
struct state {
char lid[16];
char backend[16];
char call[64];
char message[128];
int enabled;
int battery;
int sms;
int test;
};
static int test_value(const char *mode)
{
if(!mode)
return 0;
if(strcmp(mode, "bars") == 0)
return 2;
if(strcmp(mode, "clock") == 0)
return 1;
if(strcmp(mode, "solid") == 0)
return 3;
if(strcmp(mode, "off") == 0)
return 4;
return 0;
}
static void handle_command(char *buffer, struct state *state, int *redraw)
{
char command[32];
char *save = NULL;
char *token = strtok_r(buffer, " \t\r\n", &save);
if(!token)
return;
safe_copy(command, sizeof(command), token);
upper_string(command);
if(strcmp(command, "LID") == 0) {
char *value = strtok_r(NULL, " \t\r\n", &save);
safe_copy(state->lid, sizeof(state->lid),
value && strcasecmp(value, "CLOSED") == 0 ? "closed" : "open");
*redraw = 1;
} else if(strcmp(command, "NOTIFY") == 0) {
char *rest = save;
while(rest && (*rest == ' ' || *rest == '\t'))
rest++;
safe_copy(state->message, sizeof(state->message), rest ? rest : "");
trim(state->message);
*redraw = 1;
} else if(strcmp(command, "CALL") == 0) {
char *value = strtok_r(NULL, " \t\r\n", &save);
if(value) {
safe_copy(state->call, sizeof(state->call), value);
lower_string(state->call);
}
} else if(strcmp(command, "SMS") == 0) {
char *value = strtok_r(NULL, " \t\r\n", &save);
if(value)
state->sms = (int)strtol(value, NULL, 10);
} else if(strcmp(command, "BATTERY") == 0) {
char *value = strtok_r(NULL, " \t\r\n", &save);
if(value)
state->battery = (int)strtol(value, NULL, 10);
} else if(strcmp(command, "SET") == 0) {
char *key = strtok_r(NULL, " \t\r\n", &save);
char *value = strtok_r(NULL, " \t\r\n", &save);
if(key && value && strcmp(key, "enabled") == 0)
state->enabled = (int)strtol(value, NULL, 10);
} else if(strcmp(command, "TEST") == 0) {
char *mode = strtok_r(NULL, " \t\r\n", &save);
if(mode)
lower_string(mode);
state->test = test_value(mode);
*redraw = 1;
} else if(strcmp(command, "BLANK") == 0) {
state->test = 4;
*redraw = 1;
} else if(strcmp(command, "UNBLANK") == 0) {
state->test = 0;
*redraw = 1;
} else if(strcmp(command, "REFRESH") == 0 ||
strcmp(command, "RELOAD_THEME") == 0) {
*redraw = 1;
}
}
static void reply_for(char *buffer, const struct state *state,
char *reply, size_t len)
{
char command[32];
char *save = NULL;
char *token = strtok_r(buffer, " \t\r\n", &save);
if(!token) {
snprintf(reply, len, "OK\n");
return;
}
safe_copy(command, sizeof(command), token);
upper_string(command);
if(strcmp(command, "PING") == 0) {
snprintf(reply, len, "OK pong\n");
} else if(strcmp(command, "STATUS") == 0) {
snprintf(reply, len,
"OK lid=%s backend=%s enabled=%d battery=%d call=%s sms=%d\n",
state->lid, state->backend, state->enabled, state->battery,
state->call, state->sms);
} 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;
time_t last_draw = 0;
if(lock_fd < 0)
return 1;
if(argc > 2 && strcmp(argv[1], "--config") == 0)
config = argv[2];
read_config(config, socket_path, sizeof(socket_path),
fb_path, sizeof(fb_path));
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);
memset(&state, 0, sizeof(state));
snprintf(state.lid, sizeof(state.lid), "open");
snprintf(state.backend, sizeof(state.backend), "none");
snprintf(state.call, sizeof(state.call), "none");
state.enabled = 1;
state.battery = -1;
while(1) {
struct pollfd pfd;
char buffer[512];
char original[512];
char reply[256];
struct sockaddr_un peer;
socklen_t peer_len = sizeof(peer);
ssize_t nread;
pfd.fd = server;
pfd.events = POLLIN;
pfd.revents = 0;
if(poll(&pfd, 1, 1000) > 0 && (pfd.revents & POLLIN)) {
nread = recvfrom(server, buffer, sizeof(buffer) - 1, 0,
(struct sockaddr *)&peer, &peer_len);
if(nread > 0) {
int redraw = 0;
buffer[nread] = '\0';
snprintf(original, sizeof(original), "%s", buffer);
handle_command(buffer, &state, &redraw);
reply_for(original, &state, reply, sizeof(reply));
sendto(server, reply, strlen(reply), 0,
(struct sockaddr *)&peer, peer_len);
if(redraw)
last_draw = 0;
if(strncasecmp(original, "QUIT", 4) == 0) {
close(server);
unlink(socket_path);
return 0;
}
}
}
if(state.enabled && time(NULL) - last_draw > 30) {
last_draw = time(NULL);
snprintf(state.backend, sizeof(state.backend), "%s",
draw_frame(fb_path, state.message, state.test) ? "fb" : "none");
}
}
return 0;
}
+328
View File
@@ -0,0 +1,328 @@
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
/* Compiled flip-lid handler for nokia-beatles. No Python in the idle path. */
#include <poll.h>
#include <sys/mount.h>
#include <sys/socket.h>
#include <sys/un.h>
#include "common.h"
#define LID_LOCK "/run/bananarchy-lid.lock"
#define LID_STATE_FILE "/run/bananarchy/lid-state"
#define LOCK_FLAG "/tmp/bananarchy_screen_locked"
#define LOCKER_FIFO "/tmp/dwm_plugin_locker.fifo"
#define LID_STABLE_MS 80
#define GPIO_FILE "/sys/kernel/debug/gpio"
#define COVER_SOCKET "/run/bananarchy/cover.sock"
#define LID_CLIENT_SOCKET "/run/bananarchy/lid-client.sock"
#define LID_BROADCAST_MS 2000
#define EV_SW 0x05
#define SW_LID 0x00
#define SW_KEYPAD_SLIDE 0x0a
#define CLOSED_MASK ((1u << SW_LID) | (1u << SW_KEYPAD_SLIDE))
#define BA_EVIOCGSW(len) BA_IOC(2U, 'E', 0x1bU, (unsigned int)(len))
static char *const session_env[] = {
(char *)"PATH=/home/user/.local/bin:/home/user/.config/sxmo/hooks:/usr/share/sxmo/default_hooks:/usr/local/bin:/usr/bin:/bin:/sbin:/usr/sbin",
(char *)"HOME=/home/user",
(char *)"USER=user",
(char *)"DISPLAY=:0",
(char *)"XDG_RUNTIME_DIR=/run/user/10000",
(char *)"SHELL=/bin/sh",
NULL
};
static void run_sxmo_state(const char *action)
{
char *const argv[] = {
(char *)"/usr/bin/sxmo_state.sh", (char *)"set", (char *)action, NULL
};
daemon_spawn_env(argv, (char *const *)session_env);
}
static void write_lid_state(int closed)
{
const char *value = closed ? "closed\n" : "open\n";
int fd;
mkdir("/run/bananarchy", 0755);
fd = open(LID_STATE_FILE, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
if(fd < 0)
return;
if(write(fd, value, strlen(value)) < 0) {
close(fd);
return;
}
close(fd);
chmod(LID_STATE_FILE, 0644);
}
static void cover_send(const char *state)
{
int fd = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
struct sockaddr_un local;
struct sockaddr_un remote;
char payload[64];
if(fd < 0)
return;
mkdir("/run/bananarchy", 0755);
unlink(LID_CLIENT_SOCKET);
memset(&local, 0, sizeof(local));
local.sun_family = AF_UNIX;
snprintf(local.sun_path, sizeof(local.sun_path), "%s", LID_CLIENT_SOCKET);
if(bind(fd, (struct sockaddr *)&local, sizeof(local)) < 0) {
close(fd);
return;
}
memset(&remote, 0, sizeof(remote));
remote.sun_family = AF_UNIX;
snprintf(remote.sun_path, sizeof(remote.sun_path), "%s", COVER_SOCKET);
snprintf(payload, sizeof(payload), "LID %s", state);
sendto(fd, payload, strlen(payload), 0,
(struct sockaddr *)&remote, sizeof(remote));
close(fd);
unlink(LID_CLIENT_SOCKET);
}
static void locker_send(const char *payload)
{
int fd = open(LOCKER_FIFO, O_WRONLY | O_NONBLOCK | O_CLOEXEC);
if(fd < 0)
return;
(void)write(fd, payload, strlen(payload));
close(fd);
}
static void on_transition(int closed)
{
write_lid_state(closed);
if(closed) {
write_text_file("/sys/class/backlight/backlight/brightness", "0");
write_text_file("/sys/class/backlight/backlight/bl_power", "4");
write_text_file("/sys/class/leds/white:kbd_backlight/brightness", "0");
cover_send("CLOSED");
locker_send("SCREENOFF\n");
run_sxmo_state("screenoff");
} else {
unlink(LOCK_FLAG);
write_text_file("/sys/class/backlight/backlight/bl_power", "0");
write_text_file("/sys/class/backlight/backlight/brightness", "10");
write_text_file("/sys/class/leds/white:kbd_backlight/brightness", "1");
cover_send("OPEN");
locker_send("CLOSE\n");
run_sxmo_state("unlock");
}
}
static int hall_line_closed(const char *line)
{
char buf[256];
char *save = NULL;
char *token;
char *name = NULL;
char *level = NULL;
int index = 0;
safe_copy(buf, sizeof(buf), line);
token = strtok_r(buf, " \t\r\n", &save);
while(token) {
if(index == 0)
name = token;
if(index == 3)
level = token;
index++;
token = strtok_r(NULL, " \t\r\n", &save);
}
if(!name || !level)
return 0;
if(strcmp(name, "gpio36") != 0 && strcmp(name, "gpio92") != 0)
return 0;
return strcmp(level, "low") == 0;
}
static int hall_state(void)
{
const char *override = getenv("BANANARCHY_LID_GPIO_FILE");
const char *path = override ? override : GPIO_FILE;
char line[256];
FILE *handle;
int closed = 0;
if(access(path, R_OK) < 0) {
mount("none", "/sys/kernel/debug", "debugfs", 0, NULL);
}
handle = fopen(path, "r");
if(!handle)
return -1;
while(fgets(line, sizeof(line), handle)) {
if(hall_line_closed(line)) {
closed = 1;
break;
}
}
fclose(handle);
return closed;
}
static unsigned int switch_bit(unsigned int code)
{
if(code == SW_LID || code == SW_KEYPAD_SLIDE)
return 1u << code;
return 0;
}
static int device_switch_mask(int fd)
{
unsigned char bits[16];
unsigned int mask = 0;
memset(bits, 0, sizeof(bits));
if(ioctl(fd, BA_EVIOCGSW(sizeof(bits)), bits) < 0)
return -1;
if(bits[SW_LID / 8] & (1u << (SW_LID % 8)))
mask |= switch_bit(SW_LID);
if(bits[SW_KEYPAD_SLIDE / 8] & (1u << (SW_KEYPAD_SLIDE % 8)))
mask |= switch_bit(SW_KEYPAD_SLIDE);
return (int)mask;
}
static int current_switch_mask(int *found)
{
glob_t gl;
size_t i;
int mask = 0;
*found = 0;
if(glob("/dev/input/event*", 0, NULL, &gl) != 0)
return 0;
for(i = 0; i < gl.gl_pathc; i++) {
int fd = open(gl.gl_pathv[i], O_RDONLY | O_NONBLOCK | O_CLOEXEC);
int devmask;
if(fd < 0)
continue;
if(!wanted_device(fd, WATCH_SWITCHES)) {
close(fd);
continue;
}
devmask = device_switch_mask(fd);
if(devmask >= 0) {
*found = 1;
mask |= devmask;
}
close(fd);
}
globfree(&gl);
return mask & (int)CLOSED_MASK;
}
static void read_ready(struct watch_list *list, struct pollfd *fds, int nfds,
int *switch_mask)
{
int i;
for(i = 0; i < nfds; i++) {
struct input_event ev;
ssize_t nread = 0;
if(fds[i].revents & (POLLHUP | POLLERR | POLLNVAL)) {
drop_device(list, fds[i].fd);
continue;
}
if(!(fds[i].revents & POLLIN))
continue;
while((nread = read(fds[i].fd, &ev, sizeof(ev))) == (ssize_t)sizeof(ev)) {
unsigned int bit;
if(ev.type != EV_SW)
continue;
bit = switch_bit(ev.code);
if(!bit)
continue;
if(ev.value)
*switch_mask |= (int)bit;
else
*switch_mask &= ~(int)bit;
}
if(nread < 0 && errno != EAGAIN && errno != EWOULDBLOCK &&
errno != EINTR) {
drop_device(list, fds[i].fd);
}
}
}
int main(void)
{
int lock_fd = lock_singleton(LID_LOCK);
struct watch_list list;
long long now;
long long last_scan = 0;
long long last_transition = 0;
long long last_broadcast = 0;
long long candidate_since;
int evdev_found = 0;
int switch_mask;
int stable_closed;
int candidate;
int hall;
if(lock_fd < 0)
return 1;
install_reaper();
memset(&list, 0, sizeof(list));
switch_mask = current_switch_mask(&evdev_found);
if(evdev_found)
stable_closed = (switch_mask & (int)CLOSED_MASK) != 0;
else {
hall = hall_state();
stable_closed = hall >= 0 ? hall : 0;
}
candidate = stable_closed;
now = now_ms();
candidate_since = now;
write_lid_state(stable_closed);
cover_send(stable_closed ? "CLOSED" : "OPEN");
last_broadcast = now;
while(1) {
struct pollfd fds[WATCH_MAX];
int nfds = 0;
int i;
int observed;
now = now_ms();
if(now - last_scan > 2000) {
scan_devices(&list, WATCH_SWITCHES);
last_scan = now;
if(list.count > 0 && !evdev_found) {
switch_mask = current_switch_mask(&evdev_found);
}
}
for(i = 0; i < list.count; i++) {
fds[nfds].fd = list.devs[i].fd;
fds[nfds].events = POLLIN;
fds[nfds].revents = 0;
nfds++;
}
if(poll(fds, (nfds_t)nfds, 100) < 0 && errno != EINTR) {
for(i = 0; i < list.count; i++)
drop_device(&list, list.devs[i].fd);
}
if(nfds > 0)
read_ready(&list, fds, nfds, &switch_mask);
if(list.count > 0) {
observed = (switch_mask & (int)CLOSED_MASK) != 0;
} else {
hall = hall_state();
observed = hall >= 0 ? hall :
(switch_mask & (int)CLOSED_MASK) != 0;
}
if(observed != candidate) {
candidate = observed;
candidate_since = now;
}
if(candidate >= 0 && candidate != stable_closed &&
now - candidate_since >= LID_STABLE_MS &&
now - last_transition >= 150) {
stable_closed = candidate;
last_transition = now;
on_transition(stable_closed);
}
if(now - last_broadcast >= LID_BROADCAST_MS) {
write_lid_state(stable_closed);
cover_send(stable_closed ? "CLOSED" : "OPEN");
last_broadcast = now;
}
}
return 0;
}
@@ -0,0 +1,111 @@
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
/* Compiled side-volume-key reader for nokia-beatles. No Python in the idle path. */
#include <poll.h>
#include "common.h"
#define VOLUME_LOCK "/run/bananarchy-volume-keys.lock"
#ifndef EV_KEY
#define EV_KEY 0x01
#endif
#ifndef KEY_VOLUMEDOWN
#define KEY_VOLUMEDOWN 114
#endif
#ifndef KEY_VOLUMEUP
#define KEY_VOLUMEUP 115
#endif
static char *const session_env[] = {
(char *)"PATH=/home/user/.local/bin:/home/user/.config/sxmo/hooks:/usr/share/sxmo/default_hooks:/usr/local/bin:/usr/bin:/bin:/sbin:/usr/sbin",
(char *)"HOME=/home/user",
(char *)"USER=user",
(char *)"DISPLAY=:0",
(char *)"XDG_RUNTIME_DIR=/run/user/10000",
(char *)"SHELL=/bin/sh",
NULL
};
static void apply_volume(const char *action)
{
char *const argv[] = {
(char *)"/usr/bin/sxmo_audio.sh", (char *)"vol", (char *)action,
(char *)"5", NULL
};
daemon_spawn_env(argv, (char *const *)session_env);
}
static int stale_errno(int value)
{
return value == ENODEV || value == EIO || value == EBADF ||
value == EINVAL;
}
static void read_ready(struct watch_list *list, struct pollfd *fds, int nfds)
{
int i;
for(i = 0; i < nfds; i++) {
struct input_event ev;
ssize_t nread;
if(fds[i].revents & (POLLHUP | POLLERR | POLLNVAL)) {
drop_device(list, fds[i].fd);
continue;
}
if(!(fds[i].revents & POLLIN))
continue;
while((nread = read(fds[i].fd, &ev, sizeof(ev))) == (ssize_t)sizeof(ev)) {
if(ev.type != EV_KEY || ev.value != 1)
continue;
if(ev.code == KEY_VOLUMEUP)
apply_volume("up");
else if(ev.code == KEY_VOLUMEDOWN)
apply_volume("down");
}
if(nread < 0 && errno != EAGAIN && errno != EWOULDBLOCK &&
errno != EINTR) {
if(stale_errno(errno))
drop_device(list, fds[i].fd);
else
drop_device(list, fds[i].fd);
}
}
}
int main(void)
{
int lock_fd = lock_singleton(VOLUME_LOCK);
struct watch_list list;
long long last_scan = 0;
if(lock_fd < 0)
return 1;
install_reaper();
memset(&list, 0, sizeof(list));
while(1) {
long long now = now_ms();
struct pollfd fds[WATCH_MAX];
int nfds = 0;
int i;
if(now - last_scan > 2000) {
scan_devices(&list, WATCH_VOLUME_KEYS);
last_scan = now;
}
if(list.count == 0) {
poll(NULL, 0, 500);
continue;
}
for(i = 0; i < list.count; i++) {
fds[nfds].fd = list.devs[i].fd;
fds[nfds].events = POLLIN;
fds[nfds].revents = 0;
nfds++;
}
if(poll(fds, (nfds_t)nfds, 1000) < 0 && errno != EINTR) {
for(i = 0; i < list.count; i++)
drop_device(&list, list.devs[i].fd);
}
read_ready(&list, fds, nfds);
}
return 0;
}
+238
View File
@@ -0,0 +1,238 @@
/* Shared helpers for the compiled Bananarchy idle daemons. */
#ifndef BANANARCHY_DAEMONS_COMMON_H
#define BANANARCHY_DAEMONS_COMMON_H
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <errno.h>
#include <fcntl.h>
#include <glob.h>
#include <signal.h>
#include <sys/time.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#define WATCH_MAX 32
#define SESSION_UID 10000
#define SESSION_GID 10000
#define BA_UNUSED __attribute__((unused))
#define EV_KEY 0x01
#define EV_SW 0x05
#define KEY_MAX 0x2ff
#define SW_LID 0x00
#define SW_KEYPAD_SLIDE 0x0a
#define KEY_VOLUMEDOWN 114
#define KEY_VOLUMEUP 115
#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_EVIOCGBIT(ev, len) \
BA_IOC(2U, 'E', 0x20U + (unsigned int)(ev), (unsigned int)(len))
struct input_event {
struct timeval time;
uint16_t type;
uint16_t code;
int32_t value;
};
enum watch_filter {
WATCH_ALL = 0,
WATCH_SWITCHES,
WATCH_VOLUME_KEYS,
};
struct watch_dev {
int fd;
ino_t ino;
char path[64];
};
struct watch_list {
struct watch_dev devs[WATCH_MAX];
int count;
};
static BA_UNUSED void safe_copy(char *dst, size_t len, const char *src)
{
size_t n;
if(len == 0)
return;
n = strlen(src);
if(n >= len)
n = len - 1;
memcpy(dst, src, n);
dst[n] = '\0';
}
static BA_UNUSED int write_text_file(const char *path, const char *value)
{
int fd = open(path, O_WRONLY | O_CLOEXEC);
if(fd < 0)
return -1;
if(write(fd, value, strlen(value)) < 0) {
close(fd);
return -1;
}
close(fd);
return 0;
}
static BA_UNUSED int lock_singleton(const char *path)
{
int fd = open(path, O_CREAT | O_RDWR | O_CLOEXEC, 0644);
if(fd < 0)
return -1;
if(flock(fd, LOCK_EX | LOCK_NB) < 0) {
close(fd);
return -1;
}
return fd;
}
static BA_UNUSED void reap_children(int sig)
{
int saved = errno;
(void)sig;
while(waitpid(-1, NULL, WNOHANG) > 0) {
}
errno = saved;
}
static BA_UNUSED void install_reaper(void)
{
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = reap_children;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_NOCLDSTOP | SA_RESTART;
sigaction(SIGCHLD, &sa, NULL);
}
static BA_UNUSED void daemon_spawn_env(char *const argv[], char *const envp[])
{
pid_t pid = fork();
if(pid < 0)
return;
if(pid > 0)
return;
int nullfd = open("/dev/null", O_RDWR);
if(nullfd >= 0) {
dup2(nullfd, STDIN_FILENO);
dup2(nullfd, STDOUT_FILENO);
dup2(nullfd, STDERR_FILENO);
if(nullfd > STDERR_FILENO)
close(nullfd);
}
if(setgid(SESSION_GID) == 0 && setuid(SESSION_UID) == 0)
execve(argv[0], argv, envp);
_exit(127);
}
static BA_UNUSED void drop_device(struct watch_list *list, int fd)
{
int i;
for(i = 0; i < list->count; i++) {
if(list->devs[i].fd != fd)
continue;
close(list->devs[i].fd);
memmove(&list->devs[i], &list->devs[i + 1],
sizeof(list->devs[0]) * (size_t)(list->count - i - 1));
list->count--;
return;
}
}
static BA_UNUSED int device_has_code(int fd, unsigned int ev, unsigned int code)
{
unsigned char bits[(KEY_MAX + 7) / 8 + 1];
memset(bits, 0, sizeof(bits));
if(ioctl(fd, BA_EVIOCGBIT(ev, sizeof(bits)), bits) < 0)
return 0;
if(code >= sizeof(bits) * 8)
return 0;
return (bits[code / 8] & (1u << (code % 8))) != 0;
}
static BA_UNUSED int wanted_device(int fd, enum watch_filter filter)
{
if(filter == WATCH_ALL)
return 1;
if(filter == WATCH_SWITCHES) {
return device_has_code(fd, EV_SW, SW_LID) ||
device_has_code(fd, EV_SW, SW_KEYPAD_SLIDE);
}
return device_has_code(fd, EV_KEY, KEY_VOLUMEUP) ||
device_has_code(fd, EV_KEY, KEY_VOLUMEDOWN);
}
static BA_UNUSED void prune_devices(struct watch_list *list)
{
int i;
for(i = list->count - 1; i >= 0; i--) {
struct stat st;
if(stat(list->devs[i].path, &st) == 0 && st.st_ino == list->devs[i].ino)
continue;
drop_device(list, list->devs[i].fd);
}
}
static BA_UNUSED void scan_devices(struct watch_list *list, enum watch_filter filter)
{
glob_t gl;
size_t i;
prune_devices(list);
if(glob("/dev/input/event*", 0, NULL, &gl) != 0)
return;
for(i = 0; i < gl.gl_pathc && list->count < WATCH_MAX; i++) {
const char *path = gl.gl_pathv[i];
struct stat st;
int j;
int fd;
int seen = 0;
if(stat(path, &st) < 0)
continue;
for(j = 0; j < list->count; j++) {
if(list->devs[j].ino == st.st_ino) {
seen = 1;
break;
}
}
if(seen)
continue;
fd = open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
if(fd < 0)
continue;
if(!wanted_device(fd, filter)) {
close(fd);
continue;
}
list->devs[list->count].fd = fd;
list->devs[list->count].ino = st.st_ino;
strncpy(list->devs[list->count].path, path,
sizeof(list->devs[list->count].path) - 1);
list->devs[list->count].path[sizeof(list->devs[0].path) - 1] = '\0';
list->count++;
}
globfree(&gl);
}
static BA_UNUSED long long now_ms(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (long long)ts.tv_sec * 1000LL + ts.tv_nsec / 1000000LL;
}
#endif
+12 -4
View File
@@ -781,7 +781,7 @@ int locker_run_main(int argc, char** argv) {
XFlush(dpy);
visible = false;
}
writeSoftkeySuppress(nowMs() + 350);
writeSoftkeySuppress(nowMs() + 120);
setSxmoStateFile("unlock");
wakeScreenBacklight();
wasScreenBlanked = false;
@@ -809,7 +809,7 @@ int locker_run_main(int argc, char** argv) {
statusMsg.clear();
statusIsError = false;
lockActivatedMs = now;
writeSoftkeySuppress(now + 400);
writeSoftkeySuppress(now + 120);
}
if (!visible) {
XMapRaised(dpy, win);
@@ -887,7 +887,7 @@ int locker_run_main(int argc, char** argv) {
render();
return;
}
if (now - lockActivatedMs < 80) {
if (now - lockActivatedMs < 40) {
return;
}
@@ -1096,7 +1096,15 @@ int locker_run_main(int argc, char** argv) {
}
continue;
}
// Ignore CLOSE while locked so external scripts cannot dismiss the lockscreen
// A trusted root lid daemon clears the lock flag before sending CLOSE.
// If the flag is gone, release any stale locker grab/window immediately;
// otherwise ignore CLOSE so user scripts cannot dismiss the lockscreen.
if(cmd == "CLOSE" || cmd == "UNLOCK") {
if(::access(kLockFlagPath, F_OK) != 0 && (visible || grabHeld)) {
unlockPhone(false);
}
continue;
}
}
}
+4 -5
View File
@@ -210,11 +210,10 @@ static unsigned int msm8905_pon_prepare_restart_mode(const char *cmd)
strcmp(trimmed, "fastboot") == 0) {
if (pon_regmap) {
/*
* Stock Nokia aboot (0x2327c) reads PON_SOFT_RB_SPARE (0x088f), clears
* bits [7:2] via `and r1, r4, #3`, and writes bits [1:0] back to 0x088f
* before chainloading lk2nd from the boot partition.
* Write 0x02 (FASTBOOT_MODE = 2 in bits [1:0], 0 in bits [7:2]) so
* stock aboot boots lk2nd normally and lk2nd enters its Fastboot menu.
* The stock boot chain scrubs PON_SOFT_RB_SPARE[7:2] and passes
* bits [1:0] through to the chain-loaded lk2nd image. The beatles
* lk2nd port reads those low bits as the reboot reason, so 0x02
* selects FASTBOOT_MODE and shows the menu instead of booting Linux.
*/
regmap_write(pon_regmap, QPNP_PON_SOFT_RB_SPARE, 0x02U);
}