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_skeleton(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_skeleton(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) {
70 /* Set a few default values to simplify manually crafted layout files. */
71 if (json_node->layout == L_DEFAULT) {
72 DLOG("Setting layout = L_SPLITH\n");
73 json_node->layout = L_SPLITH;
77 con_attach(json_node, json_node->parent, true);
78 LOG("Creating window\n");
79 x_con_init(json_node, json_node->depth);
80 json_node = json_node->parent;
84 if (parsing_window_rect)
85 parsing_window_rect = false;
87 parsing_geometry = false;
91 static int json_end_array(void *ctx) {
92 LOG("end of array\n");
93 parsing_swallows = false;
95 /* Clear the list of focus mappings */
96 struct focus_mapping *mapping;
97 TAILQ_FOREACH_REVERSE(mapping, &focus_mappings, focus_mappings_head, focus_mappings) {
98 LOG("focus (reverse) %d\n", mapping->old_id);
100 TAILQ_FOREACH(con, &(json_node->focus_head), focused) {
101 if (con->old_id != mapping->old_id)
103 LOG("got it! %p\n", con);
104 /* Move this entry to the top of the focus list. */
105 TAILQ_REMOVE(&(json_node->focus_head), con, focused);
106 TAILQ_INSERT_HEAD(&(json_node->focus_head), con, focused);
110 while (!TAILQ_EMPTY(&focus_mappings)) {
111 mapping = TAILQ_FIRST(&focus_mappings);
112 TAILQ_REMOVE(&focus_mappings, mapping, focus_mappings);
115 parsing_focus = false;
121 static int json_key(void *ctx, const unsigned char *val, unsigned int len) {
123 static int json_key(void *ctx, const unsigned char *val, size_t len) {
125 LOG("key: %.*s\n", (int)len, val);
127 last_key = scalloc((len+1) * sizeof(char));
128 memcpy(last_key, val, len);
129 if (strcasecmp(last_key, "swallows") == 0)
130 parsing_swallows = true;
132 if (strcasecmp(last_key, "rect") == 0)
135 if (strcasecmp(last_key, "window_rect") == 0)
136 parsing_window_rect = true;
138 if (strcasecmp(last_key, "geometry") == 0)
139 parsing_geometry = true;
141 if (strcasecmp(last_key, "focus") == 0)
142 parsing_focus = true;
148 static int json_string(void *ctx, const unsigned char *val, size_t len) {
150 static int json_string(void *ctx, const unsigned char *val, unsigned int len) {
152 LOG("string: %.*s for key %s\n", (int)len, val, last_key);
153 if (parsing_swallows) {
154 /* TODO: the other swallowing keys */
155 if (strcasecmp(last_key, "class") == 0) {
156 current_swallow->class = scalloc((len+1) * sizeof(char));
157 memcpy(current_swallow->class, val, len);
159 LOG("unhandled yet: swallow\n");
161 if (strcasecmp(last_key, "name") == 0) {
162 json_node->name = scalloc((len+1) * sizeof(char));
163 memcpy(json_node->name, val, len);
164 } else if (strcasecmp(last_key, "sticky_group") == 0) {
165 json_node->sticky_group = scalloc((len+1) * sizeof(char));
166 memcpy(json_node->sticky_group, val, len);
167 LOG("sticky_group of this container is %s\n", json_node->sticky_group);
168 } else if (strcasecmp(last_key, "orientation") == 0) {
169 /* Upgrade path from older versions of i3 (doing an inplace restart
170 * to a newer version):
171 * "orientation" is dumped before "layout". Therefore, we store
172 * whether the orientation was horizontal or vertical in the
173 * last_split_layout. When we then encounter layout == "default",
174 * we will use the last_split_layout as layout instead. */
176 sasprintf(&buf, "%.*s", (int)len, val);
177 if (strcasecmp(buf, "none") == 0 ||
178 strcasecmp(buf, "horizontal") == 0)
179 json_node->last_split_layout = L_SPLITH;
180 else if (strcasecmp(buf, "vertical") == 0)
181 json_node->last_split_layout = L_SPLITV;
182 else LOG("Unhandled orientation: %s\n", buf);
184 } else if (strcasecmp(last_key, "border") == 0) {
186 sasprintf(&buf, "%.*s", (int)len, val);
187 if (strcasecmp(buf, "none") == 0)
188 json_node->border_style = BS_NONE;
189 else if (strcasecmp(buf, "1pixel") == 0) {
190 json_node->border_style = BS_PIXEL;
191 json_node->current_border_width = 1;
192 } else if (strcasecmp(buf, "pixel") == 0)
193 json_node->border_style = BS_PIXEL;
194 else if (strcasecmp(buf, "normal") == 0)
195 json_node->border_style = BS_NORMAL;
196 else LOG("Unhandled \"border\": %s\n", buf);
198 } else if (strcasecmp(last_key, "layout") == 0) {
200 sasprintf(&buf, "%.*s", (int)len, val);
201 if (strcasecmp(buf, "default") == 0)
202 /* This set above when we read "orientation". */
203 json_node->layout = json_node->last_split_layout;
204 else if (strcasecmp(buf, "stacked") == 0)
205 json_node->layout = L_STACKED;
206 else if (strcasecmp(buf, "tabbed") == 0)
207 json_node->layout = L_TABBED;
208 else if (strcasecmp(buf, "dockarea") == 0)
209 json_node->layout = L_DOCKAREA;
210 else if (strcasecmp(buf, "output") == 0)
211 json_node->layout = L_OUTPUT;
212 else if (strcasecmp(buf, "splith") == 0)
213 json_node->layout = L_SPLITH;
214 else if (strcasecmp(buf, "splitv") == 0)
215 json_node->layout = L_SPLITV;
216 else LOG("Unhandled \"layout\": %s\n", buf);
218 } else if (strcasecmp(last_key, "workspace_layout") == 0) {
220 sasprintf(&buf, "%.*s", (int)len, val);
221 if (strcasecmp(buf, "default") == 0)
222 json_node->workspace_layout = L_DEFAULT;
223 else if (strcasecmp(buf, "stacked") == 0)
224 json_node->workspace_layout = L_STACKED;
225 else if (strcasecmp(buf, "tabbed") == 0)
226 json_node->workspace_layout = L_TABBED;
227 else LOG("Unhandled \"workspace_layout\": %s\n", buf);
229 } else if (strcasecmp(last_key, "last_split_layout") == 0) {
231 sasprintf(&buf, "%.*s", (int)len, val);
232 if (strcasecmp(buf, "splith") == 0)
233 json_node->last_split_layout = L_SPLITH;
234 else if (strcasecmp(buf, "splitv") == 0)
235 json_node->last_split_layout = L_SPLITV;
236 else LOG("Unhandled \"last_splitlayout\": %s\n", buf);
238 } else if (strcasecmp(last_key, "mark") == 0) {
240 sasprintf(&buf, "%.*s", (int)len, val);
241 json_node->mark = buf;
242 } else if (strcasecmp(last_key, "floating") == 0) {
244 sasprintf(&buf, "%.*s", (int)len, val);
245 if (strcasecmp(buf, "auto_off") == 0)
246 json_node->floating = FLOATING_AUTO_OFF;
247 else if (strcasecmp(buf, "auto_on") == 0)
248 json_node->floating = FLOATING_AUTO_ON;
249 else if (strcasecmp(buf, "user_off") == 0)
250 json_node->floating = FLOATING_USER_OFF;
251 else if (strcasecmp(buf, "user_on") == 0)
252 json_node->floating = FLOATING_USER_ON;
254 } else if (strcasecmp(last_key, "scratchpad_state") == 0) {
256 sasprintf(&buf, "%.*s", (int)len, val);
257 if (strcasecmp(buf, "none") == 0)
258 json_node->scratchpad_state = SCRATCHPAD_NONE;
259 else if (strcasecmp(buf, "fresh") == 0)
260 json_node->scratchpad_state = SCRATCHPAD_FRESH;
261 else if (strcasecmp(buf, "changed") == 0)
262 json_node->scratchpad_state = SCRATCHPAD_CHANGED;
270 static int json_int(void *ctx, long long val) {
271 LOG("int %lld for key %s\n", val, last_key);
273 static int json_int(void *ctx, long val) {
274 LOG("int %ld for key %s\n", val, last_key);
276 if (strcasecmp(last_key, "type") == 0)
277 json_node->type = val;
279 if (strcasecmp(last_key, "fullscreen_mode") == 0)
280 json_node->fullscreen_mode = val;
282 if (strcasecmp(last_key, "num") == 0)
283 json_node->num = val;
285 if (strcasecmp(last_key, "current_border_width") == 0)
286 json_node->current_border_width = val;
288 if (strcasecmp(last_key, "depth") == 0)
289 json_node->depth = val;
291 if (!parsing_swallows && strcasecmp(last_key, "id") == 0)
292 json_node->old_id = val;
295 struct focus_mapping *focus_mapping = scalloc(sizeof(struct focus_mapping));
296 focus_mapping->old_id = val;
297 TAILQ_INSERT_TAIL(&focus_mappings, focus_mapping, focus_mappings);
300 if (parsing_rect || parsing_window_rect || parsing_geometry) {
303 r = &(json_node->rect);
304 else if (parsing_window_rect)
305 r = &(json_node->window_rect);
306 else r = &(json_node->geometry);
307 if (strcasecmp(last_key, "x") == 0)
309 else if (strcasecmp(last_key, "y") == 0)
311 else if (strcasecmp(last_key, "width") == 0)
313 else if (strcasecmp(last_key, "height") == 0)
315 else printf("WARNING: unknown key %s in rect\n", last_key);
316 printf("rect now: (%d, %d, %d, %d)\n",
317 r->x, r->y, r->width, r->height);
319 if (parsing_swallows) {
320 if (strcasecmp(last_key, "id") == 0) {
321 current_swallow->id = val;
323 if (strcasecmp(last_key, "dock") == 0) {
324 current_swallow->dock = val;
326 if (strcasecmp(last_key, "insert_where") == 0) {
327 current_swallow->insert_where = val;
334 static int json_bool(void *ctx, int val) {
335 LOG("bool %d for key %s\n", val, last_key);
336 if (strcasecmp(last_key, "focused") == 0 && val) {
337 to_focus = json_node;
340 if (parsing_swallows) {
341 if (strcasecmp(last_key, "restart_mode") == 0)
342 current_swallow->restart_mode = val;
348 static int json_double(void *ctx, double val) {
349 LOG("double %f for key %s\n", val, last_key);
350 if (strcasecmp(last_key, "percent") == 0) {
351 json_node->percent = val;
356 void tree_append_json(const char *filename) {
357 /* TODO: percent of other windows are not correctly fixed at the moment */
359 if ((f = fopen(filename, "r")) == NULL) {
360 LOG("Cannot open file \"%s\"\n", filename);
364 if (fstat(fileno(f), &stbuf) != 0) {
365 LOG("Cannot fstat() the file\n");
369 char *buf = smalloc(stbuf.st_size);
370 int n = fread(buf, 1, stbuf.st_size, f);
371 if (n != stbuf.st_size) {
372 LOG("File \"%s\" could not be read entirely, not loading.\n", filename);
376 LOG("read %d bytes\n", n);
379 yajl_callbacks callbacks;
380 memset(&callbacks, '\0', sizeof(yajl_callbacks));
381 callbacks.yajl_start_map = json_start_map;
382 callbacks.yajl_end_map = json_end_map;
383 callbacks.yajl_end_array = json_end_array;
384 callbacks.yajl_string = json_string;
385 callbacks.yajl_map_key = json_key;
386 callbacks.yajl_integer = json_int;
387 callbacks.yajl_double = json_double;
388 callbacks.yajl_boolean = json_bool;
390 g = yajl_gen_alloc(NULL);
391 hand = yajl_alloc(&callbacks, NULL, (void*)g);
393 g = yajl_gen_alloc(NULL, NULL);
394 hand = yajl_alloc(&callbacks, NULL, NULL, (void*)g);
399 parsing_rect = false;
400 parsing_window_rect = false;
401 parsing_geometry = false;
402 setlocale(LC_NUMERIC, "C");
403 stat = yajl_parse(hand, (const unsigned char*)buf, n);
404 if (stat != yajl_status_ok)
406 unsigned char * str = yajl_get_error(hand, 1, (const unsigned char*)buf, n);
407 fprintf(stderr, "%s\n", (const char *) str);
408 yajl_free_error(hand, str);
411 setlocale(LC_NUMERIC, "");
413 yajl_complete_parse(hand);
415 yajl_parse_complete(hand);