touchpad: Add clickpad-style software buttons
[platform/upstream/libinput.git] / test / touchpad.c
1 /*
2  * Copyright © 2014 Red Hat, Inc.
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation for any purpose is hereby granted without fee, provided
6  * that the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation, and that the name of the copyright holders not be used in
9  * advertising or publicity pertaining to distribution of the software
10  * without specific, written prior permission.  The copyright holders make
11  * no representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 #include <config.h>
24
25 #include <check.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <libinput.h>
29 #include <unistd.h>
30
31 #include "libinput-util.h"
32 #include "litest.h"
33
34 START_TEST(touchpad_1fg_motion)
35 {
36         struct litest_device *dev = litest_current_device();
37         struct libinput *li = dev->libinput;
38         struct libinput_event *event;
39         struct libinput_event_pointer *ptrev;
40
41         litest_drain_events(li);
42
43         litest_touch_down(dev, 0, 50, 50);
44         litest_touch_move_to(dev, 0, 50, 50, 80, 50, 5);
45         litest_touch_up(dev, 0);
46
47         libinput_dispatch(li);
48
49         event = libinput_get_event(li);
50         ck_assert(event != NULL);
51
52         while (event) {
53                 ck_assert_int_eq(libinput_event_get_type(event),
54                                  LIBINPUT_EVENT_POINTER_MOTION);
55
56                 ptrev = libinput_event_get_pointer_event(event);
57                 ck_assert_int_ge(libinput_event_pointer_get_dx(ptrev), 0);
58                 ck_assert_int_eq(libinput_event_pointer_get_dy(ptrev), 0);
59                 libinput_event_destroy(event);
60                 event = libinput_get_event(li);
61         }
62 }
63 END_TEST
64
65 START_TEST(touchpad_2fg_no_motion)
66 {
67         struct litest_device *dev = litest_current_device();
68         struct libinput *li = dev->libinput;
69         struct libinput_event *event;
70
71         litest_drain_events(li);
72
73         litest_touch_down(dev, 0, 50, 50);
74         litest_touch_down(dev, 1, 70, 70);
75         litest_touch_move_to(dev, 0, 50, 50, 80, 50, 5);
76         litest_touch_move_to(dev, 1, 70, 70, 80, 50, 5);
77         litest_touch_up(dev, 0);
78         litest_touch_up(dev, 1);
79
80         libinput_dispatch(li);
81
82         event = libinput_get_event(li);
83         while (event) {
84                 ck_assert_int_ne(libinput_event_get_type(event),
85                                  LIBINPUT_EVENT_POINTER_MOTION);
86                 libinput_event_destroy(event);
87                 event = libinput_get_event(li);
88         }
89 }
90 END_TEST
91
92 static void
93 assert_button_event(struct libinput *li, int button,
94                     enum libinput_pointer_button_state state)
95 {
96         struct libinput_event *event;
97         struct libinput_event_pointer *ptrev;
98
99         libinput_dispatch(li);
100         event = libinput_get_event(li);
101
102         ck_assert(event != NULL);
103         ck_assert_int_eq(libinput_event_get_type(event),
104                          LIBINPUT_EVENT_POINTER_BUTTON);
105         ptrev = libinput_event_get_pointer_event(event);
106         ck_assert_int_eq(libinput_event_pointer_get_button(ptrev),
107                          button);
108         ck_assert_int_eq(libinput_event_pointer_get_button_state(ptrev),
109                          state);
110         libinput_event_destroy(event);
111 }
112
113 START_TEST(touchpad_1fg_tap)
114 {
115         struct litest_device *dev = litest_current_device();
116         struct libinput *li = dev->libinput;
117         struct libinput_event *event;
118
119         litest_drain_events(li);
120
121         litest_touch_down(dev, 0, 50, 50);
122         litest_touch_up(dev, 0);
123
124         libinput_dispatch(li);
125
126         assert_button_event(li, BTN_LEFT,
127                             LIBINPUT_POINTER_BUTTON_STATE_PRESSED);
128         usleep(300000); /* tap-n-drag timeout */
129         assert_button_event(li, BTN_LEFT,
130                             LIBINPUT_POINTER_BUTTON_STATE_RELEASED);
131
132         libinput_dispatch(li);
133         event = libinput_get_event(li);
134         ck_assert(event == NULL);
135 }
136 END_TEST
137
138 START_TEST(touchpad_1fg_tap_n_drag)
139 {
140         struct litest_device *dev = litest_current_device();
141         struct libinput *li = dev->libinput;
142         struct libinput_event *event;
143
144         litest_drain_events(li);
145
146         litest_touch_down(dev, 0, 50, 50);
147         litest_touch_up(dev, 0);
148         litest_touch_down(dev, 0, 50, 50);
149         litest_touch_move_to(dev, 0, 50, 50, 80, 80, 5);
150         litest_touch_up(dev, 0);
151
152         libinput_dispatch(li);
153
154         assert_button_event(li, BTN_LEFT,
155                             LIBINPUT_POINTER_BUTTON_STATE_PRESSED);
156
157         libinput_dispatch(li);
158         while (libinput_next_event_type(li) == LIBINPUT_EVENT_POINTER_MOTION) {
159                 event = libinput_get_event(li);
160                 libinput_event_destroy(event);
161                 libinput_dispatch(li);
162         }
163
164         ck_assert_int_eq(libinput_next_event_type(li), LIBINPUT_EVENT_NONE);
165
166         /* lift finger, set down again, should continue dragging */
167         litest_touch_down(dev, 0, 50, 50);
168         litest_touch_move_to(dev, 0, 50, 50, 80, 80, 5);
169         litest_touch_up(dev, 0);
170
171         libinput_dispatch(li);
172         while (libinput_next_event_type(li) == LIBINPUT_EVENT_POINTER_MOTION) {
173                 event = libinput_get_event(li);
174                 libinput_event_destroy(event);
175                 libinput_dispatch(li);
176         }
177
178         ck_assert_int_eq(libinput_next_event_type(li), LIBINPUT_EVENT_NONE);
179
180         usleep(300000); /* tap-n-drag timeout */
181
182         assert_button_event(li, BTN_LEFT,
183                             LIBINPUT_POINTER_BUTTON_STATE_RELEASED);
184
185         libinput_dispatch(li);
186         event = libinput_get_event(li);
187         ck_assert(event == NULL);
188 }
189 END_TEST
190
191 START_TEST(touchpad_2fg_tap)
192 {
193         struct litest_device *dev = litest_current_device();
194         struct libinput *li = dev->libinput;
195         struct libinput_event *event;
196
197         litest_drain_events(dev->libinput);
198
199         litest_touch_down(dev, 0, 50, 50);
200         litest_touch_down(dev, 1, 70, 70);
201         litest_touch_up(dev, 0);
202         litest_touch_up(dev, 1);
203
204         libinput_dispatch(li);
205
206         assert_button_event(li, BTN_RIGHT,
207                             LIBINPUT_POINTER_BUTTON_STATE_PRESSED);
208         usleep(300000); /* tap-n-drag timeout */
209         assert_button_event(li, BTN_RIGHT,
210                             LIBINPUT_POINTER_BUTTON_STATE_RELEASED);
211
212         libinput_dispatch(li);
213         event = libinput_get_event(li);
214         ck_assert(event == NULL);
215 }
216 END_TEST
217
218 START_TEST(touchpad_1fg_clickfinger)
219 {
220         struct litest_device *dev = litest_create_device(LITEST_BCM5974);
221         struct libinput *li = dev->libinput;
222         struct libinput_event *event;
223         struct libinput_event_pointer *ptrev;
224
225         litest_drain_events(li);
226
227         litest_touch_down(dev, 0, 50, 50);
228         litest_event(dev, EV_KEY, BTN_LEFT, 1);
229         litest_event(dev, EV_SYN, SYN_REPORT, 0);
230         litest_event(dev, EV_KEY, BTN_LEFT, 0);
231         litest_event(dev, EV_SYN, SYN_REPORT, 0);
232         litest_touch_up(dev, 0);
233
234         libinput_dispatch(li);
235
236         assert_button_event(li, BTN_LEFT,
237                             LIBINPUT_POINTER_BUTTON_STATE_PRESSED);
238         assert_button_event(li, BTN_LEFT,
239                             LIBINPUT_POINTER_BUTTON_STATE_RELEASED);
240
241         litest_delete_device(dev);
242 }
243 END_TEST
244
245 START_TEST(touchpad_2fg_clickfinger)
246 {
247         struct litest_device *dev = litest_create_device(LITEST_BCM5974);
248         struct libinput *li = dev->libinput;
249         struct libinput_event *event;
250         struct libinput_event_pointer *ptrev;
251
252         litest_drain_events(li);
253
254         litest_touch_down(dev, 0, 50, 50);
255         litest_touch_down(dev, 1, 70, 70);
256         litest_event(dev, EV_KEY, BTN_LEFT, 1);
257         litest_event(dev, EV_SYN, SYN_REPORT, 0);
258         litest_event(dev, EV_KEY, BTN_LEFT, 0);
259         litest_event(dev, EV_SYN, SYN_REPORT, 0);
260         litest_touch_up(dev, 0);
261         litest_touch_up(dev, 1);
262
263         libinput_dispatch(li);
264
265         assert_button_event(li, BTN_RIGHT,
266                             LIBINPUT_POINTER_BUTTON_STATE_PRESSED);
267         assert_button_event(li, BTN_RIGHT,
268                             LIBINPUT_POINTER_BUTTON_STATE_RELEASED);
269
270         litest_delete_device(dev);
271 }
272 END_TEST
273
274 START_TEST(touchpad_btn_left)
275 {
276         struct litest_device *dev = litest_current_device();
277         struct libinput *li = dev->libinput;
278         struct libinput_event *event;
279         struct libinput_event_pointer *ptrev;
280         enum libinput_pointer_button_state btnstate;
281
282         litest_drain_events(li);
283
284         litest_event(dev, EV_KEY, BTN_LEFT, 1);
285         litest_event(dev, EV_SYN, SYN_REPORT, 0);
286         litest_event(dev, EV_KEY, BTN_LEFT, 0);
287         litest_event(dev, EV_SYN, SYN_REPORT, 0);
288
289         libinput_dispatch(li);
290
291         assert_button_event(li, BTN_LEFT,
292                             LIBINPUT_POINTER_BUTTON_STATE_PRESSED);
293         assert_button_event(li, BTN_LEFT,
294                             LIBINPUT_POINTER_BUTTON_STATE_RELEASED);
295 }
296 END_TEST
297
298 START_TEST(clickpad_btn_left)
299 {
300         struct litest_device *dev = litest_current_device();
301         struct libinput *li = dev->libinput;
302
303         litest_drain_events(li);
304
305         /* A clickpad always needs a finger down to tell where the
306            click happens */
307         litest_event(dev, EV_KEY, BTN_LEFT, 1);
308         litest_event(dev, EV_SYN, SYN_REPORT, 0);
309         litest_event(dev, EV_KEY, BTN_LEFT, 0);
310         litest_event(dev, EV_SYN, SYN_REPORT, 0);
311
312         libinput_dispatch(li);
313         ck_assert_int_eq(libinput_next_event_type(li), LIBINPUT_EVENT_NONE);
314 }
315 END_TEST
316
317 START_TEST(clickpad_click_n_drag)
318 {
319         struct litest_device *dev = litest_current_device();
320         struct libinput *li = dev->libinput;
321         struct libinput_event *event;
322         struct libinput_event_pointer *ptrev;
323
324         litest_drain_events(li);
325
326         litest_touch_down(dev, 0, 50, 50);
327         litest_event(dev, EV_KEY, BTN_LEFT, 1);
328         litest_event(dev, EV_SYN, SYN_REPORT, 0);
329
330         libinput_dispatch(li);
331         assert_button_event(li, BTN_LEFT,
332                             LIBINPUT_POINTER_BUTTON_STATE_PRESSED);
333
334         libinput_dispatch(li);
335         ck_assert_int_eq(libinput_next_event_type(li), LIBINPUT_EVENT_NONE);
336
337         /* now put a second finger down */
338         litest_touch_down(dev, 1, 70, 70);
339         litest_touch_move_to(dev, 1, 70, 70, 80, 50, 5);
340         litest_touch_up(dev, 1);
341
342         libinput_dispatch(li);
343         ck_assert_int_eq(libinput_next_event_type(li),
344                          LIBINPUT_EVENT_POINTER_MOTION);
345         do {
346                 event = libinput_get_event(li);
347                 libinput_event_destroy(event);
348                 libinput_dispatch(li);
349         } while (libinput_next_event_type(li) == LIBINPUT_EVENT_POINTER_MOTION);
350
351         litest_event(dev, EV_KEY, BTN_LEFT, 0);
352         litest_event(dev, EV_SYN, SYN_REPORT, 0);
353         litest_touch_up(dev, 0);
354
355         assert_button_event(li, BTN_LEFT,
356                             LIBINPUT_POINTER_BUTTON_STATE_RELEASED);
357 }
358 END_TEST
359
360 int main(int argc, char **argv) {
361
362         litest_add("touchpad:motion", touchpad_1fg_motion, LITEST_TOUCHPAD, LITEST_ANY);
363         litest_add("touchpad:motion", touchpad_2fg_no_motion, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH);
364
365         litest_add("touchpad:tap", touchpad_1fg_tap, LITEST_TOUCHPAD, LITEST_ANY);
366         litest_add("touchpad:tap", touchpad_1fg_tap_n_drag, LITEST_TOUCHPAD, LITEST_ANY);
367         litest_add("touchpad:tap", touchpad_2fg_tap, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH);
368
369         litest_add_no_device("touchpad:clickfinger", touchpad_1fg_clickfinger);
370         litest_add_no_device("touchpad:clickfinger", touchpad_2fg_clickfinger);
371
372         litest_add("touchpad:click", touchpad_btn_left, LITEST_TOUCHPAD, LITEST_CLICKPAD);
373         litest_add("touchpad:click", clickpad_btn_left, LITEST_CLICKPAD, LITEST_ANY);
374         litest_add("touchpad:click", clickpad_click_n_drag, LITEST_CLICKPAD, LITEST_SINGLE_TOUCH);
375
376         return litest_run(argc, argv);
377 }