From c8456086a164fd62c26fe3f3ade5f1e59d25eda0 Mon Sep 17 00:00:00 2001 From: Simon Zeni Date: Thu, 24 Feb 2022 11:00:11 -0500 Subject: [PATCH] backend/libinput: rework tablet interface The wlr_libinput_device owns its wlr_tablet and its associated wlr_tablet_tools --- backend/libinput/backend.c | 3 + backend/libinput/events.c | 33 ++-- backend/libinput/tablet_tool.c | 278 +++++++++++---------------------- include/backend/libinput.h | 15 +- 4 files changed, 116 insertions(+), 213 deletions(-) diff --git a/backend/libinput/backend.c b/backend/libinput/backend.c index 2e26a269..111ebf3e 100644 --- a/backend/libinput/backend.c +++ b/backend/libinput/backend.c @@ -252,6 +252,9 @@ struct libinput_device *wlr_libinput_get_device_handle( case WLR_INPUT_DEVICE_TOUCH: dev = device_from_touch(wlr_dev->touch); break; + case WLR_INPUT_DEVICE_TABLET_TOOL: + dev = device_from_tablet(wlr_dev->tablet); + break; default: dev = (struct wlr_libinput_input_device *)wlr_dev; break; diff --git a/backend/libinput/events.c b/backend/libinput/events.c index 14678dbd..76e7709c 100644 --- a/backend/libinput/events.c +++ b/backend/libinput/events.c @@ -49,6 +49,9 @@ void destroy_libinput_input_device(struct wlr_libinput_input_device *dev) if (dev->touch.impl) { wlr_touch_destroy(&dev->touch); } + if (dev->tablet.impl) { + wlr_tablet_destroy(&dev->tablet); + } } libinput_device_unref(dev->handle); @@ -162,6 +165,14 @@ static void handle_device_added(struct wlr_libinput_backend *backend, dev_used = true; } + if (libinput_device_has_capability(libinput_dev, + LIBINPUT_DEVICE_CAP_TABLET_TOOL)) { + init_device_tablet(dev); + wlr_signal_emit_safe(&backend->backend.events.new_input, + &dev->tablet.base); + dev_used = true; + } + if (dev_used) { wl_list_insert(&backend->devices, &dev->link); return; @@ -177,20 +188,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_TABLET_TOOL)) { - struct wlr_input_device *wlr_dev = allocate_device(backend, - libinput_dev, wlr_devices, WLR_INPUT_DEVICE_TABLET_TOOL); - if (!wlr_dev) { - goto fail; - } - wlr_dev->tablet = create_libinput_tablet(libinput_dev); - if (!wlr_dev->tablet) { - 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_PAD)) { struct wlr_input_device *wlr_dev = allocate_device(backend, @@ -316,16 +313,16 @@ void handle_libinput_event(struct wlr_libinput_backend *backend, handle_touch_frame(event, &dev->touch); break; case LIBINPUT_EVENT_TABLET_TOOL_AXIS: - handle_tablet_tool_axis(event, libinput_dev); + handle_tablet_tool_axis(event, &dev->tablet); break; case LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY: - handle_tablet_tool_proximity(event, libinput_dev); + handle_tablet_tool_proximity(event, &dev->tablet); break; case LIBINPUT_EVENT_TABLET_TOOL_TIP: - handle_tablet_tool_tip(event, libinput_dev); + handle_tablet_tool_tip(event, &dev->tablet); break; case LIBINPUT_EVENT_TABLET_TOOL_BUTTON: - handle_tablet_tool_button(event, libinput_dev); + handle_tablet_tool_button(event, &dev->tablet); break; case LIBINPUT_EVENT_TABLET_PAD_BUTTON: handle_tablet_pad_button(event, libinput_dev); diff --git a/backend/libinput/tablet_tool.c b/backend/libinput/tablet_tool.c index 373f87d7..af212218 100644 --- a/backend/libinput/tablet_tool.c +++ b/backend/libinput/tablet_tool.c @@ -1,92 +1,60 @@ -#ifndef _POSIX_C_SOURCE #define _POSIX_C_SOURCE 200809L -#endif #include #include #include #include -#include -#include #include -#include #include #include "backend/libinput.h" -#include "util/array.h" #include "util/signal.h" -static bool tablet_is_libinput(struct wlr_tablet *tablet) { - return tablet->impl == &libinput_tablet_impl; -} - -struct wlr_libinput_tablet_tool { +struct tablet_tool { struct wlr_tablet_tool wlr_tool; - - struct libinput_tablet_tool *libinput_tool; - - bool unique; - // Refcount for destroy + release - size_t pad_refs; + struct libinput_tablet_tool *handle; + struct wl_list link; // wlr_libinput_input_device::tablet_tools }; -struct wlr_libinput_tablet { - struct wlr_tablet wlr_tablet; - struct wl_array tools; // struct wlr_libinput_tablet_tool * -}; - -static void destroy_tool(struct wlr_libinput_tablet_tool *tool) { +static void tool_destroy(struct tablet_tool *tool) { wlr_signal_emit_safe(&tool->wlr_tool.events.destroy, &tool->wlr_tool); - libinput_tablet_tool_ref(tool->libinput_tool); - libinput_tablet_tool_set_user_data(tool->libinput_tool, NULL); + libinput_tablet_tool_unref(tool->handle); + libinput_tablet_tool_set_user_data(tool->handle, NULL); + wl_list_remove(&tool->link); free(tool); } -static void destroy_tablet(struct wlr_tablet *wlr_tablet) { - assert(tablet_is_libinput(wlr_tablet)); - struct wlr_libinput_tablet *tablet = - wl_container_of(wlr_tablet, tablet, wlr_tablet); - - struct wlr_libinput_tablet_tool **tool_ptr; - wl_array_for_each(tool_ptr, &tablet->tools) { - struct wlr_libinput_tablet_tool *tool = *tool_ptr; - if (--tool->pad_refs == 0) { - destroy_tool(tool); - } +static void tablet_destroy(struct wlr_tablet *wlr_tablet) { + struct wlr_libinput_input_device *dev = device_from_tablet(wlr_tablet); + struct tablet_tool *tool, *tmp; + wl_list_for_each_safe(tool, tmp, &dev->tablet_tools, link) { + tool_destroy(tool); } - wl_array_release(&tablet->tools); - - free(tablet); } const struct wlr_tablet_impl libinput_tablet_impl = { - .destroy = destroy_tablet, + .destroy = tablet_destroy, }; -struct wlr_tablet *create_libinput_tablet( - struct libinput_device *libinput_dev) { - assert(libinput_dev); - struct wlr_libinput_tablet *libinput_tablet = - calloc(1, sizeof(struct wlr_libinput_tablet)); - if (!libinput_tablet) { - wlr_log(WLR_ERROR, "Unable to allocate wlr_tablet_tool"); - return NULL; - } - - struct wlr_tablet *wlr_tablet = &libinput_tablet->wlr_tablet; - const char *name = libinput_device_get_name(libinput_dev); - +void init_device_tablet(struct wlr_libinput_input_device *dev) { + const char *name = libinput_device_get_name(dev->handle); + struct wlr_tablet *wlr_tablet = &dev->tablet; wlr_tablet_init(wlr_tablet, &libinput_tablet_impl, name); - wlr_tablet->base.vendor = libinput_device_get_id_vendor(libinput_dev); - wlr_tablet->base.product = libinput_device_get_id_product(libinput_dev); + wlr_tablet->base.vendor = libinput_device_get_id_vendor(dev->handle); + wlr_tablet->base.product = libinput_device_get_id_product(dev->handle); - struct udev_device *udev = libinput_device_get_udev_device(libinput_dev); + struct udev_device *udev = libinput_device_get_udev_device(dev->handle); char **dst = wl_array_add(&wlr_tablet->paths, sizeof(char *)); *dst = strdup(udev_device_get_syspath(udev)); - wlr_tablet->name = strdup(libinput_device_get_name(libinput_dev)); + wl_list_init(&dev->tablet_tools); +} - wl_array_init(&libinput_tablet->tools); +struct wlr_libinput_input_device *device_from_tablet( + struct wlr_tablet *wlr_tablet) { + assert(wlr_tablet->impl == &libinput_tablet_impl); - return wlr_tablet; + struct wlr_libinput_input_device *dev = + wl_container_of(wlr_tablet, dev, tablet); + return dev; } static enum wlr_tablet_tool_type wlr_type_from_libinput_type( @@ -112,87 +80,55 @@ static enum wlr_tablet_tool_type wlr_type_from_libinput_type( abort(); // unreachable } -static struct wlr_libinput_tablet_tool *get_wlr_tablet_tool( - struct libinput_tablet_tool *tool) { - struct wlr_libinput_tablet_tool *ret = - libinput_tablet_tool_get_user_data(tool); - - if (ret) { - return ret; +static struct tablet_tool *get_tablet_tool( + struct wlr_libinput_input_device *dev, + struct libinput_tablet_tool *libinput_tool) { + struct tablet_tool *tool = + libinput_tablet_tool_get_user_data(libinput_tool); + if (tool) { + return tool; } - ret = calloc(1, sizeof(struct wlr_libinput_tablet_tool)); - if (!ret) { + tool = calloc(1, sizeof(struct tablet_tool)); + if (tool == NULL) { + wlr_log_errno(WLR_ERROR, "failed to allocate wlr_libinput_tablet_tool"); return NULL; } - ret->libinput_tool = libinput_tablet_tool_ref(tool); - ret->wlr_tool.pressure = libinput_tablet_tool_has_pressure(tool); - ret->wlr_tool.distance = libinput_tablet_tool_has_distance(tool); - ret->wlr_tool.tilt = libinput_tablet_tool_has_tilt(tool); - ret->wlr_tool.rotation = libinput_tablet_tool_has_rotation(tool); - ret->wlr_tool.slider = libinput_tablet_tool_has_slider(tool); - ret->wlr_tool.wheel = libinput_tablet_tool_has_wheel(tool); + tool->wlr_tool.type = wlr_type_from_libinput_type( + libinput_tablet_tool_get_type(libinput_tool)); + tool->wlr_tool.hardware_serial = + libinput_tablet_tool_get_serial(libinput_tool); + tool->wlr_tool.hardware_wacom = + libinput_tablet_tool_get_tool_id(libinput_tool); - ret->wlr_tool.hardware_serial = libinput_tablet_tool_get_serial(tool); - ret->wlr_tool.hardware_wacom = libinput_tablet_tool_get_tool_id(tool); - ret->wlr_tool.type = wlr_type_from_libinput_type( - libinput_tablet_tool_get_type(tool)); + tool->wlr_tool.pressure = libinput_tablet_tool_has_pressure(libinput_tool); + tool->wlr_tool.distance = libinput_tablet_tool_has_distance(libinput_tool); + tool->wlr_tool.tilt = libinput_tablet_tool_has_tilt(libinput_tool); + tool->wlr_tool.rotation = libinput_tablet_tool_has_rotation(libinput_tool); + tool->wlr_tool.slider = libinput_tablet_tool_has_slider(libinput_tool); + tool->wlr_tool.wheel = libinput_tablet_tool_has_wheel(libinput_tool); - ret->unique = libinput_tablet_tool_is_unique(tool); + wl_signal_init(&tool->wlr_tool.events.destroy); - wl_signal_init(&ret->wlr_tool.events.destroy); + tool->handle = libinput_tablet_tool_ref(libinput_tool); + libinput_tablet_tool_set_user_data(libinput_tool, tool); - libinput_tablet_tool_set_user_data(tool, ret); - return ret; -} - -static void ensure_tool_reference(struct wlr_libinput_tablet_tool *tool, - struct wlr_tablet *wlr_dev) { - assert(tablet_is_libinput(wlr_dev)); - struct wlr_libinput_tablet *tablet = - wl_container_of(wlr_dev, tablet, wlr_tablet); - - struct wlr_libinput_tablet_tool **tool_ptr; - wl_array_for_each(tool_ptr, &tablet->tools) { - if (*tool_ptr == tool) { // We already have a ref - // XXX: We *could* optimize the tool to the front of - // the list here, since we will probably get the next - // couple of events from the same tool. - // BUT the list should always be rather short (probably - // single digit amount of tools) so it might be more - // work than it saves - return; - } - } - - struct wlr_libinput_tablet_tool **dst = - wl_array_add(&tablet->tools, sizeof(tool)); - if (!dst) { - wlr_log(WLR_ERROR, "Failed to allocate memory for tracking tablet tool"); - return; - } - *dst = tool; - ++tool->pad_refs; + wl_list_insert(&dev->tablet_tools, &tool->link); + return tool; } void handle_tablet_tool_axis(struct libinput_event *event, - struct libinput_device *libinput_dev) { - struct wlr_input_device *wlr_dev = - get_appropriate_device(WLR_INPUT_DEVICE_TABLET_TOOL, libinput_dev); - if (!wlr_dev) { - wlr_log(WLR_DEBUG, - "Got a tablet tool event for a device with no tablet tools?"); - return; - } + struct wlr_tablet *wlr_tablet) { struct libinput_event_tablet_tool *tevent = libinput_event_get_tablet_tool_event(event); - struct wlr_event_tablet_tool_axis wlr_event = { 0 }; - struct wlr_libinput_tablet_tool *tool = get_wlr_tablet_tool( - libinput_event_tablet_tool_get_tool(tevent)); - ensure_tool_reference(tool, wlr_dev->tablet); + struct wlr_libinput_input_device *dev = device_from_tablet(wlr_tablet); + struct tablet_tool *tool = + get_tablet_tool(dev, libinput_event_tablet_tool_get_tool(tevent)); - wlr_event.device = wlr_dev; + struct wlr_event_tablet_tool_axis wlr_event = { 0 }; + + wlr_event.device = &wlr_tablet->base; wlr_event.tool = &tool->wlr_tool; wlr_event.time_msec = usec_to_msec(libinput_event_tablet_tool_get_time_usec(tevent)); @@ -234,27 +170,20 @@ void handle_tablet_tool_axis(struct libinput_event *event, wlr_event.updated_axes |= WLR_TABLET_TOOL_AXIS_WHEEL; wlr_event.wheel_delta = libinput_event_tablet_tool_get_wheel_delta(tevent); } - wlr_signal_emit_safe(&wlr_dev->tablet->events.axis, &wlr_event); + wlr_signal_emit_safe(&wlr_tablet->events.axis, &wlr_event); } void handle_tablet_tool_proximity(struct libinput_event *event, - struct libinput_device *libinput_dev) { - struct wlr_input_device *wlr_dev = - get_appropriate_device(WLR_INPUT_DEVICE_TABLET_TOOL, libinput_dev); - if (!wlr_dev) { - wlr_log(WLR_DEBUG, - "Got a tablet tool event for a device with no tablet tools?"); - return; - } + struct wlr_tablet *wlr_tablet) { struct libinput_event_tablet_tool *tevent = libinput_event_get_tablet_tool_event(event); - struct wlr_event_tablet_tool_proximity wlr_event = { 0 }; - struct wlr_libinput_tablet_tool *tool = get_wlr_tablet_tool( - libinput_event_tablet_tool_get_tool(tevent)); - ensure_tool_reference(tool, wlr_dev->tablet); + struct wlr_libinput_input_device *dev = device_from_tablet(wlr_tablet); + struct tablet_tool *tool = + get_tablet_tool(dev, libinput_event_tablet_tool_get_tool(tevent)); + struct wlr_event_tablet_tool_proximity wlr_event = { 0 }; wlr_event.tool = &tool->wlr_tool; - wlr_event.device = wlr_dev; + wlr_event.device = &wlr_tablet->base; wlr_event.time_msec = usec_to_msec(libinput_event_tablet_tool_get_time_usec(tevent)); wlr_event.x = libinput_event_tablet_tool_get_x_transformed(tevent, 1); @@ -268,56 +197,34 @@ void handle_tablet_tool_proximity(struct libinput_event *event, wlr_event.state = WLR_TABLET_TOOL_PROXIMITY_IN; break; } - wlr_signal_emit_safe(&wlr_dev->tablet->events.proximity, &wlr_event); + wlr_signal_emit_safe(&wlr_tablet->events.proximity, &wlr_event); if (libinput_event_tablet_tool_get_proximity_state(tevent) == LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_IN) { - handle_tablet_tool_axis(event, libinput_dev); + handle_tablet_tool_axis(event, wlr_tablet); } // If the tool is not unique, libinput will not find it again after the // proximity out, so we should destroy it - if (!tool->unique && - libinput_event_tablet_tool_get_proximity_state(tevent) == + if (!libinput_tablet_tool_is_unique(tool->handle) + && libinput_event_tablet_tool_get_proximity_state(tevent) == LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_OUT) { // The tool isn't unique, it can't be on multiple tablets - assert(tool->pad_refs == 1); - assert(tablet_is_libinput(wlr_dev->tablet)); - struct wlr_libinput_tablet *tablet = - wl_container_of(wlr_dev->tablet, tablet, wlr_tablet); - - size_t i = 0; - struct wlr_libinput_tablet_tool **tool_ptr; - wl_array_for_each(tool_ptr, &tablet->tools) { - if (*tool_ptr == tool) { - array_remove_at(&tablet->tools, i * sizeof(tool), sizeof(tool)); - break; - } - i++; - } - - destroy_tool(tool); + tool_destroy(tool); } } void handle_tablet_tool_tip(struct libinput_event *event, - struct libinput_device *libinput_dev) { - struct wlr_input_device *wlr_dev = - get_appropriate_device(WLR_INPUT_DEVICE_TABLET_TOOL, libinput_dev); - if (!wlr_dev) { - wlr_log(WLR_DEBUG, - "Got a tablet tool event for a device with no tablet tools?"); - return; - } - handle_tablet_tool_axis(event, libinput_dev); + struct wlr_tablet *wlr_tablet) { + handle_tablet_tool_axis(event, wlr_tablet); struct libinput_event_tablet_tool *tevent = libinput_event_get_tablet_tool_event(event); - struct wlr_event_tablet_tool_tip wlr_event = { 0 }; - struct wlr_libinput_tablet_tool *tool = get_wlr_tablet_tool( - libinput_event_tablet_tool_get_tool(tevent)); - ensure_tool_reference(tool, wlr_dev->tablet); + struct wlr_libinput_input_device *dev = device_from_tablet(wlr_tablet); + struct tablet_tool *tool = + get_tablet_tool(dev, libinput_event_tablet_tool_get_tool(tevent)); - wlr_event.device = wlr_dev; + struct wlr_event_tablet_tool_tip wlr_event = { 0 }; + wlr_event.device = &wlr_tablet->base; wlr_event.tool = &tool->wlr_tool; wlr_event.time_msec = usec_to_msec(libinput_event_tablet_tool_get_time_usec(tevent)); @@ -332,27 +239,20 @@ void handle_tablet_tool_tip(struct libinput_event *event, wlr_event.state = WLR_TABLET_TOOL_TIP_DOWN; break; } - wlr_signal_emit_safe(&wlr_dev->tablet->events.tip, &wlr_event); + wlr_signal_emit_safe(&wlr_tablet->events.tip, &wlr_event); } void handle_tablet_tool_button(struct libinput_event *event, - struct libinput_device *libinput_dev) { - struct wlr_input_device *wlr_dev = - get_appropriate_device(WLR_INPUT_DEVICE_TABLET_TOOL, libinput_dev); - if (!wlr_dev) { - wlr_log(WLR_DEBUG, - "Got a tablet tool event for a device with no tablet tools?"); - return; - } - handle_tablet_tool_axis(event, libinput_dev); + struct wlr_tablet *wlr_tablet) { + handle_tablet_tool_axis(event, wlr_tablet); struct libinput_event_tablet_tool *tevent = libinput_event_get_tablet_tool_event(event); - struct wlr_event_tablet_tool_button wlr_event = { 0 }; - struct wlr_libinput_tablet_tool *tool = get_wlr_tablet_tool( - libinput_event_tablet_tool_get_tool(tevent)); - ensure_tool_reference(tool, wlr_dev->tablet); + struct wlr_libinput_input_device *dev = device_from_tablet(wlr_tablet); + struct tablet_tool *tool = + get_tablet_tool(dev, libinput_event_tablet_tool_get_tool(tevent)); - wlr_event.device = wlr_dev; + struct wlr_event_tablet_tool_button wlr_event = { 0 }; + wlr_event.device = &wlr_tablet->base; wlr_event.tool = &tool->wlr_tool; wlr_event.time_msec = usec_to_msec(libinput_event_tablet_tool_get_time_usec(tevent)); @@ -365,5 +265,5 @@ void handle_tablet_tool_button(struct libinput_event *event, wlr_event.state = WLR_BUTTON_PRESSED; break; } - wlr_signal_emit_safe(&wlr_dev->tablet->events.button, &wlr_event); + wlr_signal_emit_safe(&wlr_tablet->events.button, &wlr_event); } diff --git a/include/backend/libinput.h b/include/backend/libinput.h index c8719af3..808da101 100644 --- a/include/backend/libinput.h +++ b/include/backend/libinput.h @@ -38,6 +38,8 @@ struct wlr_libinput_input_device { struct wlr_pointer pointer; struct wlr_switch switch_device; struct wlr_touch touch; + struct wlr_tablet tablet; + struct wl_list tablet_tools; // see backend/libinput/tablet_tool.c struct wl_list link; }; @@ -111,16 +113,17 @@ void handle_touch_cancel(struct libinput_event *event, void handle_touch_frame(struct libinput_event *event, struct wlr_touch *touch); -struct wlr_tablet *create_libinput_tablet( - struct libinput_device *device); +void init_device_tablet(struct wlr_libinput_input_device *dev); +struct wlr_libinput_input_device *device_from_tablet( + struct wlr_tablet *tablet); void handle_tablet_tool_axis(struct libinput_event *event, - struct libinput_device *device); + struct wlr_tablet *tablet); void handle_tablet_tool_proximity(struct libinput_event *event, - struct libinput_device *device); + struct wlr_tablet *tablet); void handle_tablet_tool_tip(struct libinput_event *event, - struct libinput_device *device); + struct wlr_tablet *tablet); void handle_tablet_tool_button(struct libinput_event *event, - struct libinput_device *device); + struct wlr_tablet *tablet); struct wlr_tablet_pad *create_libinput_tablet_pad( struct libinput_device *device);