]> git.sur5r.net Git - i3/i3/blob - src/load_layout.c
Allow multiple marks on windows.
[i3/i3] / src / load_layout.c
1 #undef I3__FILE__
2 #define I3__FILE__ "load_layout.c"
3 /*
4  * vim:ts=4:sw=4:expandtab
5  *
6  * i3 - an improved dynamic tiling window manager
7  * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
8  *
9  * load_layout.c: Restore (parts of) the layout, for example after an inplace
10  *                restart.
11  *
12  */
13 #include "all.h"
14
15 #include <yajl/yajl_common.h>
16 #include <yajl/yajl_gen.h>
17 #include <yajl/yajl_parse.h>
18 #include <yajl/yajl_version.h>
19
20 /* TODO: refactor the whole parsing thing */
21
22 static char *last_key;
23 static Con *json_node;
24 static Con *to_focus;
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;
33
34 /* This list is used for reordering the focus stack after parsing the 'focus'
35  * array. */
36 struct focus_mapping {
37     int old_id;
38     TAILQ_ENTRY(focus_mapping) focus_mappings;
39 };
40
41 static TAILQ_HEAD(focus_mappings_head, focus_mapping) focus_mappings =
42     TAILQ_HEAD_INITIALIZER(focus_mappings);
43
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);
51     } else {
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);
60             } else {
61                 Con *parent = json_node;
62                 json_node = con_new_skeleton(NULL, NULL);
63                 json_node->name = NULL;
64                 json_node->parent = parent;
65             }
66         }
67     }
68     return 1;
69 }
70
71 static int json_end_map(void *ctx) {
72     LOG("end of map\n");
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;
78         }
79
80         /* Sanity check: swallow criteria don’t make any sense on a split
81          * container. */
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);
87                 match_free(match);
88             }
89         }
90
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");
96             }
97
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”. */
101             Con *output;
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);
106             int cnt = 1;
107             while (workspace != NULL) {
108                 FREE(json_node->name);
109                 sasprintf(&(json_node->name), "%s_%d", base, cnt++);
110                 workspace = NULL;
111                 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
112                 GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, json_node->name));
113             }
114             free(base);
115
116             /* Set num accordingly so that i3bar will properly sort it. */
117             json_node->num = ws_name_to_number(json_node->name);
118         }
119
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);
129
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");
136                 Con *child;
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);
141                 }
142             }
143
144             floating_check_size(json_node);
145         }
146
147         LOG("attaching\n");
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;
152     }
153
154     parsing_rect = false;
155     parsing_deco_rect = false;
156     parsing_window_rect = false;
157     parsing_geometry = false;
158     return 1;
159 }
160
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);
165     }
166     if (parsing_swallows) {
167         parsing_swallows = false;
168     }
169     if (parsing_marks) {
170         parsing_marks = false;
171     }
172
173     if (parsing_focus) {
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);
178             Con *con;
179             TAILQ_FOREACH(con, &(json_node->focus_head), focused) {
180                 if (con->old_id != mapping->old_id)
181                     continue;
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);
186                 break;
187             }
188         }
189         while (!TAILQ_EMPTY(&focus_mappings)) {
190             mapping = TAILQ_FIRST(&focus_mappings);
191             TAILQ_REMOVE(&focus_mappings, mapping, focus_mappings);
192             free(mapping);
193         }
194         parsing_focus = false;
195     }
196     return 1;
197 }
198
199 static int json_key(void *ctx, const unsigned char *val, size_t len) {
200     LOG("key: %.*s\n", (int)len, val);
201     FREE(last_key);
202     last_key = scalloc(len + 1, 1);
203     memcpy(last_key, val, len);
204     if (strcasecmp(last_key, "swallows") == 0)
205         parsing_swallows = true;
206
207     if (strcasecmp(last_key, "rect") == 0)
208         parsing_rect = true;
209
210     if (strcasecmp(last_key, "deco_rect") == 0)
211         parsing_deco_rect = true;
212
213     if (strcasecmp(last_key, "window_rect") == 0)
214         parsing_window_rect = true;
215
216     if (strcasecmp(last_key, "geometry") == 0)
217         parsing_geometry = true;
218
219     if (strcasecmp(last_key, "focus") == 0)
220         parsing_focus = true;
221
222     if (strcasecmp(last_key, "marks") == 0)
223         parsing_marks = true;
224
225     return 1;
226 }
227
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) {
231         char *sval;
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);
241         } else {
242             ELOG("swallow key %s unknown\n", last_key);
243         }
244         free(sval);
245     } else if (parsing_marks) {
246         char *mark;
247         sasprintf(&mark, "%.*s", (int)len, val);
248
249         con_mark(json_node, mark);
250     } else {
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. */
265             char *buf = NULL;
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;
272             else
273                 LOG("Unhandled orientation: %s\n", buf);
274             free(buf);
275         } else if (strcasecmp(last_key, "border") == 0) {
276             char *buf = NULL;
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;
287             else
288                 LOG("Unhandled \"border\": %s\n", buf);
289             free(buf);
290         } else if (strcasecmp(last_key, "type") == 0) {
291             char *buf = NULL;
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;
305             else
306                 LOG("Unhandled \"type\": %s\n", buf);
307             free(buf);
308         } else if (strcasecmp(last_key, "layout") == 0) {
309             char *buf = NULL;
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;
326             else
327                 LOG("Unhandled \"layout\": %s\n", buf);
328             free(buf);
329         } else if (strcasecmp(last_key, "workspace_layout") == 0) {
330             char *buf = NULL;
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;
338             else
339                 LOG("Unhandled \"workspace_layout\": %s\n", buf);
340             free(buf);
341         } else if (strcasecmp(last_key, "last_split_layout") == 0) {
342             char *buf = NULL;
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;
348             else
349                 LOG("Unhandled \"last_splitlayout\": %s\n", buf);
350             free(buf);
351         } else if (strcasecmp(last_key, "mark") == 0) {
352             DLOG("Found deprecated key \"mark\".\n");
353
354             char *buf = NULL;
355             sasprintf(&buf, "%.*s", (int)len, val);
356
357             con_mark(json_node, buf);
358         } else if (strcasecmp(last_key, "floating") == 0) {
359             char *buf = NULL;
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;
369             free(buf);
370         } else if (strcasecmp(last_key, "scratchpad_state") == 0) {
371             char *buf = NULL;
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;
379             free(buf);
380         }
381     }
382     return 1;
383 }
384
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;
390
391     if (strcasecmp(last_key, "fullscreen_mode") == 0)
392         json_node->fullscreen_mode = val;
393
394     if (strcasecmp(last_key, "num") == 0)
395         json_node->num = val;
396
397     if (strcasecmp(last_key, "current_border_width") == 0)
398         json_node->current_border_width = val;
399
400     if (strcasecmp(last_key, "depth") == 0)
401         json_node->depth = val;
402
403     if (!parsing_swallows && strcasecmp(last_key, "id") == 0)
404         json_node->old_id = val;
405
406     if (parsing_focus) {
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);
410     }
411
412     if (parsing_rect || parsing_window_rect || parsing_geometry) {
413         Rect *r;
414         if (parsing_rect)
415             r = &(json_node->rect);
416         else if (parsing_window_rect)
417             r = &(json_node->window_rect);
418         else
419             r = &(json_node->geometry);
420         if (strcasecmp(last_key, "x") == 0)
421             r->x = val;
422         else if (strcasecmp(last_key, "y") == 0)
423             r->y = val;
424         else if (strcasecmp(last_key, "width") == 0)
425             r->width = val;
426         else if (strcasecmp(last_key, "height") == 0)
427             r->height = val;
428         else
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);
432     }
433     if (parsing_swallows) {
434         if (strcasecmp(last_key, "id") == 0) {
435             current_swallow->id = val;
436         }
437         if (strcasecmp(last_key, "dock") == 0) {
438             current_swallow->dock = val;
439         }
440         if (strcasecmp(last_key, "insert_where") == 0) {
441             current_swallow->insert_where = val;
442         }
443     }
444
445     return 1;
446 }
447
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;
452     }
453
454     if (strcasecmp(last_key, "sticky") == 0)
455         json_node->sticky = val;
456
457     if (parsing_swallows) {
458         if (strcasecmp(last_key, "restart_mode") == 0)
459             current_swallow->restart_mode = val;
460     }
461
462     return 1;
463 }
464
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;
469     }
470     return 1;
471 }
472
473 static json_content_t content_result;
474 static int content_level;
475
476 static int json_determine_content_deeper(void *ctx) {
477     content_level++;
478     return 1;
479 }
480
481 static int json_determine_content_shallower(void *ctx) {
482     content_level--;
483     return 1;
484 }
485
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)
488         return 1;
489
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;
493     return 0;
494 }
495
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.
499  * */
500 json_content_t json_determine_content(const char *filename) {
501     FILE *f;
502     if ((f = fopen(filename, "r")) == NULL) {
503         ELOG("Cannot open file \"%s\"\n", filename);
504         return JSON_CONTENT_UNKNOWN;
505     }
506     struct stat stbuf;
507     if (fstat(fileno(f), &stbuf) != 0) {
508         ELOG("Cannot fstat() \"%s\"\n", filename);
509         fclose(f);
510         return JSON_CONTENT_UNKNOWN;
511     }
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);
516         fclose(f);
517         return JSON_CONTENT_UNKNOWN;
518     }
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;
523     content_level = 0;
524     yajl_gen g;
525     yajl_handle hand;
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,
533     };
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);
540     yajl_status stat;
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);
547     }
548
549     setlocale(LC_NUMERIC, "");
550     yajl_complete_parse(hand);
551
552     fclose(f);
553
554     return content_result;
555 }
556
557 void tree_append_json(Con *con, const char *filename, char **errormsg) {
558     FILE *f;
559     if ((f = fopen(filename, "r")) == NULL) {
560         ELOG("Cannot open file \"%s\"\n", filename);
561         return;
562     }
563     struct stat stbuf;
564     if (fstat(fileno(f), &stbuf) != 0) {
565         ELOG("Cannot fstat() \"%s\"\n", filename);
566         fclose(f);
567         return;
568     }
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);
573         fclose(f);
574         return;
575     }
576     DLOG("read %d bytes\n", n);
577     yajl_gen g;
578     yajl_handle hand;
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,
588     };
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);
595     yajl_status stat;
596     json_node = con;
597     to_focus = NULL;
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);
613     }
614
615     /* In case not all containers were restored, we need to fix the
616      * percentages, otherwise i3 will crash immediately when rendering the
617      * next time. */
618     con_fix_percent(con);
619
620     setlocale(LC_NUMERIC, "");
621     yajl_complete_parse(hand);
622
623     fclose(f);
624     if (to_focus)
625         con_focus(to_focus);
626 }