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