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;
34 /* We need to save each container that needs to be marked if we want to support
35 * marking non-leaf containers. In their case, the end_map for their children is
36 * called before their own end_map, so marking json_node would end up marking
37 * the latest child. We can't just mark containers immediately after we parse a
38 * mark because of #2511. */
39 struct pending_marks {
41 Con *con_to_be_marked;
44 /* This list is used for reordering the focus stack after parsing the 'focus'
46 struct focus_mapping {
49 TAILQ_ENTRY(focus_mapping)
53 static TAILQ_HEAD(focus_mappings_head, focus_mapping) focus_mappings =
54 TAILQ_HEAD_INITIALIZER(focus_mappings);
56 static int json_start_map(void *ctx) {
57 LOG("start of map, last_key = %s\n", last_key);
58 if (parsing_swallows) {
59 LOG("creating new swallow\n");
60 current_swallow = smalloc(sizeof(Match));
61 match_init(current_swallow);
62 current_swallow->dock = M_DONTCHECK;
63 TAILQ_INSERT_TAIL(&(json_node->swallow_head), current_swallow, matches);
64 swallow_is_empty = true;
66 if (!parsing_rect && !parsing_deco_rect && !parsing_window_rect && !parsing_geometry) {
67 if (last_key && strcasecmp(last_key, "floating_nodes") == 0) {
68 DLOG("New floating_node\n");
69 Con *ws = con_get_workspace(json_node);
70 json_node = con_new_skeleton(NULL, NULL);
71 json_node->name = NULL;
72 json_node->parent = ws;
73 DLOG("Parent is workspace = %p\n", ws);
75 Con *parent = json_node;
76 json_node = con_new_skeleton(NULL, NULL);
77 json_node->name = NULL;
78 json_node->parent = parent;
80 /* json_node is incomplete and should be removed if parsing fails */
82 DLOG("incomplete = %d\n", incomplete);
88 static int json_end_map(void *ctx) {
90 if (!parsing_swallows && !parsing_rect && !parsing_deco_rect && !parsing_window_rect && !parsing_geometry) {
91 /* Set a few default values to simplify manually crafted layout files. */
92 if (json_node->layout == L_DEFAULT) {
93 DLOG("Setting layout = L_SPLITH\n");
94 json_node->layout = L_SPLITH;
97 /* Sanity check: swallow criteria don’t make any sense on a split
99 if (con_is_split(json_node) > 0 && !TAILQ_EMPTY(&(json_node->swallow_head))) {
100 DLOG("sanity check: removing swallows specification from split container\n");
101 while (!TAILQ_EMPTY(&(json_node->swallow_head))) {
102 Match *match = TAILQ_FIRST(&(json_node->swallow_head));
103 TAILQ_REMOVE(&(json_node->swallow_head), match, matches);
109 if (json_node->type == CT_WORKSPACE) {
110 /* Ensure the workspace has a name. */
111 DLOG("Attaching workspace. name = %s\n", json_node->name);
112 if (json_node->name == NULL || strcmp(json_node->name, "") == 0) {
113 json_node->name = sstrdup("unnamed");
116 /* Prevent name clashes when appending a workspace, e.g. when the
117 * user tries to restore a workspace called “1” but already has a
118 * workspace called “1”. */
119 char *base = sstrdup(json_node->name);
121 while (get_existing_workspace_by_name(json_node->name) != NULL) {
122 FREE(json_node->name);
123 sasprintf(&(json_node->name), "%s_%d", base, cnt++);
127 /* Set num accordingly so that i3bar will properly sort it. */
128 json_node->num = ws_name_to_number(json_node->name);
131 // When appending JSON layout files that only contain the workspace
132 // _contents_, we might not have an upfront signal that the
133 // container we’re currently parsing is a floating container (like
134 // the “floating_nodes” key of the workspace container itself).
135 // That’s why we make sure the con is attached at the right place
136 // in the hierarchy in case it’s floating.
137 if (json_node->type == CT_FLOATING_CON) {
138 DLOG("fixing parent which currently is %p / %s\n", json_node->parent, json_node->parent->name);
139 json_node->parent = con_get_workspace(json_node->parent);
141 // Also set a size if none was supplied, otherwise the placeholder
142 // window cannot be created as X11 requests with width=0 or
143 // height=0 are invalid.
144 const Rect zero = {0, 0, 0, 0};
145 if (memcmp(&(json_node->rect), &zero, sizeof(Rect)) == 0) {
146 DLOG("Geometry not set, combining children\n");
148 TAILQ_FOREACH(child, &(json_node->nodes_head), nodes) {
149 DLOG("child geometry: %d x %d\n", child->geometry.width, child->geometry.height);
150 json_node->rect.width += child->geometry.width;
151 json_node->rect.height = max(json_node->rect.height, child->geometry.height);
155 floating_check_size(json_node, false);
159 for (int i = 0; i < num_marks; i++) {
160 Con *con = marks[i].con_to_be_marked;
161 char *mark = marks[i].mark;
162 con_mark(con, mark, MM_ADD);
171 con_attach(json_node, json_node->parent, true);
172 LOG("Creating window\n");
173 x_con_init(json_node);
174 json_node = json_node->parent;
176 DLOG("incomplete = %d\n", incomplete);
179 if (parsing_swallows && swallow_is_empty) {
180 /* We parsed an empty swallow definition. This is an invalid layout
181 * definition, hence we reject it. */
182 ELOG("Layout file is invalid: found an empty swallow definition.\n");
186 parsing_rect = false;
187 parsing_deco_rect = false;
188 parsing_window_rect = false;
189 parsing_geometry = false;
193 static int json_end_array(void *ctx) {
194 LOG("end of array\n");
195 if (!parsing_swallows && !parsing_focus && !parsing_marks) {
196 con_fix_percent(json_node);
198 if (parsing_swallows) {
199 parsing_swallows = false;
202 parsing_marks = false;
206 /* Clear the list of focus mappings */
207 struct focus_mapping *mapping;
208 TAILQ_FOREACH_REVERSE(mapping, &focus_mappings, focus_mappings_head, focus_mappings) {
209 LOG("focus (reverse) %d\n", mapping->old_id);
211 TAILQ_FOREACH(con, &(json_node->focus_head), focused) {
212 if (con->old_id != mapping->old_id)
214 LOG("got it! %p\n", con);
215 /* Move this entry to the top of the focus list. */
216 TAILQ_REMOVE(&(json_node->focus_head), con, focused);
217 TAILQ_INSERT_HEAD(&(json_node->focus_head), con, focused);
221 while (!TAILQ_EMPTY(&focus_mappings)) {
222 mapping = TAILQ_FIRST(&focus_mappings);
223 TAILQ_REMOVE(&focus_mappings, mapping, focus_mappings);
226 parsing_focus = false;
231 static int json_key(void *ctx, const unsigned char *val, size_t len) {
232 LOG("key: %.*s\n", (int)len, val);
234 last_key = scalloc(len + 1, 1);
235 memcpy(last_key, val, len);
236 if (strcasecmp(last_key, "swallows") == 0)
237 parsing_swallows = true;
239 if (strcasecmp(last_key, "rect") == 0)
242 if (strcasecmp(last_key, "deco_rect") == 0)
243 parsing_deco_rect = true;
245 if (strcasecmp(last_key, "window_rect") == 0)
246 parsing_window_rect = true;
248 if (strcasecmp(last_key, "geometry") == 0)
249 parsing_geometry = true;
251 if (strcasecmp(last_key, "focus") == 0)
252 parsing_focus = true;
254 if (strcasecmp(last_key, "marks") == 0) {
256 parsing_marks = true;
262 static int json_string(void *ctx, const unsigned char *val, size_t len) {
263 LOG("string: %.*s for key %s\n", (int)len, val, last_key);
264 if (parsing_swallows) {
266 sasprintf(&sval, "%.*s", len, val);
267 if (strcasecmp(last_key, "class") == 0) {
268 current_swallow->class = regex_new(sval);
269 swallow_is_empty = false;
270 } else if (strcasecmp(last_key, "instance") == 0) {
271 current_swallow->instance = regex_new(sval);
272 swallow_is_empty = false;
273 } else if (strcasecmp(last_key, "window_role") == 0) {
274 current_swallow->window_role = regex_new(sval);
275 swallow_is_empty = false;
276 } else if (strcasecmp(last_key, "title") == 0) {
277 current_swallow->title = regex_new(sval);
278 swallow_is_empty = false;
280 ELOG("swallow key %s unknown\n", last_key);
283 } else if (parsing_marks) {
285 sasprintf(&mark, "%.*s", (int)len, val);
287 marks = srealloc(marks, (++num_marks) * sizeof(struct pending_marks));
288 marks[num_marks - 1].mark = sstrdup(mark);
289 marks[num_marks - 1].con_to_be_marked = json_node;
291 if (strcasecmp(last_key, "name") == 0) {
292 json_node->name = scalloc(len + 1, 1);
293 memcpy(json_node->name, val, len);
294 } else if (strcasecmp(last_key, "title_format") == 0) {
295 json_node->title_format = scalloc(len + 1, 1);
296 memcpy(json_node->title_format, val, len);
297 } else if (strcasecmp(last_key, "sticky_group") == 0) {
298 json_node->sticky_group = scalloc(len + 1, 1);
299 memcpy(json_node->sticky_group, val, len);
300 LOG("sticky_group of this container is %s\n", json_node->sticky_group);
301 } else if (strcasecmp(last_key, "orientation") == 0) {
302 /* Upgrade path from older versions of i3 (doing an inplace restart
303 * to a newer version):
304 * "orientation" is dumped before "layout". Therefore, we store
305 * whether the orientation was horizontal or vertical in the
306 * last_split_layout. When we then encounter layout == "default",
307 * we will use the last_split_layout as layout instead. */
309 sasprintf(&buf, "%.*s", (int)len, val);
310 if (strcasecmp(buf, "none") == 0 ||
311 strcasecmp(buf, "horizontal") == 0)
312 json_node->last_split_layout = L_SPLITH;
313 else if (strcasecmp(buf, "vertical") == 0)
314 json_node->last_split_layout = L_SPLITV;
316 LOG("Unhandled orientation: %s\n", buf);
318 } else if (strcasecmp(last_key, "border") == 0) {
320 sasprintf(&buf, "%.*s", (int)len, val);
321 if (strcasecmp(buf, "none") == 0)
322 json_node->border_style = BS_NONE;
323 else if (strcasecmp(buf, "1pixel") == 0) {
324 json_node->border_style = BS_PIXEL;
325 json_node->current_border_width = 1;
326 } else if (strcasecmp(buf, "pixel") == 0)
327 json_node->border_style = BS_PIXEL;
328 else if (strcasecmp(buf, "normal") == 0)
329 json_node->border_style = BS_NORMAL;
331 LOG("Unhandled \"border\": %s\n", buf);
333 } else if (strcasecmp(last_key, "type") == 0) {
335 sasprintf(&buf, "%.*s", (int)len, val);
336 if (strcasecmp(buf, "root") == 0)
337 json_node->type = CT_ROOT;
338 else if (strcasecmp(buf, "output") == 0)
339 json_node->type = CT_OUTPUT;
340 else if (strcasecmp(buf, "con") == 0)
341 json_node->type = CT_CON;
342 else if (strcasecmp(buf, "floating_con") == 0)
343 json_node->type = CT_FLOATING_CON;
344 else if (strcasecmp(buf, "workspace") == 0)
345 json_node->type = CT_WORKSPACE;
346 else if (strcasecmp(buf, "dockarea") == 0)
347 json_node->type = CT_DOCKAREA;
349 LOG("Unhandled \"type\": %s\n", buf);
351 } else if (strcasecmp(last_key, "layout") == 0) {
353 sasprintf(&buf, "%.*s", (int)len, val);
354 if (strcasecmp(buf, "default") == 0)
355 /* This set above when we read "orientation". */
356 json_node->layout = json_node->last_split_layout;
357 else if (strcasecmp(buf, "stacked") == 0)
358 json_node->layout = L_STACKED;
359 else if (strcasecmp(buf, "tabbed") == 0)
360 json_node->layout = L_TABBED;
361 else if (strcasecmp(buf, "dockarea") == 0)
362 json_node->layout = L_DOCKAREA;
363 else if (strcasecmp(buf, "output") == 0)
364 json_node->layout = L_OUTPUT;
365 else if (strcasecmp(buf, "splith") == 0)
366 json_node->layout = L_SPLITH;
367 else if (strcasecmp(buf, "splitv") == 0)
368 json_node->layout = L_SPLITV;
370 LOG("Unhandled \"layout\": %s\n", buf);
372 } else if (strcasecmp(last_key, "workspace_layout") == 0) {
374 sasprintf(&buf, "%.*s", (int)len, val);
375 if (strcasecmp(buf, "default") == 0)
376 json_node->workspace_layout = L_DEFAULT;
377 else if (strcasecmp(buf, "stacked") == 0)
378 json_node->workspace_layout = L_STACKED;
379 else if (strcasecmp(buf, "tabbed") == 0)
380 json_node->workspace_layout = L_TABBED;
382 LOG("Unhandled \"workspace_layout\": %s\n", buf);
384 } else if (strcasecmp(last_key, "last_split_layout") == 0) {
386 sasprintf(&buf, "%.*s", (int)len, val);
387 if (strcasecmp(buf, "splith") == 0)
388 json_node->last_split_layout = L_SPLITH;
389 else if (strcasecmp(buf, "splitv") == 0)
390 json_node->last_split_layout = L_SPLITV;
392 LOG("Unhandled \"last_splitlayout\": %s\n", buf);
394 } else if (strcasecmp(last_key, "mark") == 0) {
395 DLOG("Found deprecated key \"mark\".\n");
398 sasprintf(&buf, "%.*s", (int)len, val);
400 con_mark(json_node, buf, MM_REPLACE);
401 } else if (strcasecmp(last_key, "floating") == 0) {
403 sasprintf(&buf, "%.*s", (int)len, val);
404 if (strcasecmp(buf, "auto_off") == 0)
405 json_node->floating = FLOATING_AUTO_OFF;
406 else if (strcasecmp(buf, "auto_on") == 0)
407 json_node->floating = FLOATING_AUTO_ON;
408 else if (strcasecmp(buf, "user_off") == 0)
409 json_node->floating = FLOATING_USER_OFF;
410 else if (strcasecmp(buf, "user_on") == 0)
411 json_node->floating = FLOATING_USER_ON;
413 } else if (strcasecmp(last_key, "scratchpad_state") == 0) {
415 sasprintf(&buf, "%.*s", (int)len, val);
416 if (strcasecmp(buf, "none") == 0)
417 json_node->scratchpad_state = SCRATCHPAD_NONE;
418 else if (strcasecmp(buf, "fresh") == 0)
419 json_node->scratchpad_state = SCRATCHPAD_FRESH;
420 else if (strcasecmp(buf, "changed") == 0)
421 json_node->scratchpad_state = SCRATCHPAD_CHANGED;
428 static int json_int(void *ctx, long long val) {
429 LOG("int %lld for key %s\n", val, last_key);
430 /* For backwards compatibility with i3 < 4.8 */
431 if (strcasecmp(last_key, "type") == 0)
432 json_node->type = val;
434 if (strcasecmp(last_key, "fullscreen_mode") == 0)
435 json_node->fullscreen_mode = val;
437 if (strcasecmp(last_key, "num") == 0)
438 json_node->num = val;
440 if (strcasecmp(last_key, "current_border_width") == 0)
441 json_node->current_border_width = val;
443 if (strcasecmp(last_key, "depth") == 0)
444 json_node->depth = val;
446 if (!parsing_swallows && strcasecmp(last_key, "id") == 0)
447 json_node->old_id = val;
450 struct focus_mapping *focus_mapping = scalloc(1, sizeof(struct focus_mapping));
451 focus_mapping->old_id = val;
452 TAILQ_INSERT_TAIL(&focus_mappings, focus_mapping, focus_mappings);
455 if (parsing_rect || parsing_window_rect || parsing_geometry) {
458 r = &(json_node->rect);
459 else if (parsing_window_rect)
460 r = &(json_node->window_rect);
462 r = &(json_node->geometry);
463 if (strcasecmp(last_key, "x") == 0)
465 else if (strcasecmp(last_key, "y") == 0)
467 else if (strcasecmp(last_key, "width") == 0)
469 else if (strcasecmp(last_key, "height") == 0)
472 ELOG("WARNING: unknown key %s in rect\n", last_key);
473 DLOG("rect now: (%d, %d, %d, %d)\n",
474 r->x, r->y, r->width, r->height);
476 if (parsing_swallows) {
477 if (strcasecmp(last_key, "id") == 0) {
478 current_swallow->id = val;
479 swallow_is_empty = false;
481 if (strcasecmp(last_key, "dock") == 0) {
482 current_swallow->dock = val;
483 swallow_is_empty = false;
485 if (strcasecmp(last_key, "insert_where") == 0) {
486 current_swallow->insert_where = val;
487 swallow_is_empty = false;
494 static int json_bool(void *ctx, int val) {
495 LOG("bool %d for key %s\n", val, last_key);
496 if (strcasecmp(last_key, "focused") == 0 && val) {
497 to_focus = json_node;
500 if (strcasecmp(last_key, "sticky") == 0)
501 json_node->sticky = val;
503 if (parsing_swallows) {
504 if (strcasecmp(last_key, "restart_mode") == 0) {
505 current_swallow->restart_mode = val;
506 swallow_is_empty = false;
513 static int json_double(void *ctx, double val) {
514 LOG("double %f for key %s\n", val, last_key);
515 if (strcasecmp(last_key, "percent") == 0) {
516 json_node->percent = val;
521 static json_content_t content_result;
522 static int content_level;
524 static int json_determine_content_deeper(void *ctx) {
529 static int json_determine_content_shallower(void *ctx) {
534 static int json_determine_content_string(void *ctx, const unsigned char *val, size_t len) {
535 if (strcasecmp(last_key, "type") != 0 || content_level > 1)
538 DLOG("string = %.*s, last_key = %s\n", (int)len, val, last_key);
539 if (strncasecmp((const char *)val, "workspace", len) == 0)
540 content_result = JSON_CONTENT_WORKSPACE;
545 * Returns true if the provided JSON could be parsed by yajl.
548 bool json_validate(const char *buf, const size_t len) {
550 yajl_handle hand = yajl_alloc(NULL, NULL, NULL);
551 /* Allowing comments allows for more user-friendly layout files. */
552 yajl_config(hand, yajl_allow_comments, true);
553 /* Allow multiple values, i.e. multiple nodes to attach */
554 yajl_config(hand, yajl_allow_multiple_values, true);
556 setlocale(LC_NUMERIC, "C");
557 if (yajl_parse(hand, (const unsigned char *)buf, len) != yajl_status_ok) {
558 unsigned char *str = yajl_get_error(hand, 1, (const unsigned char *)buf, len);
559 ELOG("JSON parsing error: %s\n", str);
560 yajl_free_error(hand, str);
563 setlocale(LC_NUMERIC, "");
565 yajl_complete_parse(hand);
571 /* Parses the given JSON file until it encounters the first “type” property to
572 * determine whether the file contains workspaces or regular containers, which
573 * is important to know when deciding where (and how) to append the contents.
575 json_content_t json_determine_content(const char *buf, const size_t len) {
576 // We default to JSON_CONTENT_CON because it is legal to not include
577 // “"type": "con"” in the JSON files for better readability.
578 content_result = JSON_CONTENT_CON;
580 static yajl_callbacks callbacks = {
581 .yajl_string = json_determine_content_string,
582 .yajl_map_key = json_key,
583 .yajl_start_array = json_determine_content_deeper,
584 .yajl_start_map = json_determine_content_deeper,
585 .yajl_end_map = json_determine_content_shallower,
586 .yajl_end_array = json_determine_content_shallower,
588 yajl_handle hand = yajl_alloc(&callbacks, NULL, NULL);
589 /* Allowing comments allows for more user-friendly layout files. */
590 yajl_config(hand, yajl_allow_comments, true);
591 /* Allow multiple values, i.e. multiple nodes to attach */
592 yajl_config(hand, yajl_allow_multiple_values, true);
593 setlocale(LC_NUMERIC, "C");
594 const yajl_status stat = yajl_parse(hand, (const unsigned char *)buf, len);
595 if (stat != yajl_status_ok && stat != yajl_status_client_canceled) {
596 unsigned char *str = yajl_get_error(hand, 1, (const unsigned char *)buf, len);
597 ELOG("JSON parsing error: %s\n", str);
598 yajl_free_error(hand, str);
601 setlocale(LC_NUMERIC, "");
602 yajl_complete_parse(hand);
605 return content_result;
608 void tree_append_json(Con *con, const char *buf, const size_t len, char **errormsg) {
609 static yajl_callbacks callbacks = {
610 .yajl_boolean = json_bool,
611 .yajl_integer = json_int,
612 .yajl_double = json_double,
613 .yajl_string = json_string,
614 .yajl_start_map = json_start_map,
615 .yajl_map_key = json_key,
616 .yajl_end_map = json_end_map,
617 .yajl_end_array = json_end_array,
619 yajl_handle hand = yajl_alloc(&callbacks, NULL, NULL);
620 /* Allowing comments allows for more user-friendly layout files. */
621 yajl_config(hand, yajl_allow_comments, true);
622 /* Allow multiple values, i.e. multiple nodes to attach */
623 yajl_config(hand, yajl_allow_multiple_values, true);
624 /* We don't need to validate that the input is valid UTF8 here.
625 * tree_append_json is called in two cases:
626 * 1. With the append_layout command. json_validate is called first and will
627 * fail on invalid UTF8 characters so we don't need to recheck.
628 * 2. With an in-place restart. The rest of the codebase should be
629 * responsible for producing valid UTF8 JSON output. If not,
630 * tree_append_json will just preserve invalid UTF8 strings in the tree
631 * instead of failing to parse the layout file which could lead to
632 * problems like in #3156.
633 * Either way, disabling UTF8 validation slightly speeds up yajl. */
634 yajl_config(hand, yajl_dont_validate_strings, true);
638 parsing_swallows = false;
639 parsing_rect = false;
640 parsing_deco_rect = false;
641 parsing_window_rect = false;
642 parsing_geometry = false;
643 parsing_focus = false;
644 parsing_marks = false;
645 setlocale(LC_NUMERIC, "C");
646 const yajl_status stat = yajl_parse(hand, (const unsigned char *)buf, len);
647 if (stat != yajl_status_ok) {
648 unsigned char *str = yajl_get_error(hand, 1, (const unsigned char *)buf, len);
649 ELOG("JSON parsing error: %s\n", str);
650 if (errormsg != NULL)
651 *errormsg = sstrdup((const char *)str);
652 yajl_free_error(hand, str);
653 while (incomplete-- > 0) {
654 Con *parent = json_node->parent;
655 DLOG("freeing incomplete container %p\n", json_node);
656 if (json_node == to_focus) {
664 /* In case not all containers were restored, we need to fix the
665 * percentages, otherwise i3 will crash immediately when rendering the
667 con_fix_percent(con);
669 setlocale(LC_NUMERIC, "");
670 yajl_complete_parse(hand);
674 con_activate(to_focus);