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 struct Match *current_swallow;
33 /* This list is used for reordering the focus stack after parsing the 'focus'
35 struct focus_mapping {
37 TAILQ_ENTRY(focus_mapping) focus_mappings;
40 static TAILQ_HEAD(focus_mappings_head, focus_mapping) focus_mappings =
41 TAILQ_HEAD_INITIALIZER(focus_mappings);
43 static int json_start_map(void *ctx) {
44 LOG("start of map, last_key = %s\n", last_key);
45 if (parsing_swallows) {
46 LOG("creating new swallow\n");
47 current_swallow = smalloc(sizeof(Match));
48 match_init(current_swallow);
49 TAILQ_INSERT_TAIL(&(json_node->swallow_head), current_swallow, matches);
51 if (!parsing_rect && !parsing_deco_rect && !parsing_window_rect && !parsing_geometry) {
52 if (last_key && strcasecmp(last_key, "floating_nodes") == 0) {
53 DLOG("New floating_node\n");
54 Con *ws = con_get_workspace(json_node);
55 json_node = con_new_skeleton(NULL, NULL);
56 json_node->name = NULL;
57 json_node->parent = ws;
58 DLOG("Parent is workspace = %p\n", ws);
60 Con *parent = json_node;
61 json_node = con_new_skeleton(NULL, NULL);
62 json_node->name = NULL;
63 json_node->parent = parent;
70 static int json_end_map(void *ctx) {
72 if (!parsing_swallows && !parsing_rect && !parsing_deco_rect && !parsing_window_rect && !parsing_geometry) {
73 /* Set a few default values to simplify manually crafted layout files. */
74 if (json_node->layout == L_DEFAULT) {
75 DLOG("Setting layout = L_SPLITH\n");
76 json_node->layout = L_SPLITH;
79 /* Sanity check: swallow criteria don’t make any sense on a split
81 if (con_is_split(json_node) > 0 && !TAILQ_EMPTY(&(json_node->swallow_head))) {
82 DLOG("sanity check: removing swallows specification from split container\n");
83 while (!TAILQ_EMPTY(&(json_node->swallow_head))) {
84 Match *match = TAILQ_FIRST(&(json_node->swallow_head));
85 TAILQ_REMOVE(&(json_node->swallow_head), match, matches);
90 if (json_node->type == CT_WORKSPACE) {
91 /* Ensure the workspace has a name. */
92 DLOG("Attaching workspace. name = %s\n", json_node->name);
93 if (json_node->name == NULL || strcmp(json_node->name, "") == 0) {
94 json_node->name = sstrdup("unnamed");
97 /* Prevent name clashes when appending a workspace, e.g. when the
98 * user tries to restore a workspace called “1” but already has a
99 * workspace called “1”. */
101 Con *workspace = NULL;
102 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
103 GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, json_node->name));
104 char *base = sstrdup(json_node->name);
106 while (workspace != NULL) {
107 FREE(json_node->name);
108 sasprintf(&(json_node->name), "%s_%d", base, cnt++);
110 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
111 GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, json_node->name));
115 /* Set num accordingly so that i3bar will properly sort it. */
116 json_node->num = ws_name_to_number(json_node->name);
119 // When appending JSON layout files that only contain the workspace
120 // _contents_, we might not have an upfront signal that the
121 // container we’re currently parsing is a floating container (like
122 // the “floating_nodes” key of the workspace container itself).
123 // That’s why we make sure the con is attached at the right place
124 // in the hierarchy in case it’s floating.
125 if (json_node->type == CT_FLOATING_CON) {
126 DLOG("fixing parent which currently is %p / %s\n", json_node->parent, json_node->parent->name);
127 json_node->parent = con_get_workspace(json_node->parent);
129 // Also set a size if none was supplied, otherwise the placeholder
130 // window cannot be created as X11 requests with width=0 or
131 // height=0 are invalid.
132 const Rect zero = {0, 0, 0, 0};
133 if (memcmp(&(json_node->rect), &zero, sizeof(Rect)) == 0) {
134 DLOG("Geometry not set, combining children\n");
136 TAILQ_FOREACH(child, &(json_node->nodes_head), nodes) {
137 DLOG("child geometry: %d x %d\n", child->geometry.width, child->geometry.height);
138 json_node->rect.width += child->geometry.width;
139 json_node->rect.height = max(json_node->rect.height, child->geometry.height);
143 floating_check_size(json_node);
147 con_attach(json_node, json_node->parent, true);
148 LOG("Creating window\n");
149 x_con_init(json_node, json_node->depth);
150 json_node = json_node->parent;
153 parsing_rect = false;
154 parsing_deco_rect = false;
155 parsing_window_rect = false;
156 parsing_geometry = false;
160 static int json_end_array(void *ctx) {
161 LOG("end of array\n");
162 if (!parsing_swallows && !parsing_focus) {
163 con_fix_percent(json_node);
165 if (parsing_swallows) {
166 parsing_swallows = false;
169 /* Clear the list of focus mappings */
170 struct focus_mapping *mapping;
171 TAILQ_FOREACH_REVERSE(mapping, &focus_mappings, focus_mappings_head, focus_mappings) {
172 LOG("focus (reverse) %d\n", mapping->old_id);
174 TAILQ_FOREACH(con, &(json_node->focus_head), focused) {
175 if (con->old_id != mapping->old_id)
177 LOG("got it! %p\n", con);
178 /* Move this entry to the top of the focus list. */
179 TAILQ_REMOVE(&(json_node->focus_head), con, focused);
180 TAILQ_INSERT_HEAD(&(json_node->focus_head), con, focused);
184 while (!TAILQ_EMPTY(&focus_mappings)) {
185 mapping = TAILQ_FIRST(&focus_mappings);
186 TAILQ_REMOVE(&focus_mappings, mapping, focus_mappings);
189 parsing_focus = false;
194 static int json_key(void *ctx, const unsigned char *val, size_t len) {
195 LOG("key: %.*s\n", (int)len, val);
197 last_key = scalloc(len + 1, 1);
198 memcpy(last_key, val, len);
199 if (strcasecmp(last_key, "swallows") == 0)
200 parsing_swallows = true;
202 if (strcasecmp(last_key, "rect") == 0)
205 if (strcasecmp(last_key, "deco_rect") == 0)
206 parsing_deco_rect = true;
208 if (strcasecmp(last_key, "window_rect") == 0)
209 parsing_window_rect = true;
211 if (strcasecmp(last_key, "geometry") == 0)
212 parsing_geometry = true;
214 if (strcasecmp(last_key, "focus") == 0)
215 parsing_focus = true;
220 static int json_string(void *ctx, const unsigned char *val, size_t len) {
221 LOG("string: %.*s for key %s\n", (int)len, val, last_key);
222 if (parsing_swallows) {
224 sasprintf(&sval, "%.*s", len, val);
225 if (strcasecmp(last_key, "class") == 0) {
226 current_swallow->class = regex_new(sval);
227 } else if (strcasecmp(last_key, "instance") == 0) {
228 current_swallow->instance = regex_new(sval);
229 } else if (strcasecmp(last_key, "window_role") == 0) {
230 current_swallow->window_role = regex_new(sval);
231 } else if (strcasecmp(last_key, "title") == 0) {
232 current_swallow->title = regex_new(sval);
234 ELOG("swallow key %s unknown\n", last_key);
238 if (strcasecmp(last_key, "name") == 0) {
239 json_node->name = scalloc(len + 1, 1);
240 memcpy(json_node->name, val, len);
241 } else if (strcasecmp(last_key, "sticky_group") == 0) {
242 json_node->sticky_group = scalloc(len + 1, 1);
243 memcpy(json_node->sticky_group, val, len);
244 LOG("sticky_group of this container is %s\n", json_node->sticky_group);
245 } else if (strcasecmp(last_key, "orientation") == 0) {
246 /* Upgrade path from older versions of i3 (doing an inplace restart
247 * to a newer version):
248 * "orientation" is dumped before "layout". Therefore, we store
249 * whether the orientation was horizontal or vertical in the
250 * last_split_layout. When we then encounter layout == "default",
251 * we will use the last_split_layout as layout instead. */
253 sasprintf(&buf, "%.*s", (int)len, val);
254 if (strcasecmp(buf, "none") == 0 ||
255 strcasecmp(buf, "horizontal") == 0)
256 json_node->last_split_layout = L_SPLITH;
257 else if (strcasecmp(buf, "vertical") == 0)
258 json_node->last_split_layout = L_SPLITV;
260 LOG("Unhandled orientation: %s\n", buf);
262 } else if (strcasecmp(last_key, "border") == 0) {
264 sasprintf(&buf, "%.*s", (int)len, val);
265 if (strcasecmp(buf, "none") == 0)
266 json_node->border_style = BS_NONE;
267 else if (strcasecmp(buf, "1pixel") == 0) {
268 json_node->border_style = BS_PIXEL;
269 json_node->current_border_width = 1;
270 } else if (strcasecmp(buf, "pixel") == 0)
271 json_node->border_style = BS_PIXEL;
272 else if (strcasecmp(buf, "normal") == 0)
273 json_node->border_style = BS_NORMAL;
275 LOG("Unhandled \"border\": %s\n", buf);
277 } else if (strcasecmp(last_key, "type") == 0) {
279 sasprintf(&buf, "%.*s", (int)len, val);
280 if (strcasecmp(buf, "root") == 0)
281 json_node->type = CT_ROOT;
282 else if (strcasecmp(buf, "output") == 0)
283 json_node->type = CT_OUTPUT;
284 else if (strcasecmp(buf, "con") == 0)
285 json_node->type = CT_CON;
286 else if (strcasecmp(buf, "floating_con") == 0)
287 json_node->type = CT_FLOATING_CON;
288 else if (strcasecmp(buf, "workspace") == 0)
289 json_node->type = CT_WORKSPACE;
290 else if (strcasecmp(buf, "dockarea") == 0)
291 json_node->type = CT_DOCKAREA;
293 LOG("Unhandled \"type\": %s\n", buf);
295 } else if (strcasecmp(last_key, "layout") == 0) {
297 sasprintf(&buf, "%.*s", (int)len, val);
298 if (strcasecmp(buf, "default") == 0)
299 /* This set above when we read "orientation". */
300 json_node->layout = json_node->last_split_layout;
301 else if (strcasecmp(buf, "stacked") == 0)
302 json_node->layout = L_STACKED;
303 else if (strcasecmp(buf, "tabbed") == 0)
304 json_node->layout = L_TABBED;
305 else if (strcasecmp(buf, "dockarea") == 0)
306 json_node->layout = L_DOCKAREA;
307 else if (strcasecmp(buf, "output") == 0)
308 json_node->layout = L_OUTPUT;
309 else if (strcasecmp(buf, "splith") == 0)
310 json_node->layout = L_SPLITH;
311 else if (strcasecmp(buf, "splitv") == 0)
312 json_node->layout = L_SPLITV;
314 LOG("Unhandled \"layout\": %s\n", buf);
316 } else if (strcasecmp(last_key, "workspace_layout") == 0) {
318 sasprintf(&buf, "%.*s", (int)len, val);
319 if (strcasecmp(buf, "default") == 0)
320 json_node->workspace_layout = L_DEFAULT;
321 else if (strcasecmp(buf, "stacked") == 0)
322 json_node->workspace_layout = L_STACKED;
323 else if (strcasecmp(buf, "tabbed") == 0)
324 json_node->workspace_layout = L_TABBED;
326 LOG("Unhandled \"workspace_layout\": %s\n", buf);
328 } else if (strcasecmp(last_key, "last_split_layout") == 0) {
330 sasprintf(&buf, "%.*s", (int)len, val);
331 if (strcasecmp(buf, "splith") == 0)
332 json_node->last_split_layout = L_SPLITH;
333 else if (strcasecmp(buf, "splitv") == 0)
334 json_node->last_split_layout = L_SPLITV;
336 LOG("Unhandled \"last_splitlayout\": %s\n", buf);
338 } else if (strcasecmp(last_key, "mark") == 0) {
340 sasprintf(&buf, "%.*s", (int)len, val);
342 /* We unmark any containers using this mark to avoid duplicates. */
345 json_node->mark = buf;
346 } else if (strcasecmp(last_key, "floating") == 0) {
348 sasprintf(&buf, "%.*s", (int)len, val);
349 if (strcasecmp(buf, "auto_off") == 0)
350 json_node->floating = FLOATING_AUTO_OFF;
351 else if (strcasecmp(buf, "auto_on") == 0)
352 json_node->floating = FLOATING_AUTO_ON;
353 else if (strcasecmp(buf, "user_off") == 0)
354 json_node->floating = FLOATING_USER_OFF;
355 else if (strcasecmp(buf, "user_on") == 0)
356 json_node->floating = FLOATING_USER_ON;
358 } else if (strcasecmp(last_key, "scratchpad_state") == 0) {
360 sasprintf(&buf, "%.*s", (int)len, val);
361 if (strcasecmp(buf, "none") == 0)
362 json_node->scratchpad_state = SCRATCHPAD_NONE;
363 else if (strcasecmp(buf, "fresh") == 0)
364 json_node->scratchpad_state = SCRATCHPAD_FRESH;
365 else if (strcasecmp(buf, "changed") == 0)
366 json_node->scratchpad_state = SCRATCHPAD_CHANGED;
373 static int json_int(void *ctx, long long val) {
374 LOG("int %lld for key %s\n", val, last_key);
375 /* For backwards compatibility with i3 < 4.8 */
376 if (strcasecmp(last_key, "type") == 0)
377 json_node->type = val;
379 if (strcasecmp(last_key, "fullscreen_mode") == 0)
380 json_node->fullscreen_mode = val;
382 if (strcasecmp(last_key, "num") == 0)
383 json_node->num = val;
385 if (strcasecmp(last_key, "current_border_width") == 0)
386 json_node->current_border_width = val;
388 if (strcasecmp(last_key, "depth") == 0)
389 json_node->depth = val;
391 if (!parsing_swallows && strcasecmp(last_key, "id") == 0)
392 json_node->old_id = val;
395 struct focus_mapping *focus_mapping = scalloc(1, sizeof(struct focus_mapping));
396 focus_mapping->old_id = val;
397 TAILQ_INSERT_TAIL(&focus_mappings, focus_mapping, focus_mappings);
400 if (parsing_rect || parsing_window_rect || parsing_geometry) {
403 r = &(json_node->rect);
404 else if (parsing_window_rect)
405 r = &(json_node->window_rect);
407 r = &(json_node->geometry);
408 if (strcasecmp(last_key, "x") == 0)
410 else if (strcasecmp(last_key, "y") == 0)
412 else if (strcasecmp(last_key, "width") == 0)
414 else if (strcasecmp(last_key, "height") == 0)
417 ELOG("WARNING: unknown key %s in rect\n", last_key);
418 DLOG("rect now: (%d, %d, %d, %d)\n",
419 r->x, r->y, r->width, r->height);
421 if (parsing_swallows) {
422 if (strcasecmp(last_key, "id") == 0) {
423 current_swallow->id = val;
425 if (strcasecmp(last_key, "dock") == 0) {
426 current_swallow->dock = val;
428 if (strcasecmp(last_key, "insert_where") == 0) {
429 current_swallow->insert_where = val;
436 static int json_bool(void *ctx, int val) {
437 LOG("bool %d for key %s\n", val, last_key);
438 if (strcasecmp(last_key, "focused") == 0 && val) {
439 to_focus = json_node;
442 if (strcasecmp(last_key, "sticky") == 0)
443 json_node->sticky = val;
445 if (parsing_swallows) {
446 if (strcasecmp(last_key, "restart_mode") == 0)
447 current_swallow->restart_mode = val;
453 static int json_double(void *ctx, double val) {
454 LOG("double %f for key %s\n", val, last_key);
455 if (strcasecmp(last_key, "percent") == 0) {
456 json_node->percent = val;
461 static json_content_t content_result;
462 static int content_level;
464 static int json_determine_content_deeper(void *ctx) {
469 static int json_determine_content_shallower(void *ctx) {
474 static int json_determine_content_string(void *ctx, const unsigned char *val, size_t len) {
475 if (strcasecmp(last_key, "type") != 0 || content_level > 1)
478 DLOG("string = %.*s, last_key = %s\n", (int)len, val, last_key);
479 if (strncasecmp((const char *)val, "workspace", len) == 0)
480 content_result = JSON_CONTENT_WORKSPACE;
484 /* Parses the given JSON file until it encounters the first “type” property to
485 * determine whether the file contains workspaces or regular containers, which
486 * is important to know when deciding where (and how) to append the contents.
488 json_content_t json_determine_content(const char *filename) {
490 if ((f = fopen(filename, "r")) == NULL) {
491 ELOG("Cannot open file \"%s\"\n", filename);
492 return JSON_CONTENT_UNKNOWN;
495 if (fstat(fileno(f), &stbuf) != 0) {
496 ELOG("Cannot fstat() \"%s\"\n", filename);
498 return JSON_CONTENT_UNKNOWN;
500 char *buf = smalloc(stbuf.st_size);
501 int n = fread(buf, 1, stbuf.st_size, f);
502 if (n != stbuf.st_size) {
503 ELOG("File \"%s\" could not be read entirely, not loading.\n", filename);
505 return JSON_CONTENT_UNKNOWN;
507 DLOG("read %d bytes\n", n);
508 // We default to JSON_CONTENT_CON because it is legal to not include
509 // “"type": "con"” in the JSON files for better readability.
510 content_result = JSON_CONTENT_CON;
514 static yajl_callbacks callbacks = {
515 .yajl_string = json_determine_content_string,
516 .yajl_map_key = json_key,
517 .yajl_start_array = json_determine_content_deeper,
518 .yajl_start_map = json_determine_content_deeper,
519 .yajl_end_map = json_determine_content_shallower,
520 .yajl_end_array = json_determine_content_shallower,
522 g = yajl_gen_alloc(NULL);
523 hand = yajl_alloc(&callbacks, NULL, (void *)g);
524 /* Allowing comments allows for more user-friendly layout files. */
525 yajl_config(hand, yajl_allow_comments, true);
526 /* Allow multiple values, i.e. multiple nodes to attach */
527 yajl_config(hand, yajl_allow_multiple_values, true);
529 setlocale(LC_NUMERIC, "C");
530 stat = yajl_parse(hand, (const unsigned char *)buf, n);
531 if (stat != yajl_status_ok && stat != yajl_status_client_canceled) {
532 unsigned char *str = yajl_get_error(hand, 1, (const unsigned char *)buf, n);
533 ELOG("JSON parsing error: %s\n", str);
534 yajl_free_error(hand, str);
537 setlocale(LC_NUMERIC, "");
538 yajl_complete_parse(hand);
542 return content_result;
545 void tree_append_json(Con *con, const char *filename, char **errormsg) {
547 if ((f = fopen(filename, "r")) == NULL) {
548 ELOG("Cannot open file \"%s\"\n", filename);
552 if (fstat(fileno(f), &stbuf) != 0) {
553 ELOG("Cannot fstat() \"%s\"\n", filename);
557 char *buf = smalloc(stbuf.st_size);
558 int n = fread(buf, 1, stbuf.st_size, f);
559 if (n != stbuf.st_size) {
560 ELOG("File \"%s\" could not be read entirely, not loading.\n", filename);
564 DLOG("read %d bytes\n", n);
567 static yajl_callbacks callbacks = {
568 .yajl_boolean = json_bool,
569 .yajl_integer = json_int,
570 .yajl_double = json_double,
571 .yajl_string = json_string,
572 .yajl_start_map = json_start_map,
573 .yajl_map_key = json_key,
574 .yajl_end_map = json_end_map,
575 .yajl_end_array = json_end_array,
577 g = yajl_gen_alloc(NULL);
578 hand = yajl_alloc(&callbacks, NULL, (void *)g);
579 /* Allowing comments allows for more user-friendly layout files. */
580 yajl_config(hand, yajl_allow_comments, true);
581 /* Allow multiple values, i.e. multiple nodes to attach */
582 yajl_config(hand, yajl_allow_multiple_values, true);
586 parsing_swallows = false;
587 parsing_rect = false;
588 parsing_deco_rect = false;
589 parsing_window_rect = false;
590 parsing_geometry = false;
591 parsing_focus = false;
592 setlocale(LC_NUMERIC, "C");
593 stat = yajl_parse(hand, (const unsigned char *)buf, n);
594 if (stat != yajl_status_ok) {
595 unsigned char *str = yajl_get_error(hand, 1, (const unsigned char *)buf, n);
596 ELOG("JSON parsing error: %s\n", str);
597 if (errormsg != NULL)
598 *errormsg = sstrdup((const char *)str);
599 yajl_free_error(hand, str);
602 /* In case not all containers were restored, we need to fix the
603 * percentages, otherwise i3 will crash immediately when rendering the
605 con_fix_percent(con);
607 setlocale(LC_NUMERIC, "");
608 yajl_complete_parse(hand);