2 #define I3__FILE__ "load_layout.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009-2012 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_window_rect;
28 static bool parsing_geometry;
29 static bool parsing_focus;
30 struct Match *current_swallow;
32 /* This list is used for reordering the focus stack after parsing the 'focus'
34 struct focus_mapping {
36 TAILQ_ENTRY(focus_mapping) focus_mappings;
39 static TAILQ_HEAD(focus_mappings_head, focus_mapping) focus_mappings =
40 TAILQ_HEAD_INITIALIZER(focus_mappings);
42 static int json_start_map(void *ctx) {
43 LOG("start of map, last_key = %s\n", last_key);
44 if (parsing_swallows) {
45 LOG("creating new swallow\n");
46 current_swallow = smalloc(sizeof(Match));
47 match_init(current_swallow);
48 TAILQ_INSERT_TAIL(&(json_node->swallow_head), current_swallow, matches);
50 if (!parsing_rect && !parsing_window_rect && !parsing_geometry) {
51 if (last_key && strcasecmp(last_key, "floating_nodes") == 0) {
52 DLOG("New floating_node\n");
53 Con *ws = con_get_workspace(json_node);
54 json_node = con_new_skeleton(NULL, NULL);
55 json_node->name = NULL;
56 json_node->parent = ws;
57 DLOG("Parent is workspace = %p\n", ws);
59 Con *parent = json_node;
60 json_node = con_new_skeleton(NULL, NULL);
61 json_node->name = NULL;
62 json_node->parent = parent;
69 static int json_end_map(void *ctx) {
71 if (!parsing_swallows && !parsing_rect && !parsing_window_rect && !parsing_geometry) {
72 /* Set a few default values to simplify manually crafted layout files. */
73 if (json_node->layout == L_DEFAULT) {
74 DLOG("Setting layout = L_SPLITH\n");
75 json_node->layout = L_SPLITH;
78 /* Sanity check: swallow criteria don’t make any sense on a split
80 if (con_is_split(json_node) > 0 && !TAILQ_EMPTY(&(json_node->swallow_head))) {
81 DLOG("sanity check: removing swallows specification from split container\n");
82 while (!TAILQ_EMPTY(&(json_node->swallow_head))) {
83 Match *match = TAILQ_FIRST(&(json_node->swallow_head));
84 TAILQ_REMOVE(&(json_node->swallow_head), match, matches);
89 if (json_node->type == CT_WORKSPACE) {
90 /* Ensure the workspace has a name. */
91 DLOG("Attaching workspace. name = %s\n", json_node->name);
92 if (json_node->name == NULL || strcmp(json_node->name, "") == 0) {
93 json_node->name = sstrdup("unnamed");
96 /* Prevent name clashes when appending a workspace, e.g. when the
97 * user tries to restore a workspace called “1” but already has a
98 * workspace called “1”. */
100 Con *workspace = NULL;
101 TAILQ_FOREACH (output, &(croot->nodes_head), nodes)
102 GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, json_node->name));
103 char *base = sstrdup(json_node->name);
105 while (workspace != NULL) {
106 FREE(json_node->name);
107 asprintf(&(json_node->name), "%s_%d", base, cnt++);
109 TAILQ_FOREACH (output, &(croot->nodes_head), nodes)
110 GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, json_node->name));
114 /* Set num accordingly so that i3bar will properly sort it. */
115 json_node->num = ws_name_to_number(json_node->name);
117 // TODO: remove this in the “next” branch.
118 if (json_node->name == NULL || strcmp(json_node->name, "") == 0) {
119 json_node->name = sstrdup("#ff0000");
124 con_attach(json_node, json_node->parent, true);
125 LOG("Creating window\n");
126 x_con_init(json_node, json_node->depth);
127 json_node = json_node->parent;
130 parsing_rect = false;
131 if (parsing_window_rect)
132 parsing_window_rect = false;
133 if (parsing_geometry)
134 parsing_geometry = false;
138 static int json_end_array(void *ctx) {
139 LOG("end of array\n");
140 if (!parsing_swallows && !parsing_focus) {
141 con_fix_percent(json_node);
143 if (parsing_swallows) {
144 parsing_swallows = false;
147 /* Clear the list of focus mappings */
148 struct focus_mapping *mapping;
149 TAILQ_FOREACH_REVERSE (mapping, &focus_mappings, focus_mappings_head, focus_mappings) {
150 LOG("focus (reverse) %d\n", mapping->old_id);
152 TAILQ_FOREACH (con, &(json_node->focus_head), focused) {
153 if (con->old_id != mapping->old_id)
155 LOG("got it! %p\n", con);
156 /* Move this entry to the top of the focus list. */
157 TAILQ_REMOVE(&(json_node->focus_head), con, focused);
158 TAILQ_INSERT_HEAD(&(json_node->focus_head), con, focused);
162 while (!TAILQ_EMPTY(&focus_mappings)) {
163 mapping = TAILQ_FIRST(&focus_mappings);
164 TAILQ_REMOVE(&focus_mappings, mapping, focus_mappings);
167 parsing_focus = false;
172 static int json_key(void *ctx, const unsigned char *val, size_t len) {
173 LOG("key: %.*s\n", (int)len, val);
175 last_key = scalloc((len + 1) * sizeof(char));
176 memcpy(last_key, val, len);
177 if (strcasecmp(last_key, "swallows") == 0)
178 parsing_swallows = true;
180 if (strcasecmp(last_key, "rect") == 0)
183 if (strcasecmp(last_key, "window_rect") == 0)
184 parsing_window_rect = true;
186 if (strcasecmp(last_key, "geometry") == 0)
187 parsing_geometry = true;
189 if (strcasecmp(last_key, "focus") == 0)
190 parsing_focus = true;
195 static int json_string(void *ctx, const unsigned char *val, size_t len) {
196 LOG("string: %.*s for key %s\n", (int)len, val, last_key);
197 if (parsing_swallows) {
199 sasprintf(&sval, "%.*s", len, val);
200 if (strcasecmp(last_key, "class") == 0) {
201 current_swallow->class = regex_new(sval);
202 } else if (strcasecmp(last_key, "instance") == 0) {
203 current_swallow->instance = regex_new(sval);
204 } else if (strcasecmp(last_key, "window_role") == 0) {
205 current_swallow->window_role = regex_new(sval);
206 } else if (strcasecmp(last_key, "title") == 0) {
207 current_swallow->title = regex_new(sval);
209 ELOG("swallow key %s unknown\n", last_key);
213 if (strcasecmp(last_key, "name") == 0) {
214 json_node->name = scalloc((len + 1) * sizeof(char));
215 memcpy(json_node->name, val, len);
216 } else if (strcasecmp(last_key, "sticky_group") == 0) {
217 json_node->sticky_group = scalloc((len + 1) * sizeof(char));
218 memcpy(json_node->sticky_group, val, len);
219 LOG("sticky_group of this container is %s\n", json_node->sticky_group);
220 } else if (strcasecmp(last_key, "orientation") == 0) {
221 /* Upgrade path from older versions of i3 (doing an inplace restart
222 * to a newer version):
223 * "orientation" is dumped before "layout". Therefore, we store
224 * whether the orientation was horizontal or vertical in the
225 * last_split_layout. When we then encounter layout == "default",
226 * we will use the last_split_layout as layout instead. */
228 sasprintf(&buf, "%.*s", (int)len, val);
229 if (strcasecmp(buf, "none") == 0 ||
230 strcasecmp(buf, "horizontal") == 0)
231 json_node->last_split_layout = L_SPLITH;
232 else if (strcasecmp(buf, "vertical") == 0)
233 json_node->last_split_layout = L_SPLITV;
235 LOG("Unhandled orientation: %s\n", buf);
237 } else if (strcasecmp(last_key, "border") == 0) {
239 sasprintf(&buf, "%.*s", (int)len, val);
240 if (strcasecmp(buf, "none") == 0)
241 json_node->border_style = BS_NONE;
242 else if (strcasecmp(buf, "1pixel") == 0) {
243 json_node->border_style = BS_PIXEL;
244 json_node->current_border_width = 1;
245 } else if (strcasecmp(buf, "pixel") == 0)
246 json_node->border_style = BS_PIXEL;
247 else if (strcasecmp(buf, "normal") == 0)
248 json_node->border_style = BS_NORMAL;
250 LOG("Unhandled \"border\": %s\n", buf);
252 } else if (strcasecmp(last_key, "type") == 0) {
254 sasprintf(&buf, "%.*s", (int)len, val);
255 if (strcasecmp(buf, "root") == 0)
256 json_node->type = CT_ROOT;
257 else if (strcasecmp(buf, "output") == 0)
258 json_node->type = CT_OUTPUT;
259 else if (strcasecmp(buf, "con") == 0)
260 json_node->type = CT_CON;
261 else if (strcasecmp(buf, "floating_con") == 0)
262 json_node->type = CT_FLOATING_CON;
263 else if (strcasecmp(buf, "workspace") == 0)
264 json_node->type = CT_WORKSPACE;
265 else if (strcasecmp(buf, "dockarea") == 0)
266 json_node->type = CT_DOCKAREA;
268 LOG("Unhandled \"type\": %s\n", buf);
270 } else if (strcasecmp(last_key, "layout") == 0) {
272 sasprintf(&buf, "%.*s", (int)len, val);
273 if (strcasecmp(buf, "default") == 0)
274 /* This set above when we read "orientation". */
275 json_node->layout = json_node->last_split_layout;
276 else if (strcasecmp(buf, "stacked") == 0)
277 json_node->layout = L_STACKED;
278 else if (strcasecmp(buf, "tabbed") == 0)
279 json_node->layout = L_TABBED;
280 else if (strcasecmp(buf, "dockarea") == 0)
281 json_node->layout = L_DOCKAREA;
282 else if (strcasecmp(buf, "output") == 0)
283 json_node->layout = L_OUTPUT;
284 else if (strcasecmp(buf, "splith") == 0)
285 json_node->layout = L_SPLITH;
286 else if (strcasecmp(buf, "splitv") == 0)
287 json_node->layout = L_SPLITV;
289 LOG("Unhandled \"layout\": %s\n", buf);
291 } else if (strcasecmp(last_key, "workspace_layout") == 0) {
293 sasprintf(&buf, "%.*s", (int)len, val);
294 if (strcasecmp(buf, "default") == 0)
295 json_node->workspace_layout = L_DEFAULT;
296 else if (strcasecmp(buf, "stacked") == 0)
297 json_node->workspace_layout = L_STACKED;
298 else if (strcasecmp(buf, "tabbed") == 0)
299 json_node->workspace_layout = L_TABBED;
301 LOG("Unhandled \"workspace_layout\": %s\n", buf);
303 } else if (strcasecmp(last_key, "last_split_layout") == 0) {
305 sasprintf(&buf, "%.*s", (int)len, val);
306 if (strcasecmp(buf, "splith") == 0)
307 json_node->last_split_layout = L_SPLITH;
308 else if (strcasecmp(buf, "splitv") == 0)
309 json_node->last_split_layout = L_SPLITV;
311 LOG("Unhandled \"last_splitlayout\": %s\n", buf);
313 } else if (strcasecmp(last_key, "mark") == 0) {
315 sasprintf(&buf, "%.*s", (int)len, val);
316 json_node->mark = buf;
317 } else if (strcasecmp(last_key, "floating") == 0) {
319 sasprintf(&buf, "%.*s", (int)len, val);
320 if (strcasecmp(buf, "auto_off") == 0)
321 json_node->floating = FLOATING_AUTO_OFF;
322 else if (strcasecmp(buf, "auto_on") == 0)
323 json_node->floating = FLOATING_AUTO_ON;
324 else if (strcasecmp(buf, "user_off") == 0)
325 json_node->floating = FLOATING_USER_OFF;
326 else if (strcasecmp(buf, "user_on") == 0)
327 json_node->floating = FLOATING_USER_ON;
329 } else if (strcasecmp(last_key, "scratchpad_state") == 0) {
331 sasprintf(&buf, "%.*s", (int)len, val);
332 if (strcasecmp(buf, "none") == 0)
333 json_node->scratchpad_state = SCRATCHPAD_NONE;
334 else if (strcasecmp(buf, "fresh") == 0)
335 json_node->scratchpad_state = SCRATCHPAD_FRESH;
336 else if (strcasecmp(buf, "changed") == 0)
337 json_node->scratchpad_state = SCRATCHPAD_CHANGED;
344 static int json_int(void *ctx, long long val) {
345 LOG("int %lld for key %s\n", val, last_key);
346 /* For backwards compatibility with i3 < 4.8 */
347 if (strcasecmp(last_key, "type") == 0)
348 json_node->type = val;
350 if (strcasecmp(last_key, "fullscreen_mode") == 0)
351 json_node->fullscreen_mode = val;
353 if (strcasecmp(last_key, "num") == 0)
354 json_node->num = val;
356 if (strcasecmp(last_key, "current_border_width") == 0)
357 json_node->current_border_width = val;
359 if (strcasecmp(last_key, "depth") == 0)
360 json_node->depth = val;
362 if (!parsing_swallows && strcasecmp(last_key, "id") == 0)
363 json_node->old_id = val;
366 struct focus_mapping *focus_mapping = scalloc(sizeof(struct focus_mapping));
367 focus_mapping->old_id = val;
368 TAILQ_INSERT_TAIL(&focus_mappings, focus_mapping, focus_mappings);
371 if (parsing_rect || parsing_window_rect || parsing_geometry) {
374 r = &(json_node->rect);
375 else if (parsing_window_rect)
376 r = &(json_node->window_rect);
378 r = &(json_node->geometry);
379 if (strcasecmp(last_key, "x") == 0)
381 else if (strcasecmp(last_key, "y") == 0)
383 else if (strcasecmp(last_key, "width") == 0)
385 else if (strcasecmp(last_key, "height") == 0)
388 ELOG("WARNING: unknown key %s in rect\n", last_key);
389 DLOG("rect now: (%d, %d, %d, %d)\n",
390 r->x, r->y, r->width, r->height);
392 if (parsing_swallows) {
393 if (strcasecmp(last_key, "id") == 0) {
394 current_swallow->id = val;
396 if (strcasecmp(last_key, "dock") == 0) {
397 current_swallow->dock = val;
399 if (strcasecmp(last_key, "insert_where") == 0) {
400 current_swallow->insert_where = val;
407 static int json_bool(void *ctx, int val) {
408 LOG("bool %d for key %s\n", val, last_key);
409 if (strcasecmp(last_key, "focused") == 0 && val) {
410 to_focus = json_node;
413 if (parsing_swallows) {
414 if (strcasecmp(last_key, "restart_mode") == 0)
415 current_swallow->restart_mode = val;
421 static int json_double(void *ctx, double val) {
422 LOG("double %f for key %s\n", val, last_key);
423 if (strcasecmp(last_key, "percent") == 0) {
424 json_node->percent = val;
429 static json_content_t content_result;
430 static int content_level;
432 static int json_determine_content_deeper(void *ctx) {
437 static int json_determine_content_shallower(void *ctx) {
442 static int json_determine_content_string(void *ctx, const unsigned char *val, size_t len) {
443 if (strcasecmp(last_key, "type") != 0 || content_level > 1)
446 DLOG("string = %.*s, last_key = %s\n", (int)len, val, last_key);
447 if (strncasecmp((const char *)val, "workspace", len) == 0)
448 content_result = JSON_CONTENT_WORKSPACE;
452 /* Parses the given JSON file until it encounters the first “type” property to
453 * determine whether the file contains workspaces or regular containers, which
454 * is important to know when deciding where (and how) to append the contents.
456 json_content_t json_determine_content(const char *filename) {
458 if ((f = fopen(filename, "r")) == NULL) {
459 ELOG("Cannot open file \"%s\"\n", filename);
460 return JSON_CONTENT_UNKNOWN;
463 if (fstat(fileno(f), &stbuf) != 0) {
464 ELOG("Cannot fstat() \"%s\"\n", filename);
466 return JSON_CONTENT_UNKNOWN;
468 char *buf = smalloc(stbuf.st_size);
469 int n = fread(buf, 1, stbuf.st_size, f);
470 if (n != stbuf.st_size) {
471 ELOG("File \"%s\" could not be read entirely, not loading.\n", filename);
473 return JSON_CONTENT_UNKNOWN;
475 DLOG("read %d bytes\n", n);
476 // We default to JSON_CONTENT_CON because it is legal to not include
477 // “"type": "con"” in the JSON files for better readability.
478 content_result = JSON_CONTENT_CON;
482 static yajl_callbacks callbacks = {
483 .yajl_string = json_determine_content_string,
484 .yajl_map_key = json_key,
485 .yajl_start_array = json_determine_content_deeper,
486 .yajl_start_map = json_determine_content_deeper,
487 .yajl_end_map = json_determine_content_shallower,
488 .yajl_end_array = json_determine_content_shallower,
490 g = yajl_gen_alloc(NULL);
491 hand = yajl_alloc(&callbacks, NULL, (void *)g);
492 /* Allowing comments allows for more user-friendly layout files. */
493 yajl_config(hand, yajl_allow_comments, true);
494 /* Allow multiple values, i.e. multiple nodes to attach */
495 yajl_config(hand, yajl_allow_multiple_values, true);
497 setlocale(LC_NUMERIC, "C");
498 stat = yajl_parse(hand, (const unsigned char *)buf, n);
499 if (stat != yajl_status_ok && stat != yajl_status_client_canceled) {
500 unsigned char *str = yajl_get_error(hand, 1, (const unsigned char *)buf, n);
501 ELOG("JSON parsing error: %s\n", str);
502 yajl_free_error(hand, str);
505 setlocale(LC_NUMERIC, "");
506 yajl_complete_parse(hand);
510 return content_result;
513 void tree_append_json(Con *con, const char *filename, char **errormsg) {
515 if ((f = fopen(filename, "r")) == NULL) {
516 ELOG("Cannot open file \"%s\"\n", filename);
520 if (fstat(fileno(f), &stbuf) != 0) {
521 ELOG("Cannot fstat() \"%s\"\n", filename);
525 char *buf = smalloc(stbuf.st_size);
526 int n = fread(buf, 1, stbuf.st_size, f);
527 if (n != stbuf.st_size) {
528 ELOG("File \"%s\" could not be read entirely, not loading.\n", filename);
532 DLOG("read %d bytes\n", n);
535 static yajl_callbacks callbacks = {
536 .yajl_boolean = json_bool,
537 .yajl_integer = json_int,
538 .yajl_double = json_double,
539 .yajl_string = json_string,
540 .yajl_start_map = json_start_map,
541 .yajl_map_key = json_key,
542 .yajl_end_map = json_end_map,
543 .yajl_end_array = json_end_array,
545 g = yajl_gen_alloc(NULL);
546 hand = yajl_alloc(&callbacks, NULL, (void *)g);
547 /* Allowing comments allows for more user-friendly layout files. */
548 yajl_config(hand, yajl_allow_comments, true);
549 /* Allow multiple values, i.e. multiple nodes to attach */
550 yajl_config(hand, yajl_allow_multiple_values, true);
554 parsing_swallows = false;
555 parsing_rect = false;
556 parsing_window_rect = false;
557 parsing_geometry = false;
558 parsing_focus = false;
559 setlocale(LC_NUMERIC, "C");
560 stat = yajl_parse(hand, (const unsigned char *)buf, n);
561 if (stat != yajl_status_ok) {
562 unsigned char *str = yajl_get_error(hand, 1, (const unsigned char *)buf, n);
563 ELOG("JSON parsing error: %s\n", str);
564 if (errormsg != NULL)
565 *errormsg = sstrdup((const char *)str);
566 yajl_free_error(hand, str);
569 /* In case not all containers were restored, we need to fix the
570 * percentages, otherwise i3 will crash immediately when rendering the
572 con_fix_percent(con);
574 setlocale(LC_NUMERIC, "");
575 yajl_complete_parse(hand);