1 #include <yajl/yajl_common.h>
2 #include <yajl/yajl_gen.h>
3 #include <yajl/yajl_parse.h>
7 /* TODO: refactor the whole parsing thing */
10 static Con *json_node;
11 static bool parsing_swallows;
12 static bool parsing_rect;
13 struct Match *current_swallow;
15 static int json_start_map(void *ctx) {
16 LOG("start of map\n");
17 if (parsing_swallows) {
18 LOG("TODO: create new swallow\n");
19 current_swallow = scalloc(sizeof(Match));
20 TAILQ_INSERT_TAIL(&(json_node->swallow_head), current_swallow, matches);
23 json_node = con_new(json_node);
28 static int json_end_map(void *ctx) {
30 if (!parsing_swallows && !parsing_rect)
31 json_node = json_node->parent;
37 static int json_end_array(void *ctx) {
38 LOG("end of array\n");
39 parsing_swallows = false;
43 static int json_key(void *ctx, const unsigned char *val, unsigned int len) {
44 LOG("key: %.*s\n", len, val);
46 last_key = scalloc((len+1) * sizeof(char));
47 memcpy(last_key, val, len);
48 if (strcasecmp(last_key, "swallows") == 0) {
49 parsing_swallows = true;
51 if (strcasecmp(last_key, "rect") == 0)
56 static int json_string(void *ctx, const unsigned char *val, unsigned int len) {
57 LOG("string: %.*s for key %s\n", len, val, last_key);
58 if (parsing_swallows) {
59 /* TODO: the other swallowing keys */
60 if (strcasecmp(last_key, "class") == 0) {
61 current_swallow->class = scalloc((len+1) * sizeof(char));
62 memcpy(current_swallow->class, val, len);
64 LOG("unhandled yet: swallow\n");
66 if (strcasecmp(last_key, "name") == 0) {
67 json_node->name = scalloc((len+1) * sizeof(char));
68 memcpy(json_node->name, val, len);
74 static int json_int(void *ctx, long val) {
75 LOG("int %d for key %s\n", val, last_key);
76 if (strcasecmp(last_key, "orientation") == 0) {
77 json_node->orientation = val;
79 if (strcasecmp(last_key, "layout") == 0) {
80 json_node->layout = val;
82 if (strcasecmp(last_key, "type") == 0) {
83 json_node->type = val;
85 if (strcasecmp(last_key, "fullscreen_mode") == 0) {
86 json_node->fullscreen_mode = val;
90 if (strcasecmp(last_key, "x") == 0)
91 json_node->rect.x = val;
92 else if (strcasecmp(last_key, "y") == 0)
93 json_node->rect.y = val;
94 else if (strcasecmp(last_key, "width") == 0)
95 json_node->rect.width = val;
96 else if (strcasecmp(last_key, "height") == 0)
97 json_node->rect.height = val;
98 else printf("WARNING: unknown key %s in rect\n", last_key);
99 printf("rect now: (%d, %d, %d, %d)\n",
100 json_node->rect.x, json_node->rect.y,
101 json_node->rect.width, json_node->rect.height);
103 if (parsing_swallows) {
104 if (strcasecmp(last_key, "id") == 0) {
105 current_swallow->id = val;
112 static int json_double(void *ctx, double val) {
113 LOG("double %f for key %s\n", val, last_key);
114 if (strcasecmp(last_key, "percent") == 0) {
115 json_node->percent = val;
120 void tree_append_json(const char *filename) {
121 /* TODO: percent of other windows are not correctly fixed at the moment */
123 if ((f = fopen(filename, "r")) == NULL) {
124 LOG("Cannot open file\n");
127 char *buf = malloc(65535); /* TODO */
128 int n = fread(buf, 1, 65535, f);
129 LOG("read %d bytes\n", n);
132 yajl_callbacks callbacks;
133 memset(&callbacks, '\0', sizeof(yajl_callbacks));
134 callbacks.yajl_start_map = json_start_map;
135 callbacks.yajl_end_map = json_end_map;
136 callbacks.yajl_end_array = json_end_array;
137 callbacks.yajl_string = json_string;
138 callbacks.yajl_map_key = json_key;
139 callbacks.yajl_integer = json_int;
140 callbacks.yajl_double = json_double;
141 g = yajl_gen_alloc(NULL, NULL);
142 hand = yajl_alloc(&callbacks, NULL, NULL, (void*)g);
145 setlocale(LC_NUMERIC, "C");
146 stat = yajl_parse(hand, (const unsigned char*)buf, n);
147 if (stat != yajl_status_ok &&
148 stat != yajl_status_insufficient_data)
150 unsigned char * str = yajl_get_error(hand, 1, (const unsigned char*)buf, n);
151 fprintf(stderr, (const char *) str);
152 yajl_free_error(hand, str);
155 setlocale(LC_NUMERIC, "");
156 yajl_parse_complete(hand);
159 //con_focus(json_node);