i3
load_layout.c
Go to the documentation of this file.
1 #undef I3__FILE__
2 #define I3__FILE__ "load_layout.c"
3 /*
4  * vim:ts=4:sw=4:expandtab
5  *
6  * i3 - an improved dynamic tiling window manager
7  * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
8  *
9  * load_layout.c: Restore (parts of) the layout, for example after an inplace
10  * restart.
11  *
12  */
13 #include "all.h"
14 
15 #include <yajl/yajl_common.h>
16 #include <yajl/yajl_gen.h>
17 #include <yajl/yajl_parse.h>
18 #include <yajl/yajl_version.h>
19 
20 /* TODO: refactor the whole parsing thing */
21 
22 static char *last_key;
23 static Con *json_node;
24 static Con *to_focus;
25 static bool parsing_swallows;
26 static bool parsing_rect;
27 static bool parsing_window_rect;
28 static bool parsing_geometry;
29 static bool parsing_focus;
31 
32 /* This list is used for reordering the focus stack after parsing the 'focus'
33  * array. */
34 struct focus_mapping {
35  int old_id;
36  TAILQ_ENTRY(focus_mapping) focus_mappings;
37 };
38 
39 static TAILQ_HEAD(focus_mappings_head, focus_mapping) focus_mappings =
40  TAILQ_HEAD_INITIALIZER(focus_mappings);
41 
42 static int json_start_map(void *ctx) {
43  LOG("start of map, last_key = %s\n", last_key);
44  if (parsing_swallows) {
45  LOG("creating new swallow\n");
46  current_swallow = smalloc(sizeof(Match));
47  match_init(current_swallow);
48  TAILQ_INSERT_TAIL(&(json_node->swallow_head), current_swallow, matches);
49  } else {
51  if (last_key && strcasecmp(last_key, "floating_nodes") == 0) {
52  DLOG("New floating_node\n");
53  Con *ws = con_get_workspace(json_node);
54  json_node = con_new(NULL, NULL);
55  json_node->parent = ws;
56  DLOG("Parent is workspace = %p\n", ws);
57  } else {
58  Con *parent = json_node;
59  json_node = con_new(NULL, NULL);
60  json_node->parent = parent;
61  }
62  }
63  }
64  return 1;
65 }
66 
67 static int json_end_map(void *ctx) {
68  LOG("end of map\n");
70  LOG("attaching\n");
71  con_attach(json_node, json_node->parent, true);
72  json_node = json_node->parent;
73  }
74  if (parsing_rect)
75  parsing_rect = false;
77  parsing_window_rect = false;
78  if (parsing_geometry)
79  parsing_geometry = false;
80  return 1;
81 }
82 
83 static int json_end_array(void *ctx) {
84  LOG("end of array\n");
85  parsing_swallows = false;
86  if (parsing_focus) {
87  /* Clear the list of focus mappings */
88  struct focus_mapping *mapping;
89  TAILQ_FOREACH_REVERSE(mapping, &focus_mappings, focus_mappings_head, focus_mappings) {
90  LOG("focus (reverse) %d\n", mapping->old_id);
91  Con *con;
92  TAILQ_FOREACH(con, &(json_node->focus_head), focused) {
93  if (con->old_id != mapping->old_id)
94  continue;
95  LOG("got it! %p\n", con);
96  /* Move this entry to the top of the focus list. */
97  TAILQ_REMOVE(&(json_node->focus_head), con, focused);
98  TAILQ_INSERT_HEAD(&(json_node->focus_head), con, focused);
99  break;
100  }
101  }
102  while (!TAILQ_EMPTY(&focus_mappings)) {
103  mapping = TAILQ_FIRST(&focus_mappings);
104  TAILQ_REMOVE(&focus_mappings, mapping, focus_mappings);
105  free(mapping);
106  }
107  parsing_focus = false;
108  }
109  return 1;
110 }
111 
112 #if YAJL_MAJOR < 2
113 static int json_key(void *ctx, const unsigned char *val, unsigned int len) {
114 #else
115 static int json_key(void *ctx, const unsigned char *val, size_t len) {
116 #endif
117  LOG("key: %.*s\n", (int)len, val);
118  FREE(last_key);
119  last_key = scalloc((len+1) * sizeof(char));
120  memcpy(last_key, val, len);
121  if (strcasecmp(last_key, "swallows") == 0)
122  parsing_swallows = true;
123 
124  if (strcasecmp(last_key, "rect") == 0)
125  parsing_rect = true;
126 
127  if (strcasecmp(last_key, "window_rect") == 0)
128  parsing_window_rect = true;
129 
130  if (strcasecmp(last_key, "geometry") == 0)
131  parsing_geometry = true;
132 
133  if (strcasecmp(last_key, "focus") == 0)
134  parsing_focus = true;
135 
136  return 1;
137 }
138 
139 #if YAJL_MAJOR >= 2
140 static int json_string(void *ctx, const unsigned char *val, size_t len) {
141 #else
142 static int json_string(void *ctx, const unsigned char *val, unsigned int len) {
143 #endif
144  LOG("string: %.*s for key %s\n", (int)len, val, last_key);
145  if (parsing_swallows) {
146  /* TODO: the other swallowing keys */
147  if (strcasecmp(last_key, "class") == 0) {
148  current_swallow->class = scalloc((len+1) * sizeof(char));
149  memcpy(current_swallow->class, val, len);
150  }
151  LOG("unhandled yet: swallow\n");
152  } else {
153  if (strcasecmp(last_key, "name") == 0) {
154  json_node->name = scalloc((len+1) * sizeof(char));
155  memcpy(json_node->name, val, len);
156  } else if (strcasecmp(last_key, "sticky_group") == 0) {
157  json_node->sticky_group = scalloc((len+1) * sizeof(char));
158  memcpy(json_node->sticky_group, val, len);
159  LOG("sticky_group of this container is %s\n", json_node->sticky_group);
160  } else if (strcasecmp(last_key, "orientation") == 0) {
161  /* Upgrade path from older versions of i3 (doing an inplace restart
162  * to a newer version):
163  * "orientation" is dumped before "layout". Therefore, we store
164  * whether the orientation was horizontal or vertical in the
165  * last_split_layout. When we then encounter layout == "default",
166  * we will use the last_split_layout as layout instead. */
167  char *buf = NULL;
168  sasprintf(&buf, "%.*s", (int)len, val);
169  if (strcasecmp(buf, "none") == 0 ||
170  strcasecmp(buf, "horizontal") == 0)
171  json_node->last_split_layout = L_SPLITH;
172  else if (strcasecmp(buf, "vertical") == 0)
173  json_node->last_split_layout = L_SPLITV;
174  else LOG("Unhandled orientation: %s\n", buf);
175  free(buf);
176  } else if (strcasecmp(last_key, "border") == 0) {
177  char *buf = NULL;
178  sasprintf(&buf, "%.*s", (int)len, val);
179  if (strcasecmp(buf, "none") == 0)
180  json_node->border_style = BS_NONE;
181  else if (strcasecmp(buf, "1pixel") == 0) {
182  json_node->border_style = BS_PIXEL;
183  json_node->current_border_width = 1;
184  } else if (strcasecmp(buf, "pixel") == 0)
185  json_node->border_style = BS_PIXEL;
186  else if (strcasecmp(buf, "normal") == 0)
187  json_node->border_style = BS_NORMAL;
188  else LOG("Unhandled \"border\": %s\n", buf);
189  free(buf);
190  } else if (strcasecmp(last_key, "layout") == 0) {
191  char *buf = NULL;
192  sasprintf(&buf, "%.*s", (int)len, val);
193  if (strcasecmp(buf, "default") == 0)
194  /* This set above when we read "orientation". */
195  json_node->layout = json_node->last_split_layout;
196  else if (strcasecmp(buf, "stacked") == 0)
197  json_node->layout = L_STACKED;
198  else if (strcasecmp(buf, "tabbed") == 0)
199  json_node->layout = L_TABBED;
200  else if (strcasecmp(buf, "dockarea") == 0)
201  json_node->layout = L_DOCKAREA;
202  else if (strcasecmp(buf, "output") == 0)
203  json_node->layout = L_OUTPUT;
204  else if (strcasecmp(buf, "splith") == 0)
205  json_node->layout = L_SPLITH;
206  else if (strcasecmp(buf, "splitv") == 0)
207  json_node->layout = L_SPLITV;
208  else LOG("Unhandled \"layout\": %s\n", buf);
209  free(buf);
210  } else if (strcasecmp(last_key, "workspace_layout") == 0) {
211  char *buf = NULL;
212  sasprintf(&buf, "%.*s", (int)len, val);
213  if (strcasecmp(buf, "default") == 0)
214  json_node->workspace_layout = L_DEFAULT;
215  else if (strcasecmp(buf, "stacked") == 0)
216  json_node->workspace_layout = L_STACKED;
217  else if (strcasecmp(buf, "tabbed") == 0)
218  json_node->workspace_layout = L_TABBED;
219  else LOG("Unhandled \"workspace_layout\": %s\n", buf);
220  free(buf);
221  } else if (strcasecmp(last_key, "last_split_layout") == 0) {
222  char *buf = NULL;
223  sasprintf(&buf, "%.*s", (int)len, val);
224  if (strcasecmp(buf, "splith") == 0)
225  json_node->last_split_layout = L_SPLITH;
226  else if (strcasecmp(buf, "splitv") == 0)
227  json_node->last_split_layout = L_SPLITV;
228  else LOG("Unhandled \"last_splitlayout\": %s\n", buf);
229  free(buf);
230  } else if (strcasecmp(last_key, "mark") == 0) {
231  char *buf = NULL;
232  sasprintf(&buf, "%.*s", (int)len, val);
233  json_node->mark = buf;
234  } else if (strcasecmp(last_key, "floating") == 0) {
235  char *buf = NULL;
236  sasprintf(&buf, "%.*s", (int)len, val);
237  if (strcasecmp(buf, "auto_off") == 0)
238  json_node->floating = FLOATING_AUTO_OFF;
239  else if (strcasecmp(buf, "auto_on") == 0)
240  json_node->floating = FLOATING_AUTO_ON;
241  else if (strcasecmp(buf, "user_off") == 0)
242  json_node->floating = FLOATING_USER_OFF;
243  else if (strcasecmp(buf, "user_on") == 0)
244  json_node->floating = FLOATING_USER_ON;
245  free(buf);
246  } else if (strcasecmp(last_key, "scratchpad_state") == 0) {
247  char *buf = NULL;
248  sasprintf(&buf, "%.*s", (int)len, val);
249  if (strcasecmp(buf, "none") == 0)
250  json_node->scratchpad_state = SCRATCHPAD_NONE;
251  else if (strcasecmp(buf, "fresh") == 0)
252  json_node->scratchpad_state = SCRATCHPAD_FRESH;
253  else if (strcasecmp(buf, "changed") == 0)
254  json_node->scratchpad_state = SCRATCHPAD_CHANGED;
255  free(buf);
256  }
257  }
258  return 1;
259 }
260 
261 #if YAJL_MAJOR >= 2
262 static int json_int(void *ctx, long long val) {
263  LOG("int %lld for key %s\n", val, last_key);
264 #else
265 static int json_int(void *ctx, long val) {
266  LOG("int %ld for key %s\n", val, last_key);
267 #endif
268  if (strcasecmp(last_key, "type") == 0)
269  json_node->type = val;
270 
271  if (strcasecmp(last_key, "fullscreen_mode") == 0)
272  json_node->fullscreen_mode = val;
273 
274  if (strcasecmp(last_key, "num") == 0)
275  json_node->num = val;
276 
277  if (strcasecmp(last_key, "current_border_width") == 0)
278  json_node->current_border_width = val;
279 
280  if (!parsing_swallows && strcasecmp(last_key, "id") == 0)
281  json_node->old_id = val;
282 
283  if (parsing_focus) {
284  struct focus_mapping *focus_mapping = scalloc(sizeof(struct focus_mapping));
285  focus_mapping->old_id = val;
286  TAILQ_INSERT_TAIL(&focus_mappings, focus_mapping, focus_mappings);
287  }
288 
290  Rect *r;
291  if (parsing_rect)
292  r = &(json_node->rect);
293  else if (parsing_window_rect)
294  r = &(json_node->window_rect);
295  else r = &(json_node->geometry);
296  if (strcasecmp(last_key, "x") == 0)
297  r->x = val;
298  else if (strcasecmp(last_key, "y") == 0)
299  r->y = val;
300  else if (strcasecmp(last_key, "width") == 0)
301  r->width = val;
302  else if (strcasecmp(last_key, "height") == 0)
303  r->height = val;
304  else printf("WARNING: unknown key %s in rect\n", last_key);
305  printf("rect now: (%d, %d, %d, %d)\n",
306  r->x, r->y, r->width, r->height);
307  }
308  if (parsing_swallows) {
309  if (strcasecmp(last_key, "id") == 0) {
310  current_swallow->id = val;
311  }
312  if (strcasecmp(last_key, "dock") == 0) {
313  current_swallow->dock = val;
314  }
315  if (strcasecmp(last_key, "insert_where") == 0) {
316  current_swallow->insert_where = val;
317  }
318  }
319 
320  return 1;
321 }
322 
323 static int json_bool(void *ctx, int val) {
324  LOG("bool %d for key %s\n", val, last_key);
325  if (strcasecmp(last_key, "focused") == 0 && val) {
326  to_focus = json_node;
327  }
328 
329  if (parsing_swallows) {
330  if (strcasecmp(last_key, "restart_mode") == 0)
331  current_swallow->restart_mode = val;
332  }
333 
334  return 1;
335 }
336 
337 static int json_double(void *ctx, double val) {
338  LOG("double %f for key %s\n", val, last_key);
339  if (strcasecmp(last_key, "percent") == 0) {
340  json_node->percent = val;
341  }
342  return 1;
343 }
344 
345 void tree_append_json(const char *filename) {
346  /* TODO: percent of other windows are not correctly fixed at the moment */
347  FILE *f;
348  if ((f = fopen(filename, "r")) == NULL) {
349  LOG("Cannot open file \"%s\"\n", filename);
350  return;
351  }
352  struct stat stbuf;
353  if (fstat(fileno(f), &stbuf) != 0) {
354  LOG("Cannot fstat() the file\n");
355  fclose(f);
356  return;
357  }
358  char *buf = smalloc(stbuf.st_size);
359  int n = fread(buf, 1, stbuf.st_size, f);
360  if (n != stbuf.st_size) {
361  LOG("File \"%s\" could not be read entirely, not loading.\n", filename);
362  fclose(f);
363  return;
364  }
365  LOG("read %d bytes\n", n);
366  yajl_gen g;
367  yajl_handle hand;
368  yajl_callbacks callbacks;
369  memset(&callbacks, '\0', sizeof(yajl_callbacks));
370  callbacks.yajl_start_map = json_start_map;
371  callbacks.yajl_end_map = json_end_map;
372  callbacks.yajl_end_array = json_end_array;
373  callbacks.yajl_string = json_string;
374  callbacks.yajl_map_key = json_key;
375  callbacks.yajl_integer = json_int;
376  callbacks.yajl_double = json_double;
377  callbacks.yajl_boolean = json_bool;
378 #if YAJL_MAJOR >= 2
379  g = yajl_gen_alloc(NULL);
380  hand = yajl_alloc(&callbacks, NULL, (void*)g);
381 #else
382  g = yajl_gen_alloc(NULL, NULL);
383  hand = yajl_alloc(&callbacks, NULL, NULL, (void*)g);
384 #endif
385  yajl_status stat;
386  json_node = focused;
387  to_focus = NULL;
388  parsing_rect = false;
389  parsing_window_rect = false;
390  parsing_geometry = false;
391  setlocale(LC_NUMERIC, "C");
392  stat = yajl_parse(hand, (const unsigned char*)buf, n);
393  if (stat != yajl_status_ok)
394  {
395  unsigned char * str = yajl_get_error(hand, 1, (const unsigned char*)buf, n);
396  fprintf(stderr, "%s\n", (const char *) str);
397  yajl_free_error(hand, str);
398  }
399 
400  setlocale(LC_NUMERIC, "");
401 #if YAJL_MAJOR >= 2
402  yajl_complete_parse(hand);
403 #else
404  yajl_parse_complete(hand);
405 #endif
406 
407  fclose(f);
408  if (to_focus)
409  con_focus(to_focus);
410 }