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
+30
-16
@@ -73,22 +73,19 @@ static const char *audiocmd[] = { "geminux-audio", NULL };
|
||||
static const char *cameracmd[] = { "geminux-camera", NULL };
|
||||
static const char *themescmd[] = { "geminux-themes", NULL };
|
||||
static const char *lockcmd[] = { "geminux-lock", NULL };
|
||||
static const char *shotcmd[] = {
|
||||
"/bin/sh", "-c",
|
||||
"mkdir -p ~/Pictures && scrot ~/Pictures/screenshot-$(date +%Y%m%d-%H%M%S).png",
|
||||
NULL
|
||||
};
|
||||
static const char *briupcmd[] = { "brightnessctl", "set", "+10%", NULL };
|
||||
static const char *bridowncmd[] = { "brightnessctl", "set", "10%-", NULL };
|
||||
static const char *volupcmd[] = {
|
||||
"/bin/sh", "-c", "wpctl set-volume -l 1.0 @DEFAULT_AUDIO_SINK@ 5%+ && pkill -USR1 geminux-statusd || true", NULL
|
||||
};
|
||||
static const char *voldowncmd[] = {
|
||||
"/bin/sh", "-c", "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%- && pkill -USR1 geminux-statusd || true", NULL
|
||||
};
|
||||
static const char *volmutecmd[] = {
|
||||
"/bin/sh", "-c", "wpctl set-mute @DEFAULT_AUDIO_SINK@ toggle && pkill -USR1 geminux-statusd || true", NULL
|
||||
};
|
||||
static const char *shotcmd[] = { "geminux-media-key", "screenshot", NULL };
|
||||
static const char *shotwincmd[] = { "geminux-media-key", "screenshot-window", NULL };
|
||||
static const char *briupcmd[] = { "geminux-media-key", "bri-up", NULL };
|
||||
static const char *bridowncmd[] = { "geminux-media-key", "bri-down", NULL };
|
||||
static const char *volupcmd[] = { "geminux-media-key", "vol-up", NULL };
|
||||
static const char *voldowncmd[] = { "geminux-media-key", "vol-down", NULL };
|
||||
static const char *volmutecmd[] = { "geminux-media-key", "vol-mute", NULL };
|
||||
static const char *micmutecmd[] = { "geminux-media-key", "mic-mute", NULL };
|
||||
static const char *playpausecmd[] = { "geminux-media-key", "play-pause", NULL };
|
||||
static const char *medprevcmd[] = { "geminux-media-key", "prev", NULL };
|
||||
static const char *mednextcmd[] = { "geminux-media-key", "next", NULL };
|
||||
static const char *medstopcmd[] = { "geminux-media-key", "stop", NULL };
|
||||
static const char *suspendcmd[] = { "doas", "/usr/bin/geminux-power-ctl", "suspend", NULL };
|
||||
|
||||
static const Key keys[] = {
|
||||
/* modifier key function argument */
|
||||
@@ -107,11 +104,28 @@ static const Key keys[] = {
|
||||
{ MODKEY|ShiftMask, XK_l, spawn, {.v = lockcmd} },
|
||||
{ 0, XF86XK_ScreenSaver, spawn, {.v = lockcmd} },
|
||||
{ 0, XK_Print, spawn, {.v = shotcmd} },
|
||||
{ ControlMask, XK_Print, spawn, {.v = shotwincmd} },
|
||||
{ ShiftMask, XK_Print, spawn, {.v = shotwincmd} },
|
||||
{ 0, XF86XK_MonBrightnessUp, spawn, {.v = briupcmd} },
|
||||
{ 0, XF86XK_MonBrightnessDown, spawn, {.v = bridowncmd} },
|
||||
{ 0, XF86XK_AudioRaiseVolume, spawn, {.v = volupcmd} },
|
||||
{ 0, XF86XK_AudioLowerVolume, spawn, {.v = voldowncmd} },
|
||||
{ 0, XF86XK_AudioMute, spawn, {.v = volmutecmd} },
|
||||
{ 0, XF86XK_AudioRecord, spawn, {.v = micmutecmd} },
|
||||
{ 0, XF86XK_AudioMicMute, spawn, {.v = micmutecmd} },
|
||||
{ 0, XF86XK_AudioPlay, spawn, {.v = playpausecmd} },
|
||||
{ 0, XF86XK_AudioPause, spawn, {.v = playpausecmd} },
|
||||
{ 0, XF86XK_AudioPrev, spawn, {.v = medprevcmd} },
|
||||
{ 0, XF86XK_AudioNext, spawn, {.v = mednextcmd} },
|
||||
{ 0, XF86XK_AudioStop, spawn, {.v = medstopcmd} },
|
||||
{ 0, XF86XK_Tools, spawn, {.v = controlcmd} },
|
||||
{ 0, XF86XK_Option, spawn, {.v = audiocmd} },
|
||||
{ 0, XF86XK_TaskPane, spawn, {.v = launchercmd} },
|
||||
{ 0, XF86XK_TopMenu, spawn, {.v = launchercmd} },
|
||||
{ 0, XF86XK_UWB, spawn, {.v = netcmd} },
|
||||
{ 0, XF86XK_WLAN, spawn, {.v = netcmd} },
|
||||
{ 0, XF86XK_Sleep, spawn, {.v = suspendcmd} },
|
||||
{ 0, XF86XK_PowerDown, spawn, {.v = controlcmd} },
|
||||
{ MODKEY, XK_b, togglebar, {0} },
|
||||
{ MODKEY, XK_j, focusstack, {.i = +1} },
|
||||
{ MODKEY, XK_k, focusstack, {.i = -1} },
|
||||
|
||||
+69
-23
@@ -970,18 +970,39 @@ static void grabkeys(void) {
|
||||
}
|
||||
}
|
||||
|
||||
int min_kc = 8, max_kc = 255;
|
||||
XDisplayKeycodes(dpy, &min_kc, &max_kc);
|
||||
|
||||
for (i = 0; i < LENGTH(keys); i++) {
|
||||
KeyCode kc = XKeysymToKeycode(dpy, keys[i].keysym);
|
||||
if (!kc) continue;
|
||||
for (j = 0; j < LENGTH(modifiers); j++) {
|
||||
XGrabKey(dpy, kc, keys[i].mod | modifiers[j], root, True, GrabModeAsync, GrabModeAsync);
|
||||
if (keys[i].mod & Mod4Mask) {
|
||||
/* Fn+Alt frequently preserves Mod1Mask and/or Mod5Mask alongside Mod4Mask under XKB */
|
||||
XGrabKey(dpy, kc, keys[i].mod | Mod5Mask | modifiers[j], root, True, GrabModeAsync, GrabModeAsync);
|
||||
XGrabKey(dpy, kc, keys[i].mod | Mod1Mask | modifiers[j], root, True, GrabModeAsync, GrabModeAsync);
|
||||
XGrabKey(dpy, kc, keys[i].mod | Mod1Mask | Mod5Mask | modifiers[j], root, True, GrabModeAsync, GrabModeAsync);
|
||||
if (active_latch) {
|
||||
XGrabKey(dpy, kc, (keys[i].mod & ~Mod4Mask) | modifiers[j], root, True, GrabModeAsync, GrabModeAsync);
|
||||
int kc_i;
|
||||
for (kc_i = min_kc; kc_i <= max_kc; kc_i++) {
|
||||
KeyCode kc = (KeyCode)kc_i;
|
||||
KeySym s0 = XkbKeycodeToKeysym(dpy, kc, 0, 0);
|
||||
KeySym s2 = XkbKeycodeToKeysym(dpy, kc, 0, 2);
|
||||
KeySym s3 = XkbKeycodeToKeysym(dpy, kc, 0, 3);
|
||||
|
||||
if (s0 == keys[i].keysym) {
|
||||
for (j = 0; j < LENGTH(modifiers); j++) {
|
||||
XGrabKey(dpy, kc, keys[i].mod | modifiers[j], root, True, GrabModeAsync, GrabModeAsync);
|
||||
if (keys[i].mod & Mod4Mask) {
|
||||
/* Fn+Alt frequently preserves Mod1Mask and/or Mod5Mask alongside Mod4Mask under XKB */
|
||||
XGrabKey(dpy, kc, keys[i].mod | Mod5Mask | modifiers[j], root, True, GrabModeAsync, GrabModeAsync);
|
||||
XGrabKey(dpy, kc, keys[i].mod | Mod1Mask | modifiers[j], root, True, GrabModeAsync, GrabModeAsync);
|
||||
XGrabKey(dpy, kc, keys[i].mod | Mod1Mask | Mod5Mask | modifiers[j], root, True, GrabModeAsync, GrabModeAsync);
|
||||
if (active_latch) {
|
||||
XGrabKey(dpy, kc, (keys[i].mod & ~Mod4Mask) | modifiers[j], root, True, GrabModeAsync, GrabModeAsync);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (!(keys[i].mod & Mod4Mask) && s2 == keys[i].keysym && s0 != keys[i].keysym) {
|
||||
/* Grab Fn (ISO_Level3_Shift / Mod5Mask) Layer-3 multimedia/system keys */
|
||||
for (j = 0; j < LENGTH(modifiers); j++) {
|
||||
XGrabKey(dpy, kc, keys[i].mod | Mod5Mask | modifiers[j], root, True, GrabModeAsync, GrabModeAsync);
|
||||
}
|
||||
} else if (!(keys[i].mod & Mod4Mask) && s3 == keys[i].keysym && s2 != keys[i].keysym && s0 != keys[i].keysym) {
|
||||
/* Grab Shift+Fn (ShiftMask | Mod5Mask) Layer-4 keysyms */
|
||||
for (j = 0; j < LENGTH(modifiers); j++) {
|
||||
XGrabKey(dpy, kc, keys[i].mod | ShiftMask | Mod5Mask | modifiers[j], root, True, GrabModeAsync, GrabModeAsync);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1020,6 +1041,8 @@ static void keypress(XEvent *e) {
|
||||
XKeyEvent *ev = &e->xkey;
|
||||
KeySym sym_ev = XLookupKeysym(ev, 0);
|
||||
KeySym sym0 = XkbKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0, 0);
|
||||
KeySym sym_lvl2 = XkbKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0, 2);
|
||||
KeySym sym_lvl3 = XkbKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0, 3);
|
||||
unsigned long long t = now_ms();
|
||||
int active_latch = super_latched && (t <= super_latch_until);
|
||||
|
||||
@@ -1038,19 +1061,32 @@ static void keypress(XEvent *e) {
|
||||
}
|
||||
|
||||
for (i = 0; i < LENGTH(keys); i++) {
|
||||
if (sym0 != keys[i].keysym && sym_ev != keys[i].keysym)
|
||||
continue;
|
||||
int matched = 0;
|
||||
if (CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)) {
|
||||
matched = 1;
|
||||
} else if ((keys[i].mod & Mod4Mask) &&
|
||||
CLEANMASK(keys[i].mod) == CLEANMASK(ev->state & ~(Mod1Mask | Mod5Mask))) {
|
||||
matched = 1;
|
||||
} else if (active_latch && (keys[i].mod & Mod4Mask) &&
|
||||
CLEANMASK(keys[i].mod & ~Mod4Mask) == CLEANMASK(ev->state & ~(Mod1Mask | Mod5Mask))) {
|
||||
matched = 1;
|
||||
super_latched = 0;
|
||||
grabkeys();
|
||||
if (sym0 == keys[i].keysym || sym_ev == keys[i].keysym) {
|
||||
if (CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)) {
|
||||
matched = 1;
|
||||
} else if ((keys[i].mod & Mod4Mask) &&
|
||||
CLEANMASK(keys[i].mod) == CLEANMASK(ev->state & ~(Mod1Mask | Mod5Mask))) {
|
||||
matched = 1;
|
||||
} else if (active_latch && (keys[i].mod & Mod4Mask) &&
|
||||
CLEANMASK(keys[i].mod & ~Mod4Mask) == CLEANMASK(ev->state & ~(Mod1Mask | Mod5Mask))) {
|
||||
matched = 1;
|
||||
super_latched = 0;
|
||||
grabkeys();
|
||||
}
|
||||
}
|
||||
if (!matched && !(keys[i].mod & Mod4Mask) &&
|
||||
(ev->state & Mod5Mask) && !(ev->state & (Mod4Mask | Mod1Mask)) &&
|
||||
sym_lvl2 == keys[i].keysym && sym0 != keys[i].keysym) {
|
||||
if (CLEANMASK(keys[i].mod) == CLEANMASK(ev->state & ~Mod5Mask))
|
||||
matched = 1;
|
||||
}
|
||||
if (!matched && !(keys[i].mod & Mod4Mask) &&
|
||||
(ev->state & (ShiftMask | Mod5Mask)) == (ShiftMask | Mod5Mask) &&
|
||||
!(ev->state & (Mod4Mask | Mod1Mask)) &&
|
||||
sym_lvl3 == keys[i].keysym && sym_lvl2 != keys[i].keysym && sym0 != keys[i].keysym) {
|
||||
if (CLEANMASK(keys[i].mod) == CLEANMASK(ev->state & ~(ShiftMask | Mod5Mask)))
|
||||
matched = 1;
|
||||
}
|
||||
if (matched && keys[i].func) {
|
||||
keys[i].func(&(keys[i].arg));
|
||||
@@ -1228,6 +1264,16 @@ static void manage(Window w, XWindowAttributes *wa) {
|
||||
grabbuttons(c, 0);
|
||||
if (!c->isfloating)
|
||||
c->isfloating = c->oldstate = (trans != None || c->isfixed);
|
||||
if (c->isfloating && !c->isfullscreen && wa->x == 0 && wa->y == 0) {
|
||||
c->x = c->mon->wx + (c->mon->ww - WIDTH(c)) / 2;
|
||||
c->y = c->mon->wy + (c->mon->wh - HEIGHT(c)) / 2;
|
||||
if (c->x + WIDTH(c) > c->mon->wx + c->mon->ww)
|
||||
c->x = c->mon->wx + c->mon->ww - WIDTH(c);
|
||||
if (c->y + HEIGHT(c) > c->mon->wy + c->mon->wh)
|
||||
c->y = c->mon->wy + c->mon->wh - HEIGHT(c);
|
||||
c->x = MAX(c->x, c->mon->wx);
|
||||
c->y = MAX(c->y, c->mon->wy);
|
||||
}
|
||||
if (c->isfloating)
|
||||
XRaiseWindow(dpy, c->win);
|
||||
attach(c);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -40,13 +40,15 @@ geminux_profile_params_t geminux_policy_params(geminux_profile_t prof, bool on_a
|
||||
geminux_profile_params_t out;
|
||||
out.governor = "schedutil";
|
||||
if (prof == POWER_PROFILE_ECO) {
|
||||
out.governor = "schedutil";
|
||||
out.a72_online = false;
|
||||
out.a53_ll_max_khz = 624000UL;
|
||||
out.a53_max_khz = 832000UL;
|
||||
out.a53_ll_max_khz = 715000UL;
|
||||
out.a53_max_khz = 962000UL;
|
||||
out.a72_max_khz = 0UL;
|
||||
out.dim_timeout_s = on_ac ? 180 : 60;
|
||||
out.suspend_timeout_s = on_ac ? 0 : 180;
|
||||
} else if (prof == POWER_PROFILE_PERFORMANCE) {
|
||||
out.governor = "performance";
|
||||
out.a72_online = true;
|
||||
out.a53_ll_max_khz = 1547000UL;
|
||||
out.a53_max_khz = 1846000UL;
|
||||
@@ -54,9 +56,10 @@ geminux_profile_params_t geminux_policy_params(geminux_profile_t prof, bool on_a
|
||||
out.dim_timeout_s = on_ac ? 600 : 180;
|
||||
out.suspend_timeout_s = 0;
|
||||
} else {
|
||||
out.governor = "schedutil";
|
||||
out.a72_online = true;
|
||||
out.a53_ll_max_khz = on_ac ? 897000UL : 806000UL;
|
||||
out.a53_max_khz = on_ac ? 1209000UL : 1092000UL;
|
||||
out.a53_ll_max_khz = on_ac ? 1547000UL : 897000UL;
|
||||
out.a53_max_khz = on_ac ? 1846000UL : 1209000UL;
|
||||
out.a72_max_khz = 2002000UL;
|
||||
out.dim_timeout_s = on_ac ? 300 : 120;
|
||||
out.suspend_timeout_s = on_ac ? 0 : 300;
|
||||
@@ -140,3 +143,62 @@ bool geminux_should_suspend_on_lid(const geminux_power_policy_t *p) {
|
||||
if (p->on_ac_power && p->inhibit_ac_lid) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool geminux_fg_r_valid(uint32_t r_uohm) {
|
||||
return r_uohm >= 50000U && r_uohm <= 1500000U;
|
||||
}
|
||||
|
||||
static bool geminux_fg_full_valid(uint32_t full_uah) {
|
||||
return full_uah >= 1000000U && full_uah <= 10000000U;
|
||||
}
|
||||
|
||||
bool geminux_fg_state_parse(const char *text, uint32_t *r_uohm, uint32_t *full_uah) {
|
||||
if (!text || !r_uohm || !full_uah) return false;
|
||||
uint32_t r = 0, full = 0;
|
||||
bool got_r = false, got_full = false;
|
||||
const char *p = text;
|
||||
while (*p) {
|
||||
const char *nl = strchr(p, '\n');
|
||||
size_t len = nl ? (size_t)(nl - p) : strlen(p);
|
||||
size_t start = 0;
|
||||
while (start < len && (p[start] == ' ' || p[start] == '\t')) {
|
||||
start++;
|
||||
}
|
||||
while (len > start && (p[len - 1] == '\r' || p[len - 1] == ' ' || p[len - 1] == '\t')) {
|
||||
len--;
|
||||
}
|
||||
size_t trim_len = len - start;
|
||||
if (trim_len > 0 && trim_len < 96) {
|
||||
char line[96];
|
||||
memcpy(line, p + start, trim_len);
|
||||
line[trim_len] = '\0';
|
||||
unsigned long long val = 0;
|
||||
char tail = '\0';
|
||||
if (sscanf(line, "learned_resistance_uohm=%llu%c", &val, &tail) == 1 &&
|
||||
val <= 0xffffffffULL) {
|
||||
r = (uint32_t)val;
|
||||
got_r = true;
|
||||
} else if (sscanf(line, "learned_full_uah=%llu%c", &val, &tail) == 1 &&
|
||||
val <= 0xffffffffULL) {
|
||||
full = (uint32_t)val;
|
||||
got_full = true;
|
||||
}
|
||||
}
|
||||
p = nl ? (nl + 1) : (p + strlen(p));
|
||||
}
|
||||
if (!got_r || !got_full || !geminux_fg_r_valid(r) || !geminux_fg_full_valid(full)) {
|
||||
return false;
|
||||
}
|
||||
*r_uohm = r;
|
||||
*full_uah = full;
|
||||
return true;
|
||||
}
|
||||
|
||||
int geminux_fg_state_format(uint32_t r_uohm, uint32_t full_uah, char *buf, size_t sz) {
|
||||
if (!buf || sz == 0 || !geminux_fg_r_valid(r_uohm) || !geminux_fg_full_valid(full_uah)) {
|
||||
return -1;
|
||||
}
|
||||
int n = snprintf(buf, sz, "learned_resistance_uohm=%u\nlearned_full_uah=%u\n",
|
||||
r_uohm, full_uah);
|
||||
return (n > 0 && (size_t)n < sz) ? 0 : -1;
|
||||
}
|
||||
@@ -60,6 +60,8 @@ geminux_idle_action_t geminux_policy_eval_idle(
|
||||
bool geminux_policy_add_inhibit(geminux_power_policy_t *p, const char *tag);
|
||||
bool geminux_policy_del_inhibit(geminux_power_policy_t *p, const char *tag);
|
||||
bool geminux_should_suspend_on_lid(const geminux_power_policy_t *p);
|
||||
bool geminux_fg_state_parse(const char *text, uint32_t *r_uohm, uint32_t *full_uah);
|
||||
int geminux_fg_state_format(uint32_t r_uohm, uint32_t full_uah, char *buf, size_t sz);
|
||||
|
||||
static inline bool geminux_uevent_has_subsystem(const char *buf, size_t len, const char *subsys) {
|
||||
if (!buf || len == 0 || !subsys || !subsys[0]) return false;
|
||||
|
||||
@@ -27,6 +27,9 @@
|
||||
|
||||
#define SOCK_PATH "/run/geminux/powerd.sock"
|
||||
#define PROFILE_FILE "/run/geminux/power-profile"
|
||||
#define FG_STATE_DIR "/var/lib/geminux"
|
||||
#define FG_STATE_FILE "/var/lib/geminux/fuel-gauge.state"
|
||||
#define FG_SYSFS_DIR "/sys/class/power_supply/battery/device"
|
||||
#define MAX_EVDEVS 16
|
||||
|
||||
typedef struct {
|
||||
@@ -42,11 +45,16 @@ typedef struct {
|
||||
geminux_idle_stage_t idle_stage;
|
||||
geminux_power_policy_t policy;
|
||||
bool charge_thresh_set;
|
||||
bool fg_restored;
|
||||
bool fg_file_synced;
|
||||
uint32_t saved_fg_r_uohm;
|
||||
uint32_t saved_fg_full_uah;
|
||||
bool policy_applied;
|
||||
geminux_profile_t applied_profile;
|
||||
bool applied_ac;
|
||||
bool applied_a72_ready;
|
||||
bool applied_cpufreq_present;
|
||||
bool applied_interactive_boost;
|
||||
pid_t consys_restore_pid;
|
||||
bool consys_suspended;
|
||||
bool bt_was_loaded;
|
||||
@@ -89,6 +97,91 @@ static void ensure_run_dir(void) {
|
||||
if (gr) chown("/run/geminux", 0, gr->gr_gid);
|
||||
}
|
||||
|
||||
static int read_sysfs_u32(const char *path, uint32_t *out) {
|
||||
char buf[48] = {0};
|
||||
if (read_sysfs_line(path, buf, sizeof(buf)) != 0 || !buf[0]) return -1;
|
||||
char *end = NULL;
|
||||
errno = 0;
|
||||
unsigned long long v = strtoull(buf, &end, 10);
|
||||
if (errno != 0 || end == buf || *end != '\0' || v > 0xffffffffULL) return -1;
|
||||
*out = (uint32_t)v;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void restore_fuel_gauge_state(powerd_ctx_t *ctx) {
|
||||
const char *r_path = FG_SYSFS_DIR "/learned_resistance_uohm";
|
||||
const char *f_path = FG_SYSFS_DIR "/learned_full_uah";
|
||||
if (access(r_path, F_OK) != 0 || access(f_path, F_OK) != 0) return;
|
||||
|
||||
FILE *f = fopen(FG_STATE_FILE, "r");
|
||||
if (f) {
|
||||
char buf[256] = {0};
|
||||
size_t n = fread(buf, 1, sizeof(buf) - 1, f);
|
||||
fclose(f);
|
||||
buf[n] = '\0';
|
||||
uint32_t r_val = 0, f_val = 0;
|
||||
if (geminux_fg_state_parse(buf, &r_val, &f_val)) {
|
||||
char r_str[32], f_str[32];
|
||||
snprintf(r_str, sizeof(r_str), "%u", r_val);
|
||||
snprintf(f_str, sizeof(f_str), "%u", f_val);
|
||||
write_sysfs_str(r_path, r_str);
|
||||
write_sysfs_str(f_path, f_str);
|
||||
ctx->fg_file_synced = true;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t cur_r = 0, cur_f = 0;
|
||||
if (read_sysfs_u32(r_path, &cur_r) == 0 && read_sysfs_u32(f_path, &cur_f) == 0) {
|
||||
ctx->saved_fg_r_uohm = cur_r;
|
||||
ctx->saved_fg_full_uah = cur_f;
|
||||
}
|
||||
ctx->fg_restored = true;
|
||||
}
|
||||
|
||||
static void save_fuel_gauge_state(powerd_ctx_t *ctx) {
|
||||
uint32_t r_val = 0, f_val = 0;
|
||||
if (read_sysfs_u32(FG_SYSFS_DIR "/learned_resistance_uohm", &r_val) != 0 ||
|
||||
read_sysfs_u32(FG_SYSFS_DIR "/learned_full_uah", &f_val) != 0) {
|
||||
return;
|
||||
}
|
||||
char content[128];
|
||||
if (geminux_fg_state_format(r_val, f_val, content, sizeof(content)) != 0) return;
|
||||
if (ctx->fg_file_synced &&
|
||||
r_val == ctx->saved_fg_r_uohm &&
|
||||
f_val == ctx->saved_fg_full_uah) {
|
||||
return;
|
||||
}
|
||||
|
||||
mkdir("/var/lib", 0755);
|
||||
mkdir(FG_STATE_DIR, 0755);
|
||||
char tmp_path[128];
|
||||
snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", FG_STATE_FILE);
|
||||
int fd = open(tmp_path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
|
||||
if (fd < 0) return;
|
||||
|
||||
size_t len = strlen(content), off = 0;
|
||||
bool ok = true;
|
||||
while (off < len) {
|
||||
ssize_t w = write(fd, content + off, len - off);
|
||||
if (w <= 0) {
|
||||
if (w < 0 && errno == EINTR) continue;
|
||||
ok = false;
|
||||
break;
|
||||
}
|
||||
off += (size_t)w;
|
||||
}
|
||||
if (ok && fsync(fd) != 0) ok = false;
|
||||
if (close(fd) != 0) ok = false;
|
||||
|
||||
if (ok && rename(tmp_path, FG_STATE_FILE) == 0) {
|
||||
ctx->saved_fg_r_uohm = r_val;
|
||||
ctx->saved_fg_full_uah = f_val;
|
||||
ctx->fg_file_synced = true;
|
||||
} else {
|
||||
unlink(tmp_path);
|
||||
}
|
||||
}
|
||||
|
||||
static void refresh_ac_and_hdmi(powerd_ctx_t *ctx) {
|
||||
if (!ctx->charge_thresh_set) {
|
||||
if (write_sysfs_str("/sys/class/power_supply/battery/charge_control_end_threshold", "100") == 0) {
|
||||
@@ -156,6 +249,9 @@ static void toggle_backlight(powerd_ctx_t *ctx, bool on) {
|
||||
save_active_brightness(ctx, p);
|
||||
write_sysfs_str(p, "0");
|
||||
} else {
|
||||
char pwr[320];
|
||||
snprintf(pwr, sizeof(pwr), "/sys/class/backlight/%s/bl_power", de->d_name);
|
||||
write_sysfs_str(pwr, "0");
|
||||
char restore[32];
|
||||
int val = ctx->saved_brightness > 0 ? ctx->saved_brightness : 128;
|
||||
snprintf(restore, sizeof(restore), "%d", val);
|
||||
@@ -217,11 +313,13 @@ static void evaluate_idle(powerd_ctx_t *ctx) {
|
||||
if (act == IDLE_ACT_DIM) {
|
||||
dim_backlight(ctx);
|
||||
ctx->idle_stage = IDLE_STAGE_DIMMED;
|
||||
apply_cpu_sysfs(ctx, false);
|
||||
arm_timer_ms(ctx->tmr_fd, next_wait_ms);
|
||||
} else if (act == IDLE_ACT_LOCK_BLANK) {
|
||||
lock_x11_session();
|
||||
toggle_backlight(ctx, false);
|
||||
ctx->idle_stage = IDLE_STAGE_LOCKED_BLANK;
|
||||
apply_cpu_sysfs(ctx, false);
|
||||
arm_timer_ms(ctx->tmr_fd, 0);
|
||||
} else if (act == IDLE_ACT_SUSPEND) {
|
||||
arm_timer_ms(ctx->tmr_fd, 0);
|
||||
@@ -236,6 +334,7 @@ static void note_user_activity(powerd_ctx_t *ctx) {
|
||||
if (ctx->idle_stage != IDLE_STAGE_ACTIVE) {
|
||||
ctx->idle_stage = IDLE_STAGE_ACTIVE;
|
||||
toggle_backlight(ctx, true);
|
||||
apply_cpu_sysfs(ctx, false);
|
||||
evaluate_idle(ctx);
|
||||
}
|
||||
}
|
||||
@@ -243,11 +342,15 @@ static void note_user_activity(powerd_ctx_t *ctx) {
|
||||
static void apply_cpu_sysfs(powerd_ctx_t *ctx, bool force) {
|
||||
bool a72_ready = (access("/sys/module/mt6797_a72", F_OK) == 0);
|
||||
bool cpufreq_present = (access("/sys/devices/system/cpu/cpufreq/policy0", F_OK) == 0);
|
||||
bool interactive_boost = (ctx->policy.profile == POWER_PROFILE_BALANCED &&
|
||||
ctx->idle_stage == IDLE_STAGE_ACTIVE &&
|
||||
!ctx->policy.lid_closed);
|
||||
if (!force && ctx->policy_applied &&
|
||||
ctx->applied_profile == ctx->policy.profile &&
|
||||
ctx->applied_ac == ctx->policy.on_ac_power &&
|
||||
ctx->applied_a72_ready == a72_ready &&
|
||||
ctx->applied_cpufreq_present == cpufreq_present) {
|
||||
ctx->applied_cpufreq_present == cpufreq_present &&
|
||||
ctx->applied_interactive_boost == interactive_boost) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -266,17 +369,35 @@ static void apply_cpu_sysfs(powerd_ctx_t *ctx, bool force) {
|
||||
struct dirent *de;
|
||||
while ((de = readdir(d)) != NULL) {
|
||||
if (strncmp(de->d_name, "policy", 6) != 0) continue;
|
||||
char p[256], freq_buf[32];
|
||||
char p[256], freq_buf[32], min_buf[32] = "221000", max_hw_buf[32] = {0};
|
||||
int first_cpu = atoi(de->d_name + 6);
|
||||
snprintf(p, sizeof(p), "/sys/devices/system/cpu/cpufreq/%s/cpuinfo_min_freq", de->d_name);
|
||||
(void)read_sysfs_line(p, min_buf, sizeof(min_buf));
|
||||
snprintf(p, sizeof(p), "/sys/devices/system/cpu/cpufreq/%s/cpuinfo_max_freq", de->d_name);
|
||||
unsigned long hw_max =
|
||||
(read_sysfs_line(p, max_hw_buf, sizeof(max_hw_buf)) == 0) ? strtoul(max_hw_buf, NULL, 10) : 0UL;
|
||||
|
||||
snprintf(p, sizeof(p), "/sys/devices/system/cpu/cpufreq/%s/scaling_governor", de->d_name);
|
||||
write_sysfs_str(p, par.governor);
|
||||
|
||||
int first_cpu = atoi(de->d_name + 6);
|
||||
unsigned long cap = (first_cpu >= 8) ? par.a72_max_khz :
|
||||
(first_cpu >= 4) ? par.a53_max_khz : par.a53_ll_max_khz;
|
||||
if (hw_max > 0 && (cap == 0 || cap > hw_max)) {
|
||||
cap = hw_max;
|
||||
}
|
||||
|
||||
/* Reset floor before adjusting ceiling so scaling_min_freq never blocks scaling_max_freq. */
|
||||
snprintf(p, sizeof(p), "/sys/devices/system/cpu/cpufreq/%s/scaling_min_freq", de->d_name);
|
||||
write_sysfs_str(p, min_buf);
|
||||
|
||||
if (cap > 0) {
|
||||
snprintf(p, sizeof(p), "/sys/devices/system/cpu/cpufreq/%s/scaling_max_freq", de->d_name);
|
||||
snprintf(freq_buf, sizeof(freq_buf), "%lu", cap);
|
||||
write_sysfs_str(p, freq_buf);
|
||||
if (interactive_boost && first_cpu < 8) {
|
||||
snprintf(p, sizeof(p), "/sys/devices/system/cpu/cpufreq/%s/scaling_min_freq", de->d_name);
|
||||
write_sysfs_str(p, freq_buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
closedir(d);
|
||||
@@ -292,6 +413,7 @@ static void apply_cpu_sysfs(powerd_ctx_t *ctx, bool force) {
|
||||
ctx->applied_ac = ctx->policy.on_ac_power;
|
||||
ctx->applied_a72_ready = a72_ready;
|
||||
ctx->applied_cpufreq_present = cpufreq_present;
|
||||
ctx->applied_interactive_boost = interactive_boost;
|
||||
}
|
||||
|
||||
static void apply_cpu_policy(powerd_ctx_t *ctx, bool force) {
|
||||
@@ -542,15 +664,93 @@ static void refresh_lid_state(powerd_ctx_t *ctx) {
|
||||
}
|
||||
}
|
||||
|
||||
/* gpio-keys SW_LID applies a 64 ms debounce timer on wakeup; wait up to
|
||||
* timeout_ms using poll() so a lid-open wake sees SW_LID == 0 before we
|
||||
* decide whether to light the backlight. */
|
||||
static void wait_post_resume_lid_events(powerd_ctx_t *ctx, int timeout_ms) {
|
||||
refresh_lid_state(ctx);
|
||||
if (!ctx->policy.lid_closed || ctx->num_ev <= 0) return;
|
||||
|
||||
uint64_t deadline = monotonic_ms() + (uint64_t)timeout_ms;
|
||||
struct pollfd pfds[MAX_EVDEVS];
|
||||
while (ctx->policy.lid_closed) {
|
||||
uint64_t now = monotonic_ms();
|
||||
if (now >= deadline) break;
|
||||
int slice = (int)(deadline - now);
|
||||
if (slice > 20) slice = 20;
|
||||
|
||||
for (int i = 0; i < ctx->num_ev; i++) {
|
||||
pfds[i].fd = ctx->ev_fds[i];
|
||||
pfds[i].events = POLLIN;
|
||||
pfds[i].revents = 0;
|
||||
}
|
||||
int pr = poll(pfds, (nfds_t)ctx->num_ev, slice);
|
||||
if (pr < 0) {
|
||||
if (errno == EINTR) continue;
|
||||
break;
|
||||
}
|
||||
for (int i = 0; i < ctx->num_ev; i++) {
|
||||
if (!(pfds[i].revents & POLLIN)) continue;
|
||||
struct input_event iev;
|
||||
while (read(ctx->ev_fds[i], &iev, sizeof(iev)) == (ssize_t)sizeof(iev)) {
|
||||
if (iev.type == EV_SW && iev.code == SW_LID) {
|
||||
ctx->policy.lid_closed = (iev.value != 0);
|
||||
} else if (iev.type == EV_KEY || iev.type == EV_REL || iev.type == EV_ABS) {
|
||||
ctx->last_input_ms = monotonic_ms();
|
||||
}
|
||||
}
|
||||
}
|
||||
refresh_lid_state(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
static void prepare_cpus_for_suspend(void) {
|
||||
DIR *d = opendir("/sys/devices/system/cpu/cpufreq");
|
||||
if (d) {
|
||||
struct dirent *de;
|
||||
while ((de = readdir(d)) != NULL) {
|
||||
if (strncmp(de->d_name, "policy", 6) != 0) continue;
|
||||
char p[256], min_buf[32] = "221000";
|
||||
snprintf(p, sizeof(p), "/sys/devices/system/cpu/cpufreq/%s/cpuinfo_min_freq", de->d_name);
|
||||
(void)read_sysfs_line(p, min_buf, sizeof(min_buf));
|
||||
snprintf(p, sizeof(p), "/sys/devices/system/cpu/cpufreq/%s/scaling_min_freq", de->d_name);
|
||||
write_sysfs_str(p, min_buf);
|
||||
snprintf(p, sizeof(p), "/sys/devices/system/cpu/cpufreq/%s/scaling_governor", de->d_name);
|
||||
write_sysfs_str(p, "powersave");
|
||||
snprintf(p, sizeof(p), "/sys/devices/system/cpu/cpufreq/%s/scaling_max_freq", de->d_name);
|
||||
write_sysfs_str(p, min_buf);
|
||||
}
|
||||
closedir(d);
|
||||
}
|
||||
for (int cpu = 9; cpu >= 1; cpu--) {
|
||||
char p[128];
|
||||
snprintf(p, sizeof(p), "/sys/devices/system/cpu/cpu%d/online", cpu);
|
||||
write_sysfs_str(p, "0");
|
||||
}
|
||||
}
|
||||
|
||||
static void restore_cpus_after_resume(powerd_ctx_t *ctx) {
|
||||
for (int cpu = 1; cpu <= 7; cpu++) {
|
||||
char p[128];
|
||||
snprintf(p, sizeof(p), "/sys/devices/system/cpu/cpu%d/online", cpu);
|
||||
write_sysfs_str(p, "1");
|
||||
}
|
||||
ctx->policy_applied = false;
|
||||
}
|
||||
|
||||
static void trigger_suspend(powerd_ctx_t *ctx) {
|
||||
arm_timer_ms(ctx->tmr_fd, 0);
|
||||
lock_x11_session();
|
||||
toggle_backlight(ctx, false);
|
||||
save_fuel_gauge_state(ctx);
|
||||
prepare_consys_for_suspend(ctx);
|
||||
prepare_cpus_for_suspend();
|
||||
write_sysfs_str("/sys/power/mem_sleep", "deep");
|
||||
bool suspended = (write_sysfs_str("/sys/power/state", "mem") == 0 ||
|
||||
write_sysfs_str("/sys/power/state", "freeze") == 0);
|
||||
restore_cpus_after_resume(ctx);
|
||||
restore_consys_after_resume(ctx);
|
||||
refresh_lid_state(ctx);
|
||||
wait_post_resume_lid_events(ctx, 90);
|
||||
refresh_ac_and_hdmi(ctx);
|
||||
if (suspended && !ctx->policy.lid_closed) {
|
||||
ctx->idle_stage = IDLE_STAGE_ACTIVE;
|
||||
@@ -707,6 +907,7 @@ int main(void) {
|
||||
|
||||
ctx.last_input_ms = monotonic_ms();
|
||||
ctx.idle_stage = IDLE_STAGE_ACTIVE;
|
||||
restore_fuel_gauge_state(&ctx);
|
||||
refresh_ac_and_hdmi(&ctx);
|
||||
scan_input_devices(&ctx);
|
||||
apply_cpu_policy(&ctx, true);
|
||||
@@ -747,17 +948,27 @@ int main(void) {
|
||||
char ubuf[2048];
|
||||
ssize_t ur;
|
||||
bool need_ev_rescan = false;
|
||||
bool need_ps = false;
|
||||
bool need_ps_or_drm = false;
|
||||
while ((ur = recv(ctx.uev_fd, ubuf, sizeof(ubuf), 0)) > 0) {
|
||||
if (geminux_uevent_has_subsystem(ubuf, (size_t)ur, "input")) {
|
||||
need_ev_rescan = true;
|
||||
}
|
||||
if (geminux_uevent_has_subsystem(ubuf, (size_t)ur, "power_supply") ||
|
||||
geminux_uevent_has_subsystem(ubuf, (size_t)ur, "drm")) {
|
||||
if (geminux_uevent_has_subsystem(ubuf, (size_t)ur, "power_supply")) {
|
||||
need_ps = true;
|
||||
need_ps_or_drm = true;
|
||||
} else if (geminux_uevent_has_subsystem(ubuf, (size_t)ur, "drm")) {
|
||||
need_ps_or_drm = true;
|
||||
}
|
||||
}
|
||||
if (need_ev_rescan) scan_input_devices(&ctx);
|
||||
if (need_ps) {
|
||||
if (!ctx.fg_restored) {
|
||||
restore_fuel_gauge_state(&ctx);
|
||||
} else {
|
||||
save_fuel_gauge_state(&ctx);
|
||||
}
|
||||
}
|
||||
if (need_ps_or_drm) {
|
||||
bool prev_ac = ctx.policy.on_ac_power;
|
||||
bool prev_hdmi = ctx.policy.hdmi_connected;
|
||||
@@ -810,6 +1021,7 @@ int main(void) {
|
||||
}
|
||||
}
|
||||
|
||||
save_fuel_gauge_state(&ctx);
|
||||
unlink(SOCK_PATH);
|
||||
close(ctx.epfd);
|
||||
return 0;
|
||||
|
||||
+91
-16
@@ -35,6 +35,10 @@ struct gemui_app {
|
||||
Window root;
|
||||
Window win;
|
||||
Pixmap backbuf;
|
||||
cairo_surface_t *img_surf;
|
||||
cairo_t *img_cr;
|
||||
cairo_surface_t *xlib_surf;
|
||||
PangoLayout *pango_layout;
|
||||
GC gc;
|
||||
int bw;
|
||||
int bh;
|
||||
@@ -71,6 +75,7 @@ struct gemui_app {
|
||||
};
|
||||
|
||||
static int g_sig_pipe[2] = {-1, -1};
|
||||
static PangoLayout *g_active_layout = NULL;
|
||||
|
||||
static void gemui_sigusr1_handler(int sig) {
|
||||
(void)sig;
|
||||
@@ -292,7 +297,26 @@ void gemui_theme_reload(gemui_app_t *app) {
|
||||
|
||||
static void gemui_ensure_backbuf(gemui_app_t *app) {
|
||||
if (app->w <= 0 || app->h <= 0) return;
|
||||
if (app->backbuf && app->bw == app->w && app->bh == app->h) return;
|
||||
if (app->backbuf && app->img_cr && app->xlib_surf &&
|
||||
app->bw == app->w && app->bh == app->h) {
|
||||
return;
|
||||
}
|
||||
if (app->pango_layout) {
|
||||
g_object_unref(app->pango_layout);
|
||||
app->pango_layout = NULL;
|
||||
}
|
||||
if (app->img_cr) {
|
||||
cairo_destroy(app->img_cr);
|
||||
app->img_cr = NULL;
|
||||
}
|
||||
if (app->img_surf) {
|
||||
cairo_surface_destroy(app->img_surf);
|
||||
app->img_surf = NULL;
|
||||
}
|
||||
if (app->xlib_surf) {
|
||||
cairo_surface_destroy(app->xlib_surf);
|
||||
app->xlib_surf = NULL;
|
||||
}
|
||||
if (app->backbuf) {
|
||||
XFreePixmap(app->dpy, app->backbuf);
|
||||
app->backbuf = 0;
|
||||
@@ -300,8 +324,17 @@ static void gemui_ensure_backbuf(gemui_app_t *app) {
|
||||
app->backbuf = XCreatePixmap(app->dpy, app->win, (unsigned int)app->w,
|
||||
(unsigned int)app->h,
|
||||
(unsigned int)DefaultDepth(app->dpy, app->screen));
|
||||
app->xlib_surf = cairo_xlib_surface_create(app->dpy, app->backbuf,
|
||||
DefaultVisual(app->dpy, app->screen),
|
||||
app->w, app->h);
|
||||
app->img_surf = cairo_image_surface_create(CAIRO_FORMAT_RGB24, app->w, app->h);
|
||||
app->img_cr = cairo_create(app->img_surf);
|
||||
app->pango_layout = pango_cairo_create_layout(app->img_cr);
|
||||
app->bw = app->w;
|
||||
app->bh = app->h;
|
||||
if (app->backbuf) {
|
||||
XSetWindowBackgroundPixmap(app->dpy, app->win, app->backbuf);
|
||||
}
|
||||
}
|
||||
|
||||
static int gemui_ignore_xerror(Display *dpy, XErrorEvent *ev) {
|
||||
@@ -360,11 +393,16 @@ gemui_app_t *gemui_create(const char *res_name, const char *title,
|
||||
app->w = w;
|
||||
app->h = h;
|
||||
|
||||
int wx = fullscreen_override ? 0 : (sw - w) / 2;
|
||||
int wy = fullscreen_override ? 0 : (sh - h) / 2;
|
||||
if (wx < 0) wx = 0;
|
||||
if (wy < 0) wy = 0;
|
||||
|
||||
gemui_theme_reload(app);
|
||||
|
||||
XSetWindowAttributes swa;
|
||||
memset(&swa, 0, sizeof(swa));
|
||||
swa.background_pixel = BlackPixel(dpy, app->screen);
|
||||
swa.background_pixmap = None;
|
||||
swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask |
|
||||
ButtonReleaseMask | PointerMotionMask |
|
||||
StructureNotifyMask | FocusChangeMask;
|
||||
@@ -372,9 +410,21 @@ gemui_app_t *gemui_create(const char *res_name, const char *title,
|
||||
swa.override_redirect = True;
|
||||
}
|
||||
|
||||
unsigned long mask = CWBackPixel | CWEventMask | (fullscreen_override ? CWOverrideRedirect : 0);
|
||||
app->win = XCreateWindow(dpy, app->root, 0, 0, (unsigned int)w, (unsigned int)h, 0,
|
||||
unsigned long mask = CWBackPixmap | CWEventMask | (fullscreen_override ? CWOverrideRedirect : 0);
|
||||
app->win = XCreateWindow(dpy, app->root, wx, wy, (unsigned int)w, (unsigned int)h, 0,
|
||||
CopyFromParent, InputOutput, CopyFromParent, mask, &swa);
|
||||
{
|
||||
XSizeHints *shints = XAllocSizeHints();
|
||||
if (shints) {
|
||||
shints->flags = PPosition | PSize;
|
||||
shints->x = wx;
|
||||
shints->y = wy;
|
||||
shints->width = w;
|
||||
shints->height = h;
|
||||
XSetWMNormalHints(dpy, app->win, shints);
|
||||
XFree(shints);
|
||||
}
|
||||
}
|
||||
{
|
||||
Cursor cur = XCreateFontCursor(dpy, XC_left_ptr);
|
||||
if (cur != None) {
|
||||
@@ -412,8 +462,9 @@ gemui_app_t *gemui_create(const char *res_name, const char *title,
|
||||
XNClientWindow, app->win, XNFocusWindow, app->win, NULL);
|
||||
}
|
||||
|
||||
XMapRaised(dpy, app->win);
|
||||
if (fullscreen_override) {
|
||||
XMapRaised(dpy, app->win);
|
||||
app->mapped = true;
|
||||
XMoveResizeWindow(dpy, app->win, 0, 0, (unsigned int)sw, (unsigned int)sh);
|
||||
gemui_save_prev_focus(app);
|
||||
XSetInputFocus(dpy, app->win, RevertToPointerRoot, CurrentTime);
|
||||
@@ -428,6 +479,10 @@ void gemui_destroy(gemui_app_t *app) {
|
||||
gemui_restore_prev_focus(app);
|
||||
if (app->xic) XDestroyIC(app->xic);
|
||||
if (app->xim) XCloseIM(app->xim);
|
||||
if (app->pango_layout) g_object_unref(app->pango_layout);
|
||||
if (app->img_cr) cairo_destroy(app->img_cr);
|
||||
if (app->img_surf) cairo_surface_destroy(app->img_surf);
|
||||
if (app->xlib_surf) cairo_surface_destroy(app->xlib_surf);
|
||||
if (app->backbuf) XFreePixmap(app->dpy, app->backbuf);
|
||||
if (app->gc) XFreeGC(app->dpy, app->gc);
|
||||
if (app->win) XDestroyWindow(app->dpy, app->win);
|
||||
@@ -518,13 +573,18 @@ void gemui_quit(gemui_app_t *app, int code) {
|
||||
|
||||
bool gemui_grab_input(gemui_app_t *app) {
|
||||
if (!app) return false;
|
||||
if (!app->mapped) {
|
||||
gemui_flush_draw(app);
|
||||
}
|
||||
bool ok_kbd = XGrabKeyboard(app->dpy, app->win, True, GrabModeAsync,
|
||||
GrabModeAsync, CurrentTime) == GrabSuccess;
|
||||
XGrabPointer(app->dpy, app->win, True,
|
||||
ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
|
||||
GrabModeAsync, GrabModeAsync, app->win, None, CurrentTime);
|
||||
gemui_save_prev_focus(app);
|
||||
XSetInputFocus(app->dpy, app->win, RevertToPointerRoot, CurrentTime);
|
||||
if (app->mapped) {
|
||||
XSetInputFocus(app->dpy, app->win, RevertToPointerRoot, CurrentTime);
|
||||
}
|
||||
XFlush(app->dpy);
|
||||
return ok_kbd;
|
||||
}
|
||||
@@ -563,23 +623,31 @@ const gemui_hit_t *gemui_hit_test(const gemui_app_t *app, int x, int y) {
|
||||
static void gemui_render_frame(gemui_app_t *app) {
|
||||
if (!app || !app->need_redraw || app->w <= 0 || app->h <= 0) return;
|
||||
gemui_ensure_backbuf(app);
|
||||
if (!app->backbuf) return;
|
||||
|
||||
cairo_surface_t *surf = cairo_xlib_surface_create(
|
||||
app->dpy, app->backbuf, DefaultVisual(app->dpy, app->screen), app->w, app->h);
|
||||
cairo_t *cr = cairo_create(surf);
|
||||
if (!app->backbuf || !app->img_cr || !app->xlib_surf) return;
|
||||
|
||||
cairo_t *cr = app->img_cr;
|
||||
g_active_layout = app->pango_layout;
|
||||
gemui_hits_clear(app);
|
||||
gemui_fill_rect(cr, (gemui_rect_t){0, 0, app->w, app->h}, app->theme.bg_main);
|
||||
if (app->handlers.on_draw) {
|
||||
app->handlers.on_draw(app, cr, app->w, app->h, app->userdata);
|
||||
}
|
||||
gemui_draw_toast(app, cr);
|
||||
g_active_layout = NULL;
|
||||
cairo_surface_flush(app->img_surf);
|
||||
|
||||
cairo_destroy(cr);
|
||||
cairo_surface_flush(surf);
|
||||
cairo_surface_destroy(surf);
|
||||
cairo_t *xcr = cairo_create(app->xlib_surf);
|
||||
cairo_set_operator(xcr, CAIRO_OPERATOR_SOURCE);
|
||||
cairo_set_source_surface(xcr, app->img_surf, 0, 0);
|
||||
cairo_paint(xcr);
|
||||
cairo_destroy(xcr);
|
||||
cairo_surface_flush(app->xlib_surf);
|
||||
|
||||
if (!app->mapped) {
|
||||
XSetWindowBackgroundPixmap(app->dpy, app->win, app->backbuf);
|
||||
XMapRaised(app->dpy, app->win);
|
||||
app->mapped = true;
|
||||
}
|
||||
XCopyArea(app->dpy, app->backbuf, app->win, app->gc, 0, 0,
|
||||
(unsigned int)app->w, (unsigned int)app->h, 0, 0);
|
||||
XFlush(app->dpy);
|
||||
@@ -658,6 +726,12 @@ static void gemui_handle_x_events(gemui_app_t *app) {
|
||||
}
|
||||
break;
|
||||
case MotionNotify:
|
||||
while (XPending(app->dpy) > 0) {
|
||||
XEvent pev;
|
||||
XPeekEvent(app->dpy, &pev);
|
||||
if (pev.type != MotionNotify || pev.xmotion.window != ev.xmotion.window) break;
|
||||
XNextEvent(app->dpy, &ev);
|
||||
}
|
||||
if (app->ptr_down && app->handlers.on_ptr) {
|
||||
app->handlers.on_ptr(app, GEMUI_PTR_MOVE, ev.xmotion.x, ev.xmotion.y,
|
||||
0, app->userdata);
|
||||
@@ -807,13 +881,14 @@ static int gemui_draw_pango(cairo_t *cr, const char *face, int px_size,
|
||||
gemui_rect_t r, const char *text, bool bold,
|
||||
int align, gemui_color_t c) {
|
||||
if (!text || !text[0] || r.w <= 4 || r.h <= 4) return 0;
|
||||
PangoLayout *layout = pango_cairo_create_layout(cr);
|
||||
PangoLayout *layout = g_active_layout ? g_active_layout : pango_cairo_create_layout(cr);
|
||||
char desc_str[160];
|
||||
snprintf(desc_str, sizeof(desc_str), "%s%s %dpx",
|
||||
face && face[0] ? face : "Fira Code",
|
||||
bold ? " Bold" : "",
|
||||
px_size > 8 ? px_size : 14);
|
||||
PangoFontDescription *desc = pango_font_description_from_string(desc_str);
|
||||
pango_layout_set_attributes(layout, NULL);
|
||||
pango_layout_set_font_description(layout, desc);
|
||||
pango_layout_set_width(layout, r.w * PANGO_SCALE);
|
||||
pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
|
||||
@@ -835,7 +910,7 @@ static int gemui_draw_pango(cairo_t *cr, const char *face, int px_size,
|
||||
pango_cairo_show_layout(cr, layout);
|
||||
|
||||
pango_font_description_free(desc);
|
||||
g_object_unref(layout);
|
||||
if (!g_active_layout) g_object_unref(layout);
|
||||
return tw;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user