i3
load_layout.c
Go to the documentation of this file.
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_gaps;
24 static bool parsing_swallows;
25 static bool parsing_rect;
27 static bool parsing_deco_rect;
28 static bool parsing_window_rect;
29 static bool parsing_geometry;
30 static bool parsing_focus;
31 static bool parsing_marks;
33 static bool swallow_is_empty;
34 static 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. */
40 struct pending_marks {
41  char *mark;
43 } * marks;
44 
45 /* This list is used for reordering the focus stack after parsing the 'focus'
46  * array. */
47 struct focus_mapping {
48  int old_id;
49  TAILQ_ENTRY(focus_mapping) focus_mappings;
50 };
51 
52 static TAILQ_HEAD(focus_mappings_head, focus_mapping) focus_mappings =
53  TAILQ_HEAD_INITIALIZER(focus_mappings);
54 
55 static 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 
92 static 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;
131  while (get_existing_workspace_by_name(json_node->name) != NULL) {
132  FREE(json_node->name);
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;
192  floating_enable(json_node, false);
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;
210  parsing_actual_deco_rect = false;
211  parsing_deco_rect = false;
212  parsing_window_rect = false;
213  parsing_geometry = false;
214  return 1;
215 }
216 
217 static 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  LOG("got it! %p\n", con);
239  /* Move this entry to the top of the focus list. */
240  TAILQ_REMOVE(&(json_node->focus_head), con, focused);
241  TAILQ_INSERT_HEAD(&(json_node->focus_head), con, focused);
242  break;
243  }
244  }
245  while (!TAILQ_EMPTY(&focus_mappings)) {
246  mapping = TAILQ_FIRST(&focus_mappings);
247  TAILQ_REMOVE(&focus_mappings, mapping, focus_mappings);
248  free(mapping);
249  }
250  parsing_focus = false;
251  }
252  return 1;
253 }
254 
255 static int json_key(void *ctx, const unsigned char *val, size_t len) {
256  LOG("key: %.*s\n", (int)len, val);
257  FREE(last_key);
258  last_key = scalloc(len + 1, 1);
259  memcpy(last_key, val, len);
260  if (strcasecmp(last_key, "swallows") == 0)
261  parsing_swallows = true;
262 
263  if (strcasecmp(last_key, "gaps") == 0)
264  parsing_gaps = true;
265 
266  if (strcasecmp(last_key, "rect") == 0)
267  parsing_rect = true;
268 
269  if (strcasecmp(last_key, "actual_deco_rect") == 0)
271 
272  if (strcasecmp(last_key, "deco_rect") == 0)
273  parsing_deco_rect = true;
274 
275  if (strcasecmp(last_key, "window_rect") == 0)
276  parsing_window_rect = true;
277 
278  if (strcasecmp(last_key, "geometry") == 0)
279  parsing_geometry = true;
280 
281  if (strcasecmp(last_key, "focus") == 0)
282  parsing_focus = true;
283 
284  if (strcasecmp(last_key, "marks") == 0) {
285  num_marks = 0;
286  parsing_marks = true;
287  }
288 
289  return 1;
290 }
291 
292 static int json_string(void *ctx, const unsigned char *val, size_t len) {
293  LOG("string: %.*s for key %s\n", (int)len, val, last_key);
294  if (parsing_swallows) {
295  char *sval;
296  sasprintf(&sval, "%.*s", len, val);
297  if (strcasecmp(last_key, "class") == 0) {
299  swallow_is_empty = false;
300  } else if (strcasecmp(last_key, "instance") == 0) {
302  swallow_is_empty = false;
303  } else if (strcasecmp(last_key, "window_role") == 0) {
305  swallow_is_empty = false;
306  } else if (strcasecmp(last_key, "title") == 0) {
308  swallow_is_empty = false;
309  } else if (strcasecmp(last_key, "machine") == 0) {
311  swallow_is_empty = false;
312  } else {
313  ELOG("swallow key %s unknown\n", last_key);
314  }
315  free(sval);
316  } else if (parsing_marks) {
317  char *mark;
318  sasprintf(&mark, "%.*s", (int)len, val);
319 
320  marks = srealloc(marks, (++num_marks) * sizeof(struct pending_marks));
321  marks[num_marks - 1].mark = sstrdup(mark);
323  } else {
324  if (strcasecmp(last_key, "name") == 0) {
325  json_node->name = scalloc(len + 1, 1);
326  memcpy(json_node->name, val, len);
327  } else if (strcasecmp(last_key, "title_format") == 0) {
328  json_node->title_format = scalloc(len + 1, 1);
329  memcpy(json_node->title_format, val, len);
330  } else if (strcasecmp(last_key, "sticky_group") == 0) {
331  json_node->sticky_group = scalloc(len + 1, 1);
332  memcpy(json_node->sticky_group, val, len);
333  LOG("sticky_group of this container is %s\n", json_node->sticky_group);
334  } else if (strcasecmp(last_key, "orientation") == 0) {
335  /* Upgrade path from older versions of i3 (doing an inplace restart
336  * to a newer version):
337  * "orientation" is dumped before "layout". Therefore, we store
338  * whether the orientation was horizontal or vertical in the
339  * last_split_layout. When we then encounter layout == "default",
340  * we will use the last_split_layout as layout instead. */
341  char *buf = NULL;
342  sasprintf(&buf, "%.*s", (int)len, val);
343  if (strcasecmp(buf, "none") == 0 ||
344  strcasecmp(buf, "horizontal") == 0)
346  else if (strcasecmp(buf, "vertical") == 0)
348  else
349  LOG("Unhandled orientation: %s\n", buf);
350  free(buf);
351  } else if (strcasecmp(last_key, "border") == 0) {
352  char *buf = NULL;
353  sasprintf(&buf, "%.*s", (int)len, val);
354  if (strcasecmp(buf, "none") == 0)
356  else if (strcasecmp(buf, "1pixel") == 0) {
359  } else if (strcasecmp(buf, "pixel") == 0)
361  else if (strcasecmp(buf, "normal") == 0)
363  else
364  LOG("Unhandled \"border\": %s\n", buf);
365  free(buf);
366  } else if (strcasecmp(last_key, "type") == 0) {
367  char *buf = NULL;
368  sasprintf(&buf, "%.*s", (int)len, val);
369  if (strcasecmp(buf, "root") == 0)
370  json_node->type = CT_ROOT;
371  else if (strcasecmp(buf, "output") == 0)
372  json_node->type = CT_OUTPUT;
373  else if (strcasecmp(buf, "con") == 0)
374  json_node->type = CT_CON;
375  else if (strcasecmp(buf, "floating_con") == 0)
376  json_node->type = CT_FLOATING_CON;
377  else if (strcasecmp(buf, "workspace") == 0)
378  json_node->type = CT_WORKSPACE;
379  else if (strcasecmp(buf, "dockarea") == 0)
380  json_node->type = CT_DOCKAREA;
381  else
382  LOG("Unhandled \"type\": %s\n", buf);
383  free(buf);
384  } else if (strcasecmp(last_key, "layout") == 0) {
385  char *buf = NULL;
386  sasprintf(&buf, "%.*s", (int)len, val);
387  if (strcasecmp(buf, "default") == 0)
388  /* This set above when we read "orientation". */
390  else if (strcasecmp(buf, "stacked") == 0)
392  else if (strcasecmp(buf, "tabbed") == 0)
394  else if (strcasecmp(buf, "dockarea") == 0)
396  else if (strcasecmp(buf, "output") == 0)
398  else if (strcasecmp(buf, "splith") == 0)
400  else if (strcasecmp(buf, "splitv") == 0)
402  else
403  LOG("Unhandled \"layout\": %s\n", buf);
404  free(buf);
405  } else if (strcasecmp(last_key, "workspace_layout") == 0) {
406  char *buf = NULL;
407  sasprintf(&buf, "%.*s", (int)len, val);
408  if (strcasecmp(buf, "default") == 0)
410  else if (strcasecmp(buf, "stacked") == 0)
412  else if (strcasecmp(buf, "tabbed") == 0)
414  else
415  LOG("Unhandled \"workspace_layout\": %s\n", buf);
416  free(buf);
417  } else if (strcasecmp(last_key, "last_split_layout") == 0) {
418  char *buf = NULL;
419  sasprintf(&buf, "%.*s", (int)len, val);
420  if (strcasecmp(buf, "splith") == 0)
422  else if (strcasecmp(buf, "splitv") == 0)
424  else
425  LOG("Unhandled \"last_splitlayout\": %s\n", buf);
426  free(buf);
427  } else if (strcasecmp(last_key, "mark") == 0) {
428  DLOG("Found deprecated key \"mark\".\n");
429 
430  char *buf = NULL;
431  sasprintf(&buf, "%.*s", (int)len, val);
432 
434  } else if (strcasecmp(last_key, "floating") == 0) {
435  char *buf = NULL;
436  sasprintf(&buf, "%.*s", (int)len, val);
437  if (strcasecmp(buf, "auto_off") == 0)
438  json_node->floating = FLOATING_AUTO_OFF;
439  else if (strcasecmp(buf, "auto_on") == 0)
440  json_node->floating = FLOATING_AUTO_ON;
441  else if (strcasecmp(buf, "user_off") == 0)
442  json_node->floating = FLOATING_USER_OFF;
443  else if (strcasecmp(buf, "user_on") == 0)
444  json_node->floating = FLOATING_USER_ON;
445  free(buf);
446  } else if (strcasecmp(last_key, "scratchpad_state") == 0) {
447  char *buf = NULL;
448  sasprintf(&buf, "%.*s", (int)len, val);
449  if (strcasecmp(buf, "none") == 0)
450  json_node->scratchpad_state = SCRATCHPAD_NONE;
451  else if (strcasecmp(buf, "fresh") == 0)
452  json_node->scratchpad_state = SCRATCHPAD_FRESH;
453  else if (strcasecmp(buf, "changed") == 0)
454  json_node->scratchpad_state = SCRATCHPAD_CHANGED;
455  free(buf);
456  } else if (strcasecmp(last_key, "previous_workspace_name") == 0) {
458  previous_workspace_name = sstrndup((const char *)val, len);
459  }
460  }
461  return 1;
462 }
463 
464 static int json_int(void *ctx, long long val) {
465  LOG("int %lld for key %s\n", val, last_key);
466  /* For backwards compatibility with i3 < 4.8 */
467  if (strcasecmp(last_key, "type") == 0)
468  json_node->type = val;
469 
470  if (strcasecmp(last_key, "fullscreen_mode") == 0)
471  json_node->fullscreen_mode = val;
472 
473  if (strcasecmp(last_key, "num") == 0)
474  json_node->num = val;
475 
476  if (strcasecmp(last_key, "current_border_width") == 0)
478 
479  if (strcasecmp(last_key, "window_icon_padding") == 0) {
481  }
482 
483  if (strcasecmp(last_key, "depth") == 0)
484  json_node->depth = val;
485 
486  if (!parsing_swallows && strcasecmp(last_key, "id") == 0)
487  json_node->old_id = val;
488 
489  if (parsing_focus) {
490  struct focus_mapping *focus_mapping = scalloc(1, sizeof(struct focus_mapping));
491  focus_mapping->old_id = val;
492  TAILQ_INSERT_TAIL(&focus_mappings, focus_mapping, focus_mappings);
493  }
494 
496  Rect *r;
497  if (parsing_rect)
498  r = &(json_node->rect);
499  else if (parsing_window_rect)
500  r = &(json_node->window_rect);
501  else
502  r = &(json_node->geometry);
503  if (strcasecmp(last_key, "x") == 0)
504  r->x = val;
505  else if (strcasecmp(last_key, "y") == 0)
506  r->y = val;
507  else if (strcasecmp(last_key, "width") == 0)
508  r->width = val;
509  else if (strcasecmp(last_key, "height") == 0)
510  r->height = val;
511  else
512  ELOG("WARNING: unknown key %s in rect\n", last_key);
513  DLOG("rect now: (%d, %d, %d, %d)\n",
514  r->x, r->y, r->width, r->height);
515  }
516  if (parsing_swallows) {
517  if (strcasecmp(last_key, "id") == 0) {
518  current_swallow->id = val;
519  swallow_is_empty = false;
520  }
521  if (strcasecmp(last_key, "dock") == 0) {
522  current_swallow->dock = val;
523  swallow_is_empty = false;
524  }
525  if (strcasecmp(last_key, "insert_where") == 0) {
527  swallow_is_empty = false;
528  }
529  }
530  if (parsing_gaps) {
531  if (strcasecmp(last_key, "inner") == 0)
532  json_node->gaps.inner = val;
533  else if (strcasecmp(last_key, "top") == 0)
534  json_node->gaps.top = val;
535  else if (strcasecmp(last_key, "right") == 0)
536  json_node->gaps.right = val;
537  else if (strcasecmp(last_key, "bottom") == 0)
538  json_node->gaps.bottom = val;
539  else if (strcasecmp(last_key, "left") == 0)
540  json_node->gaps.left = val;
541  }
542 
543  return 1;
544 }
545 
546 static int json_bool(void *ctx, int val) {
547  LOG("bool %d for key %s\n", val, last_key);
548  if (strcasecmp(last_key, "focused") == 0 && val) {
550  }
551 
552  if (strcasecmp(last_key, "sticky") == 0)
553  json_node->sticky = val;
554 
555  if (parsing_swallows) {
556  if (strcasecmp(last_key, "restart_mode") == 0) {
558  swallow_is_empty = false;
559  }
560  }
561 
562  return 1;
563 }
564 
565 static int json_double(void *ctx, double val) {
566  LOG("double %f for key %s\n", val, last_key);
567  if (strcasecmp(last_key, "percent") == 0) {
568  json_node->percent = val;
569  }
570  return 1;
571 }
572 
574 static int content_level;
575 
577  content_level++;
578  return 1;
579 }
580 
582  content_level--;
583  return 1;
584 }
585 
586 static int json_determine_content_string(void *ctx, const unsigned char *val, size_t len) {
587  if (strcasecmp(last_key, "type") != 0 || content_level > 1)
588  return 1;
589 
590  DLOG("string = %.*s, last_key = %s\n", (int)len, val, last_key);
591  if (strncasecmp((const char *)val, "workspace", len) == 0)
593  return 0;
594 }
595 
596 /*
597  * Returns true if the provided JSON could be parsed by yajl.
598  *
599  */
600 bool json_validate(const char *buf, const size_t len) {
601  bool valid = true;
602  yajl_handle hand = yajl_alloc(NULL, NULL, NULL);
603  /* Allowing comments allows for more user-friendly layout files. */
604  yajl_config(hand, yajl_allow_comments, true);
605  /* Allow multiple values, i.e. multiple nodes to attach */
606  yajl_config(hand, yajl_allow_multiple_values, true);
607 
608  setlocale(LC_NUMERIC, "C");
609  if (yajl_parse(hand, (const unsigned char *)buf, len) != yajl_status_ok) {
610  unsigned char *str = yajl_get_error(hand, 1, (const unsigned char *)buf, len);
611  ELOG("JSON parsing error: %s\n", str);
612  yajl_free_error(hand, str);
613  valid = false;
614  }
615  setlocale(LC_NUMERIC, "");
616 
617  yajl_complete_parse(hand);
618  yajl_free(hand);
619 
620  return valid;
621 }
622 
623 /* Parses the given JSON file until it encounters the first “type” property to
624  * determine whether the file contains workspaces or regular containers, which
625  * is important to know when deciding where (and how) to append the contents.
626  * */
627 json_content_t json_determine_content(const char *buf, const size_t len) {
628  // We default to JSON_CONTENT_CON because it is legal to not include
629  // “"type": "con"” in the JSON files for better readability.
631  content_level = 0;
632  static yajl_callbacks callbacks = {
633  .yajl_string = json_determine_content_string,
634  .yajl_map_key = json_key,
635  .yajl_start_array = json_determine_content_deeper,
636  .yajl_start_map = json_determine_content_deeper,
637  .yajl_end_map = json_determine_content_shallower,
638  .yajl_end_array = json_determine_content_shallower,
639  };
640  yajl_handle hand = yajl_alloc(&callbacks, NULL, NULL);
641  /* Allowing comments allows for more user-friendly layout files. */
642  yajl_config(hand, yajl_allow_comments, true);
643  /* Allow multiple values, i.e. multiple nodes to attach */
644  yajl_config(hand, yajl_allow_multiple_values, true);
645  setlocale(LC_NUMERIC, "C");
646  const yajl_status stat = yajl_parse(hand, (const unsigned char *)buf, len);
647  if (stat != yajl_status_ok && stat != yajl_status_client_canceled) {
648  unsigned char *str = yajl_get_error(hand, 1, (const unsigned char *)buf, len);
649  ELOG("JSON parsing error: %s\n", str);
650  yajl_free_error(hand, str);
651  }
652 
653  setlocale(LC_NUMERIC, "");
654  yajl_complete_parse(hand);
655  yajl_free(hand);
656 
657  return content_result;
658 }
659 
660 void tree_append_json(Con *con, const char *buf, const size_t len, char **errormsg) {
661  static yajl_callbacks callbacks = {
662  .yajl_boolean = json_bool,
663  .yajl_integer = json_int,
664  .yajl_double = json_double,
665  .yajl_string = json_string,
666  .yajl_start_map = json_start_map,
667  .yajl_map_key = json_key,
668  .yajl_end_map = json_end_map,
669  .yajl_end_array = json_end_array,
670  };
671  yajl_handle hand = yajl_alloc(&callbacks, NULL, NULL);
672  /* Allowing comments allows for more user-friendly layout files. */
673  yajl_config(hand, yajl_allow_comments, true);
674  /* Allow multiple values, i.e. multiple nodes to attach */
675  yajl_config(hand, yajl_allow_multiple_values, true);
676  /* We don't need to validate that the input is valid UTF8 here.
677  * tree_append_json is called in two cases:
678  * 1. With the append_layout command. json_validate is called first and will
679  * fail on invalid UTF8 characters so we don't need to recheck.
680  * 2. With an in-place restart. The rest of the codebase should be
681  * responsible for producing valid UTF8 JSON output. If not,
682  * tree_append_json will just preserve invalid UTF8 strings in the tree
683  * instead of failing to parse the layout file which could lead to
684  * problems like in #3156.
685  * Either way, disabling UTF8 validation slightly speeds up yajl. */
686  yajl_config(hand, yajl_dont_validate_strings, true);
687  json_node = con;
688  to_focus = NULL;
689  parsing_gaps = false;
690  incomplete = 0;
691  parsing_swallows = false;
692  parsing_rect = false;
693  parsing_actual_deco_rect = false;
694  parsing_deco_rect = false;
695  parsing_window_rect = false;
696  parsing_geometry = false;
697  parsing_focus = false;
698  parsing_marks = false;
699  setlocale(LC_NUMERIC, "C");
700  const yajl_status stat = yajl_parse(hand, (const unsigned char *)buf, len);
701  if (stat != yajl_status_ok) {
702  unsigned char *str = yajl_get_error(hand, 1, (const unsigned char *)buf, len);
703  ELOG("JSON parsing error: %s\n", str);
704  if (errormsg != NULL)
705  *errormsg = sstrdup((const char *)str);
706  yajl_free_error(hand, str);
707  while (incomplete-- > 0) {
708  Con *parent = json_node->parent;
709  DLOG("freeing incomplete container %p\n", json_node);
710  if (json_node == to_focus) {
711  to_focus = NULL;
712  }
714  json_node = parent;
715  }
716  }
717 
718  /* In case not all containers were restored, we need to fix the
719  * percentages, otherwise i3 will crash immediately when rendering the
720  * next time. */
721  con_fix_percent(con);
722 
723  setlocale(LC_NUMERIC, "");
724  yajl_complete_parse(hand);
725  yajl_free(hand);
726 
727  if (to_focus) {
729  }
730 }
void con_free(Con *con)
Frees the specified container.
Definition: con.c:79
struct pending_marks * marks
void con_fix_percent(Con *con)
Updates the percent attribute of the children of the given container.
Definition: con.c:1047
int left
Definition: data.h:155
uint16_t depth
Definition: data.h:832
#define ELOG(fmt,...)
Definition: libi3.h:100
static bool parsing_actual_deco_rect
Definition: load_layout.c:26
struct Match * current_swallow
Definition: load_layout.c:32
static int json_determine_content_string(void *ctx, const unsigned char *val, size_t len)
Definition: load_layout.c:586
Definition: data.h:68
Definition: data.h:67
bool sticky
Definition: data.h:767
uint32_t y
Definition: data.h:209
Definition: data.h:112
char * name
Definition: data.h:720
static TAILQ_HEAD(focus_mappings_head, focus_mapping)
Definition: load_layout.c:52
Con * con_to_be_marked
Definition: load_layout.c:42
A "match" is a data structure which acts like a mask or expression to match certain windows or not...
Definition: data.h:557
#define TAILQ_FIRST(head)
Definition: queue.h:336
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
Stores a rectangle, for example the size of a window, the child window etc.
Definition: data.h:207
int top
Definition: data.h:152
static int json_int(void *ctx, long long val)
Definition: load_layout.c:464
enum Match::@13 dock
void x_con_init(Con *con)
Initializes the X11 part for the given container.
Definition: x.c:127
static bool parsing_window_rect
Definition: load_layout.c:28
char * sticky_group
Definition: data.h:733
static bool parsing_rect
Definition: load_layout.c:25
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
layout_t workspace_layout
Definition: data.h:783
struct regex * window_role
Definition: data.h:566
Definition: data.h:110
int current_border_width
Definition: data.h:744
static char * last_key
Definition: load_layout.c:19
int right
Definition: data.h:153
uint32_t height
Definition: data.h:211
char * title_format
The format with which the window&#39;s name should be displayed.
Definition: data.h:723
enum Con::@18 type
static int incomplete
Definition: load_layout.c:20
enum Con::@20 scratchpad_state
static int json_determine_content_shallower(void *ctx)
Definition: load_layout.c:581
static int json_bool(void *ctx, int val)
Definition: load_layout.c:546
static int json_determine_content_deeper(void *ctx)
Definition: load_layout.c:576
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 con_mark(Con *con, const char *mark, mark_mode_t mode)
Assigns a mark to the container.
Definition: con.c:806
static bool swallow_is_empty
Definition: load_layout.c:33
static bool parsing_geometry
Definition: load_layout.c:29
int num
the workspace number, if this Con is of type CT_WORKSPACE and the workspace is not a named workspace ...
Definition: data.h:701
int max(int a, int b)
Definition: util.c:28
int inner
Definition: data.h:151
struct regex * instance
Definition: data.h:564
static int json_string(void *ctx, const unsigned char *val, size_t len)
Definition: load_layout.c:292
static Con * json_node
Definition: load_layout.c:21
void tree_append_json(Con *con, const char *buf, const size_t len, char **errormsg)
Definition: load_layout.c:660
#define TAILQ_EMPTY(head)
Definition: queue.h:344
json_content_t json_determine_content(const char *buf, const size_t len)
Definition: load_layout.c:627
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:347
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
Definition: con.c:477
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
void * smalloc(size_t size)
Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory a...
bool rect_equals(Rect a, Rect b)
Definition: util.c:59
static bool parsing_deco_rect
Definition: load_layout.c:27
json_content_t
Definition: load_layout.h:15
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...
#define TAILQ_INSERT_HEAD(head, elm, field)
Definition: queue.h:366
fullscreen_mode_t fullscreen_mode
Definition: data.h:762
#define TAILQ_ENTRY(type)
Definition: queue.h:327
Definition: data.h:108
struct regex * machine
Definition: data.h:568
#define FREE(pointer)
Definition: util.h:47
xcb_window_t id
Definition: data.h:582
struct regex * regex_new(const char *pattern)
Creates a new &#39;regex&#39; struct containing the given pattern and a PCRE compiled regular expression...
Definition: regex.c:22
bool con_is_split(Con *con)
Returns true if a container should be considered split.
Definition: con.c:385
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...
Definition: data.h:100
static Con * to_focus
Definition: load_layout.c:22
void floating_check_size(Con *floating_con, bool prefer_height)
Called when a floating window is created or resized.
Definition: floating.c:75
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:232
double percent
Definition: data.h:740
bool json_validate(const char *buf, const size_t len)
Returns true if the provided JSON could be parsed by yajl.
Definition: load_layout.c:600
static int content_level
Definition: load_layout.c:574
layout_t last_split_layout
Definition: data.h:783
struct Rect rect
Definition: data.h:710
static int json_key(void *ctx, const unsigned char *val, size_t len)
Definition: load_layout.c:255
static json_content_t content_result
Definition: load_layout.c:573
static bool parsing_focus
Definition: load_layout.c:30
void match_free(Match *match)
Frees the given match.
Definition: match.c:275
int old_id
Definition: data.h:829
struct Rect window_rect
Definition: data.h:713
int window_icon_padding
Whether the window icon should be displayed, and with what padding.
Definition: data.h:728
uint32_t x
Definition: data.h:208
border_style_t border_style
Definition: data.h:785
static int json_end_array(void *ctx)
Definition: load_layout.c:217
struct Rect geometry
the geometry this window requested when getting mapped
Definition: data.h:718
static bool parsing_swallows
Definition: load_layout.c:24
enum Con::@19 floating
floating? (= not in tiling layout) This cannot be simply a bool because we want to keep track of whet...
int ws_name_to_number(const char *name)
Parses the workspace name as a number.
Definition: util.c:109
layout_t layout
Definition: data.h:783
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:376
uint32_t width
Definition: data.h:210
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
A &#39;Con&#39; represents everything from the X11 root window down to a single X11 window.
Definition: data.h:671
static int num_marks
Definition: load_layout.c:34
struct regex * title
Definition: data.h:561
#define DLOG(fmt,...)
Definition: libi3.h:105
gaps_t gaps
Only applicable for containers of type CT_WORKSPACE.
Definition: data.h:704
static bool parsing_marks
Definition: load_layout.c:31
void con_attach(Con *con, Con *parent, bool ignore_focus)
Attaches the given container to the given parent.
Definition: con.c:222
int bottom
Definition: data.h:154
#define TAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:324
void con_activate(Con *con)
Sets input focus to the given container and raises it to the top.
Definition: con.c:287
struct regex * class
Definition: data.h:563
struct Con * focused
Definition: tree.c:13
void * srealloc(void *ptr, size_t size)
Safe-wrapper around realloc which exits if realloc returns NULL (meaning that there is no more memory...
#define LOG(fmt,...)
Definition: libi3.h:95
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:402
static int json_double(void *ctx, double val)
Definition: load_layout.c:565
Definition: data.h:111
static int json_end_map(void *ctx)
Definition: load_layout.c:92
Definition: data.h:66
struct Con * parent
Definition: data.h:706
static xcb_cursor_context_t * ctx
Definition: xcursor.c:19
enum Match::@15 insert_where
static bool parsing_gaps
Definition: load_layout.c:23
#define TAILQ_FOREACH_REVERSE(var, head, headname, field)
Definition: queue.h:352
void match_init(Match *match)
Initializes the Match data structure.
Definition: match.c:26
bool restart_mode
Definition: data.h:611