]> git.sur5r.net Git - i3/i3/blob - src/load_layout.c
Merge branch 'master' into next
[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-2011 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 struct Match *current_swallow;
28
29 static int json_start_map(void *ctx) {
30     LOG("start of map, last_key = %s\n", last_key);
31     if (parsing_swallows) {
32         LOG("creating new swallow\n");
33         current_swallow = smalloc(sizeof(Match));
34         match_init(current_swallow);
35         TAILQ_INSERT_TAIL(&(json_node->swallow_head), current_swallow, matches);
36     } else {
37         if (!parsing_rect && !parsing_window_rect && !parsing_geometry) {
38             if (last_key && strcasecmp(last_key, "floating_nodes") == 0) {
39                 DLOG("New floating_node\n");
40                 Con *ws = con_get_workspace(json_node);
41                 json_node = con_new(NULL, NULL);
42                 json_node->parent = ws;
43                 DLOG("Parent is workspace = %p\n", ws);
44             } else {
45                 Con *parent = json_node;
46                 json_node = con_new(NULL, NULL);
47                 json_node->parent = parent;
48             }
49         }
50     }
51     return 1;
52 }
53
54 static int json_end_map(void *ctx) {
55     LOG("end of map\n");
56     if (!parsing_swallows && !parsing_rect && !parsing_window_rect && !parsing_geometry) {
57         LOG("attaching\n");
58         con_attach(json_node, json_node->parent, true);
59         json_node = json_node->parent;
60     }
61     if (parsing_rect)
62         parsing_rect = false;
63     if (parsing_window_rect)
64         parsing_window_rect = false;
65     if (parsing_geometry)
66         parsing_geometry = false;
67     return 1;
68 }
69
70 static int json_end_array(void *ctx) {
71     LOG("end of array\n");
72     parsing_swallows = false;
73     return 1;
74 }
75
76 #if YAJL_MAJOR < 2
77 static int json_key(void *ctx, const unsigned char *val, unsigned int len) {
78 #else
79 static int json_key(void *ctx, const unsigned char *val, size_t len) {
80 #endif
81     LOG("key: %.*s\n", (int)len, val);
82     FREE(last_key);
83     last_key = scalloc((len+1) * sizeof(char));
84     memcpy(last_key, val, len);
85     if (strcasecmp(last_key, "swallows") == 0) {
86         parsing_swallows = true;
87     }
88     if (strcasecmp(last_key, "rect") == 0)
89         parsing_rect = true;
90     if (strcasecmp(last_key, "window_rect") == 0)
91         parsing_window_rect = true;
92     if (strcasecmp(last_key, "geometry") == 0)
93         parsing_geometry = true;
94     return 1;
95 }
96
97 #if YAJL_MAJOR >= 2
98 static int json_string(void *ctx, const unsigned char *val, size_t len) {
99 #else
100 static int json_string(void *ctx, const unsigned char *val, unsigned int len) {
101 #endif
102     LOG("string: %.*s for key %s\n", len, val, last_key);
103     if (parsing_swallows) {
104         /* TODO: the other swallowing keys */
105         if (strcasecmp(last_key, "class") == 0) {
106             current_swallow->class = scalloc((len+1) * sizeof(char));
107             memcpy(current_swallow->class, val, len);
108         }
109         LOG("unhandled yet: swallow\n");
110     } else {
111         if (strcasecmp(last_key, "name") == 0) {
112             json_node->name = scalloc((len+1) * sizeof(char));
113             memcpy(json_node->name, val, len);
114         } else if (strcasecmp(last_key, "sticky_group") == 0) {
115             json_node->sticky_group = scalloc((len+1) * sizeof(char));
116             memcpy(json_node->sticky_group, val, len);
117             LOG("sticky_group of this container is %s\n", json_node->sticky_group);
118         } else if (strcasecmp(last_key, "orientation") == 0) {
119             char *buf = NULL;
120             sasprintf(&buf, "%.*s", (int)len, val);
121             if (strcasecmp(buf, "none") == 0)
122                 json_node->orientation = NO_ORIENTATION;
123             else if (strcasecmp(buf, "horizontal") == 0)
124                 json_node->orientation = HORIZ;
125             else if (strcasecmp(buf, "vertical") == 0)
126                 json_node->orientation = VERT;
127             else LOG("Unhandled orientation: %s\n", buf);
128             free(buf);
129         } else if (strcasecmp(last_key, "border") == 0) {
130             char *buf = NULL;
131             sasprintf(&buf, "%.*s", (int)len, val);
132             if (strcasecmp(buf, "none") == 0)
133                 json_node->border_style = BS_NONE;
134             else if (strcasecmp(buf, "1pixel") == 0)
135                 json_node->border_style = BS_1PIXEL;
136             else if (strcasecmp(buf, "normal") == 0)
137                 json_node->border_style = BS_NORMAL;
138             else LOG("Unhandled \"border\": %s\n", buf);
139             free(buf);
140         } else if (strcasecmp(last_key, "layout") == 0) {
141             char *buf = NULL;
142             sasprintf(&buf, "%.*s", (int)len, val);
143             if (strcasecmp(buf, "default") == 0)
144                 json_node->layout = L_DEFAULT;
145             else if (strcasecmp(buf, "stacked") == 0)
146                 json_node->layout = L_STACKED;
147             else if (strcasecmp(buf, "tabbed") == 0)
148                 json_node->layout = L_TABBED;
149             else if (strcasecmp(buf, "dockarea") == 0)
150                 json_node->layout = L_DOCKAREA;
151             else if (strcasecmp(buf, "output") == 0)
152                 json_node->layout = L_OUTPUT;
153             else LOG("Unhandled \"layout\": %s\n", buf);
154             free(buf);
155         } else if (strcasecmp(last_key, "mark") == 0) {
156             char *buf = NULL;
157             sasprintf(&buf, "%.*s", (int)len, val);
158             json_node->mark = buf;
159         } else if (strcasecmp(last_key, "floating") == 0) {
160             char *buf = NULL;
161             sasprintf(&buf, "%.*s", (int)len, val);
162             if (strcasecmp(buf, "auto_off") == 0)
163                 json_node->floating = FLOATING_AUTO_OFF;
164             else if (strcasecmp(buf, "auto_on") == 0)
165                 json_node->floating = FLOATING_AUTO_ON;
166             else if (strcasecmp(buf, "user_off") == 0)
167                 json_node->floating = FLOATING_USER_OFF;
168             else if (strcasecmp(buf, "user_on") == 0)
169                 json_node->floating = FLOATING_USER_ON;
170             free(buf);
171         } else if (strcasecmp(last_key, "scratchpad_state") == 0) {
172             char *buf = NULL;
173             sasprintf(&buf, "%.*s", (int)len, val);
174             if (strcasecmp(buf, "none") == 0)
175                 json_node->scratchpad_state = SCRATCHPAD_NONE;
176             else if (strcasecmp(buf, "fresh") == 0)
177                 json_node->scratchpad_state = SCRATCHPAD_FRESH;
178             else if (strcasecmp(buf, "changed") == 0)
179                 json_node->scratchpad_state = SCRATCHPAD_CHANGED;
180             free(buf);
181         }
182     }
183     return 1;
184 }
185
186 #if YAJL_MAJOR >= 2
187 static int json_int(void *ctx, long long val) {
188     LOG("int %lld for key %s\n", val, last_key);
189 #else
190 static int json_int(void *ctx, long val) {
191     LOG("int %ld for key %s\n", val, last_key);
192 #endif
193     if (strcasecmp(last_key, "type") == 0) {
194         json_node->type = val;
195     }
196     if (strcasecmp(last_key, "fullscreen_mode") == 0) {
197         json_node->fullscreen_mode = val;
198     }
199     if (strcasecmp(last_key, "num") == 0)
200         json_node->num = val;
201
202     if (parsing_rect || parsing_window_rect || parsing_geometry) {
203         Rect *r;
204         if (parsing_rect)
205             r = &(json_node->rect);
206         else if (parsing_window_rect)
207             r = &(json_node->window_rect);
208         else r = &(json_node->geometry);
209         if (strcasecmp(last_key, "x") == 0)
210             r->x = val;
211         else if (strcasecmp(last_key, "y") == 0)
212             r->y = val;
213         else if (strcasecmp(last_key, "width") == 0)
214             r->width = val;
215         else if (strcasecmp(last_key, "height") == 0)
216             r->height = val;
217         else printf("WARNING: unknown key %s in rect\n", last_key);
218         printf("rect now: (%d, %d, %d, %d)\n",
219                 r->x, r->y, r->width, r->height);
220     }
221     if (parsing_swallows) {
222         if (strcasecmp(last_key, "id") == 0) {
223             current_swallow->id = val;
224         }
225         if (strcasecmp(last_key, "dock") == 0) {
226             current_swallow->dock = val;
227         }
228         if (strcasecmp(last_key, "insert_where") == 0) {
229             current_swallow->insert_where = val;
230         }
231     }
232
233     return 1;
234 }
235
236 static int json_bool(void *ctx, int val) {
237     LOG("bool %d for key %s\n", val, last_key);
238     if (strcasecmp(last_key, "focused") == 0 && val) {
239         to_focus = json_node;
240     }
241
242     return 1;
243 }
244
245 static int json_double(void *ctx, double val) {
246     LOG("double %f for key %s\n", val, last_key);
247     if (strcasecmp(last_key, "percent") == 0) {
248         json_node->percent = val;
249     }
250     return 1;
251 }
252
253 void tree_append_json(const char *filename) {
254     /* TODO: percent of other windows are not correctly fixed at the moment */
255     FILE *f;
256     if ((f = fopen(filename, "r")) == NULL) {
257         LOG("Cannot open file\n");
258         return;
259     }
260     char *buf = malloc(65535); /* TODO */
261     int n = fread(buf, 1, 65535, f);
262     LOG("read %d bytes\n", n);
263     yajl_gen g;
264     yajl_handle hand;
265     yajl_callbacks callbacks;
266     memset(&callbacks, '\0', sizeof(yajl_callbacks));
267     callbacks.yajl_start_map = json_start_map;
268     callbacks.yajl_end_map = json_end_map;
269     callbacks.yajl_end_array = json_end_array;
270     callbacks.yajl_string = json_string;
271     callbacks.yajl_map_key = json_key;
272     callbacks.yajl_integer = json_int;
273     callbacks.yajl_double = json_double;
274     callbacks.yajl_boolean = json_bool;
275 #if YAJL_MAJOR >= 2
276     g = yajl_gen_alloc(NULL);
277     hand = yajl_alloc(&callbacks, NULL, (void*)g);
278 #else
279     g = yajl_gen_alloc(NULL, NULL);
280     hand = yajl_alloc(&callbacks, NULL, NULL, (void*)g);
281 #endif
282     yajl_status stat;
283     json_node = focused;
284     to_focus = NULL;
285     parsing_rect = false;
286     parsing_window_rect = false;
287     parsing_geometry = false;
288     setlocale(LC_NUMERIC, "C");
289     stat = yajl_parse(hand, (const unsigned char*)buf, n);
290     if (stat != yajl_status_ok)
291     {
292         unsigned char * str = yajl_get_error(hand, 1, (const unsigned char*)buf, n);
293         fprintf(stderr, "%s\n", (const char *) str);
294         yajl_free_error(hand, str);
295     }
296
297     setlocale(LC_NUMERIC, "");
298 #if YAJL_MAJOR >= 2
299     yajl_complete_parse(hand);
300 #else
301     yajl_parse_complete(hand);
302 #endif
303
304     fclose(f);
305     if (to_focus)
306         con_focus(to_focus);
307 }