]> git.sur5r.net Git - i3/i3/blob - i3bar/src/workspaces.c
Add licensing information
[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 = get_string_width(params->workspaces_walk->ucs2_name,
119                                                                    params->workspaces_walk->name_glyphs);
120
121             printf("Got Workspace %s, name_width: %d, glyphs: %d\n",
122                    params->workspaces_walk->name,
123                    params->workspaces_walk->name_width,
124                    params->workspaces_walk->name_glyphs);
125             FREE(params->cur_key);
126
127             return 1;
128         }
129
130         if (!strcmp(params->cur_key, "output")) {
131             /* We add the ws to the TAILQ of the output, it belongs to */
132             output_name = malloc(sizeof(const unsigned char) * (len + 1));
133             strncpy(output_name, (const char*) val, len);
134             output_name[len] = '\0';
135             params->workspaces_walk->output = get_output_by_name(output_name);
136
137             TAILQ_INSERT_TAIL(params->workspaces_walk->output->workspaces,
138                               params->workspaces_walk,
139                               tailq);
140
141             FREE(output_name);
142             return 1;
143         }
144
145         return 0;
146 }
147
148 /*
149  * We hit the start of a json-map (rect or a new output)
150  *
151  */
152 static int workspaces_start_map_cb(void *params_) {
153     struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
154
155     i3_ws *new_workspace = NULL;
156
157     if (params->cur_key == NULL) {
158         new_workspace = malloc(sizeof(i3_ws));
159         new_workspace->num = -1;
160         new_workspace->name = NULL;
161         new_workspace->visible = 0;
162         new_workspace->focused = 0;
163         new_workspace->urgent = 0;
164         memset(&new_workspace->rect, 0, sizeof(rect));
165         new_workspace->output = NULL;
166
167         params->workspaces_walk = new_workspace;
168         return 1;
169     }
170
171     return 1;
172 }
173
174 /*
175  * Parse a key.
176  *
177  * Essentially we just save it in the parsing-state
178  *
179  */
180 static int workspaces_map_key_cb(void *params_, const unsigned char *keyVal, unsigned int keyLen) {
181     struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
182     FREE(params->cur_key);
183
184     params->cur_key = malloc(sizeof(unsigned char) * (keyLen + 1));
185     if (params->cur_key == NULL) {
186         printf("ERROR: Could not allocate memory!\n");
187         exit(EXIT_FAILURE);
188     }
189     strncpy(params->cur_key, (const char*) keyVal, keyLen);
190     params->cur_key[keyLen] = '\0';
191
192     return 1;
193 }
194
195 /* A datastructure to pass all these callbacks to yajl */
196 yajl_callbacks workspaces_callbacks = {
197     NULL,
198     &workspaces_boolean_cb,
199     &workspaces_integer_cb,
200     NULL,
201     NULL,
202     &workspaces_string_cb,
203     &workspaces_start_map_cb,
204     &workspaces_map_key_cb,
205     NULL,
206     NULL,
207     NULL
208 };
209
210 /*
211  * Start parsing the received json-string
212  *
213  */
214 void parse_workspaces_json(char *json) {
215     /* FIXME: Fasciliate stream-processing, i.e. allow starting to interpret
216      * JSON in chunks */
217     struct workspaces_json_params params;
218
219     free_workspaces();
220
221     params.workspaces_walk = NULL;
222     params.cur_key = NULL;
223     params.json = json;
224
225     yajl_handle handle;
226     yajl_parser_config parse_conf = { 0, 0 };
227     yajl_status state;
228
229     handle = yajl_alloc(&workspaces_callbacks, &parse_conf, NULL, (void*) &params);
230
231     state = yajl_parse(handle, (const unsigned char*) json, strlen(json));
232
233     /* FIXME: Propper errorhandling for JSON-parsing */
234     switch (state) {
235         case yajl_status_ok:
236             break;
237         case yajl_status_client_canceled:
238         case yajl_status_insufficient_data:
239         case yajl_status_error:
240             printf("ERROR: Could not parse workspaces-reply!\n");
241             exit(EXIT_FAILURE);
242             break;
243     }
244
245     yajl_free(handle);
246
247     FREE(params.cur_key);
248 }
249
250 /*
251  * free() all workspace data-structures
252  *
253  */
254 void free_workspaces() {
255     i3_output *outputs_walk;
256     SLIST_FOREACH(outputs_walk, outputs, slist) {
257         if (outputs_walk->workspaces != NULL && !TAILQ_EMPTY(outputs_walk->workspaces)) {
258             FREE_TAILQ(outputs_walk->workspaces, i3_ws);
259         }
260     }
261 }