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;
165 static int json_int(void *ctx, long long val) {
166 LOG("int %lld for key %s\n", val, last_key);
168 static int json_int(void *ctx, long val) {
169 LOG("int %ld for key %s\n", val, last_key);
171 if (strcasecmp(last_key, "type") == 0) {
172 json_node->type = val;
174 if (strcasecmp(last_key, "fullscreen_mode") == 0) {
175 json_node->fullscreen_mode = val;
177 if (strcasecmp(last_key, "num") == 0)
178 json_node->num = val;
180 if (parsing_rect || parsing_window_rect || parsing_geometry) {
183 r = &(json_node->rect);
184 else if (parsing_window_rect)
185 r = &(json_node->window_rect);
186 else r = &(json_node->geometry);
187 if (strcasecmp(last_key, "x") == 0)
189 else if (strcasecmp(last_key, "y") == 0)
191 else if (strcasecmp(last_key, "width") == 0)
193 else if (strcasecmp(last_key, "height") == 0)
195 else printf("WARNING: unknown key %s in rect\n", last_key);
196 printf("rect now: (%d, %d, %d, %d)\n",
197 r->x, r->y, r->width, r->height);
199 if (parsing_swallows) {
200 if (strcasecmp(last_key, "id") == 0) {
201 current_swallow->id = val;
203 if (strcasecmp(last_key, "dock") == 0) {
204 current_swallow->dock = val;
206 if (strcasecmp(last_key, "insert_where") == 0) {
207 current_swallow->insert_where = val;
214 static int json_bool(void *ctx, int val) {
215 LOG("bool %d for key %s\n", val, last_key);
216 if (strcasecmp(last_key, "focused") == 0 && val) {
217 to_focus = json_node;
223 static int json_double(void *ctx, double val) {
224 LOG("double %f for key %s\n", val, last_key);
225 if (strcasecmp(last_key, "percent") == 0) {
226 json_node->percent = val;
231 void tree_append_json(const char *filename) {
232 /* TODO: percent of other windows are not correctly fixed at the moment */
234 if ((f = fopen(filename, "r")) == NULL) {
235 LOG("Cannot open file\n");
238 char *buf = malloc(65535); /* TODO */
239 int n = fread(buf, 1, 65535, f);
240 LOG("read %d bytes\n", n);
243 yajl_callbacks callbacks;
244 memset(&callbacks, '\0', sizeof(yajl_callbacks));
245 callbacks.yajl_start_map = json_start_map;
246 callbacks.yajl_end_map = json_end_map;
247 callbacks.yajl_end_array = json_end_array;
248 callbacks.yajl_string = json_string;
249 callbacks.yajl_map_key = json_key;
250 callbacks.yajl_integer = json_int;
251 callbacks.yajl_double = json_double;
252 callbacks.yajl_boolean = json_bool;
254 g = yajl_gen_alloc(NULL);
255 hand = yajl_alloc(&callbacks, NULL, (void*)g);
257 g = yajl_gen_alloc(NULL, NULL);
258 hand = yajl_alloc(&callbacks, NULL, NULL, (void*)g);
263 parsing_rect = false;
264 parsing_window_rect = false;
265 parsing_geometry = false;
266 setlocale(LC_NUMERIC, "C");
267 stat = yajl_parse(hand, (const unsigned char*)buf, n);
268 if (stat != yajl_status_ok)
270 unsigned char * str = yajl_get_error(hand, 1, (const unsigned char*)buf, n);
271 fprintf(stderr, "%s\n", (const char *) str);
272 yajl_free_error(hand, str);
275 setlocale(LC_NUMERIC, "");
277 yajl_complete_parse(hand);
279 yajl_parse_complete(hand);