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