touchpad: normalize the touchpad resolution to 400dpi, not 10 units/mm
[platform/upstream/libinput.git] / test / touch.c
1 /*
2  * Copyright © 2013 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(touch_frame_events)
35 {
36         struct litest_device *dev = litest_current_device();
37         struct libinput *li = dev->libinput;
38         struct libinput_event *event;
39         int have_frame_event = 0;
40
41         litest_drain_events(dev->libinput);
42
43         litest_touch_down(dev, 0, 10, 10);
44         libinput_dispatch(li);
45
46         while ((event = libinput_get_event(li))) {
47                 if (libinput_event_get_type(event) == LIBINPUT_EVENT_TOUCH_FRAME)
48                         have_frame_event++;
49                 libinput_event_destroy(event);
50         }
51         ck_assert_int_eq(have_frame_event, 1);
52
53         litest_touch_down(dev, 1, 10, 10);
54         libinput_dispatch(li);
55
56         while ((event = libinput_get_event(li))) {
57                 if (libinput_event_get_type(event) == LIBINPUT_EVENT_TOUCH_FRAME)
58                         have_frame_event++;
59                 libinput_event_destroy(event);
60         }
61         ck_assert_int_eq(have_frame_event, 2);
62 }
63 END_TEST
64
65 START_TEST(touch_abs_transform)
66 {
67         struct litest_device *dev;
68         struct libinput *libinput;
69         struct libinput_event *ev;
70         struct libinput_event_touch *tev;
71         double fx, fy;
72         bool tested = false;
73
74         struct input_absinfo abs[] = {
75                 { ABS_X, 0, 32767, 75, 0, 0 },
76                 { ABS_Y, 0, 32767, 129, 0, 0 },
77                 { ABS_MT_POSITION_X, 0, 32767, 0, 0, 10 },
78                 { ABS_MT_POSITION_Y, 0, 32767, 0, 0, 9 },
79                 { .value = -1 },
80         };
81
82         dev = litest_create_device_with_overrides(LITEST_WACOM_TOUCH,
83                                                   "litest Highres touch device",
84                                                   NULL, abs, NULL);
85
86         libinput = dev->libinput;
87
88         litest_touch_down(dev, 0, 100, 100);
89
90         libinput_dispatch(libinput);
91
92         while ((ev = libinput_get_event(libinput))) {
93                 if (libinput_event_get_type(ev) != LIBINPUT_EVENT_TOUCH_DOWN) {
94                         libinput_event_destroy(ev);
95                         continue;
96                 }
97
98                 tev = libinput_event_get_touch_event(ev);
99                 fx = libinput_event_touch_get_x_transformed(tev, 1920);
100                 ck_assert_int_eq(fx, 1919.0);
101                 fy = libinput_event_touch_get_y_transformed(tev, 720);
102                 ck_assert_int_eq(fy, 719.0);
103
104                 tested = true;
105
106                 libinput_event_destroy(ev);
107         }
108
109         ck_assert(tested);
110
111         litest_delete_device(dev);
112 }
113 END_TEST
114
115
116 START_TEST(touch_many_slots)
117 {
118         struct libinput *libinput;
119         struct litest_device *dev;
120         struct libinput_event *ev;
121         int slot;
122         const int num_tps = 100;
123         int slot_count = 0;
124         enum libinput_event_type type;
125
126         struct input_absinfo abs[] = {
127                 { ABS_MT_SLOT, 0, num_tps - 1, 0, 0, 0 },
128                 { .value = -1 },
129         };
130
131         dev = litest_create_device_with_overrides(LITEST_WACOM_TOUCH,
132                                                   "litest Multi-touch device",
133                                                   NULL, abs, NULL);
134         libinput = dev->libinput;
135
136         for (slot = 0; slot < num_tps; ++slot)
137                 litest_touch_down(dev, slot, 0, 0);
138         for (slot = 0; slot < num_tps; ++slot)
139                 litest_touch_up(dev, slot);
140
141         libinput_dispatch(libinput);
142         while ((ev = libinput_get_event(libinput))) {
143                 type = libinput_event_get_type(ev);
144
145                 if (type == LIBINPUT_EVENT_TOUCH_DOWN)
146                         slot_count++;
147                 else if (type == LIBINPUT_EVENT_TOUCH_UP)
148                         break;
149
150                 libinput_event_destroy(ev);
151                 libinput_dispatch(libinput);
152         }
153
154         ck_assert_notnull(ev);
155         ck_assert_int_gt(slot_count, 0);
156
157         libinput_dispatch(libinput);
158         do {
159                 type = libinput_event_get_type(ev);
160                 ck_assert_int_ne(type, LIBINPUT_EVENT_TOUCH_DOWN);
161                 if (type == LIBINPUT_EVENT_TOUCH_UP)
162                         slot_count--;
163
164                 libinput_event_destroy(ev);
165                 libinput_dispatch(libinput);
166         } while ((ev = libinput_get_event(libinput)));
167
168         ck_assert_int_eq(slot_count, 0);
169
170         litest_delete_device(dev);
171 }
172 END_TEST
173
174 START_TEST(touch_double_touch_down_up)
175 {
176         struct libinput *libinput;
177         struct litest_device *dev;
178         struct libinput_event *ev;
179         bool got_down = false;
180         bool got_up = false;
181
182         dev = litest_current_device();
183         libinput = dev->libinput;
184
185         litest_touch_down(dev, 0, 0, 0);
186         litest_touch_down(dev, 0, 0, 0);
187         litest_touch_up(dev, 0);
188         litest_touch_up(dev, 0);
189
190         libinput_dispatch(libinput);
191
192         while ((ev = libinput_get_event(libinput))) {
193                 switch (libinput_event_get_type(ev)) {
194                 case LIBINPUT_EVENT_TOUCH_DOWN:
195                         ck_assert(!got_down);
196                         got_down = true;
197                         break;
198                 case LIBINPUT_EVENT_TOUCH_UP:
199                         ck_assert(got_down);
200                         ck_assert(!got_up);
201                         got_up = true;
202                         break;
203                 default:
204                         break;
205                 }
206
207                 libinput_event_destroy(ev);
208                 libinput_dispatch(libinput);
209         }
210
211         ck_assert(got_down);
212         ck_assert(got_up);
213 }
214 END_TEST
215
216 int
217 main(int argc, char **argv)
218 {
219         litest_add("touch:frame", touch_frame_events, LITEST_TOUCH, LITEST_ANY);
220         litest_add_no_device("touch:abs-transform", touch_abs_transform);
221         litest_add_no_device("touch:many-slots", touch_many_slots);
222         litest_add("touch:double-touch-down-up", touch_double_touch_down_up, LITEST_TOUCH, LITEST_ANY);
223
224         return litest_run(argc, argv);
225 }