2 #define I3__FILE__ "load_layout.c"
15 #include <yajl/yajl_common.h>
16 #include <yajl/yajl_gen.h>
17 #include <yajl/yajl_parse.h>
18 #include <yajl/yajl_version.h>
45 LOG(
"creating new swallow\n");
52 DLOG(
"New floating_node\n");
56 DLOG(
"Parent is workspace = %p\n", ws);
60 json_node->
parent = parent;
72 LOG(
"Creating window\n");
74 json_node = json_node->
parent;
86 LOG(
"end of array\n");
92 LOG(
"focus (reverse) %d\n", mapping->
old_id);
97 LOG(
"got it! %p\n", con);
115 static int json_key(
void *ctx,
const unsigned char *val,
unsigned int len) {
117 static int json_key(
void *ctx,
const unsigned char *val,
size_t len) {
119 LOG(
"key: %.*s\n", (
int)len, val);
123 if (strcasecmp(
last_key,
"swallows") == 0)
126 if (strcasecmp(
last_key,
"rect") == 0)
129 if (strcasecmp(
last_key,
"window_rect") == 0)
132 if (strcasecmp(
last_key,
"geometry") == 0)
135 if (strcasecmp(
last_key,
"focus") == 0)
142 static int json_string(
void *ctx,
const unsigned char *val,
size_t len) {
144 static int json_string(
void *ctx,
const unsigned char *val,
unsigned int len) {
146 LOG(
"string: %.*s for key %s\n", (
int)len, val,
last_key);
149 if (strcasecmp(
last_key,
"class") == 0) {
150 current_swallow->
class =
scalloc((len+1) *
sizeof(
char));
151 memcpy(current_swallow->
class, val, len);
153 LOG(
"unhandled yet: swallow\n");
155 if (strcasecmp(
last_key,
"name") == 0) {
157 memcpy(json_node->
name, val, len);
158 }
else if (strcasecmp(
last_key,
"sticky_group") == 0) {
161 LOG(
"sticky_group of this container is %s\n", json_node->
sticky_group);
162 }
else if (strcasecmp(
last_key,
"orientation") == 0) {
171 if (strcasecmp(buf,
"none") == 0 ||
172 strcasecmp(buf,
"horizontal") == 0)
174 else if (strcasecmp(buf,
"vertical") == 0)
176 else LOG(
"Unhandled orientation: %s\n", buf);
178 }
else if (strcasecmp(
last_key,
"border") == 0) {
181 if (strcasecmp(buf,
"none") == 0)
183 else if (strcasecmp(buf,
"1pixel") == 0) {
186 }
else if (strcasecmp(buf,
"pixel") == 0)
188 else if (strcasecmp(buf,
"normal") == 0)
190 else LOG(
"Unhandled \"border\": %s\n", buf);
192 }
else if (strcasecmp(
last_key,
"layout") == 0) {
195 if (strcasecmp(buf,
"default") == 0)
198 else if (strcasecmp(buf,
"stacked") == 0)
200 else if (strcasecmp(buf,
"tabbed") == 0)
202 else if (strcasecmp(buf,
"dockarea") == 0)
204 else if (strcasecmp(buf,
"output") == 0)
206 else if (strcasecmp(buf,
"splith") == 0)
208 else if (strcasecmp(buf,
"splitv") == 0)
210 else LOG(
"Unhandled \"layout\": %s\n", buf);
212 }
else if (strcasecmp(
last_key,
"workspace_layout") == 0) {
215 if (strcasecmp(buf,
"default") == 0)
217 else if (strcasecmp(buf,
"stacked") == 0)
219 else if (strcasecmp(buf,
"tabbed") == 0)
221 else LOG(
"Unhandled \"workspace_layout\": %s\n", buf);
223 }
else if (strcasecmp(
last_key,
"last_split_layout") == 0) {
226 if (strcasecmp(buf,
"splith") == 0)
228 else if (strcasecmp(buf,
"splitv") == 0)
230 else LOG(
"Unhandled \"last_splitlayout\": %s\n", buf);
232 }
else if (strcasecmp(
last_key,
"mark") == 0) {
235 json_node->
mark = buf;
236 }
else if (strcasecmp(
last_key,
"floating") == 0) {
239 if (strcasecmp(buf,
"auto_off") == 0)
240 json_node->
floating = FLOATING_AUTO_OFF;
241 else if (strcasecmp(buf,
"auto_on") == 0)
242 json_node->
floating = FLOATING_AUTO_ON;
243 else if (strcasecmp(buf,
"user_off") == 0)
244 json_node->
floating = FLOATING_USER_OFF;
245 else if (strcasecmp(buf,
"user_on") == 0)
246 json_node->
floating = FLOATING_USER_ON;
248 }
else if (strcasecmp(
last_key,
"scratchpad_state") == 0) {
251 if (strcasecmp(buf,
"none") == 0)
253 else if (strcasecmp(buf,
"fresh") == 0)
255 else if (strcasecmp(buf,
"changed") == 0)
264 static int json_int(
void *ctx,
long long val) {
270 if (strcasecmp(
last_key,
"type") == 0)
271 json_node->
type = val;
273 if (strcasecmp(
last_key,
"fullscreen_mode") == 0)
276 if (strcasecmp(
last_key,
"num") == 0)
277 json_node->
num = val;
279 if (strcasecmp(
last_key,
"current_border_width") == 0)
282 if (strcasecmp(
last_key,
"depth") == 0)
283 json_node->
depth = val;
290 focus_mapping->
old_id = val;
297 r = &(json_node->
rect);
303 else if (strcasecmp(
last_key,
"y") == 0)
305 else if (strcasecmp(
last_key,
"width") == 0)
307 else if (strcasecmp(
last_key,
"height") == 0)
309 else printf(
"WARNING: unknown key %s in rect\n",
last_key);
310 printf(
"rect now: (%d, %d, %d, %d)\n",
314 if (strcasecmp(
last_key,
"id") == 0) {
315 current_swallow->
id = val;
317 if (strcasecmp(
last_key,
"dock") == 0) {
318 current_swallow->
dock = val;
320 if (strcasecmp(
last_key,
"insert_where") == 0) {
330 if (strcasecmp(
last_key,
"focused") == 0 && val) {
335 if (strcasecmp(
last_key,
"restart_mode") == 0)
344 if (strcasecmp(
last_key,
"percent") == 0) {
353 if ((f = fopen(filename,
"r")) == NULL) {
354 LOG(
"Cannot open file \"%s\"\n", filename);
358 if (fstat(fileno(f), &stbuf) != 0) {
359 LOG(
"Cannot fstat() the file\n");
363 char *buf =
smalloc(stbuf.st_size);
364 int n = fread(buf, 1, stbuf.st_size, f);
365 if (n != stbuf.st_size) {
366 LOG(
"File \"%s\" could not be read entirely, not loading.\n", filename);
370 LOG(
"read %d bytes\n", n);
373 yajl_callbacks callbacks;
374 memset(&callbacks,
'\0',
sizeof(yajl_callbacks));
384 g = yajl_gen_alloc(NULL);
385 hand = yajl_alloc(&callbacks, NULL, (
void*)g);
387 g = yajl_gen_alloc(NULL, NULL);
388 hand = yajl_alloc(&callbacks, NULL, NULL, (
void*)g);
396 setlocale(LC_NUMERIC,
"C");
397 stat = yajl_parse(hand, (
const unsigned char*)buf, n);
398 if (stat != yajl_status_ok)
400 unsigned char * str = yajl_get_error(hand, 1, (
const unsigned char*)buf, n);
401 fprintf(stderr,
"%s\n", (
const char *) str);
402 yajl_free_error(hand, str);
405 setlocale(LC_NUMERIC,
"");
407 yajl_complete_parse(hand);
409 yajl_parse_complete(hand);