]> 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 is ignored, we already have it in config.bar_id */
71     if (!strcmp(cur_key, "id"))
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, "hide", strlen("hide")));
77         return 1;
78     }
79
80     if (!strcmp(cur_key, "modifier")) {
81         DLOG("modifier = %.*s\n", len, val);
82         if (len == 5 && !strncmp((const char*)val, "shift", strlen("shift"))) {
83             config.modifier = ShiftMask;
84             return 1;
85         }
86         if (len == 4 && !strncmp((const char*)val, "ctrl", strlen("ctrl"))) {
87             config.modifier = ControlMask;
88             return 1;
89         }
90         if (len == 4 && !strncmp((const char*)val, "Mod", strlen("Mod"))) {
91             switch (val[3]) {
92                 case '1':
93                     config.modifier = Mod1Mask;
94                     return 1;
95                 case '2':
96                     config.modifier = Mod2Mask;
97                     return 1;
98                 case '3':
99                     config.modifier = Mod3Mask;
100                     return 1;
101                 /*
102                 case '4':
103                     config.modifier = Mod4Mask;
104                     return 1;
105                 */
106                 case '5':
107                     config.modifier = Mod5Mask;
108                     return 1;
109             }
110         }
111         config.modifier = Mod4Mask;
112         return 1;
113     }
114
115     if (!strcmp(cur_key, "position")) {
116         DLOG("position = %.*s\n", len, val);
117         config.position = (len == 3 && !strncmp((const char*)val, "top", strlen("top")) ? POS_TOP : POS_BOT);
118         return 1;
119     }
120
121     if (!strcmp(cur_key, "status_command")) {
122         /* We cannot directly start the child here, because start_child() also
123          * needs to be run when no command was specified (to setup stdin).
124          * Therefore we save the command in 'config' and access it later in
125          * got_bar_config() */
126         DLOG("command = %.*s\n", len, val);
127         sasprintf(&config.command, "%.*s", len, val);
128         return 1;
129     }
130
131     if (!strcmp(cur_key, "font")) {
132         DLOG("font = %.*s\n", len, val);
133         sasprintf(&config.fontname, "%.*s", len, val);
134         return 1;
135     }
136
137     if (!strcmp(cur_key, "outputs")) {
138         DLOG("+output %.*s\n", len, val);
139         int new_num_outputs = config.num_outputs + 1;
140         config.outputs = srealloc(config.outputs, sizeof(char*) * new_num_outputs);
141         sasprintf(&config.outputs[config.num_outputs], "%.*s", len, val);
142         config.num_outputs = new_num_outputs;
143         return 1;
144     }
145
146     if (!strcmp(cur_key, "tray_output")) {
147         DLOG("tray_output %.*s\n", len, val);
148         FREE(config.tray_output);
149         sasprintf(&config.tray_output, "%.*s", len, val);
150         return 1;
151     }
152
153 #define COLOR(json_name, struct_name) \
154     do { \
155         if (!strcmp(cur_key, #json_name)) { \
156             DLOG(#json_name " = " #struct_name " = %.*s\n", len, val); \
157             sasprintf(&(config.colors.struct_name), "%.*s", len, val); \
158             return 1; \
159         } \
160     } while (0)
161
162     COLOR(statusline, bar_fg);
163     COLOR(background, bar_bg);
164     COLOR(focused_workspace_text, focus_ws_fg);
165     COLOR(focused_workspace_bg, focus_ws_bg);
166     COLOR(active_workspace_text, active_ws_fg);
167     COLOR(active_workspace_bg, active_ws_bg);
168     COLOR(inactive_workspace_text, inactive_ws_fg);
169     COLOR(inactive_workspace_bg, inactive_ws_bg);
170     COLOR(urgent_workspace_text, urgent_ws_fg);
171     COLOR(urgent_workspace_bg, urgent_ws_bg);
172
173     printf("got unexpected string %.*s for cur_key = %s\n", len, val, cur_key);
174
175     return 0;
176 }
177
178 /*
179  * Parse a boolean value
180  *
181  */
182 static int config_boolean_cb(void *params_, int val) {
183     if (!strcmp(cur_key, "workspace_buttons")) {
184         DLOG("workspace_buttons = %d\n", val);
185         config.disable_ws = !val;
186         return 1;
187     }
188
189     if (!strcmp(cur_key, "verbose")) {
190         DLOG("verbose = %d\n", val);
191         config.verbose = val;
192         return 1;
193     }
194
195     return 0;
196 }
197
198 /* A datastructure to pass all these callbacks to yajl */
199 static yajl_callbacks outputs_callbacks = {
200     &config_null_cb,
201     &config_boolean_cb,
202     NULL,
203     NULL,
204     NULL,
205     &config_string_cb,
206     NULL,
207     &config_map_key_cb,
208     NULL,
209     NULL,
210     NULL
211 };
212
213 /*
214  * Start parsing the received bar configuration json-string
215  *
216  */
217 void parse_config_json(char *json) {
218     yajl_handle handle;
219     yajl_status state;
220 #if YAJL_MAJOR < 2
221     yajl_parser_config parse_conf = { 0, 0 };
222
223     handle = yajl_alloc(&outputs_callbacks, &parse_conf, NULL, NULL);
224 #else
225     handle = yajl_alloc(&outputs_callbacks, NULL, NULL);
226 #endif
227
228     state = yajl_parse(handle, (const unsigned char*) json, strlen(json));
229
230     /* FIXME: Proper errorhandling for JSON-parsing */
231     switch (state) {
232         case yajl_status_ok:
233             break;
234         case yajl_status_client_canceled:
235 #if YAJL_MAJOR < 2
236         case yajl_status_insufficient_data:
237 #endif
238         case yajl_status_error:
239             ELOG("Could not parse config-reply!\n");
240             exit(EXIT_FAILURE);
241             break;
242     }
243
244     yajl_free(handle);
245 }
246
247 /*
248  * free()s the color strings as soon as they are not needed anymore.
249  *
250  */
251 void free_colors(struct xcb_color_strings_t *colors) {
252 #define FREE_COLOR(x) \
253     do { \
254         if (colors->x) \
255             free(colors->x); \
256     } while (0)
257     FREE_COLOR(bar_fg);
258     FREE_COLOR(bar_bg);
259     FREE_COLOR(active_ws_fg);
260     FREE_COLOR(active_ws_bg);
261     FREE_COLOR(inactive_ws_fg);
262     FREE_COLOR(inactive_ws_bg);
263     FREE_COLOR(urgent_ws_fg);
264     FREE_COLOR(urgent_ws_bg);
265     FREE_COLOR(focus_ws_fg);
266     FREE_COLOR(focus_ws_bg);
267 #undef FREE_COLOR
268 }
269