diff options
author | Gustaf Rydholm <gustaf.rydholm@gmail.com> | 2022-01-16 13:31:06 +0100 |
---|---|---|
committer | Gustaf Rydholm <gustaf.rydholm@gmail.com> | 2022-01-16 13:31:06 +0100 |
commit | 5e46f37f059eec10779886b55445e05cdfbfbe65 (patch) | |
tree | f4a026218a076be9f1993c940a2f9eb20811b5e3 /slock.c | |
parent | d1f03eeb3e2a39b0386c9d17845dcfc1ec1cea38 (diff) |
Apply xresources patch
Diffstat (limited to 'slock.c')
-rw-r--r-- | slock.c | 68 |
1 files changed, 68 insertions, 0 deletions
@@ -7,6 +7,7 @@ #include <ctype.h> #include <errno.h> +#include <math.h> #include <grp.h> #include <pwd.h> #include <stdarg.h> @@ -25,6 +26,7 @@ #include <X11/Xlib.h> #include <X11/Xutil.h> #include <X11/Xft/Xft.h> +#include <X11/Xresource.h> #include "arg.h" #include "util.h" @@ -41,6 +43,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 { @@ -364,6 +379,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) { @@ -423,6 +489,8 @@ main(int argc, char **argv) { if (setuid(duid) < 0) die("slock: setuid: %s\n", strerror(errno)); + config_init(dpy); + /* check for Xrandr support */ rr.active = XRRQueryExtension(dpy, &rr.evbase, &rr.errbase); |