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 <assert.h>
27
28#include "libinput-util.h"
29
30#include "litest.h"
31#include "litest-int.h"
32
33struct alps {
34	unsigned int first, second;
35	unsigned int active_touches;
36};
37
38static bool
39alps_create(struct litest_device *d)
40{
41	d->private = zalloc(sizeof(struct alps));
42	return true; /* we want litest to create our device */
43}
44
45static bool
46touch_down(struct litest_device *d, unsigned int slot, double x, double y)
47{
48	struct alps *alps = d->private;
49
50	alps->active_touches++;
51
52	if (alps->active_touches == 1)
53		alps->first = slot;
54	if (alps->active_touches == 2)
55		alps->second = slot;
56
57	/* This device announces 4 slots but only does two slots. So
58	 * anything over 2 slots we just drop for events,
59	 * litest takes care of BTN_TOOL_* for us. */
60	if (alps->active_touches > 2) {
61		/* Need to send SYN_REPORT to flush litest's BTN_TOOL_* updates */
62		litest_event(d, EV_SYN, SYN_REPORT,  0);
63		return true;
64	}
65
66	return false;
67}
68
69static bool
70touch_move(struct litest_device *d, unsigned int slot, double x, double y)
71{
72	struct alps *alps = d->private;
73
74	if (alps->active_touches > 2 &&
75	    slot != alps->first &&
76	    slot != alps->second)
77		return true;
78
79	return false;
80}
81
82static bool
83touch_up(struct litest_device *d, unsigned int slot)
84{
85	struct alps *alps = d->private;
86
87	assert(alps->active_touches >= 1);
88	alps->active_touches--;
89
90	/* Need to send SYN_REPORT to flush litest's BTN_TOOL_* updates */
91	if (alps->active_touches > 2 &&
92	    slot != alps->first &&
93	    slot != alps->second) {
94		litest_event(d, EV_SYN, SYN_REPORT,  0);
95		return true;
96	}
97
98	if (slot == alps->first)
99		alps->first = UINT_MAX;
100	if (slot == alps->second)
101		alps->second = UINT_MAX;
102
103	return false;
104}
105
106static struct input_event down[] = {
107	{ .type = EV_ABS, .code = ABS_X, .value = LITEST_AUTO_ASSIGN  },
108	{ .type = EV_ABS, .code = ABS_Y, .value = LITEST_AUTO_ASSIGN },
109	{ .type = EV_ABS, .code = ABS_MT_SLOT, .value = LITEST_AUTO_ASSIGN },
110	{ .type = EV_ABS, .code = ABS_MT_TRACKING_ID, .value = LITEST_AUTO_ASSIGN },
111	{ .type = EV_ABS, .code = ABS_MT_POSITION_X, .value = LITEST_AUTO_ASSIGN },
112	{ .type = EV_ABS, .code = ABS_MT_POSITION_Y, .value = LITEST_AUTO_ASSIGN },
113	{ .type = EV_SYN, .code = SYN_REPORT, .value = 0 },
114	{ .type = -1, .code = -1 },
115};
116
117static struct input_event move[] = {
118	{ .type = EV_ABS, .code = ABS_MT_SLOT, .value = LITEST_AUTO_ASSIGN },
119	{ .type = EV_ABS, .code = ABS_X, .value = LITEST_AUTO_ASSIGN  },
120	{ .type = EV_ABS, .code = ABS_Y, .value = LITEST_AUTO_ASSIGN },
121	{ .type = EV_ABS, .code = ABS_MT_POSITION_X, .value = LITEST_AUTO_ASSIGN },
122	{ .type = EV_ABS, .code = ABS_MT_POSITION_Y, .value = LITEST_AUTO_ASSIGN },
123	{ .type = EV_SYN, .code = SYN_REPORT, .value = 0 },
124	{ .type = -1, .code = -1 },
125};
126
127static struct litest_device_interface interface = {
128	.touch_down_events = down,
129	.touch_move_events = move,
130
131	.touch_down = touch_down,
132	.touch_move = touch_move,
133	.touch_up = touch_up,
134};
135
136static struct input_id input_id = {
137	.bustype = 0x11,
138	.vendor = 0x2,
139	.product = 0x8,
140	.version = 0x700,
141};
142
143static int events[] = {
144	EV_KEY, BTN_LEFT,
145	EV_KEY, BTN_RIGHT,
146	EV_KEY, BTN_MIDDLE,
147	EV_KEY, BTN_TOOL_FINGER,
148	EV_KEY, BTN_TOUCH,
149	EV_KEY, BTN_TOOL_DOUBLETAP,
150	EV_KEY, BTN_TOOL_TRIPLETAP,
151	EV_KEY, BTN_TOOL_QUADTAP,
152	EV_KEY, BTN_TOOL_QUINTTAP,
153	INPUT_PROP_MAX, INPUT_PROP_POINTER,
154	-1, -1,
155};
156
157/* Note: we use the user-supplied resolution here, see #408 */
158static struct input_absinfo absinfo[] = {
159	{ ABS_X, 0, 4095, 0, 0, 37 },
160	{ ABS_Y, 0, 2047, 0, 0, 26 },
161	{ ABS_PRESSURE, 0, 127, 0, 0, 0 },
162	{ ABS_MT_SLOT, 0, 3, 0, 0, 0 },
163	{ ABS_MT_POSITION_X, 0, 4095, 0, 0, 37 },
164	{ ABS_MT_POSITION_Y, 0, 2047, 0, 0, 26 },
165	{ ABS_MT_TRACKING_ID, 0, 65535, 0, 0, 0 },
166	{ .value = -1 }
167};
168
169TEST_DEVICE("alps-3fg",
170	.type = LITEST_ALPS_3FG,
171	.features = LITEST_TOUCHPAD | LITEST_BUTTON,
172	.interface = &interface,
173
174	.name = "AlpsPS/2 ALPS GlidePoint",
175	.id = &input_id,
176	.events = events,
177	.absinfo = absinfo,
178	.create = alps_create,
179)
180