Files
geminux/src/geminux-apps/geminux-control.c
T

221 lines
7.9 KiB
C
Raw Normal View History

#define _POSIX_C_SOURCE 200809L
#include "gemui.h"
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HIT_BRI 1
#define HIT_VOL 2
#define HIT_PROF 3
#define HIT_LED 4
#define HIT_LOCK 5
#define HIT_SUSPEND 6
#define HIT_REBOOT 7
#define HIT_POWER 8
typedef struct {
gemui_app_t *app;
char bl_dir[256];
int bl_cur;
int bl_max;
int vol_pct;
int profile; /* 0=eco, 1=balanced, 2=performance */
gemui_rect_t bri_rect;
gemui_rect_t vol_rect;
int active_drag;
} ctrl_state_t;
static void detect_backlight(ctrl_state_t *st) {
st->bl_dir[0] = '\0';
st->bl_cur = 128;
st->bl_max = 255;
DIR *d = opendir("/sys/class/backlight");
if (!d) return;
struct dirent *de;
while ((de = readdir(d)) != NULL) {
if (de->d_name[0] == '.') continue;
snprintf(st->bl_dir, sizeof(st->bl_dir), "/sys/class/backlight/%s", de->d_name);
char b[64];
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);
break;
}
closedir(d);
}
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);
}
char prof[64] = {0};
if (gemui_read_file_line("/run/geminux/power-profile", prof, sizeof(prof)) == 0) {
if (strcmp(prof, "eco") == 0) st->profile = 0;
else if (strcmp(prof, "performance") == 0) st->profile = 2;
else st->profile = 1;
}
}
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);
char arg[32];
snprintf(arg, sizeof(arg), "%d%%", pct);
const char *cmd[] = {"brightnessctl", "set", arg, NULL};
gemui_run_cmd_capture(cmd, NULL, 0);
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);
gemui_request_redraw(st->app);
}
static void on_draw(gemui_app_t *app, cairo_t *cr, int w, int h, void *ud) {
ctrl_state_t *st = ud;
const gemui_theme_t *t = gemui_theme(app);
gemui_draw_header(app, cr, "\xef\x80\x93", "Geminux Control Center", "Power, Audio, Display & LEDs");
int pad = 24;
int cw = (w - pad * 2);
st->bri_rect = (gemui_rect_t){pad, 88, cw, 92};
char btxt[32];
double bnorm = st->bl_max > 0 ? (double)st->bl_cur / st->bl_max : 0.5;
snprintf(btxt, sizeof(btxt), "%d%%", (int)(bnorm * 100.0 + 0.5));
gemui_draw_slider(app, cr, st->bri_rect, "Display Backlight", bnorm, btxt, HIT_BRI, 0, false);
st->vol_rect = (gemui_rect_t){pad, 196, cw, 92};
char vtxt[32];
snprintf(vtxt, sizeof(vtxt), "%d%%", st->vol_pct);
gemui_draw_slider(app, cr, st->vol_rect, "Master Audio Volume", st->vol_pct / 100.0, vtxt, HIT_VOL, 0, false);
gemui_draw_text(cr, t, (gemui_rect_t){pad, 308, cw, 34},
"MT6797X Power Profile (A72 Big Cluster + Schedutil)",
-1, true, GEMUI_ALIGN_LEFT, t->fg_bright);
int pw = (cw - 24) / 3;
static const char *profs[3] = {"Eco (A53 Only)", "Balanced", "Performance (10-Core)"};
for (int i = 0; i < 3; i++) {
gemui_draw_button(app, cr, (gemui_rect_t){pad + i * (pw + 12), 348, pw, 72},
profs[i], HIT_PROF, i, false, st->profile == i, false);
}
gemui_draw_text(cr, t, (gemui_rect_t){pad, 444, cw, 34},
"AW9120 LED Strip Indicator", -1, true, GEMUI_ALIGN_LEFT, t->fg_bright);
int lw = (cw - 36) / 4;
static const char *leds[4] = {"Off", "Cyan Pulse", "Green OK", "Amber Warn"};
for (int i = 0; i < 4; i++) {
gemui_draw_button(app, cr, (gemui_rect_t){pad + i * (lw + 12), 484, lw, 68},
leds[i], HIT_LED, i, false, false, false);
}
int ay = h - 104;
int aw = (cw - 36) / 4;
gemui_draw_button(app, cr, (gemui_rect_t){pad, ay, aw, 78},
"Lock Screen", HIT_LOCK, 0, false, false, false);
gemui_draw_button(app, cr, (gemui_rect_t){pad + aw + 12, ay, aw, 78},
"Suspend Now", HIT_SUSPEND, 0, true, false, false);
gemui_draw_button(app, cr, (gemui_rect_t){pad + 2 * (aw + 12), ay, aw, 78},
"Reboot", HIT_REBOOT, 0, false, false, false);
gemui_draw_button(app, cr, (gemui_rect_t){pad + 3 * (aw + 12), ay, aw, 78},
"Power Off", HIT_POWER, 0, false, false, false);
}
static void on_key(gemui_app_t *app, KeySym ks, unsigned int state, const char *utf8, void *ud) {
(void)state;
(void)utf8;
ctrl_state_t *st = ud;
if (ks == XK_Escape || ks == XK_q) gemui_quit(app, 0);
else if (ks == XK_Left) set_brightness_norm(st, ((double)st->bl_cur / st->bl_max) - 0.1);
else if (ks == XK_Right) set_brightness_norm(st, ((double)st->bl_cur / st->bl_max) + 0.1);
}
static void on_ptr(gemui_app_t *app, int type, int x, int y, unsigned int btn, void *ud) {
(void)btn;
ctrl_state_t *st = ud;
if (type == GEMUI_PTR_UP) {
st->active_drag = 0;
return;
}
if (type == GEMUI_PTR_MOVE) {
if (st->active_drag == HIT_BRI) {
set_brightness_norm(st, (double)(x - st->bri_rect.x - 16) / (st->bri_rect.w - 32));
} else if (st->active_drag == HIT_VOL) {
set_volume_norm(st, (double)(x - st->vol_rect.x - 16) / (st->vol_rect.w - 32));
}
return;
}
if (type != GEMUI_PTR_DOWN) return;
const gemui_hit_t *hit = gemui_hit_test(app, x, y);
if (!hit) return;
if (hit->id == HIT_BRI) {
st->active_drag = HIT_BRI;
set_brightness_norm(st, (double)(x - st->bri_rect.x - 16) / (st->bri_rect.w - 32));
} else if (hit->id == HIT_VOL) {
st->active_drag = HIT_VOL;
set_volume_norm(st, (double)(x - st->vol_rect.x - 16) / (st->vol_rect.w - 32));
} else if (hit->id == HIT_PROF) {
static const char *pnames[3] = {"eco", "balanced", "performance"};
st->profile = hit->val;
const char *cmd[] = {"doas", "/usr/bin/geminux-power-ctl", "profile", pnames[hit->val], NULL};
gemui_run_cmd_capture(cmd, NULL, 0);
gemui_show_toast(app, "Updated power profile", 1600);
gemui_request_redraw(app);
} else if (hit->id == HIT_LED) {
static const char *lcmds[4] = {"off", "rgb 0 40 60", "rgb 0 64 16", "rgb 64 40 0"};
gemui_unix_cmd("/run/geminux/leds.sock", lcmds[hit->val], NULL, 0);
gemui_show_toast(app, "Updated AW9120 LED state", 1400);
} else if (hit->id == HIT_LOCK) {
const char *cmd[] = {"geminux-lock", NULL};
gemui_spawn_detached(cmd);
gemui_quit(app, 0);
} else if (hit->id == HIT_SUSPEND) {
const char *cmd[] = {"doas", "/usr/bin/geminux-power-ctl", "suspend", NULL};
gemui_spawn_detached(cmd);
gemui_quit(app, 0);
} else if (hit->id == HIT_REBOOT) {
const char *cmd[] = {"doas", "/sbin/reboot", NULL};
gemui_run_cmd_capture(cmd, NULL, 0);
} else if (hit->id == HIT_POWER) {
const char *cmd[] = {"doas", "/sbin/poweroff", NULL};
gemui_run_cmd_capture(cmd, NULL, 0);
}
}
int main(void) {
ctrl_state_t st;
memset(&st, 0, sizeof(st));
st.vol_pct = 50;
st.profile = 1;
refresh_hw(&st);
st.app = gemui_create("geminux-control", "Geminux Control Center", 1480, 720, false);
if (!st.app) return 1;
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);
gemui_destroy(st.app);
return rc;
}