aboutsummaryrefslogtreecommitdiff
path: root/sway/tree/container.c
diff options
context:
space:
mode:
Diffstat (limited to 'sway/tree/container.c')
-rw-r--r--sway/tree/container.c578
1 files changed, 324 insertions, 254 deletions
diff --git a/sway/tree/container.c b/sway/tree/container.c
index a8c6e667..ee019098 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) {
@@ -104,210 +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);
- list_free(cont->outputs);
-
- 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 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);
- }
-}
-
-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);
}
}
- 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;
-
- 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);
@@ -318,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;
}
@@ -371,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);
@@ -687,10 +476,11 @@ struct sway_container *container_at(struct sway_container *workspace,
return NULL;
}
struct sway_container *c;
- // Focused view's popups
struct sway_seat *seat = input_manager_current_seat(input_manager);
struct sway_container *focus =
seat_get_focus_inactive(seat, &root_container);
+ bool is_floating = focus && container_is_floating_or_child(focus);
+ // Focused view's popups
if (focus && focus->type == C_VIEW) {
surface_at_view(focus, lx, ly, surface, sx, sy);
if (*surface && surface_is_popup(*surface)) {
@@ -698,11 +488,27 @@ struct sway_container *container_at(struct sway_container *workspace,
}
*surface = NULL;
}
- // Floating
+ // If focused is floating, focused view's non-popups
+ if (focus && focus->type == C_VIEW && is_floating) {
+ surface_at_view(focus, lx, ly, surface, sx, sy);
+ if (*surface) {
+ return focus;
+ }
+ *surface = NULL;
+ }
+ // Floating (non-focused)
if ((c = floating_container_at(lx, ly, surface, sx, sy))) {
return c;
}
- // Tiling
+ // If focused is tiling, focused view's non-popups
+ if (focus && focus->type == C_VIEW && !is_floating) {
+ surface_at_view(focus, lx, ly, surface, sx, sy);
+ if (*surface) {
+ return focus;
+ }
+ *surface = NULL;
+ }
+ // Tiling (non-focused)
if ((c = tiling_container_at(workspace, lx, ly, surface, sx, sy))) {
return c;
}
@@ -1331,3 +1137,267 @@ void container_discover_outputs(struct sway_container *con) {
}
}
}
+
+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) {
+ return;
+ }
+ // Linear containers don't have gaps because it'd create double gaps
+ if (c->type == C_CONTAINER &&
+ c->layout != L_TABBED && c->layout != L_STACKED) {
+ return;
+ }
+ // Children of tabbed/stacked containers re-use the gaps of the container
+ enum sway_container_layout layout = c->parent->layout;
+ if (layout == L_TABBED || layout == L_STACKED) {
+ return;
+ }
+
+ struct sway_container *ws = container_parent(c, C_WORKSPACE);
+
+ c->current_gaps = ws->has_gaps ? ws->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);
+
+ cont->prev_split_layout = L_NONE;
+ cont->width = child->width;
+ cont->height = child->height;
+ cont->x = child->x;
+ cont->y = child->y;
+ cont->current_gaps = child->current_gaps;
+
+ struct sway_seat *seat = input_manager_get_default_seat(input_manager);
+ bool set_focus = (seat_get_focus(seat) == child);
+
+ 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;
+}