wlroots-hyprland/backend/libinput/events.c

332 lines
10 KiB
C
Raw Normal View History

#define _POSIX_C_SOURCE 200809L
2017-06-09 23:31:21 +02:00
#include <assert.h>
#include <libinput.h>
2018-02-12 21:29:23 +01:00
#include <stdlib.h>
#include <wayland-util.h>
2017-07-11 09:18:34 +02:00
#include <wlr/backend/session.h>
2017-06-21 16:27:45 +02:00
#include <wlr/interfaces/wlr_input_device.h>
2017-06-21 18:10:07 +02:00
#include <wlr/util/log.h>
#include "backend/libinput.h"
#include "util/array.h"
2018-02-12 21:29:23 +01:00
#include "util/signal.h"
2017-06-09 23:31:21 +02:00
static struct wlr_libinput_input_device *get_libinput_device_from_device(
2018-09-17 21:53:03 +02:00
struct wlr_input_device *wlr_dev) {
assert(wlr_input_device_is_libinput(wlr_dev));
return (struct wlr_libinput_input_device *)wlr_dev;
}
struct wlr_input_device *get_appropriate_device(
2017-06-09 23:52:11 +02: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 23:52:11 +02:00
return NULL;
}
struct wlr_libinput_input_device *dev;
wl_list_for_each(dev, wlr_devices, link) {
if (dev->wlr_input_device.type == desired_type) {
return &dev->wlr_input_device;
2017-06-09 23:52:11 +02:00
}
}
return NULL;
}
2018-09-17 21:53:03 +02:00
static void input_device_destroy(struct wlr_input_device *wlr_dev) {
struct wlr_libinput_input_device *dev =
get_libinput_device_from_device(wlr_dev);
2017-08-14 14:54:53 +02:00
libinput_device_unref(dev->handle);
wl_list_remove(&dev->link);
2017-08-14 14:54:53 +02:00
free(dev);
2017-06-10 18:21:54 +02:00
}
2018-03-06 07:59:10 +01:00
static const struct wlr_input_device_impl input_device_impl = {
.destroy = input_device_destroy,
2017-06-10 18:21:54 +02:00
};
2017-06-14 17:40:03 +02:00
static struct wlr_input_device *allocate_device(
2018-09-17 21:53:03 +02:00
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);
2018-09-17 21:53:03 +02:00
struct wlr_libinput_input_device *dev =
calloc(1, sizeof(struct wlr_libinput_input_device));
if (dev == NULL) {
return NULL;
}
2018-09-17 21:53:03 +02:00
struct wlr_input_device *wlr_dev = &dev->wlr_input_device;
2018-03-28 17:40:35 +02:00
libinput_device_get_size(libinput_dev,
&wlr_dev->width_mm, &wlr_dev->height_mm);
const char *output_name = libinput_device_get_output_name(libinput_dev);
if (output_name != NULL) {
wlr_dev->output_name = strdup(output_name);
}
wl_list_insert(wlr_devices, &dev->link);
2018-09-17 21:53:03 +02:00
dev->handle = libinput_dev;
libinput_device_ref(libinput_dev);
2017-08-14 14:54:53 +02:00
wlr_input_device_init(wlr_dev, type, &input_device_impl,
name, vendor, product);
return wlr_dev;
2017-06-13 16:27:15 +02:00
}
bool wlr_input_device_is_libinput(struct wlr_input_device *wlr_dev) {
2018-09-17 21:53:03 +02:00
return wlr_dev->impl == &input_device_impl;
}
static void handle_device_added(struct wlr_libinput_backend *backend,
struct libinput_device *libinput_dev) {
2017-06-09 23:31:21 +02: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) {
2018-07-09 23:49:54 +02:00
wlr_log(WLR_ERROR, "Allocation failed");
return;
}
wl_list_init(wlr_devices);
2018-07-09 23:49:54 +02:00
wlr_log(WLR_DEBUG, "Added %s [%d:%d]", name, vendor, product);
2017-06-09 23:31:21 +02:00
2018-09-17 21:53:03 +02:00
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 = create_libinput_keyboard(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 23:31:21 +02:00
}
2018-09-17 21:53:03 +02: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 = create_libinput_pointer(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 23:31:21 +02:00
}
2018-09-17 21:53:03 +02: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 = 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);
2017-06-09 23:31:21 +02:00
}
2018-09-17 21:53:03 +02: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 = 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);
2017-06-09 23:31:21 +02:00
}
2018-09-17 21:53:03 +02: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 = create_libinput_tablet_pad(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 23:31:21 +02:00
}
2018-09-17 21:53:03 +02:00
if (libinput_device_has_capability(
libinput_dev, LIBINPUT_DEVICE_CAP_GESTURE)) {
2017-06-09 23:31:21 +02:00
// TODO
}
2018-09-17 21:53:03 +02:00
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);
2017-06-09 23:31:21 +02:00
}
2018-09-17 21:53:03 +02:00
if (!wl_list_empty(wlr_devices)) {
struct wl_list **dst = wl_array_add(&backend->wlr_device_lists, sizeof(wlr_devices));
if (!dst) {
goto fail;
}
*dst = wlr_devices;
libinput_device_set_user_data(libinput_dev, wlr_devices);
2017-06-09 23:31:21 +02:00
} else {
free(wlr_devices);
2017-06-09 23:31:21 +02:00
}
return;
fail:
2018-07-09 23:49:54 +02:00
wlr_log(WLR_ERROR, "Could not allocate new device");
struct wlr_libinput_input_device *dev, *tmp_dev;
wl_list_for_each_safe(dev, tmp_dev, wlr_devices, link) {
free(dev);
}
free(wlr_devices);
2017-06-09 23:31:21 +02: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);
2018-07-09 23:49:54 +02:00
wlr_log(WLR_DEBUG, "Removing %s [%d:%d]", name, vendor, product);
if (!wlr_devices) {
return;
}
struct wlr_libinput_input_device *dev, *tmp_dev;
wl_list_for_each_safe(dev, tmp_dev, wlr_devices, link) {
wlr_input_device_destroy(&dev->wlr_input_device);
}
size_t i = 0;
Fix invalid uses of wl_array_for_each [1] and [2] have introduced new wl_array usage in wlroots, but contains a mistake: wl_array_for_each iterates over pointers to the wl_array entries, not over entries themselves. Fix all wl_array_for_each call sites. Name the variables "ptr" to avoid confusion. Found via ASan: ==148752==ERROR: AddressSanitizer: attempting free on address which was not malloc()-ed: 0x602000214111 in thread T0 #0 0x7f6ff2235f19 in __interceptor_free /build/gcc/src/gcc/libsanitizer/asan/asan_malloc_linux.cpp:127 #1 0x7f6ff1c04004 in wlr_tablet_destroy ../subprojects/wlroots/types/wlr_tablet_tool.c:24 #2 0x7f6ff1b8463c in wlr_input_device_destroy ../subprojects/wlroots/types/wlr_input_device.c:51 #3 0x7f6ff1ab9941 in backend_destroy ../subprojects/wlroots/backend/wayland/backend.c:306 #4 0x7f6ff1a68323 in wlr_backend_destroy ../subprojects/wlroots/backend/backend.c:57 #5 0x7f6ff1ab36b4 in multi_backend_destroy ../subprojects/wlroots/backend/multi/backend.c:57 #6 0x7f6ff1ab417c in handle_display_destroy ../subprojects/wlroots/backend/multi/backend.c:124 #7 0x7f6ff106184e in wl_display_destroy (/usr/lib/libwayland-server.so.0+0x884e) #8 0x55cd1a77c9e5 in server_fini ../sway/server.c:218 #9 0x55cd1a77893f in main ../sway/main.c:400 #10 0x7f6ff04bdb24 in __libc_start_main (/usr/lib/libc.so.6+0x27b24) #11 0x55cd1a73a7ad in _start (/home/simon/src/sway/build/sway/sway+0x33a7ad) 0x602000214111 is located 1 bytes inside of 16-byte region [0x602000214110,0x602000214120) freed by thread T0 here: #0 0x7f6ff2235f19 in __interceptor_free /build/gcc/src/gcc/libsanitizer/asan/asan_malloc_linux.cpp:127 #1 0x7f6ff1c04004 in wlr_tablet_destroy ../subprojects/wlroots/types/wlr_tablet_tool.c:24 #2 0x7f6ff1b8463c in wlr_input_device_destroy ../subprojects/wlroots/types/wlr_input_device.c:51 #3 0x7f6ff1ab9941 in backend_destroy ../subprojects/wlroots/backend/wayland/backend.c:306 #4 0x7f6ff1a68323 in wlr_backend_destroy ../subprojects/wlroots/backend/backend.c:57 #5 0x7f6ff1ab36b4 in multi_backend_destroy ../subprojects/wlroots/backend/multi/backend.c:57 #6 0x7f6ff1ab417c in handle_display_destroy ../subprojects/wlroots/backend/multi/backend.c:124 #7 0x7f6ff106184e in wl_display_destroy (/usr/lib/libwayland-server.so.0+0x884e) previously allocated by thread T0 here: #0 0x7f6ff2236279 in __interceptor_malloc /build/gcc/src/gcc/libsanitizer/asan/asan_malloc_linux.cpp:145 #1 0x7f6ff1066d03 in wl_array_add (/usr/lib/libwayland-server.so.0+0xdd03) [1]: https://github.com/swaywm/wlroots/pull/3002 [2]: https://github.com/swaywm/wlroots/pull/3004
2021-07-08 13:39:33 +02:00
struct wl_list **ptr;
wl_array_for_each(ptr, &backend->wlr_device_lists) {
struct wl_list *iter = *ptr;
if (iter == wlr_devices) {
array_remove_at(&backend->wlr_device_lists,
i * sizeof(struct wl_list *), sizeof(struct wl_list *));
break;
}
i++;
}
free(wlr_devices);
2017-06-09 23:31:21 +02:00
}
void handle_libinput_event(struct wlr_libinput_backend *backend,
2017-06-09 23:31:21 +02:00
struct libinput_event *event) {
struct libinput_device *libinput_dev = libinput_event_get_device(event);
2017-06-09 23:31:21 +02: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 23:31:21 +02:00
break;
case LIBINPUT_EVENT_DEVICE_REMOVED:
handle_device_removed(backend, libinput_dev);
2017-06-09 23:52:11 +02:00
break;
case LIBINPUT_EVENT_KEYBOARD_KEY:
handle_keyboard_key(event, libinput_dev);
2017-06-09 23:31:21 +02:00
break;
2017-06-13 16:27:15 +02:00
case LIBINPUT_EVENT_POINTER_MOTION:
handle_pointer_motion(event, libinput_dev);
2017-06-13 16:27:15 +02:00
break;
case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
handle_pointer_motion_abs(event, libinput_dev);
2017-06-13 16:27:15 +02:00
break;
case LIBINPUT_EVENT_POINTER_BUTTON:
handle_pointer_button(event, libinput_dev);
2017-06-13 16:27:15 +02:00
break;
case LIBINPUT_EVENT_POINTER_AXIS:
handle_pointer_axis(event, libinput_dev);
2017-06-13 16:27:15 +02:00
break;
2017-06-14 20:50:09 +02:00
case LIBINPUT_EVENT_TOUCH_DOWN:
handle_touch_down(event, libinput_dev);
2017-06-14 20:50:09 +02:00
break;
case LIBINPUT_EVENT_TOUCH_UP:
handle_touch_up(event, libinput_dev);
2017-06-14 20:50:09 +02:00
break;
case LIBINPUT_EVENT_TOUCH_MOTION:
handle_touch_motion(event, libinput_dev);
2017-06-14 20:50:09 +02:00
break;
case LIBINPUT_EVENT_TOUCH_CANCEL:
handle_touch_cancel(event, libinput_dev);
2017-06-14 20:50:09 +02:00
break;
case LIBINPUT_EVENT_TOUCH_FRAME:
handle_touch_frame(event, libinput_dev);
2017-06-14 20:50:09 +02:00
break;
2017-06-15 20:32:28 +02:00
case LIBINPUT_EVENT_TABLET_TOOL_AXIS:
handle_tablet_tool_axis(event, libinput_dev);
2017-06-15 20:32:28 +02:00
break;
case LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY:
handle_tablet_tool_proximity(event, libinput_dev);
2017-06-15 20:32:28 +02:00
break;
case LIBINPUT_EVENT_TABLET_TOOL_TIP:
handle_tablet_tool_tip(event, libinput_dev);
2017-06-15 20:32:28 +02:00
break;
case LIBINPUT_EVENT_TABLET_TOOL_BUTTON:
handle_tablet_tool_button(event, libinput_dev);
2017-06-15 20:32:28 +02:00
break;
2017-06-19 20:49:07 +02:00
case LIBINPUT_EVENT_TABLET_PAD_BUTTON:
handle_tablet_pad_button(event, libinput_dev);
2017-06-19 20:49:07 +02:00
break;
case LIBINPUT_EVENT_TABLET_PAD_RING:
handle_tablet_pad_ring(event, libinput_dev);
2017-06-19 20:49:07 +02:00
break;
case LIBINPUT_EVENT_TABLET_PAD_STRIP:
handle_tablet_pad_strip(event, libinput_dev);
2017-06-19 20:49:07 +02:00
break;
case LIBINPUT_EVENT_SWITCH_TOGGLE:
handle_switch_toggle(event, libinput_dev);
break;
case LIBINPUT_EVENT_GESTURE_SWIPE_BEGIN:
handle_pointer_swipe_begin(event, libinput_dev);
break;
case LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE:
handle_pointer_swipe_update(event, libinput_dev);
break;
case LIBINPUT_EVENT_GESTURE_SWIPE_END:
handle_pointer_swipe_end(event, libinput_dev);
break;
case LIBINPUT_EVENT_GESTURE_PINCH_BEGIN:
handle_pointer_pinch_begin(event, libinput_dev);
break;
case LIBINPUT_EVENT_GESTURE_PINCH_UPDATE:
handle_pointer_pinch_update(event, libinput_dev);
break;
case LIBINPUT_EVENT_GESTURE_PINCH_END:
handle_pointer_pinch_end(event, libinput_dev);
break;
#if LIBINPUT_HAS_HOLD_GESTURES
case LIBINPUT_EVENT_GESTURE_HOLD_BEGIN:
handle_pointer_hold_begin(event, libinput_dev);
break;
case LIBINPUT_EVENT_GESTURE_HOLD_END:
handle_pointer_hold_end(event, libinput_dev);
break;
#endif
2017-06-09 23:31:21 +02:00
default:
2018-07-09 23:49:54 +02:00
wlr_log(WLR_DEBUG, "Unknown libinput event %d", event_type);
2017-06-09 23:31:21 +02:00
break;
}
}