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;
15 static bool parsing_swallows;
16 static bool parsing_rect;
17 struct Match *current_swallow;
19 static int json_start_map(void *ctx) {
20 LOG("start of map\n");
21 if (parsing_swallows) {
22 LOG("TODO: create new swallow\n");
23 current_swallow = smalloc(sizeof(Match));
24 match_init(current_swallow);
25 TAILQ_INSERT_TAIL(&(json_node->swallow_head), current_swallow, matches);
28 json_node = con_new(json_node);
33 static int json_end_map(void *ctx) {
35 if (!parsing_swallows && !parsing_rect)
36 json_node = json_node->parent;
42 static int json_end_array(void *ctx) {
43 LOG("end of array\n");
44 parsing_swallows = false;
48 static int json_key(void *ctx, const unsigned char *val, unsigned int len) {
49 LOG("key: %.*s\n", len, val);
51 last_key = scalloc((len+1) * sizeof(char));
52 memcpy(last_key, val, len);
53 if (strcasecmp(last_key, "swallows") == 0) {
54 parsing_swallows = true;
56 if (strcasecmp(last_key, "rect") == 0)
61 static int json_string(void *ctx, const unsigned char *val, unsigned int len) {
62 LOG("string: %.*s for key %s\n", len, val, last_key);
63 if (parsing_swallows) {
64 /* TODO: the other swallowing keys */
65 if (strcasecmp(last_key, "class") == 0) {
66 current_swallow->class = scalloc((len+1) * sizeof(char));
67 memcpy(current_swallow->class, val, len);
69 LOG("unhandled yet: swallow\n");
71 if (strcasecmp(last_key, "name") == 0) {
72 json_node->name = scalloc((len+1) * sizeof(char));
73 memcpy(json_node->name, val, len);
79 static int json_int(void *ctx, long val) {
80 LOG("int %d for key %s\n", val, last_key);
81 if (strcasecmp(last_key, "orientation") == 0) {
82 json_node->orientation = val;
84 if (strcasecmp(last_key, "layout") == 0) {
85 json_node->layout = val;
87 if (strcasecmp(last_key, "type") == 0) {
88 json_node->type = val;
90 if (strcasecmp(last_key, "fullscreen_mode") == 0) {
91 json_node->fullscreen_mode = val;
95 if (strcasecmp(last_key, "x") == 0)
96 json_node->rect.x = val;
97 else if (strcasecmp(last_key, "y") == 0)
98 json_node->rect.y = val;
99 else if (strcasecmp(last_key, "width") == 0)
100 json_node->rect.width = val;
101 else if (strcasecmp(last_key, "height") == 0)
102 json_node->rect.height = val;
103 else printf("WARNING: unknown key %s in rect\n", last_key);
104 printf("rect now: (%d, %d, %d, %d)\n",
105 json_node->rect.x, json_node->rect.y,
106 json_node->rect.width, json_node->rect.height);
108 if (parsing_swallows) {
109 if (strcasecmp(last_key, "id") == 0) {
110 current_swallow->id = val;
112 if (strcasecmp(last_key, "dock") == 0) {
113 current_swallow->dock = true;
120 static int json_double(void *ctx, double val) {
121 LOG("double %f for key %s\n", val, last_key);
122 if (strcasecmp(last_key, "percent") == 0) {
123 json_node->percent = val;
128 void tree_append_json(const char *filename) {
129 /* TODO: percent of other windows are not correctly fixed at the moment */
131 if ((f = fopen(filename, "r")) == NULL) {
132 LOG("Cannot open file\n");
135 char *buf = malloc(65535); /* TODO */
136 int n = fread(buf, 1, 65535, f);
137 LOG("read %d bytes\n", n);
140 yajl_callbacks callbacks;
141 memset(&callbacks, '\0', sizeof(yajl_callbacks));
142 callbacks.yajl_start_map = json_start_map;
143 callbacks.yajl_end_map = json_end_map;
144 callbacks.yajl_end_array = json_end_array;
145 callbacks.yajl_string = json_string;
146 callbacks.yajl_map_key = json_key;
147 callbacks.yajl_integer = json_int;
148 callbacks.yajl_double = json_double;
149 g = yajl_gen_alloc(NULL, NULL);
150 hand = yajl_alloc(&callbacks, NULL, NULL, (void*)g);
153 setlocale(LC_NUMERIC, "C");
154 stat = yajl_parse(hand, (const unsigned char*)buf, n);
155 if (stat != yajl_status_ok &&
156 stat != yajl_status_insufficient_data)
158 unsigned char * str = yajl_get_error(hand, 1, (const unsigned char*)buf, n);
159 fprintf(stderr, (const char *) str);
160 yajl_free_error(hand, str);
163 setlocale(LC_NUMERIC, "");
164 yajl_parse_complete(hand);
167 //con_focus(json_node);