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.
master
Simon Zeni 3 years ago committed by Kirill Primak
parent d750c5ac67
commit 4f4dd95223

@ -249,6 +249,9 @@ struct libinput_device *wlr_libinput_get_device_handle(
case WLR_INPUT_DEVICE_SWITCH: case WLR_INPUT_DEVICE_SWITCH:
dev = device_from_switch(wlr_dev->switch_device); dev = device_from_switch(wlr_dev->switch_device);
break; break;
case WLR_INPUT_DEVICE_TOUCH:
dev = device_from_touch(wlr_dev->touch);
break;
default: default:
dev = (struct wlr_libinput_input_device *)wlr_dev; dev = (struct wlr_libinput_input_device *)wlr_dev;
break; break;

@ -46,6 +46,9 @@ void destroy_libinput_input_device(struct wlr_libinput_input_device *dev)
if (dev->switch_device.impl) { if (dev->switch_device.impl) {
wlr_switch_destroy(&dev->switch_device); wlr_switch_destroy(&dev->switch_device);
} }
if (dev->touch.impl) {
wlr_touch_destroy(&dev->touch);
}
} }
libinput_device_unref(dev->handle); libinput_device_unref(dev->handle);
@ -151,6 +154,14 @@ static void handle_device_added(struct wlr_libinput_backend *backend,
dev_used = true; 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) { if (dev_used) {
wl_list_insert(&backend->devices, &dev->link); wl_list_insert(&backend->devices, &dev->link);
return; return;
@ -166,20 +177,6 @@ static void handle_device_added(struct wlr_libinput_backend *backend,
} }
wl_list_init(wlr_devices); 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, if (libinput_device_has_capability(libinput_dev,
LIBINPUT_DEVICE_CAP_TABLET_TOOL)) { LIBINPUT_DEVICE_CAP_TABLET_TOOL)) {
struct wlr_input_device *wlr_dev = allocate_device(backend, 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); handle_pointer_axis(event, &dev->pointer);
break; break;
case LIBINPUT_EVENT_TOUCH_DOWN: case LIBINPUT_EVENT_TOUCH_DOWN:
handle_touch_down(event, libinput_dev); handle_touch_down(event, &dev->touch);
break; break;
case LIBINPUT_EVENT_TOUCH_UP: case LIBINPUT_EVENT_TOUCH_UP:
handle_touch_up(event, libinput_dev); handle_touch_up(event, &dev->touch);
break; break;
case LIBINPUT_EVENT_TOUCH_MOTION: case LIBINPUT_EVENT_TOUCH_MOTION:
handle_touch_motion(event, libinput_dev); handle_touch_motion(event, &dev->touch);
break; break;
case LIBINPUT_EVENT_TOUCH_CANCEL: case LIBINPUT_EVENT_TOUCH_CANCEL:
handle_touch_cancel(event, libinput_dev); handle_touch_cancel(event, &dev->touch);
break; break;
case LIBINPUT_EVENT_TOUCH_FRAME: case LIBINPUT_EVENT_TOUCH_FRAME:
handle_touch_frame(event, libinput_dev); handle_touch_frame(event, &dev->touch);
break; break;
case LIBINPUT_EVENT_TABLET_TOOL_AXIS: case LIBINPUT_EVENT_TABLET_TOOL_AXIS:
handle_tablet_tool_axis(event, libinput_dev); handle_tablet_tool_axis(event, libinput_dev);

@ -1,113 +1,87 @@
#include <assert.h> #include <assert.h>
#include <libinput.h> #include <libinput.h>
#include <stdlib.h>
#include <wlr/backend/session.h>
#include <wlr/interfaces/wlr_touch.h> #include <wlr/interfaces/wlr_touch.h>
#include <wlr/types/wlr_input_device.h>
#include <wlr/util/log.h>
#include "backend/libinput.h" #include "backend/libinput.h"
#include "util/signal.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_init(wlr_touch, &libinput_touch_impl, name);
wlr_touch->base.vendor = libinput_device_get_id_vendor(libinput_dev); wlr_touch->base.vendor = libinput_device_get_id_vendor(dev->handle);
wlr_touch->base.product = libinput_device_get_id_product(libinput_dev); wlr_touch->base.product = libinput_device_get_id_product(dev->handle);
return wlr_touch;
} }
void handle_touch_down(struct libinput_event *event, struct wlr_libinput_input_device *device_from_touch(
struct libinput_device *libinput_dev) { struct wlr_touch *wlr_touch) {
struct wlr_input_device *wlr_dev = assert(wlr_touch->impl == &libinput_touch_impl);
get_appropriate_device(WLR_INPUT_DEVICE_TOUCH, libinput_dev);
if (!wlr_dev) { struct wlr_libinput_input_device *dev =
wlr_log(WLR_DEBUG, "Got a touch event for a device with no touch?"); wl_container_of(wlr_touch, dev, touch);
return; return dev;
} }
void handle_touch_down(struct libinput_event *event,
struct wlr_touch *touch) {
struct libinput_event_touch *tevent = struct libinput_event_touch *tevent =
libinput_event_get_touch_event(event); libinput_event_get_touch_event(event);
struct wlr_event_touch_down wlr_event = { 0 }; struct wlr_event_touch_down wlr_event = { 0 };
wlr_event.device = wlr_dev; wlr_event.device = &touch->base;
wlr_event.time_msec = wlr_event.time_msec =
usec_to_msec(libinput_event_touch_get_time_usec(tevent)); usec_to_msec(libinput_event_touch_get_time_usec(tevent));
wlr_event.touch_id = libinput_event_touch_get_seat_slot(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.x = libinput_event_touch_get_x_transformed(tevent, 1);
wlr_event.y = libinput_event_touch_get_y_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, void handle_touch_up(struct libinput_event *event,
struct libinput_device *libinput_dev) { struct wlr_touch *touch) {
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 libinput_event_touch *tevent = struct libinput_event_touch *tevent =
libinput_event_get_touch_event(event); libinput_event_get_touch_event(event);
struct wlr_event_touch_up wlr_event = { 0 }; struct wlr_event_touch_up wlr_event = { 0 };
wlr_event.device = wlr_dev; wlr_event.device = &touch->base;
wlr_event.time_msec = wlr_event.time_msec =
usec_to_msec(libinput_event_touch_get_time_usec(tevent)); usec_to_msec(libinput_event_touch_get_time_usec(tevent));
wlr_event.touch_id = libinput_event_touch_get_seat_slot(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, void handle_touch_motion(struct libinput_event *event,
struct libinput_device *libinput_dev) { struct wlr_touch *touch) {
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 libinput_event_touch *tevent = struct libinput_event_touch *tevent =
libinput_event_get_touch_event(event); libinput_event_get_touch_event(event);
struct wlr_event_touch_motion wlr_event = { 0 }; struct wlr_event_touch_motion wlr_event = { 0 };
wlr_event.device = wlr_dev; wlr_event.device = &touch->base;
wlr_event.time_msec = wlr_event.time_msec =
usec_to_msec(libinput_event_touch_get_time_usec(tevent)); usec_to_msec(libinput_event_touch_get_time_usec(tevent));
wlr_event.touch_id = libinput_event_touch_get_seat_slot(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.x = libinput_event_touch_get_x_transformed(tevent, 1);
wlr_event.y = libinput_event_touch_get_y_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, void handle_touch_cancel(struct libinput_event *event,
struct libinput_device *libinput_dev) { struct wlr_touch *touch) {
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 libinput_event_touch *tevent = struct libinput_event_touch *tevent =
libinput_event_get_touch_event(event); libinput_event_get_touch_event(event);
struct wlr_event_touch_cancel wlr_event = { 0 }; struct wlr_event_touch_cancel wlr_event = { 0 };
wlr_event.device = wlr_dev; wlr_event.device = &touch->base;
wlr_event.time_msec = wlr_event.time_msec =
usec_to_msec(libinput_event_touch_get_time_usec(tevent)); usec_to_msec(libinput_event_touch_get_time_usec(tevent));
wlr_event.touch_id = libinput_event_touch_get_seat_slot(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, void handle_touch_frame(struct libinput_event *event,
struct libinput_device *libinput_dev) { struct wlr_touch *touch) {
struct wlr_input_device *wlr_dev = wlr_signal_emit_safe(&touch->events.frame, NULL);
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);
} }

@ -37,6 +37,7 @@ struct wlr_libinput_input_device {
struct wlr_keyboard keyboard; struct wlr_keyboard keyboard;
struct wlr_pointer pointer; struct wlr_pointer pointer;
struct wlr_switch switch_device; struct wlr_switch switch_device;
struct wlr_touch touch;
struct wl_list link; struct wl_list link;
}; };
@ -96,18 +97,19 @@ struct wlr_libinput_input_device *device_from_switch(
void handle_switch_toggle(struct libinput_event *event, void handle_switch_toggle(struct libinput_event *event,
struct wlr_switch *switch_device); struct wlr_switch *switch_device);
struct wlr_touch *create_libinput_touch( void init_device_touch(struct wlr_libinput_input_device *dev);
struct libinput_device *device); struct wlr_libinput_input_device *device_from_touch(
struct wlr_touch *touch);
void handle_touch_down(struct libinput_event *event, void handle_touch_down(struct libinput_event *event,
struct libinput_device *device); struct wlr_touch *touch);
void handle_touch_up(struct libinput_event *event, void handle_touch_up(struct libinput_event *event,
struct libinput_device *device); struct wlr_touch *touch);
void handle_touch_motion(struct libinput_event *event, void handle_touch_motion(struct libinput_event *event,
struct libinput_device *device); struct wlr_touch *touch);
void handle_touch_cancel(struct libinput_event *event, void handle_touch_cancel(struct libinput_event *event,
struct libinput_device *device); struct wlr_touch *touch);
void handle_touch_frame(struct libinput_event *event, void handle_touch_frame(struct libinput_event *event,
struct libinput_device *device); struct wlr_touch *touch);
struct wlr_tablet *create_libinput_tablet( struct wlr_tablet *create_libinput_tablet(
struct libinput_device *device); struct libinput_device *device);

Loading…
Cancel
Save