]> git.sur5r.net Git - i3/i3/blob - i3bar/src/workspaces.c
Merge branch 'next'
[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 <yajl/yajl_parse.h>
15
16 #include "common.h"
17
18 /* A datatype to pass through the callbacks to save the state */
19 struct workspaces_json_params {
20     struct ws_head *workspaces;
21     i3_ws          *workspaces_walk;
22     char           *cur_key;
23     char           *json;
24 };
25
26 /*
27  * Parse a booleant-value (visible, focused, urgent)
28  *
29  */
30 static int workspaces_boolean_cb(void *params_, bool val) {
31     struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
32
33     if (!strcmp(params->cur_key, "visible")) {
34         params->workspaces_walk->visible = val;
35         FREE(params->cur_key);
36         return 1;
37     }
38
39     if (!strcmp(params->cur_key, "focused")) {
40         params->workspaces_walk->focused = val;
41         FREE(params->cur_key);
42         return 1;
43     }
44
45     if (!strcmp(params->cur_key, "urgent")) {
46         params->workspaces_walk->urgent = val;
47         FREE(params->cur_key);
48         return 1;
49     }
50
51     FREE(params->cur_key);
52
53     return 0;
54 }
55
56 /*
57  * Parse an integer (num or the rect)
58  *
59  */
60 static int workspaces_integer_cb(void *params_, long val) {
61     struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
62
63     if (!strcmp(params->cur_key, "num")) {
64         params->workspaces_walk->num = (int) val;
65         FREE(params->cur_key);
66         return 1;
67     }
68
69     if (!strcmp(params->cur_key, "x")) {
70         params->workspaces_walk->rect.x = (int) val;
71         FREE(params->cur_key);
72         return 1;
73     }
74
75     if (!strcmp(params->cur_key, "y")) {
76         params->workspaces_walk->rect.y = (int) val;
77         FREE(params->cur_key);
78         return 1;
79     }
80
81     if (!strcmp(params->cur_key, "width")) {
82         params->workspaces_walk->rect.w = (int) val;
83         FREE(params->cur_key);
84         return 1;
85     }
86
87     if (!strcmp(params->cur_key, "height")) {
88         params->workspaces_walk->rect.h = (int) val;
89         FREE(params->cur_key);
90         return 1;
91     }
92
93     FREE(params->cur_key);
94     return 0;
95 }
96
97 /*
98  * Parse a string (name, output)
99  *
100  */
101 static int workspaces_string_cb(void *params_, const unsigned char *val, unsigned int len) {
102
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             /* Save the name */
109             params->workspaces_walk->name = malloc(sizeof(const unsigned char) * (len + 1));
110             strncpy(params->workspaces_walk->name, (const char*) val, len);
111             params->workspaces_walk->name[len] = '\0';
112
113             /* Convert the name to ucs2, save it's length in glyphs and calculate it'srendered width */
114             int ucs2_len;
115             xcb_char2b_t *ucs2_name = (xcb_char2b_t*) convert_utf8_to_ucs2(params->workspaces_walk->name, &ucs2_len);
116             params->workspaces_walk->ucs2_name = ucs2_name;
117             params->workspaces_walk->name_glyphs = ucs2_len;
118             params->workspaces_walk->name_width =
119                 predict_text_extents(params->workspaces_walk->ucs2_name,
120                 params->workspaces_walk->name_glyphs);
121
122             DLOG("Got Workspace %s, name_width: %d, glyphs: %d\n",
123                  params->workspaces_walk->name,
124                  params->workspaces_walk->name_width,
125                  params->workspaces_walk->name_glyphs);
126             FREE(params->cur_key);
127
128             return 1;
129         }
130
131         if (!strcmp(params->cur_key, "output")) {
132             /* We add the ws to the TAILQ of the output, it belongs to */
133             output_name = malloc(sizeof(const unsigned char) * (len + 1));
134             strncpy(output_name, (const char*) val, len);
135             output_name[len] = '\0';
136             params->workspaces_walk->output = get_output_by_name(output_name);
137
138             TAILQ_INSERT_TAIL(params->workspaces_walk->output->workspaces,
139                               params->workspaces_walk,
140                               tailq);
141
142             FREE(output_name);
143             return 1;
144         }
145
146         return 0;
147 }
148
149 /*
150  * We hit the start of a json-map (rect or a new output)
151  *
152  */
153 static int workspaces_start_map_cb(void *params_) {
154     struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
155
156     i3_ws *new_workspace = NULL;
157
158     if (params->cur_key == NULL) {
159         new_workspace = malloc(sizeof(i3_ws));
160         new_workspace->num = -1;
161         new_workspace->name = NULL;
162         new_workspace->visible = 0;
163         new_workspace->focused = 0;
164         new_workspace->urgent = 0;
165         memset(&new_workspace->rect, 0, sizeof(rect));
166         new_workspace->output = NULL;
167
168         params->workspaces_walk = new_workspace;
169         return 1;
170     }
171
172     return 1;
173 }
174
175 /*
176  * Parse a key.
177  *
178  * Essentially we just save it in the parsing-state
179  *
180  */
181 static int workspaces_map_key_cb(void *params_, const unsigned char *keyVal, unsigned int keyLen) {
182     struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
183     FREE(params->cur_key);
184
185     params->cur_key = malloc(sizeof(unsigned char) * (keyLen + 1));
186     if (params->cur_key == NULL) {
187         ELOG("Could not allocate memory!\n");
188         exit(EXIT_FAILURE);
189     }
190     strncpy(params->cur_key, (const char*) keyVal, keyLen);
191     params->cur_key[keyLen] = '\0';
192
193     return 1;
194 }
195
196 /* A datastructure to pass all these callbacks to yajl */
197 yajl_callbacks workspaces_callbacks = {
198     NULL,
199     &workspaces_boolean_cb,
200     &workspaces_integer_cb,
201     NULL,
202     NULL,
203     &workspaces_string_cb,
204     &workspaces_start_map_cb,
205     &workspaces_map_key_cb,
206     NULL,
207     NULL,
208     NULL
209 };
210
211 /*
212  * Start parsing the received json-string
213  *
214  */
215 void parse_workspaces_json(char *json) {
216     /* FIXME: Fasciliate stream-processing, i.e. allow starting to interpret
217      * JSON in chunks */
218     struct workspaces_json_params params;
219
220     free_workspaces();
221
222     params.workspaces_walk = NULL;
223     params.cur_key = NULL;
224     params.json = json;
225
226     yajl_handle handle;
227     yajl_parser_config parse_conf = { 0, 0 };
228     yajl_status state;
229
230     handle = yajl_alloc(&workspaces_callbacks, &parse_conf, NULL, (void*) &params);
231
232     state = yajl_parse(handle, (const unsigned char*) json, strlen(json));
233
234     /* FIXME: Propper errorhandling for JSON-parsing */
235     switch (state) {
236         case yajl_status_ok:
237             break;
238         case yajl_status_client_canceled:
239         case yajl_status_insufficient_data:
240         case yajl_status_error:
241             ELOG("Could not parse workspaces-reply!\n");
242             exit(EXIT_FAILURE);
243             break;
244     }
245
246     yajl_free(handle);
247
248     FREE(params.cur_key);
249 }
250
251 /*
252  * free() all workspace data-structures
253  *
254  */
255 void free_workspaces() {
256     i3_output *outputs_walk;
257     SLIST_FOREACH(outputs_walk, outputs, slist) {
258         if (outputs_walk->workspaces != NULL && !TAILQ_EMPTY(outputs_walk->workspaces)) {
259             FREE_TAILQ(outputs_walk->workspaces, i3_ws);
260         }
261     }
262 }