Skip to content

Commit

Permalink
patch++ xresources + xrdb
Browse files Browse the repository at this point in the history
- https://dwm.suckless.org/patches/xresources/dwm-xresources-6.2.diff
- https://dwm.suckless.org/patches/xrdb/dwm-xrdb-6.2.diff
- This patch is a combination of above two patches. xresources patch is
  used to read Xresources & xrdb patch calls loadxresources() to change
  Xresources at runtime with a key binding.
  • Loading branch information
jitessh committed Jun 18, 2021
1 parent 6af22c5 commit 10ba0c0
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 17 deletions.
27 changes: 27 additions & 0 deletions colors.h
@@ -0,0 +1,27 @@
/* ---------- styling guidlines ----------- */
/* norm_fg : text color of unselected tags & status bar : (color15) light color same as urg_fg */
/* norm_bg : bg color of unselected tags & status bar : (color0) dark color same as sel_fg */
/* norm_border : border color of unselected clients : (color8) medium color */
/* sel_fg : text color of selected tags : (color0) dark color same as norm_bg */
/* sel_bg : bg color of selected tags : (color4) main color same as sel_border */
/* sel_border : border color of selected tags : (color4) main color same as sel_bg */
/* urg_fg : text color of urgent tags : (color15) light color same as norm_fg */
/* urg_bg : bg color of urgent tags : (color1) urgent color same as urg_border */
/* urg_border : border color of urgent clients : (color1) urgent color same as urg_bg */

/* ------------ gruvbox theme ------------- */
static char norm_fg[] = "#ebdbb2";
static char norm_bg[] = "#282828";
static char norm_border[] = "#928374";
static char sel_fg[] = "#282828";
static char sel_bg[] = "#458588";
static char sel_border[] = "#458588";
static char urg_fg[] = "#ebdbb2";
static char urg_bg[] = "#cc241d";
static char urg_border[] = "#cc241d";
static char *colors[][3] = {
/* fg bg border */
[SchemeNorm] = { norm_fg, norm_bg, norm_border },
[SchemeSel] = { sel_fg, sel_bg, sel_border },
[SchemeUrg] = { urg_fg, urg_bg, urg_border },
};
33 changes: 18 additions & 15 deletions config.h
@@ -1,6 +1,7 @@
/* See LICENSE file for copyright and license details. */

/* -------------- appearance ------------- */
#include "colors.h"
static const char *fonts[] = { "monospace:size=10" };
static const unsigned int borderpx = 2; /* border pixel of windows */
static const unsigned int snap = 10; /* snap pixel */
Expand All @@ -11,24 +12,11 @@ static const unsigned int gappiv = 10; /* vert inner gap between window
static const unsigned int gappoh = 10; /* horiz outer gap between windows and screen edge */
static const unsigned int gappov = 10; /* vert outer gap between windows and screen edge */

static const int barheight = 25; /* 0 means dwm will calculate bar height wrt font */
static int barheight = 25; /* 0 means dwm will calculate bar height wrt font */
static const int showbar = 1; /* 0 means no bar */
static const int topbar = 1; /* 0 means bottom bar */
#define VIEWONTAG 1 /* switch view on tag switch */

static const char col_gray1[] = "#222222";
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 col_urgborder[] = "#ff0000";
static const char *colors[][3] = {
/* fg bg border */
[SchemeNorm] = { col_gray3, col_gray1, col_gray2 },
[SchemeSel] = { col_gray4, col_cyan, col_cyan },
[SchemeUrg] = { col_gray4, col_cyan, col_urgborder },
};

/* ----------------- sticky -------------- */
static const XPoint stickyicon[] = { {0,0}, {4,0}, {4,8}, {2,6}, {0,8}, {0,0} }; /* represents the icon as an array of vertices */
static const XPoint stickyiconbb = {4,8}; /* defines the bottom right corner of the polygon's bounding box (speeds up scaling) */
Expand Down Expand Up @@ -80,6 +68,20 @@ static const Layout layouts[] = {
{ NULL, NULL },
};

/* --------------- Xresources ------------ */
ResourcePref resources[] = {
{ "color15", STRING, &norm_fg },
{ "color0", STRING, &norm_bg },
{ "color8", STRING, &norm_border },
{ "color0", STRING, &sel_fg },
{ "color4", STRING, &sel_bg },
{ "color4", STRING, &sel_border },
{ "color15", STRING, &urg_fg },
{ "color1", STRING, &urg_bg },
{ "color1", STRING, &urg_border },
{ "barheight", INTEGER, &barheight },
};

