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