]> git.sur5r.net Git - i3/i3/blob - src/load_layout.c
Merge branch 'fix-ws-con'
[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 static int content_level;
431
432 static int json_determine_content_deeper(void *ctx) {
433     content_level++;
434     return 1;
435 }
436
437 static int json_determine_content_shallower(void *ctx) {
438     content_level--;
439     return 1;
440 }
441
442 static int json_determine_content_string(void *ctx, const unsigned char *val, size_t len) {
443     if (strcasecmp(last_key, "type") != 0 || content_level > 1)
444         return 1;
445
446     DLOG("string = %.*s, last_key = %s\n", (int)len, val, last_key);
447     if (strncasecmp((const char *)val, "workspace", len) == 0)
448         content_result = JSON_CONTENT_WORKSPACE;
449     return 0;
450 }
451
452 /* Parses the given JSON file until it encounters the first “type” property to
453  * determine whether the file contains workspaces or regular containers, which
454  * is important to know when deciding where (and how) to append the contents.
455  * */
456 json_content_t json_determine_content(const char *filename) {
457     FILE *f;
458     if ((f = fopen(filename, "r")) == NULL) {
459         ELOG("Cannot open file \"%s\"\n", filename);
460         return JSON_CONTENT_UNKNOWN;
461     }
462     struct stat stbuf;
463     if (fstat(fileno(f), &stbuf) != 0) {
464         ELOG("Cannot fstat() \"%s\"\n", filename);
465         fclose(f);
466         return JSON_CONTENT_UNKNOWN;
467     }
468     char *buf = smalloc(stbuf.st_size);
469     int n = fread(buf, 1, stbuf.st_size, f);
470     if (n != stbuf.st_size) {
471         ELOG("File \"%s\" could not be read entirely, not loading.\n", filename);
472         fclose(f);
473         return JSON_CONTENT_UNKNOWN;
474     }
475     DLOG("read %d bytes\n", n);
476     // We default to JSON_CONTENT_CON because it is legal to not include
477     // “"type": "con"” in the JSON files for better readability.
478     content_result = JSON_CONTENT_CON;
479     content_level = 0;
480     yajl_gen g;
481     yajl_handle hand;
482     static yajl_callbacks callbacks = {
483         .yajl_string = json_determine_content_string,
484         .yajl_map_key = json_key,
485         .yajl_start_array = json_determine_content_deeper,
486         .yajl_start_map = json_determine_content_deeper,
487         .yajl_end_map = json_determine_content_shallower,
488         .yajl_end_array = json_determine_content_shallower,
489     };
490     g = yajl_gen_alloc(NULL);
491     hand = yajl_alloc(&callbacks, NULL, (void *)g);
492     /* Allowing comments allows for more user-friendly layout files. */
493     yajl_config(hand, yajl_allow_comments, true);
494     /* Allow multiple values, i.e. multiple nodes to attach */
495     yajl_config(hand, yajl_allow_multiple_values, true);
496     yajl_status stat;
497     setlocale(LC_NUMERIC, "C");
498     stat = yajl_parse(hand, (const unsigned char *)buf, n);
499     if (stat != yajl_status_ok && stat != yajl_status_client_canceled) {
500         unsigned char *str = yajl_get_error(hand, 1, (const unsigned char *)buf, n);
501         ELOG("JSON parsing error: %s\n", str);
502         yajl_free_error(hand, str);
503     }
504
505     setlocale(LC_NUMERIC, "");
506     yajl_complete_parse(hand);
507
508     fclose(f);
509
510     return content_result;
511 }
512
513 void tree_append_json(Con *con, const char *filename, char **errormsg) {
514     FILE *f;
515     if ((f = fopen(filename, "r")) == NULL) {
516         ELOG("Cannot open file \"%s\"\n", filename);
517         return;
518     }
519     struct stat stbuf;
520     if (fstat(fileno(f), &stbuf) != 0) {
521         ELOG("Cannot fstat() \"%s\"\n", filename);
522         fclose(f);
523         return;
524     }
525     char *buf = smalloc(stbuf.st_size);
526     int n = fread(buf, 1, stbuf.st_size, f);
527     if (n != stbuf.st_size) {
528         ELOG("File \"%s\" could not be read entirely, not loading.\n", filename);
529         fclose(f);
530         return;
531     }
532     DLOG("read %d bytes\n", n);
533     yajl_gen g;
534     yajl_handle hand;
535     static yajl_callbacks callbacks = {
536         .yajl_boolean = json_bool,
537         .yajl_integer = json_int,
538         .yajl_double = json_double,
539         .yajl_string = json_string,
540         .yajl_start_map = json_start_map,
541         .yajl_map_key = json_key,
542         .yajl_end_map = json_end_map,
543         .yajl_end_array = json_end_array,
544     };
545     g = yajl_gen_alloc(NULL);
546     hand = yajl_alloc(&callbacks, NULL, (void *)g);
547     /* Allowing comments allows for more user-friendly layout files. */
548     yajl_config(hand, yajl_allow_comments, true);
549     /* Allow multiple values, i.e. multiple nodes to attach */
550     yajl_config(hand, yajl_allow_multiple_values, true);
551     yajl_status stat;
552     json_node = con;
553     to_focus = NULL;
554     parsing_swallows = false;
555     parsing_rect = false;
556     parsing_window_rect = false;
557     parsing_geometry = false;
558     parsing_focus = false;
559     setlocale(LC_NUMERIC, "C");
560     stat = yajl_parse(hand, (const unsigned char *)buf, n);
561     if (stat != yajl_status_ok) {
562         unsigned char *str = yajl_get_error(hand, 1, (const unsigned char *)buf, n);
563         ELOG("JSON parsing error: %s\n", str);
564         if (errormsg != NULL)
565             *errormsg = sstrdup((const char *)str);
566         yajl_free_error(hand, str);
567     }
568
569     /* In case not all containers were restored, we need to fix the
570      * percentages, otherwise i3 will crash immediately when rendering the
571      * next time. */
572     con_fix_percent(con);
573
574     setlocale(LC_NUMERIC, "");
575     yajl_complete_parse(hand);
576
577     fclose(f);
578     if (to_focus)
579         con_focus(to_focus);
580 }