add xresources

This commit is contained in:
speedie 2023-07-04 18:28:47 +02:00
parent c94ae037d5
commit 7849a60e12
2 changed files with 121 additions and 13 deletions

View file

@ -1,18 +1,18 @@
/* See LICENSE file for copyright and license details. */
/* appearance */
static const unsigned int borderpx = 1; /* border pixel of windows */
static const unsigned int snap = 32; /* snap pixel */
static const unsigned int gappih = 20; /* horiz inner gap between windows */
static const unsigned int gappiv = 10; /* vert inner gap between windows */
static const unsigned int gappoh = 10; /* horiz outer gap between windows and screen edge */
static const unsigned int gappov = 30; /* vert outer gap between windows and screen edge */
static unsigned int borderpx = 1; /* border pixel of windows */
static unsigned int snap = 32; /* snap pixel */
static unsigned int gappih = 20; /* horiz inner gap between windows */
static unsigned int gappiv = 10; /* vert inner gap between windows */
static unsigned int gappoh = 10; /* horiz outer gap between windows and screen edge */
static unsigned int gappov = 30; /* vert outer gap between windows and screen edge */
static int smartgaps = 0; /* 1 means no outer gap when there is only one window */
static const int showbar = 1; /* 0 means no bar */
static const int topbar = 1; /* 0 means bottom bar */
static const unsigned int systrayspacing = 2; /* systray spacing */
static const int showsystray = 1; /* 0 means no systray */
static const char statussep = ';'; /* separator between status bars */
static int showbar = 1; /* 0 means no bar */
static int topbar = 1; /* 0 means bottom bar */
static unsigned int systrayspacing = 2; /* systray spacing */
static int showsystray = 1; /* 0 means no systray */
static char statussep = ';'; /* separator between status bars */
static char font[] = "monospace 10";
static char col_gray1[] = "#222222";
static char col_gray2[] = "#444444";
@ -20,8 +20,8 @@ static char col_gray3[] = "#bbbbbb";
static char col_gray4[] = "#eeeeee";
static char col_cyan[] = "#005577";
static const int hidevacant = 1;
static const int monoclenogaps = 1;
static int hidevacant = 1;
static int monoclenogaps = 1;
/* tagging */
static const char *tags[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
@ -192,3 +192,28 @@ static const Button buttons[] = {
{ ClkTagBar, MODKEY, Button3, toggletag, {0} },
};
/*
* Xresources preferences to load at startup
*/
ResourcePref resources[] = {
{ "font", STRING, &font },
{ "color0", STRING, &col_gray2 },
{ "color8", STRING, &col_cyan },
{ "color0", STRING, &col_gray1 },
{ "color4", STRING, &col_gray3 },
{ "color0", STRING, &col_gray4 },
{ "color4", STRING, &col_cyan },
{ "col_gray1", STRING, &col_gray1 },
{ "col_gray2", STRING, &col_gray2 },
{ "col_gray3", STRING, &col_gray3 },
{ "col_cyan", STRING, &col_cyan },
{ "col_gray4", STRING, &col_gray4 },
{ "borderpx", INTEGER, &borderpx },
{ "snap", INTEGER, &snap },
{ "showbar", INTEGER, &showbar },
{ "topbar", INTEGER, &topbar },
{ "nmaster", INTEGER, &nmaster },
{ "resizehints", INTEGER, &resizehints },
{ "mfact", FLOAT, &mfact },
};

83
dwm.c
View file

@ -40,6 +40,7 @@
#ifdef XINERAMA
#include <X11/extensions/Xinerama.h>
#endif /* XINERAMA */
#include <X11/Xresource.h>
#include "drw.h"
#include "util.h"
@ -215,6 +216,19 @@ typedef struct {
int monitor;
} 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);
@ -315,6 +329,9 @@ static int xerror(Display *dpy, XErrorEvent *ee);
static int xerrordummy(Display *dpy, XErrorEvent *ee);
static int xerrorstart(Display *dpy, XErrorEvent *ee);
static void zoom(const Arg *arg);
static void load_xresources(void);
static void resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst);
static void xrdb(void);
#include "libs/bar/include.h"
#include "libs/lt/tile.h"
@ -503,6 +520,8 @@ arrange(Monitor *m)
restack(m);
} else for (m = mons; m; m = m->next)
arrangemon(m);
xrdb();
}
void
@ -2821,6 +2840,68 @@ zoom(const Arg *arg)
pop(c);
}
void
resource_load(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
xrdb(void)
{
load_xresources();
focus(NULL);
}
void
load_xresources(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++)
resource_load(db, p->name, p->type, p->dst);
XCloseDisplay(display);
}
int
main(int argc, char *argv[])
{
@ -2833,6 +2914,8 @@ main(int argc, char *argv[])
if (!(dpy = XOpenDisplay(NULL)))
die("dwm: cannot open display");
checkotherwm();
XrmInitialize();
load_xresources();
setup();
#ifdef __OpenBSD__
if (pledge("stdio rpath proc exec", NULL) == -1)