commit 8c92e0ba2882e38fd655568822793a8afeac270e
parent 95562252ec3cf5848ad859e5849d5a7693ff4c3b
Author: tetrakist <tetrakist@mutandum.com>
Date: Mon, 8 Mar 2021 09:18:22 -0800
Add Xresource handling for fonts and colors.
Signed-off-by: Maarten van Gompel <proycon@anaproy.nl>
Diffstat:
| M | config.def.h | | | 4 | ++-- |
| M | dwm.1 | | | 43 | ++++++++++++++++++++++++++++++++++++++++++- |
| M | dwm.c | | | 67 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- |
3 files changed, 110 insertions(+), 4 deletions(-)
diff --git a/config.def.h b/config.def.h
@@ -5,7 +5,7 @@ static const unsigned int borderpx = 1; /* border pixel of windows */
static const unsigned int snap = 32; /* snap pixel */
static const int showbar = 1; /* 0 means no bar */
static const int topbar = 1; /* 0 means bottom bar */
-static const char *fonts[] = {
+static const char *defaultfonts[] = {
"monospace:size=15", /* PP default start index */
"monospace:size=10", /* PB default start index */
};
@@ -15,7 +15,7 @@ static const char col_gray2[] = "#444444";
static const char col_gray3[] = "#bbbbbb";
static const char col_gray4[] = "#eeeeee";
static const char col_cyan[] = "#005577";
-static const char *colors[][3] = {
+static const char *defaultcolors[][3] = {
/* fg bg border */
[SchemeNorm] = { col_gray3, col_gray1, col_gray2 },
[SchemeSel] = { col_gray4, col_cyan, col_cyan },
diff --git a/dwm.1 b/dwm.1
@@ -163,7 +163,48 @@ Toggles focused window between floating and tiled state.
Resize focused window while dragging. Tiled windows will be toggled to the floating state.
.SH CUSTOMIZATION
dwm is customized by creating a custom config.h and (re)compiling the source
-code. This keeps it fast, secure and simple.
+code, or by setting X resources.
+.P
+The following X resource settings can be set:
+.TP
+.BI dwm.font
+defines the font or font set used.
+.TP
+.BI dwm.background
+defines the normal background color.
+.IR #RGB ,
+.IR #RRGGBB ,
+and X color names are supported.
+.TP
+.BI dwm.border
+defines the normal border color
+.TP
+.BI dwm.selbackground
+defines the selected background color
+.TP
+.BI dwm.selforeground
+defines the selected foreground color
+.TP
+.BI dwm.selborder
+defines the selected border color
+.TP
+.BI dwm.tabactivebackground
+defines the background color for active tabs
+.TP
+.BI dwm.tabactiveforeground
+defines the foreground color for active tabs
+.TP
+.BI dwm.tabactiveborder
+defines the border color for active tabs
+.TP
+.BI dwm.tabinactivebackground
+defines the background color for inactive tabs
+.TP
+.BI dwm.tabinactiveforeground
+defines the foreground color for inactive tabs
+.TP
+.BI dwm.tabinactiveborder
+defines the border color for inactive tabs
.SH SEE ALSO
.BR dmenu (1),
.BR st (1)
diff --git a/dwm.c b/dwm.c
@@ -41,6 +41,7 @@
#include <X11/extensions/Xinerama.h>
#endif /* XINERAMA */
#include <X11/Xft/Xft.h>
+#include <X11/Xresource.h>
#include <X11/Xlib-xcb.h>
#include <xcb/res.h>
@@ -302,6 +303,10 @@ static Drw *drw;
static Monitor *mons, *selmon;
static Window root, wmcheckwin;
+/* Empty arrays to be filled from command line, Xresources, and defaults, in decreasing order of precedence */
+static char *colors[4][3]; /* 4 schemes, 3 colors each */
+static char *fonts[] = {0, 0};
+
#define KEYPRESS_MS_THRESHOLD 200
#define KEYHOLD_MS_THRESHOLD 700
static int multikeypendingindex = -1;
@@ -1899,7 +1904,7 @@ setmfact(const Arg *arg)
void
setup(void)
{
- int i;
+ int i,j;
XSetWindowAttributes wa;
Atom utf8string;
@@ -1913,6 +1918,20 @@ setup(void)
root = RootWindow(dpy, screen);
drw = drw_create(dpy, screen, root, sw, sh);
+ readxresources();
+ /* Apply defaults to font and colors*/
+ if ( !fonts[0] )
+ fonts[0] = strdup(defaultfonts[0]);
+ if ( !fonts[1] )
+ fonts[1] = strdup(defaultfonts[1]);
+ for (i = 0; i <= SchemeTabInactive; ++i){
+ for (j = 0; j <= ColBorder; ++j){
+ if ( !colors[i][j] )
+ colors[i][j] = strdup(defaultcolors[i][j]);
+ }
+ }
+
+
if (!drw_fontset_create(drw, fonts + (iswide() ? 1: 0), LENGTH(fonts) - (iswide() ? 1 : 0)))
die("no fonts could be loaded.");
lrpad = drw->fonts->h;
@@ -2643,6 +2662,52 @@ zoom(const Arg *arg)
pop(c);
}
+
+void
+readxresources(void) {
+ XrmInitialize();
+
+ char* xrm;
+ if ((xrm = XResourceManagerString(drw->dpy))) {
+ char *type;
+ XrmDatabase xdb = XrmGetStringDatabase(xrm);
+ XrmValue xval;
+
+ if (XrmGetResource(xdb, "dwm.font", "*", &type, &xval) && !fonts[0])
+ fonts[0] = strdup(xval.addr);
+
+ if (XrmGetResource(xdb, "dwm.background", "*", &type, &xval) && !colors[SchemeNorm][ColBg] )
+ colors[SchemeNorm][ColBg] = strdup(xval.addr);
+ if (XrmGetResource(xdb, "dwm.foreground", "*", &type, &xval) && !colors[SchemeNorm][ColFg] )
+ colors[SchemeNorm][ColFg] = strdup(xval.addr);
+ if (XrmGetResource(xdb, "dwm.border", "*", &type, &xval) && !colors[SchemeNorm][ColBorder] )
+ colors[SchemeNorm][ColBorder] = strdup(xval.addr);
+
+ if (XrmGetResource(xdb, "dwm.selbackground", "*", &type, &xval) && !colors[SchemeSel][ColBg] )
+ colors[SchemeSel][ColBg] = strdup(xval.addr);
+ if (XrmGetResource(xdb, "dwm.selforeground", "*", &type, &xval) && !colors[SchemeSel][ColFg] )
+ colors[SchemeSel][ColFg] = strdup(xval.addr);
+ if (XrmGetResource(xdb, "dwm.selborder", "*", &type, &xval) && !colors[SchemeSel][ColBorder] )
+ colors[SchemeSel][ColBorder] = strdup(xval.addr);
+
+ if (XrmGetResource(xdb, "dwm.tabactivebackground", "*", &type, &xval) && !colors[SchemeTabActive][ColBg] )
+ colors[SchemeTabActive][ColBg] = strdup(xval.addr);
+ if (XrmGetResource(xdb, "dwm.tabactiveforeground", "*", &type, &xval) && !colors[SchemeTabActive][ColFg] )
+ colors[SchemeTabActive][ColFg] = strdup(xval.addr);
+ if (XrmGetResource(xdb, "dwm.tabactiveborder", "*", &type, &xval) && !colors[SchemeTabActive][ColBorder] )
+ colors[SchemeTabActive][ColBorder] = strdup(xval.addr);
+
+ if (XrmGetResource(xdb, "dwm.tabinactivebackground", "*", &type, &xval) && !colors[SchemeTabInactive][ColBg] )
+ colors[SchemeTabInactive][ColBg] = strdup(xval.addr);
+ if (XrmGetResource(xdb, "dwm.tabinactiveforeground", "*", &type, &xval) && !colors[SchemeTabInactive][ColFg] )
+ colors[SchemeTabInactive][ColFg] = strdup(xval.addr);
+ if (XrmGetResource(xdb, "dwm.tabinactiveborder", "*", &type, &xval) && !colors[SchemeTabInactive][ColBorder] )
+ colors[SchemeTabInactive][ColBorder] = strdup(xval.addr);
+
+ XrmDestroyDatabase(xdb);
+ }
+}
+
int
main(int argc, char *argv[])
{