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/*
29 * Protocol testing for XIQueryPointer request.
30 */
31#include <stdint.h>
32#include <X11/X.h>
33#include <X11/Xproto.h>
34#include <X11/extensions/XI2proto.h>
35#include "inputstr.h"
36#include "windowstr.h"
37#include "scrnintstr.h"
38#include "xiquerypointer.h"
39#include "exevents.h"
40
41#include "protocol-common.h"
42#include <glib.h>
43
44static ClientRec client_request;
45static void reply_XIQueryPointer_data(ClientPtr client, int len,
46                                      char *data, void *userdata);
47
48static struct {
49    DeviceIntPtr dev;
50    WindowPtr win;
51} test_data;
52
53
54/* dixLookupWindow requires a lot of setup not necessary for this test.
55 * Simple wrapper that returns either one of the fake root window or the
56 * fake client window. If the requested ID is neither of those wanted,
57 * return whatever the real dixLookupWindow does.
58 */
59int __wrap_dixLookupWindow(WindowPtr *win, XID id, ClientPtr client, Mask access)
60{
61    if (id == root.drawable.id)
62    {
63        *win = &root;
64        return Success;
65    } else if (id == window.drawable.id)
66    {
67        *win = &window;
68        return Success;
69    }
70
71    return __real_dixLookupWindow(win, id, client, access);
72}
73
74static void reply_XIQueryPointer(ClientPtr client, int len, char *data,
75                                 void *userdata)
76{
77    xXIQueryPointerReply *rep = (xXIQueryPointerReply*)data;
78    SpritePtr sprite;
79
80    if (!rep->repType)
81        return;
82
83    if (client->swapped)
84    {
85        char n;
86        swapl(&rep->length, n);
87        swaps(&rep->sequenceNumber, n);
88        swapl(&rep->root, n);
89        swapl(&rep->child, n);
90        swapl(&rep->root_x, n);
91        swapl(&rep->root_y, n);
92        swapl(&rep->win_x, n);
93        swapl(&rep->win_y, n);
94        swaps(&rep->buttons_len, n);
95    }
96
97    reply_check_defaults(rep, len, XIQueryPointer);
98
99    g_assert(rep->root == root.drawable.id);
100    g_assert(rep->same_screen == xTrue);
101
102    sprite = test_data.dev->spriteInfo->sprite;
103    g_assert((rep->root_x >> 16) == sprite->hot.x);
104    g_assert((rep->root_y >> 16) == sprite->hot.y);
105
106    if (test_data.win == &root)
107    {
108        g_assert(rep->root_x == rep->win_x);
109        g_assert(rep->root_y == rep->win_y);
110        g_assert(rep->child == window.drawable.id);
111    } else
112    {
113        int x, y;
114
115        x = sprite->hot.x - window.drawable.x;
116        y = sprite->hot.y - window.drawable.y;
117
118        g_assert((rep->win_x >> 16) == x);
119        g_assert((rep->win_y >> 16) == y);
120        g_assert(rep->child == None);
121    }
122
123
124    g_assert(rep->same_screen == xTrue);
125
126    reply_handler = reply_XIQueryPointer_data;
127}
128
129static void reply_XIQueryPointer_data(ClientPtr client, int len, char *data, void *userdata)
130{
131    reply_handler = reply_XIQueryPointer;
132}
133
134static void request_XIQueryPointer(ClientPtr client, xXIQueryPointerReq* req, int error)
135{
136    char n;
137    int rc;
138
139    rc = ProcXIQueryPointer(&client_request);
140    g_assert(rc == error);
141
142    if (rc == BadDevice)
143        g_assert(client_request.errorValue == req->deviceid);
144
145    client_request.swapped = TRUE;
146    swaps(&req->deviceid, n);
147    swaps(&req->length, n);
148    rc = SProcXIQueryPointer(&client_request);
149    g_assert(rc == error);
150
151    if (rc == BadDevice)
152        g_assert(client_request.errorValue == req->deviceid);
153}
154
155static void test_XIQueryPointer(void)
156{
157    int i;
158    xXIQueryPointerReq request;
159
160    memset(&request, 0, sizeof(request));
161
162    request_init(&request, XIQueryPointer);
163
164    reply_handler = reply_XIQueryPointer;
165
166    client_request = init_client(request.length, &request);
167
168    request.deviceid = XIAllDevices;
169    request_XIQueryPointer(&client_request, &request, BadDevice);
170
171    request.deviceid = XIAllMasterDevices;
172    request_XIQueryPointer(&client_request, &request, BadDevice);
173
174    request.win = root.drawable.id;
175    test_data.win = &root;
176
177    test_data.dev = devices.vcp;
178    request.deviceid = devices.vcp->id;
179    request_XIQueryPointer(&client_request, &request, Success);
180    request.deviceid = devices.vck->id;
181    request_XIQueryPointer(&client_request, &request, BadDevice);
182    request.deviceid = devices.mouse->id;
183    request_XIQueryPointer(&client_request, &request, BadDevice);
184    request.deviceid = devices.kbd->id;
185    request_XIQueryPointer(&client_request, &request, BadDevice);
186
187    test_data.dev = devices.mouse;
188    devices.mouse->u.master = NULL; /* Float, kind-of */
189    request.deviceid = devices.mouse->id;
190    request_XIQueryPointer(&client_request, &request, Success);
191
192    for (i = devices.kbd->id + 1; i <= 0xFFFF; i++)
193    {
194        request.deviceid = i;
195        request_XIQueryPointer(&client_request, &request, BadDevice);
196    }
197
198    request.win = window.drawable.id;
199
200    test_data.dev = devices.vcp;
201    test_data.win = &window;
202    request.deviceid = devices.vcp->id;
203    request_XIQueryPointer(&client_request, &request, Success);
204
205    test_data.dev = devices.mouse;
206    request.deviceid = devices.mouse->id;
207    request_XIQueryPointer(&client_request, &request, Success);
208
209    /* test REQUEST_SIZE_MATCH */
210    client_request.req_len -= 4;
211    request_XIQueryPointer(&client_request, &request, BadLength);
212}
213
214int main(int argc, char** argv)
215{
216    g_test_init(&argc, &argv,NULL);
217    g_test_bug_base("https://bugzilla.freedesktop.org/show_bug.cgi?id=");
218
219    init_simple();
220
221    g_test_add_func("/xi2/protocol/XIQueryPointer", test_XIQueryPointer);
222
223    return g_test_run();
224}
225