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.c648
1 files changed, 376 insertions, 272 deletions
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;
+}