sites

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

dwm-xresources-20260524-44dbc68.diff (8580B)


      1 From 18fee48e6c25a6f05af186af0858072cacec2259 Mon Sep 17 00:00:00 2001
      2 From: Justinas Grigas <dev@jstnas.com>
      3 Date: Thu, 7 May 2026 18:29:50 +0100
      4 Subject: [PATCH] xresources: runtime configuration using X resources
      5 
      6 ---
      7  config.def.h | 56 ++++++++++++++++++++++++-------------
      8  dwm.c        | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++
      9  2 files changed, 115 insertions(+), 19 deletions(-)
     10 
     11 diff --git a/config.def.h b/config.def.h
     12 index 81c3fc0..3208f10 100644
     13 --- a/config.def.h
     14 +++ b/config.def.h
     15 @@ -1,21 +1,15 @@
     16  /* See LICENSE file for copyright and license details. */
     17  
     18  /* appearance */
     19 -static const unsigned int borderpx  = 1;        /* border pixel of windows */
     20 -static const unsigned int snap      = 32;       /* snap pixel */
     21 -static const int showbar            = 1;        /* 0 means no bar */
     22 -static const int topbar             = 1;        /* 0 means bottom bar */
     23 -static const char *fonts[]          = { "monospace:size=10" };
     24 -static const char dmenufont[]       = "monospace:size=10";
     25 -static const char col_gray1[]       = "#222222";
     26 -static const char col_gray2[]       = "#444444";
     27 -static const char col_gray3[]       = "#bbbbbb";
     28 -static const char col_gray4[]       = "#eeeeee";
     29 -static const char col_cyan[]        = "#005577";
     30 -static const char *colors[][3]      = {
     31 -	/*               fg         bg         border   */
     32 -	[SchemeNorm] = { col_gray3, col_gray1, col_gray2 },
     33 -	[SchemeSel]  = { col_gray4, col_cyan,  col_cyan  },
     34 +static unsigned int borderpx   = 1;        /* border pixel of windows */
     35 +static unsigned int snap       = 32;       /* snap pixel */
     36 +static int showbar             = 1;        /* 0 means no bar */
     37 +static int topbar              = 1;        /* 0 means bottom bar */
     38 +static const char *fonts[]     = { "monospace:size=10" };
     39 +static const char *colors[][3] = {
     40 +       /* scheme        fg         bg         border   */
     41 +       [SchemeNorm] = { "#bbbbbb", "#222222", "#444444" },
     42 +       [SchemeSel]  = { "#eeeeee", "#005577", "#005577" },
     43  };
     44  
     45  /* tagging */
     46 @@ -32,9 +26,9 @@ static const Rule rules[] = {
     47  };
     48  
     49  /* layout(s) */
     50 -static const float mfact     = 0.55; /* factor of master area size [0.05..0.95] */
     51 -static const int nmaster     = 1;    /* number of clients in master area */
     52 -static const int resizehints = 1;    /* 1 means respect size hints in tiled resizals */
     53 +static float mfact = 0.55;           /* factor of master area size [0.05..0.95] */
     54 +static int nmaster = 1;              /* number of clients in master area */
     55 +static int resizehints = 1;          /* 1 means respect size hints in tiled resizals */
     56  static const int lockfullscreen = 1; /* 1 will force focus on the fullscreen window */
     57  static const int refreshrate = 120;  /* refresh rate (per second) for client move/resize */
     58  
     59 @@ -58,7 +52,7 @@ static const Layout layouts[] = {
     60  
     61  /* commands */
     62  static char dmenumon[2] = "0"; /* component of dmenucmd, manipulated in spawn() */
     63 -static const char *dmenucmd[] = { "dmenu_run", "-m", dmenumon, "-fn", dmenufont, "-nb", col_gray1, "-nf", col_gray3, "-sb", col_cyan, "-sf", col_gray4, NULL };
     64 +static char *dmenucmd[] = { "dmenu_run", "-m", dmenumon, "-fn", "monospace:size=10", "-nb", "#222222", "-nf", "#bbbbbb", "-sb", "#005577", "-sf", "#eeeeee", NULL };
     65  static const char *termcmd[]  = { "st", NULL };
     66  
     67  static const Key keys[] = {
     68 @@ -86,6 +80,7 @@ static const Key keys[] = {
     69  	{ MODKEY,                       XK_period, focusmon,       {.i = +1 } },
     70  	{ MODKEY|ShiftMask,             XK_comma,  tagmon,         {.i = -1 } },
     71  	{ MODKEY|ShiftMask,             XK_period, tagmon,         {.i = +1 } },
     72 +	{ MODKEY,                       XK_F5,     xresreload,     {0} },
     73  	TAGKEYS(                        XK_1,                      0)
     74  	TAGKEYS(                        XK_2,                      1)
     75  	TAGKEYS(                        XK_3,                      2)
     76 @@ -115,3 +110,26 @@ static const Button buttons[] = {
     77  	{ ClkTagBar,            MODKEY,         Button3,        toggletag,      {0} },
     78  };
     79  
     80 +/* X resources to update */
     81 +static const XResPref resources[] = {
     82 +	/* name                type     address */
     83 +	{ "dwm.font",          STRING,  &fonts[0] },
     84 +	{ "dwm.dmenufont",     STRING,  &dmenucmd[4] },
     85 +	{ "dwm.background",    STRING,  &dmenucmd[6] },
     86 +	{ "dwm.foreground",    STRING,  &dmenucmd[8] },
     87 +	{ "dwm.backgroundSel", STRING,  &dmenucmd[10] },
     88 +	{ "dwm.foregroundSel", STRING,  &dmenucmd[12] },
     89 +	{ "dwm.foreground",    STRING,  &colors[SchemeNorm][ColFg] },
     90 +	{ "dwm.background",    STRING,  &colors[SchemeNorm][ColBg] },
     91 +	{ "dwm.border",        STRING,  &colors[SchemeNorm][ColBorder] },
     92 +	{ "dwm.foregroundSel", STRING,  &colors[SchemeSel][ColFg] },
     93 +	{ "dwm.backgroundSel", STRING,  &colors[SchemeSel][ColBg] },
     94 +	{ "dwm.borderSel",     STRING,  &colors[SchemeSel][ColBorder] },
     95 +	{ "dwm.borderpx",      INTEGER, &borderpx },
     96 +	{ "dwm.snap",          INTEGER, &snap },
     97 +	{ "dwm.showbar",       INTEGER, &showbar },
     98 +	{ "dwm.topbar",        INTEGER, &topbar },
     99 +	{ "dwm.nmaster",       INTEGER, &nmaster },
    100 +	{ "dwm.resizehints",   INTEGER, &resizehints },
    101 +	{ "dwm.mfact",         FLOAT,   &mfact },
    102 +};
    103 diff --git a/dwm.c b/dwm.c
    104 index ab3a84c..b4c9174 100644
    105 --- a/dwm.c
    106 +++ b/dwm.c
    107 @@ -36,6 +36,7 @@
    108  #include <X11/Xlib.h>
    109  #include <X11/Xproto.h>
    110  #include <X11/Xutil.h>
    111 +#include <X11/Xresource.h>
    112  #ifdef XINERAMA
    113  #include <X11/extensions/Xinerama.h>
    114  #endif /* XINERAMA */
    115 @@ -65,6 +66,7 @@ enum { NetSupported, NetWMName, NetWMState, NetWMCheck,
    116  enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
    117  enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
    118         ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
    119 +enum XResType {STRING, INTEGER, FLOAT};
    120  
    121  typedef union {
    122  	int i;
    123 @@ -140,6 +142,12 @@ typedef struct {
    124  	int monitor;
    125  } Rule;
    126  
    127 +typedef struct {
    128 +	const char *name;
    129 +	enum XResType type;
    130 +	void *dst;
    131 +} XResPref;
    132 +
    133  /* function declarations */
    134  static void applyrules(Client *c);
    135  static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact);
    136 @@ -232,6 +240,9 @@ static int xerror(Display *dpy, XErrorEvent *ee);
    137  static int xerrordummy(Display *dpy, XErrorEvent *ee);
    138  static int xerrorstart(Display *dpy, XErrorEvent *ee);
    139  static void zoom(const Arg *arg);
    140 +static void xresload(const XResPref *resource);
    141 +static void xresupdate(void);
    142 +static void xresreload(const Arg *arg);
    143  
    144  /* variables */
    145  static const char broken[] = "broken";
    146 @@ -266,6 +277,7 @@ static Display *dpy;
    147  static Drw *drw;
    148  static Monitor *mons, *selmon;
    149  static Window root, wmcheckwin;
    150 +static XrmDatabase xrdb = NULL;
    151  
    152  /* configuration, allows nested code to access above variables */
    153  #include "config.h"
    154 @@ -490,6 +502,7 @@ cleanup(void)
    155  	free(scheme);
    156  	XDestroyWindow(dpy, wmcheckwin);
    157  	drw_free(drw);
    158 +	XrmDestroyDatabase(xrdb);
    159  	XSync(dpy, False);
    160  	XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
    161  	XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
    162 @@ -2140,6 +2153,69 @@ zoom(const Arg *arg)
    163  	pop(c);
    164  }
    165  
    166 +void
    167 +xresload(const XResPref *resource)
    168 +{
    169 +	char *type;
    170 +	XrmValue ret;
    171 +
    172 +	if (!XrmGetResource(xrdb, resource->name, NULL, &type, &ret))
    173 +		return;
    174 +	if (!ret.addr || strncmp(type, "String", sizeof("String")))
    175 +		return;
    176 +
    177 +	switch (resource->type) {
    178 +	case STRING:
    179 +		*(char **)resource->dst = ret.addr;
    180 +		break;
    181 +	case INTEGER:
    182 +		*(int *)resource->dst = strtoul(ret.addr, NULL, 10);
    183 +		break;
    184 +	case FLOAT:
    185 +		*(float *)resource->dst = strtof(ret.addr, NULL);
    186 +		break;
    187 +	}
    188 +}
    189 +
    190 +void
    191 +xresupdate(void)
    192 +{
    193 +	Display *display;
    194 +	char *resm;
    195 +	const XResPref *p;
    196 +
    197 +	display = XOpenDisplay(NULL);
    198 +	if (!display)
    199 +		return;
    200 +	resm = XResourceManagerString(display);
    201 +	if (resm) {
    202 +		if (xrdb)
    203 +			XrmDestroyDatabase(xrdb);
    204 +		xrdb = XrmGetStringDatabase(resm);
    205 +		if (xrdb) {
    206 +			for (p = resources; p < resources + LENGTH(resources); ++p)
    207 +				xresload(p);
    208 +		}
    209 +	}
    210 +	XCloseDisplay(display);
    211 +}
    212 +
    213 +void
    214 +xresreload(const Arg *arg)
    215 +{
    216 +	int i;
    217 +
    218 +	xresupdate();
    219 +	for (i = 0; i < LENGTH(colors); ++i) {
    220 +		drw_scm_free(drw, scheme[i], 3);
    221 +		scheme[i] = drw_scm_create(drw, colors[i], 3);
    222 +	}
    223 +	drw_fontset_free(drw->fonts);
    224 +	drw_fontset_create(drw, fonts, LENGTH(fonts));
    225 +	focus(NULL);
    226 +	arrange(NULL);
    227 +}
    228 +
    229  int
    230  main(int argc, char *argv[])
    231  {
    232 @@ -2152,6 +2228,8 @@ main(int argc, char *argv[])
    233  	if (!(dpy = XOpenDisplay(NULL)))
    234  		die("dwm: cannot open display");
    235  	checkotherwm();
    236 +	XrmInitialize();
    237 +	xresupdate();
    238  	setup();
    239  #ifdef __OpenBSD__
    240  	if (pledge("stdio rpath proc exec", NULL) == -1)
    241 -- 
    242 2.53.0
    243