Finalize verified 2720 Flip release source and untrack image outputs
This commit is contained in:
1 parent
2a3078e42c
commit
1c011b360a
121 files changed
+8467
-686
No files matched your search
@@ -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;
|
||||
}
|
||||
Reference in new issue
Block a user