suckless-patches

My patches for http://suckless.org
git clone git://mahdi.pw/suckless-patches.git
Log | Files | Refs | README | LICENSE

st-xresources-signal-reloading-20220312-6685098.diff (4393B)


      1 From 6685098b4e368a750b200adda0be64198ac386a8 Mon Sep 17 00:00:00 2001
      2 From: MahdiMirzade <[email protected]>
      3 Date: Sat, 12 Mar 2022 16:05:23 +0330
      4 Subject: [PATCH] handle st settings from Xresources + reload all st instances
      5  by running 'pidof st | xargs kill -s USR1'
      6 
      7 ---
      8  x.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      9  1 file changed, 127 insertions(+)
     10 
     11 diff --git a/x.c b/x.c
     12 index cd96575..61701b5 100644
     13 --- a/x.c
     14 +++ b/x.c
     15 @@ -14,6 +14,7 @@
     16  #include <X11/keysym.h>
     17  #include <X11/Xft/Xft.h>
     18  #include <X11/XKBlib.h>
     19 +#include <X11/Xresource.h>
     20  
     21  char *argv0;
     22  #include "arg.h"
     23 @@ -2011,6 +2012,130 @@ run(void)
     24  	}
     25  }
     26  
     27 +#define XRESOURCE_LOAD_META(NAME)					\
     28 +	if(!XrmGetResource(xrdb, "st." NAME, "st." NAME, &type, &ret))	\
     29 +		XrmGetResource(xrdb, "*." NAME, "*." NAME, &type, &ret); \
     30 +	if (ret.addr != NULL && !strncmp("String", type, 64))
     31 +
     32 +#define XRESOURCE_LOAD_STRING(NAME, DST)	\
     33 +	XRESOURCE_LOAD_META(NAME)		\
     34 +		DST = ret.addr;
     35 +
     36 +#define XRESOURCE_LOAD_CHAR(NAME, DST)		\
     37 +	XRESOURCE_LOAD_META(NAME)		\
     38 +		DST = ret.addr[0];
     39 +
     40 +#define XRESOURCE_LOAD_INTEGER(NAME, DST)		\
     41 +	XRESOURCE_LOAD_META(NAME)			\
     42 +		DST = strtoul(ret.addr, NULL, 10);
     43 +
     44 +#define XRESOURCE_LOAD_FLOAT(NAME, DST)		\
     45 +	XRESOURCE_LOAD_META(NAME)		\
     46 +		DST = strtof(ret.addr, NULL);
     47 +
     48 +void
     49 +xrdb_load(void)
     50 +{
     51 +	/* XXX */
     52 +	char *xrm;
     53 +	char *type;
     54 +	XrmDatabase xrdb;
     55 +	XrmValue ret;
     56 +	Display *dpy;
     57 +
     58 +	if(!(dpy = XOpenDisplay(NULL)))
     59 +		die("Can't open display\n");
     60 +
     61 +	XrmInitialize();
     62 +	xrm = XResourceManagerString(dpy);
     63 +
     64 +	if (xrm != NULL) {
     65 +		xrdb = XrmGetStringDatabase(xrm);
     66 +
     67 +		/* handling colors here without macros to do via loop. */
     68 +		int i = 0;
     69 +		char loadValue[12] = "";
     70 +		for (i = 0; i < 256; i++)
     71 +		{
     72 +			sprintf(loadValue, "%s%d", "st.color", i);
     73 +
     74 +			if(!XrmGetResource(xrdb, loadValue, loadValue, &type, &ret))
     75 +			{
     76 +				sprintf(loadValue, "%s%d", "*.color", i);
     77 +				if (!XrmGetResource(xrdb, loadValue, loadValue, &type, &ret))
     78 +					/* reset if not found (unless in range for defaults). */
     79 +					if (i > 15)
     80 +						colorname[i] = NULL;
     81 +			}
     82 +
     83 +			if (ret.addr != NULL && !strncmp("String", type, 64))
     84 +				colorname[i] = ret.addr;
     85 +		}
     86 +
     87 +		XRESOURCE_LOAD_STRING("foreground", colorname[defaultfg]);
     88 +		XRESOURCE_LOAD_STRING("background", colorname[defaultbg]);
     89 +		XRESOURCE_LOAD_STRING("cursorfg", colorname[defaultcs])
     90 +		else {
     91 +		  // this looks confusing because we are chaining off of the if
     92 +		  // in the macro. probably we should be wrapping everything blocks
     93 +		  // so this isn't possible...
     94 +		  defaultcs = defaultfg;
     95 +		}
     96 +		XRESOURCE_LOAD_STRING("reverse-cursor", colorname[defaultrcs])
     97 +		else {
     98 +		  // see above.
     99 +		  defaultrcs = defaultbg;
    100 +		}
    101 +
    102 +		XRESOURCE_LOAD_STRING("font", font);
    103 +		XRESOURCE_LOAD_STRING("termname", termname);
    104 +
    105 +		/* XRESOURCE_LOAD_INTEGER("xfps", xfps); */
    106 +		/* XRESOURCE_LOAD_INTEGER("actionfps", actionfps); */
    107 +		XRESOURCE_LOAD_INTEGER("blinktimeout", blinktimeout);
    108 +		XRESOURCE_LOAD_INTEGER("bellvolume", bellvolume);
    109 +		XRESOURCE_LOAD_INTEGER("borderpx", borderpx);
    110 +		/* XRESOURCE_LOAD_INTEGER("borderless", borderless); */
    111 +		XRESOURCE_LOAD_INTEGER("cursorshape", cursorshape);
    112 +
    113 +		/* cursorblinkstate = 1; // in case if cursor shape was changed from a blinking one to a non-blinking */
    114 +		/* XRESOURCE_LOAD_INTEGER("cursorthickness", cursorthickness); */
    115 +		/* XRESOURCE_LOAD_INTEGER("cursorblinkstyle", cursorblinkstyle); */
    116 +		/* XRESOURCE_LOAD_INTEGER("cursorblinkontype", cursorblinkontype); */
    117 +
    118 +		/* todo: https://github.com/gnotclub/xst/commit/1e82647b0e04077e975679a4b4cf1eb02b04e6bc */
    119 +		/* XRESOURCE_LOAD_INTEGER("mouseScrollLines", mousescrolllines); */
    120 +
    121 +		XRESOURCE_LOAD_FLOAT("cwscale", cwscale);
    122 +		XRESOURCE_LOAD_FLOAT("chscale", chscale);
    123 +
    124 +		/* XRESOURCE_LOAD_CHAR("prompt_char", prompt_char); */
    125 +
    126 +	}
    127 +	XFlush(dpy);
    128 +}
    129 +
    130 +void
    131 +reload(int sig)
    132 +{
    133 +	xrdb_load();
    134 +
    135 +	/* colors, fonts */
    136 +	xloadcols();
    137 +	xunloadfonts();
    138 +	xloadfonts(font, 0);
    139 +
    140 +	/* pretend the window just got resized */
    141 +	cresize(win.w, win.h);
    142 +
    143 +	redraw();
    144 +
    145 +	/* triggers re-render if we're visible. */
    146 +	ttywrite("\033[O", 3, 1);
    147 +
    148 +	signal(SIGUSR1, reload);
    149 +}
    150 +
    151  void
    152  usage(void)
    153  {
    154 @@ -2084,6 +2209,8 @@ run:
    155  
    156  	setlocale(LC_CTYPE, "");
    157  	XSetLocaleModifiers("");
    158 +	xrdb_load();
    159 +	signal(SIGUSR1, reload);
    160  	cols = MAX(cols, 1);
    161  	rows = MAX(rows, 1);
    162  	tnew(cols, rows);
    163 -- 
    164 2.35.1
    165