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