This repository has been archived by the owner on Aug 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.c
368 lines (323 loc) · 8.75 KB
/
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <errno.h>
#include <libgen.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wordexp.h>
#include <wayland-client.h>
#include "config.h"
#include "oguri.h"
static char * expand_tilde(const char * path) {
if (strchr(path, '~') != path) {
return strdup(path);
}
char * home = getenv("HOME");
if (home == NULL || strcmp(home, "") == 0) {
fprintf(stderr, "$HOME is not set, cannot expand ~\n");
return NULL;
}
int length = snprintf(NULL, 0, "%s/%s", home, path + 1);
char * expanded = calloc(length, sizeof(char));
sprintf(expanded, "%s/%s", home, path + 1);
return expanded;
}
static char * expand_config_path(const char * path) {
// TODO: Return perror messages to caller for nicer display
if (!getenv("XDG_CONFIG_HOME")) {
char * home = getenv("HOME");
if (!home) {
return NULL;
}
char config_home[strlen(home) + strlen("/.config") + 1];
strcpy(config_home, home);
strcat(config_home, "/.config");
setenv("XDG_CONFIG_HOME", config_home, 1);
}
wordexp_t p = {0};
errno = 0;
if (wordexp(path, &p, WRDE_UNDEF|WRDE_NOCMD) != 0 || errno != 0) {
perror("Shell expansion error");
wordfree(&p);
return NULL;
}
if (access(p.we_wordv[0], R_OK) == -1 || errno != 0) {
perror(p.we_wordv[0]);
wordfree(&p);
return NULL;
}
char * expanded = strdup(p.we_wordv[0]);
if (!expanded) {
perror("Agh");
return NULL;
}
wordfree(&p);
return expanded;
}
//
// Output configs
//
struct oguri_output_config * oguri_output_config_create(
struct oguri_state * oguri, const char * output_name) {
struct oguri_output_config * opc = calloc(1, sizeof(struct oguri_output_config));
if (!opc) {
fprintf(stderr, "Failed to allocate memory for output config\n");
return false;
}
wl_list_init(&opc->link);
wl_list_insert(oguri->output_configs.prev, &opc->link);
opc->name = strdup(output_name);
opc->image_path = strdup("");
opc->scaling_mode = SCALING_MODE_FILL;
opc->anchor = ANCHOR_CENTER;
opc->filter = CAIRO_FILTER_BEST;
return opc;
}
void oguri_output_config_destroy(struct oguri_output_config * opc) {
wl_list_remove(&opc->link);
free(opc->image_path);
free(opc->name);
free(opc);
}
//
// Configurators
//
bool configure_global(
struct oguri_state * oguri __attribute__((unused)),
char * name __attribute__((unused)),
char * property __attribute__((unused)),
char * value __attribute__((unused))) {
fprintf(stderr, "Not in an output section\n");
return false;
}
bool configure_output(
struct oguri_state * oguri,
char * output_name,
char * property,
char * value) {
struct oguri_output_config * output = NULL;
// Try to find an existing config that matches the name.
struct oguri_output_config * cfg = NULL;
wl_list_for_each(cfg, &oguri->output_configs, link) {
if (strcmp(cfg->name, output_name) == 0) {
output = cfg;
break;
}
}
// If we didn't find one, make a new one.
// TODO: Should we avoid creating this until the property is validated?
if (!output) {
output = oguri_output_config_create(oguri, output_name);
}
if (strcmp(property, "image") == 0) {
char * expanded = expand_tilde(value);
if (access(expanded, R_OK)) {
fprintf(stderr, "Unable to access image '%s'\n", value);
free(expanded);
return false;
}
free(output->image_path);
output->image_path = expanded;
return true;
}
else if (strcmp(property, "scaling-mode") == 0) {
if (strcmp(value, "fill") == 0) {
output->scaling_mode = SCALING_MODE_FILL;
return true;
}
else if (strcmp(value, "stretch") == 0) {
output->scaling_mode = SCALING_MODE_STRETCH;
return true;
}
else if (strcmp(value, "tile") == 0) {
output->scaling_mode = SCALING_MODE_TILE;
return true;
}
else {
fprintf(stderr, "Unknown scaling mode: '%s'\n", value);
return false;
}
}
else if (strcmp(property, "anchor") == 0) {
// The anchor property consists of up to two points, separated by a
// dash. The order of the points doesn't matter, but some of them are
// mutually exclusive. The default is `center-center`, which is the
// same as just `center` (zero). Just for fun, we're not actually
// restricting the number of components of the anchor. After two, they
// all become either no-ops or invalid combos anyway.
int anchor = ANCHOR_CENTER;
char * saveptr;
char * point = strtok_r(value, "-", &saveptr);
while (point) {
if (strcmp(point, "top") == 0) {
if (anchor & ANCHOR_BOTTOM) {
fprintf(stderr,
"Top and bottom anchors are mutually exclusive\n");
return false;
}
anchor |= ANCHOR_TOP;
}
else if (strcmp(point, "left") == 0) {
if (anchor & ANCHOR_RIGHT) {
fprintf(stderr,
"Left and right anchors are mutually exclusive\n");
return false;
}
anchor |= ANCHOR_LEFT;
}
else if (strcmp(point, "bottom") == 0) {
if (anchor & ANCHOR_TOP) {
fprintf(stderr,
"Top and bottom anchors are mutually exclusive\n");
return false;
}
anchor |= ANCHOR_BOTTOM;
}
else if (strcmp(point, "right") == 0) {
if (anchor & ANCHOR_LEFT) {
fprintf(stderr,
"Left and right anchors are mutually exclusive\n");
return false;
}
anchor |= ANCHOR_RIGHT;
}
else if (strcmp(point, "center") == 0) {
// Center is actually just a no-op.
}
else {
fprintf(stderr, "Invalid anchor point: '%s'\n", point);
return false;
}
point = strtok_r(NULL, "-", &saveptr);
}
output->anchor = anchor;
return true;
}
else if (strcmp(property, "filter") == 0) {
// https://cairographics.org/manual/cairo-cairo-pattern-t.html#cairo-filter-t
if (strcmp(value, "fast") == 0) {
output->filter = CAIRO_FILTER_FAST;
return true;
}
else if (strcmp(value, "good") == 0) {
output->filter = CAIRO_FILTER_GOOD;
return true;
}
else if (strcmp(value, "best") == 0) {
output->filter = CAIRO_FILTER_BEST;
return true;
}
else if (strcmp(value, "nearest") == 0) {
output->filter = CAIRO_FILTER_NEAREST;
return true;
}
else if (strcmp(value, "bilinear") == 0) {
output->filter = CAIRO_FILTER_BILINEAR;
return true;
}
else {
fprintf(stderr, "Unknown filter: '%s'\n", value);
return false;
}
}
else {
fprintf(stderr, "Invalid output property: '%s'\n", property);
return false;
}
assert(!"Unreached");
}
oguri_configurator_t * configurator_from_string(const char * name) {
if (strcmp(name, "global") == 0) {
return configure_global;
}
else if (strcmp(name, "output") == 0) {
return configure_output;
}
return NULL;
}
//
// Config file parsing
//
int load_config_file(struct oguri_state * oguri, const char * path) {
char * expanded_path = expand_config_path(path);
if (!expanded_path) {
return -1;
}
FILE * config_file = fopen(expanded_path, "r");
if (!config_file) {
perror(expanded_path);
free(expanded_path);
return -1;
}
const char * filename = basename(expanded_path);
int result = load_config(oguri, config_file, filename);
free(expanded_path);
return result;
}
int load_config(struct oguri_state * oguri, FILE * config_file,
const char * filename) {
// The top of the file is the global section until we hit the first set of
// square brackets.
oguri_configurator_t * configurator = configure_global;
char * section_name = NULL;
int ret = 0;
int lineno = 0;
char * line = NULL;
size_t n = 0;
while (getline(&line, &n, config_file) > 0) {
++lineno;
// Ignore blank lines and comments.
if (line[0] == '\0' || line[0] == '\n' || line[0] == '#') {
continue;
}
// Strip the newline off, it is not part of the value.
if (line[strlen(line) - 1] == '\n') {
line[strlen(line) - 1] = '\0';
}
// Select our configurator based on the type of section.
if (line[0] == '[' && line[strlen(line) - 1] == ']') {
char * saveptr = NULL;
char * section_type = strtok_r(line + 1, " ]", &saveptr);
configurator = configurator_from_string(section_type);
if (!configurator) {
fprintf(stderr, "[%s:%d] Invalid section type: '%s'\n",
filename, lineno, section_type);
ret = -1;
break;
}
if (!saveptr) {
fprintf(stderr, "[%s:%d] Must specify name for %s\n",
filename, lineno, section_type);
ret = -1;
break;
}
free(section_name);
section_name = strdup(strtok_r(NULL, "]", &saveptr));
if (!section_name) {
fprintf(stderr, "[%s:%d] No closing bracket found\n",
filename, lineno);
ret = -1;
break;
}
continue;
}
char * eq = strchr(line, '=');
if (!eq) {
fprintf(stderr, "[%s:%d] Expected key=value\n", filename, lineno);
ret = -1;
break;
}
eq[0] = '\0';
if (!configurator(oguri, section_name, line, eq + 1)) {
fprintf(stderr, "[%s:%d] Failed to parse property '%s'\n",
filename, lineno, line);
ret = -1;
break;
}
}
free(section_name);
free(line);
fclose(config_file);
return ret;
}