Files
anode/src/apps/anode-display.c
T

319 lines
11 KiB
C
Raw Normal View History

#define _POSIX_C_SOURCE 200809L
#include "gemui.h"
#include "anode_plat.h"
#include "anode_scale.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/extensions/Xrandr.h>
#define MAX_OUTPUTS 8
#define MAX_MODES 24
typedef struct {
char name[32];
int cur_w, cur_h;
double cur_hz;
int mode_w[MAX_MODES];
int mode_h[MAX_MODES];
double mode_hz[MAX_MODES];
int num_modes;
int rot_idx; /* 0=normal, 1=left, 2=inverted, 3=right */
bool active;
} out_info_t;
typedef struct {
gemui_app_t *app;
out_info_t outs[MAX_OUTPUTS];
int n_outs;
int sel_out;
int sel_mode;
double scale;
bool touch_mode;
int bri_pct;
} disp_state_t;
enum {
HIT_OUT = 1,
HIT_MODE,
HIT_ROT,
HIT_SCALE_PRESET,
HIT_SCALE_STEP,
HIT_TOUCH,
HIT_BRI,
HIT_APPLY_MODE,
HIT_QUIT
};
static const double PRESETS[] = {0.75, 1.00, 1.25, 1.50, 1.75, 2.00, 2.50, 3.00};
static const char *ROTS[] = {"normal", "left", "inverted", "right"};
static void scan_outputs(disp_state_t *st) {
Display *dpy = gemui_display(st->app);
Window root = DefaultRootWindow(dpy);
st->n_outs = 0;
XRRScreenResources *res = XRRGetScreenResourcesCurrent(dpy, root);
if (!res) res = XRRGetScreenResources(dpy, root);
if (!res) return;
for (int i = 0; i < res->noutput && st->n_outs < MAX_OUTPUTS; i++) {
XRROutputInfo *oi = XRRGetOutputInfo(dpy, res, res->outputs[i]);
if (!oi || oi->connection != RR_Connected) {
if (oi) XRRFreeOutputInfo(oi);
continue;
}
out_info_t *o = &st->outs[st->n_outs++];
memset(o, 0, sizeof(*o));
snprintf(o->name, sizeof(o->name), "%s", oi->name);
RRMode cur_mode = None;
if (oi->crtc) {
XRRCrtcInfo *ci = XRRGetCrtcInfo(dpy, res, oi->crtc);
if (ci) {
o->active = (ci->width > 0 && ci->height > 0);
o->cur_w = (int)ci->width;
o->cur_h = (int)ci->height;
cur_mode = ci->mode;
if (ci->rotation & RR_Rotate_90) o->rot_idx = 1;
else if (ci->rotation & RR_Rotate_180) o->rot_idx = 2;
else if (ci->rotation & RR_Rotate_270) o->rot_idx = 3;
else o->rot_idx = 0;
XRRFreeCrtcInfo(ci);
}
}
for (int m = 0; m < oi->nmode && o->num_modes < MAX_MODES; m++) {
for (int k = 0; k < res->nmode; k++) {
if (res->modes[k].id == oi->modes[m]) {
XRRModeInfo *mi = &res->modes[k];
double hz = (mi->hTotal && mi->vTotal)
? (double)mi->dotClock / ((double)mi->hTotal * (double)mi->vTotal)
: 60.0;
bool dup = false;
for (int d = 0; d < o->num_modes; d++) {
if (o->mode_w[d] == (int)mi->width && o->mode_h[d] == (int)mi->height &&
(int)(o->mode_hz[d] + 0.5) == (int)(hz + 0.5)) {
dup = true;
break;
}
}
if (!dup) {
int idx = o->num_modes++;
o->mode_w[idx] = (int)mi->width;
o->mode_h[idx] = (int)mi->height;
o->mode_hz[idx] = hz;
if (mi->id == cur_mode) {
o->cur_hz = hz;
st->sel_mode = idx;
}
}
break;
}
}
}
XRRFreeOutputInfo(oi);
}
XRRFreeScreenResources(res);
double dpi = 96.0, sc = 1.0;
int tm = 0;
anode_read_xres_scale(dpy, &dpi, &sc, &tm);
st->scale = sc;
st->touch_mode = (tm != 0);
int bcur = 50, bmax = 100;
if (anode_backlight_get(&bcur, &bmax, NULL, 0) && bmax > 0) {
st->bri_pct = (bcur * 100 + bmax / 2) / bmax;
} else {
st->bri_pct = 100;
}
}
static void apply_scale_now(disp_state_t *st, const char *scale_arg, int touch_override) {
char sbuf[16], tbuf[16];
snprintf(sbuf, sizeof(sbuf), "%s", scale_arg);
int t = (touch_override >= 0) ? touch_override : (st->touch_mode ? 1 : 0);
snprintf(tbuf, sizeof(tbuf), "--touch=%d", t);
const char *av[] = {"anode-apply-scale", sbuf, tbuf, NULL};
if (anode_run_cmd_timeout(av, NULL, 0, 5000) == 0) {
gemui_theme_reload(st->app);
const gemui_theme_t *th = gemui_theme(st->app);
st->scale = th ? th->scale : st->scale;
st->touch_mode = th ? th->touch_mode : (t != 0);
gemui_show_toast(st->app, "Display scale updated", 1800);
} else {
gemui_show_toast(st->app, "Scale update failed", 2000);
}
gemui_request_redraw(st->app);
}
static void apply_selected_mode(disp_state_t *st) {
if (st->sel_out < 0 || st->sel_out >= st->n_outs) return;
out_info_t *o = &st->outs[st->sel_out];
if (st->sel_mode < 0 || st->sel_mode >= o->num_modes) return;
char mode[48], rate[24];
snprintf(mode, sizeof(mode), "%dx%d", o->mode_w[st->sel_mode], o->mode_h[st->sel_mode]);
snprintf(rate, sizeof(rate), "%.2f", o->mode_hz[st->sel_mode]);
const char *av[] = {
"xrandr", "--output", o->name, "--mode", mode, "--rate", rate,
"--rotate", ROTS[o->rot_idx], NULL
};
if (anode_run_cmd_timeout(av, NULL, 0, 4000) == 0) {
scan_outputs(st);
gemui_show_toast(st->app, "Output mode applied", 1800);
} else {
gemui_show_toast(st->app, "xrandr mode apply failed", 2000);
}
gemui_request_redraw(st->app);
}
static void on_draw(gemui_app_t *app, cairo_t *cr, int w, int h, void *ud) {
disp_state_t *st = (disp_state_t *)ud;
const gemui_theme_t *t = gemui_theme(app);
char badge[64];
snprintf(badge, sizeof(badge), "Scale %.2fx (%d DPI)", st->scale, t->dpi);
gemui_draw_header(app, cr, NULL, "ANODE Display & Fractional Scaling", badge);
int pad = gemui_scale_px(app, 12);
int y = gemui_scale_px(app, 42) + pad;
int cw = w - pad * 2;
int btn_h = gemui_touch_row_px(app, 32);
/* Outputs strip */
if (st->n_outs > 0) {
int ow = (cw - pad * (st->n_outs - 1)) / st->n_outs;
for (int i = 0; i < st->n_outs; i++) {
char lbl[64];
snprintf(lbl, sizeof(lbl), "%s (%dx%d)", st->outs[i].name, st->outs[i].cur_w, st->outs[i].cur_h);
gemui_draw_button(app, cr, (gemui_rect_t){pad + i * (ow + pad), y, ow, btn_h},
lbl, HIT_OUT, i, false, i == st->sel_out, false);
}
y += btn_h + pad;
}
/* Scale presets grid (2 rows of 4 + Auto & -/+ steps) */
int pw = (cw - pad * 3) / 4;
for (int i = 0; i < 8; i++) {
int r = i / 4, c = i % 4;
char lbl[24];
snprintf(lbl, sizeof(lbl), "%.2fx", PRESETS[i]);
bool act = (st->scale >= PRESETS[i] - 0.02 && st->scale <= PRESETS[i] + 0.02);
gemui_draw_button(app, cr, (gemui_rect_t){pad + c * (pw + pad), y + r * (btn_h + 6), pw, btn_h},
lbl, HIT_SCALE_PRESET, i, false, act, false);
}
y += (btn_h + 6) * 2 + pad / 2;
int sw = (cw - pad * 2) / 3;
gemui_draw_button(app, cr, (gemui_rect_t){pad, y, sw, btn_h}, "-0.05 Scale", HIT_SCALE_STEP, -1, false, false, false);
gemui_draw_button(app, cr, (gemui_rect_t){pad + sw + pad, y, sw, btn_h}, "Auto EDID", HIT_SCALE_STEP, 0, true, false, false);
gemui_draw_button(app, cr, (gemui_rect_t){pad + (sw + pad) * 2, y, sw, btn_h}, "+0.05 Scale", HIT_SCALE_STEP, 1, false, false, false);
y += btn_h + pad;
/* Touch & Virtual Keyboard toggle */
gemui_draw_toggle(app, cr, (gemui_rect_t){pad, y, cw, btn_h + 4},
"Touch Device Mode & Larger Hit Targets", st->touch_mode, HIT_TOUCH, 0, false);
y += btn_h + 4 + pad;
/* Brightness */
int sli_h = gemui_touch_row_px(app, 50);
char btxt[24];
snprintf(btxt, sizeof(btxt), "%d%%", st->bri_pct);
gemui_draw_slider(app, cr, (gemui_rect_t){pad, y, cw, sli_h},
"Screen Brightness", (double)st->bri_pct / 100.0, btxt, HIT_BRI, 0, false);
y += sli_h + pad;
/* Rotation + top modes */
int rw = (cw - pad * 3) / 4;
int cur_rot = (st->n_outs > 0) ? st->outs[st->sel_out].rot_idx : 0;
for (int i = 0; i < 4; i++) {
gemui_draw_button(app, cr, (gemui_rect_t){pad + i * (rw + pad), y, rw, btn_h},
ROTS[i], HIT_ROT, i, false, i == cur_rot, false);
}
y += btn_h + pad;
if (st->n_outs > 0 && y + btn_h + pad < h - btn_h - pad) {
out_info_t *o = &st->outs[st->sel_out];
int n_show = o->num_modes > 4 ? 4 : o->num_modes;
int mw = n_show > 0 ? (cw - pad * (n_show - 1)) / n_show : cw;
for (int i = 0; i < n_show; i++) {
char mlbl[32];
snprintf(mlbl, sizeof(mlbl), "%dx%d@%.0f", o->mode_w[i], o->mode_h[i], o->mode_hz[i]);
gemui_draw_button(app, cr, (gemui_rect_t){pad + i * (mw + pad), y, mw, btn_h},
mlbl, HIT_MODE, i, false, i == st->sel_mode, false);
}
}
/* Footer */
int bw = gemui_scale_px(app, 140);
int fy = h - btn_h - pad;
gemui_draw_button(app, cr, (gemui_rect_t){w - pad * 2 - bw * 2, fy, bw, btn_h},
"Apply Output", HIT_APPLY_MODE, 0, true, false, false);
gemui_draw_button(app, cr, (gemui_rect_t){w - pad - bw, fy, bw, btn_h},
"Close", HIT_QUIT, 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;
disp_state_t *st = (disp_state_t *)ud;
if (ks == XK_Escape || ks == XK_q) gemui_quit(app, 0);
else if (ks == XK_r) { scan_outputs(st); gemui_request_redraw(app); }
else if (ks == XK_plus || ks == XK_equal) {
char b[16]; snprintf(b, sizeof(b), "%.2f", anode_clamp_scale(st->scale + 0.05));
apply_scale_now(st, b, -1);
} else if (ks == XK_minus) {
char b[16]; snprintf(b, sizeof(b), "%.2f", anode_clamp_scale(st->scale - 0.05));
apply_scale_now(st, b, -1);
}
}
static void on_ptr(gemui_app_t *app, int type, int x, int y, unsigned int btn, void *ud) {
disp_state_t *st = (disp_state_t *)ud;
if (type != GEMUI_PTR_DOWN || btn != Button1) return;
const gemui_hit_t *hit = gemui_hit_test(app, x, y);
if (!hit) return;
if (hit->id == HIT_OUT) { st->sel_out = hit->val; st->sel_mode = 0; gemui_request_redraw(app); }
else if (hit->id == HIT_MODE) { st->sel_mode = hit->val; gemui_request_redraw(app); }
else if (hit->id == HIT_ROT && st->n_outs > 0) {
st->outs[st->sel_out].rot_idx = hit->val;
gemui_request_redraw(app);
} else if (hit->id == HIT_SCALE_PRESET) {
char b[16]; snprintf(b, sizeof(b), "%.2f", PRESETS[hit->val]);
apply_scale_now(st, b, -1);
} else if (hit->id == HIT_SCALE_STEP) {
if (hit->val == 0) apply_scale_now(st, "auto", -1);
else {
char b[16]; snprintf(b, sizeof(b), "%.2f", anode_clamp_scale(st->scale + (hit->val > 0 ? 0.05 : -0.05)));
apply_scale_now(st, b, -1);
}
} else if (hit->id == HIT_TOUCH) {
char b[16]; snprintf(b, sizeof(b), "%.2f", st->scale);
apply_scale_now(st, b, st->touch_mode ? 0 : 1);
} else if (hit->id == HIT_BRI) {
int pct = ((x - hit->r.x) * 100) / (hit->r.w > 0 ? hit->r.w : 1);
if (pct < 5) pct = 5;
if (pct > 100) pct = 100;
st->bri_pct = pct;
anode_backlight_set((double)pct / 100.0);
gemui_request_redraw(app);
} else if (hit->id == HIT_APPLY_MODE) {
apply_selected_mode(st);
} else if (hit->id == HIT_QUIT) {
gemui_quit(app, 0);
}
}
int main(void) {
disp_state_t st;
memset(&st, 0, sizeof(st));
st.app = gemui_create("anode-display", "ANODE Display & Scaling", 840, 560, false);
if (!st.app) return 1;
scan_outputs(&st);
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;
}