2 #define I3__FILE__ "load_layout.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
9 * load_layout.c: Restore (parts of) the layout, for example after an inplace
15 #include <yajl/yajl_common.h>
16 #include <yajl/yajl_gen.h>
17 #include <yajl/yajl_parse.h>
18 #include <yajl/yajl_version.h>
20 /* TODO: refactor the whole parsing thing */
22 static char *last_key;
23 static Con *json_node;
25 static bool parsing_swallows;
26 static bool parsing_rect;
27 static bool parsing_deco_rect;
28 static bool parsing_window_rect;
29 static bool parsing_geometry;
30 static bool parsing_focus;
31 static bool parsing_marks;
32 struct Match *current_swallow;
34 /* This list is used for reordering the focus stack after parsing the 'focus'
36 struct focus_mapping {
38 TAILQ_ENTRY(focus_mapping) focus_mappings;
41 static TAILQ_HEAD(focus_mappings_head, focus_mapping) focus_mappings =
42 TAILQ_HEAD_INITIALIZER(focus_mappings);
44 static int json_start_map(void *ctx) {
45 LOG("start of map, last_key = %s\n", last_key);
46 if (parsing_swallows) {
47 LOG("creating new swallow\n");
48 current_swallow = smalloc(sizeof(Match));
49 match_init(current_swallow);
50 TAILQ_INSERT_TAIL(&(json_node->swallow_head), current_swallow, matches);
52 if (!parsing_rect && !parsing_deco_rect && !parsing_window_rect && !parsing_geometry) {
53 if (last_key && strcasecmp(last_key, "floating_nodes") == 0) {
54 DLOG("New floating_node\n");
55 Con *ws = con_get_workspace(json_node);
56 json_node = con_new_skeleton(NULL, NULL);
57 json_node->name = NULL;
58 json_node->parent = ws;
59 DLOG("Parent is workspace = %p\n", ws);
61 Con *parent = json_node;
62 json_node = con_new_skeleton(NULL, NULL);
63 json_node->name = NULL;
64 json_node->parent = parent;
71 static int json_end_map(void *ctx) {
73 if (!parsing_swallows && !parsing_rect && !parsing_deco_rect && !parsing_window_rect && !parsing_geometry) {
74 /* Set a few default values to simplify manually crafted layout files. */
75 if (json_node->layout == L_DEFAULT) {
76 DLOG("Setting layout = L_SPLITH\n");
77 json_node->layout = L_SPLITH;
80 /* Sanity check: swallow criteria don’t make any sense on a split
82 if (con_is_split(json_node) > 0 && !TAILQ_EMPTY(&(json_node->swallow_head))) {
83 DLOG("sanity check: removing swallows specification from split container\n");
84 while (!TAILQ_EMPTY(&(json_node->swallow_head))) {
85 Match *match = TAILQ_FIRST(&(json_node->swallow_head));
86 TAILQ_REMOVE(&(json_node->swallow_head), match, matches);
91 if (json_node->type == CT_WORKSPACE) {
92 /* Ensure the workspace has a name. */
93 DLOG("Attaching workspace. name = %s\n", json_node->name);
94 if (json_node->name == NULL || strcmp(json_node->name, "") == 0) {
95 json_node->name = sstrdup("unnamed");
98 /* Prevent name clashes when appending a workspace, e.g. when the
99 * user tries to restore a workspace called “1” but already has a
100 * workspace called “1”. */
102 Con *workspace = NULL;
103 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
104 GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, json_node->name));
105 char *base = sstrdup(json_node->name);
107 while (workspace != NULL) {
108 FREE(json_node->name);
109 sasprintf(&(json_node->name), "%s_%d", base, cnt++);
111 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
112 GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, json_node->name));
116 /* Set num accordingly so that i3bar will properly sort it. */
117 json_node->num = ws_name_to_number(json_node->name);
120 // When appending JSON layout files that only contain the workspace
121 // _contents_, we might not have an upfront signal that the
122 // container we’re currently parsing is a floating container (like
123 // the “floating_nodes” key of the workspace container itself).
124 // That’s why we make sure the con is attached at the right place
125 // in the hierarchy in case it’s floating.
126 if (json_node->type == CT_FLOATING_CON) {
127 DLOG("fixing parent which currently is %p / %s\n", json_node->parent, json_node->parent->name);
128 json_node->parent = con_get_workspace(json_node->parent);
130 // Also set a size if none was supplied, otherwise the placeholder
131 // window cannot be created as X11 requests with width=0 or
132 // height=0 are invalid.
133 const Rect zero = {0, 0, 0, 0};
134 if (memcmp(&(json_node->rect), &zero, sizeof(Rect)) == 0) {
135 DLOG("Geometry not set, combining children\n");
137 TAILQ_FOREACH(child, &(json_node->nodes_head), nodes) {
138 DLOG("child geometry: %d x %d\n", child->geometry.width, child->geometry.height);
139 json_node->rect.width += child->geometry.width;
140 json_node->rect.height = max(json_node->rect.height, child->geometry.height);
144 floating_check_size(json_node);
148 con_attach(json_node, json_node->parent, true);
149 LOG("Creating window\n");
150 x_con_init(json_node, json_node->depth);
151 json_node = json_node->parent;
154 parsing_rect = false;
155 parsing_deco_rect = false;
156 parsing_window_rect = false;
157 parsing_geometry = false;
161 static int json_end_array(void *ctx) {
162 LOG("end of array\n");
163 if (!parsing_swallows && !parsing_focus && !parsing_marks) {
164 con_fix_percent(json_node);
166 if (parsing_swallows) {
167 parsing_swallows = false;
170 parsing_marks = false;
174 /* Clear the list of focus mappings */
175 struct focus_mapping *mapping;
176 TAILQ_FOREACH_REVERSE(mapping, &focus_mappings, focus_mappings_head, focus_mappings) {
177 LOG("focus (reverse) %d\n", mapping->old_id);
179 TAILQ_FOREACH(con, &(json_node->focus_head), focused) {
180 if (con->old_id != mapping->old_id)
182 LOG("got it! %p\n", con);
183 /* Move this entry to the top of the focus list. */
184 TAILQ_REMOVE(&(json_node->focus_head), con, focused);
185 TAILQ_INSERT_HEAD(&(json_node->focus_head), con, focused);
189 while (!TAILQ_EMPTY(&focus_mappings)) {
190 mapping = TAILQ_FIRST(&focus_mappings);
191 TAILQ_REMOVE(&focus_mappings, mapping, focus_mappings);
194 parsing_focus = false;
199 static int json_key(void *ctx, const unsigned char *val, size_t len) {
200 LOG("key: %.*s\n", (int)len, val);
202 last_key = scalloc(len + 1, 1);
203 memcpy(last_key, val, len);
204 if (strcasecmp(last_key, "swallows") == 0)
205 parsing_swallows = true;
207 if (strcasecmp(last_key, "rect") == 0)
210 if (strcasecmp(last_key, "deco_rect") == 0)
211 parsing_deco_rect = true;
213 if (strcasecmp(last_key, "window_rect") == 0)
214 parsing_window_rect = true;
216 if (strcasecmp(last_key, "geometry") == 0)
217 parsing_geometry = true;
219 if (strcasecmp(last_key, "focus") == 0)
220 parsing_focus = true;
222 if (strcasecmp(last_key, "marks") == 0)
223 parsing_marks = true;
228 static int json_string(void *ctx, const unsigned char *val, size_t len) {
229 LOG("string: %.*s for key %s\n", (int)len, val, last_key);
230 if (parsing_swallows) {
232 sasprintf(&sval, "%.*s", len, val);
233 if (strcasecmp(last_key, "class") == 0) {
234 current_swallow->class = regex_new(sval);
235 } else if (strcasecmp(last_key, "instance") == 0) {
236 current_swallow->instance = regex_new(sval);
237 } else if (strcasecmp(last_key, "window_role") == 0) {
238 current_swallow->window_role = regex_new(sval);
239 } else if (strcasecmp(last_key, "title") == 0) {
240 current_swallow->title = regex_new(sval);
242 ELOG("swallow key %s unknown\n", last_key);
245 } else if (parsing_marks) {
247 sasprintf(&mark, "%.*s", (int)len, val);
249 con_mark(json_node, mark, MM_ADD);
251 if (strcasecmp(last_key, "name") == 0) {
252 json_node->name = scalloc(len + 1, 1);
253 memcpy(json_node->name, val, len);
254 } else if (strcasecmp(last_key, "sticky_group") == 0) {
255 json_node->sticky_group = scalloc(len + 1, 1);
256 memcpy(json_node->sticky_group, val, len);
257 LOG("sticky_group of this container is %s\n", json_node->sticky_group);
258 } else if (strcasecmp(last_key, "orientation") == 0) {
259 /* Upgrade path from older versions of i3 (doing an inplace restart
260 * to a newer version):
261 * "orientation" is dumped before "layout". Therefore, we store
262 * whether the orientation was horizontal or vertical in the
263 * last_split_layout. When we then encounter layout == "default",
264 * we will use the last_split_layout as layout instead. */
266 sasprintf(&buf, "%.*s", (int)len, val);
267 if (strcasecmp(buf, "none") == 0 ||
268 strcasecmp(buf, "horizontal") == 0)
269 json_node->last_split_layout = L_SPLITH;
270 else if (strcasecmp(buf, "vertical") == 0)
271 json_node->last_split_layout = L_SPLITV;
273 LOG("Unhandled orientation: %s\n", buf);
275 } else if (strcasecmp(last_key, "border") == 0) {
277 sasprintf(&buf, "%.*s", (int)len, val);
278 if (strcasecmp(buf, "none") == 0)
279 json_node->border_style = BS_NONE;
280 else if (strcasecmp(buf, "1pixel") == 0) {
281 json_node->border_style = BS_PIXEL;
282 json_node->current_border_width = 1;
283 } else if (strcasecmp(buf, "pixel") == 0)
284 json_node->border_style = BS_PIXEL;
285 else if (strcasecmp(buf, "normal") == 0)
286 json_node->border_style = BS_NORMAL;
288 LOG("Unhandled \"border\": %s\n", buf);
290 } else if (strcasecmp(last_key, "type") == 0) {
292 sasprintf(&buf, "%.*s", (int)len, val);
293 if (strcasecmp(buf, "root") == 0)
294 json_node->type = CT_ROOT;
295 else if (strcasecmp(buf, "output") == 0)
296 json_node->type = CT_OUTPUT;
297 else if (strcasecmp(buf, "con") == 0)
298 json_node->type = CT_CON;
299 else if (strcasecmp(buf, "floating_con") == 0)
300 json_node->type = CT_FLOATING_CON;
301 else if (strcasecmp(buf, "workspace") == 0)
302 json_node->type = CT_WORKSPACE;
303 else if (strcasecmp(buf, "dockarea") == 0)
304 json_node->type = CT_DOCKAREA;
306 LOG("Unhandled \"type\": %s\n", buf);
308 } else if (strcasecmp(last_key, "layout") == 0) {
310 sasprintf(&buf, "%.*s", (int)len, val);
311 if (strcasecmp(buf, "default") == 0)
312 /* This set above when we read "orientation". */
313 json_node->layout = json_node->last_split_layout;
314 else if (strcasecmp(buf, "stacked") == 0)
315 json_node->layout = L_STACKED;
316 else if (strcasecmp(buf, "tabbed") == 0)
317 json_node->layout = L_TABBED;
318 else if (strcasecmp(buf, "dockarea") == 0)
319 json_node->layout = L_DOCKAREA;
320 else if (strcasecmp(buf, "output") == 0)
321 json_node->layout = L_OUTPUT;
322 else if (strcasecmp(buf, "splith") == 0)
323 json_node->layout = L_SPLITH;
324 else if (strcasecmp(buf, "splitv") == 0)
325 json_node->layout = L_SPLITV;
327 LOG("Unhandled \"layout\": %s\n", buf);
329 } else if (strcasecmp(last_key, "workspace_layout") == 0) {
331 sasprintf(&buf, "%.*s", (int)len, val);
332 if (strcasecmp(buf, "default") == 0)
333 json_node->workspace_layout = L_DEFAULT;
334 else if (strcasecmp(buf, "stacked") == 0)
335 json_node->workspace_layout = L_STACKED;
336 else if (strcasecmp(buf, "tabbed") == 0)
337 json_node->workspace_layout = L_TABBED;
339 LOG("Unhandled \"workspace_layout\": %s\n", buf);
341 } else if (strcasecmp(last_key, "last_split_layout") == 0) {
343 sasprintf(&buf, "%.*s", (int)len, val);
344 if (strcasecmp(buf, "splith") == 0)
345 json_node->last_split_layout = L_SPLITH;
346 else if (strcasecmp(buf, "splitv") == 0)
347 json_node->last_split_layout = L_SPLITV;
349 LOG("Unhandled \"last_splitlayout\": %s\n", buf);
351 } else if (strcasecmp(last_key, "mark") == 0) {
352 DLOG("Found deprecated key \"mark\".\n");
355 sasprintf(&buf, "%.*s", (int)len, val);
357 con_mark(json_node, buf, MM_REPLACE);
358 } else if (strcasecmp(last_key, "floating") == 0) {
360 sasprintf(&buf, "%.*s", (int)len, val);
361 if (strcasecmp(buf, "auto_off") == 0)
362 json_node->floating = FLOATING_AUTO_OFF;
363 else if (strcasecmp(buf, "auto_on") == 0)
364 json_node->floating = FLOATING_AUTO_ON;
365 else if (strcasecmp(buf, "user_off") == 0)
366 json_node->floating = FLOATING_USER_OFF;
367 else if (strcasecmp(buf, "user_on") == 0)
368 json_node->floating = FLOATING_USER_ON;
370 } else if (strcasecmp(last_key, "scratchpad_state") == 0) {
372 sasprintf(&buf, "%.*s", (int)len, val);
373 if (strcasecmp(buf, "none") == 0)
374 json_node->scratchpad_state = SCRATCHPAD_NONE;
375 else if (strcasecmp(buf, "fresh") == 0)
376 json_node->scratchpad_state = SCRATCHPAD_FRESH;
377 else if (strcasecmp(buf, "changed") == 0)
378 json_node->scratchpad_state = SCRATCHPAD_CHANGED;
385 static int json_int(void *ctx, long long val) {
386 LOG("int %lld for key %s\n", val, last_key);
387 /* For backwards compatibility with i3 < 4.8 */
388 if (strcasecmp(last_key, "type") == 0)
389 json_node->type = val;
391 if (strcasecmp(last_key, "fullscreen_mode") == 0)
392 json_node->fullscreen_mode = val;
394 if (strcasecmp(last_key, "num") == 0)
395 json_node->num = val;
397 if (strcasecmp(last_key, "current_border_width") == 0)
398 json_node->current_border_width = val;
400 if (strcasecmp(last_key, "depth") == 0)
401 json_node->depth = val;
403 if (!parsing_swallows && strcasecmp(last_key, "id") == 0)
404 json_node->old_id = val;
407 struct focus_mapping *focus_mapping = scalloc(1, sizeof(struct focus_mapping));
408 focus_mapping->old_id = val;
409 TAILQ_INSERT_TAIL(&focus_mappings, focus_mapping, focus_mappings);
412 if (parsing_rect || parsing_window_rect || parsing_geometry) {
415 r = &(json_node->rect);
416 else if (parsing_window_rect)
417 r = &(json_node->window_rect);
419 r = &(json_node->geometry);
420 if (strcasecmp(last_key, "x") == 0)
422 else if (strcasecmp(last_key, "y") == 0)
424 else if (strcasecmp(last_key, "width") == 0)
426 else if (strcasecmp(last_key, "height") == 0)
429 ELOG("WARNING: unknown key %s in rect\n", last_key);
430 DLOG("rect now: (%d, %d, %d, %d)\n",
431 r->x, r->y, r->width, r->height);
433 if (parsing_swallows) {
434 if (strcasecmp(last_key, "id") == 0) {
435 current_swallow->id = val;
437 if (strcasecmp(last_key, "dock") == 0) {
438 current_swallow->dock = val;
440 if (strcasecmp(last_key, "insert_where") == 0) {
441 current_swallow->insert_where = val;
448 static int json_bool(void *ctx, int val) {
449 LOG("bool %d for key %s\n", val, last_key);
450 if (strcasecmp(last_key, "focused") == 0 && val) {
451 to_focus = json_node;
454 if (strcasecmp(last_key, "sticky") == 0)
455 json_node->sticky = val;
457 if (parsing_swallows) {
458 if (strcasecmp(last_key, "restart_mode") == 0)
459 current_swallow->restart_mode = val;
465 static int json_double(void *ctx, double val) {
466 LOG("double %f for key %s\n", val, last_key);
467 if (strcasecmp(last_key, "percent") == 0) {
468 json_node->percent = val;
473 static json_content_t content_result;
474 static int content_level;
476 static int json_determine_content_deeper(void *ctx) {
481 static int json_determine_content_shallower(void *ctx) {
486 static int json_determine_content_string(void *ctx, const unsigned char *val, size_t len) {
487 if (strcasecmp(last_key, "type") != 0 || content_level > 1)
490 DLOG("string = %.*s, last_key = %s\n", (int)len, val, last_key);
491 if (strncasecmp((const char *)val, "workspace", len) == 0)
492 content_result = JSON_CONTENT_WORKSPACE;
496 /* Parses the given JSON file until it encounters the first “type” property to
497 * determine whether the file contains workspaces or regular containers, which
498 * is important to know when deciding where (and how) to append the contents.
500 json_content_t json_determine_content(const char *filename) {
502 if ((f = fopen(filename, "r")) == NULL) {
503 ELOG("Cannot open file \"%s\"\n", filename);
504 return JSON_CONTENT_UNKNOWN;
507 if (fstat(fileno(f), &stbuf) != 0) {
508 ELOG("Cannot fstat() \"%s\"\n", filename);
510 return JSON_CONTENT_UNKNOWN;
512 char *buf = smalloc(stbuf.st_size);
513 int n = fread(buf, 1, stbuf.st_size, f);
514 if (n != stbuf.st_size) {
515 ELOG("File \"%s\" could not be read entirely, not loading.\n", filename);
517 return JSON_CONTENT_UNKNOWN;
519 DLOG("read %d bytes\n", n);
520 // We default to JSON_CONTENT_CON because it is legal to not include
521 // “"type": "con"” in the JSON files for better readability.
522 content_result = JSON_CONTENT_CON;
526 static yajl_callbacks callbacks = {
527 .yajl_string = json_determine_content_string,
528 .yajl_map_key = json_key,
529 .yajl_start_array = json_determine_content_deeper,
530 .yajl_start_map = json_determine_content_deeper,
531 .yajl_end_map = json_determine_content_shallower,
532 .yajl_end_array = json_determine_content_shallower,
534 g = yajl_gen_alloc(NULL);
535 hand = yajl_alloc(&callbacks, NULL, (void *)g);
536 /* Allowing comments allows for more user-friendly layout files. */
537 yajl_config(hand, yajl_allow_comments, true);
538 /* Allow multiple values, i.e. multiple nodes to attach */
539 yajl_config(hand, yajl_allow_multiple_values, true);
541 setlocale(LC_NUMERIC, "C");
542 stat = yajl_parse(hand, (const unsigned char *)buf, n);
543 if (stat != yajl_status_ok && stat != yajl_status_client_canceled) {
544 unsigned char *str = yajl_get_error(hand, 1, (const unsigned char *)buf, n);
545 ELOG("JSON parsing error: %s\n", str);
546 yajl_free_error(hand, str);
549 setlocale(LC_NUMERIC, "");
550 yajl_complete_parse(hand);
554 return content_result;
557 void tree_append_json(Con *con, const char *filename, char **errormsg) {
559 if ((f = fopen(filename, "r")) == NULL) {
560 ELOG("Cannot open file \"%s\"\n", filename);
564 if (fstat(fileno(f), &stbuf) != 0) {
565 ELOG("Cannot fstat() \"%s\"\n", filename);
569 char *buf = smalloc(stbuf.st_size);
570 int n = fread(buf, 1, stbuf.st_size, f);
571 if (n != stbuf.st_size) {
572 ELOG("File \"%s\" could not be read entirely, not loading.\n", filename);
576 DLOG("read %d bytes\n", n);
579 static yajl_callbacks callbacks = {
580 .yajl_boolean = json_bool,
581 .yajl_integer = json_int,
582 .yajl_double = json_double,
583 .yajl_string = json_string,
584 .yajl_start_map = json_start_map,
585 .yajl_map_key = json_key,
586 .yajl_end_map = json_end_map,
587 .yajl_end_array = json_end_array,
589 g = yajl_gen_alloc(NULL);
590 hand = yajl_alloc(&callbacks, NULL, (void *)g);
591 /* Allowing comments allows for more user-friendly layout files. */
592 yajl_config(hand, yajl_allow_comments, true);
593 /* Allow multiple values, i.e. multiple nodes to attach */
594 yajl_config(hand, yajl_allow_multiple_values, true);
598 parsing_swallows = false;
599 parsing_rect = false;
600 parsing_deco_rect = false;
601 parsing_window_rect = false;
602 parsing_geometry = false;
603 parsing_focus = false;
604 parsing_marks = false;
605 setlocale(LC_NUMERIC, "C");
606 stat = yajl_parse(hand, (const unsigned char *)buf, n);
607 if (stat != yajl_status_ok) {
608 unsigned char *str = yajl_get_error(hand, 1, (const unsigned char *)buf, n);
609 ELOG("JSON parsing error: %s\n", str);
610 if (errormsg != NULL)
611 *errormsg = sstrdup((const char *)str);
612 yajl_free_error(hand, str);
615 /* In case not all containers were restored, we need to fix the
616 * percentages, otherwise i3 will crash immediately when rendering the
618 con_fix_percent(con);
620 setlocale(LC_NUMERIC, "");
621 yajl_complete_parse(hand);