xref: /OK3568_Linux_fs/external/xserver/Xi/xiwarppointer.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright 2007-2008 Peter Hutterer
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Permission is hereby granted, free of charge, to any person obtaining a
5*4882a593Smuzhiyun  * copy of this software and associated documentation files (the "Software"),
6*4882a593Smuzhiyun  * to deal in the Software without restriction, including without limitation
7*4882a593Smuzhiyun  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*4882a593Smuzhiyun  * and/or sell copies of the Software, and to permit persons to whom the
9*4882a593Smuzhiyun  * Software is furnished to do so, subject to the following conditions:
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * The above copyright notice and this permission notice (including the next
12*4882a593Smuzhiyun  * paragraph) shall be included in all copies or substantial portions of the
13*4882a593Smuzhiyun  * Software.
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*4882a593Smuzhiyun  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*4882a593Smuzhiyun  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18*4882a593Smuzhiyun  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*4882a593Smuzhiyun  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*4882a593Smuzhiyun  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21*4882a593Smuzhiyun  * DEALINGS IN THE SOFTWARE.
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * Author: Peter Hutterer, University of South Australia, NICTA
24*4882a593Smuzhiyun  */
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun /***********************************************************************
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  * Request to Warp the pointer location of an extension input device.
29*4882a593Smuzhiyun  *
30*4882a593Smuzhiyun  */
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun #ifdef HAVE_DIX_CONFIG_H
33*4882a593Smuzhiyun #include <dix-config.h>
34*4882a593Smuzhiyun #endif
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #include <X11/X.h>              /* for inputstr.h    */
37*4882a593Smuzhiyun #include <X11/Xproto.h>         /* Request macro     */
38*4882a593Smuzhiyun #include "inputstr.h"           /* DeviceIntPtr      */
39*4882a593Smuzhiyun #include "windowstr.h"          /* window structure  */
40*4882a593Smuzhiyun #include "scrnintstr.h"         /* screen structure  */
41*4882a593Smuzhiyun #include <X11/extensions/XI.h>
42*4882a593Smuzhiyun #include <X11/extensions/XI2proto.h>
43*4882a593Smuzhiyun #include "extnsionst.h"
44*4882a593Smuzhiyun #include "exevents.h"
45*4882a593Smuzhiyun #include "exglobals.h"
46*4882a593Smuzhiyun #include "mipointer.h"          /* for miPointerUpdateSprite */
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #include "xiwarppointer.h"
49*4882a593Smuzhiyun /***********************************************************************
50*4882a593Smuzhiyun  *
51*4882a593Smuzhiyun  * This procedure allows a client to warp the pointer of a device.
52*4882a593Smuzhiyun  *
53*4882a593Smuzhiyun  */
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun int _X_COLD
SProcXIWarpPointer(ClientPtr client)56*4882a593Smuzhiyun SProcXIWarpPointer(ClientPtr client)
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun     REQUEST(xXIWarpPointerReq);
59*4882a593Smuzhiyun     REQUEST_SIZE_MATCH(xXIWarpPointerReq);
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun     swaps(&stuff->length);
62*4882a593Smuzhiyun     swapl(&stuff->src_win);
63*4882a593Smuzhiyun     swapl(&stuff->dst_win);
64*4882a593Smuzhiyun     swapl(&stuff->src_x);
65*4882a593Smuzhiyun     swapl(&stuff->src_y);
66*4882a593Smuzhiyun     swaps(&stuff->src_width);
67*4882a593Smuzhiyun     swaps(&stuff->src_height);
68*4882a593Smuzhiyun     swapl(&stuff->dst_x);
69*4882a593Smuzhiyun     swapl(&stuff->dst_y);
70*4882a593Smuzhiyun     swaps(&stuff->deviceid);
71*4882a593Smuzhiyun     return (ProcXIWarpPointer(client));
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun int
ProcXIWarpPointer(ClientPtr client)75*4882a593Smuzhiyun ProcXIWarpPointer(ClientPtr client)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun     int rc;
78*4882a593Smuzhiyun     int x, y;
79*4882a593Smuzhiyun     WindowPtr dest = NULL;
80*4882a593Smuzhiyun     DeviceIntPtr pDev;
81*4882a593Smuzhiyun     SpritePtr pSprite;
82*4882a593Smuzhiyun     ScreenPtr newScreen;
83*4882a593Smuzhiyun     int src_x, src_y;
84*4882a593Smuzhiyun     int dst_x, dst_y;
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun     REQUEST(xXIWarpPointerReq);
87*4882a593Smuzhiyun     REQUEST_SIZE_MATCH(xXIWarpPointerReq);
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun     /* FIXME: panoramix stuff is missing, look at ProcWarpPointer */
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun     rc = dixLookupDevice(&pDev, stuff->deviceid, client, DixWriteAccess);
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun     if (rc != Success) {
94*4882a593Smuzhiyun         client->errorValue = stuff->deviceid;
95*4882a593Smuzhiyun         return rc;
96*4882a593Smuzhiyun     }
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun     if ((!IsMaster(pDev) && !IsFloating(pDev)) ||
99*4882a593Smuzhiyun         (IsMaster(pDev) && !IsPointerDevice(pDev))) {
100*4882a593Smuzhiyun         client->errorValue = stuff->deviceid;
101*4882a593Smuzhiyun         return BadDevice;
102*4882a593Smuzhiyun     }
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun     if (stuff->dst_win != None) {
105*4882a593Smuzhiyun         rc = dixLookupWindow(&dest, stuff->dst_win, client, DixGetAttrAccess);
106*4882a593Smuzhiyun         if (rc != Success) {
107*4882a593Smuzhiyun             client->errorValue = stuff->dst_win;
108*4882a593Smuzhiyun             return rc;
109*4882a593Smuzhiyun         }
110*4882a593Smuzhiyun     }
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun     pSprite = pDev->spriteInfo->sprite;
113*4882a593Smuzhiyun     x = pSprite->hotPhys.x;
114*4882a593Smuzhiyun     y = pSprite->hotPhys.y;
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun     src_x = stuff->src_x / (double) (1 << 16);
117*4882a593Smuzhiyun     src_y = stuff->src_y / (double) (1 << 16);
118*4882a593Smuzhiyun     dst_x = stuff->dst_x / (double) (1 << 16);
119*4882a593Smuzhiyun     dst_y = stuff->dst_y / (double) (1 << 16);
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun     if (stuff->src_win != None) {
122*4882a593Smuzhiyun         int winX, winY;
123*4882a593Smuzhiyun         WindowPtr src;
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun         rc = dixLookupWindow(&src, stuff->src_win, client, DixGetAttrAccess);
126*4882a593Smuzhiyun         if (rc != Success) {
127*4882a593Smuzhiyun             client->errorValue = stuff->src_win;
128*4882a593Smuzhiyun             return rc;
129*4882a593Smuzhiyun         }
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun         winX = src->drawable.x;
132*4882a593Smuzhiyun         winY = src->drawable.y;
133*4882a593Smuzhiyun         if (src->drawable.pScreen != pSprite->hotPhys.pScreen ||
134*4882a593Smuzhiyun             x < winX + src_x ||
135*4882a593Smuzhiyun             y < winY + src_y ||
136*4882a593Smuzhiyun             (stuff->src_width != 0 &&
137*4882a593Smuzhiyun              winX + src_x + (int) stuff->src_width < 0) ||
138*4882a593Smuzhiyun             (stuff->src_height != 0 &&
139*4882a593Smuzhiyun              winY + src_y + (int) stuff->src_height < y) ||
140*4882a593Smuzhiyun             !PointInWindowIsVisible(src, x, y))
141*4882a593Smuzhiyun             return Success;
142*4882a593Smuzhiyun     }
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun     if (dest) {
145*4882a593Smuzhiyun         x = dest->drawable.x;
146*4882a593Smuzhiyun         y = dest->drawable.y;
147*4882a593Smuzhiyun         newScreen = dest->drawable.pScreen;
148*4882a593Smuzhiyun     }
149*4882a593Smuzhiyun     else
150*4882a593Smuzhiyun         newScreen = pSprite->hotPhys.pScreen;
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun     x += dst_x;
153*4882a593Smuzhiyun     y += dst_y;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun     if (x < 0)
156*4882a593Smuzhiyun         x = 0;
157*4882a593Smuzhiyun     else if (x > newScreen->width)
158*4882a593Smuzhiyun         x = newScreen->width - 1;
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun     if (y < 0)
161*4882a593Smuzhiyun         y = 0;
162*4882a593Smuzhiyun     else if (y > newScreen->height)
163*4882a593Smuzhiyun         y = newScreen->height - 1;
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun     if (newScreen == pSprite->hotPhys.pScreen) {
166*4882a593Smuzhiyun         if (x < pSprite->physLimits.x1)
167*4882a593Smuzhiyun             x = pSprite->physLimits.x1;
168*4882a593Smuzhiyun         else if (x >= pSprite->physLimits.x2)
169*4882a593Smuzhiyun             x = pSprite->physLimits.x2 - 1;
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun         if (y < pSprite->physLimits.y1)
172*4882a593Smuzhiyun             y = pSprite->physLimits.y1;
173*4882a593Smuzhiyun         else if (y >= pSprite->physLimits.y2)
174*4882a593Smuzhiyun             y = pSprite->physLimits.y2 - 1;
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun         if (pSprite->hotShape)
177*4882a593Smuzhiyun             ConfineToShape(pDev, pSprite->hotShape, &x, &y);
178*4882a593Smuzhiyun         (*newScreen->SetCursorPosition) (pDev, newScreen, x, y, TRUE);
179*4882a593Smuzhiyun     }
180*4882a593Smuzhiyun     else if (!PointerConfinedToScreen(pDev)) {
181*4882a593Smuzhiyun         NewCurrentScreen(pDev, newScreen, x, y);
182*4882a593Smuzhiyun     }
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun     /* if we don't update the device, we get a jump next time it moves */
185*4882a593Smuzhiyun     pDev->last.valuators[0] = x;
186*4882a593Smuzhiyun     pDev->last.valuators[1] = y;
187*4882a593Smuzhiyun     miPointerUpdateSprite(pDev);
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun     if (*newScreen->CursorWarpedTo)
190*4882a593Smuzhiyun         (*newScreen->CursorWarpedTo) (pDev, newScreen, client,
191*4882a593Smuzhiyun                                       dest, pSprite, x, y);
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun     /* FIXME: XWarpPointer is supposed to generate an event. It doesn't do it
194*4882a593Smuzhiyun        here though. */
195*4882a593Smuzhiyun     return Success;
196*4882a593Smuzhiyun }
197