]> git.sur5r.net Git - i3/i3/blob - src/load_layout.c
b88a49be1f9d5307f998e448a95638f32f59858b
[i3/i3] / src / load_layout.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  */
5 #include <yajl/yajl_common.h>
6 #include <yajl/yajl_gen.h>
7 #include <yajl/yajl_parse.h>
8
9 #include "all.h"
10
11 /* TODO: refactor the whole parsing thing */
12
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;
18
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);
26     } else {
27         if (!parsing_rect)
28             json_node = con_new(json_node);
29     }
30     return 1;
31 }
32
33 static int json_end_map(void *ctx) {
34     LOG("end of map\n");
35     if (!parsing_swallows && !parsing_rect)
36         json_node = json_node->parent;
37     if (parsing_rect)
38         parsing_rect = false;
39     return 1;
40 }
41
42 static int json_end_array(void *ctx) {
43     LOG("end of array\n");
44     parsing_swallows = false;
45     return 1;
46 }
47
48 static int json_key(void *ctx, const unsigned char *val, unsigned int len) {
49     LOG("key: %.*s\n", len, val);
50     FREE(last_key);
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;
55     }
56     if (strcasecmp(last_key, "rect") == 0)
57         parsing_rect = true;
58     return 1;
59 }
60
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);
68         }
69         LOG("unhandled yet: swallow\n");
70     } else {
71         if (strcasecmp(last_key, "name") == 0) {
72             json_node->name = scalloc((len+1) * sizeof(char));
73             memcpy(json_node->name, val, len);
74         }
75     }
76     return 1;
77 }
78
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;
83     }
84     if (strcasecmp(last_key, "layout") == 0) {
85         json_node->layout = val;
86     }
87     if (strcasecmp(last_key, "type") == 0) {
88         json_node->type = val;
89     }
90     if (strcasecmp(last_key, "fullscreen_mode") == 0) {
91         json_node->fullscreen_mode = val;
92     }
93
94     if (parsing_rect) {
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);
107     }
108     if (parsing_swallows) {
109         if (strcasecmp(last_key, "id") == 0) {
110             current_swallow->id = val;
111         }
112         if (strcasecmp(last_key, "dock") == 0) {
113             current_swallow->dock = true;
114         }
115     }
116
117     return 1;
118 }
119
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;
124     }
125     return 1;
126 }
127
128 void tree_append_json(const char *filename) {
129     /* TODO: percent of other windows are not correctly fixed at the moment */
130     FILE *f;
131     if ((f = fopen(filename, "r")) == NULL) {
132         LOG("Cannot open file\n");
133         return;
134     }
135     char *buf = malloc(65535); /* TODO */
136     int n = fread(buf, 1, 65535, f);
137     LOG("read %d bytes\n", n);
138     yajl_gen g;
139     yajl_handle hand;
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);
151     yajl_status stat;
152     json_node = focused;
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)
157     {
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);
161     }
162
163     setlocale(LC_NUMERIC, "");
164     yajl_parse_complete(hand);
165
166     fclose(f);
167     //con_focus(json_node);
168 }