From 540a923fb95366fee26309d0b606199a5aeb25f5 Mon Sep 17 00:00:00 2001 From: Loic Coyle Date: Sat, 24 Apr 2021 00:17:23 +0200 Subject: [PATCH] Add XRESOURCES_RELOAD_PATCH --- patch/xresources.c | 29 ++++++++++++++++++++++++++++- patch/xresources.h | 6 +++++- patches.def.h | 6 ++++++ x.c | 6 ++++-- 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/patch/xresources.c b/patch/xresources.c index b7808b2..52ac90d 100644 --- a/patch/xresources.c +++ b/patch/xresources.c @@ -34,19 +34,46 @@ resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst) return 0; } +#if XRESOURCES_RELOAD_PATCH +void +config_init(Display *dpy) +{ +#else void config_init(void) { +#endif // XRESOURCES_RELOAD_PATCH char *resm; XrmDatabase db; ResourcePref *p; XrmInitialize(); + #if XRESOURCES_RELOAD_PATCH + resm = XResourceManagerString(dpy); + #else resm = XResourceManagerString(xw.dpy); + #endif // XRESOURCES_RELOAD_PATCH if (!resm) return; db = XrmGetStringDatabase(resm); for (p = resources; p < resources + LEN(resources); p++) resource_load(db, p->name, p->type, p->dst); -} \ No newline at end of file +} + +#if XRESOURCES_RELOAD_PATCH +void reload_config(int sig){ + /* Recreate a Display object to have up to date Xresources entries */ + Display *dpy; + if (!(dpy = XOpenDisplay(NULL))) + die("Can't open display\n"); + + config_init(dpy); + if (sig != -1) { + /* Called due to a SIGUSR1 */ + xloadcols(); + redraw(); + } + signal(SIGUSR1, reload_config); +} +#endif // XRESOURCES_RELOAD_PATCH diff --git a/patch/xresources.h b/patch/xresources.h index a8da8a1..e14b823 100644 --- a/patch/xresources.h +++ b/patch/xresources.h @@ -14,4 +14,8 @@ typedef struct { } ResourcePref; int resource_load(XrmDatabase, char *, enum resource_type, void *); -void config_init(void); \ No newline at end of file +#if XRESOURCES_RELOAD_PATCH +void config_init(Display *dpy); +#else +void config_init(void); +#endif // XRESOURCES_RELOAD_PATCH diff --git a/patches.def.h b/patches.def.h index 3d7acd7..4fdb3bd 100644 --- a/patches.def.h +++ b/patches.def.h @@ -292,3 +292,9 @@ * https://st.suckless.org/patches/xresources/ */ #define XRESOURCES_PATCH 0 + +/* This patch adds the ability to reload the Xresources config when a SIGUSR1 signal is received + e.g.: killall -USR1 st + Depends on the XRESOURCES_PATCH. + */ +#define XRESOURCES_RELOAD_PATCH 0 diff --git a/x.c b/x.c index 4bb4199..77ab88b 100644 --- a/x.c +++ b/x.c @@ -2576,12 +2576,14 @@ run: setlocale(LC_CTYPE, ""); XSetLocaleModifiers(""); - #if XRESOURCES_PATCH + #if XRESOURCES_RELOAD_PATCH + reload_config(-1); + #elif XRESOURCES_PATCH if (!(xw.dpy = XOpenDisplay(NULL))) die("Can't open display\n"); config_init(); - #endif // XRESOURCES_PATCH + #endif // XRESOURCES_RELOAD_PATCH cols = MAX(cols, 1); rows = MAX(rows, 1); tnew(cols, rows);