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>
11 /* TODO: refactor the whole parsing thing */
13 static char *last_key;
14 static Con *json_node;
16 static bool parsing_swallows;
17 static bool parsing_rect;
18 static bool parsing_window_rect;
19 static bool parsing_geometry;
20 struct Match *current_swallow;
22 static int json_start_map(void *ctx) {
23 LOG("start of map, last_key = %s\n", last_key);
24 if (parsing_swallows) {
25 LOG("creating new swallow\n");
26 current_swallow = smalloc(sizeof(Match));
27 match_init(current_swallow);
28 TAILQ_INSERT_TAIL(&(json_node->swallow_head), current_swallow, matches);
30 if (!parsing_rect && !parsing_window_rect && !parsing_geometry) {
31 if (last_key && strcasecmp(last_key, "floating_nodes") == 0) {
32 DLOG("New floating_node\n");
33 Con *ws = con_get_workspace(json_node);
34 json_node = con_new(NULL);
35 json_node->parent = ws;
36 DLOG("Parent is workspace = %p\n", ws);
38 Con *parent = json_node;
39 json_node = con_new(NULL);
40 json_node->parent = parent;
47 static int json_end_map(void *ctx) {
49 if (!parsing_swallows && !parsing_rect && !parsing_window_rect && !parsing_geometry) {
51 con_attach(json_node, json_node->parent, false);
52 json_node = json_node->parent;
56 if (parsing_window_rect)
57 parsing_window_rect = false;
59 parsing_geometry = false;
63 static int json_end_array(void *ctx) {
64 LOG("end of array\n");
65 parsing_swallows = false;
69 static int json_key(void *ctx, const unsigned char *val, unsigned int len) {
70 LOG("key: %.*s\n", len, val);
72 last_key = scalloc((len+1) * sizeof(char));
73 memcpy(last_key, val, len);
74 if (strcasecmp(last_key, "swallows") == 0) {
75 parsing_swallows = true;
77 if (strcasecmp(last_key, "rect") == 0)
79 if (strcasecmp(last_key, "window_rect") == 0)
80 parsing_window_rect = true;
81 if (strcasecmp(last_key, "geometry") == 0)
82 parsing_geometry = true;
86 static int json_string(void *ctx, const unsigned char *val, unsigned int len) {
87 LOG("string: %.*s for key %s\n", len, val, last_key);
88 if (parsing_swallows) {
89 /* TODO: the other swallowing keys */
90 if (strcasecmp(last_key, "class") == 0) {
91 current_swallow->class = scalloc((len+1) * sizeof(char));
92 memcpy(current_swallow->class, val, len);
94 LOG("unhandled yet: swallow\n");
96 if (strcasecmp(last_key, "name") == 0) {
97 json_node->name = scalloc((len+1) * sizeof(char));
98 memcpy(json_node->name, val, len);
99 } else if (strcasecmp(last_key, "sticky_group") == 0) {
100 json_node->sticky_group = scalloc((len+1) * sizeof(char));
101 memcpy(json_node->sticky_group, val, len);
102 LOG("sticky_group of this container is %s\n", json_node->sticky_group);
103 } else if (strcasecmp(last_key, "orientation") == 0) {
105 asprintf(&buf, "%.*s", len, val);
106 if (strcasecmp(buf, "none") == 0)
107 json_node->orientation = NO_ORIENTATION;
108 else if (strcasecmp(buf, "horizontal") == 0)
109 json_node->orientation = HORIZ;
110 else if (strcasecmp(buf, "vertical") == 0)
111 json_node->orientation = VERT;
112 else LOG("Unhandled orientation: %s\n", buf);
119 static int json_int(void *ctx, long val) {
120 LOG("int %d for key %s\n", val, last_key);
121 if (strcasecmp(last_key, "layout") == 0) {
122 json_node->layout = val;
124 if (strcasecmp(last_key, "type") == 0) {
125 json_node->type = val;
127 if (strcasecmp(last_key, "fullscreen_mode") == 0) {
128 json_node->fullscreen_mode = val;
130 if (strcasecmp(last_key, "focused") == 0 && val == 1) {
131 to_focus = json_node;
134 if (strcasecmp(last_key, "num") == 0)
135 json_node->num = val;
137 if (parsing_rect || parsing_window_rect || parsing_geometry) {
140 r = &(json_node->rect);
141 else if (parsing_window_rect)
142 r = &(json_node->window_rect);
143 else r = &(json_node->geometry);
144 if (strcasecmp(last_key, "x") == 0)
146 else if (strcasecmp(last_key, "y") == 0)
148 else if (strcasecmp(last_key, "width") == 0)
150 else if (strcasecmp(last_key, "height") == 0)
152 else printf("WARNING: unknown key %s in rect\n", last_key);
153 printf("rect now: (%d, %d, %d, %d)\n",
154 r->x, r->y, r->width, r->height);
156 if (parsing_swallows) {
157 if (strcasecmp(last_key, "id") == 0) {
158 current_swallow->id = val;
160 if (strcasecmp(last_key, "dock") == 0) {
161 current_swallow->dock = val;
163 if (strcasecmp(last_key, "insert_where") == 0) {
164 current_swallow->insert_where = val;
171 static int json_double(void *ctx, double val) {
172 LOG("double %f for key %s\n", val, last_key);
173 if (strcasecmp(last_key, "percent") == 0) {
174 json_node->percent = val;
179 void tree_append_json(const char *filename) {
180 /* TODO: percent of other windows are not correctly fixed at the moment */
182 if ((f = fopen(filename, "r")) == NULL) {
183 LOG("Cannot open file\n");
186 char *buf = malloc(65535); /* TODO */
187 int n = fread(buf, 1, 65535, f);
188 LOG("read %d bytes\n", n);
191 yajl_callbacks callbacks;
192 memset(&callbacks, '\0', sizeof(yajl_callbacks));
193 callbacks.yajl_start_map = json_start_map;
194 callbacks.yajl_end_map = json_end_map;
195 callbacks.yajl_end_array = json_end_array;
196 callbacks.yajl_string = json_string;
197 callbacks.yajl_map_key = json_key;
198 callbacks.yajl_integer = json_int;
199 callbacks.yajl_double = json_double;
200 g = yajl_gen_alloc(NULL, NULL);
201 hand = yajl_alloc(&callbacks, NULL, NULL, (void*)g);
205 parsing_rect = false;
206 parsing_window_rect = false;
207 parsing_geometry = false;
208 setlocale(LC_NUMERIC, "C");
209 stat = yajl_parse(hand, (const unsigned char*)buf, n);
210 if (stat != yajl_status_ok &&
211 stat != yajl_status_insufficient_data)
213 unsigned char * str = yajl_get_error(hand, 1, (const unsigned char*)buf, n);
214 fprintf(stderr, "%s\n", (const char *) str);
215 yajl_free_error(hand, str);
218 setlocale(LC_NUMERIC, "");
219 yajl_parse_complete(hand);