diff options
32 files changed, 6640 insertions, 23 deletions
diff --git a/configure.ac b/configure.ac index c841a5fa..4a997c90 100644 --- a/configure.ac +++ b/configure.ac @@ -189,6 +189,16 @@ if test "x$build_tests" = "xyes"; then AC_PATH_PROG(VALGRIND, [valgrind]) fi +AC_ARG_ENABLE(libwacom, + AS_HELP_STRING([--enable-libwacom], + [Use libwacom for tablet identification (default=enabled)]), + [use_libwacom="$enableval"], + [use_libwacom="yes"]) +if test "x$use_libwacom" = "xyes"; then + PKG_CHECK_MODULES(LIBWACOM, [libwacom >= 0.12], [HAVE_LIBWACOM="yes"]) + AC_DEFINE(HAVE_LIBWACOM, 1, [Build with libwacom]) +fi + AM_CONDITIONAL(HAVE_VALGRIND, [test "x$VALGRIND" != "x"]) AM_CONDITIONAL(BUILD_TESTS, [test "x$build_tests" = "xyes"]) AM_CONDITIONAL(BUILD_DOCS, [test "x$build_documentation" = "xyes"]) @@ -218,6 +228,7 @@ AC_MSG_RESULT([ Prefix ${prefix} udev base dir ${UDEV_DIR} + libwacom enabled ${use_libwacom} Build documentation ${build_documentation} Build tests ${build_tests} Tests use valgrind ${VALGRIND} diff --git a/doc/Makefile.am b/doc/Makefile.am index fe70f6a5..7a7c6cf1 100644 --- a/doc/Makefile.am +++ b/doc/Makefile.am @@ -23,6 +23,7 @@ header_files = \ $(srcdir)/scrolling.dox \ $(srcdir)/seats.dox \ $(srcdir)/t440-support.dox \ + $(srcdir)/tablet-support.dox \ $(srcdir)/tapping.dox \ $(srcdir)/test-suite.dox \ $(srcdir)/tools.dox \ diff --git a/doc/svg/tablet.svg b/doc/svg/tablet.svg new file mode 100644 index 00000000..7a9dda83 --- /dev/null +++ b/doc/svg/tablet.svg @@ -0,0 +1,199 @@ +<?xml version="1.0" encoding="UTF-8" standalone="no"?> +<!-- Created with Inkscape (http://www.inkscape.org/) --> + +<svg + xmlns:dc="http://purl.org/dc/elements/1.1/" + xmlns:cc="http://creativecommons.org/ns#" + xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" + xmlns:svg="http://www.w3.org/2000/svg" + xmlns="http://www.w3.org/2000/svg" + xmlns:xlink="http://www.w3.org/1999/xlink" + xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" + xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" + width="147.67236mm" + height="86.663628mm" + viewBox="0 0 523.24853 307.07585" + id="svg2" + version="1.1" + inkscape:version="0.91 r13725" + sodipodi:docname="tablet.svg"> + <defs + id="defs4"> + <linearGradient + inkscape:collect="always" + id="linearGradient4294"> + <stop + style="stop-color:#1a1a1a;stop-opacity:1;" + offset="0" + id="stop4296" /> + <stop + style="stop-color:#808080;stop-opacity:1" + offset="1" + id="stop4298" /> + </linearGradient> + <linearGradient + inkscape:collect="always" + xlink:href="#linearGradient4294" + id="linearGradient4300" + x1="465.81339" + y1="666.13727" + x2="454.82117" + y2="658.65521" + gradientUnits="userSpaceOnUse" /> + </defs> + <sodipodi:namedview + id="base" + pagecolor="#ffffff" + bordercolor="#666666" + borderopacity="1.0" + inkscape:pageopacity="0.0" + inkscape:pageshadow="2" + inkscape:zoom="0.98994949" + inkscape:cx="605.97861" + inkscape:cy="-35.324315" + inkscape:document-units="px" + inkscape:current-layer="layer3" + showgrid="false" + showguides="true" + inkscape:guide-bbox="true" + inkscape:window-width="1920" + inkscape:window-height="1136" + inkscape:window-x="0" + inkscape:window-y="27" + inkscape:window-maximized="1" + fit-margin-top="0" + fit-margin-left="0" + fit-margin-right="0" + fit-margin-bottom="0"> + <sodipodi:guide + position="127.77903,266.16996" + orientation="0,1" + id="guide4164" /> + <sodipodi:guide + position="125.25365,38.380555" + orientation="0,1" + id="guide4166" /> + </sodipodi:namedview> + <metadata + id="metadata7"> + <rdf:RDF> + <cc:Work + rdf:about=""> + <dc:format>image/svg+xml</dc:format> + <dc:type + rdf:resource="http://purl.org/dc/dcmitype/StillImage" /> + <dc:title></dc:title> + </cc:Work> + </rdf:RDF> + </metadata> + <g + inkscape:label="tablet" + inkscape:groupmode="layer" + id="layer1" + style="display:inline" + transform="translate(-75.261625,-133.63374)"> + <g + id="g4309" + inkscape:export-xdpi="90" + inkscape:export-ydpi="90"> + <rect + y="134.15933" + x="75.787216" + height="306.02466" + width="522.19733" + id="rect4136" + style="opacity:0.92000002;fill:#4d4d4d;fill-opacity:1;stroke:#4d4d4d;stroke-width:1.05118144;stroke-linecap:round;stroke-miterlimit:4;stroke-dasharray:4.20472551, 1.05118138;stroke-dashoffset:0;stroke-opacity:1" /> + <rect + style="opacity:0.92000002;fill:#4d4d4d;fill-opacity:1;stroke:#4d4d4d;stroke-width:0.74813837;stroke-linecap:round;stroke-miterlimit:4;stroke-dasharray:2.99255325, 0.74813831;stroke-dashoffset:0;stroke-opacity:1" + id="rect4140" + width="357.34042" + height="226.52563" + x="199.33878" + y="175.42407" /> + <rect + y="175.72914" + x="103.10225" + height="22.142857" + width="65" + id="rect4142" + style="opacity:0.92000002;fill:#4d4d4d;fill-opacity:1;stroke:#4d4d4d;stroke-width:0.98900002;stroke-linecap:round;stroke-miterlimit:4;stroke-dasharray:3.956, 0.989;stroke-dashoffset:0;stroke-opacity:1" /> + <rect + style="opacity:0.92000002;fill:#4d4d4d;fill-opacity:1;stroke:#4d4d4d;stroke-width:0.98900002;stroke-linecap:round;stroke-miterlimit:4;stroke-dasharray:3.956, 0.989;stroke-dashoffset:0;stroke-opacity:1" + id="rect4148" + width="65" + height="22.142857" + x="103.10225" + y="203.72914" /> + <rect + y="231.72913" + x="103.10225" + height="22.142857" + width="65" + id="rect4150" + style="opacity:0.92000002;fill:#4d4d4d;fill-opacity:1;stroke:#4d4d4d;stroke-width:0.98900002;stroke-linecap:round;stroke-miterlimit:4;stroke-dasharray:3.956, 0.989;stroke-dashoffset:0;stroke-opacity:1" /> + <rect + y="323.72913" + x="103.10225" + height="22.142857" + width="65" + id="rect4154" + style="opacity:0.92000002;fill:#4d4d4d;fill-opacity:1;stroke:#4d4d4d;stroke-width:0.98900002;stroke-linecap:round;stroke-miterlimit:4;stroke-dasharray:3.956, 0.989;stroke-dashoffset:0;stroke-opacity:1" /> + <rect + style="opacity:0.92000002;fill:#4d4d4d;fill-opacity:1;stroke:#4d4d4d;stroke-width:0.98900002;stroke-linecap:round;stroke-miterlimit:4;stroke-dasharray:3.956, 0.989;stroke-dashoffset:0;stroke-opacity:1" + id="rect4156" + width="65" + height="22.142857" + x="103.10225" + y="351.72913" /> + <circle + r="22.98097" + cy="287.06125" + cx="135.61298" + id="path4158" + style="opacity:0.92000002;fill:#4d4d4d;fill-opacity:1;stroke:#4d4d4d;stroke-width:0.98900002;stroke-linecap:round;stroke-miterlimit:4;stroke-dasharray:3.956, 0.989;stroke-dashoffset:0;stroke-opacity:1" /> + <ellipse + ry="12.608653" + rx="11.5985" + style="opacity:0.92000002;fill:#4d4d4d;fill-opacity:1;stroke:#4d4d4d;stroke-width:0.52043104;stroke-linecap:round;stroke-miterlimit:4;stroke-dasharray:2.08172421, 0.52043105;stroke-dashoffset:0;stroke-opacity:1" + id="circle4160" + cx="135.61298" + cy="287.06125" /> + <rect + y="379.72913" + x="103.10225" + height="22.142857" + width="65" + id="rect4162" + style="opacity:0.92000002;fill:#4d4d4d;fill-opacity:1;stroke:#4d4d4d;stroke-width:0.98900002;stroke-linecap:round;stroke-miterlimit:4;stroke-dasharray:3.956, 0.989;stroke-dashoffset:0;stroke-opacity:1" /> + </g> + </g> + <g + inkscape:groupmode="layer" + id="layer3" + inkscape:label="stylus" + style="display:inline" + transform="translate(-75.261625,-133.63374)"> + <g + id="g4304" + transform="matrix(0.37129971,0.09948946,-0.09618892,0.35898192,295.60339,7.6883643)"> + <path + sodipodi:nodetypes="czcc" + inkscape:connector-curvature="0" + id="path4286" + d="m 387.83544,799.76093 c -1.1128,3.61694 -3.2211,13.05163 -1.08543,14.07769 2.13567,1.02606 7.81039,-3.72162 10.99756,-6.69095 z" + style="display:inline;fill:#cccccc;fill-rule:evenodd;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" /> + <path + sodipodi:nodetypes="ssssccssscsssssssssssssssssss" + inkscape:connector-curvature="0" + id="path4283" + d="m 392.64431,804.79039 c -8.52094,-5.90399 -8.49394,-11.01546 0.22879,-43.30647 1.03999,-3.85 2.46829,-9.67602 3.17399,-12.9467 0.99731,-4.62219 2.39455,-7.29497 6.27321,-12 2.74456,-3.32932 5.25157,-6.2783 5.57113,-6.5533 40.78433,-60.97488 80.48307,-125.1652 118.27253,-184 9.86283,-15.675 26.59424,-42.225 37.18089,-59 10.58666,-16.775 34.01422,-53.9 52.06125,-82.5 18.04703,-28.6 35.04505,-55.31677 37.77338,-59.37059 l 4.9606,-7.3706 4.1828,0.57332 c 4.16371,0.5707 4.19706,0.54958 7.30887,-4.62941 3.75631,-6.2516 8.82067,-11.57582 12.2516,-12.88026 5.99391,-2.27888 14.03303,2.9506 14.03303,9.12854 0,3.90203 -2.51704,10.62127 -6.02878,16.09385 -1.63417,2.54664 -2.97122,4.85949 -2.97122,5.13969 0,0.28019 0.9,1.54715 2,2.81546 2.28453,2.63408 2.47267,4.21918 0.86833,7.31574 -1.28218,2.47476 -26.61383,45.18798 -55.85724,94.18426 -10.83283,18.15 -25.72943,43.1137 -33.10357,55.47489 -7.37413,12.3612 -13.69273,23.17153 -14.04131,24.02297 -0.34859,0.85144 -7.50972,12.78774 -15.91363,26.52511 -15.54138,25.40455 -32.24417,52.9052 -70.74345,116.47703 -40.26028,66.47968 -43.66308,72.46026 -49.21634,86.5 -1.74036,4.4 -3.92035,8.675 -4.8444,9.5 -0.92405,0.825 -4.36246,3.75 -7.6409,6.5 -3.27845,2.75 -9.57132,8.3067 -13.98415,12.34823 -10.62726,9.73304 -16.99729,13.87361 -22.52334,14.64034 -3.99187,0.55386 -5.03885,0.251 -9.27207,-2.6821 z" + style="display:inline;fill:#000000" /> + <path + sodipodi:nodetypes="scccs" + inkscape:connector-curvature="0" + id="path4292" + d="m 450.89044,688.88586 c 8.71518,5.62513 45.74035,-59.18436 43.57923,-75.43494 l -7.07107,-6.56599 c -29.93081,25.86352 -47.78438,74.72281 -47.78438,74.72281 0,0 0,0 11.27622,7.27812 z" + style="fill:url(#linearGradient4300);fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" /> + </g> + </g> +</svg> diff --git a/doc/tablet-support.dox b/doc/tablet-support.dox new file mode 100644 index 00000000..24d08d20 --- /dev/null +++ b/doc/tablet-support.dox @@ -0,0 +1,95 @@ +/** +@page tablet-support Tablet support + +This page provides details about the graphics tablet +support in libinput. Note that the term "tablet" in libinput refers to +graphics tablets only (e.g. Wacom Intuos), not to tablet devices like the +Apple iPad. + +@image html tablet.svg "Illustration of a graphics tablet" + +@section tablet-tools Tablet buttons vs. tablet tools + +Most tablets provide two types of devices. The pysical tablet often provides +a number of buttons and a touch ring or strip. Interaction on the drawing +surface of the tablet requires a tool, usually in the shape of a stylus. +The libinput interface exposed by devices with the @ref +LIBINPUT_DEVICE_CAP_TABLET_TOOL applies only to events generated by tools. + +Touch events on the tablet itself are exposed +through the @ref LIBINPUT_DEVICE_CAP_TOUCH capability and are often found on +a separate libinput device. See libinput_device_get_device_group() for +information on how to associate the touch part with other devices exposed by +the same physical hardware. + +@section tablet-tip Tool tip events vs. button events + +The primary use of a tablet tool is to draw on the surface of the tablet. +When the tool tip comes into contact with the surface, libinput sends an +event of type @ref LIBINPUT_EVENT_TABLET_TOOL_TIP, and again when the tip +ceases contact with the surface. + +Tablet tools may send button events; these are exclusively for extra buttons +unrelated to the tip. A button event is independent of the tip and occur +at any time. + +@section tablet-axes Special axes on tablet tools + +A tablet tool usually provides additional information beyond x/y positional +information and the tip state. A tool may provide the distance to the tablet +surface and the pressure exerted on the tip when in contact. Some tablets +additionally provide tilt information along the x and y axis. + +@image html tablet-axes.svg "Illustration of the distance, pressure and tilt axes" + +The granularity and precision of these axes varies between tablet devices +and cannot usually be mapped into a physical unit. +libinput normalizes distance and pressure into a fixed positive 2-byte +integer range. The tilt axes are normalized into a signed 2-byte integer +range. + +While the normalization range is identical for these axes, a caller should +not interpret identical values as identical across axes, i.e. a value V1 on +the distance axis has no relation to the same value V1 on the pressure axis. + +@section tablet-fake-proximity Handling of proximity events + +libinput's @ref LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY events represent the +physical proximity limits of the device. In some cases the caller should +emulate proximity based on the distance events. For example, the Wacom mouse +and lens cursor tools are usually used in relative mode, lying flat on the +tablet. A user typically expects that lifting the tool off the tablet to a +different location has the same effect as with a normal mouse. The proximity +detection on Wacom tablets however extends further than the user may lift +the mouse, i.e. the tool may not be lifted out of physical proximity. + +To enable normal use as a mouse it is recommended that the caller treats +proximity separate from libinput's proximity events. There is no simple way +to detect the proximity motion threshold, it is different on each tablet and +differs between tools. The recommended algorithm is to remember the minimum +distance value seen on the tool and assume a proximity out when the distance +exceeds a threshold above this minimum value. In pseudo-code: + +@code +const double threshold = ...; +static double min; +static bool in_proximity; + +double value; + +value = libinput_event_tablet_tool_get_axis_value(device, + LIBINPUT_TABLET_TOOL_AXIS_DISTANCE); + +if (value < min) { + min = value; + return; +} else if (in_proximity && + value > min + threshold) { + in_proximity = false; +} else if (!in_proximity && + value < min + threshold) { + in_proximity = true; +} +@endcode + +*/ diff --git a/src/Makefile.am b/src/Makefile.am index 90bdc9a8..343e75c7 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -18,6 +18,8 @@ libinput_la_SOURCES = \ evdev-mt-touchpad-buttons.c \ evdev-mt-touchpad-edge-scroll.c \ evdev-mt-touchpad-gestures.c \ + evdev-tablet.c \ + evdev-tablet.h \ filter.c \ filter.h \ filter-private.h \ @@ -32,12 +34,14 @@ libinput_la_SOURCES = \ libinput_la_LIBADD = $(MTDEV_LIBS) \ $(LIBUDEV_LIBS) \ $(LIBEVDEV_LIBS) \ + $(LIBWACOM_LIBS) \ libinput-util.la libinput_la_CFLAGS = -I$(top_srcdir)/include \ $(MTDEV_CFLAGS) \ $(LIBUDEV_CFLAGS) \ $(LIBEVDEV_CFLAGS) \ + $(LIBWACOM_CFLAGS) \ $(GCC_CFLAGS) EXTRA_libinput_la_DEPENDENCIES = $(srcdir)/libinput.sym diff --git a/src/evdev-mt-touchpad.c b/src/evdev-mt-touchpad.c index 8804658d..c90baeff 100644 --- a/src/evdev-mt-touchpad.c +++ b/src/evdev-mt-touchpad.c @@ -1415,6 +1415,7 @@ static struct evdev_dispatch_interface tp_interface = { tp_interface_device_removed, tp_interface_device_removed, /* device_suspended, treat as remove */ tp_interface_device_added, /* device_resumed, treat as add */ + NULL, /* post_added */ }; static void diff --git a/src/evdev-tablet.c b/src/evdev-tablet.c new file mode 100644 index 00000000..7e1c2d0f --- /dev/null +++ b/src/evdev-tablet.c @@ -0,0 +1,1157 @@ +/* + * Copyright © 2014 Red Hat, Inc. + * Copyright © 2014 Stephen Chandler "Lyude" Paul + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that the above copyright notice appear in all copies and that both that + * copyright notice and this permission notice appear in supporting + * documentation, and that the name of the copyright holders not be used in + * advertising or publicity pertaining to distribution of the software + * without specific, written prior permission. The copyright holders make + * no representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + * + * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS + * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY + * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER + * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF + * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ +#include "config.h" +#include "evdev-tablet.h" + +#include <assert.h> +#include <stdbool.h> +#include <string.h> + +#if HAVE_LIBWACOM +#include <libwacom/libwacom.h> +#endif + +#define tablet_set_status(tablet_,s_) (tablet_)->status |= (s_) +#define tablet_unset_status(tablet_,s_) (tablet_)->status &= ~(s_) +#define tablet_has_status(tablet_,s_) (!!((tablet_)->status & (s_))) + +static inline void +tablet_get_pressed_buttons(struct tablet_dispatch *tablet, + unsigned char *buttons, + unsigned int buttons_len) +{ + size_t i; + const struct button_state *state = &tablet->button_state, + *prev_state = &tablet->prev_button_state; + + assert(buttons_len <= ARRAY_LENGTH(state->stylus_buttons)); + + for (i = 0; i < buttons_len; i++) + buttons[i] = state->stylus_buttons[i] & + ~(prev_state->stylus_buttons[i]); +} + +static inline void +tablet_get_released_buttons(struct tablet_dispatch *tablet, + unsigned char *buttons, + unsigned int buttons_len) +{ + size_t i; + const struct button_state *state = &tablet->button_state, + *prev_state = &tablet->prev_button_state; + + assert(buttons_len <= ARRAY_LENGTH(state->stylus_buttons)); + + for (i = 0; i < buttons_len; i++) + buttons[i] = prev_state->stylus_buttons[i] & + ~(state->stylus_buttons[i]); +} + +static int +tablet_device_has_axis(struct tablet_dispatch *tablet, + enum libinput_tablet_tool_axis axis) +{ + struct libevdev *evdev = tablet->device->evdev; + bool has_axis = false; + unsigned int code; + + if (axis == LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z) { + has_axis = (libevdev_has_event_code(evdev, + EV_KEY, + BTN_TOOL_MOUSE) && + libevdev_has_event_code(evdev, + EV_ABS, + ABS_TILT_X) && + libevdev_has_event_code(evdev, + EV_ABS, + ABS_TILT_Y)); + code = axis_to_evcode(axis); + has_axis |= libevdev_has_event_code(evdev, + EV_ABS, + code); + } else if (axis == LIBINPUT_TABLET_TOOL_AXIS_REL_WHEEL) { + has_axis = libevdev_has_event_code(evdev, + EV_REL, + REL_WHEEL); + } else { + code = axis_to_evcode(axis); + has_axis = libevdev_has_event_code(evdev, + EV_ABS, + code); + } + + return has_axis; +} + +static void +tablet_process_absolute(struct tablet_dispatch *tablet, + struct evdev_device *device, + struct input_event *e, + uint64_t time) +{ + enum libinput_tablet_tool_axis axis; + + switch (e->code) { + case ABS_X: + case ABS_Y: + case ABS_Z: + case ABS_PRESSURE: + case ABS_TILT_X: + case ABS_TILT_Y: + case ABS_DISTANCE: + case ABS_WHEEL: + axis = evcode_to_axis(e->code); + if (axis == LIBINPUT_TABLET_TOOL_AXIS_NONE) { + log_bug_libinput(device->base.seat->libinput, + "Invalid ABS event code %#x\n", + e->code); + break; + } + + set_bit(tablet->changed_axes, axis); + tablet_set_status(tablet, TABLET_AXES_UPDATED); + break; + /* tool_id is the identifier for the tool we can use in libwacom + * to identify it (if we have one anyway) */ + case ABS_MISC: + tablet->current_tool_id = e->value; + break; + /* Intuos 3 strip data. Should only happen on the Pad device, not on + the Pen device. */ + case ABS_RX: + case ABS_RY: + /* Only on the 4D mouse (Intuos2), obsolete */ + case ABS_RZ: + /* Only on the 4D mouse (Intuos2), obsolete. + The 24HD sends ABS_THROTTLE on the Pad device for the second + wheel but we shouldn't get here on kernel >= 3.17. + */ + case ABS_THROTTLE: + default: + log_info(device->base.seat->libinput, + "Unhandled ABS event code %#x\n", e->code); + break; + } +} + +static void +tablet_mark_all_axes_changed(struct tablet_dispatch *tablet, + struct evdev_device *device) +{ + enum libinput_tablet_tool_axis a; + + for (a = LIBINPUT_TABLET_TOOL_AXIS_X; a <= LIBINPUT_TABLET_TOOL_AXIS_MAX; a++) { + if (tablet_device_has_axis(tablet, a)) + set_bit(tablet->changed_axes, a); + } + + tablet_set_status(tablet, TABLET_AXES_UPDATED); +} + +static void +tablet_change_to_left_handed(struct evdev_device *device) +{ + struct tablet_dispatch *tablet = + (struct tablet_dispatch*)device->dispatch; + + if (device->left_handed.enabled == device->left_handed.want_enabled) + return; + + if (!tablet_has_status(tablet, TABLET_TOOL_OUT_OF_PROXIMITY)) + return; + + device->left_handed.enabled = device->left_handed.want_enabled; +} + +static void +tablet_update_tool(struct tablet_dispatch *tablet, + struct evdev_device *device, + enum libinput_tablet_tool_type tool, + bool enabled) +{ + assert(tool != LIBINPUT_TOOL_NONE); + + if (enabled) { + tablet->current_tool_type = tool; + tablet_mark_all_axes_changed(tablet, device); + tablet_set_status(tablet, TABLET_TOOL_ENTERING_PROXIMITY); + tablet_unset_status(tablet, TABLET_TOOL_OUT_OF_PROXIMITY); + } + else if (!tablet_has_status(tablet, TABLET_TOOL_OUT_OF_PROXIMITY)) + tablet_set_status(tablet, TABLET_TOOL_LEAVING_PROXIMITY); +} + +static inline double +normalize_pressure_dist_slider(const struct input_absinfo *absinfo) +{ + double range = absinfo->maximum - absinfo->minimum; + double value = (absinfo->value - absinfo->minimum) / range; + + return value; +} + +static inline double +normalize_tilt(const struct input_absinfo *absinfo) +{ + double range = absinfo->maximum - absinfo->minimum; + double value = (absinfo->value - absinfo->minimum) / range; + + /* Map to the (-1, 1) range */ + return (value * 2) - 1; +} + +static inline int32_t +invert_axis(const struct input_absinfo *absinfo) +{ + return absinfo->maximum - (absinfo->value - absinfo->minimum); +} + +static void +convert_tilt_to_rotation(struct tablet_dispatch *tablet) +{ + const int offset = 5; + double x, y; + double angle = 0.0; + + /* Wacom Intuos 4, 5, Pro mouse calculates rotation from the x/y tilt + values. The device has a 175 degree CCW hardware offset but since we use + atan2 the effective offset is just 5 degrees. + */ + x = tablet->axes[LIBINPUT_TABLET_TOOL_AXIS_TILT_X]; + y = tablet->axes[LIBINPUT_TABLET_TOOL_AXIS_TILT_Y]; + clear_bit(tablet->changed_axes, LIBINPUT_TABLET_TOOL_AXIS_TILT_X); + clear_bit(tablet->changed_axes, LIBINPUT_TABLET_TOOL_AXIS_TILT_Y); + + /* atan2 is CCW, we want CW -> negate x */ + if (x || y) + angle = ((180.0 * atan2(-x, y)) / M_PI); + + angle = fmod(360 + angle - offset, 360); + + tablet->axes[LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z] = angle; + set_bit(tablet->changed_axes, LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z); +} + +static double +convert_to_degrees(const struct input_absinfo *absinfo, double offset) +{ + /* range is [0, 360[, i.e. range + 1 */ + double range = absinfo->maximum - absinfo->minimum + 1; + double value = (absinfo->value - absinfo->minimum) / range; + + return fmod(value * 360.0 + offset, 360.0); +} + +static inline double +normalize_wheel(struct tablet_dispatch *tablet, + int value) +{ + struct evdev_device *device = tablet->device; + + return value * device->scroll.wheel_click_angle; +} + +static inline double +guess_wheel_delta(double current, double old) +{ + double d1, d2, d3; + + d1 = current - old; + d2 = (current + 360.0) - old; + d3 = current - (old + 360.0); + + if (fabs(d2) < fabs(d1)) + d1 = d2; + + if (fabs(d3) < fabs(d1)) + d1 = d3; + + return d1; +} + +static inline double +get_delta(enum libinput_tablet_tool_axis axis, double current, double old) +{ + double delta = 0; + + switch (axis) { + case LIBINPUT_TABLET_TOOL_AXIS_X: + case LIBINPUT_TABLET_TOOL_AXIS_Y: + case LIBINPUT_TABLET_TOOL_AXIS_DISTANCE: + case LIBINPUT_TABLET_TOOL_AXIS_PRESSURE: + case LIBINPUT_TABLET_TOOL_AXIS_SLIDER: + case LIBINPUT_TABLET_TOOL_AXIS_TILT_X: + case LIBINPUT_TABLET_TOOL_AXIS_TILT_Y: + delta = current - old; + break; + case LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z: + delta = guess_wheel_delta(current, old); + break; + default: + abort(); + } + return delta; +} + +static void +tablet_check_notify_axes(struct tablet_dispatch *tablet, + struct evdev_device *device, + uint64_t time, + struct libinput_tablet_tool *tool) +{ + struct libinput_device *base = &device->base; + bool axis_update_needed = false; + int a; + double axes[LIBINPUT_TABLET_TOOL_AXIS_MAX + 1] = {0}; + double deltas[LIBINPUT_TABLET_TOOL_AXIS_MAX + 1] = {0}; + double deltas_discrete[LIBINPUT_TABLET_TOOL_AXIS_MAX + 1] = {0}; + double oldval; + + for (a = LIBINPUT_TABLET_TOOL_AXIS_X; a <= LIBINPUT_TABLET_TOOL_AXIS_MAX; a++) { + const struct input_absinfo *absinfo; + + if (!bit_is_set(tablet->changed_axes, a)) { + axes[a] = tablet->axes[a]; + continue; + } + + axis_update_needed = true; + oldval = tablet->axes[a]; + + /* ROTATION_Z is higher than TILT_X/Y so we know that the + tilt axes are already normalized and set */ + if (a == LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z && + (tablet->current_tool_type == LIBINPUT_TABLET_TOOL_TYPE_MOUSE || + tablet->current_tool_type == LIBINPUT_TABLET_TOOL_TYPE_LENS)) { + convert_tilt_to_rotation(tablet); + axes[LIBINPUT_TABLET_TOOL_AXIS_TILT_X] = 0; + axes[LIBINPUT_TABLET_TOOL_AXIS_TILT_Y] = 0; + axes[a] = tablet->axes[a]; + deltas[a] = get_delta(a, tablet->axes[a], oldval); + continue; + } else if (a == LIBINPUT_TABLET_TOOL_AXIS_REL_WHEEL) { + deltas_discrete[a] = tablet->deltas[a]; + deltas[a] = normalize_wheel(tablet, + tablet->deltas[a]); + axes[a] = 0; + continue; + } + + absinfo = libevdev_get_abs_info(device->evdev, + axis_to_evcode(a)); + + switch (a) { + case LIBINPUT_TABLET_TOOL_AXIS_X: + case LIBINPUT_TABLET_TOOL_AXIS_Y: + if (device->left_handed.enabled) + tablet->axes[a] = invert_axis(absinfo); + else + tablet->axes[a] = absinfo->value; + break; + case LIBINPUT_TABLET_TOOL_AXIS_DISTANCE: + case LIBINPUT_TABLET_TOOL_AXIS_PRESSURE: + case LIBINPUT_TABLET_TOOL_AXIS_SLIDER: + tablet->axes[a] = normalize_pressure_dist_slider(absinfo); + break; + case LIBINPUT_TABLET_TOOL_AXIS_TILT_X: + case LIBINPUT_TABLET_TOOL_AXIS_TILT_Y: + tablet->axes[a] = normalize_tilt(absinfo); + break; + case LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z: + /* artpen has 0 with buttons pointing east */ + tablet->axes[a] = convert_to_degrees(absinfo, 90); + break; + default: + log_bug_libinput(device->base.seat->libinput, + "Invalid axis update: %d\n", a); + break; + } + + axes[a] = tablet->axes[a]; + deltas[a] = get_delta(a, tablet->axes[a], oldval); + } + + /* We need to make sure that we check that the tool is not out of + * proximity before we send any axis updates. This is because many + * tablets will send axis events with incorrect values if the tablet + * tool is close enough so that the tablet can partially detect that + * it's there, but can't properly receive any data from the tool. */ + if (axis_update_needed && + !tablet_has_status(tablet, TABLET_TOOL_OUT_OF_PROXIMITY) && + !tablet_has_status(tablet, TABLET_TOOL_LEAVING_PROXIMITY)) { + if (tablet_has_status(tablet, TABLET_TOOL_ENTERING_PROXIMITY)) + tablet_notify_proximity(&device->base, + time, + tool, + LIBINPUT_TABLET_TOOL_PROXIMITY_IN, + tablet->changed_axes, + axes); + else { + enum libinput_tablet_tool_tip_state tip_state; + + tip_state = tablet_has_status(tablet, TABLET_TOOL_IN_CONTACT) ? + LIBINPUT_TABLET_TOOL_TIP_DOWN : LIBINPUT_TABLET_TOOL_TIP_UP; + tablet_notify_axis(base, + time, + tool, + tip_state, + tablet->changed_axes, + axes, + deltas, + deltas_discrete); + } + } + + memset(tablet->changed_axes, 0, sizeof(tablet->changed_axes)); +} + +static void +tablet_update_button(struct tablet_dispatch *tablet, + uint32_t evcode, + uint32_t enable) +{ + switch (evcode) { + case BTN_TOUCH: + return; + case BTN_LEFT: + case BTN_RIGHT: + case BTN_MIDDLE: + case BTN_SIDE: + case BTN_EXTRA: + case BTN_FORWARD: + case BTN_BACK: + case BTN_TASK: + case BTN_STYLUS: + case BTN_STYLUS2: + break; + default: + log_info(tablet->device->base.seat->libinput, + "Unhandled button %s (%#x)\n", + libevdev_event_code_get_name(EV_KEY, evcode), evcode); + return; + } + + if (enable) { + set_bit(tablet->button_state.stylus_buttons, evcode); + tablet_set_status(tablet, TABLET_BUTTONS_PRESSED); + } else { + clear_bit(tablet->button_state.stylus_buttons, evcode); + tablet_set_status(tablet, TABLET_BUTTONS_RELEASED); + } +} + +static inline enum libinput_tablet_tool_type +tablet_evcode_to_tool(int code) +{ + enum libinput_tablet_tool_type type; + + switch (code) { + case BTN_TOOL_PEN: type = LIBINPUT_TABLET_TOOL_TYPE_PEN; break; + case BTN_TOOL_RUBBER: type = LIBINPUT_TABLET_TOOL_TYPE_ERASER; break; + case BTN_TOOL_BRUSH: type = LIBINPUT_TABLET_TOOL_TYPE_BRUSH; break; + case BTN_TOOL_PENCIL: type = LIBINPUT_TABLET_TOOL_TYPE_PENCIL; break; + case BTN_TOOL_AIRBRUSH: type = LIBINPUT_TABLET_TOOL_TYPE_AIRBRUSH; break; + case BTN_TOOL_FINGER: type = LIBINPUT_TABLET_TOOL_TYPE_FINGER; break; + case BTN_TOOL_MOUSE: type = LIBINPUT_TABLET_TOOL_TYPE_MOUSE; break; + case BTN_TOOL_LENS: type = LIBINPUT_TABLET_TOOL_TYPE_LENS; break; + default: + abort(); + } + + return type; +} + +static void +tablet_process_key(struct tablet_dispatch *tablet, + struct evdev_device *device, + struct input_event *e, + uint64_t time) +{ + switch (e->code) { + case BTN_TOOL_PEN: + case BTN_TOOL_RUBBER: + case BTN_TOOL_BRUSH: + case BTN_TOOL_PENCIL: + case BTN_TOOL_AIRBRUSH: + case BTN_TOOL_FINGER: + case BTN_TOOL_MOUSE: + case BTN_TOOL_LENS: + tablet_update_tool(tablet, + device, + tablet_evcode_to_tool(e->code), + e->value); + break; + case BTN_TOUCH: + if (e->value) + tablet_set_status(tablet, TABLET_TOOL_ENTERING_CONTACT); + else + tablet_set_status(tablet, TABLET_TOOL_LEAVING_CONTACT); + break; + case BTN_LEFT: + case BTN_RIGHT: + case BTN_MIDDLE: + case BTN_SIDE: + case BTN_EXTRA: + case BTN_FORWARD: + case BTN_BACK: + case BTN_TASK: + case BTN_STYLUS: + case BTN_STYLUS2: + default: + tablet_update_button(tablet, e->code, e->value); + break; + } +} + +static void +tablet_process_relative(struct tablet_dispatch *tablet, + struct evdev_device *device, + struct input_event *e, + uint64_t time) +{ + enum libinput_tablet_tool_axis axis; + + switch (e->code) { + case REL_WHEEL: + axis = rel_evcode_to_axis(e->code); + if (axis == LIBINPUT_TABLET_TOOL_AXIS_NONE) { + log_bug_libinput(device->base.seat->libinput, + "Invalid ABS event code %#x\n", + e->code); + break; + } + set_bit(tablet->changed_axes, axis); + tablet->deltas[axis] = -1 * e->value; + tablet_set_status(tablet, TABLET_AXES_UPDATED); + break; + default: + log_info(tablet->device->base.seat->libinput, + "Unhandled relative axis %s (%#x)\n", + libevdev_event_code_get_name(EV_REL, e->code), + e->code); + return; + } +} + +static void +tablet_process_misc(struct tablet_dispatch *tablet, + struct evdev_device *device, + struct input_event *e, + uint64_t time) +{ + switch (e->code) { + case MSC_SERIAL: + if (e->value != -1) + tablet->current_tool_serial = e->value; + + break; + default: + log_info(device->base.seat->libinput, + "Unhandled MSC event code %s (%#x)\n", + libevdev_event_code_get_name(EV_MSC, e->code), + e->code); + break; + } +} + +static inline void +copy_axis_cap(const struct tablet_dispatch *tablet, + struct libinput_tablet_tool *tool, + enum libinput_tablet_tool_axis axis) +{ + if (bit_is_set(tablet->axis_caps, axis)) + set_bit(tool->axis_caps, axis); +} + +static inline void +copy_button_cap(const struct tablet_dispatch *tablet, + struct libinput_tablet_tool *tool, + uint32_t button) +{ + struct libevdev *evdev = tablet->device->evdev; + if (libevdev_has_event_code(evdev, EV_KEY, button)) + set_bit(tool->buttons, button); +} + +static inline int +tool_set_bits_from_libwacom(const struct tablet_dispatch *tablet, + struct libinput_tablet_tool *tool) +{ + int rc = 1; + +#if HAVE_LIBWACOM + struct libinput *libinput = tablet->device->base.seat->libinput; + WacomDeviceDatabase *db; + const WacomStylus *s = NULL; + int code; + WacomStylusType type; + WacomAxisTypeFlags axes; + + db = libwacom_database_new(); + if (!db) { + log_info(libinput, + "Failed to initialize libwacom context.\n"); + goto out; + } + s = libwacom_stylus_get_for_id(db, tool->tool_id); + if (!s) + goto out; + + type = libwacom_stylus_get_type(s); + if (type == WSTYLUS_PUCK) { + for (code = BTN_LEFT; + code < BTN_LEFT + libwacom_stylus_get_num_buttons(s); + code++) + copy_button_cap(tablet, tool, code); + } else { + if (libwacom_stylus_get_num_buttons(s) >= 2) + copy_button_cap(tablet, tool, BTN_STYLUS2); + if (libwacom_stylus_get_num_buttons(s) >= 1) + copy_button_cap(tablet, tool, BTN_STYLUS); + } + + if (libwacom_stylus_has_wheel(s)) + copy_axis_cap(tablet, tool, LIBINPUT_TABLET_TOOL_AXIS_REL_WHEEL); + + axes = libwacom_stylus_get_axes(s); + + if (axes & WACOM_AXIS_TYPE_TILT) { + /* tilt on the puck is converted to rotation */ + if (type == WSTYLUS_PUCK) { + set_bit(tool->axis_caps, + LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z); + } else { + copy_axis_cap(tablet, + tool, + LIBINPUT_TABLET_TOOL_AXIS_TILT_X); + copy_axis_cap(tablet, + tool, + LIBINPUT_TABLET_TOOL_AXIS_TILT_Y); + } + } + if (axes & WACOM_AXIS_TYPE_ROTATION_Z) + copy_axis_cap(tablet, tool, LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z); + if (axes & WACOM_AXIS_TYPE_DISTANCE) + copy_axis_cap(tablet, tool, LIBINPUT_TABLET_TOOL_AXIS_DISTANCE); + if (axes & WACOM_AXIS_TYPE_SLIDER) + copy_axis_cap(tablet, tool, LIBINPUT_TABLET_TOOL_AXIS_SLIDER); + if (axes & WACOM_AXIS_TYPE_PRESSURE) + copy_axis_cap(tablet, tool, LIBINPUT_TABLET_TOOL_AXIS_PRESSURE); + + rc = 0; +out: + if (db) + libwacom_database_destroy(db); +#endif + return rc; +} + +static void +tool_set_bits(const struct tablet_dispatch *tablet, + struct libinput_tablet_tool *tool) +{ + enum libinput_tablet_tool_type type = tool->type; + +#if HAVE_LIBWACOM + if (tool_set_bits_from_libwacom(tablet, tool) == 0) + return; +#endif + /* If we don't have libwacom, we simply copy any axis we have on the + tablet onto the tool. Except we know that mice only have rotation + anyway. + */ + switch (type) { + case LIBINPUT_TABLET_TOOL_TYPE_PEN: + case LIBINPUT_TABLET_TOOL_TYPE_ERASER: + case LIBINPUT_TABLET_TOOL_TYPE_PENCIL: + case LIBINPUT_TABLET_TOOL_TYPE_BRUSH: + case LIBINPUT_TABLET_TOOL_TYPE_AIRBRUSH: + copy_axis_cap(tablet, tool, LIBINPUT_TABLET_TOOL_AXIS_PRESSURE); + copy_axis_cap(tablet, tool, LIBINPUT_TABLET_TOOL_AXIS_DISTANCE); + copy_axis_cap(tablet, tool, LIBINPUT_TABLET_TOOL_AXIS_TILT_X); + copy_axis_cap(tablet, tool, LIBINPUT_TABLET_TOOL_AXIS_TILT_Y); + copy_axis_cap(tablet, tool, LIBINPUT_TABLET_TOOL_AXIS_SLIDER); + copy_axis_cap(tablet, tool, LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z); + break; + case LIBINPUT_TABLET_TOOL_TYPE_MOUSE: + case LIBINPUT_TABLET_TOOL_TYPE_LENS: + copy_axis_cap(tablet, tool, LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z); + copy_axis_cap(tablet, tool, LIBINPUT_TABLET_TOOL_AXIS_REL_WHEEL); + break; + default: + break; + } + + /* If we don't have libwacom, copy all pen-related ones from the + tablet vs all mouse-related ones */ + switch (type) { + case LIBINPUT_TABLET_TOOL_TYPE_PEN: + case LIBINPUT_TABLET_TOOL_TYPE_BRUSH: + case LIBINPUT_TABLET_TOOL_TYPE_AIRBRUSH: + case LIBINPUT_TABLET_TOOL_TYPE_PENCIL: + case LIBINPUT_TABLET_TOOL_TYPE_ERASER: + copy_button_cap(tablet, tool, BTN_STYLUS); + copy_button_cap(tablet, tool, BTN_STYLUS2); + break; + case LIBINPUT_TABLET_TOOL_TYPE_MOUSE: + case LIBINPUT_TABLET_TOOL_TYPE_LENS: + copy_button_cap(tablet, tool, BTN_LEFT); + copy_button_cap(tablet, tool, BTN_MIDDLE); + copy_button_cap(tablet, tool, BTN_RIGHT); + copy_button_cap(tablet, tool, BTN_SIDE); + copy_button_cap(tablet, tool, BTN_EXTRA); + break; + default: + break; + } +} + +static struct libinput_tablet_tool * +tablet_get_tool(struct tablet_dispatch *tablet, + enum libinput_tablet_tool_type type, + uint32_t tool_id, + uint32_t serial) +{ + struct libinput_tablet_tool *tool = NULL, *t; + struct list *tool_list; + + if (serial) { + tool_list = &tablet->device->base.seat->libinput->tool_list; + + /* Check if we already have the tool in our list of tools */ + list_for_each(t, tool_list, link) { + if (type == t->type && serial == t->serial) { + tool = t; + break; + } + } + } else { + /* We can't guarantee that tools without serial numbers are + * unique, so we keep them local to the tablet that they come + * into proximity of instead of storing them in the global tool + * list */ + tool_list = &tablet->tool_list; + + /* Same as above, but don't bother checking the serial number */ + list_for_each(t, tool_list, link) { + if (type == t->type) { + tool = t; + break; + } + } + } + + /* If we didn't already have the new_tool in our list of tools, + * add it */ + if (!tool) { + tool = zalloc(sizeof *tool); + *tool = (struct libinput_tablet_tool) { + .type = type, + .serial = serial, + .tool_id = tool_id, + .refcount = 1, + }; + + tool_set_bits(tablet, tool); + + list_insert(tool_list, &tool->link); + } + + return tool; +} + +static void +tablet_notify_button_mask(struct tablet_dispatch *tablet, + struct evdev_device *device, + uint64_t time, + struct libinput_tablet_tool *tool, + const unsigned char *buttons, + unsigned int buttons_len, + enum libinput_button_state state) +{ + struct libinput_device *base = &device->base; + size_t i; + size_t nbits = 8 * sizeof(buttons[0]) * buttons_len; + enum libinput_tablet_tool_tip_state tip_state; + + tip_state = tablet_has_status(tablet, TABLET_TOOL_IN_CONTACT) ? + LIBINPUT_TABLET_TOOL_TIP_DOWN : LIBINPUT_TABLET_TOOL_TIP_UP; + + for (i = 0; i < nbits; i++) { + if (!bit_is_set(buttons, i)) + continue; + + tablet_notify_button(base, + time, + tool, + tip_state, + tablet->axes, + i, + state); + } +} + +static void +tablet_notify_buttons(struct tablet_dispatch *tablet, + struct evdev_device *device, + uint64_t time, + struct libinput_tablet_tool *tool, + enum libinput_button_state state) +{ + unsigned char buttons[ARRAY_LENGTH(tablet->button_state.stylus_buttons)]; + + if (state == LIBINPUT_BUTTON_STATE_PRESSED) + tablet_get_pressed_buttons(tablet, buttons, sizeof(buttons)); + else + tablet_get_released_buttons(tablet, + buttons, + sizeof(buttons)); + + tablet_notify_button_mask(tablet, + device, + time, + tool, + buttons, + sizeof(buttons), + state); +} + +static void +sanitize_tablet_axes(struct tablet_dispatch *tablet) +{ + const struct input_absinfo *distance, + *pressure; + + distance = libevdev_get_abs_info(tablet->device->evdev, ABS_DISTANCE); + pressure = libevdev_get_abs_info(tablet->device->evdev, ABS_PRESSURE); + + /* Keep distance and pressure mutually exclusive */ + if (bit_is_set(tablet->changed_axes, LIBINPUT_TABLET_TOOL_AXIS_DISTANCE) && + distance->value > distance->minimum && + pressure->value > pressure->minimum) { + clear_bit(tablet->changed_axes, LIBINPUT_TABLET_TOOL_AXIS_DISTANCE); + tablet->axes[LIBINPUT_TABLET_TOOL_AXIS_DISTANCE] = 0; + } else if (bit_is_set(tablet->changed_axes, LIBINPUT_TABLET_TOOL_AXIS_PRESSURE) && + (!tablet_has_status(tablet, TABLET_TOOL_IN_CONTACT) && + !tablet_has_status(tablet, TABLET_TOOL_ENTERING_CONTACT))) { + /* Make sure that the last axis value sent to the caller is a 0 */ + if (tablet->axes[LIBINPUT_TABLET_TOOL_AXIS_PRESSURE] == 0) + clear_bit(tablet->changed_axes, + LIBINPUT_TABLET_TOOL_AXIS_PRESSURE); + else + tablet->axes[LIBINPUT_TABLET_TOOL_AXIS_PRESSURE] = 0; + } + + /* If we have a mouse/lens cursor and the tilt changed, the rotation + changed. Mark this, calculate the angle later */ + if ((tablet->current_tool_type == LIBINPUT_TABLET_TOOL_TYPE_MOUSE || + tablet->current_tool_type == LIBINPUT_TABLET_TOOL_TYPE_LENS) && + (bit_is_set(tablet->changed_axes, LIBINPUT_TABLET_TOOL_AXIS_TILT_X) || + bit_is_set(tablet->changed_axes, LIBINPUT_TABLET_TOOL_AXIS_TILT_Y))) + set_bit(tablet->changed_axes, LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z); +} + +static void +tablet_flush(struct tablet_dispatch *tablet, + struct evdev_device *device, + uint64_t time) +{ + struct libinput_tablet_tool *tool = + tablet_get_tool(tablet, + tablet->current_tool_type, + tablet->current_tool_id, + tablet->current_tool_serial); + + if (tablet_has_status(tablet, TABLET_TOOL_OUT_OF_PROXIMITY)) + return; + + if (tablet_has_status(tablet, TABLET_TOOL_LEAVING_PROXIMITY)) { + /* Release all stylus buttons */ + memset(tablet->button_state.stylus_buttons, + 0, + sizeof(tablet->button_state.stylus_buttons)); + tablet_set_status(tablet, TABLET_BUTTONS_RELEASED); + if (tablet_has_status(tablet, TABLET_TOOL_IN_CONTACT)) + tablet_set_status(tablet, TABLET_TOOL_LEAVING_CONTACT); + } else if (tablet_has_status(tablet, TABLET_AXES_UPDATED) || + tablet_has_status(tablet, TABLET_TOOL_ENTERING_PROXIMITY)) { + sanitize_tablet_axes(tablet); + tablet_check_notify_axes(tablet, device, time, tool); + + tablet_unset_status(tablet, TABLET_TOOL_ENTERING_PROXIMITY); + tablet_unset_status(tablet, TABLET_AXES_UPDATED); + } + + if (tablet_has_status(tablet, TABLET_TOOL_ENTERING_CONTACT)) { + tablet_notify_tip(&device->base, + time, + tool, + LIBINPUT_TABLET_TOOL_TIP_DOWN, + tablet->axes); + tablet_unset_status(tablet, TABLET_TOOL_ENTERING_CONTACT); + tablet_set_status(tablet, TABLET_TOOL_IN_CONTACT); + } + + if (tablet_has_status(tablet, TABLET_BUTTONS_RELEASED)) { + tablet_notify_buttons(tablet, + device, + time, + tool, + LIBINPUT_BUTTON_STATE_RELEASED); + tablet_unset_status(tablet, TABLET_BUTTONS_RELEASED); + } + + if (tablet_has_status(tablet, TABLET_BUTTONS_PRESSED)) { + tablet_notify_buttons(tablet, + device, + time, + tool, + LIBINPUT_BUTTON_STATE_PRESSED); + tablet_unset_status(tablet, TABLET_BUTTONS_PRESSED); + } + + if (tablet_has_status(tablet, TABLET_TOOL_LEAVING_CONTACT)) { + tablet_notify_tip(&device->base, + time, + tool, + LIBINPUT_TABLET_TOOL_TIP_UP, + tablet->axes); + tablet_unset_status(tablet, TABLET_TOOL_LEAVING_CONTACT); + tablet_unset_status(tablet, TABLET_TOOL_IN_CONTACT); + } + + if (tablet_has_status(tablet, TABLET_TOOL_LEAVING_PROXIMITY)) { + memset(tablet->changed_axes, 0, sizeof(tablet->changed_axes)); + tablet_notify_proximity(&device->base, + time, + tool, + LIBINPUT_TABLET_TOOL_PROXIMITY_OUT, + tablet->changed_axes, + tablet->axes); + + tablet_set_status(tablet, TABLET_TOOL_OUT_OF_PROXIMITY); + tablet_unset_status(tablet, TABLET_TOOL_LEAVING_PROXIMITY); + + tablet_change_to_left_handed(device); + } +} + +static inline void +tablet_reset_state(struct tablet_dispatch *tablet) +{ + /* Update state */ + memcpy(&tablet->prev_button_state, + &tablet->button_state, + sizeof(tablet->button_state)); +} + +static void +tablet_process(struct evdev_dispatch *dispatch, + struct evdev_device *device, + struct input_event *e, + uint64_t time) +{ + struct tablet_dispatch *tablet = + (struct tablet_dispatch *)dispatch; + + switch (e->type) { + case EV_ABS: + tablet_process_absolute(tablet, device, e, time); + break; + case EV_REL: + tablet_process_relative(tablet, device, e, time); + break; + case EV_KEY: + tablet_process_key(tablet, device, e, time); + break; + case EV_MSC: + tablet_process_misc(tablet, device, e, time); + break; + case EV_SYN: + tablet_flush(tablet, device, time); + tablet_reset_state(tablet); + break; + default: + log_error(device->base.seat->libinput, + "Unexpected event type %s (%#x)\n", + libevdev_event_type_get_name(e->type), + e->type); + break; + } +} + +static void +tablet_destroy(struct evdev_dispatch *dispatch) +{ + struct tablet_dispatch *tablet = + (struct tablet_dispatch*)dispatch; + struct libinput_tablet_tool *tool, *tmp; + + list_for_each_safe(tool, tmp, &tablet->tool_list, link) { + libinput_tablet_tool_unref(tool); + } + + free(tablet); +} + +static void +tablet_check_initial_proximity(struct evdev_device *device, + struct evdev_dispatch *dispatch) +{ + bool tool_in_prox = false; + int code, state; + enum libinput_tablet_tool_type tool; + struct tablet_dispatch *tablet = (struct tablet_dispatch*)dispatch; + + for (tool = LIBINPUT_TABLET_TOOL_TYPE_PEN; tool <= LIBINPUT_TABLET_TOOL_TYPE_MAX; tool++) { + code = tablet_tool_to_evcode(tool); + + /* we only expect one tool to be in proximity at a time */ + if (libevdev_fetch_event_value(device->evdev, + EV_KEY, + code, + &state) && state) { + tool_in_prox = true; + break; + } + } + + if (!tool_in_prox) + return; + + tablet_update_tool(tablet, device, tool, state); + + tablet->current_tool_id = + libevdev_get_event_value(device->evdev, + EV_ABS, + ABS_MISC); + tablet->current_tool_serial = + libevdev_get_event_value(device->evdev, + EV_MSC, + MSC_SERIAL); + + tablet_flush(tablet, + device, + libinput_now(device->base.seat->libinput)); +} + +static struct evdev_dispatch_interface tablet_interface = { + tablet_process, + NULL, /* suspend */ + NULL, /* remove */ + tablet_destroy, + NULL, /* device_added */ + NULL, /* device_removed */ + NULL, /* device_suspended */ + NULL, /* device_resumed */ + tablet_check_initial_proximity, +}; + +static int +tablet_init(struct tablet_dispatch *tablet, + struct evdev_device *device) +{ + enum libinput_tablet_tool_axis axis; + + tablet->base.interface = &tablet_interface; + tablet->device = device; + tablet->status = TABLET_NONE; + tablet->current_tool_type = LIBINPUT_TOOL_NONE; + list_init(&tablet->tool_list); + + for (axis = LIBINPUT_TABLET_TOOL_AXIS_X; + axis <= LIBINPUT_TABLET_TOOL_AXIS_MAX; + axis++) { + if (tablet_device_has_axis(tablet, axis)) + set_bit(tablet->axis_caps, axis); + } + + tablet_mark_all_axes_changed(tablet, device); + + tablet_set_status(tablet, TABLET_TOOL_OUT_OF_PROXIMITY); + + return 0; +} + +static void +tablet_init_left_handed(struct evdev_device *device) +{ +#if HAVE_LIBWACOM + struct libinput *libinput = device->base.seat->libinput; + WacomDeviceDatabase *db; + WacomDevice *d = NULL; + WacomError *error; + const char *devnode; + + db = libwacom_database_new(); + if (!db) { + log_info(libinput, + "Failed to initialize libwacom context.\n"); + return; + } + error = libwacom_error_new(); + devnode = udev_device_get_devnode(device->udev_device); + + d = libwacom_new_from_path(db, + devnode, + WFALLBACK_NONE, + error); + + if (d) { + if (libwacom_is_reversible(d)) + evdev_init_left_handed(device, + tablet_change_to_left_handed); + } else if (libwacom_error_get_code(error) == WERROR_UNKNOWN_MODEL) { + log_info(libinput, "Tablet unknown to libwacom\n"); + } else { + log_error(libinput, + "libwacom error: %s\n", + libwacom_error_get_message(error)); + } + + if (error) + libwacom_error_free(&error); + if (d) + libwacom_destroy(d); + libwacom_database_destroy(db); +#endif +} + +struct evdev_dispatch * +evdev_tablet_create(struct evdev_device *device) +{ + struct tablet_dispatch *tablet; + + tablet = zalloc(sizeof *tablet); + if (!tablet) + return NULL; + + if (tablet_init(tablet, device) != 0) { + tablet_destroy(&tablet->base); + return NULL; + } + + tablet_init_left_handed(device); + + return &tablet->base; +} diff --git a/src/evdev-tablet.h b/src/evdev-tablet.h new file mode 100644 index 00000000..aafeda9d --- /dev/null +++ b/src/evdev-tablet.h @@ -0,0 +1,182 @@ +/* + * Copyright © 2014 Red Hat, Inc. + * Copyright © 2014 Stephen Chandler "Lyude" Paul + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that the above copyright notice appear in all copies and that both that + * copyright notice and this permission notice appear in supporting + * documentation, and that the name of the copyright holders not be used in + * advertising or publicity pertaining to distribution of the software + * without specific, written prior permission. The copyright holders make + * no representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + * + * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS + * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY + * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER + * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF + * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef EVDEV_TABLET_H +#define EVDEV_TABLET_H + +#include "evdev.h" + +#define LIBINPUT_TABLET_TOOL_AXIS_NONE 0 +#define LIBINPUT_TOOL_NONE 0 +#define LIBINPUT_TABLET_TOOL_TYPE_MAX LIBINPUT_TABLET_TOOL_TYPE_LENS + +enum tablet_status { + TABLET_NONE = 0, + TABLET_AXES_UPDATED = 1 << 0, + TABLET_BUTTONS_PRESSED = 1 << 1, + TABLET_BUTTONS_RELEASED = 1 << 2, + TABLET_TOOL_IN_CONTACT = 1 << 3, + TABLET_TOOL_LEAVING_PROXIMITY = 1 << 4, + TABLET_TOOL_OUT_OF_PROXIMITY = 1 << 5, + TABLET_TOOL_ENTERING_PROXIMITY = 1 << 6, + TABLET_TOOL_ENTERING_CONTACT = 1 << 7, + TABLET_TOOL_LEAVING_CONTACT = 1 << 8, +}; + +struct button_state { + unsigned char stylus_buttons[NCHARS(KEY_CNT)]; +}; + +struct tablet_dispatch { + struct evdev_dispatch base; + struct evdev_device *device; + unsigned int status; + unsigned char changed_axes[NCHARS(LIBINPUT_TABLET_TOOL_AXIS_MAX + 1)]; + double axes[LIBINPUT_TABLET_TOOL_AXIS_MAX + 1]; + double deltas[LIBINPUT_TABLET_TOOL_AXIS_MAX + 1]; + unsigned char axis_caps[NCHARS(LIBINPUT_TABLET_TOOL_AXIS_MAX + 1)]; + + /* Only used for tablets that don't report serial numbers */ + struct list tool_list; + + struct button_state button_state; + struct button_state prev_button_state; + + enum libinput_tablet_tool_type current_tool_type; + uint32_t current_tool_id; + uint32_t current_tool_serial; +}; + +static inline enum libinput_tablet_tool_axis +evcode_to_axis(const uint32_t evcode) +{ + enum libinput_tablet_tool_axis axis; + + switch (evcode) { + case ABS_X: + axis = LIBINPUT_TABLET_TOOL_AXIS_X; + break; + case ABS_Y: + axis = LIBINPUT_TABLET_TOOL_AXIS_Y; + break; + case ABS_Z: + axis = LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z; + break; + case ABS_DISTANCE: + axis = LIBINPUT_TABLET_TOOL_AXIS_DISTANCE; + break; + case ABS_PRESSURE: + axis = LIBINPUT_TABLET_TOOL_AXIS_PRESSURE; + break; + case ABS_TILT_X: + axis = LIBINPUT_TABLET_TOOL_AXIS_TILT_X; + break; + case ABS_TILT_Y: + axis = LIBINPUT_TABLET_TOOL_AXIS_TILT_Y; + break; + case ABS_WHEEL: + axis = LIBINPUT_TABLET_TOOL_AXIS_SLIDER; + break; + default: + axis = LIBINPUT_TABLET_TOOL_AXIS_NONE; + break; + } + + return axis; +} + +static inline enum libinput_tablet_tool_axis +rel_evcode_to_axis(const uint32_t evcode) +{ + enum libinput_tablet_tool_axis axis; + + switch (evcode) { + case REL_WHEEL: + axis = LIBINPUT_TABLET_TOOL_AXIS_REL_WHEEL; + break; + default: + axis = LIBINPUT_TABLET_TOOL_AXIS_NONE; + break; + } + + return axis; +} + +static inline uint32_t +axis_to_evcode(const enum libinput_tablet_tool_axis axis) +{ + uint32_t evcode; + + switch (axis) { + case LIBINPUT_TABLET_TOOL_AXIS_X: + evcode = ABS_X; + break; + case LIBINPUT_TABLET_TOOL_AXIS_Y: + evcode = ABS_Y; + break; + case LIBINPUT_TABLET_TOOL_AXIS_DISTANCE: + evcode = ABS_DISTANCE; + break; + case LIBINPUT_TABLET_TOOL_AXIS_PRESSURE: + evcode = ABS_PRESSURE; + break; + case LIBINPUT_TABLET_TOOL_AXIS_TILT_X: + evcode = ABS_TILT_X; + break; + case LIBINPUT_TABLET_TOOL_AXIS_TILT_Y: + evcode = ABS_TILT_Y; + break; + case LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z: + evcode = ABS_Z; + break; + case LIBINPUT_TABLET_TOOL_AXIS_SLIDER: + evcode = ABS_WHEEL; + break; + default: + abort(); + } + + return evcode; +} + +static inline int +tablet_tool_to_evcode(enum libinput_tablet_tool_type type) +{ + int code; + + switch (type) { + case LIBINPUT_TABLET_TOOL_TYPE_PEN: code = BTN_TOOL_PEN; break; + case LIBINPUT_TABLET_TOOL_TYPE_ERASER: code = BTN_TOOL_RUBBER; break; + case LIBINPUT_TABLET_TOOL_TYPE_BRUSH: code = BTN_TOOL_BRUSH; break; + case LIBINPUT_TABLET_TOOL_TYPE_PENCIL: code = BTN_TOOL_PENCIL; break; + case LIBINPUT_TABLET_TOOL_TYPE_AIRBRUSH: code = BTN_TOOL_AIRBRUSH; break; + case LIBINPUT_TABLET_TOOL_TYPE_FINGER: code = BTN_TOOL_FINGER; break; + case LIBINPUT_TABLET_TOOL_TYPE_MOUSE: code = BTN_TOOL_MOUSE; break; + case LIBINPUT_TABLET_TOOL_TYPE_LENS: code = BTN_TOOL_LENS; break; + default: + abort(); + } + + return code; +} +#endif diff --git a/src/evdev.c b/src/evdev.c index ec3abc6c..d56a4702 100644 --- a/src/evdev.c +++ b/src/evdev.c @@ -948,6 +948,7 @@ struct evdev_dispatch_interface fallback_interface = { NULL, /* device_removed */ NULL, /* device_suspended */ NULL, /* device_resumed */ + NULL, /* post_added */ }; static uint32_t @@ -1969,6 +1970,7 @@ evdev_configure_device(struct evdev_device *device) struct libevdev *evdev = device->evdev; const char *devnode = udev_device_get_devnode(device->udev_device); enum evdev_device_udev_tags udev_tags; + unsigned int tablet_tags; udev_tags = evdev_device_get_udev_tags(device, device->udev_device); @@ -2046,6 +2048,21 @@ evdev_configure_device(struct evdev_device *device) } } + /* libwacom assigns touchpad (or touchscreen) _and_ tablet to the + tablet touch bits, so make sure we don't initialize the tablet + interface for the touch device */ + tablet_tags = EVDEV_UDEV_TAG_TABLET | + EVDEV_UDEV_TAG_TOUCHPAD | + EVDEV_UDEV_TAG_TOUCHSCREEN; + if ((udev_tags & tablet_tags) == EVDEV_UDEV_TAG_TABLET) { + device->dispatch = evdev_tablet_create(device); + device->seat_caps |= EVDEV_DEVICE_TABLET; + log_info(libinput, + "input device '%s', %s is a tablet\n", + device->devname, devnode); + return device->dispatch == NULL ? -1 : 0; + } + if (udev_tags & EVDEV_UDEV_TAG_TOUCHPAD) { device->dispatch = evdev_mt_touchpad_create(device); log_info(libinput, @@ -2136,6 +2153,10 @@ evdev_notify_added_device(struct evdev_device *device) } notify_added_device(&device->base); + + if (device->dispatch->interface->post_added) + device->dispatch->interface->post_added(device, + device->dispatch); } static bool @@ -2412,6 +2433,8 @@ evdev_device_has_capability(struct evdev_device *device, return !!(device->seat_caps & EVDEV_DEVICE_TOUCH); case LIBINPUT_DEVICE_CAP_GESTURE: return !!(device->seat_caps & EVDEV_DEVICE_GESTURE); + case LIBINPUT_DEVICE_CAP_TABLET_TOOL: + return !!(device->seat_caps & EVDEV_DEVICE_TABLET); default: return 0; } diff --git a/src/evdev.h b/src/evdev.h index e44a65de..1a02963b 100644 --- a/src/evdev.h +++ b/src/evdev.h @@ -60,6 +60,7 @@ enum evdev_device_seat_capability { EVDEV_DEVICE_POINTER = (1 << 0), EVDEV_DEVICE_KEYBOARD = (1 << 1), EVDEV_DEVICE_TOUCH = (1 << 2), + EVDEV_DEVICE_TABLET = (1 << 3), EVDEV_DEVICE_GESTURE = (1 << 5), }; @@ -264,6 +265,11 @@ struct evdev_dispatch_interface { /* A device was resumed */ void (*device_resumed)(struct evdev_device *device, struct evdev_device *resumed_device); + + /* Called immediately after the LIBINPUT_EVENT_DEVICE_ADDED event + * was sent */ + void (*post_added)(struct evdev_device *device, + struct evdev_dispatch *dispatch); }; struct evdev_dispatch { @@ -290,6 +296,9 @@ evdev_touchpad_create(struct evdev_device *device); struct evdev_dispatch * evdev_mt_touchpad_create(struct evdev_device *device); +struct evdev_dispatch * +evdev_tablet_create(struct evdev_device *device); + void evdev_tag_touchpad(struct evdev_device *device, struct udev_device *udev_device); diff --git a/src/libinput-private.h b/src/libinput-private.h index e146c260..38a14b8f 100644 --- a/src/libinput-private.h +++ b/src/libinput-private.h @@ -33,6 +33,8 @@ #include "libinput.h" #include "libinput-util.h" +#define LIBINPUT_TABLET_TOOL_AXIS_MAX LIBINPUT_TABLET_TOOL_AXIS_REL_WHEEL + struct libinput_source; /* A coordinate pair in device coordinates */ @@ -84,6 +86,8 @@ struct libinput { size_t events_in; size_t events_out; + struct list tool_list; + const struct libinput_interface *interface; const struct libinput_interface_backend *interface_backend; @@ -246,6 +250,17 @@ struct libinput_device { struct libinput_device_config config; }; +struct libinput_tablet_tool { + struct list link; + uint32_t serial; + uint32_t tool_id; + enum libinput_tablet_tool_type type; + unsigned char axis_caps[NCHARS(LIBINPUT_TABLET_TOOL_AXIS_MAX + 1)]; + unsigned char buttons[NCHARS(KEY_MAX) + 1]; + int refcount; + void *user_data; +}; + struct libinput_event { enum libinput_event_type type; struct libinput_device *device; @@ -444,6 +459,40 @@ void touch_notify_frame(struct libinput_device *device, uint64_t time); +void +tablet_notify_axis(struct libinput_device *device, + uint64_t time, + struct libinput_tablet_tool *tool, + enum libinput_tablet_tool_tip_state tip_state, + unsigned char *changed_axes, + double *axes, + double *deltas, + double *deltas_discrete); + +void +tablet_notify_proximity(struct libinput_device *device, + uint64_t time, + struct libinput_tablet_tool *tool, + enum libinput_tablet_tool_proximity_state state, + unsigned char *changed_axes, + double *axes); + +void +tablet_notify_tip(struct libinput_device *device, + uint64_t time, + struct libinput_tablet_tool *tool, + enum libinput_tablet_tool_tip_state tip_state, + double *axes); + +void +tablet_notify_button(struct libinput_device *device, + uint64_t time, + struct libinput_tablet_tool *tool, + enum libinput_tablet_tool_tip_state tip_state, + double *axes, + int32_t button, + enum libinput_button_state state); + static inline uint64_t libinput_now(struct libinput *libinput) { diff --git a/src/libinput-util.h b/src/libinput-util.h index ba253b57..53fe85fe 100644 --- a/src/libinput-util.h +++ b/src/libinput-util.h @@ -95,6 +95,8 @@ int list_empty(const struct list *list); #define streq(s1, s2) (strcmp((s1), (s2)) == 0) #define strneq(s1, s2, n) (strncmp((s1), (s2), (n)) == 0) +#define NCHARS(x) ((size_t)(((x) + 7) / 8)) + #ifdef DEBUG_TRACE #define debug_trace(...) \ do { \ @@ -104,6 +106,7 @@ int list_empty(const struct list *list); #else #define debug_trace(...) { } #endif + #define LIBINPUT_EXPORT __attribute__ ((visibility("default"))) static inline void * @@ -112,6 +115,28 @@ zalloc(size_t size) return calloc(1, size); } +/* This bitfield helper implementation is taken from from libevdev-util.h, + * except that it has been modified to work with arrays of unsigned chars + */ + +static inline int +bit_is_set(const unsigned char *array, int bit) +{ + return !!(array[bit / 8] & (1 << (bit % 8))); +} + +static inline void +set_bit(unsigned char *array, int bit) +{ + array[bit / 8] |= (1 << (bit % 8)); +} + +static inline void +clear_bit(unsigned char *array, int bit) +{ + array[bit / 8] &= ~(1 << (bit % 8)); +} + static inline void msleep(unsigned int ms) { diff --git a/src/libinput.c b/src/libinput.c index 24f2b693..278ca850 100644 --- a/src/libinput.c +++ b/src/libinput.c @@ -125,6 +125,21 @@ struct libinput_event_gesture { double angle; }; +struct libinput_event_tablet_tool { + struct libinput_event base; + uint32_t button; + enum libinput_button_state state; + uint32_t seat_button_count; + uint64_t time; + double axes[LIBINPUT_TABLET_TOOL_AXIS_MAX + 1]; + double deltas[LIBINPUT_TABLET_TOOL_AXIS_MAX + 1]; + double deltas_discrete[LIBINPUT_TABLET_TOOL_AXIS_MAX + 1]; + unsigned char changed_axes[NCHARS(LIBINPUT_TABLET_TOOL_AXIS_MAX + 1)]; + struct libinput_tablet_tool *tool; + enum libinput_tablet_tool_proximity_state proximity_state; + enum libinput_tablet_tool_tip_state tip_state; +}; + static void libinput_default_log_func(struct libinput *libinput, enum libinput_log_priority priority, @@ -272,7 +287,6 @@ libinput_event_get_touch_event(struct libinput_event *event) LIBINPUT_EVENT_TOUCH_MOTION, LIBINPUT_EVENT_TOUCH_CANCEL, LIBINPUT_EVENT_TOUCH_FRAME); - return (struct libinput_event_touch *) event; } @@ -292,6 +306,20 @@ libinput_event_get_gesture_event(struct libinput_event *event) return (struct libinput_event_gesture *) event; } +LIBINPUT_EXPORT struct libinput_event_tablet_tool * +libinput_event_get_tablet_tool_event(struct libinput_event *event) +{ + require_event_type(libinput_event_get_context(event), + event->type, + NULL, + LIBINPUT_EVENT_TABLET_TOOL_AXIS, + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY, + LIBINPUT_EVENT_TABLET_TOOL_TIP, + LIBINPUT_EVENT_TABLET_TOOL_BUTTON); + + return (struct libinput_event_tablet_tool *) event; +} + LIBINPUT_EXPORT struct libinput_event_device_notify * libinput_event_get_device_notify_event(struct libinput_event *event) { @@ -884,6 +912,327 @@ libinput_event_gesture_get_angle_delta(struct libinput_event_gesture *event) return event->angle; } +LIBINPUT_EXPORT int +libinput_event_tablet_tool_axis_has_changed(struct libinput_event_tablet_tool *event, + enum libinput_tablet_tool_axis axis) +{ + require_event_type(libinput_event_get_context(&event->base), + event->base.type, + 0, + LIBINPUT_EVENT_TABLET_TOOL_AXIS, + LIBINPUT_EVENT_TABLET_TOOL_TIP, + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY); + + return (NCHARS(axis) <= sizeof(event->changed_axes)) ? + bit_is_set(event->changed_axes, axis) : 0; +} + +LIBINPUT_EXPORT double +libinput_event_tablet_tool_get_axis_value(struct libinput_event_tablet_tool *event, + enum libinput_tablet_tool_axis axis) +{ + struct evdev_device *device = + (struct evdev_device *) event->base.device; + + require_event_type(libinput_event_get_context(&event->base), + event->base.type, + 0, + LIBINPUT_EVENT_TABLET_TOOL_AXIS, + LIBINPUT_EVENT_TABLET_TOOL_TIP, + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY); + + switch(axis) { + case LIBINPUT_TABLET_TOOL_AXIS_X: + return evdev_convert_to_mm(device->abs.absinfo_x, + event->axes[axis]); + case LIBINPUT_TABLET_TOOL_AXIS_Y: + return evdev_convert_to_mm(device->abs.absinfo_y, + event->axes[axis]); + case LIBINPUT_TABLET_TOOL_AXIS_DISTANCE: + case LIBINPUT_TABLET_TOOL_AXIS_PRESSURE: + case LIBINPUT_TABLET_TOOL_AXIS_TILT_X: + case LIBINPUT_TABLET_TOOL_AXIS_TILT_Y: + case LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z: + case LIBINPUT_TABLET_TOOL_AXIS_SLIDER: + case LIBINPUT_TABLET_TOOL_AXIS_REL_WHEEL: + return event->axes[axis]; + default: + return 0; + } +} + +LIBINPUT_EXPORT double +libinput_event_tablet_tool_get_axis_delta(struct libinput_event_tablet_tool *event, + enum libinput_tablet_tool_axis axis) +{ + struct evdev_device *device = + (struct evdev_device *) event->base.device; + + require_event_type(libinput_event_get_context(&event->base), + event->base.type, + 0, + LIBINPUT_EVENT_TABLET_TOOL_AXIS, + LIBINPUT_EVENT_TABLET_TOOL_TIP, + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY); + + switch(axis) { + case LIBINPUT_TABLET_TOOL_AXIS_X: + return evdev_convert_to_mm(device->abs.absinfo_x, + event->deltas[axis]); + case LIBINPUT_TABLET_TOOL_AXIS_Y: + return evdev_convert_to_mm(device->abs.absinfo_y, + event->deltas[axis]); + case LIBINPUT_TABLET_TOOL_AXIS_DISTANCE: + case LIBINPUT_TABLET_TOOL_AXIS_PRESSURE: + case LIBINPUT_TABLET_TOOL_AXIS_TILT_X: + case LIBINPUT_TABLET_TOOL_AXIS_TILT_Y: + case LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z: + case LIBINPUT_TABLET_TOOL_AXIS_SLIDER: + case LIBINPUT_TABLET_TOOL_AXIS_REL_WHEEL: + return event->deltas[axis]; + default: + return 0; + } +} + +LIBINPUT_EXPORT double +libinput_event_tablet_tool_get_axis_delta_discrete( + struct libinput_event_tablet_tool *event, + enum libinput_tablet_tool_axis axis) +{ + require_event_type(libinput_event_get_context(&event->base), + event->base.type, + 0, + LIBINPUT_EVENT_TABLET_TOOL_AXIS, + LIBINPUT_EVENT_TABLET_TOOL_TIP, + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY); + + switch(axis) { + case LIBINPUT_TABLET_TOOL_AXIS_X: + case LIBINPUT_TABLET_TOOL_AXIS_Y: + case LIBINPUT_TABLET_TOOL_AXIS_DISTANCE: + case LIBINPUT_TABLET_TOOL_AXIS_PRESSURE: + case LIBINPUT_TABLET_TOOL_AXIS_TILT_X: + case LIBINPUT_TABLET_TOOL_AXIS_TILT_Y: + case LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z: + case LIBINPUT_TABLET_TOOL_AXIS_SLIDER: + case LIBINPUT_TABLET_TOOL_AXIS_REL_WHEEL: + return event->deltas_discrete[axis]; + default: + return 0; + } +} + +LIBINPUT_EXPORT double +libinput_event_tablet_tool_get_x_transformed(struct libinput_event_tablet_tool *event, + uint32_t width) +{ + struct evdev_device *device = + (struct evdev_device *) event->base.device; + + require_event_type(libinput_event_get_context(&event->base), + event->base.type, + 0, + LIBINPUT_EVENT_TABLET_TOOL_AXIS, + LIBINPUT_EVENT_TABLET_TOOL_TIP, + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY); + + return evdev_device_transform_x(device, + event->axes[LIBINPUT_TABLET_TOOL_AXIS_X], + width); +} + +LIBINPUT_EXPORT double +libinput_event_tablet_tool_get_y_transformed(struct libinput_event_tablet_tool *event, + uint32_t height) +{ + struct evdev_device *device = + (struct evdev_device *) event->base.device; + + require_event_type(libinput_event_get_context(&event->base), + event->base.type, + 0, + LIBINPUT_EVENT_TABLET_TOOL_AXIS, + LIBINPUT_EVENT_TABLET_TOOL_TIP, + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY); + + return evdev_device_transform_y(device, + event->axes[LIBINPUT_TABLET_TOOL_AXIS_Y], + height); +} + +LIBINPUT_EXPORT struct libinput_tablet_tool * +libinput_event_tablet_tool_get_tool(struct libinput_event_tablet_tool *event) +{ + require_event_type(libinput_event_get_context(&event->base), + event->base.type, + 0, + LIBINPUT_EVENT_TABLET_TOOL_AXIS, + LIBINPUT_EVENT_TABLET_TOOL_TIP, + LIBINPUT_EVENT_TABLET_TOOL_BUTTON, + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY); + + return event->tool; +} + +LIBINPUT_EXPORT enum libinput_tablet_tool_proximity_state +libinput_event_tablet_tool_get_proximity_state(struct libinput_event_tablet_tool *event) +{ + require_event_type(libinput_event_get_context(&event->base), + event->base.type, + 0, + LIBINPUT_EVENT_TABLET_TOOL_AXIS, + LIBINPUT_EVENT_TABLET_TOOL_TIP, + LIBINPUT_EVENT_TABLET_TOOL_BUTTON, + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY); + + return event->proximity_state; +} + +LIBINPUT_EXPORT enum libinput_tablet_tool_tip_state +libinput_event_tablet_tool_get_tip_state(struct libinput_event_tablet_tool *event) +{ + require_event_type(libinput_event_get_context(&event->base), + event->base.type, + 0, + LIBINPUT_EVENT_TABLET_TOOL_AXIS, + LIBINPUT_EVENT_TABLET_TOOL_TIP, + LIBINPUT_EVENT_TABLET_TOOL_BUTTON, + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY); + + return event->tip_state; +} + +LIBINPUT_EXPORT uint32_t +libinput_event_tablet_tool_get_time(struct libinput_event_tablet_tool *event) +{ + require_event_type(libinput_event_get_context(&event->base), + event->base.type, + 0, + LIBINPUT_EVENT_TABLET_TOOL_AXIS, + LIBINPUT_EVENT_TABLET_TOOL_TIP, + LIBINPUT_EVENT_TABLET_TOOL_BUTTON, + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY); + + return us2ms(event->time); +} + +LIBINPUT_EXPORT uint64_t +libinput_event_tablet_tool_get_time_usec(struct libinput_event_tablet_tool *event) +{ + require_event_type(libinput_event_get_context(&event->base), + event->base.type, + 0, + LIBINPUT_EVENT_TABLET_TOOL_AXIS, + LIBINPUT_EVENT_TABLET_TOOL_TIP, + LIBINPUT_EVENT_TABLET_TOOL_BUTTON, + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY); + + return event->time; +} + +LIBINPUT_EXPORT uint32_t +libinput_event_tablet_tool_get_button(struct libinput_event_tablet_tool *event) +{ + require_event_type(libinput_event_get_context(&event->base), + event->base.type, + 0, + LIBINPUT_EVENT_TABLET_TOOL_BUTTON); + + return event->button; +} + +LIBINPUT_EXPORT enum libinput_button_state +libinput_event_tablet_tool_get_button_state(struct libinput_event_tablet_tool *event) +{ + require_event_type(libinput_event_get_context(&event->base), + event->base.type, + 0, + LIBINPUT_EVENT_TABLET_TOOL_BUTTON); + + return event->state; +} + +LIBINPUT_EXPORT uint32_t +libinput_event_tablet_tool_get_seat_button_count(struct libinput_event_tablet_tool *event) +{ + require_event_type(libinput_event_get_context(&event->base), + event->base.type, + 0, + LIBINPUT_EVENT_TABLET_TOOL_BUTTON); + + return event->seat_button_count; +} + +LIBINPUT_EXPORT enum libinput_tablet_tool_type +libinput_tablet_tool_get_type(struct libinput_tablet_tool *tool) +{ + return tool->type; +} + +LIBINPUT_EXPORT uint64_t +libinput_tablet_tool_get_tool_id(struct libinput_tablet_tool *tool) +{ + return tool->tool_id; +} + +LIBINPUT_EXPORT uint64_t +libinput_tablet_tool_get_serial(struct libinput_tablet_tool *tool) +{ + return tool->serial; +} + +LIBINPUT_EXPORT int +libinput_tablet_tool_has_axis(struct libinput_tablet_tool *tool, + enum libinput_tablet_tool_axis axis) +{ + return bit_is_set(tool->axis_caps, axis); +} + +LIBINPUT_EXPORT int +libinput_tablet_tool_has_button(struct libinput_tablet_tool *tool, + uint32_t code) +{ + if (NCHARS(code) > sizeof(tool->buttons)) + return 0; + + return bit_is_set(tool->buttons, code); +} + +LIBINPUT_EXPORT void +libinput_tablet_tool_set_user_data(struct libinput_tablet_tool *tool, + void *user_data) +{ + tool->user_data = user_data; +} + +LIBINPUT_EXPORT void * +libinput_tablet_tool_get_user_data(struct libinput_tablet_tool *tool) +{ + return tool->user_data; +} + +LIBINPUT_EXPORT struct libinput_tablet_tool * +libinput_tablet_tool_ref(struct libinput_tablet_tool *tool) +{ + tool->refcount++; + return tool; +} + +LIBINPUT_EXPORT struct libinput_tablet_tool * +libinput_tablet_tool_unref(struct libinput_tablet_tool *tool) +{ + assert(tool->refcount > 0); + + tool->refcount--; + if (tool->refcount > 0) + return tool; + + list_remove(&tool->link); + free(tool); + return NULL; +} + struct libinput_source * libinput_add_fd(struct libinput *libinput, int fd, @@ -948,6 +1297,7 @@ libinput_init(struct libinput *libinput, list_init(&libinput->source_destroy_list); list_init(&libinput->seat_list); list_init(&libinput->device_group_list); + list_init(&libinput->tool_list); if (libinput_timer_subsys_init(libinput) != 0) { free(libinput->events); @@ -987,6 +1337,7 @@ libinput_unref(struct libinput *libinput) struct libinput_event *event; struct libinput_device *device, *next_device; struct libinput_seat *seat, *next_seat; + struct libinput_tablet_tool *tool, *next_tool; struct libinput_device_group *group, *next_group; if (libinput == NULL) @@ -1022,6 +1373,10 @@ libinput_unref(struct libinput *libinput) libinput_device_group_destroy(group); } + list_for_each_safe(tool, next_tool, &libinput->tool_list, link) { + libinput_tablet_tool_unref(tool); + } + libinput_timer_subsys_destroy(libinput); libinput_drop_destroyed_sources(libinput); close(libinput->epoll_fd); @@ -1355,6 +1710,9 @@ device_has_cap(struct libinput_device *device, case LIBINPUT_DEVICE_CAP_GESTURE: capability = "CAP_GESTURE"; break; + case LIBINPUT_DEVICE_CAP_TABLET_TOOL: + capability = "CAP_TABLET"; + break; } log_bug_libinput(device->seat->libinput, @@ -1611,6 +1969,144 @@ touch_notify_frame(struct libinput_device *device, &touch_event->base); } +void +tablet_notify_axis(struct libinput_device *device, + uint64_t time, + struct libinput_tablet_tool *tool, + enum libinput_tablet_tool_tip_state tip_state, + unsigned char *changed_axes, + double *axes, + double *deltas, + double *deltas_discrete) +{ + struct libinput_event_tablet_tool *axis_event; + + axis_event = zalloc(sizeof *axis_event); + if (!axis_event) + return; + + *axis_event = (struct libinput_event_tablet_tool) { + .time = time, + .tool = tool, + .proximity_state = LIBINPUT_TABLET_TOOL_PROXIMITY_IN, + .tip_state = tip_state, + }; + + memcpy(axis_event->changed_axes, + changed_axes, + sizeof(axis_event->changed_axes)); + memcpy(axis_event->axes, axes, sizeof(axis_event->axes)); + memcpy(axis_event->deltas, deltas, sizeof(axis_event->deltas)); + memcpy(axis_event->deltas_discrete, + deltas_discrete, + sizeof(axis_event->deltas_discrete)); + + post_device_event(device, + time, + LIBINPUT_EVENT_TABLET_TOOL_AXIS, + &axis_event->base); +} + +void +tablet_notify_proximity(struct libinput_device *device, + uint64_t time, + struct libinput_tablet_tool *tool, + enum libinput_tablet_tool_proximity_state proximity_state, + unsigned char *changed_axes, + double *axes) +{ + struct libinput_event_tablet_tool *proximity_event; + + proximity_event = zalloc(sizeof *proximity_event); + if (!proximity_event) + return; + + *proximity_event = (struct libinput_event_tablet_tool) { + .time = time, + .tool = tool, + .proximity_state = proximity_state, + }; + memcpy(proximity_event->axes, + axes, + sizeof(proximity_event->axes)); + memcpy(proximity_event->changed_axes, + changed_axes, + sizeof(proximity_event->changed_axes)); + + /* deltas are always 0 on prox-in/out */ + + post_device_event(device, + time, + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY, + &proximity_event->base); +} + +void +tablet_notify_tip(struct libinput_device *device, + uint64_t time, + struct libinput_tablet_tool *tool, + enum libinput_tablet_tool_tip_state tip_state, + double *axes) +{ + struct libinput_event_tablet_tool *tip_event; + + tip_event = zalloc(sizeof *tip_event); + if (!tip_event) + return; + + *tip_event = (struct libinput_event_tablet_tool) { + .time = time, + .tool = tool, + .tip_state = tip_state, + .proximity_state = LIBINPUT_TABLET_TOOL_PROXIMITY_IN, + }; + memcpy(tip_event->axes, + axes, + sizeof(tip_event->axes)); + + post_device_event(device, + time, + LIBINPUT_EVENT_TABLET_TOOL_TIP, + &tip_event->base); +} + +void +tablet_notify_button(struct libinput_device *device, + uint64_t time, + struct libinput_tablet_tool *tool, + enum libinput_tablet_tool_tip_state tip_state, + double *axes, + int32_t button, + enum libinput_button_state state) +{ + struct libinput_event_tablet_tool *button_event; + int32_t seat_button_count; + + button_event = zalloc(sizeof *button_event); + if (!button_event) + return; + + seat_button_count = update_seat_button_count(device->seat, + button, + state); + + *button_event = (struct libinput_event_tablet_tool) { + .time = time, + .tool = tool, + .button = button, + .state = state, + .seat_button_count = seat_button_count, + .proximity_state = LIBINPUT_TABLET_TOOL_PROXIMITY_IN, + .tip_state = tip_state, + }; + memcpy(button_event->axes, axes, sizeof(button_event->axes)); + + post_device_event(device, + time, + LIBINPUT_EVENT_TABLET_TOOL_BUTTON, + &button_event->base); +} + static void gesture_notify(struct libinput_device *device, uint64_t time, @@ -1966,6 +2462,19 @@ libinput_event_gesture_get_base_event(struct libinput_event_gesture *event) return &event->base; } +LIBINPUT_EXPORT struct libinput_event * +libinput_event_tablet_tool_get_base_event(struct libinput_event_tablet_tool *event) +{ + require_event_type(libinput_event_get_context(&event->base), + event->base.type, + NULL, + LIBINPUT_EVENT_TABLET_TOOL_AXIS, + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY, + LIBINPUT_EVENT_TABLET_TOOL_BUTTON); + + return &event->base; +} + LIBINPUT_EXPORT struct libinput_device_group * libinput_device_group_ref(struct libinput_device_group *group) { diff --git a/src/libinput.h b/src/libinput.h index 90574463..a9eee3cc 100644 --- a/src/libinput.h +++ b/src/libinput.h @@ -58,6 +58,7 @@ enum libinput_device_capability { LIBINPUT_DEVICE_CAP_KEYBOARD = 0, LIBINPUT_DEVICE_CAP_POINTER = 1, LIBINPUT_DEVICE_CAP_TOUCH = 2, + LIBINPUT_DEVICE_CAP_TABLET_TOOL = 3, LIBINPUT_DEVICE_CAP_GESTURE = 5, }; @@ -134,6 +135,105 @@ enum libinput_pointer_axis_source { }; /** + * @ingroup device + * + * Available axis types for a device. It must have the @ref + * LIBINPUT_DEVICE_CAP_TABLET_TOOL capability. + * + * For details on the interpretation of these axes see + * libinput_event_tablet_tool_get_axis_value(). + */ +enum libinput_tablet_tool_axis { + LIBINPUT_TABLET_TOOL_AXIS_X = 1, + LIBINPUT_TABLET_TOOL_AXIS_Y = 2, + LIBINPUT_TABLET_TOOL_AXIS_DISTANCE = 3, + LIBINPUT_TABLET_TOOL_AXIS_PRESSURE = 4, + LIBINPUT_TABLET_TOOL_AXIS_TILT_X = 5, + LIBINPUT_TABLET_TOOL_AXIS_TILT_Y = 6, + LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z = 7, + LIBINPUT_TABLET_TOOL_AXIS_SLIDER = 8, + LIBINPUT_TABLET_TOOL_AXIS_REL_WHEEL = 9, +}; + +/** + * @ingroup device + * + * An object representing a tool being used by a device with the @ref + * LIBINPUT_DEVICE_CAP_TABLET_TOOL capability. + * + * Tablet events generated by such a device are bound to a specific tool + * rather than coming from the device directly. Depending on the hardware it + * is possible to track the same physical tool across multiple + * struct libinput_device devices. + */ +struct libinput_tablet_tool; + +/** + * @ingroup device + * + * Available tool types for a device with the @ref + * LIBINPUT_DEVICE_CAP_TABLET_TOOL capability. The tool type defines the default + * usage of the tool as advertised by the manufacturer. Multiple different + * physical tools may share the same tool type, e.g. a Wacom Classic Pen, + * Wacom Pro Pen and a Wacom Grip Pen are all of type @ref + * LIBINPUT_TABLET_TOOL_TYPE_PEN. + * Use libinput_tablet_tool_get_tool_id() to get a specific model where applicable. + * + * Note that on some device, the eraser tool is on the tail end of a pen + * device. On other devices, e.g. MS Surface 3, the eraser is the pen tip + * while a button is held down. + * + * @note The @ref libinput_tablet_tool_type can only describe the default physical + * type of the device. For devices with adjustible physical properties + * the tool type remains the same, i.e. putting a Wacom stroke nib into a + * classic pen leaves the tool type as @ref LIBINPUT_TABLET_TOOL_TYPE_PEN. + */ +enum libinput_tablet_tool_type { + LIBINPUT_TABLET_TOOL_TYPE_PEN = 1, /**< A generic pen */ + LIBINPUT_TABLET_TOOL_TYPE_ERASER, /**< Eraser */ + LIBINPUT_TABLET_TOOL_TYPE_BRUSH, /**< A paintbrush-like tool */ + LIBINPUT_TABLET_TOOL_TYPE_PENCIL, /**< Physical drawing tool, e.g. + Wacom Inking Pen */ + LIBINPUT_TABLET_TOOL_TYPE_AIRBRUSH, /**< An airbrush-like tool */ + LIBINPUT_TABLET_TOOL_TYPE_FINGER, /**< Touch */ + LIBINPUT_TABLET_TOOL_TYPE_MOUSE, /**< A mouse bound to the tablet */ + LIBINPUT_TABLET_TOOL_TYPE_LENS, /**< A mouse tool with a lens */ +}; + +/** + * @ingroup device + * + * The state of proximity for a tool on a device. The device must have the @ref + * LIBINPUT_DEVICE_CAP_TABLET_TOOL capability. + * + * The proximity of a tool is a binary state signalling whether the tool is + * within detectable distance of the tablet device. A tool that is out of + * proximity cannot generate events. + * + * On some hardware a tool goes out of proximity when it ceases to touch the + * surface. On other hardware, the tool is still detectable within a short + * distance (a few cm) off the surface. + */ +enum libinput_tablet_tool_proximity_state { + LIBINPUT_TABLET_TOOL_PROXIMITY_OUT = 0, + LIBINPUT_TABLET_TOOL_PROXIMITY_IN = 1, +}; + +/** + * @ingroup device + * + * The tip contact state for a tool on a device. The device must have + * the @ref LIBINPUT_DEVICE_CAP_TABLET_TOOL capability. + * + * The tip contact state of a tool is a binary state signalling whether the tool is + * touching the surface of the tablet device. + */ +enum libinput_tablet_tool_tip_state { + LIBINPUT_TABLET_TOOL_TIP_UP = 0, + LIBINPUT_TABLET_TOOL_TIP_DOWN = 1, +}; + +/** * @ingroup base * * Event type for events returned by libinput_get_event(). @@ -179,6 +279,60 @@ enum libinput_event_type { */ LIBINPUT_EVENT_TOUCH_FRAME, + /** + * One or more axes have changed state on a device with the @ref + * LIBINPUT_DEVICE_CAP_TABLET_TOOL capability. This event is only sent + * when the tool is in proximity, see @ref + * LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY for details. + * + * The proximity event contains the initial state of the axis as the + * tool comes into proximity. An event of type @ref + * LIBINPUT_EVENT_TABLET_TOOL_AXIS is only sent when an axis value + * changes from this initial state. It is possible for a tool to + * enter and leave proximity without sending an event of type @ref + * LIBINPUT_EVENT_TABLET_TOOL_AXIS. + */ + LIBINPUT_EVENT_TABLET_TOOL_AXIS = 600, + /** + * Signals that a tool has come in or out of proximity of a device with + * the @ref LIBINPUT_DEVICE_CAP_TABLET_TOOL capability. + * + * Proximity events contain each of the current values for each axis, + * and these values may be extracted from them in the same way they are + * with @ref LIBINPUT_EVENT_TABLET_TOOL_AXIS events. + * + * Some tools may always be in proximity. For these tools, events of + * type @ref LIBINPUT_TABLET_TOOL_PROXIMITY_IN are sent only once after @ref + * LIBINPUT_EVENT_DEVICE_ADDED, and events of type @ref + * LIBINPUT_TABLET_TOOL_PROXIMITY_OUT are sent only once before @ref + * LIBINPUT_EVENT_DEVICE_REMOVED. + * + * If the tool that comes into proximity supports x/y coordinates, + * libinput guarantees that both x and y are set in the proximity + * event. + * + * When a tool goes out of proximity, the value of every axis should be + * assumed to have an undefined state and any buttons that are currently held + * down on the stylus are marked as released. Button release events for + * each button that was held down on the stylus are sent before the + * proximity out event. + */ + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY, + /** + * Signals that a tool has come in contact with the surface of a + * device with the @ref LIBINPUT_DEVICE_CAP_TABLET_TOOL capability. + * + * On devices without distance proximity detection, the @ref + * LIBINPUT_EVENT_TABLET_TOOL_TIP is sent immediately after @ref + * LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY for the tip down event, and + * immediately before for the tip up event. + * + * If a button and/or axis state change occurs at the same time as a + * tip state change, the order of events is device-dependent. + */ + LIBINPUT_EVENT_TABLET_TOOL_TIP, + LIBINPUT_EVENT_TABLET_TOOL_BUTTON, + LIBINPUT_EVENT_GESTURE_SWIPE_BEGIN = 800, LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE, LIBINPUT_EVENT_GESTURE_SWIPE_END, @@ -274,6 +428,16 @@ struct libinput_event_pointer; struct libinput_event_touch; /** + * @ingroup event_tablet + * @struct libinput_event_tablet_tool + * + * Tablet event representing an axis update, button press, or tool update. Valid + * event types for this event are @ref LIBINPUT_EVENT_TABLET_TOOL_AXIS, @ref + * LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY and @ref LIBINPUT_EVENT_TABLET_TOOL_BUTTON. + */ +struct libinput_event_tablet_tool; + +/** * @defgroup event Accessing and destruction of events */ @@ -369,8 +533,6 @@ struct libinput_event_touch * libinput_event_get_touch_event(struct libinput_event *event); /** - * @ingroup event - * * Return the gesture event that is this input event. If the event type does * not match the gesture event types, this function returns NULL. * @@ -384,6 +546,19 @@ libinput_event_get_gesture_event(struct libinput_event *event); /** * @ingroup event * + * Return the tablet event that is this input event. If the event type does not + * match the tablet event types, this function returns NULL. + * + * The inverse of this function is libinput_event_tablet_tool_get_base_event(). + * + * @return A tablet event, or NULL for other events + */ +struct libinput_event_tablet_tool * +libinput_event_get_tablet_tool_event(struct libinput_event *event); + +/** + * @ingroup event + * * Return the device event that is this input event. If the event type does * not match the device event types, this function returns NULL. * @@ -1170,6 +1345,388 @@ double libinput_event_gesture_get_angle_delta(struct libinput_event_gesture *event); /** + * @defgroup event_tablet Tablet events + * + * Events that come from tablet devices. + */ + +/** + * @ingroup event_tablet + * + * @return The generic libinput_event of this event + */ +struct libinput_event * +libinput_event_tablet_tool_get_base_event(struct libinput_event_tablet_tool *event); + +/** + * @ingroup event_tablet + * + * Checks if an axis was updated in this event or return 0 otherwise. + * For tablet events that are not of type @ref LIBINPUT_EVENT_TABLET_TOOL_AXIS or + * type @ref LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY, this function returns 0. + * + * @note It is an application bug to call this function for events other than + * @ref LIBINPUT_EVENT_TABLET_TOOL_AXIS and @ref LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY. + * + * @param event The libinput tablet event + * @param axis The axis to check for updates + * @return 1 if the axis was updated or 0 otherwise + */ +int +libinput_event_tablet_tool_axis_has_changed(struct libinput_event_tablet_tool *event, + enum libinput_tablet_tool_axis axis); + +/** + * @ingroup event_tablet + * + * Return the axis value of a given axis for a tablet. The interpretation of the + * value is dependent on the axis: + * - @ref LIBINPUT_TABLET_TOOL_AXIS_X and @ref LIBINPUT_TABLET_TOOL_AXIS_Y - the X and + * Y coordinates of the tablet tool, in mm from the top left corner of the + * tablet. Use libinput_event_tablet_tool_get_x_transformed() and + * libinput_event_tablet_tool_get_y_transformed() for transforming each + * respective axis value into a different coordinate space. + * - @ref LIBINPUT_TABLET_TOOL_AXIS_DISTANCE - The distance from the tablet's + * sensor, normalized from 0 to 1 + * - @ref LIBINPUT_TABLET_TOOL_AXIS_PRESSURE - The current pressure being applied on + * the tool in use, normalized from 0 to 1 + * - @ref LIBINPUT_TABLET_TOOL_AXIS_TILT_X and @ref LIBINPUT_TABLET_TOOL_AXIS_TILT_Y - + * normalized value between -1 and 1 that indicates the X or Y tilt of the + * tool + * - @ref LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z - The z rotation of the tool in + * degrees, clockwise from the tool's logical neutral position. For the + * @ref LIBINPUT_TABLET_TOOL_TYPE_MOUSE and @ref LIBINPUT_TABLET_TOOL_TYPE_LENS tools + * the logical neutral position is pointing to the current logical north + * of the tablet. For the @ref LIBINPUT_TABLET_TOOL_TYPE_BRUSH tool, the logical + * neutral position is with the buttons pointing up. + * - @ref LIBINPUT_TABLET_TOOL_AXIS_SLIDER - A slider on the tool, normalized + * from 0 to 1. e.g. the wheel-like tool on the Wacom Airbrush. + * - @ref LIBINPUT_TABLET_TOOL_AXIS_REL_WHEEL - A relative wheel on the tool, + * similar or equivalent to a mouse wheel. The value is always 0, use + * libinput_event_tablet_tool_get_axis_delta() instead. + * + * @note This function may be called for a specific axis even if + * libinput_event_tablet_tool_axis_has_changed() returns 0 for that axis. + * libinput always includes all device axes in the event. + * + * If the event is of type @ref LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY and the + * event is a proximity out event, the value returned is the last known + * value of the tool before it left proximity. + * + * @param event The libinput tablet event + * @param axis The axis to retrieve the value of + * @return The current value of the the axis + */ +double +libinput_event_tablet_tool_get_axis_value(struct libinput_event_tablet_tool *event, + enum libinput_tablet_tool_axis axis); + +/** + * @ingroup event_tablet + * + * Return the delta for a given axis for a tablet. The interpretation of the + * value is axis-dependent: + * - @ref LIBINPUT_TABLET_TOOL_AXIS_REL_WHEEL - A relative wheel on the tool, + * similar or equivalent to a mouse wheel. The value is a delta from the + * device's previous position, in degrees. + * For all other axes, see libinput_event_tablet_tool_get_axis_value() for + * details. + * + * @param event The libinput tablet event + * @param axis The axis to retrieve the value of + * @return The delta to the previous axis value + */ +double +libinput_event_tablet_tool_get_axis_delta(struct libinput_event_tablet_tool *event, + enum libinput_tablet_tool_axis axis); + +/** + * @ingroup event_tablet + * + * Return the delta for a given axis for a tablet in discrete steps. + * How a value translates into a discrete step depends on the axis: + * - @ref LIBINPUT_TABLET_TOOL_AXIS_REL_WHEEL - the returned value is the number + * of physical mouse wheel clicks. + * For all other axes, this function returns 0. + * + * @param event The libinput tablet event + * @param axis The axis to retrieve the value of + * @return The delta to the previous axis value in discrete steps + */ +double +libinput_event_tablet_tool_get_axis_delta_discrete( + struct libinput_event_tablet_tool *event, + enum libinput_tablet_tool_axis axis); + +/** + * @ingroup event_tablet + * + * Return the current absolute x coordinate of the tablet event, transformed to + * screen coordinates. + * + * @note This function may be called for a specific axis even if + * libinput_event_tablet_tool_axis_has_changed() returns 0 for that axis. + * libinput always includes all device axes in the event. + * + * @param event The libinput tablet event + * @param width The current output screen width + * @return the current absolute x coordinate transformed to a screen coordinate + */ +double +libinput_event_tablet_tool_get_x_transformed(struct libinput_event_tablet_tool *event, + uint32_t width); + +/** + * @ingroup event_tablet + * + * Return the current absolute y coordinate of the tablet event, transformed to + * screen coordinates. + * + * @note This function may be called for a specific axis even if + * libinput_event_tablet_tool_axis_has_changed() returns 0 for that axis. + * libinput always includes all device axes in the event. + * + * @param event The libinput tablet event + * @param height The current output screen height + * @return the current absolute y coordinate transformed to a screen coordinate + */ +double +libinput_event_tablet_tool_get_y_transformed(struct libinput_event_tablet_tool *event, + uint32_t height); + +/** + * @ingroup event_tablet + * + * Returns the tool that was in use during this event. + * By default, each tool will stay valid for as long as it is being used, and is + * destroyed when another tool comes into proximity. However, the lifetime of + * the tool may be extended by using libinput_tablet_tool_ref() to increment the + * reference count of the tool. This guarantees that whenever the tool comes + * back into proximity of the tablet, that the same structure will be used to + * represent the tool. + * + * @note On tablets where the serial number of tools is not reported, each tool + * cannot be guaranteed to be unique. + * + * @param event The libinput tablet event + * @return The new tool triggering this event + */ +struct libinput_tablet_tool * +libinput_event_tablet_tool_get_tool(struct libinput_event_tablet_tool *event); + +/** + * @ingroup event_tablet + * + * Returns the new proximity state of a tool from a proximity event. + * Used to check whether or not a tool came in or out of proximity during an + * event of type @ref LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY. + * + * See @ref tablet-fake-proximity for recommendations on proximity handling. + * + * @param event The libinput tablet event + * @return The new proximity state of the tool from the event. + */ +enum libinput_tablet_tool_proximity_state +libinput_event_tablet_tool_get_proximity_state(struct libinput_event_tablet_tool *event); + +/** + * @ingroup event_tablet + * + * Returns the new tip state of a tool from a tip event. + * Used to check whether or not a tool came in contact with the tablet + * surface or left contact with the tablet surface during an + * event of type @ref LIBINPUT_EVENT_TABLET_TOOL_TIP. + * + * @param event The libinput tablet event + * @return The new tip state of the tool from the event. + */ +enum libinput_tablet_tool_tip_state +libinput_event_tablet_tool_get_tip_state(struct libinput_event_tablet_tool *event); + +/** + * @ingroup event_tablet + * + * Return the button that triggered this event. + * For tablet events that are not of type @ref LIBINPUT_EVENT_TABLET_TOOL_BUTTON, this + * function returns 0. + * + * @note It is an application bug to call this function for events other than + * @ref LIBINPUT_EVENT_TABLET_TOOL_BUTTON. + * + * @param event The libinput tablet event + * @return the button triggering this event + */ +uint32_t +libinput_event_tablet_tool_get_button(struct libinput_event_tablet_tool *event); + +/** + * @ingroup event_tablet + * + * Return the button state of the event. + * + * @note It is an application bug to call this function for events other than + * @ref LIBINPUT_EVENT_TABLET_TOOL_BUTTON. + * + * @param event The libinput tablet event + * @return the button state triggering this event + */ +enum libinput_button_state +libinput_event_tablet_tool_get_button_state(struct libinput_event_tablet_tool *event); + +/** + * @ingroup event_tablet + * + * For the button of a @ref LIBINPUT_EVENT_TABLET_TOOL_BUTTON event, return the total + * number of buttons pressed on all devices on the associated seat after the + * the event was triggered. + * + " @note It is an application bug to call this function for events other than + * @ref LIBINPUT_EVENT_TABLET_TOOL_BUTTON. For other events, this function returns 0. + * + * @return the seat wide pressed button count for the key of this event + */ +uint32_t +libinput_event_tablet_tool_get_seat_button_count(struct libinput_event_tablet_tool *event); + +/** + * @ingroup event_tablet + * + * @param event The libinput tablet event + * @return The event time for this event + */ +uint32_t +libinput_event_tablet_tool_get_time(struct libinput_event_tablet_tool *event); + +/** + * @ingroup event_tablet + * + * @param event The libinput tablet event + * @return The event time for this event in microseconds + */ +uint64_t +libinput_event_tablet_tool_get_time_usec(struct libinput_event_tablet_tool *event); + +/** + * @ingroup event_tablet + * + * Return the type of tool type for a tool object + * + * @param tool The libinput tool + * @return The tool type for this tool object + * + * @see libinput_tablet_tool_get_tool_id + */ +enum libinput_tablet_tool_type +libinput_tablet_tool_get_type(struct libinput_tablet_tool *tool); + +/** + * @ingroup event_tablet + * + * Return the tool ID for a tool object. If nonzero, this number identifies + * the specific type of the tool with more precision than the type returned in + * libinput_tablet_tool_get_type(). Not all tablets support a tool ID. + * + * Tablets known to support tool IDs include the Wacom Intuos 3, 4, 5, Wacom + * Cintiq and Wacom Intuos Pro series. + * + * @param tool The libinput tool + * @return The tool ID for this tool object or 0 if none is provided + * + * @see libinput_tablet_tool_get_type + */ +uint64_t +libinput_tablet_tool_get_tool_id(struct libinput_tablet_tool *tool); + +/** + * @ingroup event_tablet + * + * Increment the ref count of tool by one + * + * @param tool The tool to increment the ref count of + * @return The passed tool + */ +struct libinput_tablet_tool * +libinput_tablet_tool_ref(struct libinput_tablet_tool *tool); + +/** + * @ingroup event_tablet + * + * Return whether or not a tablet tool supports the specified axis + * + * @param tool The tool to check the axis capabilities of + * @param axis The axis to check for support + * @return Whether or not the axis is supported + */ +int +libinput_tablet_tool_has_axis(struct libinput_tablet_tool *tool, + enum libinput_tablet_tool_axis axis); + +/** + * @ingroup event_tablet + * + * Check if a tablet tool has a button with the + * passed-in code (see linux/input.h). + * + * @param tool A tablet tool + * @param code button code to check for + * + * @return 1 if the tool supports this button code, 0 if it does not + */ +int +libinput_tablet_tool_has_button(struct libinput_tablet_tool *tool, + uint32_t code); + +/** + * @ingroup event_tablet + * + * Decrement the ref count of tool by one. When the ref count of tool reaches 0, + * the memory allocated for tool will be freed. + * + * @param tool The tool to decrement the ref count of + * @return NULL if the tool was destroyed otherwise the passed tool + */ +struct libinput_tablet_tool * +libinput_tablet_tool_unref(struct libinput_tablet_tool *tool); + +/** + * @ingroup event_tablet + * + * Return the serial number of a tool + * + * @note Not all tablets report a serial number along with the type of tool + * being used. If the hardware does not provide a unique serial number, the + * serial number is always 0. + * + * @param tool The libinput tool + * @return The new tool serial triggering this event + */ +uint64_t +libinput_tablet_tool_get_serial(struct libinput_tablet_tool *tool); + +/** + * @ingroup event_tablet + * + * Return the user data associated with a tool object. + * + * @param tool The libinput tool + * @return The user data associated with the tool object + */ +void * +libinput_tablet_tool_get_user_data(struct libinput_tablet_tool *tool); + +/** + * @ingroup event_tablet + * + * Set the user data associated with a tool object. + * + * @param tool The libinput tool + * @param user_data The user data to associate with the tool object + */ +void +libinput_tablet_tool_set_user_data(struct libinput_tablet_tool *tool, + void *user_data); + +/** * @defgroup base Initialization and manipulation of libinput contexts */ diff --git a/src/libinput.sym b/src/libinput.sym index 15203c8d..1704903c 100644 --- a/src/libinput.sym +++ b/src/libinput.sym @@ -124,6 +124,7 @@ global: libinput_udev_assign_seat; libinput_udev_create_context; libinput_unref; + local: *; }; @@ -179,3 +180,33 @@ LIBINPUT_1.1 { libinput_device_config_accel_get_default_profile; libinput_device_config_accel_set_profile; } LIBINPUT_0.21.0; + +/* tablet APIs, they are not part of any stable API promise yet. + * keep them separate */ +LIBINPUT_TABLET_SUPPORT { + libinput_event_get_tablet_tool_event; + libinput_event_tablet_tool_axis_has_changed; + libinput_event_tablet_tool_get_axis_delta; + libinput_event_tablet_tool_get_axis_delta_discrete; + libinput_event_tablet_tool_get_axis_value; + libinput_event_tablet_tool_get_base_event; + libinput_event_tablet_tool_get_button; + libinput_event_tablet_tool_get_button_state; + libinput_event_tablet_tool_get_proximity_state; + libinput_event_tablet_tool_get_seat_button_count; + libinput_event_tablet_tool_get_time; + libinput_event_tablet_tool_get_tip_state; + libinput_event_tablet_tool_get_tool; + libinput_event_tablet_tool_get_x_transformed; + libinput_event_tablet_tool_get_y_transformed; + libinput_event_tablet_tool_get_time_usec; + libinput_tablet_tool_get_serial; + libinput_tablet_tool_get_tool_id; + libinput_tablet_tool_get_type; + libinput_tablet_tool_get_user_data; + libinput_tablet_tool_has_button; + libinput_tablet_tool_has_axis; + libinput_tablet_tool_ref; + libinput_tablet_tool_set_user_data; + libinput_tablet_tool_unref; +} LIBINPUT_1.1; diff --git a/test/Makefile.am b/test/Makefile.am index e4ed8e5d..139beb83 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -39,8 +39,13 @@ liblitest_la_SOURCES = \ litest-device-synaptics-x1-carbon-3rd.c \ litest-device-trackpoint.c \ litest-device-touch-screen.c \ + litest-device-wacom-bamboo-tablet.c \ + litest-device-wacom-cintiq-tablet.c \ + litest-device-wacom-intuos-tablet.c \ + litest-device-wacom-isdv4-tablet.c \ litest-device-wacom-touch.c \ litest-device-wacom-intuos-finger.c \ + litest-device-waltop-tablet.c \ litest-device-wheel-only.c \ litest-device-xen-virtual-pointer.c \ litest-device-vmware-virtual-usb-mouse.c \ @@ -59,6 +64,7 @@ run_tests = \ test-touchpad \ test-touchpad-tap \ test-touchpad-buttons \ + test-tablet \ test-device \ test-gestures \ test-pointer \ @@ -103,6 +109,10 @@ test_log_SOURCES = log.c test_log_LDADD = $(TEST_LIBS) test_log_LDFLAGS = -no-install +test_tablet_SOURCES = tablet.c +test_tablet_LDADD = $(TEST_LIBS) +test_tablet_LDFLAGS = -static + test_touchpad_SOURCES = touchpad.c test_touchpad_LDADD = $(TEST_LIBS) test_touchpad_LDFLAGS = -no-install diff --git a/test/device.c b/test/device.c index b7fa0e07..b829af73 100644 --- a/test/device.c +++ b/test/device.c @@ -1104,6 +1104,22 @@ START_TEST(device_udev_tag_synaptics_serial) } END_TEST +START_TEST(device_udev_tag_wacom_tablet) +{ + struct litest_device *dev = litest_current_device(); + struct libinput_device *device = dev->libinput_device; + struct udev_device *d; + const char *prop; + + d = libinput_device_get_udev_device(device); + prop = udev_device_get_property_value(d, + "ID_INPUT_TABLET"); + + ck_assert_notnull(prop); + udev_device_unref(d); +} +END_TEST + START_TEST(device_nonpointer_rel) { struct libevdev_uinput *uinput; @@ -1260,16 +1276,16 @@ litest_setup_tests(void) struct range abs_range = { 0, ABS_MISC }; struct range abs_mt_range = { ABS_MT_SLOT + 1, ABS_CNT }; - litest_add("device:sendevents", device_sendevents_config, LITEST_ANY, LITEST_TOUCHPAD); - litest_add("device:sendevents", device_sendevents_config_invalid, LITEST_ANY, LITEST_ANY); - litest_add("device:sendevents", device_sendevents_config_touchpad, LITEST_TOUCHPAD, LITEST_ANY); - litest_add("device:sendevents", device_sendevents_config_touchpad_superset, LITEST_TOUCHPAD, LITEST_ANY); - litest_add("device:sendevents", device_sendevents_config_default, LITEST_ANY, LITEST_ANY); - litest_add("device:sendevents", device_disable, LITEST_RELATIVE, LITEST_ANY); - litest_add("device:sendevents", device_disable_touchpad, LITEST_TOUCHPAD, LITEST_ANY); - litest_add("device:sendevents", device_disable_events_pending, LITEST_RELATIVE, LITEST_TOUCHPAD); - litest_add("device:sendevents", device_double_disable, LITEST_ANY, LITEST_ANY); - litest_add("device:sendevents", device_double_enable, LITEST_ANY, LITEST_ANY); + litest_add("device:sendevents", device_sendevents_config, LITEST_ANY, LITEST_TOUCHPAD|LITEST_TABLET); + litest_add("device:sendevents", device_sendevents_config_invalid, LITEST_ANY, LITEST_TABLET); + litest_add("device:sendevents", device_sendevents_config_touchpad, LITEST_TOUCHPAD, LITEST_TABLET); + litest_add("device:sendevents", device_sendevents_config_touchpad_superset, LITEST_TOUCHPAD, LITEST_TABLET); + litest_add("device:sendevents", device_sendevents_config_default, LITEST_ANY, LITEST_TABLET); + litest_add("device:sendevents", device_disable, LITEST_RELATIVE, LITEST_TABLET); + litest_add("device:sendevents", device_disable_touchpad, LITEST_TOUCHPAD, LITEST_TABLET); + litest_add("device:sendevents", device_disable_events_pending, LITEST_RELATIVE, LITEST_TOUCHPAD|LITEST_TABLET); + litest_add("device:sendevents", device_double_disable, LITEST_ANY, LITEST_TABLET); + litest_add("device:sendevents", device_double_enable, LITEST_ANY, LITEST_TABLET); litest_add_no_device("device:sendevents", device_reenable_syspath_changed); litest_add_no_device("device:sendevents", device_reenable_device_removed); litest_add_for_device("device:sendevents", device_disable_release_buttons, LITEST_MOUSE); @@ -1296,13 +1312,14 @@ litest_setup_tests(void) litest_add_no_device("device:invalid devices", abs_device_missing_res); litest_add_no_device("device:invalid devices", abs_mt_device_missing_res); - litest_add("device:wheel", device_wheel_only, LITEST_WHEEL, LITEST_RELATIVE|LITEST_ABSOLUTE); + litest_add("device:wheel", device_wheel_only, LITEST_WHEEL, LITEST_RELATIVE|LITEST_ABSOLUTE|LITEST_TABLET); litest_add_no_device("device:accelerometer", device_accelerometer); litest_add("device:udev tags", device_udev_tag_alps, LITEST_TOUCHPAD, LITEST_ANY); litest_add("device:udev tags", device_udev_tag_wacom, LITEST_TOUCHPAD, LITEST_ANY); litest_add("device:udev tags", device_udev_tag_apple, LITEST_TOUCHPAD, LITEST_ANY); litest_add("device:udev tags", device_udev_tag_synaptics_serial, LITEST_TOUCHPAD, LITEST_ANY); + litest_add("device:udev tags", device_udev_tag_wacom_tablet, LITEST_TABLET, LITEST_ANY); litest_add_no_device("device:invalid rel events", device_nonpointer_rel); litest_add_no_device("device:invalid rel events", device_touchpad_rel); diff --git a/test/litest-device-wacom-bamboo-tablet.c b/test/litest-device-wacom-bamboo-tablet.c new file mode 100644 index 00000000..d21d4be0 --- /dev/null +++ b/test/litest-device-wacom-bamboo-tablet.c @@ -0,0 +1,105 @@ +/* + * Copyright © 2014 Red Hat, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and its + * documentation for any purpose is hereby granted without fee, provided that + * the above copyright notice appear in all copies and that both that copyright + * notice and this permission notice appear in supporting documentation, and + * that the name of the copyright holders not be used in advertising or + * publicity pertaining to distribution of the software without specific, + * written prior permission. The copyright holders make no representations + * about the suitability of this software for any purpose. It is provided "as + * is" without express or implied warranty. + * + * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR + * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, + * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER + * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#if HAVE_CONFIG_H +#include "config.h" +#endif + +#include "litest.h" +#include "litest-int.h" + +static void litest_wacom_bamboo_tablet_setup(void) +{ + struct litest_device *d = litest_create_device(LITEST_WACOM_BAMBOO); + litest_set_current_device(d); +} + +static struct input_event proximity_in[] = { + { .type = EV_ABS, .code = ABS_X, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_Y, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_DISTANCE, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_KEY, .code = BTN_TOOL_PEN, .value = 1 }, + { .type = EV_SYN, .code = SYN_REPORT, .value = 0 }, + { .type = -1, .code = -1 }, +}; + +static struct input_event proximity_out[] = { + { .type = EV_ABS, .code = ABS_X, .value = 0 }, + { .type = EV_ABS, .code = ABS_Y, .value = 0 }, + { .type = EV_ABS, .code = ABS_DISTANCE, .value = 0 }, + { .type = EV_KEY, .code = BTN_TOOL_PEN, .value = 0 }, + { .type = EV_SYN, .code = SYN_REPORT, .value = 0 }, + { .type = -1, .code = -1 }, +}; + +static struct input_event motion[] = { + { .type = EV_ABS, .code = ABS_X, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_Y, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_DISTANCE, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_PRESSURE, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_SYN, .code = SYN_REPORT, .value = 0 }, + { .type = -1, .code = -1 }, +}; + +static struct litest_device_interface interface = { + .tablet_proximity_in_events = proximity_in, + .tablet_proximity_out_events = proximity_out, + .tablet_motion_events = motion, +}; + +static struct input_absinfo absinfo[] = { + { ABS_X, 0, 14720, 4, 0, 100 }, + { ABS_Y, 0, 9200, 4, 0, 100 }, + { ABS_PRESSURE, 0, 1023, 0, 0, 0 }, + { ABS_DISTANCE, 0, 31, 0, 0, 0 }, + { .value = -1 }, +}; + +static struct input_id input_id = { + .bustype = 0x3, + .vendor = 0x56a, + .product = 0xde, + .version = 0x100, +}; + +static int events[] = { + EV_KEY, BTN_TOOL_PEN, + EV_KEY, BTN_TOOL_RUBBER, + EV_KEY, BTN_TOUCH, + EV_KEY, BTN_STYLUS, + EV_KEY, BTN_STYLUS2, + INPUT_PROP_MAX, INPUT_PROP_POINTER, + -1, -1, +}; + +struct litest_test_device litest_wacom_bamboo_tablet_device = { + .type = LITEST_WACOM_BAMBOO, + .features = LITEST_TABLET | LITEST_DISTANCE, + .shortname = "wacom-bamboo-tablet", + .setup = litest_wacom_bamboo_tablet_setup, + .interface = &interface, + + .name = "Wacom Bamboo 16FG 4x5 Pen", + .id = &input_id, + .events = events, + .absinfo = absinfo, +}; diff --git a/test/litest-device-wacom-cintiq-tablet.c b/test/litest-device-wacom-cintiq-tablet.c new file mode 100644 index 00000000..0217f560 --- /dev/null +++ b/test/litest-device-wacom-cintiq-tablet.c @@ -0,0 +1,137 @@ +/* + * Copyright © 2014 Red Hat, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and its + * documentation for any purpose is hereby granted without fee, provided that + * the above copyright notice appear in all copies and that both that copyright + * notice and this permission notice appear in supporting documentation, and + * that the name of the copyright holders not be used in advertising or + * publicity pertaining to distribution of the software without specific, + * written prior permission. The copyright holders make no representations + * about the suitability of this software for any purpose. It is provided "as + * is" without express or implied warranty. + * + * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR + * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, + * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER + * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#if HAVE_CONFIG_H +#include "config.h" +#endif + +#include "litest.h" +#include "litest-int.h" + +static void litest_wacom_cintiq_tablet_setup(void) +{ + struct litest_device *d = litest_create_device(LITEST_WACOM_CINTIQ); + litest_set_current_device(d); +} + +static struct input_event proximity_in[] = { + { .type = EV_ABS, .code = ABS_X, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_Y, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_DISTANCE, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_TILT_X, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_TILT_Y, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_MISC, .value = 2083 }, + { .type = EV_MSC, .code = MSC_SERIAL, .value = 297797542 }, + { .type = EV_KEY, .code = BTN_TOOL_PEN, .value = 1 }, + { .type = EV_SYN, .code = SYN_REPORT, .value = 0 }, + { .type = -1, .code = -1 }, +}; + +static struct input_event proximity_out[] = { + { .type = EV_ABS, .code = ABS_X, .value = 0 }, + { .type = EV_ABS, .code = ABS_Y, .value = 0 }, + { .type = EV_ABS, .code = ABS_DISTANCE, .value = 0 }, + { .type = EV_ABS, .code = ABS_TILT_X, .value = 0 }, + { .type = EV_ABS, .code = ABS_TILT_Y, .value = 0 }, + { .type = EV_ABS, .code = ABS_MISC, .value = 0 }, + { .type = EV_MSC, .code = MSC_SERIAL, .value = 297797542 }, + { .type = EV_KEY, .code = BTN_TOOL_PEN, .value = 0 }, + { .type = EV_SYN, .code = SYN_REPORT, .value = 0 }, + { .type = -1, .code = -1 }, +}; + +static struct input_event motion[] = { + { .type = EV_ABS, .code = ABS_X, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_Y, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_DISTANCE, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_PRESSURE, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_TILT_X, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_TILT_Y, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_MSC, .code = MSC_SERIAL, .value = 297797542 }, + { .type = EV_SYN, .code = SYN_REPORT, .value = 0 }, + { .type = -1, .code = -1 }, +}; + +static struct litest_device_interface interface = { + .tablet_proximity_in_events = proximity_in, + .tablet_proximity_out_events = proximity_out, + .tablet_motion_events = motion, +}; + +static struct input_absinfo absinfo[] = { + { ABS_X, 0, 53020, 4, 0, 200 }, + { ABS_Y, 0, 33440, 4, 0, 200 }, + { ABS_Z, -900, 899, 0, 0, 0 }, + { ABS_RX, 0, 4096, 0, 0, 0 }, + { ABS_RY, 0, 4096, 0, 0, 0 }, + { ABS_WHEEL, 0, 1023, 0, 0, 0 }, + { ABS_PRESSURE, 0, 1023, 0, 0, 0 }, + { ABS_DISTANCE, 0, 63, 0, 0, 0 }, + { ABS_TILT_X, 0, 127, 0, 0, 0 }, + { ABS_TILT_Y, 0, 127, 0, 0, 0 }, + { ABS_MISC, 0, 0, 0, 0, 0 }, + { .value = -1 }, +}; + +static struct input_id input_id = { + .bustype = 0x3, + .vendor = 0x56a, + .product = 0xc6, + .version = 0x113, +}; + +static int events[] = { + EV_KEY, BTN_0, + EV_KEY, BTN_1, + EV_KEY, BTN_2, + EV_KEY, BTN_3, + EV_KEY, BTN_4, + EV_KEY, BTN_5, + EV_KEY, BTN_6, + EV_KEY, BTN_7, + EV_KEY, BTN_8, + EV_KEY, BTN_9, + EV_KEY, BTN_TOOL_PEN, + EV_KEY, BTN_TOOL_RUBBER, + EV_KEY, BTN_TOOL_BRUSH, + EV_KEY, BTN_TOOL_PENCIL, + EV_KEY, BTN_TOOL_AIRBRUSH, + EV_KEY, BTN_TOUCH, + EV_KEY, BTN_STYLUS, + EV_KEY, BTN_STYLUS2, + EV_MSC, MSC_SERIAL, + INPUT_PROP_MAX, INPUT_PROP_DIRECT, + -1, -1, +}; + +struct litest_test_device litest_wacom_cintiq_tablet_device = { + .type = LITEST_WACOM_CINTIQ, + .features = LITEST_TABLET | LITEST_DISTANCE | LITEST_TOOL_SERIAL, + .shortname = "wacom-cintiq-tablet", + .setup = litest_wacom_cintiq_tablet_setup, + .interface = &interface, + + .name = "Wacom Cintiq 12WX", + .id = &input_id, + .events = events, + .absinfo = absinfo, +}; diff --git a/test/litest-device-wacom-intuos-tablet.c b/test/litest-device-wacom-intuos-tablet.c new file mode 100644 index 00000000..e0e1d44b --- /dev/null +++ b/test/litest-device-wacom-intuos-tablet.c @@ -0,0 +1,141 @@ +/* + * Copyright © 2014 Red Hat, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and its + * documentation for any purpose is hereby granted without fee, provided that + * the above copyright notice appear in all copies and that both that copyright + * notice and this permission notice appear in supporting documentation, and + * that the name of the copyright holders not be used in advertising or + * publicity pertaining to distribution of the software without specific, + * written prior permission. The copyright holders make no representations + * about the suitability of this software for any purpose. It is provided "as + * is" without express or implied warranty. + * + * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR + * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, + * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER + * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#if HAVE_CONFIG_H +#include "config.h" +#endif + +#include "litest.h" +#include "litest-int.h" + +static void litest_wacom_intuos_tablet_setup(void) +{ + struct litest_device *d = litest_create_device(LITEST_WACOM_INTUOS); + litest_set_current_device(d); +} + +static struct input_event proximity_in[] = { + { .type = EV_ABS, .code = ABS_X, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_Y, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_DISTANCE, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_TILT_X, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_TILT_Y, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_MISC, .value = 1050626 }, + { .type = EV_MSC, .code = MSC_SERIAL, .value = 578837976 }, + { .type = EV_KEY, .code = BTN_TOOL_PEN, .value = 1 }, + { .type = EV_SYN, .code = SYN_REPORT, .value = 0 }, + { .type = -1, .code = -1 }, +}; + +static struct input_event proximity_out[] = { + { .type = EV_ABS, .code = ABS_X, .value = 0 }, + { .type = EV_ABS, .code = ABS_Y, .value = 0 }, + { .type = EV_ABS, .code = ABS_DISTANCE, .value = 0 }, + { .type = EV_ABS, .code = ABS_TILT_X, .value = 0 }, + { .type = EV_ABS, .code = ABS_TILT_Y, .value = 0 }, + { .type = EV_ABS, .code = ABS_MISC, .value = 0 }, + { .type = EV_MSC, .code = MSC_SERIAL, .value = 578837976 }, + { .type = EV_KEY, .code = BTN_TOOL_PEN, .value = 0 }, + { .type = EV_SYN, .code = SYN_REPORT, .value = 0 }, + { .type = -1, .code = -1 }, +}; + +static struct input_event motion[] = { + { .type = EV_ABS, .code = ABS_X, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_Y, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_DISTANCE, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_PRESSURE, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_TILT_X, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_TILT_Y, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_MSC, .code = MSC_SERIAL, .value = 578837976 }, + { .type = EV_SYN, .code = SYN_REPORT, .value = 0 }, + { .type = -1, .code = -1 }, +}; +static struct litest_device_interface interface = { + .tablet_proximity_in_events = proximity_in, + .tablet_proximity_out_events = proximity_out, + .tablet_motion_events = motion, +}; + +static struct input_absinfo absinfo[] = { + { ABS_X, 0, 44704, 4, 0, 200 }, + { ABS_Y, 0, 27940, 4, 0, 200 }, + { ABS_Z, -900, 899, 0, 0, 0 }, + { ABS_THROTTLE, -1023, 1023, 0, 0, 0 }, + { ABS_WHEEL, 0, 1023, 0, 0, 0 }, + { ABS_PRESSURE, 0, 2047, 0, 0, 0 }, + { ABS_DISTANCE, 0, 63, 0, 0, 0 }, + { ABS_TILT_X, 0, 127, 0, 0, 0 }, + { ABS_TILT_Y, 0, 127, 0, 0, 0 }, + { ABS_MISC, 0, 0, 0, 0, 0 }, + { .value = -1 }, +}; + +static struct input_id input_id = { + .bustype = 0x3, + .vendor = 0x56a, + .product = 0x27, +}; + +static int events[] = { + EV_KEY, BTN_0, + EV_KEY, BTN_1, + EV_KEY, BTN_2, + EV_KEY, BTN_3, + EV_KEY, BTN_4, + EV_KEY, BTN_5, + EV_KEY, BTN_6, + EV_KEY, BTN_7, + EV_KEY, BTN_8, + EV_KEY, BTN_LEFT, + EV_KEY, BTN_RIGHT, + EV_KEY, BTN_MIDDLE, + EV_KEY, BTN_SIDE, + EV_KEY, BTN_EXTRA, + EV_KEY, BTN_TOOL_PEN, + EV_KEY, BTN_TOOL_RUBBER, + EV_KEY, BTN_TOOL_BRUSH, + EV_KEY, BTN_TOOL_PENCIL, + EV_KEY, BTN_TOOL_AIRBRUSH, + EV_KEY, BTN_TOOL_MOUSE, + EV_KEY, BTN_TOOL_LENS, + EV_KEY, BTN_TOUCH, + EV_KEY, BTN_STYLUS, + EV_KEY, BTN_STYLUS2, + EV_REL, REL_WHEEL, + EV_MSC, MSC_SERIAL, + INPUT_PROP_MAX, INPUT_PROP_POINTER, + -1, -1, +}; + +struct litest_test_device litest_wacom_intuos_tablet_device = { + .type = LITEST_WACOM_INTUOS, + .features = LITEST_TABLET | LITEST_DISTANCE | LITEST_TOOL_SERIAL, + .shortname = "wacom-intuos-tablet", + .setup = litest_wacom_intuos_tablet_setup, + .interface = &interface, + + .name = "Wacom Intuos5 touch M Pen", + .id = &input_id, + .events = events, + .absinfo = absinfo, +}; diff --git a/test/litest-device-wacom-isdv4-tablet.c b/test/litest-device-wacom-isdv4-tablet.c new file mode 100644 index 00000000..c9d40e77 --- /dev/null +++ b/test/litest-device-wacom-isdv4-tablet.c @@ -0,0 +1,98 @@ +/* + * Copyright © 2014 Red Hat, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and its + * documentation for any purpose is hereby granted without fee, provided that + * the above copyright notice appear in all copies and that both that copyright + * notice and this permission notice appear in supporting documentation, and + * that the name of the copyright holders not be used in advertising or + * publicity pertaining to distribution of the software without specific, + * written prior permission. The copyright holders make no representations + * about the suitability of this software for any purpose. It is provided "as + * is" without express or implied warranty. + * + * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR + * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, + * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER + * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#if HAVE_CONFIG_H +#include "config.h" +#endif + +#include "litest.h" +#include "litest-int.h" + +static void litest_wacom_isdv4_tablet_setup(void) +{ + struct litest_device *d = litest_create_device(LITEST_WACOM_ISDV4); + litest_set_current_device(d); +} + +static struct input_event proximity_in[] = { + { .type = EV_ABS, .code = ABS_X, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_Y, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_KEY, .code = BTN_TOOL_PEN, .value = 1 }, + { .type = EV_SYN, .code = SYN_REPORT, .value = 0 }, + { .type = -1, .code = -1 }, +}; + +static struct input_event proximity_out[] = { + { .type = EV_KEY, .code = BTN_TOOL_PEN, .value = 0 }, + { .type = EV_SYN, .code = SYN_REPORT, .value = 0 }, + { .type = -1, .code = -1 }, +}; + +static struct input_event motion[] = { + { .type = EV_ABS, .code = ABS_X, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_Y, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_PRESSURE, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_SYN, .code = SYN_REPORT, .value = 0 }, + { .type = -1, .code = -1 }, +}; + +static struct litest_device_interface interface = { + .tablet_proximity_in_events = proximity_in, + .tablet_proximity_out_events = proximity_out, + .tablet_motion_events = motion, +}; + +static struct input_absinfo absinfo[] = { + { ABS_X, 0, 27760, 4, 0, 100 }, + { ABS_Y, 0, 15694, 4, 0, 100 }, + { ABS_PRESSURE, 0, 255, 0, 0, 0 }, + { .value = -1 }, +}; + +static struct input_id input_id = { + .bustype = 0x3, + .vendor = 0x56a, + .product = 0xe6, +}; + +static int events[] = { + EV_KEY, BTN_TOOL_PEN, + EV_KEY, BTN_TOOL_RUBBER, + EV_KEY, BTN_TOUCH, + EV_KEY, BTN_STYLUS, + EV_KEY, BTN_STYLUS2, + INPUT_PROP_MAX, INPUT_PROP_DIRECT, + -1, -1, +}; + +struct litest_test_device litest_wacom_isdv4_tablet_device = { + .type = LITEST_WACOM_ISDV4, + .features = LITEST_TABLET, + .shortname = "wacom-isdv4-tablet", + .setup = litest_wacom_isdv4_tablet_setup, + .interface = &interface, + + .name = "Wacom ISDv4 E6 Pen", + .id = &input_id, + .events = events, + .absinfo = absinfo, +}; diff --git a/test/litest-device-waltop-tablet.c b/test/litest-device-waltop-tablet.c new file mode 100644 index 00000000..93ec739d --- /dev/null +++ b/test/litest-device-waltop-tablet.c @@ -0,0 +1,222 @@ +/* + * Copyright © 2014 Red Hat, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and its + * documentation for any purpose is hereby granted without fee, provided that + * the above copyright notice appear in all copies and that both that copyright + * notice and this permission notice appear in supporting documentation, and + * that the name of the copyright holders not be used in advertising or + * publicity pertaining to distribution of the software without specific, + * written prior permission. The copyright holders make no representations + * about the suitability of this software for any purpose. It is provided "as + * is" without express or implied warranty. + * + * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR + * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, + * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER + * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#if HAVE_CONFIG_H +#include "config.h" +#endif + +#include "litest.h" +#include "litest-int.h" + +static void litest_waltop_tablet_setup(void) +{ + struct litest_device *d = litest_create_device(LITEST_WALTOP); + litest_set_current_device(d); +} + +static struct input_event proximity_in[] = { + { .type = EV_ABS, .code = ABS_X, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_Y, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_TILT_X, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_TILT_Y, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_KEY, .code = BTN_TOOL_PEN, .value = 1 }, + { .type = EV_SYN, .code = SYN_REPORT, .value = 0 }, + { .type = -1, .code = -1 }, +}; + +static struct input_event proximity_out[] = { + { .type = EV_ABS, .code = ABS_X, .value = 0 }, + { .type = EV_ABS, .code = ABS_Y, .value = 0 }, + { .type = EV_ABS, .code = ABS_TILT_X, .value = 0 }, + { .type = EV_ABS, .code = ABS_TILT_Y, .value = 0 }, + { .type = EV_KEY, .code = BTN_TOOL_PEN, .value = 0 }, + { .type = EV_SYN, .code = SYN_REPORT, .value = 0 }, + { .type = -1, .code = -1 }, +}; + +static struct input_event motion[] = { + { .type = EV_ABS, .code = ABS_X, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_Y, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_PRESSURE, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_TILT_X, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_ABS, .code = ABS_TILT_Y, .value = LITEST_AUTO_ASSIGN }, + { .type = EV_SYN, .code = SYN_REPORT, .value = 0 }, + { .type = -1, .code = -1 }, +}; +static struct litest_device_interface interface = { + .tablet_proximity_in_events = proximity_in, + .tablet_proximity_out_events = proximity_out, + .tablet_motion_events = motion, +}; + +static struct input_absinfo absinfo[] = { + { ABS_X, 0, 32000, 0, 0, 0 }, + { ABS_Y, 0, 32000, 0, 0, 0 }, + { ABS_PRESSURE, 0, 2047, 0, 0, 0 }, + { ABS_TILT_X, -127, 127, 0, 0, 0 }, + { ABS_TILT_Y, -127, 127, 0, 0, 0 }, + { .value = -1 }, +}; + +static struct input_id input_id = { + .bustype = 0x3, + .vendor = 0x172f, + .product = 0x509, +}; + +static int events[] = { + EV_KEY, KEY_ESC, + EV_KEY, KEY_1, + EV_KEY, KEY_2, + EV_KEY, KEY_3, + EV_KEY, KEY_4, + EV_KEY, KEY_5, + EV_KEY, KEY_6, + EV_KEY, KEY_7, + EV_KEY, KEY_8, + EV_KEY, KEY_9, + EV_KEY, KEY_0, + EV_KEY, KEY_MINUS, + EV_KEY, KEY_EQUAL, + EV_KEY, KEY_BACKSPACE, + EV_KEY, KEY_TAB, + EV_KEY, KEY_Q, + EV_KEY, KEY_W, + EV_KEY, KEY_E, + EV_KEY, KEY_R, + EV_KEY, KEY_T, + EV_KEY, KEY_Y, + EV_KEY, KEY_U, + EV_KEY, KEY_I, + EV_KEY, KEY_O, + EV_KEY, KEY_P, + EV_KEY, KEY_LEFTBRACE, + EV_KEY, KEY_RIGHTBRACE, + EV_KEY, KEY_ENTER, + EV_KEY, KEY_LEFTCTRL, + EV_KEY, KEY_A, + EV_KEY, KEY_S, + EV_KEY, KEY_D, + EV_KEY, KEY_F, + EV_KEY, KEY_G, + EV_KEY, KEY_H, + EV_KEY, KEY_J, + EV_KEY, KEY_K, + EV_KEY, KEY_L, + EV_KEY, KEY_SEMICOLON, + EV_KEY, KEY_APOSTROPHE, + EV_KEY, KEY_GRAVE, + EV_KEY, KEY_LEFTSHIFT, + EV_KEY, KEY_BACKSLASH, + EV_KEY, KEY_Z, + EV_KEY, KEY_X, + EV_KEY, KEY_C, + EV_KEY, KEY_V, + EV_KEY, KEY_B, + EV_KEY, KEY_N, + EV_KEY, KEY_M, + EV_KEY, KEY_COMMA, + EV_KEY, KEY_DOT, + EV_KEY, KEY_SLASH, + EV_KEY, KEY_RIGHTSHIFT, + EV_KEY, KEY_KPASTERISK, + EV_KEY, KEY_LEFTALT, + EV_KEY, KEY_SPACE, + EV_KEY, KEY_CAPSLOCK, + EV_KEY, KEY_F1, + EV_KEY, KEY_F2, + EV_KEY, KEY_F3, + EV_KEY, KEY_F4, + EV_KEY, KEY_F5, + EV_KEY, KEY_F6, + EV_KEY, KEY_F7, + EV_KEY, KEY_F8, + EV_KEY, KEY_F9, + EV_KEY, KEY_F10, + EV_KEY, KEY_NUMLOCK, + EV_KEY, KEY_SCROLLLOCK, + EV_KEY, KEY_KP7, + EV_KEY, KEY_KP8, + EV_KEY, KEY_KP9, + EV_KEY, KEY_KPMINUS, + EV_KEY, KEY_KP4, + EV_KEY, KEY_KP5, + EV_KEY, KEY_KP6, + EV_KEY, KEY_KPPLUS, + EV_KEY, KEY_KP1, + EV_KEY, KEY_KP2, + EV_KEY, KEY_KP3, + EV_KEY, KEY_KP0, + EV_KEY, KEY_KPDOT, + EV_KEY, KEY_102ND, + EV_KEY, KEY_F11, + EV_KEY, KEY_F12, + EV_KEY, KEY_KPENTER, + EV_KEY, KEY_RIGHTCTRL, + EV_KEY, KEY_KPSLASH, + EV_KEY, KEY_SYSRQ, + EV_KEY, KEY_RIGHTALT, + EV_KEY, KEY_HOME, + EV_KEY, KEY_UP, + EV_KEY, KEY_PAGEUP, + EV_KEY, KEY_LEFT, + EV_KEY, KEY_RIGHT, + EV_KEY, KEY_END, + EV_KEY, KEY_DOWN, + EV_KEY, KEY_PAGEDOWN, + EV_KEY, KEY_INSERT, + EV_KEY, KEY_DELETE, + EV_KEY, KEY_MUTE, + EV_KEY, KEY_VOLUMEDOWN, + EV_KEY, KEY_VOLUMEUP, + EV_KEY, KEY_PAUSE, + EV_KEY, KEY_LEFTMETA, + EV_KEY, KEY_RIGHTMETA, + EV_KEY, KEY_COMPOSE, + EV_KEY, BTN_0, + EV_KEY, BTN_LEFT, + EV_KEY, BTN_RIGHT, + EV_KEY, BTN_MIDDLE, + EV_KEY, BTN_SIDE, + EV_KEY, BTN_EXTRA, + EV_KEY, BTN_TOOL_PEN, + EV_KEY, BTN_TOOL_RUBBER, + EV_KEY, BTN_TOUCH, + EV_KEY, BTN_STYLUS, + EV_REL, REL_HWHEEL, + EV_REL, REL_WHEEL, + EV_MSC, MSC_SERIAL, + -1, -1, +}; + +struct litest_test_device litest_waltop_tablet_device = { + .type = LITEST_WALTOP, + .features = LITEST_TABLET | LITEST_WHEEL, + .shortname = "waltop-tablet", + .setup = litest_waltop_tablet_setup, + .interface = &interface, + + .name = " WALTOP Batteryless Tablet ", /* sic */ + .id = &input_id, + .events = events, + .absinfo = absinfo, +}; diff --git a/test/litest-int.h b/test/litest-int.h index ffa30a74..8d0308b3 100644 --- a/test/litest-int.h +++ b/test/litest-int.h @@ -97,6 +97,14 @@ struct litest_device_interface { struct input_event *touch_move_events; struct input_event *touch_up_events; + /** + * Tablet events, LITEST_AUTO_ASSIGN is allowed on event values for + * ABS_X, ABS_Y, ABS_DISTANCE and ABS_PRESSURE. + */ + struct input_event *tablet_proximity_in_events; + struct input_event *tablet_proximity_out_events; + struct input_event *tablet_motion_events; + int min[2]; /* x/y axis minimum */ int max[2]; /* x/y axis maximum */ }; diff --git a/test/litest.c b/test/litest.c index 65be6b5a..0083dac8 100644 --- a/test/litest.c +++ b/test/litest.c @@ -344,6 +344,10 @@ extern struct litest_test_device litest_trackpoint_device; extern struct litest_test_device litest_bcm5974_device; extern struct litest_test_device litest_mouse_device; extern struct litest_test_device litest_wacom_touch_device; +extern struct litest_test_device litest_wacom_bamboo_tablet_device; +extern struct litest_test_device litest_wacom_cintiq_tablet_device; +extern struct litest_test_device litest_wacom_intuos_tablet_device; +extern struct litest_test_device litest_wacom_isdv4_tablet_device; extern struct litest_test_device litest_alps_device; extern struct litest_test_device litest_generic_singletouch_device; extern struct litest_test_device litest_qemu_tablet_device; @@ -367,6 +371,7 @@ extern struct litest_test_device litest_magicpad_device; extern struct litest_test_device litest_elantech_touchpad_device; extern struct litest_test_device litest_mouse_gladius_device; extern struct litest_test_device litest_mouse_wheel_click_angle_device; +extern struct litest_test_device litest_waltop_tablet_device; struct litest_test_device* devices[] = { &litest_synaptics_clickpad_device, @@ -377,6 +382,10 @@ struct litest_test_device* devices[] = { &litest_bcm5974_device, &litest_mouse_device, &litest_wacom_touch_device, + &litest_wacom_bamboo_tablet_device, + &litest_wacom_cintiq_tablet_device, + &litest_wacom_intuos_tablet_device, + &litest_wacom_isdv4_tablet_device, &litest_alps_device, &litest_generic_singletouch_device, &litest_qemu_tablet_device, @@ -400,6 +409,7 @@ struct litest_test_device* devices[] = { &litest_elantech_touchpad_device, &litest_mouse_gladius_device, &litest_mouse_wheel_click_angle_device, + &litest_waltop_tablet_device, NULL, }; @@ -1478,6 +1488,80 @@ litest_touch_move_to(struct litest_device *d, litest_touch_move(d, slot, x_to, y_to); } +static int +auto_assign_tablet_value(struct litest_device *d, + const struct input_event *ev, + int x, int y, + struct axis_replacement *axes) +{ + int value = ev->value; + + if (value != LITEST_AUTO_ASSIGN || ev->type != EV_ABS) + return value; + + switch (ev->code) { + case ABS_X: + value = litest_scale(d, ABS_X, x); + break; + case ABS_Y: + value = litest_scale(d, ABS_Y, y); + break; + default: + axis_replacement_value(axes, ev->code, &value); + break; + } + + return value; +} + +static int +tablet_ignore_event(const struct input_event *ev, int value) +{ + return value == -1 && (ev->code == ABS_PRESSURE || ev->code == ABS_DISTANCE); +} + +void +litest_tablet_proximity_in(struct litest_device *d, int x, int y, struct axis_replacement *axes) +{ + struct input_event *ev; + + ev = d->interface->tablet_proximity_in_events; + while (ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1) { + int value = auto_assign_tablet_value(d, ev, x, y, axes); + if (!tablet_ignore_event(ev, value)) + litest_event(d, ev->type, ev->code, value); + ev++; + } +} + +void +litest_tablet_proximity_out(struct litest_device *d) +{ + struct input_event *ev; + + ev = d->interface->tablet_proximity_out_events; + while (ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1) { + int value = auto_assign_tablet_value(d, ev, -1, -1, NULL); + if (!tablet_ignore_event(ev, value)) + litest_event(d, ev->type, ev->code, value); + ev++; + } +} + +void +litest_tablet_motion(struct litest_device *d, int x, int y, struct axis_replacement *axes) +{ + struct input_event *ev; + + ev = d->interface->tablet_motion_events; + while (ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1) { + int value = auto_assign_tablet_value(d, ev, x, y, axes); + if (!tablet_ignore_event(ev, value)) + litest_event(d, ev->type, ev->code, value); + ev++; + } +} + void litest_touch_move_two_touches(struct litest_device *d, double x0, double y0, @@ -1801,6 +1885,18 @@ litest_event_type_str(struct libinput_event *event) case LIBINPUT_EVENT_GESTURE_PINCH_END: str = "GESTURE PINCH END"; break; + case LIBINPUT_EVENT_TABLET_TOOL_AXIS: + str = "TABLET AXIS"; + break; + case LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY: + str = "TABLET PROX"; + break; + case LIBINPUT_EVENT_TABLET_TOOL_TIP: + str = "TABLET TIP"; + break; + case LIBINPUT_EVENT_TABLET_TOOL_BUTTON: + str = "TABLET BUTTON"; + break; } return str; } @@ -1809,6 +1905,7 @@ static void litest_print_event(struct libinput_event *event) { struct libinput_event_pointer *p; + struct libinput_event_tablet_tool *t; struct libinput_device *dev; enum libinput_event_type type; double x, y; @@ -1854,6 +1951,22 @@ litest_print_event(struct libinput_event *event) LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL); fprintf(stderr, "vert %.f horiz %.2f", y, x); break; + case LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY: + t = libinput_event_get_tablet_tool_event(event); + fprintf(stderr, "proximity %d\n", + libinput_event_tablet_tool_get_proximity_state(t)); + break; + case LIBINPUT_EVENT_TABLET_TOOL_TIP: + t = libinput_event_get_tablet_tool_event(event); + fprintf(stderr, "tip %d\n", + libinput_event_tablet_tool_get_tip_state(t)); + break; + case LIBINPUT_EVENT_TABLET_TOOL_BUTTON: + t = libinput_event_get_tablet_tool_event(event); + fprintf(stderr, "button %d state %d\n", + libinput_event_tablet_tool_get_button(t), + libinput_event_tablet_tool_get_button_state(t)); + break; default: break; } @@ -2221,6 +2334,42 @@ litest_is_gesture_event(struct libinput_event *event, return gevent; } +struct libinput_event_tablet_tool * litest_is_tablet_event( + struct libinput_event *event, + enum libinput_event_type type) +{ + struct libinput_event_tablet_tool *tevent; + + litest_assert(event != NULL); + litest_assert_int_eq(libinput_event_get_type(event), type); + + tevent = libinput_event_get_tablet_tool_event(event); + litest_assert(tevent != NULL); + + return tevent; +} + +void +litest_assert_tablet_button_event(struct libinput *li, unsigned int button, + enum libinput_button_state state) +{ + struct libinput_event *event; + struct libinput_event_tablet_tool *tev; + enum libinput_event_type type = LIBINPUT_EVENT_TABLET_TOOL_BUTTON; + + litest_wait_for_event(li); + event = libinput_get_event(li); + + litest_assert_notnull(event); + litest_assert_int_eq(libinput_event_get_type(event), type); + tev = libinput_event_get_tablet_tool_event(event); + litest_assert_int_eq(libinput_event_tablet_tool_get_button(tev), + button); + litest_assert_int_eq(libinput_event_tablet_tool_get_button_state(tev), + state); + libinput_event_destroy(event); +} + void litest_assert_scroll(struct libinput *li, enum libinput_pointer_axis axis, diff --git a/test/litest.h b/test/litest.h index ed23c8a2..ed9ddfe7 100644 --- a/test/litest.h +++ b/test/litest.h @@ -144,6 +144,11 @@ enum litest_device_type { LITEST_ELANTECH_TOUCHPAD = -30, LITEST_MOUSE_GLADIUS = -31, LITEST_MOUSE_WHEEL_CLICK_ANGLE = -32, + LITEST_WACOM_BAMBOO = -33, + LITEST_WACOM_CINTIQ = -34, + LITEST_WACOM_INTUOS = -35, + LITEST_WACOM_ISDV4 = -36, + LITEST_WALTOP = -37, }; enum litest_device_feature { @@ -166,6 +171,9 @@ enum litest_device_feature { LITEST_PROTOCOL_A = 1 << 14, LITEST_HOVER = 1 << 15, LITEST_ELLIPSE = 1 << 16, + LITEST_TABLET = 1 << 17, + LITEST_DISTANCE = 1 << 18, + LITEST_TOOL_SERIAL = 1 << 19, }; struct litest_device { @@ -334,6 +342,15 @@ void litest_touch_move_three_touches(struct litest_device *d, double x2, double y2, double dx, double dy, int steps, int sleep_ms); + +void litest_tablet_proximity_in(struct litest_device *d, + int x, int y, + struct axis_replacement *axes); +void litest_tablet_proximity_out(struct litest_device *d); +void litest_tablet_motion(struct litest_device *d, + int x, int y, + struct axis_replacement *axes); + void litest_hover_start(struct litest_device *d, unsigned int slot, double x, @@ -387,6 +404,9 @@ struct libinput_event_gesture * litest_is_gesture_event( struct libinput_event *event, enum libinput_event_type type, int nfingers); +struct libinput_event_tablet_tool * litest_is_tablet_event( + struct libinput_event *event, + enum libinput_event_type type); void litest_assert_button_event(struct libinput *li, unsigned int button, @@ -396,6 +416,9 @@ void litest_assert_scroll(struct libinput *li, int minimum_movement); void litest_assert_only_typed_events(struct libinput *li, enum libinput_event_type type); +void litest_assert_tablet_button_event(struct libinput *li, + unsigned int button, + enum libinput_button_state state); struct libevdev_uinput * litest_create_uinput_device(const char *name, struct input_id *id, @@ -404,6 +427,23 @@ struct libevdev_uinput * litest_create_uinput_abs_device(const char *name, struct input_id *id, const struct input_absinfo *abs, ...); +#define litest_assert_double_eq(a_, b_)\ + ck_assert_int_eq((int)((a_) * 256), (int)((b_) * 256)) + +#define litest_assert_double_ne(a_, b_)\ + ck_assert_int_ne((int)((a_) * 256), (int)((b_) * 256)) + +#define litest_assert_double_lt(a_, b_)\ + ck_assert_int_lt((int)((a_) * 256), (int)((b_) * 256)) + +#define litest_assert_double_le(a_, b_)\ + ck_assert_int_le((int)((a_) * 256), (int)((b_) * 256)) + +#define litest_assert_double_gt(a_, b_)\ + ck_assert_int_gt((int)((a_) * 256), (int)((b_) * 256)) + +#define litest_assert_double_ge(a_, b_)\ + ck_assert_int_ge((int)((a_) * 256), (int)((b_) * 256)) void litest_timeout_tap(void); void litest_timeout_tapndrag(void); diff --git a/test/misc.c b/test/misc.c index 89edb145..d8804961 100644 --- a/test/misc.c +++ b/test/misc.c @@ -31,6 +31,7 @@ #include <unistd.h> #include "litest.h" +#include "libinput-util.h" static int open_restricted(const char *path, int flags, void *data) { @@ -132,6 +133,7 @@ START_TEST(event_conversion_device_notify) ck_assert(libinput_event_get_keyboard_event(event) == NULL); ck_assert(libinput_event_get_touch_event(event) == NULL); ck_assert(libinput_event_get_gesture_event(event) == NULL); + ck_assert(libinput_event_get_tablet_tool_event(event) == NULL); litest_restore_log_handler(li); } @@ -187,6 +189,7 @@ START_TEST(event_conversion_pointer) ck_assert(libinput_event_get_keyboard_event(event) == NULL); ck_assert(libinput_event_get_touch_event(event) == NULL); ck_assert(libinput_event_get_gesture_event(event) == NULL); + ck_assert(libinput_event_get_tablet_tool_event(event) == NULL); litest_restore_log_handler(li); } libinput_event_destroy(event); @@ -236,6 +239,7 @@ START_TEST(event_conversion_pointer_abs) ck_assert(libinput_event_get_keyboard_event(event) == NULL); ck_assert(libinput_event_get_touch_event(event) == NULL); ck_assert(libinput_event_get_gesture_event(event) == NULL); + ck_assert(libinput_event_get_tablet_tool_event(event) == NULL); litest_restore_log_handler(li); } libinput_event_destroy(event); @@ -278,6 +282,7 @@ START_TEST(event_conversion_key) ck_assert(libinput_event_get_pointer_event(event) == NULL); ck_assert(libinput_event_get_touch_event(event) == NULL); ck_assert(libinput_event_get_gesture_event(event) == NULL); + ck_assert(libinput_event_get_tablet_tool_event(event) == NULL); litest_restore_log_handler(li); } libinput_event_destroy(event); @@ -327,6 +332,7 @@ START_TEST(event_conversion_touch) ck_assert(libinput_event_get_pointer_event(event) == NULL); ck_assert(libinput_event_get_keyboard_event(event) == NULL); ck_assert(libinput_event_get_gesture_event(event) == NULL); + ck_assert(libinput_event_get_tablet_tool_event(event) == NULL); litest_restore_log_handler(li); } libinput_event_destroy(event); @@ -384,6 +390,89 @@ START_TEST(event_conversion_gesture) } END_TEST +START_TEST(event_conversion_tablet) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event *event; + int events = 0; + struct axis_replacement axes[] = { + { ABS_DISTANCE, 10 }, + { -1, -1 } + }; + + litest_tablet_proximity_in(dev, 50, 50, axes); + litest_tablet_motion(dev, 60, 50, axes); + litest_button_click(dev, BTN_STYLUS, true); + litest_button_click(dev, BTN_STYLUS, false); + + libinput_dispatch(li); + + while ((event = libinput_get_event(li))) { + enum libinput_event_type type; + type = libinput_event_get_type(event); + + if (type >= LIBINPUT_EVENT_TABLET_TOOL_AXIS && + type <= LIBINPUT_EVENT_TABLET_TOOL_BUTTON) { + struct libinput_event_tablet_tool *t; + struct libinput_event *base; + t = libinput_event_get_tablet_tool_event(event); + base = libinput_event_tablet_tool_get_base_event(t); + ck_assert(event == base); + + events++; + + litest_disable_log_handler(li); + ck_assert(libinput_event_get_device_notify_event(event) == NULL); + ck_assert(libinput_event_get_pointer_event(event) == NULL); + ck_assert(libinput_event_get_keyboard_event(event) == NULL); + ck_assert(libinput_event_get_touch_event(event) == NULL); + litest_restore_log_handler(li); + } + libinput_event_destroy(event); + } + + ck_assert_int_gt(events, 0); +} +END_TEST + +START_TEST(bitfield_helpers) +{ + /* This value has a bit set on all of the word boundaries we want to + * test: 0, 1, 7, 8, 31, 32, and 33 + */ + unsigned char read_bitfield[] = { 0x83, 0x1, 0x0, 0x80, 0x3 }; + unsigned char write_bitfield[ARRAY_LENGTH(read_bitfield)]; + size_t i; + + /* Now check that the bitfield we wrote to came out to be the same as + * the bitfield we were writing from */ + for (i = 0; i < ARRAY_LENGTH(read_bitfield) * 8; i++) { + switch (i) { + case 0: + case 1: + case 7: + case 8: + case 31: + case 32: + case 33: + ck_assert(bit_is_set(read_bitfield, i)); + set_bit(write_bitfield, i); + break; + default: + ck_assert(!bit_is_set(read_bitfield, i)); + clear_bit(write_bitfield, i); + break; + } + } + + ck_assert_int_eq(memcmp(read_bitfield, + write_bitfield, + sizeof(read_bitfield)), + 0); +} +END_TEST + START_TEST(context_ref_counting) { struct libinput *li; @@ -703,6 +792,8 @@ litest_setup_tests(void) litest_add_for_device("events:conversion", event_conversion_key, LITEST_KEYBOARD); litest_add_for_device("events:conversion", event_conversion_touch, LITEST_WACOM_TOUCH); litest_add_for_device("events:conversion", event_conversion_gesture, LITEST_BCM5974); + litest_add_for_device("events:conversion", event_conversion_tablet, LITEST_WACOM_CINTIQ); + litest_add_no_device("bitfield_helpers", bitfield_helpers); litest_add_no_device("context:refcount", context_ref_counting); litest_add_no_device("config:status string", config_status_string); diff --git a/test/pointer.c b/test/pointer.c index ba158158..5c33d50f 100644 --- a/test/pointer.c +++ b/test/pointer.c @@ -1593,12 +1593,12 @@ litest_setup_tests(void) litest_add("pointer:button", pointer_button, LITEST_BUTTON, LITEST_CLICKPAD); litest_add_no_device("pointer:button", pointer_button_auto_release); litest_add_no_device("pointer:button", pointer_seat_button_count); - litest_add("pointer:scroll", pointer_scroll_wheel, LITEST_WHEEL, LITEST_ANY); + litest_add("pointer:scroll", pointer_scroll_wheel, LITEST_WHEEL, LITEST_TABLET); litest_add("pointer:scroll", pointer_scroll_button, LITEST_RELATIVE|LITEST_BUTTON, LITEST_ANY); litest_add("pointer:scroll", pointer_scroll_nowheel_defaults, LITEST_RELATIVE|LITEST_BUTTON, LITEST_WHEEL); - litest_add("pointer:scroll", pointer_scroll_natural_defaults, LITEST_WHEEL, LITEST_ANY); - litest_add("pointer:scroll", pointer_scroll_natural_enable_config, LITEST_WHEEL, LITEST_ANY); - litest_add("pointer:scroll", pointer_scroll_natural_wheel, LITEST_WHEEL, LITEST_ANY); + litest_add("pointer:scroll", pointer_scroll_natural_defaults, LITEST_WHEEL, LITEST_TABLET); + litest_add("pointer:scroll", pointer_scroll_natural_enable_config, LITEST_WHEEL, LITEST_TABLET); + litest_add("pointer:scroll", pointer_scroll_natural_wheel, LITEST_WHEEL, LITEST_TABLET); litest_add("pointer:calibration", pointer_no_calibration, LITEST_ANY, LITEST_TOUCH|LITEST_SINGLE_TOUCH|LITEST_ABSOLUTE|LITEST_PROTOCOL_A); diff --git a/test/tablet.c b/test/tablet.c new file mode 100644 index 00000000..c268045a --- /dev/null +++ b/test/tablet.c @@ -0,0 +1,2351 @@ +/* + * Copyright © 2014 Red Hat, Inc. + * Copyright © 2014 Stephen Chandler "Lyude" Paul + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that the above copyright notice appear in all copies and that both that + * copyright notice and this permission notice appear in supporting + * documentation, and that the name of the copyright holders not be used in + * advertising or publicity pertaining to distribution of the software + * without specific, written prior permission. The copyright holders make + * no representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + * + * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS + * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY + * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER + * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF + * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <config.h> + +#include <check.h> +#include <errno.h> +#include <fcntl.h> +#include <libinput.h> +#include <unistd.h> +#include <stdbool.h> + +#include "libinput-util.h" +#include "evdev-tablet.h" +#include "litest.h" + +START_TEST(tip_down_up) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event *event; + struct libinput_event_tablet_tool *tablet_event; + struct axis_replacement axes[] = { + { ABS_DISTANCE, 10 }, + { -1, -1 } + }; + + litest_tablet_proximity_in(dev, 10, 10, axes); + litest_drain_events(li); + + litest_event(dev, EV_KEY, BTN_TOUCH, 1); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + + libinput_dispatch(li); + event = libinput_get_event(li); + tablet_event = litest_is_tablet_event(event, + LIBINPUT_EVENT_TABLET_TOOL_TIP); + ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event), + LIBINPUT_TABLET_TOOL_TIP_DOWN); + libinput_event_destroy(event); + litest_assert_empty_queue(li); + + litest_event(dev, EV_KEY, BTN_TOUCH, 0); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + + libinput_dispatch(li); + event = libinput_get_event(li); + tablet_event = litest_is_tablet_event(event, + LIBINPUT_EVENT_TABLET_TOOL_TIP); + ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event), + LIBINPUT_TABLET_TOOL_TIP_UP); + libinput_event_destroy(event); + litest_assert_empty_queue(li); + + litest_assert_empty_queue(li); + +} +END_TEST + +START_TEST(tip_down_prox_in) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event *event; + struct libinput_event_tablet_tool *tablet_event; + struct axis_replacement axes[] = { + { ABS_DISTANCE, 10 }, + { -1, -1 } + }; + + litest_drain_events(li); + + litest_push_event_frame(dev); + litest_tablet_proximity_in(dev, 10, 10, axes); + litest_event(dev, EV_KEY, BTN_TOUCH, 1); + litest_pop_event_frame(dev); + + libinput_dispatch(li); + event = libinput_get_event(li); + tablet_event = litest_is_tablet_event(event, + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY); + ck_assert_int_eq(libinput_event_tablet_tool_get_proximity_state(tablet_event), + LIBINPUT_TABLET_TOOL_PROXIMITY_IN); + libinput_event_destroy(event); + + libinput_dispatch(li); + event = libinput_get_event(li); + tablet_event = litest_is_tablet_event(event, + LIBINPUT_EVENT_TABLET_TOOL_TIP); + ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event), + LIBINPUT_TABLET_TOOL_TIP_DOWN); + libinput_event_destroy(event); + + litest_assert_empty_queue(li); + +} +END_TEST + +START_TEST(tip_up_prox_out) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event *event; + struct libinput_event_tablet_tool *tablet_event; + struct axis_replacement axes[] = { + { ABS_DISTANCE, 10 }, + { -1, -1 } + }; + + litest_tablet_proximity_in(dev, 10, 10, axes); + litest_event(dev, EV_KEY, BTN_TOUCH, 1); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + litest_drain_events(li); + + litest_push_event_frame(dev); + litest_event(dev, EV_KEY, BTN_TOUCH, 0); + litest_tablet_proximity_out(dev); + litest_pop_event_frame(dev); + + libinput_dispatch(li); + event = libinput_get_event(li); + tablet_event = litest_is_tablet_event(event, + LIBINPUT_EVENT_TABLET_TOOL_TIP); + ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event), + LIBINPUT_TABLET_TOOL_TIP_UP); + libinput_event_destroy(event); + + libinput_dispatch(li); + event = libinput_get_event(li); + tablet_event = litest_is_tablet_event(event, + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY); + ck_assert_int_eq(libinput_event_tablet_tool_get_proximity_state(tablet_event), + LIBINPUT_TABLET_TOOL_PROXIMITY_OUT); + libinput_event_destroy(event); + + litest_assert_empty_queue(li); + +} +END_TEST + +START_TEST(tip_up_btn_change) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event *event; + struct libinput_event_tablet_tool *tablet_event; + struct axis_replacement axes[] = { + { ABS_DISTANCE, 10 }, + { -1, -1 } + }; + + litest_tablet_proximity_in(dev, 10, 10, axes); + litest_event(dev, EV_KEY, BTN_TOUCH, 1); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + litest_drain_events(li); + + litest_event(dev, EV_KEY, BTN_STYLUS, 1); + litest_event(dev, EV_KEY, BTN_TOUCH, 0); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + + libinput_dispatch(li); + event = libinput_get_event(li); + tablet_event = litest_is_tablet_event(event, + LIBINPUT_EVENT_TABLET_TOOL_BUTTON); + ck_assert_int_eq(libinput_event_tablet_tool_get_button(tablet_event), + BTN_STYLUS); + ck_assert_int_eq(libinput_event_tablet_tool_get_button_state(tablet_event), + LIBINPUT_BUTTON_STATE_PRESSED); + libinput_event_destroy(event); + + libinput_dispatch(li); + event = libinput_get_event(li); + tablet_event = litest_is_tablet_event(event, + LIBINPUT_EVENT_TABLET_TOOL_TIP); + ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event), + LIBINPUT_TABLET_TOOL_TIP_UP); + libinput_event_destroy(event); + + litest_assert_empty_queue(li); + + litest_event(dev, EV_KEY, BTN_TOUCH, 1); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + litest_drain_events(li); + + /* same thing with a release at tip-up */ + litest_event(dev, EV_KEY, BTN_TOUCH, 0); + litest_event(dev, EV_KEY, BTN_STYLUS, 0); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + + libinput_dispatch(li); + event = libinput_get_event(li); + tablet_event = litest_is_tablet_event(event, + LIBINPUT_EVENT_TABLET_TOOL_BUTTON); + ck_assert_int_eq(libinput_event_tablet_tool_get_button(tablet_event), + BTN_STYLUS); + ck_assert_int_eq(libinput_event_tablet_tool_get_button_state(tablet_event), + LIBINPUT_BUTTON_STATE_RELEASED); + libinput_event_destroy(event); + + libinput_dispatch(li); + event = libinput_get_event(li); + tablet_event = litest_is_tablet_event(event, + LIBINPUT_EVENT_TABLET_TOOL_TIP); + ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event), + LIBINPUT_TABLET_TOOL_TIP_UP); + libinput_event_destroy(event); + + litest_assert_empty_queue(li); +} +END_TEST + +START_TEST(tip_down_btn_change) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event *event; + struct libinput_event_tablet_tool *tablet_event; + struct axis_replacement axes[] = { + { ABS_DISTANCE, 10 }, + { -1, -1 } + }; + + litest_tablet_proximity_in(dev, 10, 10, axes); + litest_drain_events(li); + + litest_event(dev, EV_KEY, BTN_STYLUS, 1); + litest_event(dev, EV_KEY, BTN_TOUCH, 1); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + + libinput_dispatch(li); + event = libinput_get_event(li); + tablet_event = litest_is_tablet_event(event, + LIBINPUT_EVENT_TABLET_TOOL_TIP); + ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event), + LIBINPUT_TABLET_TOOL_TIP_DOWN); + libinput_event_destroy(event); + + libinput_dispatch(li); + event = libinput_get_event(li); + tablet_event = litest_is_tablet_event(event, + LIBINPUT_EVENT_TABLET_TOOL_BUTTON); + ck_assert_int_eq(libinput_event_tablet_tool_get_button(tablet_event), + BTN_STYLUS); + ck_assert_int_eq(libinput_event_tablet_tool_get_button_state(tablet_event), + LIBINPUT_BUTTON_STATE_PRESSED); + libinput_event_destroy(event); + + litest_assert_empty_queue(li); + + litest_event(dev, EV_KEY, BTN_TOUCH, 0); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + litest_drain_events(li); + + /* same thing with a release at tip-down */ + litest_event(dev, EV_KEY, BTN_TOUCH, 1); + litest_event(dev, EV_KEY, BTN_STYLUS, 0); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + + libinput_dispatch(li); + event = libinput_get_event(li); + tablet_event = litest_is_tablet_event(event, + LIBINPUT_EVENT_TABLET_TOOL_TIP); + ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event), + LIBINPUT_TABLET_TOOL_TIP_DOWN); + libinput_event_destroy(event); + + libinput_dispatch(li); + event = libinput_get_event(li); + tablet_event = litest_is_tablet_event(event, + LIBINPUT_EVENT_TABLET_TOOL_BUTTON); + ck_assert_int_eq(libinput_event_tablet_tool_get_button(tablet_event), + BTN_STYLUS); + ck_assert_int_eq(libinput_event_tablet_tool_get_button_state(tablet_event), + LIBINPUT_BUTTON_STATE_RELEASED); + libinput_event_destroy(event); + + litest_assert_empty_queue(li); +} +END_TEST + +START_TEST(tip_down_motion) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event *event; + struct libinput_event_tablet_tool *tablet_event; + struct axis_replacement axes[] = { + { ABS_DISTANCE, 10 }, + { -1, -1 } + }; + double x, y, last_x, last_y; + + litest_tablet_proximity_in(dev, 10, 10, axes); + litest_drain_events(li); + + litest_push_event_frame(dev); + litest_tablet_motion(dev, 70, 70, axes); + litest_event(dev, EV_KEY, BTN_TOUCH, 1); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + litest_pop_event_frame(dev); + + libinput_dispatch(li); + event = libinput_get_event(li); + tablet_event = litest_is_tablet_event(event, + LIBINPUT_EVENT_TABLET_TOOL_AXIS); + last_x = libinput_event_tablet_tool_get_axis_value(tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_X); + last_y = libinput_event_tablet_tool_get_axis_value(tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_Y); + libinput_event_destroy(event); + + libinput_dispatch(li); + event = libinput_get_event(li); + tablet_event = litest_is_tablet_event(event, + LIBINPUT_EVENT_TABLET_TOOL_TIP); + ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event), + LIBINPUT_TABLET_TOOL_TIP_DOWN); + x = libinput_event_tablet_tool_get_axis_value(tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_X); + y = libinput_event_tablet_tool_get_axis_value(tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_Y); + ck_assert_double_eq(last_x, x); + ck_assert_double_eq(last_y, y); + libinput_event_destroy(event); + + litest_assert_empty_queue(li); +} +END_TEST + +START_TEST(tip_up_motion) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event *event; + struct libinput_event_tablet_tool *tablet_event; + struct axis_replacement axes[] = { + { ABS_DISTANCE, 10 }, + { -1, -1 } + }; + double x, y, last_x, last_y; + + litest_tablet_proximity_in(dev, 10, 10, axes); + litest_event(dev, EV_KEY, BTN_TOUCH, 1); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + litest_drain_events(li); + + litest_push_event_frame(dev); + litest_tablet_motion(dev, 70, 70, axes); + litest_event(dev, EV_KEY, BTN_TOUCH, 0); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + litest_pop_event_frame(dev); + + libinput_dispatch(li); + event = libinput_get_event(li); + tablet_event = litest_is_tablet_event(event, + LIBINPUT_EVENT_TABLET_TOOL_AXIS); + last_x = libinput_event_tablet_tool_get_axis_value(tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_X); + last_y = libinput_event_tablet_tool_get_axis_value(tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_Y); + libinput_event_destroy(event); + + libinput_dispatch(li); + event = libinput_get_event(li); + tablet_event = litest_is_tablet_event(event, + LIBINPUT_EVENT_TABLET_TOOL_TIP); + ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event), + LIBINPUT_TABLET_TOOL_TIP_UP); + x = libinput_event_tablet_tool_get_axis_value(tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_X); + y = libinput_event_tablet_tool_get_axis_value(tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_Y); + ck_assert_double_eq(last_x, x); + ck_assert_double_eq(last_y, y); + libinput_event_destroy(event); + + litest_assert_empty_queue(li); +} +END_TEST + +START_TEST(tip_state_proximity) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event *event; + struct libinput_event_tablet_tool *tablet_event; + struct axis_replacement axes[] = { + { ABS_DISTANCE, 10 }, + { -1, -1 } + }; + + litest_drain_events(li); + + litest_tablet_proximity_in(dev, 10, 10, axes); + libinput_dispatch(li); + + event = libinput_get_event(li); + tablet_event = litest_is_tablet_event(event, + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY); + ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event), + LIBINPUT_TABLET_TOOL_TIP_UP); + libinput_event_destroy(event); + + litest_event(dev, EV_KEY, BTN_TOUCH, 1); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + litest_event(dev, EV_KEY, BTN_TOUCH, 0); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + litest_drain_events(li); + + litest_tablet_proximity_out(dev); + libinput_dispatch(li); + + event = libinput_get_event(li); + tablet_event = litest_is_tablet_event(event, + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY); + ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event), + LIBINPUT_TABLET_TOOL_TIP_UP); + libinput_event_destroy(event); +} +END_TEST + +START_TEST(tip_state_axis) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event *event; + struct libinput_event_tablet_tool *tablet_event; + struct axis_replacement axes[] = { + { ABS_DISTANCE, 10 }, + { -1, -1 } + }; + + litest_tablet_proximity_in(dev, 10, 10, axes); + litest_drain_events(li); + + litest_tablet_motion(dev, 70, 70, axes); + libinput_dispatch(li); + + event = libinput_get_event(li); + tablet_event = litest_is_tablet_event(event, + LIBINPUT_EVENT_TABLET_TOOL_AXIS); + ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event), + LIBINPUT_TABLET_TOOL_TIP_UP); + libinput_event_destroy(event); + + litest_event(dev, EV_KEY, BTN_TOUCH, 1); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + litest_drain_events(li); + + litest_tablet_motion(dev, 30, 30, axes); + libinput_dispatch(li); + + event = libinput_get_event(li); + tablet_event = litest_is_tablet_event(event, + LIBINPUT_EVENT_TABLET_TOOL_AXIS); + ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event), + LIBINPUT_TABLET_TOOL_TIP_DOWN); + libinput_event_destroy(event); + + litest_event(dev, EV_KEY, BTN_TOUCH, 0); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + litest_drain_events(li); + + litest_tablet_motion(dev, 40, 80, axes); + libinput_dispatch(li); + + event = libinput_get_event(li); + tablet_event = litest_is_tablet_event(event, + LIBINPUT_EVENT_TABLET_TOOL_AXIS); + ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event), + LIBINPUT_TABLET_TOOL_TIP_UP); + libinput_event_destroy(event); + + litest_assert_empty_queue(li); +} +END_TEST + +START_TEST(tip_state_button) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event *event; + struct libinput_event_tablet_tool *tablet_event; + struct axis_replacement axes[] = { + { ABS_DISTANCE, 10 }, + { -1, -1 } + }; + + litest_tablet_proximity_in(dev, 10, 10, axes); + litest_drain_events(li); + + litest_event(dev, EV_KEY, BTN_STYLUS, 1); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + libinput_dispatch(li); + + event = libinput_get_event(li); + tablet_event = litest_is_tablet_event(event, + LIBINPUT_EVENT_TABLET_TOOL_BUTTON); + ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event), + LIBINPUT_TABLET_TOOL_TIP_UP); + libinput_event_destroy(event); + + litest_event(dev, EV_KEY, BTN_TOUCH, 1); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + litest_drain_events(li); + + litest_event(dev, EV_KEY, BTN_STYLUS, 0); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + libinput_dispatch(li); + + event = libinput_get_event(li); + tablet_event = litest_is_tablet_event(event, + LIBINPUT_EVENT_TABLET_TOOL_BUTTON); + ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event), + LIBINPUT_TABLET_TOOL_TIP_DOWN); + libinput_event_destroy(event); + + litest_event(dev, EV_KEY, BTN_TOUCH, 0); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + litest_drain_events(li); + + litest_event(dev, EV_KEY, BTN_STYLUS, 1); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + libinput_dispatch(li); + + event = libinput_get_event(li); + tablet_event = litest_is_tablet_event(event, + LIBINPUT_EVENT_TABLET_TOOL_BUTTON); + ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event), + LIBINPUT_TABLET_TOOL_TIP_UP); + libinput_event_destroy(event); + + litest_event(dev, EV_KEY, BTN_STYLUS, 0); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + libinput_dispatch(li); + + event = libinput_get_event(li); + tablet_event = litest_is_tablet_event(event, + LIBINPUT_EVENT_TABLET_TOOL_BUTTON); + ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event), + LIBINPUT_TABLET_TOOL_TIP_UP); + libinput_event_destroy(event); + + litest_assert_empty_queue(li); +} +END_TEST + +START_TEST(proximity_in_out) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event_tablet_tool *tablet_event; + struct libinput_event *event; + bool have_tool_update = false, + have_proximity_out = false; + + struct axis_replacement axes[] = { + { ABS_DISTANCE, 10 }, + { -1, -1 } + }; + + litest_drain_events(li); + + litest_tablet_proximity_in(dev, 10, 10, axes); + libinput_dispatch(li); + + while ((event = libinput_get_event(li))) { + if (libinput_event_get_type(event) == + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY) { + struct libinput_tablet_tool * tool; + + have_tool_update++; + tablet_event = libinput_event_get_tablet_tool_event(event); + tool = libinput_event_tablet_tool_get_tool(tablet_event); + ck_assert_int_eq(libinput_tablet_tool_get_type(tool), + LIBINPUT_TABLET_TOOL_TYPE_PEN); + } + libinput_event_destroy(event); + } + ck_assert(have_tool_update); + + litest_tablet_proximity_out(dev); + libinput_dispatch(li); + + while ((event = libinput_get_event(li))) { + if (libinput_event_get_type(event) == + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY) { + struct libinput_event_tablet_tool *t = + libinput_event_get_tablet_tool_event(event); + + if (libinput_event_tablet_tool_get_proximity_state(t) == + LIBINPUT_TABLET_TOOL_PROXIMITY_OUT) + have_proximity_out = true; + } + + libinput_event_destroy(event); + } + ck_assert(have_proximity_out); + + /* Proximity out must not emit axis events */ + litest_tablet_proximity_out(dev); + libinput_dispatch(li); + + while ((event = libinput_get_event(li))) { + enum libinput_event_type type = libinput_event_get_type(event); + + ck_assert(type != LIBINPUT_EVENT_TABLET_TOOL_AXIS); + + libinput_event_destroy(event); + } +} +END_TEST + +START_TEST(proximity_out_clear_buttons) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event_tablet_tool *tablet_event; + struct libinput_event *event; + uint32_t button; + + struct axis_replacement axes[] = { + { ABS_DISTANCE, 10 }, + { -1, -1 } + }; + + litest_drain_events(li); + + /* Test that proximity out events send button releases for any currently + * pressed stylus buttons + */ + for (button = BTN_TOUCH + 1; button <= BTN_STYLUS2; button++) { + bool button_released = false; + uint32_t event_button; + enum libinput_button_state state; + + if (!libevdev_has_event_code(dev->evdev, EV_KEY, button)) + continue; + + litest_tablet_proximity_in(dev, 10, 10, axes); + litest_event(dev, EV_KEY, button, 1); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + litest_tablet_proximity_out(dev); + + libinput_dispatch(li); + + while ((event = libinput_get_event(li))) { + tablet_event = libinput_event_get_tablet_tool_event(event); + + if (libinput_event_get_type(event) == + LIBINPUT_EVENT_TABLET_TOOL_BUTTON) { + + event_button = libinput_event_tablet_tool_get_button(tablet_event); + state = libinput_event_tablet_tool_get_button_state(tablet_event); + + if (event_button == button && + state == LIBINPUT_BUTTON_STATE_RELEASED) + button_released = true; + } + + libinput_event_destroy(event); + } + + ck_assert_msg(button_released, + "Button %s (%d) was not released.", + libevdev_event_code_get_name(EV_KEY, button), + event_button); + } + + litest_assert_empty_queue(li); +} +END_TEST + +START_TEST(proximity_has_axes) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event_tablet_tool *tablet_event; + struct libinput_event *event; + struct libinput_tablet_tool *tool; + double x, y, + distance; + double last_x, last_y, last_distance, + last_tx, last_ty; + + struct axis_replacement axes[] = { + { ABS_DISTANCE, 10 }, + { ABS_TILT_X, 10 }, + { ABS_TILT_Y, 10 }, + { -1, -1} + }; + + litest_drain_events(li); + + litest_tablet_proximity_in(dev, 10, 10, axes); + + litest_wait_for_event_of_type(li, LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY, -1); + + event = libinput_get_event(li); + + tablet_event = libinput_event_get_tablet_tool_event(event); + tool = libinput_event_tablet_tool_get_tool(tablet_event); + + ck_assert(libinput_event_tablet_tool_axis_has_changed( + tablet_event, LIBINPUT_TABLET_TOOL_AXIS_X)); + ck_assert(libinput_event_tablet_tool_axis_has_changed( + tablet_event, LIBINPUT_TABLET_TOOL_AXIS_Y)); + + x = libinput_event_tablet_tool_get_axis_value(tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_X); + y = libinput_event_tablet_tool_get_axis_value(tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_Y); + + litest_assert_double_ne(x, 0); + litest_assert_double_ne(y, 0); + + if (libinput_tablet_tool_has_axis(tool, LIBINPUT_TABLET_TOOL_AXIS_DISTANCE)) { + ck_assert(libinput_event_tablet_tool_axis_has_changed( + tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_DISTANCE)); + + distance = libinput_event_tablet_tool_get_axis_value( + tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_DISTANCE); + litest_assert_double_ne(distance, 0); + } + + if (libinput_tablet_tool_has_axis(tool, LIBINPUT_TABLET_TOOL_AXIS_TILT_X) && + libinput_tablet_tool_has_axis(tool, LIBINPUT_TABLET_TOOL_AXIS_TILT_Y)) { + ck_assert(libinput_event_tablet_tool_axis_has_changed( + tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_TILT_X)); + ck_assert(libinput_event_tablet_tool_axis_has_changed( + tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_TILT_Y)); + + x = libinput_event_tablet_tool_get_axis_value( + tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_TILT_X); + y = libinput_event_tablet_tool_get_axis_value( + tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_TILT_Y); + + litest_assert_double_ne(x, 0); + litest_assert_double_ne(y, 0); + } + + litest_assert_empty_queue(li); + libinput_event_destroy(event); + + axes[0].value = 20; + axes[1].value = 15; + axes[2].value = 25; + litest_tablet_motion(dev, 20, 30, axes); + libinput_dispatch(li); + litest_wait_for_event_of_type(li, LIBINPUT_EVENT_TABLET_TOOL_AXIS, -1); + event = libinput_get_event(li); + tablet_event = libinput_event_get_tablet_tool_event(event); + + last_x = libinput_event_tablet_tool_get_axis_value(tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_X); + last_y = libinput_event_tablet_tool_get_axis_value(tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_Y); + if (libinput_tablet_tool_has_axis(tool, LIBINPUT_TABLET_TOOL_AXIS_DISTANCE)) + last_distance = libinput_event_tablet_tool_get_axis_value( + tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_DISTANCE); + if (libinput_tablet_tool_has_axis(tool, LIBINPUT_TABLET_TOOL_AXIS_TILT_X) && + libinput_tablet_tool_has_axis(tool, LIBINPUT_TABLET_TOOL_AXIS_TILT_Y)) { + last_tx = libinput_event_tablet_tool_get_axis_value( + tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_TILT_X); + last_ty = libinput_event_tablet_tool_get_axis_value( + tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_TILT_Y); + } + + libinput_event_destroy(event); + + /* Make sure that the axes are still present on proximity out */ + litest_tablet_proximity_out(dev); + + litest_wait_for_event_of_type(li, LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY, -1); + event = libinput_get_event(li); + + tablet_event = libinput_event_get_tablet_tool_event(event); + tool = libinput_event_tablet_tool_get_tool(tablet_event); + + ck_assert(!libinput_event_tablet_tool_axis_has_changed( + tablet_event, LIBINPUT_TABLET_TOOL_AXIS_X)); + ck_assert(!libinput_event_tablet_tool_axis_has_changed( + tablet_event, LIBINPUT_TABLET_TOOL_AXIS_Y)); + + x = libinput_event_tablet_tool_get_axis_value(tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_X); + y = libinput_event_tablet_tool_get_axis_value(tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_Y); + litest_assert_double_eq(x, last_x); + litest_assert_double_eq(y, last_y); + + if (libinput_tablet_tool_has_axis(tool, LIBINPUT_TABLET_TOOL_AXIS_DISTANCE)) { + ck_assert(!libinput_event_tablet_tool_axis_has_changed( + tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_DISTANCE)); + + distance = libinput_event_tablet_tool_get_axis_value( + tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_DISTANCE); + litest_assert_double_eq(distance, last_distance); + } + + if (libinput_tablet_tool_has_axis(tool, LIBINPUT_TABLET_TOOL_AXIS_TILT_X) && + libinput_tablet_tool_has_axis(tool, LIBINPUT_TABLET_TOOL_AXIS_TILT_Y)) { + ck_assert(!libinput_event_tablet_tool_axis_has_changed( + tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_TILT_X)); + ck_assert(!libinput_event_tablet_tool_axis_has_changed( + tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_TILT_Y)); + + x = libinput_event_tablet_tool_get_axis_value( + tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_TILT_X); + y = libinput_event_tablet_tool_get_axis_value( + tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_TILT_Y); + + litest_assert_double_eq(x, last_tx); + litest_assert_double_eq(y, last_ty); + } + + litest_assert_empty_queue(li); + libinput_event_destroy(event); +} +END_TEST + +START_TEST(motion) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event_tablet_tool *tablet_event; + struct libinput_event *event; + int test_x, test_y; + double last_reported_x = 0, last_reported_y = 0; + enum libinput_event_type type; + struct axis_replacement axes[] = { + { ABS_DISTANCE, 10 }, + { -1, -1 } + }; + + litest_drain_events(li); + + litest_tablet_proximity_in(dev, 5, 100, axes); + libinput_dispatch(li); + + litest_wait_for_event_of_type(li, + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY, + -1); + + while ((event = libinput_get_event(li))) { + bool x_changed, y_changed; + double reported_x, reported_y; + + tablet_event = libinput_event_get_tablet_tool_event(event); + ck_assert_int_eq(libinput_event_get_type(event), + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY); + + x_changed = libinput_event_tablet_tool_axis_has_changed( + tablet_event, LIBINPUT_TABLET_TOOL_AXIS_X); + y_changed = libinput_event_tablet_tool_axis_has_changed( + tablet_event, LIBINPUT_TABLET_TOOL_AXIS_Y); + + ck_assert(x_changed); + ck_assert(y_changed); + + reported_x = libinput_event_tablet_tool_get_axis_value( + tablet_event, LIBINPUT_TABLET_TOOL_AXIS_X); + reported_y = libinput_event_tablet_tool_get_axis_value( + tablet_event, LIBINPUT_TABLET_TOOL_AXIS_Y); + + litest_assert_double_lt(reported_x, reported_y); + + last_reported_x = reported_x; + last_reported_y = reported_y; + + libinput_event_destroy(event); + } + + for (test_x = 10, test_y = 90; + test_x <= 100; + test_x += 10, test_y -= 10) { + bool x_changed, y_changed; + double reported_x, reported_y; + + litest_tablet_motion(dev, test_x, test_y, axes); + libinput_dispatch(li); + + while ((event = libinput_get_event(li))) { + tablet_event = libinput_event_get_tablet_tool_event(event); + type = libinput_event_get_type(event); + + if (type == LIBINPUT_EVENT_TABLET_TOOL_AXIS) { + x_changed = libinput_event_tablet_tool_axis_has_changed( + tablet_event, LIBINPUT_TABLET_TOOL_AXIS_X); + y_changed = libinput_event_tablet_tool_axis_has_changed( + tablet_event, LIBINPUT_TABLET_TOOL_AXIS_Y); + + ck_assert(x_changed); + ck_assert(y_changed); + + reported_x = libinput_event_tablet_tool_get_axis_value( + tablet_event, LIBINPUT_TABLET_TOOL_AXIS_X); + reported_y = libinput_event_tablet_tool_get_axis_value( + tablet_event, LIBINPUT_TABLET_TOOL_AXIS_Y); + + litest_assert_double_gt(reported_x, + last_reported_x); + litest_assert_double_lt(reported_y, + last_reported_y); + + last_reported_x = reported_x; + last_reported_y = reported_y; + } + + libinput_event_destroy(event); + } + } +} +END_TEST + +START_TEST(motion_delta) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event_tablet_tool *tablet_event; + struct libinput_event *event; + double x1, y1, x2, y2, dist1, dist2; + double delta; + struct axis_replacement axes[] = { + { ABS_DISTANCE, 10 }, + { -1, -1 } + }; + + litest_drain_events(li); + + litest_tablet_proximity_in(dev, 5, 100, axes); + libinput_dispatch(li); + + litest_wait_for_event_of_type(li, + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY, + -1); + + event = libinput_get_event(li); + tablet_event = libinput_event_get_tablet_tool_event(event); + x1 = libinput_event_tablet_tool_get_axis_value(tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_X); + y1 = libinput_event_tablet_tool_get_axis_value(tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_Y); + dist1 = libinput_event_tablet_tool_get_axis_value(tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_DISTANCE); + libinput_event_destroy(event); + + axes[0].value = 40; + litest_tablet_motion(dev, 40, 100, axes); + + litest_wait_for_event_of_type(li, + LIBINPUT_EVENT_TABLET_TOOL_AXIS, + -1); + event = libinput_get_event(li); + tablet_event = libinput_event_get_tablet_tool_event(event); + x2 = libinput_event_tablet_tool_get_axis_value(tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_X); + y2 = libinput_event_tablet_tool_get_axis_value(tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_Y); + dist2 = libinput_event_tablet_tool_get_axis_value(tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_DISTANCE); + + delta = libinput_event_tablet_tool_get_axis_delta(tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_X); + litest_assert_double_eq(delta, x2 - x1); + delta = libinput_event_tablet_tool_get_axis_delta(tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_Y); + litest_assert_double_eq(delta, y2 - y1); + delta = libinput_event_tablet_tool_get_axis_delta(tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_DISTANCE); + litest_assert_double_eq(delta, dist2 - dist1); + + libinput_event_destroy(event); +} +END_TEST + +START_TEST(motion_delta_partial) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event_tablet_tool *tablet_event; + struct libinput_event *event; + double dx, dy, ddist; + struct axis_replacement axes[] = { + { ABS_DISTANCE, 10 }, + { -1, -1 } + }; + + if (!libevdev_has_event_code(dev->evdev, EV_ABS, ABS_DISTANCE)) + return; + + litest_tablet_proximity_in(dev, 5, 100, axes); + litest_tablet_motion(dev, 40, 100, axes); + litest_drain_events(li); + + axes[0].value = 40; + litest_tablet_motion(dev, 40, 100, axes); + litest_wait_for_event_of_type(li, + LIBINPUT_EVENT_TABLET_TOOL_AXIS, + -1); + event = libinput_get_event(li); + tablet_event = libinput_event_get_tablet_tool_event(event); + + ck_assert(!libinput_event_tablet_tool_axis_has_changed(tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_X)); + dx = libinput_event_tablet_tool_get_axis_delta(tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_X); + litest_assert_double_eq(dx, 0.0); + + ck_assert(!libinput_event_tablet_tool_axis_has_changed(tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_X)); + dy = libinput_event_tablet_tool_get_axis_delta(tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_Y); + litest_assert_double_eq(dy, 0.0); + + ck_assert(libinput_event_tablet_tool_axis_has_changed(tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_DISTANCE)); + ddist = libinput_event_tablet_tool_get_axis_delta(tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_DISTANCE); + ck_assert_double_gt(ddist, 0); + + libinput_event_destroy(event); +} +END_TEST + +START_TEST(left_handed) +{ +#if HAVE_LIBWACOM + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event *event; + struct libinput_event_tablet_tool *tablet_event; + double libinput_max_x, libinput_max_y; + double last_x = -1.0, last_y = -1.0; + struct axis_replacement axes[] = { + { ABS_DISTANCE, 10 }, + { -1, -1 } + }; + + ck_assert(libinput_device_config_left_handed_is_available(dev->libinput_device)); + + libinput_device_get_size (dev->libinput_device, + &libinput_max_x, + &libinput_max_y); + + /* Test that left-handed mode doesn't go into effect until the tool has + * left proximity of the tablet. In order to test this, we have to bring + * the tool into proximity and make sure libinput processes the + * proximity events so that it updates it's internal tablet state, and + * then try setting it to left-handed mode. */ + litest_tablet_proximity_in(dev, 0, 100, axes); + libinput_dispatch(li); + libinput_device_config_left_handed_set(dev->libinput_device, 1); + + litest_wait_for_event_of_type(li, LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY, -1); + + while ((event = libinput_get_event(li))) { + tablet_event = libinput_event_get_tablet_tool_event(event); + + last_x = libinput_event_tablet_tool_get_axis_value( + tablet_event, LIBINPUT_TABLET_TOOL_AXIS_X); + last_y = libinput_event_tablet_tool_get_axis_value( + tablet_event, LIBINPUT_TABLET_TOOL_AXIS_Y); + + litest_assert_double_eq(last_x, 0); + litest_assert_double_eq(last_y, libinput_max_y); + + libinput_event_destroy(event); + } + + litest_tablet_motion(dev, 100, 0, axes); + litest_wait_for_event_of_type(li, LIBINPUT_EVENT_TABLET_TOOL_AXIS, -1); + + while ((event = libinput_get_event(li))) { + double x, y; + tablet_event = libinput_event_get_tablet_tool_event(event); + + x = libinput_event_tablet_tool_get_axis_value( + tablet_event, LIBINPUT_TABLET_TOOL_AXIS_X); + y = libinput_event_tablet_tool_get_axis_value( + tablet_event, LIBINPUT_TABLET_TOOL_AXIS_Y); + + litest_assert_double_eq(x, libinput_max_x); + litest_assert_double_eq(y, 0); + + litest_assert_double_gt(x, last_x); + litest_assert_double_lt(y, last_y); + + libinput_event_destroy(event); + } + + litest_tablet_proximity_out(dev); + litest_drain_events(li); + + /* Since we've drained the events and libinput's aware the tool is out + * of proximity, it should have finally transitioned into left-handed + * mode, so the axes should be inverted once we bring it back into + * proximity */ + litest_tablet_proximity_in(dev, 0, 100, axes); + + litest_wait_for_event_of_type(li, LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY, -1); + + while ((event = libinput_get_event(li))) { + tablet_event = libinput_event_get_tablet_tool_event(event); + + last_x = libinput_event_tablet_tool_get_axis_value( + tablet_event, LIBINPUT_TABLET_TOOL_AXIS_X); + last_y = libinput_event_tablet_tool_get_axis_value( + tablet_event, LIBINPUT_TABLET_TOOL_AXIS_Y); + + litest_assert_double_eq(last_x, libinput_max_x); + litest_assert_double_eq(last_y, 0); + + libinput_event_destroy(event); + } + + litest_tablet_motion(dev, 100, 0, axes); + litest_wait_for_event_of_type(li, LIBINPUT_EVENT_TABLET_TOOL_AXIS, -1); + + while ((event = libinput_get_event(li))) { + double x, y; + tablet_event = libinput_event_get_tablet_tool_event(event); + + x = libinput_event_tablet_tool_get_axis_value( + tablet_event, LIBINPUT_TABLET_TOOL_AXIS_X); + y = libinput_event_tablet_tool_get_axis_value( + tablet_event, LIBINPUT_TABLET_TOOL_AXIS_Y); + + litest_assert_double_eq(x, 0); + litest_assert_double_eq(y, libinput_max_y); + + litest_assert_double_lt(x, last_x); + litest_assert_double_gt(y, last_y); + + libinput_event_destroy(event); + } +#endif +} +END_TEST + +START_TEST(no_left_handed) +{ + struct litest_device *dev = litest_current_device(); + + ck_assert(!libinput_device_config_left_handed_is_available(dev->libinput_device)); +} +END_TEST + +START_TEST(motion_event_state) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event *event; + struct libinput_event_tablet_tool *tablet_event; + int test_x, test_y; + double last_x, last_y; + + struct axis_replacement axes[] = { + { ABS_DISTANCE, 10 }, + { -1, -1 } + }; + + litest_drain_events(li); + litest_tablet_proximity_in(dev, 5, 100, axes); + litest_drain_events(li); + + /* couple of events that go left/bottom to right/top */ + for (test_x = 0, test_y = 100; test_x < 100; test_x += 10, test_y -= 10) + litest_tablet_motion(dev, test_x, test_y, axes); + + libinput_dispatch(li); + + while ((event = libinput_get_event(li))) { + if (libinput_event_get_type(event) == LIBINPUT_EVENT_TABLET_TOOL_AXIS) + break; + libinput_event_destroy(event); + } + + /* pop the first event off */ + ck_assert_notnull(event); + tablet_event = libinput_event_get_tablet_tool_event(event); + ck_assert_notnull(tablet_event); + + last_x = libinput_event_tablet_tool_get_axis_value(tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_X); + last_y = libinput_event_tablet_tool_get_axis_value(tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_Y); + + /* mark with a button event, then go back to bottom/left */ + litest_event(dev, EV_KEY, BTN_STYLUS, 1); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + + for (test_x = 100, test_y = 0; test_x > 0; test_x -= 10, test_y += 10) + litest_tablet_motion(dev, test_x, test_y, axes); + + libinput_event_destroy(event); + libinput_dispatch(li); + ck_assert_int_eq(libinput_next_event_type(li), + LIBINPUT_EVENT_TABLET_TOOL_AXIS); + + /* we expect all events up to the button event to go from + bottom/left to top/right */ + while ((event = libinput_get_event(li))) { + double x, y; + + if (libinput_event_get_type(event) != LIBINPUT_EVENT_TABLET_TOOL_AXIS) + break; + + tablet_event = libinput_event_get_tablet_tool_event(event); + ck_assert_notnull(tablet_event); + + x = libinput_event_tablet_tool_get_axis_value(tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_X); + y = libinput_event_tablet_tool_get_axis_value(tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_Y); + + ck_assert(x > last_x); + ck_assert(y < last_y); + + last_x = x; + last_y = y; + libinput_event_destroy(event); + } + + ck_assert_int_eq(libinput_event_get_type(event), + LIBINPUT_EVENT_TABLET_TOOL_BUTTON); + libinput_event_destroy(event); +} +END_TEST + +START_TEST(bad_distance_events) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + const struct input_absinfo *absinfo; + struct axis_replacement axes[] = { + { -1, -1 }, + }; + + litest_tablet_proximity_in(dev, 10, 10, axes); + litest_tablet_proximity_out(dev); + litest_drain_events(li); + + absinfo = libevdev_get_abs_info(dev->evdev, ABS_DISTANCE); + ck_assert(absinfo != NULL); + + litest_event(dev, EV_ABS, ABS_DISTANCE, absinfo->maximum); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + litest_event(dev, EV_ABS, ABS_DISTANCE, absinfo->minimum); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + + litest_assert_empty_queue(li); +} +END_TEST + +START_TEST(normalization) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event_tablet_tool *tablet_event; + struct libinput_event *event; + double pressure, + tilt_vertical, + tilt_horizontal; + const struct input_absinfo *pressure_absinfo, + *tilt_vertical_absinfo, + *tilt_horizontal_absinfo; + + litest_drain_events(li); + + pressure_absinfo = libevdev_get_abs_info(dev->evdev, ABS_PRESSURE); + tilt_vertical_absinfo = libevdev_get_abs_info(dev->evdev, ABS_TILT_X); + tilt_horizontal_absinfo = libevdev_get_abs_info(dev->evdev, ABS_TILT_Y); + + /* Test minimum */ + if (pressure_absinfo != NULL) + litest_event(dev, + EV_ABS, + ABS_PRESSURE, + pressure_absinfo->minimum); + + if (tilt_vertical_absinfo != NULL) + litest_event(dev, + EV_ABS, + ABS_TILT_X, + tilt_vertical_absinfo->minimum); + + if (tilt_horizontal_absinfo != NULL) + litest_event(dev, + EV_ABS, + ABS_TILT_Y, + tilt_horizontal_absinfo->minimum); + + litest_event(dev, EV_SYN, SYN_REPORT, 0); + + libinput_dispatch(li); + + while ((event = libinput_get_event(li))) { + if (libinput_event_get_type(event) == LIBINPUT_EVENT_TABLET_TOOL_AXIS) { + tablet_event = libinput_event_get_tablet_tool_event(event); + + if (libinput_event_tablet_tool_axis_has_changed( + tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_PRESSURE)) { + pressure = libinput_event_tablet_tool_get_axis_value( + tablet_event, LIBINPUT_TABLET_TOOL_AXIS_PRESSURE); + + litest_assert_double_eq(pressure, 0); + } + + if (libinput_event_tablet_tool_axis_has_changed( + tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_TILT_X)) { + tilt_vertical = + libinput_event_tablet_tool_get_axis_value( + tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_TILT_X); + + litest_assert_double_eq(tilt_vertical, -1); + } + + if (libinput_event_tablet_tool_axis_has_changed( + tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_TILT_Y)) { + tilt_horizontal = + libinput_event_tablet_tool_get_axis_value( + tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_TILT_Y); + + litest_assert_double_eq(tilt_horizontal, -1); + } + } + + libinput_event_destroy(event); + } + + /* Test maximum */ + if (pressure_absinfo != NULL) + litest_event(dev, + EV_ABS, + ABS_PRESSURE, + pressure_absinfo->maximum); + + if (tilt_vertical_absinfo != NULL) + litest_event(dev, + EV_ABS, + ABS_TILT_X, + tilt_vertical_absinfo->maximum); + + if (tilt_horizontal_absinfo != NULL) + litest_event(dev, + EV_ABS, + ABS_TILT_Y, + tilt_horizontal_absinfo->maximum); + + litest_event(dev, EV_SYN, SYN_REPORT, 0); + + libinput_dispatch(li); + + while ((event = libinput_get_event(li))) { + if (libinput_event_get_type(event) == LIBINPUT_EVENT_TABLET_TOOL_AXIS) { + tablet_event = libinput_event_get_tablet_tool_event(event); + + if (libinput_event_tablet_tool_axis_has_changed( + tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_PRESSURE)) { + pressure = libinput_event_tablet_tool_get_axis_value( + tablet_event, LIBINPUT_TABLET_TOOL_AXIS_PRESSURE); + + litest_assert_double_eq(pressure, 1); + } + + if (libinput_event_tablet_tool_axis_has_changed( + tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_TILT_X)) { + tilt_vertical = + libinput_event_tablet_tool_get_axis_value( + tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_TILT_X); + + litest_assert_double_eq(tilt_vertical, 1); + } + + if (libinput_event_tablet_tool_axis_has_changed( + tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_TILT_Y)) { + tilt_horizontal = + libinput_event_tablet_tool_get_axis_value( + tablet_event, + LIBINPUT_TABLET_TOOL_AXIS_TILT_Y); + + litest_assert_double_eq(tilt_horizontal, 1); + } + } + + libinput_event_destroy(event); + } + +} +END_TEST + +START_TEST(tool_serial) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event_tablet_tool *tablet_event; + struct libinput_event *event; + struct libinput_tablet_tool *tool; + + litest_drain_events(li); + + litest_event(dev, EV_KEY, BTN_TOOL_PEN, 1); + litest_event(dev, EV_MSC, MSC_SERIAL, 1000); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + + litest_wait_for_event_of_type(li, + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY, + -1); + event = libinput_get_event(li); + tablet_event = libinput_event_get_tablet_tool_event(event); + tool = libinput_event_tablet_tool_get_tool(tablet_event); + ck_assert_uint_eq(libinput_tablet_tool_get_serial(tool), 1000); + libinput_event_destroy(event); +} +END_TEST + +START_TEST(serial_changes_tool) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event_tablet_tool *tablet_event; + struct libinput_event *event; + struct libinput_tablet_tool *tool; + + litest_drain_events(li); + + litest_event(dev, EV_KEY, BTN_TOOL_PEN, 1); + litest_event(dev, EV_MSC, MSC_SERIAL, 1000); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + litest_event(dev, EV_KEY, BTN_TOOL_PEN, 0); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + litest_drain_events(li); + + litest_event(dev, EV_KEY, BTN_TOOL_PEN, 1); + litest_event(dev, EV_MSC, MSC_SERIAL, 2000); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + + litest_wait_for_event_of_type(li, + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY, + -1); + event = libinput_get_event(li); + tablet_event = libinput_event_get_tablet_tool_event(event); + tool = libinput_event_tablet_tool_get_tool(tablet_event); + + ck_assert_uint_eq(libinput_tablet_tool_get_serial(tool), 2000); + libinput_event_destroy(event); +} +END_TEST + +START_TEST(invalid_serials) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event *event; + struct libinput_event_tablet_tool *tablet_event; + struct libinput_tablet_tool *tool; + + litest_drain_events(li); + + litest_event(dev, EV_KEY, BTN_TOOL_PEN, 1); + litest_event(dev, EV_MSC, MSC_SERIAL, 1000); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + litest_event(dev, EV_KEY, BTN_TOOL_PEN, 0); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + litest_drain_events(li); + + litest_event(dev, EV_KEY, BTN_TOOL_PEN, 1); + litest_event(dev, EV_MSC, MSC_SERIAL, -1); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + + libinput_dispatch(li); + while ((event = libinput_get_event(li))) { + if (libinput_event_get_type(event) == + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY) { + tablet_event = libinput_event_get_tablet_tool_event(event); + tool = libinput_event_tablet_tool_get_tool(tablet_event); + + ck_assert_uint_eq(libinput_tablet_tool_get_serial(tool), 1000); + } + + libinput_event_destroy(event); + } +} +END_TEST + +START_TEST(tool_ref) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event_tablet_tool *tablet_event; + struct libinput_event *event; + struct libinput_tablet_tool *tool; + + litest_drain_events(li); + + litest_event(dev, EV_KEY, BTN_TOOL_PEN, 1); + litest_event(dev, EV_MSC, MSC_SERIAL, 1000); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + + litest_wait_for_event_of_type(li, + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY, + -1); + event = libinput_get_event(li); + tablet_event = libinput_event_get_tablet_tool_event(event); + tool = libinput_event_tablet_tool_get_tool(tablet_event); + + ck_assert_notnull(tool); + ck_assert(tool == libinput_tablet_tool_ref(tool)); + ck_assert(tool == libinput_tablet_tool_unref(tool)); + ck_assert(libinput_tablet_tool_unref(tool) == NULL); + + libinput_event_destroy(event); +} +END_TEST + +START_TEST(pad_buttons_ignored) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event *event; + struct axis_replacement axes[] = { + { ABS_DISTANCE, 10 }, + { -1, -1 } + }; + int button; + + litest_drain_events(li); + + for (button = BTN_0; button < BTN_MOUSE; button++) { + litest_event(dev, EV_KEY, button, 1); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + litest_event(dev, EV_KEY, button, 0); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + libinput_dispatch(li); + } + + while ((event = libinput_get_event(li))) { + ck_assert_int_ne(libinput_event_get_type(event), + LIBINPUT_EVENT_TABLET_TOOL_BUTTON); + libinput_event_destroy(event); + libinput_dispatch(li); + } + + /* same thing while in prox */ + litest_tablet_proximity_in(dev, 10, 10, axes); + for (button = BTN_0; button < BTN_MOUSE; button++) { + litest_event(dev, EV_KEY, button, 1); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + litest_event(dev, EV_KEY, button, 0); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + libinput_dispatch(li); + } + litest_tablet_proximity_out(dev); + + libinput_dispatch(li); + while ((event = libinput_get_event(li))) { + ck_assert_int_ne(libinput_event_get_type(event), + LIBINPUT_EVENT_TABLET_TOOL_BUTTON); + libinput_event_destroy(event); + libinput_dispatch(li); + } +} +END_TEST + +START_TEST(tools_with_serials) +{ + struct libinput *li = litest_create_context(); + struct litest_device *dev[2]; + struct libinput_tablet_tool *tool[2] = {0}; + struct libinput_event *event; + int i; + + for (i = 0; i < 2; i++) { + dev[i] = litest_add_device_with_overrides(li, + LITEST_WACOM_INTUOS, + NULL, + NULL, + NULL, + NULL); + /* WARNING: this test fails if UI_GET_SYSNAME isn't + * available or isn't used by libevdev (1.3, commit 2ff45c73). + * Put a sleep(1) here and that usually fixes it. + */ + + litest_event(dev[i], EV_KEY, BTN_TOOL_PEN, 1); + litest_event(dev[i], EV_MSC, MSC_SERIAL, 100); + litest_event(dev[i], EV_SYN, SYN_REPORT, 0); + + libinput_dispatch(li); + while ((event = libinput_get_event(li))) { + if (libinput_event_get_type(event) == + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY) { + struct libinput_event_tablet_tool *t = + libinput_event_get_tablet_tool_event(event); + + tool[i] = libinput_event_tablet_tool_get_tool(t); + } + + libinput_event_destroy(event); + } + } + + /* We should get the same object for both devices */ + ck_assert_notnull(tool[0]); + ck_assert_notnull(tool[1]); + ck_assert_ptr_eq(tool[0], tool[1]); + + litest_delete_device(dev[0]); + litest_delete_device(dev[1]); + libinput_unref(li); +} +END_TEST + +START_TEST(tools_without_serials) +{ + struct libinput *li = litest_create_context(); + struct litest_device *dev[2]; + struct libinput_tablet_tool *tool[2] = {0}; + struct libinput_event *event; + int i; + + for (i = 0; i < 2; i++) { + dev[i] = litest_add_device_with_overrides(li, + LITEST_WACOM_ISDV4, + NULL, + NULL, + NULL, + NULL); + + /* WARNING: this test fails if UI_GET_SYSNAME isn't + * available or isn't used by libevdev (1.3, commit 2ff45c73). + * Put a sleep(1) here and that usually fixes it. + */ + + litest_event(dev[i], EV_KEY, BTN_TOOL_PEN, 1); + litest_event(dev[i], EV_SYN, SYN_REPORT, 0); + + libinput_dispatch(li); + while ((event = libinput_get_event(li))) { + if (libinput_event_get_type(event) == + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY) { + struct libinput_event_tablet_tool *t = + libinput_event_get_tablet_tool_event(event); + + tool[i] = libinput_event_tablet_tool_get_tool(t); + } + + libinput_event_destroy(event); + } + } + + /* We should get different tool objects for each device */ + ck_assert_notnull(tool[0]); + ck_assert_notnull(tool[1]); + ck_assert_ptr_ne(tool[0], tool[1]); + + litest_delete_device(dev[0]); + litest_delete_device(dev[1]); + libinput_unref(li); +} +END_TEST + +START_TEST(tool_capabilities) +{ + struct libinput *li = litest_create_context(); + struct litest_device *intuos; + struct litest_device *bamboo; + struct libinput_event *event; + struct libinput_event_tablet_tool *t; + struct libinput_tablet_tool *tool; + + /* The axis capabilities of a tool can differ depending on the type of + * tablet the tool is being used with */ + bamboo = litest_add_device(li, LITEST_WACOM_BAMBOO); + intuos = litest_add_device(li, LITEST_WACOM_INTUOS); + + litest_event(bamboo, EV_KEY, BTN_TOOL_PEN, 1); + litest_event(bamboo, EV_SYN, SYN_REPORT, 0); + + libinput_dispatch(li); + + litest_wait_for_event_of_type(li, + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY, + -1); + + event = libinput_get_event(li); + t = libinput_event_get_tablet_tool_event(event); + tool = libinput_event_tablet_tool_get_tool(t); + + ck_assert(libinput_tablet_tool_has_axis(tool, + LIBINPUT_TABLET_TOOL_AXIS_PRESSURE)); + ck_assert(libinput_tablet_tool_has_axis(tool, + LIBINPUT_TABLET_TOOL_AXIS_DISTANCE)); + ck_assert(!libinput_tablet_tool_has_axis(tool, + LIBINPUT_TABLET_TOOL_AXIS_TILT_X)); + ck_assert(!libinput_tablet_tool_has_axis(tool, + LIBINPUT_TABLET_TOOL_AXIS_TILT_Y)); + + libinput_event_destroy(event); + litest_assert_empty_queue(li); + + litest_event(intuos, EV_KEY, BTN_TOOL_PEN, 1); + litest_event(intuos, EV_SYN, SYN_REPORT, 0); + + litest_wait_for_event_of_type(li, + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY, + -1); + + event = libinput_get_event(li); + t = libinput_event_get_tablet_tool_event(event); + tool = libinput_event_tablet_tool_get_tool(t); + + ck_assert(libinput_tablet_tool_has_axis(tool, + LIBINPUT_TABLET_TOOL_AXIS_PRESSURE)); + ck_assert(libinput_tablet_tool_has_axis(tool, + LIBINPUT_TABLET_TOOL_AXIS_DISTANCE)); + ck_assert(libinput_tablet_tool_has_axis(tool, + LIBINPUT_TABLET_TOOL_AXIS_TILT_X)); + ck_assert(libinput_tablet_tool_has_axis(tool, + LIBINPUT_TABLET_TOOL_AXIS_TILT_Y)); + + libinput_event_destroy(event); + litest_assert_empty_queue(li); + + litest_delete_device(bamboo); + litest_delete_device(intuos); + libinput_unref(li); +} +END_TEST + +START_TEST(tool_in_prox_before_start) +{ + struct libinput *li; + struct litest_device *dev = litest_current_device(); + struct libinput_event *event; + struct axis_replacement axes[] = { + { ABS_DISTANCE, 10 }, + { ABS_TILT_X, 0 }, + { ABS_TILT_Y, 0 }, + { -1, -1 } + }; + const char *devnode; + + litest_tablet_proximity_in(dev, 10, 10, axes); + + /* for simplicity, we create a new litest context */ + devnode = libevdev_uinput_get_devnode(dev->uinput); + li = litest_create_context(); + libinput_path_add_device(li, devnode); + + litest_wait_for_event_of_type(li, + LIBINPUT_EVENT_DEVICE_ADDED, + -1); + event = libinput_get_event(li); + libinput_event_destroy(event); + + litest_wait_for_event_of_type(li, + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY, + -1); + event = libinput_get_event(li); + libinput_event_destroy(event); + litest_assert_empty_queue(li); + + litest_tablet_motion(dev, 10, 20, axes); + litest_tablet_motion(dev, 30, 40, axes); + + litest_assert_only_typed_events(li, LIBINPUT_EVENT_TABLET_TOOL_AXIS); + litest_assert_empty_queue(li); + litest_event(dev, EV_KEY, BTN_STYLUS, 1); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + litest_event(dev, EV_KEY, BTN_STYLUS, 1); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + litest_assert_only_typed_events(li, LIBINPUT_EVENT_TABLET_TOOL_BUTTON); + litest_tablet_proximity_out(dev); + + litest_wait_for_event_of_type(li, + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY, + -1); + libinput_unref(li); +} +END_TEST + +START_TEST(mouse_tool) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event *event; + struct libinput_event_tablet_tool *tev; + struct libinput_tablet_tool *tool; + + if (!libevdev_has_event_code(dev->evdev, + EV_KEY, + BTN_TOOL_MOUSE)) + return; + + litest_drain_events(li); + + litest_event(dev, EV_KEY, BTN_TOOL_MOUSE, 1); + litest_event(dev, EV_MSC, MSC_SERIAL, 1000); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + + litest_wait_for_event_of_type(li, + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY, + -1); + event = libinput_get_event(li); + tev = libinput_event_get_tablet_tool_event(event); + tool = libinput_event_tablet_tool_get_tool(tev); + ck_assert_notnull(tool); + ck_assert_int_eq(libinput_tablet_tool_get_type(tool), + LIBINPUT_TABLET_TOOL_TYPE_MOUSE); + + libinput_event_destroy(event); +} +END_TEST + +START_TEST(mouse_buttons) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event *event; + struct libinput_event_tablet_tool *tev; + struct libinput_tablet_tool *tool; + int code; + + if (!libevdev_has_event_code(dev->evdev, + EV_KEY, + BTN_TOOL_MOUSE)) + return; + + litest_drain_events(li); + + litest_event(dev, EV_KEY, BTN_TOOL_MOUSE, 1); + litest_event(dev, EV_ABS, ABS_MISC, 0x806); /* 5-button mouse tool_id */ + litest_event(dev, EV_MSC, MSC_SERIAL, 1000); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + + litest_wait_for_event_of_type(li, + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY, + -1); + event = libinput_get_event(li); + tev = libinput_event_get_tablet_tool_event(event); + tool = libinput_event_tablet_tool_get_tool(tev); + ck_assert_notnull(tool); + libinput_tablet_tool_ref(tool); + + libinput_event_destroy(event); + + for (code = BTN_LEFT; code <= BTN_TASK; code++) { + bool has_button = libevdev_has_event_code(dev->evdev, + EV_KEY, + code); + ck_assert_int_eq(!!has_button, + !!libinput_tablet_tool_has_button(tool, code)); + + if (!has_button) + continue; + + litest_event(dev, EV_KEY, code, 1); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + libinput_dispatch(li); + litest_event(dev, EV_KEY, code, 0); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + libinput_dispatch(li); + + litest_assert_tablet_button_event(li, + code, + LIBINPUT_BUTTON_STATE_PRESSED); + litest_assert_tablet_button_event(li, + code, + LIBINPUT_BUTTON_STATE_RELEASED); + } + + libinput_tablet_tool_unref(tool); +} +END_TEST + +START_TEST(mouse_rotation) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event *event; + struct libinput_event_tablet_tool *tev; + int angle; + int tilt_center_x, tilt_center_y; + const struct input_absinfo *abs; + double val, old_val = 0; + + struct axis_replacement axes[] = { + { ABS_DISTANCE, 10 }, + { ABS_TILT_X, 0 }, + { ABS_TILT_Y, 0 }, + { -1, -1 } + }; + + if (!libevdev_has_event_code(dev->evdev, + EV_KEY, + BTN_TOOL_MOUSE)) + return; + + abs = libevdev_get_abs_info(dev->evdev, ABS_TILT_X); + ck_assert_notnull(abs); + tilt_center_x = (abs->maximum - abs->minimum + 1) / 2; + + abs = libevdev_get_abs_info(dev->evdev, ABS_TILT_Y); + ck_assert_notnull(abs); + tilt_center_y = (abs->maximum - abs->minimum + 1) / 2; + + litest_drain_events(li); + + litest_push_event_frame(dev); + litest_tablet_proximity_in(dev, 10, 10, axes); + litest_event(dev, EV_KEY, BTN_TOOL_MOUSE, 1); + litest_pop_event_frame(dev); + + litest_drain_events(li); + + /* cos/sin are 90 degrees offset from the north-is-zero that + libinput uses. 175 is the CCW offset in the mouse HW */ + for (angle = 5; angle < 360; angle += 5) { + double a = (angle - 90 - 175)/180.0 * M_PI; + int x, y; + + x = cos(a) * 20 + tilt_center_x; + y = sin(a) * 20 + tilt_center_y; + + litest_event(dev, EV_ABS, ABS_TILT_X, x); + litest_event(dev, EV_ABS, ABS_TILT_Y, y); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + + litest_wait_for_event_of_type(li, + LIBINPUT_EVENT_TABLET_TOOL_AXIS, + -1); + event = libinput_get_event(li); + tev = libinput_event_get_tablet_tool_event(event); + ck_assert(libinput_event_tablet_tool_axis_has_changed(tev, + LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z)); + val = libinput_event_tablet_tool_get_axis_value(tev, + LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z); + + /* rounding error galore, we can't test for anything more + precise than these */ + litest_assert_double_lt(val, 360.0); + litest_assert_double_gt(val, old_val); + litest_assert_double_lt(val, angle + 5); + + old_val = val; + libinput_event_destroy(event); + litest_assert_empty_queue(li); + } +} +END_TEST + +START_TEST(mouse_wheel) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event *event; + struct libinput_event_tablet_tool *tev; + struct libinput_tablet_tool *tool; + const struct input_absinfo *abs; + double val; + int i; + + if (!libevdev_has_event_code(dev->evdev, + EV_REL, + REL_WHEEL)) + return; + + litest_drain_events(li); + + litest_event(dev, EV_KEY, BTN_TOOL_MOUSE, 1); + litest_event(dev, EV_ABS, ABS_MISC, 0x806); /* 5-button mouse tool_id */ + litest_event(dev, EV_MSC, MSC_SERIAL, 1000); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + + litest_wait_for_event_of_type(li, + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY, + -1); + event = libinput_get_event(li); + tev = libinput_event_get_tablet_tool_event(event); + tool = libinput_event_tablet_tool_get_tool(tev); + ck_assert_notnull(tool); + libinput_tablet_tool_ref(tool); + + libinput_event_destroy(event); + + ck_assert(libinput_tablet_tool_has_axis(tool, + LIBINPUT_TABLET_TOOL_AXIS_REL_WHEEL)); + + for (i = 0; i < 3; i++) { + litest_event(dev, EV_REL, REL_WHEEL, -1); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + + litest_wait_for_event_of_type(li, LIBINPUT_EVENT_TABLET_TOOL_AXIS, -1); + + event = libinput_get_event(li); + tev = libinput_event_get_tablet_tool_event(event); + ck_assert(libinput_event_tablet_tool_axis_has_changed(tev, + LIBINPUT_TABLET_TOOL_AXIS_REL_WHEEL)); + val = libinput_event_tablet_tool_get_axis_value(tev, + LIBINPUT_TABLET_TOOL_AXIS_REL_WHEEL); + ck_assert_int_eq(val, 0); + + val = libinput_event_tablet_tool_get_axis_delta(tev, + LIBINPUT_TABLET_TOOL_AXIS_REL_WHEEL); + ck_assert_int_eq(val, 15); + + val = libinput_event_tablet_tool_get_axis_delta_discrete(tev, + LIBINPUT_TABLET_TOOL_AXIS_REL_WHEEL); + ck_assert_int_eq(val, 1); + + libinput_event_destroy(event); + + litest_assert_empty_queue(li); + } + + for (i = 2; i < 5; i++) { + /* send x/y events to make sure we reset the wheel */ + abs = libevdev_get_abs_info(dev->evdev, ABS_X); + litest_event(dev, EV_ABS, ABS_X, (abs->maximum - abs->minimum)/i); + abs = libevdev_get_abs_info(dev->evdev, ABS_Y); + litest_event(dev, EV_ABS, ABS_Y, (abs->maximum - abs->minimum)/i); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + + litest_wait_for_event_of_type(li, LIBINPUT_EVENT_TABLET_TOOL_AXIS, -1); + + event = libinput_get_event(li); + tev = libinput_event_get_tablet_tool_event(event); + ck_assert(!libinput_event_tablet_tool_axis_has_changed(tev, + LIBINPUT_TABLET_TOOL_AXIS_REL_WHEEL)); + val = libinput_event_tablet_tool_get_axis_value(tev, + LIBINPUT_TABLET_TOOL_AXIS_REL_WHEEL); + ck_assert_int_eq(val, 0); + + val = libinput_event_tablet_tool_get_axis_delta(tev, + LIBINPUT_TABLET_TOOL_AXIS_REL_WHEEL); + ck_assert_int_eq(val, 0); + + val = libinput_event_tablet_tool_get_axis_delta_discrete(tev, + LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z); + ck_assert_int_eq(val, 0); + + libinput_event_destroy(event); + + litest_assert_empty_queue(li); + } + + libinput_tablet_tool_unref(tool); +} +END_TEST + +START_TEST(airbrush_tool) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event *event; + struct libinput_event_tablet_tool *tev; + struct libinput_tablet_tool *tool; + + if (!libevdev_has_event_code(dev->evdev, + EV_KEY, + BTN_TOOL_AIRBRUSH)) + return; + + litest_drain_events(li); + + litest_event(dev, EV_KEY, BTN_TOOL_AIRBRUSH, 1); + litest_event(dev, EV_MSC, MSC_SERIAL, 1000); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + + litest_wait_for_event_of_type(li, + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY, + -1); + event = libinput_get_event(li); + tev = libinput_event_get_tablet_tool_event(event); + tool = libinput_event_tablet_tool_get_tool(tev); + ck_assert_notnull(tool); + ck_assert_int_eq(libinput_tablet_tool_get_type(tool), + LIBINPUT_TABLET_TOOL_TYPE_AIRBRUSH); + + libinput_event_destroy(event); +} +END_TEST + +START_TEST(airbrush_wheel) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event *event; + struct libinput_event_tablet_tool *tev; + const struct input_absinfo *abs; + double val; + double scale; + int v; + + if (!libevdev_has_event_code(dev->evdev, + EV_KEY, + BTN_TOOL_AIRBRUSH)) + return; + + litest_drain_events(li); + + abs = libevdev_get_abs_info(dev->evdev, ABS_WHEEL); + ck_assert_notnull(abs); + + litest_event(dev, EV_KEY, BTN_TOOL_AIRBRUSH, 1); + litest_event(dev, EV_MSC, MSC_SERIAL, 1000); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + + /* start with non-zero */ + litest_event(dev, EV_ABS, ABS_WHEEL, 10); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + + litest_drain_events(li); + + scale = abs->maximum - abs->minimum; + for (v = abs->minimum; v < abs->maximum; v += 8) { + litest_event(dev, EV_ABS, ABS_WHEEL, v); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + + litest_wait_for_event_of_type(li, + LIBINPUT_EVENT_TABLET_TOOL_AXIS, + -1); + event = libinput_get_event(li); + tev = libinput_event_get_tablet_tool_event(event); + ck_assert(libinput_event_tablet_tool_axis_has_changed(tev, + LIBINPUT_TABLET_TOOL_AXIS_SLIDER)); + val = libinput_event_tablet_tool_get_axis_value(tev, + LIBINPUT_TABLET_TOOL_AXIS_SLIDER); + + ck_assert_int_eq(val, (v - abs->minimum)/scale); + libinput_event_destroy(event); + litest_assert_empty_queue(li); + } +} +END_TEST + +START_TEST(artpen_tool) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event *event; + struct libinput_event_tablet_tool *tev; + struct libinput_tablet_tool *tool; + + if (!libevdev_has_event_code(dev->evdev, + EV_ABS, + ABS_Z)) + return; + + litest_drain_events(li); + + litest_event(dev, EV_KEY, BTN_TOOL_PEN, 1); + litest_event(dev, EV_ABS, ABS_MISC, 0x804); /* Art Pen */ + litest_event(dev, EV_MSC, MSC_SERIAL, 1000); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + + litest_wait_for_event_of_type(li, + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY, + -1); + event = libinput_get_event(li); + tev = libinput_event_get_tablet_tool_event(event); + tool = libinput_event_tablet_tool_get_tool(tev); + ck_assert_notnull(tool); + ck_assert_int_eq(libinput_tablet_tool_get_type(tool), + LIBINPUT_TABLET_TOOL_TYPE_PEN); + ck_assert(libinput_tablet_tool_has_axis(tool, + LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z)); + + libinput_event_destroy(event); +} +END_TEST + +START_TEST(artpen_rotation) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event *event; + struct libinput_event_tablet_tool *tev; + const struct input_absinfo *abs; + double val; + double scale; + int angle; + + if (!libevdev_has_event_code(dev->evdev, + EV_ABS, + ABS_Z)) + return; + + litest_drain_events(li); + + abs = libevdev_get_abs_info(dev->evdev, ABS_Z); + ck_assert_notnull(abs); + scale = (abs->maximum - abs->minimum + 1)/360.0; + + litest_event(dev, EV_KEY, BTN_TOOL_BRUSH, 1); + litest_event(dev, EV_ABS, ABS_MISC, 0x804); /* Art Pen */ + litest_event(dev, EV_MSC, MSC_SERIAL, 1000); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + + litest_event(dev, EV_ABS, ABS_Z, abs->minimum); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + + litest_drain_events(li); + + for (angle = 8; angle < 360; angle += 8) { + int a = angle * scale + abs->minimum; + + litest_event(dev, EV_ABS, ABS_Z, a); + litest_event(dev, EV_SYN, SYN_REPORT, 0); + + litest_wait_for_event_of_type(li, + LIBINPUT_EVENT_TABLET_TOOL_AXIS, + -1); + event = libinput_get_event(li); + tev = libinput_event_get_tablet_tool_event(event); + ck_assert(libinput_event_tablet_tool_axis_has_changed(tev, + LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z)); + val = libinput_event_tablet_tool_get_axis_value(tev, + LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z); + + /* artpen has a 90 deg offset cw */ + ck_assert_int_eq(round(val), (angle + 90) % 360); + + val = libinput_event_tablet_tool_get_axis_delta(tev, + LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z); + ck_assert_int_eq(val, 8); + + libinput_event_destroy(event); + litest_assert_empty_queue(li); + + } +} +END_TEST + +START_TEST(tablet_time_usec) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event *event; + struct libinput_event_tablet_tool *tev; + struct axis_replacement axes[] = { + { ABS_DISTANCE, 10 }, + { -1, -1 } + }; + + litest_drain_events(li); + + litest_tablet_proximity_in(dev, 5, 100, axes); + libinput_dispatch(li); + + litest_wait_for_event_of_type(li, + LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY, + -1); + event = libinput_get_event(li); + tev = libinput_event_get_tablet_tool_event(event); + ck_assert_int_eq(libinput_event_tablet_tool_get_time(tev), + libinput_event_tablet_tool_get_time_usec(tev) / 1000); + libinput_event_destroy(event); +} +END_TEST + +void +litest_setup_tests(void) +{ + litest_add("tablet:tool", tool_ref, LITEST_TABLET | LITEST_TOOL_SERIAL, LITEST_ANY); + litest_add_no_device("tablet:tool", tool_capabilities); + litest_add("tablet:tool", tool_in_prox_before_start, LITEST_TABLET, LITEST_ANY); + litest_add("tablet:tool_serial", tool_serial, LITEST_TABLET | LITEST_TOOL_SERIAL, LITEST_ANY); + litest_add("tablet:tool_serial", serial_changes_tool, LITEST_TABLET | LITEST_TOOL_SERIAL, LITEST_ANY); + litest_add("tablet:tool_serial", invalid_serials, LITEST_TABLET | LITEST_TOOL_SERIAL, LITEST_ANY); + litest_add_no_device("tablet:tool_serial", tools_with_serials); + litest_add_no_device("tablet:tool_serial", tools_without_serials); + litest_add("tablet:proximity", proximity_out_clear_buttons, LITEST_TABLET, LITEST_ANY); + litest_add("tablet:proximity", proximity_in_out, LITEST_TABLET, LITEST_ANY); + litest_add("tablet:proximity", proximity_has_axes, LITEST_TABLET, LITEST_ANY); + litest_add("tablet:proximity", bad_distance_events, LITEST_TABLET | LITEST_DISTANCE, LITEST_ANY); + litest_add("tablet:tip", tip_down_up, LITEST_TABLET, LITEST_ANY); + litest_add("tablet:tip", tip_down_prox_in, LITEST_TABLET, LITEST_ANY); + litest_add("tablet:tip", tip_up_prox_out, LITEST_TABLET, LITEST_ANY); + litest_add("tablet:tip", tip_down_btn_change, LITEST_TABLET, LITEST_ANY); + litest_add("tablet:tip", tip_up_btn_change, LITEST_TABLET, LITEST_ANY); + litest_add("tablet:tip", tip_down_motion, LITEST_TABLET, LITEST_ANY); + litest_add("tablet:tip", tip_up_motion, LITEST_TABLET, LITEST_ANY); + litest_add("tablet:tip", tip_state_proximity, LITEST_TABLET, LITEST_ANY); + litest_add("tablet:tip", tip_state_axis, LITEST_TABLET, LITEST_ANY); + litest_add("tablet:tip", tip_state_button, LITEST_TABLET, LITEST_ANY); + litest_add("tablet:motion", motion, LITEST_TABLET, LITEST_ANY); + litest_add("tablet:motion", motion_delta, LITEST_TABLET, LITEST_ANY); + litest_add("tablet:motion", motion_delta_partial, LITEST_TABLET, LITEST_ANY); + litest_add("tablet:motion", motion_event_state, LITEST_TABLET, LITEST_ANY); + litest_add_for_device("tablet:left_handed", left_handed, LITEST_WACOM_INTUOS); + litest_add_for_device("tablet:left_handed", no_left_handed, LITEST_WACOM_CINTIQ); + litest_add("tablet:normalization", normalization, LITEST_TABLET, LITEST_ANY); + litest_add("tablet:pad", pad_buttons_ignored, LITEST_TABLET, LITEST_ANY); + litest_add("tablet:mouse", mouse_tool, LITEST_TABLET, LITEST_ANY); + litest_add("tablet:mouse", mouse_buttons, LITEST_TABLET, LITEST_ANY); + litest_add("tablet:mouse", mouse_rotation, LITEST_TABLET, LITEST_ANY); + litest_add("tablet:mouse", mouse_wheel, LITEST_TABLET, LITEST_WHEEL); + litest_add("tablet:airbrush", airbrush_tool, LITEST_TABLET, LITEST_ANY); + litest_add("tablet:airbrush", airbrush_wheel, LITEST_TABLET, LITEST_ANY); + litest_add("tablet:artpen", artpen_tool, LITEST_TABLET, LITEST_ANY); + litest_add("tablet:artpen", artpen_rotation, LITEST_TABLET, LITEST_ANY); + + litest_add("tablet:time", tablet_time_usec, LITEST_TABLET, LITEST_ANY); +} diff --git a/test/valgrind.suppressions b/test/valgrind.suppressions index 50b5c58a..b7f43499 100644 --- a/test/valgrind.suppressions +++ b/test/valgrind.suppressions @@ -14,6 +14,24 @@ fun:mtdev_put_event } { + <g_type_register_static> + Memcheck:Leak + ... + fun:g_type_register_static +} +{ + <g_type_register_static> + Memcheck:Leak + ... + fun:g_type_register_fundamental +} +{ + <g_type_register_static> + Memcheck:Leak + ... + fun:g_malloc0 +} +{ libunwind:msync_uninitialized_bytes Memcheck:Param msync(start) diff --git a/tools/event-debug.c b/tools/event-debug.c index c6032962..bb32f4b2 100644 --- a/tools/event-debug.c +++ b/tools/event-debug.c @@ -23,6 +23,7 @@ #define _GNU_SOURCE #include <errno.h> +#include <inttypes.h> #include <fcntl.h> #include <poll.h> #include <stdio.h> @@ -108,9 +109,21 @@ print_event_header(struct libinput_event *ev) case LIBINPUT_EVENT_GESTURE_PINCH_END: type = "GESTURE_PINCH_END"; break; + case LIBINPUT_EVENT_TABLET_TOOL_AXIS: + type = "TABLET_AXIS"; + break; + case LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY: + type = "TABLET_PROXIMITY"; + break; + case LIBINPUT_EVENT_TABLET_TOOL_TIP: + type = "TABLET_TIP"; + break; + case LIBINPUT_EVENT_TABLET_TOOL_BUTTON: + type = "TABLET_BUTTON"; + break; } - printf("%-7s %s ", libinput_device_get_sysname(dev), type); + printf("%-7s %-16s ", libinput_device_get_sysname(dev), type); } static void @@ -156,6 +169,9 @@ print_device_notify(struct libinput_event *ev) if (libinput_device_has_capability(dev, LIBINPUT_DEVICE_CAP_GESTURE)) printf("g"); + if (libinput_device_has_capability(dev, + LIBINPUT_DEVICE_CAP_TABLET_TOOL)) + printf("T"); if (libinput_device_get_size(dev, &w, &h) == 0) printf("\tsize %.2f/%.2fmm", w, h); @@ -251,7 +267,7 @@ print_absmotion_event(struct libinput_event *ev) } static void -print_button_event(struct libinput_event *ev) +print_pointer_button_event(struct libinput_event *ev) { struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev); enum libinput_button_state state; @@ -266,7 +282,34 @@ print_button_event(struct libinput_event *ev) } static void -print_axis_event(struct libinput_event *ev) +print_tablet_tip_event(struct libinput_event *ev) +{ + struct libinput_event_tablet_tool *p = libinput_event_get_tablet_tool_event(ev); + enum libinput_tablet_tool_tip_state state; + + print_event_time(libinput_event_tablet_tool_get_time(p)); + + state = libinput_event_tablet_tool_get_tip_state(p); + printf("%s\n", state == LIBINPUT_TABLET_TOOL_TIP_DOWN ? "down" : "up"); +} + +static void +print_tablet_button_event(struct libinput_event *ev) +{ + struct libinput_event_tablet_tool *p = libinput_event_get_tablet_tool_event(ev); + enum libinput_button_state state; + + print_event_time(libinput_event_tablet_tool_get_time(p)); + + state = libinput_event_tablet_tool_get_button_state(p); + printf("%3d %s, seat count: %u\n", + libinput_event_tablet_tool_get_button(p), + state == LIBINPUT_BUTTON_STATE_PRESSED ? "pressed" : "released", + libinput_event_tablet_tool_get_seat_button_count(p)); +} + +static void +print_pointer_axis_event(struct libinput_event *ev) { struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev); double v = 0, h = 0; @@ -283,6 +326,124 @@ print_axis_event(struct libinput_event *ev) printf("vert %.2f horiz %.2f\n", v, h); } +static const char* +tablet_axis_changed_sym(struct libinput_event_tablet_tool *t, + enum libinput_tablet_tool_axis axis) +{ + if (libinput_event_tablet_tool_axis_has_changed(t, axis)) + return "*"; + else + return ""; +} + +static void +print_tablet_axes(struct libinput_event_tablet_tool *t) +{ + struct libinput_tablet_tool *tool = libinput_event_tablet_tool_get_tool(t); + double x, y, dx, dy; + double dist, pressure; + double rotation, slider, wheel; + double delta; + + x = libinput_event_tablet_tool_get_axis_value(t, LIBINPUT_TABLET_TOOL_AXIS_X); + y = libinput_event_tablet_tool_get_axis_value(t, LIBINPUT_TABLET_TOOL_AXIS_Y); + dx = libinput_event_tablet_tool_get_axis_delta(t, LIBINPUT_TABLET_TOOL_AXIS_X); + dy = libinput_event_tablet_tool_get_axis_delta(t, LIBINPUT_TABLET_TOOL_AXIS_Y); + printf("\t%.2f%s/%.2f%s (%.2f/%.2f)", + x, tablet_axis_changed_sym(t, LIBINPUT_TABLET_TOOL_AXIS_X), + y, tablet_axis_changed_sym(t, LIBINPUT_TABLET_TOOL_AXIS_Y), + dx, dy); + + if (libinput_tablet_tool_has_axis(tool, LIBINPUT_TABLET_TOOL_AXIS_TILT_X) || + libinput_tablet_tool_has_axis(tool, LIBINPUT_TABLET_TOOL_AXIS_TILT_Y)) { + x = libinput_event_tablet_tool_get_axis_value(t, + LIBINPUT_TABLET_TOOL_AXIS_TILT_X); + y = libinput_event_tablet_tool_get_axis_value(t, + LIBINPUT_TABLET_TOOL_AXIS_TILT_Y); + dx = libinput_event_tablet_tool_get_axis_delta(t, + LIBINPUT_TABLET_TOOL_AXIS_TILT_X); + dy = libinput_event_tablet_tool_get_axis_delta(t, + LIBINPUT_TABLET_TOOL_AXIS_TILT_Y); + printf("\ttilt: %.2f%s/%.2f%s (%.2f/%.2f)", + x, tablet_axis_changed_sym(t, + LIBINPUT_TABLET_TOOL_AXIS_TILT_X), + y, tablet_axis_changed_sym(t, + LIBINPUT_TABLET_TOOL_AXIS_TILT_Y), + dx, dy); + } + + if (libinput_tablet_tool_has_axis(tool, LIBINPUT_TABLET_TOOL_AXIS_DISTANCE) || + libinput_tablet_tool_has_axis(tool, LIBINPUT_TABLET_TOOL_AXIS_PRESSURE)) { + dist = libinput_event_tablet_tool_get_axis_value(t, + LIBINPUT_TABLET_TOOL_AXIS_DISTANCE); + pressure = libinput_event_tablet_tool_get_axis_value(t, + LIBINPUT_TABLET_TOOL_AXIS_PRESSURE); + if (dist) { + delta = libinput_event_tablet_tool_get_axis_delta(t, + LIBINPUT_TABLET_TOOL_AXIS_DISTANCE); + printf("\tdistance: %.2f%s (%.2f)", + dist, + tablet_axis_changed_sym(t, + LIBINPUT_TABLET_TOOL_AXIS_DISTANCE), + delta); + } else { + delta = libinput_event_tablet_tool_get_axis_delta(t, + LIBINPUT_TABLET_TOOL_AXIS_PRESSURE); + printf("\tpressure: %.2f%s (%.2f)", + pressure, + tablet_axis_changed_sym(t, + LIBINPUT_TABLET_TOOL_AXIS_PRESSURE), + delta); + } + } + + if (libinput_tablet_tool_has_axis(tool, LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z)) { + rotation = libinput_event_tablet_tool_get_axis_value(t, + LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z); + delta = libinput_event_tablet_tool_get_axis_delta(t, + LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z); + printf("\trotation: %.2f%s (%.2f)", + rotation, + tablet_axis_changed_sym(t, + LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z), + delta); + } + + if (libinput_tablet_tool_has_axis(tool, LIBINPUT_TABLET_TOOL_AXIS_SLIDER)) { + slider = libinput_event_tablet_tool_get_axis_value(t, + LIBINPUT_TABLET_TOOL_AXIS_SLIDER); + delta = libinput_event_tablet_tool_get_axis_delta(t, + LIBINPUT_TABLET_TOOL_AXIS_SLIDER); + printf("\tslider: %.2f%s (%.2f)", + slider, + tablet_axis_changed_sym(t, + LIBINPUT_TABLET_TOOL_AXIS_SLIDER), + delta); + } + + if (libinput_tablet_tool_has_axis(tool, LIBINPUT_TABLET_TOOL_AXIS_REL_WHEEL)) { + wheel = libinput_event_tablet_tool_get_axis_value(t, + LIBINPUT_TABLET_TOOL_AXIS_REL_WHEEL); + delta = libinput_event_tablet_tool_get_axis_delta_discrete(t, + LIBINPUT_TABLET_TOOL_AXIS_REL_WHEEL); + printf("\twheel: %.2f%s (%d)", + wheel, + tablet_axis_changed_sym(t, + LIBINPUT_TABLET_TOOL_AXIS_REL_WHEEL), + (int)delta); + } +} + +static void +print_tablet_axis_event(struct libinput_event *ev) +{ + struct libinput_event_tablet_tool *t = libinput_event_get_tablet_tool_event(ev); + + print_event_time(libinput_event_tablet_tool_get_time(t)); + print_tablet_axes(t); + printf("\n"); +} + static void print_touch_event_without_coords(struct libinput_event *ev) { @@ -293,6 +454,100 @@ print_touch_event_without_coords(struct libinput_event *ev) } static void +print_proximity_event(struct libinput_event *ev) +{ + struct libinput_event_tablet_tool *t = libinput_event_get_tablet_tool_event(ev); + struct libinput_tablet_tool *tool = libinput_event_tablet_tool_get_tool(t); + enum libinput_tablet_tool_proximity_state state; + const char *tool_str, + *state_str; + + switch (libinput_tablet_tool_get_type(tool)) { + case LIBINPUT_TABLET_TOOL_TYPE_PEN: + tool_str = "pen"; + break; + case LIBINPUT_TABLET_TOOL_TYPE_ERASER: + tool_str = "eraser"; + break; + case LIBINPUT_TABLET_TOOL_TYPE_BRUSH: + tool_str = "brush"; + break; + case LIBINPUT_TABLET_TOOL_TYPE_PENCIL: + tool_str = "pencil"; + break; + case LIBINPUT_TABLET_TOOL_TYPE_AIRBRUSH: + tool_str = "airbrush"; + break; + case LIBINPUT_TABLET_TOOL_TYPE_FINGER: + tool_str = "finger"; + break; + case LIBINPUT_TABLET_TOOL_TYPE_MOUSE: + tool_str = "mouse"; + break; + case LIBINPUT_TABLET_TOOL_TYPE_LENS: + tool_str = "lens"; + break; + default: + abort(); + } + + state = libinput_event_tablet_tool_get_proximity_state(t); + + print_event_time(libinput_event_tablet_tool_get_time(t)); + + if (state == LIBINPUT_TABLET_TOOL_PROXIMITY_IN) { + print_tablet_axes(t); + state_str = "proximity-in"; + } else if (state == LIBINPUT_TABLET_TOOL_PROXIMITY_OUT) { + state_str = "proximity-out"; + printf("\t"); + } else { + abort(); + } + + printf("\t%s (%#" PRIx64 ", id %#" PRIx64 ") %s", + tool_str, + libinput_tablet_tool_get_serial(tool), + libinput_tablet_tool_get_tool_id(tool), + state_str); + + printf("\taxes:"); + if (libinput_tablet_tool_has_axis(tool, LIBINPUT_TABLET_TOOL_AXIS_DISTANCE)) + printf("d"); + if (libinput_tablet_tool_has_axis(tool, LIBINPUT_TABLET_TOOL_AXIS_PRESSURE)) + printf("p"); + if (libinput_tablet_tool_has_axis(tool, LIBINPUT_TABLET_TOOL_AXIS_TILT_X) || + libinput_tablet_tool_has_axis(tool, LIBINPUT_TABLET_TOOL_AXIS_TILT_Y)) + printf("t"); + if (libinput_tablet_tool_has_axis(tool, LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z)) + printf("r"); + if (libinput_tablet_tool_has_axis(tool, LIBINPUT_TABLET_TOOL_AXIS_SLIDER)) + printf("s"); + if (libinput_tablet_tool_has_axis(tool, LIBINPUT_TABLET_TOOL_AXIS_REL_WHEEL)) + printf("w"); + + printf("\tbtn:"); + if (libinput_tablet_tool_has_button(tool, BTN_TOUCH)) + printf("T"); + if (libinput_tablet_tool_has_button(tool, BTN_STYLUS)) + printf("S"); + if (libinput_tablet_tool_has_button(tool, BTN_STYLUS2)) + printf("S2"); + if (libinput_tablet_tool_has_button(tool, BTN_LEFT)) + printf("L"); + if (libinput_tablet_tool_has_button(tool, BTN_MIDDLE)) + printf("M"); + if (libinput_tablet_tool_has_button(tool, BTN_RIGHT)) + printf("R"); + if (libinput_tablet_tool_has_button(tool, BTN_SIDE)) + printf("Sd"); + if (libinput_tablet_tool_has_button(tool, BTN_EXTRA)) + printf("Ex"); + + printf("\n"); +} + +static void print_touch_event_with_coords(struct libinput_event *ev) { struct libinput_event_touch *t = libinput_event_get_touch_event(ev); @@ -383,10 +638,10 @@ handle_and_print_events(struct libinput *li) print_absmotion_event(ev); break; case LIBINPUT_EVENT_POINTER_BUTTON: - print_button_event(ev); + print_pointer_button_event(ev); break; case LIBINPUT_EVENT_POINTER_AXIS: - print_axis_event(ev); + print_pointer_axis_event(ev); break; case LIBINPUT_EVENT_TOUCH_DOWN: print_touch_event_with_coords(ev); @@ -421,6 +676,18 @@ handle_and_print_events(struct libinput *li) case LIBINPUT_EVENT_GESTURE_PINCH_END: print_gesture_event_without_coords(ev); break; + case LIBINPUT_EVENT_TABLET_TOOL_AXIS: + print_tablet_axis_event(ev); + break; + case LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY: + print_proximity_event(ev); + break; + case LIBINPUT_EVENT_TABLET_TOOL_TIP: + print_tablet_tip_event(ev); + break; + case LIBINPUT_EVENT_TABLET_TOOL_BUTTON: + print_tablet_button_event(ev); + break; } libinput_event_destroy(ev); diff --git a/tools/event-gui.c b/tools/event-gui.c index 0b0e9d71..996842e0 100644 --- a/tools/event-gui.c +++ b/tools/event-gui.c @@ -85,6 +85,16 @@ struct window { double x, y; } pinch; + struct { + double x, y; + double x_in, y_in; + double x_down, y_down; + double x_up, y_up; + double pressure; + double distance; + double tilt_x, tilt_y; + } tool; + struct libinput_device *devices[50]; }; @@ -216,6 +226,41 @@ draw(GtkWidget *widget, cairo_t *cr, gpointer data) cairo_stroke(cr); cairo_restore(cr); + /* tablet tool, square for prox-in location */ + cairo_save(cr); + cairo_set_source_rgb(cr, .8, .8, .8); + if (w->tool.x_in && w->tool.y_in) { + cairo_rectangle(cr, w->tool.x_in - 15, w->tool.y_in - 15, 30, 30); + cairo_stroke(cr); + cairo_restore(cr); + cairo_save(cr); + } + + if (w->tool.x_down && w->tool.y_down) { + cairo_rectangle(cr, w->tool.x_down - 10, w->tool.y_down - 10, 20, 20); + cairo_stroke(cr); + cairo_restore(cr); + cairo_save(cr); + } + + if (w->tool.x_up && w->tool.y_up) { + cairo_rectangle(cr, w->tool.x_up - 10, w->tool.y_up - 10, 20, 20); + cairo_stroke(cr); + cairo_restore(cr); + cairo_save(cr); + } + + if (w->tool.pressure) + cairo_set_source_rgb(cr, .8, .8, .2); + + cairo_translate(cr, w->tool.x, w->tool.y); + cairo_scale(cr, 1.0 + w->tool.tilt_x, 1.0 + w->tool.tilt_y); + cairo_arc(cr, 0, 0, + 1 + 10 * max(w->tool.pressure, w->tool.distance), + 0, 2 * M_PI); + cairo_fill(cr); + cairo_restore(cr); + return TRUE; } @@ -551,6 +596,62 @@ handle_event_pinch(struct libinput_event *ev, struct window *w) } } +static void +handle_event_tablet(struct libinput_event *ev, struct window *w) +{ + struct libinput_event_tablet_tool *t = libinput_event_get_tablet_tool_event(ev); + double x, y; + + switch (libinput_event_get_type(ev)) { + case LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY: + if (libinput_event_tablet_tool_get_proximity_state(t) == + LIBINPUT_TABLET_TOOL_PROXIMITY_OUT) { + w->tool.x_in = 0; + w->tool.y_in = 0; + w->tool.x_down = 0; + w->tool.y_down = 0; + w->tool.x_up = 0; + w->tool.y_up = 0; + } else { + w->tool.x_in = libinput_event_tablet_tool_get_x_transformed(t, + w->width); + w->tool.y_in = libinput_event_tablet_tool_get_y_transformed(t, + w->height); + } + break; + case LIBINPUT_EVENT_TABLET_TOOL_AXIS: + w->tool.x = libinput_event_tablet_tool_get_x_transformed(t, + w->width); + w->tool.y = libinput_event_tablet_tool_get_y_transformed(t, + w->height); + w->tool.pressure = libinput_event_tablet_tool_get_axis_value(t, + LIBINPUT_TABLET_TOOL_AXIS_PRESSURE); + w->tool.distance = libinput_event_tablet_tool_get_axis_value(t, + LIBINPUT_TABLET_TOOL_AXIS_DISTANCE); + w->tool.tilt_x = libinput_event_tablet_tool_get_axis_value(t, + LIBINPUT_TABLET_TOOL_AXIS_TILT_X); + w->tool.tilt_y = libinput_event_tablet_tool_get_axis_value(t, + LIBINPUT_TABLET_TOOL_AXIS_TILT_Y); + break; + case LIBINPUT_EVENT_TABLET_TOOL_TIP: + x = libinput_event_tablet_tool_get_x_transformed(t, w->width); + y = libinput_event_tablet_tool_get_y_transformed(t, w->height); + if (libinput_event_tablet_tool_get_tip_state(t) == + LIBINPUT_TABLET_TOOL_TIP_DOWN) { + w->tool.x_down = x; + w->tool.y_down = y; + } else { + w->tool.x_up = x; + w->tool.y_up = y; + } + break; + case LIBINPUT_EVENT_TABLET_TOOL_BUTTON: + break; + default: + abort(); + } +} + static gboolean handle_event_libinput(GIOChannel *source, GIOCondition condition, gpointer data) { @@ -606,6 +707,12 @@ handle_event_libinput(GIOChannel *source, GIOCondition condition, gpointer data) case LIBINPUT_EVENT_GESTURE_PINCH_END: handle_event_pinch(ev, w); break; + case LIBINPUT_EVENT_TABLET_TOOL_AXIS: + case LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY: + case LIBINPUT_EVENT_TABLET_TOOL_TIP: + case LIBINPUT_EVENT_TABLET_TOOL_BUTTON: + handle_event_tablet(ev, w); + break; } libinput_event_destroy(ev); diff --git a/tools/libinput-list-devices.c b/tools/libinput-list-devices.c index 21685d9b..62eb915b 100644 --- a/tools/libinput-list-devices.c +++ b/tools/libinput-list-devices.c @@ -257,6 +257,9 @@ print_device_notify(struct libinput_event *ev) if (libinput_device_has_capability(dev, LIBINPUT_DEVICE_CAP_TOUCH)) printf("touch"); + if (libinput_device_has_capability(dev, + LIBINPUT_DEVICE_CAP_TABLET_TOOL)) + printf("tablet"); printf("\n"); printf("Tap-to-click: %s\n", tap_default(dev)); |