2 * vim:ts=4:sw=4:expandtab
4 * i3bar - an xcb-based status- and ws-bar for i3
6 * © 2010-2011 Axel Wagner and contributors
8 * See file LICNSE for license information
10 * src/workspaces.c: Maintaining the workspace-lists
17 #include <yajl/yajl_parse.h>
18 #include <yajl/yajl_version.h>
22 /* A datatype to pass through the callbacks to save the state */
23 struct workspaces_json_params {
24 struct ws_head *workspaces;
25 i3_ws *workspaces_walk;
31 * Parse a boolean value (visible, focused, urgent)
34 static int workspaces_boolean_cb(void *params_, int val) {
35 struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
37 if (!strcmp(params->cur_key, "visible")) {
38 params->workspaces_walk->visible = val;
39 FREE(params->cur_key);
43 if (!strcmp(params->cur_key, "focused")) {
44 params->workspaces_walk->focused = val;
45 FREE(params->cur_key);
49 if (!strcmp(params->cur_key, "urgent")) {
50 params->workspaces_walk->urgent = val;
51 FREE(params->cur_key);
55 FREE(params->cur_key);
61 * Parse an integer (num or the rect)
65 static int workspaces_integer_cb(void *params_, long long val) {
67 static int workspaces_integer_cb(void *params_, long val) {
69 struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
71 if (!strcmp(params->cur_key, "num")) {
72 params->workspaces_walk->num = (int) val;
73 FREE(params->cur_key);
77 if (!strcmp(params->cur_key, "x")) {
78 params->workspaces_walk->rect.x = (int) val;
79 FREE(params->cur_key);
83 if (!strcmp(params->cur_key, "y")) {
84 params->workspaces_walk->rect.y = (int) val;
85 FREE(params->cur_key);
89 if (!strcmp(params->cur_key, "width")) {
90 params->workspaces_walk->rect.w = (int) val;
91 FREE(params->cur_key);
95 if (!strcmp(params->cur_key, "height")) {
96 params->workspaces_walk->rect.h = (int) val;
97 FREE(params->cur_key);
101 FREE(params->cur_key);
106 * Parse a string (name, output)
110 static int workspaces_string_cb(void *params_, const unsigned char *val, size_t len) {
112 static int workspaces_string_cb(void *params_, const unsigned char *val, unsigned int len) {
114 struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
118 if (!strcmp(params->cur_key, "name")) {
120 params->workspaces_walk->name = smalloc(sizeof(const unsigned char) * (len + 1));
121 strncpy(params->workspaces_walk->name, (const char*) val, len);
122 params->workspaces_walk->name[len] = '\0';
124 /* Convert the name to ucs2, save its length in glyphs and calculate its rendered width */
126 xcb_char2b_t *ucs2_name = (xcb_char2b_t*) convert_utf8_to_ucs2(params->workspaces_walk->name, &ucs2_len);
127 params->workspaces_walk->ucs2_name = ucs2_name;
128 params->workspaces_walk->name_glyphs = ucs2_len;
129 params->workspaces_walk->name_width =
130 predict_text_extents(params->workspaces_walk->ucs2_name,
131 params->workspaces_walk->name_glyphs);
133 DLOG("Got Workspace %s, name_width: %d, glyphs: %d\n",
134 params->workspaces_walk->name,
135 params->workspaces_walk->name_width,
136 params->workspaces_walk->name_glyphs);
137 FREE(params->cur_key);
142 if (!strcmp(params->cur_key, "output")) {
143 /* We add the ws to the TAILQ of the output, it belongs to */
144 output_name = smalloc(sizeof(const unsigned char) * (len + 1));
145 strncpy(output_name, (const char*) val, len);
146 output_name[len] = '\0';
147 i3_output *target = get_output_by_name(output_name);
149 params->workspaces_walk->output = target;
151 TAILQ_INSERT_TAIL(params->workspaces_walk->output->workspaces,
152 params->workspaces_walk,
164 * We hit the start of a json-map (rect or a new output)
167 static int workspaces_start_map_cb(void *params_) {
168 struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
170 i3_ws *new_workspace = NULL;
172 if (params->cur_key == NULL) {
173 new_workspace = smalloc(sizeof(i3_ws));
174 new_workspace->num = -1;
175 new_workspace->name = NULL;
176 new_workspace->visible = 0;
177 new_workspace->focused = 0;
178 new_workspace->urgent = 0;
179 memset(&new_workspace->rect, 0, sizeof(rect));
180 new_workspace->output = NULL;
182 params->workspaces_walk = new_workspace;
192 * Essentially we just save it in the parsing-state
196 static int workspaces_map_key_cb(void *params_, const unsigned char *keyVal, size_t keyLen) {
198 static int workspaces_map_key_cb(void *params_, const unsigned char *keyVal, unsigned int keyLen) {
200 struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
201 FREE(params->cur_key);
203 params->cur_key = smalloc(sizeof(unsigned char) * (keyLen + 1));
204 strncpy(params->cur_key, (const char*) keyVal, keyLen);
205 params->cur_key[keyLen] = '\0';
210 /* A datastructure to pass all these callbacks to yajl */
211 yajl_callbacks workspaces_callbacks = {
213 &workspaces_boolean_cb,
214 &workspaces_integer_cb,
217 &workspaces_string_cb,
218 &workspaces_start_map_cb,
219 &workspaces_map_key_cb,
226 * Start parsing the received json-string
229 void parse_workspaces_json(char *json) {
230 /* FIXME: Fasciliate stream-processing, i.e. allow starting to interpret
232 struct workspaces_json_params params;
236 params.workspaces_walk = NULL;
237 params.cur_key = NULL;
243 yajl_parser_config parse_conf = { 0, 0 };
245 handle = yajl_alloc(&workspaces_callbacks, &parse_conf, NULL, (void*) ¶ms);
247 handle = yajl_alloc(&workspaces_callbacks, NULL, (void*) ¶ms);
250 state = yajl_parse(handle, (const unsigned char*) json, strlen(json));
252 /* FIXME: Propper errorhandling for JSON-parsing */
256 case yajl_status_client_canceled:
258 case yajl_status_insufficient_data:
260 case yajl_status_error:
261 ELOG("Could not parse workspaces-reply!\n");
268 FREE(params.cur_key);
272 * free() all workspace data-structures. Does not free() the heads of the tailqueues.
275 void free_workspaces() {
276 i3_output *outputs_walk;
277 if (outputs == NULL) {
282 SLIST_FOREACH(outputs_walk, outputs, slist) {
283 if (outputs_walk->workspaces != NULL && !TAILQ_EMPTY(outputs_walk->workspaces)) {
284 TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
286 FREE(ws_walk->ucs2_name);
288 FREE_TAILQ(outputs_walk->workspaces, i3_ws);