]> git.sur5r.net Git - i3/i3/blob - i3bar/src/config.c
Merge pull request #3065 from orestisf1993/issue-3064
[i3/i3] / i3bar / src / config.c
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 <string.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <errno.h>
16 #include <i3/ipc.h>
17 #include <yajl/yajl_parse.h>
18 #include <yajl/yajl_version.h>
19
20 #include <X11/Xlib.h>
21
22 static char *cur_key;
23 static bool parsing_bindings;
24 static bool parsing_tray_outputs;
25
26 /*
27  * Parse a key.
28  *
29  * Essentially we just save it in cur_key.
30  *
31  */
32 static int config_map_key_cb(void *params_, const unsigned char *keyVal, size_t keyLen) {
33     FREE(cur_key);
34     sasprintf(&(cur_key), "%.*s", keyLen, keyVal);
35
36     if (strcmp(cur_key, "bindings") == 0) {
37         parsing_bindings = true;
38     }
39
40     if (strcmp(cur_key, "tray_outputs") == 0) {
41         parsing_tray_outputs = true;
42     }
43
44     return 1;
45 }
46
47 static int config_end_array_cb(void *params_) {
48     parsing_bindings = false;
49     parsing_tray_outputs = false;
50     return 1;
51 }
52
53 /*
54  * Parse a null value (current_workspace)
55  *
56  */
57 static int config_null_cb(void *params_) {
58     if (!strcmp(cur_key, "id")) {
59         /* If 'id' is NULL, the bar config was not found. Error out. */
60         ELOG("No such bar config. Use 'i3-msg -t get_bar_config' to get the available configs.\n");
61         ELOG("Are you starting i3bar by hand? You should not:\n");
62         ELOG("Configure a 'bar' block in your i3 config and i3 will launch i3bar automatically.\n");
63         exit(EXIT_FAILURE);
64     }
65
66     return 1;
67 }
68
69 /*
70  * Parse a string
71  *
72  */
73 static int config_string_cb(void *params_, const unsigned char *val, size_t _len) {
74     int len = (int)_len;
75     /* The id and socket_path are ignored, we already know them. */
76     if (!strcmp(cur_key, "id") || !strcmp(cur_key, "socket_path"))
77         return 1;
78
79     if (parsing_bindings) {
80         if (strcmp(cur_key, "command") == 0) {
81             binding_t *binding = TAILQ_LAST(&(config.bindings), bindings_head);
82             if (binding == NULL) {
83                 ELOG("There is no binding to put the current command onto. This is a bug in i3.\n");
84                 return 0;
85             }
86
87             if (binding->command != NULL) {
88                 ELOG("The binding for input_code = %d already has a command. This is a bug in i3.\n", binding->input_code);
89                 return 0;
90             }
91
92             sasprintf(&(binding->command), "%.*s", len, val);
93             return 1;
94         }
95
96         ELOG("Unknown key \"%s\" while parsing bar bindings.\n", cur_key);
97         return 0;
98     }
99
100     if (parsing_tray_outputs) {
101         DLOG("Adding tray_output = %.*s to the list.\n", len, val);
102         tray_output_t *tray_output = scalloc(1, sizeof(tray_output_t));
103         sasprintf(&(tray_output->output), "%.*s", len, val);
104         TAILQ_INSERT_TAIL(&(config.tray_outputs), tray_output, tray_outputs);
105         return 1;
106     }
107
108     if (!strcmp(cur_key, "mode")) {
109         DLOG("mode = %.*s, len = %d\n", len, val, len);
110         config.hide_on_modifier = (len == strlen("dock") && !strncmp((const char *)val, "dock", strlen("dock")) ? M_DOCK
111                                                                                                                 : (len == strlen("hide") && !strncmp((const char *)val, "hide", strlen("hide")) ? M_HIDE
112                                                                                                                                                                                                 : M_INVISIBLE));
113         return 1;
114     }
115
116     if (!strcmp(cur_key, "hidden_state")) {
117         DLOG("hidden_state = %.*s, len = %d\n", len, val, len);
118         config.hidden_state = (len == strlen("hide") && !strncmp((const char *)val, "hide", strlen("hide")) ? S_HIDE : S_SHOW);
119         return 1;
120     }
121
122     if (!strcmp(cur_key, "modifier")) {
123         DLOG("modifier = %.*s\n", len, val);
124         if (len == strlen("none") && !strncmp((const char *)val, "none", strlen("none"))) {
125             config.modifier = XCB_NONE;
126             return 1;
127         }
128
129         if (len == strlen("shift") && !strncmp((const char *)val, "shift", strlen("shift"))) {
130             config.modifier = ShiftMask;
131             return 1;
132         }
133         if (len == strlen("ctrl") && !strncmp((const char *)val, "ctrl", strlen("ctrl"))) {
134             config.modifier = ControlMask;
135             return 1;
136         }
137         if (len == strlen("Mod") + 1 && !strncmp((const char *)val, "Mod", strlen("Mod"))) {
138             switch (val[3]) {
139                 case '1':
140                     config.modifier = Mod1Mask;
141                     return 1;
142                 case '2':
143                     config.modifier = Mod2Mask;
144                     return 1;
145                 case '3':
146                     config.modifier = Mod3Mask;
147                     return 1;
148                 case '5':
149                     config.modifier = Mod5Mask;
150                     return 1;
151             }
152         }
153
154         config.modifier = Mod4Mask;
155         return 1;
156     }
157
158     /* This key was sent in <= 4.10.2. We keep it around to avoid breakage for
159      * users updating from that version and restarting i3bar before i3. */
160     if (!strcmp(cur_key, "wheel_up_cmd")) {
161         DLOG("wheel_up_cmd = %.*s\n", len, val);
162         binding_t *binding = scalloc(1, sizeof(binding_t));
163         binding->input_code = 4;
164         sasprintf(&(binding->command), "%.*s", len, val);
165         TAILQ_INSERT_TAIL(&(config.bindings), binding, bindings);
166         return 1;
167     }
168
169     /* This key was sent in <= 4.10.2. We keep it around to avoid breakage for
170      * users updating from that version and restarting i3bar before i3. */
171     if (!strcmp(cur_key, "wheel_down_cmd")) {
172         DLOG("wheel_down_cmd = %.*s\n", len, val);
173         binding_t *binding = scalloc(1, sizeof(binding_t));
174         binding->input_code = 5;
175         sasprintf(&(binding->command), "%.*s", len, val);
176         TAILQ_INSERT_TAIL(&(config.bindings), binding, bindings);
177         return 1;
178     }
179
180     if (!strcmp(cur_key, "position")) {
181         DLOG("position = %.*s\n", len, val);
182         config.position = (len == strlen("top") && !strncmp((const char *)val, "top", strlen("top")) ? POS_TOP : POS_BOT);
183         return 1;
184     }
185
186     if (!strcmp(cur_key, "status_command")) {
187         DLOG("command = %.*s\n", len, val);
188         FREE(config.command);
189         sasprintf(&config.command, "%.*s", len, val);
190         return 1;
191     }
192
193     if (!strcmp(cur_key, "font")) {
194         DLOG("font = %.*s\n", len, val);
195         sasprintf(&config.fontname, "%.*s", len, val);
196         return 1;
197     }
198
199     if (!strcmp(cur_key, "separator_symbol")) {
200         DLOG("separator = %.*s\n", len, val);
201         I3STRING_FREE(config.separator_symbol);
202         config.separator_symbol = i3string_from_utf8_with_length((const char *)val, len);
203         return 1;
204     }
205
206     if (!strcmp(cur_key, "outputs")) {
207         DLOG("+output %.*s\n", len, val);
208         int new_num_outputs = config.num_outputs + 1;
209         config.outputs = srealloc(config.outputs, sizeof(char *) * new_num_outputs);
210         sasprintf(&config.outputs[config.num_outputs], "%.*s", len, val);
211         config.num_outputs = new_num_outputs;
212         return 1;
213     }
214
215     /* We keep the old single tray_output working for users who only restart i3bar
216      * after updating. */
217     if (!strcmp(cur_key, "tray_output")) {
218         DLOG("Found deprecated key tray_output %.*s.\n", len, val);
219         tray_output_t *tray_output = scalloc(1, sizeof(tray_output_t));
220         sasprintf(&(tray_output->output), "%.*s", len, val);
221         TAILQ_INSERT_TAIL(&(config.tray_outputs), tray_output, tray_outputs);
222         return 1;
223     }
224
225 #define COLOR(json_name, struct_name)                                  \
226     do {                                                               \
227         if (!strcmp(cur_key, #json_name)) {                            \
228             DLOG(#json_name " = " #struct_name " = %.*s\n", len, val); \
229             sasprintf(&(config.colors.struct_name), "%.*s", len, val); \
230             return 1;                                                  \
231         }                                                              \
232     } while (0)
233
234     COLOR(statusline, bar_fg);
235     COLOR(background, bar_bg);
236     COLOR(separator, sep_fg);
237     COLOR(focused_statusline, focus_bar_fg);
238     COLOR(focused_background, focus_bar_bg);
239     COLOR(focused_separator, focus_sep_fg);
240     COLOR(focused_workspace_border, focus_ws_border);
241     COLOR(focused_workspace_bg, focus_ws_bg);
242     COLOR(focused_workspace_text, focus_ws_fg);
243     COLOR(active_workspace_border, active_ws_border);
244     COLOR(active_workspace_bg, active_ws_bg);
245     COLOR(active_workspace_text, active_ws_fg);
246     COLOR(inactive_workspace_border, inactive_ws_border);
247     COLOR(inactive_workspace_bg, inactive_ws_bg);
248     COLOR(inactive_workspace_text, inactive_ws_fg);
249     COLOR(urgent_workspace_border, urgent_ws_border);
250     COLOR(urgent_workspace_bg, urgent_ws_bg);
251     COLOR(urgent_workspace_text, urgent_ws_fg);
252     COLOR(binding_mode_border, binding_mode_border);
253     COLOR(binding_mode_bg, binding_mode_bg);
254     COLOR(binding_mode_text, binding_mode_fg);
255
256     printf("got unexpected string %.*s for cur_key = %s\n", len, val, cur_key);
257
258     return 0;
259 }
260
261 /*
262  * Parse a boolean value
263  *
264  */
265 static int config_boolean_cb(void *params_, int val) {
266     if (!strcmp(cur_key, "binding_mode_indicator")) {
267         DLOG("binding_mode_indicator = %d\n", val);
268         config.disable_binding_mode_indicator = !val;
269         return 1;
270     }
271
272     if (!strcmp(cur_key, "workspace_buttons")) {
273         DLOG("workspace_buttons = %d\n", val);
274         config.disable_ws = !val;
275         return 1;
276     }
277
278     if (!strcmp(cur_key, "strip_workspace_numbers")) {
279         DLOG("strip_workspace_numbers = %d\n", val);
280         config.strip_ws_numbers = val;
281         return 1;
282     }
283
284     if (!strcmp(cur_key, "verbose")) {
285         DLOG("verbose = %d\n", val);
286         config.verbose = val;
287         return 1;
288     }
289
290     return 0;
291 }
292
293 /*
294  * Parse an integer value
295  *
296  */
297 static int config_integer_cb(void *params_, long long val) {
298     if (parsing_bindings) {
299         if (strcmp(cur_key, "input_code") == 0) {
300             binding_t *binding = scalloc(1, sizeof(binding_t));
301             binding->input_code = val;
302             TAILQ_INSERT_TAIL(&(config.bindings), binding, bindings);
303
304             return 1;
305         }
306
307         ELOG("Unknown key \"%s\" while parsing bar bindings.\n", cur_key);
308         return 0;
309     }
310
311     if (!strcmp(cur_key, "tray_padding")) {
312         DLOG("tray_padding = %lld\n", val);
313         config.tray_padding = val;
314         return 1;
315     }
316
317     return 0;
318 }
319
320 /* A datastructure to pass all these callbacks to yajl */
321 static yajl_callbacks outputs_callbacks = {
322     .yajl_null = config_null_cb,
323     .yajl_boolean = config_boolean_cb,
324     .yajl_integer = config_integer_cb,
325     .yajl_string = config_string_cb,
326     .yajl_end_array = config_end_array_cb,
327     .yajl_map_key = config_map_key_cb,
328 };
329
330 /*
331  * Start parsing the received bar configuration JSON string
332  *
333  */
334 void parse_config_json(char *json) {
335     yajl_handle handle;
336     yajl_status state;
337     handle = yajl_alloc(&outputs_callbacks, NULL, NULL);
338
339     TAILQ_INIT(&(config.bindings));
340     TAILQ_INIT(&(config.tray_outputs));
341
342     state = yajl_parse(handle, (const unsigned char *)json, strlen(json));
343
344     /* FIXME: Proper error handling for JSON parsing */
345     switch (state) {
346         case yajl_status_ok:
347             break;
348         case yajl_status_client_canceled:
349         case yajl_status_error:
350             ELOG("Could not parse config reply!\n");
351             exit(EXIT_FAILURE);
352             break;
353     }
354
355     yajl_free(handle);
356 }
357
358 /*
359  * free()s the color strings as soon as they are not needed anymore.
360  *
361  */
362 void free_colors(struct xcb_color_strings_t *colors) {
363 #define FREE_COLOR(x)    \
364     do {                 \
365         FREE(colors->x); \
366     } while (0)
367     FREE_COLOR(bar_fg);
368     FREE_COLOR(bar_bg);
369     FREE_COLOR(sep_fg);
370     FREE_COLOR(focus_bar_fg);
371     FREE_COLOR(focus_bar_bg);
372     FREE_COLOR(focus_sep_fg);
373     FREE_COLOR(active_ws_fg);
374     FREE_COLOR(active_ws_bg);
375     FREE_COLOR(active_ws_border);
376     FREE_COLOR(inactive_ws_fg);
377     FREE_COLOR(inactive_ws_bg);
378     FREE_COLOR(inactive_ws_border);
379     FREE_COLOR(urgent_ws_fg);
380     FREE_COLOR(urgent_ws_bg);
381     FREE_COLOR(urgent_ws_border);
382     FREE_COLOR(focus_ws_fg);
383     FREE_COLOR(focus_ws_bg);
384     FREE_COLOR(focus_ws_border);
385     FREE_COLOR(binding_mode_fg);
386     FREE_COLOR(binding_mode_bg);
387     FREE_COLOR(binding_mode_border);
388 #undef FREE_COLOR
389 }