fix(wm,st,theme): fix live theme reload across apps and top-bar artifacts

- Read XA_RESOURCE_MANAGER directly via XGetWindowProperty in anode_open_xres_db()
  instead of XResourceManagerString(), avoiding stale startup Xlib caches.
- Switch xrdb -merge to xrdb -load in anode-apply-theme and reset default
  palettes/resources on reload in dwm and st so omitted keys do not linger.
- Update Mod4+Shift+r (reloadxres) in dwm to run xrdb -load, signal running
  ANODE processes, and run anode-autostart reload while freeing old XftColors,
  preserving fullscreen borders, and updating non-ARGB tray icon backgrounds.
- Watch root PropertyNotify(XA_RESOURCE_MANAGER) in st, use sigaction(SIGUSR1),
  free reloaded resource strings, and refresh window background and cursor colors.
- Fix drw_text() return x-coordinate in dwm and dmenu and clear the bar pixmap
  in drawbar() to eliminate unpainted gaps before window titles on reload.
- Sync active_name on external theme reload in anode-themes and add pixel-level
  test_live_theme_reload_and_bar_artifacts() in tests/test_x11_xvfb.py.
This commit is contained in:
Luxferre committed 2026-11-26 21:01:58 +02:00
1 parent d1eff5562d
commit 86ca30db03
8 files changed
+343 -42

No files matched your search

