diff options
Diffstat (limited to 'backend/libinput')
-rw-r--r-- | backend/libinput/backend.c | 121 | ||||
-rw-r--r-- | backend/libinput/events.c | 176 | ||||
-rw-r--r-- | backend/libinput/keyboard.c | 44 | ||||
-rw-r--r-- | backend/libinput/pointer.c | 129 | ||||
-rw-r--r-- | backend/libinput/tablet_tool.c | 150 | ||||
-rw-r--r-- | backend/libinput/touch.c | 93 |
6 files changed, 713 insertions, 0 deletions
diff --git a/backend/libinput/backend.c b/backend/libinput/backend.c new file mode 100644 index 00000000..82ba44a6 --- /dev/null +++ b/backend/libinput/backend.c @@ -0,0 +1,121 @@ +#include <stdlib.h> +#include <assert.h> +#include <libinput.h> +#include <wlr/session.h> +#include <wlr/backend/interface.h> +#include "backend/udev.h" +#include "backend/libinput.h" +#include "common/log.h" + +static int wlr_libinput_open_restricted(const char *path, + int flags, void *_state) { + struct wlr_backend_state *state = _state; + return wlr_session_open_file(state->session, path); +} + +static void wlr_libinput_close_restricted(int fd, void *_state) { + struct wlr_backend_state *state = _state; + wlr_session_close_file(state->session, fd); +} + +static const struct libinput_interface libinput_impl = { + .open_restricted = wlr_libinput_open_restricted, + .close_restricted = wlr_libinput_close_restricted +}; + +static int wlr_libinput_readable(int fd, uint32_t mask, void *_state) { + struct wlr_backend_state *state = _state; + if (libinput_dispatch(state->libinput) != 0) { + wlr_log(L_ERROR, "Failed to dispatch libinput"); + // TODO: some kind of abort? + return 0; + } + struct libinput_event *event; + while ((event = libinput_get_event(state->libinput))) { + wlr_libinput_event(state, event); + } + return 0; +} + +static void wlr_libinput_log(struct libinput *libinput, + enum libinput_log_priority priority, const char *fmt, va_list args) { + _wlr_vlog(L_ERROR, fmt, args); +} + +static bool wlr_libinput_backend_init(struct wlr_backend_state *state) { + wlr_log(L_DEBUG, "Initializing libinput"); + state->libinput = libinput_udev_create_context(&libinput_impl, state, + state->udev->udev); + if (!state->libinput) { + wlr_log(L_ERROR, "Failed to create libinput context"); + return false; + } + + // TODO: Let user customize seat used + if (libinput_udev_assign_seat(state->libinput, "seat0") != 0) { + wlr_log(L_ERROR, "Failed to assign libinput seat"); + return false; + } + + // TODO: More sophisticated logging + libinput_log_set_handler(state->libinput, wlr_libinput_log); + libinput_log_set_priority(state->libinput, LIBINPUT_LOG_PRIORITY_ERROR); + + struct wl_event_loop *event_loop = + wl_display_get_event_loop(state->display); + if (state->input_event) { + wl_event_source_remove(state->input_event); + } + state->input_event = wl_event_loop_add_fd(event_loop, + libinput_get_fd(state->libinput), WL_EVENT_READABLE, + wlr_libinput_readable, state); + if (!state->input_event) { + wlr_log(L_ERROR, "Failed to create input event on event loop"); + return false; + } + wlr_log(L_DEBUG, "libinput sucessfully initialized"); + return true; +} + +static void wlr_libinput_backend_destroy(struct wlr_backend_state *state) { + // TODO +} + +static struct wlr_backend_impl backend_impl = { + .init = wlr_libinput_backend_init, + .destroy = wlr_libinput_backend_destroy +}; + +struct wlr_backend *wlr_libinput_backend_create(struct wl_display *display, + struct wlr_session *session, struct wlr_udev *udev) { + assert(display && session && udev); + + struct wlr_backend_state *state = calloc(1, sizeof(struct wlr_backend_state)); + if (!state) { + wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); + return NULL; + } + + struct wlr_backend *backend = wlr_backend_create(&backend_impl, state); + if (!backend) { + wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); + goto error_state; + } + + if (!(state->devices = list_create())) { + wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); + goto error_backend; + } + + state->backend = backend; + state->session = session; + state->udev = udev; + state->display = display; + + return backend; +error_state: + free(state); +error_backend: + wlr_backend_destroy(backend); + return NULL; +} diff --git a/backend/libinput/events.c b/backend/libinput/events.c new file mode 100644 index 00000000..8ebec63b --- /dev/null +++ b/backend/libinput/events.c @@ -0,0 +1,176 @@ +#include <stdlib.h> +#include <assert.h> +#include <libinput.h> +#include <wlr/session.h> +#include <wlr/types.h> +#include <wlr/common/list.h> +#include "backend/libinput.h" +#include "common/log.h" +#include "types.h" + +struct wlr_input_device *get_appropriate_device( + enum wlr_input_device_type desired_type, + struct libinput_device *device) { + list_t *devices = libinput_device_get_user_data(device); + if (!devices) { + return NULL; + } + for (size_t i = 0; i < devices->length; ++i) { + struct wlr_input_device *dev = devices->items[i]; + if (dev->type == desired_type) { + return dev; + } + } + return NULL; +} + +static void wlr_libinput_device_destroy(struct wlr_input_device_state *state) { + libinput_device_unref(state->handle); + free(state); +} + +static struct wlr_input_device_impl input_device_impl = { + .destroy = wlr_libinput_device_destroy +}; + +static struct wlr_input_device *allocate_device( + struct wlr_backend_state *state, struct libinput_device *device, + list_t *devices, enum wlr_input_device_type type) { + int vendor = libinput_device_get_id_vendor(device); + int product = libinput_device_get_id_product(device); + const char *name = libinput_device_get_name(device); + struct wlr_input_device_state *devstate = + calloc(1, sizeof(struct wlr_input_device_state)); + devstate->handle = device; + libinput_device_ref(device); + struct wlr_input_device *wlr_device = wlr_input_device_create( + type, &input_device_impl, devstate, + name, vendor, product); + list_add(devices, wlr_device); + list_add(state->devices, wlr_device); + return wlr_device; +} + +static void handle_device_added(struct wlr_backend_state *state, + struct libinput_device *device) { + assert(state && device); + /* + * 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(device); + int product = libinput_device_get_id_product(device); + const char *name = libinput_device_get_name(device); + list_t *devices = list_create(); + wlr_log(L_DEBUG, "Added %s [%d:%d]", name, vendor, product); + + if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_KEYBOARD)) { + struct wlr_input_device *wlr_device = allocate_device(state, + device, devices, WLR_INPUT_DEVICE_KEYBOARD); + wlr_device->keyboard = wlr_libinput_keyboard_create(device); + wl_signal_emit(&state->backend->events.input_add, wlr_device); + } + if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_POINTER)) { + struct wlr_input_device *wlr_device = allocate_device(state, + device, devices, WLR_INPUT_DEVICE_POINTER); + wlr_device->pointer = wlr_libinput_pointer_create(device); + wl_signal_emit(&state->backend->events.input_add, wlr_device); + } + if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TOUCH)) { + struct wlr_input_device *wlr_device = allocate_device(state, + device, devices, WLR_INPUT_DEVICE_TOUCH); + wlr_device->touch = wlr_libinput_touch_create(device); + wl_signal_emit(&state->backend->events.input_add, wlr_device); + } + if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TABLET_TOOL)) { + struct wlr_input_device *wlr_device = allocate_device(state, + device, devices, WLR_INPUT_DEVICE_TABLET_TOOL); + wlr_device->tablet_tool = wlr_libinput_tablet_tool_create(device); + wl_signal_emit(&state->backend->events.input_add, wlr_device); + } + if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TABLET_PAD)) { + // TODO + } + if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_GESTURE)) { + // TODO + } + if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_SWITCH)) { + // TODO + } + + if (devices->length > 0) { + libinput_device_set_user_data(device, devices); + } else { + list_free(devices); + } +} + +static void handle_device_removed(struct wlr_backend_state *state, + struct libinput_device *device) { + wlr_log(L_DEBUG, "libinput device removed"); + // TODO +} + +void wlr_libinput_event(struct wlr_backend_state *state, + struct libinput_event *event) { + assert(state && event); + struct libinput *context = libinput_event_get_context(event); + struct libinput_device *device = libinput_event_get_device(event); + enum libinput_event_type event_type = libinput_event_get_type(event); + (void)context; + switch (event_type) { + case LIBINPUT_EVENT_DEVICE_ADDED: + handle_device_added(state, device); + break; + case LIBINPUT_EVENT_DEVICE_REMOVED: + handle_device_removed(state, device); + break; + case LIBINPUT_EVENT_KEYBOARD_KEY: + handle_keyboard_key(event, device); + break; + case LIBINPUT_EVENT_POINTER_MOTION: + handle_pointer_motion(event, device); + break; + case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE: + handle_pointer_motion_abs(event, device); + break; + case LIBINPUT_EVENT_POINTER_BUTTON: + handle_pointer_button(event, device); + break; + case LIBINPUT_EVENT_POINTER_AXIS: + handle_pointer_axis(event, device); + break; + case LIBINPUT_EVENT_TOUCH_DOWN: + handle_touch_down(event, device); + break; + case LIBINPUT_EVENT_TOUCH_UP: + handle_touch_up(event, device); + break; + case LIBINPUT_EVENT_TOUCH_MOTION: + handle_touch_motion(event, device); + break; + case LIBINPUT_EVENT_TOUCH_CANCEL: + handle_touch_cancel(event, device); + break; + case LIBINPUT_EVENT_TOUCH_FRAME: + // no-op (at least for now) + break; + case LIBINPUT_EVENT_TABLET_TOOL_AXIS: + handle_tablet_tool_axis(event, device); + break; + case LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY: + handle_tablet_tool_proximity(event, device); + break; + case LIBINPUT_EVENT_TABLET_TOOL_TIP: + handle_tablet_tool_tip(event, device); + break; + case LIBINPUT_EVENT_TABLET_TOOL_BUTTON: + handle_tablet_tool_button(event, device); + break; + default: + wlr_log(L_DEBUG, "Unknown libinput event %d", event_type); + break; + } +} diff --git a/backend/libinput/keyboard.c b/backend/libinput/keyboard.c new file mode 100644 index 00000000..9ad41a78 --- /dev/null +++ b/backend/libinput/keyboard.c @@ -0,0 +1,44 @@ +#include <stdlib.h> +#include <assert.h> +#include <libinput.h> +#include <wlr/session.h> +#include <wlr/types.h> +#include <wlr/common/list.h> +#include "backend/libinput.h" +#include "common/log.h" +#include "types.h" + +struct wlr_keyboard *wlr_libinput_keyboard_create( + struct libinput_device *device) { + assert(device); + libinput_device_led_update(device, 0); + return wlr_keyboard_create(NULL, NULL); +} + +void handle_keyboard_key(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_KEYBOARD, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a keyboard event for a device with no keyboards?"); + return; + } + struct libinput_event_keyboard *kbevent = + libinput_event_get_keyboard_event(event); + struct wlr_keyboard_key *wlr_event = + calloc(1, sizeof(struct wlr_keyboard_key)); + wlr_event->time_sec = libinput_event_keyboard_get_time(kbevent); + wlr_event->time_usec = libinput_event_keyboard_get_time_usec(kbevent); + wlr_event->keycode = libinput_event_keyboard_get_key(kbevent); + enum libinput_key_state state = + libinput_event_keyboard_get_key_state(kbevent); + switch (state) { + case LIBINPUT_KEY_STATE_RELEASED: + wlr_event->state = WLR_KEY_RELEASED; + break; + case LIBINPUT_KEY_STATE_PRESSED: + wlr_event->state = WLR_KEY_PRESSED; + break; + } + wl_signal_emit(&dev->keyboard->events.key, wlr_event); +} diff --git a/backend/libinput/pointer.c b/backend/libinput/pointer.c new file mode 100644 index 00000000..0139fd60 --- /dev/null +++ b/backend/libinput/pointer.c @@ -0,0 +1,129 @@ +#include <stdlib.h> +#include <assert.h> +#include <libinput.h> +#include <wlr/session.h> +#include <wlr/types.h> +#include <wlr/common/list.h> +#include "backend/libinput.h" +#include "common/log.h" +#include "types.h" + +struct wlr_pointer *wlr_libinput_pointer_create( + struct libinput_device *device) { + assert(device); + return wlr_pointer_create(NULL, NULL); +} + +void handle_pointer_motion(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_POINTER, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a pointer event for a device with no pointers?"); + return; + } + struct libinput_event_pointer *pevent = + libinput_event_get_pointer_event(event); + struct wlr_pointer_motion *wlr_event = + calloc(1, sizeof(struct wlr_pointer_motion)); + wlr_event->time_sec = libinput_event_pointer_get_time(pevent); + wlr_event->time_usec = libinput_event_pointer_get_time_usec(pevent); + wlr_event->delta_x = libinput_event_pointer_get_dx(pevent); + wlr_event->delta_y = libinput_event_pointer_get_dy(pevent); + wl_signal_emit(&dev->pointer->events.motion, wlr_event); +} + +void handle_pointer_motion_abs(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_POINTER, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a pointer event for a device with no pointers?"); + return; + } + struct libinput_event_pointer *pevent = + libinput_event_get_pointer_event(event); + struct wlr_pointer_motion_absolute *wlr_event = + calloc(1, sizeof(struct wlr_pointer_motion_absolute)); + wlr_event->time_sec = libinput_event_pointer_get_time(pevent); + wlr_event->time_usec = libinput_event_pointer_get_time_usec(pevent); + wlr_event->x_mm = libinput_event_pointer_get_absolute_x(pevent); + wlr_event->y_mm = libinput_event_pointer_get_absolute_y(pevent); + libinput_device_get_size(device, &wlr_event->width_mm, &wlr_event->height_mm); + wl_signal_emit(&dev->pointer->events.motion_absolute, wlr_event); +} + +void handle_pointer_button(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_POINTER, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a pointer event for a device with no pointers?"); + return; + } + struct libinput_event_pointer *pevent = + libinput_event_get_pointer_event(event); + struct wlr_pointer_button *wlr_event = + calloc(1, sizeof(struct wlr_pointer_button)); + wlr_event->time_sec = libinput_event_pointer_get_time(pevent); + wlr_event->time_usec = libinput_event_pointer_get_time_usec(pevent); + wlr_event->button = libinput_event_pointer_get_button(pevent); + switch (libinput_event_pointer_get_button_state(pevent)) { + case LIBINPUT_BUTTON_STATE_PRESSED: + wlr_event->state = WLR_BUTTON_PRESSED; + break; + case LIBINPUT_BUTTON_STATE_RELEASED: + wlr_event->state = WLR_BUTTON_RELEASED; + break; + } + wl_signal_emit(&dev->pointer->events.button, wlr_event); +} + +void handle_pointer_axis(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_POINTER, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a pointer event for a device with no pointers?"); + return; + } + struct libinput_event_pointer *pevent = + libinput_event_get_pointer_event(event); + struct wlr_pointer_axis *wlr_event = + calloc(1, sizeof(struct wlr_pointer_axis)); + wlr_event->time_sec = libinput_event_pointer_get_time(pevent); + wlr_event->time_usec = libinput_event_pointer_get_time_usec(pevent); + switch (libinput_event_pointer_get_axis_source(pevent)) { + case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL: + wlr_event->source = WLR_AXIS_SOURCE_WHEEL; + break; + case LIBINPUT_POINTER_AXIS_SOURCE_FINGER: + wlr_event->source = WLR_AXIS_SOURCE_FINGER; + break; + case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS: + wlr_event->source = WLR_AXIS_SOURCE_CONTINUOUS; + break; + case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL_TILT: + wlr_event->source = WLR_AXIS_SOURCE_WHEEL_TILT; + break; + } + enum libinput_pointer_axis axies[] = { + LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL, + LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL, + }; + for (size_t i = 0; i < sizeof(axies) / sizeof(axies[0]); ++i) { + if (libinput_event_pointer_has_axis(pevent, axies[i])) { + switch (axies[i]) { + case LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL: + wlr_event->orientation = WLR_AXIS_ORIENTATION_VERTICAL; + break; + case LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL: + wlr_event->orientation = WLR_AXIS_ORIENTATION_HORIZONTAL; + break; + } + wlr_event->delta = libinput_event_pointer_get_axis_value( + pevent, axies[i]); + } + wl_signal_emit(&dev->pointer->events.axis, wlr_event); + } +} diff --git a/backend/libinput/tablet_tool.c b/backend/libinput/tablet_tool.c new file mode 100644 index 00000000..e8a3bc7d --- /dev/null +++ b/backend/libinput/tablet_tool.c @@ -0,0 +1,150 @@ +#include <stdlib.h> +#include <assert.h> +#include <libinput.h> +#include <wlr/session.h> +#include <wlr/types.h> +#include <wlr/common/list.h> +#include "backend/libinput.h" +#include "common/log.h" +#include "types.h" + +struct wlr_tablet_tool *wlr_libinput_tablet_tool_create( + struct libinput_device *device) { + assert(device); + return wlr_tablet_tool_create(NULL, NULL); +} + +void handle_tablet_tool_axis(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_TABLET_TOOL, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a tablet tool event for a device with no tablet tools?"); + return; + } + struct libinput_event_tablet_tool *tevent = + libinput_event_get_tablet_tool_event(event); + struct wlr_tablet_tool_axis *wlr_event = + calloc(1, sizeof(struct wlr_tablet_tool_axis)); + wlr_event->time_sec = libinput_event_tablet_tool_get_time(tevent); + wlr_event->time_usec = libinput_event_tablet_tool_get_time_usec(tevent); + libinput_device_get_size(device, &wlr_event->width_mm, &wlr_event->height_mm); + if (libinput_event_tablet_tool_x_has_changed(tevent)) { + wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_X; + wlr_event->x_mm = libinput_event_tablet_tool_get_x(tevent); + } + if (libinput_event_tablet_tool_y_has_changed(tevent)) { + wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_Y; + wlr_event->y_mm = libinput_event_tablet_tool_get_y(tevent); + } + if (libinput_event_tablet_tool_pressure_has_changed(tevent)) { + wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_PRESSURE; + wlr_event->pressure = libinput_event_tablet_tool_get_pressure(tevent); + } + if (libinput_event_tablet_tool_distance_has_changed(tevent)) { + wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_DISTANCE; + wlr_event->distance = libinput_event_tablet_tool_get_distance(tevent); + } + if (libinput_event_tablet_tool_tilt_x_has_changed(tevent)) { + wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_TILT_X; + wlr_event->tilt_x = libinput_event_tablet_tool_get_tilt_x(tevent); + } + if (libinput_event_tablet_tool_tilt_y_has_changed(tevent)) { + wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_TILT_Y; + wlr_event->tilt_y = libinput_event_tablet_tool_get_tilt_y(tevent); + } + if (libinput_event_tablet_tool_rotation_has_changed(tevent)) { + wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_ROTATION; + wlr_event->rotation = libinput_event_tablet_tool_get_rotation(tevent); + } + if (libinput_event_tablet_tool_slider_has_changed(tevent)) { + wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_SLIDER; + wlr_event->slider = libinput_event_tablet_tool_get_slider_position(tevent); + } + if (libinput_event_tablet_tool_wheel_has_changed(tevent)) { + wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_WHEEL; + wlr_event->wheel_delta = libinput_event_tablet_tool_get_wheel_delta(tevent); + } + wl_signal_emit(&dev->tablet_tool->events.axis, wlr_event); +} + +void handle_tablet_tool_proximity(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_TABLET_TOOL, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a tablet tool event for a device with no tablet tools?"); + return; + } + struct libinput_event_tablet_tool *tevent = + libinput_event_get_tablet_tool_event(event); + struct wlr_tablet_tool_proximity *wlr_event = + calloc(1, sizeof(struct wlr_tablet_tool_proximity)); + wlr_event->time_sec = libinput_event_tablet_tool_get_time(tevent); + wlr_event->time_usec = libinput_event_tablet_tool_get_time_usec(tevent); + switch (libinput_event_tablet_tool_get_proximity_state(tevent)) { + case LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_OUT: + wlr_event->state = WLR_TABLET_TOOL_PROXIMITY_OUT; + break; + case LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_IN: + wlr_event->state = WLR_TABLET_TOOL_PROXIMITY_IN; + handle_tablet_tool_axis(event, device); + break; + } + wl_signal_emit(&dev->tablet_tool->events.proximity, wlr_event); +} + +void handle_tablet_tool_tip(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_TABLET_TOOL, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a tablet tool event for a device with no tablet tools?"); + return; + } + handle_tablet_tool_axis(event, device); + struct libinput_event_tablet_tool *tevent = + libinput_event_get_tablet_tool_event(event); + struct wlr_tablet_tool_tip *wlr_event = + calloc(1, sizeof(struct wlr_tablet_tool_tip)); + wlr_event->time_sec = libinput_event_tablet_tool_get_time(tevent); + wlr_event->time_usec = libinput_event_tablet_tool_get_time_usec(tevent); + switch (libinput_event_tablet_tool_get_tip_state(tevent)) { + case LIBINPUT_TABLET_TOOL_TIP_UP: + wlr_event->state = WLR_TABLET_TOOL_TIP_UP; + break; + case LIBINPUT_TABLET_TOOL_TIP_DOWN: + wlr_event->state = WLR_TABLET_TOOL_TIP_DOWN; + break; + } + wl_signal_emit(&dev->tablet_tool->events.tip, wlr_event); +} + +void handle_tablet_tool_button(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_TABLET_TOOL, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a tablet tool event for a device with no tablet tools?"); + return; + } + // Tip events contain axis information. We update this information + // before we send the proximity event + handle_tablet_tool_axis(event, device); + struct libinput_event_tablet_tool *tevent = + libinput_event_get_tablet_tool_event(event); + struct wlr_tablet_tool_button *wlr_event = + calloc(1, sizeof(struct wlr_tablet_tool_button)); + wlr_event->time_sec = libinput_event_tablet_tool_get_time(tevent); + wlr_event->time_usec = libinput_event_tablet_tool_get_time_usec(tevent); + wlr_event->button = libinput_event_tablet_tool_get_button(tevent); + switch (libinput_event_tablet_tool_get_button_state(tevent)) { + case LIBINPUT_BUTTON_STATE_RELEASED: + wlr_event->state = WLR_BUTTON_RELEASED; + break; + case LIBINPUT_BUTTON_STATE_PRESSED: + wlr_event->state = WLR_BUTTON_PRESSED; + break; + } + wl_signal_emit(&dev->tablet_tool->events.button, wlr_event); +} diff --git a/backend/libinput/touch.c b/backend/libinput/touch.c new file mode 100644 index 00000000..02c9cfef --- /dev/null +++ b/backend/libinput/touch.c @@ -0,0 +1,93 @@ +#include <stdlib.h> +#include <assert.h> +#include <libinput.h> +#include <wlr/session.h> +#include <wlr/types.h> +#include <wlr/common/list.h> +#include "backend/libinput.h" +#include "common/log.h" +#include "types.h" + +struct wlr_touch *wlr_libinput_touch_create( + struct libinput_device *device) { + assert(device); + return wlr_touch_create(NULL, NULL); +} + +void handle_touch_down(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_TOUCH, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a touch event for a device with no touch?"); + return; + } + struct libinput_event_touch *tevent = + libinput_event_get_touch_event(event); + struct wlr_touch_down *wlr_event = + calloc(1, sizeof(struct wlr_touch_down)); + wlr_event->time_sec = libinput_event_touch_get_time(tevent); + wlr_event->time_usec = libinput_event_touch_get_time_usec(tevent); + wlr_event->slot = libinput_event_touch_get_slot(tevent); + wlr_event->x_mm = libinput_event_touch_get_x(tevent); + wlr_event->y_mm = libinput_event_touch_get_y(tevent); + libinput_device_get_size(device, &wlr_event->width_mm, &wlr_event->height_mm); + wl_signal_emit(&dev->touch->events.down, wlr_event); +} + +void handle_touch_up(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_TOUCH, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a touch event for a device with no touch?"); + return; + } + struct libinput_event_touch *tevent = + libinput_event_get_touch_event(event); + struct wlr_touch_up *wlr_event = + calloc(1, sizeof(struct wlr_touch_up)); + wlr_event->time_sec = libinput_event_touch_get_time(tevent); + wlr_event->time_usec = libinput_event_touch_get_time_usec(tevent); + wlr_event->slot = libinput_event_touch_get_slot(tevent); + wl_signal_emit(&dev->touch->events.up, wlr_event); +} + +void handle_touch_motion(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_TOUCH, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a touch event for a device with no touch?"); + return; + } + struct libinput_event_touch *tevent = + libinput_event_get_touch_event(event); + struct wlr_touch_motion *wlr_event = + calloc(1, sizeof(struct wlr_touch_motion)); + wlr_event->time_sec = libinput_event_touch_get_time(tevent); + wlr_event->time_usec = libinput_event_touch_get_time_usec(tevent); + wlr_event->slot = libinput_event_touch_get_slot(tevent); + wlr_event->x_mm = libinput_event_touch_get_x(tevent); + wlr_event->y_mm = libinput_event_touch_get_y(tevent); + libinput_device_get_size(device, &wlr_event->width_mm, &wlr_event->height_mm); + wl_signal_emit(&dev->touch->events.motion, wlr_event); +} + +void handle_touch_cancel(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_TOUCH, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a touch event for a device with no touch?"); + return; + } + struct libinput_event_touch *tevent = + libinput_event_get_touch_event(event); + struct wlr_touch_cancel *wlr_event = + calloc(1, sizeof(struct wlr_touch_cancel)); + wlr_event->time_sec = libinput_event_touch_get_time(tevent); + wlr_event->time_usec = libinput_event_touch_get_time_usec(tevent); + wlr_event->slot = libinput_event_touch_get_slot(tevent); + wl_signal_emit(&dev->touch->events.cancel, wlr_event); +} |