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).
15 #include <yajl/yajl_parse.h>
16 #include <yajl/yajl_version.h>
27 * Essentially we just save it in cur_key.
30 static int config_map_key_cb(void *params_, const unsigned char *keyVal, size_t keyLen) {
33 cur_key = smalloc(sizeof(unsigned char) * (keyLen + 1));
34 strncpy(cur_key, (const char *)keyVal, keyLen);
35 cur_key[keyLen] = '\0';
41 * Parse a null value (current_workspace)
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");
60 static int config_string_cb(void *params_, const unsigned char *val, size_t _len) {
62 /* The id and socket_path are ignored, we already know them. */
63 if (!strcmp(cur_key, "id") || !strcmp(cur_key, "socket_path"))
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
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);
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;
86 if (len == 4 && !strncmp((const char *)val, "ctrl", strlen("ctrl"))) {
87 config.modifier = ControlMask;
90 if (len == 4 && !strncmp((const char *)val, "Mod", strlen("Mod"))) {
93 config.modifier = Mod1Mask;
96 config.modifier = Mod2Mask;
99 config.modifier = Mod3Mask;
103 config.modifier = Mod4Mask;
107 config.modifier = Mod5Mask;
111 config.modifier = Mod4Mask;
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);
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);
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);
135 if (!strcmp(cur_key, "status_command")) {
136 DLOG("command = %.*s\n", len, val);
137 sasprintf(&config.command, "%.*s", len, val);
141 if (!strcmp(cur_key, "font")) {
142 DLOG("font = %.*s\n", len, val);
143 sasprintf(&config.fontname, "%.*s", len, val);
147 if (!strcmp(cur_key, "separator_symbol")) {
148 DLOG("separator = %.*s\n", len, val);
149 I3STRING_FREE(config.separator_symbol);
150 config.separator_symbol = i3string_from_utf8_with_length((const char *)val, len);
154 if (!strcmp(cur_key, "outputs")) {
155 DLOG("+output %.*s\n", len, val);
156 int new_num_outputs = config.num_outputs + 1;
157 config.outputs = srealloc(config.outputs, sizeof(char *) * new_num_outputs);
158 sasprintf(&config.outputs[config.num_outputs], "%.*s", len, val);
159 config.num_outputs = new_num_outputs;
163 if (!strcmp(cur_key, "tray_output")) {
164 DLOG("tray_output %.*s\n", len, val);
165 FREE(config.tray_output);
166 sasprintf(&config.tray_output, "%.*s", len, val);
170 #define COLOR(json_name, struct_name) \
172 if (!strcmp(cur_key, #json_name)) { \
173 DLOG(#json_name " = " #struct_name " = %.*s\n", len, val); \
174 sasprintf(&(config.colors.struct_name), "%.*s", len, val); \
179 COLOR(statusline, bar_fg);
180 COLOR(background, bar_bg);
181 COLOR(separator, sep_fg);
182 COLOR(focused_workspace_border, focus_ws_border);
183 COLOR(focused_workspace_bg, focus_ws_bg);
184 COLOR(focused_workspace_text, focus_ws_fg);
185 COLOR(active_workspace_border, active_ws_border);
186 COLOR(active_workspace_bg, active_ws_bg);
187 COLOR(active_workspace_text, active_ws_fg);
188 COLOR(inactive_workspace_border, inactive_ws_border);
189 COLOR(inactive_workspace_bg, inactive_ws_bg);
190 COLOR(inactive_workspace_text, inactive_ws_fg);
191 COLOR(urgent_workspace_border, urgent_ws_border);
192 COLOR(urgent_workspace_bg, urgent_ws_bg);
193 COLOR(urgent_workspace_text, urgent_ws_fg);
195 printf("got unexpected string %.*s for cur_key = %s\n", len, val, cur_key);
201 * Parse a boolean value
204 static int config_boolean_cb(void *params_, int val) {
205 if (!strcmp(cur_key, "binding_mode_indicator")) {
206 DLOG("binding_mode_indicator = %d\n", val);
207 config.disable_binding_mode_indicator = !val;
211 if (!strcmp(cur_key, "workspace_buttons")) {
212 DLOG("workspace_buttons = %d\n", val);
213 config.disable_ws = !val;
217 if (!strcmp(cur_key, "strip_workspace_numbers")) {
218 DLOG("strip_workspace_numbers = %d\n", val);
219 config.strip_ws_numbers = val;
223 if (!strcmp(cur_key, "verbose")) {
224 DLOG("verbose = %d\n", val);
225 config.verbose = val;
232 /* A datastructure to pass all these callbacks to yajl */
233 static yajl_callbacks outputs_callbacks = {
234 .yajl_null = config_null_cb,
235 .yajl_boolean = config_boolean_cb,
236 .yajl_string = config_string_cb,
237 .yajl_map_key = config_map_key_cb,
241 * Start parsing the received bar configuration JSON string
244 void parse_config_json(char *json) {
247 handle = yajl_alloc(&outputs_callbacks, NULL, NULL);
249 state = yajl_parse(handle, (const unsigned char *)json, strlen(json));
251 /* FIXME: Proper error handling for JSON parsing */
255 case yajl_status_client_canceled:
256 case yajl_status_error:
257 ELOG("Could not parse config reply!\n");
266 * free()s the color strings as soon as they are not needed anymore.
269 void free_colors(struct xcb_color_strings_t *colors) {
270 #define FREE_COLOR(x) \
277 FREE_COLOR(active_ws_fg);
278 FREE_COLOR(active_ws_bg);
279 FREE_COLOR(active_ws_border);
280 FREE_COLOR(inactive_ws_fg);
281 FREE_COLOR(inactive_ws_bg);
282 FREE_COLOR(inactive_ws_border);
283 FREE_COLOR(urgent_ws_fg);
284 FREE_COLOR(urgent_ws_bg);
285 FREE_COLOR(urgent_ws_border);
286 FREE_COLOR(focus_ws_fg);
287 FREE_COLOR(focus_ws_bg);
288 FREE_COLOR(focus_ws_border);