2 * i3bar - an xcb-based status- and ws-bar for i3
4 * © 2010 Axel Wagner and contributors
6 * See file LICNSE for license information
8 * src/workspaces.c: Maintaining the workspace-lists
14 #include <yajl/yajl_parse.h>
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;
27 * Parse a booleant-value (visible, focused, urgent)
30 static int workspaces_boolean_cb(void *params_, bool val) {
31 struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
33 if (!strcmp(params->cur_key, "visible")) {
34 params->workspaces_walk->visible = val;
35 FREE(params->cur_key);
39 if (!strcmp(params->cur_key, "focused")) {
40 params->workspaces_walk->focused = val;
41 FREE(params->cur_key);
45 if (!strcmp(params->cur_key, "urgent")) {
46 params->workspaces_walk->urgent = val;
47 FREE(params->cur_key);
51 FREE(params->cur_key);
57 * Parse an integer (num or the rect)
60 static int workspaces_integer_cb(void *params_, long val) {
61 struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
63 if (!strcmp(params->cur_key, "num")) {
64 params->workspaces_walk->num = (int) val;
65 FREE(params->cur_key);
69 if (!strcmp(params->cur_key, "x")) {
70 params->workspaces_walk->rect.x = (int) val;
71 FREE(params->cur_key);
75 if (!strcmp(params->cur_key, "y")) {
76 params->workspaces_walk->rect.y = (int) val;
77 FREE(params->cur_key);
81 if (!strcmp(params->cur_key, "width")) {
82 params->workspaces_walk->rect.w = (int) val;
83 FREE(params->cur_key);
87 if (!strcmp(params->cur_key, "height")) {
88 params->workspaces_walk->rect.h = (int) val;
89 FREE(params->cur_key);
93 FREE(params->cur_key);
98 * Parse a string (name, output)
101 static int workspaces_string_cb(void *params_, const unsigned char *val, unsigned int len) {
103 struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
107 if (!strcmp(params->cur_key, "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';
113 /* Convert the name to ucs2, save it's length in glyphs and calculate it'srendered width */
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);
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);
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);
137 TAILQ_INSERT_TAIL(params->workspaces_walk->output->workspaces,
138 params->workspaces_walk,
149 * We hit the start of a json-map (rect or a new output)
152 static int workspaces_start_map_cb(void *params_) {
153 struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
155 i3_ws *new_workspace = NULL;
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;
167 params->workspaces_walk = new_workspace;
177 * Essentially we just save it in the parsing-state
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);
184 params->cur_key = malloc(sizeof(unsigned char) * (keyLen + 1));
185 if (params->cur_key == NULL) {
186 printf("ERROR: Could not allocate memory!\n");
189 strncpy(params->cur_key, (const char*) keyVal, keyLen);
190 params->cur_key[keyLen] = '\0';
195 /* A datastructure to pass all these callbacks to yajl */
196 yajl_callbacks workspaces_callbacks = {
198 &workspaces_boolean_cb,
199 &workspaces_integer_cb,
202 &workspaces_string_cb,
203 &workspaces_start_map_cb,
204 &workspaces_map_key_cb,
211 * Start parsing the received json-string
214 void parse_workspaces_json(char *json) {
215 /* FIXME: Fasciliate stream-processing, i.e. allow starting to interpret
217 struct workspaces_json_params params;
221 params.workspaces_walk = NULL;
222 params.cur_key = NULL;
226 yajl_parser_config parse_conf = { 0, 0 };
229 handle = yajl_alloc(&workspaces_callbacks, &parse_conf, NULL, (void*) ¶ms);
231 state = yajl_parse(handle, (const unsigned char*) json, strlen(json));
233 /* FIXME: Propper errorhandling for JSON-parsing */
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");
247 FREE(params.cur_key);
251 * free() all workspace data-structures
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);