sites

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

dmenu-xresources-20260510-7175c48.diff (3845B)


      1 From e0da9966255a9499085555ab43b5277730cf2e02 Mon Sep 17 00:00:00 2001
      2 From: Justinas Grigas <dev@jstnas.com>
      3 Date: Sun, 10 May 2026 15:25:31 +0100
      4 Subject: [PATCH] xresources: runtime X Resources loading
      5 
      6 ---
      7  config.def.h | 20 +++++++++++++++++-
      8  dmenu.c      | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++
      9  2 files changed, 78 insertions(+), 1 deletion(-)
     10 
     11 diff --git a/config.def.h b/config.def.h
     12 index 1edb647..30daf5a 100644
     13 --- a/config.def.h
     14 +++ b/config.def.h
     15 @@ -6,7 +6,8 @@ static int topbar = 1;                      /* -b  option; if 0, dmenu appears a
     16  static const char *fonts[] = {
     17  	"monospace:size=10"
     18  };
     19 -static const char *prompt      = NULL;      /* -p  option; prompt to the left of input field */
     20 +/* -p  option; prompt to the left of input field */
     21 +static char *prompt = NULL;
     22  static const char *colors[SchemeLast][2] = {
     23  	/*     fg         bg       */
     24  	[SchemeNorm] = { "#bbbbbb", "#222222" },
     25 @@ -21,3 +22,20 @@ static unsigned int lines      = 0;
     26   * for example: " /?\"&[]"
     27   */
     28  static const char worddelimiters[] = " ";
     29 +
     30 +/*
     31 + * Xresources preferences to load at startup
     32 + */
     33 +static const XResPref resources[] = {
     34 +	/* name                  type    address */
     35 +	{ "dmenu.font",          STRING, &fonts[0] },
     36 +	{ "dmenu.prompt",        STRING, &prompt },
     37 +	{ "dmenu.foreground",    STRING, &colors[SchemeNorm][ColFg] },
     38 +	{ "dmenu.background",    STRING, &colors[SchemeNorm][ColBg] },
     39 +	{ "dmenu.foregroundSel", STRING, &colors[SchemeSel][ColFg] },
     40 +	{ "dmenu.backgroundSel", STRING, &colors[SchemeSel][ColBg] },
     41 +	{ "dmenu.foregroundOut", STRING, &colors[SchemeOut][ColFg] },
     42 +	{ "dmenu.backgroundOut", STRING, &colors[SchemeOut][ColBg] },
     43 +	{ "dmenu.topbar",        INTEGER, &topbar },
     44 +	{ "dmenu.lines",         INTEGER, &lines },
     45 +};
     46 diff --git a/dmenu.c b/dmenu.c
     47 index 363d19f..0ca41bc 100644
     48 --- a/dmenu.c
     49 +++ b/dmenu.c
     50 @@ -11,6 +11,7 @@
     51  #include <X11/Xlib.h>
     52  #include <X11/Xatom.h>
     53  #include <X11/Xutil.h>
     54 +#include <X11/Xresource.h>
     55  #ifdef XINERAMA
     56  #include <X11/extensions/Xinerama.h>
     57  #endif
     58 @@ -52,6 +53,15 @@ static XIC xic;
     59  static Drw *drw;
     60  static Clr *scheme[SchemeLast];
     61  
     62 +/* Xresources preferences */
     63 +static XrmDatabase xrdb;
     64 +enum XResType { STRING, INTEGER, FLOAT };
     65 +typedef struct {
     66 +	const char *name;
     67 +	enum XResType type;
     68 +	void *dst;
     69 +} XResPref;
     70 +
     71  #include "config.h"
     72  
     73  static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
     74 @@ -107,6 +117,7 @@ cleanup(void)
     75  		free(items[i].text);
     76  	free(items);
     77  	drw_free(drw);
     78 +	XrmDestroyDatabase(xrdb);
     79  	XSync(dpy, False);
     80  	XCloseDisplay(dpy);
     81  }
     82 @@ -718,12 +729,60 @@ usage(void)
     83  	    "             [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]");
     84  }
     85  
     86 +void
     87 +xresload(const XResPref *resource)
     88 +{
     89 +	char *type;
     90 +	XrmValue ret;
     91 +
     92 +	if (!XrmGetResource(xrdb, resource->name, NULL, &type, &ret))
     93 +		return;
     94 +	if (!ret.addr || strncmp(type, "String", sizeof("String")))
     95 +		return;
     96 +
     97 +	switch (resource->type) {
     98 +	case STRING:
     99 +		*(char **)resource->dst = ret.addr;
    100 +		break;
    101 +	case INTEGER:
    102 +		*(int *)resource->dst = strtoul(ret.addr, NULL, 10);
    103 +		break;
    104 +	case FLOAT:
    105 +		*(float *)resource->dst = strtof(ret.addr, NULL);
    106 +		break;
    107 +	}
    108 +}
    109 +
    110 +void
    111 +xresupdate(void)
    112 +{
    113 +	Display *display;
    114 +	char *resm;
    115 +	const XResPref *p;
    116 +
    117 +	display = XOpenDisplay(NULL);
    118 +	if (!display)
    119 +		return;
    120 +	resm = XResourceManagerString(display);
    121 +	if (resm) {
    122 +		xrdb = XrmGetStringDatabase(resm);
    123 +		if (xrdb) {
    124 +			for (p = resources; p < resources + LENGTH(resources); ++p)
    125 +				xresload(p);
    126 +		}
    127 +	}
    128 +	XCloseDisplay(display);
    129 +}
    130 +
    131  int
    132  main(int argc, char *argv[])
    133  {
    134  	XWindowAttributes wa;
    135  	int i, fast = 0;
    136  
    137 +	XrmInitialize();
    138 +	xresupdate();
    139 +
    140  	for (i = 1; i < argc; i++)
    141  		/* these options take no arguments */
    142  		if (!strcmp(argv[i], "-v")) {      /* prints version information */
    143 -- 
    144 2.53.0
    145