]> git.sur5r.net Git - i3/i3/blob - i3bar/src/config.c
i3bar: spit out an error on wrong bar id
[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  *
6  * © 2010-2011 Axel Wagner and contributors
7  *
8  * See file LICENSE for license information
9  *
10  * src/outputs.c: Maintaining the output-list
11  *
12  */
13 #include <string.h>
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <errno.h>
17 #include <i3/ipc.h>
18 #include <yajl/yajl_parse.h>
19 #include <yajl/yajl_version.h>
20
21 #include "common.h"
22
23 static char *cur_key;
24
25 /*
26  * Parse a key.
27  *
28  * Essentially we just save it in cur_key.
29  *
30  */
31 #if YAJL_MAJOR >= 2
32 static int config_map_key_cb(void *params_, const unsigned char *keyVal, size_t keyLen) {
33 #else
34 static int config_map_key_cb(void *params_, const unsigned char *keyVal, unsigned keyLen) {
35 #endif
36     FREE(cur_key);
37
38     cur_key = smalloc(sizeof(unsigned char) * (keyLen + 1));
39     strncpy(cur_key, (const char*) keyVal, keyLen);
40     cur_key[keyLen] = '\0';
41
42     return 1;
43 }
44
45 /*
46  * Parse a null-value (current_workspace)
47  *
48  */
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");
55         exit(EXIT_FAILURE);
56     }
57
58     return 1;
59 }
60
61 /*
62  * Parse a string
63  *
64  */
65 #if YAJL_MAJOR >= 2
66 static int config_string_cb(void *params_, const unsigned char *val, size_t len) {
67 #else
68 static int config_string_cb(void *params_, const unsigned char *val, unsigned int len) {
69 #endif
70     /* The id is ignored, we already have it in config.bar_id */
71     if (!strcmp(cur_key, "id"))
72         return 1;
73
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")));
77         return 1;
78     }
79
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);
83         return 1;
84     }
85
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
90          * got_bar_config() */
91         DLOG("command = %.*s\n", len, val);
92         asprintf(&config.command, "%.*s", len, val);
93         return 1;
94     }
95
96     if (!strcmp(cur_key, "font")) {
97         DLOG("font = %.*s\n", len, val);
98         asprintf(&config.fontname, "%.*s", len, val);
99         return 1;
100     }
101
102     if (!strcmp(cur_key, "outputs")) {
103         printf("+output %.*s\n", len, val);
104         /* XXX: these are not implemented yet */
105         return 1;
106     }
107
108     if (!strcmp(cur_key, "tray_output")) {
109         DLOG("tray_output %.*s\n", len, val);
110         FREE(config.tray_output);
111         asprintf(&config.tray_output, "%.*s", len, val);
112         return 1;
113     }
114
115 #define COLOR(json_name, struct_name) \
116     do { \
117         if (!strcmp(cur_key, #json_name)) { \
118             DLOG(#json_name " = " #struct_name " = %.*s\n", len, val); \
119             asprintf(&(config.colors.struct_name), "%.*s", len, val); \
120             return 1; \
121         } \
122     } while (0)
123
124     COLOR(statusline, bar_fg);
125     COLOR(background, bar_bg);
126     COLOR(focused_workspace_text, focus_ws_fg);
127     COLOR(focused_workspace_bg, focus_ws_bg);
128     COLOR(active_workspace_text, active_ws_fg);
129     COLOR(active_workspace_bg, active_ws_bg);
130     COLOR(inactive_workspace_text, inactive_ws_fg);
131     COLOR(inactive_workspace_bg, inactive_ws_bg);
132     COLOR(urgent_workspace_text, urgent_ws_fg);
133     COLOR(urgent_workspace_bg, urgent_ws_bg);
134
135     printf("got unexpected string %.*s for cur_key = %s\n", len, val, cur_key);
136
137     return 0;
138 }
139
140 /*
141  * Parse a boolean value
142  *
143  */
144 static int config_boolean_cb(void *params_, int val) {
145     if (!strcmp(cur_key, "workspace_buttons")) {
146         DLOG("workspace_buttons = %d\n", val);
147         config.disable_ws = !val;
148         return 1;
149     }
150
151     if (!strcmp(cur_key, "verbose")) {
152         DLOG("verbose = %d\n", val);
153         config.verbose = val;
154         return 1;
155     }
156
157     return 0;
158 }
159
160 /* A datastructure to pass all these callbacks to yajl */
161 static yajl_callbacks outputs_callbacks = {
162     &config_null_cb,
163     &config_boolean_cb,
164     NULL,
165     NULL,
166     NULL,
167     &config_string_cb,
168     NULL,
169     &config_map_key_cb,
170     NULL,
171     NULL,
172     NULL
173 };
174
175 /*
176  * Start parsing the received bar configuration json-string
177  *
178  */
179 void parse_config_json(char *json) {
180     yajl_handle handle;
181     yajl_status state;
182 #if YAJL_MAJOR < 2
183     yajl_parser_config parse_conf = { 0, 0 };
184
185     handle = yajl_alloc(&outputs_callbacks, &parse_conf, NULL, NULL);
186 #else
187     handle = yajl_alloc(&outputs_callbacks, NULL, NULL);
188 #endif
189
190     state = yajl_parse(handle, (const unsigned char*) json, strlen(json));
191
192     /* FIXME: Proper errorhandling for JSON-parsing */
193     switch (state) {
194         case yajl_status_ok:
195             break;
196         case yajl_status_client_canceled:
197 #if YAJL_MAJOR < 2
198         case yajl_status_insufficient_data:
199 #endif
200         case yajl_status_error:
201             ELOG("Could not parse config-reply!\n");
202             exit(EXIT_FAILURE);
203             break;
204     }
205
206     yajl_free(handle);
207 }
208
209 /*
210  * free()s the color strings as soon as they are not needed anymore.
211  *
212  */
213 void free_colors(struct xcb_color_strings_t *colors) {
214 #define FREE_COLOR(x) \
215     do { \
216         if (colors->x) \
217             free(colors->x); \
218     } while (0)
219     FREE_COLOR(bar_fg);
220     FREE_COLOR(bar_bg);
221     FREE_COLOR(active_ws_fg);
222     FREE_COLOR(active_ws_bg);
223     FREE_COLOR(inactive_ws_fg);
224     FREE_COLOR(inactive_ws_bg);
225     FREE_COLOR(urgent_ws_fg);
226     FREE_COLOR(urgent_ws_bg);
227     FREE_COLOR(focus_ws_fg);
228     FREE_COLOR(focus_ws_bg);
229 #undef FREE_COLOR
230 }
231