From 8c48f1564c555bbd21758a3a70a9984e61c34a35 Mon Sep 17 00:00:00 2001 From: 6d6f7274686f6e <4648531+6d6f7274686f6e@users.noreply.github.com> Date: Wed, 17 Mar 2021 10:59:18 +0100 Subject: [PATCH] xresources support --- config.def.h | 27 ++++++++++++++------ tabbed.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 7 deletions(-) diff --git a/config.def.h b/config.def.h index defa426..244e288 100644 --- a/config.def.h +++ b/config.def.h @@ -1,13 +1,13 @@ /* See LICENSE file for copyright and license details. */ /* appearance */ -static const char font[] = "monospace:size=9"; -static const char* normbgcolor = "#222222"; -static const char* normfgcolor = "#cccccc"; -static const char* selbgcolor = "#555555"; -static const char* selfgcolor = "#ffffff"; -static const char* urgbgcolor = "#111111"; -static const char* urgfgcolor = "#cc0000"; +static char font[] = "monospace:size=9"; +static char* normbgcolor = "#222222"; +static char* normfgcolor = "#cccccc"; +static char* selbgcolor = "#555555"; +static char* selfgcolor = "#ffffff"; +static char* urgbgcolor = "#111111"; +static char* urgfgcolor = "#cc0000"; static const char before[] = "<"; static const char after[] = ">"; static const char titletrim[] = "..."; @@ -33,6 +33,19 @@ static Bool npisrelative = False; } \ } +/* + * Xresources preferences to load at startup + */ +ResourcePref resources[] = { + { "font", STRING, &font }, + { "color0", STRING, &normbgcolor }, + { "color4", STRING, &normfgcolor }, + { "color4", STRING, &selbgcolor }, + { "color7", STRING, &selfgcolor }, + { "color2", STRING, &urgbgcolor }, + { "color3", STRING, &urgfgcolor }, +}; + #define MODKEY ControlMask static Key keys[] = { /* modifier key function argument */ diff --git a/tabbed.c b/tabbed.c index eafe28a..c5bffc7 100644 --- a/tabbed.c +++ b/tabbed.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -85,11 +86,26 @@ typedef struct { Bool urgent; Bool closed; } Client; + +/* 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 buttonpress(const XEvent *e); static void cleanup(void); static void clientmessage(const XEvent *e); +static void config_init(void); static void configurenotify(const XEvent *e); static void configurerequest(const XEvent *e); static void createnotify(const XEvent *e); @@ -120,6 +136,7 @@ static void move(const Arg *arg); static void movetab(const Arg *arg); static void propertynotify(const XEvent *e); static void resize(int c, int w, int h); +static int resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst); static void rotate(const Arg *arg); static void run(void); static void sendxembed(int c, long msg, long detail, long d1, long d2); @@ -245,6 +262,23 @@ clientmessage(const XEvent *e) } } +void +config_init(void) +{ + char *resm; + XrmDatabase db; + ResourcePref *p; + + XrmInitialize(); + resm = XResourceManagerString(dpy); + if (!resm) + return; + + db = XrmGetStringDatabase(resm); + for (p = resources; p < resources + LENGTH(resources); p++) + resource_load(db, p->name, p->type, p->dst); +} + void configurenotify(const XEvent *e) { @@ -897,6 +931,40 @@ resize(int c, int w, int h) (XEvent *)&ce); } +int +resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst) +{ + char **sdst = dst; + int *idst = dst; + float *fdst = dst; + + char fullname[256]; + char fullclass[256]; + char *type; + XrmValue ret; + + snprintf(fullname, sizeof(fullname), "%s.%s", "tabbed", name); + snprintf(fullclass, sizeof(fullclass), "%s.%s", "tabbed", name); + fullname[sizeof(fullname) - 1] = fullclass[sizeof(fullclass) - 1] = '\0'; + + XrmGetResource(db, fullname, fullclass, &type, &ret); + if (ret.addr == NULL || strncmp("String", type, 64)) + return 1; + + switch (rtype) { + case STRING: + *sdst = ret.addr; + break; + case INTEGER: + *idst = strtoul(ret.addr, NULL, 10); + break; + case FLOAT: + *fdst = strtof(ret.addr, NULL); + break; + } + return 0; +} + void rotate(const Arg *arg) { @@ -1354,6 +1422,7 @@ main(int argc, char *argv[]) if (!(dpy = XOpenDisplay(NULL))) die("%s: cannot open display\n", argv0); + config_init(); setup(); printf("0x%lx\n", win); fflush(NULL); -- 2.30.2