style fix: Remove duplicate empty lines
[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, 10 },
76                 { ABS_Y, 0, 32767, 129, 0, 9 },
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 START_TEST(touch_many_slots)
116 {
117         struct libinput *libinput;
118         struct litest_device *dev;
119         struct libinput_event *ev;
120         int slot;
121         const int num_tps = 100;
122         int slot_count = 0;
123         enum libinput_event_type type;
124
125         struct input_absinfo abs[] = {
126                 { ABS_MT_SLOT, 0, num_tps - 1, 0, 0, 0 },
127                 { .value = -1 },
128         };
129
130         dev = litest_create_device_with_overrides(LITEST_WACOM_TOUCH,
131                                                   "litest Multi-touch device",
132                                                   NULL, abs, NULL);
133         libinput = dev->libinput;
134
135         for (slot = 0; slot < num_tps; ++slot)
136                 litest_touch_down(dev, slot, 0, 0);
137         for (slot = 0; slot < num_tps; ++slot)
138                 litest_touch_up(dev, slot);
139
140         libinput_dispatch(libinput);
141         while ((ev = libinput_get_event(libinput))) {
142                 type = libinput_event_get_type(ev);
143
144                 if (type == LIBINPUT_EVENT_TOUCH_DOWN)
145                         slot_count++;
146                 else if (type == LIBINPUT_EVENT_TOUCH_UP)
147                         break;
148
149                 libinput_event_destroy(ev);
150                 libinput_dispatch(libinput);
151         }
152
153         ck_assert_notnull(ev);
154         ck_assert_int_gt(slot_count, 0);
155
156         libinput_dispatch(libinput);
157         do {
158                 type = libinput_event_get_type(ev);
159                 ck_assert_int_ne(type, LIBINPUT_EVENT_TOUCH_DOWN);
160                 if (type == LIBINPUT_EVENT_TOUCH_UP)
161                         slot_count--;
162
163                 libinput_event_destroy(ev);
164                 libinput_dispatch(libinput);
165         } while ((ev = libinput_get_event(libinput)));
166
167         ck_assert_int_eq(slot_count, 0);
168
169         litest_delete_device(dev);
170 }
171 END_TEST
172
173 START_TEST(touch_double_touch_down_up)
174 {
175         struct libinput *libinput;
176         struct litest_device *dev;
177         struct libinput_event *ev;
178         bool got_down = false;
179         bool got_up = false;
180
181         dev = litest_current_device();
182         libinput = dev->libinput;
183
184         litest_touch_down(dev, 0, 0, 0);
185         litest_touch_down(dev, 0, 0, 0);
186         litest_touch_up(dev, 0);
187         litest_touch_up(dev, 0);
188
189         libinput_dispatch(libinput);
190
191         while ((ev = libinput_get_event(libinput))) {
192                 switch (libinput_event_get_type(ev)) {
193                 case LIBINPUT_EVENT_TOUCH_DOWN:
194                         ck_assert(!got_down);
195                         got_down = true;
196                         break;
197                 case LIBINPUT_EVENT_TOUCH_UP:
198                         ck_assert(got_down);
199                         ck_assert(!got_up);
200                         got_up = true;
201                         break;
202                 default:
203                         break;
204                 }
205
206                 libinput_event_destroy(ev);
207                 libinput_dispatch(libinput);
208         }
209
210         ck_assert(got_down);
211         ck_assert(got_up);
212 }
213 END_TEST
214
215 int
216 main(int argc, char **argv)
217 {
218         litest_add("touch:frame", touch_frame_events, LITEST_TOUCH, LITEST_ANY);
219         litest_add_no_device("touch:abs-transform", touch_abs_transform);
220         litest_add_no_device("touch:many-slots", touch_many_slots);
221         litest_add("touch:double-touch-down-up", touch_double_touch_down_up, LITEST_TOUCH, LITEST_ANY);
222
223         return litest_run(argc, argv);
224 }