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