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 static
int json_start_map(
void *
ctx) {
48 LOG(
"creating new swallow\n");
56 DLOG(
"New floating_node\n");
59 json_node->
name = NULL;
61 DLOG(
"Parent is workspace = %p\n", ws);
65 json_node->
name = NULL;
66 json_node->
parent = parent;
78 DLOG(
"Setting layout = L_SPLITH\n");
85 DLOG(
"sanity check: removing swallows specification from split container\n");
88 TAILQ_REMOVE(&(json_node->swallow_head), match, matches);
94 if (json_node->
type == CT_WORKSPACE) {
96 DLOG(
"Attaching workspace. name = %s\n", json_node->
name);
97 if (json_node->
name == NULL || strcmp(json_node->
name,
"") == 0) {
105 Con *workspace = NULL;
110 while (workspace != NULL) {
129 if (json_node->
type == CT_FLOATING_CON) {
136 const Rect zero = {0, 0, 0, 0};
137 if (memcmp(&(json_node->
rect), &zero,
sizeof(
Rect)) == 0) {
138 DLOG(
"Geometry not set, combining children\n");
152 LOG(
"Creating window\n");
154 json_node = json_node->
parent;
160 ELOG(
"Layout file is invalid: found an empty swallow definition.\n");
172 LOG(
"end of array\n");
187 LOG(
"focus (reverse) %d\n", mapping->
old_id);
192 LOG(
"got it! %p\n", con);
209 static int json_key(
void *
ctx,
const unsigned char *val,
size_t len) {
210 LOG(
"key: %.*s\n", (
int)len, val);
214 if (strcasecmp(
last_key,
"swallows") == 0)
217 if (strcasecmp(
last_key,
"rect") == 0)
220 if (strcasecmp(
last_key,
"deco_rect") == 0)
223 if (strcasecmp(
last_key,
"window_rect") == 0)
226 if (strcasecmp(
last_key,
"geometry") == 0)
229 if (strcasecmp(
last_key,
"focus") == 0)
232 if (strcasecmp(
last_key,
"marks") == 0)
239 LOG(
"string: %.*s for key %s\n", (
int)len, val,
last_key);
243 if (strcasecmp(
last_key,
"class") == 0) {
246 }
else if (strcasecmp(
last_key,
"instance") == 0) {
249 }
else if (strcasecmp(
last_key,
"window_role") == 0) {
252 }
else if (strcasecmp(
last_key,
"title") == 0) {
265 if (strcasecmp(
last_key,
"name") == 0) {
267 memcpy(json_node->
name, val, len);
268 }
else if (strcasecmp(
last_key,
"title_format") == 0) {
271 }
else if (strcasecmp(
last_key,
"sticky_group") == 0) {
274 LOG(
"sticky_group of this container is %s\n", json_node->
sticky_group);
275 }
else if (strcasecmp(
last_key,
"orientation") == 0) {
284 if (strcasecmp(buf,
"none") == 0 ||
285 strcasecmp(buf,
"horizontal") == 0)
287 else if (strcasecmp(buf,
"vertical") == 0)
290 LOG(
"Unhandled orientation: %s\n", buf);
292 }
else if (strcasecmp(
last_key,
"border") == 0) {
295 if (strcasecmp(buf,
"none") == 0)
297 else if (strcasecmp(buf,
"1pixel") == 0) {
300 }
else if (strcasecmp(buf,
"pixel") == 0)
302 else if (strcasecmp(buf,
"normal") == 0)
305 LOG(
"Unhandled \"border\": %s\n", buf);
307 }
else if (strcasecmp(
last_key,
"type") == 0) {
310 if (strcasecmp(buf,
"root") == 0)
311 json_node->
type = CT_ROOT;
312 else if (strcasecmp(buf,
"output") == 0)
313 json_node->
type = CT_OUTPUT;
314 else if (strcasecmp(buf,
"con") == 0)
315 json_node->
type = CT_CON;
316 else if (strcasecmp(buf,
"floating_con") == 0)
317 json_node->
type = CT_FLOATING_CON;
318 else if (strcasecmp(buf,
"workspace") == 0)
319 json_node->
type = CT_WORKSPACE;
320 else if (strcasecmp(buf,
"dockarea") == 0)
321 json_node->
type = CT_DOCKAREA;
323 LOG(
"Unhandled \"type\": %s\n", buf);
325 }
else if (strcasecmp(
last_key,
"layout") == 0) {
328 if (strcasecmp(buf,
"default") == 0)
331 else if (strcasecmp(buf,
"stacked") == 0)
333 else if (strcasecmp(buf,
"tabbed") == 0)
335 else if (strcasecmp(buf,
"dockarea") == 0)
337 else if (strcasecmp(buf,
"output") == 0)
339 else if (strcasecmp(buf,
"splith") == 0)
341 else if (strcasecmp(buf,
"splitv") == 0)
344 LOG(
"Unhandled \"layout\": %s\n", buf);
346 }
else if (strcasecmp(
last_key,
"workspace_layout") == 0) {
349 if (strcasecmp(buf,
"default") == 0)
351 else if (strcasecmp(buf,
"stacked") == 0)
353 else if (strcasecmp(buf,
"tabbed") == 0)
356 LOG(
"Unhandled \"workspace_layout\": %s\n", buf);
358 }
else if (strcasecmp(
last_key,
"last_split_layout") == 0) {
361 if (strcasecmp(buf,
"splith") == 0)
363 else if (strcasecmp(buf,
"splitv") == 0)
366 LOG(
"Unhandled \"last_splitlayout\": %s\n", buf);
368 }
else if (strcasecmp(
last_key,
"mark") == 0) {
369 DLOG(
"Found deprecated key \"mark\".\n");
375 }
else if (strcasecmp(
last_key,
"floating") == 0) {
378 if (strcasecmp(buf,
"auto_off") == 0)
379 json_node->
floating = FLOATING_AUTO_OFF;
380 else if (strcasecmp(buf,
"auto_on") == 0)
381 json_node->
floating = FLOATING_AUTO_ON;
382 else if (strcasecmp(buf,
"user_off") == 0)
383 json_node->
floating = FLOATING_USER_OFF;
384 else if (strcasecmp(buf,
"user_on") == 0)
385 json_node->
floating = FLOATING_USER_ON;
387 }
else if (strcasecmp(
last_key,
"scratchpad_state") == 0) {
390 if (strcasecmp(buf,
"none") == 0)
392 else if (strcasecmp(buf,
"fresh") == 0)
394 else if (strcasecmp(buf,
"changed") == 0)
405 if (strcasecmp(
last_key,
"type") == 0)
406 json_node->
type = val;
408 if (strcasecmp(
last_key,
"fullscreen_mode") == 0)
411 if (strcasecmp(
last_key,
"num") == 0)
412 json_node->
num = val;
414 if (strcasecmp(
last_key,
"current_border_width") == 0)
417 if (strcasecmp(
last_key,
"depth") == 0)
418 json_node->
depth = val;
425 focus_mapping->
old_id = val;
432 r = &(json_node->
rect);
439 else if (strcasecmp(
last_key,
"y") == 0)
441 else if (strcasecmp(
last_key,
"width") == 0)
443 else if (strcasecmp(
last_key,
"height") == 0)
447 DLOG(
"rect now: (%d, %d, %d, %d)\n",
451 if (strcasecmp(
last_key,
"id") == 0) {
452 current_swallow->
id = val;
455 if (strcasecmp(
last_key,
"dock") == 0) {
456 current_swallow->
dock = val;
459 if (strcasecmp(
last_key,
"insert_where") == 0) {
470 if (strcasecmp(
last_key,
"focused") == 0 && val) {
474 if (strcasecmp(
last_key,
"sticky") == 0)
478 if (strcasecmp(
last_key,
"restart_mode") == 0) {
489 if (strcasecmp(
last_key,
"percent") == 0) {
512 DLOG(
"string = %.*s, last_key = %s\n", (
int)len, val,
last_key);
513 if (strncasecmp((
const char *)val,
"workspace", len) == 0)
524 if ((f = fopen(filename,
"r")) == NULL) {
525 ELOG(
"Cannot open file \"%s\"\n", filename);
529 if (fstat(fileno(f), &stbuf) != 0) {
530 ELOG(
"Cannot fstat() \"%s\"\n", filename);
534 char *buf =
smalloc(stbuf.st_size);
535 int n = fread(buf, 1, stbuf.st_size, f);
536 if (n != stbuf.st_size) {
537 ELOG(
"File \"%s\" could not be read entirely, not loading.\n", filename);
541 DLOG(
"read %d bytes\n", n);
548 static yajl_callbacks callbacks = {
556 g = yajl_gen_alloc(NULL);
557 hand = yajl_alloc(&callbacks, NULL, (
void *)g);
559 yajl_config(hand, yajl_allow_comments,
true);
561 yajl_config(hand, yajl_allow_multiple_values,
true);
563 setlocale(LC_NUMERIC,
"C");
564 stat = yajl_parse(hand, (
const unsigned char *)buf, n);
565 if (stat != yajl_status_ok && stat != yajl_status_client_canceled) {
566 unsigned char *str = yajl_get_error(hand, 1, (
const unsigned char *)buf, n);
567 ELOG(
"JSON parsing error: %s\n", str);
568 yajl_free_error(hand, str);
571 setlocale(LC_NUMERIC,
"");
572 yajl_complete_parse(hand);
581 if ((f = fopen(filename,
"r")) == NULL) {
582 ELOG(
"Cannot open file \"%s\"\n", filename);
586 if (fstat(fileno(f), &stbuf) != 0) {
587 ELOG(
"Cannot fstat() \"%s\"\n", filename);
591 char *buf =
smalloc(stbuf.st_size);
592 int n = fread(buf, 1, stbuf.st_size, f);
593 if (n != stbuf.st_size) {
594 ELOG(
"File \"%s\" could not be read entirely, not loading.\n", filename);
598 DLOG(
"read %d bytes\n", n);
601 static yajl_callbacks callbacks = {
606 .yajl_start_map = json_start_map,
611 g = yajl_gen_alloc(NULL);
612 hand = yajl_alloc(&callbacks, NULL, (
void *)g);
614 yajl_config(hand, yajl_allow_comments,
true);
616 yajl_config(hand, yajl_allow_multiple_values,
true);
627 setlocale(LC_NUMERIC,
"C");
628 stat = yajl_parse(hand, (
const unsigned char *)buf, n);
629 if (stat != yajl_status_ok) {
630 unsigned char *str = yajl_get_error(hand, 1, (
const unsigned char *)buf, n);
631 ELOG(
"JSON parsing error: %s\n", str);
632 if (errormsg != NULL)
633 *errormsg =
sstrdup((
const char *)str);
634 yajl_free_error(hand, str);
642 setlocale(LC_NUMERIC,
"");
643 yajl_complete_parse(hand);
enum Con::@22 scratchpad_state
void con_mark(Con *con, const char *mark, mark_mode_t mode)
Assigns a mark to the container.
char * title_format
The format with which the window's name should be displayed.
#define TAILQ_ENTRY(type)
int num
the workspace number, if this Con is of type CT_WORKSPACE and the workspace is not a named workspace ...
void x_con_init(Con *con, uint16_t depth)
Initializes the X11 part for the given container.
static int json_end_map(void *ctx)
void con_focus(Con *con)
Sets input focus to the given container.
void * smalloc(size_t size)
Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory a...
#define TAILQ_EMPTY(head)
Con * output_get_content(Con *output)
Returns the output container below the given output container.
void tree_append_json(Con *con, const char *filename, char **errormsg)
enum Con::@21 floating
floating? (= not in tiling layout) This cannot be simply a bool because we want to keep track of whet...
void match_init(Match *match)
#define TAILQ_FOREACH(var, head, field)
void match_free(Match *match)
Frees the given match.
void con_attach(Con *con, Con *parent, bool ignore_focus)
Attaches the given container to the given parent.
void * scalloc(size_t num, size_t size)
Safe-wrapper around calloc which exits if malloc returns NULL (meaning that there is no more memory a...
fullscreen_mode_t fullscreen_mode
static int json_bool(void *ctx, int val)
static bool parsing_swallows
static int json_end_array(void *ctx)
static json_content_t content_result
#define GREP_FIRST(dest, head, condition)
struct Rect geometry
the geometry this window requested when getting mapped
#define TAILQ_REMOVE(head, elm, field)
static int json_determine_content_deeper(void *ctx)
static xcb_cursor_context_t * ctx
static int json_key(void *ctx, const unsigned char *val, size_t len)
#define TAILQ_HEAD(name, type)
static bool swallow_is_empty
#define TAILQ_FOREACH_REVERSE(var, head, headname, field)
static bool parsing_deco_rect
#define TAILQ_INSERT_HEAD(head, elm, field)
Stores a rectangle, for example the size of a window, the child window etc.
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
static int json_double(void *ctx, double val)
json_content_t json_determine_content(const char *filename)
static bool parsing_marks
struct regex * window_role
enum Match::@17 insert_where
static int json_determine_content_string(void *ctx, const unsigned char *val, size_t len)
A 'Con' represents everything from the X11 root window down to a single X11 window.
#define TAILQ_FIRST(head)
static int json_int(void *ctx, long long val)
int sasprintf(char **strp, const char *fmt,...)
Safe-wrapper around asprintf which exits if it returns -1 (meaning that there is no more memory avail...
void con_fix_percent(Con *con)
Updates the percent attribute of the children of the given container.
bool con_is_split(Con *con)
Returns true if a container should be considered split.
static bool parsing_window_rect
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
#define TAILQ_INSERT_TAIL(head, elm, field)
Con * con_new_skeleton(Con *parent, i3Window *window)
Create a new container (and attach it to the given parent, if not NULL).
border_style_t border_style
static bool parsing_focus
static int json_string(void *ctx, const unsigned char *val, size_t len)
struct regex * regex_new(const char *pattern)
Creates a new 'regex' struct containing the given pattern and a PCRE compiled regular expression...
long ws_name_to_number(const char *name)
Parses the workspace name as a number.
void floating_check_size(Con *floating_con)
Called when a floating window is created or resized.
A "match" is a data structure which acts like a mask or expression to match certain windows or not...
layout_t workspace_layout
struct Match * current_swallow
layout_t last_split_layout
#define TAILQ_HEAD_INITIALIZER(head)
static int json_determine_content_shallower(void *ctx)
static bool parsing_geometry