2 * i3bar - an xcb-based status- and ws-bar for i3
4 * © 2010-2011 Axel Wagner and contributors
6 * See file LICNSE for license information
8 * src/workspaces.c: Maintaining the workspace-lists
15 #include <yajl/yajl_parse.h>
16 #include <yajl/yajl_version.h>
20 /* A datatype to pass through the callbacks to save the state */
21 struct workspaces_json_params {
22 struct ws_head *workspaces;
23 i3_ws *workspaces_walk;
29 * Parse a booleant-value (visible, focused, urgent)
32 static int workspaces_boolean_cb(void *params_, bool val) {
33 struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
35 if (!strcmp(params->cur_key, "visible")) {
36 params->workspaces_walk->visible = val;
37 FREE(params->cur_key);
41 if (!strcmp(params->cur_key, "focused")) {
42 params->workspaces_walk->focused = val;
43 FREE(params->cur_key);
47 if (!strcmp(params->cur_key, "urgent")) {
48 params->workspaces_walk->urgent = val;
49 FREE(params->cur_key);
53 FREE(params->cur_key);
59 * Parse an integer (num or the rect)
63 static int workspaces_integer_cb(void *params_, long long val) {
65 static int workspaces_integer_cb(void *params_, long val) {
67 struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
69 if (!strcmp(params->cur_key, "num")) {
70 params->workspaces_walk->num = (int) val;
71 FREE(params->cur_key);
75 if (!strcmp(params->cur_key, "x")) {
76 params->workspaces_walk->rect.x = (int) val;
77 FREE(params->cur_key);
81 if (!strcmp(params->cur_key, "y")) {
82 params->workspaces_walk->rect.y = (int) val;
83 FREE(params->cur_key);
87 if (!strcmp(params->cur_key, "width")) {
88 params->workspaces_walk->rect.w = (int) val;
89 FREE(params->cur_key);
93 if (!strcmp(params->cur_key, "height")) {
94 params->workspaces_walk->rect.h = (int) val;
95 FREE(params->cur_key);
99 FREE(params->cur_key);
104 * Parse a string (name, output)
108 static int workspaces_string_cb(void *params_, const unsigned char *val, size_t len) {
110 static int workspaces_string_cb(void *params_, const unsigned char *val, unsigned int len) {
112 struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
116 if (!strcmp(params->cur_key, "name")) {
118 params->workspaces_walk->name = malloc(sizeof(const unsigned char) * (len + 1));
119 strncpy(params->workspaces_walk->name, (const char*) val, len);
120 params->workspaces_walk->name[len] = '\0';
122 /* Convert the name to ucs2, save it's length in glyphs and calculate it'srendered width */
124 xcb_char2b_t *ucs2_name = (xcb_char2b_t*) convert_utf8_to_ucs2(params->workspaces_walk->name, &ucs2_len);
125 params->workspaces_walk->ucs2_name = ucs2_name;
126 params->workspaces_walk->name_glyphs = ucs2_len;
127 params->workspaces_walk->name_width =
128 predict_text_extents(params->workspaces_walk->ucs2_name,
129 params->workspaces_walk->name_glyphs);
131 DLOG("Got Workspace %s, name_width: %d, glyphs: %d\n",
132 params->workspaces_walk->name,
133 params->workspaces_walk->name_width,
134 params->workspaces_walk->name_glyphs);
135 FREE(params->cur_key);
140 if (!strcmp(params->cur_key, "output")) {
141 /* We add the ws to the TAILQ of the output, it belongs to */
142 output_name = malloc(sizeof(const unsigned char) * (len + 1));
143 strncpy(output_name, (const char*) val, len);
144 output_name[len] = '\0';
145 params->workspaces_walk->output = get_output_by_name(output_name);
147 TAILQ_INSERT_TAIL(params->workspaces_walk->output->workspaces,
148 params->workspaces_walk,
159 * We hit the start of a json-map (rect or a new output)
162 static int workspaces_start_map_cb(void *params_) {
163 struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
165 i3_ws *new_workspace = NULL;
167 if (params->cur_key == NULL) {
168 new_workspace = malloc(sizeof(i3_ws));
169 new_workspace->num = -1;
170 new_workspace->name = NULL;
171 new_workspace->visible = 0;
172 new_workspace->focused = 0;
173 new_workspace->urgent = 0;
174 memset(&new_workspace->rect, 0, sizeof(rect));
175 new_workspace->output = NULL;
177 params->workspaces_walk = new_workspace;
187 * Essentially we just save it in the parsing-state
191 static int workspaces_map_key_cb(void *params_, const unsigned char *keyVal, size_t keyLen) {
193 static int workspaces_map_key_cb(void *params_, const unsigned char *keyVal, unsigned int keyLen) {
195 struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
196 FREE(params->cur_key);
198 params->cur_key = malloc(sizeof(unsigned char) * (keyLen + 1));
199 if (params->cur_key == NULL) {
200 ELOG("Could not allocate memory: %s\n", strerror(errno));
203 strncpy(params->cur_key, (const char*) keyVal, keyLen);
204 params->cur_key[keyLen] = '\0';
209 /* A datastructure to pass all these callbacks to yajl */
210 yajl_callbacks workspaces_callbacks = {
212 &workspaces_boolean_cb,
213 &workspaces_integer_cb,
216 &workspaces_string_cb,
217 &workspaces_start_map_cb,
218 &workspaces_map_key_cb,
225 * Start parsing the received json-string
228 void parse_workspaces_json(char *json) {
229 /* FIXME: Fasciliate stream-processing, i.e. allow starting to interpret
231 struct workspaces_json_params params;
235 params.workspaces_walk = NULL;
236 params.cur_key = NULL;
242 yajl_parser_config parse_conf = { 0, 0 };
244 handle = yajl_alloc(&workspaces_callbacks, &parse_conf, NULL, (void*) ¶ms);
246 handle = yajl_alloc(&workspaces_callbacks, NULL, (void*) ¶ms);
249 state = yajl_parse(handle, (const unsigned char*) json, strlen(json));
251 /* FIXME: Propper errorhandling for JSON-parsing */
255 case yajl_status_client_canceled:
257 case yajl_status_insufficient_data:
259 case yajl_status_error:
260 ELOG("Could not parse workspaces-reply!\n");
267 FREE(params.cur_key);
271 * free() all workspace data-structures. Does not free() the heads of the tailqueues.
274 void free_workspaces() {
275 i3_output *outputs_walk;
276 if (outputs == NULL) {
281 SLIST_FOREACH(outputs_walk, outputs, slist) {
282 if (outputs_walk->workspaces != NULL && !TAILQ_EMPTY(outputs_walk->workspaces)) {
283 TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
285 FREE(ws_walk->ucs2_name);
287 FREE_TAILQ(outputs_walk->workspaces, i3_ws);