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