]> git.sur5r.net Git - i3/i3/blob - src/load_layout.c
Merge branch 'master' into next
[i3/i3] / src / load_layout.c
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;
30 struct Match *current_swallow;
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 {
50         if (!parsing_rect && !parsing_window_rect && !parsing_geometry) {
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_skeleton(NULL, NULL);
55                 json_node->name = NULL;
56                 json_node->parent = ws;
57                 DLOG("Parent is workspace = %p\n", ws);
58             } else {
59                 Con *parent = json_node;
60                 json_node = con_new_skeleton(NULL, NULL);
61                 json_node->name = NULL;
62                 json_node->parent = parent;
63             }
64         }
65     }
66     return 1;
67 }
68
69 static int json_end_map(void *ctx) {
70     LOG("end of map\n");
71     if (!parsing_swallows && !parsing_rect && !parsing_window_rect && !parsing_geometry) {
72         /* Set a few default values to simplify manually crafted layout files. */
73         if (json_node->layout == L_DEFAULT) {
74             DLOG("Setting layout = L_SPLITH\n");
75             json_node->layout = L_SPLITH;
76         }
77
78         /* Sanity check: swallow criteria don’t make any sense on a split
79          * container. */
80         if (con_is_split(json_node) > 0 && !TAILQ_EMPTY(&(json_node->swallow_head))) {
81             DLOG("sanity check: removing swallows specification from split container\n");
82             while (!TAILQ_EMPTY(&(json_node->swallow_head))) {
83                 Match *match = TAILQ_FIRST(&(json_node->swallow_head));
84                 TAILQ_REMOVE(&(json_node->swallow_head), match, matches);
85                 match_free(match);
86             }
87         }
88
89         if (json_node->type == CT_WORKSPACE) {
90             /* Ensure the workspace has a name. */
91             DLOG("Attaching workspace. name = %s\n", json_node->name);
92             if (json_node->name == NULL || strcmp(json_node->name, "") == 0) {
93                 json_node->name = sstrdup("unnamed");
94             }
95
96             /* Prevent name clashes when appending a workspace, e.g. when the
97              * user tries to restore a workspace called “1” but already has a
98              * workspace called “1”. */
99             Con *output;
100             Con *workspace = NULL;
101             TAILQ_FOREACH (output, &(croot->nodes_head), nodes)
102                 GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, json_node->name));
103             char *base = sstrdup(json_node->name);
104             int cnt = 1;
105             while (workspace != NULL) {
106                 FREE(json_node->name);
107                 asprintf(&(json_node->name), "%s_%d", base, cnt++);
108                 workspace = NULL;
109                 TAILQ_FOREACH (output, &(croot->nodes_head), nodes)
110                     GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, json_node->name));
111             }
112             free(base);
113
114             /* Set num accordingly so that i3bar will properly sort it. */
115             json_node->num = ws_name_to_number(json_node->name);
116         } else {
117             // TODO: remove this in the “next” branch.
118             if (json_node->name == NULL || strcmp(json_node->name, "") == 0) {
119                 json_node->name = sstrdup("#ff0000");
120             }
121         }
122
123         LOG("attaching\n");
124         con_attach(json_node, json_node->parent, true);
125         LOG("Creating window\n");
126         x_con_init(json_node, json_node->depth);
127         json_node = json_node->parent;
128     }
129     if (parsing_rect)
130         parsing_rect = false;
131     if (parsing_window_rect)
132         parsing_window_rect = false;
133     if (parsing_geometry)
134         parsing_geometry = false;
135     return 1;
136 }
137
138 static int json_end_array(void *ctx) {
139     LOG("end of array\n");
140     if (!parsing_swallows && !parsing_focus) {
141         con_fix_percent(json_node);
142     }
143     if (parsing_swallows) {
144         parsing_swallows = false;
145     }
146     if (parsing_focus) {
147         /* Clear the list of focus mappings */
148         struct focus_mapping *mapping;
149         TAILQ_FOREACH_REVERSE(mapping, &focus_mappings, focus_mappings_head, focus_mappings) {
150             LOG("focus (reverse) %d\n", mapping->old_id);
151             Con *con;
152             TAILQ_FOREACH(con, &(json_node->focus_head), focused) {
153                 if (con->old_id != mapping->old_id)
154                     continue;
155                 LOG("got it! %p\n", con);
156                 /* Move this entry to the top of the focus list. */
157                 TAILQ_REMOVE(&(json_node->focus_head), con, focused);
158                 TAILQ_INSERT_HEAD(&(json_node->focus_head), con, focused);
159                 break;
160             }
161         }
162         while (!TAILQ_EMPTY(&focus_mappings)) {
163             mapping = TAILQ_FIRST(&focus_mappings);
164             TAILQ_REMOVE(&focus_mappings, mapping, focus_mappings);
165             free(mapping);
166         }
167         parsing_focus = false;
168     }
169     return 1;
170 }
171
172 static int json_key(void *ctx, const unsigned char *val, size_t len) {
173     LOG("key: %.*s\n", (int)len, val);
174     FREE(last_key);
175     last_key = scalloc((len + 1) * sizeof(char));
176     memcpy(last_key, val, len);
177     if (strcasecmp(last_key, "swallows") == 0)
178         parsing_swallows = true;
179
180     if (strcasecmp(last_key, "rect") == 0)
181         parsing_rect = true;
182
183     if (strcasecmp(last_key, "window_rect") == 0)
184         parsing_window_rect = true;
185
186     if (strcasecmp(last_key, "geometry") == 0)
187         parsing_geometry = true;
188
189     if (strcasecmp(last_key, "focus") == 0)
190         parsing_focus = true;
191
192     return 1;
193 }
194
195 static int json_string(void *ctx, const unsigned char *val, size_t len) {
196     LOG("string: %.*s for key %s\n", (int)len, val, last_key);
197     if (parsing_swallows) {
198         char *sval;
199         sasprintf(&sval, "%.*s", len, val);
200         if (strcasecmp(last_key, "class") == 0) {
201             current_swallow->class = regex_new(sval);
202         } else if (strcasecmp(last_key, "instance") == 0) {
203             current_swallow->instance = regex_new(sval);
204         } else if (strcasecmp(last_key, "window_role") == 0) {
205             current_swallow->window_role = regex_new(sval);
206         } else if (strcasecmp(last_key, "title") == 0) {
207             current_swallow->title = regex_new(sval);
208         } else {
209             ELOG("swallow key %s unknown\n", last_key);
210         }
211         free(sval);
212     } else {
213         if (strcasecmp(last_key, "name") == 0) {
214             json_node->name = scalloc((len + 1) * sizeof(char));
215             memcpy(json_node->name, val, len);
216         } else if (strcasecmp(last_key, "sticky_group") == 0) {
217             json_node->sticky_group = scalloc((len + 1) * sizeof(char));
218             memcpy(json_node->sticky_group, val, len);
219             LOG("sticky_group of this container is %s\n", json_node->sticky_group);
220         } else if (strcasecmp(last_key, "orientation") == 0) {
221             /* Upgrade path from older versions of i3 (doing an inplace restart
222              * to a newer version):
223              * "orientation" is dumped before "layout". Therefore, we store
224              * whether the orientation was horizontal or vertical in the
225              * last_split_layout. When we then encounter layout == "default",
226              * we will use the last_split_layout as layout instead. */
227             char *buf = NULL;
228             sasprintf(&buf, "%.*s", (int)len, val);
229             if (strcasecmp(buf, "none") == 0 ||
230                 strcasecmp(buf, "horizontal") == 0)
231                 json_node->last_split_layout = L_SPLITH;
232             else if (strcasecmp(buf, "vertical") == 0)
233                 json_node->last_split_layout = L_SPLITV;
234             else
235                 LOG("Unhandled orientation: %s\n", buf);
236             free(buf);
237         } else if (strcasecmp(last_key, "border") == 0) {
238             char *buf = NULL;
239             sasprintf(&buf, "%.*s", (int)len, val);
240             if (strcasecmp(buf, "none") == 0)
241                 json_node->border_style = BS_NONE;
242             else if (strcasecmp(buf, "1pixel") == 0) {
243                 json_node->border_style = BS_PIXEL;
244                 json_node->current_border_width = 1;
245             } else if (strcasecmp(buf, "pixel") == 0)
246                 json_node->border_style = BS_PIXEL;
247             else if (strcasecmp(buf, "normal") == 0)
248                 json_node->border_style = BS_NORMAL;
249             else
250                 LOG("Unhandled \"border\": %s\n", buf);
251             free(buf);
252         } else if (strcasecmp(last_key, "type") == 0) {
253             char *buf = NULL;
254             sasprintf(&buf, "%.*s", (int)len, val);
255             if (strcasecmp(buf, "root") == 0)
256                 json_node->type = CT_ROOT;
257             else if (strcasecmp(buf, "output") == 0)
258                 json_node->type = CT_OUTPUT;
259             else if (strcasecmp(buf, "con") == 0)
260                 json_node->type = CT_CON;
261             else if (strcasecmp(buf, "floating_con") == 0)
262                 json_node->type = CT_FLOATING_CON;
263             else if (strcasecmp(buf, "workspace") == 0)
264                 json_node->type = CT_WORKSPACE;
265             else if (strcasecmp(buf, "dockarea") == 0)
266                 json_node->type = CT_DOCKAREA;
267             else
268                 LOG("Unhandled \"type\": %s\n", buf);
269             free(buf);
270         } else if (strcasecmp(last_key, "layout") == 0) {
271             char *buf = NULL;
272             sasprintf(&buf, "%.*s", (int)len, val);
273             if (strcasecmp(buf, "default") == 0)
274                 /* This set above when we read "orientation". */
275                 json_node->layout = json_node->last_split_layout;
276             else if (strcasecmp(buf, "stacked") == 0)
277                 json_node->layout = L_STACKED;
278             else if (strcasecmp(buf, "tabbed") == 0)
279                 json_node->layout = L_TABBED;
280             else if (strcasecmp(buf, "dockarea") == 0)
281                 json_node->layout = L_DOCKAREA;
282             else if (strcasecmp(buf, "output") == 0)
283                 json_node->layout = L_OUTPUT;
284             else if (strcasecmp(buf, "splith") == 0)
285                 json_node->layout = L_SPLITH;
286             else if (strcasecmp(buf, "splitv") == 0)
287                 json_node->layout = L_SPLITV;
288             else
289                 LOG("Unhandled \"layout\": %s\n", buf);
290             free(buf);
291         } else if (strcasecmp(last_key, "workspace_layout") == 0) {
292             char *buf = NULL;
293             sasprintf(&buf, "%.*s", (int)len, val);
294             if (strcasecmp(buf, "default") == 0)
295                 json_node->workspace_layout = L_DEFAULT;
296             else if (strcasecmp(buf, "stacked") == 0)
297                 json_node->workspace_layout = L_STACKED;
298             else if (strcasecmp(buf, "tabbed") == 0)
299                 json_node->workspace_layout = L_TABBED;
300             else
301                 LOG("Unhandled \"workspace_layout\": %s\n", buf);
302             free(buf);
303         } else if (strcasecmp(last_key, "last_split_layout") == 0) {
304             char *buf = NULL;
305             sasprintf(&buf, "%.*s", (int)len, val);
306             if (strcasecmp(buf, "splith") == 0)
307                 json_node->last_split_layout = L_SPLITH;
308             else if (strcasecmp(buf, "splitv") == 0)
309                 json_node->last_split_layout = L_SPLITV;
310             else
311                 LOG("Unhandled \"last_splitlayout\": %s\n", buf);
312             free(buf);
313         } else if (strcasecmp(last_key, "mark") == 0) {
314             char *buf = NULL;
315             sasprintf(&buf, "%.*s", (int)len, val);
316             json_node->mark = buf;
317         } else if (strcasecmp(last_key, "floating") == 0) {
318             char *buf = NULL;
319             sasprintf(&buf, "%.*s", (int)len, val);
320             if (strcasecmp(buf, "auto_off") == 0)
321                 json_node->floating = FLOATING_AUTO_OFF;
322             else if (strcasecmp(buf, "auto_on") == 0)
323                 json_node->floating = FLOATING_AUTO_ON;
324             else if (strcasecmp(buf, "user_off") == 0)
325                 json_node->floating = FLOATING_USER_OFF;
326             else if (strcasecmp(buf, "user_on") == 0)
327                 json_node->floating = FLOATING_USER_ON;
328             free(buf);
329         } else if (strcasecmp(last_key, "scratchpad_state") == 0) {
330             char *buf = NULL;
331             sasprintf(&buf, "%.*s", (int)len, val);
332             if (strcasecmp(buf, "none") == 0)
333                 json_node->scratchpad_state = SCRATCHPAD_NONE;
334             else if (strcasecmp(buf, "fresh") == 0)
335                 json_node->scratchpad_state = SCRATCHPAD_FRESH;
336             else if (strcasecmp(buf, "changed") == 0)
337                 json_node->scratchpad_state = SCRATCHPAD_CHANGED;
338             free(buf);
339         }
340     }
341     return 1;
342 }
343
344 static int json_int(void *ctx, long long val) {
345     LOG("int %lld for key %s\n", val, last_key);
346     /* For backwards compatibility with i3 < 4.8 */
347     if (strcasecmp(last_key, "type") == 0)
348         json_node->type = val;
349
350     if (strcasecmp(last_key, "fullscreen_mode") == 0)
351         json_node->fullscreen_mode = val;
352
353     if (strcasecmp(last_key, "num") == 0)
354         json_node->num = val;
355
356     if (strcasecmp(last_key, "current_border_width") == 0)
357         json_node->current_border_width = val;
358
359     if (strcasecmp(last_key, "depth") == 0)
360         json_node->depth = val;
361
362     if (!parsing_swallows && strcasecmp(last_key, "id") == 0)
363         json_node->old_id = val;
364
365     if (parsing_focus) {
366         struct focus_mapping *focus_mapping = scalloc(sizeof(struct focus_mapping));
367         focus_mapping->old_id = val;
368         TAILQ_INSERT_TAIL(&focus_mappings, focus_mapping, focus_mappings);
369     }
370
371     if (parsing_rect || parsing_window_rect || parsing_geometry) {
372         Rect *r;
373         if (parsing_rect)
374             r = &(json_node->rect);
375         else if (parsing_window_rect)
376             r = &(json_node->window_rect);
377         else
378             r = &(json_node->geometry);
379         if (strcasecmp(last_key, "x") == 0)
380             r->x = val;
381         else if (strcasecmp(last_key, "y") == 0)
382             r->y = val;
383         else if (strcasecmp(last_key, "width") == 0)
384             r->width = val;
385         else if (strcasecmp(last_key, "height") == 0)
386             r->height = val;
387         else
388             ELOG("WARNING: unknown key %s in rect\n", last_key);
389         DLOG("rect now: (%d, %d, %d, %d)\n",
390              r->x, r->y, r->width, r->height);
391     }
392     if (parsing_swallows) {
393         if (strcasecmp(last_key, "id") == 0) {
394             current_swallow->id = val;
395         }
396         if (strcasecmp(last_key, "dock") == 0) {
397             current_swallow->dock = val;
398         }
399         if (strcasecmp(last_key, "insert_where") == 0) {
400             current_swallow->insert_where = val;
401         }
402     }
403
404     return 1;
405 }
406
407 static int json_bool(void *ctx, int val) {
408     LOG("bool %d for key %s\n", val, last_key);
409     if (strcasecmp(last_key, "focused") == 0 && val) {
410         to_focus = json_node;
411     }
412
413     if (parsing_swallows) {
414         if (strcasecmp(last_key, "restart_mode") == 0)
415             current_swallow->restart_mode = val;
416     }
417
418     return 1;
419 }
420
421 static int json_double(void *ctx, double val) {
422     LOG("double %f for key %s\n", val, last_key);
423     if (strcasecmp(last_key, "percent") == 0) {
424         json_node->percent = val;
425     }
426     return 1;
427 }
428
429 static json_content_t content_result;
430
431 static int json_determine_content_string(void *ctx, const unsigned char *val, size_t len) {
432     if (strcasecmp(last_key, "type") != 0)
433         return 1;
434
435     DLOG("string = %.*s, last_key = %s\n", (int)len, val, last_key);
436     if (strncasecmp((const char *)val, "workspace", len) == 0)
437         content_result = JSON_CONTENT_WORKSPACE;
438     return 0;
439 }
440
441 /* Parses the given JSON file until it encounters the first “type” property to
442  * determine whether the file contains workspaces or regular containers, which
443  * is important to know when deciding where (and how) to append the contents.
444  * */
445 json_content_t json_determine_content(const char *filename) {
446     FILE *f;
447     if ((f = fopen(filename, "r")) == NULL) {
448         ELOG("Cannot open file \"%s\"\n", filename);
449         return JSON_CONTENT_UNKNOWN;
450     }
451     struct stat stbuf;
452     if (fstat(fileno(f), &stbuf) != 0) {
453         ELOG("Cannot fstat() \"%s\"\n", filename);
454         fclose(f);
455         return JSON_CONTENT_UNKNOWN;
456     }
457     char *buf = smalloc(stbuf.st_size);
458     int n = fread(buf, 1, stbuf.st_size, f);
459     if (n != stbuf.st_size) {
460         ELOG("File \"%s\" could not be read entirely, not loading.\n", filename);
461         fclose(f);
462         return JSON_CONTENT_UNKNOWN;
463     }
464     DLOG("read %d bytes\n", n);
465     // We default to JSON_CONTENT_CON because it is legal to not include
466     // “"type": "con"” in the JSON files for better readability.
467     content_result = JSON_CONTENT_CON;
468     yajl_gen g;
469     yajl_handle hand;
470     static yajl_callbacks callbacks = {
471         .yajl_string = json_determine_content_string,
472         .yajl_map_key = json_key,
473     };
474     g = yajl_gen_alloc(NULL);
475     hand = yajl_alloc(&callbacks, NULL, (void *)g);
476     /* Allowing comments allows for more user-friendly layout files. */
477     yajl_config(hand, yajl_allow_comments, true);
478     /* Allow multiple values, i.e. multiple nodes to attach */
479     yajl_config(hand, yajl_allow_multiple_values, true);
480     yajl_status stat;
481     setlocale(LC_NUMERIC, "C");
482     stat = yajl_parse(hand, (const unsigned char *)buf, n);
483     if (stat != yajl_status_ok && stat != yajl_status_client_canceled) {
484         unsigned char *str = yajl_get_error(hand, 1, (const unsigned char *)buf, n);
485         ELOG("JSON parsing error: %s\n", str);
486         yajl_free_error(hand, str);
487     }
488
489     setlocale(LC_NUMERIC, "");
490     yajl_complete_parse(hand);
491
492     fclose(f);
493
494     return content_result;
495 }
496
497 void tree_append_json(Con *con, const char *filename, char **errormsg) {
498     FILE *f;
499     if ((f = fopen(filename, "r")) == NULL) {
500         ELOG("Cannot open file \"%s\"\n", filename);
501         return;
502     }
503     struct stat stbuf;
504     if (fstat(fileno(f), &stbuf) != 0) {
505         ELOG("Cannot fstat() \"%s\"\n", filename);
506         fclose(f);
507         return;
508     }
509     char *buf = smalloc(stbuf.st_size);
510     int n = fread(buf, 1, stbuf.st_size, f);
511     if (n != stbuf.st_size) {
512         ELOG("File \"%s\" could not be read entirely, not loading.\n", filename);
513         fclose(f);
514         return;
515     }
516     DLOG("read %d bytes\n", n);
517     yajl_gen g;
518     yajl_handle hand;
519     static yajl_callbacks callbacks = {
520         .yajl_boolean = json_bool,
521         .yajl_integer = json_int,
522         .yajl_double = json_double,
523         .yajl_string = json_string,
524         .yajl_start_map = json_start_map,
525         .yajl_map_key = json_key,
526         .yajl_end_map = json_end_map,
527         .yajl_end_array = json_end_array,
528     };
529     g = yajl_gen_alloc(NULL);
530     hand = yajl_alloc(&callbacks, NULL, (void *)g);
531     /* Allowing comments allows for more user-friendly layout files. */
532     yajl_config(hand, yajl_allow_comments, true);
533     /* Allow multiple values, i.e. multiple nodes to attach */
534     yajl_config(hand, yajl_allow_multiple_values, true);
535     yajl_status stat;
536     json_node = con;
537     to_focus = NULL;
538     parsing_swallows = false;
539     parsing_rect = false;
540     parsing_window_rect = false;
541     parsing_geometry = false;
542     parsing_focus = false;
543     setlocale(LC_NUMERIC, "C");
544     stat = yajl_parse(hand, (const unsigned char *)buf, n);
545     if (stat != yajl_status_ok) {
546         unsigned char *str = yajl_get_error(hand, 1, (const unsigned char *)buf, n);
547         ELOG("JSON parsing error: %s\n", str);
548         if (errormsg != NULL)
549             *errormsg = sstrdup((const char *)str);
550         yajl_free_error(hand, str);
551     }
552
553     /* In case not all containers were restored, we need to fix the
554      * percentages, otherwise i3 will crash immediately when rendering the
555      * next time. */
556     con_fix_percent(con);
557
558     setlocale(LC_NUMERIC, "");
559     yajl_complete_parse(hand);
560
561     fclose(f);
562     if (to_focus)
563         con_focus(to_focus);
564 }