]> git.sur5r.net Git - i3/i3/blob - src/load_layout.c
switch to clang-format-3.8 (#2547)
[i3/i3] / src / load_layout.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * load_layout.c: Restore (parts of) the layout, for example after an inplace
8  *                restart.
9  *
10  */
11 #include "all.h"
12
13 #include <yajl/yajl_common.h>
14 #include <yajl/yajl_gen.h>
15 #include <yajl/yajl_parse.h>
16 #include <yajl/yajl_version.h>
17
18 /* TODO: refactor the whole parsing thing */
19
20 static char *last_key;
21 static Con *json_node;
22 static Con *to_focus;
23 static bool parsing_swallows;
24 static bool parsing_rect;
25 static bool parsing_deco_rect;
26 static bool parsing_window_rect;
27 static bool parsing_geometry;
28 static bool parsing_focus;
29 static bool parsing_marks;
30 struct Match *current_swallow;
31 static bool swallow_is_empty;
32
33 /* This list is used for reordering the focus stack after parsing the 'focus'
34  * array. */
35 struct focus_mapping {
36     int old_id;
37
38     TAILQ_ENTRY(focus_mapping)
39     focus_mappings;
40 };
41
42 static TAILQ_HEAD(focus_mappings_head, focus_mapping) focus_mappings =
43     TAILQ_HEAD_INITIALIZER(focus_mappings);
44
45 static int json_start_map(void *ctx) {
46     LOG("start of map, last_key = %s\n", last_key);
47     if (parsing_swallows) {
48         LOG("creating new swallow\n");
49         current_swallow = smalloc(sizeof(Match));
50         match_init(current_swallow);
51         current_swallow->dock = M_DONTCHECK;
52         TAILQ_INSERT_TAIL(&(json_node->swallow_head), current_swallow, matches);
53         swallow_is_empty = true;
54     } else {
55         if (!parsing_rect && !parsing_deco_rect && !parsing_window_rect && !parsing_geometry) {
56             if (last_key && strcasecmp(last_key, "floating_nodes") == 0) {
57                 DLOG("New floating_node\n");
58                 Con *ws = con_get_workspace(json_node);
59                 json_node = con_new_skeleton(NULL, NULL);
60                 json_node->name = NULL;
61                 json_node->parent = ws;
62                 DLOG("Parent is workspace = %p\n", ws);
63             } else {
64                 Con *parent = json_node;
65                 json_node = con_new_skeleton(NULL, NULL);
66                 json_node->name = NULL;
67                 json_node->parent = parent;
68             }
69         }
70     }
71     return 1;
72 }
73
74 static int json_end_map(void *ctx) {
75     LOG("end of map\n");
76     if (!parsing_swallows && !parsing_rect && !parsing_deco_rect && !parsing_window_rect && !parsing_geometry) {
77         /* Set a few default values to simplify manually crafted layout files. */
78         if (json_node->layout == L_DEFAULT) {
79             DLOG("Setting layout = L_SPLITH\n");
80             json_node->layout = L_SPLITH;
81         }
82
83         /* Sanity check: swallow criteria don’t make any sense on a split
84          * container. */
85         if (con_is_split(json_node) > 0 && !TAILQ_EMPTY(&(json_node->swallow_head))) {
86             DLOG("sanity check: removing swallows specification from split container\n");
87             while (!TAILQ_EMPTY(&(json_node->swallow_head))) {
88                 Match *match = TAILQ_FIRST(&(json_node->swallow_head));
89                 TAILQ_REMOVE(&(json_node->swallow_head), match, matches);
90                 match_free(match);
91                 free(match);
92             }
93         }
94
95         if (json_node->type == CT_WORKSPACE) {
96             /* Ensure the workspace has a name. */
97             DLOG("Attaching workspace. name = %s\n", json_node->name);
98             if (json_node->name == NULL || strcmp(json_node->name, "") == 0) {
99                 json_node->name = sstrdup("unnamed");
100             }
101
102             /* Prevent name clashes when appending a workspace, e.g. when the
103              * user tries to restore a workspace called “1” but already has a
104              * workspace called “1”. */
105             Con *output;
106             Con *workspace = NULL;
107             TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
108             GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, json_node->name));
109             char *base = sstrdup(json_node->name);
110             int cnt = 1;
111             while (workspace != NULL) {
112                 FREE(json_node->name);
113                 sasprintf(&(json_node->name), "%s_%d", base, cnt++);
114                 workspace = NULL;
115                 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
116                 GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, json_node->name));
117             }
118             free(base);
119
120             /* Set num accordingly so that i3bar will properly sort it. */
121             json_node->num = ws_name_to_number(json_node->name);
122         }
123
124         // When appending JSON layout files that only contain the workspace
125         // _contents_, we might not have an upfront signal that the
126         // container we’re currently parsing is a floating container (like
127         // the “floating_nodes” key of the workspace container itself).
128         // That’s why we make sure the con is attached at the right place
129         // in the hierarchy in case it’s floating.
130         if (json_node->type == CT_FLOATING_CON) {
131             DLOG("fixing parent which currently is %p / %s\n", json_node->parent, json_node->parent->name);
132             json_node->parent = con_get_workspace(json_node->parent);
133
134             // Also set a size if none was supplied, otherwise the placeholder
135             // window cannot be created as X11 requests with width=0 or
136             // height=0 are invalid.
137             const Rect zero = {0, 0, 0, 0};
138             if (memcmp(&(json_node->rect), &zero, sizeof(Rect)) == 0) {
139                 DLOG("Geometry not set, combining children\n");
140                 Con *child;
141                 TAILQ_FOREACH(child, &(json_node->nodes_head), nodes) {
142                     DLOG("child geometry: %d x %d\n", child->geometry.width, child->geometry.height);
143                     json_node->rect.width += child->geometry.width;
144                     json_node->rect.height = max(json_node->rect.height, child->geometry.height);
145                 }
146             }
147
148             floating_check_size(json_node);
149         }
150
151         LOG("attaching\n");
152         con_attach(json_node, json_node->parent, true);
153         LOG("Creating window\n");
154         x_con_init(json_node);
155         json_node = json_node->parent;
156     }
157
158     if (parsing_swallows && swallow_is_empty) {
159         /* We parsed an empty swallow definition. This is an invalid layout
160          * definition, hence we reject it. */
161         ELOG("Layout file is invalid: found an empty swallow definition.\n");
162         return 0;
163     }
164
165     parsing_rect = false;
166     parsing_deco_rect = false;
167     parsing_window_rect = false;
168     parsing_geometry = false;
169     return 1;
170 }
171
172 static int json_end_array(void *ctx) {
173     LOG("end of array\n");
174     if (!parsing_swallows && !parsing_focus && !parsing_marks) {
175         con_fix_percent(json_node);
176     }
177     if (parsing_swallows) {
178         parsing_swallows = false;
179     }
180     if (parsing_marks) {
181         parsing_marks = false;
182     }
183
184     if (parsing_focus) {
185         /* Clear the list of focus mappings */
186         struct focus_mapping *mapping;
187         TAILQ_FOREACH_REVERSE(mapping, &focus_mappings, focus_mappings_head, focus_mappings) {
188             LOG("focus (reverse) %d\n", mapping->old_id);
189             Con *con;
190             TAILQ_FOREACH(con, &(json_node->focus_head), focused) {
191                 if (con->old_id != mapping->old_id)
192                     continue;
193                 LOG("got it! %p\n", con);
194                 /* Move this entry to the top of the focus list. */
195                 TAILQ_REMOVE(&(json_node->focus_head), con, focused);
196                 TAILQ_INSERT_HEAD(&(json_node->focus_head), con, focused);
197                 break;
198             }
199         }
200         while (!TAILQ_EMPTY(&focus_mappings)) {
201             mapping = TAILQ_FIRST(&focus_mappings);
202             TAILQ_REMOVE(&focus_mappings, mapping, focus_mappings);
203             free(mapping);
204         }
205         parsing_focus = false;
206     }
207     return 1;
208 }
209
210 static int json_key(void *ctx, const unsigned char *val, size_t len) {
211     LOG("key: %.*s\n", (int)len, val);
212     FREE(last_key);
213     last_key = scalloc(len + 1, 1);
214     memcpy(last_key, val, len);
215     if (strcasecmp(last_key, "swallows") == 0)
216         parsing_swallows = true;
217
218     if (strcasecmp(last_key, "rect") == 0)
219         parsing_rect = true;
220
221     if (strcasecmp(last_key, "deco_rect") == 0)
222         parsing_deco_rect = true;
223
224     if (strcasecmp(last_key, "window_rect") == 0)
225         parsing_window_rect = true;
226
227     if (strcasecmp(last_key, "geometry") == 0)
228         parsing_geometry = true;
229
230     if (strcasecmp(last_key, "focus") == 0)
231         parsing_focus = true;
232
233     if (strcasecmp(last_key, "marks") == 0)
234         parsing_marks = true;
235
236     return 1;
237 }
238
239 static int json_string(void *ctx, const unsigned char *val, size_t len) {
240     LOG("string: %.*s for key %s\n", (int)len, val, last_key);
241     if (parsing_swallows) {
242         char *sval;
243         sasprintf(&sval, "%.*s", len, val);
244         if (strcasecmp(last_key, "class") == 0) {
245             current_swallow->class = regex_new(sval);
246             swallow_is_empty = false;
247         } else if (strcasecmp(last_key, "instance") == 0) {
248             current_swallow->instance = regex_new(sval);
249             swallow_is_empty = false;
250         } else if (strcasecmp(last_key, "window_role") == 0) {
251             current_swallow->window_role = regex_new(sval);
252             swallow_is_empty = false;
253         } else if (strcasecmp(last_key, "title") == 0) {
254             current_swallow->title = regex_new(sval);
255             swallow_is_empty = false;
256         } else {
257             ELOG("swallow key %s unknown\n", last_key);
258         }
259         free(sval);
260     } else if (parsing_marks) {
261         char *mark;
262         sasprintf(&mark, "%.*s", (int)len, val);
263
264         con_mark(json_node, mark, MM_ADD);
265     } else {
266         if (strcasecmp(last_key, "name") == 0) {
267             json_node->name = scalloc(len + 1, 1);
268             memcpy(json_node->name, val, len);
269         } else if (strcasecmp(last_key, "title_format") == 0) {
270             json_node->title_format = scalloc(len + 1, 1);
271             memcpy(json_node->title_format, val, len);
272         } else if (strcasecmp(last_key, "sticky_group") == 0) {
273             json_node->sticky_group = scalloc(len + 1, 1);
274             memcpy(json_node->sticky_group, val, len);
275             LOG("sticky_group of this container is %s\n", json_node->sticky_group);
276         } else if (strcasecmp(last_key, "orientation") == 0) {
277             /* Upgrade path from older versions of i3 (doing an inplace restart
278              * to a newer version):
279              * "orientation" is dumped before "layout". Therefore, we store
280              * whether the orientation was horizontal or vertical in the
281              * last_split_layout. When we then encounter layout == "default",
282              * we will use the last_split_layout as layout instead. */
283             char *buf = NULL;
284             sasprintf(&buf, "%.*s", (int)len, val);
285             if (strcasecmp(buf, "none") == 0 ||
286                 strcasecmp(buf, "horizontal") == 0)
287                 json_node->last_split_layout = L_SPLITH;
288             else if (strcasecmp(buf, "vertical") == 0)
289                 json_node->last_split_layout = L_SPLITV;
290             else
291                 LOG("Unhandled orientation: %s\n", buf);
292             free(buf);
293         } else if (strcasecmp(last_key, "border") == 0) {
294             char *buf = NULL;
295             sasprintf(&buf, "%.*s", (int)len, val);
296             if (strcasecmp(buf, "none") == 0)
297                 json_node->border_style = BS_NONE;
298             else if (strcasecmp(buf, "1pixel") == 0) {
299                 json_node->border_style = BS_PIXEL;
300                 json_node->current_border_width = 1;
301             } else if (strcasecmp(buf, "pixel") == 0)
302                 json_node->border_style = BS_PIXEL;
303             else if (strcasecmp(buf, "normal") == 0)
304                 json_node->border_style = BS_NORMAL;
305             else
306                 LOG("Unhandled \"border\": %s\n", buf);
307             free(buf);
308         } else if (strcasecmp(last_key, "type") == 0) {
309             char *buf = NULL;
310             sasprintf(&buf, "%.*s", (int)len, val);
311             if (strcasecmp(buf, "root") == 0)
312                 json_node->type = CT_ROOT;
313             else if (strcasecmp(buf, "output") == 0)
314                 json_node->type = CT_OUTPUT;
315             else if (strcasecmp(buf, "con") == 0)
316                 json_node->type = CT_CON;
317             else if (strcasecmp(buf, "floating_con") == 0)
318                 json_node->type = CT_FLOATING_CON;
319             else if (strcasecmp(buf, "workspace") == 0)
320                 json_node->type = CT_WORKSPACE;
321             else if (strcasecmp(buf, "dockarea") == 0)
322                 json_node->type = CT_DOCKAREA;
323             else
324                 LOG("Unhandled \"type\": %s\n", buf);
325             free(buf);
326         } else if (strcasecmp(last_key, "layout") == 0) {
327             char *buf = NULL;
328             sasprintf(&buf, "%.*s", (int)len, val);
329             if (strcasecmp(buf, "default") == 0)
330                 /* This set above when we read "orientation". */
331                 json_node->layout = json_node->last_split_layout;
332             else if (strcasecmp(buf, "stacked") == 0)
333                 json_node->layout = L_STACKED;
334             else if (strcasecmp(buf, "tabbed") == 0)
335                 json_node->layout = L_TABBED;
336             else if (strcasecmp(buf, "dockarea") == 0)
337                 json_node->layout = L_DOCKAREA;
338             else if (strcasecmp(buf, "output") == 0)
339                 json_node->layout = L_OUTPUT;
340             else if (strcasecmp(buf, "splith") == 0)
341                 json_node->layout = L_SPLITH;
342             else if (strcasecmp(buf, "splitv") == 0)
343                 json_node->layout = L_SPLITV;
344             else
345                 LOG("Unhandled \"layout\": %s\n", buf);
346             free(buf);
347         } else if (strcasecmp(last_key, "workspace_layout") == 0) {
348             char *buf = NULL;
349             sasprintf(&buf, "%.*s", (int)len, val);
350             if (strcasecmp(buf, "default") == 0)
351                 json_node->workspace_layout = L_DEFAULT;
352             else if (strcasecmp(buf, "stacked") == 0)
353                 json_node->workspace_layout = L_STACKED;
354             else if (strcasecmp(buf, "tabbed") == 0)
355                 json_node->workspace_layout = L_TABBED;
356             else
357                 LOG("Unhandled \"workspace_layout\": %s\n", buf);
358             free(buf);
359         } else if (strcasecmp(last_key, "last_split_layout") == 0) {
360             char *buf = NULL;
361             sasprintf(&buf, "%.*s", (int)len, val);
362             if (strcasecmp(buf, "splith") == 0)
363                 json_node->last_split_layout = L_SPLITH;
364             else if (strcasecmp(buf, "splitv") == 0)
365                 json_node->last_split_layout = L_SPLITV;
366             else
367                 LOG("Unhandled \"last_splitlayout\": %s\n", buf);
368             free(buf);
369         } else if (strcasecmp(last_key, "mark") == 0) {
370             DLOG("Found deprecated key \"mark\".\n");
371
372             char *buf = NULL;
373             sasprintf(&buf, "%.*s", (int)len, val);
374
375             con_mark(json_node, buf, MM_REPLACE);
376         } else if (strcasecmp(last_key, "floating") == 0) {
377             char *buf = NULL;
378             sasprintf(&buf, "%.*s", (int)len, val);
379             if (strcasecmp(buf, "auto_off") == 0)
380                 json_node->floating = FLOATING_AUTO_OFF;
381             else if (strcasecmp(buf, "auto_on") == 0)
382                 json_node->floating = FLOATING_AUTO_ON;
383             else if (strcasecmp(buf, "user_off") == 0)
384                 json_node->floating = FLOATING_USER_OFF;
385             else if (strcasecmp(buf, "user_on") == 0)
386                 json_node->floating = FLOATING_USER_ON;
387             free(buf);
388         } else if (strcasecmp(last_key, "scratchpad_state") == 0) {
389             char *buf = NULL;
390             sasprintf(&buf, "%.*s", (int)len, val);
391             if (strcasecmp(buf, "none") == 0)
392                 json_node->scratchpad_state = SCRATCHPAD_NONE;
393             else if (strcasecmp(buf, "fresh") == 0)
394                 json_node->scratchpad_state = SCRATCHPAD_FRESH;
395             else if (strcasecmp(buf, "changed") == 0)
396                 json_node->scratchpad_state = SCRATCHPAD_CHANGED;
397             free(buf);
398         }
399     }
400     return 1;
401 }
402
403 static int json_int(void *ctx, long long val) {
404     LOG("int %lld for key %s\n", val, last_key);
405     /* For backwards compatibility with i3 < 4.8 */
406     if (strcasecmp(last_key, "type") == 0)
407         json_node->type = val;
408
409     if (strcasecmp(last_key, "fullscreen_mode") == 0)
410         json_node->fullscreen_mode = val;
411
412     if (strcasecmp(last_key, "num") == 0)
413         json_node->num = val;
414
415     if (strcasecmp(last_key, "current_border_width") == 0)
416         json_node->current_border_width = val;
417
418     if (strcasecmp(last_key, "depth") == 0)
419         json_node->depth = val;
420
421     if (!parsing_swallows && strcasecmp(last_key, "id") == 0)
422         json_node->old_id = val;
423
424     if (parsing_focus) {
425         struct focus_mapping *focus_mapping = scalloc(1, sizeof(struct focus_mapping));
426         focus_mapping->old_id = val;
427         TAILQ_INSERT_TAIL(&focus_mappings, focus_mapping, focus_mappings);
428     }
429
430     if (parsing_rect || parsing_window_rect || parsing_geometry) {
431         Rect *r;
432         if (parsing_rect)
433             r = &(json_node->rect);
434         else if (parsing_window_rect)
435             r = &(json_node->window_rect);
436         else
437             r = &(json_node->geometry);
438         if (strcasecmp(last_key, "x") == 0)
439             r->x = val;
440         else if (strcasecmp(last_key, "y") == 0)
441             r->y = val;
442         else if (strcasecmp(last_key, "width") == 0)
443             r->width = val;
444         else if (strcasecmp(last_key, "height") == 0)
445             r->height = val;
446         else
447             ELOG("WARNING: unknown key %s in rect\n", last_key);
448         DLOG("rect now: (%d, %d, %d, %d)\n",
449              r->x, r->y, r->width, r->height);
450     }
451     if (parsing_swallows) {
452         if (strcasecmp(last_key, "id") == 0) {
453             current_swallow->id = val;
454             swallow_is_empty = false;
455         }
456         if (strcasecmp(last_key, "dock") == 0) {
457             current_swallow->dock = val;
458             swallow_is_empty = false;
459         }
460         if (strcasecmp(last_key, "insert_where") == 0) {
461             current_swallow->insert_where = val;
462             swallow_is_empty = false;
463         }
464     }
465
466     return 1;
467 }
468
469 static int json_bool(void *ctx, int val) {
470     LOG("bool %d for key %s\n", val, last_key);
471     if (strcasecmp(last_key, "focused") == 0 && val) {
472         to_focus = json_node;
473     }
474
475     if (strcasecmp(last_key, "sticky") == 0)
476         json_node->sticky = val;
477
478     if (parsing_swallows) {
479         if (strcasecmp(last_key, "restart_mode") == 0) {
480             current_swallow->restart_mode = val;
481             swallow_is_empty = false;
482         }
483     }
484
485     return 1;
486 }
487
488 static int json_double(void *ctx, double val) {
489     LOG("double %f for key %s\n", val, last_key);
490     if (strcasecmp(last_key, "percent") == 0) {
491         json_node->percent = val;
492     }
493     return 1;
494 }
495
496 static json_content_t content_result;
497 static int content_level;
498
499 static int json_determine_content_deeper(void *ctx) {
500     content_level++;
501     return 1;
502 }
503
504 static int json_determine_content_shallower(void *ctx) {
505     content_level--;
506     return 1;
507 }
508
509 static int json_determine_content_string(void *ctx, const unsigned char *val, size_t len) {
510     if (strcasecmp(last_key, "type") != 0 || content_level > 1)
511         return 1;
512
513     DLOG("string = %.*s, last_key = %s\n", (int)len, val, last_key);
514     if (strncasecmp((const char *)val, "workspace", len) == 0)
515         content_result = JSON_CONTENT_WORKSPACE;
516     return 0;
517 }
518
519 /* Parses the given JSON file until it encounters the first “type” property to
520  * determine whether the file contains workspaces or regular containers, which
521  * is important to know when deciding where (and how) to append the contents.
522  * */
523 json_content_t json_determine_content(const char *filename) {
524     FILE *f;
525     if ((f = fopen(filename, "r")) == NULL) {
526         ELOG("Cannot open file \"%s\"\n", filename);
527         return JSON_CONTENT_UNKNOWN;
528     }
529     struct stat stbuf;
530     if (fstat(fileno(f), &stbuf) != 0) {
531         ELOG("Cannot fstat() \"%s\"\n", filename);
532         fclose(f);
533         return JSON_CONTENT_UNKNOWN;
534     }
535     char *buf = smalloc(stbuf.st_size);
536     int n = fread(buf, 1, stbuf.st_size, f);
537     if (n != stbuf.st_size) {
538         ELOG("File \"%s\" could not be read entirely, not loading.\n", filename);
539         fclose(f);
540         return JSON_CONTENT_UNKNOWN;
541     }
542     DLOG("read %d bytes\n", n);
543     // We default to JSON_CONTENT_CON because it is legal to not include
544     // “"type": "con"” in the JSON files for better readability.
545     content_result = JSON_CONTENT_CON;
546     content_level = 0;
547     yajl_gen g;
548     yajl_handle hand;
549     static yajl_callbacks callbacks = {
550         .yajl_string = json_determine_content_string,
551         .yajl_map_key = json_key,
552         .yajl_start_array = json_determine_content_deeper,
553         .yajl_start_map = json_determine_content_deeper,
554         .yajl_end_map = json_determine_content_shallower,
555         .yajl_end_array = json_determine_content_shallower,
556     };
557     g = yajl_gen_alloc(NULL);
558     hand = yajl_alloc(&callbacks, NULL, (void *)g);
559     /* Allowing comments allows for more user-friendly layout files. */
560     yajl_config(hand, yajl_allow_comments, true);
561     /* Allow multiple values, i.e. multiple nodes to attach */
562     yajl_config(hand, yajl_allow_multiple_values, true);
563     yajl_status stat;
564     setlocale(LC_NUMERIC, "C");
565     stat = yajl_parse(hand, (const unsigned char *)buf, n);
566     if (stat != yajl_status_ok && stat != yajl_status_client_canceled) {
567         unsigned char *str = yajl_get_error(hand, 1, (const unsigned char *)buf, n);
568         ELOG("JSON parsing error: %s\n", str);
569         yajl_free_error(hand, str);
570     }
571
572     setlocale(LC_NUMERIC, "");
573     yajl_complete_parse(hand);
574
575     fclose(f);
576
577     return content_result;
578 }
579
580 void tree_append_json(Con *con, const char *filename, char **errormsg) {
581     FILE *f;
582     if ((f = fopen(filename, "r")) == NULL) {
583         ELOG("Cannot open file \"%s\"\n", filename);
584         return;
585     }
586     struct stat stbuf;
587     if (fstat(fileno(f), &stbuf) != 0) {
588         ELOG("Cannot fstat() \"%s\"\n", filename);
589         fclose(f);
590         return;
591     }
592     char *buf = smalloc(stbuf.st_size);
593     int n = fread(buf, 1, stbuf.st_size, f);
594     if (n != stbuf.st_size) {
595         ELOG("File \"%s\" could not be read entirely, not loading.\n", filename);
596         fclose(f);
597         return;
598     }
599     DLOG("read %d bytes\n", n);
600     yajl_gen g;
601     yajl_handle hand;
602     static yajl_callbacks callbacks = {
603         .yajl_boolean = json_bool,
604         .yajl_integer = json_int,
605         .yajl_double = json_double,
606         .yajl_string = json_string,
607         .yajl_start_map = json_start_map,
608         .yajl_map_key = json_key,
609         .yajl_end_map = json_end_map,
610         .yajl_end_array = json_end_array,
611     };
612     g = yajl_gen_alloc(NULL);
613     hand = yajl_alloc(&callbacks, NULL, (void *)g);
614     /* Allowing comments allows for more user-friendly layout files. */
615     yajl_config(hand, yajl_allow_comments, true);
616     /* Allow multiple values, i.e. multiple nodes to attach */
617     yajl_config(hand, yajl_allow_multiple_values, true);
618     yajl_status stat;
619     json_node = con;
620     to_focus = NULL;
621     parsing_swallows = false;
622     parsing_rect = false;
623     parsing_deco_rect = false;
624     parsing_window_rect = false;
625     parsing_geometry = false;
626     parsing_focus = false;
627     parsing_marks = false;
628     setlocale(LC_NUMERIC, "C");
629     stat = yajl_parse(hand, (const unsigned char *)buf, n);
630     if (stat != yajl_status_ok) {
631         unsigned char *str = yajl_get_error(hand, 1, (const unsigned char *)buf, n);
632         ELOG("JSON parsing error: %s\n", str);
633         if (errormsg != NULL)
634             *errormsg = sstrdup((const char *)str);
635         yajl_free_error(hand, str);
636     }
637
638     /* In case not all containers were restored, we need to fix the
639      * percentages, otherwise i3 will crash immediately when rendering the
640      * next time. */
641     con_fix_percent(con);
642
643     setlocale(LC_NUMERIC, "");
644     yajl_complete_parse(hand);
645     yajl_free(hand);
646     yajl_gen_free(g);
647
648     fclose(f);
649     free(buf);
650     if (to_focus)
651         con_focus(to_focus);
652 }