/* ----------- key definitions ----------- */
#define MODKEY Mod4Mask
#define TAGKEYS(KEY,TAG) \
Expand Down Expand Up @@ -114,8 +116,9 @@ static Key keys[] = {
{ MODKEY, XK_Return, spawn, {.v = termcmd } },
{ MODKEY, XK_space, spawn, {.v = termcmd } }, /* one handed mode */
{ MODKEY, XK_d, spawn, {.v = menucmd } },
{ MODKEY|ShiftMask, XK_q, quit, {0} },
{ MODKEY|ShiftMask, XK_q, xrdb, {.v = NULL } },
{ MODKEY, XK_q, quit, {1} },
{ MODKEY|ControlMask, XK_q, quit, {0} },

/* ---------- layouts ---------- */
{ MODKEY, XK_t, setlayout, {.v = &layouts[0]} }, /* tile */
Expand Down
2 changes: 1 addition & 1 deletion drw.c
Expand Up @@ -208,7 +208,7 @@ drw_clr_create(Drw *drw, Clr *dest, const char *clrname)
/* Wrapper to create color schemes. The caller has to call free(3) on the
* returned color scheme when done using it. */
Clr *
drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount)
drw_scm_create(Drw *drw, char *clrnames[], size_t clrcount)
{
size_t i;
Clr *ret;
Expand Down
2 changes: 1 addition & 1 deletion drw.h
Expand Up @@ -39,7 +39,7 @@ void drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned in

/* Colorscheme abstraction */
void drw_clr_create(Drw *drw, Clr *dest, const char *clrname);
Clr *drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount);
Clr *drw_scm_create(Drw *drw, char *clrnames[], size_t clrcount);

/* Cursor abstraction */
Cur *drw_cur_create(Drw *drw, int shape);
Expand Down
84 changes: 84 additions & 0 deletions dwm.c
Expand Up @@ -36,6 +36,7 @@
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xproto.h>
#include <X11/Xresource.h>
#include <X11/Xutil.h>
#ifdef XINERAMA
#include <X11/extensions/Xinerama.h>
Expand Down Expand Up @@ -157,6 +158,19 @@ typedef struct {
const char scratchkey;
} Rule;

/* Xresources preferences */
enum resource_type {
STRING = 0,
INTEGER = 1,
FLOAT = 2
};

typedef struct {
char *name;
enum resource_type type;
void *dst;
} ResourcePref;

/* function declarations */
static void applyrules(Client *c);
static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact);
Expand Down Expand Up @@ -201,6 +215,7 @@ static void grabkeys(void);
static void incnmaster(const Arg *arg);
static void keypress(XEvent *e);
static void killclient(const Arg *arg);
static void loadxresources(void);
static void losefullscreen(Client *next);
static void manage(Window w, XWindowAttributes *wa);
static void mappingnotify(XEvent *e);
Expand All @@ -221,6 +236,7 @@ static void reorganizetags(const Arg *arg);
static void resize(Client *c, int x, int y, int w, int h, int interact);
static void resizeclient(Client *c, int x, int y, int w, int h);
static void resizemouse(const Arg *arg);
static void resourceload(XrmDatabase db, char *name, enum resource_type rtype, void *dst);
static void restack(Monitor *m);
static void rotatestack(const Arg *arg);
static void run(void);
Expand Down Expand Up @@ -273,6 +289,7 @@ static Monitor *wintomon(Window w);
static int xerror(Display *dpy, XErrorEvent *ee);
static int xerrordummy(Display *dpy, XErrorEvent *ee);
static int xerrorstart(Display *dpy, XErrorEvent *ee);
static void xrdb(const Arg *arg);
static void zoom(const Arg *arg);

/* variables */
Expand Down Expand Up @@ -1464,6 +1481,25 @@ killclient(const Arg *arg)
}
}

void
loadxresources(void)
{
Display *display;
char *resm;
XrmDatabase db;
ResourcePref *p;

display = XOpenDisplay(NULL);
resm = XResourceManagerString(display);
if (!resm)
return;

db = XrmGetStringDatabase(resm);
for (p = resources; p < resources + LENGTH(resources); p++)
resourceload(db, p->name, p->type, p->dst);
XCloseDisplay(display);
}

void
losefullscreen(Client *next)
{
Expand Down Expand Up @@ -2019,6 +2055,41 @@ resizemouse(const Arg *arg)
}
}

void
resourceload(XrmDatabase db, char *name, enum resource_type rtype, void *dst)
{
char *sdst = NULL;
int *idst = NULL;
float *fdst = NULL;

sdst = dst;
idst = dst;
fdst = dst;

char fullname[256];
char *type;
XrmValue ret;

snprintf(fullname, sizeof(fullname), "%s.%s", "dwm", name);
fullname[sizeof(fullname) - 1] = '\0';

XrmGetResource(db, fullname, "*", &type, &ret);
if (!(ret.addr == NULL || strncmp("String", type, 64)))
{
switch (rtype) {
case STRING:
strcpy(sdst, ret.addr);
break;
case INTEGER:
*idst = strtoul(ret.addr, NULL, 10);
break;
case FLOAT:
*fdst = strtof(ret.addr, NULL);
break;
}
}
}

void
restack(Monitor *m)
{
Expand Down Expand Up @@ -3265,6 +3336,17 @@ xerrorstart(Display *dpy, XErrorEvent *ee)
return -1;
}

void
xrdb(const Arg *arg)
{
loadxresources();
int i;
for (i = 0; i < LENGTH(colors); i++)
scheme[i] = drw_scm_create(drw, colors[i], 3);
focus(NULL);
arrange(NULL);
}

void
zoom(const Arg *arg)
{
Expand All @@ -3291,6 +3373,8 @@ main(int argc, char *argv[])
if (!(dpy = XOpenDisplay(NULL)))
die("dwm: cannot open display");
checkotherwm();
XrmInitialize();
loadxresources();
setup();
#ifdef __OpenBSD__
if (pledge("stdio rpath proc exec", NULL) == -1)
Expand Down

0 comments on commit 10ba0c0

Please sign in to comment.