Fix SSD2092 wake, fuel-gauge jump, XLibre rotation, media keys, and popup rendering
This commit is contained in:
1 parent
7f3b3c2c0c
commit
dbc0275979
43 files changed
+3388
-115
No files matched your search
@@ -18,10 +18,14 @@ APPS = \
|
||||
geminux-control \
|
||||
geminux-themes \
|
||||
geminux-datetime \
|
||||
geminux-lock
|
||||
geminux-lock \
|
||||
geminux-media-key
|
||||
|
||||
all: $(APPS)
|
||||
|
||||
geminux-media-key: geminux-media-key.c
|
||||
$(CC) -std=c11 -Wall -Wextra -O2 -o $@ $<
|
||||
|
||||
geminux-datetime: geminux-datetime.c ../geminux-daemons/geminux-tz-valid.h ../libgemui/gemui.h
|
||||
$(CC) $(CFLAGS) -o $@ $< $(LIBS)
|
||||
|
||||
|
||||
@@ -2,9 +2,13 @@
|
||||
#include "gemui.h"
|
||||
|
||||
#include <dirent.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/wait.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define HIT_BRI 1
|
||||
#define HIT_VOL 2
|
||||
@@ -20,9 +24,12 @@
|
||||
typedef struct {
|
||||
gemui_app_t *app;
|
||||
char bl_dir[256];
|
||||
char bl_path[320];
|
||||
int bl_cur;
|
||||
int bl_max;
|
||||
int vol_pct;
|
||||
pid_t vol_pid;
|
||||
int pending_vol_pct;
|
||||
int profile; /* 0=eco, 1=balanced, 2=performance */
|
||||
gemui_rect_t bri_rect;
|
||||
gemui_rect_t vol_rect;
|
||||
@@ -31,6 +38,7 @@ typedef struct {
|
||||
|
||||
static void detect_backlight(ctrl_state_t *st) {
|
||||
st->bl_dir[0] = '\0';
|
||||
st->bl_path[0] = '\0';
|
||||
st->bl_cur = 128;
|
||||
st->bl_max = 255;
|
||||
DIR *d = opendir("/sys/class/backlight");
|
||||
@@ -43,8 +51,8 @@ static void detect_backlight(ctrl_state_t *st) {
|
||||
char p[320];
|
||||
snprintf(p, sizeof(p), "%s/max_brightness", st->bl_dir);
|
||||
if (gemui_read_file_line(p, b, sizeof(b)) == 0 && atoi(b) > 0) st->bl_max = atoi(b);
|
||||
snprintf(p, sizeof(p), "%s/brightness", st->bl_dir);
|
||||
if (gemui_read_file_line(p, b, sizeof(b)) == 0) st->bl_cur = atoi(b);
|
||||
snprintf(st->bl_path, sizeof(st->bl_path), "%s/brightness", st->bl_dir);
|
||||
if (gemui_read_file_line(st->bl_path, b, sizeof(b)) == 0) st->bl_cur = atoi(b);
|
||||
break;
|
||||
}
|
||||
closedir(d);
|
||||
@@ -52,11 +60,32 @@ static void detect_backlight(ctrl_state_t *st) {
|
||||
|
||||
static void refresh_hw(ctrl_state_t *st) {
|
||||
detect_backlight(st);
|
||||
char out[128] = {0};
|
||||
const char *vcmd[] = {"wpctl", "get-volume", "@DEFAULT_AUDIO_SINK@", NULL};
|
||||
if (gemui_run_cmd_capture(vcmd, out, sizeof(out)) == 0) {
|
||||
char *p = strstr(out, "Volume:");
|
||||
if (p) st->vol_pct = (int)(atof(p + 7) * 100.0 + 0.5);
|
||||
bool got_vol = false;
|
||||
struct timespec ts;
|
||||
if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
|
||||
unsigned long long now_ms = (unsigned long long)ts.tv_sec * 1000ULL +
|
||||
(unsigned long long)(ts.tv_nsec / 1000000L);
|
||||
char cpath[96];
|
||||
snprintf(cpath, sizeof(cpath), "/tmp/geminux-vol-cache-%u", (unsigned)getuid());
|
||||
FILE *cf = fopen(cpath, "r");
|
||||
if (cf) {
|
||||
int pct = 0, mut = 0;
|
||||
unsigned long long cts = 0;
|
||||
if (fscanf(cf, "%d %d %llu", &pct, &mut, &cts) == 3 &&
|
||||
pct >= 0 && pct <= 200 && now_ms >= cts && (now_ms - cts) <= 4000ULL) {
|
||||
st->vol_pct = pct;
|
||||
got_vol = true;
|
||||
}
|
||||
fclose(cf);
|
||||
}
|
||||
}
|
||||
if (!got_vol) {
|
||||
char out[128] = {0};
|
||||
const char *vcmd[] = {"wpctl", "get-volume", "@DEFAULT_AUDIO_SINK@", NULL};
|
||||
if (gemui_run_cmd_capture(vcmd, out, sizeof(out)) == 0) {
|
||||
char *p = strstr(out, "Volume:");
|
||||
if (p) st->vol_pct = (int)(atof(p + 7) * 100.0 + 0.5);
|
||||
}
|
||||
}
|
||||
char prof[64] = {0};
|
||||
if (gemui_read_file_line("/run/geminux/power-profile", prof, sizeof(prof)) == 0) {
|
||||
@@ -66,28 +95,112 @@ static void refresh_hw(ctrl_state_t *st) {
|
||||
}
|
||||
}
|
||||
|
||||
static void set_brightness_norm(ctrl_state_t *st, double norm) {
|
||||
if (norm < 0.05) norm = 0.05;
|
||||
if (norm > 1.0) norm = 1.0;
|
||||
st->bl_cur = (int)(norm * st->bl_max);
|
||||
int pct = (int)(norm * 100.0 + 0.5);
|
||||
static void dispatch_vol_pct(ctrl_state_t *st, int pct) {
|
||||
struct timespec ts;
|
||||
if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
|
||||
unsigned long long now_ms = (unsigned long long)ts.tv_sec * 1000ULL +
|
||||
(unsigned long long)(ts.tv_nsec / 1000000L);
|
||||
char cpath[96], ctmp[112];
|
||||
snprintf(cpath, sizeof(cpath), "/tmp/geminux-vol-cache-%u", (unsigned)getuid());
|
||||
snprintf(ctmp, sizeof(ctmp), "%s.tmp.%ld", cpath, (long)getpid());
|
||||
int cfd = open(ctmp, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
|
||||
if (cfd >= 0) {
|
||||
char cbuf[64];
|
||||
int cn = snprintf(cbuf, sizeof(cbuf), "%d 0 %llu\n", pct, now_ms);
|
||||
if (cn > 0) {
|
||||
ssize_t cw = write(cfd, cbuf, (size_t)cn);
|
||||
(void)cw;
|
||||
}
|
||||
close(cfd);
|
||||
if (rename(ctmp, cpath) != 0) unlink(ctmp);
|
||||
}
|
||||
}
|
||||
char arg[32];
|
||||
snprintf(arg, sizeof(arg), "%d%%", pct);
|
||||
const char *cmd[] = {"brightnessctl", "set", arg, NULL};
|
||||
gemui_run_cmd_capture(cmd, NULL, 0);
|
||||
pid_t pid = fork();
|
||||
if (pid < 0) return;
|
||||
if (pid == 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) close(devnull);
|
||||
}
|
||||
const char *cmd[] = {"wpctl", "set-volume", "@DEFAULT_AUDIO_SINK@", arg, NULL};
|
||||
execvp(cmd[0], (char *const *)cmd);
|
||||
_exit(127);
|
||||
}
|
||||
st->vol_pid = pid;
|
||||
}
|
||||
|
||||
static void flush_vol_helper(ctrl_state_t *st) {
|
||||
if (st->vol_pid > 0) {
|
||||
int status = 0;
|
||||
pid_t r = waitpid(st->vol_pid, &status, WNOHANG);
|
||||
if (r == 0) return;
|
||||
st->vol_pid = -1;
|
||||
}
|
||||
if (st->pending_vol_pct >= 0) {
|
||||
int pct = st->pending_vol_pct;
|
||||
st->pending_vol_pct = -1;
|
||||
dispatch_vol_pct(st, pct);
|
||||
}
|
||||
}
|
||||
|
||||
static void on_timer(gemui_app_t *app, void *ud) {
|
||||
ctrl_state_t *st = ud;
|
||||
flush_vol_helper(st);
|
||||
if (st->vol_pid <= 0 && st->pending_vol_pct < 0) {
|
||||
gemui_set_timer_ms(app, 0, NULL, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
static void set_brightness_norm(ctrl_state_t *st, double norm) {
|
||||
if (norm < 0.01) norm = 0.01;
|
||||
if (norm > 1.0) norm = 1.0;
|
||||
if (st->bl_max <= 0) st->bl_max = 255;
|
||||
int v = (int)(norm * (double)st->bl_max + 0.5);
|
||||
if (v < 1) v = 1;
|
||||
if (v > st->bl_max) v = st->bl_max;
|
||||
if (v == st->bl_cur) return;
|
||||
|
||||
st->bl_cur = v;
|
||||
bool wrote_sysfs = false;
|
||||
if (st->bl_path[0]) {
|
||||
int fd = open(st->bl_path, O_WRONLY | O_CLOEXEC);
|
||||
if (fd >= 0) {
|
||||
char buf[32];
|
||||
int n = snprintf(buf, sizeof(buf), "%d\n", v);
|
||||
if (n > 0 && write(fd, buf, (size_t)n) == (ssize_t)n) {
|
||||
wrote_sysfs = true;
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
}
|
||||
if (!wrote_sysfs) {
|
||||
int pct = (int)(norm * 100.0 + 0.5);
|
||||
if (pct < 1) pct = 1;
|
||||
char arg[32];
|
||||
snprintf(arg, sizeof(arg), "%d%%", pct);
|
||||
const char *cmd[] = {"brightnessctl", "set", arg, NULL};
|
||||
gemui_spawn_detached(cmd);
|
||||
}
|
||||
gemui_request_redraw(st->app);
|
||||
}
|
||||
|
||||
static void set_volume_norm(ctrl_state_t *st, double norm) {
|
||||
if (norm < 0.0) norm = 0.0;
|
||||
if (norm > 1.0) norm = 1.0;
|
||||
st->vol_pct = (int)(norm * 100.0 + 0.5);
|
||||
char arg[32];
|
||||
snprintf(arg, sizeof(arg), "%d%%", st->vol_pct);
|
||||
const char *cmd[] = {"wpctl", "set-volume", "@DEFAULT_AUDIO_SINK@", arg, NULL};
|
||||
gemui_run_cmd_capture(cmd, NULL, 0);
|
||||
const char *pk[] = {"pkill", "-USR1", "geminux-statusd", NULL};
|
||||
gemui_run_cmd_capture(pk, NULL, 0);
|
||||
int pct = (int)(norm * 100.0 + 0.5);
|
||||
if (pct == st->vol_pct) return;
|
||||
|
||||
st->vol_pct = pct;
|
||||
st->pending_vol_pct = pct;
|
||||
flush_vol_helper(st);
|
||||
if (st->vol_pid > 0) {
|
||||
gemui_set_timer_ms(st->app, 40, on_timer, st);
|
||||
}
|
||||
gemui_request_redraw(st->app);
|
||||
}
|
||||
|
||||
@@ -159,6 +272,7 @@ static void on_ptr(gemui_app_t *app, int type, int x, int y, unsigned int btn, v
|
||||
(void)btn;
|
||||
ctrl_state_t *st = ud;
|
||||
if (type == GEMUI_PTR_UP) {
|
||||
if (st->active_drag == HIT_VOL) flush_vol_helper(st);
|
||||
st->active_drag = 0;
|
||||
return;
|
||||
}
|
||||
@@ -219,6 +333,8 @@ int main(void) {
|
||||
ctrl_state_t st;
|
||||
memset(&st, 0, sizeof(st));
|
||||
st.vol_pct = 50;
|
||||
st.vol_pid = -1;
|
||||
st.pending_vol_pct = -1;
|
||||
st.profile = 1;
|
||||
refresh_hw(&st);
|
||||
|
||||
@@ -228,6 +344,11 @@ int main(void) {
|
||||
gemui_handlers_t h = { .on_draw = on_draw, .on_key = on_key, .on_ptr = on_ptr };
|
||||
gemui_set_handlers(st.app, &h, &st);
|
||||
int rc = gemui_run(st.app);
|
||||
if (st.vol_pid > 0) waitpid(st.vol_pid, NULL, 0);
|
||||
if (st.pending_vol_pct >= 0) {
|
||||
dispatch_vol_pct(&st, st.pending_vol_pct);
|
||||
if (st.vol_pid > 0) waitpid(st.vol_pid, NULL, 0);
|
||||
}
|
||||
gemui_destroy(st.app);
|
||||
return rc;
|
||||
}
|
||||
@@ -23,6 +23,7 @@ typedef struct {
|
||||
int last_min;
|
||||
int last_yday;
|
||||
int timer_ms;
|
||||
int ready_fd;
|
||||
bool grabbed;
|
||||
bool checking;
|
||||
} lock_state_t;
|
||||
@@ -100,6 +101,17 @@ static void attempt_unlock(lock_state_t *st) {
|
||||
|
||||
static void on_timer(gemui_app_t *app, void *ud) {
|
||||
lock_state_t *st = ud;
|
||||
if (st->ready_fd >= 0) {
|
||||
ssize_t wr = write(st->ready_fd, "1", 1);
|
||||
(void)wr;
|
||||
close(st->ready_fd);
|
||||
st->ready_fd = -1;
|
||||
int next_ms = st->grabbed ? 15000 : 250;
|
||||
if (st->timer_ms != next_ms) {
|
||||
st->timer_ms = next_ms;
|
||||
gemui_set_timer_ms(app, next_ms, on_timer, st);
|
||||
}
|
||||
}
|
||||
if (!st->grabbed) {
|
||||
st->grabbed = gemui_grab_input(app);
|
||||
if (st->grabbed && st->timer_ms != 15000) {
|
||||
@@ -210,6 +222,7 @@ int main(void) {
|
||||
memset(&st, 0, sizeof(st));
|
||||
st.last_min = -1;
|
||||
st.last_yday = -1;
|
||||
st.ready_fd = -1;
|
||||
struct passwd *pw = getpwuid(getuid());
|
||||
snprintf(st.user, sizeof(st.user), "%s", (pw && pw->pw_name) ? pw->pw_name : "user");
|
||||
refresh_battery(&st);
|
||||
@@ -222,16 +235,15 @@ int main(void) {
|
||||
if (rfd_s && rfd_s[0]) {
|
||||
int rfd = atoi(rfd_s);
|
||||
if (rfd >= 0) {
|
||||
ssize_t wr = write(rfd, "1", 1);
|
||||
(void)wr;
|
||||
close(rfd);
|
||||
st.ready_fd = rfd;
|
||||
}
|
||||
unsetenv("GEMINUX_LOCK_READY_FD");
|
||||
}
|
||||
|
||||
gemui_handlers_t h = { .on_draw = on_draw, .on_key = on_key, .on_ptr = on_ptr };
|
||||
gemui_set_handlers(st.app, &h, &st);
|
||||
st.timer_ms = st.grabbed ? 15000 : 250;
|
||||
/* Signal readiness after the first on_draw + XFlush completes on Xorg. */
|
||||
st.timer_ms = (st.ready_fd >= 0) ? 20 : (st.grabbed ? 15000 : 250);
|
||||
gemui_set_timer_ms(st.app, st.timer_ms, on_timer, &st);
|
||||
int rc = gemui_run(st.app);
|
||||
gemui_ungrab_input(st.app);
|
||||
|
||||
@@ -0,0 +1,541 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
|
||||
#include <ctype.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/file.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/un.h>
|
||||
#include <sys/wait.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static void redirect_devnull(int keep_stdout) {
|
||||
int dn = open("/dev/null", O_RDWR);
|
||||
if (dn >= 0) {
|
||||
if (dn != STDIN_FILENO) dup2(dn, STDIN_FILENO);
|
||||
if (!keep_stdout && dn != STDOUT_FILENO) dup2(dn, STDOUT_FILENO);
|
||||
if (dn != STDERR_FILENO) dup2(dn, STDERR_FILENO);
|
||||
if (dn > 2) close(dn);
|
||||
}
|
||||
}
|
||||
|
||||
static int run_cmd(const char *const argv[], char *out, size_t out_sz) {
|
||||
int pfd[2] = {-1, -1};
|
||||
if (out && out_sz > 0) {
|
||||
out[0] = '\0';
|
||||
if (pipe(pfd) < 0) return -1;
|
||||
}
|
||||
pid_t pid = fork();
|
||||
if (pid < 0) {
|
||||
if (pfd[0] >= 0) close(pfd[0]);
|
||||
if (pfd[1] >= 0) close(pfd[1]);
|
||||
return -1;
|
||||
}
|
||||
if (pid == 0) {
|
||||
if (pfd[1] >= 0) {
|
||||
close(pfd[0]);
|
||||
if (pfd[1] != STDOUT_FILENO) dup2(pfd[1], STDOUT_FILENO);
|
||||
if (pfd[1] > 2) close(pfd[1]);
|
||||
redirect_devnull(1);
|
||||
} else {
|
||||
redirect_devnull(0);
|
||||
}
|
||||
execvp(argv[0], (char *const *)argv);
|
||||
_exit(127);
|
||||
}
|
||||
if (pfd[1] >= 0) close(pfd[1]);
|
||||
if (pfd[0] >= 0) {
|
||||
size_t pos = 0;
|
||||
while (pos + 1 < out_sz) {
|
||||
ssize_t n = read(pfd[0], out + pos, out_sz - 1 - pos);
|
||||
if (n < 0) {
|
||||
if (errno == EINTR) continue;
|
||||
break;
|
||||
}
|
||||
if (n == 0) break;
|
||||
pos += (size_t)n;
|
||||
}
|
||||
out[pos] = '\0';
|
||||
close(pfd[0]);
|
||||
}
|
||||
int status = 0;
|
||||
while (waitpid(pid, &status, 0) < 0 && errno == EINTR) {}
|
||||
return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
|
||||
}
|
||||
|
||||
static void spawn_detached(const char *const argv[]) {
|
||||
pid_t pid = fork();
|
||||
if (pid == 0) {
|
||||
setsid();
|
||||
pid_t pid2 = fork();
|
||||
if (pid2 == 0) {
|
||||
redirect_devnull(0);
|
||||
execvp(argv[0], (char *const *)argv);
|
||||
_exit(127);
|
||||
}
|
||||
_exit(0);
|
||||
}
|
||||
if (pid > 0) {
|
||||
int status = 0;
|
||||
while (waitpid(pid, &status, 0) < 0 && errno == EINTR) {}
|
||||
}
|
||||
}
|
||||
|
||||
static int read_int_file(const char *path, int *val) {
|
||||
int fd = open(path, O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0) return -1;
|
||||
char buf[64];
|
||||
ssize_t n = read(fd, buf, sizeof(buf) - 1);
|
||||
close(fd);
|
||||
if (n <= 0) return -1;
|
||||
buf[n] = '\0';
|
||||
*val = atoi(buf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int write_str_file(const char *path, const char *s) {
|
||||
int fd = open(path, O_WRONLY | O_CLOEXEC);
|
||||
if (fd < 0) return -1;
|
||||
size_t len = strlen(s);
|
||||
ssize_t w = write(fd, s, len);
|
||||
close(fd);
|
||||
return (w == (ssize_t)len) ? 0 : -1;
|
||||
}
|
||||
|
||||
static uint64_t mono_ms(void) {
|
||||
struct timespec ts;
|
||||
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) return 0;
|
||||
return (uint64_t)ts.tv_sec * 1000ULL + (uint64_t)(ts.tv_nsec / 1000000L);
|
||||
}
|
||||
|
||||
static void show_osd(const char *tag, int expire_ms, int pct_hint,
|
||||
const char *summary, const char *body) {
|
||||
char tag_hint[64];
|
||||
char val_hint[32];
|
||||
char exp_str[16];
|
||||
const char *rep_id = (strcmp(tag, "screenshot") == 0) ? "9911" : "9910";
|
||||
snprintf(tag_hint, sizeof(tag_hint), "string:x-dunst-stack-tag:%s", tag);
|
||||
snprintf(exp_str, sizeof(exp_str), "%d", expire_ms);
|
||||
|
||||
const char *argv[20];
|
||||
int idx = 0;
|
||||
argv[idx++] = "notify-send";
|
||||
argv[idx++] = "-a";
|
||||
argv[idx++] = "geminux-osd";
|
||||
argv[idx++] = "-u";
|
||||
argv[idx++] = "low";
|
||||
argv[idx++] = "-r";
|
||||
argv[idx++] = rep_id;
|
||||
argv[idx++] = "-t";
|
||||
argv[idx++] = exp_str;
|
||||
argv[idx++] = "-h";
|
||||
argv[idx++] = tag_hint;
|
||||
if (pct_hint >= 0) {
|
||||
if (pct_hint > 100) pct_hint = 100;
|
||||
snprintf(val_hint, sizeof(val_hint), "int:value:%d", pct_hint);
|
||||
argv[idx++] = "-h";
|
||||
argv[idx++] = val_hint;
|
||||
}
|
||||
argv[idx++] = summary;
|
||||
if (body && body[0]) {
|
||||
argv[idx++] = body;
|
||||
}
|
||||
argv[idx] = NULL;
|
||||
spawn_detached(argv);
|
||||
}
|
||||
|
||||
static int handle_brightness(bool up) {
|
||||
char bl_dir[280] = {0};
|
||||
DIR *d = opendir("/sys/class/backlight");
|
||||
if (d) {
|
||||
struct dirent *de;
|
||||
while ((de = readdir(d)) != NULL) {
|
||||
if (de->d_name[0] == '.') continue;
|
||||
snprintf(bl_dir, sizeof(bl_dir), "/sys/class/backlight/%s", de->d_name);
|
||||
break;
|
||||
}
|
||||
closedir(d);
|
||||
}
|
||||
|
||||
int max_bri = 255, cur_bri = 128;
|
||||
bool direct_ok = false;
|
||||
char p_bri[320] = {0}, p_max[320] = {0}, p_pwr[320] = {0};
|
||||
|
||||
if (bl_dir[0]) {
|
||||
snprintf(p_max, sizeof(p_max), "%s/max_brightness", bl_dir);
|
||||
snprintf(p_bri, sizeof(p_bri), "%s/brightness", bl_dir);
|
||||
snprintf(p_pwr, sizeof(p_pwr), "%s/bl_power", bl_dir);
|
||||
if (read_int_file(p_max, &max_bri) == 0 && max_bri > 0 &&
|
||||
read_int_file(p_bri, &cur_bri) == 0) {
|
||||
int step = (max_bri * 8 + 50) / 100;
|
||||
if (step < 1) step = 1;
|
||||
int min_bri = max_bri / 50;
|
||||
if (min_bri < 1) min_bri = 1;
|
||||
int next = up ? (cur_bri + step) : (cur_bri - step);
|
||||
if (up) {
|
||||
if (next > max_bri) next = max_bri;
|
||||
write_str_file(p_pwr, "0\n");
|
||||
} else {
|
||||
if (next < min_bri) next = min_bri;
|
||||
}
|
||||
char vbuf[32];
|
||||
snprintf(vbuf, sizeof(vbuf), "%d\n", next);
|
||||
if (write_str_file(p_bri, vbuf) == 0) {
|
||||
cur_bri = next;
|
||||
direct_ok = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!direct_ok) {
|
||||
const char *bcmd[] = {"brightnessctl", "set", up ? "+8%" : "8%-", NULL};
|
||||
run_cmd(bcmd, NULL, 0);
|
||||
if (p_bri[0]) read_int_file(p_bri, &cur_bri);
|
||||
}
|
||||
|
||||
int pct = (max_bri > 0) ? ((cur_bri * 100 + max_bri / 2) / max_bri) : 50;
|
||||
if (pct < 1) pct = 1;
|
||||
if (pct > 100) pct = 100;
|
||||
|
||||
char msg[64];
|
||||
snprintf(msg, sizeof(msg), "Brightness: %d%%", pct);
|
||||
show_osd("osd", 1400, pct, msg, NULL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool query_wpctl_vol(const char *target, int *pct_out, bool *muted_out) {
|
||||
char out[128];
|
||||
const char *cmd[] = {"wpctl", "get-volume", target, NULL};
|
||||
if (run_cmd(cmd, out, sizeof(out)) != 0) return false;
|
||||
const char *p = strstr(out, "Volume:");
|
||||
if (!p) return false;
|
||||
double v = atof(p + 7);
|
||||
if (v < 0.0) v = 0.0;
|
||||
if (v > 2.0) v = 2.0;
|
||||
*pct_out = (int)(v * 100.0 + 0.5);
|
||||
*muted_out = (strstr(out, "[MUTED]") != NULL);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool read_vol_cache(int *pct_out, bool *muted_out, uint64_t now) {
|
||||
char path[96];
|
||||
snprintf(path, sizeof(path), "/tmp/geminux-vol-cache-%u", (unsigned)getuid());
|
||||
FILE *f = fopen(path, "r");
|
||||
if (!f) return false;
|
||||
int pct = 0, mut = 0;
|
||||
unsigned long long ts = 0;
|
||||
bool ok = (fscanf(f, "%d %d %llu", &pct, &mut, &ts) == 3 &&
|
||||
pct >= 0 && pct <= 200 &&
|
||||
now >= ts && (now - ts) <= 4000ULL);
|
||||
fclose(f);
|
||||
if (ok) {
|
||||
*pct_out = pct;
|
||||
*muted_out = (mut != 0);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
static void write_vol_cache(int pct, bool muted, uint64_t now) {
|
||||
char path[96], tmp[112];
|
||||
snprintf(path, sizeof(path), "/tmp/geminux-vol-cache-%u", (unsigned)getuid());
|
||||
snprintf(tmp, sizeof(tmp), "%s.tmp.%ld", path, (long)getpid());
|
||||
int fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
|
||||
if (fd < 0) return;
|
||||
char buf[64];
|
||||
int n = snprintf(buf, sizeof(buf), "%d %d %llu\n", pct, muted ? 1 : 0, (unsigned long long)now);
|
||||
if (n > 0) {
|
||||
ssize_t w = write(fd, buf, (size_t)n);
|
||||
(void)w;
|
||||
}
|
||||
close(fd);
|
||||
if (rename(tmp, path) != 0) unlink(tmp);
|
||||
}
|
||||
|
||||
static int handle_audio(const char *action) {
|
||||
if (strcmp(action, "mic-mute") == 0) {
|
||||
const char *mmute[] = {"wpctl", "set-mute", "@DEFAULT_AUDIO_SOURCE@", "toggle", NULL};
|
||||
run_cmd(mmute, NULL, 0);
|
||||
int pct = 0;
|
||||
bool muted = false;
|
||||
if (query_wpctl_vol("@DEFAULT_AUDIO_SOURCE@", &pct, &muted)) {
|
||||
char msg[64];
|
||||
if (muted) {
|
||||
show_osd("osd", 1400, 0, "Microphone: Muted", NULL);
|
||||
} else {
|
||||
snprintf(msg, sizeof(msg), "Microphone: Active (%d%%)", pct);
|
||||
show_osd("osd", 1400, pct, msg, NULL);
|
||||
}
|
||||
} else {
|
||||
show_osd("osd", 1400, -1, "Microphone Toggled", NULL);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
char lock_path[96];
|
||||
snprintf(lock_path, sizeof(lock_path), "/tmp/geminux-vol-lock-%u", (unsigned)getuid());
|
||||
int lfd = open(lock_path, O_CREAT | O_RDWR | O_CLOEXEC, 0600);
|
||||
if (lfd >= 0) flock(lfd, LOCK_EX);
|
||||
|
||||
uint64_t now = mono_ms();
|
||||
int pct = 50;
|
||||
bool muted = false;
|
||||
bool have_state = read_vol_cache(&pct, &muted, now);
|
||||
if (!have_state) {
|
||||
have_state = query_wpctl_vol("@DEFAULT_AUDIO_SINK@", &pct, &muted);
|
||||
}
|
||||
|
||||
if (have_state) {
|
||||
int new_pct = pct;
|
||||
bool new_muted = muted;
|
||||
if (strcmp(action, "vol-up") == 0) {
|
||||
new_muted = false;
|
||||
new_pct = (pct + 5 <= 100) ? (pct + 5) : 100;
|
||||
} else if (strcmp(action, "vol-down") == 0) {
|
||||
new_pct = (pct >= 5) ? (pct - 5) : 0;
|
||||
} else if (strcmp(action, "vol-mute") == 0) {
|
||||
new_muted = !muted;
|
||||
}
|
||||
|
||||
write_vol_cache(new_pct, new_muted, mono_ms());
|
||||
if (lfd >= 0) {
|
||||
flock(lfd, LOCK_UN);
|
||||
close(lfd);
|
||||
lfd = -1;
|
||||
}
|
||||
|
||||
char msg[64];
|
||||
if (new_muted) {
|
||||
show_osd("osd", 1400, 0, "Audio: Muted", NULL);
|
||||
} else {
|
||||
snprintf(msg, sizeof(msg), "Volume: %d%%", new_pct);
|
||||
show_osd("osd", 1400, new_pct, msg, NULL);
|
||||
}
|
||||
|
||||
if (new_muted != muted) {
|
||||
const char *mcmd[] = {"wpctl", "set-mute", "@DEFAULT_AUDIO_SINK@", new_muted ? "1" : "0", NULL};
|
||||
run_cmd(mcmd, NULL, 0);
|
||||
}
|
||||
if (new_pct != pct) {
|
||||
char varg[16];
|
||||
snprintf(varg, sizeof(varg), "%d%%", new_pct);
|
||||
const char *vcmd[] = {"wpctl", "set-volume", "-l", "1.0", "@DEFAULT_AUDIO_SINK@", varg, NULL};
|
||||
run_cmd(vcmd, NULL, 0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (lfd >= 0) {
|
||||
flock(lfd, LOCK_UN);
|
||||
close(lfd);
|
||||
}
|
||||
if (strcmp(action, "vol-up") == 0) {
|
||||
const char *unmute[] = {"wpctl", "set-mute", "@DEFAULT_AUDIO_SINK@", "0", NULL};
|
||||
const char *raise[] = {"wpctl", "set-volume", "-l", "1.0", "@DEFAULT_AUDIO_SINK@", "5%+", NULL};
|
||||
run_cmd(unmute, NULL, 0);
|
||||
run_cmd(raise, NULL, 0);
|
||||
} else if (strcmp(action, "vol-down") == 0) {
|
||||
const char *lower[] = {"wpctl", "set-volume", "@DEFAULT_AUDIO_SINK@", "5%-", NULL};
|
||||
run_cmd(lower, NULL, 0);
|
||||
} else if (strcmp(action, "vol-mute") == 0) {
|
||||
const char *mute[] = {"wpctl", "set-mute", "@DEFAULT_AUDIO_SINK@", "toggle", NULL};
|
||||
run_cmd(mute, NULL, 0);
|
||||
}
|
||||
show_osd("osd", 1400, -1, "Audio Updated", NULL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int handle_screenshot(bool focused_window) {
|
||||
const char *home = getenv("HOME");
|
||||
if (!home || !home[0]) home = "/home/user";
|
||||
|
||||
char pic_dir[512];
|
||||
snprintf(pic_dir, sizeof(pic_dir), "%s/Pictures", home);
|
||||
mkdir(pic_dir, 0755);
|
||||
|
||||
time_t now = time(NULL);
|
||||
struct tm tm_now;
|
||||
localtime_r(&now, &tm_now);
|
||||
|
||||
char stamp[32];
|
||||
strftime(stamp, sizeof(stamp), "%Y%m%d-%H%M%S", &tm_now);
|
||||
|
||||
char path[600];
|
||||
snprintf(path, sizeof(path), "%s/screenshot-%s.png", pic_dir, stamp);
|
||||
for (int i = 1; i <= 99 && access(path, F_OK) == 0; i++) {
|
||||
snprintf(path, sizeof(path), "%s/screenshot-%s-%d.png", pic_dir, stamp, i);
|
||||
}
|
||||
|
||||
int rc = -1;
|
||||
if (focused_window) {
|
||||
const char *wcmd[] = {"scrot", "-u", path, NULL};
|
||||
rc = run_cmd(wcmd, NULL, 0);
|
||||
}
|
||||
if (rc != 0) {
|
||||
const char *fcmd[] = {"scrot", path, NULL};
|
||||
rc = run_cmd(fcmd, NULL, 0);
|
||||
}
|
||||
if (rc != 0) {
|
||||
show_osd("screenshot", 2500, -1, "Screenshot failed", "scrot returned an error");
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char *clip[] = {"xclip", "-selection", "clipboard", "-t", "image/png", "-i", path, NULL};
|
||||
spawn_detached(clip);
|
||||
|
||||
const char *base = strrchr(path, '/');
|
||||
base = base ? base + 1 : path;
|
||||
char body[320];
|
||||
snprintf(body, sizeof(body), "%s (copied to clipboard)", base);
|
||||
show_osd("screenshot", 2500, -1, "Screenshot saved", body);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool send_mpv_ipc(const char *json_cmd) {
|
||||
int fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
|
||||
if (fd < 0) return false;
|
||||
struct sockaddr_un addr;
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sun_family = AF_UNIX;
|
||||
snprintf(addr.sun_path, sizeof(addr.sun_path), "/tmp/geminux-mpv.sock");
|
||||
if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
|
||||
close(fd);
|
||||
return false;
|
||||
}
|
||||
size_t len = strlen(json_cmd);
|
||||
ssize_t w = send(fd, json_cmd, len, MSG_NOSIGNAL);
|
||||
close(fd);
|
||||
return (w == (ssize_t)len);
|
||||
}
|
||||
|
||||
static char read_proc_state(const char *pid_str) {
|
||||
char path[280], buf[512];
|
||||
snprintf(path, sizeof(path), "/proc/%s/stat", pid_str);
|
||||
int fd = open(path, O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0) return '\0';
|
||||
ssize_t n = read(fd, buf, sizeof(buf) - 1);
|
||||
close(fd);
|
||||
if (n <= 0) return '\0';
|
||||
buf[n] = '\0';
|
||||
const char *rparen = strrchr(buf, ')');
|
||||
if (!rparen || rparen[1] != ' ' || !rparen[2]) return '\0';
|
||||
return rparen[2];
|
||||
}
|
||||
|
||||
static int handle_media_transport(const char *action) {
|
||||
const char *mpv_json = NULL;
|
||||
const char *osd_msg = NULL;
|
||||
|
||||
if (strcmp(action, "play-pause") == 0) {
|
||||
mpv_json = "{\"command\":[\"cycle\",\"pause\"]}\n";
|
||||
osd_msg = "Media: Play / Pause";
|
||||
} else if (strcmp(action, "prev") == 0) {
|
||||
mpv_json = "{\"command\":[\"playlist-prev\",\"weak\"]}\n";
|
||||
osd_msg = "Media: Previous";
|
||||
} else if (strcmp(action, "next") == 0) {
|
||||
mpv_json = "{\"command\":[\"playlist-next\",\"weak\"]}\n";
|
||||
osd_msg = "Media: Next";
|
||||
} else if (strcmp(action, "stop") == 0) {
|
||||
mpv_json = "{\"command\":[\"quit\"]}\n";
|
||||
osd_msg = "Media: Stopped";
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (send_mpv_ipc(mpv_json)) {
|
||||
show_osd("osd", 1400, -1, osd_msg, NULL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Fallback to console audio players (openmpt123 / mpg123) owned by current UID */
|
||||
DIR *d = opendir("/proc");
|
||||
if (!d) return 0;
|
||||
uid_t me = getuid();
|
||||
bool acted = false;
|
||||
bool resumed = false;
|
||||
bool paused = false;
|
||||
struct dirent *de;
|
||||
|
||||
while ((de = readdir(d)) != NULL) {
|
||||
if (!isdigit((unsigned char)de->d_name[0])) continue;
|
||||
char path[280];
|
||||
struct stat st;
|
||||
snprintf(path, sizeof(path), "/proc/%s", de->d_name);
|
||||
if (stat(path, &st) != 0 || st.st_uid != me) continue;
|
||||
snprintf(path, sizeof(path), "/proc/%s/comm", de->d_name);
|
||||
int fd = open(path, O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0) continue;
|
||||
char comm[64];
|
||||
ssize_t n = read(fd, comm, sizeof(comm) - 1);
|
||||
close(fd);
|
||||
if (n <= 0) continue;
|
||||
comm[n] = '\0';
|
||||
while (n > 0 && (comm[n - 1] == '\n' || comm[n - 1] == '\r')) comm[--n] = '\0';
|
||||
if (strcmp(comm, "openmpt123") != 0 && strcmp(comm, "mpg123") != 0) continue;
|
||||
|
||||
pid_t pid = (pid_t)strtol(de->d_name, NULL, 10);
|
||||
if (pid <= 1) continue;
|
||||
|
||||
if (strcmp(action, "play-pause") == 0) {
|
||||
char state = read_proc_state(de->d_name);
|
||||
if (state == 'T') {
|
||||
kill(pid, SIGCONT);
|
||||
resumed = true;
|
||||
} else {
|
||||
kill(pid, SIGSTOP);
|
||||
paused = true;
|
||||
}
|
||||
acted = true;
|
||||
} else if (strcmp(action, "next") == 0) {
|
||||
kill(pid, SIGINT);
|
||||
acted = true;
|
||||
} else if (strcmp(action, "stop") == 0) {
|
||||
kill(pid, SIGTERM);
|
||||
acted = true;
|
||||
}
|
||||
}
|
||||
closedir(d);
|
||||
|
||||
if (acted) {
|
||||
if (strcmp(action, "play-pause") == 0) {
|
||||
show_osd("osd", 1400, -1, resumed ? "Media: Resumed" : (paused ? "Media: Paused" : osd_msg), NULL);
|
||||
} else {
|
||||
show_osd("osd", 1400, -1, osd_msg, NULL);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc < 2) {
|
||||
fprintf(stderr, "Usage: %s <bri-up|bri-down|vol-up|vol-down|vol-mute|mic-mute|screenshot|screenshot-window|play-pause|prev|next|stop>\n",
|
||||
argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char *act = argv[1];
|
||||
if (strcmp(act, "bri-up") == 0) return handle_brightness(true);
|
||||
if (strcmp(act, "bri-down") == 0) return handle_brightness(false);
|
||||
if (strcmp(act, "vol-up") == 0 || strcmp(act, "vol-down") == 0 ||
|
||||
strcmp(act, "vol-mute") == 0 || strcmp(act, "mic-mute") == 0) {
|
||||
return handle_audio(act);
|
||||
}
|
||||
if (strcmp(act, "screenshot") == 0) return handle_screenshot(false);
|
||||
if (strcmp(act, "screenshot-window") == 0) return handle_screenshot(true);
|
||||
if (strcmp(act, "play-pause") == 0 || strcmp(act, "prev") == 0 ||
|
||||
strcmp(act, "next") == 0 || strcmp(act, "stop") == 0) {
|
||||
return handle_media_transport(act);
|
||||
}
|
||||
|
||||
fprintf(stderr, "geminux-media-key: unknown action '%s'\n", act);
|
||||
return 1;
|
||||
}
|
||||
Reference in new issue
Block a user