Handle view destruction properly

This commit is contained in:
Drew DeVault 2017-11-25 16:30:15 -05:00
parent a57d462926
commit 8caabe59c2
6 changed files with 73 additions and 2 deletions

View file

@ -132,6 +132,8 @@ swayc_t *new_output(struct sway_output *sway_output);
swayc_t *new_workspace(swayc_t *output, const char *name); swayc_t *new_workspace(swayc_t *output, const char *name);
swayc_t *new_view(swayc_t *sibling, struct sway_view *sway_view); swayc_t *new_view(swayc_t *sibling, struct sway_view *sway_view);
swayc_t *destroy_view(swayc_t *view);
swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type); swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type);
#endif #endif

View file

@ -5,6 +5,7 @@ struct sway_container;
void init_layout(void); void init_layout(void);
void add_child(struct sway_container *parent, struct sway_container *child); void add_child(struct sway_container *parent, struct sway_container *child);
struct sway_container *remove_child(struct sway_container *child);
enum swayc_layouts default_layout(struct sway_container *output); enum swayc_layouts default_layout(struct sway_container *output);
void sort_workspaces(struct sway_container *output); void sort_workspaces(struct sway_container *output);
void arrange_windows(struct sway_container *container, double width, double height); void arrange_windows(struct sway_container *container, double width, double height);

View file

@ -14,6 +14,7 @@ struct sway_xdg_surface_v6 {
struct wl_listener request_move; struct wl_listener request_move;
struct wl_listener request_resize; struct wl_listener request_resize;
struct wl_listener request_maximize; struct wl_listener request_maximize;
struct wl_listener destroy;
int pending_width, pending_height; int pending_width, pending_height;
}; };
@ -38,7 +39,6 @@ enum sway_view_prop {
* tree (shell surfaces). * tree (shell surfaces).
*/ */
struct sway_view { struct sway_view {
struct wl_listener destroy;
enum sway_view_type type; enum sway_view_type type;
struct sway_container *swayc; struct sway_container *swayc;
struct wlr_surface *surface; struct wlr_surface *surface;

View file

@ -44,11 +44,22 @@ static void handle_commit(struct wl_listener *listener, void *data) {
sway_log(L_DEBUG, "xdg surface commit %dx%d", sway_log(L_DEBUG, "xdg surface commit %dx%d",
sway_surface->pending_width, sway_surface->pending_height); sway_surface->pending_width, sway_surface->pending_height);
// NOTE: We intentionally discard the view's desired width here // NOTE: We intentionally discard the view's desired width here
// TODO: Don't do that for floating views // TODO: Let floating views do whatever
view->width = sway_surface->pending_width; view->width = sway_surface->pending_width;
view->height = sway_surface->pending_height; view->height = sway_surface->pending_height;
} }
static void handle_destroy(struct wl_listener *listener, void *data) {
struct sway_xdg_surface_v6 *sway_xdg_surface =
wl_container_of(listener, sway_xdg_surface, destroy);
wl_list_remove(&sway_xdg_surface->commit.link);
wl_list_remove(&sway_xdg_surface->destroy.link);
swayc_t *parent = destroy_view(sway_xdg_surface->view->swayc);
free(sway_xdg_surface->view);
free(sway_xdg_surface);
arrange_windows(parent, -1, -1);
}
void handle_xdg_shell_v6_surface(struct wl_listener *listener, void *data) { void handle_xdg_shell_v6_surface(struct wl_listener *listener, void *data) {
struct sway_server *server = wl_container_of( struct sway_server *server = wl_container_of(
listener, server, xdg_shell_v6_surface); listener, server, xdg_shell_v6_surface);
@ -90,6 +101,8 @@ void handle_xdg_shell_v6_surface(struct wl_listener *listener, void *data) {
sway_surface->commit.notify = handle_commit; sway_surface->commit.notify = handle_commit;
wl_signal_add(&xdg_surface->events.commit, &sway_surface->commit); wl_signal_add(&xdg_surface->events.commit, &sway_surface->commit);
sway_surface->destroy.notify = handle_destroy;
wl_signal_add(&xdg_surface->events.destroy, &sway_surface->destroy);
// TODO: actual focus semantics // TODO: actual focus semantics
swayc_t *parent = root_container.children->items[0]; swayc_t *parent = root_container.children->items[0];

View file

@ -109,6 +109,48 @@ swayc_t *new_view(swayc_t *sibling, struct sway_view *sway_view) {
return swayc; return swayc;
} }
static void free_swayc(swayc_t *cont) {
if (!sway_assert(cont, "free_swayc passed NULL")) {
return;
}
if (cont->children) {
// remove children until there are no more, free_swayc calls
// remove_child, which removes child from this container
while (cont->children->length) {
free_swayc(cont->children->items[0]);
}
list_free(cont->children);
}
if (cont->marks) {
list_foreach(cont->marks, free);
list_free(cont->marks);
}
if (cont->parent) {
remove_child(cont);
}
if (cont->name) {
free(cont->name);
}
free(cont);
}
swayc_t *destroy_view(swayc_t *view) {
if (!sway_assert(view, "null view passed to destroy_view")) {
return NULL;
}
sway_log(L_DEBUG, "Destroying view '%s'", view->name);
swayc_t *parent = view->parent;
free_swayc(view);
// TODO WLR: Destroy empty containers
/*
if (parent && parent->type == C_CONTAINER) {
return destroy_container(parent);
}
*/
return parent;
}
swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type) { swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type) {
if (!sway_assert(container, "container is NULL")) { if (!sway_assert(container, "container is NULL")) {
return NULL; return NULL;

View file

@ -40,6 +40,19 @@ void add_child(swayc_t *parent, swayc_t *child) {
*/ */
} }
swayc_t *remove_child(swayc_t *child) {
int i;
swayc_t *parent = child->parent;
for (i = 0; i < parent->children->length; ++i) {
if (parent->children->items[i] == child) {
list_del(parent->children, i);
break;
}
}
child->parent = NULL;
return parent;
}
enum swayc_layouts default_layout(swayc_t *output) { enum swayc_layouts default_layout(swayc_t *output) {
/* TODO WLR /* TODO WLR
if (config->default_layout != L_NONE) { if (config->default_layout != L_NONE) {