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