sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

st-xresources-signal-reloading-20220309-1bb14b1.diff (10324B)


      1 From 1bb14b1cd5b394fb6e777fce1316d5f788b20bea Mon Sep 17 00:00:00 2001
      2 From: MahdiMirzade <me@mahdym.ir>
      3 Date: Wed, 9 Mar 2022 17:19:12 +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  arg.h |  50 -----------------------
      9  st.h  | 128 ----------------------------------------------------------
     10  win.h |  40 ------------------
     11  x.c   | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     12  4 files changed, 126 insertions(+), 218 deletions(-)
     13  delete mode 100644 arg.h
     14  delete mode 100644 st.h
     15  delete mode 100644 win.h
     16 
     17 diff --git a/arg.h b/arg.h
     18 deleted file mode 100644
     19 index a22e019..0000000
     20 --- a/arg.h
     21 +++ /dev/null
     22 @@ -1,50 +0,0 @@
     23 -/*
     24 - * Copy me if you can.
     25 - * by 20h
     26 - */
     27 -
     28 -#ifndef ARG_H__
     29 -#define ARG_H__
     30 -
     31 -extern char *argv0;
     32 -
     33 -/* use main(int argc, char *argv[]) */
     34 -#define ARGBEGIN	for (argv0 = *argv, argv++, argc--;\
     35 -					argv[0] && argv[0][0] == '-'\
     36 -					&& argv[0][1];\
     37 -					argc--, argv++) {\
     38 -				char argc_;\
     39 -				char **argv_;\
     40 -				int brk_;\
     41 -				if (argv[0][1] == '-' && argv[0][2] == '\0') {\
     42 -					argv++;\
     43 -					argc--;\
     44 -					break;\
     45 -				}\
     46 -				int i_;\
     47 -				for (i_ = 1, brk_ = 0, argv_ = argv;\
     48 -						argv[0][i_] && !brk_;\
     49 -						i_++) {\
     50 -					if (argv_ != argv)\
     51 -						break;\
     52 -					argc_ = argv[0][i_];\
     53 -					switch (argc_)
     54 -
     55 -#define ARGEND			}\
     56 -			}
     57 -
     58 -#define ARGC()		argc_
     59 -
     60 -#define EARGF(x)	((argv[0][i_+1] == '\0' && argv[1] == NULL)?\
     61 -				((x), abort(), (char *)0) :\
     62 -				(brk_ = 1, (argv[0][i_+1] != '\0')?\
     63 -					(&argv[0][i_+1]) :\
     64 -					(argc--, argv++, argv[0])))
     65 -
     66 -#define ARGF()		((argv[0][i_+1] == '\0' && argv[1] == NULL)?\
     67 -				(char *)0 :\
     68 -				(brk_ = 1, (argv[0][i_+1] != '\0')?\
     69 -					(&argv[0][i_+1]) :\
     70 -					(argc--, argv++, argv[0])))
     71 -
     72 -#endif
     73 diff --git a/st.h b/st.h
     74 deleted file mode 100644
     75 index 519b9bd..0000000
     76 --- a/st.h
     77 +++ /dev/null
     78 @@ -1,128 +0,0 @@
     79 -/* See LICENSE for license details. */
     80 -
     81 -#include <stdint.h>
     82 -#include <sys/types.h>
     83 -
     84 -/* macros */
     85 -#define MIN(a, b)		((a) < (b) ? (a) : (b))
     86 -#define MAX(a, b)		((a) < (b) ? (b) : (a))
     87 -#define LEN(a)			(sizeof(a) / sizeof(a)[0])
     88 -#define BETWEEN(x, a, b)	((a) <= (x) && (x) <= (b))
     89 -#define DIVCEIL(n, d)		(((n) + ((d) - 1)) / (d))
     90 -#define DEFAULT(a, b)		(a) = (a) ? (a) : (b)
     91 -#define LIMIT(x, a, b)		(x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
     92 -#define ATTRCMP(a, b)		((a).mode != (b).mode || (a).fg != (b).fg || \
     93 -				(a).bg != (b).bg)
     94 -#define TIMEDIFF(t1, t2)	((t1.tv_sec-t2.tv_sec)*1000 + \
     95 -				(t1.tv_nsec-t2.tv_nsec)/1E6)
     96 -#define MODBIT(x, set, bit)	((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
     97 -
     98 -#define TRUECOLOR(r,g,b)	(1 << 24 | (r) << 16 | (g) << 8 | (b))
     99 -#define IS_TRUECOL(x)		(1 << 24 & (x))
    100 -
    101 -enum glyph_attribute {
    102 -	ATTR_NULL       = 0,
    103 -	ATTR_BOLD       = 1 << 0,
    104 -	ATTR_FAINT      = 1 << 1,
    105 -	ATTR_ITALIC     = 1 << 2,
    106 -	ATTR_UNDERLINE  = 1 << 3,
    107 -	ATTR_BLINK      = 1 << 4,
    108 -	ATTR_REVERSE    = 1 << 5,
    109 -	ATTR_INVISIBLE  = 1 << 6,
    110 -	ATTR_STRUCK     = 1 << 7,
    111 -	ATTR_WRAP       = 1 << 8,
    112 -	ATTR_WIDE       = 1 << 9,
    113 -	ATTR_WDUMMY     = 1 << 10,
    114 -	ATTR_BOLD_FAINT = ATTR_BOLD | ATTR_FAINT,
    115 -};
    116 -
    117 -enum selection_mode {
    118 -	SEL_IDLE = 0,
    119 -	SEL_EMPTY = 1,
    120 -	SEL_READY = 2
    121 -};
    122 -
    123 -enum selection_type {
    124 -	SEL_REGULAR = 1,
    125 -	SEL_RECTANGULAR = 2
    126 -};
    127 -
    128 -enum selection_snap {
    129 -	SNAP_WORD = 1,
    130 -	SNAP_LINE = 2
    131 -};
    132 -
    133 -typedef unsigned char uchar;
    134 -typedef unsigned int uint;
    135 -typedef unsigned long ulong;
    136 -typedef unsigned short ushort;
    137 -
    138 -typedef uint_least32_t Rune;
    139 -
    140 -#define Glyph Glyph_
    141 -typedef struct {
    142 -	Rune u;           /* character code */
    143 -	ushort mode;      /* attribute flags */
    144 -	uint32_t fg;      /* foreground  */
    145 -	uint32_t bg;      /* background  */
    146 -} Glyph;
    147 -
    148 -typedef Glyph *Line;
    149 -
    150 -typedef union {
    151 -	int i;
    152 -	uint ui;
    153 -	float f;
    154 -	const void *v;
    155 -	const char *s;
    156 -} Arg;
    157 -
    158 -void die(const char *, ...);
    159 -void redraw(void);
    160 -void draw(void);
    161 -
    162 -void printscreen(const Arg *);
    163 -void printsel(const Arg *);
    164 -void sendbreak(const Arg *);
    165 -void toggleprinter(const Arg *);
    166 -
    167 -int tattrset(int);
    168 -void tnew(int, int);
    169 -void tresize(int, int);
    170 -void tsetdirtattr(int);
    171 -void ttyhangup(void);
    172 -int ttynew(const char *, char *, const char *, char **);
    173 -size_t ttyread(void);
    174 -void ttyresize(int, int);
    175 -void ttywrite(const char *, size_t, int);
    176 -
    177 -void resettitle(void);
    178 -
    179 -void selclear(void);
    180 -void selinit(void);
    181 -void selstart(int, int, int);
    182 -void selextend(int, int, int, int);
    183 -int selected(int, int);
    184 -char *getsel(void);
    185 -
    186 -size_t utf8encode(Rune, char *);
    187 -
    188 -void *xmalloc(size_t);
    189 -void *xrealloc(void *, size_t);
    190 -char *xstrdup(const char *);
    191 -
    192 -int xgetcolor(int x, unsigned char *r, unsigned char *g, unsigned char *b);
    193 -
    194 -/* config.h globals */
    195 -extern char *utmp;
    196 -extern char *scroll;
    197 -extern char *stty_args;
    198 -extern char *vtiden;
    199 -extern wchar_t *worddelimiters;
    200 -extern int allowaltscreen;
    201 -extern int allowwindowops;
    202 -extern char *termname;
    203 -extern unsigned int tabspaces;
    204 -extern unsigned int defaultfg;
    205 -extern unsigned int defaultbg;
    206 -extern unsigned int defaultcs;
    207 diff --git a/win.h b/win.h
    208 deleted file mode 100644
    209 index e6e4369..0000000
    210 --- a/win.h
    211 +++ /dev/null
    212 @@ -1,40 +0,0 @@
    213 -/* See LICENSE for license details. */
    214 -
    215 -enum win_mode {
    216 -	MODE_VISIBLE     = 1 << 0,
    217 -	MODE_FOCUSED     = 1 << 1,
    218 -	MODE_APPKEYPAD   = 1 << 2,
    219 -	MODE_MOUSEBTN    = 1 << 3,
    220 -	MODE_MOUSEMOTION = 1 << 4,
    221 -	MODE_REVERSE     = 1 << 5,
    222 -	MODE_KBDLOCK     = 1 << 6,
    223 -	MODE_HIDE        = 1 << 7,
    224 -	MODE_APPCURSOR   = 1 << 8,
    225 -	MODE_MOUSESGR    = 1 << 9,
    226 -	MODE_8BIT        = 1 << 10,
    227 -	MODE_BLINK       = 1 << 11,
    228 -	MODE_FBLINK      = 1 << 12,
    229 -	MODE_FOCUS       = 1 << 13,
    230 -	MODE_MOUSEX10    = 1 << 14,
    231 -	MODE_MOUSEMANY   = 1 << 15,
    232 -	MODE_BRCKTPASTE  = 1 << 16,
    233 -	MODE_NUMLOCK     = 1 << 17,
    234 -	MODE_MOUSE       = MODE_MOUSEBTN|MODE_MOUSEMOTION|MODE_MOUSEX10\
    235 -	                  |MODE_MOUSEMANY,
    236 -};
    237 -
    238 -void xbell(void);
    239 -void xclipcopy(void);
    240 -void xdrawcursor(int, int, Glyph, int, int, Glyph);
    241 -void xdrawline(Line, int, int, int);
    242 -void xfinishdraw(void);
    243 -void xloadcols(void);
    244 -int xsetcolorname(int, const char *);
    245 -void xseticontitle(char *);
    246 -void xsettitle(char *);
    247 -int xsetcursor(int);
    248 -void xsetmode(int, unsigned int);
    249 -void xsetpointermotion(int);
    250 -void xsetsel(char *);
    251 -int xstartdraw(void);
    252 -void xximspot(int, int);
    253 diff --git a/x.c b/x.c
    254 index cd96575..2771033 100644
    255 --- a/x.c
    256 +++ b/x.c
    257 @@ -14,6 +14,7 @@
    258  #include <X11/keysym.h>
    259  #include <X11/Xft/Xft.h>
    260  #include <X11/XKBlib.h>
    261 +#include <X11/Xresource.h>
    262  
    263  char *argv0;
    264  #include "arg.h"
    265 @@ -2011,6 +2012,129 @@ run(void)
    266  	}
    267  }
    268  
    269 +#define XRESOURCE_LOAD_META(NAME)					\
    270 +	if(!XrmGetResource(xrdb, "st." NAME, "st." NAME, &type, &ret))	\
    271 +		XrmGetResource(xrdb, "*." NAME, "*." NAME, &type, &ret); \
    272 +	if (ret.addr != NULL && !strncmp("String", type, 64))
    273 +
    274 +#define XRESOURCE_LOAD_STRING(NAME, DST)	\
    275 +	XRESOURCE_LOAD_META(NAME)		\
    276 +		DST = ret.addr;
    277 +
    278 +#define XRESOURCE_LOAD_CHAR(NAME, DST)		\
    279 +	XRESOURCE_LOAD_META(NAME)		\
    280 +		DST = ret.addr[0];
    281 +
    282 +#define XRESOURCE_LOAD_INTEGER(NAME, DST)		\
    283 +	XRESOURCE_LOAD_META(NAME)			\
    284 +		DST = strtoul(ret.addr, NULL, 10);
    285 +
    286 +#define XRESOURCE_LOAD_FLOAT(NAME, DST)		\
    287 +	XRESOURCE_LOAD_META(NAME)		\
    288 +		DST = strtof(ret.addr, NULL);
    289 +
    290 +void
    291 +xrdb_load(void)
    292 +{
    293 +	/* XXX */
    294 +	char *xrm;
    295 +	char *type;
    296 +	XrmDatabase xrdb;
    297 +	XrmValue ret;
    298 +	Display *dpy;
    299 +
    300 +	if(!(dpy = XOpenDisplay(NULL)))
    301 +		die("Can't open display\n");
    302 +
    303 +	XrmInitialize();
    304 +	xrm = XResourceManagerString(dpy);
    305 +
    306 +	if (xrm != NULL) {
    307 +		xrdb = XrmGetStringDatabase(xrm);
    308 +
    309 +		/* handling colors here without macros to do via loop. */
    310 +		int i = 0;
    311 +		char loadValue[12] = "";
    312 +		for (i = 0; i < 256; i++)
    313 +		{
    314 +			sprintf(loadValue, "%s%d", "st.color", i);
    315 +
    316 +			if(!XrmGetResource(xrdb, loadValue, loadValue, &type, &ret))
    317 +			{
    318 +				sprintf(loadValue, "%s%d", "*.color", i);
    319 +				if (!XrmGetResource(xrdb, loadValue, loadValue, &type, &ret))
    320 +					/* reset if not found (unless in range for defaults). */
    321 +					if (i > 15)
    322 +						colorname[i] = NULL;
    323 +			}
    324 +
    325 +			if (ret.addr != NULL && !strncmp("String", type, 64))
    326 +				colorname[i] = ret.addr;
    327 +		}
    328 +
    329 +		XRESOURCE_LOAD_STRING("foreground", colorname[defaultfg]);
    330 +		XRESOURCE_LOAD_STRING("background", colorname[defaultbg]);
    331 +		XRESOURCE_LOAD_STRING("cursorfg", colorname[defaultcs])
    332 +		else {
    333 +		  // this looks confusing because we are chaining off of the if
    334 +		  // in the macro. probably we should be wrapping everything blocks
    335 +		  // so this isn't possible...
    336 +		  defaultcs = defaultfg;
    337 +		}
    338 +		XRESOURCE_LOAD_STRING("reverse-cursor", colorname[defaultrcs])
    339 +		else {
    340 +		  // see above.
    341 +		  defaultrcs = defaultbg;
    342 +		}
    343 +
    344 +		XRESOURCE_LOAD_STRING("font", font);
    345 +		XRESOURCE_LOAD_STRING("termname", termname);
    346 +
    347 +		/* XRESOURCE_LOAD_INTEGER("xfps", xfps); */
    348 +		/* XRESOURCE_LOAD_INTEGER("actionfps", actionfps); */
    349 +		XRESOURCE_LOAD_INTEGER("blinktimeout", blinktimeout);
    350 +		XRESOURCE_LOAD_INTEGER("bellvolume", bellvolume);
    351 +		XRESOURCE_LOAD_INTEGER("borderpx", borderpx);
    352 +		/* XRESOURCE_LOAD_INTEGER("borderless", borderless); */
    353 +		XRESOURCE_LOAD_INTEGER("cursorshape", cursorshape);
    354 +
    355 +		/* cursorblinkstate = 1; // in case if cursor shape was changed from a blinking one to a non-blinking */
    356 +		/* XRESOURCE_LOAD_INTEGER("cursorthickness", cursorthickness); */
    357 +		/* XRESOURCE_LOAD_INTEGER("cursorblinkstyle", cursorblinkstyle); */
    358 +		/* XRESOURCE_LOAD_INTEGER("cursorblinkontype", cursorblinkontype); */
    359 +
    360 +		/* todo: https://github.com/gnotclub/xst/commit/1e82647b0e04077e975679a4b4cf1eb02b04e6bc */
    361 +		/* XRESOURCE_LOAD_INTEGER("mouseScrollLines", mousescrolllines); */
    362 +
    363 +		XRESOURCE_LOAD_FLOAT("cwscale", cwscale);
    364 +		XRESOURCE_LOAD_FLOAT("chscale", chscale);
    365 +
    366 +		/* XRESOURCE_LOAD_CHAR("prompt_char", prompt_char); */
    367 +	}
    368 +	XFlush(dpy);
    369 +}
    370 +
    371 +void
    372 +reload(int sig)
    373 +{
    374 +	xrdb_load();
    375 +
    376 +	/* colors, fonts */
    377 +	xloadcols();
    378 +	xunloadfonts();
    379 +	xloadfonts(font, 0);
    380 +
    381 +	/* pretend the window just got resized */
    382 +	cresize(win.w, win.h);
    383 +
    384 +	redraw();
    385 +
    386 +	/* triggers re-render if we're visible. */
    387 +	ttywrite("\033[O", 3, 1);
    388 +
    389 +	signal(SIGUSR1, reload);
    390 +}
    391 +
    392  void
    393  usage(void)
    394  {
    395 @@ -2084,6 +2208,8 @@ run:
    396  
    397  	setlocale(LC_CTYPE, "");
    398  	XSetLocaleModifiers("");
    399 +	xrdb_load();
    400 +	signal(SIGUSR1, reload);
    401  	cols = MAX(cols, 1);
    402  	rows = MAX(rows, 1);
    403  	tnew(cols, rows);
    404 -- 
    405 2.35.1
    406