dwm-xresources-20210314.diff (8218B)
1 From d28a26439326d7566a43459e1ef00b5b7c7f5b11 Mon Sep 17 00:00:00 2001 2 From: David JULIEN <swy7ch@protonmail.com> 3 Date: Sun, 14 Mar 2021 18:19:17 +0100 4 Subject: [PATCH] [PATCH] feat: manage font through xrdb 5 6 add font management to the dwm-resources patch, enabling to manage fonts 7 by sourcing $XRESOURCES 8 --- 9 config.def.h | 61 ++++++++++++++++++++++++++++++-------------- 10 drw.c | 2 +- 11 drw.h | 2 +- 12 dwm.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 13 4 files changed, 116 insertions(+), 21 deletions(-) 14 15 diff --git a/config.def.h b/config.def.h 16 index 1c0b587..db7b7bb 100644 17 --- a/config.def.h 18 +++ b/config.def.h 19 @@ -1,21 +1,23 @@ 20 /* See LICENSE file for copyright and license details. */ 21 22 /* appearance */ 23 -static const unsigned int borderpx = 1; /* border pixel of windows */ 24 -static const unsigned int snap = 32; /* snap pixel */ 25 -static const int showbar = 1; /* 0 means no bar */ 26 -static const int topbar = 1; /* 0 means bottom bar */ 27 -static const char *fonts[] = { "monospace:size=10" }; 28 -static const char dmenufont[] = "monospace:size=10"; 29 -static const char col_gray1[] = "#222222"; 30 -static const char col_gray2[] = "#444444"; 31 -static const char col_gray3[] = "#bbbbbb"; 32 -static const char col_gray4[] = "#eeeeee"; 33 -static const char col_cyan[] = "#005577"; 34 -static const char *colors[][3] = { 35 - /* fg bg border */ 36 - [SchemeNorm] = { col_gray3, col_gray1, col_gray2 }, 37 - [SchemeSel] = { col_gray4, col_cyan, col_cyan }, 38 +static unsigned int borderpx = 1; /* border pixel of windows */ 39 +static unsigned int snap = 32; /* snap pixel */ 40 +static int showbar = 1; /* 0 means no bar */ 41 +static int topbar = 1; /* 0 means bottom bar */ 42 +static char font[] = "monospace:size=10"; 43 +static char dmenufont[] = "monospace:size=10"; 44 +static const char *fonts[] = { font }; 45 +static char normbgcolor[] = "#222222"; 46 +static char normbordercolor[] = "#444444"; 47 +static char normfgcolor[] = "#bbbbbb"; 48 +static char selfgcolor[] = "#eeeeee"; 49 +static char selbordercolor[] = "#005577"; 50 +static char selbgcolor[] = "#005577"; 51 +static char *colors[][3] = { 52 + /* fg bg border */ 53 + [SchemeNorm] = { normfgcolor, normbgcolor, normbordercolor }, 54 + [SchemeSel] = { selfgcolor, selbgcolor, selbordercolor }, 55 }; 56 57 /* tagging */ 58 @@ -32,9 +34,9 @@ static const Rule rules[] = { 59 }; 60 61 /* layout(s) */ 62 -static const float mfact = 0.55; /* factor of master area size [0.05..0.95] */ 63 -static const int nmaster = 1; /* number of clients in master area */ 64 -static const int resizehints = 1; /* 1 means respect size hints in tiled resizals */ 65 +static float mfact = 0.55; /* factor of master area size [0.05..0.95] */ 66 +static int nmaster = 1; /* number of clients in master area */ 67 +static int resizehints = 1; /* 1 means respect size hints in tiled resizals */ 68 69 static const Layout layouts[] = { 70 /* symbol arrange function */ 71 @@ -56,9 +58,30 @@ static const Layout layouts[] = { 72 73 /* commands */ 74 static char dmenumon[2] = "0"; /* component of dmenucmd, manipulated in spawn() */ 75 -static const char *dmenucmd[] = { "dmenu_run", "-m", dmenumon, "-fn", dmenufont, "-nb", col_gray1, "-nf", col_gray3, "-sb", col_cyan, "-sf", col_gray4, NULL }; 76 +static const char *dmenucmd[] = { "dmenu_run", "-m", dmenumon, "-fn", dmenufont, "-nb", normbgcolor, "-nf", normfgcolor, "-sb", selbordercolor, "-sf", selfgcolor, NULL }; 77 static const char *termcmd[] = { "st", NULL }; 78 79 +/* 80 + * Xresources preferences to load at startup 81 + */ 82 +ResourcePref resources[] = { 83 + { "font", STRING, &font }, 84 + { "dmenufont", STRING, &dmenufont }, 85 + { "normbgcolor", STRING, &normbgcolor }, 86 + { "normbordercolor", STRING, &normbordercolor }, 87 + { "normfgcolor", STRING, &normfgcolor }, 88 + { "selbgcolor", STRING, &selbgcolor }, 89 + { "selbordercolor", STRING, &selbordercolor }, 90 + { "selfgcolor", STRING, &selfgcolor }, 91 + { "borderpx", INTEGER, &borderpx }, 92 + { "snap", INTEGER, &snap }, 93 + { "showbar", INTEGER, &showbar }, 94 + { "topbar", INTEGER, &topbar }, 95 + { "nmaster", INTEGER, &nmaster }, 96 + { "resizehints", INTEGER, &resizehints }, 97 + { "mfact", FLOAT, &mfact }, 98 +}; 99 + 100 static Key keys[] = { 101 /* modifier key function argument */ 102 { MODKEY, XK_p, spawn, {.v = dmenucmd } }, 103 diff --git a/drw.c b/drw.c 104 index 4cdbcbe..8f1059e 100644 105 --- a/drw.c 106 +++ b/drw.c 107 @@ -208,7 +208,7 @@ drw_clr_create(Drw *drw, Clr *dest, const char *clrname) 108 /* Wrapper to create color schemes. The caller has to call free(3) on the 109 * returned color scheme when done using it. */ 110 Clr * 111 -drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount) 112 +drw_scm_create(Drw *drw, char *clrnames[], size_t clrcount) 113 { 114 size_t i; 115 Clr *ret; 116 diff --git a/drw.h b/drw.h 117 index 4bcd5ad..42b04ce 100644 118 --- a/drw.h 119 +++ b/drw.h 120 @@ -39,7 +39,7 @@ void drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned in 121 122 /* Colorscheme abstraction */ 123 void drw_clr_create(Drw *drw, Clr *dest, const char *clrname); 124 -Clr *drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount); 125 +Clr *drw_scm_create(Drw *drw, char *clrnames[], size_t clrcount); 126 127 /* Cursor abstraction */ 128 Cur *drw_cur_create(Drw *drw, int shape); 129 diff --git a/dwm.c b/dwm.c 130 index 664c527..17bb21e 100644 131 --- a/dwm.c 132 +++ b/dwm.c 133 @@ -36,6 +36,7 @@ 134 #include <X11/Xlib.h> 135 #include <X11/Xproto.h> 136 #include <X11/Xutil.h> 137 +#include <X11/Xresource.h> 138 #ifdef XINERAMA 139 #include <X11/extensions/Xinerama.h> 140 #endif /* XINERAMA */ 141 @@ -141,6 +142,19 @@ typedef struct { 142 int monitor; 143 } Rule; 144 145 +/* Xresources preferences */ 146 +enum resource_type { 147 + STRING = 0, 148 + INTEGER = 1, 149 + FLOAT = 2 150 +}; 151 + 152 +typedef struct { 153 + char *name; 154 + enum resource_type type; 155 + void *dst; 156 +} ResourcePref; 157 + 158 /* function declarations */ 159 static void applyrules(Client *c); 160 static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact); 161 @@ -234,6 +248,8 @@ static int xerror(Display *dpy, XErrorEvent *ee); 162 static int xerrordummy(Display *dpy, XErrorEvent *ee); 163 static int xerrorstart(Display *dpy, XErrorEvent *ee); 164 static void zoom(const Arg *arg); 165 +static void load_xresources(void); 166 +static void resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst); 167 168 /* variables */ 169 static const char broken[] = "broken"; 170 @@ -2127,6 +2143,60 @@ zoom(const Arg *arg) 171 pop(c); 172 } 173 174 +void 175 +resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst) 176 +{ 177 + char *sdst = NULL; 178 + int *idst = NULL; 179 + float *fdst = NULL; 180 + 181 + sdst = dst; 182 + idst = dst; 183 + fdst = dst; 184 + 185 + char fullname[256]; 186 + char *type; 187 + XrmValue ret; 188 + 189 + snprintf(fullname, sizeof(fullname), "%s.%s", "dwm", name); 190 + fullname[sizeof(fullname) - 1] = '\0'; 191 + 192 + XrmGetResource(db, fullname, "*", &type, &ret); 193 + if (!(ret.addr == NULL || strncmp("String", type, 64))) 194 + { 195 + switch (rtype) { 196 + case STRING: 197 + strcpy(sdst, ret.addr); 198 + break; 199 + case INTEGER: 200 + *idst = strtoul(ret.addr, NULL, 10); 201 + break; 202 + case FLOAT: 203 + *fdst = strtof(ret.addr, NULL); 204 + break; 205 + } 206 + } 207 +} 208 + 209 +void 210 +load_xresources(void) 211 +{ 212 + Display *display; 213 + char *resm; 214 + XrmDatabase db; 215 + ResourcePref *p; 216 + 217 + display = XOpenDisplay(NULL); 218 + resm = XResourceManagerString(display); 219 + if (!resm) 220 + return; 221 + 222 + db = XrmGetStringDatabase(resm); 223 + for (p = resources; p < resources + LENGTH(resources); p++) 224 + resource_load(db, p->name, p->type, p->dst); 225 + XCloseDisplay(display); 226 +} 227 + 228 int 229 main(int argc, char *argv[]) 230 { 231 @@ -2139,6 +2209,8 @@ main(int argc, char *argv[]) 232 if (!(dpy = XOpenDisplay(NULL))) 233 die("dwm: cannot open display"); 234 checkotherwm(); 235 + XrmInitialize(); 236 + load_xresources(); 237 setup(); 238 #ifdef __OpenBSD__ 239 if (pledge("stdio rpath proc exec", NULL) == -1) 240 -- 241 2.30.2 242