From 7586f150c058997d9dde387ea7c091ffa7a3c3c7 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Thu, 30 Aug 2018 21:00:10 +1000 Subject: Implement type safe arguments and demote sway_container This commit changes the meaning of sway_container so that it only refers to layout containers and view containers. Workspaces, outputs and the root are no longer known as containers. Instead, root, outputs, workspaces and containers are all a type of node, and containers come in two types: layout containers and view containers. In addition to the above, this implements type safe variables. This means we use specific types such as sway_output and sway_workspace instead of generic containers or nodes. However, it's worth noting that in a few places places (eg. seat focus and transactions) referring to them in a generic way is unavoidable which is why we still use nodes in some places. If you want a TL;DR, look at node.h, as well as the struct definitions for root, output, workspace and container. Note that sway_output now contains a workspaces list, and workspaces now contain a tiling and floating list, and containers now contain a pointer back to the workspace. There are now functions for seat_get_focused_workspace and seat_get_focused_container. The latter will return NULL if a workspace itself is focused. Most other seat functions like seat_get_focus and seat_set_focus now accept and return nodes. In the config->handler_context struct, current_container has been replaced with three pointers: node, container and workspace. node is the same as what current_container was, while workspace is the workspace that the node resides on and container is the actual container, which may be NULL if a workspace itself is focused. The global root_container variable has been replaced with one simply called root, which is a pointer to the sway_root instance. The way outputs are created, enabled, disabled and destroyed has changed. Previously we'd wrap the sway_output in a container when it is enabled, but as we don't have containers any more it needs a different approach. The output_create and output_destroy functions previously created/destroyed the container, but now they create/destroy the sway_output. There is a new function output_disable to disable an output without destroying it. Containers have a new view property. If this is populated then the container is a view container, otherwise it's a layout container. Like before, this property is immutable for the life of the container. Containers have both a `sway_container *parent` and `sway_workspace *workspace`. As we use specific types now, parent cannot point to a workspace so it'll be NULL for containers which are direct children of the workspace. The workspace property is set for all containers, except those which are hidden in the scratchpad as they have no workspace. In some cases we need to refer to workspaces in a container-like way. For example, workspaces have layout and children, but when using specific types this makes it difficult. Likewise, it's difficult for a container to get its parent's layout when the parent could be another container or a workspace. To make it easier, some helper functions have been created: container_parent_layout and container_get_siblings. container_remove_child has been renamed to container_detach and container_replace_child has been renamed to container_replace. `container_handle_fullscreen_reparent(con, old_parent)` has had the old_parent removed. We now unfullscreen the workspace when detaching the container, so this function is simplified and only needs one argument now. container_notify_subtree_changed has been renamed to container_update_representation. This is more descriptive of its purpose. I also wanted to be able to call it with whatever container was changed rather than the container's parent, which makes bubbling up to the workspace easier. There are now state structs per node thing. ie. sway_output_state, sway_workspace_state and sway_container_state. The focus, move and layout commands have been completely refactored to work with the specific types. I considered making these a separate PR, but I'd be backporting my changes only to replace them again, and it's easier just to test everything at once. --- sway/tree/arrange.c | 110 ++++++++++++++++++++-------------------------------- 1 file changed, 43 insertions(+), 67 deletions(-) (limited to 'sway/tree/arrange.c') diff --git a/sway/tree/arrange.c b/sway/tree/arrange.c index 92f20fcc..f86d4a74 100644 --- a/sway/tree/arrange.c +++ b/sway/tree/arrange.c @@ -166,29 +166,23 @@ void arrange_container(struct sway_container *container) { if (config->reloading) { return; } - if (container->type == C_VIEW) { - view_autoconfigure(container->sway_view); - container_set_dirty(container); - return; - } - if (!sway_assert(container->type == C_CONTAINER, "Expected a container")) { + if (container->view) { + view_autoconfigure(container->view); + node_set_dirty(&container->node); return; } struct wlr_box box; container_get_box(container, &box); arrange_children(container->children, container->layout, &box); - container_set_dirty(container); + node_set_dirty(&container->node); } -void arrange_workspace(struct sway_container *workspace) { +void arrange_workspace(struct sway_workspace *workspace) { if (config->reloading) { return; } - if (!sway_assert(workspace->type == C_WORKSPACE, "Expected a workspace")) { - return; - } - struct sway_container *output = workspace->parent; - struct wlr_box *area = &output->sway_output->usable_area; + struct sway_output *output = workspace->output; + struct wlr_box *area = &output->usable_area; wlr_log(WLR_DEBUG, "Usable area for ws: %dx%d@%d,%d", area->width, area->height, area->x, area->y); workspace_remove_gaps(workspace); @@ -197,21 +191,20 @@ void arrange_workspace(struct sway_container *workspace) { double prev_y = workspace->y; workspace->width = area->width; workspace->height = area->height; - workspace->x = output->x + area->x; - workspace->y = output->y + area->y; + workspace->x = output->wlr_output->lx + area->x; + workspace->y = output->wlr_output->ly + area->y; // Adjust any floating containers double diff_x = workspace->x - prev_x; double diff_y = workspace->y - prev_y; if (diff_x != 0 || diff_y != 0) { - for (int i = 0; i < workspace->sway_workspace->floating->length; ++i) { - struct sway_container *floater = - workspace->sway_workspace->floating->items[i]; + for (int i = 0; i < workspace->floating->length; ++i) { + struct sway_container *floater = workspace->floating->items[i]; container_floating_translate(floater, diff_x, diff_y); double center_x = floater->x + floater->width / 2; double center_y = floater->y + floater->height / 2; struct wlr_box workspace_box; - container_get_box(workspace, &workspace_box); + workspace_get_box(workspace, &workspace_box); if (!wlr_box_contains_point(&workspace_box, center_x, center_y)) { container_floating_move_to_center(floater); } @@ -219,43 +212,32 @@ void arrange_workspace(struct sway_container *workspace) { } workspace_add_gaps(workspace); - container_set_dirty(workspace); + node_set_dirty(&workspace->node); wlr_log(WLR_DEBUG, "Arranging workspace '%s' at %f, %f", workspace->name, workspace->x, workspace->y); - if (workspace->sway_workspace->fullscreen) { - struct sway_container *fs = workspace->sway_workspace->fullscreen; - fs->x = workspace->parent->x; - fs->y = workspace->parent->y; - fs->width = workspace->parent->width; - fs->height = workspace->parent->height; + if (workspace->fullscreen) { + struct sway_container *fs = workspace->fullscreen; + fs->x = output->wlr_output->lx; + fs->y = output->wlr_output->ly; + fs->width = output->wlr_output->width; + fs->height = output->wlr_output->height; arrange_container(fs); } else { struct wlr_box box; - container_get_box(workspace, &box); - arrange_children(workspace->children, workspace->layout, &box); - arrange_floating(workspace->sway_workspace->floating); + workspace_get_box(workspace, &box); + arrange_children(workspace->tiling, workspace->layout, &box); + arrange_floating(workspace->floating); } } -void arrange_output(struct sway_container *output) { +void arrange_output(struct sway_output *output) { if (config->reloading) { return; } - if (!sway_assert(output->type == C_OUTPUT, "Expected an output")) { - return; - } - const struct wlr_box *output_box = wlr_output_layout_get_box( - root_container.sway_root->output_layout, - output->sway_output->wlr_output); - output->x = output_box->x; - output->y = output_box->y; - output->width = output_box->width; - output->height = output_box->height; - container_set_dirty(output); - wlr_log(WLR_DEBUG, "Arranging output '%s' at %f,%f", - output->name, output->x, output->y); - for (int i = 0; i < output->children->length; ++i) { - struct sway_container *workspace = output->children->items[i]; + // Outputs have no pending x/y/width/height, + // so all we do here is arrange the workspaces. + for (int i = 0; i < output->workspaces->length; ++i) { + struct sway_workspace *workspace = output->workspaces->items[i]; arrange_workspace(workspace); } } @@ -264,37 +246,31 @@ void arrange_root(void) { if (config->reloading) { return; } - struct wlr_output_layout *output_layout = - root_container.sway_root->output_layout; const struct wlr_box *layout_box = - wlr_output_layout_get_box(output_layout, NULL); - root_container.x = layout_box->x; - root_container.y = layout_box->y; - root_container.width = layout_box->width; - root_container.height = layout_box->height; - container_set_dirty(&root_container); - for (int i = 0; i < root_container.children->length; ++i) { - struct sway_container *output = root_container.children->items[i]; + wlr_output_layout_get_box(root->output_layout, NULL); + root->x = layout_box->x; + root->y = layout_box->y; + root->width = layout_box->width; + root->height = layout_box->height; + for (int i = 0; i < root->outputs->length; ++i) { + struct sway_output *output = root->outputs->items[i]; arrange_output(output); } } -void arrange_windows(struct sway_container *container) { - switch (container->type) { - case C_ROOT: +void arrange_node(struct sway_node *node) { + switch (node->type) { + case N_ROOT: arrange_root(); break; - case C_OUTPUT: - arrange_output(container); - break; - case C_WORKSPACE: - arrange_workspace(container); + case N_OUTPUT: + arrange_output(node->sway_output); break; - case C_CONTAINER: - case C_VIEW: - arrange_container(container); + case N_WORKSPACE: + arrange_workspace(node->sway_workspace); break; - case C_TYPES: + case N_CONTAINER: + arrange_container(node->sway_container); break; } } -- cgit v1.2.3 From acc2628c799170dea98380cda2237137137f182f Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Thu, 30 Aug 2018 21:20:31 +1000 Subject: Don't use wlr_output properties These properties are before rotation. --- include/sway/output.h | 3 +++ sway/debug-tree.c | 16 ++++++++-------- sway/desktop/output.c | 8 ++++---- sway/desktop/render.c | 4 ++-- sway/desktop/xdg_shell.c | 8 ++++---- sway/desktop/xdg_shell_v6.c | 8 ++++---- sway/tree/arrange.c | 4 ++-- sway/tree/output.c | 19 ++++++++++++------- sway/tree/view.c | 8 ++++---- sway/tree/workspace.c | 8 ++++---- 10 files changed, 47 insertions(+), 39 deletions(-) (limited to 'sway/tree/arrange.c') diff --git a/include/sway/output.h b/include/sway/output.h index 540ed8a0..19a61175 100644 --- a/include/sway/output.h +++ b/include/sway/output.h @@ -28,6 +28,9 @@ struct sway_output { struct timespec last_frame; struct wlr_output_damage *damage; + int lx, ly; + int width, height; + bool enabled; list_t *workspaces; diff --git a/sway/debug-tree.c b/sway/debug-tree.c index 2ed3c087..973c6d88 100644 --- a/sway/debug-tree.c +++ b/sway/debug-tree.c @@ -43,10 +43,10 @@ static char *get_string(struct sway_node *node) { case N_OUTPUT: snprintf(buffer, 512, "N_OUTPUT id:%zd '%s' %dx%d@%d,%d", node->id, node->sway_output->wlr_output->name, - node->sway_output->wlr_output->width, - node->sway_output->wlr_output->height, - node->sway_output->wlr_output->lx, - node->sway_output->wlr_output->ly); + node->sway_output->width, + node->sway_output->height, + node->sway_output->lx, + node->sway_output->ly); break; case N_WORKSPACE: snprintf(buffer, 512, "N_WORKSPACE id:%zd '%s' %s %dx%d@%.f,%.f", @@ -128,11 +128,11 @@ void update_debug_tree() { int width = 640, height = 480; for (int i = 0; i < root->outputs->length; ++i) { struct sway_output *output = root->outputs->items[i]; - if (output->wlr_output->width > width) { - width = output->wlr_output->width; + if (output->width > width) { + width = output->width; } - if (output->wlr_output->height > height) { - height = output->wlr_output->height; + if (output->height > height) { + height = output->height; } } cairo_surface_t *surface = diff --git a/sway/desktop/output.c b/sway/desktop/output.c index c182bad6..792a7231 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c @@ -98,8 +98,8 @@ static bool get_surface_box(struct surface_iterator_data *data, wlr_box_rotated_bounds(&box, data->rotation, &rotated_box); struct wlr_box output_box = { - .width = output->wlr_output->width, - .height = output->wlr_output->height, + .width = output->width, + .height = output->height, }; struct wlr_box intersection; @@ -249,8 +249,8 @@ bool output_has_opaque_overlay_layer_surface(struct sway_output *output) { struct sway_layer_surface *sway_layer_surface = layer_from_wlr_layer_surface(wlr_layer_surface); pixman_box32_t output_box = { - .x2 = output->wlr_output->width, - .y2 = output->wlr_output->height, + .x2 = output->width, + .y2 = output->height, }; pixman_region32_t surface_opaque_box; pixman_region32_init(&surface_opaque_box); diff --git a/sway/desktop/render.c b/sway/desktop/render.c index 99b2cf3d..9d80f3c7 100644 --- a/sway/desktop/render.c +++ b/sway/desktop/render.c @@ -238,8 +238,8 @@ static void render_saved_view(struct sway_view *view, }; struct wlr_box output_box = { - .width = output->wlr_output->width, - .height = output->wlr_output->height, + .width = output->width, + .height = output->height, }; struct wlr_box intersection; diff --git a/sway/desktop/xdg_shell.c b/sway/desktop/xdg_shell.c index e19d8e18..575f229d 100644 --- a/sway/desktop/xdg_shell.c +++ b/sway/desktop/xdg_shell.c @@ -59,10 +59,10 @@ static void popup_unconstrain(struct sway_xdg_popup *popup) { // the output box expressed in the coordinate system of the toplevel parent // of the popup struct wlr_box output_toplevel_sx_box = { - .x = output->wlr_output->lx - view->x, - .y = output->wlr_output->ly - view->y, - .width = output->wlr_output->width, - .height = output->wlr_output->height, + .x = output->lx - view->x, + .y = output->ly - view->y, + .width = output->width, + .height = output->height, }; wlr_xdg_popup_unconstrain_from_box(wlr_popup, &output_toplevel_sx_box); diff --git a/sway/desktop/xdg_shell_v6.c b/sway/desktop/xdg_shell_v6.c index b23d4577..58fbd631 100644 --- a/sway/desktop/xdg_shell_v6.c +++ b/sway/desktop/xdg_shell_v6.c @@ -58,10 +58,10 @@ static void popup_unconstrain(struct sway_xdg_popup_v6 *popup) { // the output box expressed in the coordinate system of the toplevel parent // of the popup struct wlr_box output_toplevel_sx_box = { - .x = output->wlr_output->lx - view->x, - .y = output->wlr_output->ly - view->y, - .width = output->wlr_output->width, - .height = output->wlr_output->height, + .x = output->lx - view->x, + .y = output->ly - view->y, + .width = output->width, + .height = output->height, }; wlr_xdg_popup_v6_unconstrain_from_box(wlr_popup, &output_toplevel_sx_box); diff --git a/sway/tree/arrange.c b/sway/tree/arrange.c index f86d4a74..2cccf65a 100644 --- a/sway/tree/arrange.c +++ b/sway/tree/arrange.c @@ -219,8 +219,8 @@ void arrange_workspace(struct sway_workspace *workspace) { struct sway_container *fs = workspace->fullscreen; fs->x = output->wlr_output->lx; fs->y = output->wlr_output->ly; - fs->width = output->wlr_output->width; - fs->height = output->wlr_output->height; + fs->width = output->width; + fs->height = output->height; arrange_container(fs); } else { struct wlr_box box; diff --git a/sway/tree/output.c b/sway/tree/output.c index d72eb1a1..afc336f8 100644 --- a/sway/tree/output.c +++ b/sway/tree/output.c @@ -75,6 +75,11 @@ void output_enable(struct sway_output *output, struct output_config *oc) { apply_output_config(oc, output); list_add(root->outputs, output); + output->lx = wlr_output->lx; + output->ly = wlr_output->ly; + wlr_output_transformed_resolution(wlr_output, + &output->width, &output->height); + restore_workspaces(output); if (!output->workspaces->length) { @@ -265,8 +270,8 @@ struct sway_output *output_get_in_direction(struct sway_output *reference, "got invalid direction: %d", direction)) { return NULL; } - int lx = reference->wlr_output->lx + reference->wlr_output->width / 2; - int ly = reference->wlr_output->ly + reference->wlr_output->height / 2; + int lx = reference->wlr_output->lx + reference->width / 2; + int ly = reference->wlr_output->ly + reference->height / 2; struct wlr_output *wlr_adjacent = wlr_output_layout_adjacent_output( root->output_layout, wlr_dir, reference->wlr_output, lx, ly); if (!wlr_adjacent) { @@ -346,10 +351,10 @@ void output_sort_workspaces(struct sway_output *output) { } void output_get_box(struct sway_output *output, struct wlr_box *box) { - box->x = output->wlr_output->lx; - box->y = output->wlr_output->ly; - box->width = output->wlr_output->width; - box->height = output->wlr_output->height; + box->x = output->lx; + box->y = output->ly; + box->width = output->width; + box->height = output->height; } enum sway_container_layout output_get_default_layout( @@ -360,7 +365,7 @@ enum sway_container_layout output_get_default_layout( if (config->default_orientation != L_NONE) { return config->default_orientation; } - if (output->wlr_output->height > output->wlr_output->width) { + if (output->height > output->width) { return L_VERT; } return L_HORIZ; diff --git a/sway/tree/view.c b/sway/tree/view.c index 452c2bd1..ff63df2d 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -167,10 +167,10 @@ void view_autoconfigure(struct sway_view *view) { struct sway_output *output = view->container->workspace->output; if (view->container->is_fullscreen) { - view->x = output->wlr_output->lx; - view->y = output->wlr_output->ly; - view->width = output->wlr_output->width; - view->height = output->wlr_output->height; + view->x = output->lx; + view->y = output->ly; + view->width = output->width; + view->height = output->height; return; } diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c index 38ee478e..bb1ded22 100644 --- a/sway/tree/workspace.c +++ b/sway/tree/workspace.c @@ -53,10 +53,10 @@ struct sway_workspace *workspace_create(struct sway_output *output, return NULL; } node_init(&ws->node, N_WORKSPACE, ws); - ws->x = output->wlr_output->lx; - ws->y = output->wlr_output->ly; - ws->width = output->wlr_output->width; - ws->height = output->wlr_output->height; + ws->x = output->lx; + ws->y = output->ly; + ws->width = output->width; + ws->height = output->height; ws->name = name ? strdup(name) : NULL; ws->prev_split_layout = L_NONE; ws->layout = output_get_default_layout(output); -- cgit v1.2.3 From 93ff7879f16c5e31c18a37e87b23ee5dcb5a584b Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Sat, 1 Sep 2018 08:45:19 +1000 Subject: Fix output position issue Looks like the output dimensions need to be set when arranging rather than when a mode is set. Fixes an issue with position of fullscreen views. --- sway/desktop/output.c | 4 ---- sway/tree/arrange.c | 13 +++++++++---- 2 files changed, 9 insertions(+), 8 deletions(-) (limited to 'sway/tree/arrange.c') diff --git a/sway/desktop/output.c b/sway/desktop/output.c index d9c7dbe3..7e9f7b7e 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c @@ -505,10 +505,6 @@ static void handle_destroy(struct wl_listener *listener, void *data) { static void handle_mode(struct wl_listener *listener, void *data) { struct sway_output *output = wl_container_of(listener, output, mode); - output->lx = output->wlr_output->lx; - output->ly = output->wlr_output->ly; - wlr_output_effective_resolution(output->wlr_output, - &output->width, &output->height); arrange_layers(output); arrange_output(output); transaction_commit_dirty(); diff --git a/sway/tree/arrange.c b/sway/tree/arrange.c index 2cccf65a..edb05f86 100644 --- a/sway/tree/arrange.c +++ b/sway/tree/arrange.c @@ -217,8 +217,8 @@ void arrange_workspace(struct sway_workspace *workspace) { workspace->x, workspace->y); if (workspace->fullscreen) { struct sway_container *fs = workspace->fullscreen; - fs->x = output->wlr_output->lx; - fs->y = output->wlr_output->ly; + fs->x = output->lx; + fs->y = output->ly; fs->width = output->width; fs->height = output->height; arrange_container(fs); @@ -234,8 +234,13 @@ void arrange_output(struct sway_output *output) { if (config->reloading) { return; } - // Outputs have no pending x/y/width/height, - // so all we do here is arrange the workspaces. + const struct wlr_box *output_box = wlr_output_layout_get_box( + root->output_layout, output->wlr_output); + output->lx = output_box->x; + output->ly = output_box->y; + output->width = output_box->width; + output->height = output_box->height; + for (int i = 0; i < output->workspaces->length; ++i) { struct sway_workspace *workspace = output->workspaces->items[i]; arrange_workspace(workspace); -- cgit v1.2.3