]> 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 static yajl_callbacks workspaces_callbacks = {
202     .yajl_boolean = workspaces_boolean_cb,
203     .yajl_integer = workspaces_integer_cb,
204     .yajl_string = workspaces_string_cb,
205     .yajl_start_map = workspaces_start_map_cb,
206     .yajl_map_key = workspaces_map_key_cb,
207 };
208
209 /*
210  * Start parsing the received json-string
211  *
212  */
213 void parse_workspaces_json(char *json) {
214     /* FIXME: Fasciliate stream-processing, i.e. allow starting to interpret
215      * JSON in chunks */
216     struct workspaces_json_params params;
217
218     free_workspaces();
219
220     params.workspaces_walk = NULL;
221     params.cur_key = NULL;
222     params.json = json;
223
224     yajl_handle handle;
225     yajl_status state;
226 #if YAJL_MAJOR < 2
227     yajl_parser_config parse_conf = { 0, 0 };
228
229     handle = yajl_alloc(&workspaces_callbacks, &parse_conf, NULL, (void*) &params);
230 #else
231     handle = yajl_alloc(&workspaces_callbacks, NULL, (void*) &params);
232 #endif
233
234     state = yajl_parse(handle, (const unsigned char*) json, strlen(json));
235
236     /* FIXME: Propper errorhandling for JSON-parsing */
237     switch (state) {
238         case yajl_status_ok:
239             break;
240         case yajl_status_client_canceled:
241 #if YAJL_MAJOR < 2
242         case yajl_status_insufficient_data:
243 #endif
244         case yajl_status_error:
245             ELOG("Could not parse workspaces-reply!\n");
246             exit(EXIT_FAILURE);
247             break;
248     }
249
250     yajl_free(handle);
251
252     FREE(params.cur_key);
253 }
254
255 /*
256  * free() all workspace data-structures. Does not free() the heads of the tailqueues.
257  *
258  */
259 void free_workspaces(void) {
260     i3_output *outputs_walk;
261     if (outputs == NULL) {
262         return;
263     }
264     i3_ws     *ws_walk;
265
266     SLIST_FOREACH(outputs_walk, outputs, slist) {
267         if (outputs_walk->workspaces != NULL && !TAILQ_EMPTY(outputs_walk->workspaces)) {
268             TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
269                 I3STRING_FREE(ws_walk->name);
270             }
271             FREE_TAILQ(outputs_walk->workspaces, i3_ws);
272         }
273     }
274 }