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