wlroots/backend/libinput/events.c

263 lines
8.4 KiB
C
Raw Normal View History

2017-06-09 21:31:21 +00:00
#include <assert.h>
#include <libinput.h>
2018-02-12 20:29:23 +00:00
#include <stdlib.h>
#include <wayland-util.h>
2017-07-11 07:18:34 +00:00
#include <wlr/backend/session.h>
2017-06-21 14:27:45 +00:00
#include <wlr/interfaces/wlr_input_device.h>
2017-06-21 16:10:07 +00:00
#include <wlr/util/log.h>
#include "backend/libinput.h"
2018-02-19 01:26:40 +00:00
#include "util/defs.h"
2018-02-12 20:29:23 +00:00
#include "util/signal.h"
2017-06-09 21:31:21 +00:00
struct wlr_input_device *get_appropriate_device(
2017-06-09 21:52:11 +00:00
enum wlr_input_device_type desired_type,
struct libinput_device *libinput_dev) {
struct wl_list *wlr_devices = libinput_device_get_user_data(libinput_dev);
if (!wlr_devices) {
2017-06-09 21:52:11 +00:00
return NULL;
}
struct wlr_input_device *dev;
wl_list_for_each(dev, wlr_devices, link) {
2017-06-09 21:52:11 +00:00
if (dev->type == desired_type) {
return dev;
}
}
return NULL;
}
2017-08-14 12:54:53 +00:00
static void wlr_libinput_device_destroy(struct wlr_input_device *_dev) {
struct wlr_libinput_input_device *dev = (struct wlr_libinput_input_device *)_dev;
libinput_device_unref(dev->handle);
wl_list_remove(&dev->wlr_input_device.link);
2017-08-14 12:54:53 +00:00
free(dev);
2017-06-10 16:21:54 +00:00
}
static struct wlr_input_device_impl input_device_impl = {
.destroy = wlr_libinput_device_destroy
};
2017-06-14 15:40:03 +00:00
static struct wlr_input_device *allocate_device(
struct wlr_libinput_backend *backend, struct libinput_device *libinput_dev,
struct wl_list *wlr_devices, enum wlr_input_device_type type) {
int vendor = libinput_device_get_id_vendor(libinput_dev);
int product = libinput_device_get_id_product(libinput_dev);
const char *name = libinput_device_get_name(libinput_dev);
struct wlr_libinput_input_device *wlr_libinput_dev;
if (!(wlr_libinput_dev = calloc(1, sizeof(struct wlr_libinput_input_device)))) {
return NULL;
}
2017-08-14 12:54:53 +00:00
struct wlr_input_device *wlr_dev = &wlr_libinput_dev->wlr_input_device;
wl_list_insert(wlr_devices, &wlr_dev->link);
2017-08-14 12:54:53 +00:00
wlr_libinput_dev->handle = libinput_dev;
libinput_device_ref(libinput_dev);
2017-08-14 12:54:53 +00:00
wlr_input_device_init(wlr_dev, type, &input_device_impl,
name, vendor, product);
return wlr_dev;
2017-06-13 14:27:15 +00:00
}
2018-02-19 01:26:40 +00:00
WLR_API
bool wlr_input_device_is_libinput(struct wlr_input_device *wlr_dev) {
return wlr_dev->impl == &input_device_impl;
}
static void handle_device_added(struct wlr_libinput_backend *backend,
struct libinput_device *libinput_dev) {
assert(backend && libinput_dev);
2017-06-09 21:31:21 +00:00
/*
* Note: the wlr API exposes only devices with a single capability, because
* that meshes better with how Wayland does things and is a bit simpler.
* However, libinput devices often have multiple capabilities - in such
* cases we have to create several devices.
*/
int vendor = libinput_device_get_id_vendor(libinput_dev);
int product = libinput_device_get_id_product(libinput_dev);
const char *name = libinput_device_get_name(libinput_dev);
struct wl_list *wlr_devices = calloc(1, sizeof(struct wl_list));
if (!wlr_devices) {
wlr_log(L_ERROR, "Allocation failed");
return;
}
wl_list_init(wlr_devices);
2017-06-09 21:31:21 +00:00
wlr_log(L_DEBUG, "Added %s [%d:%d]", name, vendor, product);
if (libinput_device_has_capability(libinput_dev, LIBINPUT_DEVICE_CAP_KEYBOARD)) {
struct wlr_input_device *wlr_dev = allocate_device(backend,
libinput_dev, wlr_devices, WLR_INPUT_DEVICE_KEYBOARD);
if (!wlr_dev) {
goto fail;
}
wlr_dev->keyboard = wlr_libinput_keyboard_create(libinput_dev);
if (!wlr_dev->keyboard) {
free(wlr_dev);
goto fail;
}
wlr_signal_emit_safe(&backend->backend.events.new_input, wlr_dev);
2017-06-09 21:31:21 +00:00
}
if (libinput_device_has_capability(libinput_dev, LIBINPUT_DEVICE_CAP_POINTER)) {
struct wlr_input_device *wlr_dev = allocate_device(backend,
libinput_dev, wlr_devices, WLR_INPUT_DEVICE_POINTER);
if (!wlr_dev) {
goto fail;
}
wlr_dev->pointer = wlr_libinput_pointer_create(libinput_dev);
if (!wlr_dev->pointer) {
free(wlr_dev);
goto fail;
}
wlr_signal_emit_safe(&backend->backend.events.new_input, wlr_dev);
2017-06-09 21:31:21 +00:00
}
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 = wlr_libinput_touch_create(libinput_dev);
if (!wlr_dev->touch) {
free(wlr_dev);
goto fail;
}
wlr_signal_emit_safe(&backend->backend.events.new_input, wlr_dev);
2017-06-09 21:31:21 +00:00
}
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_tool = wlr_libinput_tablet_tool_create(libinput_dev);
if (!wlr_dev->tablet_tool) {
free(wlr_dev);
goto fail;
}
wlr_signal_emit_safe(&backend->backend.events.new_input, wlr_dev);
2017-06-09 21:31:21 +00:00
}
if (libinput_device_has_capability(libinput_dev, LIBINPUT_DEVICE_CAP_TABLET_PAD)) {
struct wlr_input_device *wlr_dev = allocate_device(backend,
libinput_dev, wlr_devices, WLR_INPUT_DEVICE_TABLET_PAD);
if (!wlr_dev) {
goto fail;
}
wlr_dev->tablet_pad = wlr_libinput_tablet_pad_create(libinput_dev);
if (!wlr_dev->tablet_pad) {
free(wlr_dev);
goto fail;
}
wlr_signal_emit_safe(&backend->backend.events.new_input, wlr_dev);
2017-06-09 21:31:21 +00:00
}
if (libinput_device_has_capability(libinput_dev, LIBINPUT_DEVICE_CAP_GESTURE)) {
2017-06-09 21:31:21 +00:00
// TODO
}
if (libinput_device_has_capability(libinput_dev, LIBINPUT_DEVICE_CAP_SWITCH)) {
2017-06-09 21:31:21 +00:00
// TODO
}
if (wl_list_length(wlr_devices) > 0) {
libinput_device_set_user_data(libinput_dev, wlr_devices);
2017-11-18 23:17:40 +00:00
wlr_list_push(&backend->wlr_device_lists, wlr_devices);
2017-06-09 21:31:21 +00:00
} else {
free(wlr_devices);
2017-06-09 21:31:21 +00:00
}
return;
fail:
wlr_log(L_ERROR, "Could not allocate new device");
struct wlr_input_device *dev, *tmp_dev;
wl_list_for_each_safe(dev, tmp_dev, wlr_devices, link) {
free(dev);
}
free(wlr_devices);
2017-06-09 21:31:21 +00:00
}
static void handle_device_removed(struct wlr_libinput_backend *backend,
struct libinput_device *libinput_dev) {
struct wl_list *wlr_devices = libinput_device_get_user_data(libinput_dev);
int vendor = libinput_device_get_id_vendor(libinput_dev);
int product = libinput_device_get_id_product(libinput_dev);
const char *name = libinput_device_get_name(libinput_dev);
wlr_log(L_DEBUG, "Removing %s [%d:%d]", name, vendor, product);
if (!wlr_devices) {
return;
}
struct wlr_input_device *dev, *tmp_dev;
wl_list_for_each_safe(dev, tmp_dev, wlr_devices, link) {
wlr_input_device_destroy(dev);
}
2017-11-18 23:17:40 +00:00
for (size_t i = 0; i < backend->wlr_device_lists.length; i++) {
if (backend->wlr_device_lists.items[i] == wlr_devices) {
wlr_list_del(&backend->wlr_device_lists, i);
break;
}
}
free(wlr_devices);
2017-06-09 21:31:21 +00:00
}
void wlr_libinput_event(struct wlr_libinput_backend *backend,
2017-06-09 21:31:21 +00:00
struct libinput_event *event) {
assert(backend && event);
struct libinput_device *libinput_dev = libinput_event_get_device(event);
2017-06-09 21:31:21 +00:00
enum libinput_event_type event_type = libinput_event_get_type(event);
switch (event_type) {
case LIBINPUT_EVENT_DEVICE_ADDED:
handle_device_added(backend, libinput_dev);
2017-06-09 21:31:21 +00:00
break;
case LIBINPUT_EVENT_DEVICE_REMOVED:
handle_device_removed(backend, libinput_dev);
2017-06-09 21:52:11 +00:00
break;
case LIBINPUT_EVENT_KEYBOARD_KEY:
handle_keyboard_key(event, libinput_dev);
2017-06-09 21:31:21 +00:00
break;
2017-06-13 14:27:15 +00:00
case LIBINPUT_EVENT_POINTER_MOTION:
handle_pointer_motion(event, libinput_dev);
2017-06-13 14:27:15 +00:00
break;
case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
handle_pointer_motion_abs(event, libinput_dev);
2017-06-13 14:27:15 +00:00
break;
case LIBINPUT_EVENT_POINTER_BUTTON:
handle_pointer_button(event, libinput_dev);
2017-06-13 14:27:15 +00:00
break;
case LIBINPUT_EVENT_POINTER_AXIS:
handle_pointer_axis(event, libinput_dev);
2017-06-13 14:27:15 +00:00
break;
2017-06-14 18:50:09 +00:00
case LIBINPUT_EVENT_TOUCH_DOWN:
handle_touch_down(event, libinput_dev);
2017-06-14 18:50:09 +00:00
break;
case LIBINPUT_EVENT_TOUCH_UP:
handle_touch_up(event, libinput_dev);
2017-06-14 18:50:09 +00:00
break;
case LIBINPUT_EVENT_TOUCH_MOTION:
handle_touch_motion(event, libinput_dev);
2017-06-14 18:50:09 +00:00
break;
case LIBINPUT_EVENT_TOUCH_CANCEL:
handle_touch_cancel(event, libinput_dev);
2017-06-14 18:50:09 +00:00
break;
case LIBINPUT_EVENT_TOUCH_FRAME:
// no-op (at least for now)
break;
2017-06-15 18:32:28 +00:00
case LIBINPUT_EVENT_TABLET_TOOL_AXIS:
handle_tablet_tool_axis(event, libinput_dev);
2017-06-15 18:32:28 +00:00
break;
case LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY:
handle_tablet_tool_proximity(event, libinput_dev);
2017-06-15 18:32:28 +00:00
break;
case LIBINPUT_EVENT_TABLET_TOOL_TIP:
handle_tablet_tool_tip(event, libinput_dev);
2017-06-15 18:32:28 +00:00
break;
case LIBINPUT_EVENT_TABLET_TOOL_BUTTON:
handle_tablet_tool_button(event, libinput_dev);
2017-06-15 18:32:28 +00:00
break;
2017-06-19 18:49:07 +00:00
case LIBINPUT_EVENT_TABLET_PAD_BUTTON:
handle_tablet_pad_button(event, libinput_dev);
2017-06-19 18:49:07 +00:00
break;
case LIBINPUT_EVENT_TABLET_PAD_RING:
handle_tablet_pad_ring(event, libinput_dev);
2017-06-19 18:49:07 +00:00
break;
case LIBINPUT_EVENT_TABLET_PAD_STRIP:
handle_tablet_pad_strip(event, libinput_dev);
2017-06-19 18:49:07 +00:00
break;
2017-06-09 21:31:21 +00:00
default:
wlr_log(L_DEBUG, "Unknown libinput event %d", event_type);
break;
}
}