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 struct Match *current_swallow;
20 static int json_start_map(void *ctx) {
21 LOG("start of map\n");
22 if (parsing_swallows) {
23 LOG("TODO: create new swallow\n");
24 current_swallow = smalloc(sizeof(Match));
25 match_init(current_swallow);
26 TAILQ_INSERT_TAIL(&(json_node->swallow_head), current_swallow, matches);
29 json_node = con_new(json_node);
34 static int json_end_map(void *ctx) {
36 if (!parsing_swallows && !parsing_rect)
37 json_node = json_node->parent;
43 static int json_end_array(void *ctx) {
44 LOG("end of array\n");
45 parsing_swallows = false;
49 static int json_key(void *ctx, const unsigned char *val, unsigned int len) {
50 LOG("key: %.*s\n", len, val);
52 last_key = scalloc((len+1) * sizeof(char));
53 memcpy(last_key, val, len);
54 if (strcasecmp(last_key, "swallows") == 0) {
55 parsing_swallows = true;
57 if (strcasecmp(last_key, "rect") == 0)
62 static int json_string(void *ctx, const unsigned char *val, unsigned int len) {
63 LOG("string: %.*s for key %s\n", len, val, last_key);
64 if (parsing_swallows) {
65 /* TODO: the other swallowing keys */
66 if (strcasecmp(last_key, "class") == 0) {
67 current_swallow->class = scalloc((len+1) * sizeof(char));
68 memcpy(current_swallow->class, val, len);
70 LOG("unhandled yet: swallow\n");
72 if (strcasecmp(last_key, "name") == 0) {
73 json_node->name = scalloc((len+1) * sizeof(char));
74 memcpy(json_node->name, val, len);
75 } else if (strcasecmp(last_key, "sticky_group") == 0) {
76 json_node->sticky_group = scalloc((len+1) * sizeof(char));
77 memcpy(json_node->sticky_group, val, len);
78 LOG("sticky_group of this container is %s\n", json_node->sticky_group);
84 static int json_int(void *ctx, long val) {
85 LOG("int %d for key %s\n", val, last_key);
86 if (strcasecmp(last_key, "orientation") == 0) {
87 json_node->orientation = val;
89 if (strcasecmp(last_key, "layout") == 0) {
90 json_node->layout = val;
92 if (strcasecmp(last_key, "type") == 0) {
93 json_node->type = val;
95 if (strcasecmp(last_key, "fullscreen_mode") == 0) {
96 json_node->fullscreen_mode = val;
98 if (strcasecmp(last_key, "focused") == 0) {
103 if (strcasecmp(last_key, "x") == 0)
104 json_node->rect.x = val;
105 else if (strcasecmp(last_key, "y") == 0)
106 json_node->rect.y = val;
107 else if (strcasecmp(last_key, "width") == 0)
108 json_node->rect.width = val;
109 else if (strcasecmp(last_key, "height") == 0)
110 json_node->rect.height = val;
111 else printf("WARNING: unknown key %s in rect\n", last_key);
112 printf("rect now: (%d, %d, %d, %d)\n",
113 json_node->rect.x, json_node->rect.y,
114 json_node->rect.width, json_node->rect.height);
116 if (parsing_swallows) {
117 if (strcasecmp(last_key, "id") == 0) {
118 current_swallow->id = val;
120 if (strcasecmp(last_key, "dock") == 0) {
121 current_swallow->dock = true;
128 static int json_double(void *ctx, double val) {
129 LOG("double %f for key %s\n", val, last_key);
130 if (strcasecmp(last_key, "percent") == 0) {
131 json_node->percent = val;
136 void tree_append_json(const char *filename) {
137 /* TODO: percent of other windows are not correctly fixed at the moment */
139 if ((f = fopen(filename, "r")) == NULL) {
140 LOG("Cannot open file\n");
143 char *buf = malloc(65535); /* TODO */
144 int n = fread(buf, 1, 65535, f);
145 LOG("read %d bytes\n", n);
148 yajl_callbacks callbacks;
149 memset(&callbacks, '\0', sizeof(yajl_callbacks));
150 callbacks.yajl_start_map = json_start_map;
151 callbacks.yajl_end_map = json_end_map;
152 callbacks.yajl_end_array = json_end_array;
153 callbacks.yajl_string = json_string;
154 callbacks.yajl_map_key = json_key;
155 callbacks.yajl_integer = json_int;
156 callbacks.yajl_double = json_double;
157 g = yajl_gen_alloc(NULL, NULL);
158 hand = yajl_alloc(&callbacks, NULL, NULL, (void*)g);
162 setlocale(LC_NUMERIC, "C");
163 stat = yajl_parse(hand, (const unsigned char*)buf, n);
164 if (stat != yajl_status_ok &&
165 stat != yajl_status_insufficient_data)
167 unsigned char * str = yajl_get_error(hand, 1, (const unsigned char*)buf, n);
168 fprintf(stderr, (const char *) str);
169 yajl_free_error(hand, str);
172 setlocale(LC_NUMERIC, "");
173 yajl_parse_complete(hand);