Bring up SSD2092 display/touch, USB NCM, HiDPI XLibre/DWM, and USB charging

This commit is contained in:
Luxferre committed 2026-11-22 15:49:38 +02:00
1 parent 034c1f61b1
commit 9db15e30ce
150 files changed
+2356 -446

No files matched your search

+121 -9
View File
@@ -7,6 +7,8 @@
#include <grp.h>
#include <linux/input.h>
#include <linux/netlink.h>
#include <poll.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
@@ -18,6 +20,7 @@
#include <sys/stat.h>
#include <sys/timerfd.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <unistd.h>
#define SOCK_PATH "/run/geminux/powerd.sock"
@@ -57,7 +60,15 @@ static int read_sysfs_line(const char *path, char *buf, size_t sz) {
return 0;
}
static void ensure_run_dir(void) {
mkdir("/run/geminux", 0775);
chmod("/run/geminux", 0775);
struct group *gr = getgrnam("wheel");
if (gr) chown("/run/geminux", 0, gr->gr_gid);
}
static void refresh_ac_and_hdmi(powerd_ctx_t *ctx) {
write_sysfs_str("/sys/class/power_supply/battery/charge_control_end_threshold", "100");
ctx->policy.on_ac_power = false;
DIR *d = opendir("/sys/class/power_supply");
if (d) {
@@ -97,10 +108,13 @@ static void refresh_ac_and_hdmi(powerd_ctx_t *ctx) {
static void apply_cpu_policy(powerd_ctx_t *ctx) {
geminux_profile_params_t par = geminux_policy_params(ctx->policy.profile, ctx->policy.on_ac_power);
for (int cpu = 8; cpu <= 9; cpu++) {
char p[128];
snprintf(p, sizeof(p), "/sys/devices/system/cpu/cpu%d/online", cpu);
write_sysfs_str(p, par.a72_online ? "1" : "0");
bool a72_ready = (access("/sys/module/mt6797_a72", F_OK) == 0);
if (!par.a72_online || a72_ready) {
for (int cpu = 8; cpu <= 9; cpu++) {
char p[128];
snprintf(p, sizeof(p), "/sys/devices/system/cpu/cpu%d/online", cpu);
write_sysfs_str(p, par.a72_online ? "1" : "0");
}
}
DIR *d = opendir("/sys/devices/system/cpu/cpufreq");
@@ -123,7 +137,7 @@ static void apply_cpu_policy(powerd_ctx_t *ctx) {
closedir(d);
}
mkdir("/run/geminux", 0755);
ensure_run_dir();
write_sysfs_str(PROFILE_FILE, geminux_policy_profile_name(ctx->policy.profile));
struct itimerspec its;
@@ -158,14 +172,92 @@ static void toggle_backlight(powerd_ctx_t *ctx, bool on) {
closedir(d);
}
static bool is_lock_running(void) {
DIR *d = opendir("/proc");
if (!d) return false;
struct dirent *de;
while ((de = readdir(d)) != NULL) {
if (de->d_name[0] < '0' || de->d_name[0] > '9') continue;
char p[300], comm[32] = {0};
snprintf(p, sizeof(p), "/proc/%s/comm", de->d_name);
if (read_sysfs_line(p, comm, sizeof(comm)) == 0 && strcmp(comm, "geminux-lock") == 0) {
closedir(d);
return true;
}
}
closedir(d);
return false;
}
static void lock_x11_session(void) {
if (access("/tmp/.X11-unix/X0", F_OK) != 0) return;
if (is_lock_running()) return;
struct passwd *pw = getpwnam("user");
if (!pw) return;
int pfd[2];
if (pipe(pfd) < 0) return;
fcntl(pfd[0], F_SETFD, FD_CLOEXEC);
pid_t pid = fork();
if (pid < 0) {
close(pfd[0]);
close(pfd[1]);
return;
}
if (pid == 0) {
close(pfd[0]);
setsid();
pid_t p2 = fork();
if (p2 == 0) {
int devnull = open("/dev/null", O_RDWR);
if (devnull >= 0) {
if (devnull != STDIN_FILENO) dup2(devnull, STDIN_FILENO);
if (devnull != STDOUT_FILENO) dup2(devnull, STDOUT_FILENO);
if (devnull != STDERR_FILENO) dup2(devnull, STDERR_FILENO);
if (devnull > 2 && devnull != pfd[1]) close(devnull);
}
initgroups(pw->pw_name, pw->pw_gid);
if (setgid(pw->pw_gid) == 0 && setuid(pw->pw_uid) == 0) {
const char *home = (pw->pw_dir && pw->pw_dir[0]) ? pw->pw_dir : "/home/user";
char xauth[256], rfd_str[16];
snprintf(xauth, sizeof(xauth), "%s/.Xauthority", home);
snprintf(rfd_str, sizeof(rfd_str), "%d", pfd[1]);
setenv("HOME", home, 1);
setenv("USER", pw->pw_name, 1);
setenv("LOGNAME", pw->pw_name, 1);
setenv("DISPLAY", ":0", 1);
setenv("XAUTHORITY", xauth, 1);
setenv("GEMINUX_LOCK_READY_FD", rfd_str, 1);
const char *argv[] = {"/usr/bin/geminux-lock", NULL};
execv(argv[0], (char *const *)argv);
}
_exit(127);
}
close(pfd[1]);
_exit(0);
}
close(pfd[1]);
waitpid(pid, NULL, 0);
struct pollfd pf = { .fd = pfd[0], .events = POLLIN };
poll(&pf, 1, 500);
close(pfd[0]);
}
static void trigger_suspend(powerd_ctx_t *ctx) {
lock_x11_session();
toggle_backlight(ctx, false);
write_sysfs_str("/sys/power/mem_sleep", "deep");
if (write_sysfs_str("/sys/power/state", "mem") == 0) {
/* Woken back up from S3 suspend */
if (write_sysfs_str("/sys/power/state", "mem") == 0 ||
write_sysfs_str("/sys/power/state", "freeze") == 0) {
/* Woken back up from suspend */
toggle_backlight(ctx, true);
refresh_ac_and_hdmi(ctx);
apply_cpu_policy(ctx);
} else if (!ctx->policy.lid_closed) {
toggle_backlight(ctx, true);
}
}
@@ -175,6 +267,7 @@ static void scan_input_devices(powerd_ctx_t *ctx) {
close(ctx->ev_fds[i]);
}
ctx->num_ev = 0;
ctx->policy.lid_closed = false;
for (int i = 0; i < 32 && ctx->num_ev < MAX_EVDEVS; i++) {
char p[64];
@@ -184,6 +277,14 @@ static void scan_input_devices(powerd_ctx_t *ctx) {
unsigned long evbits = 0;
if (ioctl(fd, EVIOCGBIT(0, sizeof(evbits)), &evbits) >= 0 &&
((evbits & (1UL << EV_SW)) || (evbits & (1UL << EV_KEY)))) {
if (evbits & (1UL << EV_SW)) {
unsigned long swbits = 0, swstate = 0;
if (ioctl(fd, EVIOCGBIT(EV_SW, sizeof(swbits)), &swbits) >= 0 &&
(swbits & (1UL << SW_LID)) &&
ioctl(fd, EVIOCGSW(sizeof(swstate)), &swstate) >= 0) {
ctx->policy.lid_closed = !!(swstate & (1UL << SW_LID));
}
}
struct epoll_event ev = { .events = EPOLLIN, .data.fd = fd };
if (epoll_ctl(ctx->epfd, EPOLL_CTL_ADD, fd, &ev) == 0) {
ctx->ev_fds[ctx->num_ev++] = fd;
@@ -238,7 +339,7 @@ static void handle_client_cmd(powerd_ctx_t *ctx, int cfd) {
}
static int setup_unix_server(void) {
mkdir("/run/geminux", 0755);
ensure_run_dir();
unlink(SOCK_PATH);
int fd = socket(AF_UNIX, SOCK_STREAM | O_NONBLOCK | O_CLOEXEC, 0);
if (fd < 0) return -1;
@@ -337,7 +438,14 @@ int main(void) {
}
} else if (fd == ctx.uev_fd) {
char ubuf[2048];
while (recv(ctx.uev_fd, ubuf, sizeof(ubuf), 0) > 0) {}
ssize_t ur;
bool need_ev_rescan = false;
while ((ur = recv(ctx.uev_fd, ubuf, sizeof(ubuf), 0)) > 0) {
if (memmem(ubuf, (size_t)ur, "SUBSYSTEM=input", 15)) {
need_ev_rescan = true;
}
}
if (need_ev_rescan) scan_input_devices(&ctx);
refresh_ac_and_hdmi(&ctx);
apply_cpu_policy(&ctx);
} else {
@@ -346,11 +454,15 @@ int main(void) {
if (iev.type == EV_SW && iev.code == SW_LID) {
ctx.policy.lid_closed = (iev.value != 0);
if (ctx.policy.lid_closed) {
lock_x11_session();
if (geminux_should_suspend_on_lid(&ctx.policy)) {
trigger_suspend(&ctx);
} else {
toggle_backlight(&ctx, false);
}
} else {
toggle_backlight(&ctx, true);
refresh_ac_and_hdmi(&ctx);
apply_cpu_policy(&ctx);
}
} else if (iev.type == EV_KEY && iev.code == KEY_POWER && iev.value == 1) {