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);
74 } else if (strcasecmp(last_key, "sticky_group") == 0) {
75 json_node->sticky_group = scalloc((len+1) * sizeof(char));
76 memcpy(json_node->sticky_group, val, len);
77 LOG("sticky_group of this container is %s\n", json_node->sticky_group);
83 static int json_int(void *ctx, long val) {
84 LOG("int %d for key %s\n", val, last_key);
85 if (strcasecmp(last_key, "orientation") == 0) {
86 json_node->orientation = val;
88 if (strcasecmp(last_key, "layout") == 0) {
89 json_node->layout = val;
91 if (strcasecmp(last_key, "type") == 0) {
92 json_node->type = val;
94 if (strcasecmp(last_key, "fullscreen_mode") == 0) {
95 json_node->fullscreen_mode = val;
99 if (strcasecmp(last_key, "x") == 0)
100 json_node->rect.x = val;
101 else if (strcasecmp(last_key, "y") == 0)
102 json_node->rect.y = val;
103 else if (strcasecmp(last_key, "width") == 0)
104 json_node->rect.width = val;
105 else if (strcasecmp(last_key, "height") == 0)
106 json_node->rect.height = val;
107 else printf("WARNING: unknown key %s in rect\n", last_key);
108 printf("rect now: (%d, %d, %d, %d)\n",
109 json_node->rect.x, json_node->rect.y,
110 json_node->rect.width, json_node->rect.height);
112 if (parsing_swallows) {
113 if (strcasecmp(last_key, "id") == 0) {
114 current_swallow->id = val;
116 if (strcasecmp(last_key, "dock") == 0) {
117 current_swallow->dock = true;
124 static int json_double(void *ctx, double val) {
125 LOG("double %f for key %s\n", val, last_key);
126 if (strcasecmp(last_key, "percent") == 0) {
127 json_node->percent = val;
132 void tree_append_json(const char *filename) {
133 /* TODO: percent of other windows are not correctly fixed at the moment */
135 if ((f = fopen(filename, "r")) == NULL) {
136 LOG("Cannot open file\n");
139 char *buf = malloc(65535); /* TODO */
140 int n = fread(buf, 1, 65535, f);
141 LOG("read %d bytes\n", n);
144 yajl_callbacks callbacks;
145 memset(&callbacks, '\0', sizeof(yajl_callbacks));
146 callbacks.yajl_start_map = json_start_map;
147 callbacks.yajl_end_map = json_end_map;
148 callbacks.yajl_end_array = json_end_array;
149 callbacks.yajl_string = json_string;
150 callbacks.yajl_map_key = json_key;
151 callbacks.yajl_integer = json_int;
152 callbacks.yajl_double = json_double;
153 g = yajl_gen_alloc(NULL, NULL);
154 hand = yajl_alloc(&callbacks, NULL, NULL, (void*)g);
157 setlocale(LC_NUMERIC, "C");
158 stat = yajl_parse(hand, (const unsigned char*)buf, n);
159 if (stat != yajl_status_ok &&
160 stat != yajl_status_insufficient_data)
162 unsigned char * str = yajl_get_error(hand, 1, (const unsigned char*)buf, n);
163 fprintf(stderr, (const char *) str);
164 yajl_free_error(hand, str);
167 setlocale(LC_NUMERIC, "");
168 yajl_parse_complete(hand);
171 //con_focus(json_node);