2010-08-15 06:18:27 -04:00
|
|
|
/*
|
|
|
|
* vim:ts=4:sw=4:expandtab
|
|
|
|
*
|
2011-10-22 18:40:02 -04:00
|
|
|
* i3 - an improved dynamic tiling window manager
|
|
|
|
* © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
|
|
|
|
*
|
|
|
|
* load_layout.c: Restore (parts of) the layout, for example after an inplace
|
|
|
|
* restart.
|
|
|
|
*
|
2010-08-15 06:18:27 -04:00
|
|
|
*/
|
2011-10-22 18:40:02 -04:00
|
|
|
#include "all.h"
|
|
|
|
|
2010-03-27 10:25:51 -04:00
|
|
|
#include <yajl/yajl_common.h>
|
|
|
|
#include <yajl/yajl_gen.h>
|
|
|
|
#include <yajl/yajl_parse.h>
|
2011-04-27 13:52:53 -04:00
|
|
|
#include <yajl/yajl_version.h>
|
2010-03-27 10:25:51 -04:00
|
|
|
|
|
|
|
/* 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;
|
2011-02-20 21:01:55 -05:00
|
|
|
static bool parsing_geometry;
|
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) {
|
2011-02-28 14:44:23 -05:00
|
|
|
LOG("creating 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 {
|
2011-02-20 21:01:55 -05:00
|
|
|
if (!parsing_rect && !parsing_window_rect && !parsing_geometry) {
|
2010-11-29 15:46:00 -05:00
|
|
|
if (last_key && strcasecmp(last_key, "floating_nodes") == 0) {
|
2011-02-28 14:44:23 -05:00
|
|
|
DLOG("New floating_node\n");
|
2010-11-29 15:46:00 -05:00
|
|
|
Con *ws = con_get_workspace(json_node);
|
2011-06-02 11:21:38 -04:00
|
|
|
json_node = con_new(NULL, NULL);
|
2010-11-29 15:46:00 -05:00
|
|
|
json_node->parent = ws;
|
2011-02-28 14:44:23 -05:00
|
|
|
DLOG("Parent is workspace = %p\n", ws);
|
2010-11-29 15:46:00 -05:00
|
|
|
} else {
|
2010-12-27 16:28:59 -05:00
|
|
|
Con *parent = json_node;
|
2011-06-02 11:21:38 -04:00
|
|
|
json_node = con_new(NULL, NULL);
|
2010-12-27 16:28:59 -05:00
|
|
|
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");
|
2011-02-20 21:01:55 -05:00
|
|
|
if (!parsing_swallows && !parsing_rect && !parsing_window_rect && !parsing_geometry) {
|
2010-12-27 16:28:59 -05:00
|
|
|
LOG("attaching\n");
|
2011-05-12 01:22:17 -04:00
|
|
|
con_attach(json_node, json_node->parent, true);
|
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;
|
2011-02-20 21:01:55 -05:00
|
|
|
if (parsing_geometry)
|
|
|
|
parsing_geometry = 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;
|
|
|
|
}
|
|
|
|
|
2011-04-27 13:52:53 -04:00
|
|
|
#if YAJL_MAJOR < 2
|
2010-03-27 10:25:51 -04:00
|
|
|
static int json_key(void *ctx, const unsigned char *val, unsigned int len) {
|
2011-04-27 13:52:53 -04: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 10:25:51 -04: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 11:20:29 -05:00
|
|
|
if (strcasecmp(last_key, "window_rect") == 0)
|
|
|
|
parsing_window_rect = true;
|
2011-02-20 21:01:55 -05:00
|
|
|
if (strcasecmp(last_key, "geometry") == 0)
|
|
|
|
parsing_geometry = true;
|
2010-03-27 10:25:51 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-04-27 13:52:53 -04:00
|
|
|
#if YAJL_MAJOR >= 2
|
|
|
|
static int json_string(void *ctx, const unsigned char *val, size_t len) {
|
|
|
|
#else
|
2010-03-27 10:25:51 -04:00
|
|
|
static int json_string(void *ctx, const unsigned char *val, unsigned int len) {
|
2011-04-27 13:52:53 -04:00
|
|
|
#endif
|
2010-03-27 10:25:51 -04: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 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;
|
2011-10-23 08:16:56 -04:00
|
|
|
sasprintf(&buf, "%.*s", (int)len, val);
|
2011-01-21 16:58:22 -05: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 14:45:56 -04:00
|
|
|
} else if (strcasecmp(last_key, "border") == 0) {
|
|
|
|
char *buf = NULL;
|
2011-10-23 08:16:56 -04:00
|
|
|
sasprintf(&buf, "%.*s", (int)len, val);
|
2011-05-11 14:45:56 -04:00
|
|
|
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 11:12:18 -04:00
|
|
|
} else if (strcasecmp(last_key, "layout") == 0) {
|
|
|
|
char *buf = NULL;
|
2011-10-23 08:16:56 -04:00
|
|
|
sasprintf(&buf, "%.*s", (int)len, val);
|
2011-06-02 11:12:18 -04:00
|
|
|
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);
|
2011-08-07 12:42:23 -04:00
|
|
|
} else if (strcasecmp(last_key, "mark") == 0) {
|
|
|
|
char *buf = NULL;
|
2011-10-23 08:16:56 -04:00
|
|
|
sasprintf(&buf, "%.*s", (int)len, val);
|
2011-08-07 12:42:23 -04:00
|
|
|
json_node->mark = buf;
|
2012-01-05 16:39:18 -05:00
|
|
|
} else if (strcasecmp(last_key, "floating") == 0) {
|
|
|
|
char *buf = NULL;
|
|
|
|
sasprintf(&buf, "%.*s", (int)len, val);
|
|
|
|
if (strcasecmp(buf, "auto_off") == 0)
|
|
|
|
json_node->floating = FLOATING_AUTO_OFF;
|
|
|
|
else if (strcasecmp(buf, "auto_on") == 0)
|
|
|
|
json_node->floating = FLOATING_AUTO_ON;
|
|
|
|
else if (strcasecmp(buf, "user_off") == 0)
|
|
|
|
json_node->floating = FLOATING_USER_OFF;
|
|
|
|
else if (strcasecmp(buf, "user_on") == 0)
|
|
|
|
json_node->floating = FLOATING_USER_ON;
|
|
|
|
free(buf);
|
|
|
|
} else if (strcasecmp(last_key, "scratchpad_state") == 0) {
|
|
|
|
char *buf = NULL;
|
|
|
|
sasprintf(&buf, "%.*s", (int)len, val);
|
|
|
|
if (strcasecmp(buf, "none") == 0)
|
|
|
|
json_node->scratchpad_state = SCRATCHPAD_NONE;
|
|
|
|
else if (strcasecmp(buf, "fresh") == 0)
|
|
|
|
json_node->scratchpad_state = SCRATCHPAD_FRESH;
|
|
|
|
else if (strcasecmp(buf, "changed") == 0)
|
|
|
|
json_node->scratchpad_state = SCRATCHPAD_CHANGED;
|
|
|
|
free(buf);
|
2010-03-27 10:25:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-04-27 13:52:53 -04:00
|
|
|
#if YAJL_MAJOR >= 2
|
|
|
|
static int json_int(void *ctx, long long val) {
|
2011-08-04 17:34:23 -04:00
|
|
|
LOG("int %lld for key %s\n", val, last_key);
|
2011-04-27 13:52:53 -04:00
|
|
|
#else
|
2010-03-27 10:25:51 -04:00
|
|
|
static int json_int(void *ctx, long val) {
|
2011-08-04 17:34:23 -04:00
|
|
|
LOG("int %ld for key %s\n", val, last_key);
|
2011-04-27 13:52:53 -04:00
|
|
|
#endif
|
2010-03-27 10:25:51 -04: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 16:28:59 -05:00
|
|
|
if (strcasecmp(last_key, "num") == 0)
|
|
|
|
json_node->num = val;
|
|
|
|
|
2011-02-20 21:01:55 -05: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 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) {
|
2011-02-20 21:01:55 -05:00
|
|
|
current_swallow->dock = val;
|
|
|
|
}
|
|
|
|
if (strcasecmp(last_key, "insert_where") == 0) {
|
|
|
|
current_swallow->insert_where = val;
|
2010-08-15 06:18:27 -04:00
|
|
|
}
|
2010-03-27 10:25:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-08-04 16:58:19 -04: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 10:25:51 -04: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 16:58:19 -04:00
|
|
|
callbacks.yajl_boolean = json_bool;
|
2011-04-27 13:52:53 -04:00
|
|
|
#if YAJL_MAJOR >= 2
|
|
|
|
g = yajl_gen_alloc(NULL);
|
|
|
|
hand = yajl_alloc(&callbacks, NULL, (void*)g);
|
|
|
|
#else
|
2010-03-27 10:25:51 -04:00
|
|
|
g = yajl_gen_alloc(NULL, NULL);
|
|
|
|
hand = yajl_alloc(&callbacks, NULL, NULL, (void*)g);
|
2011-04-27 13:52:53 -04:00
|
|
|
#endif
|
2010-03-27 10:25:51 -04:00
|
|
|
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;
|
2011-02-20 21:01:55 -05:00
|
|
|
parsing_geometry = false;
|
2010-03-27 10:25:51 -04:00
|
|
|
setlocale(LC_NUMERIC, "C");
|
|
|
|
stat = yajl_parse(hand, (const unsigned char*)buf, n);
|
2011-04-27 13:52:53 -04:00
|
|
|
if (stat != yajl_status_ok)
|
2010-03-27 10:25:51 -04:00
|
|
|
{
|
|
|
|
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, "");
|
2011-04-27 13:52:53 -04:00
|
|
|
#if YAJL_MAJOR >= 2
|
|
|
|
yajl_complete_parse(hand);
|
|
|
|
#else
|
2010-03-27 10:25:51 -04:00
|
|
|
yajl_parse_complete(hand);
|
2011-04-27 13:52:53 -04:00
|
|
|
#endif
|
2010-03-27 10:25:51 -04:00
|
|
|
|
|
|
|
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
|
|
|
}
|