2 * vim:ts=4:sw=4:expandtab
5 #include <yajl/yajl_common.h>
6 #include <yajl/yajl_gen.h>
7 #include <yajl/yajl_parse.h>
8 #include <yajl/yajl_version.h>
12 /* TODO: refactor the whole parsing thing */
14 static char *last_key;
15 static Con *json_node;
17 static bool parsing_swallows;
18 static bool parsing_rect;
19 static bool parsing_window_rect;
20 static bool parsing_geometry;
21 struct Match *current_swallow;
23 static int json_start_map(void *ctx) {
24 LOG("start of map, last_key = %s\n", last_key);
25 if (parsing_swallows) {
26 LOG("creating new swallow\n");
27 current_swallow = smalloc(sizeof(Match));
28 match_init(current_swallow);
29 TAILQ_INSERT_TAIL(&(json_node->swallow_head), current_swallow, matches);
31 if (!parsing_rect && !parsing_window_rect && !parsing_geometry) {
32 if (last_key && strcasecmp(last_key, "floating_nodes") == 0) {
33 DLOG("New floating_node\n");
34 Con *ws = con_get_workspace(json_node);
35 json_node = con_new(NULL);
36 json_node->parent = ws;
37 DLOG("Parent is workspace = %p\n", ws);
39 Con *parent = json_node;
40 json_node = con_new(NULL);
41 json_node->parent = parent;
48 static int json_end_map(void *ctx) {
50 if (!parsing_swallows && !parsing_rect && !parsing_window_rect && !parsing_geometry) {
52 con_attach(json_node, json_node->parent, true);
53 json_node = json_node->parent;
57 if (parsing_window_rect)
58 parsing_window_rect = false;
60 parsing_geometry = false;
64 static int json_end_array(void *ctx) {
65 LOG("end of array\n");
66 parsing_swallows = false;
71 static int json_key(void *ctx, const unsigned char *val, unsigned int len) {
73 static int json_key(void *ctx, const unsigned char *val, size_t len) {
75 LOG("key: %.*s\n", (int)len, val);
77 last_key = scalloc((len+1) * sizeof(char));
78 memcpy(last_key, val, len);
79 if (strcasecmp(last_key, "swallows") == 0) {
80 parsing_swallows = true;
82 if (strcasecmp(last_key, "rect") == 0)
84 if (strcasecmp(last_key, "window_rect") == 0)
85 parsing_window_rect = true;
86 if (strcasecmp(last_key, "geometry") == 0)
87 parsing_geometry = true;
92 static int json_string(void *ctx, const unsigned char *val, size_t len) {
94 static int json_string(void *ctx, const unsigned char *val, unsigned int len) {
96 LOG("string: %.*s for key %s\n", len, val, last_key);
97 if (parsing_swallows) {
98 /* TODO: the other swallowing keys */
99 if (strcasecmp(last_key, "class") == 0) {
100 current_swallow->class = scalloc((len+1) * sizeof(char));
101 memcpy(current_swallow->class, val, len);
103 LOG("unhandled yet: swallow\n");
105 if (strcasecmp(last_key, "name") == 0) {
106 json_node->name = scalloc((len+1) * sizeof(char));
107 memcpy(json_node->name, val, len);
108 } else if (strcasecmp(last_key, "sticky_group") == 0) {
109 json_node->sticky_group = scalloc((len+1) * sizeof(char));
110 memcpy(json_node->sticky_group, val, len);
111 LOG("sticky_group of this container is %s\n", json_node->sticky_group);
112 } else if (strcasecmp(last_key, "orientation") == 0) {
114 asprintf(&buf, "%.*s", (int)len, val);
115 if (strcasecmp(buf, "none") == 0)
116 json_node->orientation = NO_ORIENTATION;
117 else if (strcasecmp(buf, "horizontal") == 0)
118 json_node->orientation = HORIZ;
119 else if (strcasecmp(buf, "vertical") == 0)
120 json_node->orientation = VERT;
121 else LOG("Unhandled orientation: %s\n", buf);
123 } else if (strcasecmp(last_key, "border") == 0) {
125 asprintf(&buf, "%.*s", (int)len, val);
126 if (strcasecmp(buf, "none") == 0)
127 json_node->border_style = BS_NONE;
128 else if (strcasecmp(buf, "1pixel") == 0)
129 json_node->border_style = BS_1PIXEL;
130 else if (strcasecmp(buf, "normal") == 0)
131 json_node->border_style = BS_NORMAL;
132 else LOG("Unhandled \"border\": %s\n", buf);
140 static int json_int(void *ctx, long long val) {
142 static int json_int(void *ctx, long val) {
144 LOG("int %d for key %s\n", val, last_key);
145 if (strcasecmp(last_key, "layout") == 0) {
146 json_node->layout = val;
148 if (strcasecmp(last_key, "type") == 0) {
149 json_node->type = val;
151 if (strcasecmp(last_key, "fullscreen_mode") == 0) {
152 json_node->fullscreen_mode = val;
154 if (strcasecmp(last_key, "focused") == 0 && val == 1) {
155 to_focus = json_node;
158 if (strcasecmp(last_key, "num") == 0)
159 json_node->num = val;
161 if (parsing_rect || parsing_window_rect || parsing_geometry) {
164 r = &(json_node->rect);
165 else if (parsing_window_rect)
166 r = &(json_node->window_rect);
167 else r = &(json_node->geometry);
168 if (strcasecmp(last_key, "x") == 0)
170 else if (strcasecmp(last_key, "y") == 0)
172 else if (strcasecmp(last_key, "width") == 0)
174 else if (strcasecmp(last_key, "height") == 0)
176 else printf("WARNING: unknown key %s in rect\n", last_key);
177 printf("rect now: (%d, %d, %d, %d)\n",
178 r->x, r->y, r->width, r->height);
180 if (parsing_swallows) {
181 if (strcasecmp(last_key, "id") == 0) {
182 current_swallow->id = val;
184 if (strcasecmp(last_key, "dock") == 0) {
185 current_swallow->dock = val;
187 if (strcasecmp(last_key, "insert_where") == 0) {
188 current_swallow->insert_where = val;
195 static int json_double(void *ctx, double val) {
196 LOG("double %f for key %s\n", val, last_key);
197 if (strcasecmp(last_key, "percent") == 0) {
198 json_node->percent = val;
203 void tree_append_json(const char *filename) {
204 /* TODO: percent of other windows are not correctly fixed at the moment */
206 if ((f = fopen(filename, "r")) == NULL) {
207 LOG("Cannot open file\n");
210 char *buf = malloc(65535); /* TODO */
211 int n = fread(buf, 1, 65535, f);
212 LOG("read %d bytes\n", n);
215 yajl_callbacks callbacks;
216 memset(&callbacks, '\0', sizeof(yajl_callbacks));
217 callbacks.yajl_start_map = json_start_map;
218 callbacks.yajl_end_map = json_end_map;
219 callbacks.yajl_end_array = json_end_array;
220 callbacks.yajl_string = json_string;
221 callbacks.yajl_map_key = json_key;
222 callbacks.yajl_integer = json_int;
223 callbacks.yajl_double = json_double;
225 g = yajl_gen_alloc(NULL);
226 hand = yajl_alloc(&callbacks, NULL, (void*)g);
228 g = yajl_gen_alloc(NULL, NULL);
229 hand = yajl_alloc(&callbacks, NULL, NULL, (void*)g);
234 parsing_rect = false;
235 parsing_window_rect = false;
236 parsing_geometry = false;
237 setlocale(LC_NUMERIC, "C");
238 stat = yajl_parse(hand, (const unsigned char*)buf, n);
239 if (stat != yajl_status_ok)
241 unsigned char * str = yajl_get_error(hand, 1, (const unsigned char*)buf, n);
242 fprintf(stderr, "%s\n", (const char *) str);
243 yajl_free_error(hand, str);
246 setlocale(LC_NUMERIC, "");
248 yajl_complete_parse(hand);
250 yajl_parse_complete(hand);