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