2 * vim:ts=4:sw=4:expandtab
4 * i3bar - an xcb-based status- and ws-bar for i3
5 * © 2010-2012 Axel Wagner and contributors (see also: LICENSE)
7 * workspaces.c: Maintaining the workspace-lists
14 #include <yajl/yajl_parse.h>
15 #include <yajl/yajl_version.h>
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;
28 * Parse a boolean value (visible, focused, urgent)
31 static int workspaces_boolean_cb(void *params_, int val) {
32 struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
34 if (!strcmp(params->cur_key, "visible")) {
35 params->workspaces_walk->visible = val;
36 FREE(params->cur_key);
40 if (!strcmp(params->cur_key, "focused")) {
41 params->workspaces_walk->focused = val;
42 FREE(params->cur_key);
46 if (!strcmp(params->cur_key, "urgent")) {
47 params->workspaces_walk->urgent = val;
48 FREE(params->cur_key);
52 FREE(params->cur_key);
58 * Parse an integer (num or the rect)
61 static int workspaces_integer_cb(void *params_, long long val) {
62 struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
64 if (!strcmp(params->cur_key, "num")) {
65 params->workspaces_walk->num = (int) val;
66 FREE(params->cur_key);
70 if (!strcmp(params->cur_key, "x")) {
71 params->workspaces_walk->rect.x = (int) val;
72 FREE(params->cur_key);
76 if (!strcmp(params->cur_key, "y")) {
77 params->workspaces_walk->rect.y = (int) val;
78 FREE(params->cur_key);
82 if (!strcmp(params->cur_key, "width")) {
83 params->workspaces_walk->rect.w = (int) val;
84 FREE(params->cur_key);
88 if (!strcmp(params->cur_key, "height")) {
89 params->workspaces_walk->rect.h = (int) val;
90 FREE(params->cur_key);
94 FREE(params->cur_key);
99 * Parse a string (name, output)
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_;
107 if (!strcmp(params->cur_key, "name")) {
109 params->workspaces_walk->name = i3string_from_utf8_with_length((const char *)val, len);
111 /* Save its rendered width */
112 params->workspaces_walk->name_width =
113 predict_text_width(params->workspaces_walk->name);
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);
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);
131 params->workspaces_walk->output = target;
133 TAILQ_INSERT_TAIL(params->workspaces_walk->output->workspaces,
134 params->workspaces_walk,
146 * We hit the start of a json-map (rect or a new output)
149 static int workspaces_start_map_cb(void *params_) {
150 struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
152 i3_ws *new_workspace = NULL;
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;
164 params->workspaces_walk = new_workspace;
174 * Essentially we just save it in the parsing-state
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);
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';
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,
198 * Start parsing the received json-string
201 void parse_workspaces_json(char *json) {
202 /* FIXME: Fasciliate stream-processing, i.e. allow starting to interpret
204 struct workspaces_json_params params;
208 params.workspaces_walk = NULL;
209 params.cur_key = NULL;
214 handle = yajl_alloc(&workspaces_callbacks, NULL, (void*) ¶ms);
216 state = yajl_parse(handle, (const unsigned char*) json, strlen(json));
218 /* FIXME: Propper errorhandling for JSON-parsing */
222 case yajl_status_client_canceled:
223 case yajl_status_error:
224 ELOG("Could not parse workspaces-reply!\n");
231 FREE(params.cur_key);
235 * free() all workspace data-structures. Does not free() the heads of the tailqueues.
238 void free_workspaces(void) {
239 i3_output *outputs_walk;
240 if (outputs == NULL) {
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);
250 FREE_TAILQ(outputs_walk->workspaces, i3_ws);