Files
anode/src/apps/anode-display.c
T
Luxferre 4ec3f59e02 feat(theme,display,wm,power,make): add Anodized theme, editors, multi-monitor arrangement, power timeouts, bar compaction, and replace state preservation
- Replace deprecated AnodeDefault with dark-silvery Anodized default theme
- Add interactive theme editor, duplicator, and non-standard .xth importer to anode-themes (+ CLI --install)
- Fix multi-monitor dwm/lock/vkbd/EDID edge cases and integrate monitor arrangement + 15s revert into anode-display and anode-apply-display
- Gate anode-touchd behind WITH_TOUCHD=1 (keeping anode-vkbd unconditional) and add help/rebuild/install-touch/install-system targets to Makefile
- Add idle screen dim, lock, suspend, and suspend-on-AC timeout controls to anode-power and anode-display backed by power.conf + SIGUSR1
- Prevent lockscreen subtitle overflow and progressively compact status bar segments so focused window titles remain visible
- Preserve workspaces, active desktop, monitor state, floating/fullscreen states, and focus across anode-replace and restarts
2026-11-27 08:31:35 +02:00

968 lines
35 KiB
C

#define _POSIX_C_SOURCE 200809L
#include "gemui.h"
#include "anode_plat.h"
#include "anode_scale.h"
#include "anode_display_cfg.h"
#include "anode_power_policy.h"
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/extensions/Xrandr.h>
#define MAX_MODES 36
#define MODES_PER_PAGE 4
typedef struct {
int mode_w[MAX_MODES];
int mode_h[MAX_MODES];
double mode_hz[MAX_MODES];
int num_modes;
int sel_mode;
} out_modes_t;
enum { TAB_ARRANGE = 0, TAB_SCALING = 1 };
typedef struct {
gemui_app_t *app;
int tab;
out_modes_t modes[ANODE_DISP_MAX_OUTS];
anode_disp_out_t cfg[ANODE_DISP_MAX_OUTS];
anode_disp_out_t pre_apply[ANODE_DISP_MAX_OUTS];
int n_outs;
int n_pre;
int sel_out;
int mode_page;
int confirm_sec;
bool dragging;
bool drag_moved;
int drag_idx;
int drag_start_px;
int drag_start_py;
int drag_orig_x;
int drag_orig_y;
gemui_rect_t canvas_rect;
double map_scale;
int map_vx0;
int map_vy0;
int map_cx0;
int map_cy0;
bool bri_dragging;
gemui_rect_t bri_rect;
double scale;
bool touch_mode;
int bri_pct;
anode_power_cfg_t pwr_cfg;
} disp_state_t;
enum {
HIT_TAB = 1,
HIT_CANVAS_MON,
HIT_PRESET,
HIT_ENABLE_TOGGLE,
HIT_SET_PRIMARY,
HIT_ROT,
HIT_MODE_PAGE,
HIT_MODE,
HIT_REFRESH,
HIT_SAVE_PROFILE,
HIT_APPLY_LAYOUT,
HIT_CONFIRM_KEEP,
HIT_CONFIRM_REVERT,
HIT_SCALE_PRESET,
HIT_SCALE_STEP,
HIT_TOUCH,
HIT_BRI,
HIT_DIM_STEP,
HIT_LOCK_STEP,
HIT_SUSP_STEP,
HIT_SUSP_AC,
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 const char *PRESET_LABELS[] = {
"Extend Right", "Extend Left", "Stack Above", "Stack Below", "Mirror", "Single"
};
static void sync_sel_mode_for(disp_state_t *st, int oi) {
if (oi < 0 || oi >= st->n_outs) return;
out_modes_t *om = &st->modes[oi];
const anode_disp_out_t *co = &st->cfg[oi];
om->sel_mode = 0;
for (int i = 0; i < om->num_modes; i++) {
if (om->mode_w[i] == co->w && om->mode_h[i] == co->h &&
(int)(om->mode_hz[i] + 0.5) == (int)(co->hz + 0.5)) {
om->sel_mode = i;
break;
}
}
}
static void load_power_cfg(disp_state_t *st) {
char dir[512], path[576];
anode_config_dir(dir, sizeof(dir));
snprintf(path, sizeof(path), "%s/power.conf", dir);
anode_power_cfg_load(path, &st->pwr_cfg);
}
static void save_power_cfg(disp_state_t *st, const char *toast_msg) {
char dir[512], path[576];
anode_config_dir(dir, sizeof(dir));
snprintf(path, sizeof(path), "%s/power.conf", dir);
if (anode_power_cfg_save(path, &st->pwr_cfg)) {
const char *pd[] = {"anode-powerd"};
anode_signal_procs(pd, 1, SIGUSR1);
gemui_show_toast(st->app, toast_msg, 1800);
} else {
gemui_show_toast(st->app, "Failed to save power.conf", 2200);
}
}
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);
RROutput primary_out = XRRGetOutputPrimary(dpy, root);
if (res) {
for (int i = 0; i < res->noutput && st->n_outs < ANODE_DISP_MAX_OUTS; i++) {
XRROutputInfo *oi = XRRGetOutputInfo(dpy, res, res->outputs[i]);
if (!oi || oi->connection != RR_Connected) {
if (oi) XRRFreeOutputInfo(oi);
continue;
}
int idx = st->n_outs++;
out_modes_t *om = &st->modes[idx];
anode_disp_out_t *co = &st->cfg[idx];
memset(om, 0, sizeof(*om));
memset(co, 0, sizeof(*co));
snprintf(co->name, sizeof(co->name), "%s", oi->name);
co->primary = (res->outputs[i] == primary_out);
RRMode cur_mode = None;
if (oi->crtc) {
XRRCrtcInfo *ci = XRRGetCrtcInfo(dpy, res, oi->crtc);
if (ci) {
co->enabled = (ci->width > 0 && ci->height > 0);
co->x = ci->x;
co->y = ci->y;
co->w = (int)ci->width;
co->h = (int)ci->height;
cur_mode = ci->mode;
if (ci->rotation & RR_Rotate_90) co->rot = 1;
else if (ci->rotation & RR_Rotate_180) co->rot = 2;
else if (ci->rotation & RR_Rotate_270) co->rot = 3;
else co->rot = 0;
/* Reverse physical W/H back to unrotated mode dimensions */
if (co->rot == 1 || co->rot == 3) {
int tmp = co->w;
co->w = co->h;
co->h = tmp;
}
XRRFreeCrtcInfo(ci);
}
}
for (int m = 0; m < oi->nmode && om->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 < om->num_modes; d++) {
if (om->mode_w[d] == (int)mi->width && om->mode_h[d] == (int)mi->height &&
(int)(om->mode_hz[d] + 0.5) == (int)(hz + 0.5)) {
dup = true;
break;
}
}
if (!dup) {
int midx = om->num_modes++;
om->mode_w[midx] = (int)mi->width;
om->mode_h[midx] = (int)mi->height;
om->mode_hz[midx] = hz;
if (mi->id == cur_mode) {
co->hz = hz;
om->sel_mode = midx;
}
}
break;
}
}
}
if (om->num_modes > 0) {
if (co->w <= 0 || co->h <= 0) {
co->w = om->mode_w[0];
co->h = om->mode_h[0];
co->hz = om->mode_hz[0];
} else if (co->hz <= 0.0) {
co->hz = om->mode_hz[om->sel_mode];
}
}
if (!co->enabled) co->primary = false;
XRRFreeOutputInfo(oi);
}
XRRFreeScreenResources(res);
}
anode_disp_normalize(st->cfg, st->n_outs);
if (st->sel_out >= st->n_outs) st->sel_out = st->n_outs > 0 ? st->n_outs - 1 : 0;
for (int i = 0; i < st->n_outs; i++) sync_sel_mode_for(st, i);
if (st->n_outs > 0) {
st->mode_page = st->modes[st->sel_out].sel_mode / MODES_PER_PAGE;
}
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;
}
load_power_cfg(st);
}
static bool save_current_profile(disp_state_t *st, bool quiet_toast) {
if (st->n_outs <= 0) return false;
char dir[512], path[544];
anode_config_dir(dir, sizeof(dir));
snprintf(path, sizeof(path), "%s/displays.conf", dir);
bool ok = anode_disp_save_profile(path, st->cfg, st->n_outs);
if (!quiet_toast) {
gemui_show_toast(st->app,
ok ? "Saved monitor profile to displays.conf" : "Failed to save monitor profile",
2000);
}
return ok;
}
static void confirm_revert(disp_state_t *st);
static void on_timer(gemui_app_t *app, void *ud) {
(void)app;
disp_state_t *st = (disp_state_t *)ud;
if (st->confirm_sec <= 0) {
gemui_set_timer_ms(st->app, 0, NULL, NULL);
return;
}
st->confirm_sec--;
if (st->confirm_sec <= 0) {
confirm_revert(st);
} else {
gemui_request_redraw(st->app);
}
}
static void confirm_keep(disp_state_t *st) {
gemui_set_timer_ms(st->app, 0, NULL, NULL);
st->confirm_sec = 0;
save_current_profile(st, true);
scan_outputs(st);
gemui_show_toast(st->app, "Layout kept & saved to displays.conf", 2200);
gemui_request_redraw(st->app);
}
static void confirm_revert(disp_state_t *st) {
gemui_set_timer_ms(st->app, 0, NULL, NULL);
st->confirm_sec = 0;
if (st->n_pre > 0) {
const char *av[96];
char tokbuf[1024];
if (anode_disp_build_xrandr_argv(st->pre_apply, st->n_pre, av, 96, tokbuf, sizeof(tokbuf)) > 0) {
anode_run_cmd_timeout(av, NULL, 0, 5000);
}
}
scan_outputs(st);
gemui_show_toast(st->app, "Reverted previous monitor layout", 2200);
gemui_request_redraw(st->app);
}
static void apply_layout_now(disp_state_t *st) {
if (st->n_outs <= 0) return;
bool any_en = false;
for (int i = 0; i < st->n_outs; i++) {
if (st->cfg[i].enabled) { any_en = true; break; }
}
if (!any_en) {
gemui_show_toast(st->app, "At least one monitor must stay enabled", 2000);
gemui_request_redraw(st->app);
return;
}
anode_disp_normalize(st->cfg, st->n_outs);
if (st->confirm_sec == 0) {
/* Snapshot live XRandR outputs right before applying */
disp_state_t live_snap;
memset(&live_snap, 0, sizeof(live_snap));
live_snap.app = st->app;
scan_outputs(&live_snap);
st->n_pre = live_snap.n_outs;
if (st->n_pre > 0) {
memcpy(st->pre_apply, live_snap.cfg, (size_t)st->n_pre * sizeof(st->pre_apply[0]));
}
}
const char *av[96];
char tokbuf[1024];
if (anode_disp_build_xrandr_argv(st->cfg, st->n_outs, av, 96, tokbuf, sizeof(tokbuf)) <= 0) {
gemui_show_toast(st->app, "Invalid layout specification", 2000);
gemui_request_redraw(st->app);
return;
}
if (anode_run_cmd_timeout(av, NULL, 0, 5000) == 0) {
st->confirm_sec = 15;
gemui_set_timer_ms(st->app, 1000, on_timer, st);
gemui_show_toast(st->app, "Layout applied - click Keep within 15s", 2200);
} else {
gemui_show_toast(st->app, "xrandr layout apply failed", 2200);
}
gemui_request_redraw(st->app);
}
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);
unsetenv("ANODE_SCALE");
unsetenv("ANODE_TOUCH");
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);
char msg[64];
snprintf(msg, sizeof(msg), "Display scale: %.2fx (%d DPI)",
st->scale, th ? th->dpi : (int)(96.0 * st->scale + 0.5));
gemui_show_toast(st->app, msg, 1800);
} else {
gemui_show_toast(st->app, "Scale update failed", 2000);
}
gemui_request_redraw(st->app);
}
static void draw_arrangement_tab(disp_state_t *st, cairo_t *cr, int w, int h, int y) {
const gemui_theme_t *t = gemui_theme(st->app);
int pad = gemui_scale_px(st->app, 12);
int cw = w - pad * 2;
int btn_h = gemui_touch_row_px(st->app, 30);
/* Confirmation countdown bar when testing new layout */
if (st->confirm_sec > 0) {
int cbh = gemui_touch_row_px(st->app, 38);
gemui_rect_t cbr = {pad, y, cw, cbh};
gemui_fill_round(cr, cbr, (double)gemui_scale_px(st->app, 6), t->bg_toast);
gemui_stroke_round(cr, cbr, (double)gemui_scale_px(st->app, 6), 1.5, t->highlight);
char cmsg[96];
snprintf(cmsg, sizeof(cmsg), "Keep this monitor layout? Reverting in %ds", st->confirm_sec);
int bbw = gemui_scale_px(st->app, 118);
gemui_draw_text(cr, t, (gemui_rect_t){pad + gemui_scale_px(st->app, 10), y, cw - bbw * 2 - pad * 3, cbh},
cmsg, 0, true, GEMUI_ALIGN_LEFT, t->fg_bright);
gemui_draw_button(st->app, cr, (gemui_rect_t){w - pad * 2 - bbw * 2 - gemui_scale_px(st->app, 6), y + 4, bbw, cbh - 8},
"Keep & Save", HIT_CONFIRM_KEEP, 0, true, false, false);
gemui_draw_button(st->app, cr, (gemui_rect_t){w - pad - bbw - gemui_scale_px(st->app, 4), y + 4, bbw, cbh - 8},
"Revert Now", HIT_CONFIRM_REVERT, 0, false, false, false);
y += cbh + pad / 2;
}
/* Quick preset bar */
int prw = (cw - pad / 2 * 5) / 6;
for (int i = 0; i < 6; i++) {
gemui_draw_button(st->app, cr, (gemui_rect_t){pad + i * (prw + pad / 2), y, prw, btn_h},
PRESET_LABELS[i], HIT_PRESET, i, false, false, false);
}
y += btn_h + pad / 2;
/* 2D interactive monitor arrangement canvas */
int ftr_h = btn_h + pad * 2;
int insp_h = btn_h * 4 + pad * 2;
int can_h = h - ftr_h - insp_h - y - pad / 2;
int min_can = gemui_scale_px(st->app, 130);
if (can_h < min_can) can_h = min_can;
gemui_rect_t can_r = {pad, y, cw, can_h};
st->canvas_rect = can_r;
gemui_fill_round(cr, can_r, (double)gemui_scale_px(st->app, 6), t->bg_alt_even);
gemui_stroke_round(cr, can_r, (double)gemui_scale_px(st->app, 6), 1.0, t->border);
if (st->n_outs <= 0) {
gemui_draw_text(cr, t, can_r, "No connected XRandR outputs detected", 0, false, GEMUI_ALIGN_CENTER, t->fg_muted);
} else {
int vx[ANODE_DISP_MAX_OUTS], vy[ANODE_DISP_MAX_OUTS];
int vw[ANODE_DISP_MAX_OUTS], vh[ANODE_DISP_MAX_OUTS];
int act_max_x = 0;
bool any_en = false;
for (int i = 0; i < st->n_outs; i++) {
anode_disp_eff_size(st->cfg[i].w > 0 ? st->cfg[i].w : 1280,
st->cfg[i].h > 0 ? st->cfg[i].h : 720,
st->cfg[i].rot, &vw[i], &vh[i]);
if (st->cfg[i].enabled) {
vx[i] = st->cfg[i].x;
vy[i] = st->cfg[i].y;
if (!any_en || vx[i] + vw[i] > act_max_x) act_max_x = vx[i] + vw[i];
any_en = true;
}
}
int off_y = 0;
int off_x = any_en ? (act_max_x + 260) : 0;
for (int i = 0; i < st->n_outs; i++) {
if (!st->cfg[i].enabled) {
vx[i] = off_x;
vy[i] = off_y;
off_y += vh[i] + 180;
}
}
int min_vx = vx[0], min_vy = vy[0], max_vx = vx[0] + vw[0], max_vy = vy[0] + vh[0];
for (int i = 1; i < st->n_outs; i++) {
if (vx[i] < min_vx) min_vx = vx[i];
if (vy[i] < min_vy) min_vy = vy[i];
if (vx[i] + vw[i] > max_vx) max_vx = vx[i] + vw[i];
if (vy[i] + vh[i] > max_vy) max_vy = vy[i] + vh[i];
}
int span_w = max_vx - min_vx;
int span_h = max_vy - min_vy;
if (span_w < 640) span_w = 640;
if (span_h < 480) span_h = 480;
int inset = gemui_scale_px(st->app, 16);
int inner_w = can_r.w - inset * 2;
int inner_h = can_r.h - inset * 2;
double sx = (double)inner_w / (double)span_w;
double sy = (double)inner_h / (double)span_h;
double sc = sx < sy ? sx : sy;
if (sc <= 0.0) sc = 0.05;
int drawn_w = (int)(span_w * sc);
int drawn_h = (int)(span_h * sc);
int cx0 = can_r.x + inset + (inner_w - drawn_w) / 2;
int cy0 = can_r.y + inset + (inner_h - drawn_h) / 2;
st->map_scale = sc;
st->map_vx0 = min_vx;
st->map_vy0 = min_vy;
st->map_cx0 = cx0;
st->map_cy0 = cy0;
for (int i = 0; i < st->n_outs; i++) {
int rx = cx0 + (int)((vx[i] - min_vx) * sc);
int ry = cy0 + (int)((vy[i] - min_vy) * sc);
int rw = (int)(vw[i] * sc);
int rh = (int)(vh[i] * sc);
if (rw < gemui_scale_px(st->app, 54)) rw = gemui_scale_px(st->app, 54);
if (rh < gemui_scale_px(st->app, 36)) rh = gemui_scale_px(st->app, 36);
bool is_sel = (i == st->sel_out);
gemui_rect_t mr = {rx, ry, rw, rh};
gemui_color_t mbg = !st->cfg[i].enabled ? t->bg_main : (is_sel ? t->sel_bg : t->bg_button);
gemui_color_t mbd = is_sel ? t->accent : (st->cfg[i].enabled ? t->border : t->divider);
double mrad = (double)gemui_scale_px(st->app, 5);
gemui_fill_round(cr, mr, mrad, mbg);
gemui_stroke_round(cr, mr, mrad, is_sel ? 2.0 : 1.0, mbd);
char line1[48], line2[48];
snprintf(line1, sizeof(line1), "%s%s", st->cfg[i].primary ? "* " : "", st->cfg[i].name);
if (st->cfg[i].enabled) {
snprintf(line2, sizeof(line2), "%dx%d @%d,%d", vw[i], vh[i], st->cfg[i].x, st->cfg[i].y);
} else {
snprintf(line2, sizeof(line2), "[OFF]");
}
int lh = mr.h / 2;
gemui_draw_text(cr, t, (gemui_rect_t){mr.x + 4, mr.y + 4, mr.w - 8, lh},
line1, -1, true, GEMUI_ALIGN_CENTER,
st->cfg[i].enabled ? t->fg_bright : t->fg_muted);
gemui_draw_text(cr, t, (gemui_rect_t){mr.x + 4, mr.y + lh - 2, mr.w - 8, lh},
line2, -2, false, GEMUI_ALIGN_CENTER,
is_sel ? t->accent : t->fg_muted);
gemui_hit_add(st->app, mr, HIT_CANVAS_MON, i);
}
}
y += can_h + pad / 2;
/* Inspector for selected output */
if (st->n_outs > 0 && st->sel_out >= 0 && st->sel_out < st->n_outs) {
anode_disp_out_t *so = &st->cfg[st->sel_out];
out_modes_t *om = &st->modes[st->sel_out];
/* Row 1: info + Enable + Primary */
int ctrl_w = gemui_scale_px(st->app, 128);
int info_w = cw - ctrl_w * 2 - pad;
char ilbl[128];
snprintf(ilbl, sizeof(ilbl), "%s (%d, %d) %s",
so->name, so->x, so->y, so->primary ? "Primary" : (so->enabled ? "Secondary" : "Disabled"));
gemui_draw_text(cr, t, (gemui_rect_t){pad, y, info_w, btn_h},
ilbl, 0, true, GEMUI_ALIGN_LEFT, t->fg_bright);
gemui_draw_toggle(st->app, cr, (gemui_rect_t){pad + info_w + pad / 2, y, ctrl_w, btn_h},
"Enabled", so->enabled, HIT_ENABLE_TOGGLE, st->sel_out, false);
gemui_draw_button(st->app, cr, (gemui_rect_t){pad + info_w + ctrl_w + pad, y, ctrl_w, btn_h},
"Set Primary", HIT_SET_PRIMARY, st->sel_out, false, so->primary, false);
y += btn_h + 6;
/* Row 2: Rotation */
int rot_lbl_w = gemui_scale_px(st->app, 86);
int rw = (cw - rot_lbl_w - pad * 3) / 4;
gemui_draw_text(cr, t, (gemui_rect_t){pad, y, rot_lbl_w, btn_h},
"Rotation", -1, false, GEMUI_ALIGN_LEFT, t->fg_muted);
for (int i = 0; i < 4; i++) {
gemui_draw_button(st->app, cr, (gemui_rect_t){pad + rot_lbl_w + i * (rw + pad), y, rw, btn_h},
ROTS[i], HIT_ROT, i, false, i == so->rot, false);
}
y += btn_h + 6;
/* Row 3: Paginated full Modes list */
int nav_w = gemui_scale_px(st->app, 52);
int total_pages = (om->num_modes + MODES_PER_PAGE - 1) / MODES_PER_PAGE;
if (total_pages < 1) total_pages = 1;
if (st->mode_page >= total_pages) st->mode_page = total_pages - 1;
if (st->mode_page < 0) st->mode_page = 0;
gemui_draw_text(cr, t, (gemui_rect_t){pad, y, rot_lbl_w, btn_h},
"Mode", -1, false, GEMUI_ALIGN_LEFT, t->fg_muted);
int mstart_x = pad + rot_lbl_w;
int m_area_w = cw - rot_lbl_w - nav_w * 2 - pad * 2;
int mw = (m_area_w - (MODES_PER_PAGE - 1) * (pad / 2)) / MODES_PER_PAGE;
int first = st->mode_page * MODES_PER_PAGE;
for (int i = 0; i < MODES_PER_PAGE && first + i < om->num_modes; i++) {
int mi = first + i;
char mlbl[32];
snprintf(mlbl, sizeof(mlbl), "%dx%d@%.0f", om->mode_w[mi], om->mode_h[mi], om->mode_hz[mi]);
gemui_draw_button(st->app, cr, (gemui_rect_t){mstart_x + i * (mw + pad / 2), y, mw, btn_h},
mlbl, HIT_MODE, mi, false, mi == om->sel_mode, false);
}
gemui_draw_button(st->app, cr, (gemui_rect_t){w - pad - nav_w * 2 - pad / 2, y, nav_w, btn_h},
"<", HIT_MODE_PAGE, -1, false, false, false);
gemui_draw_button(st->app, cr, (gemui_rect_t){w - pad - nav_w, y, nav_w, btn_h},
">", HIT_MODE_PAGE, 1, false, false, false);
}
/* Footer */
int bw = gemui_scale_px(st->app, 128);
int sbw = gemui_scale_px(st->app, 108);
int fy = h - btn_h - pad;
gemui_draw_button(st->app, cr, (gemui_rect_t){pad, fy, sbw, btn_h},
"Rescan", HIT_REFRESH, 0, false, false, false);
gemui_draw_button(st->app, cr, (gemui_rect_t){pad + sbw + pad / 2, fy, sbw, btn_h},
"Save Profile", HIT_SAVE_PROFILE, 0, false, false, false);
gemui_draw_button(st->app, cr, (gemui_rect_t){w - pad * 2 - bw * 2, fy, bw, btn_h},
"Apply Layout", HIT_APPLY_LAYOUT, 0, true, false, false);
gemui_draw_button(st->app, cr, (gemui_rect_t){w - pad - bw, fy, bw, btn_h},
"Close", HIT_QUIT, 0, false, false, false);
}
static void draw_timeout_stepper(gemui_app_t *app, cairo_t *cr, gemui_rect_t r,
const char *label, uint32_t ms, int hit_id) {
const gemui_theme_t *t = gemui_theme(app);
double rad = (double)gemui_scale_px(app, 6);
double lw = (t->scale >= 1.5 ? 1.5 : 1.0);
gemui_fill_round(cr, r, rad, t->bg_alt_even);
gemui_stroke_round(cr, r, rad, lw, t->border);
int pad_x = gemui_scale_px(app, 10);
int gap = gemui_scale_px(app, 4);
int pad_y = gemui_scale_px(app, 4);
int bh = r.h - pad_y * 2;
if (bh < gemui_scale_px(app, 20)) bh = r.h - 4;
int bw = bh;
if (bw < gemui_scale_px(app, 28)) bw = gemui_scale_px(app, 28);
int vw = gemui_scale_px(app, 48);
int ctrl_w = bw * 2 + vw + gap * 2;
gemui_draw_text(cr, t, (gemui_rect_t){r.x + pad_x, r.y, r.w - ctrl_w - pad_x * 2, r.h},
label, 0, false, GEMUI_ALIGN_LEFT, t->fg_main);
int cx = r.x + r.w - pad_x - ctrl_w;
int by = r.y + (r.h - bh) / 2;
gemui_draw_button(app, cr, (gemui_rect_t){cx, by, bw, bh}, "-", hit_id, -1, false, false, false);
char vtxt[16];
anode_power_format_timeout(ms, vtxt, sizeof(vtxt));
gemui_draw_text(cr, t, (gemui_rect_t){cx + bw + gap, r.y, vw, r.h},
vtxt, 0, true, GEMUI_ALIGN_CENTER,
ms > 0U ? t->fg_bright : t->fg_muted);
gemui_draw_button(app, cr, (gemui_rect_t){cx + bw + gap + vw + gap, by, bw, bh},
"+", hit_id, 1, false, false, false);
}
static bool adjust_timeout_hit(disp_state_t *st, int hit_id, int dir) {
struct {
int hit;
uint32_t *field;
const char *label;
bool is_susp;
} items[3] = {
{HIT_DIM_STEP, &st->pwr_cfg.dim_ms, "Dim brightness after", false},
{HIT_LOCK_STEP, &st->pwr_cfg.lock_ms, "Lock screen after", false},
{HIT_SUSP_STEP, &st->pwr_cfg.suspend_ms, "Auto suspend after", true}
};
for (int i = 0; i < 3; i++) {
if (items[i].hit == hit_id) {
*items[i].field = items[i].is_susp
? anode_power_step_suspend_timeout_ms(*items[i].field, dir)
: anode_power_step_screen_timeout_ms(*items[i].field, dir);
char vtxt[16], msg[64];
anode_power_format_timeout(*items[i].field, vtxt, sizeof(vtxt));
snprintf(msg, sizeof(msg), "%s: %s", items[i].label, vtxt);
save_power_cfg(st, msg);
return true;
}
}
return false;
}
static void draw_scaling_tab(disp_state_t *st, cairo_t *cr, int w, int h, int y) {
int pad = gemui_scale_px(st->app, 12);
int cw = w - pad * 2;
int btn_h = gemui_touch_row_px(st->app, 32);
/* 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(st->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(st->app, cr, (gemui_rect_t){pad, y, sw, btn_h}, "-0.05 Scale", HIT_SCALE_STEP, -1, false, false, false);
gemui_draw_button(st->app, cr, (gemui_rect_t){pad + sw + pad, y, sw, btn_h}, "Auto EDID", HIT_SCALE_STEP, 0, true, false, false);
gemui_draw_button(st->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(st->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(st->app, 50);
char btxt[24];
snprintf(btxt, sizeof(btxt), "%d%%", st->bri_pct);
gemui_draw_slider(st->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;
/* Screen Idle Timeouts */
const gemui_theme_t *t = gemui_theme(st->app);
int lbl_h = gemui_scale_px(st->app, 20);
gemui_draw_text(cr, t, (gemui_rect_t){pad, y, cw, lbl_h},
"Screen Idle Timeouts", -1, true, GEMUI_ALIGN_LEFT, t->fg_muted);
y += lbl_h + pad / 3;
int tw = (cw - pad) / 2;
draw_timeout_stepper(st->app, cr, (gemui_rect_t){pad, y, tw, btn_h},
"Dim Brightness", st->pwr_cfg.dim_ms, HIT_DIM_STEP);
draw_timeout_stepper(st->app, cr, (gemui_rect_t){pad + tw + pad, y, tw, btn_h},
"Lock Screen", st->pwr_cfg.lock_ms, HIT_LOCK_STEP);
y += btn_h + pad / 2;
draw_timeout_stepper(st->app, cr, (gemui_rect_t){pad, y, tw, btn_h},
"Auto Suspend", st->pwr_cfg.suspend_ms, HIT_SUSP_STEP);
gemui_draw_toggle(st->app, cr, (gemui_rect_t){pad + tw + pad, y, tw, btn_h},
"Suspend on AC", st->pwr_cfg.suspend_on_ac, HIT_SUSP_AC, 0, false);
int bw = gemui_scale_px(st->app, 128);
int fy = h - btn_h - pad;
gemui_draw_button(st->app, cr, (gemui_rect_t){w - pad - bw, fy, bw, btn_h},
"Close", HIT_QUIT, 0, false, false, false);
}
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 & Arrangement", badge);
int pad = gemui_scale_px(st->app, 12);
int y = gemui_scale_px(st->app, 42) + pad / 2;
int cw = w - pad * 2;
int tab_h = gemui_touch_row_px(app, 30);
int tab_w = (cw - pad) / 2;
gemui_draw_button(app, cr, (gemui_rect_t){pad, y, tab_w, tab_h},
"Monitor Arrangement", HIT_TAB, TAB_ARRANGE, false, st->tab == TAB_ARRANGE, false);
gemui_draw_button(app, cr, (gemui_rect_t){pad + tab_w + pad, y, tab_w, tab_h},
"Scaling, Brightness & Timeouts", HIT_TAB, TAB_SCALING, false, st->tab == TAB_SCALING, false);
y += tab_h + pad / 2;
if (st->tab == TAB_ARRANGE) draw_arrangement_tab(st, cr, w, h, y);
else draw_scaling_tab(st, cr, w, h, y);
}
static void on_key(gemui_app_t *app, KeySym ks, unsigned int state, const char *utf8, void *ud) {
(void)utf8;
disp_state_t *st = (disp_state_t *)ud;
if (ks == XK_Escape) {
if (st->confirm_sec > 0) confirm_revert(st);
else gemui_quit(app, 0);
return;
}
if (ks == XK_q) {
if (st->confirm_sec > 0) confirm_revert(st);
gemui_quit(app, 0);
return;
}
if (ks == XK_Return && st->confirm_sec > 0) {
confirm_keep(st);
return;
}
if (ks == XK_Tab) {
st->tab = (st->tab == TAB_ARRANGE) ? TAB_SCALING : TAB_ARRANGE;
if (st->tab == TAB_SCALING) load_power_cfg(st);
gemui_request_redraw(app);
return;
}
if (ks == XK_r) {
scan_outputs(st);
gemui_request_redraw(app);
return;
}
if (st->tab == TAB_ARRANGE && st->n_outs > 0 && st->sel_out >= 0 && st->sel_out < st->n_outs) {
int step = (state & ShiftMask) ? 128 : 16;
anode_disp_out_t *so = &st->cfg[st->sel_out];
if (ks == XK_Left && so->enabled) {
int nx = 0, ny = 0;
anode_disp_snap(st->cfg, st->n_outs, st->sel_out, so->x - step, so->y, 24, &nx, &ny);
so->x = nx; so->y = ny;
anode_disp_normalize(st->cfg, st->n_outs);
gemui_request_redraw(app);
return;
} else if (ks == XK_Right && so->enabled) {
int nx = 0, ny = 0;
anode_disp_snap(st->cfg, st->n_outs, st->sel_out, so->x + step, so->y, 24, &nx, &ny);
so->x = nx; so->y = ny;
anode_disp_normalize(st->cfg, st->n_outs);
gemui_request_redraw(app);
return;
} else if (ks == XK_Up && so->enabled) {
int nx = 0, ny = 0;
anode_disp_snap(st->cfg, st->n_outs, st->sel_out, so->x, so->y - step, 24, &nx, &ny);
so->x = nx; so->y = ny;
anode_disp_normalize(st->cfg, st->n_outs);
gemui_request_redraw(app);
return;
} else if (ks == XK_Down && so->enabled) {
int nx = 0, ny = 0;
anode_disp_snap(st->cfg, st->n_outs, st->sel_out, so->x, so->y + step, 24, &nx, &ny);
so->x = nx; so->y = ny;
anode_disp_normalize(st->cfg, st->n_outs);
gemui_request_redraw(app);
return;
}
}
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_UP) {
if (st->dragging) {
if (st->drag_moved) {
anode_disp_normalize(st->cfg, st->n_outs);
gemui_request_redraw(app);
}
st->dragging = false;
st->drag_moved = false;
}
st->bri_dragging = false;
return;
}
if (type == GEMUI_PTR_MOVE) {
if (st->dragging && st->drag_idx >= 0 && st->drag_idx < st->n_outs &&
st->cfg[st->drag_idx].enabled && st->map_scale > 0.0) {
int dx_px = x - st->drag_start_px;
int dy_px = y - st->drag_start_py;
if (!st->drag_moved && (abs(dx_px) + abs(dy_px) < 4)) return;
st->drag_moved = true;
int raw_x = st->drag_orig_x + (int)((double)dx_px / st->map_scale);
int raw_y = st->drag_orig_y + (int)((double)dy_px / st->map_scale);
int sx = raw_x, sy = raw_y;
anode_disp_snap(st->cfg, st->n_outs, st->drag_idx, raw_x, raw_y, 64, &sx, &sy);
st->cfg[st->drag_idx].x = sx;
st->cfg[st->drag_idx].y = sy;
gemui_request_redraw(app);
} else if (st->bri_dragging && st->bri_rect.w > 0) {
int pct = ((x - st->bri_rect.x) * 100) / st->bri_rect.w;
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);
}
return;
}
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_TAB) {
st->tab = hit->val;
if (st->tab == TAB_SCALING) load_power_cfg(st);
gemui_request_redraw(app);
} else if (hit->id == HIT_CANVAS_MON && hit->val >= 0 && hit->val < st->n_outs) {
st->sel_out = hit->val;
st->mode_page = st->modes[st->sel_out].sel_mode / MODES_PER_PAGE;
if (st->cfg[hit->val].enabled) {
st->dragging = true;
st->drag_moved = false;
st->drag_idx = hit->val;
st->drag_start_px = x;
st->drag_start_py = y;
st->drag_orig_x = st->cfg[hit->val].x;
st->drag_orig_y = st->cfg[hit->val].y;
}
gemui_request_redraw(app);
} else if (hit->id == HIT_PRESET && hit->val >= 0 && hit->val <= ANODE_DISP_PRESET_PRIMARY_ONLY) {
anode_disp_apply_preset(st->cfg, st->n_outs, st->sel_out, (anode_disp_preset_t)hit->val);
gemui_request_redraw(app);
} else if (hit->id == HIT_ENABLE_TOGGLE && hit->val >= 0 && hit->val < st->n_outs) {
if (st->cfg[hit->val].enabled) {
int en_cnt = 0;
for (int i = 0; i < st->n_outs; i++) {
if (st->cfg[i].enabled) en_cnt++;
}
if (en_cnt <= 1) {
gemui_show_toast(app, "At least one monitor must stay enabled", 2000);
return;
}
st->cfg[hit->val].enabled = false;
st->cfg[hit->val].primary = false;
} else {
st->cfg[hit->val].enabled = true;
if (st->cfg[hit->val].w <= 0 && st->modes[hit->val].num_modes > 0) {
st->cfg[hit->val].w = st->modes[hit->val].mode_w[0];
st->cfg[hit->val].h = st->modes[hit->val].mode_h[0];
st->cfg[hit->val].hz = st->modes[hit->val].mode_hz[0];
}
int max_x = 0;
for (int i = 0; i < st->n_outs; i++) {
if (i != hit->val && st->cfg[i].enabled) {
int ew = 0, eh = 0;
anode_disp_eff_size(st->cfg[i].w, st->cfg[i].h, st->cfg[i].rot, &ew, &eh);
if (st->cfg[i].x + ew > max_x) max_x = st->cfg[i].x + ew;
}
}
st->cfg[hit->val].x = max_x;
st->cfg[hit->val].y = 0;
}
anode_disp_normalize(st->cfg, st->n_outs);
gemui_request_redraw(app);
} else if (hit->id == HIT_SET_PRIMARY && hit->val >= 0 && hit->val < st->n_outs) {
if (!st->cfg[hit->val].enabled) st->cfg[hit->val].enabled = true;
for (int i = 0; i < st->n_outs; i++) st->cfg[i].primary = (i == hit->val);
anode_disp_normalize(st->cfg, st->n_outs);
gemui_request_redraw(app);
} else if (hit->id == HIT_ROT && st->n_outs > 0) {
st->cfg[st->sel_out].rot = hit->val & 3;
gemui_request_redraw(app);
} else if (hit->id == HIT_MODE_PAGE && st->n_outs > 0) {
int n_modes = st->modes[st->sel_out].num_modes;
int pages = (n_modes + MODES_PER_PAGE - 1) / MODES_PER_PAGE;
if (pages < 1) pages = 1;
st->mode_page += hit->val;
if (st->mode_page < 0) st->mode_page = 0;
if (st->mode_page >= pages) st->mode_page = pages - 1;
gemui_request_redraw(app);
} else if (hit->id == HIT_MODE && st->n_outs > 0) {
out_modes_t *om = &st->modes[st->sel_out];
if (hit->val >= 0 && hit->val < om->num_modes) {
om->sel_mode = hit->val;
st->cfg[st->sel_out].w = om->mode_w[hit->val];
st->cfg[st->sel_out].h = om->mode_h[hit->val];
st->cfg[st->sel_out].hz = om->mode_hz[hit->val];
gemui_request_redraw(app);
}
} else if (hit->id == HIT_REFRESH) {
scan_outputs(st);
gemui_show_toast(app, "Rescanned connected displays", 1400);
gemui_request_redraw(app);
} else if (hit->id == HIT_SAVE_PROFILE) {
save_current_profile(st, false);
} else if (hit->id == HIT_APPLY_LAYOUT) {
apply_layout_now(st);
} else if (hit->id == HIT_CONFIRM_KEEP) {
confirm_keep(st);
} else if (hit->id == HIT_CONFIRM_REVERT) {
confirm_revert(st);
} 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) {
st->bri_dragging = true;
st->bri_rect = hit->r;
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 (adjust_timeout_hit(st, hit->id, hit->val)) {
gemui_request_redraw(app);
} else if (hit->id == HIT_SUSP_AC) {
st->pwr_cfg.suspend_on_ac = !st->pwr_cfg.suspend_on_ac;
save_power_cfg(st, st->pwr_cfg.suspend_on_ac ? "Suspend on AC: On" : "Suspend on AC: Off");
gemui_request_redraw(app);
} else if (hit->id == HIT_QUIT) {
if (st->confirm_sec > 0) confirm_revert(st);
gemui_quit(app, 0);
}
}
int main(int argc, char **argv) {
bool force_arrange = false;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--arrange") == 0) force_arrange = true;
}
disp_state_t st;
memset(&st, 0, sizeof(st));
st.app = gemui_create("anode-display", "ANODE Display & Arrangement", 920, 620, false);
if (!st.app) return 1;
scan_outputs(&st);
st.tab = (force_arrange || st.n_outs > 1) ? TAB_ARRANGE : TAB_SCALING;
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;
}