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