2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
7 * load_layout.c: Restore (parts of) the layout, for example after an inplace
13 #include <yajl/yajl_common.h>
14 #include <yajl/yajl_gen.h>
15 #include <yajl/yajl_parse.h>
16 #include <yajl/yajl_version.h>
18 /* TODO: refactor the whole parsing thing */
20 static char *last_key;
21 static Con *json_node;
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;
30 /* This list is used for reordering the focus stack after parsing the 'focus'
32 struct focus_mapping {
34 TAILQ_ENTRY(focus_mapping) focus_mappings;
37 static TAILQ_HEAD(focus_mappings_head, focus_mapping) focus_mappings =
38 TAILQ_HEAD_INITIALIZER(focus_mappings);
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);
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);
56 Con *parent = json_node;
57 json_node = con_new(NULL, NULL);
58 json_node->parent = parent;
65 static int json_end_map(void *ctx) {
67 if (!parsing_swallows && !parsing_rect && !parsing_window_rect && !parsing_geometry) {
69 con_attach(json_node, json_node->parent, true);
70 json_node = json_node->parent;
74 if (parsing_window_rect)
75 parsing_window_rect = false;
77 parsing_geometry = false;
81 static int json_end_array(void *ctx) {
82 LOG("end of array\n");
83 parsing_swallows = false;
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);
90 TAILQ_FOREACH(con, &(json_node->focus_head), focused) {
91 if (con->old_id != mapping->old_id)
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);
100 while (!TAILQ_EMPTY(&focus_mappings)) {
101 mapping = TAILQ_FIRST(&focus_mappings);
102 TAILQ_REMOVE(&focus_mappings, mapping, focus_mappings);
105 parsing_focus = false;
111 static int json_key(void *ctx, const unsigned char *val, unsigned int len) {
113 static int json_key(void *ctx, const unsigned char *val, size_t len) {
115 LOG("key: %.*s\n", (int)len, val);
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;
122 if (strcasecmp(last_key, "rect") == 0)
125 if (strcasecmp(last_key, "window_rect") == 0)
126 parsing_window_rect = true;
128 if (strcasecmp(last_key, "geometry") == 0)
129 parsing_geometry = true;
131 if (strcasecmp(last_key, "focus") == 0)
132 parsing_focus = true;
138 static int json_string(void *ctx, const unsigned char *val, size_t len) {
140 static int json_string(void *ctx, const unsigned char *val, unsigned int len) {
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);
149 LOG("unhandled yet: swallow\n");
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. */
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);
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;
179 } else if (strcasecmp(last_key, "border") == 0) {
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);
190 } else if (strcasecmp(last_key, "layout") == 0) {
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);
212 } else if (strcasecmp(last_key, "last_split_layout") == 0) {
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);
221 } else if (strcasecmp(last_key, "mark") == 0) {
223 sasprintf(&buf, "%.*s", (int)len, val);
224 json_node->mark = buf;
225 } else if (strcasecmp(last_key, "floating") == 0) {
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;
237 } else if (strcasecmp(last_key, "scratchpad_state") == 0) {
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;
253 static int json_int(void *ctx, long long val) {
254 LOG("int %lld for key %s\n", val, last_key);
256 static int json_int(void *ctx, long val) {
257 LOG("int %ld for key %s\n", val, last_key);
259 if (strcasecmp(last_key, "type") == 0)
260 json_node->type = val;
262 if (strcasecmp(last_key, "fullscreen_mode") == 0)
263 json_node->fullscreen_mode = val;
265 if (strcasecmp(last_key, "num") == 0)
266 json_node->num = val;
268 if (!parsing_swallows && strcasecmp(last_key, "id") == 0)
269 json_node->old_id = val;
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);
277 if (parsing_rect || parsing_window_rect || parsing_geometry) {
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)
286 else if (strcasecmp(last_key, "y") == 0)
288 else if (strcasecmp(last_key, "width") == 0)
290 else if (strcasecmp(last_key, "height") == 0)
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);
296 if (parsing_swallows) {
297 if (strcasecmp(last_key, "id") == 0) {
298 current_swallow->id = val;
300 if (strcasecmp(last_key, "dock") == 0) {
301 current_swallow->dock = val;
303 if (strcasecmp(last_key, "insert_where") == 0) {
304 current_swallow->insert_where = val;
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;
317 if (strcasecmp(last_key, "split") == 0)
318 json_node->split = val;
320 if (parsing_swallows) {
321 if (strcasecmp(last_key, "restart_mode") == 0)
322 current_swallow->restart_mode = val;
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;
336 void tree_append_json(const char *filename) {
337 /* TODO: percent of other windows are not correctly fixed at the moment */
339 if ((f = fopen(filename, "r")) == NULL) {
340 LOG("Cannot open file\n");
343 char *buf = malloc(65535); /* TODO */
344 int n = fread(buf, 1, 65535, f);
345 LOG("read %d bytes\n", n);
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;
359 g = yajl_gen_alloc(NULL);
360 hand = yajl_alloc(&callbacks, NULL, (void*)g);
362 g = yajl_gen_alloc(NULL, NULL);
363 hand = yajl_alloc(&callbacks, NULL, NULL, (void*)g);
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)
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);
380 setlocale(LC_NUMERIC, "");
382 yajl_complete_parse(hand);
384 yajl_parse_complete(hand);