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