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