1/*
2 * Copyright 2007-2008 Peter Hutterer
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 * Author: Peter Hutterer, University of South Australia, NICTA
24 */
25
26/***********************************************************************
27 *
28 * Request to Warp the pointer location of an extension input device.
29 *
30 */
31
32#ifdef HAVE_DIX_CONFIG_H
33#include <dix-config.h>
34#endif
35
36#include <X11/X.h>	/* for inputstr.h    */
37#include <X11/Xproto.h>	/* Request macro     */
38#include "inputstr.h"	/* DeviceIntPtr      */
39#include "windowstr.h"	/* window structure  */
40#include "scrnintstr.h"	/* screen structure  */
41#include <X11/extensions/XI.h>
42#include <X11/extensions/XI2proto.h>
43#include "extnsionst.h"
44#include "exevents.h"
45#include "exglobals.h"
46#include "mipointer.h" /* for miPointerUpdateSprite */
47
48
49#include "xiwarppointer.h"
50/***********************************************************************
51 *
52 * This procedure allows a client to warp the pointer of a device.
53 *
54 */
55
56int
57SProcXIWarpPointer(ClientPtr client)
58{
59    char n;
60
61    REQUEST(xXIWarpPointerReq);
62    REQUEST_SIZE_MATCH(xXIWarpPointerReq);
63
64    swaps(&stuff->length, n);
65    swapl(&stuff->src_win, n);
66    swapl(&stuff->dst_win, n);
67    swapl(&stuff->src_x, n);
68    swapl(&stuff->src_y, n);
69    swaps(&stuff->src_width, n);
70    swaps(&stuff->src_height, n);
71    swapl(&stuff->dst_x, n);
72    swapl(&stuff->dst_y, n);
73    swaps(&stuff->deviceid, n);
74    return (ProcXIWarpPointer(client));
75}
76
77int
78ProcXIWarpPointer(ClientPtr client)
79{
80    int rc;
81    int x, y;
82    WindowPtr dest = NULL;
83    DeviceIntPtr pDev;
84    SpritePtr pSprite;
85    ScreenPtr newScreen;
86    int src_x, src_y;
87    int dst_x, dst_y;
88
89    REQUEST(xXIWarpPointerReq);
90    REQUEST_SIZE_MATCH(xXIWarpPointerReq);
91
92    /* FIXME: panoramix stuff is missing, look at ProcWarpPointer */
93
94    rc = dixLookupDevice(&pDev, stuff->deviceid, client, DixWriteAccess);
95
96    if (rc != Success)
97    {
98        client->errorValue = stuff->deviceid;
99        return rc;
100    }
101
102    if ((!IsMaster(pDev) && pDev->u.master) ||
103        (IsMaster(pDev) && !IsPointerDevice(pDev)))
104    {
105        client->errorValue = stuff->deviceid;
106        return BadDevice;
107    }
108
109    if (stuff->dst_win != None)
110    {
111        rc = dixLookupWindow(&dest, stuff->dst_win, client, DixGetAttrAccess);
112        if (rc != Success)
113        {
114            client->errorValue = stuff->dst_win;
115            return rc;
116        }
117    }
118
119    pSprite = pDev->spriteInfo->sprite;
120    x = pSprite->hotPhys.x;
121    y = pSprite->hotPhys.y;
122
123    src_x = stuff->src_x / (double)(1 << 16);
124    src_y = stuff->src_y / (double)(1 << 16);
125    dst_x = stuff->dst_x / (double)(1 << 16);
126    dst_y = stuff->dst_y / (double)(1 << 16);
127
128    if (stuff->src_win != None)
129    {
130        int winX, winY;
131        WindowPtr src;
132
133        rc = dixLookupWindow(&src, stuff->src_win, client, DixGetAttrAccess);
134        if (rc != Success)
135        {
136            client->errorValue = stuff->src_win;
137            return rc;
138        }
139
140        winX = src->drawable.x;
141        winY = src->drawable.y;
142        if (src->drawable.pScreen != pSprite->hotPhys.pScreen ||
143                x < winX + src_x ||
144                y < winY + src_y ||
145                (stuff->src_width != 0 &&
146                 winX + src_x + (int)stuff->src_width < 0) ||
147                (stuff->src_height != 0 &&
148                 winY + src_y + (int)stuff->src_height < y) ||
149                !PointInWindowIsVisible(src, x, y))
150            return Success;
151    }
152
153    if (dest)
154    {
155        x = dest->drawable.x;
156        y = dest->drawable.y;
157        newScreen = dest->drawable.pScreen;
158    } else
159        newScreen = pSprite->hotPhys.pScreen;
160
161    x += dst_x;
162    y += dst_y;
163
164    if (x < 0)
165        x = 0;
166    else if (x > newScreen->width)
167        x = newScreen->width - 1;
168
169    if (y < 0)
170        y = 0;
171    else if (y > newScreen->height)
172        y = newScreen->height - 1;
173
174    if (newScreen == pSprite->hotPhys.pScreen)
175    {
176        if (x < pSprite->physLimits.x1)
177            x = pSprite->physLimits.x1;
178        else if (x >= pSprite->physLimits.x2)
179            x = pSprite->physLimits.x2 - 1;
180
181        if (y < pSprite->physLimits.y1)
182            y = pSprite->physLimits.y1;
183        else if (y >= pSprite->physLimits.y2)
184            y = pSprite->physLimits.y2 - 1;
185
186        if (pSprite->hotShape)
187            ConfineToShape(pDev, pSprite->hotShape, &x, &y);
188        (*newScreen->SetCursorPosition)(pDev, newScreen, x, y, TRUE);
189    } else if (!PointerConfinedToScreen(pDev))
190    {
191        NewCurrentScreen(pDev, newScreen, x, y);
192    }
193
194    /* if we don't update the device, we get a jump next time it moves */
195    pDev->last.valuators[0] = x;
196    pDev->last.valuators[1] = y;
197    pDev->last.remainder[0] = 0;
198    pDev->last.remainder[1] = 0;
199    miPointerUpdateSprite(pDev);
200
201    /* FIXME: XWarpPointer is supposed to generate an event. It doesn't do it
202       here though. */
203    return Success;
204}
205
206