Debian Codegraph @

i3bar/src/config.c

raw
1 /*
2 * vim:ts=4:sw=4:expandtab
3 *
4 * i3bar - an xcb-based status- and ws-bar for i3
5 * © 2010 Axel Wagner and contributors (see also: LICENSE)
6 *
7 * config.c: Parses the configuration (received from i3).
8 *
9 */
10 #include "common.h"
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include <yajl/yajl_parse.h>
17
18 config_t config;
19 static char *cur_key;
20 static bool parsing_bindings;
21 static bool parsing_tray_outputs;
22 static bool parsing_padding;
23
24 /*
25 * Parse a key.
26 *
27 * Essentially we just save it in cur_key.
28 *
29 */
30 static int config_map_key_cb(void *params_, const unsigned char *keyVal, size_t keyLen) {
31 FREE(cur_key);
32 sasprintf(&(cur_key), "%.*s", (int)keyLen, keyVal);
33
34 if (strcmp(cur_key, "bindings") == 0) {
35 parsing_bindings = true;
36 }
37
38 if (strcmp(cur_key, "tray_outputs") == 0) {
39 parsing_tray_outputs = true;
40 }
41
42 if (strcmp(cur_key, "padding") == 0) {
43 parsing_padding = true;
44 }
45
46 return 1;
47 }
48
49 static int config_end_array_cb(void *params_) {
50 parsing_bindings = false;
51 parsing_tray_outputs = false;
52 parsing_padding = false;
53 return 1;
54 }
55
56 /*
57 * Parse a null value (current_workspace)
58 *
59 */
60 static int config_null_cb(void *params_) {
61 if (!strcmp(cur_key, "id")) {
62 /* If 'id' is NULL, the bar config was not found. Error out. */
63 ELOG("No such bar config. Use 'i3-msg -t get_bar_config' to get the available configs.\n");
64 ELOG("Are you starting i3bar by hand? You should not:\n");
65 ELOG("Configure a 'bar' block in your i3 config and i3 will launch i3bar automatically.\n");
66 exit(EXIT_FAILURE);
67 }
68
69 return 1;
70 }
71
72 /*
73 * Parse a string
74 *
75 */
76 static int config_string_cb(void *params_, const unsigned char *val, size_t _len) {
77 int len = (int)_len;
78 /* The id and socket_path are ignored, we already know them. */
79 if (!strcmp(cur_key, "id") || !strcmp(cur_key, "socket_path")) {
80 return 1;
81 }
82
83 if (parsing_bindings) {
84 if (strcmp(cur_key, "command") == 0) {
85 binding_t *binding = TAILQ_LAST(&(config.bindings), bindings_head);
86 if (binding == NULL) {
87 ELOG("There is no binding to put the current command onto. This is a bug in i3.\n");
88 return 0;
89 }
90
91 if (binding->command != NULL) {
92 ELOG("The binding for input_code = %d already has a command. This is a bug in i3.\n", binding->input_code);
93 return 0;
94 }
95
96 sasprintf(&(binding->command), "%.*s", len, val);
97 return 1;
98 }
99
100 ELOG("Unknown key \"%s\" while parsing bar bindings.\n", cur_key);
101 return 0;
102 }
103
104 if (parsing_tray_outputs) {
105 DLOG("Adding tray_output = %.*s to the list.\n", len, val);
106 tray_output_t *tray_output = scalloc(1, sizeof(tray_output_t));
107 sasprintf(&(tray_output->output), "%.*s", len, val);
108 TAILQ_INSERT_TAIL(&(config.tray_outputs), tray_output, tray_outputs);
109 return 1;
110 }
111
112 if (!strcmp(cur_key, "mode")) {
113 DLOG("mode = %.*s, len = %d\n", len, val, len);
114 config.hide_on_modifier = (len == strlen("dock") && !strncmp((const char *)val, "dock", strlen("dock")) ? M_DOCK
115 : (len == strlen("hide") && !strncmp((const char *)val, "hide", strlen("hide")) ? M_HIDE
116 : M_INVISIBLE));
117 return 1;
118 }
119
120 if (!strcmp(cur_key, "hidden_state")) {
121 DLOG("hidden_state = %.*s, len = %d\n", len, val, len);
122 config.hidden_state = (len == strlen("hide") && !strncmp((const char *)val, "hide", strlen("hide")) ? S_HIDE : S_SHOW);
123 return 1;
124 }
125
126 /* Kept for backwards compatibility. */
127 if (!strcmp(cur_key, "modifier")) {
128 DLOG("modifier = %.*s\n", len, val);
129 if (len == strlen("none") && !strncmp((const char *)val, "none", strlen("none"))) {
130 config.modifier = XCB_NONE;
131 return 1;
132 }
133
134 if (len == strlen("shift") && !strncmp((const char *)val, "shift", strlen("shift"))) {
135 config.modifier = XCB_MOD_MASK_SHIFT;
136 return 1;
137 }
138 if (len == strlen("ctrl") && !strncmp((const char *)val, "ctrl", strlen("ctrl"))) {
139 config.modifier = XCB_MOD_MASK_CONTROL;
140 return 1;
141 }
142 if (len == strlen("Mod") + 1 && !strncmp((const char *)val, "Mod", strlen("Mod"))) {
143 switch (val[3]) {
144 case '1':
145 config.modifier = XCB_MOD_MASK_1;
146 return 1;
147 case '2':
148 config.modifier = XCB_MOD_MASK_2;
149 return 1;
150 case '3':
151 config.modifier = XCB_MOD_MASK_3;
152 return 1;
153 case '5':
154 config.modifier = XCB_MOD_MASK_5;
155 return 1;
156 }
157 }
158
159 config.modifier = XCB_MOD_MASK_4;
160 return 1;
161 }
162
163 /* This key was sent in <= 4.10.2. We keep it around to avoid breakage for
164 * users updating from that version and restarting i3bar before i3. */
165 if (!strcmp(cur_key, "wheel_up_cmd")) {
166 DLOG("wheel_up_cmd = %.*s\n", len, val);
167 binding_t *binding = scalloc(1, sizeof(binding_t));
168 binding->input_code = 4;
169 sasprintf(&(binding->command), "%.*s", len, val);
170 TAILQ_INSERT_TAIL(&(config.bindings), binding, bindings);
171 return 1;
172 }
173
174 /* This key was sent in <= 4.10.2. We keep it around to avoid breakage for
175 * users updating from that version and restarting i3bar before i3. */
176 if (!strcmp(cur_key, "wheel_down_cmd")) {
177 DLOG("wheel_down_cmd = %.*s\n", len, val);
178 binding_t *binding = scalloc(1, sizeof(binding_t));
179 binding->input_code = 5;
180 sasprintf(&(binding->command), "%.*s", len, val);
181 TAILQ_INSERT_TAIL(&(config.bindings), binding, bindings);
182 return 1;
183 }
184
185 if (!strcmp(cur_key, "position")) {
186 DLOG("position = %.*s\n", len, val);
187 config.position = (len == strlen("top") && !strncmp((const char *)val, "top", strlen("top")) ? POS_TOP : POS_BOT);
188 return 1;
189 }
190
191 if (!strcmp(cur_key, "status_command")) {
192 DLOG("status_command = %.*s\n", len, val);
193 sasprintf(&config.command, "%.*s", len, val);
194 return 1;
195 }
196
197 if (!strcmp(cur_key, "workspace_command")) {
198 DLOG("workspace_command = %.*s\n", len, val);
199 sasprintf(&config.workspace_command, "%.*s", len, val);
200 return 1;
201 }
202
203 if (!strcmp(cur_key, "font")) {
204 DLOG("font = %.*s\n", len, val);
205 FREE(config.fontname);
206 sasprintf(&config.fontname, "%.*s", len, val);
207 return 1;
208 }
209
210 if (!strcmp(cur_key, "separator_symbol")) {
211 DLOG("separator = %.*s\n", len, val);
212 I3STRING_FREE(config.separator_symbol);
213 config.separator_symbol = i3string_from_utf8_with_length((const char *)val, len);
214 return 1;
215 }
216
217 if (!strcmp(cur_key, "outputs")) {
218 DLOG("+output %.*s\n", len, val);
219 int new_num_outputs = config.num_outputs + 1;
220 config.outputs = srealloc(config.outputs, sizeof(char *) * new_num_outputs);
221 sasprintf(&config.outputs[config.num_outputs], "%.*s", len, val);
222 config.num_outputs = new_num_outputs;
223 return 1;
224 }
225
226 /* We keep the old single tray_output working for users who only restart i3bar
227 * after updating. */
228 if (!strcmp(cur_key, "tray_output")) {
229 DLOG("Found deprecated key tray_output %.*s.\n", len, val);
230 tray_output_t *tray_output = scalloc(1, sizeof(tray_output_t));
231 sasprintf(&(tray_output->output), "%.*s", len, val);
232 TAILQ_INSERT_TAIL(&(config.tray_outputs), tray_output, tray_outputs);
233 return 1;
234 }
235
236 #define COLOR(json_name, struct_name) \
237 do { \
238 if (!strcmp(cur_key, #json_name)) { \
239 DLOG(#json_name " = " #struct_name " = %.*s\n", len, val); \
240 sasprintf(&(config.colors.struct_name), "%.*s", len, val); \
241 return 1; \
242 } \
243 } while (0)
244
245 COLOR(statusline, bar_fg);
246 COLOR(background, bar_bg);
247 COLOR(separator, sep_fg);
248 COLOR(focused_statusline, focus_bar_fg);
249 COLOR(focused_background, focus_bar_bg);
250 COLOR(focused_separator, focus_sep_fg);
251 COLOR(focused_workspace_border, focus_ws_border);
252 COLOR(focused_workspace_bg, focus_ws_bg);
253 COLOR(focused_workspace_text, focus_ws_fg);
254 COLOR(active_workspace_border, active_ws_border);
255 COLOR(active_workspace_bg, active_ws_bg);
256 COLOR(active_workspace_text, active_ws_fg);
257 COLOR(inactive_workspace_border, inactive_ws_border);
258 COLOR(inactive_workspace_bg, inactive_ws_bg);
259 COLOR(inactive_workspace_text, inactive_ws_fg);
260 COLOR(urgent_workspace_border, urgent_ws_border);
261 COLOR(urgent_workspace_bg, urgent_ws_bg);
262 COLOR(urgent_workspace_text, urgent_ws_fg);
263 COLOR(binding_mode_border, binding_mode_border);
264 COLOR(binding_mode_bg, binding_mode_bg);
265 COLOR(binding_mode_text, binding_mode_fg);
266
267 printf("got unexpected string %.*s for cur_key = %s\n", len, val, cur_key);
268
269 return 0;
270 }
271
272 /*
273 * Parse a boolean value
274 *
275 */
276 static int config_boolean_cb(void *params_, int val) {
277 if (parsing_bindings) {
278 if (strcmp(cur_key, "release") == 0) {
279 binding_t *binding = TAILQ_LAST(&(config.bindings), bindings_head);
280 if (binding == NULL) {
281 ELOG("There is no binding to put the current command onto. This is a bug in i3.\n");
282 return 0;
283 }
284
285 binding->release = val;
286 return 1;
287 }
288
289 ELOG("Unknown key \"%s\" while parsing bar bindings.\n", cur_key);
290 }
291
292 if (!strcmp(cur_key, "binding_mode_indicator")) {
293 DLOG("binding_mode_indicator = %d\n", val);
294 config.disable_binding_mode_indicator = !val;
295 return 1;
296 }
297
298 if (!strcmp(cur_key, "workspace_buttons")) {
299 DLOG("workspace_buttons = %d\n", val);
300 config.disable_ws = !val;
301 return 1;
302 }
303
304 if (!strcmp(cur_key, "strip_workspace_numbers")) {
305 DLOG("strip_workspace_numbers = %d\n", val);
306 config.strip_ws_numbers = val;
307 return 1;
308 }
309
310 if (!strcmp(cur_key, "strip_workspace_name")) {
311 DLOG("strip_workspace_name = %d\n", val);
312 config.strip_ws_name = val;
313 return 1;
314 }
315
316 if (!strcmp(cur_key, "verbose")) {
317 if (!config.verbose) {
318 DLOG("verbose = %d\n", val);
319 config.verbose = val;
320 }
321 return 1;
322 }
323
324 return 0;
325 }
326
327 /*
328 * Parse an integer value
329 *
330 */
331 static int config_integer_cb(void *params_, long long val) {
332 if (parsing_bindings) {
333 if (strcmp(cur_key, "input_code") == 0) {
334 binding_t *binding = scalloc(1, sizeof(binding_t));
335 binding->input_code = val;
336 TAILQ_INSERT_TAIL(&(config.bindings), binding, bindings);
337
338 return 1;
339 }
340
341 ELOG("Unknown key \"%s\" while parsing bar bindings.\n", cur_key);
342 return 0;
343 }
344
345 if (parsing_padding) {
346 if (strcmp(cur_key, "x") == 0) {
347 DLOG("padding.x = %lld\n", val);
348 config.padding.x = (uint32_t)val;
349 return 1;
350 }
351 if (strcmp(cur_key, "y") == 0) {
352 DLOG("padding.y = %lld\n", val);
353 config.padding.y = (uint32_t)val;
354 return 1;
355 }
356 if (strcmp(cur_key, "width") == 0) {
357 DLOG("padding.width = %lld\n", val);
358 config.padding.width = (uint32_t)val;
359 return 1;
360 }
361 if (strcmp(cur_key, "height") == 0) {
362 DLOG("padding.height = %lld\n", val);
363 config.padding.height = (uint32_t)val;
364 return 1;
365 }
366 }
367
368 if (!strcmp(cur_key, "bar_height")) {
369 DLOG("bar_height = %lld\n", val);
370 config.bar_height = (uint32_t)val;
371 return 1;
372 }
373
374 if (!strcmp(cur_key, "tray_padding")) {
375 DLOG("tray_padding = %lld\n", val);
376 config.tray_padding = val;
377 return 1;
378 }
379
380 if (!strcmp(cur_key, "modifier")) {
381 DLOG("modifier = %lld\n", val);
382 config.modifier = (uint32_t)val;
383 return 1;
384 }
385
386 if (!strcmp(cur_key, "workspace_min_width")) {
387 DLOG("workspace_min_width = %lld\n", val);
388 config.ws_min_width = val;
389 return 1;
390 }
391
392 return 0;
393 }
394
395 /* A datastructure to pass all these callbacks to yajl */
396 static yajl_callbacks outputs_callbacks = {
397 .yajl_null = config_null_cb,
398 .yajl_integer = config_integer_cb,
399 .yajl_boolean = config_boolean_cb,
400 .yajl_string = config_string_cb,
401 .yajl_end_array = config_end_array_cb,
402 .yajl_map_key = config_map_key_cb,
403 };
404
405 /*
406 * Parse the received bar configuration JSON string
407 *
408 */
409 void parse_config_json(const unsigned char *json, size_t size) {
410 TAILQ_INIT(&(config.bindings));
411 TAILQ_INIT(&(config.tray_outputs));
412
413 yajl_handle handle = yajl_alloc(&outputs_callbacks, NULL, NULL);
414 yajl_status state = yajl_parse(handle, json, size);
415
416 /* FIXME: Proper error handling for JSON parsing */
417 switch (state) {
418 case yajl_status_ok:
419 break;
420 case yajl_status_client_canceled:
421 case yajl_status_error:
422 ELOG("Could not parse config reply!\n");
423 exit(EXIT_FAILURE);
424 break;
425 }
426
427 if (config.disable_ws && config.workspace_command) {
428 ELOG("You have specified 'workspace_buttons no'. Your 'workspace_command %s' will be ignored.\n", config.workspace_command);
429 FREE(config.workspace_command);
430 }
431
432 yajl_free(handle);
433 }
434
435 static int i3bar_config_string_cb(void *params_, const unsigned char *val, size_t _len) {
436 sasprintf(&config.bar_id, "%.*s", (int)_len, val);
437 return 0; /* Stop parsing */
438 }
439
440 /*
441 * Parse the received bar configuration list. The only usecase right now is to
442 * automatically get the first bar id.
443 *
444 */
445 void parse_get_first_i3bar_config(const unsigned char *json, size_t size) {
446 yajl_callbacks configs_callbacks = {
447 .yajl_string = i3bar_config_string_cb,
448 };
449 yajl_handle handle = yajl_alloc(&configs_callbacks, NULL, NULL);
450 yajl_parse(handle, json, size);
451 yajl_free(handle);
452 }
453
454 /*
455 * free()s the color strings as soon as they are not needed anymore.
456 *
457 */
458 void free_colors(struct xcb_color_strings_t *colors) {
459 #define FREE_COLOR(x) \
460 do { \
461 FREE(colors->x); \
462 } while (0)
463 FREE_COLOR(bar_fg);
464 FREE_COLOR(bar_bg);
465 FREE_COLOR(sep_fg);
466 FREE_COLOR(focus_bar_fg);
467 FREE_COLOR(focus_bar_bg);
468 FREE_COLOR(focus_sep_fg);
469 FREE_COLOR(active_ws_fg);
470 FREE_COLOR(active_ws_bg);
471 FREE_COLOR(active_ws_border);
472 FREE_COLOR(inactive_ws_fg);
473 FREE_COLOR(inactive_ws_bg);
474 FREE_COLOR(inactive_ws_border);
475 FREE_COLOR(urgent_ws_fg);
476 FREE_COLOR(urgent_ws_bg);
477 FREE_COLOR(urgent_ws_border);
478 FREE_COLOR(focus_ws_fg);
479 FREE_COLOR(focus_ws_bg);
480 FREE_COLOR(focus_ws_border);
481 FREE_COLOR(binding_mode_fg);
482 FREE_COLOR(binding_mode_bg);
483 FREE_COLOR(binding_mode_border);
484 #undef FREE_COLOR
485 }