]> git.sur5r.net Git - i3/i3/blob - i3bar/src/workspaces.c
Merge branch 'master' into next
[i3/i3] / i3bar / src / workspaces.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3bar - an xcb-based status- and ws-bar for i3
5  * © 2010-2012 Axel Wagner and contributors (see also: LICENSE)
6  *
7  * workspaces.c: Maintaining the workspace-lists
8  *
9  */
10 #include <string.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <errno.h>
14 #include <yajl/yajl_parse.h>
15 #include <yajl/yajl_version.h>
16
17 #include "common.h"
18
19 /* A datatype to pass through the callbacks to save the state */
20 struct workspaces_json_params {
21     struct ws_head *workspaces;
22     i3_ws          *workspaces_walk;
23     char           *cur_key;
24     char           *json;
25 };
26
27 /*
28  * Parse a boolean value (visible, focused, urgent)
29  *
30  */
31 static int workspaces_boolean_cb(void *params_, int val) {
32     struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
33
34     if (!strcmp(params->cur_key, "visible")) {
35         params->workspaces_walk->visible = val;
36         FREE(params->cur_key);
37         return 1;
38     }
39
40     if (!strcmp(params->cur_key, "focused")) {
41         params->workspaces_walk->focused = val;
42         FREE(params->cur_key);
43         return 1;
44     }
45
46     if (!strcmp(params->cur_key, "urgent")) {
47         params->workspaces_walk->urgent = val;
48         FREE(params->cur_key);
49         return 1;
50     }
51
52     FREE(params->cur_key);
53
54     return 0;
55 }
56
57 /*
58  * Parse an integer (num or the rect)
59  *
60  */
61 #if YAJL_MAJOR >= 2
62 static int workspaces_integer_cb(void *params_, long long val) {
63 #else
64 static int workspaces_integer_cb(void *params_, long val) {
65 #endif
66     struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
67
68     if (!strcmp(params->cur_key, "num")) {
69         params->workspaces_walk->num = (int) val;
70         FREE(params->cur_key);
71         return 1;
72     }
73
74     if (!strcmp(params->cur_key, "x")) {
75         params->workspaces_walk->rect.x = (int) val;
76         FREE(params->cur_key);
77         return 1;
78     }
79
80     if (!strcmp(params->cur_key, "y")) {
81         params->workspaces_walk->rect.y = (int) val;
82         FREE(params->cur_key);
83         return 1;
84     }
85
86     if (!strcmp(params->cur_key, "width")) {
87         params->workspaces_walk->rect.w = (int) val;
88         FREE(params->cur_key);
89         return 1;
90     }
91
92     if (!strcmp(params->cur_key, "height")) {
93         params->workspaces_walk->rect.h = (int) val;
94         FREE(params->cur_key);
95         return 1;
96     }
97
98     FREE(params->cur_key);
99     return 0;
100 }
101
102 /*
103  * Parse a string (name, output)
104  *
105  */
106 #if YAJL_MAJOR >= 2
107 static int workspaces_string_cb(void *params_, const unsigned char *val, size_t len) {
108 #else
109 static int workspaces_string_cb(void *params_, const unsigned char *val, unsigned int len) {
110 #endif
111         struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
112
113         char *output_name;
114
115         if (!strcmp(params->cur_key, "name")) {
116             /* Save the name */
117             params->workspaces_walk->name = i3string_from_utf8_with_length((const char *)val, len);
118
119             /* Save its rendered width */
120             params->workspaces_walk->name_width =
121                 predict_text_width(params->workspaces_walk->name);
122
123             DLOG("Got Workspace %s, name_width: %d, glyphs: %zu\n",
124                  i3string_as_utf8(params->workspaces_walk->name),
125                  params->workspaces_walk->name_width,
126                  i3string_get_num_glyphs(params->workspaces_walk->name));
127             FREE(params->cur_key);
128
129             return 1;
130         }
131
132         if (!strcmp(params->cur_key, "output")) {
133             /* We add the ws to the TAILQ of the output, it belongs to */
134             output_name = smalloc(sizeof(const unsigned char) * (len + 1));
135             strncpy(output_name, (const char*) val, len);
136             output_name[len] = '\0';
137             i3_output *target = get_output_by_name(output_name);
138             if (target) {
139                 params->workspaces_walk->output = target;
140
141                 TAILQ_INSERT_TAIL(params->workspaces_walk->output->workspaces,
142                                   params->workspaces_walk,
143                                   tailq);
144             }
145
146             FREE(output_name);
147             return 1;
148         }
149
150         return 0;
151 }
152
153 /*
154  * We hit the start of a json-map (rect or a new output)
155  *
156  */
157 static int workspaces_start_map_cb(void *params_) {
158     struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
159
160     i3_ws *new_workspace = NULL;
161
162     if (params->cur_key == NULL) {
163         new_workspace = smalloc(sizeof(i3_ws));
164         new_workspace->num = -1;
165         new_workspace->name = NULL;
166         new_workspace->visible = 0;
167         new_workspace->focused = 0;
168         new_workspace->urgent = 0;
169         memset(&new_workspace->rect, 0, sizeof(rect));
170         new_workspace->output = NULL;
171
172         params->workspaces_walk = new_workspace;
173         return 1;
174     }
175
176     return 1;
177 }
178
179 /*
180  * Parse a key.
181  *
182  * Essentially we just save it in the parsing-state
183  *
184  */
185 #if YAJL_MAJOR >= 2
186 static int workspaces_map_key_cb(void *params_, const unsigned char *keyVal, size_t keyLen) {
187 #else
188 static int workspaces_map_key_cb(void *params_, const unsigned char *keyVal, unsigned int keyLen) {
189 #endif
190     struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
191     FREE(params->cur_key);
192
193     params->cur_key = smalloc(sizeof(unsigned char) * (keyLen + 1));
194     strncpy(params->cur_key, (const char*) keyVal, keyLen);
195     params->cur_key[keyLen] = '\0';
196
197     return 1;
198 }
199
200 /* A datastructure to pass all these callbacks to yajl */
201 yajl_callbacks workspaces_callbacks = {
202     NULL,
203     &workspaces_boolean_cb,
204     &workspaces_integer_cb,
205     NULL,
206     NULL,
207     &workspaces_string_cb,
208     &workspaces_start_map_cb,
209     &workspaces_map_key_cb,
210     NULL,
211     NULL,
212     NULL
213 };
214
215 /*
216  * Start parsing the received json-string
217  *
218  */
219 void parse_workspaces_json(char *json) {
220     /* FIXME: Fasciliate stream-processing, i.e. allow starting to interpret
221      * JSON in chunks */
222     struct workspaces_json_params params;
223
224     free_workspaces();
225
226     params.workspaces_walk = NULL;
227     params.cur_key = NULL;
228     params.json = json;
229
230     yajl_handle handle;
231     yajl_status state;
232 #if YAJL_MAJOR < 2
233     yajl_parser_config parse_conf = { 0, 0 };
234
235     handle = yajl_alloc(&workspaces_callbacks, &parse_conf, NULL, (void*) &params);
236 #else
237     handle = yajl_alloc(&workspaces_callbacks, NULL, (void*) &params);
238 #endif
239
240     state = yajl_parse(handle, (const unsigned char*) json, strlen(json));
241
242     /* FIXME: Propper errorhandling for JSON-parsing */
243     switch (state) {
244         case yajl_status_ok:
245             break;
246         case yajl_status_client_canceled:
247 #if YAJL_MAJOR < 2
248         case yajl_status_insufficient_data:
249 #endif
250         case yajl_status_error:
251             ELOG("Could not parse workspaces-reply!\n");
252             exit(EXIT_FAILURE);
253             break;
254     }
255
256     yajl_free(handle);
257
258     FREE(params.cur_key);
259 }
260
261 /*
262  * free() all workspace data-structures. Does not free() the heads of the tailqueues.
263  *
264  */
265 void free_workspaces(void) {
266     i3_output *outputs_walk;
267     if (outputs == NULL) {
268         return;
269     }
270     i3_ws     *ws_walk;
271
272     SLIST_FOREACH(outputs_walk, outputs, slist) {
273         if (outputs_walk->workspaces != NULL && !TAILQ_EMPTY(outputs_walk->workspaces)) {
274             TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
275                 I3STRING_FREE(ws_walk->name);
276             }
277             FREE_TAILQ(outputs_walk->workspaces, i3_ws);
278         }
279     }
280 }