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;
|
2010-11-28 11:20:29 -05:00
|
|
|
static bool parsing_window_rect;
|
2010-03-27 10:25:51 -04:00
|
|
|
struct Match *current_swallow;
|
|
|
|
|
|
|
|
static int json_start_map(void *ctx) {
|
2010-11-29 15:46:00 -05:00
|
|
|
LOG("start of map, last_key = %s\n", last_key);
|
2010-03-27 10:25:51 -04:00
|
|
|
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 {
|
2010-11-29 15:46:00 -05:00
|
|
|
if (!parsing_rect && !parsing_window_rect) {
|
|
|
|
if (last_key && strcasecmp(last_key, "floating_nodes") == 0) {
|
|
|
|
Con *ws = con_get_workspace(json_node);
|
|
|
|
json_node = con_new(NULL);
|
|
|
|
json_node->parent = ws;
|
|
|
|
TAILQ_INSERT_TAIL(&(ws->floating_head), json_node, floating_windows);
|
|
|
|
TAILQ_INSERT_TAIL(&(ws->focus_head), json_node, focused);
|
|
|
|
} else {
|
2010-12-27 16:28:59 -05:00
|
|
|
Con *parent = json_node;
|
|
|
|
json_node = con_new(NULL);
|
|
|
|
json_node->parent = parent;
|
2010-11-29 15:46:00 -05:00
|
|
|
}
|
|
|
|
}
|
2010-03-27 10:25:51 -04:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int json_end_map(void *ctx) {
|
|
|
|
LOG("end of map\n");
|
2010-12-27 16:28:59 -05:00
|
|
|
if (!parsing_swallows && !parsing_rect && !parsing_window_rect) {
|
|
|
|
LOG("attaching\n");
|
|
|
|
con_attach(json_node, json_node->parent, false);
|
2010-03-27 10:25:51 -04:00
|
|
|
json_node = json_node->parent;
|
2010-12-27 16:28:59 -05:00
|
|
|
}
|
2010-03-27 10:25:51 -04:00
|
|
|
if (parsing_rect)
|
|
|
|
parsing_rect = false;
|
2010-11-28 11:20:29 -05:00
|
|
|
if (parsing_window_rect)
|
|
|
|
parsing_window_rect = false;
|
2010-03-27 10:25:51 -04:00
|
|
|
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;
|
2010-11-28 11:20:29 -05:00
|
|
|
if (strcasecmp(last_key, "window_rect") == 0)
|
|
|
|
parsing_window_rect = true;
|
2010-03-27 10:25:51 -04:00
|
|
|
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);
|
2011-01-21 16:58:22 -05:00
|
|
|
} else if (strcasecmp(last_key, "orientation") == 0) {
|
|
|
|
char *buf = NULL;
|
|
|
|
asprintf(&buf, "%.*s", len, val);
|
|
|
|
if (strcasecmp(buf, "none") == 0)
|
|
|
|
json_node->orientation = NO_ORIENTATION;
|
|
|
|
else if (strcasecmp(buf, "horizontal") == 0)
|
|
|
|
json_node->orientation = HORIZ;
|
|
|
|
else if (strcasecmp(buf, "vertical") == 0)
|
|
|
|
json_node->orientation = VERT;
|
|
|
|
else LOG("Unhandled orientation: %s\n", buf);
|
|
|
|
free(buf);
|
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, "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 11:20:16 -05:00
|
|
|
if (strcasecmp(last_key, "focused") == 0 && val == 1) {
|
2010-11-28 08:27:44 -05:00
|
|
|
to_focus = json_node;
|
|
|
|
}
|
2010-03-27 10:25:51 -04:00
|
|
|
|
2010-12-27 16:28:59 -05:00
|
|
|
if (strcasecmp(last_key, "num") == 0)
|
|
|
|
json_node->num = val;
|
|
|
|
|
2010-11-28 11:20:29 -05:00
|
|
|
if (parsing_rect || parsing_window_rect) {
|
|
|
|
Rect *r = (parsing_rect ? &(json_node->rect) : &(json_node->window_rect));
|
2010-03-27 10:25:51 -04:00
|
|
|
if (strcasecmp(last_key, "x") == 0)
|
2010-11-28 11:20:29 -05:00
|
|
|
r->x = val;
|
2010-03-27 10:25:51 -04:00
|
|
|
else if (strcasecmp(last_key, "y") == 0)
|
2010-11-28 11:20:29 -05:00
|
|
|
r->y = val;
|
2010-03-27 10:25:51 -04:00
|
|
|
else if (strcasecmp(last_key, "width") == 0)
|
2010-11-28 11:20:29 -05:00
|
|
|
r->width = val;
|
2010-03-27 10:25:51 -04:00
|
|
|
else if (strcasecmp(last_key, "height") == 0)
|
2010-11-28 11:20:29 -05:00
|
|
|
r->height = val;
|
2010-03-27 10:25:51 -04:00
|
|
|
else printf("WARNING: unknown key %s in rect\n", last_key);
|
|
|
|
printf("rect now: (%d, %d, %d, %d)\n",
|
2010-11-28 11:20:29 -05:00
|
|
|
r->x, r->y, r->width, r->height);
|
2010-03-27 10:25:51 -04:00
|
|
|
}
|
|
|
|
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-11-28 11:20:29 -05:00
|
|
|
parsing_rect = false;
|
|
|
|
parsing_window_rect = false;
|
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);
|
2011-02-01 09:43:59 -05:00
|
|
|
fprintf(stderr, "%s\n", (const char *) str);
|
2010-03-27 10:25:51 -04:00
|
|
|
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
|
|
|
}
|