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);
119 con_attach(json_node, json_node->parent, true);
120 LOG("Creating window\n");
121 x_con_init(json_node, json_node->depth);
122 json_node = json_node->parent;
125 parsing_rect = false;
126 if (parsing_window_rect)
127 parsing_window_rect = false;
128 if (parsing_geometry)
129 parsing_geometry = false;
133 static int json_end_array(void *ctx) {
134 LOG("end of array\n");
135 if (!parsing_swallows && !parsing_focus) {
136 con_fix_percent(json_node);
138 if (parsing_swallows) {
139 parsing_swallows = false;
142 /* Clear the list of focus mappings */
143 struct focus_mapping *mapping;
144 TAILQ_FOREACH_REVERSE(mapping, &focus_mappings, focus_mappings_head, focus_mappings) {
145 LOG("focus (reverse) %d\n", mapping->old_id);
147 TAILQ_FOREACH(con, &(json_node->focus_head), focused) {
148 if (con->old_id != mapping->old_id)
150 LOG("got it! %p\n", con);
151 /* Move this entry to the top of the focus list. */
152 TAILQ_REMOVE(&(json_node->focus_head), con, focused);
153 TAILQ_INSERT_HEAD(&(json_node->focus_head), con, focused);
157 while (!TAILQ_EMPTY(&focus_mappings)) {
158 mapping = TAILQ_FIRST(&focus_mappings);
159 TAILQ_REMOVE(&focus_mappings, mapping, focus_mappings);
162 parsing_focus = false;
167 static int json_key(void *ctx, const unsigned char *val, size_t len) {
168 LOG("key: %.*s\n", (int)len, val);
170 last_key = scalloc((len + 1) * sizeof(char));
171 memcpy(last_key, val, len);
172 if (strcasecmp(last_key, "swallows") == 0)
173 parsing_swallows = true;
175 if (strcasecmp(last_key, "rect") == 0)
178 if (strcasecmp(last_key, "window_rect") == 0)
179 parsing_window_rect = true;
181 if (strcasecmp(last_key, "geometry") == 0)
182 parsing_geometry = true;
184 if (strcasecmp(last_key, "focus") == 0)
185 parsing_focus = true;
190 static int json_string(void *ctx, const unsigned char *val, size_t len) {
191 LOG("string: %.*s for key %s\n", (int)len, val, last_key);
192 if (parsing_swallows) {
194 sasprintf(&sval, "%.*s", len, val);
195 if (strcasecmp(last_key, "class") == 0) {
196 current_swallow->class = regex_new(sval);
197 } else if (strcasecmp(last_key, "instance") == 0) {
198 current_swallow->instance = regex_new(sval);
199 } else if (strcasecmp(last_key, "window_role") == 0) {
200 current_swallow->window_role = regex_new(sval);
201 } else if (strcasecmp(last_key, "title") == 0) {
202 current_swallow->title = regex_new(sval);
204 ELOG("swallow key %s unknown\n", last_key);
208 if (strcasecmp(last_key, "name") == 0) {
209 json_node->name = scalloc((len + 1) * sizeof(char));
210 memcpy(json_node->name, val, len);
211 } else if (strcasecmp(last_key, "sticky_group") == 0) {
212 json_node->sticky_group = scalloc((len + 1) * sizeof(char));
213 memcpy(json_node->sticky_group, val, len);
214 LOG("sticky_group of this container is %s\n", json_node->sticky_group);
215 } else if (strcasecmp(last_key, "orientation") == 0) {
216 /* Upgrade path from older versions of i3 (doing an inplace restart
217 * to a newer version):
218 * "orientation" is dumped before "layout". Therefore, we store
219 * whether the orientation was horizontal or vertical in the
220 * last_split_layout. When we then encounter layout == "default",
221 * we will use the last_split_layout as layout instead. */
223 sasprintf(&buf, "%.*s", (int)len, val);
224 if (strcasecmp(buf, "none") == 0 ||
225 strcasecmp(buf, "horizontal") == 0)
226 json_node->last_split_layout = L_SPLITH;
227 else if (strcasecmp(buf, "vertical") == 0)
228 json_node->last_split_layout = L_SPLITV;
230 LOG("Unhandled orientation: %s\n", buf);
232 } else if (strcasecmp(last_key, "border") == 0) {
234 sasprintf(&buf, "%.*s", (int)len, val);
235 if (strcasecmp(buf, "none") == 0)
236 json_node->border_style = BS_NONE;
237 else if (strcasecmp(buf, "1pixel") == 0) {
238 json_node->border_style = BS_PIXEL;
239 json_node->current_border_width = 1;
240 } else if (strcasecmp(buf, "pixel") == 0)
241 json_node->border_style = BS_PIXEL;
242 else if (strcasecmp(buf, "normal") == 0)
243 json_node->border_style = BS_NORMAL;
245 LOG("Unhandled \"border\": %s\n", buf);
247 } else if (strcasecmp(last_key, "type") == 0) {
249 sasprintf(&buf, "%.*s", (int)len, val);
250 if (strcasecmp(buf, "root") == 0)
251 json_node->type = CT_ROOT;
252 else if (strcasecmp(buf, "output") == 0)
253 json_node->type = CT_OUTPUT;
254 else if (strcasecmp(buf, "con") == 0)
255 json_node->type = CT_CON;
256 else if (strcasecmp(buf, "floating_con") == 0)
257 json_node->type = CT_FLOATING_CON;
258 else if (strcasecmp(buf, "workspace") == 0)
259 json_node->type = CT_WORKSPACE;
260 else if (strcasecmp(buf, "dockarea") == 0)
261 json_node->type = CT_DOCKAREA;
263 LOG("Unhandled \"type\": %s\n", buf);
265 } else if (strcasecmp(last_key, "layout") == 0) {
267 sasprintf(&buf, "%.*s", (int)len, val);
268 if (strcasecmp(buf, "default") == 0)
269 /* This set above when we read "orientation". */
270 json_node->layout = json_node->last_split_layout;
271 else if (strcasecmp(buf, "stacked") == 0)
272 json_node->layout = L_STACKED;
273 else if (strcasecmp(buf, "tabbed") == 0)
274 json_node->layout = L_TABBED;
275 else if (strcasecmp(buf, "dockarea") == 0)
276 json_node->layout = L_DOCKAREA;
277 else if (strcasecmp(buf, "output") == 0)
278 json_node->layout = L_OUTPUT;
279 else if (strcasecmp(buf, "splith") == 0)
280 json_node->layout = L_SPLITH;
281 else if (strcasecmp(buf, "splitv") == 0)
282 json_node->layout = L_SPLITV;
284 LOG("Unhandled \"layout\": %s\n", buf);
286 } else if (strcasecmp(last_key, "workspace_layout") == 0) {
288 sasprintf(&buf, "%.*s", (int)len, val);
289 if (strcasecmp(buf, "default") == 0)
290 json_node->workspace_layout = L_DEFAULT;
291 else if (strcasecmp(buf, "stacked") == 0)
292 json_node->workspace_layout = L_STACKED;
293 else if (strcasecmp(buf, "tabbed") == 0)
294 json_node->workspace_layout = L_TABBED;
296 LOG("Unhandled \"workspace_layout\": %s\n", buf);
298 } else if (strcasecmp(last_key, "last_split_layout") == 0) {
300 sasprintf(&buf, "%.*s", (int)len, val);
301 if (strcasecmp(buf, "splith") == 0)
302 json_node->last_split_layout = L_SPLITH;
303 else if (strcasecmp(buf, "splitv") == 0)
304 json_node->last_split_layout = L_SPLITV;
306 LOG("Unhandled \"last_splitlayout\": %s\n", buf);
308 } else if (strcasecmp(last_key, "mark") == 0) {
310 sasprintf(&buf, "%.*s", (int)len, val);
311 json_node->mark = buf;
312 } else if (strcasecmp(last_key, "floating") == 0) {
314 sasprintf(&buf, "%.*s", (int)len, val);
315 if (strcasecmp(buf, "auto_off") == 0)
316 json_node->floating = FLOATING_AUTO_OFF;
317 else if (strcasecmp(buf, "auto_on") == 0)
318 json_node->floating = FLOATING_AUTO_ON;
319 else if (strcasecmp(buf, "user_off") == 0)
320 json_node->floating = FLOATING_USER_OFF;
321 else if (strcasecmp(buf, "user_on") == 0)
322 json_node->floating = FLOATING_USER_ON;
324 } else if (strcasecmp(last_key, "scratchpad_state") == 0) {
326 sasprintf(&buf, "%.*s", (int)len, val);
327 if (strcasecmp(buf, "none") == 0)
328 json_node->scratchpad_state = SCRATCHPAD_NONE;
329 else if (strcasecmp(buf, "fresh") == 0)
330 json_node->scratchpad_state = SCRATCHPAD_FRESH;
331 else if (strcasecmp(buf, "changed") == 0)
332 json_node->scratchpad_state = SCRATCHPAD_CHANGED;
339 static int json_int(void *ctx, long long val) {
340 LOG("int %lld for key %s\n", val, last_key);
341 /* For backwards compatibility with i3 < 4.8 */
342 if (strcasecmp(last_key, "type") == 0)
343 json_node->type = val;
345 if (strcasecmp(last_key, "fullscreen_mode") == 0)
346 json_node->fullscreen_mode = val;
348 if (strcasecmp(last_key, "num") == 0)
349 json_node->num = val;
351 if (strcasecmp(last_key, "current_border_width") == 0)
352 json_node->current_border_width = val;
354 if (strcasecmp(last_key, "depth") == 0)
355 json_node->depth = val;
357 if (!parsing_swallows && strcasecmp(last_key, "id") == 0)
358 json_node->old_id = val;
361 struct focus_mapping *focus_mapping = scalloc(sizeof(struct focus_mapping));
362 focus_mapping->old_id = val;
363 TAILQ_INSERT_TAIL(&focus_mappings, focus_mapping, focus_mappings);
366 if (parsing_rect || parsing_window_rect || parsing_geometry) {
369 r = &(json_node->rect);
370 else if (parsing_window_rect)
371 r = &(json_node->window_rect);
373 r = &(json_node->geometry);
374 if (strcasecmp(last_key, "x") == 0)
376 else if (strcasecmp(last_key, "y") == 0)
378 else if (strcasecmp(last_key, "width") == 0)
380 else if (strcasecmp(last_key, "height") == 0)
383 ELOG("WARNING: unknown key %s in rect\n", last_key);
384 DLOG("rect now: (%d, %d, %d, %d)\n",
385 r->x, r->y, r->width, r->height);
387 if (parsing_swallows) {
388 if (strcasecmp(last_key, "id") == 0) {
389 current_swallow->id = val;
391 if (strcasecmp(last_key, "dock") == 0) {
392 current_swallow->dock = val;
394 if (strcasecmp(last_key, "insert_where") == 0) {
395 current_swallow->insert_where = val;
402 static int json_bool(void *ctx, int val) {
403 LOG("bool %d for key %s\n", val, last_key);
404 if (strcasecmp(last_key, "focused") == 0 && val) {
405 to_focus = json_node;
408 if (parsing_swallows) {
409 if (strcasecmp(last_key, "restart_mode") == 0)
410 current_swallow->restart_mode = val;
416 static int json_double(void *ctx, double val) {
417 LOG("double %f for key %s\n", val, last_key);
418 if (strcasecmp(last_key, "percent") == 0) {
419 json_node->percent = val;
424 static json_content_t content_result;
426 static int json_determine_content_string(void *ctx, const unsigned char *val, size_t len) {
427 if (strcasecmp(last_key, "type") != 0)
430 DLOG("string = %.*s, last_key = %s\n", (int)len, val, last_key);
431 if (strncasecmp((const char *)val, "workspace", len) == 0)
432 content_result = JSON_CONTENT_WORKSPACE;
436 /* Parses the given JSON file until it encounters the first “type” property to
437 * determine whether the file contains workspaces or regular containers, which
438 * is important to know when deciding where (and how) to append the contents.
440 json_content_t json_determine_content(const char *filename) {
442 if ((f = fopen(filename, "r")) == NULL) {
443 ELOG("Cannot open file \"%s\"\n", filename);
444 return JSON_CONTENT_UNKNOWN;
447 if (fstat(fileno(f), &stbuf) != 0) {
448 ELOG("Cannot fstat() \"%s\"\n", filename);
450 return JSON_CONTENT_UNKNOWN;
452 char *buf = smalloc(stbuf.st_size);
453 int n = fread(buf, 1, stbuf.st_size, f);
454 if (n != stbuf.st_size) {
455 ELOG("File \"%s\" could not be read entirely, not loading.\n", filename);
457 return JSON_CONTENT_UNKNOWN;
459 DLOG("read %d bytes\n", n);
460 // We default to JSON_CONTENT_CON because it is legal to not include
461 // “"type": "con"” in the JSON files for better readability.
462 content_result = JSON_CONTENT_CON;
465 static yajl_callbacks callbacks = {
466 .yajl_string = json_determine_content_string,
467 .yajl_map_key = json_key,
469 g = yajl_gen_alloc(NULL);
470 hand = yajl_alloc(&callbacks, NULL, (void *)g);
471 /* Allowing comments allows for more user-friendly layout files. */
472 yajl_config(hand, yajl_allow_comments, true);
473 /* Allow multiple values, i.e. multiple nodes to attach */
474 yajl_config(hand, yajl_allow_multiple_values, true);
476 setlocale(LC_NUMERIC, "C");
477 stat = yajl_parse(hand, (const unsigned char *)buf, n);
478 if (stat != yajl_status_ok && stat != yajl_status_client_canceled) {
479 unsigned char *str = yajl_get_error(hand, 1, (const unsigned char *)buf, n);
480 ELOG("JSON parsing error: %s\n", str);
481 yajl_free_error(hand, str);
484 setlocale(LC_NUMERIC, "");
485 yajl_complete_parse(hand);
489 return content_result;
492 void tree_append_json(Con *con, const char *filename, char **errormsg) {
494 if ((f = fopen(filename, "r")) == NULL) {
495 ELOG("Cannot open file \"%s\"\n", filename);
499 if (fstat(fileno(f), &stbuf) != 0) {
500 ELOG("Cannot fstat() \"%s\"\n", filename);
504 char *buf = smalloc(stbuf.st_size);
505 int n = fread(buf, 1, stbuf.st_size, f);
506 if (n != stbuf.st_size) {
507 ELOG("File \"%s\" could not be read entirely, not loading.\n", filename);
511 DLOG("read %d bytes\n", n);
514 static yajl_callbacks callbacks = {
515 .yajl_boolean = json_bool,
516 .yajl_integer = json_int,
517 .yajl_double = json_double,
518 .yajl_string = json_string,
519 .yajl_start_map = json_start_map,
520 .yajl_map_key = json_key,
521 .yajl_end_map = json_end_map,
522 .yajl_end_array = json_end_array,
524 g = yajl_gen_alloc(NULL);
525 hand = yajl_alloc(&callbacks, NULL, (void *)g);
526 /* Allowing comments allows for more user-friendly layout files. */
527 yajl_config(hand, yajl_allow_comments, true);
528 /* Allow multiple values, i.e. multiple nodes to attach */
529 yajl_config(hand, yajl_allow_multiple_values, true);
533 parsing_swallows = false;
534 parsing_rect = false;
535 parsing_window_rect = false;
536 parsing_geometry = false;
537 parsing_focus = false;
538 setlocale(LC_NUMERIC, "C");
539 stat = yajl_parse(hand, (const unsigned char *)buf, n);
540 if (stat != yajl_status_ok) {
541 unsigned char *str = yajl_get_error(hand, 1, (const unsigned char *)buf, n);
542 ELOG("JSON parsing error: %s\n", str);
543 if (errormsg != NULL)
544 *errormsg = sstrdup((const char *)str);
545 yajl_free_error(hand, str);
548 /* In case not all containers were restored, we need to fix the
549 * percentages, otherwise i3 will crash immediately when rendering the
551 con_fix_percent(con);
553 setlocale(LC_NUMERIC, "");
554 yajl_complete_parse(hand);