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