13 #include <yajl/yajl_common.h> 14 #include <yajl/yajl_gen.h> 15 #include <yajl/yajl_parse.h> 16 #include <yajl/yajl_version.h> 43 static int json_start_map(
void *
ctx) {
46 LOG(
"creating new swallow\n");
49 current_swallow->
dock = M_DONTCHECK;
55 DLOG(
"New floating_node\n");
58 json_node->
name = NULL;
60 DLOG(
"Parent is workspace = %p\n", ws);
64 json_node->
name = NULL;
65 json_node->
parent = parent;
77 DLOG(
"Setting layout = L_SPLITH\n");
84 DLOG(
"sanity check: removing swallows specification from split container\n");
87 TAILQ_REMOVE(&(json_node->swallow_head), match, matches);
93 if (json_node->
type == CT_WORKSPACE) {
95 DLOG(
"Attaching workspace. name = %s\n", json_node->
name);
96 if (json_node->
name == NULL || strcmp(json_node->
name,
"") == 0) {
104 Con *workspace = NULL;
109 while (workspace != NULL) {
128 if (json_node->
type == CT_FLOATING_CON) {
135 const Rect zero = {0, 0, 0, 0};
136 if (memcmp(&(json_node->
rect), &zero,
sizeof(
Rect)) == 0) {
137 DLOG(
"Geometry not set, combining children\n");
151 LOG(
"Creating window\n");
153 json_node = json_node->
parent;
159 ELOG(
"Layout file is invalid: found an empty swallow definition.\n");
171 LOG(
"end of array\n");
186 LOG(
"focus (reverse) %d\n", mapping->
old_id);
191 LOG(
"got it! %p\n", con);
208 static int json_key(
void *ctx,
const unsigned char *val,
size_t len) {
209 LOG(
"key: %.*s\n", (
int)len, val);
213 if (strcasecmp(
last_key,
"swallows") == 0)
216 if (strcasecmp(
last_key,
"rect") == 0)
219 if (strcasecmp(
last_key,
"deco_rect") == 0)
222 if (strcasecmp(
last_key,
"window_rect") == 0)
225 if (strcasecmp(
last_key,
"geometry") == 0)
228 if (strcasecmp(
last_key,
"focus") == 0)
231 if (strcasecmp(
last_key,
"marks") == 0)
237 static int json_string(
void *ctx,
const unsigned char *val,
size_t len) {
238 LOG(
"string: %.*s for key %s\n", (
int)len, val,
last_key);
242 if (strcasecmp(
last_key,
"class") == 0) {
245 }
else if (strcasecmp(
last_key,
"instance") == 0) {
248 }
else if (strcasecmp(
last_key,
"window_role") == 0) {
251 }
else if (strcasecmp(
last_key,
"title") == 0) {
264 if (strcasecmp(
last_key,
"name") == 0) {
266 memcpy(json_node->
name, val, len);
267 }
else if (strcasecmp(
last_key,
"title_format") == 0) {
270 }
else if (strcasecmp(
last_key,
"sticky_group") == 0) {
273 LOG(
"sticky_group of this container is %s\n", json_node->
sticky_group);
274 }
else if (strcasecmp(
last_key,
"orientation") == 0) {
283 if (strcasecmp(buf,
"none") == 0 ||
284 strcasecmp(buf,
"horizontal") == 0)
286 else if (strcasecmp(buf,
"vertical") == 0)
289 LOG(
"Unhandled orientation: %s\n", buf);
291 }
else if (strcasecmp(
last_key,
"border") == 0) {
294 if (strcasecmp(buf,
"none") == 0)
296 else if (strcasecmp(buf,
"1pixel") == 0) {
299 }
else if (strcasecmp(buf,
"pixel") == 0)
301 else if (strcasecmp(buf,
"normal") == 0)
304 LOG(
"Unhandled \"border\": %s\n", buf);
306 }
else if (strcasecmp(
last_key,
"type") == 0) {
309 if (strcasecmp(buf,
"root") == 0)
310 json_node->
type = CT_ROOT;
311 else if (strcasecmp(buf,
"output") == 0)
312 json_node->
type = CT_OUTPUT;
313 else if (strcasecmp(buf,
"con") == 0)
314 json_node->
type = CT_CON;
315 else if (strcasecmp(buf,
"floating_con") == 0)
316 json_node->
type = CT_FLOATING_CON;
317 else if (strcasecmp(buf,
"workspace") == 0)
318 json_node->
type = CT_WORKSPACE;
319 else if (strcasecmp(buf,
"dockarea") == 0)
320 json_node->
type = CT_DOCKAREA;
322 LOG(
"Unhandled \"type\": %s\n", buf);
324 }
else if (strcasecmp(
last_key,
"layout") == 0) {
327 if (strcasecmp(buf,
"default") == 0)
330 else if (strcasecmp(buf,
"stacked") == 0)
332 else if (strcasecmp(buf,
"tabbed") == 0)
334 else if (strcasecmp(buf,
"dockarea") == 0)
336 else if (strcasecmp(buf,
"output") == 0)
338 else if (strcasecmp(buf,
"splith") == 0)
340 else if (strcasecmp(buf,
"splitv") == 0)
343 LOG(
"Unhandled \"layout\": %s\n", buf);
345 }
else if (strcasecmp(
last_key,
"workspace_layout") == 0) {
348 if (strcasecmp(buf,
"default") == 0)
350 else if (strcasecmp(buf,
"stacked") == 0)
352 else if (strcasecmp(buf,
"tabbed") == 0)
355 LOG(
"Unhandled \"workspace_layout\": %s\n", buf);
357 }
else if (strcasecmp(
last_key,
"last_split_layout") == 0) {
360 if (strcasecmp(buf,
"splith") == 0)
362 else if (strcasecmp(buf,
"splitv") == 0)
365 LOG(
"Unhandled \"last_splitlayout\": %s\n", buf);
367 }
else if (strcasecmp(
last_key,
"mark") == 0) {
368 DLOG(
"Found deprecated key \"mark\".\n");
374 }
else if (strcasecmp(
last_key,
"floating") == 0) {
377 if (strcasecmp(buf,
"auto_off") == 0)
378 json_node->
floating = FLOATING_AUTO_OFF;
379 else if (strcasecmp(buf,
"auto_on") == 0)
380 json_node->
floating = FLOATING_AUTO_ON;
381 else if (strcasecmp(buf,
"user_off") == 0)
382 json_node->
floating = FLOATING_USER_OFF;
383 else if (strcasecmp(buf,
"user_on") == 0)
384 json_node->
floating = FLOATING_USER_ON;
386 }
else if (strcasecmp(
last_key,
"scratchpad_state") == 0) {
389 if (strcasecmp(buf,
"none") == 0)
391 else if (strcasecmp(buf,
"fresh") == 0)
393 else if (strcasecmp(buf,
"changed") == 0)
404 if (strcasecmp(
last_key,
"type") == 0)
405 json_node->
type = val;
407 if (strcasecmp(
last_key,
"fullscreen_mode") == 0)
410 if (strcasecmp(
last_key,
"num") == 0)
411 json_node->
num = val;
413 if (strcasecmp(
last_key,
"current_border_width") == 0)
416 if (strcasecmp(
last_key,
"depth") == 0)
417 json_node->
depth = val;
424 focus_mapping->
old_id = val;
431 r = &(json_node->
rect);
438 else if (strcasecmp(
last_key,
"y") == 0)
440 else if (strcasecmp(
last_key,
"width") == 0)
442 else if (strcasecmp(
last_key,
"height") == 0)
446 DLOG(
"rect now: (%d, %d, %d, %d)\n",
450 if (strcasecmp(
last_key,
"id") == 0) {
451 current_swallow->
id = val;
454 if (strcasecmp(
last_key,
"dock") == 0) {
455 current_swallow->
dock = val;
458 if (strcasecmp(
last_key,
"insert_where") == 0) {
469 if (strcasecmp(
last_key,
"focused") == 0 && val) {
473 if (strcasecmp(
last_key,
"sticky") == 0)
477 if (strcasecmp(
last_key,
"restart_mode") == 0) {
488 if (strcasecmp(
last_key,
"percent") == 0) {
508 if (strcasecmp(
last_key,
"type") != 0 || content_level > 1)
511 DLOG(
"string = %.*s, last_key = %s\n", (
int)len, val,
last_key);
512 if (strncasecmp((
const char *)val,
"workspace", len) == 0)
523 if ((f = fopen(filename,
"r")) == NULL) {
524 ELOG(
"Cannot open file \"%s\"\n", filename);
528 if (fstat(fileno(f), &stbuf) != 0) {
529 ELOG(
"Cannot fstat() \"%s\"\n", filename);
533 char *buf =
smalloc(stbuf.st_size);
534 int n = fread(buf, 1, stbuf.st_size, f);
535 if (n != stbuf.st_size) {
536 ELOG(
"File \"%s\" could not be read entirely, not loading.\n", filename);
540 DLOG(
"read %d bytes\n", n);
547 static yajl_callbacks callbacks = {
555 g = yajl_gen_alloc(NULL);
556 hand = yajl_alloc(&callbacks, NULL, (
void *)g);
558 yajl_config(hand, yajl_allow_comments,
true);
560 yajl_config(hand, yajl_allow_multiple_values,
true);
562 setlocale(LC_NUMERIC,
"C");
563 stat = yajl_parse(hand, (
const unsigned char *)buf, n);
564 if (stat != yajl_status_ok && stat != yajl_status_client_canceled) {
565 unsigned char *str = yajl_get_error(hand, 1, (
const unsigned char *)buf, n);
566 ELOG(
"JSON parsing error: %s\n", str);
567 yajl_free_error(hand, str);
570 setlocale(LC_NUMERIC,
"");
571 yajl_complete_parse(hand);
580 if ((f = fopen(filename,
"r")) == NULL) {
581 ELOG(
"Cannot open file \"%s\"\n", filename);
585 if (fstat(fileno(f), &stbuf) != 0) {
586 ELOG(
"Cannot fstat() \"%s\"\n", filename);
590 char *buf =
smalloc(stbuf.st_size);
591 int n = fread(buf, 1, stbuf.st_size, f);
592 if (n != stbuf.st_size) {
593 ELOG(
"File \"%s\" could not be read entirely, not loading.\n", filename);
597 DLOG(
"read %d bytes\n", n);
600 static yajl_callbacks callbacks = {
605 .yajl_start_map = json_start_map,
610 g = yajl_gen_alloc(NULL);
611 hand = yajl_alloc(&callbacks, NULL, (
void *)g);
613 yajl_config(hand, yajl_allow_comments,
true);
615 yajl_config(hand, yajl_allow_multiple_values,
true);
626 setlocale(LC_NUMERIC,
"C");
627 stat = yajl_parse(hand, (
const unsigned char *)buf, n);
628 if (stat != yajl_status_ok) {
629 unsigned char *str = yajl_get_error(hand, 1, (
const unsigned char *)buf, n);
630 ELOG(
"JSON parsing error: %s\n", str);
631 if (errormsg != NULL)
632 *errormsg =
sstrdup((
const char *)str);
633 yajl_free_error(hand, str);
641 setlocale(LC_NUMERIC,
"");
642 yajl_complete_parse(hand);
#define TAILQ_FOREACH_REVERSE(var, head, headname, field)
static int json_string(void *ctx, const unsigned char *val, size_t len)
void con_fix_percent(Con *con)
Updates the percent attribute of the children of the given container.
static bool parsing_focus
static int json_end_array(void *ctx)
int num
the workspace number, if this Con is of type CT_WORKSPACE and the workspace is not a named workspace ...
layout_t workspace_layout
#define TAILQ_ENTRY(type)
static bool parsing_swallows
Con * con_new_skeleton(Con *parent, i3Window *window)
Create a new container (and attach it to the given parent, if not NULL).
layout_t last_split_layout
fullscreen_mode_t fullscreen_mode
void con_focus(Con *con)
Sets input focus to the given container.
static int json_key(void *ctx, const unsigned char *val, size_t len)
static bool parsing_marks
static json_content_t content_result
void match_free(Match *match)
Frees the given match.
Con * output_get_content(Con *output)
Returns the output container below the given output container.
void x_con_init(Con *con)
Initializes the X11 part for the given container.
static int json_double(void *ctx, double val)
enum Match::@17 insert_where
Stores a rectangle, for example the size of a window, the child window etc.
json_content_t json_determine_content(const char *filename)
void con_mark(Con *con, const char *mark, mark_mode_t mode)
Assigns a mark to the container.
#define TAILQ_INSERT_TAIL(head, elm, field)
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_HEAD_INITIALIZER(head)
#define TAILQ_FOREACH(var, head, field)
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
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...
long ws_name_to_number(const char *name)
Parses the workspace name as a number.
#define TAILQ_FIRST(head)
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...
static TAILQ_HEAD(focus_mappings_head, focus_mapping)
A "match" is a data structure which acts like a mask or expression to match certain windows or not...
static int json_end_map(void *ctx)
border_style_t border_style
#define TAILQ_REMOVE(head, elm, field)
struct regex * window_role
bool con_is_split(Con *con)
Returns true if a container should be considered split.
void match_init(Match *match)
static bool parsing_window_rect
void tree_append_json(Con *con, const char *filename, char **errormsg)
struct Rect geometry
the geometry this window requested when getting mapped
struct Match * current_swallow
static int json_determine_content_string(void *ctx, const unsigned char *val, size_t len)
static xcb_cursor_context_t * ctx
void * smalloc(size_t size)
Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory a...
static int json_determine_content_shallower(void *ctx)
static int json_determine_content_deeper(void *ctx)
A 'Con' represents everything from the X11 root window down to a single X11 window.
static int json_int(void *ctx, long long val)
static bool swallow_is_empty
#define GREP_FIRST(dest, head, condition)
enum Con::@21 floating
floating? (= not in tiling layout) This cannot be simply a bool because we want to keep track of whet...
static bool parsing_geometry
void floating_check_size(Con *floating_con)
Called when a floating window is created or resized.
enum Con::@22 scratchpad_state
void con_attach(Con *con, Con *parent, bool ignore_focus)
Attaches the given container to the given parent.
#define TAILQ_EMPTY(head)
static bool parsing_deco_rect
char * title_format
The format with which the window's name should be displayed.
static int json_bool(void *ctx, int val)
#define TAILQ_INSERT_HEAD(head, elm, field)
struct regex * regex_new(const char *pattern)
Creates a new 'regex' struct containing the given pattern and a PCRE compiled regular expression...