2 * vim:ts=4:sw=4:expandtab
4 * i3bar - an xcb-based status- and ws-bar for i3
6 * © 2010-2011 Axel Wagner and contributors
8 * See file LICENSE for license information
10 * src/outputs.c: Maintaining the output-list
18 #include <yajl/yajl_parse.h>
19 #include <yajl/yajl_version.h>
28 * 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 static int config_map_key_cb(void *params_, const unsigned char *keyVal, unsigned keyLen) {
38 cur_key = smalloc(sizeof(unsigned char) * (keyLen + 1));
39 strncpy(cur_key, (const char*) keyVal, keyLen);
40 cur_key[keyLen] = '\0';
46 * Parse a null-value (current_workspace)
49 static int config_null_cb(void *params_) {
50 if (!strcmp(cur_key, "id")) {
51 /* If 'id' is NULL, the bar config was not found. Error out. */
52 ELOG("No such bar config. Use 'i3-msg -t get_bar_config' to get the available configs.\n");
53 ELOG("Are you starting i3bar by hand? You should not:\n");
54 ELOG("Configure a 'bar' block in your i3 config and i3 will launch i3bar automatically.\n");
66 static int config_string_cb(void *params_, const unsigned char *val, size_t len) {
68 static int config_string_cb(void *params_, const unsigned char *val, unsigned int len) {
70 /* The id is ignored, we already have it in config.bar_id */
71 if (!strcmp(cur_key, "id"))
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")));
80 if (!strcmp(cur_key, "position")) {
81 DLOG("position = %.*s\n", len, val);
82 config.position = (len == 3 && !strncmp((const char*)val, "top", strlen("top")) ? POS_TOP : POS_BOT);
86 if (!strcmp(cur_key, "status_command")) {
87 /* We cannot directly start the child here, because start_child() also
88 * needs to be run when no command was specified (to setup stdin).
89 * Therefore we save the command in 'config' and access it later in
91 DLOG("command = %.*s\n", len, val);
92 sasprintf(&config.command, "%.*s", len, val);
96 if (!strcmp(cur_key, "font")) {
97 DLOG("font = %.*s\n", len, val);
98 sasprintf(&config.fontname, "%.*s", len, val);
102 if (!strcmp(cur_key, "outputs")) {
103 DLOG("+output %.*s\n", len, val);
104 int new_num_outputs = config.num_outputs + 1;
105 config.outputs = srealloc(config.outputs, sizeof(char*) * new_num_outputs);
106 sasprintf(&config.outputs[config.num_outputs], "%.*s", len, val);
107 config.num_outputs = new_num_outputs;
111 if (!strcmp(cur_key, "tray_output")) {
112 DLOG("tray_output %.*s\n", len, val);
113 FREE(config.tray_output);
114 sasprintf(&config.tray_output, "%.*s", len, val);
118 #define COLOR(json_name, struct_name) \
120 if (!strcmp(cur_key, #json_name)) { \
121 DLOG(#json_name " = " #struct_name " = %.*s\n", len, val); \
122 sasprintf(&(config.colors.struct_name), "%.*s", len, val); \
127 COLOR(statusline, bar_fg);
128 COLOR(background, bar_bg);
129 COLOR(focused_workspace_text, focus_ws_fg);
130 COLOR(focused_workspace_bg, focus_ws_bg);
131 COLOR(active_workspace_text, active_ws_fg);
132 COLOR(active_workspace_bg, active_ws_bg);
133 COLOR(inactive_workspace_text, inactive_ws_fg);
134 COLOR(inactive_workspace_bg, inactive_ws_bg);
135 COLOR(urgent_workspace_text, urgent_ws_fg);
136 COLOR(urgent_workspace_bg, urgent_ws_bg);
138 printf("got unexpected string %.*s for cur_key = %s\n", len, val, cur_key);
144 * Parse a boolean value
147 static int config_boolean_cb(void *params_, int val) {
148 if (!strcmp(cur_key, "workspace_buttons")) {
149 DLOG("workspace_buttons = %d\n", val);
150 config.disable_ws = !val;
154 if (!strcmp(cur_key, "verbose")) {
155 DLOG("verbose = %d\n", val);
156 config.verbose = val;
163 /* A datastructure to pass all these callbacks to yajl */
164 static yajl_callbacks outputs_callbacks = {
179 * Start parsing the received bar configuration json-string
182 void parse_config_json(char *json) {
186 yajl_parser_config parse_conf = { 0, 0 };
188 handle = yajl_alloc(&outputs_callbacks, &parse_conf, NULL, NULL);
190 handle = yajl_alloc(&outputs_callbacks, NULL, NULL);
193 state = yajl_parse(handle, (const unsigned char*) json, strlen(json));
195 /* FIXME: Proper errorhandling for JSON-parsing */
199 case yajl_status_client_canceled:
201 case yajl_status_insufficient_data:
203 case yajl_status_error:
204 ELOG("Could not parse config-reply!\n");
213 * free()s the color strings as soon as they are not needed anymore.
216 void free_colors(struct xcb_color_strings_t *colors) {
217 #define FREE_COLOR(x) \
224 FREE_COLOR(active_ws_fg);
225 FREE_COLOR(active_ws_bg);
226 FREE_COLOR(inactive_ws_fg);
227 FREE_COLOR(inactive_ws_bg);
228 FREE_COLOR(urgent_ws_fg);
229 FREE_COLOR(urgent_ws_bg);
230 FREE_COLOR(focus_ws_fg);
231 FREE_COLOR(focus_ws_bg);