From 5a612138a93d536f2510e4eaf6bf2cffc77d212c Mon Sep 17 00:00:00 2001 From: Ryan Date: Wed, 20 Sep 2023 20:50:24 -0400 Subject: [PATCH] apply xresources patch --- config.def.h | 10 ++++++++ slock.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ util.h | 3 +++ 3 files changed, 81 insertions(+) diff --git a/config.def.h b/config.def.h index 8ade6ea..770a936 100644 --- a/config.def.h +++ b/config.def.h @@ -9,6 +9,16 @@ static const char *colorname[NUMCOLS] = { [FAILED] = "#CC3333", /* wrong password */ }; +/* + * Xresources preferences to load at startup + */ +ResourcePref resources[] = { + { "normbgcolor", STRING, &colorname[BACKGROUND] }, + { "normfgcolor", STRING, &colorname[INIT] }, + { "selfgcolor", STRING, &colorname[INPUT] }, + { "failfgcolor", STRING, &colorname[FAILED] }, +}; + /* treat a cleared input like a wrong password (color) */ static const int failonclear = 1; diff --git a/slock.c b/slock.c index 07715d8..336fe8f 100644 --- a/slock.c +++ b/slock.c @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -23,6 +24,7 @@ #include #include #include +#include #include #include "arg.h" @@ -38,6 +40,19 @@ enum { NUMCOLS }; +/* Xresources preferences */ +enum resource_type { + STRING = 0, + INTEGER = 1, + FLOAT = 2 +}; + +typedef struct { + char *name; + enum resource_type type; + void *dst; +} ResourcePref; + #include "config.h" struct lock { @@ -376,6 +391,57 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen) return NULL; } +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", "slock", name); + snprintf(fullclass, sizeof(fullclass), "%s.%s", "Slock", 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 +config_init(Display *dpy) +{ + char *resm; + XrmDatabase db; + ResourcePref *p; + + XrmInitialize(); + resm = XResourceManagerString(dpy); + if (!resm) + return; + + db = XrmGetStringDatabase(resm); + for (p = resources; p < resources + LEN(resources); p++) + resource_load(db, p->name, p->type, p->dst); +} + static void usage(void) { @@ -489,6 +555,8 @@ main(int argc, char **argv) { #endif + config_init(dpy); + /* check for Xrandr support */ rr.active = XRRQueryExtension(dpy, &rr.evbase, &rr.errbase); diff --git a/util.h b/util.h index 6f748b8..148dbc1 100644 --- a/util.h +++ b/util.h @@ -1,2 +1,5 @@ +/* macros */ +#define LEN(a) (sizeof(a) / sizeof(a)[0]) + #undef explicit_bzero void explicit_bzero(void *, size_t);