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

+53 -17
View File
@@ -16,9 +16,10 @@
#include <time.h>
#include <unistd.h>
#define MODEM_SOCK "/run/geminux/modem.sock"
#define STATE_FILE "/run/geminux/modem-state"
#define AT_DEV "/dev/ttyC0"
#define MODEM_SOCK "/run/geminux/modem.sock"
#define MODEM_SOCK_FALLBACK "/tmp/geminux-modem.sock"
#define STATE_FILE "/run/geminux/modem-state"
#define AT_DEV "/dev/ttyC0"
typedef struct {
int epfd;
@@ -29,6 +30,7 @@ typedef struct {
bool muted;
bool speaker;
char active_peer[64];
char sock_path[64];
} modem_ctx_t;
static void spawn_bg(const char *const argv[]) {
@@ -182,9 +184,31 @@ static void handle_cmd(modem_ctx_t *ctx, int cfd) {
}
}
static int bind_modem_socket(modem_ctx_t *ctx) {
const char *candidates[2] = { MODEM_SOCK, MODEM_SOCK_FALLBACK };
for (int i = 0; i < 2; i++) {
unlink(candidates[i]);
int fd = socket(AF_UNIX, SOCK_STREAM | O_NONBLOCK | O_CLOEXEC, 0);
if (fd < 0) return -1;
struct sockaddr_un addr;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", candidates[i]);
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) == 0 && listen(fd, 8) == 0) {
chmod(candidates[i], 0660);
snprintf(ctx->sock_path, sizeof(ctx->sock_path), "%s", candidates[i]);
return fd;
}
close(fd);
}
ctx->sock_path[0] = '\0';
return -1;
}
int main(void) {
modem_ctx_t ctx;
memset(&ctx, 0, sizeof(ctx));
ctx.srv_fd = -1;
ctx.at_fd = -1;
mkdir("/run/geminux", 0755);
@@ -195,18 +219,7 @@ int main(void) {
sigprocmask(SIG_BLOCK, &mask, NULL);
ctx.sig_fd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
unlink(MODEM_SOCK);
ctx.srv_fd = socket(AF_UNIX, SOCK_STREAM | O_NONBLOCK | O_CLOEXEC, 0);
if (ctx.srv_fd >= 0) {
struct sockaddr_un addr;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", MODEM_SOCK);
if (bind(ctx.srv_fd, (struct sockaddr *)&addr, sizeof(addr)) == 0) {
listen(ctx.srv_fd, 8);
chmod(MODEM_SOCK, 0660);
}
}
ctx.srv_fd = bind_modem_socket(&ctx);
ctx.epfd = epoll_create1(EPOLL_CLOEXEC);
int fixed[2] = {ctx.sig_fd, ctx.srv_fd};
@@ -232,15 +245,38 @@ int main(void) {
if (fd == ctx.sig_fd) {
running = false;
} else if (fd == ctx.srv_fd) {
if (events[i].events & (EPOLLERR | EPOLLHUP)) {
epoll_ctl(ctx.epfd, EPOLL_CTL_DEL, ctx.srv_fd, NULL);
close(ctx.srv_fd);
ctx.srv_fd = -1;
continue;
}
int cfd;
while ((cfd = accept4(ctx.srv_fd, NULL, NULL, SOCK_CLOEXEC)) >= 0) {
handle_cmd(&ctx, cfd);
close(cfd);
}
if (errno == EINVAL || errno == EBADF || errno == ENOTSOCK || errno == EOPNOTSUPP) {
epoll_ctl(ctx.epfd, EPOLL_CTL_DEL, ctx.srv_fd, NULL);
close(ctx.srv_fd);
ctx.srv_fd = -1;
}
} else if (fd == ctx.at_fd) {
if (events[i].events & (EPOLLHUP | EPOLLERR)) {
epoll_ctl(ctx.epfd, EPOLL_CTL_DEL, ctx.at_fd, NULL);
close(ctx.at_fd);
ctx.at_fd = -1;
write_state(&ctx);
continue;
}
char atbuf[512] = {0};
ssize_t r = read(ctx.at_fd, atbuf, sizeof(atbuf) - 1);
if (r > 0) {
if (r == 0 || (r < 0 && errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)) {
epoll_ctl(ctx.epfd, EPOLL_CTL_DEL, ctx.at_fd, NULL);
close(ctx.at_fd);
ctx.at_fd = -1;
write_state(&ctx);
} else if (r > 0) {
if (strstr(atbuf, "RING")) {
const char *ntf[] = {"notify-send", "Incoming Call", "Voice call on MT6797 LTE", NULL};
spawn_bg(ntf);
@@ -255,7 +291,7 @@ int main(void) {
}
}
unlink(MODEM_SOCK);
if (ctx.sock_path[0]) unlink(ctx.sock_path);
close(ctx.epfd);
return 0;
}
+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) {
+30 -16
View File
@@ -41,34 +41,48 @@ static void read_battery(char *out, size_t sz) {
snprintf(out, sz, "BAT:--");
DIR *d = opendir("/sys/class/power_supply");
if (!d) return;
bool usb_online = false;
char cap[16] = {0}, st[24] = {0};
bool have_bat = false;
struct dirent *de;
while ((de = readdir(d)) != NULL) {
if (de->d_name[0] == '.') continue;
char p[320], typ[32] = {0}, cap[16] = {0}, st[24] = {0};
char p[320], typ[32] = {0};
snprintf(p, sizeof(p), "/sys/class/power_supply/%s/type", de->d_name);
if (read_line(p, typ, sizeof(typ)) != 0 || strcmp(typ, "Battery") != 0) continue;
snprintf(p, sizeof(p), "/sys/class/power_supply/%s/capacity", de->d_name);
read_line(p, cap, sizeof(cap));
snprintf(p, sizeof(p), "/sys/class/power_supply/%s/status", de->d_name);
read_line(p, st, sizeof(st));
const char *tag = (strcmp(st, "Charging") == 0) ? "+" :
(strcmp(st, "Full") == 0) ? "=" : "";
snprintf(out, sz, "%s%% %s%s", cap[0] ? cap : "?", tag,
strcmp(st, "Charging") == 0 ? "CHG" : "BAT");
break;
if (read_line(p, typ, sizeof(typ)) != 0) continue;
if (strcmp(typ, "Battery") == 0 && !have_bat) {
snprintf(p, sizeof(p), "/sys/class/power_supply/%s/capacity", de->d_name);
read_line(p, cap, sizeof(cap));
snprintf(p, sizeof(p), "/sys/class/power_supply/%s/status", de->d_name);
read_line(p, st, sizeof(st));
have_bat = true;
} else if (strcmp(typ, "Battery") != 0) {
char onl[16] = {0};
snprintf(p, sizeof(p), "/sys/class/power_supply/%s/online", de->d_name);
if (read_line(p, onl, sizeof(onl)) == 0 && atoi(onl) == 1) {
usb_online = true;
}
}
}
closedir(d);
if (!have_bat) return;
if (strcmp(st, "Charging") == 0) {
snprintf(out, sz, "%s%% +CHG", cap[0] ? cap : "?");
} else if (strcmp(st, "Full") == 0) {
snprintf(out, sz, "%s%% =FULL", cap[0] ? cap : "?");
} else if (usb_online) {
snprintf(out, sz, "%s%% ~USB", cap[0] ? cap : "?");
} else {
snprintf(out, sz, "%s%% BAT", cap[0] ? cap : "?");
}
}
static void update_status(Display *dpy) {
char bat[48], mdm[64] = {0}, prof[32] = {0}, net[64] = {0};
char bat[48], mdm[64] = {0}, net[64] = {0};
read_battery(bat, sizeof(bat));
if (read_line("/run/geminux/modem-state", mdm, sizeof(mdm)) != 0) {
snprintf(mdm, sizeof(mdm), "4G:OFF");
}
if (read_line("/run/geminux/power-profile", prof, sizeof(prof)) != 0) {
snprintf(prof, sizeof(prof), "balanced");
}
if (iface_up("wlan0")) snprintf(net, sizeof(net), "WiFi:UP");
else if (iface_up("usb0")) snprintf(net, sizeof(net), "USB:UP");
@@ -82,7 +96,7 @@ static void update_status(Display *dpy) {
strftime(clk, sizeof(clk), "%a %d %H:%M", &tm);
char bar[256];
snprintf(bar, sizeof(bar), " %s | %s | %s | %s | %s ", net, mdm, prof, bat, clk);
snprintf(bar, sizeof(bar), " %s | %s | %s | %s ", net, mdm, bat, clk);
XStoreName(dpy, DefaultRootWindow(dpy), bar);
XFlush(dpy);
}