Skip to content

Commit

Permalink
Track seats so that they are properly freed on exit
Browse files Browse the repository at this point in the history
  • Loading branch information
ianyfan committed Aug 16, 2018
1 parent 57bae56 commit 8625fe4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
6 changes: 6 additions & 0 deletions include/slurp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
25 changes: 23 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 8625fe4

Please sign in to comment.