]> git.sur5r.net Git - i3/i3/blob - i3bar/src/config.c
Merge branch 'master' into next
[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         DLOG("command = %.*s\n", len, val);
131         sasprintf(&config.command, "%.*s", len, val);
132         return 1;
133     }
134
135     if (!strcmp(cur_key, "font")) {
136         DLOG("font = %.*s\n", len, val);
137         sasprintf(&config.fontname, "%.*s", len, val);
138         return 1;
139     }
140
141     if (!strcmp(cur_key, "outputs")) {
142         DLOG("+output %.*s\n", len, val);
143         int new_num_outputs = config.num_outputs + 1;
144         config.outputs = srealloc(config.outputs, sizeof(char*) * new_num_outputs);
145         sasprintf(&config.outputs[config.num_outputs], "%.*s", len, val);
146         config.num_outputs = new_num_outputs;
147         return 1;
148     }
149
150     if (!strcmp(cur_key, "tray_output")) {
151         DLOG("tray_output %.*s\n", len, val);
152         FREE(config.tray_output);
153         sasprintf(&config.tray_output, "%.*s", len, val);
154         return 1;
155     }
156
157 #define COLOR(json_name, struct_name) \
158     do { \
159         if (!strcmp(cur_key, #json_name)) { \
160             DLOG(#json_name " = " #struct_name " = %.*s\n", len, val); \
161             sasprintf(&(config.colors.struct_name), "%.*s", len, val); \
162             return 1; \
163         } \
164     } while (0)
165
166     COLOR(statusline, bar_fg);
167     COLOR(background, bar_bg);
168     COLOR(separator, sep_fg);
169     COLOR(focused_workspace_border, focus_ws_border);
170     COLOR(focused_workspace_bg, focus_ws_bg);
171     COLOR(focused_workspace_text, focus_ws_fg);
172     COLOR(active_workspace_border, active_ws_border);
173     COLOR(active_workspace_bg, active_ws_bg);
174     COLOR(active_workspace_text, active_ws_fg);
175     COLOR(inactive_workspace_border, inactive_ws_border);
176     COLOR(inactive_workspace_bg, inactive_ws_bg);
177     COLOR(inactive_workspace_text, inactive_ws_fg);
178     COLOR(urgent_workspace_border, urgent_ws_border);
179     COLOR(urgent_workspace_bg, urgent_ws_bg);
180     COLOR(urgent_workspace_text, urgent_ws_fg);
181
182     printf("got unexpected string %.*s for cur_key = %s\n", len, val, cur_key);
183
184     return 0;
185 }
186
187 /*
188  * Parse a boolean value
189  *
190  */
191 static int config_boolean_cb(void *params_, int val) {
192     if (!strcmp(cur_key, "binding_mode_indicator")) {
193         DLOG("binding_mode_indicator = %d\n", val);
194         config.disable_binding_mode_indicator = !val;
195         return 1;
196     }
197
198     if (!strcmp(cur_key, "workspace_buttons")) {
199         DLOG("workspace_buttons = %d\n", val);
200         config.disable_ws = !val;
201         return 1;
202     }
203
204     if (!strcmp(cur_key, "verbose")) {
205         DLOG("verbose = %d\n", val);
206         config.verbose = val;
207         return 1;
208     }
209
210     return 0;
211 }
212
213 /* A datastructure to pass all these callbacks to yajl */
214 static yajl_callbacks outputs_callbacks = {
215     .yajl_null = config_null_cb,
216     .yajl_boolean = config_boolean_cb,
217     .yajl_string = config_string_cb,
218     .yajl_map_key = config_map_key_cb,
219 };
220
221 /*
222  * Start parsing the received bar configuration json-string
223  *
224  */
225 void parse_config_json(char *json) {
226     yajl_handle handle;
227     yajl_status state;
228 #if YAJL_MAJOR < 2
229     yajl_parser_config parse_conf = { 0, 0 };
230
231     handle = yajl_alloc(&outputs_callbacks, &parse_conf, NULL, NULL);
232 #else
233     handle = yajl_alloc(&outputs_callbacks, NULL, NULL);
234 #endif
235
236     state = yajl_parse(handle, (const unsigned char*) json, strlen(json));
237
238     /* FIXME: Proper errorhandling for JSON-parsing */
239     switch (state) {
240         case yajl_status_ok:
241             break;
242         case yajl_status_client_canceled:
243 #if YAJL_MAJOR < 2
244         case yajl_status_insufficient_data:
245 #endif
246         case yajl_status_error:
247             ELOG("Could not parse config-reply!\n");
248             exit(EXIT_FAILURE);
249             break;
250     }
251
252     yajl_free(handle);
253 }
254
255 /*
256  * free()s the color strings as soon as they are not needed anymore.
257  *
258  */
259 void free_colors(struct xcb_color_strings_t *colors) {
260 #define FREE_COLOR(x) \
261     do { \
262         if (colors->x) \
263             free(colors->x); \
264     } while (0)
265     FREE_COLOR(bar_fg);
266     FREE_COLOR(bar_bg);
267     FREE_COLOR(sep_fg);
268     FREE_COLOR(active_ws_fg);
269     FREE_COLOR(active_ws_bg);
270     FREE_COLOR(active_ws_border);
271     FREE_COLOR(inactive_ws_fg);
272     FREE_COLOR(inactive_ws_bg);
273     FREE_COLOR(inactive_ws_border);
274     FREE_COLOR(urgent_ws_fg);
275     FREE_COLOR(urgent_ws_bg);
276     FREE_COLOR(urgent_ws_border);
277     FREE_COLOR(focus_ws_fg);
278     FREE_COLOR(focus_ws_bg);
279     FREE_COLOR(focus_ws_border);
280 #undef FREE_COLOR
281 }
282