1/*
2 * Copyright © 2020 Red Hat, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24#include "config.h"
25
26#include "litest.h"
27#include "litest-int.h"
28
29static struct input_event proximity_in_events[] = {
30	{ .type = EV_ABS, .code = ABS_X, .value = LITEST_AUTO_ASSIGN },
31	{ .type = EV_ABS, .code = ABS_Y, .value = LITEST_AUTO_ASSIGN },
32	{ .type = EV_ABS, .code = ABS_PRESSURE, .value = LITEST_AUTO_ASSIGN },
33	{ .type = EV_KEY, .code = LITEST_BTN_TOOL_AUTO, .value = 1 },
34	{ .type = EV_SYN, .code = SYN_REPORT, .value = 0 },
35	{ .type = -1, .code = -1 },
36};
37
38static struct input_event proximity_out_events[] = {
39	{ .type = EV_KEY, .code = LITEST_BTN_TOOL_AUTO, .value = 0 },
40	{ .type = EV_SYN, .code = SYN_REPORT, .value = 0 },
41	{ .type = -1, .code = -1 },
42};
43
44static struct input_event motion_events[] = {
45	{ .type = EV_ABS, .code = ABS_X, .value = LITEST_AUTO_ASSIGN },
46	{ .type = EV_ABS, .code = ABS_Y, .value = LITEST_AUTO_ASSIGN },
47	{ .type = EV_ABS, .code = ABS_PRESSURE, .value = LITEST_AUTO_ASSIGN },
48	{ .type = EV_SYN, .code = SYN_REPORT, .value = 0 },
49	{ .type = -1, .code = -1 },
50};
51
52static bool
53proximity_in(struct litest_device *d,
54	     unsigned int tool_type,
55	     double *x, double *y,
56	     struct axis_replacement *axes)
57{
58	/* nothing special needed for the pen tool, so let litest handle
59	 * this */
60	if (tool_type == BTN_TOOL_PEN)
61		return false;
62
63	/* a non-pen tool requires the pen to be in proximity as well.  */
64	int sx = litest_scale(d, ABS_X, *x);
65	int sy = litest_scale(d, ABS_Y, *y);
66	litest_event(d, EV_ABS, ABS_X, sx);
67	litest_event(d, EV_ABS, ABS_X, sy);
68	litest_event(d, EV_KEY, BTN_TOOL_PEN, 1);
69	litest_event(d, EV_SYN, SYN_REPORT, 0);
70
71	/* litest will append the proximity_in_events if we return false,
72	 * including the right tool event */
73	return false;
74}
75
76static bool
77proximity_out(struct litest_device *d, unsigned int tool_type)
78{
79	/* a non-pen tool requires the pen to go out of proximity as well.
80	 * litest will append the proximity_out_events if we return false
81	 */
82	if (tool_type != BTN_TOOL_PEN)
83		litest_event(d, EV_KEY, BTN_TOOL_PEN, 0);
84
85	return false;
86}
87
88static int
89get_axis_default(struct litest_device *d, unsigned int evcode, int32_t *value)
90{
91	switch (evcode) {
92	case ABS_PRESSURE:
93		*value = 100;
94		return 0;
95	}
96	return 1;
97}
98
99static struct litest_device_interface interface = {
100	.tablet_proximity_in_events = proximity_in_events,
101	.tablet_proximity_out_events = proximity_out_events,
102	.tablet_motion_events = motion_events,
103	.tablet_proximity_in = proximity_in,
104	.tablet_proximity_out = proximity_out,
105
106	.get_axis_default = get_axis_default,
107};
108
109static struct input_absinfo absinfo[] = {
110	{ ABS_X, 0, 18176, 0, 0, 62 },
111	{ ABS_Y, 0, 10240, 0, 0, 62 },
112	{ ABS_PRESSURE, 0, 4096, 0, 0, 0 },
113	{ .value = -1 },
114};
115
116static struct input_id input_id = {
117	.bustype = 0x18,
118	.vendor = 0x4f3,
119	.product = 0x23b9,
120	.version = 0x100,
121};
122
123/* Note: this tablet is one that sets both BTN_TOOL_PEN and BTN_TOOL_RUBBER,
124 * see https://gitlab.freedesktop.org/libinput/libinput/-/issues/259
125 * The one in the issue isn't the exact same model, but only the pid and x/y
126 * axis max differs differs.
127 */
128static int events[] = {
129	EV_KEY, BTN_TOOL_PEN,
130	EV_KEY, BTN_TOOL_RUBBER,
131	EV_KEY, BTN_TOUCH,
132	EV_KEY, BTN_STYLUS,
133	EV_MSC, MSC_SCAN,
134	-1, -1,
135};
136
137TEST_DEVICE("elan-tablet",
138	.type = LITEST_ELAN_TABLET,
139	.features = LITEST_TABLET,
140	.interface = &interface,
141
142	.name = "ELAN2514:00 04F3:23B9",
143	.id = &input_id,
144	.events = events,
145	.absinfo = absinfo,
146)
147