diff options
Diffstat (limited to 'sway/tree')
-rw-r--r-- | sway/tree/arrange.c | 75 | ||||
-rw-r--r-- | sway/tree/container.c | 648 | ||||
-rw-r--r-- | sway/tree/layout.c | 1027 | ||||
-rw-r--r-- | sway/tree/output.c | 126 | ||||
-rw-r--r-- | sway/tree/root.c | 4 | ||||
-rw-r--r-- | sway/tree/view.c | 72 | ||||
-rw-r--r-- | sway/tree/workspace.c | 99 |
7 files changed, 643 insertions, 1408 deletions
diff --git a/sway/tree/arrange.c b/sway/tree/arrange.c index cf4a5d9a..60e5b951 100644 --- a/sway/tree/arrange.c +++ b/sway/tree/arrange.c @@ -7,7 +7,6 @@ #include <wlr/types/wlr_output_layout.h> #include "sway/tree/arrange.h" #include "sway/tree/container.h" -#include "sway/tree/layout.h" #include "sway/output.h" #include "sway/tree/workspace.h" #include "sway/tree/view.h" @@ -39,7 +38,7 @@ static void apply_horiz_layout(struct sway_container *parent) { child->width = parent->width; } } - remove_gaps(child); + container_remove_gaps(child); total_width += child->width; } double scale = parent->width / total_width; @@ -62,7 +61,7 @@ static void apply_horiz_layout(struct sway_container *parent) { if (i == num_children - 1) { child->width = parent->x + parent->width - child->x; } - add_gaps(child); + container_add_gaps(child); } } @@ -91,7 +90,7 @@ static void apply_vert_layout(struct sway_container *parent) { child->height = parent_height; } } - remove_gaps(child); + container_remove_gaps(child); total_height += child->height; } double scale = parent_height / total_height; @@ -115,7 +114,7 @@ static void apply_vert_layout(struct sway_container *parent) { child->height = parent->y + parent_offset + parent_height - child->y; } - add_gaps(child); + container_add_gaps(child); } } @@ -133,12 +132,12 @@ static void apply_tabbed_or_stacked_layout(struct sway_container *parent) { size_t parent_height = parent->height - parent_offset; for (int i = 0; i < parent->children->length; ++i) { struct sway_container *child = parent->children->items[i]; - remove_gaps(child); + container_remove_gaps(child); child->x = parent->x; child->y = parent->y + parent_offset; child->width = parent->width; child->height = parent_height; - add_gaps(child); + container_add_gaps(child); } } @@ -205,12 +204,32 @@ static void arrange_workspace(struct sway_container *workspace) { struct wlr_box *area = &output->sway_output->usable_area; wlr_log(WLR_DEBUG, "Usable area for ws: %dx%d@%d,%d", area->width, area->height, area->x, area->y); - remove_gaps(workspace); + workspace_remove_gaps(workspace); + + double prev_x = workspace->x; + 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; - add_gaps(workspace); + + // Adjust any floating containers + double diff_x = workspace->x - prev_x; + double diff_y = workspace->y - prev_y; + for (int i = 0; i < workspace->sway_workspace->floating->length; ++i) { + struct sway_container *floater = + workspace->sway_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); + if (!wlr_box_contains_point(&workspace_box, center_x, center_y)) { + container_floating_move_to_center(floater); + } + } + + workspace_add_gaps(workspace); container_set_dirty(workspace); wlr_log(WLR_DEBUG, "Arranging workspace '%s' at %f, %f", workspace->name, workspace->x, workspace->y); @@ -294,41 +313,3 @@ void arrange_windows(struct sway_container *container) { break; } } - -void remove_gaps(struct sway_container *c) { - if (c->current_gaps == 0) { - wlr_log(WLR_DEBUG, "Removing gaps: not gapped: %p", c); - return; - } - - c->width += c->current_gaps * 2; - c->height += c->current_gaps * 2; - c->x -= c->current_gaps; - c->y -= c->current_gaps; - - c->current_gaps = 0; - - wlr_log(WLR_DEBUG, "Removing gaps %p", c); -} - -void add_gaps(struct sway_container *c) { - if (c->current_gaps > 0 || c->type == C_CONTAINER) { - wlr_log(WLR_DEBUG, "Not adding gaps: %p", c); - return; - } - - if (c->type == C_WORKSPACE && - !(config->edge_gaps || (config->smart_gaps && c->children->length > 1))) { - return; - } - - double gaps = c->has_gaps ? c->gaps_inner : config->gaps_inner; - - c->x += gaps; - c->y += gaps; - c->width -= 2 * gaps; - c->height -= 2 * gaps; - c->current_gaps = gaps; - - wlr_log(WLR_DEBUG, "Adding gaps: %p", c); -} diff --git a/sway/tree/container.c b/sway/tree/container.c index ff947ca8..04454ab6 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -19,7 +19,6 @@ #include "sway/output.h" #include "sway/server.h" #include "sway/tree/arrange.h" -#include "sway/tree/layout.h" #include "sway/tree/view.h" #include "sway/tree/workspace.h" #include "log.h" @@ -43,14 +42,12 @@ const char *container_type_to_str(enum sway_container_type type) { } void container_create_notify(struct sway_container *container) { - // TODO send ipc event type based on the container type - wl_signal_emit(&root_container.sway_root->events.new_container, container); - if (container->type == C_VIEW) { ipc_event_window(container, "new"); } else if (container->type == C_WORKSPACE) { ipc_event_workspace(NULL, container, "init"); } + wl_signal_emit(&root_container.sway_root->events.new_container, container); } void container_update_textures_recursive(struct sway_container *con) { @@ -76,31 +73,6 @@ void container_update_textures_recursive(struct sway_container *con) { } } -static void handle_reparent(struct wl_listener *listener, - void *data) { - struct sway_container *container = - wl_container_of(listener, container, reparent); - struct sway_container *old_parent = data; - - struct sway_container *old_output = old_parent; - if (old_output != NULL && old_output->type != C_OUTPUT) { - old_output = container_parent(old_output, C_OUTPUT); - } - - struct sway_container *new_output = container->parent; - if (new_output != NULL && new_output->type != C_OUTPUT) { - new_output = container_parent(new_output, C_OUTPUT); - } - - if (old_output && new_output) { - float old_scale = old_output->sway_output->wlr_output->scale; - float new_scale = new_output->sway_output->wlr_output->scale; - if (old_scale != new_scale) { - container_update_textures_recursive(container); - } - } -} - struct sway_container *container_create(enum sway_container_type type) { // next id starts at 1 because 0 is assigned to root_container in layout.c static size_t next_id = 1; @@ -117,12 +89,9 @@ struct sway_container *container_create(enum sway_container_type type) { c->children = create_list(); c->current.children = create_list(); } + c->outputs = create_list(); wl_signal_init(&c->events.destroy); - wl_signal_init(&c->events.reparent); - - wl_signal_add(&c->events.reparent, &c->reparent); - c->reparent.notify = handle_reparent; c->has_gaps = false; c->gaps_inner = 0; @@ -132,199 +101,53 @@ struct sway_container *container_create(enum sway_container_type type) { return c; } -static void container_workspace_free(struct sway_workspace *ws) { - list_foreach(ws->output_priority, free); - list_free(ws->output_priority); - list_free(ws->floating); - free(ws); -} - -void container_free(struct sway_container *cont) { - if (!sway_assert(cont->destroying, +void container_destroy(struct sway_container *con) { + if (!sway_assert(con->type == C_CONTAINER || con->type == C_VIEW, + "Expected a container or view")) { + return; + } + if (!sway_assert(con->destroying, "Tried to free container which wasn't marked as destroying")) { return; } - if (!sway_assert(cont->ntxnrefs == 0, "Tried to free container " + if (!sway_assert(con->ntxnrefs == 0, "Tried to free container " "which is still referenced by transactions")) { return; } - free(cont->name); - free(cont->formatted_title); - wlr_texture_destroy(cont->title_focused); - wlr_texture_destroy(cont->title_focused_inactive); - wlr_texture_destroy(cont->title_unfocused); - wlr_texture_destroy(cont->title_urgent); - list_free(cont->children); - list_free(cont->current.children); - - switch (cont->type) { - case C_ROOT: - break; - case C_OUTPUT: - break; - case C_WORKSPACE: - container_workspace_free(cont->sway_workspace); - break; - case C_CONTAINER: - break; - case C_VIEW: - { - struct sway_view *view = cont->sway_view; - view->swayc = NULL; - free(view->title_format); - view->title_format = NULL; - - if (view->destroying) { - view_free(view); - } - } - break; - case C_TYPES: - sway_assert(false, "Didn't expect to see C_TYPES here"); - break; - } - - free(cont); -} - -static struct sway_container *container_destroy_noreaping( - struct sway_container *con); - -static struct sway_container *container_workspace_destroy( - struct sway_container *workspace) { - if (!sway_assert(workspace, "cannot destroy null workspace")) { - return NULL; - } - - struct sway_container *output = container_parent(workspace, C_OUTPUT); - - // If we're destroying the output, it will be NULL here. Return the root so - // that it doesn't appear that the workspace has refused to be destoyed, - // which would leave it in a broken state with no parent. - if (output == NULL) { - return &root_container; - } - - // Do not destroy this if it's the last workspace on this output - if (output->children->length == 1) { - return NULL; - } - - wlr_log(WLR_DEBUG, "destroying workspace '%s'", workspace->name); - - if (!workspace_is_empty(workspace)) { - // Move children to a different workspace on this output - struct sway_container *new_workspace = NULL; - for (int i = 0; i < output->children->length; i++) { - if (output->children->items[i] != workspace) { - new_workspace = output->children->items[i]; - break; - } - } - - wlr_log(WLR_DEBUG, "moving children to different workspace '%s' -> '%s'", - workspace->name, new_workspace->name); - for (int i = 0; i < workspace->children->length; i++) { - container_move_to(workspace->children->items[i], new_workspace); - } - list_t *floating = workspace->sway_workspace->floating; - for (int i = 0; i < floating->length; i++) { - struct sway_container *floater = floating->items[i]; - container_remove_child(floater); - workspace_add_floating(new_workspace, floater); - } - } - - return output; -} - -static struct sway_container *container_output_destroy( - struct sway_container *output) { - if (!sway_assert(output, "cannot destroy null output")) { - return NULL; - } - - if (output->children->length > 0) { - // TODO save workspaces when there are no outputs. - // TODO also check if there will ever be no outputs except for exiting - // program - if (root_container.children->length > 1) { - // Move workspace from this output to another output - struct sway_container *fallback_output = - root_container.children->items[0]; - if (fallback_output == output) { - fallback_output = root_container.children->items[1]; - } - - while (output->children->length) { - struct sway_container *workspace = output->children->items[0]; - - struct sway_container *new_output = - workspace_output_get_highest_available(workspace, output); - if (!new_output) { - new_output = fallback_output; - workspace_output_add_priority(workspace, new_output); - } + free(con->name); + free(con->formatted_title); + wlr_texture_destroy(con->title_focused); + wlr_texture_destroy(con->title_focused_inactive); + wlr_texture_destroy(con->title_unfocused); + wlr_texture_destroy(con->title_urgent); + list_free(con->children); + list_free(con->current.children); + list_free(con->outputs); - container_remove_child(workspace); - if (!workspace_is_empty(workspace)) { - container_add_child(new_output, workspace); - ipc_event_workspace(NULL, workspace, "move"); - } else { - container_destroy(workspace); - } + if (con->type == C_VIEW) { + struct sway_view *view = con->sway_view; + view->swayc = NULL; + free(view->title_format); + view->title_format = NULL; - output_sort_workspaces(new_output); - } + if (view->destroying) { + view_destroy(view); } } - wl_list_remove(&output->sway_output->mode.link); - wl_list_remove(&output->sway_output->transform.link); - wl_list_remove(&output->sway_output->scale.link); - - wl_list_remove(&output->sway_output->damage_destroy.link); - wl_list_remove(&output->sway_output->damage_frame.link); - - output->sway_output->swayc = NULL; - output->sway_output = NULL; - - wlr_log(WLR_DEBUG, "OUTPUT: Destroying output '%s'", output->name); - - return &root_container; + free(con); } -/** - * Implement the actual destroy logic, without reaping. - */ -static struct sway_container *container_destroy_noreaping( - struct sway_container *con) { - if (con == NULL) { - return NULL; - } - if (con->destroying) { - return NULL; +void container_begin_destroy(struct sway_container *con) { + if (!sway_assert(con->type == C_CONTAINER || con->type == C_VIEW, + "Expected a container or view")) { + return; } - wl_signal_emit(&con->events.destroy, con); - - // emit IPC event if (con->type == C_VIEW) { ipc_event_window(con, "close"); - } else if (con->type == C_WORKSPACE) { - ipc_event_workspace(NULL, con, "empty"); - } - - // The below functions move their children to somewhere else. - if (con->type == C_OUTPUT) { - container_output_destroy(con); - } else if (con->type == C_WORKSPACE) { - // Workspaces will refuse to be destroyed if they're the last workspace - // on their output. - if (!container_workspace_destroy(con)) { - return NULL; - } } + wl_signal_emit(&con->events.destroy, con); container_end_mouse_operation(con); @@ -335,51 +158,22 @@ static struct sway_container *container_destroy_noreaping( root_scratchpad_remove_container(con); } - if (!con->parent) { - return NULL; - } - - return container_remove_child(con); -} - -bool container_reap_empty(struct sway_container *con) { - switch (con->type) { - case C_ROOT: - case C_OUTPUT: - // dont reap these - break; - case C_WORKSPACE: - if (!workspace_is_visible(con) && workspace_is_empty(con)) { - wlr_log(WLR_DEBUG, "Destroying workspace via reaper"); - container_destroy_noreaping(con); - return true; - } - break; - case C_CONTAINER: - if (con->children->length == 0) { - container_destroy_noreaping(con); - return true; - } - case C_VIEW: - break; - case C_TYPES: - sway_assert(false, "container_reap_empty called on an invalid " - "container"); - break; + if (con->parent) { + container_remove_child(con); } - - return false; } -struct sway_container *container_reap_empty_recursive( - struct sway_container *con) { - while (con) { +struct sway_container *container_reap_empty(struct sway_container *con) { + while (con && con->type == C_CONTAINER) { struct sway_container *next = con->parent; - if (!container_reap_empty(con)) { - break; + if (con->children->length == 0) { + container_begin_destroy(con); } con = next; } + if (con && con->type == C_WORKSPACE) { + workspace_consider_destroy(con); + } return con; } @@ -388,34 +182,12 @@ struct sway_container *container_flatten(struct sway_container *container) { struct sway_container *child = container->children->items[0]; struct sway_container *parent = container->parent; container_replace_child(container, child); - container_destroy_noreaping(container); + container_begin_destroy(container); container = parent; } return container; } -/** - * container_destroy() is the first step in destroying a container. We'll emit - * events, detach it from the tree and mark it as destroying. The container will - * remain in memory until it's no longer used by a transaction, then it will be - * freed via container_free(). - * - * This function just wraps container_destroy_noreaping(), then does reaping. - */ -struct sway_container *container_destroy(struct sway_container *con) { - if (con->is_fullscreen) { - struct sway_container *ws = container_parent(con, C_WORKSPACE); - ws->sway_workspace->fullscreen = NULL; - } - struct sway_container *parent = container_destroy_noreaping(con); - - if (!parent) { - return NULL; - } - - return container_reap_empty_recursive(parent); -} - static void container_close_func(struct sway_container *container, void *data) { if (container->type == C_VIEW) { view_close(container->sway_view); @@ -794,13 +566,24 @@ void container_damage_whole(struct sway_container *container) { } } +/** + * Return the output which will be used for scale purposes. + * This is the most recently entered output. + */ +struct sway_output *container_get_effective_output(struct sway_container *con) { + if (con->outputs->length == 0) { + return NULL; + } + return con->outputs->items[con->outputs->length - 1]; +} + static void update_title_texture(struct sway_container *con, struct wlr_texture **texture, struct border_colors *class) { if (!sway_assert(con->type == C_CONTAINER || con->type == C_VIEW, "Unexpected type %s", container_type_to_str(con->type))) { return; } - struct sway_container *output = container_parent(con, C_OUTPUT); + struct sway_output *output = container_get_effective_output(con); if (!output) { return; } @@ -812,7 +595,7 @@ static void update_title_texture(struct sway_container *con, return; } - double scale = output->sway_output->wlr_output->scale; + double scale = output->wlr_output->scale; int width = 0; int height = con->title_height * scale; @@ -840,7 +623,7 @@ static void update_title_texture(struct sway_container *con, unsigned char *data = cairo_image_surface_get_data(surface); int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width); struct wlr_renderer *renderer = wlr_backend_get_renderer( - output->sway_output->wlr_output->backend); + output->wlr_output->backend); *texture = wlr_texture_from_pixels( renderer, WL_SHM_FORMAT_ARGB8888, stride, width, height, data); cairo_surface_destroy(surface); @@ -1224,6 +1007,7 @@ void container_set_fullscreen(struct sway_container *container, bool enable) { container_set_fullscreen(workspace->sway_workspace->fullscreen, false); } + set_fullscreen_iterator(container, &enable); container_for_each_child(container, set_fullscreen_iterator, &enable); container->is_fullscreen = enable; @@ -1289,3 +1073,323 @@ bool container_is_fullscreen_or_child(struct sway_container *container) { return false; } + +static void surface_send_enter_iterator(struct wlr_surface *surface, + int x, int y, void *data) { + struct wlr_output *wlr_output = data; + wlr_surface_send_enter(surface, wlr_output); +} + +static void surface_send_leave_iterator(struct wlr_surface *surface, + int x, int y, void *data) { + struct wlr_output *wlr_output = data; + wlr_surface_send_leave(surface, wlr_output); +} + +void container_discover_outputs(struct sway_container *con) { + if (!sway_assert(con->type == C_CONTAINER || con->type == C_VIEW, + "Expected a container or view")) { + return; + } + struct wlr_box con_box = { + .x = con->current.swayc_x, + .y = con->current.swayc_y, + .width = con->current.swayc_width, + .height = con->current.swayc_height, + }; + struct sway_output *old_output = container_get_effective_output(con); + + for (int i = 0; i < root_container.children->length; ++i) { + struct sway_container *output = root_container.children->items[i]; + struct sway_output *sway_output = output->sway_output; + struct wlr_box output_box; + container_get_box(output, &output_box); + struct wlr_box intersection; + bool intersects = + wlr_box_intersection(&con_box, &output_box, &intersection); + int index = list_find(con->outputs, sway_output); + + if (intersects && index == -1) { + // Send enter + wlr_log(WLR_DEBUG, "Con %p entered output %p", con, sway_output); + if (con->type == C_VIEW) { + view_for_each_surface(con->sway_view, + surface_send_enter_iterator, sway_output->wlr_output); + } + list_add(con->outputs, sway_output); + } else if (!intersects && index != -1) { + // Send leave + wlr_log(WLR_DEBUG, "Con %p left output %p", con, sway_output); + if (con->type == C_VIEW) { + view_for_each_surface(con->sway_view, + surface_send_leave_iterator, sway_output->wlr_output); + } + list_del(con->outputs, index); + } + } + struct sway_output *new_output = container_get_effective_output(con); + double old_scale = old_output ? old_output->wlr_output->scale : -1; + double new_scale = new_output ? new_output->wlr_output->scale : -1; + if (old_scale != new_scale) { + container_update_title_textures(con); + if (con->type == C_VIEW) { + view_update_marks_textures(con->sway_view); + } + } +} + +void container_remove_gaps(struct sway_container *c) { + if (!sway_assert(c->type == C_CONTAINER || c->type == C_VIEW, + "Expected a container or view")) { + return; + } + if (c->current_gaps == 0) { + return; + } + + c->width += c->current_gaps * 2; + c->height += c->current_gaps * 2; + c->x -= c->current_gaps; + c->y -= c->current_gaps; + c->current_gaps = 0; +} + +void container_add_gaps(struct sway_container *c) { + if (!sway_assert(c->type == C_CONTAINER || c->type == C_VIEW, + "Expected a container or view")) { + return; + } + if (c->current_gaps > 0 || c->type != C_VIEW) { + return; + } + + c->current_gaps = c->has_gaps ? c->gaps_inner : config->gaps_inner; + c->x += c->current_gaps; + c->y += c->current_gaps; + c->width -= 2 * c->current_gaps; + c->height -= 2 * c->current_gaps; +} + +int container_sibling_index(const struct sway_container *child) { + return list_find(child->parent->children, child); +} + +void container_handle_fullscreen_reparent(struct sway_container *con, + struct sway_container *old_parent) { + if (!con->is_fullscreen) { + return; + } + struct sway_container *old_workspace = old_parent; + if (old_workspace && old_workspace->type != C_WORKSPACE) { + old_workspace = container_parent(old_workspace, C_WORKSPACE); + } + struct sway_container *new_workspace = container_parent(con, C_WORKSPACE); + if (old_workspace == new_workspace) { + return; + } + // Unmark the old workspace as fullscreen + if (old_workspace) { + old_workspace->sway_workspace->fullscreen = NULL; + } + + // Mark the new workspace as fullscreen + if (new_workspace->sway_workspace->fullscreen) { + container_set_fullscreen( + new_workspace->sway_workspace->fullscreen, false); + } + new_workspace->sway_workspace->fullscreen = con; + + // Resize container to new output dimensions + struct sway_container *output = new_workspace->parent; + con->x = output->x; + con->y = output->y; + con->width = output->width; + con->height = output->height; + + if (con->type == C_VIEW) { + struct sway_view *view = con->sway_view; + view->x = output->x; + view->y = output->y; + view->width = output->width; + view->height = output->height; + } else { + arrange_windows(new_workspace); + } +} + +void container_insert_child(struct sway_container *parent, + struct sway_container *child, int i) { + struct sway_container *old_parent = child->parent; + if (old_parent) { + container_remove_child(child); + } + wlr_log(WLR_DEBUG, "Inserting id:%zd at index %d", child->id, i); + list_insert(parent->children, i, child); + child->parent = parent; + container_handle_fullscreen_reparent(child, old_parent); +} + +struct sway_container *container_add_sibling(struct sway_container *fixed, + struct sway_container *active) { + // TODO handle floating + struct sway_container *old_parent = NULL; + if (active->parent) { + old_parent = active->parent; + container_remove_child(active); + } + struct sway_container *parent = fixed->parent; + int i = container_sibling_index(fixed); + list_insert(parent->children, i + 1, active); + active->parent = parent; + container_handle_fullscreen_reparent(active, old_parent); + return active->parent; +} + +void container_add_child(struct sway_container *parent, + struct sway_container *child) { + wlr_log(WLR_DEBUG, "Adding %p (%d, %fx%f) to %p (%d, %fx%f)", + child, child->type, child->width, child->height, + parent, parent->type, parent->width, parent->height); + struct sway_container *old_parent = child->parent; + list_add(parent->children, child); + child->parent = parent; + container_handle_fullscreen_reparent(child, old_parent); + if (old_parent) { + container_set_dirty(old_parent); + } + container_set_dirty(child); +} + +struct sway_container *container_remove_child(struct sway_container *child) { + if (child->is_fullscreen) { + struct sway_container *workspace = container_parent(child, C_WORKSPACE); + workspace->sway_workspace->fullscreen = NULL; + } + + struct sway_container *parent = child->parent; + list_t *list = container_is_floating(child) ? + parent->sway_workspace->floating : parent->children; + int index = list_find(list, child); + if (index != -1) { + list_del(list, index); + } + child->parent = NULL; + container_notify_subtree_changed(parent); + + container_set_dirty(parent); + container_set_dirty(child); + + return parent; +} + +enum sway_container_layout container_get_default_layout( + struct sway_container *con) { + if (con->type != C_OUTPUT) { + con = container_parent(con, C_OUTPUT); + } + + if (!sway_assert(con != NULL, + "container_get_default_layout must be called on an attached" + " container below the root container")) { + return 0; + } + + if (config->default_layout != L_NONE) { + return config->default_layout; + } else if (config->default_orientation != L_NONE) { + return config->default_orientation; + } else if (con->width >= con->height) { + return L_HORIZ; + } else { + return L_VERT; + } +} + +struct sway_container *container_replace_child(struct sway_container *child, + struct sway_container *new_child) { + struct sway_container *parent = child->parent; + if (parent == NULL) { + return NULL; + } + + list_t *list = container_is_floating(child) ? + parent->sway_workspace->floating : parent->children; + int i = list_find(list, child); + + if (new_child->parent) { + container_remove_child(new_child); + } + list->items[i] = new_child; + new_child->parent = parent; + child->parent = NULL; + + // Set geometry for new child + new_child->x = child->x; + new_child->y = child->y; + new_child->width = child->width; + new_child->height = child->height; + + // reset geometry for child + child->width = 0; + child->height = 0; + + return parent; +} + +struct sway_container *container_split(struct sway_container *child, + enum sway_container_layout layout) { + // TODO floating: cannot split a floating container + if (!sway_assert(child, "child cannot be null")) { + return NULL; + } + if (child->type == C_WORKSPACE && child->children->length == 0) { + // Special case: this just behaves like splitt + child->prev_split_layout = child->layout; + child->layout = layout; + return child; + } + + struct sway_container *cont = container_create(C_CONTAINER); + + wlr_log(WLR_DEBUG, "creating container %p around %p", cont, child); + + child->type == C_WORKSPACE ? workspace_remove_gaps(child) + : container_remove_gaps(child); + + cont->prev_split_layout = L_NONE; + cont->width = child->width; + cont->height = child->height; + cont->x = child->x; + cont->y = child->y; + + struct sway_seat *seat = input_manager_get_default_seat(input_manager); + bool set_focus = (seat_get_focus(seat) == child); + + container_add_gaps(cont); + + if (child->type == C_WORKSPACE) { + struct sway_container *workspace = child; + while (workspace->children->length) { + struct sway_container *ws_child = workspace->children->items[0]; + container_remove_child(ws_child); + container_add_child(cont, ws_child); + } + + container_add_child(workspace, cont); + enum sway_container_layout old_layout = workspace->layout; + workspace->layout = layout; + cont->layout = old_layout; + } else { + cont->layout = layout; + container_replace_child(child, cont); + container_add_child(cont, child); + } + + if (set_focus) { + seat_set_focus(seat, cont); + seat_set_focus(seat, child); + } + + container_notify_subtree_changed(cont); + return cont; +} diff --git a/sway/tree/layout.c b/sway/tree/layout.c deleted file mode 100644 index 2f22a3dd..00000000 --- a/sway/tree/layout.c +++ /dev/null @@ -1,1027 +0,0 @@ -#define _POSIX_C_SOURCE 200809L -#include <math.h> -#include <stdbool.h> -#include <stdlib.h> -#include <string.h> -#include <wlr/types/wlr_output.h> -#include <wlr/types/wlr_output_layout.h> -#include "config.h" -#include "sway/debug.h" -#include "sway/tree/arrange.h" -#include "sway/tree/container.h" -#include "sway/tree/layout.h" -#include "sway/output.h" -#include "sway/tree/workspace.h" -#include "sway/tree/view.h" -#include "sway/input/seat.h" -#include "sway/ipc-server.h" -#include "list.h" -#include "log.h" - -static int index_child(const struct sway_container *child) { - return list_find(child->parent->children, child); -} - -static void container_handle_fullscreen_reparent(struct sway_container *con, - struct sway_container *old_parent) { - if (!con->is_fullscreen) { - return; - } - struct sway_container *old_workspace = old_parent; - if (old_workspace && old_workspace->type != C_WORKSPACE) { - old_workspace = container_parent(old_workspace, C_WORKSPACE); - } - struct sway_container *new_workspace = container_parent(con, C_WORKSPACE); - if (old_workspace == new_workspace) { - return; - } - // Unmark the old workspace as fullscreen - if (old_workspace) { - old_workspace->sway_workspace->fullscreen = NULL; - } - - // Mark the new workspace as fullscreen - if (new_workspace->sway_workspace->fullscreen) { - container_set_fullscreen( - new_workspace->sway_workspace->fullscreen, false); - } - new_workspace->sway_workspace->fullscreen = con; - - // Resize container to new output dimensions - struct sway_container *output = new_workspace->parent; - con->x = output->x; - con->y = output->y; - con->width = output->width; - con->height = output->height; - - if (con->type == C_VIEW) { - struct sway_view *view = con->sway_view; - view->x = output->x; - view->y = output->y; - view->width = output->width; - view->height = output->height; - } else { - arrange_windows(new_workspace); - } -} - -void container_insert_child(struct sway_container *parent, - struct sway_container *child, int i) { - struct sway_container *old_parent = child->parent; - if (old_parent) { - container_remove_child(child); - } - wlr_log(WLR_DEBUG, "Inserting id:%zd at index %d", child->id, i); - list_insert(parent->children, i, child); - child->parent = parent; - container_handle_fullscreen_reparent(child, old_parent); - wl_signal_emit(&child->events.reparent, old_parent); -} - -struct sway_container *container_add_sibling(struct sway_container *fixed, - struct sway_container *active) { - // TODO handle floating - struct sway_container *old_parent = NULL; - if (active->parent) { - old_parent = active->parent; - container_remove_child(active); - } - struct sway_container *parent = fixed->parent; - int i = index_child(fixed); - list_insert(parent->children, i + 1, active); - active->parent = parent; - container_handle_fullscreen_reparent(active, old_parent); - wl_signal_emit(&active->events.reparent, old_parent); - return active->parent; -} - -void container_add_child(struct sway_container *parent, - struct sway_container *child) { - wlr_log(WLR_DEBUG, "Adding %p (%d, %fx%f) to %p (%d, %fx%f)", - child, child->type, child->width, child->height, - parent, parent->type, parent->width, parent->height); - struct sway_container *old_parent = child->parent; - list_add(parent->children, child); - child->parent = parent; - container_handle_fullscreen_reparent(child, old_parent); - if (old_parent) { - container_set_dirty(old_parent); - } - container_set_dirty(child); -} - -struct sway_container *container_remove_child(struct sway_container *child) { - if (child->is_fullscreen) { - struct sway_container *workspace = container_parent(child, C_WORKSPACE); - workspace->sway_workspace->fullscreen = NULL; - } - - struct sway_container *parent = child->parent; - list_t *list = container_is_floating(child) ? - parent->sway_workspace->floating : parent->children; - int index = list_find(list, child); - if (index != -1) { - list_del(list, index); - } - child->parent = NULL; - container_notify_subtree_changed(parent); - - container_set_dirty(parent); - container_set_dirty(child); - - return parent; -} - -void container_move_to(struct sway_container *container, - struct sway_container *destination) { - if (!sway_assert(container->type == C_CONTAINER || - container->type == C_VIEW, "Expected a container or view")) { - return; - } - if (container == destination - || container_has_ancestor(container, destination)) { - return; - } - struct sway_container *old_parent = NULL; - struct sway_container *new_parent = NULL; - if (container_is_floating(container)) { - // Resolve destination into a workspace - struct sway_container *new_ws = NULL; - if (destination->type == C_OUTPUT) { - new_ws = output_get_active_workspace(destination->sway_output); - } else if (destination->type == C_WORKSPACE) { - new_ws = destination; - } else { - new_ws = container_parent(destination, C_WORKSPACE); - } - if (!new_ws) { - // This can happen if the user has run "move container to mark foo", - // where mark foo is on a hidden scratchpad container. - return; - } - struct sway_container *old_output = - container_parent(container, C_OUTPUT); - old_parent = container_remove_child(container); - workspace_add_floating(new_ws, container); - container_handle_fullscreen_reparent(container, old_parent); - // If changing output, center it within the workspace - if (old_output != new_ws->parent && !container->is_fullscreen) { - container_floating_move_to_center(container); - } - } else { - old_parent = container_remove_child(container); - container->width = container->height = 0; - container->saved_width = container->saved_height = 0; - - if (destination->type == C_VIEW) { - new_parent = container_add_sibling(destination, container); - } else { - new_parent = destination; - container_add_child(destination, container); - } - } - - wl_signal_emit(&container->events.reparent, old_parent); - - if (container->type == C_VIEW) { - ipc_event_window(container, "move"); - } - container_notify_subtree_changed(old_parent); - container_notify_subtree_changed(new_parent); - - // If view was moved to a fullscreen workspace, refocus the fullscreen view - struct sway_container *new_workspace = container; - if (new_workspace->type != C_WORKSPACE) { - new_workspace = container_parent(new_workspace, C_WORKSPACE); - } - if (new_workspace->sway_workspace->fullscreen) { - struct sway_seat *seat; - struct sway_container *focus, *focus_ws; - wl_list_for_each(seat, &input_manager->seats, link) { - focus = seat_get_focus(seat); - focus_ws = focus; - if (focus_ws->type != C_WORKSPACE) { - focus_ws = container_parent(focus_ws, C_WORKSPACE); - } - if (focus_ws == new_workspace) { - struct sway_container *new_focus = seat_get_focus_inactive(seat, - new_workspace->sway_workspace->fullscreen); - seat_set_focus(seat, new_focus); - } - } - } - // Update workspace urgent state - struct sway_container *old_workspace = old_parent; - if (old_workspace->type != C_WORKSPACE) { - old_workspace = container_parent(old_workspace, C_WORKSPACE); - } - if (new_workspace != old_workspace) { - workspace_detect_urgent(new_workspace); - if (old_workspace) { - workspace_detect_urgent(old_workspace); - } - } -} - -static bool sway_dir_to_wlr(enum movement_direction dir, - enum wlr_direction *out) { - switch (dir) { - case MOVE_UP: - *out = WLR_DIRECTION_UP; - break; - case MOVE_DOWN: - *out = WLR_DIRECTION_DOWN; - break; - case MOVE_LEFT: - *out = WLR_DIRECTION_LEFT; - break; - case MOVE_RIGHT: - *out = WLR_DIRECTION_RIGHT; - break; - default: - return false; - } - - return true; -} - -static bool is_parallel(enum sway_container_layout layout, - enum movement_direction dir) { - switch (layout) { - case L_TABBED: - case L_HORIZ: - return dir == MOVE_LEFT || dir == MOVE_RIGHT; - case L_STACKED: - case L_VERT: - return dir == MOVE_UP || dir == MOVE_DOWN; - default: - return false; - } -} - -static enum movement_direction invert_movement(enum movement_direction dir) { - switch (dir) { - case MOVE_LEFT: - return MOVE_RIGHT; - case MOVE_RIGHT: - return MOVE_LEFT; - case MOVE_UP: - return MOVE_DOWN; - case MOVE_DOWN: - return MOVE_UP; - default: - sway_assert(0, "This function expects left|right|up|down"); - return MOVE_LEFT; - } -} - -static int move_offs(enum movement_direction move_dir) { - return move_dir == MOVE_LEFT || move_dir == MOVE_UP ? -1 : 1; -} - -/* Gets the index of the most extreme member based on the movement offset */ -static int container_limit(struct sway_container *container, - enum movement_direction move_dir) { - return move_offs(move_dir) < 0 ? 0 : container->children->length; -} - -/* Takes one child, sets it aside, wraps the rest of the children in a new - * container, switches the layout of the workspace, and drops the child back in. - * In other words, rejigger it. */ -static void workspace_rejigger(struct sway_container *ws, - struct sway_container *child, enum movement_direction move_dir) { - struct sway_container *original_parent = child->parent; - struct sway_container *new_parent = - container_split(ws, ws->layout); - - container_remove_child(child); - for (int i = 0; i < ws->children->length; ++i) { - struct sway_container *_child = ws->children->items[i]; - container_move_to(new_parent, _child); - } - - int index = move_offs(move_dir); - container_insert_child(ws, child, index < 0 ? 0 : 1); - ws->layout = - move_dir == MOVE_LEFT || move_dir == MOVE_RIGHT ? L_HORIZ : L_VERT; - - container_flatten(ws); - container_reap_empty_recursive(original_parent); - wl_signal_emit(&child->events.reparent, original_parent); - container_create_notify(new_parent); -} - -static void move_out_of_tabs_stacks(struct sway_container *container, - struct sway_container *current, enum movement_direction move_dir, - int offs) { - if (container->parent == current->parent - && current->parent->children->length == 1) { - wlr_log(WLR_DEBUG, "Changing layout of %zd", current->parent->id); - current->parent->layout = move_dir == - MOVE_LEFT || move_dir == MOVE_RIGHT ? L_HORIZ : L_VERT; - return; - } - - wlr_log(WLR_DEBUG, "Moving out of tab/stack into a split"); - bool is_workspace = current->parent->type == C_WORKSPACE; - struct sway_container *new_parent = container_split(current->parent, - move_dir == MOVE_LEFT || move_dir == MOVE_RIGHT ? L_HORIZ : L_VERT); - if (is_workspace) { - container_insert_child(new_parent->parent, container, offs < 0 ? 0 : 1); - } else { - container_insert_child(new_parent, container, offs < 0 ? 0 : 1); - container_reap_empty_recursive(new_parent->parent); - container_flatten(new_parent->parent); - } - container_create_notify(new_parent); - container_notify_subtree_changed(new_parent); -} - -void container_move(struct sway_container *container, - enum movement_direction move_dir, int move_amt) { - if (!sway_assert( - container->type != C_CONTAINER || container->type != C_VIEW, - "Can only move containers and views")) { - return; - } - int offs = move_offs(move_dir); - - struct sway_container *sibling = NULL; - struct sway_container *current = container; - struct sway_container *parent = current->parent; - struct sway_container *top = &root_container; - - // If moving a fullscreen view, only consider outputs - if (container->is_fullscreen) { - current = container_parent(container, C_OUTPUT); - } else if (container_is_fullscreen_or_child(container) || - container_is_floating_or_child(container)) { - // If we've fullscreened a split container, only allow the child to move - // around within the fullscreen parent. - // Same with floating a split container. - struct sway_container *ws = container_parent(container, C_WORKSPACE); - top = ws->sway_workspace->fullscreen; - } - - struct sway_container *new_parent = container_flatten(parent); - if (new_parent != parent) { - // Special case: we were the last one in this container, so leave - return; - } - - while (!sibling) { - if (current == top) { - return; - } - - parent = current->parent; - wlr_log(WLR_DEBUG, "Visiting %p %s '%s'", current, - container_type_to_str(current->type), current->name); - - int index = index_child(current); - - switch (current->type) { - case C_OUTPUT: { - enum wlr_direction wlr_dir = 0; - if (!sway_assert(sway_dir_to_wlr(move_dir, &wlr_dir), - "got invalid direction: %d", move_dir)) { - return; - } - double ref_lx = current->x + current->width / 2; - double ref_ly = current->y + current->height / 2; - struct wlr_output *next = wlr_output_layout_adjacent_output( - root_container.sway_root->output_layout, wlr_dir, - current->sway_output->wlr_output, ref_lx, ref_ly); - if (!next) { - wlr_log(WLR_DEBUG, "Hit edge of output, nowhere else to go"); - return; - } - struct sway_output *next_output = next->data; - current = next_output->swayc; - wlr_log(WLR_DEBUG, "Selected next output (%s)", current->name); - // Select workspace and get outta here - current = seat_get_focus_inactive( - config->handler_context.seat, current); - if (current->type != C_WORKSPACE) { - current = container_parent(current, C_WORKSPACE); - } - sibling = current; - break; - } - case C_WORKSPACE: - if (!is_parallel(current->layout, move_dir)) { - if (current->children->length >= 2) { - wlr_log(WLR_DEBUG, "Rejiggering the workspace (%d kiddos)", - current->children->length); - workspace_rejigger(current, container, move_dir); - return; - } else { - wlr_log(WLR_DEBUG, "Selecting output"); - current = current->parent; - } - } else if (current->layout == L_TABBED - || current->layout == L_STACKED) { - wlr_log(WLR_DEBUG, "Rejiggering out of tabs/stacks"); - workspace_rejigger(current, container, move_dir); - } else { - wlr_log(WLR_DEBUG, "Selecting output"); - current = current->parent; - } - break; - case C_CONTAINER: - case C_VIEW: - if (is_parallel(parent->layout, move_dir)) { - if ((index == parent->children->length - 1 && offs > 0) - || (index == 0 && offs < 0)) { - if (current->parent == container->parent) { - if (!parent->is_fullscreen && - (parent->layout == L_TABBED || - parent->layout == L_STACKED)) { - move_out_of_tabs_stacks(container, current, - move_dir, offs); - return; - } else { - wlr_log(WLR_DEBUG, "Hit limit, selecting parent"); - current = current->parent; - } - } else { - wlr_log(WLR_DEBUG, "Hit limit, " - "promoting descendant to sibling"); - // Special case - container_insert_child(current->parent, container, - index + (offs < 0 ? 0 : 1)); - container->width = container->height = 0; - return; - } - } else { - sibling = parent->children->items[index + offs]; - wlr_log(WLR_DEBUG, "Selecting sibling id:%zd", sibling->id); - } - } else if (!parent->is_fullscreen && (parent->layout == L_TABBED || - parent->layout == L_STACKED)) { - move_out_of_tabs_stacks(container, current, move_dir, offs); - return; - } else { - wlr_log(WLR_DEBUG, "Moving up to find a parallel container"); - current = current->parent; - } - break; - default: - sway_assert(0, "Not expecting to see container of type %s here", - container_type_to_str(current->type)); - return; - } - } - - // Part two: move stuff around - int index = index_child(container); - struct sway_container *old_parent = container->parent; - - while (sibling) { - switch (sibling->type) { - case C_VIEW: - if (sibling->parent == container->parent) { - wlr_log(WLR_DEBUG, "Swapping siblings"); - sibling->parent->children->items[index + offs] = container; - sibling->parent->children->items[index] = sibling; - } else { - wlr_log(WLR_DEBUG, "Promoting to sibling of cousin"); - container_insert_child(sibling->parent, container, - index_child(sibling) + (offs > 0 ? 0 : 1)); - container->width = container->height = 0; - } - sibling = NULL; - break; - case C_WORKSPACE: // Note: only in the case of moving between outputs - case C_CONTAINER: - if (is_parallel(sibling->layout, move_dir)) { - int limit = container_limit(sibling, invert_movement(move_dir)); - wlr_log(WLR_DEBUG, "limit: %d", limit); - wlr_log(WLR_DEBUG, - "Reparenting container (parallel) to index %d " - "(move dir: %d)", limit, move_dir); - container_insert_child(sibling, container, limit); - container->width = container->height = 0; - sibling = NULL; - } else { - wlr_log(WLR_DEBUG, "Reparenting container (perpendicular)"); - struct sway_container *focus_inactive = seat_get_focus_inactive( - config->handler_context.seat, sibling); - if (focus_inactive && focus_inactive != sibling) { - while (focus_inactive->parent != sibling) { - focus_inactive = focus_inactive->parent; - } - wlr_log(WLR_DEBUG, "Focus inactive: id:%zd", - focus_inactive->id); - sibling = focus_inactive; - continue; - } else if (sibling->children->length) { - wlr_log(WLR_DEBUG, "No focus-inactive, adding arbitrarily"); - container_remove_child(container); - container_add_sibling(sibling->children->items[0], container); - } else { - wlr_log(WLR_DEBUG, "No kiddos, adding container alone"); - container_remove_child(container); - container_add_child(sibling, container); - } - container->width = container->height = 0; - sibling = NULL; - } - break; - default: - sway_assert(0, "Not expecting to see container of type %s here", - container_type_to_str(sibling->type)); - return; - } - } - - container_notify_subtree_changed(old_parent); - container_notify_subtree_changed(container->parent); - - if (container->type == C_VIEW) { - ipc_event_window(container, "move"); - } - - if (old_parent) { - seat_set_focus(config->handler_context.seat, old_parent); - seat_set_focus(config->handler_context.seat, container); - } - - struct sway_container *last_ws = old_parent; - struct sway_container *next_ws = container->parent; - if (last_ws && last_ws->type != C_WORKSPACE) { - last_ws = container_parent(last_ws, C_WORKSPACE); - } - if (next_ws && next_ws->type != C_WORKSPACE) { - next_ws = container_parent(next_ws, C_WORKSPACE); - } - if (last_ws && next_ws && last_ws != next_ws) { - ipc_event_workspace(last_ws, next_ws, "focus"); - workspace_detect_urgent(last_ws); - workspace_detect_urgent(next_ws); - } - container_end_mouse_operation(container); -} - -enum sway_container_layout container_get_default_layout( - struct sway_container *con) { - if (con->type != C_OUTPUT) { - con = container_parent(con, C_OUTPUT); - } - - if (!sway_assert(con != NULL, - "container_get_default_layout must be called on an attached" - " container below the root container")) { - return 0; - } - - if (config->default_layout != L_NONE) { - return config->default_layout; - } else if (config->default_orientation != L_NONE) { - return config->default_orientation; - } else if (con->width >= con->height) { - return L_HORIZ; - } else { - return L_VERT; - } -} - -/** - * Get swayc in the direction of newly entered output. - */ -static struct sway_container *get_swayc_in_output_direction( - struct sway_container *output, enum movement_direction dir, - struct sway_seat *seat) { - if (!output) { - return NULL; - } - - struct sway_container *ws = seat_get_focus_inactive(seat, output); - if (ws->type != C_WORKSPACE) { - ws = container_parent(ws, C_WORKSPACE); - } - - if (ws == NULL) { - wlr_log(WLR_ERROR, "got an output without a workspace"); - return NULL; - } - - if (ws->children->length > 0) { - switch (dir) { - case MOVE_LEFT: - if (ws->layout == L_HORIZ || ws->layout == L_TABBED) { - // get most right child of new output - return ws->children->items[ws->children->length-1]; - } else { - return seat_get_focus_inactive(seat, ws); - } - case MOVE_RIGHT: - if (ws->layout == L_HORIZ || ws->layout == L_TABBED) { - // get most left child of new output - return ws->children->items[0]; - } else { - return seat_get_focus_inactive(seat, ws); - } - case MOVE_UP: - case MOVE_DOWN: { - struct sway_container *focused = - seat_get_focus_inactive(seat, ws); - if (focused && focused->parent) { - struct sway_container *parent = focused->parent; - if (parent->layout == L_VERT) { - if (dir == MOVE_UP) { - // get child furthest down on new output - int idx = parent->children->length - 1; - return parent->children->items[idx]; - } else if (dir == MOVE_DOWN) { - // get child furthest up on new output - return parent->children->items[0]; - } - } - return focused; - } - break; - } - default: - break; - } - } - - return ws; -} - -static struct sway_container *sway_output_from_wlr(struct wlr_output *output) { - if (output == NULL) { - return NULL; - } - for (int i = 0; i < root_container.children->length; ++i) { - struct sway_container *o = root_container.children->items[i]; - if (o->type == C_OUTPUT && o->sway_output->wlr_output == output) { - return o; - } - } - return NULL; -} - -struct sway_container *container_get_in_direction( - struct sway_container *container, struct sway_seat *seat, - enum movement_direction dir) { - struct sway_container *parent = container->parent; - - if (dir == MOVE_CHILD) { - return seat_get_focus_inactive(seat, container); - } - if (container->is_fullscreen) { - if (dir == MOVE_PARENT) { - return NULL; - } - container = container_parent(container, C_OUTPUT); - parent = container->parent; - } else { - if (dir == MOVE_PARENT) { - if (parent->type == C_OUTPUT || container_is_floating(container)) { - return NULL; - } else { - return parent; - } - } - } - - struct sway_container *wrap_candidate = NULL; - while (true) { - bool can_move = false; - int desired; - int idx = index_child(container); - if (idx == -1) { - return NULL; - } - if (parent->type == C_ROOT) { - enum wlr_direction wlr_dir = 0; - if (!sway_assert(sway_dir_to_wlr(dir, &wlr_dir), - "got invalid direction: %d", dir)) { - return NULL; - } - int lx = container->x + container->width / 2; - int ly = container->y + container->height / 2; - struct wlr_output_layout *layout = - root_container.sway_root->output_layout; - struct wlr_output *wlr_adjacent = - wlr_output_layout_adjacent_output(layout, wlr_dir, - container->sway_output->wlr_output, lx, ly); - struct sway_container *adjacent = - sway_output_from_wlr(wlr_adjacent); - - if (!adjacent || adjacent == container) { - if (!wrap_candidate) { - return NULL; - } - return seat_get_focus_inactive_view(seat, wrap_candidate); - } - struct sway_container *next = - get_swayc_in_output_direction(adjacent, dir, seat); - if (next == NULL) { - return NULL; - } - struct sway_container *next_workspace = next; - if (next_workspace->type != C_WORKSPACE) { - next_workspace = container_parent(next_workspace, C_WORKSPACE); - } - sway_assert(next_workspace, "Next container has no workspace"); - if (next_workspace->sway_workspace->fullscreen) { - return seat_get_focus_inactive(seat, - next_workspace->sway_workspace->fullscreen); - } - if (next->children && next->children->length) { - // TODO consider floating children as well - return seat_get_focus_inactive_view(seat, next); - } else { - return next; - } - } else { - if (dir == MOVE_LEFT || dir == MOVE_RIGHT) { - if (parent->layout == L_HORIZ || parent->layout == L_TABBED) { - can_move = true; - desired = idx + (dir == MOVE_LEFT ? -1 : 1); - } - } else { - if (parent->layout == L_VERT || parent->layout == L_STACKED) { - can_move = true; - desired = idx + (dir == MOVE_UP ? -1 : 1); - } - } - } - - if (can_move) { - // TODO handle floating - if (desired < 0 || desired >= parent->children->length) { - can_move = false; - int len = parent->children->length; - if (config->focus_wrapping != WRAP_NO && !wrap_candidate - && len > 1) { - if (desired < 0) { - wrap_candidate = parent->children->items[len-1]; - } else { - wrap_candidate = parent->children->items[0]; - } - if (config->focus_wrapping == WRAP_FORCE) { - return seat_get_focus_inactive_view(seat, - wrap_candidate); - } - } - } else { - struct sway_container *desired_con = - parent->children->items[desired]; - wlr_log(WLR_DEBUG, - "cont %d-%p dir %i sibling %d: %p", idx, - container, dir, desired, desired_con); - return seat_get_focus_inactive_view(seat, desired_con); - } - } - - if (!can_move) { - container = parent; - parent = parent->parent; - if (!parent) { - // wrapping is the last chance - if (!wrap_candidate) { - return NULL; - } - return seat_get_focus_inactive_view(seat, wrap_candidate); - } - } - } -} - -struct sway_container *container_replace_child(struct sway_container *child, - struct sway_container *new_child) { - struct sway_container *parent = child->parent; - if (parent == NULL) { - return NULL; - } - - list_t *list = container_is_floating(child) ? - parent->sway_workspace->floating : parent->children; - int i = list_find(list, child); - - if (new_child->parent) { - container_remove_child(new_child); - } - list->items[i] = new_child; - new_child->parent = parent; - child->parent = NULL; - - // Set geometry for new child - new_child->x = child->x; - new_child->y = child->y; - new_child->width = child->width; - new_child->height = child->height; - - // reset geometry for child - child->width = 0; - child->height = 0; - - return parent; -} - -struct sway_container *container_split(struct sway_container *child, - enum sway_container_layout layout) { - // TODO floating: cannot split a floating container - if (!sway_assert(child, "child cannot be null")) { - return NULL; - } - if (child->type == C_WORKSPACE && child->children->length == 0) { - // Special case: this just behaves like splitt - child->prev_split_layout = child->layout; - child->layout = layout; - return child; - } - - struct sway_container *cont = container_create(C_CONTAINER); - - wlr_log(WLR_DEBUG, "creating container %p around %p", cont, child); - - remove_gaps(child); - - cont->prev_split_layout = L_NONE; - cont->width = child->width; - cont->height = child->height; - cont->x = child->x; - cont->y = child->y; - - struct sway_seat *seat = input_manager_get_default_seat(input_manager); - bool set_focus = (seat_get_focus(seat) == child); - - add_gaps(cont); - - if (child->type == C_WORKSPACE) { - struct sway_container *workspace = child; - while (workspace->children->length) { - struct sway_container *ws_child = workspace->children->items[0]; - container_remove_child(ws_child); - container_add_child(cont, ws_child); - wl_signal_emit(&ws_child->events.reparent, workspace); - } - - container_add_child(workspace, cont); - enum sway_container_layout old_layout = workspace->layout; - workspace->layout = layout; - cont->layout = old_layout; - } else { - struct sway_container *old_parent = child->parent; - cont->layout = layout; - container_replace_child(child, cont); - container_add_child(cont, child); - wl_signal_emit(&child->events.reparent, old_parent); - } - - if (set_focus) { - seat_set_focus(seat, cont); - seat_set_focus(seat, child); - } - - container_notify_subtree_changed(cont); - return cont; -} - -void container_recursive_resize(struct sway_container *container, - double amount, enum resize_edge edge) { - bool layout_match = true; - wlr_log(WLR_DEBUG, "Resizing %p with amount: %f", container, amount); - if (edge == RESIZE_EDGE_LEFT || edge == RESIZE_EDGE_RIGHT) { - container->width += amount; - layout_match = container->layout == L_HORIZ; - } else if (edge == RESIZE_EDGE_TOP || edge == RESIZE_EDGE_BOTTOM) { - container->height += amount; - layout_match = container->layout == L_VERT; - } - if (container->children) { - for (int i = 0; i < container->children->length; i++) { - struct sway_container *child = container->children->items[i]; - double amt = layout_match ? - amount / container->children->length : amount; - container_recursive_resize(child, amt, edge); - } - } -} - -static void swap_places(struct sway_container *con1, - struct sway_container *con2) { - struct sway_container *temp = malloc(sizeof(struct sway_container)); - temp->x = con1->x; - temp->y = con1->y; - temp->width = con1->width; - temp->height = con1->height; - temp->parent = con1->parent; - - con1->x = con2->x; - con1->y = con2->y; - con1->width = con2->width; - con1->height = con2->height; - - con2->x = temp->x; - con2->y = temp->y; - con2->width = temp->width; - con2->height = temp->height; - - int temp_index = index_child(con1); - container_insert_child(con2->parent, con1, index_child(con2)); - container_insert_child(temp->parent, con2, temp_index); - - free(temp); -} - -static void swap_focus(struct sway_container *con1, - struct sway_container *con2, struct sway_seat *seat, - struct sway_container *focus) { - if (focus == con1 || focus == con2) { - struct sway_container *ws1 = container_parent(con1, C_WORKSPACE); - struct sway_container *ws2 = container_parent(con2, C_WORKSPACE); - if (focus == con1 && (con2->parent->layout == L_TABBED - || con2->parent->layout == L_STACKED)) { - if (workspace_is_visible(ws2)) { - seat_set_focus_warp(seat, con2, false, true); - } - seat_set_focus(seat, ws1 != ws2 ? con2 : con1); - } else if (focus == con2 && (con1->parent->layout == L_TABBED - || con1->parent->layout == L_STACKED)) { - if (workspace_is_visible(ws1)) { - seat_set_focus_warp(seat, con1, false, true); - } - seat_set_focus(seat, ws1 != ws2 ? con1 : con2); - } else if (ws1 != ws2) { - seat_set_focus(seat, focus == con1 ? con2 : con1); - } else { - seat_set_focus(seat, focus); - } - } else { - seat_set_focus(seat, focus); - } -} - -void container_swap(struct sway_container *con1, struct sway_container *con2) { - if (!sway_assert(con1 && con2, "Cannot swap with nothing")) { - return; - } - if (!sway_assert(con1->type >= C_CONTAINER && con2->type >= C_CONTAINER, - "Can only swap containers and views")) { - return; - } - if (!sway_assert(!container_has_ancestor(con1, con2) - && !container_has_ancestor(con2, con1), - "Cannot swap ancestor and descendant")) { - return; - } - if (!sway_assert(!container_is_floating(con1) - && !container_is_floating(con2), - "Swapping with floating containers is not supported")) { - return; - } - - wlr_log(WLR_DEBUG, "Swapping containers %zu and %zu", con1->id, con2->id); - - int fs1 = con1->is_fullscreen; - int fs2 = con2->is_fullscreen; - if (fs1) { - container_set_fullscreen(con1, false); - } - if (fs2) { - container_set_fullscreen(con2, false); - } - - struct sway_seat *seat = input_manager_get_default_seat(input_manager); - struct sway_container *focus = seat_get_focus(seat); - struct sway_container *vis1 = container_parent( - seat_get_focus_inactive(seat, container_parent(con1, C_OUTPUT)), - C_WORKSPACE); - struct sway_container *vis2 = container_parent( - seat_get_focus_inactive(seat, container_parent(con2, C_OUTPUT)), - C_WORKSPACE); - - char *stored_prev_name = NULL; - if (prev_workspace_name) { - stored_prev_name = strdup(prev_workspace_name); - } - - swap_places(con1, con2); - - if (!workspace_is_visible(vis1)) { - seat_set_focus(seat, seat_get_focus_inactive(seat, vis1)); - } - if (!workspace_is_visible(vis2)) { - seat_set_focus(seat, seat_get_focus_inactive(seat, vis2)); - } - - swap_focus(con1, con2, seat, focus); - - if (stored_prev_name) { - free(prev_workspace_name); - prev_workspace_name = stored_prev_name; - } - - if (fs1) { - container_set_fullscreen(con2, true); - } - if (fs2) { - container_set_fullscreen(con1, true); - } -} diff --git a/sway/tree/output.c b/sway/tree/output.c index 6da63064..6601220b 100644 --- a/sway/tree/output.c +++ b/sway/tree/output.c @@ -10,6 +10,7 @@ #include "log.h" static void restore_workspaces(struct sway_container *output) { + // Workspace output priority for (int i = 0; i < root_container.children->length; i++) { struct sway_container *other = root_container.children->items[i]; if (other == output) { @@ -29,6 +30,15 @@ static void restore_workspaces(struct sway_container *output) { } } + // Saved workspaces + list_t *saved = root_container.sway_root->saved_workspaces; + for (int i = 0; i < saved->length; ++i) { + struct sway_container *ws = saved->items[i]; + container_add_child(output, ws); + ipc_event_workspace(NULL, ws, "move"); + } + saved->length = 0; + output_sort_workspaces(output); } @@ -68,7 +78,7 @@ struct sway_container *output_create( output->sway_output = sway_output; output->name = strdup(name); if (output->name == NULL) { - container_destroy(output); + output_begin_destroy(output); return NULL; } @@ -103,6 +113,120 @@ struct sway_container *output_create( return output; } +static void output_evacuate(struct sway_container *output) { + if (!output->children->length) { + return; + } + struct sway_container *fallback_output = NULL; + if (root_container.children->length > 1) { + fallback_output = root_container.children->items[0]; + if (fallback_output == output) { + fallback_output = root_container.children->items[1]; + } + } + + while (output->children->length) { + struct sway_container *workspace = output->children->items[0]; + + container_remove_child(workspace); + + if (workspace_is_empty(workspace)) { + workspace_begin_destroy(workspace); + continue; + } + + struct sway_container *new_output = + workspace_output_get_highest_available(workspace, output); + if (!new_output) { + new_output = fallback_output; + } + + if (new_output) { + workspace_output_add_priority(workspace, new_output); + container_add_child(new_output, workspace); + output_sort_workspaces(new_output); + ipc_event_workspace(NULL, workspace, "move"); + } else { + list_add(root_container.sway_root->saved_workspaces, workspace); + } + } +} + +void output_destroy(struct sway_container *output) { + if (!sway_assert(output->type == C_OUTPUT, "Expected an output")) { + return; + } + if (!sway_assert(output->destroying, + "Tried to free output which wasn't marked as destroying")) { + return; + } + if (!sway_assert(output->ntxnrefs == 0, "Tried to free output " + "which is still referenced by transactions")) { + return; + } + free(output->name); + free(output->formatted_title); + wlr_texture_destroy(output->title_focused); + wlr_texture_destroy(output->title_focused_inactive); + wlr_texture_destroy(output->title_unfocused); + wlr_texture_destroy(output->title_urgent); + list_free(output->children); + list_free(output->current.children); + list_free(output->outputs); + free(output); + + // NOTE: We don't actually destroy the sway_output here +} + +static void untrack_output(struct sway_container *con, void *data) { + struct sway_output *output = data; + int index = list_find(con->outputs, output); + if (index != -1) { + list_del(con->outputs, index); + } +} + +void output_begin_destroy(struct sway_container *output) { + if (!sway_assert(output->type == C_OUTPUT, "Expected an output")) { + return; + } + wlr_log(WLR_DEBUG, "OUTPUT: Destroying output '%s'", output->name); + wl_signal_emit(&output->events.destroy, output); + + output_evacuate(output); + + output->destroying = true; + container_set_dirty(output); + + root_for_each_container(untrack_output, output->sway_output); + + wl_list_remove(&output->sway_output->mode.link); + wl_list_remove(&output->sway_output->transform.link); + wl_list_remove(&output->sway_output->scale.link); + wl_list_remove(&output->sway_output->damage_destroy.link); + wl_list_remove(&output->sway_output->damage_frame.link); + + output->sway_output->swayc = NULL; + output->sway_output = NULL; + + if (output->parent) { + container_remove_child(output); + } +} + +struct sway_container *output_from_wlr_output(struct wlr_output *output) { + if (output == NULL) { + return NULL; + } + for (int i = 0; i < root_container.children->length; ++i) { + struct sway_container *o = root_container.children->items[i]; + if (o->type == C_OUTPUT && o->sway_output->wlr_output == output) { + return o; + } + } + return NULL; +} + void output_for_each_workspace(struct sway_container *output, void (*f)(struct sway_container *con, void *data), void *data) { if (!sway_assert(output->type == C_OUTPUT, "Expected an output")) { diff --git a/sway/tree/root.c b/sway/tree/root.c index c27ff2c3..2dd8f9f2 100644 --- a/sway/tree/root.c +++ b/sway/tree/root.c @@ -32,13 +32,14 @@ void root_create(void) { root_container.sway_root = calloc(1, sizeof(*root_container.sway_root)); root_container.sway_root->output_layout = wlr_output_layout_create(); - wl_list_init(&root_container.sway_root->outputs); + wl_list_init(&root_container.sway_root->all_outputs); #ifdef HAVE_XWAYLAND wl_list_init(&root_container.sway_root->xwayland_unmanaged); #endif wl_list_init(&root_container.sway_root->drag_icons); wl_signal_init(&root_container.sway_root->events.new_container); root_container.sway_root->scratchpad = create_list(); + root_container.sway_root->saved_workspaces = create_list(); root_container.sway_root->output_layout_change.notify = output_layout_handle_change; @@ -50,6 +51,7 @@ void root_destroy(void) { // sway_root wl_list_remove(&root_container.sway_root->output_layout_change.link); list_free(root_container.sway_root->scratchpad); + list_free(root_container.sway_root->saved_workspaces); wlr_output_layout_destroy(root_container.sway_root->output_layout); free(root_container.sway_root); diff --git a/sway/tree/view.c b/sway/tree/view.c index b77a9bb2..2870d4f5 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -18,7 +18,6 @@ #include "sway/input/seat.h" #include "sway/tree/arrange.h" #include "sway/tree/container.h" -#include "sway/tree/layout.h" #include "sway/tree/view.h" #include "sway/tree/workspace.h" #include "sway/config.h" @@ -35,7 +34,7 @@ void view_init(struct sway_view *view, enum sway_view_type type, wl_signal_init(&view->events.unmap); } -void view_free(struct sway_view *view) { +void view_destroy(struct sway_view *view) { if (!sway_assert(view->surface == NULL, "Tried to free mapped view")) { return; } @@ -75,14 +74,14 @@ void view_free(struct sway_view *view) { * destroying flag will make the view get freed when the transaction is * finished. */ -void view_destroy(struct sway_view *view) { +void view_begin_destroy(struct sway_view *view) { if (!sway_assert(view->surface == NULL, "Tried to destroy a mapped view")) { return; } view->destroying = true; if (!view->swayc) { - view_free(view); + view_destroy(view); } } @@ -236,7 +235,12 @@ void view_autoconfigure(struct sway_view *view) { view->border_top = false; } - switch (view->border) { + enum sway_container_border border = view->border; + if (view->using_csd) { + border = B_NONE; + } + + switch (border) { case B_NONE: x = con->x; y = con->y + y_offset; @@ -364,48 +368,6 @@ static void view_handle_surface_new_subsurface(struct wl_listener *listener, view_subsurface_create(view, subsurface); } -static void surface_send_enter_iterator(struct wlr_surface *surface, - int x, int y, void *data) { - struct wlr_output *wlr_output = data; - wlr_surface_send_enter(surface, wlr_output); -} - -static void surface_send_leave_iterator(struct wlr_surface *surface, - int x, int y, void *data) { - struct wlr_output *wlr_output = data; - wlr_surface_send_leave(surface, wlr_output); -} - -static void view_handle_container_reparent(struct wl_listener *listener, - void *data) { - struct sway_view *view = - wl_container_of(listener, view, container_reparent); - struct sway_container *old_parent = data; - - struct sway_container *old_output = old_parent; - if (old_output != NULL && old_output->type != C_OUTPUT) { - old_output = container_parent(old_output, C_OUTPUT); - } - - struct sway_container *new_output = view->swayc->parent; - if (new_output != NULL && new_output->type != C_OUTPUT) { - new_output = container_parent(new_output, C_OUTPUT); - } - - if (old_output == new_output) { - return; - } - - if (old_output != NULL) { - view_for_each_surface(view, surface_send_leave_iterator, - old_output->sway_output->wlr_output); - } - if (new_output != NULL) { - view_for_each_surface(view, surface_send_enter_iterator, - new_output->sway_output->wlr_output); - } -} - static bool view_has_executed_criteria(struct sway_view *view, struct criteria *criteria) { for (int i = 0; i < view->executed_criteria->length; ++i) { @@ -567,9 +529,6 @@ void view_map(struct sway_view *view, struct wlr_surface *wlr_surface) { &view->surface_new_subsurface); view->surface_new_subsurface.notify = view_handle_surface_new_subsurface; - wl_signal_add(&view->swayc->events.reparent, &view->container_reparent); - view->container_reparent.notify = view_handle_container_reparent; - if (view->impl->wants_floating && view->impl->wants_floating(view)) { view->border = config->floating_border; view->border_thickness = config->floating_border_thickness; @@ -587,15 +546,12 @@ void view_map(struct sway_view *view, struct wlr_surface *wlr_surface) { view_update_title(view, false); container_notify_subtree_changed(view->swayc->parent); view_execute_criteria(view); - - view_handle_container_reparent(&view->container_reparent, NULL); } void view_unmap(struct sway_view *view) { wl_signal_emit(&view->events.unmap, view); wl_list_remove(&view->surface_new_subsurface.link); - wl_list_remove(&view->container_reparent.link); if (view->urgent_timer) { wl_event_source_remove(view->urgent_timer); @@ -603,7 +559,9 @@ void view_unmap(struct sway_view *view) { } bool was_fullscreen = view->swayc->is_fullscreen; - struct sway_container *surviving_ancestor = container_destroy(view->swayc); + struct sway_container *parent = view->swayc->parent; + container_begin_destroy(view->swayc); + struct sway_container *surviving_ancestor = container_reap_empty(parent); // If the workspace wasn't reaped if (surviving_ancestor && surviving_ancestor->type >= C_WORKSPACE) { @@ -937,7 +895,7 @@ void view_add_mark(struct sway_view *view, char *mark) { static void update_marks_texture(struct sway_view *view, struct wlr_texture **texture, struct border_colors *class) { - struct sway_container *output = container_parent(view->swayc, C_OUTPUT); + struct sway_output *output = container_get_effective_output(view->swayc); if (!output) { return; } @@ -973,7 +931,7 @@ static void update_marks_texture(struct sway_view *view, } free(part); - double scale = output->sway_output->wlr_output->scale; + double scale = output->wlr_output->scale; int width = 0; int height = view->swayc->title_height * scale; @@ -999,7 +957,7 @@ static void update_marks_texture(struct sway_view *view, unsigned char *data = cairo_image_surface_get_data(surface); int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width); struct wlr_renderer *renderer = wlr_backend_get_renderer( - output->sway_output->wlr_output->backend); + output->wlr_output->backend); *texture = wlr_texture_from_pixels( renderer, WL_SHM_FORMAT_ARGB8888, stride, width, height, data); cairo_surface_destroy(surface); diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c index 292f2c9a..d930826e 100644 --- a/sway/tree/workspace.c +++ b/sway/tree/workspace.c @@ -79,6 +79,65 @@ struct sway_container *workspace_create(struct sway_container *output, return workspace; } +void workspace_destroy(struct sway_container *workspace) { + if (!sway_assert(workspace->type == C_WORKSPACE, "Expected a workspace")) { + return; + } + if (!sway_assert(workspace->destroying, + "Tried to free workspace which wasn't marked as destroying")) { + return; + } + if (!sway_assert(workspace->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); +} + +void workspace_begin_destroy(struct sway_container *workspace) { + if (!sway_assert(workspace->type == C_WORKSPACE, "Expected a workspace")) { + return; + } + wlr_log(WLR_DEBUG, "Destroying workspace '%s'", workspace->name); + wl_signal_emit(&workspace->events.destroy, workspace); + ipc_event_workspace(NULL, workspace, "empty"); // intentional + + workspace->destroying = true; + container_set_dirty(workspace); + + if (workspace->parent) { + container_remove_child(workspace); + } +} + +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) { + workspace_begin_destroy(ws); + } +} + char *prev_workspace_name = NULL; void next_name_map(struct sway_container *ws, void *data) { @@ -205,6 +264,7 @@ char *workspace_next_name(const char *output_name) { && workspace_by_name(wso->workspace) == NULL) { free(target); target = strdup(wso->workspace); + break; } } if (target != NULL) { @@ -420,9 +480,7 @@ bool workspace_switch(struct sway_container *workspace, // no op. We therefore need to send the IPC event and clean up the old // workspace here. ipc_event_workspace(active_ws, workspace, "focus"); - if (!workspace_is_visible(active_ws) && workspace_is_empty(active_ws)) { - container_destroy(active_ws); - } + workspace_consider_destroy(active_ws); } seat_set_focus(seat, next); struct sway_container *output = container_parent(workspace, C_OUTPUT); @@ -607,3 +665,38 @@ void workspace_add_floating(struct sway_container *workspace, container_set_dirty(workspace); container_set_dirty(con); } + +void workspace_remove_gaps(struct sway_container *ws) { + if (!sway_assert(ws->type == C_WORKSPACE, "Expected a workspace")) { + return; + } + if (ws->current_gaps == 0) { + return; + } + + ws->width += ws->current_gaps * 2; + ws->height += ws->current_gaps * 2; + ws->x -= ws->current_gaps; + ws->y -= ws->current_gaps; + ws->current_gaps = 0; +} + +void workspace_add_gaps(struct sway_container *ws) { + if (!sway_assert(ws->type == C_WORKSPACE, "Expected a workspace")) { + return; + } + if (ws->current_gaps > 0) { + return; + } + bool should_apply = + config->edge_gaps || (config->smart_gaps && ws->children->length > 1); + if (!should_apply) { + return; + } + + ws->current_gaps = ws->has_gaps ? ws->gaps_inner : config->gaps_inner; + ws->x += ws->current_gaps; + ws->y += ws->current_gaps; + ws->width -= 2 * ws->current_gaps; + ws->height -= 2 * ws->current_gaps; +} |