st

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit c872aa8386c742e022bc67db64dc4bce469e1d93
parent 9d2229e2958cf082deb17a068868ab07f8895214
Author: siduck76 <siduckk76@protonmail.com>
Date:   Sun, 28 Mar 2021 17:48:20 +0530

xresources : live reload !!

Diffstat:
Mx.c | 149+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------
Ax.c.rej | 150+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 277 insertions(+), 22 deletions(-)

diff --git a/x.c b/x.c @@ -2089,6 +2089,131 @@ run(void) } } +#define XRESOURCE_LOAD_META(NAME) \ + if(!XrmGetResource(xrdb, "st." NAME, "st." NAME, &type, &ret)) \ + XrmGetResource(xrdb, "*." NAME, "*." NAME, &type, &ret); \ + if (ret.addr != NULL && !strncmp("String", type, 64)) + +#define XRESOURCE_LOAD_STRING(NAME, DST) \ + XRESOURCE_LOAD_META(NAME) \ + DST = ret.addr; + +#define XRESOURCE_LOAD_CHAR(NAME, DST) \ + XRESOURCE_LOAD_META(NAME) \ + DST = ret.addr[0]; + +#define XRESOURCE_LOAD_INTEGER(NAME, DST) \ + XRESOURCE_LOAD_META(NAME) \ + DST = strtoul(ret.addr, NULL, 10); + +#define XRESOURCE_LOAD_FLOAT(NAME, DST) \ + XRESOURCE_LOAD_META(NAME) \ + DST = strtof(ret.addr, NULL); + +void +xrdb_load(void) +{ + /* XXX */ + char *xrm; + char *type; + XrmDatabase xrdb; + XrmValue ret; + Display *dpy; + + if(!(dpy = XOpenDisplay(NULL))) + die("Can't open display\n"); + + XrmInitialize(); + xrm = XResourceManagerString(dpy); + + if (xrm != NULL) { + xrdb = XrmGetStringDatabase(xrm); + + /* handling colors here without macros to do via loop. */ + int i = 0; + char loadValue[12] = ""; + for (i = 0; i < 256; i++) + { + sprintf(loadValue, "%s%d", "st.color", i); + + if(!XrmGetResource(xrdb, loadValue, loadValue, &type, &ret)) + { + sprintf(loadValue, "%s%d", "*.color", i); + if (!XrmGetResource(xrdb, loadValue, loadValue, &type, &ret)) + /* reset if not found (unless in range for defaults). */ + if (i > 15) + colorname[i] = NULL; + } + + if (ret.addr != NULL && !strncmp("String", type, 64)) + colorname[i] = ret.addr; + } + + XRESOURCE_LOAD_STRING("foreground", colorname[defaultfg]); + XRESOURCE_LOAD_STRING("background", colorname[defaultbg]); + XRESOURCE_LOAD_STRING("cursorfg", colorname[defaultcs]) + else { + // this looks confusing because we are chaining off of the if + // in the macro. probably we should be wrapping everything blocks + // so this isn't possible... + defaultcs = defaultfg; + } + XRESOURCE_LOAD_STRING("reverse-cursor", colorname[defaultrcs]) + else { + // see above. + defaultrcs = defaultbg; + } + + XRESOURCE_LOAD_STRING("font", font); + XRESOURCE_LOAD_STRING("termname", termname); + + /* XRESOURCE_LOAD_INTEGER("xfps", xfps); */ + /* XRESOURCE_LOAD_INTEGER("actionfps", actionfps); */ + XRESOURCE_LOAD_INTEGER("blinktimeout", blinktimeout); + XRESOURCE_LOAD_INTEGER("bellvolume", bellvolume); + XRESOURCE_LOAD_INTEGER("borderpx", borderpx); + /* XRESOURCE_LOAD_INTEGER("borderless", borderless); */ + XRESOURCE_LOAD_INTEGER("cursorshape", cursorshape); + + /* cursorblinkstate = 1; // in case if cursor shape was changed from a blinking one to a non-blinking */ + /* XRESOURCE_LOAD_INTEGER("cursorthickness", cursorthickness); */ + /* XRESOURCE_LOAD_INTEGER("cursorblinkstyle", cursorblinkstyle); */ + /* XRESOURCE_LOAD_INTEGER("cursorblinkontype", cursorblinkontype); */ + + /* todo: https://github.com/gnotclub/xst/commit/1e82647b0e04077e975679a4b4cf1eb02b04e6bc */ + /* XRESOURCE_LOAD_INTEGER("mouseScrollLines", mousescrolllines); */ + + XRESOURCE_LOAD_FLOAT("cwscale", cwscale); + XRESOURCE_LOAD_FLOAT("chscale", chscale); + + /* XRESOURCE_LOAD_CHAR("prompt_char", prompt_char); */ + + } + XFlush(dpy); +} + +void +reload(int sig) +{ + xrdb_load(); + + /* colors, fonts */ + xloadcols(); + xunloadfonts(); + xloadfonts(font, 0); + + /* pretend the window just got resized */ + cresize(win.w, win.h); + + redraw(); + + /* triggers re-render if we're visible. */ + ttywrite("\033[O", 3, 1); + + signal(SIGUSR1, reload); +} + + int resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst) { @@ -2155,28 +2280,6 @@ usage(void) " [stty_args ...]\n", argv0, argv0); } -void -reload(int sig) -{ - config_init(); - - /* colors, fonts */ - xloadcols(); - xunloadfonts(); - usedfont = (opt_font == NULL)? font : opt_font; - xloadfonts(usedfont, 0); - - /* pretend the window just got resized */ - cresize(win.w, win.h); - - redraw(); - - /* triggers re-render if we're visible. */ - ttywrite("\033[O", 3, 1); - - signal(SIGUSR1, reload); -} - int main(int argc, char *argv[]) { @@ -2240,6 +2343,8 @@ run: setlocale(LC_CTYPE, ""); XSetLocaleModifiers(""); + xrdb_load(); + signal(SIGUSR1, reload); if(!(xw.dpy = XOpenDisplay(NULL))) die("Can't open display\n"); diff --git a/x.c.rej b/x.c.rej @@ -0,0 +1,150 @@ +--- x.c ++++ x.c +@@ -14,6 +14,7 @@ + #include <X11/keysym.h> + #include <X11/Xft/Xft.h> + #include <X11/XKBlib.h> ++#include <X11/Xresource.h> + + char *argv0; + #include "arg.h" +@@ -1978,6 +1979,130 @@ run(void) + } + } + ++#define XRESOURCE_LOAD_META(NAME) \ ++ if(!XrmGetResource(xrdb, "st." NAME, "st." NAME, &type, &ret)) \ ++ XrmGetResource(xrdb, "*." NAME, "*." NAME, &type, &ret); \ ++ if (ret.addr != NULL && !strncmp("String", type, 64)) ++ ++#define XRESOURCE_LOAD_STRING(NAME, DST) \ ++ XRESOURCE_LOAD_META(NAME) \ ++ DST = ret.addr; ++ ++#define XRESOURCE_LOAD_CHAR(NAME, DST) \ ++ XRESOURCE_LOAD_META(NAME) \ ++ DST = ret.addr[0]; ++ ++#define XRESOURCE_LOAD_INTEGER(NAME, DST) \ ++ XRESOURCE_LOAD_META(NAME) \ ++ DST = strtoul(ret.addr, NULL, 10); ++ ++#define XRESOURCE_LOAD_FLOAT(NAME, DST) \ ++ XRESOURCE_LOAD_META(NAME) \ ++ DST = strtof(ret.addr, NULL); ++ ++void ++xrdb_load(void) ++{ ++ /* XXX */ ++ char *xrm; ++ char *type; ++ XrmDatabase xrdb; ++ XrmValue ret; ++ Display *dpy; ++ ++ if(!(dpy = XOpenDisplay(NULL))) ++ die("Can't open display\n"); ++ ++ XrmInitialize(); ++ xrm = XResourceManagerString(dpy); ++ ++ if (xrm != NULL) { ++ xrdb = XrmGetStringDatabase(xrm); ++ ++ /* handling colors here without macros to do via loop. */ ++ int i = 0; ++ char loadValue[12] = ""; ++ for (i = 0; i < 256; i++) ++ { ++ sprintf(loadValue, "%s%d", "st.color", i); ++ ++ if(!XrmGetResource(xrdb, loadValue, loadValue, &type, &ret)) ++ { ++ sprintf(loadValue, "%s%d", "*.color", i); ++ if (!XrmGetResource(xrdb, loadValue, loadValue, &type, &ret)) ++ /* reset if not found (unless in range for defaults). */ ++ if (i > 15) ++ colorname[i] = NULL; ++ } ++ ++ if (ret.addr != NULL && !strncmp("String", type, 64)) ++ colorname[i] = ret.addr; ++ } ++ ++ XRESOURCE_LOAD_STRING("foreground", colorname[defaultfg]); ++ XRESOURCE_LOAD_STRING("background", colorname[defaultbg]); ++ XRESOURCE_LOAD_STRING("cursorfg", colorname[defaultcs]) ++ else { ++ // this looks confusing because we are chaining off of the if ++ // in the macro. probably we should be wrapping everything blocks ++ // so this isn't possible... ++ defaultcs = defaultfg; ++ } ++ XRESOURCE_LOAD_STRING("reverse-cursor", colorname[defaultrcs]) ++ else { ++ // see above. ++ defaultrcs = defaultbg; ++ } ++ ++ XRESOURCE_LOAD_STRING("font", font); ++ XRESOURCE_LOAD_STRING("termname", termname); ++ ++ /* XRESOURCE_LOAD_INTEGER("xfps", xfps); */ ++ /* XRESOURCE_LOAD_INTEGER("actionfps", actionfps); */ ++ XRESOURCE_LOAD_INTEGER("blinktimeout", blinktimeout); ++ XRESOURCE_LOAD_INTEGER("bellvolume", bellvolume); ++ XRESOURCE_LOAD_INTEGER("borderpx", borderpx); ++ /* XRESOURCE_LOAD_INTEGER("borderless", borderless); */ ++ XRESOURCE_LOAD_INTEGER("cursorshape", cursorshape); ++ ++ /* cursorblinkstate = 1; // in case if cursor shape was changed from a blinking one to a non-blinking */ ++ /* XRESOURCE_LOAD_INTEGER("cursorthickness", cursorthickness); */ ++ /* XRESOURCE_LOAD_INTEGER("cursorblinkstyle", cursorblinkstyle); */ ++ /* XRESOURCE_LOAD_INTEGER("cursorblinkontype", cursorblinkontype); */ ++ ++ /* todo: https://github.com/gnotclub/xst/commit/1e82647b0e04077e975679a4b4cf1eb02b04e6bc */ ++ /* XRESOURCE_LOAD_INTEGER("mouseScrollLines", mousescrolllines); */ ++ ++ XRESOURCE_LOAD_FLOAT("cwscale", cwscale); ++ XRESOURCE_LOAD_FLOAT("chscale", chscale); ++ ++ /* XRESOURCE_LOAD_CHAR("prompt_char", prompt_char); */ ++ ++ } ++ XFlush(dpy); ++} ++ ++void ++reload(int sig) ++{ ++ xrdb_load(); ++ ++ /* colors, fonts */ ++ xloadcols(); ++ xunloadfonts(); ++ xloadfonts(font, 0); ++ ++ /* pretend the window just got resized */ ++ cresize(win.w, win.h); ++ ++ redraw(); ++ ++ /* triggers re-render if we're visible. */ ++ ttywrite("\033[O", 3, 1); ++ ++ signal(SIGUSR1, reload); ++} ++ + void + usage(void) + { +@@ -2051,6 +2176,8 @@ main(int argc, char *argv[]) + + setlocale(LC_CTYPE, ""); + XSetLocaleModifiers(""); ++ xrdb_load(); ++ signal(SIGUSR1, reload); + cols = MAX(cols, 1); + rows = MAX(rows, 1); + tnew(cols, rows);