]> git.sur5r.net Git - i3/i3/blob - i3bar/src/workspaces.c
Bugfix: free() workspace-names seperately (thx dothebart)
[i3/i3] / i3bar / src / workspaces.c
1 /*
2  * i3bar - an xcb-based status- and ws-bar for i3
3  *
4  * © 2010 Axel Wagner and contributors
5  *
6  * See file LICNSE for license information
7  *
8  * src/workspaces.c: Maintaining the workspace-lists
9  *
10  */
11 #include <string.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <errno.h>
15 #include <yajl/yajl_parse.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 booleant-value (visible, focused, urgent)
29  *
30  */
31 static int workspaces_boolean_cb(void *params_, bool 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 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, unsigned int len) {
103
104         struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
105
106         char *output_name;
107
108         if (!strcmp(params->cur_key, "name")) {
109             /* Save the name */
110             params->workspaces_walk->name = malloc(sizeof(const unsigned char) * (len + 1));
111             strncpy(params->workspaces_walk->name, (const char*) val, len);
112             params->workspaces_walk->name[len] = '\0';
113
114             /* Convert the name to ucs2, save it's length in glyphs and calculate it'srendered width */
115             int ucs2_len;
116             xcb_char2b_t *ucs2_name = (xcb_char2b_t*) convert_utf8_to_ucs2(params->workspaces_walk->name, &ucs2_len);
117             params->workspaces_walk->ucs2_name = ucs2_name;
118             params->workspaces_walk->name_glyphs = ucs2_len;
119             params->workspaces_walk->name_width =
120                 predict_text_extents(params->workspaces_walk->ucs2_name,
121                 params->workspaces_walk->name_glyphs);
122
123             DLOG("Got Workspace %s, name_width: %d, glyphs: %d\n",
124                  params->workspaces_walk->name,
125                  params->workspaces_walk->name_width,
126                  params->workspaces_walk->name_glyphs);
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 = malloc(sizeof(const unsigned char) * (len + 1));
135             strncpy(output_name, (const char*) val, len);
136             output_name[len] = '\0';
137             params->workspaces_walk->output = get_output_by_name(output_name);
138
139             TAILQ_INSERT_TAIL(params->workspaces_walk->output->workspaces,
140                               params->workspaces_walk,
141                               tailq);
142
143             FREE(output_name);
144             return 1;
145         }
146
147         return 0;
148 }
149
150 /*
151  * We hit the start of a json-map (rect or a new output)
152  *
153  */
154 static int workspaces_start_map_cb(void *params_) {
155     struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
156
157     i3_ws *new_workspace = NULL;
158
159     if (params->cur_key == NULL) {
160         new_workspace = malloc(sizeof(i3_ws));
161         new_workspace->num = -1;
162         new_workspace->name = NULL;
163         new_workspace->visible = 0;
164         new_workspace->focused = 0;
165         new_workspace->urgent = 0;
166         memset(&new_workspace->rect, 0, sizeof(rect));
167         new_workspace->output = NULL;
168
169         params->workspaces_walk = new_workspace;
170         return 1;
171     }
172
173     return 1;
174 }
175
176 /*
177  * Parse a key.
178  *
179  * Essentially we just save it in the parsing-state
180  *
181  */
182 static int workspaces_map_key_cb(void *params_, const unsigned char *keyVal, unsigned int keyLen) {
183     struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
184     FREE(params->cur_key);
185
186     params->cur_key = malloc(sizeof(unsigned char) * (keyLen + 1));
187     if (params->cur_key == NULL) {
188         ELOG("Could not allocate memory: %s\n", strerror(errno));
189         exit(EXIT_FAILURE);
190     }
191     strncpy(params->cur_key, (const char*) keyVal, keyLen);
192     params->cur_key[keyLen] = '\0';
193
194     return 1;
195 }
196
197 /* A datastructure to pass all these callbacks to yajl */
198 yajl_callbacks workspaces_callbacks = {
199     NULL,
200     &workspaces_boolean_cb,
201     &workspaces_integer_cb,
202     NULL,
203     NULL,
204     &workspaces_string_cb,
205     &workspaces_start_map_cb,
206     &workspaces_map_key_cb,
207     NULL,
208     NULL,
209     NULL
210 };
211
212 /*
213  * Start parsing the received json-string
214  *
215  */
216 void parse_workspaces_json(char *json) {
217     /* FIXME: Fasciliate stream-processing, i.e. allow starting to interpret
218      * JSON in chunks */
219     struct workspaces_json_params params;
220
221     free_workspaces();
222
223     params.workspaces_walk = NULL;
224     params.cur_key = NULL;
225     params.json = json;
226
227     yajl_handle handle;
228     yajl_parser_config parse_conf = { 0, 0 };
229     yajl_status state;
230
231     handle = yajl_alloc(&workspaces_callbacks, &parse_conf, NULL, (void*) &params);
232
233     state = yajl_parse(handle, (const unsigned char*) json, strlen(json));
234
235     /* FIXME: Propper errorhandling for JSON-parsing */
236     switch (state) {
237         case yajl_status_ok:
238             break;
239         case yajl_status_client_canceled:
240         case yajl_status_insufficient_data:
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
254  *
255  */
256 void free_workspaces() {
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                 FREE(ws_walk->name);
267                 FREE(ws_walk->ucs2_name);
268             }
269             FREE_TAILQ(outputs_walk->workspaces, i3_ws);
270         }
271     }
272 }