2 * vim:ts=4:sw=4:expandtab
4 * i3bar - an xcb-based status- and ws-bar for i3
5 * © 2010 Axel Wagner and contributors (see also: LICENSE)
7 * config.c: Parses the configuration (received from i3).
17 #include <yajl/yajl_parse.h>
18 #include <yajl/yajl_version.h>
23 static bool parsing_bindings;
24 static bool parsing_tray_outputs;
29 * Essentially we just save it in cur_key.
32 static int config_map_key_cb(void *params_, const unsigned char *keyVal, size_t keyLen) {
34 sasprintf(&(cur_key), "%.*s", keyLen, keyVal);
36 if (strcmp(cur_key, "bindings") == 0) {
37 parsing_bindings = true;
40 if (strcmp(cur_key, "tray_outputs") == 0) {
41 parsing_tray_outputs = true;
47 static int config_end_array_cb(void *params_) {
48 parsing_bindings = false;
49 parsing_tray_outputs = false;
54 * Parse a null value (current_workspace)
57 static int config_null_cb(void *params_) {
58 if (!strcmp(cur_key, "id")) {
59 /* If 'id' is NULL, the bar config was not found. Error out. */
60 ELOG("No such bar config. Use 'i3-msg -t get_bar_config' to get the available configs.\n");
61 ELOG("Are you starting i3bar by hand? You should not:\n");
62 ELOG("Configure a 'bar' block in your i3 config and i3 will launch i3bar automatically.\n");
73 static int config_string_cb(void *params_, const unsigned char *val, size_t _len) {
75 /* The id and socket_path are ignored, we already know them. */
76 if (!strcmp(cur_key, "id") || !strcmp(cur_key, "socket_path"))
79 if (parsing_bindings) {
80 if (strcmp(cur_key, "command") == 0) {
81 binding_t *binding = TAILQ_LAST(&(config.bindings), bindings_head);
82 if (binding == NULL) {
83 ELOG("There is no binding to put the current command onto. This is a bug in i3.\n");
87 if (binding->command != NULL) {
88 ELOG("The binding for input_code = %d already has a command. This is a bug in i3.\n", binding->input_code);
92 sasprintf(&(binding->command), "%.*s", len, val);
96 ELOG("Unknown key \"%s\" while parsing bar bindings.\n", cur_key);
100 if (parsing_tray_outputs) {
101 DLOG("Adding tray_output = %.*s to the list.\n", len, val);
102 tray_output_t *tray_output = scalloc(1, sizeof(tray_output_t));
103 sasprintf(&(tray_output->output), "%.*s", len, val);
104 TAILQ_INSERT_TAIL(&(config.tray_outputs), tray_output, tray_outputs);
108 if (!strcmp(cur_key, "mode")) {
109 DLOG("mode = %.*s, len = %d\n", len, val, len);
110 config.hide_on_modifier = (len == strlen("dock") && !strncmp((const char *)val, "dock", strlen("dock")) ? M_DOCK
111 : (len == strlen("hide") && !strncmp((const char *)val, "hide", strlen("hide")) ? M_HIDE
116 if (!strcmp(cur_key, "hidden_state")) {
117 DLOG("hidden_state = %.*s, len = %d\n", len, val, len);
118 config.hidden_state = (len == strlen("hide") && !strncmp((const char *)val, "hide", strlen("hide")) ? S_HIDE : S_SHOW);
122 /* Kept for backwards compatibility. */
123 if (!strcmp(cur_key, "modifier")) {
124 DLOG("modifier = %.*s\n", len, val);
125 if (len == strlen("none") && !strncmp((const char *)val, "none", strlen("none"))) {
126 config.modifier = XCB_NONE;
130 if (len == strlen("shift") && !strncmp((const char *)val, "shift", strlen("shift"))) {
131 config.modifier = ShiftMask;
134 if (len == strlen("ctrl") && !strncmp((const char *)val, "ctrl", strlen("ctrl"))) {
135 config.modifier = ControlMask;
138 if (len == strlen("Mod") + 1 && !strncmp((const char *)val, "Mod", strlen("Mod"))) {
141 config.modifier = Mod1Mask;
144 config.modifier = Mod2Mask;
147 config.modifier = Mod3Mask;
150 config.modifier = Mod5Mask;
155 config.modifier = Mod4Mask;
159 /* This key was sent in <= 4.10.2. We keep it around to avoid breakage for
160 * users updating from that version and restarting i3bar before i3. */
161 if (!strcmp(cur_key, "wheel_up_cmd")) {
162 DLOG("wheel_up_cmd = %.*s\n", len, val);
163 binding_t *binding = scalloc(1, sizeof(binding_t));
164 binding->input_code = 4;
165 sasprintf(&(binding->command), "%.*s", len, val);
166 TAILQ_INSERT_TAIL(&(config.bindings), binding, bindings);
170 /* This key was sent in <= 4.10.2. We keep it around to avoid breakage for
171 * users updating from that version and restarting i3bar before i3. */
172 if (!strcmp(cur_key, "wheel_down_cmd")) {
173 DLOG("wheel_down_cmd = %.*s\n", len, val);
174 binding_t *binding = scalloc(1, sizeof(binding_t));
175 binding->input_code = 5;
176 sasprintf(&(binding->command), "%.*s", len, val);
177 TAILQ_INSERT_TAIL(&(config.bindings), binding, bindings);
181 if (!strcmp(cur_key, "position")) {
182 DLOG("position = %.*s\n", len, val);
183 config.position = (len == strlen("top") && !strncmp((const char *)val, "top", strlen("top")) ? POS_TOP : POS_BOT);
187 if (!strcmp(cur_key, "status_command")) {
188 DLOG("command = %.*s\n", len, val);
189 FREE(config.command);
190 sasprintf(&config.command, "%.*s", len, val);
194 if (!strcmp(cur_key, "font")) {
195 DLOG("font = %.*s\n", len, val);
196 FREE(config.fontname);
197 sasprintf(&config.fontname, "%.*s", len, val);
201 if (!strcmp(cur_key, "separator_symbol")) {
202 DLOG("separator = %.*s\n", len, val);
203 I3STRING_FREE(config.separator_symbol);
204 config.separator_symbol = i3string_from_utf8_with_length((const char *)val, len);
208 if (!strcmp(cur_key, "outputs")) {
209 DLOG("+output %.*s\n", len, val);
210 int new_num_outputs = config.num_outputs + 1;
211 config.outputs = srealloc(config.outputs, sizeof(char *) * new_num_outputs);
212 sasprintf(&config.outputs[config.num_outputs], "%.*s", len, val);
213 config.num_outputs = new_num_outputs;
217 /* We keep the old single tray_output working for users who only restart i3bar
219 if (!strcmp(cur_key, "tray_output")) {
220 DLOG("Found deprecated key tray_output %.*s.\n", len, val);
221 tray_output_t *tray_output = scalloc(1, sizeof(tray_output_t));
222 sasprintf(&(tray_output->output), "%.*s", len, val);
223 TAILQ_INSERT_TAIL(&(config.tray_outputs), tray_output, tray_outputs);
227 #define COLOR(json_name, struct_name) \
229 if (!strcmp(cur_key, #json_name)) { \
230 DLOG(#json_name " = " #struct_name " = %.*s\n", len, val); \
231 sasprintf(&(config.colors.struct_name), "%.*s", len, val); \
236 COLOR(statusline, bar_fg);
237 COLOR(background, bar_bg);
238 COLOR(separator, sep_fg);
239 COLOR(focused_statusline, focus_bar_fg);
240 COLOR(focused_background, focus_bar_bg);
241 COLOR(focused_separator, focus_sep_fg);
242 COLOR(focused_workspace_border, focus_ws_border);
243 COLOR(focused_workspace_bg, focus_ws_bg);
244 COLOR(focused_workspace_text, focus_ws_fg);
245 COLOR(active_workspace_border, active_ws_border);
246 COLOR(active_workspace_bg, active_ws_bg);
247 COLOR(active_workspace_text, active_ws_fg);
248 COLOR(inactive_workspace_border, inactive_ws_border);
249 COLOR(inactive_workspace_bg, inactive_ws_bg);
250 COLOR(inactive_workspace_text, inactive_ws_fg);
251 COLOR(urgent_workspace_border, urgent_ws_border);
252 COLOR(urgent_workspace_bg, urgent_ws_bg);
253 COLOR(urgent_workspace_text, urgent_ws_fg);
254 COLOR(binding_mode_border, binding_mode_border);
255 COLOR(binding_mode_bg, binding_mode_bg);
256 COLOR(binding_mode_text, binding_mode_fg);
258 printf("got unexpected string %.*s for cur_key = %s\n", len, val, cur_key);
264 * Parse a boolean value
267 static int config_boolean_cb(void *params_, int val) {
268 if (parsing_bindings) {
269 if (strcmp(cur_key, "release") == 0) {
270 binding_t *binding = TAILQ_LAST(&(config.bindings), bindings_head);
271 if (binding == NULL) {
272 ELOG("There is no binding to put the current command onto. This is a bug in i3.\n");
276 binding->release = val;
280 ELOG("Unknown key \"%s\" while parsing bar bindings.\n", cur_key);
283 if (!strcmp(cur_key, "binding_mode_indicator")) {
284 DLOG("binding_mode_indicator = %d\n", val);
285 config.disable_binding_mode_indicator = !val;
289 if (!strcmp(cur_key, "workspace_buttons")) {
290 DLOG("workspace_buttons = %d\n", val);
291 config.disable_ws = !val;
295 if (!strcmp(cur_key, "strip_workspace_numbers")) {
296 DLOG("strip_workspace_numbers = %d\n", val);
297 config.strip_ws_numbers = val;
301 if (!strcmp(cur_key, "strip_workspace_name")) {
302 DLOG("strip_workspace_name = %d\n", val);
303 config.strip_ws_name = val;
307 if (!strcmp(cur_key, "verbose")) {
308 if (!config.verbose) {
309 DLOG("verbose = %d\n", val);
310 config.verbose = val;
319 * Parse an integer value
322 static int config_integer_cb(void *params_, long long val) {
323 if (parsing_bindings) {
324 if (strcmp(cur_key, "input_code") == 0) {
325 binding_t *binding = scalloc(1, sizeof(binding_t));
326 binding->input_code = val;
327 TAILQ_INSERT_TAIL(&(config.bindings), binding, bindings);
332 ELOG("Unknown key \"%s\" while parsing bar bindings.\n", cur_key);
336 if (!strcmp(cur_key, "tray_padding")) {
337 DLOG("tray_padding = %lld\n", val);
338 config.tray_padding = val;
342 if (!strcmp(cur_key, "modifier")) {
343 DLOG("modifier = %lld\n", val);
344 config.modifier = (uint32_t)val;
351 /* A datastructure to pass all these callbacks to yajl */
352 static yajl_callbacks outputs_callbacks = {
353 .yajl_null = config_null_cb,
354 .yajl_boolean = config_boolean_cb,
355 .yajl_integer = config_integer_cb,
356 .yajl_string = config_string_cb,
357 .yajl_end_array = config_end_array_cb,
358 .yajl_map_key = config_map_key_cb,
362 * Start parsing the received bar configuration JSON string
365 void parse_config_json(char *json) {
368 handle = yajl_alloc(&outputs_callbacks, NULL, NULL);
370 TAILQ_INIT(&(config.bindings));
371 TAILQ_INIT(&(config.tray_outputs));
373 state = yajl_parse(handle, (const unsigned char *)json, strlen(json));
375 /* FIXME: Proper error handling for JSON parsing */
379 case yajl_status_client_canceled:
380 case yajl_status_error:
381 ELOG("Could not parse config reply!\n");
390 * free()s the color strings as soon as they are not needed anymore.
393 void free_colors(struct xcb_color_strings_t *colors) {
394 #define FREE_COLOR(x) \
401 FREE_COLOR(focus_bar_fg);
402 FREE_COLOR(focus_bar_bg);
403 FREE_COLOR(focus_sep_fg);
404 FREE_COLOR(active_ws_fg);
405 FREE_COLOR(active_ws_bg);
406 FREE_COLOR(active_ws_border);
407 FREE_COLOR(inactive_ws_fg);
408 FREE_COLOR(inactive_ws_bg);
409 FREE_COLOR(inactive_ws_border);
410 FREE_COLOR(urgent_ws_fg);
411 FREE_COLOR(urgent_ws_bg);
412 FREE_COLOR(urgent_ws_border);
413 FREE_COLOR(focus_ws_fg);
414 FREE_COLOR(focus_ws_bg);
415 FREE_COLOR(focus_ws_border);
416 FREE_COLOR(binding_mode_fg);
417 FREE_COLOR(binding_mode_bg);
418 FREE_COLOR(binding_mode_border);