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/workspace.c | 493 ++++++++++++++++++++++++-------------------------- 1 file changed, 238 insertions(+), 255 deletions(-) (limited to 'sway/tree/workspace.c') diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c index 1957d94f..38ee478e 100644 --- a/sway/tree/workspace.c +++ b/sway/tree/workspace.c @@ -12,128 +12,105 @@ #include "sway/output.h" #include "sway/tree/arrange.h" #include "sway/tree/container.h" +#include "sway/tree/node.h" #include "sway/tree/view.h" #include "sway/tree/workspace.h" #include "list.h" #include "log.h" #include "util.h" -struct sway_container *workspace_get_initial_output(const char *name) { - struct sway_container *parent; +struct sway_output *workspace_get_initial_output(const char *name) { // Search for workspace<->output pair - int e = config->workspace_outputs->length; for (int i = 0; i < config->workspace_outputs->length; ++i) { struct workspace_output *wso = config->workspace_outputs->items[i]; if (strcasecmp(wso->workspace, name) == 0) { // Find output to use if it exists - e = root_container.children->length; - for (i = 0; i < e; ++i) { - parent = root_container.children->items[i]; - if (strcmp(parent->name, wso->output) == 0) { - return parent; - } + struct sway_output *output = output_by_name(wso->output); + if (output) { + return output; } break; } } // Otherwise put it on the focused output struct sway_seat *seat = input_manager_current_seat(input_manager); - struct sway_container *focus = - seat_get_focus_inactive(seat, &root_container); - parent = focus; - parent = container_parent(parent, C_OUTPUT); - return parent; + struct sway_workspace *focus = seat_get_focused_workspace(seat); + return focus->output; } -struct sway_container *workspace_create(struct sway_container *output, +struct sway_workspace *workspace_create(struct sway_output *output, const char *name) { if (output == NULL) { output = workspace_get_initial_output(name); } - wlr_log(WLR_DEBUG, "Added workspace %s for output %s", name, output->name); - struct sway_container *workspace = container_create(C_WORKSPACE); - - workspace->x = output->x; - workspace->y = output->y; - workspace->width = output->width; - workspace->height = output->height; - workspace->name = !name ? NULL : strdup(name); - workspace->prev_split_layout = L_NONE; - workspace->layout = container_get_default_layout(output); + wlr_log(WLR_DEBUG, "Adding workspace %s for output %s", name, + output->wlr_output->name); - struct sway_workspace *swayws = calloc(1, sizeof(struct sway_workspace)); - if (!swayws) { + struct sway_workspace *ws = calloc(1, sizeof(struct sway_workspace)); + if (!ws) { + wlr_log(WLR_ERROR, "Unable to allocate sway_workspace"); return NULL; } - swayws->swayc = workspace; - swayws->floating = create_list(); - swayws->output_priority = create_list(); - workspace->sway_workspace = swayws; - workspace_output_add_priority(workspace, output); - - container_add_child(output, workspace); + 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->name = name ? strdup(name) : NULL; + ws->prev_split_layout = L_NONE; + ws->layout = output_get_default_layout(output); + ws->floating = create_list(); + ws->tiling = create_list(); + ws->output_priority = create_list(); + workspace_output_add_priority(ws, output); + + output_add_workspace(output, ws); output_sort_workspaces(output); - container_create_notify(workspace); - return workspace; + ipc_event_workspace(NULL, ws, "init"); + wl_signal_emit(&root->events.new_node, &ws->node); + + return ws; } -void workspace_destroy(struct sway_container *workspace) { - if (!sway_assert(workspace->type == C_WORKSPACE, "Expected a workspace")) { - return; - } - if (!sway_assert(workspace->destroying, +void workspace_destroy(struct sway_workspace *workspace) { + if (!sway_assert(workspace->node.destroying, "Tried to free workspace which wasn't marked as destroying")) { return; } - if (!sway_assert(workspace->ntxnrefs == 0, "Tried to free workspace " + if (!sway_assert(workspace->node.ntxnrefs == 0, "Tried to free workspace " "which is still referenced by transactions")) { return; } - // sway_workspace - struct sway_workspace *ws = workspace->sway_workspace; - list_foreach(ws->output_priority, free); - list_free(ws->output_priority); - list_free(ws->floating); - free(ws); - // swayc free(workspace->name); - free(workspace->formatted_title); - wlr_texture_destroy(workspace->title_focused); - wlr_texture_destroy(workspace->title_focused_inactive); - wlr_texture_destroy(workspace->title_unfocused); - wlr_texture_destroy(workspace->title_urgent); - list_free(workspace->children); - list_free(workspace->current.children); - list_free(workspace->outputs); + free(workspace->representation); + list_foreach(workspace->output_priority, free); + list_free(workspace->output_priority); + list_free(workspace->floating); + list_free(workspace->tiling); + list_free(workspace->current.floating); + list_free(workspace->current.tiling); free(workspace); } -void workspace_begin_destroy(struct sway_container *workspace) { - if (!sway_assert(workspace->type == C_WORKSPACE, "Expected a workspace")) { - return; - } +void workspace_begin_destroy(struct sway_workspace *workspace) { wlr_log(WLR_DEBUG, "Destroying workspace '%s'", workspace->name); - wl_signal_emit(&workspace->events.destroy, workspace); ipc_event_workspace(NULL, workspace, "empty"); // intentional + wl_signal_emit(&workspace->node.events.destroy, &workspace->node); - workspace->destroying = true; - container_set_dirty(workspace); - - if (workspace->parent) { - container_remove_child(workspace); + if (workspace->output) { + workspace_detach(workspace); } + + workspace->node.destroying = true; + node_set_dirty(&workspace->node); } -void workspace_consider_destroy(struct sway_container *ws) { - if (!sway_assert(ws->type == C_WORKSPACE, "Expected a workspace")) { - return; - } - struct sway_seat *seat = input_manager_current_seat(input_manager); - if (ws->children->length == 0 && ws->sway_workspace->floating->length == 0 - && seat_get_active_child(seat, ws->parent) != ws) { +void workspace_consider_destroy(struct sway_workspace *ws) { + if (ws->tiling->length == 0 && ws->floating->length == 0 + && output_get_active_workspace(ws->output) != ws) { workspace_begin_destroy(ws); } } @@ -272,59 +249,49 @@ char *workspace_next_name(const char *output_name) { } // As a fall back, get the current number of active workspaces // and return that + 1 for the next workspace's name - int ws_num = root_container.children->length; + int ws_num = root->outputs->length; int l = snprintf(NULL, 0, "%d", ws_num); char *name = malloc(l + 1); - if (!sway_assert(name, "Cloud not allocate workspace name")) { + if (!sway_assert(name, "Could not allocate workspace name")) { return NULL; } sprintf(name, "%d", ws_num++); return name; } -static bool _workspace_by_number(struct sway_container *view, void *data) { - if (view->type != C_WORKSPACE) { - return false; - } +static bool _workspace_by_number(struct sway_workspace *ws, void *data) { char *name = data; - char *view_name = view->name; + char *ws_name = ws->name; while (isdigit(*name)) { - if (*name++ != *view_name++) { + if (*name++ != *ws_name++) { return false; } } - return !isdigit(*view_name); + return !isdigit(*ws_name); } -struct sway_container *workspace_by_number(const char* name) { +struct sway_workspace *workspace_by_number(const char* name) { return root_find_workspace(_workspace_by_number, (void *) name); } -static bool _workspace_by_name(struct sway_container *view, void *data) { - return (view->type == C_WORKSPACE) && - (strcasecmp(view->name, (char *) data) == 0); +static bool _workspace_by_name(struct sway_workspace *ws, void *data) { + return strcasecmp(ws->name, data) == 0; } -struct sway_container *workspace_by_name(const char *name) { +struct sway_workspace *workspace_by_name(const char *name) { struct sway_seat *seat = input_manager_current_seat(input_manager); - struct sway_container *current_workspace = NULL, *current_output = NULL; - struct sway_container *focus = seat_get_focus(seat); - if (focus) { - current_workspace = focus->type == C_WORKSPACE ? - focus : container_parent(focus, C_WORKSPACE); - current_output = container_parent(focus, C_OUTPUT); - } + struct sway_workspace *current = seat_get_focused_workspace(seat); if (strcmp(name, "prev") == 0) { - return workspace_prev(current_workspace); + return workspace_prev(current); } else if (strcmp(name, "prev_on_output") == 0) { - return workspace_output_prev(current_output); + return workspace_output_prev(current); } else if (strcmp(name, "next") == 0) { - return workspace_next(current_workspace); + return workspace_next(current); } else if (strcmp(name, "next_on_output") == 0) { - return workspace_output_next(current_output); + return workspace_output_next(current); } else if (strcmp(name, "current") == 0) { - return current_workspace; + return current; } else if (strcasecmp(name, "back_and_forth") == 0) { return prev_workspace_name ? root_find_workspace(_workspace_by_name, (void*)prev_workspace_name) @@ -339,97 +306,68 @@ struct sway_container *workspace_by_name(const char *name) { * the end and beginning. If next is false, the previous workspace is returned, * otherwise the next one is returned. */ -static struct sway_container *workspace_output_prev_next_impl( - struct sway_container *output, int dir) { - if (!output) { - return NULL; - } - if (!sway_assert(output->type == C_OUTPUT, - "Argument must be an output, is %d", output->type)) { - return NULL; - } - +static struct sway_workspace *workspace_output_prev_next_impl( + struct sway_output *output, int dir) { struct sway_seat *seat = input_manager_current_seat(input_manager); - struct sway_container *focus = seat_get_focus_inactive(seat, output); - struct sway_container *workspace = (focus->type == C_WORKSPACE ? - focus : - container_parent(focus, C_WORKSPACE)); + struct sway_workspace *workspace = seat_get_focused_workspace(seat); - int index = list_find(output->children, workspace); - size_t new_index = wrap(index + dir, output->children->length); - return output->children->items[new_index]; + int index = list_find(output->workspaces, workspace); + size_t new_index = wrap(index + dir, output->workspaces->length); + return output->workspaces->items[new_index]; } /** * Get the previous or next workspace. If the first/last workspace on an output * is active, proceed to the previous/next output's previous/next workspace. */ -static struct sway_container *workspace_prev_next_impl( - struct sway_container *workspace, int dir) { - if (!workspace) { - return NULL; - } - if (!sway_assert(workspace->type == C_WORKSPACE, - "Argument must be a workspace, is %d", workspace->type)) { - return NULL; - } - - struct sway_container *output = workspace->parent; - int index = list_find(output->children, workspace); +static struct sway_workspace *workspace_prev_next_impl( + struct sway_workspace *workspace, int dir) { + struct sway_output *output = workspace->output; + int index = list_find(output->workspaces, workspace); int new_index = index + dir; - if (new_index >= 0 && new_index < output->children->length) { - return output->children->items[index + dir]; + if (new_index >= 0 && new_index < output->workspaces->length) { + return output->workspaces->items[new_index]; } // Look on a different output - int output_index = list_find(root_container.children, output); - new_index = wrap(output_index + dir, root_container.children->length); - output = root_container.children->items[new_index]; + int output_index = list_find(root->outputs, output); + new_index = wrap(output_index + dir, root->outputs->length); + output = root->outputs->items[new_index]; if (dir == 1) { - return output->children->items[0]; + return output->workspaces->items[0]; } else { - return output->children->items[output->children->length - 1]; + return output->workspaces->items[output->workspaces->length - 1]; } } -struct sway_container *workspace_output_next(struct sway_container *current) { - return workspace_output_prev_next_impl(current, 1); +struct sway_workspace *workspace_output_next(struct sway_workspace *current) { + return workspace_output_prev_next_impl(current->output, 1); } -struct sway_container *workspace_next(struct sway_container *current) { +struct sway_workspace *workspace_next(struct sway_workspace *current) { return workspace_prev_next_impl(current, 1); } -struct sway_container *workspace_output_prev(struct sway_container *current) { - return workspace_output_prev_next_impl(current, -1); +struct sway_workspace *workspace_output_prev(struct sway_workspace *current) { + return workspace_output_prev_next_impl(current->output, -1); } -struct sway_container *workspace_prev(struct sway_container *current) { +struct sway_workspace *workspace_prev(struct sway_workspace *current) { return workspace_prev_next_impl(current, -1); } -bool workspace_switch(struct sway_container *workspace, +bool workspace_switch(struct sway_workspace *workspace, bool no_auto_back_and_forth) { - if (!workspace) { - return false; - } struct sway_seat *seat = input_manager_current_seat(input_manager); - struct sway_container *focus = - seat_get_focus_inactive(seat, &root_container); - if (!seat || !focus) { - return false; - } - struct sway_container *active_ws = focus; - if (active_ws->type != C_WORKSPACE) { - active_ws = container_parent(focus, C_WORKSPACE); - } + struct sway_node *focus = seat_get_focus_inactive(seat, &root->node); + struct sway_workspace *active_ws = seat_get_focused_workspace(seat); if (!no_auto_back_and_forth && config->auto_back_and_forth && active_ws == workspace && prev_workspace_name) { - struct sway_container *new_ws = workspace_by_name(prev_workspace_name); + struct sway_workspace *new_ws = workspace_by_name(prev_workspace_name); workspace = new_ws ? new_ws : workspace_create(NULL, prev_workspace_name); @@ -447,21 +385,21 @@ bool workspace_switch(struct sway_container *workspace, } // Move sticky containers to new workspace - struct sway_container *next_output = workspace->parent; - struct sway_container *next_output_prev_ws = - seat_get_active_child(seat, next_output); - list_t *floating = next_output_prev_ws->sway_workspace->floating; + struct sway_output *next_output = workspace->output; + struct sway_workspace *next_output_prev_ws = + output_get_active_workspace(next_output); bool has_sticky = false; if (workspace != next_output_prev_ws) { - for (int i = 0; i < floating->length; ++i) { - struct sway_container *floater = floating->items[i]; + for (int i = 0; i < next_output_prev_ws->floating->length; ++i) { + struct sway_container *floater = + next_output_prev_ws->floating->items[i]; if (floater->is_sticky) { has_sticky = true; - container_remove_child(floater); + container_detach(floater); workspace_add_floating(workspace, floater); - if (floater == focus) { + if (&floater->node == focus) { seat_set_focus(seat, NULL); - seat_set_focus(seat, floater); + seat_set_focus(seat, &floater->node); } --i; } @@ -470,9 +408,9 @@ bool workspace_switch(struct sway_container *workspace, wlr_log(WLR_DEBUG, "Switching to workspace %p:%s", workspace, workspace->name); - struct sway_container *next = seat_get_focus_inactive(seat, workspace); + struct sway_node *next = seat_get_focus_inactive(seat, &workspace->node); if (next == NULL) { - next = workspace; + next = &workspace->node; } if (has_sticky) { // If there's a sticky container, we might be setting focus to the same @@ -483,35 +421,24 @@ bool workspace_switch(struct sway_container *workspace, workspace_consider_destroy(active_ws); } seat_set_focus(seat, next); - struct sway_container *output = container_parent(workspace, C_OUTPUT); - arrange_windows(output); + arrange_workspace(workspace); return true; } -bool workspace_is_visible(struct sway_container *ws) { - if (ws->destroying) { +bool workspace_is_visible(struct sway_workspace *ws) { + if (ws->node.destroying) { return false; } - struct sway_container *output = container_parent(ws, C_OUTPUT); - struct sway_seat *seat = input_manager_current_seat(input_manager); - struct sway_container *focus = seat_get_focus_inactive(seat, output); - if (focus->type != C_WORKSPACE) { - focus = container_parent(focus, C_WORKSPACE); - } - return focus == ws; + return output_get_active_workspace(ws->output) == ws; } -bool workspace_is_empty(struct sway_container *ws) { - if (!sway_assert(ws->type == C_WORKSPACE, "Expected a workspace")) { - return false; - } - if (ws->children->length) { +bool workspace_is_empty(struct sway_workspace *ws) { + if (ws->tiling->length) { return false; } // Sticky views are not considered to be part of this workspace - list_t *floating = ws->sway_workspace->floating; - for (int i = 0; i < floating->length; ++i) { - struct sway_container *floater = floating->items[i]; + for (int i = 0; i < ws->floating->length; ++i) { + struct sway_container *floater = ws->floating->items[i]; if (!floater->is_sticky) { return false; } @@ -523,20 +450,19 @@ static int find_output(const void *id1, const void *id2) { return strcmp(id1, id2) ? 0 : 1; } -void workspace_output_raise_priority(struct sway_container *workspace, - struct sway_container *old_output, struct sway_container *output) { - struct sway_workspace *ws = workspace->sway_workspace; - +void workspace_output_raise_priority(struct sway_workspace *ws, + struct sway_output *old_output, struct sway_output *output) { int old_index = list_seq_find(ws->output_priority, find_output, - old_output->name); + old_output->wlr_output->name); if (old_index < 0) { return; } int new_index = list_seq_find(ws->output_priority, find_output, - output->name); + output->wlr_output->name); if (new_index < 0) { - list_insert(ws->output_priority, old_index, strdup(output->name)); + list_insert(ws->output_priority, old_index, + strdup(output->wlr_output->name)); } else if (new_index > old_index) { char *name = ws->output_priority->items[new_index]; list_del(ws->output_priority, new_index); @@ -544,29 +470,24 @@ void workspace_output_raise_priority(struct sway_container *workspace, } } -void workspace_output_add_priority(struct sway_container *workspace, - struct sway_container *output) { - int index = list_seq_find(workspace->sway_workspace->output_priority, - find_output, output->name); +void workspace_output_add_priority(struct sway_workspace *workspace, + struct sway_output *output) { + int index = list_seq_find(workspace->output_priority, + find_output, output->wlr_output->name); if (index < 0) { - list_add(workspace->sway_workspace->output_priority, - strdup(output->name)); + list_add(workspace->output_priority, strdup(output->wlr_output->name)); } } -static bool _output_by_name(struct sway_container *output, void *data) { - return output->type == C_OUTPUT && strcasecmp(output->name, data) == 0; -} - -struct sway_container *workspace_output_get_highest_available( - struct sway_container *ws, struct sway_container *exclude) { - for (int i = 0; i < ws->sway_workspace->output_priority->length; i++) { - char *name = ws->sway_workspace->output_priority->items[i]; - if (exclude && strcasecmp(name, exclude->name) == 0) { +struct sway_output *workspace_output_get_highest_available( + struct sway_workspace *ws, struct sway_output *exclude) { + for (int i = 0; i < ws->output_priority->length; i++) { + char *name = ws->output_priority->items[i]; + if (exclude && strcasecmp(name, exclude->wlr_output->name) == 0) { continue; } - struct sway_container *output = root_find_output(_output_by_name, name); + struct sway_output *output = output_by_name(name); if (output) { return output; } @@ -576,49 +497,42 @@ struct sway_container *workspace_output_get_highest_available( } static bool find_urgent_iterator(struct sway_container *con, void *data) { - return con->type == C_VIEW && view_is_urgent(con->sway_view); + return con->view && view_is_urgent(con->view); } -void workspace_detect_urgent(struct sway_container *workspace) { +void workspace_detect_urgent(struct sway_workspace *workspace) { bool new_urgent = (bool)workspace_find_container(workspace, find_urgent_iterator, NULL); - if (workspace->sway_workspace->urgent != new_urgent) { - workspace->sway_workspace->urgent = new_urgent; + if (workspace->urgent != new_urgent) { + workspace->urgent = new_urgent; ipc_event_workspace(NULL, workspace, "urgent"); - container_damage_whole(workspace); + output_damage_whole(workspace->output); } } -void workspace_for_each_container(struct sway_container *ws, +void workspace_for_each_container(struct sway_workspace *ws, void (*f)(struct sway_container *con, void *data), void *data) { - if (!sway_assert(ws->type == C_WORKSPACE, "Expected a workspace")) { - return; - } // Tiling - for (int i = 0; i < ws->children->length; ++i) { - struct sway_container *container = ws->children->items[i]; + for (int i = 0; i < ws->tiling->length; ++i) { + struct sway_container *container = ws->tiling->items[i]; f(container, data); container_for_each_child(container, f, data); } // Floating - for (int i = 0; i < ws->sway_workspace->floating->length; ++i) { - struct sway_container *container = - ws->sway_workspace->floating->items[i]; + for (int i = 0; i < ws->floating->length; ++i) { + struct sway_container *container = ws->floating->items[i]; f(container, data); container_for_each_child(container, f, data); } } -struct sway_container *workspace_find_container(struct sway_container *ws, +struct sway_container *workspace_find_container(struct sway_workspace *ws, bool (*test)(struct sway_container *con, void *data), void *data) { - if (!sway_assert(ws->type == C_WORKSPACE, "Expected a workspace")) { - return NULL; - } struct sway_container *result = NULL; // Tiling - for (int i = 0; i < ws->children->length; ++i) { - struct sway_container *child = ws->children->items[i]; + for (int i = 0; i < ws->tiling->length; ++i) { + struct sway_container *child = ws->tiling->items[i]; if (test(child, data)) { return child; } @@ -627,8 +541,8 @@ struct sway_container *workspace_find_container(struct sway_container *ws, } } // Floating - for (int i = 0; i < ws->sway_workspace->floating->length; ++i) { - struct sway_container *child = ws->sway_workspace->floating->items[i]; + for (int i = 0; i < ws->floating->length; ++i) { + struct sway_container *child = ws->floating->items[i]; if (test(child, data)) { return child; } @@ -639,37 +553,76 @@ struct sway_container *workspace_find_container(struct sway_container *ws, return NULL; } -struct sway_container *workspace_wrap_children(struct sway_container *ws) { - struct sway_container *middle = container_create(C_CONTAINER); +struct sway_container *workspace_wrap_children(struct sway_workspace *ws) { + struct sway_container *middle = container_create(NULL); middle->layout = ws->layout; - while (ws->children->length) { - struct sway_container *child = ws->children->items[0]; - container_remove_child(child); + while (ws->tiling->length) { + struct sway_container *child = ws->tiling->items[0]; + container_detach(child); container_add_child(middle, child); } - container_add_child(ws, middle); + workspace_add_tiling(ws, middle); return middle; } -void workspace_add_floating(struct sway_container *workspace, - struct sway_container *con) { - if (!sway_assert(workspace->type == C_WORKSPACE, "Expected a workspace")) { - return; +void workspace_detach(struct sway_workspace *workspace) { + struct sway_output *output = workspace->output; + int index = list_find(output->workspaces, workspace); + if (index != -1) { + list_del(output->workspaces, index); } - if (!sway_assert(con->parent == NULL, "Expected an orphan container")) { - return; + workspace->output = NULL; + + node_set_dirty(&workspace->node); + node_set_dirty(&output->node); +} + +static void set_workspace(struct sway_container *container, void *data) { + container->workspace = container->parent->workspace; +} + +void workspace_add_tiling(struct sway_workspace *workspace, + struct sway_container *con) { + if (con->workspace) { + container_detach(con); } + list_add(workspace->tiling, con); + con->workspace = workspace; + container_for_each_child(con, set_workspace, NULL); + container_handle_fullscreen_reparent(con); + workspace_update_representation(workspace); + node_set_dirty(&workspace->node); + node_set_dirty(&con->node); +} - list_add(workspace->sway_workspace->floating, con); - con->parent = workspace; - container_set_dirty(workspace); - container_set_dirty(con); +void workspace_add_floating(struct sway_workspace *workspace, + struct sway_container *con) { + if (con->workspace) { + container_detach(con); + } + list_add(workspace->floating, con); + con->workspace = workspace; + container_for_each_child(con, set_workspace, NULL); + container_handle_fullscreen_reparent(con); + node_set_dirty(&workspace->node); + node_set_dirty(&con->node); } -void workspace_remove_gaps(struct sway_container *ws) { - if (!sway_assert(ws->type == C_WORKSPACE, "Expected a workspace")) { - return; +void workspace_insert_tiling(struct sway_workspace *workspace, + struct sway_container *con, int index) { + if (con->workspace) { + container_detach(con); } + list_insert(workspace->tiling, index, con); + con->workspace = workspace; + container_for_each_child(con, set_workspace, NULL); + container_handle_fullscreen_reparent(con); + workspace_update_representation(workspace); + node_set_dirty(&workspace->node); + node_set_dirty(&con->node); +} + +void workspace_remove_gaps(struct sway_workspace *ws) { if (ws->current_gaps == 0) { return; } @@ -681,15 +634,12 @@ void workspace_remove_gaps(struct sway_container *ws) { ws->current_gaps = 0; } -void workspace_add_gaps(struct sway_container *ws) { - if (!sway_assert(ws->type == C_WORKSPACE, "Expected a workspace")) { - return; - } +void workspace_add_gaps(struct sway_workspace *ws) { if (ws->current_gaps > 0) { return; } bool should_apply = - config->edge_gaps || (config->smart_gaps && ws->children->length > 1); + config->edge_gaps || (config->smart_gaps && ws->tiling->length > 1); if (!should_apply) { return; } @@ -708,3 +658,36 @@ void workspace_add_gaps(struct sway_container *ws) { ws->width -= 2 * ws->current_gaps; ws->height -= 2 * ws->current_gaps; } + +struct sway_container *workspace_split(struct sway_workspace *workspace, + enum sway_container_layout layout) { + if (workspace->tiling->length == 0) { + workspace->prev_split_layout = workspace->layout; + workspace->layout = layout; + return NULL; + } + + enum sway_container_layout old_layout = workspace->layout; + struct sway_container *middle = workspace_wrap_children(workspace); + workspace->layout = layout; + middle->layout = old_layout; + + return middle; +} + +void workspace_update_representation(struct sway_workspace *ws) { + size_t len = container_build_representation(ws->layout, ws->tiling, NULL); + free(ws->representation); + ws->representation = calloc(len + 1, sizeof(char)); + if (!sway_assert(ws->representation, "Unable to allocate title string")) { + return; + } + container_build_representation(ws->layout, ws->tiling, ws->representation); +} + +void workspace_get_box(struct sway_workspace *workspace, struct wlr_box *box) { + box->x = workspace->x; + box->y = workspace->y; + box->width = workspace->width; + box->height = workspace->height; +} -- 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/workspace.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