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