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 && params->workspaces_walk->num >= 0) {
110 /* Special case: strip off the workspace number */
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 /* Offset may be equal to length, in which case display the number */
123 params->workspaces_walk->name = (offset < len
124 ? i3string_from_markup_with_length(ws_name + offset, len - offset)
125 : i3string_from_markup(ws_num));
128 /* Default case: just save the name */
129 params->workspaces_walk->name = i3string_from_markup_with_length(ws_name, len);
132 /* Save its rendered width */
133 params->workspaces_walk->name_width =
134 predict_text_width(params->workspaces_walk->name);
136 DLOG("Got workspace canonical: %s, name: '%s', name_width: %d, glyphs: %zu\n",
137 params->workspaces_walk->canonical_name,
138 i3string_as_utf8(params->workspaces_walk->name),
139 params->workspaces_walk->name_width,
140 i3string_get_num_glyphs(params->workspaces_walk->name));
141 FREE(params->cur_key);
146 if (!strcmp(params->cur_key, "output")) {
147 /* We add the ws to the TAILQ of the output, it belongs to */
148 char *output_name = NULL;
149 sasprintf(&output_name, "%.*s", len, val);
151 i3_output *target = get_output_by_name(output_name);
152 if (target != NULL) {
153 params->workspaces_walk->output = target;
155 TAILQ_INSERT_TAIL(params->workspaces_walk->output->workspaces,
156 params->workspaces_walk,
168 * We hit the start of a JSON map (rect or a new output)
171 static int workspaces_start_map_cb(void *params_) {
172 struct workspaces_json_params *params = (struct workspaces_json_params *)params_;
174 i3_ws *new_workspace = NULL;
176 if (params->cur_key == NULL) {
177 new_workspace = smalloc(sizeof(i3_ws));
178 new_workspace->num = -1;
179 new_workspace->name = NULL;
180 new_workspace->visible = 0;
181 new_workspace->focused = 0;
182 new_workspace->urgent = 0;
183 memset(&new_workspace->rect, 0, sizeof(rect));
184 new_workspace->output = NULL;
186 params->workspaces_walk = new_workspace;
196 * Essentially we just save it in the parsing state
199 static int workspaces_map_key_cb(void *params_, const unsigned char *keyVal, size_t keyLen) {
200 struct workspaces_json_params *params = (struct workspaces_json_params *)params_;
201 FREE(params->cur_key);
202 sasprintf(&(params->cur_key), "%.*s", keyLen, keyVal);
206 /* A datastructure to pass all these callbacks to yajl */
207 static yajl_callbacks workspaces_callbacks = {
208 .yajl_boolean = workspaces_boolean_cb,
209 .yajl_integer = workspaces_integer_cb,
210 .yajl_string = workspaces_string_cb,
211 .yajl_start_map = workspaces_start_map_cb,
212 .yajl_map_key = workspaces_map_key_cb,
216 * Start parsing the received JSON string
219 void parse_workspaces_json(char *json) {
220 /* FIXME: Fasciliate stream processing, i.e. allow starting to interpret
222 struct workspaces_json_params params;
226 params.workspaces_walk = NULL;
227 params.cur_key = NULL;
232 handle = yajl_alloc(&workspaces_callbacks, NULL, (void *)¶ms);
234 state = yajl_parse(handle, (const unsigned char *)json, strlen(json));
236 /* FIXME: Proper error handling for JSON parsing */
240 case yajl_status_client_canceled:
241 case yajl_status_error:
242 ELOG("Could not parse workspaces reply!\n");
249 FREE(params.cur_key);
253 * free() all workspace data structures. Does not free() the heads of the tailqueues.
256 void free_workspaces(void) {
257 i3_output *outputs_walk;
258 if (outputs == NULL) {
263 SLIST_FOREACH(outputs_walk, outputs, slist) {
264 if (outputs_walk->workspaces != NULL && !TAILQ_EMPTY(outputs_walk->workspaces)) {
265 TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
266 I3STRING_FREE(ws_walk->name);
267 FREE(ws_walk->canonical_name);
269 FREE_TAILQ(outputs_walk->workspaces, i3_ws);