]> git.sur5r.net Git - i3/i3/blob - src/load_layout.c
Merge branch 'fix-i3bar-multi-dpy'
[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         }
160     }
161     return 1;
162 }
163
164 #if YAJL_MAJOR >= 2
165 static int json_int(void *ctx, long long val) {
166     LOG("int %lld for key %s\n", val, last_key);
167 #else
168 static int json_int(void *ctx, long val) {
169     LOG("int %ld for key %s\n", val, last_key);
170 #endif
171     if (strcasecmp(last_key, "type") == 0) {
172         json_node->type = val;
173     }
174     if (strcasecmp(last_key, "fullscreen_mode") == 0) {
175         json_node->fullscreen_mode = val;
176     }
177     if (strcasecmp(last_key, "num") == 0)
178         json_node->num = val;
179
180     if (parsing_rect || parsing_window_rect || parsing_geometry) {
181         Rect *r;
182         if (parsing_rect)
183             r = &(json_node->rect);
184         else if (parsing_window_rect)
185             r = &(json_node->window_rect);
186         else r = &(json_node->geometry);
187         if (strcasecmp(last_key, "x") == 0)
188             r->x = val;
189         else if (strcasecmp(last_key, "y") == 0)
190             r->y = val;
191         else if (strcasecmp(last_key, "width") == 0)
192             r->width = val;
193         else if (strcasecmp(last_key, "height") == 0)
194             r->height = val;
195         else printf("WARNING: unknown key %s in rect\n", last_key);
196         printf("rect now: (%d, %d, %d, %d)\n",
197                 r->x, r->y, r->width, r->height);
198     }
199     if (parsing_swallows) {
200         if (strcasecmp(last_key, "id") == 0) {
201             current_swallow->id = val;
202         }
203         if (strcasecmp(last_key, "dock") == 0) {
204             current_swallow->dock = val;
205         }
206         if (strcasecmp(last_key, "insert_where") == 0) {
207             current_swallow->insert_where = val;
208         }
209     }
210
211     return 1;
212 }
213
214 static int json_bool(void *ctx, int val) {
215     LOG("bool %d for key %s\n", val, last_key);
216     if (strcasecmp(last_key, "focused") == 0 && val) {
217         to_focus = json_node;
218     }
219
220     return 1;
221 }
222
223 static int json_double(void *ctx, double val) {
224     LOG("double %f for key %s\n", val, last_key);
225     if (strcasecmp(last_key, "percent") == 0) {
226         json_node->percent = val;
227     }
228     return 1;
229 }
230
231 void tree_append_json(const char *filename) {
232     /* TODO: percent of other windows are not correctly fixed at the moment */
233     FILE *f;
234     if ((f = fopen(filename, "r")) == NULL) {
235         LOG("Cannot open file\n");
236         return;
237     }
238     char *buf = malloc(65535); /* TODO */
239     int n = fread(buf, 1, 65535, f);
240     LOG("read %d bytes\n", n);
241     yajl_gen g;
242     yajl_handle hand;
243     yajl_callbacks callbacks;
244     memset(&callbacks, '\0', sizeof(yajl_callbacks));
245     callbacks.yajl_start_map = json_start_map;
246     callbacks.yajl_end_map = json_end_map;
247     callbacks.yajl_end_array = json_end_array;
248     callbacks.yajl_string = json_string;
249     callbacks.yajl_map_key = json_key;
250     callbacks.yajl_integer = json_int;
251     callbacks.yajl_double = json_double;
252     callbacks.yajl_boolean = json_bool;
253 #if YAJL_MAJOR >= 2
254     g = yajl_gen_alloc(NULL);
255     hand = yajl_alloc(&callbacks, NULL, (void*)g);
256 #else
257     g = yajl_gen_alloc(NULL, NULL);
258     hand = yajl_alloc(&callbacks, NULL, NULL, (void*)g);
259 #endif
260     yajl_status stat;
261     json_node = focused;
262     to_focus = NULL;
263     parsing_rect = false;
264     parsing_window_rect = false;
265     parsing_geometry = false;
266     setlocale(LC_NUMERIC, "C");
267     stat = yajl_parse(hand, (const unsigned char*)buf, n);
268     if (stat != yajl_status_ok)
269     {
270         unsigned char * str = yajl_get_error(hand, 1, (const unsigned char*)buf, n);
271         fprintf(stderr, "%s\n", (const char *) str);
272         yajl_free_error(hand, str);
273     }
274
275     setlocale(LC_NUMERIC, "");
276 #if YAJL_MAJOR >= 2
277     yajl_complete_parse(hand);
278 #else
279     yajl_parse_complete(hand);
280 #endif
281
282     fclose(f);
283     if (to_focus)
284         con_focus(to_focus);
285 }