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 query 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 <X11/extensions/XI.h>
41*4882a593Smuzhiyun #include <X11/extensions/XI2proto.h>
42*4882a593Smuzhiyun #include "extnsionst.h"
43*4882a593Smuzhiyun #include "exevents.h"
44*4882a593Smuzhiyun #include "exglobals.h"
45*4882a593Smuzhiyun #include "eventconvert.h"
46*4882a593Smuzhiyun #include "scrnintstr.h"
47*4882a593Smuzhiyun #include "xkbsrv.h"
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun #ifdef PANORAMIX
50*4882a593Smuzhiyun #include "panoramiXsrv.h"
51*4882a593Smuzhiyun #endif
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun #include "inpututils.h"
54*4882a593Smuzhiyun #include "xiquerypointer.h"
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun /***********************************************************************
57*4882a593Smuzhiyun *
58*4882a593Smuzhiyun * This procedure allows a client to query the pointer of a device.
59*4882a593Smuzhiyun *
60*4882a593Smuzhiyun */
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun int _X_COLD
SProcXIQueryPointer(ClientPtr client)63*4882a593Smuzhiyun SProcXIQueryPointer(ClientPtr client)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun REQUEST(xXIQueryPointerReq);
66*4882a593Smuzhiyun REQUEST_SIZE_MATCH(xXIQueryPointerReq);
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun swaps(&stuff->length);
69*4882a593Smuzhiyun swaps(&stuff->deviceid);
70*4882a593Smuzhiyun swapl(&stuff->win);
71*4882a593Smuzhiyun return (ProcXIQueryPointer(client));
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun int
ProcXIQueryPointer(ClientPtr client)75*4882a593Smuzhiyun ProcXIQueryPointer(ClientPtr client)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun int rc;
78*4882a593Smuzhiyun xXIQueryPointerReply rep;
79*4882a593Smuzhiyun DeviceIntPtr pDev, kbd;
80*4882a593Smuzhiyun WindowPtr pWin, t;
81*4882a593Smuzhiyun SpritePtr pSprite;
82*4882a593Smuzhiyun XkbStatePtr state;
83*4882a593Smuzhiyun char *buttons = NULL;
84*4882a593Smuzhiyun int buttons_size = 0; /* size of buttons array */
85*4882a593Smuzhiyun XIClientPtr xi_client;
86*4882a593Smuzhiyun Bool have_xi22 = FALSE;
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun REQUEST(xXIQueryPointerReq);
89*4882a593Smuzhiyun REQUEST_SIZE_MATCH(xXIQueryPointerReq);
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun /* Check if client is compliant with XInput 2.2 or later. Earlier clients
92*4882a593Smuzhiyun * do not know about touches, so we must report emulated button presses. 2.2
93*4882a593Smuzhiyun * and later clients are aware of touches, so we don't include emulated
94*4882a593Smuzhiyun * button presses in the reply. */
95*4882a593Smuzhiyun xi_client = dixLookupPrivate(&client->devPrivates, XIClientPrivateKey);
96*4882a593Smuzhiyun if (version_compare(xi_client->major_version,
97*4882a593Smuzhiyun xi_client->minor_version, 2, 2) >= 0)
98*4882a593Smuzhiyun have_xi22 = TRUE;
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun rc = dixLookupDevice(&pDev, stuff->deviceid, client, DixReadAccess);
101*4882a593Smuzhiyun if (rc != Success) {
102*4882a593Smuzhiyun client->errorValue = stuff->deviceid;
103*4882a593Smuzhiyun return rc;
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun if (pDev->valuator == NULL || IsKeyboardDevice(pDev) || (!IsMaster(pDev) && !IsFloating(pDev))) { /* no attached devices */
107*4882a593Smuzhiyun client->errorValue = stuff->deviceid;
108*4882a593Smuzhiyun return BadDevice;
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun rc = dixLookupWindow(&pWin, stuff->win, client, DixGetAttrAccess);
112*4882a593Smuzhiyun if (rc != Success) {
113*4882a593Smuzhiyun client->errorValue = stuff->win;
114*4882a593Smuzhiyun return rc;
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun if (pDev->valuator->motionHintWindow)
118*4882a593Smuzhiyun MaybeStopHint(pDev, client);
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun if (IsMaster(pDev))
121*4882a593Smuzhiyun kbd = GetMaster(pDev, MASTER_KEYBOARD);
122*4882a593Smuzhiyun else
123*4882a593Smuzhiyun kbd = (pDev->key) ? pDev : NULL;
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun pSprite = pDev->spriteInfo->sprite;
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun rep = (xXIQueryPointerReply) {
128*4882a593Smuzhiyun .repType = X_Reply,
129*4882a593Smuzhiyun .RepType = X_XIQueryPointer,
130*4882a593Smuzhiyun .sequenceNumber = client->sequence,
131*4882a593Smuzhiyun .length = 6,
132*4882a593Smuzhiyun .root = (GetCurrentRootWindow(pDev))->drawable.id,
133*4882a593Smuzhiyun .root_x = double_to_fp1616(pSprite->hot.x),
134*4882a593Smuzhiyun .root_y = double_to_fp1616(pSprite->hot.y),
135*4882a593Smuzhiyun .child = None
136*4882a593Smuzhiyun };
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun if (kbd) {
139*4882a593Smuzhiyun state = &kbd->key->xkbInfo->state;
140*4882a593Smuzhiyun rep.mods.base_mods = state->base_mods;
141*4882a593Smuzhiyun rep.mods.latched_mods = state->latched_mods;
142*4882a593Smuzhiyun rep.mods.locked_mods = state->locked_mods;
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun rep.group.base_group = state->base_group;
145*4882a593Smuzhiyun rep.group.latched_group = state->latched_group;
146*4882a593Smuzhiyun rep.group.locked_group = state->locked_group;
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun if (pDev->button) {
150*4882a593Smuzhiyun int i;
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun rep.buttons_len =
153*4882a593Smuzhiyun bytes_to_int32(bits_to_bytes(pDev->button->numButtons));
154*4882a593Smuzhiyun rep.length += rep.buttons_len;
155*4882a593Smuzhiyun buttons = calloc(rep.buttons_len, 4);
156*4882a593Smuzhiyun if (!buttons)
157*4882a593Smuzhiyun return BadAlloc;
158*4882a593Smuzhiyun buttons_size = rep.buttons_len * 4;
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun for (i = 1; i < pDev->button->numButtons; i++)
161*4882a593Smuzhiyun if (BitIsOn(pDev->button->down, i))
162*4882a593Smuzhiyun SetBit(buttons, pDev->button->map[i]);
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun if (!have_xi22 && pDev->touch && pDev->touch->buttonsDown > 0)
165*4882a593Smuzhiyun SetBit(buttons, pDev->button->map[1]);
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun else
168*4882a593Smuzhiyun rep.buttons_len = 0;
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun if (pSprite->hot.pScreen == pWin->drawable.pScreen) {
171*4882a593Smuzhiyun rep.same_screen = xTrue;
172*4882a593Smuzhiyun rep.win_x = double_to_fp1616(pSprite->hot.x - pWin->drawable.x);
173*4882a593Smuzhiyun rep.win_y = double_to_fp1616(pSprite->hot.y - pWin->drawable.y);
174*4882a593Smuzhiyun for (t = pSprite->win; t; t = t->parent)
175*4882a593Smuzhiyun if (t->parent == pWin) {
176*4882a593Smuzhiyun rep.child = t->drawable.id;
177*4882a593Smuzhiyun break;
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun else {
181*4882a593Smuzhiyun rep.same_screen = xFalse;
182*4882a593Smuzhiyun rep.win_x = 0;
183*4882a593Smuzhiyun rep.win_y = 0;
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun #ifdef PANORAMIX
187*4882a593Smuzhiyun if (!noPanoramiXExtension) {
188*4882a593Smuzhiyun rep.root_x += double_to_fp1616(screenInfo.screens[0]->x);
189*4882a593Smuzhiyun rep.root_y += double_to_fp1616(screenInfo.screens[0]->y);
190*4882a593Smuzhiyun if (stuff->win == rep.root) {
191*4882a593Smuzhiyun rep.win_x += double_to_fp1616(screenInfo.screens[0]->x);
192*4882a593Smuzhiyun rep.win_y += double_to_fp1616(screenInfo.screens[0]->y);
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun #endif
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun WriteReplyToClient(client, sizeof(xXIQueryPointerReply), &rep);
198*4882a593Smuzhiyun if (buttons)
199*4882a593Smuzhiyun WriteToClient(client, buttons_size, buttons);
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun free(buttons);
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun return Success;
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun /***********************************************************************
207*4882a593Smuzhiyun *
208*4882a593Smuzhiyun * This procedure writes the reply for the XIQueryPointer function,
209*4882a593Smuzhiyun * if the client and server have a different byte ordering.
210*4882a593Smuzhiyun *
211*4882a593Smuzhiyun */
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun void
SRepXIQueryPointer(ClientPtr client,int size,xXIQueryPointerReply * rep)214*4882a593Smuzhiyun SRepXIQueryPointer(ClientPtr client, int size, xXIQueryPointerReply * rep)
215*4882a593Smuzhiyun {
216*4882a593Smuzhiyun swaps(&rep->sequenceNumber);
217*4882a593Smuzhiyun swapl(&rep->length);
218*4882a593Smuzhiyun swapl(&rep->root);
219*4882a593Smuzhiyun swapl(&rep->child);
220*4882a593Smuzhiyun swapl(&rep->root_x);
221*4882a593Smuzhiyun swapl(&rep->root_y);
222*4882a593Smuzhiyun swapl(&rep->win_x);
223*4882a593Smuzhiyun swapl(&rep->win_y);
224*4882a593Smuzhiyun swaps(&rep->buttons_len);
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun WriteToClient(client, size, rep);
227*4882a593Smuzhiyun }
228