test: fix two more failing FP3232 tests
[gstreamer-omap:xserver.git] / test / xi2 / protocol-xiquerydevice.c
1 /**
2  * Copyright © 2009 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 #ifdef HAVE_DIX_CONFIG_H
25 #include <dix-config.h>
26 #endif
27
28 #include <stdint.h>
29 #include <X11/X.h>
30 #include <X11/Xproto.h>
31 #include <X11/extensions/XI2proto.h>
32 #include <X11/Xatom.h>
33 #include "inputstr.h"
34 #include "extinit.h"
35 #include "scrnintstr.h"
36 #include "xkbsrv.h"
37
38 #include "xiquerydevice.h"
39
40 #include "protocol-common.h"
41 /*
42  * Protocol testing for XIQueryDevice request and reply.
43  *
44  * Test approach:
45  * Wrap WriteToClient to intercept server's reply. ProcXIQueryDevice returns
46  * data in two batches, once for the request, once for the trailing data
47  * with the device information.
48  * Repeatedly test with varying deviceids and check against data in reply.
49  */
50
51 struct test_data {
52     int which_device;
53     int num_devices_in_reply;
54 };
55
56 static void reply_XIQueryDevice_data(ClientPtr client, int len, char *data, void *userdata);
57 static void reply_XIQueryDevice(ClientPtr client, int len, char* data, void *userdata);
58
59 /* reply handling for the first bytes that constitute the reply */
60 static void reply_XIQueryDevice(ClientPtr client, int len, char* data, void *userdata)
61 {
62     xXIQueryDeviceReply *rep = (xXIQueryDeviceReply*)data;
63     struct test_data *querydata = (struct test_data*)userdata;
64
65     if (client->swapped)
66     {
67         swapl(&rep->length);
68         swaps(&rep->sequenceNumber);
69         swaps(&rep->num_devices);
70     }
71
72     reply_check_defaults(rep, len, XIQueryDevice);
73
74     if (querydata->which_device == XIAllDevices)
75         assert(rep->num_devices == devices.num_devices);
76     else if (querydata->which_device == XIAllMasterDevices)
77         assert(rep->num_devices == devices.num_master_devices);
78     else
79         assert(rep->num_devices == 1);
80
81     querydata->num_devices_in_reply = rep->num_devices;
82     reply_handler = reply_XIQueryDevice_data;
83 }
84
85 /* reply handling for the trailing bytes that constitute the device info */
86 static void reply_XIQueryDevice_data(ClientPtr client, int len, char *data, void *userdata)
87 {
88     int i, j;
89     struct test_data *querydata = (struct test_data*)userdata;
90
91     DeviceIntPtr dev;
92     xXIDeviceInfo *info = (xXIDeviceInfo*)data;
93     xXIAnyInfo *any;
94
95     for (i = 0; i < querydata->num_devices_in_reply; i++)
96     {
97         if (client->swapped)
98         {
99             swaps(&info->deviceid);
100             swaps(&info->attachment);
101             swaps(&info->use);
102             swaps(&info->num_classes);
103             swaps(&info->name_len);
104         }
105
106         if (querydata->which_device > XIAllMasterDevices)
107             assert(info->deviceid == querydata->which_device);
108
109         assert(info->deviceid >=  2); /* 0 and 1 is reserved */
110
111
112         switch(info->deviceid)
113         {
114             case 2:  /* VCP */
115                 dev = devices.vcp;
116                 assert(info->use == XIMasterPointer);
117                 assert(info->attachment == devices.vck->id);
118                 assert(info->num_classes == 3); /* 2 axes + button */
119                 break;
120             case 3:  /* VCK */
121                 dev = devices.vck;
122                 assert(info->use == XIMasterKeyboard);
123                 assert(info->attachment == devices.vcp->id);
124                 assert(info->num_classes == 1);
125                 break;
126             case 4:  /* mouse */
127                 dev = devices.mouse;
128                 assert(info->use == XISlavePointer);
129                 assert(info->attachment == devices.vcp->id);
130                 assert(info->num_classes == 7); /* 4 axes + button + 2 scroll*/
131                 break;
132             case 5:  /* keyboard */
133                 dev = devices.kbd;
134                 assert(info->use == XISlaveKeyboard);
135                 assert(info->attachment == devices.vck->id);
136                 assert(info->num_classes == 1);
137                 break;
138
139             default:
140                 /* We shouldn't get here */
141                 assert(0);
142                 break;
143         }
144         assert(info->enabled == dev->enabled);
145         assert(info->name_len == strlen(dev->name));
146         assert(strncmp((char*)&info[1], dev->name, info->name_len) == 0);
147
148         any = (xXIAnyInfo*)((char*)&info[1] + ((info->name_len + 3)/4) * 4);
149         for (j = 0; j < info->num_classes; j++)
150         {
151             if (client->swapped)
152             {
153                 swaps(&any->type);
154                 swaps(&any->length);
155                 swaps(&any->sourceid);
156             }
157
158             switch(info->deviceid)
159             {
160                 case 3: /* VCK and kbd have the same properties */
161                 case 5:
162                     {
163                         int k;
164                         xXIKeyInfo *ki = (xXIKeyInfo*)any;
165                         XkbDescPtr xkb = devices.vck->key->xkbInfo->desc;
166                         uint32_t *kc;
167
168                         if (client->swapped)
169                             swaps(&ki->num_keycodes);
170
171                         assert(any->type == XIKeyClass);
172                         assert(ki->num_keycodes == (xkb->max_key_code - xkb->min_key_code + 1));
173                         assert(any->length == (2 + ki->num_keycodes));
174
175                         kc = (uint32_t*)&ki[1];
176                         for (k = 0; k < ki->num_keycodes; k++, kc++)
177                         {
178                             if (client->swapped)
179                                 swapl(kc);
180
181                             assert(*kc >= xkb->min_key_code);
182                             assert(*kc <= xkb->max_key_code);
183                         }
184                         break;
185                     }
186                 case 4:
187                     {
188                         assert(any->type == XIButtonClass ||
189                                any->type == XIValuatorClass ||
190                                any->type == XIScrollClass);
191
192                         if (any->type == XIScrollClass)
193                         {
194                             xXIScrollInfo *si = (xXIScrollInfo*)any;
195
196                             if (client->swapped)
197                             {
198                                 swaps(&si->number);
199                                 swaps(&si->scroll_type);
200                                 swapl(&si->increment.integral);
201                                 swapl(&si->increment.frac);
202                             }
203                             assert(si->length == 6);
204                             assert(si->number == 2 || si->number == 3);
205                             if (si->number == 2) {
206                                 assert(si->scroll_type == XIScrollTypeVertical);
207                                 assert(!si->flags);
208                             }
209                             if (si->number == 3) {
210                                 assert(si->scroll_type == XIScrollTypeHorizontal);
211                                 assert(si->flags & XIScrollFlagPreferred);
212                                 assert(!(si->flags & ~XIScrollFlagPreferred));
213                             }
214
215                             assert(si->increment.integral == si->number);
216                             /* protocol-common.c sets up increments of 2.4 and 3.5 */
217                             assert(si->increment.frac > 0.3  * (1ULL << 32));
218                             assert(si->increment.frac < 0.6  * (1ULL << 32));
219                         }
220
221                     }
222                     /* fall through */
223                 case 2: /* VCP and mouse have the same properties except for scroll */
224                     {
225                         if (info->deviceid == 2 ) /* VCP */
226                             assert(any->type == XIButtonClass ||
227                                    any->type == XIValuatorClass);
228
229                         if (any->type == XIButtonClass)
230                         {
231                             int len;
232                             xXIButtonInfo *bi = (xXIButtonInfo*)any;
233
234                             if (client->swapped)
235                                 swaps(&bi->num_buttons);
236
237                             assert(bi->num_buttons == devices.vcp->button->numButtons);
238
239                             len = 2 + bi->num_buttons + bytes_to_int32(bits_to_bytes(bi->num_buttons));
240                             assert(bi->length == len);
241                         } else if (any->type == XIValuatorClass)
242                         {
243                             xXIValuatorInfo *vi = (xXIValuatorInfo*)any;
244
245                             if (client->swapped)
246                             {
247                                 swaps(&vi->number);
248                                 swapl(&vi->label);
249                                 swapl(&vi->min.integral);
250                                 swapl(&vi->min.frac);
251                                 swapl(&vi->max.integral);
252                                 swapl(&vi->max.frac);
253                                 swapl(&vi->resolution);
254                             }
255
256                             assert(vi->length == 11);
257                             assert(vi->number >= 0 && vi->number < 4);
258                             if (info->deviceid == 2) /* VCP */
259                                 assert(vi->number < 2);
260
261                             assert(vi->mode == XIModeRelative);
262                             /* device was set up as relative, so standard
263                              * values here. */
264                             assert(vi->min.integral == -1);
265                             assert(vi->min.frac == 0);
266                             assert(vi->max.integral == -1);
267                             assert(vi->max.frac == 0);
268                             assert(vi->resolution == 0);
269                         }
270                     }
271                     break;
272             }
273             any = (xXIAnyInfo*)(((char*)any) + any->length * 4);
274         }
275
276         info = (xXIDeviceInfo*)any;
277     }
278 }
279
280 static void request_XIQueryDevice(struct test_data *querydata,
281                                  int deviceid, int error)
282 {
283     int rc;
284     ClientRec client;
285     xXIQueryDeviceReq request;
286
287     request_init(&request, XIQueryDevice);
288     client = init_client(request.length, &request);
289     reply_handler = reply_XIQueryDevice;
290
291     querydata->which_device = deviceid;
292
293     request.deviceid = deviceid;
294     rc = ProcXIQueryDevice(&client);
295     assert(rc == error);
296
297     if (rc != Success)
298         assert(client.errorValue == deviceid);
299
300     reply_handler = reply_XIQueryDevice;
301
302     client.swapped = TRUE;
303     swaps(&request.length);
304     swaps(&request.deviceid);
305     rc = SProcXIQueryDevice(&client);
306     assert(rc == error);
307
308     if (rc != Success)
309         assert(client.errorValue == deviceid);
310 }
311
312 static void test_XIQueryDevice(void)
313 {
314     int i;
315     xXIQueryDeviceReq request;
316     struct test_data data;
317
318     reply_handler = reply_XIQueryDevice;
319     userdata = &data;
320     request_init(&request, XIQueryDevice);
321
322     printf("Testing XIAllDevices.\n");
323     request_XIQueryDevice(&data, XIAllDevices, Success);
324     printf("Testing XIAllMasterDevices.\n");
325     request_XIQueryDevice(&data, XIAllMasterDevices, Success);
326
327     printf("Testing existing device ids.\n");
328     for (i = 2; i < 6; i++)
329         request_XIQueryDevice(&data, i, Success);
330
331     printf("Testing non-existing device ids.\n");
332     for (i = 6; i <= 0xFFFF; i++)
333         request_XIQueryDevice(&data, i, BadDevice);
334
335
336     reply_handler = NULL;
337
338 }
339
340 int main(int argc, char** argv)
341 {
342     init_simple();
343
344     test_XIQueryDevice();
345
346     return 0;
347 }
348