This repository has been archived by the owner on Jul 6, 2023. It is now read-only.
forked from Lixxia/i3lock
-
Notifications
You must be signed in to change notification settings - Fork 3
/
i3lock_config.c
320 lines (294 loc) · 8.25 KB
/
i3lock_config.c
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <wordexp.h>
#include <err.h>
#include <errno.h>
#include <stdbool.h>
#include <xkbcommon/xkbcommon.h>
#include <time.h>
#include "cursors.h"
#include "i3lock_config.h"
#include "util.h"
/* Length of the key -> setter function array */
#define VALID_KEYS_LEN 16
const int CMD_KEY_SHIFT = 1;
const int CMD_KEY_CTRL = 2;
const int CMD_KEY_ALT = 4;
const int CMD_KEY_SUPER = 8;
struct config configuration = {
.dont_fork = false,
.beep = false,
.color = "ffffff",
.verifycolor = "00ff00",
.wrongcolor = "ff0000",
.idlecolor = "000000",
.circle_alpha = 20,
.curs_choice = CURS_NONE,
.unlock_indicator = true,
.image_path = NULL,
.tiling = false,
.ignore_empty_password = false,
.show_failed_attempts = false,
.tfstring = "%l:%M %p",
.dfstring = "",
.commands = NULL
};
/*
* Setters for the single config values
*/
static int set_dont_fork(char* val) {
if (!strcmp("true", val)) {
configuration.dont_fork = true;
} else if (!strcmp("false", val)) {
configuration.dont_fork = false;
} else {
return EINVAL;
}
return 0;
}
static int set_beep(char* val) {
if (!strcmp("true", val)) {
configuration.beep = true;
} else if (!strcmp("false", val)) {
configuration.beep = false;
} else {
return EINVAL;
}
return 0;
}
static int set_color(char* val) {
return verify_hex(val, configuration.color, "color");
}
static int set_verify_color(char* val) {
return verify_hex(val, configuration.verifycolor, "verifycolor");
}
static int set_wrong_color(char* val) {
return verify_hex(val, configuration.wrongcolor, "wrongcolor");
}
static int set_idle_color(char* val) {
return verify_hex(val, configuration.idlecolor, "idlecolor");
}
int set_circle_alpha(char* val) {
errno = 0;
long la = strtol(val, NULL, 10);
if (errno) {
return 1;
}
if (la < 0 || la > 100) {
return 1;
}
configuration.circle_alpha = (int) la;
return 0;
}
static int set_pointer(char* val) {
if (!strcmp("none", val)) {
configuration.curs_choice = CURS_NONE;
} else if (!strcmp("default", val)) {
configuration.curs_choice = CURS_DEFAULT;
} else if (!strcmp("win", val)) {
configuration.curs_choice = CURS_WIN;
} else {
return EINVAL;
}
return 0;
}
static int set_image(char* val) {
configuration.image_path = strdup(val);
return 0;
}
static int set_image_tiling(char* val) {
if (!strcmp("true", val)) {
configuration.tiling = true;
} else if (!strcmp("false", val)) {
configuration.tiling = false;
} else {
return EINVAL;
}
return 0;
}
static int set_ignore_empty_password(char* val) {
if (!strcmp("true", val)) {
configuration.ignore_empty_password = true;
} else if (!strcmp("false", val)) {
configuration.ignore_empty_password = false;
} else {
return EINVAL;
}
return 0;
}
static int set_show_unlock_indicator(char* val) {
if (!strcmp("true", val)) {
configuration.unlock_indicator = true;
} else if (!strcmp("false", val)) {
configuration.unlock_indicator = false;
} else {
return EINVAL;
}
return 0;
}
static int set_show_failed_attempts(char* val) {
if (!strcmp("true", val)) {
configuration.show_failed_attempts = true;
} else if (!strcmp("false", val)) {
configuration.show_failed_attempts = false;
} else {
return EINVAL;
}
return 0;
}
int set_fstring(char* val, int is_date) {
size_t testlen;
char testout[100];
time_t rawtime;
struct tm *tm;
if (strlen(val) == 0) {
return 0;
}
time(&rawtime);
tm = localtime(&rawtime);
testlen = strftime(testout, sizeof(testout), val, tm);
/* May yield some false negatives, but still better than passing buffers with
undefined content to Xlib... */
if (testlen == 0UL) {
return 1;
}
if (is_date) {
strncpy(configuration.dfstring, val, sizeof(configuration.dfstring));
} else {
strncpy(configuration.tfstring, val, sizeof(configuration.tfstring));
}
return 0;
}
static int set_tfstring(char *val) {
return set_fstring(val, 0);
}
static int set_dfstring(char *val) {
return set_fstring(val, 1);
}
static int append_command(char* val) {
char *keys;
char *key;
char *cmd;
int mod = 0;
xkb_keysym_t ksym;
/* Separate key combination and command to execute */
char* keys_untr = strtok(val, "=");
keys = trim(keys_untr);
char* cmd_untr = strtok(NULL, "");
cmd = trim(cmd_untr);
/* Decompose key combination into single keys */
key = strtok(keys, "+");
while (key) {
/* If the key is a modifier, set the appropriate flag */
if (!strcmp("ctrl", key)) {
mod |= CMD_KEY_CTRL;
} else if (!strcmp("alt", key)) {
mod |= CMD_KEY_ALT;
} else if (!strcmp("super", key)) {
mod |= CMD_KEY_SUPER;
} else if (!strcmp("shift", key)) {
mod |= CMD_KEY_SHIFT;
} else {
/* Convert the key name into a XKB keysym and check whether it actually exists */
ksym = xkb_keysym_from_name(key, 0);
if (ksym == XKB_KEY_NoSymbol) {
fprintf(stderr, "Unknown value: `%s`\n", key);
return 1;
}
/* Prepend a new entry to the configuration's command list */
struct cmdlist *item = (struct cmdlist*) malloc(sizeof(struct cmdlist));
item->mods = mod;
item->ksym = ksym;
item->command = (char*) malloc((strlen(cmd)+1)*sizeof(char));
strcpy(item->command, cmd);
item->next = configuration.commands;
configuration.commands = item;
/* Try to get one more key for error checking */
key = strtok(NULL, "+");
break;
}
key = strtok(NULL, "+");
}
/*
* If a non-modifier key was encountered and there are still other keys,
* assume an error in the config.
*/
if (key) {
fprintf(stderr, "Invalid key combination token: `%s`\n", key);
return 1;
}
return 0;
}
/*
* Array of valid config file keys with a pointer to the corresponding
* setter function.
*/
struct {
const char *key;
int (*setter) (char*);
} valid_keys[VALID_KEYS_LEN] = {
{ .key = "nofork", .setter = set_dont_fork },
{ .key = "beep", .setter = set_beep },
{ .key = "color", .setter = set_color },
{ .key = "verify-color", .setter = set_verify_color },
{ .key = "wrong-color", .setter = set_wrong_color },
{ .key = "idle-color", .setter = set_idle_color },
{ .key = "circle-alpha", .setter = set_circle_alpha },
{ .key = "pointer", .setter = set_pointer },
{ .key = "image", .setter = set_image },
{ .key = "tiling", .setter = set_image_tiling },
{ .key = "ignore-empty-password", .setter = set_ignore_empty_password },
{ .key = "unlock-indicator", .setter = set_show_unlock_indicator },
{ .key = "show-failed-attempts", .setter = set_show_failed_attempts },
{ .key = "time-format", .setter = set_tfstring },
{ .key = "date-format", .setter = set_dfstring },
{ .key = "command", .setter = append_command }
};
int parse_config(char* filename) {
/* Expand file name (e.g. `~` -> `/home/username`) and open file */
char *fexp = expand_path(filename);
errno = 0;
FILE *conffile = fopen(fexp, "r");
if (!conffile) {
return errno;
}
/* Read line after line */
char *linebuf = (char*) malloc(sizeof(char)*4096);
for (int lc = 1; fgets(linebuf, 4096, conffile); ++lc) {
/* Extract option key and value */
char *untr_key = strdup(strtok(linebuf, "="));
char *key = trim(untr_key);
if (key == NULL || strlen(key) == 0 || key[0] == '#') {
/* Skip, if the line is empty or a comment (starts with #) */
continue;
}
char *untr_val = strdup(strtok(NULL, ""));
char *val = trim(untr_val);
free(untr_key);
free(untr_val);
/*
* Search a matching key in the list of valid keys and call the
* corresponding setter. If an invalid key or value is found, print
* an error, but ignore it, as we want to lock the screen after all.
*/
bool valid_key = false;
for (int i = 0; i < VALID_KEYS_LEN; ++i) {
if (!strcmp(valid_keys[i].key, key)) {
valid_key = true;
if (valid_keys[i].setter(val)) {
errx(EXIT_FAILURE, "Parsing error: `%s` (%s:%d)", val, filename, lc);
}
}
}
if (!valid_key) {
errx(EXIT_FAILURE, "Unknown key: `%s` (%s:%d)", key, filename, lc);
}
free(key);
free(val);
}
fclose(conffile);
free(linebuf);
return 0;
}