]> git.sur5r.net Git - i3/i3/blob - src/load_layout.c
Implement sticky windows
[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         } else if (strcasecmp(last_key, "sticky_group") == 0) {
75             json_node->sticky_group = scalloc((len+1) * sizeof(char));
76             memcpy(json_node->sticky_group, val, len);
77             LOG("sticky_group of this container is %s\n", json_node->sticky_group);
78         }
79     }
80     return 1;
81 }
82
83 static int json_int(void *ctx, long val) {
84     LOG("int %d for key %s\n", val, last_key);
85     if (strcasecmp(last_key, "orientation") == 0) {
86         json_node->orientation = val;
87     }
88     if (strcasecmp(last_key, "layout") == 0) {
89         json_node->layout = val;
90     }
91     if (strcasecmp(last_key, "type") == 0) {
92         json_node->type = val;
93     }
94     if (strcasecmp(last_key, "fullscreen_mode") == 0) {
95         json_node->fullscreen_mode = val;
96     }
97
98     if (parsing_rect) {
99         if (strcasecmp(last_key, "x") == 0)
100             json_node->rect.x = val;
101         else if (strcasecmp(last_key, "y") == 0)
102             json_node->rect.y = val;
103         else if (strcasecmp(last_key, "width") == 0)
104             json_node->rect.width = val;
105         else if (strcasecmp(last_key, "height") == 0)
106             json_node->rect.height = val;
107         else printf("WARNING: unknown key %s in rect\n", last_key);
108         printf("rect now: (%d, %d, %d, %d)\n",
109                 json_node->rect.x, json_node->rect.y,
110                 json_node->rect.width, json_node->rect.height);
111     }
112     if (parsing_swallows) {
113         if (strcasecmp(last_key, "id") == 0) {
114             current_swallow->id = val;
115         }
116         if (strcasecmp(last_key, "dock") == 0) {
117             current_swallow->dock = true;
118         }
119     }
120
121     return 1;
122 }
123
124 static int json_double(void *ctx, double val) {
125     LOG("double %f for key %s\n", val, last_key);
126     if (strcasecmp(last_key, "percent") == 0) {
127         json_node->percent = val;
128     }
129     return 1;
130 }
131
132 void tree_append_json(const char *filename) {
133     /* TODO: percent of other windows are not correctly fixed at the moment */
134     FILE *f;
135     if ((f = fopen(filename, "r")) == NULL) {
136         LOG("Cannot open file\n");
137         return;
138     }
139     char *buf = malloc(65535); /* TODO */
140     int n = fread(buf, 1, 65535, f);
141     LOG("read %d bytes\n", n);
142     yajl_gen g;
143     yajl_handle hand;
144     yajl_callbacks callbacks;
145     memset(&callbacks, '\0', sizeof(yajl_callbacks));
146     callbacks.yajl_start_map = json_start_map;
147     callbacks.yajl_end_map = json_end_map;
148     callbacks.yajl_end_array = json_end_array;
149     callbacks.yajl_string = json_string;
150     callbacks.yajl_map_key = json_key;
151     callbacks.yajl_integer = json_int;
152     callbacks.yajl_double = json_double;
153     g = yajl_gen_alloc(NULL, NULL);
154     hand = yajl_alloc(&callbacks, NULL, NULL, (void*)g);
155     yajl_status stat;
156     json_node = focused;
157     setlocale(LC_NUMERIC, "C");
158     stat = yajl_parse(hand, (const unsigned char*)buf, n);
159     if (stat != yajl_status_ok &&
160         stat != yajl_status_insufficient_data)
161     {
162         unsigned char * str = yajl_get_error(hand, 1, (const unsigned char*)buf, n);
163         fprintf(stderr, (const char *) str);
164         yajl_free_error(hand, str);
165     }
166
167     setlocale(LC_NUMERIC, "");
168     yajl_parse_complete(hand);
169
170     fclose(f);
171     //con_focus(json_node);
172 }