]> git.sur5r.net Git - i3/i3/blob - i3bar/src/workspaces.c
233249893ecf10fc96e3d598d9cee64779b0837b
[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 Axel Wagner and contributors (see also: LICENSE)
6  *
7  * workspaces.c: Maintaining the workspace lists
8  *
9  */
10 #include "common.h"
11
12 #include <string.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <errno.h>
16 #include <yajl/yajl_parse.h>
17 #include <yajl/yajl_version.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     if (!strcmp(params->cur_key, "name")) {
106         const char *ws_name = (const char *)val;
107         params->workspaces_walk->canonical_name = sstrndup(ws_name, len);
108
109         if (config.strip_ws_numbers && params->workspaces_walk->num >= 0) {
110             /* Special case: strip off the workspace number */
111             static char ws_num[10];
112
113             snprintf(ws_num, sizeof(ws_num), "%d", params->workspaces_walk->num);
114
115             /* Calculate the length of the number str in the name */
116             size_t offset = strspn(ws_name, ws_num);
117
118             /* Also strip off the conventional ws name delimiter */
119             if (offset && ws_name[offset] == ':')
120                 offset += 1;
121
122             /* Offset may be equal to length, in which case display the number */
123             params->workspaces_walk->name = (offset < len
124                                                  ? i3string_from_markup_with_length(ws_name + offset, len - offset)
125                                                  : i3string_from_markup(ws_num));
126
127         } else {
128             /* Default case: just save the name */
129             params->workspaces_walk->name = i3string_from_markup_with_length(ws_name, len);
130         }
131
132         /* Save its rendered width */
133         params->workspaces_walk->name_width =
134             predict_text_width(params->workspaces_walk->name);
135
136         DLOG("Got workspace canonical: %s, name: '%s', name_width: %d, glyphs: %zu\n",
137              params->workspaces_walk->canonical_name,
138              i3string_as_utf8(params->workspaces_walk->name),
139              params->workspaces_walk->name_width,
140              i3string_get_num_glyphs(params->workspaces_walk->name));
141         FREE(params->cur_key);
142
143         return 1;
144     }
145
146     if (!strcmp(params->cur_key, "output")) {
147         /* We add the ws to the TAILQ of the output, it belongs to */
148         char *output_name = NULL;
149         sasprintf(&output_name, "%.*s", len, val);
150
151         i3_output *target = get_output_by_name(output_name);
152         if (target != NULL) {
153             params->workspaces_walk->output = target;
154
155             TAILQ_INSERT_TAIL(params->workspaces_walk->output->workspaces,
156                               params->workspaces_walk,
157                               tailq);
158         }
159
160         FREE(output_name);
161         return 1;
162     }
163
164     return 0;
165 }
166
167 /*
168  * We hit the start of a JSON map (rect or a new output)
169  *
170  */
171 static int workspaces_start_map_cb(void *params_) {
172     struct workspaces_json_params *params = (struct workspaces_json_params *)params_;
173
174     i3_ws *new_workspace = NULL;
175
176     if (params->cur_key == NULL) {
177         new_workspace = smalloc(sizeof(i3_ws));
178         new_workspace->num = -1;
179         new_workspace->name = NULL;
180         new_workspace->visible = 0;
181         new_workspace->focused = 0;
182         new_workspace->urgent = 0;
183         memset(&new_workspace->rect, 0, sizeof(rect));
184         new_workspace->output = NULL;
185
186         params->workspaces_walk = new_workspace;
187         return 1;
188     }
189
190     return 1;
191 }
192
193 /*
194  * Parse a key.
195  *
196  * Essentially we just save it in the parsing state
197  *
198  */
199 static int workspaces_map_key_cb(void *params_, const unsigned char *keyVal, size_t keyLen) {
200     struct workspaces_json_params *params = (struct workspaces_json_params *)params_;
201     FREE(params->cur_key);
202     sasprintf(&(params->cur_key), "%.*s", keyLen, keyVal);
203     return 1;
204 }
205
206 /* A datastructure to pass all these callbacks to yajl */
207 static yajl_callbacks workspaces_callbacks = {
208     .yajl_boolean = workspaces_boolean_cb,
209     .yajl_integer = workspaces_integer_cb,
210     .yajl_string = workspaces_string_cb,
211     .yajl_start_map = workspaces_start_map_cb,
212     .yajl_map_key = workspaces_map_key_cb,
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     handle = yajl_alloc(&workspaces_callbacks, NULL, (void *)&params);
233
234     state = yajl_parse(handle, (const unsigned char *)json, strlen(json));
235
236     /* FIXME: Proper error handling for JSON parsing */
237     switch (state) {
238         case yajl_status_ok:
239             break;
240         case yajl_status_client_canceled:
241         case yajl_status_error:
242             ELOG("Could not parse workspaces reply!\n");
243             exit(EXIT_FAILURE);
244             break;
245     }
246
247     yajl_free(handle);
248
249     FREE(params.cur_key);
250 }
251
252 /*
253  * free() all workspace data structures. Does not free() the heads of the tailqueues.
254  *
255  */
256 void free_workspaces(void) {
257     i3_output *outputs_walk;
258     if (outputs == NULL) {
259         return;
260     }
261     i3_ws *ws_walk;
262
263     SLIST_FOREACH(outputs_walk, outputs, slist) {
264         if (outputs_walk->workspaces != NULL && !TAILQ_EMPTY(outputs_walk->workspaces)) {
265             TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
266                 I3STRING_FREE(ws_walk->name);
267                 FREE(ws_walk->canonical_name);
268             }
269             FREE_TAILQ(outputs_walk->workspaces, i3_ws);
270         }
271     }
272 }