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")) {
108 const char *ws_name = (const char *)val;
109 params->workspaces_walk->canonical_name = strndup(ws_name, len);
111 if (config.strip_ws_numbers && params->workspaces_walk->num >= 0) {
112 /* Special case: strip off the workspace number */
113 static char ws_num[10];
115 snprintf(ws_num, sizeof(ws_num), "%d", params->workspaces_walk->num);
117 /* Calculate the length of the number str in the name */
118 size_t offset = strspn(ws_name, ws_num);
120 /* Also strip off the conventional ws name delimiter */
121 if (offset && ws_name[offset] == ':')
124 /* Offset may be equal to length, in which case display the number */
125 params->workspaces_walk->name = (offset < len
126 ? i3string_from_markup_with_length(ws_name + offset, len - offset)
127 : i3string_from_markup(ws_num));
130 /* Default case: just save the name */
131 params->workspaces_walk->name = i3string_from_markup_with_length(ws_name, len);
134 /* Save its rendered width */
135 params->workspaces_walk->name_width =
136 predict_text_width(params->workspaces_walk->name);
138 DLOG("Got Workspace canonical: %s, name: '%s', name_width: %d, glyphs: %zu\n",
139 params->workspaces_walk->canonical_name,
140 i3string_as_utf8(params->workspaces_walk->name),
141 params->workspaces_walk->name_width,
142 i3string_get_num_glyphs(params->workspaces_walk->name));
143 FREE(params->cur_key);
148 if (!strcmp(params->cur_key, "output")) {
149 /* We add the ws to the TAILQ of the output, it belongs to */
150 output_name = smalloc(sizeof(const unsigned char) * (len + 1));
151 strncpy(output_name, (const char *)val, len);
152 output_name[len] = '\0';
153 i3_output *target = get_output_by_name(output_name);
155 params->workspaces_walk->output = target;
157 TAILQ_INSERT_TAIL(params->workspaces_walk->output->workspaces,
158 params->workspaces_walk,
170 * We hit the start of a JSON map (rect or a new output)
173 static int workspaces_start_map_cb(void *params_) {
174 struct workspaces_json_params *params = (struct workspaces_json_params *)params_;
176 i3_ws *new_workspace = NULL;
178 if (params->cur_key == NULL) {
179 new_workspace = smalloc(sizeof(i3_ws));
180 new_workspace->num = -1;
181 new_workspace->name = NULL;
182 new_workspace->visible = 0;
183 new_workspace->focused = 0;
184 new_workspace->urgent = 0;
185 memset(&new_workspace->rect, 0, sizeof(rect));
186 new_workspace->output = NULL;
188 params->workspaces_walk = new_workspace;
198 * Essentially we just save it in the parsing state
201 static int workspaces_map_key_cb(void *params_, const unsigned char *keyVal, size_t keyLen) {
202 struct workspaces_json_params *params = (struct workspaces_json_params *)params_;
203 FREE(params->cur_key);
205 params->cur_key = smalloc(sizeof(unsigned char) * (keyLen + 1));
206 strncpy(params->cur_key, (const char *)keyVal, keyLen);
207 params->cur_key[keyLen] = '\0';
212 /* A datastructure to pass all these callbacks to yajl */
213 static yajl_callbacks workspaces_callbacks = {
214 .yajl_boolean = workspaces_boolean_cb,
215 .yajl_integer = workspaces_integer_cb,
216 .yajl_string = workspaces_string_cb,
217 .yajl_start_map = workspaces_start_map_cb,
218 .yajl_map_key = workspaces_map_key_cb,
222 * Start parsing the received JSON string
225 void parse_workspaces_json(char *json) {
226 /* FIXME: Fasciliate stream processing, i.e. allow starting to interpret
228 struct workspaces_json_params params;
232 params.workspaces_walk = NULL;
233 params.cur_key = NULL;
238 handle = yajl_alloc(&workspaces_callbacks, NULL, (void *)¶ms);
240 state = yajl_parse(handle, (const unsigned char *)json, strlen(json));
242 /* FIXME: Propper errorhandling for JSON-parsing */
246 case yajl_status_client_canceled:
247 case yajl_status_error:
248 ELOG("Could not parse workspaces-reply!\n");
255 FREE(params.cur_key);
259 * free() all workspace data structures. Does not free() the heads of the tailqueues.
262 void free_workspaces(void) {
263 i3_output *outputs_walk;
264 if (outputs == NULL) {
269 SLIST_FOREACH(outputs_walk, outputs, slist) {
270 if (outputs_walk->workspaces != NULL && !TAILQ_EMPTY(outputs_walk->workspaces)) {
271 TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
272 I3STRING_FREE(ws_walk->name);
273 FREE(ws_walk->canonical_name);
275 FREE_TAILQ(outputs_walk->workspaces, i3_ws);