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 struct Match *current_swallow;
21 static int json_start_map(void *ctx) {
22 LOG("start of map, last_key = %s\n", last_key);
23 if (parsing_swallows) {
24 LOG("TODO: create new swallow\n");
25 current_swallow = smalloc(sizeof(Match));
26 match_init(current_swallow);
27 TAILQ_INSERT_TAIL(&(json_node->swallow_head), current_swallow, matches);
29 if (!parsing_rect && !parsing_window_rect) {
30 if (last_key && strcasecmp(last_key, "floating_nodes") == 0) {
31 Con *ws = con_get_workspace(json_node);
32 json_node = con_new(NULL);
33 json_node->parent = ws;
34 TAILQ_INSERT_TAIL(&(ws->floating_head), json_node, floating_windows);
35 TAILQ_INSERT_TAIL(&(ws->focus_head), json_node, focused);
37 Con *parent = json_node;
38 json_node = con_new(NULL);
39 json_node->parent = parent;
46 static int json_end_map(void *ctx) {
48 if (!parsing_swallows && !parsing_rect && !parsing_window_rect) {
50 con_attach(json_node, json_node->parent, false);
51 json_node = json_node->parent;
55 if (parsing_window_rect)
56 parsing_window_rect = false;
60 static int json_end_array(void *ctx) {
61 LOG("end of array\n");
62 parsing_swallows = false;
66 static int json_key(void *ctx, const unsigned char *val, unsigned int len) {
67 LOG("key: %.*s\n", len, val);
69 last_key = scalloc((len+1) * sizeof(char));
70 memcpy(last_key, val, len);
71 if (strcasecmp(last_key, "swallows") == 0) {
72 parsing_swallows = true;
74 if (strcasecmp(last_key, "rect") == 0)
76 if (strcasecmp(last_key, "window_rect") == 0)
77 parsing_window_rect = true;
81 static int json_string(void *ctx, const unsigned char *val, unsigned int len) {
82 LOG("string: %.*s for key %s\n", len, val, last_key);
83 if (parsing_swallows) {
84 /* TODO: the other swallowing keys */
85 if (strcasecmp(last_key, "class") == 0) {
86 current_swallow->class = scalloc((len+1) * sizeof(char));
87 memcpy(current_swallow->class, val, len);
89 LOG("unhandled yet: swallow\n");
91 if (strcasecmp(last_key, "name") == 0) {
92 json_node->name = scalloc((len+1) * sizeof(char));
93 memcpy(json_node->name, val, len);
94 } else if (strcasecmp(last_key, "sticky_group") == 0) {
95 json_node->sticky_group = scalloc((len+1) * sizeof(char));
96 memcpy(json_node->sticky_group, val, len);
97 LOG("sticky_group of this container is %s\n", json_node->sticky_group);
98 } else if (strcasecmp(last_key, "orientation") == 0) {
100 asprintf(&buf, "%.*s", len, val);
101 if (strcasecmp(buf, "none") == 0)
102 json_node->orientation = NO_ORIENTATION;
103 else if (strcasecmp(buf, "horizontal") == 0)
104 json_node->orientation = HORIZ;
105 else if (strcasecmp(buf, "vertical") == 0)
106 json_node->orientation = VERT;
107 else LOG("Unhandled orientation: %s\n", buf);
114 static int json_int(void *ctx, long val) {
115 LOG("int %d for key %s\n", val, last_key);
116 if (strcasecmp(last_key, "layout") == 0) {
117 json_node->layout = val;
119 if (strcasecmp(last_key, "type") == 0) {
120 json_node->type = val;
122 if (strcasecmp(last_key, "fullscreen_mode") == 0) {
123 json_node->fullscreen_mode = val;
125 if (strcasecmp(last_key, "focused") == 0 && val == 1) {
126 to_focus = json_node;
129 if (strcasecmp(last_key, "num") == 0)
130 json_node->num = val;
132 if (parsing_rect || parsing_window_rect) {
133 Rect *r = (parsing_rect ? &(json_node->rect) : &(json_node->window_rect));
134 if (strcasecmp(last_key, "x") == 0)
136 else if (strcasecmp(last_key, "y") == 0)
138 else if (strcasecmp(last_key, "width") == 0)
140 else if (strcasecmp(last_key, "height") == 0)
142 else printf("WARNING: unknown key %s in rect\n", last_key);
143 printf("rect now: (%d, %d, %d, %d)\n",
144 r->x, r->y, r->width, r->height);
146 if (parsing_swallows) {
147 if (strcasecmp(last_key, "id") == 0) {
148 current_swallow->id = val;
150 if (strcasecmp(last_key, "dock") == 0) {
151 current_swallow->dock = true;
158 static int json_double(void *ctx, double val) {
159 LOG("double %f for key %s\n", val, last_key);
160 if (strcasecmp(last_key, "percent") == 0) {
161 json_node->percent = val;
166 void tree_append_json(const char *filename) {
167 /* TODO: percent of other windows are not correctly fixed at the moment */
169 if ((f = fopen(filename, "r")) == NULL) {
170 LOG("Cannot open file\n");
173 char *buf = malloc(65535); /* TODO */
174 int n = fread(buf, 1, 65535, f);
175 LOG("read %d bytes\n", n);
178 yajl_callbacks callbacks;
179 memset(&callbacks, '\0', sizeof(yajl_callbacks));
180 callbacks.yajl_start_map = json_start_map;
181 callbacks.yajl_end_map = json_end_map;
182 callbacks.yajl_end_array = json_end_array;
183 callbacks.yajl_string = json_string;
184 callbacks.yajl_map_key = json_key;
185 callbacks.yajl_integer = json_int;
186 callbacks.yajl_double = json_double;
187 g = yajl_gen_alloc(NULL, NULL);
188 hand = yajl_alloc(&callbacks, NULL, NULL, (void*)g);
192 parsing_rect = false;
193 parsing_window_rect = false;
194 setlocale(LC_NUMERIC, "C");
195 stat = yajl_parse(hand, (const unsigned char*)buf, n);
196 if (stat != yajl_status_ok &&
197 stat != yajl_status_insufficient_data)
199 unsigned char * str = yajl_get_error(hand, 1, (const unsigned char*)buf, n);
200 fprintf(stderr, "%s\n", (const char *) str);
201 yajl_free_error(hand, str);
204 setlocale(LC_NUMERIC, "");
205 yajl_parse_complete(hand);