From b4c5f79725142c78a398a22981392d645bc9d2e9 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Tue, 3 Apr 2018 12:25:19 -0400 Subject: move view and workspace destructors to container.c --- include/sway/tree/container.h | 7 ++-- sway/input/seat.c | 3 +- sway/tree/container.c | 83 ++++++++++++++++++++++++++++++++++++++----- sway/tree/view.c | 13 ++----- sway/tree/workspace.c | 39 -------------------- 5 files changed, 83 insertions(+), 62 deletions(-) diff --git a/include/sway/tree/container.h b/include/sway/tree/container.h index 1286316a..278505ce 100644 --- a/include/sway/tree/container.h +++ b/include/sway/tree/container.h @@ -128,11 +128,12 @@ struct sway_container *container_view_create( struct sway_container *sibling, struct sway_view *sway_view); // TODO don't return the parent on destroy -void container_destroy(struct sway_container *container); +struct sway_container *container_destroy(struct sway_container *container); + +// TODO make me private +struct sway_container *container_finish(struct sway_container *cont); -struct sway_container *container_workspace_destroy(struct sway_container *container); struct sway_container *container_output_destroy(struct sway_container *container); -void container_view_destroy(struct sway_container *container); struct sway_container *container_close(struct sway_container *container); diff --git a/sway/input/seat.c b/sway/input/seat.c index c41f7b2e..d752acb8 100644 --- a/sway/input/seat.c +++ b/sway/input/seat.c @@ -381,7 +381,8 @@ void seat_set_focus_warp(struct sway_seat *seat, if (last_ws) { ipc_event_workspace(last_ws, container, "focus"); if (last_ws->children->length == 0) { - container_workspace_destroy(last_ws); + output_damage_whole(last_ws->parent->sway_output); + container_destroy(last_ws); } } struct sway_container *last_output = last_focus; diff --git a/sway/tree/container.c b/sway/tree/container.c index d9fc61e7..c1ebf4f1 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -77,7 +77,7 @@ struct sway_container *container_create(enum sway_container_type type) { return c; } -static struct sway_container *container_finish(struct sway_container *cont) { +struct sway_container *container_finish(struct sway_container *cont) { if (cont == NULL) { return NULL; } @@ -91,7 +91,7 @@ static struct sway_container *container_finish(struct sway_container *cont) { while (cont->children != NULL && cont->children->length != 0) { struct sway_container *child = cont->children->items[0]; container_remove_child(child); - container_destroy(child); + container_finish(child); } } if (cont->marks) { @@ -109,6 +109,45 @@ static struct sway_container *container_finish(struct sway_container *cont) { free(cont); return parent; } +static struct sway_container *container_workspace_destroy( + struct sway_container *workspace) { + if (!sway_assert(workspace, "cannot destroy null workspace")) { + return NULL; + } + + // Do not destroy this if it's the last workspace on this output + struct sway_container *output = container_parent(workspace, C_OUTPUT); + if (output && output->children->length == 1) { + return NULL; + } + + struct sway_container *parent = workspace->parent; + if (workspace->children->length == 0) { + // destroy the WS if there are no children (TODO check for floating) + wlr_log(L_DEBUG, "destroying workspace '%s'", workspace->name); + ipc_event_workspace(workspace, NULL, "empty"); + } else { + // Move children to a different workspace on this output + struct sway_container *new_workspace = NULL; + // TODO move floating + for (int i = 0; i < output->children->length; i++) { + if (output->children->items[i] != workspace) { + new_workspace = output->children->items[i]; + break; + } + } + + wlr_log(L_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); + } + } + + container_finish(workspace); + return parent; +} + static void reap_empty_func(struct sway_container *con, void *data) { switch (con->type) { @@ -146,18 +185,46 @@ struct sway_container *container_reap_empty(struct sway_container *container) { return parent; } +static void container_root_finish(struct sway_container *con) { + wlr_log(L_ERROR, "TODO: destroy the root container"); +} -void container_destroy(struct sway_container *cont) { - if (cont == NULL) { - return; +struct sway_container *container_destroy(struct sway_container *con) { + if (con == NULL) { + return NULL; } - if (cont->children != NULL && cont->children->length) { - assert(false && "dont destroy containers with children"); + struct sway_container *anscestor = NULL; + + switch (con->type) { + case C_ROOT: + container_root_finish(con); + break; + case C_OUTPUT: + anscestor = container_output_destroy(con); + break; + case C_WORKSPACE: + anscestor = container_workspace_destroy(con); + break; + case C_CONTAINER: + if (con->children != NULL && con->children->length) { + assert(false && "dont destroy container containers with children"); + } + container_finish(con); + // TODO return parent to arrange maybe? + break; + case C_VIEW: + container_finish(con); + // TODO return parent to arrange maybe? + break; + case C_TYPES: + wlr_log(L_ERROR, "tried to destroy an invalid container"); + break; } - container_finish(cont); container_reap_empty(&root_container); + + return anscestor; } static void container_close_func(struct sway_container *container, void *data) { diff --git a/sway/tree/view.c b/sway/tree/view.c index eeadc5d8..c06924f5 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -27,8 +27,7 @@ void view_destroy(struct sway_view *view) { view_unmap(view); } - container_view_destroy(view->swayc); - free(view); + container_destroy(view->swayc); } const char *view_get_title(struct sway_view *view) { @@ -78,14 +77,6 @@ void view_close(struct sway_view *view) { } } -void container_view_destroy(struct sway_container *view) { - if (!view) { - return; - } - wlr_log(L_DEBUG, "Destroying view '%s'", view->name); - container_destroy(view); -} - void view_damage_whole(struct sway_view *view) { for (int i = 0; i < root_container.children->length; ++i) { struct sway_container *cont = root_container.children->items[i]; @@ -158,7 +149,7 @@ void view_unmap(struct sway_view *view) { view_damage_whole(view); - container_view_destroy(view->swayc); + container_destroy(view->swayc); view->swayc = NULL; view->surface = NULL; diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c index 74330884..7d180009 100644 --- a/sway/tree/workspace.c +++ b/sway/tree/workspace.c @@ -208,45 +208,6 @@ struct sway_container *workspace_create(const char *name) { return new_ws; } -struct sway_container *container_workspace_destroy( - struct sway_container *workspace) { - if (!sway_assert(workspace, "cannot destroy null workspace")) { - return NULL; - } - - // Do not destroy this if it's the last workspace on this output - struct sway_container *output = container_parent(workspace, C_OUTPUT); - if (output && output->children->length == 1) { - return NULL; - } - - struct sway_container *parent = workspace->parent; - if (workspace->children->length == 0) { - // destroy the WS if there are no children (TODO check for floating) - wlr_log(L_DEBUG, "destroying workspace '%s'", workspace->name); - ipc_event_workspace(workspace, NULL, "empty"); - } else { - // Move children to a different workspace on this output - struct sway_container *new_workspace = NULL; - // TODO move floating - for (int i = 0; i < output->children->length; i++) { - if (output->children->items[i] != workspace) { - new_workspace = output->children->items[i]; - break; - } - } - - wlr_log(L_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); - } - } - - container_destroy(workspace); - return parent; -} - /** * Get the previous or next workspace on the specified output. Wraps around at * the end and beginning. If next is false, the previous workspace is returned, -- cgit v1.2.3-54-g00ecf