/* * POWERKEY * Handles the KEY2 LE power button and the idle state of the console. * * A short press locks the console (the password is required to get back in) * and powers the panel down; the heavy background daemons are frozen so the * soc is not kept busy while the phone sits in a pocket. A second short * press brings the panel back and the lock asks for the password. A long * press still powers the machine off. * * USAGE * $ powerkey [event-device] * * AUTHOR * Written by 'baby' contributors. */ #include #include #include #include #include #include #include #include #include #include #include #define EVENT "/dev/input/event0" #define BLANK "/sys/class/graphics/fb0/blank" #define BLDIR "/sys/class/backlight/backlight" #define LOGFILE "/var/log/powerkey.log" #define KEY_POWER 116 #define LONG_PRESS 1.5 #define EVSZ 24 static const char *freeze[] = { "/usr/bin/opencode", "opencode serve", "/usr/bin/syncthing", "/usr/bin/warp-plus", "/usr/bin/opencode-warp", "rclone", }; static int frozen[4096]; static int nfrozen; static int rd(const char *path, int def) { char buf[32]; int fd, n; fd = open(path, O_RDONLY); if(fd < 0) return def; n = read(fd, buf, sizeof buf - 1); close(fd); if(n <= 0) return def; buf[n] = '\0'; return atoi(buf); } static void wr(const char *path, int val) { char buf[32]; int fd, n; n = snprintf(buf, sizeof buf, "%d", val); fd = open(path, O_WRONLY); if(fd < 0) return; write(fd, buf, n); close(fd); } static void logmsg(const char *fmt, ...) { va_list ap; FILE *f; f = fopen(LOGFILE, "a"); if(!f) return; fprintf(f, "%.3f ", (double)time(NULL)); va_start(ap, fmt); vfprintf(f, fmt, ap); va_end(ap); fputc('\n', f); fclose(f); } static int maxbright = 4095; static int level = 512; static int screenon = 1; static int matches(const char *cmd, size_t n) { size_t i, j, l; for(i = 0; i < sizeof freeze / sizeof *freeze; i++){ l = strlen(freeze[i]); if(l > n) continue; for(j = 0; j + l <= n; j++) if(memcmp(cmd + j, freeze[i], l) == 0) return 1; } return 0; } static void freezeprocs(int stop) { DIR *d; struct dirent *e; char path[64], cmd[4096]; int fd, n; d = opendir("/proc"); if(!d) return; while((e = readdir(d)) != NULL){ int pid; if(e->d_name[0] < '0' || e->d_name[0] > '9') continue; pid = atoi(e->d_name); if(pid <= 1) continue; snprintf(path, sizeof path, "/proc/%d/cmdline", pid); fd = open(path, O_RDONLY); if(fd < 0) continue; n = read(fd, cmd, sizeof cmd - 1); close(fd); if(n <= 0) continue; cmd[n] = '\0'; if(!matches(cmd, n)) continue; if(stop){ if(kill(pid, SIGSTOP) == 0 && nfrozen < (int)(sizeof frozen / sizeof *frozen)) frozen[nfrozen++] = pid; }else kill(pid, SIGCONT); } closedir(d); if(!stop) nfrozen = 0; } static void screenoff(void) { wr(BLDIR "/brightness", 0); wr(BLANK, 1); screenon = 0; } static void screenwake(void) { wr(BLDIR "/brightness", level); wr(BLANK, 0); screenon = 1; } static void idle(void) { screenoff(); freezeprocs(1); logmsg("idle: %d frozen", nfrozen); } static void wake(void) { freezeprocs(0); screenwake(); logmsg("wake"); } static double now(void) { struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return ts.tv_sec + ts.tv_nsec / 1e9; } int main(int argc, char *argv[]) { const char *dev = argc > 1 ? argv[1] : EVENT; unsigned char buf[EVSZ * 16]; int fd, len = 0; double down = 0; signal(SIGHUP, SIG_IGN); maxbright = rd(BLDIR "/max_brightness", 4095); level = rd(BLDIR "/brightness", 512); if(level <= 0) level = 512; screenon = rd(BLANK, 0) == 0; if(!screenon) wr(BLDIR "/brightness", 0); logmsg("start on=%d level=%d max=%d", screenon, level, maxbright); for(;;){ ssize_t n; int off; fd = open(dev, O_RDONLY); if(fd < 0){ sleep(1); continue; } for(;;){ unsigned short type; short code; int value; n = read(fd, buf + len, sizeof buf - len); if(n <= 0) break; len += n; while(len >= EVSZ){ memcpy(&type, buf + 16, 2); memcpy(&code, buf + 18, 2); memcpy(&value, buf + 20, 4); memmove(buf, buf + EVSZ, len - EVSZ); len -= EVSZ; if(type != 1 || code != KEY_POWER) continue; if(value == 1) down = now(); else if(value == 0 && down > 0){ off = now() - down >= LONG_PRESS; down = 0; if(off){ logmsg("long press: poweroff"); sync(); execl("/sbin/poweroff", "poweroff", (char *)0); }else if(screenon) idle(); else wake(); } } } close(fd); usleep(200000); } }