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 XIWarpPointer 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 "xiwarppointer.h"
39#include "exevents.h"
40
41#include "protocol-common.h"
42#include <glib.h>
43
44static int expected_x = SPRITE_X;
45static int expected_y = SPRITE_Y;
46
47/* dixLookupWindow requires a lot of setup not necessary for this test.
48 * Simple wrapper that returns either one of the fake root window or the
49 * fake client window. If the requested ID is neither of those wanted,
50 * return whatever the real dixLookupWindow does.
51 */
52int __wrap_dixLookupWindow(WindowPtr *win, XID id, ClientPtr client, Mask access)
53{
54    if (id == root.drawable.id)
55    {
56        *win = &root;
57        return Success;
58    } else if (id == window.drawable.id)
59    {
60        *win = &window;
61        return Success;
62    }
63
64    return __real_dixLookupWindow(win, id, client, access);
65}
66
67/**
68 * This function overrides the one in the screen rec.
69 */
70static Bool ScreenSetCursorPosition(DeviceIntPtr dev, ScreenPtr screen,
71                                    int x, int y, Bool generateEvent)
72{
73    g_assert(x == expected_x);
74    g_assert(y == expected_y);
75    return TRUE;
76}
77
78
79static void request_XIWarpPointer(ClientPtr client, xXIWarpPointerReq* req,
80        int error)
81{
82    char n;
83    int rc;
84
85    rc = ProcXIWarpPointer(client);
86    g_assert(rc == error);
87
88    if (rc == BadDevice)
89        g_assert(client->errorValue == req->deviceid);
90    else if (rc == BadWindow)
91        g_assert(client->errorValue == req->dst_win ||
92                 client->errorValue == req->src_win);
93
94
95    client->swapped = TRUE;
96
97    swapl(&req->src_win, n);
98    swapl(&req->dst_win, n);
99    swapl(&req->src_x, n);
100    swapl(&req->src_y, n);
101    swapl(&req->dst_x, n);
102    swapl(&req->dst_y, n);
103    swaps(&req->src_width, n);
104    swaps(&req->src_height, n);
105    swaps(&req->deviceid, n);
106
107    rc = SProcXIWarpPointer(client);
108    g_assert(rc == error);
109
110    if (rc == BadDevice)
111        g_assert(client->errorValue == req->deviceid);
112    else if (rc == BadWindow)
113        g_assert(client->errorValue == req->dst_win ||
114                 client->errorValue == req->src_win);
115
116    client->swapped = FALSE;
117}
118
119static void test_XIWarpPointer(void)
120{
121    int i;
122    ClientRec client_request;
123    xXIWarpPointerReq request;
124
125    memset(&request, 0, sizeof(request));
126
127    request_init(&request, XIWarpPointer);
128
129    client_request = init_client(request.length, &request);
130
131    request.deviceid = XIAllDevices;
132    request_XIWarpPointer(&client_request, &request, BadDevice);
133
134    request.deviceid = XIAllMasterDevices;
135    request_XIWarpPointer(&client_request, &request, BadDevice);
136
137    request.src_win = root.drawable.id;
138    request.dst_win = root.drawable.id;
139    request.deviceid = devices.vcp->id;
140    request_XIWarpPointer(&client_request, &request, Success);
141    request.deviceid = devices.vck->id;
142    request_XIWarpPointer(&client_request, &request, BadDevice);
143    request.deviceid = devices.mouse->id;
144    request_XIWarpPointer(&client_request, &request, BadDevice);
145    request.deviceid = devices.kbd->id;
146    request_XIWarpPointer(&client_request, &request, BadDevice);
147
148    devices.mouse->u.master = NULL; /* Float, kind-of */
149    request.deviceid = devices.mouse->id;
150    request_XIWarpPointer(&client_request, &request, Success);
151
152    for (i = devices.kbd->id + 1; i <= 0xFFFF; i++)
153    {
154        request.deviceid = i;
155        request_XIWarpPointer(&client_request, &request, BadDevice);
156    }
157
158    request.src_win = window.drawable.id;
159    request.deviceid = devices.vcp->id;
160    request_XIWarpPointer(&client_request, &request, Success);
161
162    request.deviceid = devices.mouse->id;
163    request_XIWarpPointer(&client_request, &request, Success);
164
165    request.src_win = root.drawable.id;
166    request.dst_win = 0xFFFF; /* invalid window */
167    request_XIWarpPointer(&client_request, &request, BadWindow);
168
169    request.src_win = 0xFFFF; /* invalid window */
170    request.dst_win = root.drawable.id;
171    request_XIWarpPointer(&client_request, &request, BadWindow);
172
173    request.src_win = None;
174    request.dst_win = None;
175
176    request.dst_y = 0;
177    expected_y = SPRITE_Y;
178
179    request.dst_x = 1 << 16;
180    expected_x = SPRITE_X + 1;
181    request.deviceid = devices.vcp->id;
182    request_XIWarpPointer(&client_request, &request, Success);
183
184    request.dst_x = -1 << 16;
185    expected_x = SPRITE_X - 1;
186    request.deviceid = devices.vcp->id;
187    request_XIWarpPointer(&client_request, &request, Success);
188
189    request.dst_x = 0;
190    expected_x = SPRITE_X;
191
192    request.dst_y = 1 << 16;
193    expected_y = SPRITE_Y + 1;
194    request.deviceid = devices.vcp->id;
195    request_XIWarpPointer(&client_request, &request, Success);
196
197    request.dst_y = -1 << 16;
198    expected_y = SPRITE_Y - 1;
199    request.deviceid = devices.vcp->id;
200    request_XIWarpPointer(&client_request, &request, Success);
201
202    /* FIXME: src_x/y checks */
203
204    client_request.req_len -= 2; /* invalid length */
205    request_XIWarpPointer(&client_request, &request, BadLength);
206}
207
208int main(int argc, char** argv)
209{
210    g_test_init(&argc, &argv,NULL);
211    g_test_bug_base("https://bugzilla.freedesktop.org/show_bug.cgi?id=");
212
213    init_simple();
214    screen.SetCursorPosition = ScreenSetCursorPosition;
215
216    g_test_add_func("/xi2/protocol/XIWarpPointer", test_XIWarpPointer);
217
218    return g_test_run();
219}
220