]> git.sur5r.net Git - i3/i3/blob - i3bar/src/workspaces.c
debian: add 4.1.2-2 upload to changelog
[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-2011 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 = smalloc(sizeof(const unsigned char) * (len + 1));
118             strncpy(params->workspaces_walk->name, (const char*) val, len);
119             params->workspaces_walk->name[len] = '\0';
120
121             /* Convert the name to ucs2, save its length in glyphs and calculate its rendered width */
122             int ucs2_len;
123             xcb_char2b_t *ucs2_name = (xcb_char2b_t*) convert_utf8_to_ucs2(params->workspaces_walk->name, &ucs2_len);
124             params->workspaces_walk->ucs2_name = ucs2_name;
125             params->workspaces_walk->name_glyphs = ucs2_len;
126             params->workspaces_walk->name_width =
127                 predict_text_extents(params->workspaces_walk->ucs2_name,
128                 params->workspaces_walk->name_glyphs);
129
130             DLOG("Got Workspace %s, name_width: %d, glyphs: %d\n",
131                  params->workspaces_walk->name,
132                  params->workspaces_walk->name_width,
133                  params->workspaces_walk->name_glyphs);
134             FREE(params->cur_key);
135
136             return 1;
137         }
138
139         if (!strcmp(params->cur_key, "output")) {
140             /* We add the ws to the TAILQ of the output, it belongs to */
141             output_name = smalloc(sizeof(const unsigned char) * (len + 1));
142             strncpy(output_name, (const char*) val, len);
143             output_name[len] = '\0';
144             i3_output *target = get_output_by_name(output_name);
145             if (target) {
146                 params->workspaces_walk->output = target;
147
148                 TAILQ_INSERT_TAIL(params->workspaces_walk->output->workspaces,
149                                   params->workspaces_walk,
150                                   tailq);
151             }
152
153             FREE(output_name);
154             return 1;
155         }
156
157         return 0;
158 }
159
160 /*
161  * We hit the start of a json-map (rect or a new output)
162  *
163  */
164 static int workspaces_start_map_cb(void *params_) {
165     struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
166
167     i3_ws *new_workspace = NULL;
168
169     if (params->cur_key == NULL) {
170         new_workspace = smalloc(sizeof(i3_ws));
171         new_workspace->num = -1;
172         new_workspace->name = NULL;
173         new_workspace->visible = 0;
174         new_workspace->focused = 0;
175         new_workspace->urgent = 0;
176         memset(&new_workspace->rect, 0, sizeof(rect));
177         new_workspace->output = NULL;
178
179         params->workspaces_walk = new_workspace;
180         return 1;
181     }
182
183     return 1;
184 }
185
186 /*
187  * Parse a key.
188  *
189  * Essentially we just save it in the parsing-state
190  *
191  */
192 #if YAJL_MAJOR >= 2
193 static int workspaces_map_key_cb(void *params_, const unsigned char *keyVal, size_t keyLen) {
194 #else
195 static int workspaces_map_key_cb(void *params_, const unsigned char *keyVal, unsigned int keyLen) {
196 #endif
197     struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
198     FREE(params->cur_key);
199
200     params->cur_key = smalloc(sizeof(unsigned char) * (keyLen + 1));
201     strncpy(params->cur_key, (const char*) keyVal, keyLen);
202     params->cur_key[keyLen] = '\0';
203
204     return 1;
205 }
206
207 /* A datastructure to pass all these callbacks to yajl */
208 yajl_callbacks workspaces_callbacks = {
209     NULL,
210     &workspaces_boolean_cb,
211     &workspaces_integer_cb,
212     NULL,
213     NULL,
214     &workspaces_string_cb,
215     &workspaces_start_map_cb,
216     &workspaces_map_key_cb,
217     NULL,
218     NULL,
219     NULL
220 };
221
222 /*
223  * Start parsing the received json-string
224  *
225  */
226 void parse_workspaces_json(char *json) {
227     /* FIXME: Fasciliate stream-processing, i.e. allow starting to interpret
228      * JSON in chunks */
229     struct workspaces_json_params params;
230
231     free_workspaces();
232
233     params.workspaces_walk = NULL;
234     params.cur_key = NULL;
235     params.json = json;
236
237     yajl_handle handle;
238     yajl_status state;
239 #if YAJL_MAJOR < 2
240     yajl_parser_config parse_conf = { 0, 0 };
241
242     handle = yajl_alloc(&workspaces_callbacks, &parse_conf, NULL, (void*) &params);
243 #else
244     handle = yajl_alloc(&workspaces_callbacks, NULL, (void*) &params);
245 #endif
246
247     state = yajl_parse(handle, (const unsigned char*) json, strlen(json));
248
249     /* FIXME: Propper errorhandling for JSON-parsing */
250     switch (state) {
251         case yajl_status_ok:
252             break;
253         case yajl_status_client_canceled:
254 #if YAJL_MAJOR < 2
255         case yajl_status_insufficient_data:
256 #endif
257         case yajl_status_error:
258             ELOG("Could not parse workspaces-reply!\n");
259             exit(EXIT_FAILURE);
260             break;
261     }
262
263     yajl_free(handle);
264
265     FREE(params.cur_key);
266 }
267
268 /*
269  * free() all workspace data-structures. Does not free() the heads of the tailqueues.
270  *
271  */
272 void free_workspaces() {
273     i3_output *outputs_walk;
274     if (outputs == NULL) {
275         return;
276     }
277     i3_ws     *ws_walk;
278
279     SLIST_FOREACH(outputs_walk, outputs, slist) {
280         if (outputs_walk->workspaces != NULL && !TAILQ_EMPTY(outputs_walk->workspaces)) {
281             TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
282                 FREE(ws_walk->name);
283                 FREE(ws_walk->ucs2_name);
284             }
285             FREE_TAILQ(outputs_walk->workspaces, i3_ws);
286         }
287     }
288 }