+8 -1
View File
@@ -114,6 +114,13 @@ static void apply_selected(themes_state_t *st) {
gemui_request_redraw(st->app);
}
static void on_theme(gemui_app_t *app, void *ud) {
themes_state_t *st = (themes_state_t *)ud;
const gemui_theme_t *cur = gemui_theme(app);
if (cur && cur->name[0])
snprintf(st->active_name, sizeof(st->active_name), "%s", cur->name);
}
static void on_draw(gemui_app_t *app, cairo_t *cr, int w, int h, void *ud) {
themes_state_t *st = (themes_state_t *)ud;
const gemui_theme_t *t = gemui_theme(app);
@@ -252,7 +259,7 @@ int main(void) {
st.app = gemui_create("anode-themes", "ANODE Themes", 820, 520, false);
if (!st.app) return 1;
reload_themes(&st);
gemui_handlers_t h = { .on_draw = on_draw, .on_key = on_key, .on_ptr = on_ptr };
gemui_handlers_t h = { .on_draw = on_draw, .on_key = on_key, .on_ptr = on_ptr, .on_theme = on_theme };
gemui_set_handlers(st.app, &h, &st);
int rc = gemui_run(st.app);
gemui_destroy(st.app);
+17 -14
View File
@@ -84,23 +84,26 @@ static inline XrmDatabase anode_open_xres_db(Display *dpy) {
}
if (dpy) {
char *xrm = XResourceManagerString(dpy);
Atom actual_type = None;
int actual_format = 0;
unsigned long nitems = 0, bytes_after = 0;
unsigned char *prop = NULL;
XrmDatabase mdb = NULL;
if (xrm && xrm[0]) {
mdb = XrmGetStringDatabase(xrm);
} else {
Atom actual_type = None;
int actual_format = 0;
unsigned long nitems = 0, bytes_after = 0;
unsigned char *prop = NULL;
if (XGetWindowProperty(dpy, DefaultRootWindow(dpy), XA_RESOURCE_MANAGER,
0L, 65536L, False, XA_STRING, &actual_type,
&actual_format, &nitems, &bytes_after, &prop) == Success && prop) {
if (actual_type == XA_STRING && actual_format == 8 && nitems > 0) {
mdb = XrmGetStringDatabase((const char *)prop);
/* Always query root XA_RESOURCE_MANAGER directly; XResourceManagerString(dpy)
caches the startup string in Xlib and misses live xrdb updates. */
if (XGetWindowProperty(dpy, DefaultRootWindow(dpy), XA_RESOURCE_MANAGER,
0L, 262144L, False, XA_STRING, &actual_type,
&actual_format, &nitems, &bytes_after, &prop) == Success && prop) {
if (actual_type == XA_STRING && actual_format == 8 && nitems > 0) {
char *buf = (char *)malloc(nitems + 1);
if (buf) {
memcpy(buf, prop, nitems);
buf[nitems] = '\0';
mdb = XrmGetStringDatabase(buf);
free(buf);
}
XFree(prop);
}
XFree(prop);
}
if (mdb) {
if (db) {
+4 -2
View File
@@ -116,6 +116,8 @@ void drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled
int drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h,
unsigned int lpad, const char *text, int invert) {
char buf[1024];
int orig_x = x;
unsigned int orig_w = w;
int ty, render = x || y || w || h;
unsigned int ew, eh;
XftDraw *d = NULL;
@@ -132,7 +134,7 @@ int drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h,
DefaultVisual(drw->dpy, drw->screen),
DefaultColormap(drw->dpy, drw->screen));
x += (int)lpad;
if (w > lpad) w -= lpad;
w = (w > lpad) ? (w - lpad) : 0;
}
size_t len = strlen(text);
@@ -160,7 +162,7 @@ int drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h,
}
if (d) XftDrawDestroy(d);
return x + (render ? (int)w : 0);
return render ? (orig_x + (int)orig_w) : x;
}
int drw_text_align(Drw *drw, int x, int y, unsigned int w, unsigned int h,
+4 -2
View File
@@ -111,6 +111,8 @@ void drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled
int drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h,
unsigned int lpad, const char *text, int invert, Bool markup) {
char buf[1024];
int orig_x = x;
unsigned int orig_w = w;
int ty, render = x || y || w || h;
unsigned int ew, eh;
XftDraw *d = NULL;
@@ -127,7 +129,7 @@ int drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h,
DefaultVisual(drw->dpy, drw->screen),
DefaultColormap(drw->dpy, drw->screen));
x += (int)lpad;
if (w > lpad) w -= lpad;
w = (w > lpad) ? (w - lpad) : 0;
}
size_t len = strlen(text);
@@ -161,7 +163,7 @@ int drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h,
}
if (d) XftDrawDestroy(d);
return x + (render ? (int)w : 0);
return render ? (orig_x + (int)orig_w) : x;
}
void drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h) {
+49 -5
View File
@@ -957,6 +957,9 @@ static void drawbar(Monitor *m) {
if (showsystray && m == systraytomon(m) && !systrayonleft)
stw = (int)getsystraywidth();
drw_setscheme(drw, scheme[SchemeNorm]);
drw_rect(drw, 0, 0, (unsigned int)m->ww, (unsigned int)bh, 1, 1);
if (m == selmon) {
drw_setscheme(drw, scheme[SchemeNorm]);
tw = (int)TEXTW(stext) - lrpad / 2 + 2;
@@ -1545,6 +1548,12 @@ static void loadxres(void) {
}
int cur_sz = 0;
snprintf(normbgcolor, sizeof(normbgcolor), "#0b0e14");
snprintf(normbordercolor, sizeof(normbordercolor), "#283444");
snprintf(normfgcolor, sizeof(normfgcolor), "#e6e1cf");
snprintf(selbgcolor, sizeof(selbgcolor), "#2b5278");
snprintf(selbordercolor, sizeof(selbordercolor), "#59c2ff");
snprintf(selfgcolor, sizeof(selfgcolor), "#ffffff");
if (db) {
if ((v = xrm_lookup(db, "dwm.normbgcolor", "dwm.background", "theme.bgBar")))
snprintf(normbgcolor, sizeof(normbgcolor), "%s", v);
@@ -1617,8 +1626,13 @@ static void reloadxres(const Arg *arg) {
if (cursor[CurNormal])
XDefineCursor(dpy, root, cursor[CurNormal]->cursor);
free(scheme[SchemeNorm]);
free(scheme[SchemeSel]);
for (int si = 0; si < 2; si++) {
if (scheme[si]) {
for (int ci = 0; ci < 3; ci++)
XftColorFree(dpy, DefaultVisual(dpy, screen), DefaultColormap(dpy, screen), &scheme[si][ci]);
free(scheme[si]);
}
}
scheme[SchemeNorm] = drw_scm_create(drw, (const char **)colors[SchemeNorm], 3);
scheme[SchemeSel] = drw_scm_create(drw, (const char **)colors[SchemeSel], 3);
@@ -1627,14 +1641,20 @@ static void reloadxres(const Arg *arg) {
updatebarpos(m);
resizebarwin(m);
for (c = m->clients; c; c = c->next) {
c->bw = (int)borderpx;
if (!c->isfullscreen)
c->bw = (int)borderpx;
else
c->oldbw = (int)borderpx;
XSetWindowBorder(dpy, c->win,
scheme[c == selmon->sel ? SchemeSel : SchemeNorm][ColBorder].pixel);
}
}
if (showsystray && systray) {
for (c = systray->icons; c; c = c->next)
for (c = systray->icons; c; c = c->next) {
updatesystrayicongeom(c, c->w, c->h);
if (!systray->use_argb)
XSetWindowBackground(dpy, c->win, scheme[SchemeNorm][ColBg].pixel);
}
}
updatesystray(1);
updateworkarea();
@@ -1642,7 +1662,31 @@ static void reloadxres(const Arg *arg) {
drawbars();
XSync(dpy, False);
if (arg && arg->i) {
Arg a = {.v = autostartreloadcmd};
static const char *reloadcmd[] = {
"/bin/sh", "-c",
"xr=\"${ANODE_XRESOURCES:-$HOME/.Xresources}\"; "
"if [ -r \"$xr\" ] && command -v xrdb >/dev/null 2>&1; then "
" xrdb -load \"$xr\" >/dev/null 2>&1 || true; "
"fi; "
"if [ \"${ANODE_NO_SIGNAL:-0}\" != \"1\" ]; then "
" _pids=\"$( (ps -eo pid=,comm= 2>/dev/null || ps -ax -o pid=,comm= 2>/dev/null || true) | awk '"
" { pid = $1; cmd = $2; sub(/^.*\\//, \"\", cmd); "
" if (cmd == \"st\" || cmd == \"anode-statusd\" || "
" cmd == \"anode-net\" || cmd == \"anode-audio\" || "
" cmd == \"anode-display\" || cmd == \"anode-datetime\" || "
" cmd == \"anode-power\" || cmd == \"anode-themes\") print \"USR1 \" pid; "
" else if (cmd == \"anode-vkbd\") print \"HUP \" pid; "
" }' )\"; "
" if [ -n \"${_pids:-}\" ]; then "
" printf '%s\\n' \"$_pids\" | while read -r _sig _p; do "
" [ -n \"${_p:-}\" ] && kill -\"$_sig\" \"$_p\" 2>/dev/null || true; "
" done; "
" fi; "
"fi; "
"exec anode-autostart reload",
NULL
};
Arg a = {.v = reloadcmd};
spawn(&a);
}
}
+81 -15
View File
@@ -274,6 +274,7 @@ static char *usedfont = NULL;
static double usedfontsize = 0;
static double defaultfontsize = 0;
static double st_xft_dpi = 96.0;
static volatile sig_atomic_t st_reload_requested = 0;
static char *opt_class = NULL;
static char **opt_cmd = NULL;
@@ -692,6 +693,11 @@ propnotify(XEvent *e)
Atom clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
xpev = &e->xproperty;
if (xpev->window == XRootWindow(xw.dpy, xw.scr)) {
if (xpev->atom == XA_RESOURCE_MANAGER && xpev->state != PropertyDelete)
st_reload_requested = 1;
return;
}
if (xpev->state == PropertyNewValue &&
(xpev->atom == XA_PRIMARY ||
xpev->atom == clipboard)) {
@@ -1405,6 +1411,8 @@ xinit(int cols, int rows)
xw.selectionmode = XInternAtom(xw.dpy, "_ST_SELMODE", False);
updateselectionmodeatom();
XSelectInput(xw.dpy, XRootWindow(xw.dpy, xw.scr), PropertyChangeMask);
/* pointer cursor setup and blank cursor for hide-while-typing */
{
int ev_base = 0, err_base = 0;
@@ -2342,7 +2350,6 @@ resize(XEvent *e)
cresize(e->xconfigure.width, e->xconfigure.height);
}
static volatile sig_atomic_t st_reload_requested = 0;
static char st_theme_font[256] = "";
static char st_theme_bg[32] = "";
static char st_theme_fg[32] = "";
@@ -2364,7 +2371,7 @@ run(void)
int w = win.w, h = win.h;
fd_set rfd;
int xfd = XConnectionNumber(xw.dpy), ttyfd, xev, drawing;
struct timespec seltv, *tv, now, lastblink, trigger;
struct timespec seltv, *tv, now, lastblink, trigger = {0};
double timeout;
/* Waiting for window mapping */
@@ -2387,15 +2394,32 @@ run(void)
ttyfd = ttynew(opt_line, shell, opt_io, opt_cmd);
cresize(w, h);
signal(SIGUSR1, st_sigusr1);
{
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = st_sigusr1;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(SIGUSR1, &sa, NULL);
}
for (timeout = -1, drawing = 0, lastblink = (struct timespec){0};;) {
if (st_reload_requested) {
XColor xmousefg = {0}, xmousebg = {0};
st_reload_requested = 0;
drawing = 0;
timeout = -1;
config_init();
xloadcols();
XSetWindowBackground(xw.dpy, xw.win, dc.col[defaultbg].pixel);
xmousefg.red = dc.col[mousefg].color.red;
xmousefg.green = dc.col[mousefg].color.green;
xmousefg.blue = dc.col[mousefg].color.blue;
xmousebg.red = dc.col[mousebg].color.red;
xmousebg.green = dc.col[mousebg].color.green;
xmousebg.blue = dc.col[mousebg].color.blue;
XRecolorCursor(xw.dpy, xw.vpointer, &xmousefg, &xmousebg);
xunloadfonts();
hbunloadfonts();
usedfont = opt_font ? opt_font : font;
xloadfonts(usedfont, 0);
cresize(win.w, win.h);
@@ -2449,6 +2473,8 @@ run(void)
if (handler[ev.type])
(handler[ev.type])(&ev);
}
if (st_reload_requested)
continue;
/*
* To reduce flicker and tearing, when new content or event
@@ -2492,8 +2518,16 @@ run(void)
}
}
static int st_defaults_saved = 0;
static const char *default_colorname[LEN(colorname)];
static char *default_font = NULL;
static char *default_termname = NULL;
static char *default_shell = NULL;
static char *st_res_alloc[LEN(resources)];
static char *st_cap_font_alloc = NULL;
int
resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst)
resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst, char **alloc_slot)
{
char **sdst = dst;
int *idst = dst;
@@ -2501,8 +2535,8 @@ resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst)
char fullname[256];
char fullclass[256];
char *type;
XrmValue ret;
char *type = NULL;
XrmValue ret = {0};
snprintf(fullname, sizeof(fullname), "%s.%s",
opt_name ? opt_name : "st", name);
@@ -2510,14 +2544,22 @@ resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst)
opt_class ? opt_class : "St", name);
fullname[sizeof(fullname) - 1] = fullclass[sizeof(fullclass) - 1] = '\0';
XrmGetResource(db, fullname, fullclass, &type, &ret);
if (ret.addr == NULL || strncmp("String", type, 64))
if (!XrmGetResource(db, fullname, fullclass, &type, &ret) ||
ret.addr == NULL || !type || strncmp("String", type, 64))
return 1;
switch (rtype) {
case STRING:
*sdst = strdup(ret.addr);
case STRING: {
char *dup = strdup(ret.addr);
if (dup) {
*sdst = dup;
if (alloc_slot) {
free(*alloc_slot);
*alloc_slot = dup;
}
}
break;
}
case INTEGER:
*idst = strtoul(ret.addr, NULL, 10);
break;
@@ -2537,6 +2579,25 @@ config_init(void)
XrmValue ret;
double dpi = 96.0, scale = 1.0;
if (!st_defaults_saved) {
memcpy(default_colorname, colorname, sizeof(default_colorname));
default_font = font;
default_termname = termname;
default_shell = shell;
st_defaults_saved = 1;
} else {
for (size_t ri = 0; ri < LEN(resources); ri++) {
free(st_res_alloc[ri]);
st_res_alloc[ri] = NULL;
}
free(st_cap_font_alloc);
st_cap_font_alloc = NULL;
memcpy(colorname, default_colorname, sizeof(default_colorname));
font = default_font;
termname = default_termname;
shell = default_shell;
}
XrmInitialize();
db = anode_open_xres_db(xw.dpy);
anode_read_xres_scale_from_db(db, xw.dpy, &dpi, &scale, NULL);
@@ -2549,8 +2610,9 @@ config_init(void)
int has_font = 0, has_bg = 0, has_fg = 0, has_cs = 0;
if (db) {
for (p = resources; p < resources + LEN(resources); p++) {
if (resource_load(db, p->name, p->type, p->dst) == 0) {
for (size_t ri = 0; ri < LEN(resources); ri++) {
p = &resources[ri];
if (resource_load(db, p->name, p->type, p->dst, &st_res_alloc[ri]) == 0) {
if (strcmp(p->name, "font") == 0) has_font = 1;
else if (strcmp(p->name, "background") == 0) has_bg = 1;
else if (strcmp(p->name, "foreground") == 0) has_fg = 1;
@@ -2566,8 +2628,12 @@ config_init(void)
"__anode_probe__.font", &probe_type, &probe_val) &&
probe_type && strcmp(probe_type, "String") == 0 && probe_val.addr;
if (!have_wild || strcmp(cap_val.addr, probe_val.addr) != 0) {
font = strdup(cap_val.addr);
has_font = 1;
free(st_cap_font_alloc);
st_cap_font_alloc = strdup(cap_val.addr);
if (st_cap_font_alloc) {
font = st_cap_font_alloc;
has_font = 1;
}
}
}
}