]> git.sur5r.net Git - i3/i3/blob - src/load_layout.c
c7e77eef80f9c690c77b693225da90319c9b4d39
[i3/i3] / src / load_layout.c
1 #include <yajl/yajl_common.h>
2 #include <yajl/yajl_gen.h>
3 #include <yajl/yajl_parse.h>
4
5 #include "all.h"
6
7 /* TODO: refactor the whole parsing thing */
8
9 static char *last_key;
10 static Con *json_node;
11 static bool parsing_swallows;
12 static bool parsing_rect;
13 struct Match *current_swallow;
14
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);
21     } else {
22         if (!parsing_rect)
23             json_node = con_new(json_node);
24     }
25     return 1;
26 }
27
28 static int json_end_map(void *ctx) {
29     LOG("end of map\n");
30     if (!parsing_swallows && !parsing_rect)
31         json_node = json_node->parent;
32     if (parsing_rect)
33         parsing_rect = false;
34     return 1;
35 }
36
37 static int json_end_array(void *ctx) {
38     LOG("end of array\n");
39     parsing_swallows = false;
40     return 1;
41 }
42
43 static int json_key(void *ctx, const unsigned char *val, unsigned int len) {
44     LOG("key: %.*s\n", len, val);
45     FREE(last_key);
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;
50     }
51     if (strcasecmp(last_key, "rect") == 0)
52         parsing_rect = true;
53     return 1;
54 }
55
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);
63         }
64         LOG("unhandled yet: swallow\n");
65     } else {
66         if (strcasecmp(last_key, "name") == 0) {
67             json_node->name = scalloc((len+1) * sizeof(char));
68             memcpy(json_node->name, val, len);
69         }
70     }
71     return 1;
72 }
73
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;
78     }
79     if (strcasecmp(last_key, "layout") == 0) {
80         json_node->layout = val;
81     }
82     if (strcasecmp(last_key, "type") == 0) {
83         json_node->type = val;
84     }
85     if (strcasecmp(last_key, "fullscreen_mode") == 0) {
86         json_node->fullscreen_mode = val;
87     }
88
89     if (parsing_rect) {
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);
102     }
103     if (parsing_swallows) {
104         if (strcasecmp(last_key, "id") == 0) {
105             current_swallow->id = val;
106         }
107     }
108
109     return 1;
110 }
111
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;
116     }
117     return 1;
118 }
119
120 void tree_append_json(const char *filename) {
121     /* TODO: percent of other windows are not correctly fixed at the moment */
122     FILE *f;
123     if ((f = fopen(filename, "r")) == NULL) {
124         LOG("Cannot open file\n");
125         return;
126     }
127     char *buf = malloc(65535); /* TODO */
128     int n = fread(buf, 1, 65535, f);
129     LOG("read %d bytes\n", n);
130     yajl_gen g;
131     yajl_handle hand;
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);
143     yajl_status stat;
144     json_node = focused;
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)
149     {
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);
153     }
154
155     setlocale(LC_NUMERIC, "");
156     yajl_parse_complete(hand);
157
158     fclose(f);
159     //con_focus(json_node);
160 }