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