wlroots-hyprland/backend/libinput/tablet_tool.c

272 lines
9.8 KiB
C
Raw Normal View History

2018-04-23 08:55:49 +02:00
#define _POSIX_C_SOURCE 200809L
#include <string.h>
2017-06-15 20:32:28 +02:00
#include <assert.h>
#include <libinput.h>
2018-02-12 21:29:23 +01:00
#include <stdlib.h>
2017-06-21 16:27:45 +02:00
#include <wlr/interfaces/wlr_tablet_tool.h>
2017-06-21 18:10:07 +02:00
#include <wlr/util/log.h>
2017-06-15 20:32:28 +02:00
#include "backend/libinput.h"
struct tablet_tool {
struct wlr_tablet_tool wlr_tool;
struct libinput_tablet_tool *handle;
struct wl_list link; // wlr_libinput_input_device.tablet_tools
};
const struct wlr_tablet_impl libinput_tablet_impl = {
.name = "libinput-tablet-tool",
};
void init_device_tablet(struct wlr_libinput_input_device *dev) {
const char *name = libinput_device_get_name(dev->handle);
struct wlr_tablet *wlr_tablet = &dev->tablet;
wlr_tablet_init(wlr_tablet, &libinput_tablet_impl, name);
wlr_tablet->base.vendor = libinput_device_get_id_vendor(dev->handle);
wlr_tablet->base.product = libinput_device_get_id_product(dev->handle);
2018-04-23 08:55:49 +02:00
libinput_device_get_size(dev->handle, &wlr_tablet->width_mm,
&wlr_tablet->height_mm);
struct udev_device *udev = libinput_device_get_udev_device(dev->handle);
2021-07-01 10:09:35 +02:00
char **dst = wl_array_add(&wlr_tablet->paths, sizeof(char *));
*dst = strdup(udev_device_get_syspath(udev));
wl_list_init(&dev->tablet_tools);
}
2021-07-01 10:09:35 +02:00
static void tool_destroy(struct tablet_tool *tool) {
2022-08-18 13:16:16 +02:00
wl_signal_emit_mutable(&tool->wlr_tool.events.destroy, &tool->wlr_tool);
libinput_tablet_tool_unref(tool->handle);
libinput_tablet_tool_set_user_data(tool->handle, NULL);
wl_list_remove(&tool->link);
free(tool);
}
void finish_device_tablet(struct wlr_libinput_input_device *dev) {
struct tablet_tool *tool, *tmp;
wl_list_for_each_safe(tool, tmp, &dev->tablet_tools, link) {
tool_destroy(tool);
}
wlr_tablet_finish(&dev->tablet);
}
struct wlr_libinput_input_device *device_from_tablet(
struct wlr_tablet *wlr_tablet) {
assert(wlr_tablet->impl == &libinput_tablet_impl);
2018-04-23 08:55:49 +02:00
struct wlr_libinput_input_device *dev =
wl_container_of(wlr_tablet, dev, tablet);
return dev;
2017-06-15 20:32:28 +02:00
}
static enum wlr_tablet_tool_type wlr_type_from_libinput_type(
enum libinput_tablet_tool_type value) {
switch (value) {
case LIBINPUT_TABLET_TOOL_TYPE_PEN:
return WLR_TABLET_TOOL_TYPE_PEN;
case LIBINPUT_TABLET_TOOL_TYPE_ERASER:
return WLR_TABLET_TOOL_TYPE_ERASER;
case LIBINPUT_TABLET_TOOL_TYPE_BRUSH:
return WLR_TABLET_TOOL_TYPE_BRUSH;
case LIBINPUT_TABLET_TOOL_TYPE_PENCIL:
return WLR_TABLET_TOOL_TYPE_PENCIL;
case LIBINPUT_TABLET_TOOL_TYPE_AIRBRUSH:
return WLR_TABLET_TOOL_TYPE_AIRBRUSH;
case LIBINPUT_TABLET_TOOL_TYPE_MOUSE:
return WLR_TABLET_TOOL_TYPE_MOUSE;
case LIBINPUT_TABLET_TOOL_TYPE_LENS:
return WLR_TABLET_TOOL_TYPE_LENS;
case LIBINPUT_TABLET_TOOL_TYPE_TOTEM:
return WLR_TABLET_TOOL_TYPE_TOTEM;
}
abort(); // unreachable
}
static struct tablet_tool *get_tablet_tool(
struct wlr_libinput_input_device *dev,
struct libinput_tablet_tool *libinput_tool) {
struct tablet_tool *tool =
libinput_tablet_tool_get_user_data(libinput_tool);
if (tool) {
return tool;
}
tool = calloc(1, sizeof(*tool));
if (tool == NULL) {
wlr_log_errno(WLR_ERROR, "failed to allocate wlr_libinput_tablet_tool");
return NULL;
}
tool->wlr_tool.type = wlr_type_from_libinput_type(
libinput_tablet_tool_get_type(libinput_tool));
tool->wlr_tool.hardware_serial =
libinput_tablet_tool_get_serial(libinput_tool);
tool->wlr_tool.hardware_wacom =
libinput_tablet_tool_get_tool_id(libinput_tool);
tool->wlr_tool.pressure = libinput_tablet_tool_has_pressure(libinput_tool);
tool->wlr_tool.distance = libinput_tablet_tool_has_distance(libinput_tool);
tool->wlr_tool.tilt = libinput_tablet_tool_has_tilt(libinput_tool);
tool->wlr_tool.rotation = libinput_tablet_tool_has_rotation(libinput_tool);
tool->wlr_tool.slider = libinput_tablet_tool_has_slider(libinput_tool);
tool->wlr_tool.wheel = libinput_tablet_tool_has_wheel(libinput_tool);
wl_signal_init(&tool->wlr_tool.events.destroy);
tool->handle = libinput_tablet_tool_ref(libinput_tool);
libinput_tablet_tool_set_user_data(libinput_tool, tool);
wl_list_insert(&dev->tablet_tools, &tool->link);
return tool;
}
2017-06-15 20:32:28 +02:00
void handle_tablet_tool_axis(struct libinput_event *event,
struct wlr_tablet *wlr_tablet) {
2017-06-15 20:32:28 +02:00
struct libinput_event_tablet_tool *tevent =
libinput_event_get_tablet_tool_event(event);
struct wlr_libinput_input_device *dev = device_from_tablet(wlr_tablet);
struct tablet_tool *tool =
get_tablet_tool(dev, libinput_event_tablet_tool_get_tool(tevent));
struct wlr_tablet_tool_axis_event wlr_event = {
.tablet = wlr_tablet,
.tool = &tool->wlr_tool,
.time_msec = usec_to_msec(libinput_event_tablet_tool_get_time_usec(tevent)),
};
2017-06-15 20:32:28 +02:00
if (libinput_event_tablet_tool_x_has_changed(tevent)) {
wlr_event.updated_axes |= WLR_TABLET_TOOL_AXIS_X;
2018-03-28 17:40:35 +02:00
wlr_event.x = libinput_event_tablet_tool_get_x_transformed(tevent, 1);
2018-05-25 10:04:25 +02:00
wlr_event.dx = libinput_event_tablet_tool_get_dx(tevent);
2017-06-15 20:32:28 +02:00
}
if (libinput_event_tablet_tool_y_has_changed(tevent)) {
wlr_event.updated_axes |= WLR_TABLET_TOOL_AXIS_Y;
2018-03-28 17:40:35 +02:00
wlr_event.y = libinput_event_tablet_tool_get_y_transformed(tevent, 1);
2018-05-25 10:04:25 +02:00
wlr_event.dy = libinput_event_tablet_tool_get_dy(tevent);
2017-06-15 20:32:28 +02:00
}
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);
2017-06-15 20:32:28 +02:00
}
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);
2017-06-15 20:32:28 +02:00
}
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);
2017-06-15 20:32:28 +02:00
}
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);
2017-06-15 20:32:28 +02:00
}
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);
2017-06-15 20:32:28 +02:00
}
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);
2017-06-15 20:32:28 +02:00
}
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);
2017-06-15 20:32:28 +02:00
}
2022-08-18 13:16:16 +02:00
wl_signal_emit_mutable(&wlr_tablet->events.axis, &wlr_event);
2017-06-15 20:32:28 +02:00
}
void handle_tablet_tool_proximity(struct libinput_event *event,
struct wlr_tablet *wlr_tablet) {
2017-06-15 20:32:28 +02:00
struct libinput_event_tablet_tool *tevent =
libinput_event_get_tablet_tool_event(event);
struct wlr_libinput_input_device *dev = device_from_tablet(wlr_tablet);
struct tablet_tool *tool =
get_tablet_tool(dev, libinput_event_tablet_tool_get_tool(tevent));
struct wlr_tablet_tool_proximity_event wlr_event = {
.tablet = wlr_tablet,
.tool = &tool->wlr_tool,
.time_msec = usec_to_msec(libinput_event_tablet_tool_get_time_usec(tevent)),
.x = libinput_event_tablet_tool_get_x_transformed(tevent, 1),
.y = libinput_event_tablet_tool_get_y_transformed(tevent, 1),
};
2017-06-15 20:32:28 +02:00
switch (libinput_event_tablet_tool_get_proximity_state(tevent)) {
case LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_OUT:
wlr_event.state = WLR_TABLET_TOOL_PROXIMITY_OUT;
2017-06-15 20:32:28 +02:00
break;
case LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_IN:
wlr_event.state = WLR_TABLET_TOOL_PROXIMITY_IN;
2017-06-15 20:32:28 +02:00
break;
}
2022-08-18 13:16:16 +02:00
wl_signal_emit_mutable(&wlr_tablet->events.proximity, &wlr_event);
2018-09-17 21:53:03 +02:00
if (libinput_event_tablet_tool_get_proximity_state(tevent) ==
LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_IN) {
handle_tablet_tool_axis(event, wlr_tablet);
2018-05-04 11:46:32 +02:00
}
// If the tool is not unique, libinput will not find it again after the
// proximity out, so we should destroy it
if (!libinput_tablet_tool_is_unique(tool->handle)
&& libinput_event_tablet_tool_get_proximity_state(tevent) ==
2018-09-17 21:53:03 +02:00
LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_OUT) {
// The tool isn't unique, it can't be on multiple tablets
tool_destroy(tool);
}
2017-06-15 20:32:28 +02:00
}
void handle_tablet_tool_tip(struct libinput_event *event,
struct wlr_tablet *wlr_tablet) {
handle_tablet_tool_axis(event, wlr_tablet);
2017-06-15 20:32:28 +02:00
struct libinput_event_tablet_tool *tevent =
libinput_event_get_tablet_tool_event(event);
struct wlr_libinput_input_device *dev = device_from_tablet(wlr_tablet);
struct tablet_tool *tool =
get_tablet_tool(dev, libinput_event_tablet_tool_get_tool(tevent));
struct wlr_tablet_tool_tip_event wlr_event = {
.tablet = wlr_tablet,
.tool = &tool->wlr_tool,
.time_msec = usec_to_msec(libinput_event_tablet_tool_get_time_usec(tevent)),
.x = libinput_event_tablet_tool_get_x_transformed(tevent, 1),
.y = libinput_event_tablet_tool_get_y_transformed(tevent, 1),
};
2017-06-15 20:32:28 +02:00
switch (libinput_event_tablet_tool_get_tip_state(tevent)) {
case LIBINPUT_TABLET_TOOL_TIP_UP:
wlr_event.state = WLR_TABLET_TOOL_TIP_UP;
2017-06-15 20:32:28 +02:00
break;
case LIBINPUT_TABLET_TOOL_TIP_DOWN:
wlr_event.state = WLR_TABLET_TOOL_TIP_DOWN;
2017-06-15 20:32:28 +02:00
break;
}
2022-08-18 13:16:16 +02:00
wl_signal_emit_mutable(&wlr_tablet->events.tip, &wlr_event);
2017-06-15 20:32:28 +02:00
}
void handle_tablet_tool_button(struct libinput_event *event,
struct wlr_tablet *wlr_tablet) {
handle_tablet_tool_axis(event, wlr_tablet);
2017-06-15 20:32:28 +02:00
struct libinput_event_tablet_tool *tevent =
libinput_event_get_tablet_tool_event(event);
struct wlr_libinput_input_device *dev = device_from_tablet(wlr_tablet);
struct tablet_tool *tool =
get_tablet_tool(dev, libinput_event_tablet_tool_get_tool(tevent));
struct wlr_tablet_tool_button_event wlr_event = {
.tablet = wlr_tablet,
.tool = &tool->wlr_tool,
.time_msec = usec_to_msec(libinput_event_tablet_tool_get_time_usec(tevent)),
.button = libinput_event_tablet_tool_get_button(tevent),
};
2017-06-15 20:32:28 +02:00
switch (libinput_event_tablet_tool_get_button_state(tevent)) {
case LIBINPUT_BUTTON_STATE_RELEASED:
wlr_event.state = WLR_BUTTON_RELEASED;
2017-06-15 20:32:28 +02:00
break;
case LIBINPUT_BUTTON_STATE_PRESSED:
wlr_event.state = WLR_BUTTON_PRESSED;
2017-06-15 20:32:28 +02:00
break;
}
2022-08-18 13:16:16 +02:00
wl_signal_emit_mutable(&wlr_tablet->events.button, &wlr_event);
2017-06-15 20:32:28 +02:00
}