]> git.sur5r.net Git - i3/i3/blob - src/load_layout.c
Introduce splith/splitv layouts, remove orientation
[i3/i3] / src / load_layout.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * load_layout.c: Restore (parts of) the layout, for example after an inplace
8  *                restart.
9  *
10  */
11 #include "all.h"
12
13 #include <yajl/yajl_common.h>
14 #include <yajl/yajl_gen.h>
15 #include <yajl/yajl_parse.h>
16 #include <yajl/yajl_version.h>
17
18 /* TODO: refactor the whole parsing thing */
19
20 static char *last_key;
21 static Con *json_node;
22 static Con *to_focus;
23 static bool parsing_swallows;
24 static bool parsing_rect;
25 static bool parsing_window_rect;
26 static bool parsing_geometry;
27 static bool parsing_focus;
28 struct Match *current_swallow;
29
30 /* This list is used for reordering the focus stack after parsing the 'focus'
31  * array. */
32 struct focus_mapping {
33     int old_id;
34     TAILQ_ENTRY(focus_mapping) focus_mappings;
35 };
36
37 static TAILQ_HEAD(focus_mappings_head, focus_mapping) focus_mappings =
38   TAILQ_HEAD_INITIALIZER(focus_mappings);
39
40 static int json_start_map(void *ctx) {
41     LOG("start of map, last_key = %s\n", last_key);
42     if (parsing_swallows) {
43         LOG("creating new swallow\n");
44         current_swallow = smalloc(sizeof(Match));
45         match_init(current_swallow);
46         TAILQ_INSERT_TAIL(&(json_node->swallow_head), current_swallow, matches);
47     } else {
48         if (!parsing_rect && !parsing_window_rect && !parsing_geometry) {
49             if (last_key && strcasecmp(last_key, "floating_nodes") == 0) {
50                 DLOG("New floating_node\n");
51                 Con *ws = con_get_workspace(json_node);
52                 json_node = con_new(NULL, NULL);
53                 json_node->parent = ws;
54                 DLOG("Parent is workspace = %p\n", ws);
55             } else {
56                 Con *parent = json_node;
57                 json_node = con_new(NULL, NULL);
58                 json_node->parent = parent;
59             }
60         }
61     }
62     return 1;
63 }
64
65 static int json_end_map(void *ctx) {
66     LOG("end of map\n");
67     if (!parsing_swallows && !parsing_rect && !parsing_window_rect && !parsing_geometry) {
68         LOG("attaching\n");
69         con_attach(json_node, json_node->parent, true);
70         json_node = json_node->parent;
71     }
72     if (parsing_rect)
73         parsing_rect = false;
74     if (parsing_window_rect)
75         parsing_window_rect = false;
76     if (parsing_geometry)
77         parsing_geometry = false;
78     return 1;
79 }
80
81 static int json_end_array(void *ctx) {
82     LOG("end of array\n");
83     parsing_swallows = false;
84     if (parsing_focus) {
85         /* Clear the list of focus mappings */
86         struct focus_mapping *mapping;
87         TAILQ_FOREACH_REVERSE(mapping, &focus_mappings, focus_mappings_head, focus_mappings) {
88             LOG("focus (reverse) %d\n", mapping->old_id);
89             Con *con;
90             TAILQ_FOREACH(con, &(json_node->focus_head), focused) {
91                 if (con->old_id != mapping->old_id)
92                     continue;
93                 LOG("got it! %p\n", con);
94                 /* Move this entry to the top of the focus list. */
95                 TAILQ_REMOVE(&(json_node->focus_head), con, focused);
96                 TAILQ_INSERT_HEAD(&(json_node->focus_head), con, focused);
97                 break;
98             }
99         }
100         while (!TAILQ_EMPTY(&focus_mappings)) {
101             mapping = TAILQ_FIRST(&focus_mappings);
102             TAILQ_REMOVE(&focus_mappings, mapping, focus_mappings);
103             free(mapping);
104         }
105         parsing_focus = false;
106     }
107     return 1;
108 }
109
110 #if YAJL_MAJOR < 2
111 static int json_key(void *ctx, const unsigned char *val, unsigned int len) {
112 #else
113 static int json_key(void *ctx, const unsigned char *val, size_t len) {
114 #endif
115     LOG("key: %.*s\n", (int)len, val);
116     FREE(last_key);
117     last_key = scalloc((len+1) * sizeof(char));
118     memcpy(last_key, val, len);
119     if (strcasecmp(last_key, "swallows") == 0)
120         parsing_swallows = true;
121
122     if (strcasecmp(last_key, "rect") == 0)
123         parsing_rect = true;
124
125     if (strcasecmp(last_key, "window_rect") == 0)
126         parsing_window_rect = true;
127
128     if (strcasecmp(last_key, "geometry") == 0)
129         parsing_geometry = true;
130
131     if (strcasecmp(last_key, "focus") == 0)
132         parsing_focus = true;
133
134     return 1;
135 }
136
137 #if YAJL_MAJOR >= 2
138 static int json_string(void *ctx, const unsigned char *val, size_t len) {
139 #else
140 static int json_string(void *ctx, const unsigned char *val, unsigned int len) {
141 #endif
142     LOG("string: %.*s for key %s\n", len, val, last_key);
143     if (parsing_swallows) {
144         /* TODO: the other swallowing keys */
145         if (strcasecmp(last_key, "class") == 0) {
146             current_swallow->class = scalloc((len+1) * sizeof(char));
147             memcpy(current_swallow->class, val, len);
148         }
149         LOG("unhandled yet: swallow\n");
150     } else {
151         if (strcasecmp(last_key, "name") == 0) {
152             json_node->name = scalloc((len+1) * sizeof(char));
153             memcpy(json_node->name, val, len);
154         } else if (strcasecmp(last_key, "sticky_group") == 0) {
155             json_node->sticky_group = scalloc((len+1) * sizeof(char));
156             memcpy(json_node->sticky_group, val, len);
157             LOG("sticky_group of this container is %s\n", json_node->sticky_group);
158         } else if (strcasecmp(last_key, "orientation") == 0) {
159             /* Upgrade path from older versions of i3 (doing an inplace restart
160              * to a newer version):
161              * "orientation" is dumped before "layout". Therefore, we store
162              * whether the orientation was horizontal or vertical in the
163              * last_split_layout. When we then encounter layout == "default",
164              * we will use the last_split_layout as layout instead. */
165             char *buf = NULL;
166             sasprintf(&buf, "%.*s", (int)len, val);
167             if (strcasecmp(buf, "none") == 0 ||
168                 strcasecmp(buf, "horizontal") == 0)
169                 json_node->last_split_layout = L_SPLITH;
170             else if (strcasecmp(buf, "vertical") == 0)
171                 json_node->last_split_layout = L_SPLITV;
172             else LOG("Unhandled orientation: %s\n", buf);
173
174             /* What used to be an implicit check whether orientation !=
175              * NO_ORIENTATION is now a proper separate flag. */
176             if (strcasecmp(buf, "none") != 0)
177                 json_node->split = true;
178             free(buf);
179         } else if (strcasecmp(last_key, "border") == 0) {
180             char *buf = NULL;
181             sasprintf(&buf, "%.*s", (int)len, val);
182             if (strcasecmp(buf, "none") == 0)
183                 json_node->border_style = BS_NONE;
184             else if (strcasecmp(buf, "1pixel") == 0)
185                 json_node->border_style = BS_1PIXEL;
186             else if (strcasecmp(buf, "normal") == 0)
187                 json_node->border_style = BS_NORMAL;
188             else LOG("Unhandled \"border\": %s\n", buf);
189             free(buf);
190         } else if (strcasecmp(last_key, "layout") == 0) {
191             char *buf = NULL;
192             sasprintf(&buf, "%.*s", (int)len, val);
193             if (strcasecmp(buf, "default") == 0)
194                 /* This set above when we read "orientation". */
195                 json_node->layout = json_node->last_split_layout;
196             else if (strcasecmp(buf, "stacked") == 0)
197                 json_node->layout = L_STACKED;
198             else if (strcasecmp(buf, "tabbed") == 0)
199                 json_node->layout = L_TABBED;
200             else if (strcasecmp(buf, "dockarea") == 0) {
201                 json_node->layout = L_DOCKAREA;
202                 /* Necessary for migrating from older versions of i3. */
203                 json_node->split = false;
204             } else if (strcasecmp(buf, "output") == 0)
205                 json_node->layout = L_OUTPUT;
206             else if (strcasecmp(buf, "splith") == 0)
207                 json_node->layout = L_SPLITH;
208             else if (strcasecmp(buf, "splitv") == 0)
209                 json_node->layout = L_SPLITV;
210             else LOG("Unhandled \"layout\": %s\n", buf);
211             free(buf);
212         } else if (strcasecmp(last_key, "last_split_layout") == 0) {
213             char *buf = NULL;
214             sasprintf(&buf, "%.*s", (int)len, val);
215             if (strcasecmp(buf, "splith") == 0)
216                 json_node->last_split_layout = L_SPLITH;
217             else if (strcasecmp(buf, "splitv") == 0)
218                 json_node->last_split_layout = L_SPLITV;
219             else LOG("Unhandled \"last_splitlayout\": %s\n", buf);
220             free(buf);
221         } else if (strcasecmp(last_key, "mark") == 0) {
222             char *buf = NULL;
223             sasprintf(&buf, "%.*s", (int)len, val);
224             json_node->mark = buf;
225         } else if (strcasecmp(last_key, "floating") == 0) {
226             char *buf = NULL;
227             sasprintf(&buf, "%.*s", (int)len, val);
228             if (strcasecmp(buf, "auto_off") == 0)
229                 json_node->floating = FLOATING_AUTO_OFF;
230             else if (strcasecmp(buf, "auto_on") == 0)
231                 json_node->floating = FLOATING_AUTO_ON;
232             else if (strcasecmp(buf, "user_off") == 0)
233                 json_node->floating = FLOATING_USER_OFF;
234             else if (strcasecmp(buf, "user_on") == 0)
235                 json_node->floating = FLOATING_USER_ON;
236             free(buf);
237         } else if (strcasecmp(last_key, "scratchpad_state") == 0) {
238             char *buf = NULL;
239             sasprintf(&buf, "%.*s", (int)len, val);
240             if (strcasecmp(buf, "none") == 0)
241                 json_node->scratchpad_state = SCRATCHPAD_NONE;
242             else if (strcasecmp(buf, "fresh") == 0)
243                 json_node->scratchpad_state = SCRATCHPAD_FRESH;
244             else if (strcasecmp(buf, "changed") == 0)
245                 json_node->scratchpad_state = SCRATCHPAD_CHANGED;
246             free(buf);
247         }
248     }
249     return 1;
250 }
251
252 #if YAJL_MAJOR >= 2
253 static int json_int(void *ctx, long long val) {
254     LOG("int %lld for key %s\n", val, last_key);
255 #else
256 static int json_int(void *ctx, long val) {
257     LOG("int %ld for key %s\n", val, last_key);
258 #endif
259     if (strcasecmp(last_key, "type") == 0)
260         json_node->type = val;
261
262     if (strcasecmp(last_key, "fullscreen_mode") == 0)
263         json_node->fullscreen_mode = val;
264
265     if (strcasecmp(last_key, "num") == 0)
266         json_node->num = val;
267
268     if (!parsing_swallows && strcasecmp(last_key, "id") == 0)
269         json_node->old_id = val;
270
271     if (parsing_focus) {
272         struct focus_mapping *focus_mapping = scalloc(sizeof(struct focus_mapping));
273         focus_mapping->old_id = val;
274         TAILQ_INSERT_TAIL(&focus_mappings, focus_mapping, focus_mappings);
275     }
276
277     if (parsing_rect || parsing_window_rect || parsing_geometry) {
278         Rect *r;
279         if (parsing_rect)
280             r = &(json_node->rect);
281         else if (parsing_window_rect)
282             r = &(json_node->window_rect);
283         else r = &(json_node->geometry);
284         if (strcasecmp(last_key, "x") == 0)
285             r->x = val;
286         else if (strcasecmp(last_key, "y") == 0)
287             r->y = val;
288         else if (strcasecmp(last_key, "width") == 0)
289             r->width = val;
290         else if (strcasecmp(last_key, "height") == 0)
291             r->height = val;
292         else printf("WARNING: unknown key %s in rect\n", last_key);
293         printf("rect now: (%d, %d, %d, %d)\n",
294                 r->x, r->y, r->width, r->height);
295     }
296     if (parsing_swallows) {
297         if (strcasecmp(last_key, "id") == 0) {
298             current_swallow->id = val;
299         }
300         if (strcasecmp(last_key, "dock") == 0) {
301             current_swallow->dock = val;
302         }
303         if (strcasecmp(last_key, "insert_where") == 0) {
304             current_swallow->insert_where = val;
305         }
306     }
307
308     return 1;
309 }
310
311 static int json_bool(void *ctx, int val) {
312     LOG("bool %d for key %s\n", val, last_key);
313     if (strcasecmp(last_key, "focused") == 0 && val) {
314         to_focus = json_node;
315     }
316
317     if (strcasecmp(last_key, "split") == 0)
318         json_node->split = val;
319
320     if (parsing_swallows) {
321         if (strcasecmp(last_key, "restart_mode") == 0)
322             current_swallow->restart_mode = val;
323     }
324
325     return 1;
326 }
327
328 static int json_double(void *ctx, double val) {
329     LOG("double %f for key %s\n", val, last_key);
330     if (strcasecmp(last_key, "percent") == 0) {
331         json_node->percent = val;
332     }
333     return 1;
334 }
335
336 void tree_append_json(const char *filename) {
337     /* TODO: percent of other windows are not correctly fixed at the moment */
338     FILE *f;
339     if ((f = fopen(filename, "r")) == NULL) {
340         LOG("Cannot open file\n");
341         return;
342     }
343     char *buf = malloc(65535); /* TODO */
344     int n = fread(buf, 1, 65535, f);
345     LOG("read %d bytes\n", n);
346     yajl_gen g;
347     yajl_handle hand;
348     yajl_callbacks callbacks;
349     memset(&callbacks, '\0', sizeof(yajl_callbacks));
350     callbacks.yajl_start_map = json_start_map;
351     callbacks.yajl_end_map = json_end_map;
352     callbacks.yajl_end_array = json_end_array;
353     callbacks.yajl_string = json_string;
354     callbacks.yajl_map_key = json_key;
355     callbacks.yajl_integer = json_int;
356     callbacks.yajl_double = json_double;
357     callbacks.yajl_boolean = json_bool;
358 #if YAJL_MAJOR >= 2
359     g = yajl_gen_alloc(NULL);
360     hand = yajl_alloc(&callbacks, NULL, (void*)g);
361 #else
362     g = yajl_gen_alloc(NULL, NULL);
363     hand = yajl_alloc(&callbacks, NULL, NULL, (void*)g);
364 #endif
365     yajl_status stat;
366     json_node = focused;
367     to_focus = NULL;
368     parsing_rect = false;
369     parsing_window_rect = false;
370     parsing_geometry = false;
371     setlocale(LC_NUMERIC, "C");
372     stat = yajl_parse(hand, (const unsigned char*)buf, n);
373     if (stat != yajl_status_ok)
374     {
375         unsigned char * str = yajl_get_error(hand, 1, (const unsigned char*)buf, n);
376         fprintf(stderr, "%s\n", (const char *) str);
377         yajl_free_error(hand, str);
378     }
379
380     setlocale(LC_NUMERIC, "");
381 #if YAJL_MAJOR >= 2
382     yajl_complete_parse(hand);
383 #else
384     yajl_parse_complete(hand);
385 #endif
386
387     fclose(f);
388     if (to_focus)
389         con_focus(to_focus);
390 }