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';
50 static int config_string_cb(void *params_, const unsigned char *val, size_t len) {
52 static int config_string_cb(void *params_, const unsigned char *val, unsigned int len) {
54 /* The id is ignored, we already have it in config.bar_id */
55 if (!strcmp(cur_key, "id"))
58 if (!strcmp(cur_key, "mode")) {
59 DLOG("mode = %.*s, len = %d\n", len, val, len);
60 config.hide_on_modifier = (len == 4 && !strncmp((const char*)val, "hide", strlen("hide")));
64 if (!strcmp(cur_key, "position")) {
65 DLOG("position = %.*s\n", len, val);
66 config.position = (len == 3 && !strncmp((const char*)val, "top", strlen("top")) ? POS_TOP : POS_BOT);
70 if (!strcmp(cur_key, "status_command")) {
71 /* We cannot directly start the child here, because start_child() also
72 * needs to be run when no command was specified (to setup stdin).
73 * Therefore we save the command in 'config' and access it later in
75 DLOG("command = %.*s\n", len, val);
76 asprintf(&config.command, "%.*s", len, val);
80 if (!strcmp(cur_key, "font")) {
81 DLOG("font = %.*s\n", len, val);
82 asprintf(&config.fontname, "%.*s", len, val);
86 if (!strcmp(cur_key, "outputs")) {
87 printf("+output %.*s\n", len, val);
88 /* XXX: these are not implemented yet */
92 if (!strcmp(cur_key, "tray_output")) {
93 DLOG("tray_output %.*s\n", len, val);
94 FREE(config.tray_output);
95 asprintf(&config.tray_output, "%.*s", len, val);
99 #define COLOR(json_name, struct_name) \
101 if (!strcmp(cur_key, #json_name)) { \
102 DLOG(#json_name " = " #struct_name " = %.*s\n", len, val); \
103 asprintf(&(config.colors.struct_name), "%.*s", len, val); \
108 COLOR(statusline, bar_fg);
109 COLOR(background, bar_bg);
110 COLOR(focused_workspace_text, focus_ws_fg);
111 COLOR(focused_workspace_bg, focus_ws_bg);
112 COLOR(active_workspace_text, active_ws_fg);
113 COLOR(active_workspace_bg, active_ws_bg);
114 COLOR(inactive_workspace_text, inactive_ws_fg);
115 COLOR(inactive_workspace_bg, inactive_ws_bg);
116 COLOR(urgent_workspace_text, urgent_ws_fg);
117 COLOR(urgent_workspace_bg, urgent_ws_bg);
119 printf("got unexpected string %.*s for cur_key = %s\n", len, val, cur_key);
125 * Parse a boolean value
128 static int config_boolean_cb(void *params_, int val) {
129 if (!strcmp(cur_key, "workspace_buttons")) {
130 DLOG("workspace_buttons = %d\n", val);
131 config.disable_ws = !val;
135 if (!strcmp(cur_key, "verbose")) {
136 DLOG("verbose = %d\n", val);
137 config.verbose = val;
144 /* A datastructure to pass all these callbacks to yajl */
145 static yajl_callbacks outputs_callbacks = {
160 * Start parsing the received bar configuration json-string
163 void parse_config_json(char *json) {
167 yajl_parser_config parse_conf = { 0, 0 };
169 handle = yajl_alloc(&outputs_callbacks, &parse_conf, NULL, NULL);
171 handle = yajl_alloc(&outputs_callbacks, NULL, NULL);
174 state = yajl_parse(handle, (const unsigned char*) json, strlen(json));
176 /* FIXME: Proper errorhandling for JSON-parsing */
180 case yajl_status_client_canceled:
182 case yajl_status_insufficient_data:
184 case yajl_status_error:
185 ELOG("Could not parse config-reply!\n");
194 * free()s the color strings as soon as they are not needed anymore.
197 void free_colors(struct xcb_color_strings_t *colors) {
198 #define FREE_COLOR(x) \
205 FREE_COLOR(active_ws_fg);
206 FREE_COLOR(active_ws_bg);
207 FREE_COLOR(inactive_ws_fg);
208 FREE_COLOR(inactive_ws_bg);
209 FREE_COLOR(urgent_ws_fg);
210 FREE_COLOR(urgent_ws_bg);
211 FREE_COLOR(focus_ws_fg);
212 FREE_COLOR(focus_ws_bg);