2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
7 * load_layout.c: Restore (parts of) the layout, for example after an inplace
13 #include <yajl/yajl_common.h>
14 #include <yajl/yajl_gen.h>
15 #include <yajl/yajl_parse.h>
16 #include <yajl/yajl_version.h>
18 /* TODO: refactor the whole parsing thing */
20 static char *last_key;
21 static int incomplete;
22 static Con *json_node;
24 static bool parsing_swallows;
25 static bool parsing_rect;
26 static bool parsing_deco_rect;
27 static bool parsing_window_rect;
28 static bool parsing_geometry;
29 static bool parsing_focus;
30 static bool parsing_marks;
31 struct Match *current_swallow;
32 static bool swallow_is_empty;
36 /* This list is used for reordering the focus stack after parsing the 'focus'
38 struct focus_mapping {
41 TAILQ_ENTRY(focus_mapping)
45 static TAILQ_HEAD(focus_mappings_head, focus_mapping) focus_mappings =
46 TAILQ_HEAD_INITIALIZER(focus_mappings);
48 static int json_start_map(void *ctx) {
49 LOG("start of map, last_key = %s\n", last_key);
50 if (parsing_swallows) {
51 LOG("creating new swallow\n");
52 current_swallow = smalloc(sizeof(Match));
53 match_init(current_swallow);
54 current_swallow->dock = M_DONTCHECK;
55 TAILQ_INSERT_TAIL(&(json_node->swallow_head), current_swallow, matches);
56 swallow_is_empty = true;
58 if (!parsing_rect && !parsing_deco_rect && !parsing_window_rect && !parsing_geometry) {
59 if (last_key && strcasecmp(last_key, "floating_nodes") == 0) {
60 DLOG("New floating_node\n");
61 Con *ws = con_get_workspace(json_node);
62 json_node = con_new_skeleton(NULL, NULL);
63 json_node->name = NULL;
64 json_node->parent = ws;
65 DLOG("Parent is workspace = %p\n", ws);
67 Con *parent = json_node;
68 json_node = con_new_skeleton(NULL, NULL);
69 json_node->name = NULL;
70 json_node->parent = parent;
72 /* json_node is incomplete and should be removed if parsing fails */
74 DLOG("incomplete = %d\n", incomplete);
80 static int json_end_map(void *ctx) {
82 if (!parsing_swallows && !parsing_rect && !parsing_deco_rect && !parsing_window_rect && !parsing_geometry) {
83 /* Set a few default values to simplify manually crafted layout files. */
84 if (json_node->layout == L_DEFAULT) {
85 DLOG("Setting layout = L_SPLITH\n");
86 json_node->layout = L_SPLITH;
89 /* Sanity check: swallow criteria don’t make any sense on a split
91 if (con_is_split(json_node) > 0 && !TAILQ_EMPTY(&(json_node->swallow_head))) {
92 DLOG("sanity check: removing swallows specification from split container\n");
93 while (!TAILQ_EMPTY(&(json_node->swallow_head))) {
94 Match *match = TAILQ_FIRST(&(json_node->swallow_head));
95 TAILQ_REMOVE(&(json_node->swallow_head), match, matches);
101 if (json_node->type == CT_WORKSPACE) {
102 /* Ensure the workspace has a name. */
103 DLOG("Attaching workspace. name = %s\n", json_node->name);
104 if (json_node->name == NULL || strcmp(json_node->name, "") == 0) {
105 json_node->name = sstrdup("unnamed");
108 /* Prevent name clashes when appending a workspace, e.g. when the
109 * user tries to restore a workspace called “1” but already has a
110 * workspace called “1”. */
112 Con *workspace = NULL;
113 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
114 GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, json_node->name));
115 char *base = sstrdup(json_node->name);
117 while (workspace != NULL) {
118 FREE(json_node->name);
119 sasprintf(&(json_node->name), "%s_%d", base, cnt++);
121 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
122 GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, json_node->name));
126 /* Set num accordingly so that i3bar will properly sort it. */
127 json_node->num = ws_name_to_number(json_node->name);
130 // When appending JSON layout files that only contain the workspace
131 // _contents_, we might not have an upfront signal that the
132 // container we’re currently parsing is a floating container (like
133 // the “floating_nodes” key of the workspace container itself).
134 // That’s why we make sure the con is attached at the right place
135 // in the hierarchy in case it’s floating.
136 if (json_node->type == CT_FLOATING_CON) {
137 DLOG("fixing parent which currently is %p / %s\n", json_node->parent, json_node->parent->name);
138 json_node->parent = con_get_workspace(json_node->parent);
140 // Also set a size if none was supplied, otherwise the placeholder
141 // window cannot be created as X11 requests with width=0 or
142 // height=0 are invalid.
143 const Rect zero = {0, 0, 0, 0};
144 if (memcmp(&(json_node->rect), &zero, sizeof(Rect)) == 0) {
145 DLOG("Geometry not set, combining children\n");
147 TAILQ_FOREACH(child, &(json_node->nodes_head), nodes) {
148 DLOG("child geometry: %d x %d\n", child->geometry.width, child->geometry.height);
149 json_node->rect.width += child->geometry.width;
150 json_node->rect.height = max(json_node->rect.height, child->geometry.height);
154 floating_check_size(json_node);
158 for (int i = 0; i < num_marks; i++) {
159 con_mark(json_node, marks[i], MM_ADD);
169 con_attach(json_node, json_node->parent, true);
170 LOG("Creating window\n");
171 x_con_init(json_node);
172 json_node = json_node->parent;
174 DLOG("incomplete = %d\n", incomplete);
177 if (parsing_swallows && swallow_is_empty) {
178 /* We parsed an empty swallow definition. This is an invalid layout
179 * definition, hence we reject it. */
180 ELOG("Layout file is invalid: found an empty swallow definition.\n");
184 parsing_rect = false;
185 parsing_deco_rect = false;
186 parsing_window_rect = false;
187 parsing_geometry = false;
191 static int json_end_array(void *ctx) {
192 LOG("end of array\n");
193 if (!parsing_swallows && !parsing_focus && !parsing_marks) {
194 con_fix_percent(json_node);
196 if (parsing_swallows) {
197 parsing_swallows = false;
200 parsing_marks = false;
204 /* Clear the list of focus mappings */
205 struct focus_mapping *mapping;
206 TAILQ_FOREACH_REVERSE(mapping, &focus_mappings, focus_mappings_head, focus_mappings) {
207 LOG("focus (reverse) %d\n", mapping->old_id);
209 TAILQ_FOREACH(con, &(json_node->focus_head), focused) {
210 if (con->old_id != mapping->old_id)
212 LOG("got it! %p\n", con);
213 /* Move this entry to the top of the focus list. */
214 TAILQ_REMOVE(&(json_node->focus_head), con, focused);
215 TAILQ_INSERT_HEAD(&(json_node->focus_head), con, focused);
219 while (!TAILQ_EMPTY(&focus_mappings)) {
220 mapping = TAILQ_FIRST(&focus_mappings);
221 TAILQ_REMOVE(&focus_mappings, mapping, focus_mappings);
224 parsing_focus = false;
229 static int json_key(void *ctx, const unsigned char *val, size_t len) {
230 LOG("key: %.*s\n", (int)len, val);
232 last_key = scalloc(len + 1, 1);
233 memcpy(last_key, val, len);
234 if (strcasecmp(last_key, "swallows") == 0)
235 parsing_swallows = true;
237 if (strcasecmp(last_key, "rect") == 0)
240 if (strcasecmp(last_key, "deco_rect") == 0)
241 parsing_deco_rect = true;
243 if (strcasecmp(last_key, "window_rect") == 0)
244 parsing_window_rect = true;
246 if (strcasecmp(last_key, "geometry") == 0)
247 parsing_geometry = true;
249 if (strcasecmp(last_key, "focus") == 0)
250 parsing_focus = true;
252 if (strcasecmp(last_key, "marks") == 0) {
254 parsing_marks = true;
260 static int json_string(void *ctx, const unsigned char *val, size_t len) {
261 LOG("string: %.*s for key %s\n", (int)len, val, last_key);
262 if (parsing_swallows) {
264 sasprintf(&sval, "%.*s", len, val);
265 if (strcasecmp(last_key, "class") == 0) {
266 current_swallow->class = regex_new(sval);
267 swallow_is_empty = false;
268 } else if (strcasecmp(last_key, "instance") == 0) {
269 current_swallow->instance = regex_new(sval);
270 swallow_is_empty = false;
271 } else if (strcasecmp(last_key, "window_role") == 0) {
272 current_swallow->window_role = regex_new(sval);
273 swallow_is_empty = false;
274 } else if (strcasecmp(last_key, "title") == 0) {
275 current_swallow->title = regex_new(sval);
276 swallow_is_empty = false;
278 ELOG("swallow key %s unknown\n", last_key);
281 } else if (parsing_marks) {
283 sasprintf(&mark, "%.*s", (int)len, val);
285 marks = srealloc(marks, (++num_marks) * sizeof(char *));
286 marks[num_marks - 1] = sstrdup(mark);
288 if (strcasecmp(last_key, "name") == 0) {
289 json_node->name = scalloc(len + 1, 1);
290 memcpy(json_node->name, val, len);
291 } else if (strcasecmp(last_key, "title_format") == 0) {
292 json_node->title_format = scalloc(len + 1, 1);
293 memcpy(json_node->title_format, val, len);
294 } else if (strcasecmp(last_key, "sticky_group") == 0) {
295 json_node->sticky_group = scalloc(len + 1, 1);
296 memcpy(json_node->sticky_group, val, len);
297 LOG("sticky_group of this container is %s\n", json_node->sticky_group);
298 } else if (strcasecmp(last_key, "orientation") == 0) {
299 /* Upgrade path from older versions of i3 (doing an inplace restart
300 * to a newer version):
301 * "orientation" is dumped before "layout". Therefore, we store
302 * whether the orientation was horizontal or vertical in the
303 * last_split_layout. When we then encounter layout == "default",
304 * we will use the last_split_layout as layout instead. */
306 sasprintf(&buf, "%.*s", (int)len, val);
307 if (strcasecmp(buf, "none") == 0 ||
308 strcasecmp(buf, "horizontal") == 0)
309 json_node->last_split_layout = L_SPLITH;
310 else if (strcasecmp(buf, "vertical") == 0)
311 json_node->last_split_layout = L_SPLITV;
313 LOG("Unhandled orientation: %s\n", buf);
315 } else if (strcasecmp(last_key, "border") == 0) {
317 sasprintf(&buf, "%.*s", (int)len, val);
318 if (strcasecmp(buf, "none") == 0)
319 json_node->border_style = BS_NONE;
320 else if (strcasecmp(buf, "1pixel") == 0) {
321 json_node->border_style = BS_PIXEL;
322 json_node->current_border_width = 1;
323 } else if (strcasecmp(buf, "pixel") == 0)
324 json_node->border_style = BS_PIXEL;
325 else if (strcasecmp(buf, "normal") == 0)
326 json_node->border_style = BS_NORMAL;
328 LOG("Unhandled \"border\": %s\n", buf);
330 } else if (strcasecmp(last_key, "type") == 0) {
332 sasprintf(&buf, "%.*s", (int)len, val);
333 if (strcasecmp(buf, "root") == 0)
334 json_node->type = CT_ROOT;
335 else if (strcasecmp(buf, "output") == 0)
336 json_node->type = CT_OUTPUT;
337 else if (strcasecmp(buf, "con") == 0)
338 json_node->type = CT_CON;
339 else if (strcasecmp(buf, "floating_con") == 0)
340 json_node->type = CT_FLOATING_CON;
341 else if (strcasecmp(buf, "workspace") == 0)
342 json_node->type = CT_WORKSPACE;
343 else if (strcasecmp(buf, "dockarea") == 0)
344 json_node->type = CT_DOCKAREA;
346 LOG("Unhandled \"type\": %s\n", buf);
348 } else if (strcasecmp(last_key, "layout") == 0) {
350 sasprintf(&buf, "%.*s", (int)len, val);
351 if (strcasecmp(buf, "default") == 0)
352 /* This set above when we read "orientation". */
353 json_node->layout = json_node->last_split_layout;
354 else if (strcasecmp(buf, "stacked") == 0)
355 json_node->layout = L_STACKED;
356 else if (strcasecmp(buf, "tabbed") == 0)
357 json_node->layout = L_TABBED;
358 else if (strcasecmp(buf, "dockarea") == 0)
359 json_node->layout = L_DOCKAREA;
360 else if (strcasecmp(buf, "output") == 0)
361 json_node->layout = L_OUTPUT;
362 else if (strcasecmp(buf, "splith") == 0)
363 json_node->layout = L_SPLITH;
364 else if (strcasecmp(buf, "splitv") == 0)
365 json_node->layout = L_SPLITV;
367 LOG("Unhandled \"layout\": %s\n", buf);
369 } else if (strcasecmp(last_key, "workspace_layout") == 0) {
371 sasprintf(&buf, "%.*s", (int)len, val);
372 if (strcasecmp(buf, "default") == 0)
373 json_node->workspace_layout = L_DEFAULT;
374 else if (strcasecmp(buf, "stacked") == 0)
375 json_node->workspace_layout = L_STACKED;
376 else if (strcasecmp(buf, "tabbed") == 0)
377 json_node->workspace_layout = L_TABBED;
379 LOG("Unhandled \"workspace_layout\": %s\n", buf);
381 } else if (strcasecmp(last_key, "last_split_layout") == 0) {
383 sasprintf(&buf, "%.*s", (int)len, val);
384 if (strcasecmp(buf, "splith") == 0)
385 json_node->last_split_layout = L_SPLITH;
386 else if (strcasecmp(buf, "splitv") == 0)
387 json_node->last_split_layout = L_SPLITV;
389 LOG("Unhandled \"last_splitlayout\": %s\n", buf);
391 } else if (strcasecmp(last_key, "mark") == 0) {
392 DLOG("Found deprecated key \"mark\".\n");
395 sasprintf(&buf, "%.*s", (int)len, val);
397 con_mark(json_node, buf, MM_REPLACE);
398 } else if (strcasecmp(last_key, "floating") == 0) {
400 sasprintf(&buf, "%.*s", (int)len, val);
401 if (strcasecmp(buf, "auto_off") == 0)
402 json_node->floating = FLOATING_AUTO_OFF;
403 else if (strcasecmp(buf, "auto_on") == 0)
404 json_node->floating = FLOATING_AUTO_ON;
405 else if (strcasecmp(buf, "user_off") == 0)
406 json_node->floating = FLOATING_USER_OFF;
407 else if (strcasecmp(buf, "user_on") == 0)
408 json_node->floating = FLOATING_USER_ON;
410 } else if (strcasecmp(last_key, "scratchpad_state") == 0) {
412 sasprintf(&buf, "%.*s", (int)len, val);
413 if (strcasecmp(buf, "none") == 0)
414 json_node->scratchpad_state = SCRATCHPAD_NONE;
415 else if (strcasecmp(buf, "fresh") == 0)
416 json_node->scratchpad_state = SCRATCHPAD_FRESH;
417 else if (strcasecmp(buf, "changed") == 0)
418 json_node->scratchpad_state = SCRATCHPAD_CHANGED;
425 static int json_int(void *ctx, long long val) {
426 LOG("int %lld for key %s\n", val, last_key);
427 /* For backwards compatibility with i3 < 4.8 */
428 if (strcasecmp(last_key, "type") == 0)
429 json_node->type = val;
431 if (strcasecmp(last_key, "fullscreen_mode") == 0)
432 json_node->fullscreen_mode = val;
434 if (strcasecmp(last_key, "num") == 0)
435 json_node->num = val;
437 if (strcasecmp(last_key, "current_border_width") == 0)
438 json_node->current_border_width = val;
440 if (strcasecmp(last_key, "depth") == 0)
441 json_node->depth = val;
443 if (!parsing_swallows && strcasecmp(last_key, "id") == 0)
444 json_node->old_id = val;
447 struct focus_mapping *focus_mapping = scalloc(1, sizeof(struct focus_mapping));
448 focus_mapping->old_id = val;
449 TAILQ_INSERT_TAIL(&focus_mappings, focus_mapping, focus_mappings);
452 if (parsing_rect || parsing_window_rect || parsing_geometry) {
455 r = &(json_node->rect);
456 else if (parsing_window_rect)
457 r = &(json_node->window_rect);
459 r = &(json_node->geometry);
460 if (strcasecmp(last_key, "x") == 0)
462 else if (strcasecmp(last_key, "y") == 0)
464 else if (strcasecmp(last_key, "width") == 0)
466 else if (strcasecmp(last_key, "height") == 0)
469 ELOG("WARNING: unknown key %s in rect\n", last_key);
470 DLOG("rect now: (%d, %d, %d, %d)\n",
471 r->x, r->y, r->width, r->height);
473 if (parsing_swallows) {
474 if (strcasecmp(last_key, "id") == 0) {
475 current_swallow->id = val;
476 swallow_is_empty = false;
478 if (strcasecmp(last_key, "dock") == 0) {
479 current_swallow->dock = val;
480 swallow_is_empty = false;
482 if (strcasecmp(last_key, "insert_where") == 0) {
483 current_swallow->insert_where = val;
484 swallow_is_empty = false;
491 static int json_bool(void *ctx, int val) {
492 LOG("bool %d for key %s\n", val, last_key);
493 if (strcasecmp(last_key, "focused") == 0 && val) {
494 to_focus = json_node;
497 if (strcasecmp(last_key, "sticky") == 0)
498 json_node->sticky = val;
500 if (parsing_swallows) {
501 if (strcasecmp(last_key, "restart_mode") == 0) {
502 current_swallow->restart_mode = val;
503 swallow_is_empty = false;
510 static int json_double(void *ctx, double val) {
511 LOG("double %f for key %s\n", val, last_key);
512 if (strcasecmp(last_key, "percent") == 0) {
513 json_node->percent = val;
518 static json_content_t content_result;
519 static int content_level;
521 static int json_determine_content_deeper(void *ctx) {
526 static int json_determine_content_shallower(void *ctx) {
531 static int json_determine_content_string(void *ctx, const unsigned char *val, size_t len) {
532 if (strcasecmp(last_key, "type") != 0 || content_level > 1)
535 DLOG("string = %.*s, last_key = %s\n", (int)len, val, last_key);
536 if (strncasecmp((const char *)val, "workspace", len) == 0)
537 content_result = JSON_CONTENT_WORKSPACE;
542 * Returns true if the provided JSON could be parsed by yajl.
545 bool json_validate(const char *buf, const size_t len) {
547 yajl_handle hand = yajl_alloc(NULL, NULL, NULL);
548 /* Allowing comments allows for more user-friendly layout files. */
549 yajl_config(hand, yajl_allow_comments, true);
550 /* Allow multiple values, i.e. multiple nodes to attach */
551 yajl_config(hand, yajl_allow_multiple_values, true);
553 setlocale(LC_NUMERIC, "C");
554 if (yajl_parse(hand, (const unsigned char *)buf, len) != yajl_status_ok) {
555 unsigned char *str = yajl_get_error(hand, 1, (const unsigned char *)buf, len);
556 ELOG("JSON parsing error: %s\n", str);
557 yajl_free_error(hand, str);
560 setlocale(LC_NUMERIC, "");
562 yajl_complete_parse(hand);
568 /* Parses the given JSON file until it encounters the first “type” property to
569 * determine whether the file contains workspaces or regular containers, which
570 * is important to know when deciding where (and how) to append the contents.
572 json_content_t json_determine_content(const char *buf, const size_t len) {
573 // We default to JSON_CONTENT_CON because it is legal to not include
574 // “"type": "con"” in the JSON files for better readability.
575 content_result = JSON_CONTENT_CON;
577 static yajl_callbacks callbacks = {
578 .yajl_string = json_determine_content_string,
579 .yajl_map_key = json_key,
580 .yajl_start_array = json_determine_content_deeper,
581 .yajl_start_map = json_determine_content_deeper,
582 .yajl_end_map = json_determine_content_shallower,
583 .yajl_end_array = json_determine_content_shallower,
585 yajl_handle hand = yajl_alloc(&callbacks, NULL, NULL);
586 /* Allowing comments allows for more user-friendly layout files. */
587 yajl_config(hand, yajl_allow_comments, true);
588 /* Allow multiple values, i.e. multiple nodes to attach */
589 yajl_config(hand, yajl_allow_multiple_values, true);
590 setlocale(LC_NUMERIC, "C");
591 const yajl_status stat = yajl_parse(hand, (const unsigned char *)buf, len);
592 if (stat != yajl_status_ok && stat != yajl_status_client_canceled) {
593 unsigned char *str = yajl_get_error(hand, 1, (const unsigned char *)buf, len);
594 ELOG("JSON parsing error: %s\n", str);
595 yajl_free_error(hand, str);
598 setlocale(LC_NUMERIC, "");
599 yajl_complete_parse(hand);
602 return content_result;
605 void tree_append_json(Con *con, const char *buf, const size_t len, char **errormsg) {
606 static yajl_callbacks callbacks = {
607 .yajl_boolean = json_bool,
608 .yajl_integer = json_int,
609 .yajl_double = json_double,
610 .yajl_string = json_string,
611 .yajl_start_map = json_start_map,
612 .yajl_map_key = json_key,
613 .yajl_end_map = json_end_map,
614 .yajl_end_array = json_end_array,
616 yajl_handle hand = yajl_alloc(&callbacks, NULL, NULL);
617 /* Allowing comments allows for more user-friendly layout files. */
618 yajl_config(hand, yajl_allow_comments, true);
619 /* Allow multiple values, i.e. multiple nodes to attach */
620 yajl_config(hand, yajl_allow_multiple_values, true);
624 parsing_swallows = false;
625 parsing_rect = false;
626 parsing_deco_rect = false;
627 parsing_window_rect = false;
628 parsing_geometry = false;
629 parsing_focus = false;
630 parsing_marks = false;
631 setlocale(LC_NUMERIC, "C");
632 const yajl_status stat = yajl_parse(hand, (const unsigned char *)buf, len);
633 if (stat != yajl_status_ok) {
634 unsigned char *str = yajl_get_error(hand, 1, (const unsigned char *)buf, len);
635 ELOG("JSON parsing error: %s\n", str);
636 if (errormsg != NULL)
637 *errormsg = sstrdup((const char *)str);
638 yajl_free_error(hand, str);
639 while (incomplete-- > 0) {
640 Con *parent = json_node->parent;
641 DLOG("freeing incomplete container %p\n", json_node);
647 /* In case not all containers were restored, we need to fix the
648 * percentages, otherwise i3 will crash immediately when rendering the
650 con_fix_percent(con);
652 setlocale(LC_NUMERIC, "");
653 yajl_complete_parse(hand);