xiwarppointer.c revision 6747b715
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    swaps(&stuff->length, n);
63    swapl(&stuff->src_win, n);
64    swapl(&stuff->dst_win, n);
65    swapl(&stuff->src_x, n);
66    swapl(&stuff->src_y, n);
67    swaps(&stuff->src_width, n);
68    swaps(&stuff->src_height, n);
69    swapl(&stuff->dst_x, n);
70    swapl(&stuff->dst_y, n);
71    swaps(&stuff->deviceid, n);
72    return (ProcXIWarpPointer(client));
73}
74
75int
76ProcXIWarpPointer(ClientPtr client)
77{
78    int rc;
79    int x, y;
80    WindowPtr dest = NULL;
81    DeviceIntPtr pDev;
82    SpritePtr pSprite;
83    ScreenPtr newScreen;
84    int src_x, src_y;
85    int dst_x, dst_y;
86
87    REQUEST(xXIWarpPointerReq);
88    REQUEST_SIZE_MATCH(xXIWarpPointerReq);
89
90    /* FIXME: panoramix stuff is missing, look at ProcWarpPointer */
91
92    rc = dixLookupDevice(&pDev, stuff->deviceid, client, DixWriteAccess);
93
94    if (rc != Success)
95    {
96        client->errorValue = stuff->deviceid;
97        return rc;
98    }
99
100    if ((!IsMaster(pDev) && pDev->u.master) ||
101        (IsMaster(pDev) && !IsPointerDevice(pDev)))
102    {
103        client->errorValue = stuff->deviceid;
104        return BadDevice;
105    }
106
107    if (stuff->dst_win != None)
108    {
109        rc = dixLookupWindow(&dest, stuff->dst_win, client, DixGetAttrAccess);
110        if (rc != Success)
111        {
112            client->errorValue = stuff->dst_win;
113            return rc;
114        }
115    }
116
117    pSprite = pDev->spriteInfo->sprite;
118    x = pSprite->hotPhys.x;
119    y = pSprite->hotPhys.y;
120
121    src_x = stuff->src_x / (double)(1 << 16);
122    src_y = stuff->src_y / (double)(1 << 16);
123    dst_x = stuff->dst_x / (double)(1 << 16);
124    dst_y = stuff->dst_y / (double)(1 << 16);
125
126    if (stuff->src_win != None)
127    {
128        int winX, winY;
129        WindowPtr src;
130
131        rc = dixLookupWindow(&src, stuff->src_win, client, DixGetAttrAccess);
132        if (rc != Success)
133        {
134            client->errorValue = stuff->src_win;
135            return rc;
136        }
137
138        winX = src->drawable.x;
139        winY = src->drawable.y;
140        if (src->drawable.pScreen != pSprite->hotPhys.pScreen ||
141                x < winX + src_x ||
142                y < winY + src_y ||
143                (stuff->src_width != 0 &&
144                 winX + src_x + (int)stuff->src_width < 0) ||
145                (stuff->src_height != 0 &&
146                 winY + src_y + (int)stuff->src_height < y) ||
147                !PointInWindowIsVisible(src, x, y))
148            return Success;
149    }
150
151    if (dest)
152    {
153        x = dest->drawable.x;
154        y = dest->drawable.y;
155        newScreen = dest->drawable.pScreen;
156    } else
157        newScreen = pSprite->hotPhys.pScreen;
158
159    x += dst_x;
160    y += dst_y;
161
162    if (x < 0)
163        x = 0;
164    else if (x > newScreen->width)
165        x = newScreen->width - 1;
166
167    if (y < 0)
168        y = 0;
169    else if (y > newScreen->height)
170        y = newScreen->height - 1;
171
172    if (newScreen == pSprite->hotPhys.pScreen)
173    {
174        if (x < pSprite->physLimits.x1)
175            x = pSprite->physLimits.x1;
176        else if (x >= pSprite->physLimits.x2)
177            x = pSprite->physLimits.x2 - 1;
178
179        if (y < pSprite->physLimits.y1)
180            y = pSprite->physLimits.y1;
181        else if (y >= pSprite->physLimits.y2)
182            y = pSprite->physLimits.y2 - 1;
183
184        if (pSprite->hotShape)
185            ConfineToShape(pDev, pSprite->hotShape, &x, &y);
186        (*newScreen->SetCursorPosition)(pDev, newScreen, x, y, TRUE);
187    } else if (!PointerConfinedToScreen(pDev))
188    {
189        NewCurrentScreen(pDev, newScreen, x, y);
190    }
191
192    /* if we don't update the device, we get a jump next time it moves */
193    pDev->last.valuators[0] = x;
194    pDev->last.valuators[1] = y;
195    miPointerUpdateSprite(pDev);
196
197    /* FIXME: XWarpPointer is supposed to generate an event. It doesn't do it
198       here though. */
199    return Success;
200}
201
202