]> git.sur5r.net Git - i3/i3/blob - i3bar/src/config.c
Merge branch 'fix-urgent-crash'
[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-2011 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
24 /*
25  * Parse a key.
26  *
27  * Essentially we just save it in cur_key.
28  *
29  */
30 #if YAJL_MAJOR >= 2
31 static int config_map_key_cb(void *params_, const unsigned char *keyVal, size_t keyLen) {
32 #else
33 static int config_map_key_cb(void *params_, const unsigned char *keyVal, unsigned keyLen) {
34 #endif
35     FREE(cur_key);
36
37     cur_key = smalloc(sizeof(unsigned char) * (keyLen + 1));
38     strncpy(cur_key, (const char*) keyVal, keyLen);
39     cur_key[keyLen] = '\0';
40
41     return 1;
42 }
43
44 /*
45  * Parse a null-value (current_workspace)
46  *
47  */
48 static int config_null_cb(void *params_) {
49     if (!strcmp(cur_key, "id")) {
50         /* If 'id' is NULL, the bar config was not found. Error out. */
51         ELOG("No such bar config. Use 'i3-msg -t get_bar_config' to get the available configs.\n");
52         ELOG("Are you starting i3bar by hand? You should not:\n");
53         ELOG("Configure a 'bar' block in your i3 config and i3 will launch i3bar automatically.\n");
54         exit(EXIT_FAILURE);
55     }
56
57     return 1;
58 }
59
60 /*
61  * Parse a string
62  *
63  */
64 #if YAJL_MAJOR >= 2
65 static int config_string_cb(void *params_, const unsigned char *val, size_t _len) {
66 #else
67 static int config_string_cb(void *params_, const unsigned char *val, unsigned int _len) {
68 #endif
69     int len = (int)_len;
70     /* The id and socket_path are ignored, we already know them. */
71     if (!strcmp(cur_key, "id") || !strcmp(cur_key, "socket_path"))
72         return 1;
73
74     if (!strcmp(cur_key, "mode")) {
75         DLOG("mode = %.*s, len = %d\n", len, val, len);
76         config.hide_on_modifier = (len == 4 && !strncmp((const char*)val, "dock", strlen("dock")) ? M_DOCK
77             : (len == 4 && !strncmp((const char*)val, "hide", strlen("hide")) ? M_HIDE
78                 : M_INVISIBLE));
79         return 1;
80     }
81
82     if (!strcmp(cur_key, "hidden_state")) {
83         DLOG("hidden_state = %.*s, len = %d\n", len, val, len);
84         config.hidden_state = (len == 4 && !strncmp((const char*)val, "hide", strlen("hide")) ? S_HIDE : S_SHOW);
85         return 1;
86     }
87
88     if (!strcmp(cur_key, "modifier")) {
89         DLOG("modifier = %.*s\n", len, val);
90         if (len == 5 && !strncmp((const char*)val, "shift", strlen("shift"))) {
91             config.modifier = ShiftMask;
92             return 1;
93         }
94         if (len == 4 && !strncmp((const char*)val, "ctrl", strlen("ctrl"))) {
95             config.modifier = ControlMask;
96             return 1;
97         }
98         if (len == 4 && !strncmp((const char*)val, "Mod", strlen("Mod"))) {
99             switch (val[3]) {
100                 case '1':
101                     config.modifier = Mod1Mask;
102                     return 1;
103                 case '2':
104                     config.modifier = Mod2Mask;
105                     return 1;
106                 case '3':
107                     config.modifier = Mod3Mask;
108                     return 1;
109                 /*
110                 case '4':
111                     config.modifier = Mod4Mask;
112                     return 1;
113                 */
114                 case '5':
115                     config.modifier = Mod5Mask;
116                     return 1;
117             }
118         }
119         config.modifier = Mod4Mask;
120         return 1;
121     }
122
123     if (!strcmp(cur_key, "position")) {
124         DLOG("position = %.*s\n", len, val);
125         config.position = (len == 3 && !strncmp((const char*)val, "top", strlen("top")) ? POS_TOP : POS_BOT);
126         return 1;
127     }
128
129     if (!strcmp(cur_key, "status_command")) {
130         /* We cannot directly start the child here, because start_child() also
131          * needs to be run when no command was specified (to setup stdin).
132          * Therefore we save the command in 'config' and access it later in
133          * got_bar_config() */
134         DLOG("command = %.*s\n", len, val);
135         sasprintf(&config.command, "%.*s", len, val);
136         return 1;
137     }
138
139     if (!strcmp(cur_key, "font")) {
140         DLOG("font = %.*s\n", len, val);
141         sasprintf(&config.fontname, "%.*s", len, val);
142         return 1;
143     }
144
145     if (!strcmp(cur_key, "outputs")) {
146         DLOG("+output %.*s\n", len, val);
147         int new_num_outputs = config.num_outputs + 1;
148         config.outputs = srealloc(config.outputs, sizeof(char*) * new_num_outputs);
149         sasprintf(&config.outputs[config.num_outputs], "%.*s", len, val);
150         config.num_outputs = new_num_outputs;
151         return 1;
152     }
153
154     if (!strcmp(cur_key, "tray_output")) {
155         DLOG("tray_output %.*s\n", len, val);
156         FREE(config.tray_output);
157         sasprintf(&config.tray_output, "%.*s", len, val);
158         return 1;
159     }
160
161 #define COLOR(json_name, struct_name) \
162     do { \
163         if (!strcmp(cur_key, #json_name)) { \
164             DLOG(#json_name " = " #struct_name " = %.*s\n", len, val); \
165             sasprintf(&(config.colors.struct_name), "%.*s", len, val); \
166             return 1; \
167         } \
168     } while (0)
169
170     COLOR(statusline, bar_fg);
171     COLOR(background, bar_bg);
172     COLOR(separator, sep_fg);
173     COLOR(focused_workspace_border, focus_ws_border);
174     COLOR(focused_workspace_bg, focus_ws_bg);
175     COLOR(focused_workspace_text, focus_ws_fg);
176     COLOR(active_workspace_border, active_ws_border);
177     COLOR(active_workspace_bg, active_ws_bg);
178     COLOR(active_workspace_text, active_ws_fg);
179     COLOR(inactive_workspace_border, inactive_ws_border);
180     COLOR(inactive_workspace_bg, inactive_ws_bg);
181     COLOR(inactive_workspace_text, inactive_ws_fg);
182     COLOR(urgent_workspace_border, urgent_ws_border);
183     COLOR(urgent_workspace_bg, urgent_ws_bg);
184     COLOR(urgent_workspace_text, urgent_ws_fg);
185
186     printf("got unexpected string %.*s for cur_key = %s\n", len, val, cur_key);
187
188     return 0;
189 }
190
191 /*
192  * Parse a boolean value
193  *
194  */
195 static int config_boolean_cb(void *params_, int val) {
196     if (!strcmp(cur_key, "workspace_buttons")) {
197         DLOG("workspace_buttons = %d\n", val);
198         config.disable_ws = !val;
199         return 1;
200     }
201
202     if (!strcmp(cur_key, "verbose")) {
203         DLOG("verbose = %d\n", val);
204         config.verbose = val;
205         return 1;
206     }
207
208     return 0;
209 }
210
211 /* A datastructure to pass all these callbacks to yajl */
212 static yajl_callbacks outputs_callbacks = {
213     &config_null_cb,
214     &config_boolean_cb,
215     NULL,
216     NULL,
217     NULL,
218     &config_string_cb,
219     NULL,
220     &config_map_key_cb,
221     NULL,
222     NULL,
223     NULL
224 };
225
226 /*
227  * Start parsing the received bar configuration json-string
228  *
229  */
230 void parse_config_json(char *json) {
231     yajl_handle handle;
232     yajl_status state;
233 #if YAJL_MAJOR < 2
234     yajl_parser_config parse_conf = { 0, 0 };
235
236     handle = yajl_alloc(&outputs_callbacks, &parse_conf, NULL, NULL);
237 #else
238     handle = yajl_alloc(&outputs_callbacks, NULL, NULL);
239 #endif
240
241     state = yajl_parse(handle, (const unsigned char*) json, strlen(json));
242
243     /* FIXME: Proper errorhandling for JSON-parsing */
244     switch (state) {
245         case yajl_status_ok:
246             break;
247         case yajl_status_client_canceled:
248 #if YAJL_MAJOR < 2
249         case yajl_status_insufficient_data:
250 #endif
251         case yajl_status_error:
252             ELOG("Could not parse config-reply!\n");
253             exit(EXIT_FAILURE);
254             break;
255     }
256
257     yajl_free(handle);
258 }
259
260 /*
261  * free()s the color strings as soon as they are not needed anymore.
262  *
263  */
264 void free_colors(struct xcb_color_strings_t *colors) {
265 #define FREE_COLOR(x) \
266     do { \
267         if (colors->x) \
268             free(colors->x); \
269     } while (0)
270     FREE_COLOR(bar_fg);
271     FREE_COLOR(bar_bg);
272     FREE_COLOR(sep_fg);
273     FREE_COLOR(active_ws_fg);
274     FREE_COLOR(active_ws_bg);
275     FREE_COLOR(active_ws_border);
276     FREE_COLOR(inactive_ws_fg);
277     FREE_COLOR(inactive_ws_bg);
278     FREE_COLOR(inactive_ws_border);
279     FREE_COLOR(urgent_ws_fg);
280     FREE_COLOR(urgent_ws_bg);
281     FREE_COLOR(urgent_ws_border);
282     FREE_COLOR(focus_ws_fg);
283     FREE_COLOR(focus_ws_bg);
284     FREE_COLOR(focus_ws_border);
285 #undef FREE_COLOR
286 }
287