dwm-xresources.diff 8.0 KB

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