2 * vim:ts=4:sw=4:expandtab
4 * i3bar - an xcb-based status- and ws-bar for i3
5 * © 2010 Axel Wagner and contributors (see also: LICENSE)
7 * workspaces.c: Maintaining the workspace lists
16 #include <yajl/yajl_parse.h>
17 #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_;
105 if (!strcmp(params->cur_key, "name")) {
106 const char *ws_name = (const char *)val;
107 params->workspaces_walk->canonical_name = sstrndup(ws_name, len);
109 if ((config.strip_ws_numbers || config.strip_ws_name) && params->workspaces_walk->num >= 0) {
110 /* Special case: strip off the workspace number/name */
111 static char ws_num[10];
113 snprintf(ws_num, sizeof(ws_num), "%d", params->workspaces_walk->num);
115 /* Calculate the length of the number str in the name */
116 size_t offset = strspn(ws_name, ws_num);
118 /* Also strip off the conventional ws name delimiter */
119 if (offset && ws_name[offset] == ':')
122 if (config.strip_ws_numbers) {
123 /* Offset may be equal to length, in which case display the number */
124 params->workspaces_walk->name = (offset < len
125 ? i3string_from_markup_with_length(ws_name + offset, len - offset)
126 : i3string_from_markup(ws_num));
128 params->workspaces_walk->name = i3string_from_markup(ws_num);
131 /* Default case: just save the name */
132 params->workspaces_walk->name = i3string_from_markup_with_length(ws_name, len);
135 /* Save its rendered width */
136 params->workspaces_walk->name_width =
137 predict_text_width(params->workspaces_walk->name);
139 DLOG("Got workspace canonical: %s, name: '%s', name_width: %d, glyphs: %zu\n",
140 params->workspaces_walk->canonical_name,
141 i3string_as_utf8(params->workspaces_walk->name),
142 params->workspaces_walk->name_width,
143 i3string_get_num_glyphs(params->workspaces_walk->name));
144 FREE(params->cur_key);
149 if (!strcmp(params->cur_key, "output")) {
150 /* We add the ws to the TAILQ of the output, it belongs to */
151 char *output_name = NULL;
152 sasprintf(&output_name, "%.*s", len, val);
154 i3_output *target = get_output_by_name(output_name);
155 if (target != NULL) {
156 params->workspaces_walk->output = target;
158 TAILQ_INSERT_TAIL(params->workspaces_walk->output->workspaces,
159 params->workspaces_walk,
171 * We hit the start of a JSON map (rect or a new output)
174 static int workspaces_start_map_cb(void *params_) {
175 struct workspaces_json_params *params = (struct workspaces_json_params *)params_;
177 i3_ws *new_workspace = NULL;
179 if (params->cur_key == NULL) {
180 new_workspace = smalloc(sizeof(i3_ws));
181 new_workspace->num = -1;
182 new_workspace->name = NULL;
183 new_workspace->visible = 0;
184 new_workspace->focused = 0;
185 new_workspace->urgent = 0;
186 memset(&new_workspace->rect, 0, sizeof(rect));
187 new_workspace->output = NULL;
189 params->workspaces_walk = new_workspace;
199 * Essentially we just save it in the parsing state
202 static int workspaces_map_key_cb(void *params_, const unsigned char *keyVal, size_t keyLen) {
203 struct workspaces_json_params *params = (struct workspaces_json_params *)params_;
204 FREE(params->cur_key);
205 sasprintf(&(params->cur_key), "%.*s", keyLen, keyVal);
209 /* A datastructure to pass all these callbacks to yajl */
210 static yajl_callbacks workspaces_callbacks = {
211 .yajl_boolean = workspaces_boolean_cb,
212 .yajl_integer = workspaces_integer_cb,
213 .yajl_string = workspaces_string_cb,
214 .yajl_start_map = workspaces_start_map_cb,
215 .yajl_map_key = workspaces_map_key_cb,
219 * Start parsing the received JSON string
222 void parse_workspaces_json(char *json) {
223 /* FIXME: Fasciliate stream processing, i.e. allow starting to interpret
225 struct workspaces_json_params params;
229 params.workspaces_walk = NULL;
230 params.cur_key = NULL;
235 handle = yajl_alloc(&workspaces_callbacks, NULL, (void *)¶ms);
237 state = yajl_parse(handle, (const unsigned char *)json, strlen(json));
239 /* FIXME: Proper error handling for JSON parsing */
243 case yajl_status_client_canceled:
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);
270 FREE(ws_walk->canonical_name);
272 FREE_TAILQ(outputs_walk->workspaces, i3_ws);