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