2010-08-15 12:18:27 +02:00
|
|
|
/*
|
|
|
|
* vim:ts=4:sw=4:expandtab
|
|
|
|
*
|
|
|
|
*/
|
2010-03-27 15:25:51 +01:00
|
|
|
#include <yajl/yajl_common.h>
|
|
|
|
#include <yajl/yajl_gen.h>
|
|
|
|
#include <yajl/yajl_parse.h>
|
2011-04-27 19:52:53 +02:00
|
|
|
#include <yajl/yajl_version.h>
|
2010-03-27 15:25:51 +01:00
|
|
|
|
|
|
|
#include "all.h"
|
|
|
|
|
|
|
|
/* TODO: refactor the whole parsing thing */
|
|
|
|
|
|
|
|
static char *last_key;
|
|
|
|
static Con *json_node;
|
2010-11-28 14:27:44 +01:00
|
|
|
static Con *to_focus;
|
2010-03-27 15:25:51 +01:00
|
|
|
static bool parsing_swallows;
|
|
|
|
static bool parsing_rect;
|
2010-11-28 17:20:29 +01:00
|
|
|
static bool parsing_window_rect;
|
2011-02-21 03:01:55 +01:00
|
|
|
static bool parsing_geometry;
|
2010-03-27 15:25:51 +01:00
|
|
|
struct Match *current_swallow;
|
|
|
|
|
|
|
|
static int json_start_map(void *ctx) {
|
2010-11-29 21:46:00 +01:00
|
|
|
LOG("start of map, last_key = %s\n", last_key);
|
2010-03-27 15:25:51 +01:00
|
|
|
if (parsing_swallows) {
|
2011-02-28 20:44:23 +01:00
|
|
|
LOG("creating new swallow\n");
|
2010-08-15 12:18:27 +02:00
|
|
|
current_swallow = smalloc(sizeof(Match));
|
|
|
|
match_init(current_swallow);
|
2010-03-27 15:25:51 +01:00
|
|
|
TAILQ_INSERT_TAIL(&(json_node->swallow_head), current_swallow, matches);
|
|
|
|
} else {
|
2011-02-21 03:01:55 +01:00
|
|
|
if (!parsing_rect && !parsing_window_rect && !parsing_geometry) {
|
2010-11-29 21:46:00 +01:00
|
|
|
if (last_key && strcasecmp(last_key, "floating_nodes") == 0) {
|
2011-02-28 20:44:23 +01:00
|
|
|
DLOG("New floating_node\n");
|
2010-11-29 21:46:00 +01:00
|
|
|
Con *ws = con_get_workspace(json_node);
|
2011-06-02 17:21:38 +02:00
|
|
|
json_node = con_new(NULL, NULL);
|
2010-11-29 21:46:00 +01:00
|
|
|
json_node->parent = ws;
|
2011-02-28 20:44:23 +01:00
|
|
|
DLOG("Parent is workspace = %p\n", ws);
|
2010-11-29 21:46:00 +01:00
|
|
|
} else {
|
2010-12-27 22:28:59 +01:00
|
|
|
Con *parent = json_node;
|
2011-06-02 17:21:38 +02:00
|
|
|
json_node = con_new(NULL, NULL);
|
2010-12-27 22:28:59 +01:00
|
|
|
json_node->parent = parent;
|
2010-11-29 21:46:00 +01:00
|
|
|
}
|
|
|
|
}
|
2010-03-27 15:25:51 +01:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int json_end_map(void *ctx) {
|
|
|
|
LOG("end of map\n");
|
2011-02-21 03:01:55 +01:00
|
|
|
if (!parsing_swallows && !parsing_rect && !parsing_window_rect && !parsing_geometry) {
|
2010-12-27 22:28:59 +01:00
|
|
|
LOG("attaching\n");
|
2011-05-12 07:22:17 +02:00
|
|
|
con_attach(json_node, json_node->parent, true);
|
2010-03-27 15:25:51 +01:00
|
|
|
json_node = json_node->parent;
|
2010-12-27 22:28:59 +01:00
|
|
|
}
|
2010-03-27 15:25:51 +01:00
|
|
|
if (parsing_rect)
|
|
|
|
parsing_rect = false;
|
2010-11-28 17:20:29 +01:00
|
|
|
if (parsing_window_rect)
|
|
|
|
parsing_window_rect = false;
|
2011-02-21 03:01:55 +01:00
|
|
|
if (parsing_geometry)
|
|
|
|
parsing_geometry = false;
|
2010-03-27 15:25:51 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int json_end_array(void *ctx) {
|
|
|
|
LOG("end of array\n");
|
|
|
|
parsing_swallows = false;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-04-27 19:52:53 +02:00
|
|
|
#if YAJL_MAJOR < 2
|
2010-03-27 15:25:51 +01:00
|
|
|
static int json_key(void *ctx, const unsigned char *val, unsigned int len) {
|
2011-04-27 19:52:53 +02:00
|
|
|
#else
|
|
|
|
static int json_key(void *ctx, const unsigned char *val, size_t len) {
|
|
|
|
#endif
|
|
|
|
LOG("key: %.*s\n", (int)len, val);
|
2010-03-27 15:25:51 +01:00
|
|
|
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 17:20:29 +01:00
|
|
|
if (strcasecmp(last_key, "window_rect") == 0)
|
|
|
|
parsing_window_rect = true;
|
2011-02-21 03:01:55 +01:00
|
|
|
if (strcasecmp(last_key, "geometry") == 0)
|
|
|
|
parsing_geometry = true;
|
2010-03-27 15:25:51 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-04-27 19:52:53 +02:00
|
|
|
#if YAJL_MAJOR >= 2
|
|
|
|
static int json_string(void *ctx, const unsigned char *val, size_t len) {
|
|
|
|
#else
|
2010-03-27 15:25:51 +01:00
|
|
|
static int json_string(void *ctx, const unsigned char *val, unsigned int len) {
|
2011-04-27 19:52:53 +02:00
|
|
|
#endif
|
2010-03-27 15:25:51 +01:00
|
|
|
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 18:11:01 +02: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 22:58:22 +01:00
|
|
|
} else if (strcasecmp(last_key, "orientation") == 0) {
|
|
|
|
char *buf = NULL;
|
2011-04-27 19:52:53 +02:00
|
|
|
asprintf(&buf, "%.*s", (int)len, val);
|
2011-01-21 22:58:22 +01:00
|
|
|
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);
|
2011-05-11 20:45:56 +02:00
|
|
|
} else if (strcasecmp(last_key, "border") == 0) {
|
|
|
|
char *buf = NULL;
|
|
|
|
asprintf(&buf, "%.*s", (int)len, val);
|
|
|
|
if (strcasecmp(buf, "none") == 0)
|
|
|
|
json_node->border_style = BS_NONE;
|
|
|
|
else if (strcasecmp(buf, "1pixel") == 0)
|
|
|
|
json_node->border_style = BS_1PIXEL;
|
|
|
|
else if (strcasecmp(buf, "normal") == 0)
|
|
|
|
json_node->border_style = BS_NORMAL;
|
|
|
|
else LOG("Unhandled \"border\": %s\n", buf);
|
|
|
|
free(buf);
|
2011-06-02 17:12:18 +02:00
|
|
|
} else if (strcasecmp(last_key, "layout") == 0) {
|
|
|
|
char *buf = NULL;
|
|
|
|
asprintf(&buf, "%.*s", (int)len, val);
|
|
|
|
if (strcasecmp(buf, "default") == 0)
|
|
|
|
json_node->layout = L_DEFAULT;
|
|
|
|
else if (strcasecmp(buf, "stacked") == 0)
|
|
|
|
json_node->layout = L_STACKED;
|
|
|
|
else if (strcasecmp(buf, "tabbed") == 0)
|
|
|
|
json_node->layout = L_TABBED;
|
|
|
|
else if (strcasecmp(buf, "dockarea") == 0)
|
|
|
|
json_node->layout = L_DOCKAREA;
|
|
|
|
else if (strcasecmp(buf, "output") == 0)
|
|
|
|
json_node->layout = L_OUTPUT;
|
|
|
|
else LOG("Unhandled \"layout\": %s\n", buf);
|
|
|
|
free(buf);
|
2010-03-27 15:25:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-04-27 19:52:53 +02:00
|
|
|
#if YAJL_MAJOR >= 2
|
|
|
|
static int json_int(void *ctx, long long val) {
|
2011-08-04 23:34:23 +02:00
|
|
|
LOG("int %lld for key %s\n", val, last_key);
|
2011-04-27 19:52:53 +02:00
|
|
|
#else
|
2010-03-27 15:25:51 +01:00
|
|
|
static int json_int(void *ctx, long val) {
|
2011-08-04 23:34:23 +02:00
|
|
|
LOG("int %ld for key %s\n", val, last_key);
|
2011-04-27 19:52:53 +02:00
|
|
|
#endif
|
2010-03-27 15:25:51 +01:00
|
|
|
if (strcasecmp(last_key, "type") == 0) {
|
|
|
|
json_node->type = val;
|
|
|
|
}
|
|
|
|
if (strcasecmp(last_key, "fullscreen_mode") == 0) {
|
|
|
|
json_node->fullscreen_mode = val;
|
|
|
|
}
|
2010-12-27 22:28:59 +01:00
|
|
|
if (strcasecmp(last_key, "num") == 0)
|
|
|
|
json_node->num = val;
|
|
|
|
|
2011-02-21 03:01:55 +01:00
|
|
|
if (parsing_rect || parsing_window_rect || parsing_geometry) {
|
|
|
|
Rect *r;
|
|
|
|
if (parsing_rect)
|
|
|
|
r = &(json_node->rect);
|
|
|
|
else if (parsing_window_rect)
|
|
|
|
r = &(json_node->window_rect);
|
|
|
|
else r = &(json_node->geometry);
|
2010-03-27 15:25:51 +01:00
|
|
|
if (strcasecmp(last_key, "x") == 0)
|
2010-11-28 17:20:29 +01:00
|
|
|
r->x = val;
|
2010-03-27 15:25:51 +01:00
|
|
|
else if (strcasecmp(last_key, "y") == 0)
|
2010-11-28 17:20:29 +01:00
|
|
|
r->y = val;
|
2010-03-27 15:25:51 +01:00
|
|
|
else if (strcasecmp(last_key, "width") == 0)
|
2010-11-28 17:20:29 +01:00
|
|
|
r->width = val;
|
2010-03-27 15:25:51 +01:00
|
|
|
else if (strcasecmp(last_key, "height") == 0)
|
2010-11-28 17:20:29 +01:00
|
|
|
r->height = val;
|
2010-03-27 15:25:51 +01:00
|
|
|
else printf("WARNING: unknown key %s in rect\n", last_key);
|
|
|
|
printf("rect now: (%d, %d, %d, %d)\n",
|
2010-11-28 17:20:29 +01:00
|
|
|
r->x, r->y, r->width, r->height);
|
2010-03-27 15:25:51 +01:00
|
|
|
}
|
|
|
|
if (parsing_swallows) {
|
|
|
|
if (strcasecmp(last_key, "id") == 0) {
|
|
|
|
current_swallow->id = val;
|
|
|
|
}
|
2010-08-15 12:18:27 +02:00
|
|
|
if (strcasecmp(last_key, "dock") == 0) {
|
2011-02-21 03:01:55 +01:00
|
|
|
current_swallow->dock = val;
|
|
|
|
}
|
|
|
|
if (strcasecmp(last_key, "insert_where") == 0) {
|
|
|
|
current_swallow->insert_where = val;
|
2010-08-15 12:18:27 +02:00
|
|
|
}
|
2010-03-27 15:25:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-08-04 22:58:19 +02:00
|
|
|
static int json_bool(void *ctx, int val) {
|
|
|
|
LOG("bool %d for key %s\n", val, last_key);
|
|
|
|
if (strcasecmp(last_key, "focused") == 0 && val) {
|
|
|
|
to_focus = json_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-03-27 15:25:51 +01:00
|
|
|
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;
|
2011-08-04 22:58:19 +02:00
|
|
|
callbacks.yajl_boolean = json_bool;
|
2011-04-27 19:52:53 +02:00
|
|
|
#if YAJL_MAJOR >= 2
|
|
|
|
g = yajl_gen_alloc(NULL);
|
|
|
|
hand = yajl_alloc(&callbacks, NULL, (void*)g);
|
|
|
|
#else
|
2010-03-27 15:25:51 +01:00
|
|
|
g = yajl_gen_alloc(NULL, NULL);
|
|
|
|
hand = yajl_alloc(&callbacks, NULL, NULL, (void*)g);
|
2011-04-27 19:52:53 +02:00
|
|
|
#endif
|
2010-03-27 15:25:51 +01:00
|
|
|
yajl_status stat;
|
|
|
|
json_node = focused;
|
2010-11-28 14:27:44 +01:00
|
|
|
to_focus = NULL;
|
2010-11-28 17:20:29 +01:00
|
|
|
parsing_rect = false;
|
|
|
|
parsing_window_rect = false;
|
2011-02-21 03:01:55 +01:00
|
|
|
parsing_geometry = false;
|
2010-03-27 15:25:51 +01:00
|
|
|
setlocale(LC_NUMERIC, "C");
|
|
|
|
stat = yajl_parse(hand, (const unsigned char*)buf, n);
|
2011-04-27 19:52:53 +02:00
|
|
|
if (stat != yajl_status_ok)
|
2010-03-27 15:25:51 +01:00
|
|
|
{
|
|
|
|
unsigned char * str = yajl_get_error(hand, 1, (const unsigned char*)buf, n);
|
2011-02-01 15:43:59 +01:00
|
|
|
fprintf(stderr, "%s\n", (const char *) str);
|
2010-03-27 15:25:51 +01:00
|
|
|
yajl_free_error(hand, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
setlocale(LC_NUMERIC, "");
|
2011-04-27 19:52:53 +02:00
|
|
|
#if YAJL_MAJOR >= 2
|
|
|
|
yajl_complete_parse(hand);
|
|
|
|
#else
|
2010-03-27 15:25:51 +01:00
|
|
|
yajl_parse_complete(hand);
|
2011-04-27 19:52:53 +02:00
|
|
|
#endif
|
2010-03-27 15:25:51 +01:00
|
|
|
|
|
|
|
fclose(f);
|
2010-11-28 14:27:44 +01:00
|
|
|
if (to_focus)
|
|
|
|
con_focus(to_focus);
|
2010-03-27 15:25:51 +01:00
|
|
|
}
|