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