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