]> git.sur5r.net Git - i3/i3/blob - src/load_layout.c
b46485f951dc2427a7928bc8ff037582245289c4
[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 Con *to_focus;
16 static bool parsing_swallows;
17 static bool parsing_rect;
18 struct Match *current_swallow;
19
20 static int json_start_map(void *ctx) {
21     LOG("start of map\n");
22     if (parsing_swallows) {
23         LOG("TODO: create new swallow\n");
24         current_swallow = smalloc(sizeof(Match));
25         match_init(current_swallow);
26         TAILQ_INSERT_TAIL(&(json_node->swallow_head), current_swallow, matches);
27     } else {
28         if (!parsing_rect)
29             json_node = con_new(json_node);
30     }
31     return 1;
32 }
33
34 static int json_end_map(void *ctx) {
35     LOG("end of map\n");
36     if (!parsing_swallows && !parsing_rect)
37         json_node = json_node->parent;
38     if (parsing_rect)
39         parsing_rect = false;
40     return 1;
41 }
42
43 static int json_end_array(void *ctx) {
44     LOG("end of array\n");
45     parsing_swallows = false;
46     return 1;
47 }
48
49 static int json_key(void *ctx, const unsigned char *val, unsigned int len) {
50     LOG("key: %.*s\n", len, val);
51     FREE(last_key);
52     last_key = scalloc((len+1) * sizeof(char));
53     memcpy(last_key, val, len);
54     if (strcasecmp(last_key, "swallows") == 0) {
55         parsing_swallows = true;
56     }
57     if (strcasecmp(last_key, "rect") == 0)
58         parsing_rect = true;
59     return 1;
60 }
61
62 static int json_string(void *ctx, const unsigned char *val, unsigned int len) {
63     LOG("string: %.*s for key %s\n", len, val, last_key);
64     if (parsing_swallows) {
65         /* TODO: the other swallowing keys */
66         if (strcasecmp(last_key, "class") == 0) {
67             current_swallow->class = scalloc((len+1) * sizeof(char));
68             memcpy(current_swallow->class, val, len);
69         }
70         LOG("unhandled yet: swallow\n");
71     } else {
72         if (strcasecmp(last_key, "name") == 0) {
73             json_node->name = scalloc((len+1) * sizeof(char));
74             memcpy(json_node->name, val, len);
75         } else if (strcasecmp(last_key, "sticky_group") == 0) {
76             json_node->sticky_group = scalloc((len+1) * sizeof(char));
77             memcpy(json_node->sticky_group, val, len);
78             LOG("sticky_group of this container is %s\n", json_node->sticky_group);
79         }
80     }
81     return 1;
82 }
83
84 static int json_int(void *ctx, long val) {
85     LOG("int %d for key %s\n", val, last_key);
86     if (strcasecmp(last_key, "orientation") == 0) {
87         json_node->orientation = val;
88     }
89     if (strcasecmp(last_key, "layout") == 0) {
90         json_node->layout = val;
91     }
92     if (strcasecmp(last_key, "type") == 0) {
93         json_node->type = val;
94     }
95     if (strcasecmp(last_key, "fullscreen_mode") == 0) {
96         json_node->fullscreen_mode = val;
97     }
98     if (strcasecmp(last_key, "focused") == 0) {
99         to_focus = json_node;
100     }
101
102     if (parsing_rect) {
103         if (strcasecmp(last_key, "x") == 0)
104             json_node->rect.x = val;
105         else if (strcasecmp(last_key, "y") == 0)
106             json_node->rect.y = val;
107         else if (strcasecmp(last_key, "width") == 0)
108             json_node->rect.width = val;
109         else if (strcasecmp(last_key, "height") == 0)
110             json_node->rect.height = val;
111         else printf("WARNING: unknown key %s in rect\n", last_key);
112         printf("rect now: (%d, %d, %d, %d)\n",
113                 json_node->rect.x, json_node->rect.y,
114                 json_node->rect.width, json_node->rect.height);
115     }
116     if (parsing_swallows) {
117         if (strcasecmp(last_key, "id") == 0) {
118             current_swallow->id = val;
119         }
120         if (strcasecmp(last_key, "dock") == 0) {
121             current_swallow->dock = true;
122         }
123     }
124
125     return 1;
126 }
127
128 static int json_double(void *ctx, double val) {
129     LOG("double %f for key %s\n", val, last_key);
130     if (strcasecmp(last_key, "percent") == 0) {
131         json_node->percent = val;
132     }
133     return 1;
134 }
135
136 void tree_append_json(const char *filename) {
137     /* TODO: percent of other windows are not correctly fixed at the moment */
138     FILE *f;
139     if ((f = fopen(filename, "r")) == NULL) {
140         LOG("Cannot open file\n");
141         return;
142     }
143     char *buf = malloc(65535); /* TODO */
144     int n = fread(buf, 1, 65535, f);
145     LOG("read %d bytes\n", n);
146     yajl_gen g;
147     yajl_handle hand;
148     yajl_callbacks callbacks;
149     memset(&callbacks, '\0', sizeof(yajl_callbacks));
150     callbacks.yajl_start_map = json_start_map;
151     callbacks.yajl_end_map = json_end_map;
152     callbacks.yajl_end_array = json_end_array;
153     callbacks.yajl_string = json_string;
154     callbacks.yajl_map_key = json_key;
155     callbacks.yajl_integer = json_int;
156     callbacks.yajl_double = json_double;
157     g = yajl_gen_alloc(NULL, NULL);
158     hand = yajl_alloc(&callbacks, NULL, NULL, (void*)g);
159     yajl_status stat;
160     json_node = focused;
161     to_focus = NULL;
162     setlocale(LC_NUMERIC, "C");
163     stat = yajl_parse(hand, (const unsigned char*)buf, n);
164     if (stat != yajl_status_ok &&
165         stat != yajl_status_insufficient_data)
166     {
167         unsigned char * str = yajl_get_error(hand, 1, (const unsigned char*)buf, n);
168         fprintf(stderr, (const char *) str);
169         yajl_free_error(hand, str);
170     }
171
172     setlocale(LC_NUMERIC, "");
173     yajl_parse_complete(hand);
174
175     fclose(f);
176     if (to_focus)
177         con_focus(to_focus);
178 }