~ob/slock

ref: e253e96c0a0b5d1f5b9878a2bbdbe4f12685be87 slock/patches/slock-xresources-20191126-53e56c7.diff -rw-r--r-- 3.3 KiB
e253e96cOscar Benedito Apply xresources patch 1 year, 1 month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
From 53e56c751b3f2be4154760788850c51dbffc0add Mon Sep 17 00:00:00 2001
From: Arnas Udovicius <zordsdavini@gmail.com>
Date: Tue, 26 Nov 2019 16:16:15 +0200
Subject: [PATCH] Read colors from Xresources

---
 config.def.h | 14 +++++++++--
 slock.c      | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 util.h       |  3 +++
 3 files changed, 83 insertions(+), 2 deletions(-)

diff --git a/config.def.h b/config.def.h
index 6288856..bfc1ba0 100644
--- a/config.def.h
+++ b/config.def.h
@@ -8,5 +8,15 @@ static const char *user  = "nobody";
 	[FAILED] = "#CC3333",   /* wrong password */
 };
 
+/*
+ * Xresources preferences to load at startup
+ */
+ResourcePref resources[] = {
+		{ "color0",       STRING,  &colorname[INIT] },
+		{ "color4",       STRING,  &colorname[INPUT] },
+		{ "color1",       STRING,  &colorname[FAILED] },
+		{ "color3",       STRING,  &colorname[CAPS] },
+};
+
 /* treat a cleared input like a wrong password (color) */
 static const int failonclear = 1;
diff --git a/slock.c b/slock.c
index 5f4fb7a..2395547 100644
--- a/slock.c
+++ b/slock.c
@@ -6,6 +6,7 @@
 
 #include <ctype.h>
 #include <errno.h>
+#include <math.h>
 #include <grp.h>
 #include <pwd.h>
 #include <stdarg.h>
@@ -18,6 +19,7 @@
 #include <X11/keysym.h>
 #include <X11/Xlib.h>
 #include <X11/Xutil.h>
+#include <X11/Xresource.h>
 
 #include "arg.h"
 #include "util.h"
@@ -46,6 +48,19 @@ struct xrandr {
 	int errbase;
 };
 
+/* 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"
 
 static void
@@ -306,6 +321,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)
 {
@@ -364,6 +430,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);
 
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);
-- 
2.24.0