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