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