541 lines
16 KiB
C
541 lines
16 KiB
C
#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;
|
||
|
|
}
|