#ifndef _GNU_SOURCE #define _GNU_SOURCE #endif /* Compiled idle cover IPC/renderer for nokia-beatles. No Python in the idle path. */ #include #include #include #include #include #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; }