2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2011 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 struct Match *current_swallow;
29 static int json_start_map(void *ctx) {
30 LOG("start of map, last_key = %s\n", last_key);
31 if (parsing_swallows) {
32 LOG("creating new swallow\n");
33 current_swallow = smalloc(sizeof(Match));
34 match_init(current_swallow);
35 TAILQ_INSERT_TAIL(&(json_node->swallow_head), current_swallow, matches);
37 if (!parsing_rect && !parsing_window_rect && !parsing_geometry) {
38 if (last_key && strcasecmp(last_key, "floating_nodes") == 0) {
39 DLOG("New floating_node\n");
40 Con *ws = con_get_workspace(json_node);
41 json_node = con_new(NULL, NULL);
42 json_node->parent = ws;
43 DLOG("Parent is workspace = %p\n", ws);
45 Con *parent = json_node;
46 json_node = con_new(NULL, NULL);
47 json_node->parent = parent;
54 static int json_end_map(void *ctx) {
56 if (!parsing_swallows && !parsing_rect && !parsing_window_rect && !parsing_geometry) {
58 con_attach(json_node, json_node->parent, true);
59 json_node = json_node->parent;
63 if (parsing_window_rect)
64 parsing_window_rect = false;
66 parsing_geometry = false;
70 static int json_end_array(void *ctx) {
71 LOG("end of array\n");
72 parsing_swallows = false;
77 static int json_key(void *ctx, const unsigned char *val, unsigned int len) {
79 static int json_key(void *ctx, const unsigned char *val, size_t len) {
81 LOG("key: %.*s\n", (int)len, val);
83 last_key = scalloc((len+1) * sizeof(char));
84 memcpy(last_key, val, len);
85 if (strcasecmp(last_key, "swallows") == 0) {
86 parsing_swallows = true;
88 if (strcasecmp(last_key, "rect") == 0)
90 if (strcasecmp(last_key, "window_rect") == 0)
91 parsing_window_rect = true;
92 if (strcasecmp(last_key, "geometry") == 0)
93 parsing_geometry = true;
98 static int json_string(void *ctx, const unsigned char *val, size_t len) {
100 static int json_string(void *ctx, const unsigned char *val, unsigned int len) {
102 LOG("string: %.*s for key %s\n", len, val, last_key);
103 if (parsing_swallows) {
104 /* TODO: the other swallowing keys */
105 if (strcasecmp(last_key, "class") == 0) {
106 current_swallow->class = scalloc((len+1) * sizeof(char));
107 memcpy(current_swallow->class, val, len);
109 LOG("unhandled yet: swallow\n");
111 if (strcasecmp(last_key, "name") == 0) {
112 json_node->name = scalloc((len+1) * sizeof(char));
113 memcpy(json_node->name, val, len);
114 } else if (strcasecmp(last_key, "sticky_group") == 0) {
115 json_node->sticky_group = scalloc((len+1) * sizeof(char));
116 memcpy(json_node->sticky_group, val, len);
117 LOG("sticky_group of this container is %s\n", json_node->sticky_group);
118 } else if (strcasecmp(last_key, "orientation") == 0) {
120 sasprintf(&buf, "%.*s", (int)len, val);
121 if (strcasecmp(buf, "none") == 0)
122 json_node->orientation = NO_ORIENTATION;
123 else if (strcasecmp(buf, "horizontal") == 0)
124 json_node->orientation = HORIZ;
125 else if (strcasecmp(buf, "vertical") == 0)
126 json_node->orientation = VERT;
127 else LOG("Unhandled orientation: %s\n", buf);
129 } else if (strcasecmp(last_key, "border") == 0) {
131 sasprintf(&buf, "%.*s", (int)len, val);
132 if (strcasecmp(buf, "none") == 0)
133 json_node->border_style = BS_NONE;
134 else if (strcasecmp(buf, "1pixel") == 0)
135 json_node->border_style = BS_1PIXEL;
136 else if (strcasecmp(buf, "normal") == 0)
137 json_node->border_style = BS_NORMAL;
138 else LOG("Unhandled \"border\": %s\n", buf);
140 } else if (strcasecmp(last_key, "layout") == 0) {
142 sasprintf(&buf, "%.*s", (int)len, val);
143 if (strcasecmp(buf, "default") == 0)
144 json_node->layout = L_DEFAULT;
145 else if (strcasecmp(buf, "stacked") == 0)
146 json_node->layout = L_STACKED;
147 else if (strcasecmp(buf, "tabbed") == 0)
148 json_node->layout = L_TABBED;
149 else if (strcasecmp(buf, "dockarea") == 0)
150 json_node->layout = L_DOCKAREA;
151 else if (strcasecmp(buf, "output") == 0)
152 json_node->layout = L_OUTPUT;
153 else LOG("Unhandled \"layout\": %s\n", buf);
155 } else if (strcasecmp(last_key, "mark") == 0) {
157 sasprintf(&buf, "%.*s", (int)len, val);
158 json_node->mark = buf;
159 } else if (strcasecmp(last_key, "floating") == 0) {
161 sasprintf(&buf, "%.*s", (int)len, val);
162 if (strcasecmp(buf, "auto_off") == 0)
163 json_node->floating = FLOATING_AUTO_OFF;
164 else if (strcasecmp(buf, "auto_on") == 0)
165 json_node->floating = FLOATING_AUTO_ON;
166 else if (strcasecmp(buf, "user_off") == 0)
167 json_node->floating = FLOATING_USER_OFF;
168 else if (strcasecmp(buf, "user_on") == 0)
169 json_node->floating = FLOATING_USER_ON;
171 } else if (strcasecmp(last_key, "scratchpad_state") == 0) {
173 sasprintf(&buf, "%.*s", (int)len, val);
174 if (strcasecmp(buf, "none") == 0)
175 json_node->scratchpad_state = SCRATCHPAD_NONE;
176 else if (strcasecmp(buf, "fresh") == 0)
177 json_node->scratchpad_state = SCRATCHPAD_FRESH;
178 else if (strcasecmp(buf, "changed") == 0)
179 json_node->scratchpad_state = SCRATCHPAD_CHANGED;
187 static int json_int(void *ctx, long long val) {
188 LOG("int %lld for key %s\n", val, last_key);
190 static int json_int(void *ctx, long val) {
191 LOG("int %ld for key %s\n", val, last_key);
193 if (strcasecmp(last_key, "type") == 0) {
194 json_node->type = val;
196 if (strcasecmp(last_key, "fullscreen_mode") == 0) {
197 json_node->fullscreen_mode = val;
199 if (strcasecmp(last_key, "num") == 0)
200 json_node->num = val;
202 if (parsing_rect || parsing_window_rect || parsing_geometry) {
205 r = &(json_node->rect);
206 else if (parsing_window_rect)
207 r = &(json_node->window_rect);
208 else r = &(json_node->geometry);
209 if (strcasecmp(last_key, "x") == 0)
211 else if (strcasecmp(last_key, "y") == 0)
213 else if (strcasecmp(last_key, "width") == 0)
215 else if (strcasecmp(last_key, "height") == 0)
217 else printf("WARNING: unknown key %s in rect\n", last_key);
218 printf("rect now: (%d, %d, %d, %d)\n",
219 r->x, r->y, r->width, r->height);
221 if (parsing_swallows) {
222 if (strcasecmp(last_key, "id") == 0) {
223 current_swallow->id = val;
225 if (strcasecmp(last_key, "dock") == 0) {
226 current_swallow->dock = val;
228 if (strcasecmp(last_key, "insert_where") == 0) {
229 current_swallow->insert_where = val;
236 static int json_bool(void *ctx, int val) {
237 LOG("bool %d for key %s\n", val, last_key);
238 if (strcasecmp(last_key, "focused") == 0 && val) {
239 to_focus = json_node;
245 static int json_double(void *ctx, double val) {
246 LOG("double %f for key %s\n", val, last_key);
247 if (strcasecmp(last_key, "percent") == 0) {
248 json_node->percent = val;
253 void tree_append_json(const char *filename) {
254 /* TODO: percent of other windows are not correctly fixed at the moment */
256 if ((f = fopen(filename, "r")) == NULL) {
257 LOG("Cannot open file\n");
260 char *buf = malloc(65535); /* TODO */
261 int n = fread(buf, 1, 65535, f);
262 LOG("read %d bytes\n", n);
265 yajl_callbacks callbacks;
266 memset(&callbacks, '\0', sizeof(yajl_callbacks));
267 callbacks.yajl_start_map = json_start_map;
268 callbacks.yajl_end_map = json_end_map;
269 callbacks.yajl_end_array = json_end_array;
270 callbacks.yajl_string = json_string;
271 callbacks.yajl_map_key = json_key;
272 callbacks.yajl_integer = json_int;
273 callbacks.yajl_double = json_double;
274 callbacks.yajl_boolean = json_bool;
276 g = yajl_gen_alloc(NULL);
277 hand = yajl_alloc(&callbacks, NULL, (void*)g);
279 g = yajl_gen_alloc(NULL, NULL);
280 hand = yajl_alloc(&callbacks, NULL, NULL, (void*)g);
285 parsing_rect = false;
286 parsing_window_rect = false;
287 parsing_geometry = false;
288 setlocale(LC_NUMERIC, "C");
289 stat = yajl_parse(hand, (const unsigned char*)buf, n);
290 if (stat != yajl_status_ok)
292 unsigned char * str = yajl_get_error(hand, 1, (const unsigned char*)buf, n);
293 fprintf(stderr, "%s\n", (const char *) str);
294 yajl_free_error(hand, str);
297 setlocale(LC_NUMERIC, "");
299 yajl_complete_parse(hand);
301 yajl_parse_complete(hand);