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