From d750c5ac676850d93ba7abf0a08476c4535ff1b6 Mon Sep 17 00:00:00 2001 From: Simon Zeni Date: Wed, 23 Feb 2022 13:41:34 -0500 Subject: [PATCH] backend/libinput: rework switch interface The wlr_libinput_input_device now owns its wlr_switch, instead of creating a new wlr_libinput_input_device for it. --- backend/libinput/backend.c | 3 +++ backend/libinput/events.c | 27 +++++++++----------- backend/libinput/switch.c | 51 ++++++++++++++++++-------------------- include/backend/libinput.h | 8 +++--- 4 files changed, 44 insertions(+), 45 deletions(-) diff --git a/backend/libinput/backend.c b/backend/libinput/backend.c index e22c4bfa..92b5effa 100644 --- a/backend/libinput/backend.c +++ b/backend/libinput/backend.c @@ -246,6 +246,9 @@ struct libinput_device *wlr_libinput_get_device_handle( case WLR_INPUT_DEVICE_POINTER: dev = device_from_pointer(wlr_dev->pointer); break; + case WLR_INPUT_DEVICE_SWITCH: + dev = device_from_switch(wlr_dev->switch_device); + break; default: dev = (struct wlr_libinput_input_device *)wlr_dev; break; diff --git a/backend/libinput/events.c b/backend/libinput/events.c index a0563ecd..ad346c4f 100644 --- a/backend/libinput/events.c +++ b/backend/libinput/events.c @@ -43,6 +43,9 @@ void destroy_libinput_input_device(struct wlr_libinput_input_device *dev) if (dev->pointer.impl) { wlr_pointer_destroy(&dev->pointer); } + if (dev->switch_device.impl) { + wlr_switch_destroy(&dev->switch_device); + } } libinput_device_unref(dev->handle); @@ -140,6 +143,14 @@ static void handle_device_added(struct wlr_libinput_backend *backend, dev_used = true; } + if (libinput_device_has_capability( + libinput_dev, LIBINPUT_DEVICE_CAP_SWITCH)) { + init_device_switch(dev); + wlr_signal_emit_safe(&backend->backend.events.new_input, + &dev->switch_device.base); + dev_used = true; + } + if (dev_used) { wl_list_insert(&backend->devices, &dev->link); return; @@ -201,20 +212,6 @@ static void handle_device_added(struct wlr_libinput_backend *backend, libinput_dev, LIBINPUT_DEVICE_CAP_GESTURE)) { // TODO } - if (libinput_device_has_capability( - libinput_dev, LIBINPUT_DEVICE_CAP_SWITCH)) { - struct wlr_input_device *wlr_dev = allocate_device(backend, - libinput_dev, wlr_devices, WLR_INPUT_DEVICE_SWITCH); - if (!wlr_dev) { - goto fail; - } - wlr_dev->switch_device = create_libinput_switch(libinput_dev); - if (!wlr_dev->switch_device) { - free(wlr_dev); - goto fail; - } - wlr_signal_emit_safe(&backend->backend.events.new_input, wlr_dev); - } if (!wl_list_empty(wlr_devices)) { struct wl_list **dst = wl_array_add(&backend->wlr_device_lists, sizeof(wlr_devices)); @@ -343,7 +340,7 @@ void handle_libinput_event(struct wlr_libinput_backend *backend, handle_tablet_pad_strip(event, libinput_dev); break; case LIBINPUT_EVENT_SWITCH_TOGGLE: - handle_switch_toggle(event, libinput_dev); + handle_switch_toggle(event, &dev->switch_device); break; case LIBINPUT_EVENT_GESTURE_SWIPE_BEGIN: handle_pointer_swipe_begin(event, &dev->pointer); diff --git a/backend/libinput/switch.c b/backend/libinput/switch.c index 0e710f09..f2d5409b 100644 --- a/backend/libinput/switch.c +++ b/backend/libinput/switch.c @@ -1,43 +1,40 @@ #include #include -#include -#include #include -#include -#include #include "backend/libinput.h" #include "util/signal.h" -const struct wlr_switch_impl libinput_switch_impl; +static void switch_destroy(struct wlr_switch *wlr_switch) { + /* wlr_switch belongs to the wlr_libinput_input_device */ +} -struct wlr_switch *create_libinput_switch( - struct libinput_device *libinput_dev) { - assert(libinput_dev); - struct wlr_switch *wlr_switch = calloc(1, sizeof(struct wlr_switch)); - if (!wlr_switch) { - wlr_log(WLR_ERROR, "Unable to allocate wlr_switch"); - return NULL; - } - const char *name = libinput_device_get_name(libinput_dev); +const struct wlr_switch_impl libinput_switch_impl = { + .destroy = switch_destroy, +}; + +void init_device_switch(struct wlr_libinput_input_device *dev) { + const char *name = libinput_device_get_name(dev->handle); + struct wlr_switch *wlr_switch = &dev->switch_device; wlr_switch_init(wlr_switch, &libinput_switch_impl, name); - wlr_log(WLR_DEBUG, "Created switch for device %s", name); - wlr_switch->base.vendor = libinput_device_get_id_vendor(libinput_dev); - wlr_switch->base.product = libinput_device_get_id_product(libinput_dev); - return wlr_switch; + wlr_switch->base.vendor = libinput_device_get_id_vendor(dev->handle); + wlr_switch->base.product = libinput_device_get_id_product(dev->handle); +} + +struct wlr_libinput_input_device *device_from_switch( + struct wlr_switch *wlr_switch) { + assert(wlr_switch->impl == &libinput_switch_impl); + + struct wlr_libinput_input_device *dev = + wl_container_of(wlr_switch, dev, switch_device); + return dev; } void handle_switch_toggle(struct libinput_event *event, - struct libinput_device *libinput_dev) { - struct wlr_input_device *wlr_dev = - get_appropriate_device(WLR_INPUT_DEVICE_SWITCH, libinput_dev); - if (!wlr_dev) { - wlr_log(WLR_DEBUG, "Got a switch event for a device with no switch?"); - return; - } + struct wlr_switch *wlr_switch) { struct libinput_event_switch *sevent = libinput_event_get_switch_event (event); struct wlr_event_switch_toggle wlr_event = { 0 }; - wlr_event.device = wlr_dev; + wlr_event.device = &wlr_switch->base; switch (libinput_event_switch_get_switch(sevent)) { case LIBINPUT_SWITCH_LID: wlr_event.switch_type = WLR_SWITCH_TYPE_LID; @@ -56,5 +53,5 @@ void handle_switch_toggle(struct libinput_event *event, } wlr_event.time_msec = usec_to_msec(libinput_event_switch_get_time_usec(sevent)); - wlr_signal_emit_safe(&wlr_dev->switch_device->events.toggle, &wlr_event); + wlr_signal_emit_safe(&wlr_switch->events.toggle, &wlr_event); } diff --git a/include/backend/libinput.h b/include/backend/libinput.h index bcb36a71..69b30f26 100644 --- a/include/backend/libinput.h +++ b/include/backend/libinput.h @@ -36,6 +36,7 @@ struct wlr_libinput_input_device { struct wlr_keyboard keyboard; struct wlr_pointer pointer; + struct wlr_switch switch_device; struct wl_list link; }; @@ -89,10 +90,11 @@ void handle_pointer_hold_begin(struct libinput_event *event, void handle_pointer_hold_end(struct libinput_event *event, struct wlr_pointer *pointer); -struct wlr_switch *create_libinput_switch( - struct libinput_device *device); +void init_device_switch(struct wlr_libinput_input_device *dev); +struct wlr_libinput_input_device *device_from_switch( + struct wlr_switch *switch_device); void handle_switch_toggle(struct libinput_event *event, - struct libinput_device *device); + struct wlr_switch *switch_device); struct wlr_touch *create_libinput_touch( struct libinput_device *device);