]> git.sur5r.net Git - i3/i3/blob - i3bar/src/workspaces.c
e8184d400c4fc560987bb0d143f465fb0cc77761
[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 static int workspaces_integer_cb(void *params_, long long val) {
62     struct workspaces_json_params *params = (struct workspaces_json_params *)params_;
63
64     if (!strcmp(params->cur_key, "num")) {
65         params->workspaces_walk->num = (int)val;
66         FREE(params->cur_key);
67         return 1;
68     }
69
70     if (!strcmp(params->cur_key, "x")) {
71         params->workspaces_walk->rect.x = (int)val;
72         FREE(params->cur_key);
73         return 1;
74     }
75
76     if (!strcmp(params->cur_key, "y")) {
77         params->workspaces_walk->rect.y = (int)val;
78         FREE(params->cur_key);
79         return 1;
80     }
81
82     if (!strcmp(params->cur_key, "width")) {
83         params->workspaces_walk->rect.w = (int)val;
84         FREE(params->cur_key);
85         return 1;
86     }
87
88     if (!strcmp(params->cur_key, "height")) {
89         params->workspaces_walk->rect.h = (int)val;
90         FREE(params->cur_key);
91         return 1;
92     }
93
94     FREE(params->cur_key);
95     return 0;
96 }
97
98 /*
99  * Parse a string (name, output)
100  *
101  */
102 static int workspaces_string_cb(void *params_, const unsigned char *val, size_t len) {
103     struct workspaces_json_params *params = (struct workspaces_json_params *)params_;
104
105     char *output_name;
106
107     if (!strcmp(params->cur_key, "name")) {
108         const char *ws_name = (const char *)val;
109         params->workspaces_walk->canonical_name = strndup(ws_name, len);
110
111         if (config.strip_ws_numbers && params->workspaces_walk->num >= 0) {
112             /* Special case: strip off the workspace number */
113             static char ws_num[10];
114
115             snprintf(ws_num, sizeof(ws_num), "%d", params->workspaces_walk->num);
116
117             /* Calculate the length of the number str in the name */
118             size_t offset = strspn(ws_name, ws_num);
119
120             /* Also strip off the conventional ws name delimiter */
121             if (offset && ws_name[offset] == ':')
122                 offset += 1;
123
124             /* Offset may be equal to length, in which case display the number */
125             params->workspaces_walk->name = (offset < len
126                                                  ? i3string_from_utf8_with_length(ws_name + offset, len - offset)
127                                                  : i3string_from_utf8(ws_num));
128
129         } else {
130             /* Default case: just save the name */
131             params->workspaces_walk->name = i3string_from_utf8_with_length(ws_name, len);
132         }
133
134         /* Save its rendered width */
135         params->workspaces_walk->name_width =
136             predict_text_width(params->workspaces_walk->name);
137
138         DLOG("Got Workspace canonical: %s, name: '%s', name_width: %d, glyphs: %zu\n",
139              params->workspaces_walk->canonical_name,
140              i3string_as_utf8(params->workspaces_walk->name),
141              params->workspaces_walk->name_width,
142              i3string_get_num_glyphs(params->workspaces_walk->name));
143         FREE(params->cur_key);
144
145         return 1;
146     }
147
148     if (!strcmp(params->cur_key, "output")) {
149         /* We add the ws to the TAILQ of the output, it belongs to */
150         output_name = smalloc(sizeof(const unsigned char) * (len + 1));
151         strncpy(output_name, (const char *)val, len);
152         output_name[len] = '\0';
153         i3_output *target = get_output_by_name(output_name);
154         if (target) {
155             params->workspaces_walk->output = target;
156
157             TAILQ_INSERT_TAIL(params->workspaces_walk->output->workspaces,
158                               params->workspaces_walk,
159                               tailq);
160         }
161
162         FREE(output_name);
163         return 1;
164     }
165
166     return 0;
167 }
168
169 /*
170  * We hit the start of a json-map (rect or a new output)
171  *
172  */
173 static int workspaces_start_map_cb(void *params_) {
174     struct workspaces_json_params *params = (struct workspaces_json_params *)params_;
175
176     i3_ws *new_workspace = NULL;
177
178     if (params->cur_key == NULL) {
179         new_workspace = smalloc(sizeof(i3_ws));
180         new_workspace->num = -1;
181         new_workspace->name = NULL;
182         new_workspace->visible = 0;
183         new_workspace->focused = 0;
184         new_workspace->urgent = 0;
185         memset(&new_workspace->rect, 0, sizeof(rect));
186         new_workspace->output = NULL;
187
188         params->workspaces_walk = new_workspace;
189         return 1;
190     }
191
192     return 1;
193 }
194
195 /*
196  * Parse a key.
197  *
198  * Essentially we just save it in the parsing-state
199  *
200  */
201 static int workspaces_map_key_cb(void *params_, const unsigned char *keyVal, size_t keyLen) {
202     struct workspaces_json_params *params = (struct workspaces_json_params *)params_;
203     FREE(params->cur_key);
204
205     params->cur_key = smalloc(sizeof(unsigned char) * (keyLen + 1));
206     strncpy(params->cur_key, (const char *)keyVal, keyLen);
207     params->cur_key[keyLen] = '\0';
208
209     return 1;
210 }
211
212 /* A datastructure to pass all these callbacks to yajl */
213 static yajl_callbacks workspaces_callbacks = {
214     .yajl_boolean = workspaces_boolean_cb,
215     .yajl_integer = workspaces_integer_cb,
216     .yajl_string = workspaces_string_cb,
217     .yajl_start_map = workspaces_start_map_cb,
218     .yajl_map_key = workspaces_map_key_cb,
219 };
220
221 /*
222  * Start parsing the received json-string
223  *
224  */
225 void parse_workspaces_json(char *json) {
226     /* FIXME: Fasciliate stream-processing, i.e. allow starting to interpret
227      * JSON in chunks */
228     struct workspaces_json_params params;
229
230     free_workspaces();
231
232     params.workspaces_walk = NULL;
233     params.cur_key = NULL;
234     params.json = json;
235
236     yajl_handle handle;
237     yajl_status state;
238     handle = yajl_alloc(&workspaces_callbacks, NULL, (void *)&params);
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         case yajl_status_error:
248             ELOG("Could not parse workspaces-reply!\n");
249             exit(EXIT_FAILURE);
250             break;
251     }
252
253     yajl_free(handle);
254
255     FREE(params.cur_key);
256 }
257
258 /*
259  * free() all workspace data-structures. Does not free() the heads of the tailqueues.
260  *
261  */
262 void free_workspaces(void) {
263     i3_output *outputs_walk;
264     if (outputs == NULL) {
265         return;
266     }
267     i3_ws *ws_walk;
268
269     SLIST_FOREACH(outputs_walk, outputs, slist) {
270         if (outputs_walk->workspaces != NULL && !TAILQ_EMPTY(outputs_walk->workspaces)) {
271             TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
272                 I3STRING_FREE(ws_walk->name);
273                 FREE(ws_walk->canonical_name);
274             }
275             FREE_TAILQ(outputs_walk->workspaces, i3_ws);
276         }
277     }
278 }