From 3d8c3e560b9c984fbcc06c3a5edbace90a34aebe Mon Sep 17 00:00:00 2001 From: Armin Preiml Date: Sun, 2 Sep 2018 13:22:19 +0200 Subject: [PATCH 1/4] missing ipc stuff: rebase to typesafety --- sway/ipc-json.c | 136 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 120 insertions(+), 16 deletions(-) diff --git a/sway/ipc-json.c b/sway/ipc-json.c index abc4a086..745d129a 100644 --- a/sway/ipc-json.c +++ b/sway/ipc-json.c @@ -30,6 +30,20 @@ static const char *ipc_json_layout_description(enum sway_container_layout l) { return "none"; } +static const char *ipc_json_orientation_description(enum sway_container_layout l) { + switch (l) { + case L_VERT: + return "vertical"; + case L_HORIZ: + return "horizontal"; + case L_TABBED: + case L_STACKED: + case L_NONE: + break; + } + return "none"; +} + json_object *ipc_json_get_version() { int major = 0, minor = 0, patch = 0; json_object *version = json_object_new_object(); @@ -57,9 +71,17 @@ static json_object *ipc_json_create_rect(struct wlr_box *box) { return rect; } +static json_object *ipc_json_create_empty_rect(void) { + struct wlr_box empty = {0, 0, 0, 0}; + + return ipc_json_create_rect(&empty); +} + static void ipc_json_describe_root(struct sway_root *root, json_object *object) { json_object_object_add(object, "type", json_object_new_string("root")); json_object_object_add(object, "layout", json_object_new_string("splith")); + json_object_object_add(object, "orientation", + json_object_new_string(ipc_json_orientation_description(L_HORIZ))); } static const char *ipc_json_get_output_transform(enum wl_output_transform transform) { @@ -124,6 +146,22 @@ static void ipc_json_describe_output(struct sway_output *output, json_object_object_add(object, "modes", modes_array); json_object_object_add(object, "layout", json_object_new_string("output")); + + struct sway_node *parent = node_get_parent(&output->node); + struct wlr_box parent_box = {0, 0, 0, 0}; + + if (parent != NULL) { + node_get_box(parent, &parent_box); + } + + if (parent_box.width == 0 || parent_box.height == 0) { + json_object_object_add(object, "percent", NULL); + } + else { + double percent = (output->width / parent_box.width) + * (output->height / parent_box.height); + json_object_object_add(object, "percent", json_object_new_double(percent)); + } } json_object *ipc_json_describe_disabled_output(struct sway_output *output) { @@ -143,6 +181,8 @@ json_object *ipc_json_describe_disabled_output(struct sway_output *output) { json_object_new_string(wlr_output->serial)); json_object_object_add(object, "modes", json_object_new_array()); + json_object_object_add(object, "percent", NULL); + return object; } @@ -162,6 +202,9 @@ static void ipc_json_describe_workspace(struct sway_workspace *workspace, const char *layout = ipc_json_layout_description(workspace->layout); json_object_object_add(object, "layout", json_object_new_string(layout)); + const char *orientation = ipc_json_orientation_description(workspace->layout); + json_object_object_add(object, "orientation", json_object_new_string(orientation)); + // Floating json_object *floating_array = json_object_new_array(); for (int i = 0; i < workspace->floating->length; ++i) { @@ -172,35 +215,96 @@ static void ipc_json_describe_workspace(struct sway_workspace *workspace, json_object_object_add(object, "floating_nodes", floating_array); } +static const char *describe_container_border(enum sway_container_border border) { + switch (border) { + case B_NONE: + return "none"; + case B_PIXEL: + return "pixel"; + case B_NORMAL: + return "normal"; + } + return "unknown"; +} + static void ipc_json_describe_view(struct sway_container *c, json_object *object) { - json_object_object_add(object, "name", - c->title ? json_object_new_string(c->title) : NULL); - json_object_object_add(object, "type", json_object_new_string("con")); + const char *app_id = view_get_app_id(c->view); + json_object_object_add(object, "app_id", + app_id ? json_object_new_string(app_id) : NULL); + + const char *class = view_get_class(c->view); + json_object_object_add(object, "class", + class ? json_object_new_string(class) : NULL); + + json_object *marks = json_object_new_array(); + list_t *view_marks = c->view->marks; + for (int i = 0; i < view_marks->length; ++i) { + json_object_array_add(marks, json_object_new_string(view_marks->items[i])); + } - if (c->view) { - const char *app_id = view_get_app_id(c->view); - json_object_object_add(object, "app_id", - app_id ? json_object_new_string(app_id) : NULL); + json_object_object_add(object, "marks", marks); - const char *class = view_get_class(c->view); - json_object_object_add(object, "class", - class ? json_object_new_string(class) : NULL); + struct wlr_box window_box = { + c->view->x - c->x, + (c->current.border == B_PIXEL) ? c->current.border_thickness : 0, + c->view->width, + c->view->height + }; + + json_object_object_add(object, "window_rect", ipc_json_create_rect(&window_box)); + + struct wlr_box deco_box = {0, 0, 0, 0}; + + if (c->current.border == B_NORMAL) { + deco_box.width = c->width; + deco_box.height = c->view->y - c->y; } + json_object_object_add(object, "deco_rect", ipc_json_create_rect(&deco_box)); +} + +static void ipc_json_describe_container(struct sway_container *c, json_object *object) { + json_object_object_add(object, "name", + c->title ? json_object_new_string(c->title) : NULL); + json_object_object_add(object, "type", json_object_new_string("con")); + json_object_object_add(object, "layout", json_object_new_string(ipc_json_layout_description(c->layout))); + json_object_object_add(object, "orientation", + json_object_new_string(ipc_json_orientation_description(c->layout))); + bool urgent = c->view ? view_is_urgent(c->view) : container_has_urgent_child(c); json_object_object_add(object, "urgent", json_object_new_boolean(urgent)); + struct sway_node *parent = node_get_parent(&c->node); + struct wlr_box parent_box = {0, 0, 0, 0}; + + if (parent != NULL) { + node_get_box(parent, &parent_box); + } + + if (parent_box.width == 0 || parent_box.height == 0) { + json_object_object_add(object, "percent", NULL); + } + else { + double percent = (c->width / parent_box.width) + * (c->height / parent_box.height); + json_object_object_add(object, "percent", json_object_new_double(percent)); + } + + json_object_object_add(object, "window_rect", ipc_json_create_empty_rect()); + json_object_object_add(object, "deco_rect", ipc_json_create_empty_rect()); + + json_object_object_add(object, "border", + json_object_new_string(describe_container_border(c->current.border))); + json_object_object_add(object, "current_border_width", + json_object_new_int(c->current.border_thickness)); + json_object_object_add(object, "floating_nodes", json_object_new_array()); + if (c->view) { - json_object *marks = json_object_new_array(); - list_t *view_marks = c->view->marks; - for (int i = 0; i < view_marks->length; ++i) { - json_object_array_add(marks, json_object_new_string(view_marks->items[i])); - } - json_object_object_add(object, "marks", marks); + ipc_json_describe_view(c, object); } } From 79eaa26b3e74e9332564dfde1e39fe695a0fb18b Mon Sep 17 00:00:00 2001 From: Armin Preiml Date: Wed, 5 Sep 2018 16:18:12 +0200 Subject: [PATCH 2/4] get_tree: default values Added default values for all nodes, because the i3 get_tree always returns all fields inside the json objects. Add geometry and window for views. Window is only availabel on x11 windows otherwise it's NULL. Calculate percent only if parent is not empty to avoid division by 0. --- sway/ipc-json.c | 49 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/sway/ipc-json.c b/sway/ipc-json.c index 745d129a..fc65b5f2 100644 --- a/sway/ipc-json.c +++ b/sway/ipc-json.c @@ -79,9 +79,6 @@ static json_object *ipc_json_create_empty_rect(void) { static void ipc_json_describe_root(struct sway_root *root, json_object *object) { json_object_object_add(object, "type", json_object_new_string("root")); - json_object_object_add(object, "layout", json_object_new_string("splith")); - json_object_object_add(object, "orientation", - json_object_new_string(ipc_json_orientation_description(L_HORIZ))); } static const char *ipc_json_get_output_transform(enum wl_output_transform transform) { @@ -113,6 +110,8 @@ static void ipc_json_describe_output(struct sway_output *output, json_object_object_add(object, "active", json_object_new_boolean(true)); json_object_object_add(object, "primary", json_object_new_boolean(false)); json_object_object_add(object, "layout", json_object_new_string("output")); + json_object_object_add(object, "orientation", + json_object_new_string(ipc_json_orientation_description(L_NONE))); json_object_object_add(object, "make", json_object_new_string(wlr_output->make)); json_object_object_add(object, "model", @@ -145,7 +144,6 @@ static void ipc_json_describe_output(struct sway_output *output, } json_object_object_add(object, "modes", modes_array); - json_object_object_add(object, "layout", json_object_new_string("output")); struct sway_node *parent = node_get_parent(&output->node); struct wlr_box parent_box = {0, 0, 0, 0}; @@ -154,10 +152,7 @@ static void ipc_json_describe_output(struct sway_output *output, node_get_box(parent, &parent_box); } - if (parent_box.width == 0 || parent_box.height == 0) { - json_object_object_add(object, "percent", NULL); - } - else { + if (parent_box.width != 0 && parent_box.height != 0) { double percent = (output->width / parent_box.width) * (output->height / parent_box.height); json_object_object_add(object, "percent", json_object_new_double(percent)); @@ -261,6 +256,16 @@ static void ipc_json_describe_view(struct sway_container *c, json_object *object } json_object_object_add(object, "deco_rect", ipc_json_create_rect(&deco_box)); + + struct wlr_box geometry = {0, 0, c->view->natural_width, c->view->natural_height }; + json_object_object_add(object, "geometry", ipc_json_create_rect(&geometry)); + +#ifdef HAVE_XWAYLAND + if (c->view->type == SWAY_VIEW_XWAYLAND) { + json_object_object_add(object, "window", + json_object_new_int(view_get_x11_window_id(c->view))); + } +#endif } static void ipc_json_describe_container(struct sway_container *c, json_object *object) { @@ -277,6 +282,7 @@ static void ipc_json_describe_container(struct sway_container *c, json_object *o bool urgent = c->view ? view_is_urgent(c->view) : container_has_urgent_child(c); json_object_object_add(object, "urgent", json_object_new_boolean(urgent)); + json_object_object_add(object, "sticky", json_object_new_boolean(c->is_sticky)); struct sway_node *parent = node_get_parent(&c->node); struct wlr_box parent_box = {0, 0, 0, 0}; @@ -285,18 +291,12 @@ static void ipc_json_describe_container(struct sway_container *c, json_object *o node_get_box(parent, &parent_box); } - if (parent_box.width == 0 || parent_box.height == 0) { - json_object_object_add(object, "percent", NULL); - } - else { + if (parent_box.width != 0 && parent_box.height != 0) { double percent = (c->width / parent_box.width) * (c->height / parent_box.height); json_object_object_add(object, "percent", json_object_new_double(percent)); } - json_object_object_add(object, "window_rect", ipc_json_create_empty_rect()); - json_object_object_add(object, "deco_rect", ipc_json_create_empty_rect()); - json_object_object_add(object, "border", json_object_new_string(describe_container_border(c->current.border))); json_object_object_add(object, "current_border_width", @@ -344,6 +344,23 @@ json_object *ipc_json_describe_node(struct sway_node *node) { seat_for_each_node(seat, focus_inactive_children_iterator, &data); json_object_object_add(object, "focus", focus); + // set default values to be compatible with i3 + json_object_object_add(object, "border", + json_object_new_string(describe_container_border(B_NONE))); + json_object_object_add(object, "current_border_width", json_object_new_int(0)); + json_object_object_add(object, "layout", + json_object_new_string(ipc_json_layout_description(L_HORIZ))); + json_object_object_add(object, "orientation", + json_object_new_string(ipc_json_orientation_description(L_HORIZ))); + json_object_object_add(object, "percent", NULL); + json_object_object_add(object, "window_rect", ipc_json_create_empty_rect()); + json_object_object_add(object, "deco_rect", ipc_json_create_empty_rect()); + json_object_object_add(object, "geometry", ipc_json_create_empty_rect()); + json_object_object_add(object, "window", NULL); + json_object_object_add(object, "urgent", json_object_new_boolean(false)); + json_object_object_add(object, "floating_nodes", json_object_new_array()); + json_object_object_add(object, "sticky", json_object_new_boolean(false)); + switch (node->type) { case N_ROOT: ipc_json_describe_root(root, object); @@ -352,7 +369,7 @@ json_object *ipc_json_describe_node(struct sway_node *node) { ipc_json_describe_output(node->sway_output, object); break; case N_CONTAINER: - ipc_json_describe_view(node->sway_container, object); + ipc_json_describe_container(node->sway_container, object); break; case N_WORKSPACE: ipc_json_describe_workspace(node->sway_workspace, object); From 2dec0d894b3db4c6bdffadbb3b506c32fcfd6354 Mon Sep 17 00:00:00 2001 From: Armin Preiml Date: Thu, 6 Sep 2018 08:46:50 +0200 Subject: [PATCH 3/4] get_tree: percent cast to double on division --- sway/ipc-json.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sway/ipc-json.c b/sway/ipc-json.c index fc65b5f2..682186d5 100644 --- a/sway/ipc-json.c +++ b/sway/ipc-json.c @@ -153,8 +153,8 @@ static void ipc_json_describe_output(struct sway_output *output, } if (parent_box.width != 0 && parent_box.height != 0) { - double percent = (output->width / parent_box.width) - * (output->height / parent_box.height); + double percent = ((double)output->width / (double)parent_box.width) + * ((double)output->height / (double)parent_box.height); json_object_object_add(object, "percent", json_object_new_double(percent)); } } @@ -292,8 +292,8 @@ static void ipc_json_describe_container(struct sway_container *c, json_object *o } if (parent_box.width != 0 && parent_box.height != 0) { - double percent = (c->width / parent_box.width) - * (c->height / parent_box.height); + double percent = ((double)c->width / (double)parent_box.width) + * ((double)c->height / (double)parent_box.height); json_object_object_add(object, "percent", json_object_new_double(percent)); } From 7919ae5e44bc461f3eedaa7ac77f7e4da6b8b28e Mon Sep 17 00:00:00 2001 From: Armin Preiml Date: Sat, 8 Sep 2018 13:11:38 +0200 Subject: [PATCH 4/4] code style fixes --- sway/ipc-json.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/sway/ipc-json.c b/sway/ipc-json.c index 682186d5..8b786145 100644 --- a/sway/ipc-json.c +++ b/sway/ipc-json.c @@ -31,16 +31,14 @@ static const char *ipc_json_layout_description(enum sway_container_layout l) { } static const char *ipc_json_orientation_description(enum sway_container_layout l) { - switch (l) { - case L_VERT: + if (l == L_VERT) { return "vertical"; - case L_HORIZ: + } + + if (l == L_HORIZ) { return "horizontal"; - case L_TABBED: - case L_STACKED: - case L_NONE: - break; } + return "none"; } @@ -153,8 +151,8 @@ static void ipc_json_describe_output(struct sway_output *output, } if (parent_box.width != 0 && parent_box.height != 0) { - double percent = ((double)output->width / (double)parent_box.width) - * ((double)output->height / (double)parent_box.height); + double percent = ((double)output->width / parent_box.width) + * ((double)output->height / parent_box.height); json_object_object_add(object, "percent", json_object_new_double(percent)); } } @@ -257,7 +255,7 @@ static void ipc_json_describe_view(struct sway_container *c, json_object *object json_object_object_add(object, "deco_rect", ipc_json_create_rect(&deco_box)); - struct wlr_box geometry = {0, 0, c->view->natural_width, c->view->natural_height }; + struct wlr_box geometry = {0, 0, c->view->natural_width, c->view->natural_height}; json_object_object_add(object, "geometry", ipc_json_create_rect(&geometry)); #ifdef HAVE_XWAYLAND @@ -292,8 +290,8 @@ static void ipc_json_describe_container(struct sway_container *c, json_object *o } if (parent_box.width != 0 && parent_box.height != 0) { - double percent = ((double)c->width / (double)parent_box.width) - * ((double)c->height / (double)parent_box.height); + double percent = ((double)c->width / parent_box.width) + * ((double)c->height / parent_box.height); json_object_object_add(object, "percent", json_object_new_double(percent)); }