From 8625fe4245fe140462f37e6470595cd31293485c Mon Sep 17 00:00:00 2001 From: Ian Date: Thu, 16 Aug 2018 14:40:55 +0100 Subject: [PATCH 1/2] Track seats so that they are properly freed on exit --- include/slurp.h | 6 ++++++ main.c | 25 +++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/include/slurp.h b/include/slurp.h index 7244516..4af6ffe 100644 --- a/include/slurp.h +++ b/include/slurp.h @@ -23,6 +23,7 @@ struct slurp_state { struct zwlr_layer_shell_v1 *layer_shell; struct wl_list outputs; // slurp_output::link struct wl_list pointers; // slurp_pointer::link + struct wl_list seats; // slurp_seat::link struct { uint32_t background; @@ -71,6 +72,11 @@ struct slurp_pointer { struct wl_surface *cursor_surface; }; +struct slurp_seat { + struct wl_seat *wl_seat; + struct wl_list link; // slurp_state::seats +}; + void pointer_get_box(struct slurp_pointer *pointer, int *x, int *y, int *width, int *height); diff --git a/main.c b/main.c index 85c9cfc..b162e74 100644 --- a/main.c +++ b/main.c @@ -151,6 +151,22 @@ static const struct wl_seat_listener seat_listener = { .capabilities = seat_handle_capabilities, }; +static void create_seat(struct slurp_state *state, struct wl_seat *wl_seat) { + struct slurp_seat *seat = calloc(1, sizeof(struct slurp_seat)); + if (seat == NULL) { + fprintf(stderr, "allocation failed\n"); + return; + } + seat->wl_seat = wl_seat; + wl_list_insert(&state->seats, &seat->link); + wl_seat_add_listener(wl_seat, &seat_listener, state); +} + +static void destroy_seat(struct slurp_seat *seat) { + wl_list_remove(&seat->link); + wl_seat_destroy(seat->wl_seat); + free(seat); +} static void output_handle_geometry(void *data, struct wl_output *wl_output, int32_t x, int32_t y, int32_t physical_width, int32_t physical_height, @@ -315,9 +331,9 @@ static void handle_global(void *data, struct wl_registry *registry, state->layer_shell = wl_registry_bind(registry, name, &zwlr_layer_shell_v1_interface, 1); } else if (strcmp(interface, wl_seat_interface.name) == 0) { - struct wl_seat *seat = + struct wl_seat *wl_seat = wl_registry_bind(registry, name, &wl_seat_interface, 1); - wl_seat_add_listener(seat, &seat_listener, state); + create_seat(state, wl_seat); } else if (strcmp(interface, wl_output_interface.name) == 0) { struct wl_output *wl_output = wl_registry_bind(registry, name, &wl_output_interface, 3); @@ -405,6 +421,7 @@ int main(int argc, char *argv[]) { wl_list_init(&state.outputs); wl_list_init(&state.pointers); + wl_list_init(&state.seats); state.display = wl_display_connect(NULL); if (state.display == NULL) { @@ -481,6 +498,10 @@ int main(int argc, char *argv[]) { wl_list_for_each_safe(output, output_tmp, &state.outputs, link) { destroy_output(output); } + struct slurp_seat *seat, *seat_tmp; + wl_list_for_each_safe(seat, seat_tmp, &state.seats, link) { + destroy_seat(seat); + } // Make sure the compositor has unmapped our surfaces by the time we exit wl_display_roundtrip(state.display); From d32660454153bd51b724199f269d8bb354be2a1e Mon Sep 17 00:00:00 2001 From: Ian Date: Thu, 16 Aug 2018 14:41:23 +0100 Subject: [PATCH 2/2] Remove destroyed output from list --- main.c | 1 + 1 file changed, 1 insertion(+) diff --git a/main.c b/main.c index b162e74..a3b3999 100644 --- a/main.c +++ b/main.c @@ -211,6 +211,7 @@ static void destroy_output(struct slurp_output *output) { if (output == NULL) { return; } + wl_list_remove(&output->link); finish_buffer(&output->buffers[0]); finish_buffer(&output->buffers[1]); wl_cursor_theme_destroy(output->cursor_theme);