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)
62 static int workspaces_integer_cb(void *params_, long long val) {
64 static int workspaces_integer_cb(void *params_, long val) {
66 struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
68 if (!strcmp(params->cur_key, "num")) {
69 params->workspaces_walk->num = (int) val;
70 FREE(params->cur_key);
74 if (!strcmp(params->cur_key, "x")) {
75 params->workspaces_walk->rect.x = (int) val;
76 FREE(params->cur_key);
80 if (!strcmp(params->cur_key, "y")) {
81 params->workspaces_walk->rect.y = (int) val;
82 FREE(params->cur_key);
86 if (!strcmp(params->cur_key, "width")) {
87 params->workspaces_walk->rect.w = (int) val;
88 FREE(params->cur_key);
92 if (!strcmp(params->cur_key, "height")) {
93 params->workspaces_walk->rect.h = (int) val;
94 FREE(params->cur_key);
98 FREE(params->cur_key);
103 * Parse a string (name, output)
107 static int workspaces_string_cb(void *params_, const unsigned char *val, size_t len) {
109 static int workspaces_string_cb(void *params_, const unsigned char *val, unsigned int len) {
111 struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
115 if (!strcmp(params->cur_key, "name")) {
117 params->workspaces_walk->name = i3string_from_utf8_with_length((const char *)val, len);
119 /* Save its rendered width */
120 params->workspaces_walk->name_width =
121 predict_text_width(params->workspaces_walk->name);
123 DLOG("Got Workspace %s, name_width: %d, glyphs: %zu\n",
124 i3string_as_utf8(params->workspaces_walk->name),
125 params->workspaces_walk->name_width,
126 i3string_get_num_glyphs(params->workspaces_walk->name));
127 FREE(params->cur_key);
132 if (!strcmp(params->cur_key, "output")) {
133 /* We add the ws to the TAILQ of the output, it belongs to */
134 output_name = smalloc(sizeof(const unsigned char) * (len + 1));
135 strncpy(output_name, (const char*) val, len);
136 output_name[len] = '\0';
137 i3_output *target = get_output_by_name(output_name);
139 params->workspaces_walk->output = target;
141 TAILQ_INSERT_TAIL(params->workspaces_walk->output->workspaces,
142 params->workspaces_walk,
154 * We hit the start of a json-map (rect or a new output)
157 static int workspaces_start_map_cb(void *params_) {
158 struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
160 i3_ws *new_workspace = NULL;
162 if (params->cur_key == NULL) {
163 new_workspace = smalloc(sizeof(i3_ws));
164 new_workspace->num = -1;
165 new_workspace->name = NULL;
166 new_workspace->visible = 0;
167 new_workspace->focused = 0;
168 new_workspace->urgent = 0;
169 memset(&new_workspace->rect, 0, sizeof(rect));
170 new_workspace->output = NULL;
172 params->workspaces_walk = new_workspace;
182 * Essentially we just save it in the parsing-state
186 static int workspaces_map_key_cb(void *params_, const unsigned char *keyVal, size_t keyLen) {
188 static int workspaces_map_key_cb(void *params_, const unsigned char *keyVal, unsigned int keyLen) {
190 struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
191 FREE(params->cur_key);
193 params->cur_key = smalloc(sizeof(unsigned char) * (keyLen + 1));
194 strncpy(params->cur_key, (const char*) keyVal, keyLen);
195 params->cur_key[keyLen] = '\0';
200 /* A datastructure to pass all these callbacks to yajl */
201 static yajl_callbacks workspaces_callbacks = {
202 .yajl_boolean = workspaces_boolean_cb,
203 .yajl_integer = workspaces_integer_cb,
204 .yajl_string = workspaces_string_cb,
205 .yajl_start_map = workspaces_start_map_cb,
206 .yajl_map_key = workspaces_map_key_cb,
210 * Start parsing the received json-string
213 void parse_workspaces_json(char *json) {
214 /* FIXME: Fasciliate stream-processing, i.e. allow starting to interpret
216 struct workspaces_json_params params;
220 params.workspaces_walk = NULL;
221 params.cur_key = NULL;
227 yajl_parser_config parse_conf = { 0, 0 };
229 handle = yajl_alloc(&workspaces_callbacks, &parse_conf, NULL, (void*) ¶ms);
231 handle = yajl_alloc(&workspaces_callbacks, NULL, (void*) ¶ms);
234 state = yajl_parse(handle, (const unsigned char*) json, strlen(json));
236 /* FIXME: Propper errorhandling for JSON-parsing */
240 case yajl_status_client_canceled:
242 case yajl_status_insufficient_data:
244 case yajl_status_error:
245 ELOG("Could not parse workspaces-reply!\n");
252 FREE(params.cur_key);
256 * free() all workspace data-structures. Does not free() the heads of the tailqueues.
259 void free_workspaces(void) {
260 i3_output *outputs_walk;
261 if (outputs == NULL) {
266 SLIST_FOREACH(outputs_walk, outputs, slist) {
267 if (outputs_walk->workspaces != NULL && !TAILQ_EMPTY(outputs_walk->workspaces)) {
268 TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
269 I3STRING_FREE(ws_walk->name);
271 FREE_TAILQ(outputs_walk->workspaces, i3_ws);