From 5c845f14ed72bcdbd45a2e719223c441846f62ad Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Mon, 28 Aug 2017 10:29:53 -0400 Subject: [PATCH] handle input device destroy --- examples/pointer.c | 15 +++++++++++++-- examples/shared.c | 5 +++++ examples/shared.h | 2 ++ include/wlr/types/wlr_input_device.h | 4 ++++ types/wlr_cursor.c | 11 +++++++++++ types/wlr_input_device.c | 4 ++++ 6 files changed, 39 insertions(+), 2 deletions(-) diff --git a/examples/pointer.c b/examples/pointer.c index 3863c0e8..8ad18442 100644 --- a/examples/pointer.c +++ b/examples/pointer.c @@ -188,6 +188,18 @@ static void handle_input_add(struct compositor_state *state, struct } } +static void handle_input_remove(struct compositor_state *state, struct + wlr_input_device *device) { + struct sample_state *sample = state->data; + struct sample_input_device *s_device, *tmp = NULL; + wl_list_for_each_safe(s_device, tmp, &sample->devices, link) { + if (s_device->device == device) { + wl_list_remove(&s_device->link); + free(s_device); + } + } +} + static void handle_cursor_motion(struct wl_listener *listener, void *data) { struct sample_state *sample = wl_container_of(listener, sample, cursor_motion); struct wlr_event_pointer_motion *event = data; @@ -339,8 +351,7 @@ int main(int argc, char *argv[]) { compositor.output_resolution_cb = handle_output_resolution; compositor.output_frame_cb = handle_output_frame; compositor.input_add_cb = handle_input_add; - // TODO input_remove_cb - //compositor.input_remove_cb = handle_input_add; + compositor.input_remove_cb = handle_input_remove; state.compositor = &compositor; diff --git a/examples/shared.c b/examples/shared.c index f9d687e3..0346c96d 100644 --- a/examples/shared.c +++ b/examples/shared.c @@ -401,6 +401,11 @@ static void tablet_pad_remove(struct wlr_input_device *device, struct compositor static void input_remove_notify(struct wl_listener *listener, void *data) { struct wlr_input_device *device = data; struct compositor_state *state = wl_container_of(listener, state, input_remove); + + if (state->input_remove_cb) { + state->input_remove_cb(state, device); + } + switch (device->type) { case WLR_INPUT_DEVICE_KEYBOARD: keyboard_remove(device, state); diff --git a/examples/shared.h b/examples/shared.h index 06108494..7cf4db63 100644 --- a/examples/shared.h +++ b/examples/shared.h @@ -77,6 +77,8 @@ struct tablet_pad_state { struct compositor_state { void (*input_add_cb)(struct compositor_state *compositor, struct wlr_input_device *device); + void (*input_remove_cb)(struct compositor_state *compositor, + struct wlr_input_device *device); void (*output_add_cb)(struct output_state *s); void (*keyboard_add_cb)(struct keyboard_state *s); void (*output_frame_cb)(struct output_state *s, struct timespec *ts); diff --git a/include/wlr/types/wlr_input_device.h b/include/wlr/types/wlr_input_device.h index 642892ff..5a41ce9d 100644 --- a/include/wlr/types/wlr_input_device.h +++ b/include/wlr/types/wlr_input_device.h @@ -40,6 +40,10 @@ struct wlr_input_device { struct wlr_tablet_pad *tablet_pad; }; + struct { + struct wl_signal destroy; + } events; + void *data; }; diff --git a/types/wlr_cursor.c b/types/wlr_cursor.c index c62fba9b..ff19bb2b 100644 --- a/types/wlr_cursor.c +++ b/types/wlr_cursor.c @@ -29,6 +29,8 @@ struct wlr_cursor_device { struct wl_listener tablet_tool_proximity; struct wl_listener tablet_tool_tip; struct wl_listener tablet_tool_button; + + struct wl_listener destroy; }; struct wlr_cursor_state { @@ -318,6 +320,12 @@ static void handle_tablet_tool_proximity(struct wl_listener *listener, void *dat wl_signal_emit(&device->cursor->events.tablet_tool_proximity, event); } +static void handle_device_destroy(struct wl_listener *listener, void *data) { + struct wlr_cursor_device *c_device; + c_device = wl_container_of(listener, c_device, destroy); + wlr_cursor_detach_input_device(c_device->cursor, c_device->device); +} + void wlr_cursor_attach_input_device(struct wlr_cursor *cur, struct wlr_input_device *dev) { if (dev->type != WLR_INPUT_DEVICE_POINTER && @@ -348,6 +356,9 @@ void wlr_cursor_attach_input_device(struct wlr_cursor *cur, // listen to events + wl_signal_add(&dev->events.destroy, &device->destroy); + device->destroy.notify = handle_device_destroy; + if (dev->type == WLR_INPUT_DEVICE_POINTER) { wl_signal_add(&dev->pointer->events.motion, &device->motion); device->motion.notify = handle_pointer_motion; diff --git a/types/wlr_input_device.c b/types/wlr_input_device.c index 409e8bd1..002c2b54 100644 --- a/types/wlr_input_device.c +++ b/types/wlr_input_device.c @@ -20,12 +20,16 @@ void wlr_input_device_init(struct wlr_input_device *dev, dev->name = strdup(name); dev->vendor = vendor; dev->product = product; + + wl_signal_init(&dev->events.destroy); } void wlr_input_device_destroy(struct wlr_input_device *dev) { if (!dev) { return; } + + wl_signal_emit(&dev->events.destroy, dev); if (dev->_device) { switch (dev->type) {