2010-08-15 06:18:27 -04:00
|
|
|
/*
|
|
|
|
* vim:ts=4:sw=4:expandtab
|
|
|
|
*
|
|
|
|
*/
|
2010-03-27 10:25:51 -04:00
|
|
|
#include <yajl/yajl_common.h>
|
|
|
|
#include <yajl/yajl_gen.h>
|
|
|
|
#include <yajl/yajl_parse.h>
|
|
|
|
|
|
|
|
#include "all.h"
|
|
|
|
|
|
|
|
/* TODO: refactor the whole parsing thing */
|
|
|
|
|
|
|
|
static char *last_key;
|
|
|
|
static Con *json_node;
|
2010-11-28 08:27:44 -05:00
|
|
|
static Con *to_focus;
|
2010-03-27 10:25:51 -04:00
|
|
|
static bool parsing_swallows;
|
|
|
|
static bool parsing_rect;
|
|
|
|
struct Match *current_swallow;
|
|
|
|
|
|
|
|
static int json_start_map(void *ctx) {
|
|
|
|
LOG("start of map\n");
|
|
|
|
if (parsing_swallows) {
|
|
|
|
LOG("TODO: create new swallow\n");
|
2010-08-15 06:18:27 -04:00
|
|
|
current_swallow = smalloc(sizeof(Match));
|
|
|
|
match_init(current_swallow);
|
2010-03-27 10:25:51 -04:00
|
|
|
TAILQ_INSERT_TAIL(&(json_node->swallow_head), current_swallow, matches);
|
|
|
|
} else {
|
|
|
|
if (!parsing_rect)
|
|
|
|
json_node = con_new(json_node);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int json_end_map(void *ctx) {
|
|
|
|
LOG("end of map\n");
|
|
|
|
if (!parsing_swallows && !parsing_rect)
|
|
|
|
json_node = json_node->parent;
|
|
|
|
if (parsing_rect)
|
|
|
|
parsing_rect = false;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int json_end_array(void *ctx) {
|
|
|
|
LOG("end of array\n");
|
|
|
|
parsing_swallows = false;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int json_key(void *ctx, const unsigned char *val, unsigned int len) {
|
|
|
|
LOG("key: %.*s\n", len, val);
|
|
|
|
FREE(last_key);
|
|
|
|
last_key = scalloc((len+1) * sizeof(char));
|
|
|
|
memcpy(last_key, val, len);
|
|
|
|
if (strcasecmp(last_key, "swallows") == 0) {
|
|
|
|
parsing_swallows = true;
|
|
|
|
}
|
|
|
|
if (strcasecmp(last_key, "rect") == 0)
|
|
|
|
parsing_rect = true;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int json_string(void *ctx, const unsigned char *val, unsigned int len) {
|
|
|
|
LOG("string: %.*s for key %s\n", len, val, last_key);
|
|
|
|
if (parsing_swallows) {
|
|
|
|
/* TODO: the other swallowing keys */
|
|
|
|
if (strcasecmp(last_key, "class") == 0) {
|
|
|
|
current_swallow->class = scalloc((len+1) * sizeof(char));
|
|
|
|
memcpy(current_swallow->class, val, len);
|
|
|
|
}
|
|
|
|
LOG("unhandled yet: swallow\n");
|
|
|
|
} else {
|
|
|
|
if (strcasecmp(last_key, "name") == 0) {
|
|
|
|
json_node->name = scalloc((len+1) * sizeof(char));
|
|
|
|
memcpy(json_node->name, val, len);
|
2010-09-01 12:11:01 -04:00
|
|
|
} else if (strcasecmp(last_key, "sticky_group") == 0) {
|
|
|
|
json_node->sticky_group = scalloc((len+1) * sizeof(char));
|
|
|
|
memcpy(json_node->sticky_group, val, len);
|
|
|
|
LOG("sticky_group of this container is %s\n", json_node->sticky_group);
|
2010-03-27 10:25:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int json_int(void *ctx, long val) {
|
|
|
|
LOG("int %d for key %s\n", val, last_key);
|
|
|
|
if (strcasecmp(last_key, "orientation") == 0) {
|
|
|
|
json_node->orientation = val;
|
|
|
|
}
|
|
|
|
if (strcasecmp(last_key, "layout") == 0) {
|
|
|
|
json_node->layout = val;
|
|
|
|
}
|
|
|
|
if (strcasecmp(last_key, "type") == 0) {
|
|
|
|
json_node->type = val;
|
|
|
|
}
|
|
|
|
if (strcasecmp(last_key, "fullscreen_mode") == 0) {
|
|
|
|
json_node->fullscreen_mode = val;
|
|
|
|
}
|
2010-11-28 08:27:44 -05:00
|
|
|
if (strcasecmp(last_key, "focused") == 0) {
|
|
|
|
to_focus = json_node;
|
|
|
|
}
|
2010-03-27 10:25:51 -04:00
|
|
|
|
|
|
|
if (parsing_rect) {
|
|
|
|
if (strcasecmp(last_key, "x") == 0)
|
|
|
|
json_node->rect.x = val;
|
|
|
|
else if (strcasecmp(last_key, "y") == 0)
|
|
|
|
json_node->rect.y = val;
|
|
|
|
else if (strcasecmp(last_key, "width") == 0)
|
|
|
|
json_node->rect.width = val;
|
|
|
|
else if (strcasecmp(last_key, "height") == 0)
|
|
|
|
json_node->rect.height = val;
|
|
|
|
else printf("WARNING: unknown key %s in rect\n", last_key);
|
|
|
|
printf("rect now: (%d, %d, %d, %d)\n",
|
|
|
|
json_node->rect.x, json_node->rect.y,
|
|
|
|
json_node->rect.width, json_node->rect.height);
|
|
|
|
}
|
|
|
|
if (parsing_swallows) {
|
|
|
|
if (strcasecmp(last_key, "id") == 0) {
|
|
|
|
current_swallow->id = val;
|
|
|
|
}
|
2010-08-15 06:18:27 -04:00
|
|
|
if (strcasecmp(last_key, "dock") == 0) {
|
|
|
|
current_swallow->dock = true;
|
|
|
|
}
|
2010-03-27 10:25:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int json_double(void *ctx, double val) {
|
|
|
|
LOG("double %f for key %s\n", val, last_key);
|
|
|
|
if (strcasecmp(last_key, "percent") == 0) {
|
|
|
|
json_node->percent = val;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tree_append_json(const char *filename) {
|
|
|
|
/* TODO: percent of other windows are not correctly fixed at the moment */
|
|
|
|
FILE *f;
|
|
|
|
if ((f = fopen(filename, "r")) == NULL) {
|
|
|
|
LOG("Cannot open file\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
char *buf = malloc(65535); /* TODO */
|
|
|
|
int n = fread(buf, 1, 65535, f);
|
|
|
|
LOG("read %d bytes\n", n);
|
|
|
|
yajl_gen g;
|
|
|
|
yajl_handle hand;
|
|
|
|
yajl_callbacks callbacks;
|
|
|
|
memset(&callbacks, '\0', sizeof(yajl_callbacks));
|
|
|
|
callbacks.yajl_start_map = json_start_map;
|
|
|
|
callbacks.yajl_end_map = json_end_map;
|
|
|
|
callbacks.yajl_end_array = json_end_array;
|
|
|
|
callbacks.yajl_string = json_string;
|
|
|
|
callbacks.yajl_map_key = json_key;
|
|
|
|
callbacks.yajl_integer = json_int;
|
|
|
|
callbacks.yajl_double = json_double;
|
|
|
|
g = yajl_gen_alloc(NULL, NULL);
|
|
|
|
hand = yajl_alloc(&callbacks, NULL, NULL, (void*)g);
|
|
|
|
yajl_status stat;
|
|
|
|
json_node = focused;
|
2010-11-28 08:27:44 -05:00
|
|
|
to_focus = NULL;
|
2010-03-27 10:25:51 -04:00
|
|
|
setlocale(LC_NUMERIC, "C");
|
|
|
|
stat = yajl_parse(hand, (const unsigned char*)buf, n);
|
|
|
|
if (stat != yajl_status_ok &&
|
|
|
|
stat != yajl_status_insufficient_data)
|
|
|
|
{
|
|
|
|
unsigned char * str = yajl_get_error(hand, 1, (const unsigned char*)buf, n);
|
|
|
|
fprintf(stderr, (const char *) str);
|
|
|
|
yajl_free_error(hand, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
setlocale(LC_NUMERIC, "");
|
|
|
|
yajl_parse_complete(hand);
|
|
|
|
|
|
|
|
fclose(f);
|
2010-11-28 08:27:44 -05:00
|
|
|
if (to_focus)
|
|
|
|
con_focus(to_focus);
|
2010-03-27 10:25:51 -04:00
|
|
|
}
|