xresources
[dwm.git] / dwm.c
diff --git a/dwm.c b/dwm.c
index 527bea2..4edd7ce 100644 (file)
--- a/dwm.c
+++ b/dwm.c
@@ -37,6 +37,7 @@
 #include <X11/Xlib.h>
 #include <X11/Xproto.h>
 #include <X11/Xutil.h>
+#include <X11/Xresource.h>
 #ifdef XINERAMA
 #include <X11/extensions/Xinerama.h>
 #endif /* XINERAMA */
@@ -143,6 +144,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);
@@ -238,6 +252,8 @@ 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);
 
 /* variables */
 static const char autostartblocksh[] = "autostart_blocking.sh";
@@ -2223,6 +2239,60 @@ 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
+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[])
 {
@@ -2235,6 +2305,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)