From 4f4dd9522378f4d62a47ee9b1472378c9960b2f2 Mon Sep 17 00:00:00 2001 From: Simon Zeni Date: Wed, 23 Feb 2022 14:06:02 -0500 Subject: [PATCH] backend/libinput: rework touch interface The wlr_libinput_input_device now owns its wlr_touch, instead of creating a new wlr_libinput_input_device for it. --- backend/libinput/backend.c | 3 ++ backend/libinput/events.c | 35 +++++++------- backend/libinput/touch.c | 96 ++++++++++++++------------------------ include/backend/libinput.h | 16 ++++--- 4 files changed, 63 insertions(+), 87 deletions(-) diff --git a/backend/libinput/backend.c b/backend/libinput/backend.c index 92b5effa..2e26a269 100644 --- a/backend/libinput/backend.c +++ b/backend/libinput/backend.c @@ -249,6 +249,9 @@ struct libinput_device *wlr_libinput_get_device_handle( case WLR_INPUT_DEVICE_SWITCH: dev = device_from_switch(wlr_dev->switch_device); break; + case WLR_INPUT_DEVICE_TOUCH: + dev = device_from_touch(wlr_dev->touch); + break; default: dev = (struct wlr_libinput_input_device *)wlr_dev; break; diff --git a/backend/libinput/events.c b/backend/libinput/events.c index ad346c4f..14678dbd 100644 --- a/backend/libinput/events.c +++ b/backend/libinput/events.c @@ -46,6 +46,9 @@ void destroy_libinput_input_device(struct wlr_libinput_input_device *dev) if (dev->switch_device.impl) { wlr_switch_destroy(&dev->switch_device); } + if (dev->touch.impl) { + wlr_touch_destroy(&dev->touch); + } } libinput_device_unref(dev->handle); @@ -151,6 +154,14 @@ static void handle_device_added(struct wlr_libinput_backend *backend, dev_used = true; } + if (libinput_device_has_capability( + libinput_dev, LIBINPUT_DEVICE_CAP_TOUCH)) { + init_device_touch(dev); + wlr_signal_emit_safe(&backend->backend.events.new_input, + &dev->touch.base); + dev_used = true; + } + if (dev_used) { wl_list_insert(&backend->devices, &dev->link); return; @@ -166,20 +177,6 @@ static void handle_device_added(struct wlr_libinput_backend *backend, } wl_list_init(wlr_devices); - if (libinput_device_has_capability( - libinput_dev, LIBINPUT_DEVICE_CAP_TOUCH)) { - struct wlr_input_device *wlr_dev = allocate_device(backend, - libinput_dev, wlr_devices, WLR_INPUT_DEVICE_TOUCH); - if (!wlr_dev) { - goto fail; - } - wlr_dev->touch = create_libinput_touch(libinput_dev); - if (!wlr_dev->touch) { - free(wlr_dev); - goto fail; - } - wlr_signal_emit_safe(&backend->backend.events.new_input, wlr_dev); - } if (libinput_device_has_capability(libinput_dev, LIBINPUT_DEVICE_CAP_TABLET_TOOL)) { struct wlr_input_device *wlr_dev = allocate_device(backend, @@ -304,19 +301,19 @@ void handle_libinput_event(struct wlr_libinput_backend *backend, handle_pointer_axis(event, &dev->pointer); break; case LIBINPUT_EVENT_TOUCH_DOWN: - handle_touch_down(event, libinput_dev); + handle_touch_down(event, &dev->touch); break; case LIBINPUT_EVENT_TOUCH_UP: - handle_touch_up(event, libinput_dev); + handle_touch_up(event, &dev->touch); break; case LIBINPUT_EVENT_TOUCH_MOTION: - handle_touch_motion(event, libinput_dev); + handle_touch_motion(event, &dev->touch); break; case LIBINPUT_EVENT_TOUCH_CANCEL: - handle_touch_cancel(event, libinput_dev); + handle_touch_cancel(event, &dev->touch); break; case LIBINPUT_EVENT_TOUCH_FRAME: - handle_touch_frame(event, libinput_dev); + handle_touch_frame(event, &dev->touch); break; case LIBINPUT_EVENT_TABLET_TOOL_AXIS: handle_tablet_tool_axis(event, libinput_dev); diff --git a/backend/libinput/touch.c b/backend/libinput/touch.c index 2e22a97a..5b4dcbd6 100644 --- a/backend/libinput/touch.c +++ b/backend/libinput/touch.c @@ -1,113 +1,87 @@ #include #include -#include -#include #include -#include -#include #include "backend/libinput.h" #include "util/signal.h" -const struct wlr_touch_impl libinput_touch_impl; +static void touch_destroy(struct wlr_touch *touch) { + /* wlr_touch belongs to the wlr_libinput_input_device */ +} -struct wlr_touch *create_libinput_touch( - struct libinput_device *libinput_dev) { - assert(libinput_dev); - struct wlr_touch *wlr_touch = calloc(1, sizeof(struct wlr_touch)); - if (!wlr_touch) { - wlr_log(WLR_ERROR, "Unable to allocate wlr_touch"); - return NULL; - } - const char *name = libinput_device_get_name(libinput_dev); +const struct wlr_touch_impl libinput_touch_impl = { + .destroy = touch_destroy, +}; + +void init_device_touch(struct wlr_libinput_input_device *dev) { + const char *name = libinput_device_get_name(dev->handle); + struct wlr_touch *wlr_touch = &dev->touch; wlr_touch_init(wlr_touch, &libinput_touch_impl, name); - wlr_touch->base.vendor = libinput_device_get_id_vendor(libinput_dev); - wlr_touch->base.product = libinput_device_get_id_product(libinput_dev); - return wlr_touch; + wlr_touch->base.vendor = libinput_device_get_id_vendor(dev->handle); + wlr_touch->base.product = libinput_device_get_id_product(dev->handle); +} + +struct wlr_libinput_input_device *device_from_touch( + struct wlr_touch *wlr_touch) { + assert(wlr_touch->impl == &libinput_touch_impl); + + struct wlr_libinput_input_device *dev = + wl_container_of(wlr_touch, dev, touch); + return dev; } void handle_touch_down(struct libinput_event *event, - struct libinput_device *libinput_dev) { - struct wlr_input_device *wlr_dev = - get_appropriate_device(WLR_INPUT_DEVICE_TOUCH, libinput_dev); - if (!wlr_dev) { - wlr_log(WLR_DEBUG, "Got a touch event for a device with no touch?"); - return; - } + struct wlr_touch *touch) { struct libinput_event_touch *tevent = libinput_event_get_touch_event(event); struct wlr_event_touch_down wlr_event = { 0 }; - wlr_event.device = wlr_dev; + wlr_event.device = &touch->base; wlr_event.time_msec = usec_to_msec(libinput_event_touch_get_time_usec(tevent)); wlr_event.touch_id = libinput_event_touch_get_seat_slot(tevent); wlr_event.x = libinput_event_touch_get_x_transformed(tevent, 1); wlr_event.y = libinput_event_touch_get_y_transformed(tevent, 1); - wlr_signal_emit_safe(&wlr_dev->touch->events.down, &wlr_event); + wlr_signal_emit_safe(&touch->events.down, &wlr_event); } void handle_touch_up(struct libinput_event *event, - struct libinput_device *libinput_dev) { - struct wlr_input_device *wlr_dev = - get_appropriate_device(WLR_INPUT_DEVICE_TOUCH, libinput_dev); - if (!wlr_dev) { - wlr_log(WLR_DEBUG, "Got a touch event for a device with no touch?"); - return; - } + struct wlr_touch *touch) { struct libinput_event_touch *tevent = libinput_event_get_touch_event(event); struct wlr_event_touch_up wlr_event = { 0 }; - wlr_event.device = wlr_dev; + wlr_event.device = &touch->base; wlr_event.time_msec = usec_to_msec(libinput_event_touch_get_time_usec(tevent)); wlr_event.touch_id = libinput_event_touch_get_seat_slot(tevent); - wlr_signal_emit_safe(&wlr_dev->touch->events.up, &wlr_event); + wlr_signal_emit_safe(&touch->events.up, &wlr_event); } void handle_touch_motion(struct libinput_event *event, - struct libinput_device *libinput_dev) { - struct wlr_input_device *wlr_dev = - get_appropriate_device(WLR_INPUT_DEVICE_TOUCH, libinput_dev); - if (!wlr_dev) { - wlr_log(WLR_DEBUG, "Got a touch event for a device with no touch?"); - return; - } + struct wlr_touch *touch) { struct libinput_event_touch *tevent = libinput_event_get_touch_event(event); struct wlr_event_touch_motion wlr_event = { 0 }; - wlr_event.device = wlr_dev; + wlr_event.device = &touch->base; wlr_event.time_msec = usec_to_msec(libinput_event_touch_get_time_usec(tevent)); wlr_event.touch_id = libinput_event_touch_get_seat_slot(tevent); wlr_event.x = libinput_event_touch_get_x_transformed(tevent, 1); wlr_event.y = libinput_event_touch_get_y_transformed(tevent, 1); - wlr_signal_emit_safe(&wlr_dev->touch->events.motion, &wlr_event); + wlr_signal_emit_safe(&touch->events.motion, &wlr_event); } void handle_touch_cancel(struct libinput_event *event, - struct libinput_device *libinput_dev) { - struct wlr_input_device *wlr_dev = - get_appropriate_device(WLR_INPUT_DEVICE_TOUCH, libinput_dev); - if (!wlr_dev) { - wlr_log(WLR_DEBUG, "Got a touch event for a device with no touch?"); - return; - } + struct wlr_touch *touch) { struct libinput_event_touch *tevent = libinput_event_get_touch_event(event); struct wlr_event_touch_cancel wlr_event = { 0 }; - wlr_event.device = wlr_dev; + wlr_event.device = &touch->base; wlr_event.time_msec = usec_to_msec(libinput_event_touch_get_time_usec(tevent)); wlr_event.touch_id = libinput_event_touch_get_seat_slot(tevent); - wlr_signal_emit_safe(&wlr_dev->touch->events.cancel, &wlr_event); + wlr_signal_emit_safe(&touch->events.cancel, &wlr_event); } void handle_touch_frame(struct libinput_event *event, - struct libinput_device *libinput_dev) { - struct wlr_input_device *wlr_dev = - get_appropriate_device(WLR_INPUT_DEVICE_TOUCH, libinput_dev); - if (!wlr_dev) { - wlr_log(WLR_DEBUG, "Got a touch event for a device with no touch?"); - return; - } - wlr_signal_emit_safe(&wlr_dev->touch->events.frame, NULL); + struct wlr_touch *touch) { + wlr_signal_emit_safe(&touch->events.frame, NULL); } diff --git a/include/backend/libinput.h b/include/backend/libinput.h index 69b30f26..c8719af3 100644 --- a/include/backend/libinput.h +++ b/include/backend/libinput.h @@ -37,6 +37,7 @@ struct wlr_libinput_input_device { struct wlr_keyboard keyboard; struct wlr_pointer pointer; struct wlr_switch switch_device; + struct wlr_touch touch; struct wl_list link; }; @@ -96,18 +97,19 @@ struct wlr_libinput_input_device *device_from_switch( void handle_switch_toggle(struct libinput_event *event, struct wlr_switch *switch_device); -struct wlr_touch *create_libinput_touch( - struct libinput_device *device); +void init_device_touch(struct wlr_libinput_input_device *dev); +struct wlr_libinput_input_device *device_from_touch( + struct wlr_touch *touch); void handle_touch_down(struct libinput_event *event, - struct libinput_device *device); + struct wlr_touch *touch); void handle_touch_up(struct libinput_event *event, - struct libinput_device *device); + struct wlr_touch *touch); void handle_touch_motion(struct libinput_event *event, - struct libinput_device *device); + struct wlr_touch *touch); void handle_touch_cancel(struct libinput_event *event, - struct libinput_device *device); + struct wlr_touch *touch); void handle_touch_frame(struct libinput_event *event, - struct libinput_device *device); + struct wlr_touch *touch); struct wlr_tablet *create_libinput_tablet( struct libinput_device *device);