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