1/**
2 * Copyright © 2011 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/* Test relies on assert() */
25#undef NDEBUG
26
27#ifdef HAVE_DIX_CONFIG_H
28#include <dix-config.h>
29#endif
30
31#include <stdint.h>
32#include "inputstr.h"
33#include "assert.h"
34#include "scrnintstr.h"
35
36#include "tests-common.h"
37
38static void
39touch_grow_queue(void)
40{
41    DeviceIntRec dev;
42    ValuatorClassRec val;
43    TouchClassRec touch;
44    size_t size, new_size;
45    int i;
46
47    memset(&dev, 0, sizeof(dev));
48    dev.name = xnfstrdup("test device");
49    dev.id = 2;
50    dev.valuator = &val;
51    val.numAxes = 5;
52    dev.touch = &touch;
53    inputInfo.devices = &dev;
54
55    size = 5;
56
57    dev.last.num_touches = size;
58    dev.last.touches = calloc(dev.last.num_touches, sizeof(*dev.last.touches));
59    assert(dev.last.touches);
60    for (i = 0; i < size; i++) {
61        dev.last.touches[i].active = TRUE;
62        dev.last.touches[i].ddx_id = i;
63        dev.last.touches[i].client_id = i * 2;
64    }
65
66    /* no more space, should've reallocated and succeeded */
67    assert(TouchBeginDDXTouch(&dev, 1234) != NULL);
68
69    new_size = size + size / 2 + 1;
70    assert(dev.last.num_touches == new_size);
71
72    /* make sure we haven't touched those */
73    for (i = 0; i < size; i++) {
74        DDXTouchPointInfoPtr t = &dev.last.touches[i];
75
76        assert(t->active == TRUE);
77        assert(t->ddx_id == i);
78        assert(t->client_id == i * 2);
79    }
80
81    assert(dev.last.touches[size].active == TRUE);
82    assert(dev.last.touches[size].ddx_id == 1234);
83    assert(dev.last.touches[size].client_id == 1);
84
85    /* make sure those are zero-initialized */
86    for (i = size + 1; i < new_size; i++) {
87        DDXTouchPointInfoPtr t = &dev.last.touches[i];
88
89        assert(t->active == FALSE);
90        assert(t->client_id == 0);
91        assert(t->ddx_id == 0);
92    }
93
94    free(dev.name);
95}
96
97static void
98touch_find_ddxid(void)
99{
100    DeviceIntRec dev;
101    DDXTouchPointInfoPtr ti, ti2;
102    ValuatorClassRec val;
103    TouchClassRec touch;
104    int size = 5;
105    int i;
106
107    memset(&dev, 0, sizeof(dev));
108    dev.name = xnfstrdup("test device");
109    dev.id = 2;
110    dev.valuator = &val;
111    val.numAxes = 5;
112    dev.touch = &touch;
113    dev.last.num_touches = size;
114    dev.last.touches = calloc(dev.last.num_touches, sizeof(*dev.last.touches));
115    inputInfo.devices = &dev;
116    assert(dev.last.touches);
117
118    dev.last.touches[0].active = TRUE;
119    dev.last.touches[0].ddx_id = 10;
120    dev.last.touches[0].client_id = 20;
121
122    /* existing */
123    ti = TouchFindByDDXID(&dev, 10, FALSE);
124    assert(ti == &dev.last.touches[0]);
125
126    /* non-existing */
127    ti = TouchFindByDDXID(&dev, 20, FALSE);
128    assert(ti == NULL);
129
130    /* Non-active */
131    dev.last.touches[0].active = FALSE;
132    ti = TouchFindByDDXID(&dev, 10, FALSE);
133    assert(ti == NULL);
134
135    /* create on number 2 */
136    dev.last.touches[0].active = TRUE;
137
138    ti = TouchFindByDDXID(&dev, 20, TRUE);
139    assert(ti == &dev.last.touches[1]);
140    assert(ti->active);
141    assert(ti->ddx_id == 20);
142
143    /* set all to active */
144    for (i = 0; i < size; i++)
145        dev.last.touches[i].active = TRUE;
146
147    /* Try to create more, succeed */
148    ti = TouchFindByDDXID(&dev, 30, TRUE);
149    assert(ti != NULL);
150    ti2 = TouchFindByDDXID(&dev, 30, TRUE);
151    assert(ti == ti2);
152    /* make sure we have resized */
153    assert(dev.last.num_touches == 8); /* EQ grows from 5 to 8 */
154
155    /* stop one touchpoint, try to create, succeed */
156    dev.last.touches[2].active = FALSE;
157    ti = TouchFindByDDXID(&dev, 35, TRUE);
158    assert(ti == &dev.last.touches[2]);
159    ti = TouchFindByDDXID(&dev, 40, TRUE);
160    assert(ti == &dev.last.touches[size+1]);
161
162    free(dev.name);
163}
164
165static void
166touch_begin_ddxtouch(void)
167{
168    DeviceIntRec dev;
169    DDXTouchPointInfoPtr ti;
170    ValuatorClassRec val;
171    TouchClassRec touch;
172    int ddx_id = 123;
173    unsigned int last_client_id = 0;
174    int size = 5;
175
176    memset(&dev, 0, sizeof(dev));
177    dev.name = xnfstrdup("test device");
178    dev.id = 2;
179    dev.valuator = &val;
180    val.numAxes = 5;
181    touch.mode = XIDirectTouch;
182    dev.touch = &touch;
183    dev.last.num_touches = size;
184    dev.last.touches = calloc(dev.last.num_touches, sizeof(*dev.last.touches));
185    inputInfo.devices = &dev;
186    assert(dev.last.touches);
187
188    ti = TouchBeginDDXTouch(&dev, ddx_id);
189    assert(ti);
190    assert(ti->ddx_id == ddx_id);
191    /* client_id == ddx_id can happen in real life, but not in this test */
192    assert(ti->client_id != ddx_id);
193    assert(ti->active);
194    assert(ti->client_id > last_client_id);
195    assert(ti->emulate_pointer);
196    last_client_id = ti->client_id;
197
198    ddx_id += 10;
199    ti = TouchBeginDDXTouch(&dev, ddx_id);
200    assert(ti);
201    assert(ti->ddx_id == ddx_id);
202    /* client_id == ddx_id can happen in real life, but not in this test */
203    assert(ti->client_id != ddx_id);
204    assert(ti->active);
205    assert(ti->client_id > last_client_id);
206    assert(!ti->emulate_pointer);
207    last_client_id = ti->client_id;
208
209    free(dev.name);
210}
211
212static void
213touch_begin_touch(void)
214{
215    DeviceIntRec dev;
216    TouchClassRec touch;
217    ValuatorClassRec val;
218    TouchPointInfoPtr ti;
219    int touchid = 12434;
220    int sourceid = 23;
221    SpriteInfoRec sprite;
222    ScreenRec screen;
223
224    screenInfo.screens[0] = &screen;
225
226    memset(&dev, 0, sizeof(dev));
227    dev.name = xnfstrdup("test device");
228    dev.id = 2;
229
230    memset(&sprite, 0, sizeof(sprite));
231    dev.spriteInfo = &sprite;
232
233    memset(&touch, 0, sizeof(touch));
234    touch.num_touches = 0;
235
236    memset(&val, 0, sizeof(val));
237    dev.valuator = &val;
238    val.numAxes = 2;
239
240    ti = TouchBeginTouch(&dev, sourceid, touchid, TRUE);
241    assert(!ti);
242
243    dev.touch = &touch;
244    ti = TouchBeginTouch(&dev, sourceid, touchid, TRUE);
245    assert(ti);
246    assert(ti->client_id == touchid);
247    assert(ti->active);
248    assert(ti->sourceid == sourceid);
249    assert(ti->emulate_pointer);
250
251    assert(touch.num_touches == 1);
252
253    free(dev.name);
254}
255
256static void
257touch_init(void)
258{
259    DeviceIntRec dev;
260    Atom labels[2] = { 0 };
261    int rc;
262    SpriteInfoRec sprite;
263    ScreenRec screen;
264
265    screenInfo.screens[0] = &screen;
266
267    memset(&dev, 0, sizeof(dev));
268    dev.name = xnfstrdup("test device");
269
270    memset(&sprite, 0, sizeof(sprite));
271    dev.spriteInfo = &sprite;
272
273    InitAtoms();
274    rc = InitTouchClassDeviceStruct(&dev, 1, XIDirectTouch, 2);
275    assert(rc == FALSE);
276
277    InitValuatorClassDeviceStruct(&dev, 2, labels, 10, Absolute);
278    rc = InitTouchClassDeviceStruct(&dev, 1, XIDirectTouch, 2);
279    assert(rc == TRUE);
280    assert(dev.touch);
281
282    free(dev.name);
283}
284
285int
286touch_test(void)
287{
288    printf("touch_test: start...\n");
289    touch_grow_queue();
290    touch_find_ddxid();
291    touch_begin_ddxtouch();
292    touch_init();
293    touch_begin_touch();
294
295    printf("touch_test: exiting successfully\n");
296    return 0;
297}
298