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 Con *json_node;
23 static bool parsing_swallows;
24 static bool parsing_rect;
25 static bool parsing_deco_rect;
26 static bool parsing_window_rect;
27 static bool parsing_geometry;
28 static bool parsing_focus;
29 static bool parsing_marks;
30 struct Match *current_swallow;
31 static bool swallow_is_empty;
33 /* This list is used for reordering the focus stack after parsing the 'focus'
35 struct focus_mapping {
38 TAILQ_ENTRY(focus_mapping)
42 static TAILQ_HEAD(focus_mappings_head, focus_mapping) focus_mappings =
43 TAILQ_HEAD_INITIALIZER(focus_mappings);
45 static int json_start_map(void *ctx) {
46 LOG("start of map, last_key = %s\n", last_key);
47 if (parsing_swallows) {
48 LOG("creating new swallow\n");
49 current_swallow = smalloc(sizeof(Match));
50 match_init(current_swallow);
51 current_swallow->dock = M_DONTCHECK;
52 TAILQ_INSERT_TAIL(&(json_node->swallow_head), current_swallow, matches);
53 swallow_is_empty = true;
55 if (!parsing_rect && !parsing_deco_rect && !parsing_window_rect && !parsing_geometry) {
56 if (last_key && strcasecmp(last_key, "floating_nodes") == 0) {
57 DLOG("New floating_node\n");
58 Con *ws = con_get_workspace(json_node);
59 json_node = con_new_skeleton(NULL, NULL);
60 json_node->name = NULL;
61 json_node->parent = ws;
62 DLOG("Parent is workspace = %p\n", ws);
64 Con *parent = json_node;
65 json_node = con_new_skeleton(NULL, NULL);
66 json_node->name = NULL;
67 json_node->parent = parent;
74 static int json_end_map(void *ctx) {
76 if (!parsing_swallows && !parsing_rect && !parsing_deco_rect && !parsing_window_rect && !parsing_geometry) {
77 /* Set a few default values to simplify manually crafted layout files. */
78 if (json_node->layout == L_DEFAULT) {
79 DLOG("Setting layout = L_SPLITH\n");
80 json_node->layout = L_SPLITH;
83 /* Sanity check: swallow criteria don’t make any sense on a split
85 if (con_is_split(json_node) > 0 && !TAILQ_EMPTY(&(json_node->swallow_head))) {
86 DLOG("sanity check: removing swallows specification from split container\n");
87 while (!TAILQ_EMPTY(&(json_node->swallow_head))) {
88 Match *match = TAILQ_FIRST(&(json_node->swallow_head));
89 TAILQ_REMOVE(&(json_node->swallow_head), match, matches);
95 if (json_node->type == CT_WORKSPACE) {
96 /* Ensure the workspace has a name. */
97 DLOG("Attaching workspace. name = %s\n", json_node->name);
98 if (json_node->name == NULL || strcmp(json_node->name, "") == 0) {
99 json_node->name = sstrdup("unnamed");
102 /* Prevent name clashes when appending a workspace, e.g. when the
103 * user tries to restore a workspace called “1” but already has a
104 * workspace called “1”. */
106 Con *workspace = NULL;
107 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
108 GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, json_node->name));
109 char *base = sstrdup(json_node->name);
111 while (workspace != NULL) {
112 FREE(json_node->name);
113 sasprintf(&(json_node->name), "%s_%d", base, cnt++);
115 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
116 GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, json_node->name));
120 /* Set num accordingly so that i3bar will properly sort it. */
121 json_node->num = ws_name_to_number(json_node->name);
124 // When appending JSON layout files that only contain the workspace
125 // _contents_, we might not have an upfront signal that the
126 // container we’re currently parsing is a floating container (like
127 // the “floating_nodes” key of the workspace container itself).
128 // That’s why we make sure the con is attached at the right place
129 // in the hierarchy in case it’s floating.
130 if (json_node->type == CT_FLOATING_CON) {
131 DLOG("fixing parent which currently is %p / %s\n", json_node->parent, json_node->parent->name);
132 json_node->parent = con_get_workspace(json_node->parent);
134 // Also set a size if none was supplied, otherwise the placeholder
135 // window cannot be created as X11 requests with width=0 or
136 // height=0 are invalid.
137 const Rect zero = {0, 0, 0, 0};
138 if (memcmp(&(json_node->rect), &zero, sizeof(Rect)) == 0) {
139 DLOG("Geometry not set, combining children\n");
141 TAILQ_FOREACH(child, &(json_node->nodes_head), nodes) {
142 DLOG("child geometry: %d x %d\n", child->geometry.width, child->geometry.height);
143 json_node->rect.width += child->geometry.width;
144 json_node->rect.height = max(json_node->rect.height, child->geometry.height);
148 floating_check_size(json_node);
152 con_attach(json_node, json_node->parent, true);
153 LOG("Creating window\n");
154 x_con_init(json_node);
155 json_node = json_node->parent;
158 if (parsing_swallows && swallow_is_empty) {
159 /* We parsed an empty swallow definition. This is an invalid layout
160 * definition, hence we reject it. */
161 ELOG("Layout file is invalid: found an empty swallow definition.\n");
165 parsing_rect = false;
166 parsing_deco_rect = false;
167 parsing_window_rect = false;
168 parsing_geometry = false;
172 static int json_end_array(void *ctx) {
173 LOG("end of array\n");
174 if (!parsing_swallows && !parsing_focus && !parsing_marks) {
175 con_fix_percent(json_node);
177 if (parsing_swallows) {
178 parsing_swallows = false;
181 parsing_marks = false;
185 /* Clear the list of focus mappings */
186 struct focus_mapping *mapping;
187 TAILQ_FOREACH_REVERSE(mapping, &focus_mappings, focus_mappings_head, focus_mappings) {
188 LOG("focus (reverse) %d\n", mapping->old_id);
190 TAILQ_FOREACH(con, &(json_node->focus_head), focused) {
191 if (con->old_id != mapping->old_id)
193 LOG("got it! %p\n", con);
194 /* Move this entry to the top of the focus list. */
195 TAILQ_REMOVE(&(json_node->focus_head), con, focused);
196 TAILQ_INSERT_HEAD(&(json_node->focus_head), con, focused);
200 while (!TAILQ_EMPTY(&focus_mappings)) {
201 mapping = TAILQ_FIRST(&focus_mappings);
202 TAILQ_REMOVE(&focus_mappings, mapping, focus_mappings);
205 parsing_focus = false;
210 static int json_key(void *ctx, const unsigned char *val, size_t len) {
211 LOG("key: %.*s\n", (int)len, val);
213 last_key = scalloc(len + 1, 1);
214 memcpy(last_key, val, len);
215 if (strcasecmp(last_key, "swallows") == 0)
216 parsing_swallows = true;
218 if (strcasecmp(last_key, "rect") == 0)
221 if (strcasecmp(last_key, "deco_rect") == 0)
222 parsing_deco_rect = true;
224 if (strcasecmp(last_key, "window_rect") == 0)
225 parsing_window_rect = true;
227 if (strcasecmp(last_key, "geometry") == 0)
228 parsing_geometry = true;
230 if (strcasecmp(last_key, "focus") == 0)
231 parsing_focus = true;
233 if (strcasecmp(last_key, "marks") == 0)
234 parsing_marks = true;
239 static int json_string(void *ctx, const unsigned char *val, size_t len) {
240 LOG("string: %.*s for key %s\n", (int)len, val, last_key);
241 if (parsing_swallows) {
243 sasprintf(&sval, "%.*s", len, val);
244 if (strcasecmp(last_key, "class") == 0) {
245 current_swallow->class = regex_new(sval);
246 swallow_is_empty = false;
247 } else if (strcasecmp(last_key, "instance") == 0) {
248 current_swallow->instance = regex_new(sval);
249 swallow_is_empty = false;
250 } else if (strcasecmp(last_key, "window_role") == 0) {
251 current_swallow->window_role = regex_new(sval);
252 swallow_is_empty = false;
253 } else if (strcasecmp(last_key, "title") == 0) {
254 current_swallow->title = regex_new(sval);
255 swallow_is_empty = false;
257 ELOG("swallow key %s unknown\n", last_key);
260 } else if (parsing_marks) {
262 sasprintf(&mark, "%.*s", (int)len, val);
264 con_mark(json_node, mark, MM_ADD);
266 if (strcasecmp(last_key, "name") == 0) {
267 json_node->name = scalloc(len + 1, 1);
268 memcpy(json_node->name, val, len);
269 } else if (strcasecmp(last_key, "title_format") == 0) {
270 json_node->title_format = scalloc(len + 1, 1);
271 memcpy(json_node->title_format, val, len);
272 } else if (strcasecmp(last_key, "sticky_group") == 0) {
273 json_node->sticky_group = scalloc(len + 1, 1);
274 memcpy(json_node->sticky_group, val, len);
275 LOG("sticky_group of this container is %s\n", json_node->sticky_group);
276 } else if (strcasecmp(last_key, "orientation") == 0) {
277 /* Upgrade path from older versions of i3 (doing an inplace restart
278 * to a newer version):
279 * "orientation" is dumped before "layout". Therefore, we store
280 * whether the orientation was horizontal or vertical in the
281 * last_split_layout. When we then encounter layout == "default",
282 * we will use the last_split_layout as layout instead. */
284 sasprintf(&buf, "%.*s", (int)len, val);
285 if (strcasecmp(buf, "none") == 0 ||
286 strcasecmp(buf, "horizontal") == 0)
287 json_node->last_split_layout = L_SPLITH;
288 else if (strcasecmp(buf, "vertical") == 0)
289 json_node->last_split_layout = L_SPLITV;
291 LOG("Unhandled orientation: %s\n", buf);
293 } else if (strcasecmp(last_key, "border") == 0) {
295 sasprintf(&buf, "%.*s", (int)len, val);
296 if (strcasecmp(buf, "none") == 0)
297 json_node->border_style = BS_NONE;
298 else if (strcasecmp(buf, "1pixel") == 0) {
299 json_node->border_style = BS_PIXEL;
300 json_node->current_border_width = 1;
301 } else if (strcasecmp(buf, "pixel") == 0)
302 json_node->border_style = BS_PIXEL;
303 else if (strcasecmp(buf, "normal") == 0)
304 json_node->border_style = BS_NORMAL;
306 LOG("Unhandled \"border\": %s\n", buf);
308 } else if (strcasecmp(last_key, "type") == 0) {
310 sasprintf(&buf, "%.*s", (int)len, val);
311 if (strcasecmp(buf, "root") == 0)
312 json_node->type = CT_ROOT;
313 else if (strcasecmp(buf, "output") == 0)
314 json_node->type = CT_OUTPUT;
315 else if (strcasecmp(buf, "con") == 0)
316 json_node->type = CT_CON;
317 else if (strcasecmp(buf, "floating_con") == 0)
318 json_node->type = CT_FLOATING_CON;
319 else if (strcasecmp(buf, "workspace") == 0)
320 json_node->type = CT_WORKSPACE;
321 else if (strcasecmp(buf, "dockarea") == 0)
322 json_node->type = CT_DOCKAREA;
324 LOG("Unhandled \"type\": %s\n", buf);
326 } else if (strcasecmp(last_key, "layout") == 0) {
328 sasprintf(&buf, "%.*s", (int)len, val);
329 if (strcasecmp(buf, "default") == 0)
330 /* This set above when we read "orientation". */
331 json_node->layout = json_node->last_split_layout;
332 else if (strcasecmp(buf, "stacked") == 0)
333 json_node->layout = L_STACKED;
334 else if (strcasecmp(buf, "tabbed") == 0)
335 json_node->layout = L_TABBED;
336 else if (strcasecmp(buf, "dockarea") == 0)
337 json_node->layout = L_DOCKAREA;
338 else if (strcasecmp(buf, "output") == 0)
339 json_node->layout = L_OUTPUT;
340 else if (strcasecmp(buf, "splith") == 0)
341 json_node->layout = L_SPLITH;
342 else if (strcasecmp(buf, "splitv") == 0)
343 json_node->layout = L_SPLITV;
345 LOG("Unhandled \"layout\": %s\n", buf);
347 } else if (strcasecmp(last_key, "workspace_layout") == 0) {
349 sasprintf(&buf, "%.*s", (int)len, val);
350 if (strcasecmp(buf, "default") == 0)
351 json_node->workspace_layout = L_DEFAULT;
352 else if (strcasecmp(buf, "stacked") == 0)
353 json_node->workspace_layout = L_STACKED;
354 else if (strcasecmp(buf, "tabbed") == 0)
355 json_node->workspace_layout = L_TABBED;
357 LOG("Unhandled \"workspace_layout\": %s\n", buf);
359 } else if (strcasecmp(last_key, "last_split_layout") == 0) {
361 sasprintf(&buf, "%.*s", (int)len, val);
362 if (strcasecmp(buf, "splith") == 0)
363 json_node->last_split_layout = L_SPLITH;
364 else if (strcasecmp(buf, "splitv") == 0)
365 json_node->last_split_layout = L_SPLITV;
367 LOG("Unhandled \"last_splitlayout\": %s\n", buf);
369 } else if (strcasecmp(last_key, "mark") == 0) {
370 DLOG("Found deprecated key \"mark\".\n");
373 sasprintf(&buf, "%.*s", (int)len, val);
375 con_mark(json_node, buf, MM_REPLACE);
376 } else if (strcasecmp(last_key, "floating") == 0) {
378 sasprintf(&buf, "%.*s", (int)len, val);
379 if (strcasecmp(buf, "auto_off") == 0)
380 json_node->floating = FLOATING_AUTO_OFF;
381 else if (strcasecmp(buf, "auto_on") == 0)
382 json_node->floating = FLOATING_AUTO_ON;
383 else if (strcasecmp(buf, "user_off") == 0)
384 json_node->floating = FLOATING_USER_OFF;
385 else if (strcasecmp(buf, "user_on") == 0)
386 json_node->floating = FLOATING_USER_ON;
388 } else if (strcasecmp(last_key, "scratchpad_state") == 0) {
390 sasprintf(&buf, "%.*s", (int)len, val);
391 if (strcasecmp(buf, "none") == 0)
392 json_node->scratchpad_state = SCRATCHPAD_NONE;
393 else if (strcasecmp(buf, "fresh") == 0)
394 json_node->scratchpad_state = SCRATCHPAD_FRESH;
395 else if (strcasecmp(buf, "changed") == 0)
396 json_node->scratchpad_state = SCRATCHPAD_CHANGED;
403 static int json_int(void *ctx, long long val) {
404 LOG("int %lld for key %s\n", val, last_key);
405 /* For backwards compatibility with i3 < 4.8 */
406 if (strcasecmp(last_key, "type") == 0)
407 json_node->type = val;
409 if (strcasecmp(last_key, "fullscreen_mode") == 0)
410 json_node->fullscreen_mode = val;
412 if (strcasecmp(last_key, "num") == 0)
413 json_node->num = val;
415 if (strcasecmp(last_key, "current_border_width") == 0)
416 json_node->current_border_width = val;
418 if (strcasecmp(last_key, "depth") == 0)
419 json_node->depth = val;
421 if (!parsing_swallows && strcasecmp(last_key, "id") == 0)
422 json_node->old_id = val;
425 struct focus_mapping *focus_mapping = scalloc(1, sizeof(struct focus_mapping));
426 focus_mapping->old_id = val;
427 TAILQ_INSERT_TAIL(&focus_mappings, focus_mapping, focus_mappings);
430 if (parsing_rect || parsing_window_rect || parsing_geometry) {
433 r = &(json_node->rect);
434 else if (parsing_window_rect)
435 r = &(json_node->window_rect);
437 r = &(json_node->geometry);
438 if (strcasecmp(last_key, "x") == 0)
440 else if (strcasecmp(last_key, "y") == 0)
442 else if (strcasecmp(last_key, "width") == 0)
444 else if (strcasecmp(last_key, "height") == 0)
447 ELOG("WARNING: unknown key %s in rect\n", last_key);
448 DLOG("rect now: (%d, %d, %d, %d)\n",
449 r->x, r->y, r->width, r->height);
451 if (parsing_swallows) {
452 if (strcasecmp(last_key, "id") == 0) {
453 current_swallow->id = val;
454 swallow_is_empty = false;
456 if (strcasecmp(last_key, "dock") == 0) {
457 current_swallow->dock = val;
458 swallow_is_empty = false;
460 if (strcasecmp(last_key, "insert_where") == 0) {
461 current_swallow->insert_where = val;
462 swallow_is_empty = false;
469 static int json_bool(void *ctx, int val) {
470 LOG("bool %d for key %s\n", val, last_key);
471 if (strcasecmp(last_key, "focused") == 0 && val) {
472 to_focus = json_node;
475 if (strcasecmp(last_key, "sticky") == 0)
476 json_node->sticky = val;
478 if (parsing_swallows) {
479 if (strcasecmp(last_key, "restart_mode") == 0) {
480 current_swallow->restart_mode = val;
481 swallow_is_empty = false;
488 static int json_double(void *ctx, double val) {
489 LOG("double %f for key %s\n", val, last_key);
490 if (strcasecmp(last_key, "percent") == 0) {
491 json_node->percent = val;
496 static json_content_t content_result;
497 static int content_level;
499 static int json_determine_content_deeper(void *ctx) {
504 static int json_determine_content_shallower(void *ctx) {
509 static int json_determine_content_string(void *ctx, const unsigned char *val, size_t len) {
510 if (strcasecmp(last_key, "type") != 0 || content_level > 1)
513 DLOG("string = %.*s, last_key = %s\n", (int)len, val, last_key);
514 if (strncasecmp((const char *)val, "workspace", len) == 0)
515 content_result = JSON_CONTENT_WORKSPACE;
519 /* Parses the given JSON file until it encounters the first “type” property to
520 * determine whether the file contains workspaces or regular containers, which
521 * is important to know when deciding where (and how) to append the contents.
523 json_content_t json_determine_content(const char *filename) {
525 if ((f = fopen(filename, "r")) == NULL) {
526 ELOG("Cannot open file \"%s\"\n", filename);
527 return JSON_CONTENT_UNKNOWN;
530 if (fstat(fileno(f), &stbuf) != 0) {
531 ELOG("Cannot fstat() \"%s\"\n", filename);
533 return JSON_CONTENT_UNKNOWN;
535 char *buf = smalloc(stbuf.st_size);
536 int n = fread(buf, 1, stbuf.st_size, f);
537 if (n != stbuf.st_size) {
538 ELOG("File \"%s\" could not be read entirely, not loading.\n", filename);
540 return JSON_CONTENT_UNKNOWN;
542 DLOG("read %d bytes\n", n);
543 // We default to JSON_CONTENT_CON because it is legal to not include
544 // “"type": "con"” in the JSON files for better readability.
545 content_result = JSON_CONTENT_CON;
549 static yajl_callbacks callbacks = {
550 .yajl_string = json_determine_content_string,
551 .yajl_map_key = json_key,
552 .yajl_start_array = json_determine_content_deeper,
553 .yajl_start_map = json_determine_content_deeper,
554 .yajl_end_map = json_determine_content_shallower,
555 .yajl_end_array = json_determine_content_shallower,
557 g = yajl_gen_alloc(NULL);
558 hand = yajl_alloc(&callbacks, NULL, (void *)g);
559 /* Allowing comments allows for more user-friendly layout files. */
560 yajl_config(hand, yajl_allow_comments, true);
561 /* Allow multiple values, i.e. multiple nodes to attach */
562 yajl_config(hand, yajl_allow_multiple_values, true);
564 setlocale(LC_NUMERIC, "C");
565 stat = yajl_parse(hand, (const unsigned char *)buf, n);
566 if (stat != yajl_status_ok && stat != yajl_status_client_canceled) {
567 unsigned char *str = yajl_get_error(hand, 1, (const unsigned char *)buf, n);
568 ELOG("JSON parsing error: %s\n", str);
569 yajl_free_error(hand, str);
572 setlocale(LC_NUMERIC, "");
573 yajl_complete_parse(hand);
577 return content_result;
580 void tree_append_json(Con *con, const char *filename, char **errormsg) {
582 if ((f = fopen(filename, "r")) == NULL) {
583 ELOG("Cannot open file \"%s\"\n", filename);
587 if (fstat(fileno(f), &stbuf) != 0) {
588 ELOG("Cannot fstat() \"%s\"\n", filename);
592 char *buf = smalloc(stbuf.st_size);
593 int n = fread(buf, 1, stbuf.st_size, f);
594 if (n != stbuf.st_size) {
595 ELOG("File \"%s\" could not be read entirely, not loading.\n", filename);
599 DLOG("read %d bytes\n", n);
602 static yajl_callbacks callbacks = {
603 .yajl_boolean = json_bool,
604 .yajl_integer = json_int,
605 .yajl_double = json_double,
606 .yajl_string = json_string,
607 .yajl_start_map = json_start_map,
608 .yajl_map_key = json_key,
609 .yajl_end_map = json_end_map,
610 .yajl_end_array = json_end_array,
612 g = yajl_gen_alloc(NULL);
613 hand = yajl_alloc(&callbacks, NULL, (void *)g);
614 /* Allowing comments allows for more user-friendly layout files. */
615 yajl_config(hand, yajl_allow_comments, true);
616 /* Allow multiple values, i.e. multiple nodes to attach */
617 yajl_config(hand, yajl_allow_multiple_values, true);
621 parsing_swallows = false;
622 parsing_rect = false;
623 parsing_deco_rect = false;
624 parsing_window_rect = false;
625 parsing_geometry = false;
626 parsing_focus = false;
627 parsing_marks = false;
628 setlocale(LC_NUMERIC, "C");
629 stat = yajl_parse(hand, (const unsigned char *)buf, n);
630 if (stat != yajl_status_ok) {
631 unsigned char *str = yajl_get_error(hand, 1, (const unsigned char *)buf, n);
632 ELOG("JSON parsing error: %s\n", str);
633 if (errormsg != NULL)
634 *errormsg = sstrdup((const char *)str);
635 yajl_free_error(hand, str);
638 /* In case not all containers were restored, we need to fix the
639 * percentages, otherwise i3 will crash immediately when rendering the
641 con_fix_percent(con);
643 setlocale(LC_NUMERIC, "");
644 yajl_complete_parse(hand);