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>
43 static
int json_start_map(
void *
ctx) {
46 LOG(
"creating new swallow\n");
53 DLOG(
"New floating_node\n");
56 json_node->
name = NULL;
58 DLOG(
"Parent is workspace = %p\n", ws);
62 json_node->
name = NULL;
63 json_node->
parent = parent;
75 DLOG(
"Setting layout = L_SPLITH\n");
82 DLOG(
"sanity check: removing swallows specification from split container\n");
85 TAILQ_REMOVE(&(json_node->swallow_head), match, matches);
90 if (json_node->
type == CT_WORKSPACE) {
92 DLOG(
"Attaching workspace. name = %s\n", json_node->
name);
93 if (json_node->
name == NULL || strcmp(json_node->
name,
"") == 0) {
101 Con *workspace = NULL;
106 while (workspace != NULL) {
121 LOG(
"Creating window\n");
123 json_node = json_node->
parent;
134 LOG(
"end of array\n");
145 LOG(
"focus (reverse) %d\n", mapping->
old_id);
150 LOG(
"got it! %p\n", con);
167 static int json_key(
void *
ctx,
const unsigned char *val,
size_t len) {
168 LOG(
"key: %.*s\n", (
int)len, val);
172 if (strcasecmp(
last_key,
"swallows") == 0)
175 if (strcasecmp(
last_key,
"rect") == 0)
178 if (strcasecmp(
last_key,
"deco_rect") == 0)
181 if (strcasecmp(
last_key,
"window_rect") == 0)
184 if (strcasecmp(
last_key,
"geometry") == 0)
187 if (strcasecmp(
last_key,
"focus") == 0)
194 LOG(
"string: %.*s for key %s\n", (
int)len, val,
last_key);
198 if (strcasecmp(
last_key,
"class") == 0) {
200 }
else if (strcasecmp(
last_key,
"instance") == 0) {
202 }
else if (strcasecmp(
last_key,
"window_role") == 0) {
204 }
else if (strcasecmp(
last_key,
"title") == 0) {
211 if (strcasecmp(
last_key,
"name") == 0) {
212 json_node->
name =
scalloc((len + 1) *
sizeof(
char));
213 memcpy(json_node->
name, val, len);
214 }
else if (strcasecmp(
last_key,
"sticky_group") == 0) {
217 LOG(
"sticky_group of this container is %s\n", json_node->
sticky_group);
218 }
else if (strcasecmp(
last_key,
"orientation") == 0) {
227 if (strcasecmp(buf,
"none") == 0 ||
228 strcasecmp(buf,
"horizontal") == 0)
230 else if (strcasecmp(buf,
"vertical") == 0)
233 LOG(
"Unhandled orientation: %s\n", buf);
235 }
else if (strcasecmp(
last_key,
"border") == 0) {
238 if (strcasecmp(buf,
"none") == 0)
240 else if (strcasecmp(buf,
"1pixel") == 0) {
243 }
else if (strcasecmp(buf,
"pixel") == 0)
245 else if (strcasecmp(buf,
"normal") == 0)
248 LOG(
"Unhandled \"border\": %s\n", buf);
250 }
else if (strcasecmp(
last_key,
"type") == 0) {
253 if (strcasecmp(buf,
"root") == 0)
254 json_node->
type = CT_ROOT;
255 else if (strcasecmp(buf,
"output") == 0)
256 json_node->
type = CT_OUTPUT;
257 else if (strcasecmp(buf,
"con") == 0)
258 json_node->
type = CT_CON;
259 else if (strcasecmp(buf,
"floating_con") == 0)
260 json_node->
type = CT_FLOATING_CON;
261 else if (strcasecmp(buf,
"workspace") == 0)
262 json_node->
type = CT_WORKSPACE;
263 else if (strcasecmp(buf,
"dockarea") == 0)
264 json_node->
type = CT_DOCKAREA;
266 LOG(
"Unhandled \"type\": %s\n", buf);
268 }
else if (strcasecmp(
last_key,
"layout") == 0) {
271 if (strcasecmp(buf,
"default") == 0)
274 else if (strcasecmp(buf,
"stacked") == 0)
276 else if (strcasecmp(buf,
"tabbed") == 0)
278 else if (strcasecmp(buf,
"dockarea") == 0)
280 else if (strcasecmp(buf,
"output") == 0)
282 else if (strcasecmp(buf,
"splith") == 0)
284 else if (strcasecmp(buf,
"splitv") == 0)
287 LOG(
"Unhandled \"layout\": %s\n", buf);
289 }
else if (strcasecmp(
last_key,
"workspace_layout") == 0) {
292 if (strcasecmp(buf,
"default") == 0)
294 else if (strcasecmp(buf,
"stacked") == 0)
296 else if (strcasecmp(buf,
"tabbed") == 0)
299 LOG(
"Unhandled \"workspace_layout\": %s\n", buf);
301 }
else if (strcasecmp(
last_key,
"last_split_layout") == 0) {
304 if (strcasecmp(buf,
"splith") == 0)
306 else if (strcasecmp(buf,
"splitv") == 0)
309 LOG(
"Unhandled \"last_splitlayout\": %s\n", buf);
311 }
else if (strcasecmp(
last_key,
"mark") == 0) {
314 json_node->
mark = buf;
315 }
else if (strcasecmp(
last_key,
"floating") == 0) {
318 if (strcasecmp(buf,
"auto_off") == 0)
319 json_node->
floating = FLOATING_AUTO_OFF;
320 else if (strcasecmp(buf,
"auto_on") == 0)
321 json_node->
floating = FLOATING_AUTO_ON;
322 else if (strcasecmp(buf,
"user_off") == 0)
323 json_node->
floating = FLOATING_USER_OFF;
324 else if (strcasecmp(buf,
"user_on") == 0)
325 json_node->
floating = FLOATING_USER_ON;
327 }
else if (strcasecmp(
last_key,
"scratchpad_state") == 0) {
330 if (strcasecmp(buf,
"none") == 0)
332 else if (strcasecmp(buf,
"fresh") == 0)
334 else if (strcasecmp(buf,
"changed") == 0)
345 if (strcasecmp(
last_key,
"type") == 0)
346 json_node->
type = val;
348 if (strcasecmp(
last_key,
"fullscreen_mode") == 0)
351 if (strcasecmp(
last_key,
"num") == 0)
352 json_node->
num = val;
354 if (strcasecmp(
last_key,
"current_border_width") == 0)
357 if (strcasecmp(
last_key,
"depth") == 0)
358 json_node->
depth = val;
365 focus_mapping->
old_id = val;
372 r = &(json_node->
rect);
379 else if (strcasecmp(
last_key,
"y") == 0)
381 else if (strcasecmp(
last_key,
"width") == 0)
383 else if (strcasecmp(
last_key,
"height") == 0)
387 DLOG(
"rect now: (%d, %d, %d, %d)\n",
391 if (strcasecmp(
last_key,
"id") == 0) {
392 current_swallow->
id = val;
394 if (strcasecmp(
last_key,
"dock") == 0) {
395 current_swallow->
dock = val;
397 if (strcasecmp(
last_key,
"insert_where") == 0) {
407 if (strcasecmp(
last_key,
"focused") == 0 && val) {
412 if (strcasecmp(
last_key,
"restart_mode") == 0)
421 if (strcasecmp(
last_key,
"percent") == 0) {
441 if (strcasecmp(
last_key,
"type") != 0 || content_level > 1)
444 DLOG(
"string = %.*s, last_key = %s\n", (
int)len, val,
last_key);
445 if (strncasecmp((
const char *)val,
"workspace", len) == 0)
456 if ((f = fopen(filename,
"r")) == NULL) {
457 ELOG(
"Cannot open file \"%s\"\n", filename);
461 if (fstat(fileno(f), &stbuf) != 0) {
462 ELOG(
"Cannot fstat() \"%s\"\n", filename);
466 char *buf =
smalloc(stbuf.st_size);
467 int n = fread(buf, 1, stbuf.st_size, f);
468 if (n != stbuf.st_size) {
469 ELOG(
"File \"%s\" could not be read entirely, not loading.\n", filename);
473 DLOG(
"read %d bytes\n", n);
480 static yajl_callbacks callbacks = {
488 g = yajl_gen_alloc(NULL);
489 hand = yajl_alloc(&callbacks, NULL, (
void *)g);
491 yajl_config(hand, yajl_allow_comments,
true);
493 yajl_config(hand, yajl_allow_multiple_values,
true);
495 setlocale(LC_NUMERIC,
"C");
496 stat = yajl_parse(hand, (
const unsigned char *)buf, n);
497 if (stat != yajl_status_ok && stat != yajl_status_client_canceled) {
498 unsigned char *str = yajl_get_error(hand, 1, (
const unsigned char *)buf, n);
499 ELOG(
"JSON parsing error: %s\n", str);
500 yajl_free_error(hand, str);
503 setlocale(LC_NUMERIC,
"");
504 yajl_complete_parse(hand);
513 if ((f = fopen(filename,
"r")) == NULL) {
514 ELOG(
"Cannot open file \"%s\"\n", filename);
518 if (fstat(fileno(f), &stbuf) != 0) {
519 ELOG(
"Cannot fstat() \"%s\"\n", filename);
523 char *buf =
smalloc(stbuf.st_size);
524 int n = fread(buf, 1, stbuf.st_size, f);
525 if (n != stbuf.st_size) {
526 ELOG(
"File \"%s\" could not be read entirely, not loading.\n", filename);
530 DLOG(
"read %d bytes\n", n);
533 static yajl_callbacks callbacks = {
538 .yajl_start_map = json_start_map,
543 g = yajl_gen_alloc(NULL);
544 hand = yajl_alloc(&callbacks, NULL, (
void *)g);
546 yajl_config(hand, yajl_allow_comments,
true);
548 yajl_config(hand, yajl_allow_multiple_values,
true);
558 setlocale(LC_NUMERIC,
"C");
559 stat = yajl_parse(hand, (
const unsigned char *)buf, n);
560 if (stat != yajl_status_ok) {
561 unsigned char *str = yajl_get_error(hand, 1, (
const unsigned char *)buf, n);
562 ELOG(
"JSON parsing error: %s\n", str);
563 if (errormsg != NULL)
564 *errormsg =
sstrdup((
const char *)str);
565 yajl_free_error(hand, str);
573 setlocale(LC_NUMERIC,
"");
574 yajl_complete_parse(hand);
enum Con::@20 scratchpad_state
long ws_name_to_number(const char *name)
Parses the workspace name as a number.
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
json_content_t json_determine_content(const char *filename)
#define TAILQ_REMOVE(head, elm, field)
void con_attach(Con *con, Con *parent, bool ignore_focus)
Attaches the given container to the given parent.
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
Stores a rectangle, for example the size of a window, the child window etc.
static int json_determine_content_shallower(void *ctx)
struct Match * current_swallow
static int json_determine_content_deeper(void *ctx)
static int json_bool(void *ctx, int val)
struct regex * window_role
#define TAILQ_FOREACH_REVERSE(var, head, headname, field)
#define TAILQ_FIRST(head)
static json_content_t content_result
static int json_determine_content_string(void *ctx, const unsigned char *val, size_t len)
static int json_double(void *ctx, double val)
enum Con::@19 floating
floating? (= not in tiling layout) This cannot be simply a bool because we want to keep track of whet...
void tree_append_json(Con *con, const char *filename, char **errormsg)
border_style_t border_style
static bool parsing_focus
static bool parsing_deco_rect
struct regex * regex_new(const char *pattern)
Creates a new 'regex' struct containing the given pattern and a PCRE compiled regular expression...
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...
static int json_key(void *ctx, const unsigned char *val, size_t len)
static int json_end_array(void *ctx)
#define TAILQ_INSERT_HEAD(head, elm, field)
struct Rect geometry
the geometry this window requested when getting mapped
int num
the workspace number, if this Con is of type CT_WORKSPACE and the workspace is not a named workspace ...
fullscreen_mode_t fullscreen_mode
void con_focus(Con *con)
Sets input focus to the given container.
#define TAILQ_EMPTY(head)
static int json_int(void *ctx, long long val)
bool con_is_split(Con *con)
#define TAILQ_ENTRY(type)
A "match" is a data structure which acts like a mask or expression to match certain windows or not...
static xcb_cursor_context_t * ctx
#define GREP_FIRST(dest, head, condition)
A 'Con' represents everything from the X11 root window down to a single X11 window.
void * scalloc(size_t size)
Safe-wrapper around calloc which exits if malloc returns NULL (meaning that there is no more memory a...
layout_t last_split_layout
layout_t workspace_layout
enum Match::@15 insert_where
#define TAILQ_INSERT_TAIL(head, elm, field)
#define TAILQ_HEAD_INITIALIZER(head)
Con * output_get_content(Con *output)
Returns the output container below the given output container.
static bool parsing_swallows
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_FOREACH(var, head, field)
void con_fix_percent(Con *con)
Updates the percent attribute of the children of the given container.
static bool parsing_window_rect
static bool parsing_geometry
static int json_end_map(void *ctx)
static int json_string(void *ctx, const unsigned char *val, size_t len)
void match_init(Match *match)
Con * con_new_skeleton(Con *parent, i3Window *window)
Create a new container (and attach it to the given parent, if not NULL).
static TAILQ_HEAD(focus_mappings_head, focus_mapping)
void match_free(Match *match)
Frees the given match.
void x_con_init(Con *con, uint16_t depth)
Initializes the X11 part for the given container.