forked from francma/wob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.c
395 lines (348 loc) · 11.1 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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#define WOB_FILE "config.c"
#define MIN_PERCENTAGE_BAR_WIDTH 1
#define MIN_PERCENTAGE_BAR_HEIGHT 1
#define _POSIX_C_SOURCE 200809L
#include <ini.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wordexp.h>
#include "config.h"
bool
parse_output_mode(const char *str, enum wob_output_mode *value)
{
if (strcmp(str, "whitelist") == 0) {
*value = WOB_OUTPUT_MODE_WHITELIST;
return true;
}
if (strcmp(str, "all") == 0) {
*value = WOB_OUTPUT_MODE_ALL;
return true;
}
if (strcmp(str, "focused") == 0) {
*value = WOB_OUTPUT_MODE_FOCUSED;
return true;
}
return false;
}
bool
parse_overflow_mode(const char *str, enum wob_overflow_mode *value)
{
if (strcmp(str, "wrap") == 0) {
*value = WOB_OVERFLOW_MODE_WRAP;
return true;
}
if (strcmp(str, "nowrap") == 0) {
*value = WOB_OVERFLOW_MODE_NOWRAP;
return true;
}
return false;
}
bool
parse_number(const char *str, unsigned long *value)
{
char *str_end;
unsigned long ul = strtoul(str, &str_end, 10);
if (*str_end != '\0') {
return false;
}
*value = ul;
return true;
}
bool
parse_anchor(const char *str, unsigned long *anchor)
{
char str_dup[INI_MAX_LINE + 1] = {0};
strncpy(str_dup, str, INI_MAX_LINE);
char *token = strtok(str_dup, " ");
*anchor = WOB_ANCHOR_CENTER;
while (token) {
if (strcmp(token, "left") == 0) {
*anchor |= WOB_ANCHOR_LEFT;
}
else if (strcmp(token, "right") == 0) {
*anchor |= WOB_ANCHOR_RIGHT;
}
else if (strcmp(token, "top") == 0) {
*anchor |= WOB_ANCHOR_TOP;
}
else if (strcmp(token, "bottom") == 0) {
*anchor |= WOB_ANCHOR_BOTTOM;
}
else if (strcmp(token, "center") != 0) {
return false;
}
token = strtok(NULL, " ");
}
return true;
}
bool
parse_color(const char *str, struct wob_color *color)
{
char *str_end;
if (wob_color_from_string(str, &str_end, color) == false || *str_end != '\0') {
return false;
}
return true;
}
int
handler(void *user, const char *section, const char *name, const char *value)
{
struct wob_config *config = (struct wob_config *) user;
unsigned long ul;
struct wob_color color;
if (strcmp(section, "") == 0) {
if (strcmp(name, "max") == 0) {
if (parse_number(value, &ul) == false || ul < 1 || ul > 10000) {
wob_log_error("Maximum must be a value between 1 and %lu.", 10000);
return 0;
}
config->max = ul;
return 1;
}
if (strcmp(name, "timeout") == 0) {
if (parse_number(value, &ul) == false || ul < 1 || ul > 10000) {
wob_log_error("Timeout must be a value between 1 and %lu.", 10000);
return 0;
}
config->timeout_msec = ul;
return 1;
}
if (strcmp(name, "width") == 0) {
if (parse_number(value, &ul) == false) {
wob_log_error("Width must be a positive value.");
return 0;
}
config->dimensions.width = ul;
return 1;
}
if (strcmp(name, "height") == 0) {
if (parse_number(value, &ul) == false) {
wob_log_error("Height must be a positive value.");
return 0;
}
config->dimensions.height = ul;
return 1;
}
if (strcmp(name, "border_offset") == 0) {
if (parse_number(value, &ul) == false) {
wob_log_error("Border offset must be a positive value.");
return 0;
}
config->dimensions.border_offset = ul;
return 1;
}
if (strcmp(name, "border_size") == 0) {
if (parse_number(value, &ul) == false) {
wob_log_error("Border size must be a positive value.");
return 0;
}
config->dimensions.border_size = ul;
return 1;
}
if (strcmp(name, "bar_padding") == 0) {
if (parse_number(value, &ul) == false) {
wob_log_error("Bar padding must be a positive value.");
return 0;
}
config->dimensions.bar_padding = ul;
return 1;
}
if (strcmp(name, "margin") == 0) {
if (parse_number(value, &ul) == false) {
wob_log_error("Anchor margin must be a positive value.");
return 0;
}
config->margin = ul;
return 1;
}
if (strcmp(name, "anchor") == 0) {
if (parse_anchor(value, &ul) == false) {
wob_log_error("Anchor must be one of 'top', 'bottom', 'left', 'right', 'center'.");
return 0;
}
config->anchor = ul;
return 1;
}
if (strcmp(name, "background_color") == 0) {
if (!parse_color(value, &color)) {
wob_log_error("Background color must be in RRGGBB[AA] format");
return 0;
}
config->colors.background = color;
return 1;
}
if (strcmp(name, "border_color") == 0) {
if (!parse_color(value, &color)) {
wob_log_error("Border color must be in RRGGBB[AA] format.");
return 0;
}
config->colors.border = color;
return 1;
}
if (strcmp(name, "bar_color") == 0) {
if (!parse_color(value, &color)) {
wob_log_error("Bar color must be in RRGGBB[AA] format.");
return 0;
}
config->colors.value = color;
return 1;
}
if (strcmp(name, "overflow_background_color") == 0) {
if (!parse_color(value, &color)) {
wob_log_error("Overflow background color must be in RRGGBB[AA] format.");
return 0;
}
config->overflow_colors.background = color;
return 1;
}
if (strcmp(name, "overflow_border_color") == 0) {
if (!parse_color(value, &color)) {
wob_log_error("Overflow border color must be in RRGGBB[AA] format.");
return 0;
}
config->overflow_colors.border = color;
return 1;
}
if (strcmp(name, "overflow_bar_color") == 0) {
if (!parse_color(value, &color)) {
wob_log_error("Overflow bar color must be in RRGGBB[AA] format.");
return 0;
}
config->overflow_colors.value = color;
return 1;
}
if (strcmp(name, "overflow_mode") == 0) {
if (parse_overflow_mode(value, &config->overflow_mode) == false) {
wob_log_error("Invalid argument for overflow-mode. Valid options are wrap and nowrap.");
return 0;
}
return 1;
}
if (strcmp(name, "output_mode") == 0) {
if (parse_output_mode(value, &config->output_mode) == false) {
wob_log_error("Invalid argument for output_mode. Valid options are focused, whitelist and all.");
return 0;
}
return 1;
}
wob_log_error("Unknown config key %s", name);
return 0;
}
if (strncmp(section, "output.", sizeof("output.") - 1) == 0) {
if (strcmp(name, "name") == 0) {
struct wob_output_config *output_config = malloc(sizeof(struct wob_output_config));
output_config->name = strdup(value);
wl_list_insert(&config->outputs, &output_config->link);
return 1;
}
wob_log_error("Unknown config key %s", name);
return 0;
}
wob_log_error("Unknown config section %s", section);
return 0;
}
char *
wob_config_default_path()
{
static const char *config_paths[] = {
"$XDG_CONFIG_HOME/wob/wob.ini",
"$HOME/.config/wob/wob.ini",
};
for (size_t i = 0; i < sizeof(config_paths) / sizeof(char *); ++i) {
wob_log_debug("Looking for config file at %s", config_paths[i]);
wordexp_t p;
if (wordexp(config_paths[i], &p, WRDE_UNDEF) == 0) {
char *path = strdup(p.we_wordv[0]);
wordfree(&p);
if (access(path, F_OK) == 0) {
wob_log_info("Found configuration file at %s", config_paths[i]);
return path;
}
wob_log_debug("Configuration file at %s not found", config_paths[i]);
free(path);
}
}
return NULL;
}
void
wob_config_init(struct wob_config *config)
{
wl_list_init(&config->outputs);
config->max = 100;
config->timeout_msec = 1000;
config->dimensions.width = 400;
config->dimensions.height = 50;
config->dimensions.border_offset = 4;
config->dimensions.border_size = 4;
config->dimensions.bar_padding = 4;
config->margin = 0;
config->anchor = WOB_ANCHOR_CENTER;
config->overflow_mode = WOB_OVERFLOW_MODE_WRAP;
config->output_mode = WOB_OUTPUT_MODE_FOCUSED;
config->colors.background = (struct wob_color){.a = 1.0f, .r = 0.0f, .g = 0.0f, .b = 0.0f};
config->colors.value = (struct wob_color){.a = 1.0f, .r = 1.0f, .g = 1.0f, .b = 1.0f};
config->colors.border = (struct wob_color){.a = 1.0f, .r = 1.0f, .g = 1.0f, .b = 1.0f};
config->overflow_colors.background = (struct wob_color){.a = 1.0f, .r = 0.0f, .g = 0.0f, .b = 0.0f};
config->overflow_colors.value = (struct wob_color){.a = 1.0f, .r = 1.0f, .g = 0.0f, .b = 0.0f};
config->overflow_colors.border = (struct wob_color){.a = 1.0f, .r = 1.0f, .g = 1.0f, .b = 1.0f};
}
bool
wob_config_load(struct wob_config *config, const char *config_path)
{
int res = ini_parse(config_path, handler, config);
if (res == -1 || res == -2) {
wob_log_error("Failed to open config file %s", config_path);
return false;
}
if (res != 0) {
wob_log_error("Failed to parse config file %s, error at line %d", config_path, res);
return false;
}
struct wob_dimensions dimensions = config->dimensions;
if (dimensions.width < MIN_PERCENTAGE_BAR_WIDTH + 2 * (dimensions.border_offset + dimensions.border_size + dimensions.bar_padding)) {
wob_log_error("Invalid geometry: width is too small for given parameters");
return false;
}
if (dimensions.height < MIN_PERCENTAGE_BAR_HEIGHT + 2 * (dimensions.border_offset + dimensions.border_size + dimensions.bar_padding)) {
wob_log_error("Invalid geometry: height is too small for given parameters");
return false;
}
return true;
}
void
wob_config_debug(struct wob_config *config)
{
wob_log_debug("config.max = %lu", config->max);
wob_log_debug("config.timeout_msec = %lu", config->timeout_msec);
wob_log_debug("config.dimensions.width = %lu", config->dimensions.width);
wob_log_debug("config.dimensions.height = %lu", config->dimensions.height);
wob_log_debug("config.dimensions.border_offset = %lu", config->dimensions.border_offset);
wob_log_debug("config.dimensions.border_size = %lu", config->dimensions.border_size);
wob_log_debug("config.dimensions.bar_padding = %lu", config->dimensions.bar_padding);
wob_log_debug("config.margin = %lu", config->margin);
wob_log_debug("config.anchor = %lu (top = %d, bottom = %d, left = %d, right = %d)", config->anchor, WOB_ANCHOR_TOP, WOB_ANCHOR_BOTTOM, WOB_ANCHOR_LEFT, WOB_ANCHOR_RIGHT);
wob_log_debug("config.overflow_mode = %lu (wrap = %d, nowrap = %d)", config->overflow_mode, WOB_OVERFLOW_MODE_WRAP, WOB_OVERFLOW_MODE_NOWRAP);
wob_log_debug("config.output_mode = %lu (whitelist = %d, all = %d, focused = %d)", config->output_mode, WOB_OUTPUT_MODE_WHITELIST, WOB_OUTPUT_MODE_ALL, WOB_OUTPUT_MODE_FOCUSED);
wob_log_debug("config.colors.background = #%08jx", (uintmax_t) wob_color_to_rgba(config->colors.background));
wob_log_debug("config.colors.value = #%08jx", (uintmax_t) wob_color_to_rgba(config->colors.value));
wob_log_debug("config.colors.border = #%08jx", (uintmax_t) wob_color_to_rgba(config->colors.border));
wob_log_debug("config.overflow_colors.background = #%08jx", (uintmax_t) wob_color_to_rgba(config->overflow_colors.background));
wob_log_debug("config.overflow_colors.value = #%08jx", (uintmax_t) wob_color_to_rgba(config->overflow_colors.value));
wob_log_debug("config.overflow_colors.border = #%08jx", (uintmax_t) wob_color_to_rgba(config->overflow_colors.border));
struct wob_output_config *output_config;
wl_list_for_each (output_config, &config->outputs, link) {
wob_log_debug("config.output.%s.name = %s", output_config->name, output_config->name);
}
}
void
wob_config_destroy(struct wob_config *config)
{
struct wob_output_config *output, *output_tmp;
wl_list_for_each_safe (output, output_tmp, &config->outputs, link) {
free(output->name);
free(output);
}
}