]> git.sur5r.net Git - i3/i3/blob - i3bar/src/config.c
Make i3bar get its config from i3 via IPC
[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 = malloc(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 string
47  *
48  */
49 #if YAJL_MAJOR >= 2
50 static int config_string_cb(void *params_, const unsigned char *val, size_t len) {
51 #else
52 static int config_string_cb(void *params_, const unsigned char *val, unsigned int len) {
53 #endif
54     /* The id is ignored, we already have it in config.bar_id */
55     if (!strcmp(cur_key, "id"))
56         return 1;
57
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")));
61         return 1;
62     }
63
64     if (!strcmp(cur_key, "position")) {
65         DLOG("position = %.*s\n", len, val);
66         config.dockpos = (len == 3 && !strncmp((const char*)val, "top", strlen("top")) ? DOCKPOS_TOP : DOCKPOS_BOT);
67         return 1;
68     }
69
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
74          * got_bar_config() */
75         DLOG("command = %.*s\n", len, val);
76         asprintf(&config.command, "%.*s", len, val);
77         return 1;
78     }
79
80     if (!strcmp(cur_key, "font")) {
81         DLOG("font = %.*s\n", len, val);
82         asprintf(&config.fontname, "%.*s", len, val);
83         return 1;
84     }
85
86     if (!strcmp(cur_key, "outputs")) {
87         printf("+output %.*s\n", len, val);
88         /* XXX: these are not implemented yet */
89         return 1;
90     }
91
92     if (!strcmp(cur_key, "tray_output")) {
93         printf("tray_output %.*s\n", len, val);
94         /* XXX: these are not implemented yet */
95         return 1;
96     }
97
98 #define COLOR(json_name, struct_name) \
99     do { \
100         if (!strcmp(cur_key, #json_name)) { \
101             DLOG(#json_name " = " #struct_name " = %.*s\n", len, val); \
102             asprintf(&(config.colors.struct_name), "%.*s", len, val); \
103             return 1; \
104         } \
105     } while (0)
106
107     COLOR(statusline, bar_fg);
108     COLOR(background, bar_bg);
109     COLOR(focused_workspace_text, focus_ws_fg);
110     COLOR(focused_workspace_bg, focus_ws_bg);
111     COLOR(active_workspace_text, active_ws_fg);
112     COLOR(active_workspace_bg, active_ws_bg);
113     COLOR(inactive_workspace_text, inactive_ws_fg);
114     COLOR(inactive_workspace_bg, inactive_ws_bg);
115     COLOR(urgent_workspace_text, urgent_ws_fg);
116     COLOR(urgent_workspace_bg, urgent_ws_bg);
117
118     printf("got unexpected string %.*s for cur_key = %s\n", len, val, cur_key);
119
120     return 0;
121 }
122
123 /*
124  * Parse a boolean value
125  *
126  */
127 static int config_boolean_cb(void *params_, int val) {
128     if (!strcmp(cur_key, "workspace_buttons")) {
129         DLOG("workspace_buttons = %d\n", val);
130         config.disable_ws = !val;
131         return 1;
132     }
133
134     if (!strcmp(cur_key, "verbose")) {
135         DLOG("verbose = %d\n", val);
136         config.verbose = val;
137         return 1;
138     }
139
140     return 0;
141 }
142
143 /* A datastructure to pass all these callbacks to yajl */
144 static yajl_callbacks outputs_callbacks = {
145     NULL,
146     &config_boolean_cb,
147     NULL,
148     NULL,
149     NULL,
150     &config_string_cb,
151     NULL,
152     &config_map_key_cb,
153     NULL,
154     NULL,
155     NULL
156 };
157
158 /*
159  * Start parsing the received bar configuration json-string
160  *
161  */
162 void parse_config_json(char *json) {
163     yajl_handle handle;
164     yajl_status state;
165 #if YAJL_MAJOR < 2
166     yajl_parser_config parse_conf = { 0, 0 };
167
168     handle = yajl_alloc(&outputs_callbacks, &parse_conf, NULL, NULL);
169 #else
170     handle = yajl_alloc(&outputs_callbacks, NULL, NULL);
171 #endif
172
173     state = yajl_parse(handle, (const unsigned char*) json, strlen(json));
174
175     /* FIXME: Proper errorhandling for JSON-parsing */
176     switch (state) {
177         case yajl_status_ok:
178             break;
179         case yajl_status_client_canceled:
180 #if YAJL_MAJOR < 2
181         case yajl_status_insufficient_data:
182 #endif
183         case yajl_status_error:
184             ELOG("Could not parse config-reply!\n");
185             exit(EXIT_FAILURE);
186             break;
187     }
188
189     yajl_free(handle);
190 }
191
192 /*
193  * free()s the color strings as soon as they are not needed anymore.
194  *
195  */
196 void free_colors(struct xcb_color_strings_t *colors) {
197 #define FREE_COLOR(x) \
198     do { \
199         if (colors->x) \
200             free(colors->x); \
201     } while (0)
202     FREE_COLOR(bar_fg);
203     FREE_COLOR(bar_bg);
204     FREE_COLOR(active_ws_fg);
205     FREE_COLOR(active_ws_bg);
206     FREE_COLOR(inactive_ws_fg);
207     FREE_COLOR(inactive_ws_bg);
208     FREE_COLOR(urgent_ws_fg);
209     FREE_COLOR(urgent_ws_bg);
210     FREE_COLOR(focus_ws_fg);
211     FREE_COLOR(focus_ws_bg);
212 #undef FREE_COLOR
213 }
214