libinput: hook up tablet events

Based on a patches from
Peter Hutterer <peter.hutterer@who-t.net>
Lyude Paul <thatslyude@gmail.com>

Signed-off-by: Michael Olbrich <m.olbrich@pengutronix.de>
This commit is contained in:
Michael Olbrich 2021-01-20 09:21:29 +01:00 committed by Daniel Stone
parent 578922797b
commit ce99b181a3
5 changed files with 305 additions and 3 deletions

View File

@ -906,6 +906,8 @@ struct weston_tablet_tool {
int button_count;
bool tip_is_down;
struct timespec frame_time;
};
struct weston_tablet {

View File

@ -45,6 +45,13 @@
#include "shared/helpers.h"
#include "shared/timespec-util.h"
#include "tablet-unstable-v2-server-protocol.h"
struct tablet_output_listener {
struct wl_listener base;
struct wl_list tablet_list;
};
void
evdev_led_update(struct evdev_device *device, enum weston_led weston_leds)
{
@ -509,6 +516,247 @@ handle_touch_frame(struct libinput_device *libinput_device,
notify_touch_frame(device->touch_device);
}
static void
process_tablet_axis(struct weston_output *output, struct weston_tablet *tablet,
struct weston_tablet_tool *tool,
struct libinput_event_tablet_tool *axis_event)
{
struct timespec time;
const int NORMALIZED_AXIS_MAX = 65535;
timespec_from_usec(&time,
libinput_event_tablet_tool_get_time(axis_event));
if (libinput_event_tablet_tool_x_has_changed(axis_event) ||
libinput_event_tablet_tool_y_has_changed(axis_event)) {
double x, y;
uint32_t width, height;
struct weston_coord_global pos;
width = output->current_mode->width;
height = output->current_mode->height;
x = libinput_event_tablet_tool_get_x_transformed(axis_event,
width);
y = libinput_event_tablet_tool_get_y_transformed(axis_event,
height);
pos = weston_coord_global_from_output_point(x, y, output);
notify_tablet_tool_motion(tool, &time, pos);
}
if (libinput_event_tablet_tool_pressure_has_changed(axis_event)) {
double pressure;
pressure = libinput_event_tablet_tool_get_pressure(axis_event);
/* convert axis range [0.0, 1.0] to [0, 65535] */
pressure *= NORMALIZED_AXIS_MAX;
notify_tablet_tool_pressure(tool, &time, pressure);
}
if (libinput_event_tablet_tool_distance_has_changed(axis_event)) {
double distance;
distance = libinput_event_tablet_tool_get_distance(axis_event);
/* convert axis range [0.0, 1.0] to [0, 65535] */
distance *= NORMALIZED_AXIS_MAX;
notify_tablet_tool_distance(tool, &time, distance);
}
if (libinput_event_tablet_tool_tilt_x_has_changed(axis_event) ||
libinput_event_tablet_tool_tilt_y_has_changed(axis_event)) {
double tx, ty;
tx = libinput_event_tablet_tool_get_tilt_x(axis_event);
ty = libinput_event_tablet_tool_get_tilt_y(axis_event);
notify_tablet_tool_tilt(tool, &time,
wl_fixed_from_double(tx),
wl_fixed_from_double(ty));
}
}
static void
idle_notify_tablet_tool_frame(void *data)
{
struct weston_tablet_tool *tool = data;
notify_tablet_tool_frame(tool, &tool->frame_time);
timespec_from_nsec(&tool->frame_time, 0);
}
/*
* libinput does not provide frame information. So assume that all events that
* belong to the same hardware event have the same timestamp and are created
* together. Use an idle callback to delay the frame event until all events that
* blong together have been handled.
*/
static void
async_notify_tablet_tool_frame(struct weston_tablet_tool *tool,
struct timespec *time)
{
if (timespec_eq(&tool->frame_time, time))
return;
/* If this is a second timestamp, then push out the first frame and use
* the already queued callback for the new timestamp. Otherwise queue a
* new callback. */
if (!timespec_is_zero(&tool->frame_time))
notify_tablet_tool_frame(tool, &tool->frame_time);
else {
struct wl_event_loop *loop;
loop = wl_display_get_event_loop(tool->seat->compositor->wl_display);
wl_event_loop_add_idle(loop, idle_notify_tablet_tool_frame, tool);
}
tool->frame_time = *time;
}
static void
handle_tablet_proximity(struct libinput_device *libinput_device,
struct libinput_event_tablet_tool *proximity_event)
{
struct evdev_device *device;
struct weston_tablet *tablet;
struct weston_tablet_tool *tool;
struct libinput_tablet_tool *libinput_tool;
struct timespec time;
device = libinput_device_get_user_data(libinput_device);
timespec_from_usec(&time,
libinput_event_tablet_tool_get_time(proximity_event));
libinput_tool = libinput_event_tablet_tool_get_tool(proximity_event);
tool = libinput_tablet_tool_get_user_data(libinput_tool);
tablet = device->tablet;
if (libinput_event_tablet_tool_get_proximity_state(proximity_event) ==
LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_OUT) {
notify_tablet_tool_proximity_out(tool, &time);
async_notify_tablet_tool_frame(tool, &time);
return;
}
if (!tool) {
uint64_t serial;
enum libinput_tablet_tool_type libinput_tool_type;
uint32_t type;
serial = libinput_tablet_tool_get_serial(libinput_tool);
libinput_tool_type = libinput_tablet_tool_get_type(libinput_tool);
switch (libinput_tool_type) {
case LIBINPUT_TABLET_TOOL_TYPE_PEN:
type = ZWP_TABLET_TOOL_V2_TYPE_PEN;
break;
case LIBINPUT_TABLET_TOOL_TYPE_ERASER:
type = ZWP_TABLET_TOOL_V2_TYPE_ERASER;
break;
default:
weston_log("Unknown libinput tool type %d\n",
libinput_tool_type);
return;
}
tool = weston_seat_add_tablet_tool(device->seat);
tool->serial = serial;
tool->hwid = libinput_tablet_tool_get_tool_id(libinput_tool);
tool->type = type;
tool->capabilities = 0;
if (libinput_tablet_tool_has_distance(libinput_tool))
tool->capabilities |= 1 << ZWP_TABLET_TOOL_V2_CAPABILITY_DISTANCE;
if (libinput_tablet_tool_has_pressure(libinput_tool))
tool->capabilities |= 1 << ZWP_TABLET_TOOL_V2_CAPABILITY_PRESSURE;
if (libinput_tablet_tool_has_tilt(libinput_tool))
tool->capabilities |= 1 << ZWP_TABLET_TOOL_V2_CAPABILITY_TILT;
/* unique tools are tracked globally, others per tablet */
if (libinput_tablet_tool_is_unique(libinput_tool))
wl_list_insert(&device->seat->tablet_tool_list, &tool->link);
else
wl_list_insert(&tablet->tool_list, &tool->link);
libinput_tablet_tool_set_user_data(libinput_tool, tool);
notify_tablet_tool_added(tool);
}
notify_tablet_tool_proximity_in(tool, &time, tablet);
process_tablet_axis(device->output, tablet, tool, proximity_event);
async_notify_tablet_tool_frame(tool, &time);
}
static void
handle_tablet_axis(struct libinput_device *libinput_device,
struct libinput_event_tablet_tool *axis_event)
{
struct evdev_device *device =
libinput_device_get_user_data(libinput_device);
struct weston_tablet_tool *tool;
struct weston_tablet *tablet = device->tablet;
struct libinput_tablet_tool *libinput_tool;
struct timespec time;
libinput_tool = libinput_event_tablet_tool_get_tool(axis_event);
tool = libinput_tablet_tool_get_user_data(libinput_tool);
timespec_from_usec(&time,
libinput_event_tablet_tool_get_time(axis_event));
process_tablet_axis(device->output, tablet, tool, axis_event);
async_notify_tablet_tool_frame(tool, &time);
}
static void
handle_tablet_tip(struct libinput_device *libinput_device,
struct libinput_event_tablet_tool *tip_event)
{
struct evdev_device *device =
libinput_device_get_user_data(libinput_device);
struct weston_tablet_tool *tool;
struct libinput_tablet_tool *libinput_tool;
struct timespec time;
libinput_tool = libinput_event_tablet_tool_get_tool(tip_event);
tool = libinput_tablet_tool_get_user_data(libinput_tool);
timespec_from_usec(&time,
libinput_event_tablet_tool_get_time(tip_event));
process_tablet_axis(device->output, device->tablet, tool, tip_event);
if (libinput_event_tablet_tool_get_tip_state(tip_event) ==
LIBINPUT_TABLET_TOOL_TIP_DOWN)
notify_tablet_tool_down(tool, &time);
else
notify_tablet_tool_up(tool, &time);
async_notify_tablet_tool_frame(tool, &time);
}
static void
handle_tablet_button(struct libinput_device *libinput_device,
struct libinput_event_tablet_tool *button_event)
{
struct weston_tablet_tool *tool;
struct libinput_tablet_tool *libinput_tool;
struct timespec time;
uint32_t button;
enum zwp_tablet_tool_v2_button_state state;
libinput_tool = libinput_event_tablet_tool_get_tool(button_event);
tool = libinput_tablet_tool_get_user_data(libinput_tool);
timespec_from_usec(&time,
libinput_event_tablet_tool_get_time(button_event));
button = libinput_event_tablet_tool_get_button(button_event);
if (libinput_event_tablet_tool_get_button_state(button_event) ==
LIBINPUT_BUTTON_STATE_PRESSED)
state = ZWP_TABLET_TOOL_V2_BUTTON_STATE_PRESSED;
else
state = ZWP_TABLET_TOOL_V2_BUTTON_STATE_RELEASED;
notify_tablet_tool_button(tool, &time, button, state);
async_notify_tablet_tool_frame(tool, &time);
}
int
evdev_device_process_event(struct libinput_event *event)
{
@ -561,6 +809,22 @@ evdev_device_process_event(struct libinput_event *event)
handle_touch_frame(libinput_device,
libinput_event_get_touch_event(event));
break;
case LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY:
handle_tablet_proximity(libinput_device,
libinput_event_get_tablet_tool_event(event));
break;
case LIBINPUT_EVENT_TABLET_TOOL_TIP:
handle_tablet_tip(libinput_device,
libinput_event_get_tablet_tool_event(event));
break;
case LIBINPUT_EVENT_TABLET_TOOL_AXIS:
handle_tablet_axis(libinput_device,
libinput_event_get_tablet_tool_event(event));
break;
case LIBINPUT_EVENT_TABLET_TOOL_BUTTON:
handle_tablet_button(libinput_device,
libinput_event_get_tablet_tool_event(event));
break;
default:
handled = 0;
weston_log("unknown libinput event %d\n",
@ -711,6 +975,33 @@ evdev_device_set_output(struct evdev_device *device,
evdev_device_set_calibration(device);
}
static void
evdev_device_init_tablet(struct evdev_device *device,
struct libinput_device *libinput_device,
struct weston_seat *seat)
{
struct weston_tablet *tablet;
struct udev_device *udev_device;
tablet = weston_seat_add_tablet(seat);
tablet->name = strdup(libinput_device_get_name(libinput_device));
tablet->vid = libinput_device_get_id_vendor(libinput_device);
tablet->pid = libinput_device_get_id_product(libinput_device);
udev_device = libinput_device_get_udev_device(libinput_device);
if (udev_device) {
tablet->path = udev_device_get_devnode(udev_device);
udev_device_unref(udev_device);
}
wl_list_insert(&seat->tablet_list, &tablet->link);
device->seat_caps |= EVDEV_SEAT_TABLET;
device->tablet = tablet;
notify_tablet_added(tablet);
}
struct evdev_device *
evdev_device_create(struct libinput_device *libinput_device,
struct weston_seat *seat)
@ -751,6 +1042,10 @@ evdev_device_create(struct libinput_device *libinput_device,
device->seat_caps |= EVDEV_SEAT_TOUCH;
device->touch_device = create_touch_device(device);
}
if (libinput_device_has_capability(libinput_device,
LIBINPUT_DEVICE_CAP_TABLET_TOOL)) {
evdev_device_init_tablet(device, libinput_device, seat);
}
libinput_device_set_user_data(libinput_device, device);
libinput_device_ref(libinput_device);
@ -769,6 +1064,8 @@ evdev_device_destroy(struct evdev_device *device)
weston_touch_device_destroy(device->touch_device);
weston_seat_release_touch(device->seat);
}
if (device->seat_caps & EVDEV_SEAT_TABLET)
weston_seat_release_tablet(device->tablet);
if (device->output)
wl_list_remove(&device->output_destroy_listener.link);

View File

@ -38,7 +38,8 @@
enum evdev_device_seat_capability {
EVDEV_SEAT_POINTER = (1 << 0),
EVDEV_SEAT_KEYBOARD = (1 << 1),
EVDEV_SEAT_TOUCH = (1 << 2)
EVDEV_SEAT_TOUCH = (1 << 2),
EVDEV_SEAT_TABLET = (1 << 3)
};
struct evdev_device {
@ -49,6 +50,7 @@ struct evdev_device {
struct wl_list link;
struct weston_output *output;
struct wl_listener output_destroy_listener;
struct weston_tablet *tablet;
char *output_name;
int fd;
bool override_wl_calibration;

View File

@ -227,7 +227,8 @@ lib_libinput_backend = static_library(
'libinput-backend',
[
'libinput-device.c',
'libinput-seat.c'
'libinput-seat.c',
tablet_unstable_v2_server_protocol_h
],
dependencies: [
dep_libweston_private,

View File

@ -139,7 +139,7 @@ endif
dep_wayland_server = dependency('wayland-server', version: '>= 1.20.0')
dep_wayland_client = dependency('wayland-client', version: '>= 1.20.0')
dep_pixman = dependency('pixman-1', version: '>= 0.25.2')
dep_libinput = dependency('libinput', version: '>= 0.8.0')
dep_libinput = dependency('libinput', version: '>= 1.2.0')
dep_libevdev = dependency('libevdev')
dep_libm = cc.find_library('m')
dep_libdl = cc.find_library('dl')