slock-xresources-20191126-53e56c7.diff (3671B)
1 From 53e56c751b3f2be4154760788850c51dbffc0add Mon Sep 17 00:00:00 2001 2 From: Arnas Udovicius <zordsdavini@gmail.com> 3 Date: Tue, 26 Nov 2019 16:16:15 +0200 4 Subject: [PATCH] Read colors from Xresources 5 6 --- 7 config.def.h | 14 +++++++++-- 8 slock.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 9 util.h | 3 +++ 10 3 files changed, 83 insertions(+), 2 deletions(-) 11 12 diff --git a/config.def.h b/config.def.h 13 index 6288856..bfc1ba0 100644 14 --- a/config.def.h 15 +++ b/config.def.h 16 @@ -3,11 +3,21 @@ static const char *user = "nobody"; 17 static const char *group = "nogroup"; 18 19 static const char *colorname[NUMCOLS] = { 20 - [INIT] = "black", /* after initialization */ 21 - [INPUT] = "#005577", /* during input */ 22 + [INIT] = "black", /* after initialization */ 23 + [INPUT] = "#005577", /* during input */ 24 [FAILED] = "#CC3333", /* wrong password */ 25 [CAPS] = "red", /* CapsLock on */ 26 }; 27 28 +/* 29 + * Xresources preferences to load at startup 30 + */ 31 +ResourcePref resources[] = { 32 + { "color0", STRING, &colorname[INIT] }, 33 + { "color4", STRING, &colorname[INPUT] }, 34 + { "color1", STRING, &colorname[FAILED] }, 35 + { "color3", STRING, &colorname[CAPS] }, 36 +}; 37 + 38 /* treat a cleared input like a wrong password (color) */ 39 static const int failonclear = 1; 40 diff --git a/slock.c b/slock.c 41 index 5f4fb7a..2395547 100644 42 --- a/slock.c 43 +++ b/slock.c 44 @@ -6,6 +6,7 @@ 45 46 #include <ctype.h> 47 #include <errno.h> 48 +#include <math.h> 49 #include <grp.h> 50 #include <pwd.h> 51 #include <stdarg.h> 52 @@ -19,6 +20,7 @@ 53 #include <X11/Xlib.h> 54 #include <X11/Xutil.h> 55 #include <X11/XKBlib.h> 56 +#include <X11/Xresource.h> 57 58 #include "arg.h" 59 #include "util.h" 60 @@ -46,6 +48,19 @@ struct xrandr { 61 int errbase; 62 }; 63 64 +/* Xresources preferences */ 65 +enum resource_type { 66 + STRING = 0, 67 + INTEGER = 1, 68 + FLOAT = 2 69 +}; 70 + 71 +typedef struct { 72 + char *name; 73 + enum resource_type type; 74 + void *dst; 75 +} ResourcePref; 76 + 77 #include "config.h" 78 79 static void 80 @@ -306,6 +321,57 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen) 81 return NULL; 82 } 83 84 +int 85 +resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst) 86 +{ 87 + char **sdst = dst; 88 + int *idst = dst; 89 + float *fdst = dst; 90 + 91 + char fullname[256]; 92 + char fullclass[256]; 93 + char *type; 94 + XrmValue ret; 95 + 96 + snprintf(fullname, sizeof(fullname), "%s.%s", "slock", name); 97 + snprintf(fullclass, sizeof(fullclass), "%s.%s", "Slock", name); 98 + fullname[sizeof(fullname) - 1] = fullclass[sizeof(fullclass) - 1] = '\0'; 99 + 100 + XrmGetResource(db, fullname, fullclass, &type, &ret); 101 + if (ret.addr == NULL || strncmp("String", type, 64)) 102 + return 1; 103 + 104 + switch (rtype) { 105 + case STRING: 106 + *sdst = ret.addr; 107 + break; 108 + case INTEGER: 109 + *idst = strtoul(ret.addr, NULL, 10); 110 + break; 111 + case FLOAT: 112 + *fdst = strtof(ret.addr, NULL); 113 + break; 114 + } 115 + return 0; 116 +} 117 + 118 +void 119 +config_init(Display *dpy) 120 +{ 121 + char *resm; 122 + XrmDatabase db; 123 + ResourcePref *p; 124 + 125 + XrmInitialize(); 126 + resm = XResourceManagerString(dpy); 127 + if (!resm) 128 + return; 129 + 130 + db = XrmGetStringDatabase(resm); 131 + for (p = resources; p < resources + LEN(resources); p++) 132 + resource_load(db, p->name, p->type, p->dst); 133 +} 134 + 135 static void 136 usage(void) 137 { 138 @@ -364,6 +430,8 @@ main(int argc, char **argv) { 139 if (setuid(duid) < 0) 140 die("slock: setuid: %s\n", strerror(errno)); 141 142 + config_init(dpy); 143 + 144 /* check for Xrandr support */ 145 rr.active = XRRQueryExtension(dpy, &rr.evbase, &rr.errbase); 146 147 diff --git a/util.h b/util.h 148 index 6f748b8..148dbc1 100644 149 --- a/util.h 150 +++ b/util.h 151 @@ -1,2 +1,5 @@ 152 +/* macros */ 153 +#define LEN(a) (sizeof(a) / sizeof(a)[0]) 154 + 155 #undef explicit_bzero 156 void explicit_bzero(void *, size_t); 157 -- 158 2.24.0 159