2 #define I3__FILE__ "load_layout.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
9 * load_layout.c: Restore (parts of) the layout, for example after an inplace
15 #include <yajl/yajl_common.h>
16 #include <yajl/yajl_gen.h>
17 #include <yajl/yajl_parse.h>
18 #include <yajl/yajl_version.h>
20 /* TODO: refactor the whole parsing thing */
22 static char *last_key;
23 static Con *json_node;
25 static bool parsing_swallows;
26 static bool parsing_rect;
27 static bool parsing_window_rect;
28 static bool parsing_geometry;
29 static bool parsing_focus;
30 struct Match *current_swallow;
32 /* This list is used for reordering the focus stack after parsing the 'focus'
34 struct focus_mapping {
36 TAILQ_ENTRY(focus_mapping) focus_mappings;
39 static TAILQ_HEAD(focus_mappings_head, focus_mapping) focus_mappings =
40 TAILQ_HEAD_INITIALIZER(focus_mappings);
42 static int json_start_map(void *ctx) {
43 LOG("start of map, last_key = %s\n", last_key);
44 if (parsing_swallows) {
45 LOG("creating new swallow\n");
46 current_swallow = smalloc(sizeof(Match));
47 match_init(current_swallow);
48 TAILQ_INSERT_TAIL(&(json_node->swallow_head), current_swallow, matches);
50 if (!parsing_rect && !parsing_window_rect && !parsing_geometry) {
51 if (last_key && strcasecmp(last_key, "floating_nodes") == 0) {
52 DLOG("New floating_node\n");
53 Con *ws = con_get_workspace(json_node);
54 json_node = con_new(NULL, NULL);
55 json_node->parent = ws;
56 DLOG("Parent is workspace = %p\n", ws);
58 Con *parent = json_node;
59 json_node = con_new(NULL, NULL);
60 json_node->parent = parent;
67 static int json_end_map(void *ctx) {
69 if (!parsing_swallows && !parsing_rect && !parsing_window_rect && !parsing_geometry) {
71 con_attach(json_node, json_node->parent, true);
72 json_node = json_node->parent;
76 if (parsing_window_rect)
77 parsing_window_rect = false;
79 parsing_geometry = false;
83 static int json_end_array(void *ctx) {
84 LOG("end of array\n");
85 parsing_swallows = false;
87 /* Clear the list of focus mappings */
88 struct focus_mapping *mapping;
89 TAILQ_FOREACH_REVERSE(mapping, &focus_mappings, focus_mappings_head, focus_mappings) {
90 LOG("focus (reverse) %d\n", mapping->old_id);
92 TAILQ_FOREACH(con, &(json_node->focus_head), focused) {
93 if (con->old_id != mapping->old_id)
95 LOG("got it! %p\n", con);
96 /* Move this entry to the top of the focus list. */
97 TAILQ_REMOVE(&(json_node->focus_head), con, focused);
98 TAILQ_INSERT_HEAD(&(json_node->focus_head), con, focused);
102 while (!TAILQ_EMPTY(&focus_mappings)) {
103 mapping = TAILQ_FIRST(&focus_mappings);
104 TAILQ_REMOVE(&focus_mappings, mapping, focus_mappings);
107 parsing_focus = false;
113 static int json_key(void *ctx, const unsigned char *val, unsigned int len) {
115 static int json_key(void *ctx, const unsigned char *val, size_t len) {
117 LOG("key: %.*s\n", (int)len, val);
119 last_key = scalloc((len+1) * sizeof(char));
120 memcpy(last_key, val, len);
121 if (strcasecmp(last_key, "swallows") == 0)
122 parsing_swallows = true;
124 if (strcasecmp(last_key, "rect") == 0)
127 if (strcasecmp(last_key, "window_rect") == 0)
128 parsing_window_rect = true;
130 if (strcasecmp(last_key, "geometry") == 0)
131 parsing_geometry = true;
133 if (strcasecmp(last_key, "focus") == 0)
134 parsing_focus = true;
140 static int json_string(void *ctx, const unsigned char *val, size_t len) {
142 static int json_string(void *ctx, const unsigned char *val, unsigned int len) {
144 LOG("string: %.*s for key %s\n", (int)len, val, last_key);
145 if (parsing_swallows) {
146 /* TODO: the other swallowing keys */
147 if (strcasecmp(last_key, "class") == 0) {
148 current_swallow->class = scalloc((len+1) * sizeof(char));
149 memcpy(current_swallow->class, val, len);
151 LOG("unhandled yet: swallow\n");
153 if (strcasecmp(last_key, "name") == 0) {
154 json_node->name = scalloc((len+1) * sizeof(char));
155 memcpy(json_node->name, val, len);
156 } else if (strcasecmp(last_key, "sticky_group") == 0) {
157 json_node->sticky_group = scalloc((len+1) * sizeof(char));
158 memcpy(json_node->sticky_group, val, len);
159 LOG("sticky_group of this container is %s\n", json_node->sticky_group);
160 } else if (strcasecmp(last_key, "orientation") == 0) {
161 /* Upgrade path from older versions of i3 (doing an inplace restart
162 * to a newer version):
163 * "orientation" is dumped before "layout". Therefore, we store
164 * whether the orientation was horizontal or vertical in the
165 * last_split_layout. When we then encounter layout == "default",
166 * we will use the last_split_layout as layout instead. */
168 sasprintf(&buf, "%.*s", (int)len, val);
169 if (strcasecmp(buf, "none") == 0 ||
170 strcasecmp(buf, "horizontal") == 0)
171 json_node->last_split_layout = L_SPLITH;
172 else if (strcasecmp(buf, "vertical") == 0)
173 json_node->last_split_layout = L_SPLITV;
174 else LOG("Unhandled orientation: %s\n", buf);
176 /* What used to be an implicit check whether orientation !=
177 * NO_ORIENTATION is now a proper separate flag. */
178 if (strcasecmp(buf, "none") != 0)
179 json_node->split = true;
181 } else if (strcasecmp(last_key, "border") == 0) {
183 sasprintf(&buf, "%.*s", (int)len, val);
184 if (strcasecmp(buf, "none") == 0)
185 json_node->border_style = BS_NONE;
186 else if (strcasecmp(buf, "1pixel") == 0) {
187 json_node->border_style = BS_PIXEL;
188 json_node->current_border_width = 1;
189 } else if (strcasecmp(buf, "pixel") == 0)
190 json_node->border_style = BS_PIXEL;
191 else if (strcasecmp(buf, "normal") == 0)
192 json_node->border_style = BS_NORMAL;
193 else LOG("Unhandled \"border\": %s\n", buf);
195 } else if (strcasecmp(last_key, "layout") == 0) {
197 sasprintf(&buf, "%.*s", (int)len, val);
198 if (strcasecmp(buf, "default") == 0)
199 /* This set above when we read "orientation". */
200 json_node->layout = json_node->last_split_layout;
201 else if (strcasecmp(buf, "stacked") == 0)
202 json_node->layout = L_STACKED;
203 else if (strcasecmp(buf, "tabbed") == 0)
204 json_node->layout = L_TABBED;
205 else if (strcasecmp(buf, "dockarea") == 0) {
206 json_node->layout = L_DOCKAREA;
207 /* Necessary for migrating from older versions of i3. */
208 json_node->split = false;
209 } else if (strcasecmp(buf, "output") == 0)
210 json_node->layout = L_OUTPUT;
211 else if (strcasecmp(buf, "splith") == 0)
212 json_node->layout = L_SPLITH;
213 else if (strcasecmp(buf, "splitv") == 0)
214 json_node->layout = L_SPLITV;
215 else LOG("Unhandled \"layout\": %s\n", buf);
217 } else if (strcasecmp(last_key, "workspace_layout") == 0) {
219 sasprintf(&buf, "%.*s", (int)len, val);
220 if (strcasecmp(buf, "default") == 0)
221 json_node->workspace_layout = L_DEFAULT;
222 else if (strcasecmp(buf, "stacked") == 0)
223 json_node->workspace_layout = L_STACKED;
224 else if (strcasecmp(buf, "tabbed") == 0)
225 json_node->workspace_layout = L_TABBED;
226 else LOG("Unhandled \"workspace_layout\": %s\n", buf);
228 } else if (strcasecmp(last_key, "last_split_layout") == 0) {
230 sasprintf(&buf, "%.*s", (int)len, val);
231 if (strcasecmp(buf, "splith") == 0)
232 json_node->last_split_layout = L_SPLITH;
233 else if (strcasecmp(buf, "splitv") == 0)
234 json_node->last_split_layout = L_SPLITV;
235 else LOG("Unhandled \"last_splitlayout\": %s\n", buf);
237 } else if (strcasecmp(last_key, "mark") == 0) {
239 sasprintf(&buf, "%.*s", (int)len, val);
240 json_node->mark = buf;
241 } else if (strcasecmp(last_key, "floating") == 0) {
243 sasprintf(&buf, "%.*s", (int)len, val);
244 if (strcasecmp(buf, "auto_off") == 0)
245 json_node->floating = FLOATING_AUTO_OFF;
246 else if (strcasecmp(buf, "auto_on") == 0)
247 json_node->floating = FLOATING_AUTO_ON;
248 else if (strcasecmp(buf, "user_off") == 0)
249 json_node->floating = FLOATING_USER_OFF;
250 else if (strcasecmp(buf, "user_on") == 0)
251 json_node->floating = FLOATING_USER_ON;
253 } else if (strcasecmp(last_key, "scratchpad_state") == 0) {
255 sasprintf(&buf, "%.*s", (int)len, val);
256 if (strcasecmp(buf, "none") == 0)
257 json_node->scratchpad_state = SCRATCHPAD_NONE;
258 else if (strcasecmp(buf, "fresh") == 0)
259 json_node->scratchpad_state = SCRATCHPAD_FRESH;
260 else if (strcasecmp(buf, "changed") == 0)
261 json_node->scratchpad_state = SCRATCHPAD_CHANGED;
269 static int json_int(void *ctx, long long val) {
270 LOG("int %lld for key %s\n", val, last_key);
272 static int json_int(void *ctx, long val) {
273 LOG("int %ld for key %s\n", val, last_key);
275 if (strcasecmp(last_key, "type") == 0)
276 json_node->type = val;
278 if (strcasecmp(last_key, "fullscreen_mode") == 0)
279 json_node->fullscreen_mode = val;
281 if (strcasecmp(last_key, "num") == 0)
282 json_node->num = val;
284 if (strcasecmp(last_key, "current_border_width") == 0)
285 json_node->current_border_width = val;
287 if (!parsing_swallows && strcasecmp(last_key, "id") == 0)
288 json_node->old_id = val;
291 struct focus_mapping *focus_mapping = scalloc(sizeof(struct focus_mapping));
292 focus_mapping->old_id = val;
293 TAILQ_INSERT_TAIL(&focus_mappings, focus_mapping, focus_mappings);
296 if (parsing_rect || parsing_window_rect || parsing_geometry) {
299 r = &(json_node->rect);
300 else if (parsing_window_rect)
301 r = &(json_node->window_rect);
302 else r = &(json_node->geometry);
303 if (strcasecmp(last_key, "x") == 0)
305 else if (strcasecmp(last_key, "y") == 0)
307 else if (strcasecmp(last_key, "width") == 0)
309 else if (strcasecmp(last_key, "height") == 0)
311 else printf("WARNING: unknown key %s in rect\n", last_key);
312 printf("rect now: (%d, %d, %d, %d)\n",
313 r->x, r->y, r->width, r->height);
315 if (parsing_swallows) {
316 if (strcasecmp(last_key, "id") == 0) {
317 current_swallow->id = val;
319 if (strcasecmp(last_key, "dock") == 0) {
320 current_swallow->dock = val;
322 if (strcasecmp(last_key, "insert_where") == 0) {
323 current_swallow->insert_where = val;
330 static int json_bool(void *ctx, int val) {
331 LOG("bool %d for key %s\n", val, last_key);
332 if (strcasecmp(last_key, "focused") == 0 && val) {
333 to_focus = json_node;
336 if (strcasecmp(last_key, "split") == 0)
337 json_node->split = val;
339 if (parsing_swallows) {
340 if (strcasecmp(last_key, "restart_mode") == 0)
341 current_swallow->restart_mode = val;
347 static int json_double(void *ctx, double val) {
348 LOG("double %f for key %s\n", val, last_key);
349 if (strcasecmp(last_key, "percent") == 0) {
350 json_node->percent = val;
355 void tree_append_json(const char *filename) {
356 /* TODO: percent of other windows are not correctly fixed at the moment */
358 if ((f = fopen(filename, "r")) == NULL) {
359 LOG("Cannot open file \"%s\"\n", filename);
363 if (fstat(fileno(f), &stbuf) != 0) {
364 LOG("Cannot fstat() the file\n");
368 char *buf = smalloc(stbuf.st_size);
369 int n = fread(buf, 1, stbuf.st_size, f);
370 if (n != stbuf.st_size) {
371 LOG("File \"%s\" could not be read entirely, not loading.\n", filename);
375 LOG("read %d bytes\n", n);
378 yajl_callbacks callbacks;
379 memset(&callbacks, '\0', sizeof(yajl_callbacks));
380 callbacks.yajl_start_map = json_start_map;
381 callbacks.yajl_end_map = json_end_map;
382 callbacks.yajl_end_array = json_end_array;
383 callbacks.yajl_string = json_string;
384 callbacks.yajl_map_key = json_key;
385 callbacks.yajl_integer = json_int;
386 callbacks.yajl_double = json_double;
387 callbacks.yajl_boolean = json_bool;
389 g = yajl_gen_alloc(NULL);
390 hand = yajl_alloc(&callbacks, NULL, (void*)g);
392 g = yajl_gen_alloc(NULL, NULL);
393 hand = yajl_alloc(&callbacks, NULL, NULL, (void*)g);
398 parsing_rect = false;
399 parsing_window_rect = false;
400 parsing_geometry = false;
401 setlocale(LC_NUMERIC, "C");
402 stat = yajl_parse(hand, (const unsigned char*)buf, n);
403 if (stat != yajl_status_ok)
405 unsigned char * str = yajl_get_error(hand, 1, (const unsigned char*)buf, n);
406 fprintf(stderr, "%s\n", (const char *) str);
407 yajl_free_error(hand, str);
410 setlocale(LC_NUMERIC, "");
412 yajl_complete_parse(hand);
414 yajl_parse_complete(hand);