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 =
119 predict_text_extents(params->workspaces_walk->ucs2_name,
120 params->workspaces_walk->name_glyphs);
122 printf("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);
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);
138 TAILQ_INSERT_TAIL(params->workspaces_walk->output->workspaces,
139 params->workspaces_walk,
150 * We hit the start of a json-map (rect or a new output)
153 static int workspaces_start_map_cb(void *params_) {
154 struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
156 i3_ws *new_workspace = NULL;
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;
168 params->workspaces_walk = new_workspace;
178 * Essentially we just save it in the parsing-state
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);
185 params->cur_key = malloc(sizeof(unsigned char) * (keyLen + 1));
186 if (params->cur_key == NULL) {
187 printf("ERROR: Could not allocate memory!\n");
190 strncpy(params->cur_key, (const char*) keyVal, keyLen);
191 params->cur_key[keyLen] = '\0';
196 /* A datastructure to pass all these callbacks to yajl */
197 yajl_callbacks workspaces_callbacks = {
199 &workspaces_boolean_cb,
200 &workspaces_integer_cb,
203 &workspaces_string_cb,
204 &workspaces_start_map_cb,
205 &workspaces_map_key_cb,
212 * Start parsing the received json-string
215 void parse_workspaces_json(char *json) {
216 /* FIXME: Fasciliate stream-processing, i.e. allow starting to interpret
218 struct workspaces_json_params params;
222 params.workspaces_walk = NULL;
223 params.cur_key = NULL;
227 yajl_parser_config parse_conf = { 0, 0 };
230 handle = yajl_alloc(&workspaces_callbacks, &parse_conf, NULL, (void*) ¶ms);
232 state = yajl_parse(handle, (const unsigned char*) json, strlen(json));
234 /* FIXME: Propper errorhandling for JSON-parsing */
238 case yajl_status_client_canceled:
239 case yajl_status_insufficient_data:
240 case yajl_status_error:
241 printf("ERROR: Could not parse workspaces-reply!\n");
248 FREE(params.cur_key);
252 * free() all workspace data-structures
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);