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");
54 json_node =
con_new(NULL, NULL);
56 DLOG(
"Parent is workspace = %p\n", ws);
59 json_node =
con_new(NULL, NULL);
60 json_node->
parent = parent;
72 json_node = json_node->
parent;
84 LOG(
"end of array\n");
90 LOG(
"focus (reverse) %d\n", mapping->
old_id);
95 LOG(
"got it! %p\n", con);
113 static int json_key(
void *ctx,
const unsigned char *val,
unsigned int len) {
115 static int json_key(
void *ctx,
const unsigned char *val,
size_t len) {
117 LOG(
"key: %.*s\n", (
int)len, val);
121 if (strcasecmp(
last_key,
"swallows") == 0)
124 if (strcasecmp(
last_key,
"rect") == 0)
127 if (strcasecmp(
last_key,
"window_rect") == 0)
130 if (strcasecmp(
last_key,
"geometry") == 0)
133 if (strcasecmp(
last_key,
"focus") == 0)
140 static int json_string(
void *ctx,
const unsigned char *val,
size_t len) {
142 static int json_string(
void *ctx,
const unsigned char *val,
unsigned int len) {
144 LOG(
"string: %.*s for key %s\n", (
int)len, val,
last_key);
147 if (strcasecmp(
last_key,
"class") == 0) {
148 current_swallow->
class =
scalloc((len+1) *
sizeof(
char));
149 memcpy(current_swallow->
class, val, len);
151 LOG(
"unhandled yet: swallow\n");
153 if (strcasecmp(
last_key,
"name") == 0) {
155 memcpy(json_node->
name, val, len);
156 }
else if (strcasecmp(
last_key,
"sticky_group") == 0) {
159 LOG(
"sticky_group of this container is %s\n", json_node->
sticky_group);
160 }
else if (strcasecmp(
last_key,
"orientation") == 0) {
169 if (strcasecmp(buf,
"none") == 0 ||
170 strcasecmp(buf,
"horizontal") == 0)
172 else if (strcasecmp(buf,
"vertical") == 0)
174 else LOG(
"Unhandled orientation: %s\n", buf);
176 }
else if (strcasecmp(
last_key,
"border") == 0) {
179 if (strcasecmp(buf,
"none") == 0)
181 else if (strcasecmp(buf,
"1pixel") == 0) {
184 }
else if (strcasecmp(buf,
"pixel") == 0)
186 else if (strcasecmp(buf,
"normal") == 0)
188 else LOG(
"Unhandled \"border\": %s\n", buf);
190 }
else if (strcasecmp(
last_key,
"layout") == 0) {
193 if (strcasecmp(buf,
"default") == 0)
196 else if (strcasecmp(buf,
"stacked") == 0)
197 json_node->
layout = L_STACKED;
198 else if (strcasecmp(buf,
"tabbed") == 0)
199 json_node->
layout = L_TABBED;
200 else if (strcasecmp(buf,
"dockarea") == 0)
201 json_node->
layout = L_DOCKAREA;
202 else if (strcasecmp(buf,
"output") == 0)
203 json_node->
layout = L_OUTPUT;
204 else if (strcasecmp(buf,
"splith") == 0)
205 json_node->
layout = L_SPLITH;
206 else if (strcasecmp(buf,
"splitv") == 0)
207 json_node->
layout = L_SPLITV;
208 else LOG(
"Unhandled \"layout\": %s\n", buf);
210 }
else if (strcasecmp(
last_key,
"workspace_layout") == 0) {
213 if (strcasecmp(buf,
"default") == 0)
215 else if (strcasecmp(buf,
"stacked") == 0)
217 else if (strcasecmp(buf,
"tabbed") == 0)
219 else LOG(
"Unhandled \"workspace_layout\": %s\n", buf);
221 }
else if (strcasecmp(
last_key,
"last_split_layout") == 0) {
224 if (strcasecmp(buf,
"splith") == 0)
226 else if (strcasecmp(buf,
"splitv") == 0)
228 else LOG(
"Unhandled \"last_splitlayout\": %s\n", buf);
230 }
else if (strcasecmp(
last_key,
"mark") == 0) {
233 json_node->
mark = buf;
234 }
else if (strcasecmp(
last_key,
"floating") == 0) {
237 if (strcasecmp(buf,
"auto_off") == 0)
238 json_node->
floating = FLOATING_AUTO_OFF;
239 else if (strcasecmp(buf,
"auto_on") == 0)
240 json_node->
floating = FLOATING_AUTO_ON;
241 else if (strcasecmp(buf,
"user_off") == 0)
242 json_node->
floating = FLOATING_USER_OFF;
243 else if (strcasecmp(buf,
"user_on") == 0)
244 json_node->
floating = FLOATING_USER_ON;
246 }
else if (strcasecmp(
last_key,
"scratchpad_state") == 0) {
249 if (strcasecmp(buf,
"none") == 0)
251 else if (strcasecmp(buf,
"fresh") == 0)
253 else if (strcasecmp(buf,
"changed") == 0)
262 static int json_int(
void *ctx,
long long val) {
268 if (strcasecmp(
last_key,
"type") == 0)
269 json_node->
type = val;
271 if (strcasecmp(
last_key,
"fullscreen_mode") == 0)
274 if (strcasecmp(
last_key,
"num") == 0)
275 json_node->
num = val;
277 if (strcasecmp(
last_key,
"current_border_width") == 0)
285 focus_mapping->
old_id = val;
292 r = &(json_node->
rect);
298 else if (strcasecmp(
last_key,
"y") == 0)
300 else if (strcasecmp(
last_key,
"width") == 0)
302 else if (strcasecmp(
last_key,
"height") == 0)
304 else printf(
"WARNING: unknown key %s in rect\n",
last_key);
305 printf(
"rect now: (%d, %d, %d, %d)\n",
309 if (strcasecmp(
last_key,
"id") == 0) {
310 current_swallow->
id = val;
312 if (strcasecmp(
last_key,
"dock") == 0) {
313 current_swallow->
dock = val;
315 if (strcasecmp(
last_key,
"insert_where") == 0) {
325 if (strcasecmp(
last_key,
"focused") == 0 && val) {
330 if (strcasecmp(
last_key,
"restart_mode") == 0)
339 if (strcasecmp(
last_key,
"percent") == 0) {
348 if ((f = fopen(filename,
"r")) == NULL) {
349 LOG(
"Cannot open file \"%s\"\n", filename);
353 if (fstat(fileno(f), &stbuf) != 0) {
354 LOG(
"Cannot fstat() the file\n");
358 char *buf =
smalloc(stbuf.st_size);
359 int n = fread(buf, 1, stbuf.st_size, f);
360 if (n != stbuf.st_size) {
361 LOG(
"File \"%s\" could not be read entirely, not loading.\n", filename);
365 LOG(
"read %d bytes\n", n);
368 yajl_callbacks callbacks;
369 memset(&callbacks,
'\0',
sizeof(yajl_callbacks));
379 g = yajl_gen_alloc(NULL);
380 hand = yajl_alloc(&callbacks, NULL, (
void*)g);
382 g = yajl_gen_alloc(NULL, NULL);
383 hand = yajl_alloc(&callbacks, NULL, NULL, (
void*)g);
391 setlocale(LC_NUMERIC,
"C");
392 stat = yajl_parse(hand, (
const unsigned char*)buf, n);
393 if (stat != yajl_status_ok)
395 unsigned char * str = yajl_get_error(hand, 1, (
const unsigned char*)buf, n);
396 fprintf(stderr,
"%s\n", (
const char *) str);
397 yajl_free_error(hand, str);
400 setlocale(LC_NUMERIC,
"");
402 yajl_complete_parse(hand);
404 yajl_parse_complete(hand);