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