xref: /OK3568_Linux_fs/external/xserver/Xi/exevents.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /************************************************************
2*4882a593Smuzhiyun 
3*4882a593Smuzhiyun Copyright 1989, 1998  The Open Group
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun Permission to use, copy, modify, distribute, and sell this software and its
6*4882a593Smuzhiyun documentation for any purpose is hereby granted without fee, provided that
7*4882a593Smuzhiyun the above copyright notice appear in all copies and that both that
8*4882a593Smuzhiyun copyright notice and this permission notice appear in supporting
9*4882a593Smuzhiyun documentation.
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun The above copyright notice and this permission notice shall be included in
12*4882a593Smuzhiyun all copies or substantial portions of the Software.
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15*4882a593Smuzhiyun IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16*4882a593Smuzhiyun FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17*4882a593Smuzhiyun OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18*4882a593Smuzhiyun AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19*4882a593Smuzhiyun CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun Except as contained in this notice, the name of The Open Group shall not be
22*4882a593Smuzhiyun used in advertising or otherwise to promote the sale, use or other dealings
23*4882a593Smuzhiyun in this Software without prior written authorization from The Open Group.
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun Copyright 1989 by Hewlett-Packard Company, Palo Alto, California.
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun 			All Rights Reserved
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun Permission to use, copy, modify, and distribute this software and its
30*4882a593Smuzhiyun documentation for any purpose and without fee is hereby granted,
31*4882a593Smuzhiyun provided that the above copyright notice appear in all copies and that
32*4882a593Smuzhiyun both that copyright notice and this permission notice appear in
33*4882a593Smuzhiyun supporting documentation, and that the name of Hewlett-Packard not be
34*4882a593Smuzhiyun used in advertising or publicity pertaining to distribution of the
35*4882a593Smuzhiyun software without specific, written prior permission.
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun HEWLETT-PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
38*4882a593Smuzhiyun ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
39*4882a593Smuzhiyun HEWLETT-PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
40*4882a593Smuzhiyun ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
41*4882a593Smuzhiyun WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
42*4882a593Smuzhiyun ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
43*4882a593Smuzhiyun SOFTWARE.
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun ********************************************************/
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun /*
48*4882a593Smuzhiyun  * Copyright © 2010 Collabora Ltd.
49*4882a593Smuzhiyun  * Copyright © 2011 Red Hat, Inc.
50*4882a593Smuzhiyun  *
51*4882a593Smuzhiyun  * Permission is hereby granted, free of charge, to any person obtaining a
52*4882a593Smuzhiyun  * copy of this software and associated documentation files (the "Software"),
53*4882a593Smuzhiyun  * to deal in the Software without restriction, including without limitation
54*4882a593Smuzhiyun  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
55*4882a593Smuzhiyun  * and/or sell copies of the Software, and to permit persons to whom the
56*4882a593Smuzhiyun  * Software is furnished to do so, subject to the following conditions:
57*4882a593Smuzhiyun  *
58*4882a593Smuzhiyun  * The above copyright notice and this permission notice (including the next
59*4882a593Smuzhiyun  * paragraph) shall be included in all copies or substantial portions of the
60*4882a593Smuzhiyun  * Software.
61*4882a593Smuzhiyun  *
62*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
63*4882a593Smuzhiyun  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
64*4882a593Smuzhiyun  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
65*4882a593Smuzhiyun  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
66*4882a593Smuzhiyun  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
67*4882a593Smuzhiyun  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
68*4882a593Smuzhiyun  * DEALINGS IN THE SOFTWARE.
69*4882a593Smuzhiyun  *
70*4882a593Smuzhiyun  * Author: Daniel Stone <daniel@fooishbar.org>
71*4882a593Smuzhiyun  */
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun /********************************************************************
74*4882a593Smuzhiyun  *
75*4882a593Smuzhiyun  *  Routines to register and initialize extension input devices.
76*4882a593Smuzhiyun  *  This also contains ProcessOtherEvent, the routine called from DDX
77*4882a593Smuzhiyun  *  to route extension events.
78*4882a593Smuzhiyun  *
79*4882a593Smuzhiyun  */
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun #ifdef HAVE_DIX_CONFIG_H
82*4882a593Smuzhiyun #include <dix-config.h>
83*4882a593Smuzhiyun #endif
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun #include "inputstr.h"
86*4882a593Smuzhiyun #include <X11/X.h>
87*4882a593Smuzhiyun #include <X11/Xproto.h>
88*4882a593Smuzhiyun #include <X11/extensions/XI.h>
89*4882a593Smuzhiyun #include <X11/extensions/XIproto.h>
90*4882a593Smuzhiyun #include <X11/extensions/XI2proto.h>
91*4882a593Smuzhiyun #include <X11/extensions/geproto.h>
92*4882a593Smuzhiyun #include "windowstr.h"
93*4882a593Smuzhiyun #include "miscstruct.h"
94*4882a593Smuzhiyun #include "region.h"
95*4882a593Smuzhiyun #include "exevents.h"
96*4882a593Smuzhiyun #include "extnsionst.h"
97*4882a593Smuzhiyun #include "exglobals.h"
98*4882a593Smuzhiyun #include "dixevents.h"          /* DeliverFocusedEvent */
99*4882a593Smuzhiyun #include "dixgrabs.h"           /* CreateGrab() */
100*4882a593Smuzhiyun #include "scrnintstr.h"
101*4882a593Smuzhiyun #include "listdev.h"            /* for CopySwapXXXClass */
102*4882a593Smuzhiyun #include "xace.h"
103*4882a593Smuzhiyun #include "xiquerydevice.h"      /* For List*Info */
104*4882a593Smuzhiyun #include "eventconvert.h"
105*4882a593Smuzhiyun #include "eventstr.h"
106*4882a593Smuzhiyun #include "inpututils.h"
107*4882a593Smuzhiyun #include "mi.h"
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun #include <X11/extensions/XKBproto.h>
110*4882a593Smuzhiyun #include "xkbsrv.h"
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun #define WID(w) ((w) ? ((w)->drawable.id) : 0)
113*4882a593Smuzhiyun #define AllModifiersMask ( \
114*4882a593Smuzhiyun 	ShiftMask | LockMask | ControlMask | Mod1Mask | Mod2Mask | \
115*4882a593Smuzhiyun 	Mod3Mask | Mod4Mask | Mod5Mask )
116*4882a593Smuzhiyun #define AllButtonsMask ( \
117*4882a593Smuzhiyun 	Button1Mask | Button2Mask | Button3Mask | Button4Mask | Button5Mask )
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun Bool ShouldFreeInputMasks(WindowPtr /* pWin */ ,
120*4882a593Smuzhiyun                           Bool  /* ignoreSelectedEvents */
121*4882a593Smuzhiyun     );
122*4882a593Smuzhiyun static Bool MakeInputMasks(WindowPtr    /* pWin */
123*4882a593Smuzhiyun     );
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun /*
126*4882a593Smuzhiyun  * Only let the given client know of core events which will affect its
127*4882a593Smuzhiyun  * interpretation of input events, if the client's ClientPointer (or the
128*4882a593Smuzhiyun  * paired keyboard) is the current device.
129*4882a593Smuzhiyun  */
130*4882a593Smuzhiyun int
XIShouldNotify(ClientPtr client,DeviceIntPtr dev)131*4882a593Smuzhiyun XIShouldNotify(ClientPtr client, DeviceIntPtr dev)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun     DeviceIntPtr current_ptr = PickPointer(client);
134*4882a593Smuzhiyun     DeviceIntPtr current_kbd = GetMaster(current_ptr, KEYBOARD_OR_FLOAT);
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun     if (dev == current_kbd || dev == current_ptr)
137*4882a593Smuzhiyun         return 1;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun     return 0;
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun Bool
IsPointerEvent(InternalEvent * event)143*4882a593Smuzhiyun IsPointerEvent(InternalEvent *event)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun     switch (event->any.type) {
146*4882a593Smuzhiyun     case ET_ButtonPress:
147*4882a593Smuzhiyun     case ET_ButtonRelease:
148*4882a593Smuzhiyun     case ET_Motion:
149*4882a593Smuzhiyun         /* XXX: enter/leave ?? */
150*4882a593Smuzhiyun         return TRUE;
151*4882a593Smuzhiyun     default:
152*4882a593Smuzhiyun         break;
153*4882a593Smuzhiyun     }
154*4882a593Smuzhiyun     return FALSE;
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun Bool
IsTouchEvent(InternalEvent * event)158*4882a593Smuzhiyun IsTouchEvent(InternalEvent *event)
159*4882a593Smuzhiyun {
160*4882a593Smuzhiyun     switch (event->any.type) {
161*4882a593Smuzhiyun     case ET_TouchBegin:
162*4882a593Smuzhiyun     case ET_TouchUpdate:
163*4882a593Smuzhiyun     case ET_TouchEnd:
164*4882a593Smuzhiyun         return TRUE;
165*4882a593Smuzhiyun     default:
166*4882a593Smuzhiyun         break;
167*4882a593Smuzhiyun     }
168*4882a593Smuzhiyun     return FALSE;
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun /**
172*4882a593Smuzhiyun  * @return the device matching the deviceid of the device set in the event, or
173*4882a593Smuzhiyun  * NULL if the event is not an XInput event.
174*4882a593Smuzhiyun  */
175*4882a593Smuzhiyun DeviceIntPtr
XIGetDevice(xEvent * xE)176*4882a593Smuzhiyun XIGetDevice(xEvent *xE)
177*4882a593Smuzhiyun {
178*4882a593Smuzhiyun     DeviceIntPtr pDev = NULL;
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun     if (xE->u.u.type == DeviceButtonPress ||
181*4882a593Smuzhiyun         xE->u.u.type == DeviceButtonRelease ||
182*4882a593Smuzhiyun         xE->u.u.type == DeviceMotionNotify ||
183*4882a593Smuzhiyun         xE->u.u.type == ProximityIn ||
184*4882a593Smuzhiyun         xE->u.u.type == ProximityOut || xE->u.u.type == DevicePropertyNotify) {
185*4882a593Smuzhiyun         int rc;
186*4882a593Smuzhiyun         int id;
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun         id = ((deviceKeyButtonPointer *) xE)->deviceid & ~MORE_EVENTS;
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun         rc = dixLookupDevice(&pDev, id, serverClient, DixUnknownAccess);
191*4882a593Smuzhiyun         if (rc != Success)
192*4882a593Smuzhiyun             ErrorF("[dix] XIGetDevice failed on XACE restrictions (%d)\n", rc);
193*4882a593Smuzhiyun     }
194*4882a593Smuzhiyun     return pDev;
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun /**
198*4882a593Smuzhiyun  * Copy the device->key into master->key and send a mapping notify to the
199*4882a593Smuzhiyun  * clients if appropriate.
200*4882a593Smuzhiyun  * master->key needs to be allocated by the caller.
201*4882a593Smuzhiyun  *
202*4882a593Smuzhiyun  * Device is the slave device. If it is attached to a master device, we may
203*4882a593Smuzhiyun  * need to send a mapping notify to the client because it causes the MD
204*4882a593Smuzhiyun  * to change state.
205*4882a593Smuzhiyun  *
206*4882a593Smuzhiyun  * Mapping notify needs to be sent in the following cases:
207*4882a593Smuzhiyun  *      - different slave device on same master
208*4882a593Smuzhiyun  *      - different master
209*4882a593Smuzhiyun  *
210*4882a593Smuzhiyun  * XXX: They way how the code is we also send a map notify if the slave device
211*4882a593Smuzhiyun  * stays the same, but the master changes. This isn't really necessary though.
212*4882a593Smuzhiyun  *
213*4882a593Smuzhiyun  * XXX: this gives you funny behaviour with the ClientPointer. When a
214*4882a593Smuzhiyun  * MappingNotify is sent to the client, the client usually responds with a
215*4882a593Smuzhiyun  * GetKeyboardMapping. This will retrieve the ClientPointer's keyboard
216*4882a593Smuzhiyun  * mapping, regardless of which keyboard sent the last mapping notify request.
217*4882a593Smuzhiyun  * So depending on the CP setting, your keyboard may change layout in each
218*4882a593Smuzhiyun  * app...
219*4882a593Smuzhiyun  *
220*4882a593Smuzhiyun  * This code is basically the old SwitchCoreKeyboard.
221*4882a593Smuzhiyun  */
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun void
CopyKeyClass(DeviceIntPtr device,DeviceIntPtr master)224*4882a593Smuzhiyun CopyKeyClass(DeviceIntPtr device, DeviceIntPtr master)
225*4882a593Smuzhiyun {
226*4882a593Smuzhiyun     KeyClassPtr mk = master->key;
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun     if (device == master)
229*4882a593Smuzhiyun         return;
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun     mk->sourceid = device->id;
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun     if (!XkbDeviceApplyKeymap(master, device->key->xkbInfo->desc))
234*4882a593Smuzhiyun         FatalError("Couldn't pivot keymap from device to core!\n");
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun /**
238*4882a593Smuzhiyun  * Copies the feedback classes from device "from" into device "to". Classes
239*4882a593Smuzhiyun  * are duplicated (not just flipping the pointers). All feedback classes are
240*4882a593Smuzhiyun  * linked lists, the full list is duplicated.
241*4882a593Smuzhiyun  */
242*4882a593Smuzhiyun static void
DeepCopyFeedbackClasses(DeviceIntPtr from,DeviceIntPtr to)243*4882a593Smuzhiyun DeepCopyFeedbackClasses(DeviceIntPtr from, DeviceIntPtr to)
244*4882a593Smuzhiyun {
245*4882a593Smuzhiyun     ClassesPtr classes;
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun     if (from->intfeed) {
248*4882a593Smuzhiyun         IntegerFeedbackPtr *i, it;
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun         if (!to->intfeed) {
251*4882a593Smuzhiyun             classes = to->unused_classes;
252*4882a593Smuzhiyun             to->intfeed = classes->intfeed;
253*4882a593Smuzhiyun             classes->intfeed = NULL;
254*4882a593Smuzhiyun         }
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun         i = &to->intfeed;
257*4882a593Smuzhiyun         for (it = from->intfeed; it; it = it->next) {
258*4882a593Smuzhiyun             if (!(*i)) {
259*4882a593Smuzhiyun                 *i = calloc(1, sizeof(IntegerFeedbackClassRec));
260*4882a593Smuzhiyun                 if (!(*i)) {
261*4882a593Smuzhiyun                     ErrorF("[Xi] Cannot alloc memory for class copy.");
262*4882a593Smuzhiyun                     return;
263*4882a593Smuzhiyun                 }
264*4882a593Smuzhiyun             }
265*4882a593Smuzhiyun             (*i)->CtrlProc = it->CtrlProc;
266*4882a593Smuzhiyun             (*i)->ctrl = it->ctrl;
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun             i = &(*i)->next;
269*4882a593Smuzhiyun         }
270*4882a593Smuzhiyun     }
271*4882a593Smuzhiyun     else if (to->intfeed && !from->intfeed) {
272*4882a593Smuzhiyun         classes = to->unused_classes;
273*4882a593Smuzhiyun         classes->intfeed = to->intfeed;
274*4882a593Smuzhiyun         to->intfeed = NULL;
275*4882a593Smuzhiyun     }
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun     if (from->stringfeed) {
278*4882a593Smuzhiyun         StringFeedbackPtr *s, it;
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun         if (!to->stringfeed) {
281*4882a593Smuzhiyun             classes = to->unused_classes;
282*4882a593Smuzhiyun             to->stringfeed = classes->stringfeed;
283*4882a593Smuzhiyun             classes->stringfeed = NULL;
284*4882a593Smuzhiyun         }
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun         s = &to->stringfeed;
287*4882a593Smuzhiyun         for (it = from->stringfeed; it; it = it->next) {
288*4882a593Smuzhiyun             if (!(*s)) {
289*4882a593Smuzhiyun                 *s = calloc(1, sizeof(StringFeedbackClassRec));
290*4882a593Smuzhiyun                 if (!(*s)) {
291*4882a593Smuzhiyun                     ErrorF("[Xi] Cannot alloc memory for class copy.");
292*4882a593Smuzhiyun                     return;
293*4882a593Smuzhiyun                 }
294*4882a593Smuzhiyun             }
295*4882a593Smuzhiyun             (*s)->CtrlProc = it->CtrlProc;
296*4882a593Smuzhiyun             (*s)->ctrl = it->ctrl;
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun             s = &(*s)->next;
299*4882a593Smuzhiyun         }
300*4882a593Smuzhiyun     }
301*4882a593Smuzhiyun     else if (to->stringfeed && !from->stringfeed) {
302*4882a593Smuzhiyun         classes = to->unused_classes;
303*4882a593Smuzhiyun         classes->stringfeed = to->stringfeed;
304*4882a593Smuzhiyun         to->stringfeed = NULL;
305*4882a593Smuzhiyun     }
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun     if (from->bell) {
308*4882a593Smuzhiyun         BellFeedbackPtr *b, it;
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun         if (!to->bell) {
311*4882a593Smuzhiyun             classes = to->unused_classes;
312*4882a593Smuzhiyun             to->bell = classes->bell;
313*4882a593Smuzhiyun             classes->bell = NULL;
314*4882a593Smuzhiyun         }
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun         b = &to->bell;
317*4882a593Smuzhiyun         for (it = from->bell; it; it = it->next) {
318*4882a593Smuzhiyun             if (!(*b)) {
319*4882a593Smuzhiyun                 *b = calloc(1, sizeof(BellFeedbackClassRec));
320*4882a593Smuzhiyun                 if (!(*b)) {
321*4882a593Smuzhiyun                     ErrorF("[Xi] Cannot alloc memory for class copy.");
322*4882a593Smuzhiyun                     return;
323*4882a593Smuzhiyun                 }
324*4882a593Smuzhiyun             }
325*4882a593Smuzhiyun             (*b)->BellProc = it->BellProc;
326*4882a593Smuzhiyun             (*b)->CtrlProc = it->CtrlProc;
327*4882a593Smuzhiyun             (*b)->ctrl = it->ctrl;
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun             b = &(*b)->next;
330*4882a593Smuzhiyun         }
331*4882a593Smuzhiyun     }
332*4882a593Smuzhiyun     else if (to->bell && !from->bell) {
333*4882a593Smuzhiyun         classes = to->unused_classes;
334*4882a593Smuzhiyun         classes->bell = to->bell;
335*4882a593Smuzhiyun         to->bell = NULL;
336*4882a593Smuzhiyun     }
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun     if (from->leds) {
339*4882a593Smuzhiyun         LedFeedbackPtr *l, it;
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun         if (!to->leds) {
342*4882a593Smuzhiyun             classes = to->unused_classes;
343*4882a593Smuzhiyun             to->leds = classes->leds;
344*4882a593Smuzhiyun             classes->leds = NULL;
345*4882a593Smuzhiyun         }
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun         l = &to->leds;
348*4882a593Smuzhiyun         for (it = from->leds; it; it = it->next) {
349*4882a593Smuzhiyun             if (!(*l)) {
350*4882a593Smuzhiyun                 *l = calloc(1, sizeof(LedFeedbackClassRec));
351*4882a593Smuzhiyun                 if (!(*l)) {
352*4882a593Smuzhiyun                     ErrorF("[Xi] Cannot alloc memory for class copy.");
353*4882a593Smuzhiyun                     return;
354*4882a593Smuzhiyun                 }
355*4882a593Smuzhiyun             }
356*4882a593Smuzhiyun             (*l)->CtrlProc = it->CtrlProc;
357*4882a593Smuzhiyun             (*l)->ctrl = it->ctrl;
358*4882a593Smuzhiyun             if ((*l)->xkb_sli)
359*4882a593Smuzhiyun                 XkbFreeSrvLedInfo((*l)->xkb_sli);
360*4882a593Smuzhiyun             (*l)->xkb_sli = XkbCopySrvLedInfo(from, it->xkb_sli, NULL, *l);
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun             l = &(*l)->next;
363*4882a593Smuzhiyun         }
364*4882a593Smuzhiyun     }
365*4882a593Smuzhiyun     else if (to->leds && !from->leds) {
366*4882a593Smuzhiyun         classes = to->unused_classes;
367*4882a593Smuzhiyun         classes->leds = to->leds;
368*4882a593Smuzhiyun         to->leds = NULL;
369*4882a593Smuzhiyun     }
370*4882a593Smuzhiyun }
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun static void
DeepCopyKeyboardClasses(DeviceIntPtr from,DeviceIntPtr to)373*4882a593Smuzhiyun DeepCopyKeyboardClasses(DeviceIntPtr from, DeviceIntPtr to)
374*4882a593Smuzhiyun {
375*4882a593Smuzhiyun     ClassesPtr classes;
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun     /* XkbInitDevice (->XkbInitIndicatorMap->XkbFindSrvLedInfo) relies on the
378*4882a593Smuzhiyun      * kbdfeed to be set up properly, so let's do the feedback classes first.
379*4882a593Smuzhiyun      */
380*4882a593Smuzhiyun     if (from->kbdfeed) {
381*4882a593Smuzhiyun         KbdFeedbackPtr *k, it;
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun         if (!to->kbdfeed) {
384*4882a593Smuzhiyun             classes = to->unused_classes;
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun             to->kbdfeed = classes->kbdfeed;
387*4882a593Smuzhiyun             if (!to->kbdfeed)
388*4882a593Smuzhiyun                 InitKeyboardDeviceStruct(to, NULL, NULL, NULL);
389*4882a593Smuzhiyun             classes->kbdfeed = NULL;
390*4882a593Smuzhiyun         }
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun         k = &to->kbdfeed;
393*4882a593Smuzhiyun         for (it = from->kbdfeed; it; it = it->next) {
394*4882a593Smuzhiyun             if (!(*k)) {
395*4882a593Smuzhiyun                 *k = calloc(1, sizeof(KbdFeedbackClassRec));
396*4882a593Smuzhiyun                 if (!*k) {
397*4882a593Smuzhiyun                     ErrorF("[Xi] Cannot alloc memory for class copy.");
398*4882a593Smuzhiyun                     return;
399*4882a593Smuzhiyun                 }
400*4882a593Smuzhiyun             }
401*4882a593Smuzhiyun             (*k)->BellProc = it->BellProc;
402*4882a593Smuzhiyun             (*k)->CtrlProc = it->CtrlProc;
403*4882a593Smuzhiyun             (*k)->ctrl = it->ctrl;
404*4882a593Smuzhiyun             if ((*k)->xkb_sli)
405*4882a593Smuzhiyun                 XkbFreeSrvLedInfo((*k)->xkb_sli);
406*4882a593Smuzhiyun             (*k)->xkb_sli = XkbCopySrvLedInfo(from, it->xkb_sli, *k, NULL);
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun             k = &(*k)->next;
409*4882a593Smuzhiyun         }
410*4882a593Smuzhiyun     }
411*4882a593Smuzhiyun     else if (to->kbdfeed && !from->kbdfeed) {
412*4882a593Smuzhiyun         classes = to->unused_classes;
413*4882a593Smuzhiyun         classes->kbdfeed = to->kbdfeed;
414*4882a593Smuzhiyun         to->kbdfeed = NULL;
415*4882a593Smuzhiyun     }
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun     if (from->key) {
418*4882a593Smuzhiyun         if (!to->key) {
419*4882a593Smuzhiyun             classes = to->unused_classes;
420*4882a593Smuzhiyun             to->key = classes->key;
421*4882a593Smuzhiyun             if (!to->key)
422*4882a593Smuzhiyun                 InitKeyboardDeviceStruct(to, NULL, NULL, NULL);
423*4882a593Smuzhiyun             else
424*4882a593Smuzhiyun                 classes->key = NULL;
425*4882a593Smuzhiyun         }
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun         CopyKeyClass(from, to);
428*4882a593Smuzhiyun     }
429*4882a593Smuzhiyun     else if (to->key && !from->key) {
430*4882a593Smuzhiyun         classes = to->unused_classes;
431*4882a593Smuzhiyun         classes->key = to->key;
432*4882a593Smuzhiyun         to->key = NULL;
433*4882a593Smuzhiyun     }
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun     /* If a SrvLedInfoPtr's flags are XkbSLI_IsDefault, the names and maps
436*4882a593Smuzhiyun      * pointer point into the xkbInfo->desc struct.  XkbCopySrvLedInfo
437*4882a593Smuzhiyun      * didn't update the pointers so we need to do it manually here.
438*4882a593Smuzhiyun      */
439*4882a593Smuzhiyun     if (to->kbdfeed) {
440*4882a593Smuzhiyun         KbdFeedbackPtr k;
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun         for (k = to->kbdfeed; k; k = k->next) {
443*4882a593Smuzhiyun             if (!k->xkb_sli)
444*4882a593Smuzhiyun                 continue;
445*4882a593Smuzhiyun             if (k->xkb_sli->flags & XkbSLI_IsDefault) {
446*4882a593Smuzhiyun                 k->xkb_sli->names = to->key->xkbInfo->desc->names->indicators;
447*4882a593Smuzhiyun                 k->xkb_sli->maps = to->key->xkbInfo->desc->indicators->maps;
448*4882a593Smuzhiyun             }
449*4882a593Smuzhiyun         }
450*4882a593Smuzhiyun     }
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun     /* We can't just copy over the focus class. When an app sets the focus,
453*4882a593Smuzhiyun      * it'll do so on the master device. Copying the SDs focus means losing
454*4882a593Smuzhiyun      * the focus.
455*4882a593Smuzhiyun      * So we only copy the focus class if the device didn't have one,
456*4882a593Smuzhiyun      * otherwise we leave it as it is.
457*4882a593Smuzhiyun      */
458*4882a593Smuzhiyun     if (from->focus) {
459*4882a593Smuzhiyun         if (!to->focus) {
460*4882a593Smuzhiyun             WindowPtr *oldTrace;
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun             classes = to->unused_classes;
463*4882a593Smuzhiyun             to->focus = classes->focus;
464*4882a593Smuzhiyun             if (!to->focus) {
465*4882a593Smuzhiyun                 to->focus = calloc(1, sizeof(FocusClassRec));
466*4882a593Smuzhiyun                 if (!to->focus)
467*4882a593Smuzhiyun                     FatalError("[Xi] no memory for class shift.\n");
468*4882a593Smuzhiyun             }
469*4882a593Smuzhiyun             else
470*4882a593Smuzhiyun                 classes->focus = NULL;
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun             oldTrace = to->focus->trace;
473*4882a593Smuzhiyun             memcpy(to->focus, from->focus, sizeof(FocusClassRec));
474*4882a593Smuzhiyun             to->focus->trace = reallocarray(oldTrace,
475*4882a593Smuzhiyun                                             to->focus->traceSize,
476*4882a593Smuzhiyun                                             sizeof(WindowPtr));
477*4882a593Smuzhiyun             if (!to->focus->trace && to->focus->traceSize)
478*4882a593Smuzhiyun                 FatalError("[Xi] no memory for trace.\n");
479*4882a593Smuzhiyun             memcpy(to->focus->trace, from->focus->trace,
480*4882a593Smuzhiyun                    from->focus->traceSize * sizeof(WindowPtr));
481*4882a593Smuzhiyun             to->focus->sourceid = from->id;
482*4882a593Smuzhiyun         }
483*4882a593Smuzhiyun     }
484*4882a593Smuzhiyun     else if (to->focus) {
485*4882a593Smuzhiyun         classes = to->unused_classes;
486*4882a593Smuzhiyun         classes->focus = to->focus;
487*4882a593Smuzhiyun         to->focus = NULL;
488*4882a593Smuzhiyun     }
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun /* FIXME: this should really be shared with the InitValuatorAxisClassRec and
493*4882a593Smuzhiyun  * similar */
494*4882a593Smuzhiyun static void
DeepCopyPointerClasses(DeviceIntPtr from,DeviceIntPtr to)495*4882a593Smuzhiyun DeepCopyPointerClasses(DeviceIntPtr from, DeviceIntPtr to)
496*4882a593Smuzhiyun {
497*4882a593Smuzhiyun     ClassesPtr classes;
498*4882a593Smuzhiyun 
499*4882a593Smuzhiyun     /* Feedback classes must be copied first */
500*4882a593Smuzhiyun     if (from->ptrfeed) {
501*4882a593Smuzhiyun         PtrFeedbackPtr *p, it;
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun         if (!to->ptrfeed) {
504*4882a593Smuzhiyun             classes = to->unused_classes;
505*4882a593Smuzhiyun             to->ptrfeed = classes->ptrfeed;
506*4882a593Smuzhiyun             classes->ptrfeed = NULL;
507*4882a593Smuzhiyun         }
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun         p = &to->ptrfeed;
510*4882a593Smuzhiyun         for (it = from->ptrfeed; it; it = it->next) {
511*4882a593Smuzhiyun             if (!(*p)) {
512*4882a593Smuzhiyun                 *p = calloc(1, sizeof(PtrFeedbackClassRec));
513*4882a593Smuzhiyun                 if (!*p) {
514*4882a593Smuzhiyun                     ErrorF("[Xi] Cannot alloc memory for class copy.");
515*4882a593Smuzhiyun                     return;
516*4882a593Smuzhiyun                 }
517*4882a593Smuzhiyun             }
518*4882a593Smuzhiyun             (*p)->CtrlProc = it->CtrlProc;
519*4882a593Smuzhiyun             (*p)->ctrl = it->ctrl;
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun             p = &(*p)->next;
522*4882a593Smuzhiyun         }
523*4882a593Smuzhiyun     }
524*4882a593Smuzhiyun     else if (to->ptrfeed && !from->ptrfeed) {
525*4882a593Smuzhiyun         classes = to->unused_classes;
526*4882a593Smuzhiyun         classes->ptrfeed = to->ptrfeed;
527*4882a593Smuzhiyun         to->ptrfeed = NULL;
528*4882a593Smuzhiyun     }
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun     if (from->valuator) {
531*4882a593Smuzhiyun         ValuatorClassPtr v;
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun         if (!to->valuator) {
534*4882a593Smuzhiyun             classes = to->unused_classes;
535*4882a593Smuzhiyun             to->valuator = classes->valuator;
536*4882a593Smuzhiyun             if (to->valuator)
537*4882a593Smuzhiyun                 classes->valuator = NULL;
538*4882a593Smuzhiyun         }
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun         v = AllocValuatorClass(to->valuator, from->valuator->numAxes);
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun         if (!v)
543*4882a593Smuzhiyun             FatalError("[Xi] no memory for class shift.\n");
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun         to->valuator = v;
546*4882a593Smuzhiyun         memcpy(v->axes, from->valuator->axes, v->numAxes * sizeof(AxisInfo));
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun         v->sourceid = from->id;
549*4882a593Smuzhiyun     }
550*4882a593Smuzhiyun     else if (to->valuator && !from->valuator) {
551*4882a593Smuzhiyun         classes = to->unused_classes;
552*4882a593Smuzhiyun         classes->valuator = to->valuator;
553*4882a593Smuzhiyun         to->valuator = NULL;
554*4882a593Smuzhiyun     }
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun     if (from->button) {
557*4882a593Smuzhiyun         if (!to->button) {
558*4882a593Smuzhiyun             classes = to->unused_classes;
559*4882a593Smuzhiyun             to->button = classes->button;
560*4882a593Smuzhiyun             if (!to->button) {
561*4882a593Smuzhiyun                 to->button = calloc(1, sizeof(ButtonClassRec));
562*4882a593Smuzhiyun                 if (!to->button)
563*4882a593Smuzhiyun                     FatalError("[Xi] no memory for class shift.\n");
564*4882a593Smuzhiyun             }
565*4882a593Smuzhiyun             else
566*4882a593Smuzhiyun                 classes->button = NULL;
567*4882a593Smuzhiyun         }
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun         if (from->button->xkb_acts) {
570*4882a593Smuzhiyun             if (!to->button->xkb_acts) {
571*4882a593Smuzhiyun                 to->button->xkb_acts = calloc(1, sizeof(XkbAction));
572*4882a593Smuzhiyun                 if (!to->button->xkb_acts)
573*4882a593Smuzhiyun                     FatalError("[Xi] not enough memory for xkb_acts.\n");
574*4882a593Smuzhiyun             }
575*4882a593Smuzhiyun             memcpy(to->button->xkb_acts, from->button->xkb_acts,
576*4882a593Smuzhiyun                    sizeof(XkbAction));
577*4882a593Smuzhiyun         }
578*4882a593Smuzhiyun         else
579*4882a593Smuzhiyun             free(to->button->xkb_acts);
580*4882a593Smuzhiyun 
581*4882a593Smuzhiyun         memcpy(to->button->labels, from->button->labels,
582*4882a593Smuzhiyun                from->button->numButtons * sizeof(Atom));
583*4882a593Smuzhiyun         to->button->sourceid = from->id;
584*4882a593Smuzhiyun     }
585*4882a593Smuzhiyun     else if (to->button && !from->button) {
586*4882a593Smuzhiyun         classes = to->unused_classes;
587*4882a593Smuzhiyun         classes->button = to->button;
588*4882a593Smuzhiyun         to->button = NULL;
589*4882a593Smuzhiyun     }
590*4882a593Smuzhiyun 
591*4882a593Smuzhiyun     if (from->proximity) {
592*4882a593Smuzhiyun         if (!to->proximity) {
593*4882a593Smuzhiyun             classes = to->unused_classes;
594*4882a593Smuzhiyun             to->proximity = classes->proximity;
595*4882a593Smuzhiyun             if (!to->proximity) {
596*4882a593Smuzhiyun                 to->proximity = calloc(1, sizeof(ProximityClassRec));
597*4882a593Smuzhiyun                 if (!to->proximity)
598*4882a593Smuzhiyun                     FatalError("[Xi] no memory for class shift.\n");
599*4882a593Smuzhiyun             }
600*4882a593Smuzhiyun             else
601*4882a593Smuzhiyun                 classes->proximity = NULL;
602*4882a593Smuzhiyun         }
603*4882a593Smuzhiyun         memcpy(to->proximity, from->proximity, sizeof(ProximityClassRec));
604*4882a593Smuzhiyun         to->proximity->sourceid = from->id;
605*4882a593Smuzhiyun     }
606*4882a593Smuzhiyun     else if (to->proximity) {
607*4882a593Smuzhiyun         classes = to->unused_classes;
608*4882a593Smuzhiyun         classes->proximity = to->proximity;
609*4882a593Smuzhiyun         to->proximity = NULL;
610*4882a593Smuzhiyun     }
611*4882a593Smuzhiyun 
612*4882a593Smuzhiyun     if (from->touch) {
613*4882a593Smuzhiyun         TouchClassPtr t, f;
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun         if (!to->touch) {
616*4882a593Smuzhiyun             classes = to->unused_classes;
617*4882a593Smuzhiyun             to->touch = classes->touch;
618*4882a593Smuzhiyun             if (!to->touch) {
619*4882a593Smuzhiyun                 int i;
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun                 to->touch = calloc(1, sizeof(TouchClassRec));
622*4882a593Smuzhiyun                 if (!to->touch)
623*4882a593Smuzhiyun                     FatalError("[Xi] no memory for class shift.\n");
624*4882a593Smuzhiyun                 to->touch->num_touches = from->touch->num_touches;
625*4882a593Smuzhiyun                 to->touch->touches = calloc(to->touch->num_touches,
626*4882a593Smuzhiyun                                             sizeof(TouchPointInfoRec));
627*4882a593Smuzhiyun                 for (i = 0; i < to->touch->num_touches; i++)
628*4882a593Smuzhiyun                     TouchInitTouchPoint(to->touch, to->valuator, i);
629*4882a593Smuzhiyun                 if (!to->touch)
630*4882a593Smuzhiyun                     FatalError("[Xi] no memory for class shift.\n");
631*4882a593Smuzhiyun             }
632*4882a593Smuzhiyun             else
633*4882a593Smuzhiyun                 classes->touch = NULL;
634*4882a593Smuzhiyun         }
635*4882a593Smuzhiyun 
636*4882a593Smuzhiyun         t = to->touch;
637*4882a593Smuzhiyun         f = from->touch;
638*4882a593Smuzhiyun         t->sourceid = f->sourceid;
639*4882a593Smuzhiyun         t->max_touches = f->max_touches;
640*4882a593Smuzhiyun         t->mode = f->mode;
641*4882a593Smuzhiyun         t->buttonsDown = f->buttonsDown;
642*4882a593Smuzhiyun         t->state = f->state;
643*4882a593Smuzhiyun         t->motionMask = f->motionMask;
644*4882a593Smuzhiyun         /* to->touches and to->num_touches are separate on the master,
645*4882a593Smuzhiyun          * don't copy */
646*4882a593Smuzhiyun     }
647*4882a593Smuzhiyun     /* Don't remove touch class if from->touch is non-existent. The to device
648*4882a593Smuzhiyun      * may have an active touch grab, so we need to keep the touch class record
649*4882a593Smuzhiyun      * around. */
650*4882a593Smuzhiyun }
651*4882a593Smuzhiyun 
652*4882a593Smuzhiyun /**
653*4882a593Smuzhiyun  * Copies the CONTENT of the classes of device from into the classes in device
654*4882a593Smuzhiyun  * to. From and to are identical after finishing.
655*4882a593Smuzhiyun  *
656*4882a593Smuzhiyun  * If to does not have classes from currenly has, the classes are stored in
657*4882a593Smuzhiyun  * to's devPrivates system. Later, we recover it again from there if needed.
658*4882a593Smuzhiyun  * Saves a few memory allocations.
659*4882a593Smuzhiyun  */
660*4882a593Smuzhiyun void
DeepCopyDeviceClasses(DeviceIntPtr from,DeviceIntPtr to,DeviceChangedEvent * dce)661*4882a593Smuzhiyun DeepCopyDeviceClasses(DeviceIntPtr from, DeviceIntPtr to,
662*4882a593Smuzhiyun                       DeviceChangedEvent *dce)
663*4882a593Smuzhiyun {
664*4882a593Smuzhiyun     input_lock();
665*4882a593Smuzhiyun 
666*4882a593Smuzhiyun     /* generic feedback classes, not tied to pointer and/or keyboard */
667*4882a593Smuzhiyun     DeepCopyFeedbackClasses(from, to);
668*4882a593Smuzhiyun 
669*4882a593Smuzhiyun     if ((dce->flags & DEVCHANGE_KEYBOARD_EVENT))
670*4882a593Smuzhiyun         DeepCopyKeyboardClasses(from, to);
671*4882a593Smuzhiyun     if ((dce->flags & DEVCHANGE_POINTER_EVENT))
672*4882a593Smuzhiyun         DeepCopyPointerClasses(from, to);
673*4882a593Smuzhiyun 
674*4882a593Smuzhiyun     input_unlock();
675*4882a593Smuzhiyun }
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun /**
678*4882a593Smuzhiyun  * Send an XI2 DeviceChangedEvent to all interested clients.
679*4882a593Smuzhiyun  */
680*4882a593Smuzhiyun void
XISendDeviceChangedEvent(DeviceIntPtr device,DeviceChangedEvent * dce)681*4882a593Smuzhiyun XISendDeviceChangedEvent(DeviceIntPtr device, DeviceChangedEvent *dce)
682*4882a593Smuzhiyun {
683*4882a593Smuzhiyun     xXIDeviceChangedEvent *dcce;
684*4882a593Smuzhiyun     int rc;
685*4882a593Smuzhiyun 
686*4882a593Smuzhiyun     rc = EventToXI2((InternalEvent *) dce, (xEvent **) &dcce);
687*4882a593Smuzhiyun     if (rc != Success) {
688*4882a593Smuzhiyun         ErrorF("[Xi] event conversion from DCE failed with code %d\n", rc);
689*4882a593Smuzhiyun         return;
690*4882a593Smuzhiyun     }
691*4882a593Smuzhiyun 
692*4882a593Smuzhiyun     /* we don't actually swap if there's a NullClient, swapping is done
693*4882a593Smuzhiyun      * later when event is delivered. */
694*4882a593Smuzhiyun     SendEventToAllWindows(device, XI_DeviceChangedMask, (xEvent *) dcce, 1);
695*4882a593Smuzhiyun     free(dcce);
696*4882a593Smuzhiyun }
697*4882a593Smuzhiyun 
698*4882a593Smuzhiyun static void
ChangeMasterDeviceClasses(DeviceIntPtr device,DeviceChangedEvent * dce)699*4882a593Smuzhiyun ChangeMasterDeviceClasses(DeviceIntPtr device, DeviceChangedEvent *dce)
700*4882a593Smuzhiyun {
701*4882a593Smuzhiyun     DeviceIntPtr slave;
702*4882a593Smuzhiyun     int rc;
703*4882a593Smuzhiyun 
704*4882a593Smuzhiyun     /* For now, we don't have devices that change physically. */
705*4882a593Smuzhiyun     if (!IsMaster(device))
706*4882a593Smuzhiyun         return;
707*4882a593Smuzhiyun 
708*4882a593Smuzhiyun     rc = dixLookupDevice(&slave, dce->sourceid, serverClient, DixReadAccess);
709*4882a593Smuzhiyun 
710*4882a593Smuzhiyun     if (rc != Success)
711*4882a593Smuzhiyun         return;                 /* Device has disappeared */
712*4882a593Smuzhiyun 
713*4882a593Smuzhiyun     if (IsMaster(slave))
714*4882a593Smuzhiyun         return;
715*4882a593Smuzhiyun 
716*4882a593Smuzhiyun     if (IsFloating(slave))
717*4882a593Smuzhiyun         return;                 /* set floating since the event */
718*4882a593Smuzhiyun 
719*4882a593Smuzhiyun     if (GetMaster(slave, MASTER_ATTACHED)->id != dce->masterid)
720*4882a593Smuzhiyun         return;                 /* not our slave anymore, don't care */
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun     /* FIXME: we probably need to send a DCE for the new slave now */
723*4882a593Smuzhiyun 
724*4882a593Smuzhiyun     device->public.devicePrivate = slave->public.devicePrivate;
725*4882a593Smuzhiyun 
726*4882a593Smuzhiyun     /* FIXME: the classes may have changed since we generated the event. */
727*4882a593Smuzhiyun     DeepCopyDeviceClasses(slave, device, dce);
728*4882a593Smuzhiyun     dce->deviceid = device->id;
729*4882a593Smuzhiyun     XISendDeviceChangedEvent(device, dce);
730*4882a593Smuzhiyun }
731*4882a593Smuzhiyun 
732*4882a593Smuzhiyun /**
733*4882a593Smuzhiyun  * Add state and motionMask to the filter for this event. The protocol
734*4882a593Smuzhiyun  * supports some extra masks for motion when a button is down:
735*4882a593Smuzhiyun  * ButtonXMotionMask and the DeviceButtonMotionMask to trigger only when at
736*4882a593Smuzhiyun  * least one button (or that specific button is down). These masks need to
737*4882a593Smuzhiyun  * be added to the filters for core/XI motion events.
738*4882a593Smuzhiyun  *
739*4882a593Smuzhiyun  * @param device The device to update the mask for
740*4882a593Smuzhiyun  * @param state The current button state mask
741*4882a593Smuzhiyun  * @param motion_mask The motion mask (DeviceButtonMotionMask or 0)
742*4882a593Smuzhiyun  */
743*4882a593Smuzhiyun static void
UpdateDeviceMotionMask(DeviceIntPtr device,unsigned short state,Mask motion_mask)744*4882a593Smuzhiyun UpdateDeviceMotionMask(DeviceIntPtr device, unsigned short state,
745*4882a593Smuzhiyun                        Mask motion_mask)
746*4882a593Smuzhiyun {
747*4882a593Smuzhiyun     Mask mask;
748*4882a593Smuzhiyun 
749*4882a593Smuzhiyun     mask = DevicePointerMotionMask | state | motion_mask;
750*4882a593Smuzhiyun     SetMaskForEvent(device->id, mask, DeviceMotionNotify);
751*4882a593Smuzhiyun     mask = PointerMotionMask | state | motion_mask;
752*4882a593Smuzhiyun     SetMaskForEvent(device->id, mask, MotionNotify);
753*4882a593Smuzhiyun }
754*4882a593Smuzhiyun 
755*4882a593Smuzhiyun static void
IncreaseButtonCount(DeviceIntPtr dev,int key,CARD8 * buttons_down,Mask * motion_mask,unsigned short * state)756*4882a593Smuzhiyun IncreaseButtonCount(DeviceIntPtr dev, int key, CARD8 *buttons_down,
757*4882a593Smuzhiyun                     Mask *motion_mask, unsigned short *state)
758*4882a593Smuzhiyun {
759*4882a593Smuzhiyun     if (dev->valuator)
760*4882a593Smuzhiyun         dev->valuator->motionHintWindow = NullWindow;
761*4882a593Smuzhiyun 
762*4882a593Smuzhiyun     (*buttons_down)++;
763*4882a593Smuzhiyun     *motion_mask = DeviceButtonMotionMask;
764*4882a593Smuzhiyun     if (dev->button->map[key] <= 5)
765*4882a593Smuzhiyun         *state |= (Button1Mask >> 1) << dev->button->map[key];
766*4882a593Smuzhiyun }
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun static void
DecreaseButtonCount(DeviceIntPtr dev,int key,CARD8 * buttons_down,Mask * motion_mask,unsigned short * state)769*4882a593Smuzhiyun DecreaseButtonCount(DeviceIntPtr dev, int key, CARD8 *buttons_down,
770*4882a593Smuzhiyun                     Mask *motion_mask, unsigned short *state)
771*4882a593Smuzhiyun {
772*4882a593Smuzhiyun     if (dev->valuator)
773*4882a593Smuzhiyun         dev->valuator->motionHintWindow = NullWindow;
774*4882a593Smuzhiyun 
775*4882a593Smuzhiyun     if (*buttons_down >= 1 && !--(*buttons_down))
776*4882a593Smuzhiyun         *motion_mask = 0;
777*4882a593Smuzhiyun     if (dev->button->map[key] <= 5)
778*4882a593Smuzhiyun         *state &= ~((Button1Mask >> 1) << dev->button->map[key]);
779*4882a593Smuzhiyun }
780*4882a593Smuzhiyun 
781*4882a593Smuzhiyun /**
782*4882a593Smuzhiyun  * Update the device state according to the data in the event.
783*4882a593Smuzhiyun  *
784*4882a593Smuzhiyun  * return values are
785*4882a593Smuzhiyun  *   DEFAULT ... process as normal
786*4882a593Smuzhiyun  *   DONT_PROCESS ... return immediately from caller
787*4882a593Smuzhiyun  */
788*4882a593Smuzhiyun #define DEFAULT 0
789*4882a593Smuzhiyun #define DONT_PROCESS 1
790*4882a593Smuzhiyun int
UpdateDeviceState(DeviceIntPtr device,DeviceEvent * event)791*4882a593Smuzhiyun UpdateDeviceState(DeviceIntPtr device, DeviceEvent *event)
792*4882a593Smuzhiyun {
793*4882a593Smuzhiyun     int i;
794*4882a593Smuzhiyun     int key = 0, last_valuator;
795*4882a593Smuzhiyun 
796*4882a593Smuzhiyun     KeyClassPtr k = NULL;
797*4882a593Smuzhiyun     ButtonClassPtr b = NULL;
798*4882a593Smuzhiyun     ValuatorClassPtr v = NULL;
799*4882a593Smuzhiyun     TouchClassPtr t = NULL;
800*4882a593Smuzhiyun 
801*4882a593Smuzhiyun     /* This event is always the first we get, before the actual events with
802*4882a593Smuzhiyun      * the data. However, the way how the DDX is set up, "device" will
803*4882a593Smuzhiyun      * actually be the slave device that caused the event.
804*4882a593Smuzhiyun      */
805*4882a593Smuzhiyun     switch (event->type) {
806*4882a593Smuzhiyun     case ET_DeviceChanged:
807*4882a593Smuzhiyun         ChangeMasterDeviceClasses(device, (DeviceChangedEvent *) event);
808*4882a593Smuzhiyun         return DONT_PROCESS;    /* event has been sent already */
809*4882a593Smuzhiyun     case ET_Motion:
810*4882a593Smuzhiyun     case ET_ButtonPress:
811*4882a593Smuzhiyun     case ET_ButtonRelease:
812*4882a593Smuzhiyun     case ET_KeyPress:
813*4882a593Smuzhiyun     case ET_KeyRelease:
814*4882a593Smuzhiyun     case ET_ProximityIn:
815*4882a593Smuzhiyun     case ET_ProximityOut:
816*4882a593Smuzhiyun     case ET_TouchBegin:
817*4882a593Smuzhiyun     case ET_TouchUpdate:
818*4882a593Smuzhiyun     case ET_TouchEnd:
819*4882a593Smuzhiyun         break;
820*4882a593Smuzhiyun     default:
821*4882a593Smuzhiyun         /* other events don't update the device */
822*4882a593Smuzhiyun         return DEFAULT;
823*4882a593Smuzhiyun     }
824*4882a593Smuzhiyun 
825*4882a593Smuzhiyun     k = device->key;
826*4882a593Smuzhiyun     v = device->valuator;
827*4882a593Smuzhiyun     b = device->button;
828*4882a593Smuzhiyun     t = device->touch;
829*4882a593Smuzhiyun 
830*4882a593Smuzhiyun     key = event->detail.key;
831*4882a593Smuzhiyun 
832*4882a593Smuzhiyun     /* Update device axis */
833*4882a593Smuzhiyun     /* Check valuators first */
834*4882a593Smuzhiyun     last_valuator = -1;
835*4882a593Smuzhiyun     for (i = 0; i < MAX_VALUATORS; i++) {
836*4882a593Smuzhiyun         if (BitIsOn(&event->valuators.mask, i)) {
837*4882a593Smuzhiyun             if (!v) {
838*4882a593Smuzhiyun                 ErrorF("[Xi] Valuators reported for non-valuator device '%s'. "
839*4882a593Smuzhiyun                        "Ignoring event.\n", device->name);
840*4882a593Smuzhiyun                 return DONT_PROCESS;
841*4882a593Smuzhiyun             }
842*4882a593Smuzhiyun             else if (v->numAxes < i) {
843*4882a593Smuzhiyun                 ErrorF("[Xi] Too many valuators reported for device '%s'. "
844*4882a593Smuzhiyun                        "Ignoring event.\n", device->name);
845*4882a593Smuzhiyun                 return DONT_PROCESS;
846*4882a593Smuzhiyun             }
847*4882a593Smuzhiyun             last_valuator = i;
848*4882a593Smuzhiyun         }
849*4882a593Smuzhiyun     }
850*4882a593Smuzhiyun 
851*4882a593Smuzhiyun     for (i = 0; i <= last_valuator && i < v->numAxes; i++) {
852*4882a593Smuzhiyun         /* XXX: Relative/Absolute mode */
853*4882a593Smuzhiyun         if (BitIsOn(&event->valuators.mask, i))
854*4882a593Smuzhiyun             v->axisVal[i] = event->valuators.data[i];
855*4882a593Smuzhiyun     }
856*4882a593Smuzhiyun 
857*4882a593Smuzhiyun     if (event->type == ET_KeyPress) {
858*4882a593Smuzhiyun         if (!k)
859*4882a593Smuzhiyun             return DONT_PROCESS;
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun         /* don't allow ddx to generate multiple downs, but repeats are okay */
862*4882a593Smuzhiyun         if (key_is_down(device, key, KEY_PROCESSED) && !event->key_repeat)
863*4882a593Smuzhiyun             return DONT_PROCESS;
864*4882a593Smuzhiyun 
865*4882a593Smuzhiyun         if (device->valuator)
866*4882a593Smuzhiyun             device->valuator->motionHintWindow = NullWindow;
867*4882a593Smuzhiyun         set_key_down(device, key, KEY_PROCESSED);
868*4882a593Smuzhiyun     }
869*4882a593Smuzhiyun     else if (event->type == ET_KeyRelease) {
870*4882a593Smuzhiyun         if (!k)
871*4882a593Smuzhiyun             return DONT_PROCESS;
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun         if (!key_is_down(device, key, KEY_PROCESSED))   /* guard against duplicates */
874*4882a593Smuzhiyun             return DONT_PROCESS;
875*4882a593Smuzhiyun         if (device->valuator)
876*4882a593Smuzhiyun             device->valuator->motionHintWindow = NullWindow;
877*4882a593Smuzhiyun         set_key_up(device, key, KEY_PROCESSED);
878*4882a593Smuzhiyun     }
879*4882a593Smuzhiyun     else if (event->type == ET_ButtonPress) {
880*4882a593Smuzhiyun         if (!b)
881*4882a593Smuzhiyun             return DONT_PROCESS;
882*4882a593Smuzhiyun 
883*4882a593Smuzhiyun         if (button_is_down(device, key, BUTTON_PROCESSED))
884*4882a593Smuzhiyun             return DONT_PROCESS;
885*4882a593Smuzhiyun 
886*4882a593Smuzhiyun         set_button_down(device, key, BUTTON_PROCESSED);
887*4882a593Smuzhiyun 
888*4882a593Smuzhiyun         if (!b->map[key])
889*4882a593Smuzhiyun             return DONT_PROCESS;
890*4882a593Smuzhiyun 
891*4882a593Smuzhiyun         IncreaseButtonCount(device, key, &b->buttonsDown, &b->motionMask,
892*4882a593Smuzhiyun                             &b->state);
893*4882a593Smuzhiyun         UpdateDeviceMotionMask(device, b->state, b->motionMask);
894*4882a593Smuzhiyun     }
895*4882a593Smuzhiyun     else if (event->type == ET_ButtonRelease) {
896*4882a593Smuzhiyun         if (!b)
897*4882a593Smuzhiyun             return DONT_PROCESS;
898*4882a593Smuzhiyun 
899*4882a593Smuzhiyun         if (!button_is_down(device, key, BUTTON_PROCESSED))
900*4882a593Smuzhiyun             return DONT_PROCESS;
901*4882a593Smuzhiyun         if (IsMaster(device)) {
902*4882a593Smuzhiyun             DeviceIntPtr sd;
903*4882a593Smuzhiyun 
904*4882a593Smuzhiyun             /*
905*4882a593Smuzhiyun              * Leave the button down if any slave has the
906*4882a593Smuzhiyun              * button still down. Note that this depends on the
907*4882a593Smuzhiyun              * event being delivered through the slave first
908*4882a593Smuzhiyun              */
909*4882a593Smuzhiyun             for (sd = inputInfo.devices; sd; sd = sd->next) {
910*4882a593Smuzhiyun                 if (IsMaster(sd) || GetMaster(sd, MASTER_POINTER) != device)
911*4882a593Smuzhiyun                     continue;
912*4882a593Smuzhiyun                 if (!sd->button)
913*4882a593Smuzhiyun                     continue;
914*4882a593Smuzhiyun                 for (i = 1; i <= sd->button->numButtons; i++)
915*4882a593Smuzhiyun                     if (sd->button->map[i] == key &&
916*4882a593Smuzhiyun                         button_is_down(sd, i, BUTTON_PROCESSED))
917*4882a593Smuzhiyun                         return DONT_PROCESS;
918*4882a593Smuzhiyun             }
919*4882a593Smuzhiyun         }
920*4882a593Smuzhiyun         set_button_up(device, key, BUTTON_PROCESSED);
921*4882a593Smuzhiyun         if (!b->map[key])
922*4882a593Smuzhiyun             return DONT_PROCESS;
923*4882a593Smuzhiyun 
924*4882a593Smuzhiyun         DecreaseButtonCount(device, key, &b->buttonsDown, &b->motionMask,
925*4882a593Smuzhiyun                             &b->state);
926*4882a593Smuzhiyun         UpdateDeviceMotionMask(device, b->state, b->motionMask);
927*4882a593Smuzhiyun     }
928*4882a593Smuzhiyun     else if (event->type == ET_ProximityIn)
929*4882a593Smuzhiyun         device->proximity->in_proximity = TRUE;
930*4882a593Smuzhiyun     else if (event->type == ET_ProximityOut)
931*4882a593Smuzhiyun         device->proximity->in_proximity = FALSE;
932*4882a593Smuzhiyun     else if (event->type == ET_TouchBegin) {
933*4882a593Smuzhiyun         BUG_RETURN_VAL(!b || !v, DONT_PROCESS);
934*4882a593Smuzhiyun         BUG_RETURN_VAL(!t, DONT_PROCESS);
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun         if (!b->map[key])
937*4882a593Smuzhiyun             return DONT_PROCESS;
938*4882a593Smuzhiyun 
939*4882a593Smuzhiyun         if (!(event->flags & TOUCH_POINTER_EMULATED) ||
940*4882a593Smuzhiyun             (event->flags & TOUCH_REPLAYING))
941*4882a593Smuzhiyun             return DONT_PROCESS;
942*4882a593Smuzhiyun 
943*4882a593Smuzhiyun         IncreaseButtonCount(device, key, &t->buttonsDown, &t->motionMask,
944*4882a593Smuzhiyun                             &t->state);
945*4882a593Smuzhiyun         UpdateDeviceMotionMask(device, t->state, DeviceButtonMotionMask);
946*4882a593Smuzhiyun     }
947*4882a593Smuzhiyun     else if (event->type == ET_TouchEnd) {
948*4882a593Smuzhiyun         BUG_RETURN_VAL(!b || !v, DONT_PROCESS);
949*4882a593Smuzhiyun         BUG_RETURN_VAL(!t, DONT_PROCESS);
950*4882a593Smuzhiyun 
951*4882a593Smuzhiyun         if (t->buttonsDown <= 0 || !b->map[key])
952*4882a593Smuzhiyun             return DONT_PROCESS;
953*4882a593Smuzhiyun 
954*4882a593Smuzhiyun         if (!(event->flags & TOUCH_POINTER_EMULATED))
955*4882a593Smuzhiyun             return DONT_PROCESS;
956*4882a593Smuzhiyun 
957*4882a593Smuzhiyun         DecreaseButtonCount(device, key, &t->buttonsDown, &t->motionMask,
958*4882a593Smuzhiyun                             &t->state);
959*4882a593Smuzhiyun         UpdateDeviceMotionMask(device, t->state, DeviceButtonMotionMask);
960*4882a593Smuzhiyun     }
961*4882a593Smuzhiyun 
962*4882a593Smuzhiyun     return DEFAULT;
963*4882a593Smuzhiyun }
964*4882a593Smuzhiyun 
965*4882a593Smuzhiyun /**
966*4882a593Smuzhiyun  * A client that does not have the TouchOwnership mask set may not receive a
967*4882a593Smuzhiyun  * TouchBegin event if there is at least one grab active.
968*4882a593Smuzhiyun  *
969*4882a593Smuzhiyun  * @return TRUE if the client selected for ownership events on the given
970*4882a593Smuzhiyun  * window for this device, FALSE otherwise
971*4882a593Smuzhiyun  */
972*4882a593Smuzhiyun static inline Bool
TouchClientWantsOwnershipEvents(ClientPtr client,DeviceIntPtr dev,WindowPtr win)973*4882a593Smuzhiyun TouchClientWantsOwnershipEvents(ClientPtr client, DeviceIntPtr dev,
974*4882a593Smuzhiyun                                 WindowPtr win)
975*4882a593Smuzhiyun {
976*4882a593Smuzhiyun     InputClients *iclient;
977*4882a593Smuzhiyun 
978*4882a593Smuzhiyun     nt_list_for_each_entry(iclient, wOtherInputMasks(win)->inputClients, next) {
979*4882a593Smuzhiyun         if (rClient(iclient) != client)
980*4882a593Smuzhiyun             continue;
981*4882a593Smuzhiyun 
982*4882a593Smuzhiyun         return xi2mask_isset(iclient->xi2mask, dev, XI_TouchOwnership);
983*4882a593Smuzhiyun     }
984*4882a593Smuzhiyun 
985*4882a593Smuzhiyun     return FALSE;
986*4882a593Smuzhiyun }
987*4882a593Smuzhiyun 
988*4882a593Smuzhiyun static void
TouchSendOwnershipEvent(DeviceIntPtr dev,TouchPointInfoPtr ti,int reason,XID resource)989*4882a593Smuzhiyun TouchSendOwnershipEvent(DeviceIntPtr dev, TouchPointInfoPtr ti, int reason,
990*4882a593Smuzhiyun                         XID resource)
991*4882a593Smuzhiyun {
992*4882a593Smuzhiyun     int nev, i;
993*4882a593Smuzhiyun     InternalEvent *tel = InitEventList(GetMaximumEventsNum());
994*4882a593Smuzhiyun 
995*4882a593Smuzhiyun     nev = GetTouchOwnershipEvents(tel, dev, ti, reason, resource, 0);
996*4882a593Smuzhiyun     for (i = 0; i < nev; i++)
997*4882a593Smuzhiyun         mieqProcessDeviceEvent(dev, tel + i, NULL);
998*4882a593Smuzhiyun 
999*4882a593Smuzhiyun     FreeEventList(tel, GetMaximumEventsNum());
1000*4882a593Smuzhiyun }
1001*4882a593Smuzhiyun 
1002*4882a593Smuzhiyun /**
1003*4882a593Smuzhiyun  * Attempts to deliver a touch event to the given client.
1004*4882a593Smuzhiyun  */
1005*4882a593Smuzhiyun static Bool
DeliverOneTouchEvent(ClientPtr client,DeviceIntPtr dev,TouchPointInfoPtr ti,GrabPtr grab,WindowPtr win,InternalEvent * ev)1006*4882a593Smuzhiyun DeliverOneTouchEvent(ClientPtr client, DeviceIntPtr dev, TouchPointInfoPtr ti,
1007*4882a593Smuzhiyun                      GrabPtr grab, WindowPtr win, InternalEvent *ev)
1008*4882a593Smuzhiyun {
1009*4882a593Smuzhiyun     int err;
1010*4882a593Smuzhiyun     xEvent *xi2;
1011*4882a593Smuzhiyun     Mask filter;
1012*4882a593Smuzhiyun     Window child = DeepestSpriteWin(&ti->sprite)->drawable.id;
1013*4882a593Smuzhiyun 
1014*4882a593Smuzhiyun     /* FIXME: owner event handling */
1015*4882a593Smuzhiyun 
1016*4882a593Smuzhiyun     /* If the client does not have the ownership mask set and is not
1017*4882a593Smuzhiyun      * the current owner of the touch, only pretend we delivered */
1018*4882a593Smuzhiyun     if (!grab && ti->num_grabs != 0 &&
1019*4882a593Smuzhiyun         !TouchClientWantsOwnershipEvents(client, dev, win))
1020*4882a593Smuzhiyun         return TRUE;
1021*4882a593Smuzhiyun 
1022*4882a593Smuzhiyun     /* If we fail here, we're going to leave a client hanging. */
1023*4882a593Smuzhiyun     err = EventToXI2(ev, &xi2);
1024*4882a593Smuzhiyun     if (err != Success)
1025*4882a593Smuzhiyun         FatalError("[Xi] %s: XI2 conversion failed in %s"
1026*4882a593Smuzhiyun                    " (%d)\n", dev->name, __func__, err);
1027*4882a593Smuzhiyun 
1028*4882a593Smuzhiyun     FixUpEventFromWindow(&ti->sprite, xi2, win, child, FALSE);
1029*4882a593Smuzhiyun     filter = GetEventFilter(dev, xi2);
1030*4882a593Smuzhiyun     if (XaceHook(XACE_RECEIVE_ACCESS, client, win, xi2, 1) != Success)
1031*4882a593Smuzhiyun         return FALSE;
1032*4882a593Smuzhiyun     err = TryClientEvents(client, dev, xi2, 1, filter, filter, NullGrab);
1033*4882a593Smuzhiyun     free(xi2);
1034*4882a593Smuzhiyun 
1035*4882a593Smuzhiyun     if (ev->any.type == ET_TouchUpdate && ti->pending_finish) {
1036*4882a593Smuzhiyun         ev->any.type = ET_TouchEnd;
1037*4882a593Smuzhiyun         DeliverOneTouchEvent(client, dev, ti, grab, win, ev);
1038*4882a593Smuzhiyun     }
1039*4882a593Smuzhiyun 
1040*4882a593Smuzhiyun     /* Returning the value from TryClientEvents isn't useful, since all our
1041*4882a593Smuzhiyun      * resource-gone cleanups will update the delivery list anyway. */
1042*4882a593Smuzhiyun     return TRUE;
1043*4882a593Smuzhiyun }
1044*4882a593Smuzhiyun 
1045*4882a593Smuzhiyun static void
ActivateEarlyAccept(DeviceIntPtr dev,TouchPointInfoPtr ti)1046*4882a593Smuzhiyun ActivateEarlyAccept(DeviceIntPtr dev, TouchPointInfoPtr ti)
1047*4882a593Smuzhiyun {
1048*4882a593Smuzhiyun     ClientPtr client;
1049*4882a593Smuzhiyun     XID error;
1050*4882a593Smuzhiyun     GrabPtr grab = ti->listeners[0].grab;
1051*4882a593Smuzhiyun 
1052*4882a593Smuzhiyun     BUG_RETURN(ti->listeners[0].type != LISTENER_GRAB &&
1053*4882a593Smuzhiyun                ti->listeners[0].type != LISTENER_POINTER_GRAB);
1054*4882a593Smuzhiyun     BUG_RETURN(!grab);
1055*4882a593Smuzhiyun 
1056*4882a593Smuzhiyun     client = rClient(grab);
1057*4882a593Smuzhiyun 
1058*4882a593Smuzhiyun     if (TouchAcceptReject(client, dev, XIAcceptTouch, ti->client_id,
1059*4882a593Smuzhiyun                           ti->listeners[0].window->drawable.id, &error) != Success)
1060*4882a593Smuzhiyun         ErrorF("[Xi] Failed to accept touch grab after early acceptance.\n");
1061*4882a593Smuzhiyun }
1062*4882a593Smuzhiyun 
1063*4882a593Smuzhiyun /**
1064*4882a593Smuzhiyun  * Find the oldest touch that still has a pointer emulation client.
1065*4882a593Smuzhiyun  *
1066*4882a593Smuzhiyun  * Pointer emulation can only be performed for the oldest touch. Otherwise, the
1067*4882a593Smuzhiyun  * order of events seen by the client will be wrong. This function helps us find
1068*4882a593Smuzhiyun  * the next touch to be emulated.
1069*4882a593Smuzhiyun  *
1070*4882a593Smuzhiyun  * @param dev The device to find touches for.
1071*4882a593Smuzhiyun  */
1072*4882a593Smuzhiyun static TouchPointInfoPtr
FindOldestPointerEmulatedTouch(DeviceIntPtr dev)1073*4882a593Smuzhiyun FindOldestPointerEmulatedTouch(DeviceIntPtr dev)
1074*4882a593Smuzhiyun {
1075*4882a593Smuzhiyun     TouchPointInfoPtr oldest = NULL;
1076*4882a593Smuzhiyun     int i;
1077*4882a593Smuzhiyun 
1078*4882a593Smuzhiyun     for (i = 0; i < dev->touch->num_touches; i++) {
1079*4882a593Smuzhiyun         TouchPointInfoPtr ti = dev->touch->touches + i;
1080*4882a593Smuzhiyun         int j;
1081*4882a593Smuzhiyun 
1082*4882a593Smuzhiyun         if (!ti->active || !ti->emulate_pointer)
1083*4882a593Smuzhiyun             continue;
1084*4882a593Smuzhiyun 
1085*4882a593Smuzhiyun         for (j = 0; j < ti->num_listeners; j++) {
1086*4882a593Smuzhiyun             if (ti->listeners[j].type == LISTENER_POINTER_GRAB ||
1087*4882a593Smuzhiyun                 ti->listeners[j].type == LISTENER_POINTER_REGULAR)
1088*4882a593Smuzhiyun                 break;
1089*4882a593Smuzhiyun         }
1090*4882a593Smuzhiyun         if (j == ti->num_listeners)
1091*4882a593Smuzhiyun             continue;
1092*4882a593Smuzhiyun 
1093*4882a593Smuzhiyun         if (!oldest) {
1094*4882a593Smuzhiyun             oldest = ti;
1095*4882a593Smuzhiyun             continue;
1096*4882a593Smuzhiyun         }
1097*4882a593Smuzhiyun 
1098*4882a593Smuzhiyun         if (oldest->client_id - ti->client_id < UINT_MAX / 2)
1099*4882a593Smuzhiyun             oldest = ti;
1100*4882a593Smuzhiyun     }
1101*4882a593Smuzhiyun 
1102*4882a593Smuzhiyun     return oldest;
1103*4882a593Smuzhiyun }
1104*4882a593Smuzhiyun 
1105*4882a593Smuzhiyun /**
1106*4882a593Smuzhiyun  * If the current owner has rejected the event, deliver the
1107*4882a593Smuzhiyun  * TouchOwnership/TouchBegin to the next item in the sprite stack.
1108*4882a593Smuzhiyun  */
1109*4882a593Smuzhiyun static void
TouchPuntToNextOwner(DeviceIntPtr dev,TouchPointInfoPtr ti,TouchOwnershipEvent * ev)1110*4882a593Smuzhiyun TouchPuntToNextOwner(DeviceIntPtr dev, TouchPointInfoPtr ti,
1111*4882a593Smuzhiyun                      TouchOwnershipEvent *ev)
1112*4882a593Smuzhiyun {
1113*4882a593Smuzhiyun     TouchListener *listener = &ti->listeners[0]; /* new owner */
1114*4882a593Smuzhiyun     int accepted_early = listener->state == LISTENER_EARLY_ACCEPT;
1115*4882a593Smuzhiyun 
1116*4882a593Smuzhiyun     /* Deliver the ownership */
1117*4882a593Smuzhiyun     if (listener->state == LISTENER_AWAITING_OWNER || accepted_early)
1118*4882a593Smuzhiyun         DeliverTouchEvents(dev, ti, (InternalEvent *) ev,
1119*4882a593Smuzhiyun                            listener->listener);
1120*4882a593Smuzhiyun     else if (listener->state == LISTENER_AWAITING_BEGIN) {
1121*4882a593Smuzhiyun         /* We can't punt to a pointer listener unless all older pointer
1122*4882a593Smuzhiyun          * emulated touches have been seen already. */
1123*4882a593Smuzhiyun         if ((listener->type == LISTENER_POINTER_GRAB ||
1124*4882a593Smuzhiyun              listener->type == LISTENER_POINTER_REGULAR) &&
1125*4882a593Smuzhiyun             ti != FindOldestPointerEmulatedTouch(dev))
1126*4882a593Smuzhiyun             return;
1127*4882a593Smuzhiyun 
1128*4882a593Smuzhiyun         TouchEventHistoryReplay(ti, dev, listener->listener);
1129*4882a593Smuzhiyun     }
1130*4882a593Smuzhiyun 
1131*4882a593Smuzhiyun     /* New owner has Begin/Update but not end. If touch is pending_finish,
1132*4882a593Smuzhiyun      * emulate the TouchEnd now */
1133*4882a593Smuzhiyun     if (ti->pending_finish) {
1134*4882a593Smuzhiyun         TouchEmitTouchEnd(dev, ti, 0, 0);
1135*4882a593Smuzhiyun 
1136*4882a593Smuzhiyun         /* If the last owner is not a touch grab, finalise the touch, we
1137*4882a593Smuzhiyun            won't get more correspondence on this.
1138*4882a593Smuzhiyun          */
1139*4882a593Smuzhiyun         if (ti->num_listeners == 1 &&
1140*4882a593Smuzhiyun             (ti->num_grabs == 0 ||
1141*4882a593Smuzhiyun              listener->grab->grabtype != XI2 ||
1142*4882a593Smuzhiyun              !xi2mask_isset(listener->grab->xi2mask, dev, XI_TouchBegin))) {
1143*4882a593Smuzhiyun             TouchEndTouch(dev, ti);
1144*4882a593Smuzhiyun             return;
1145*4882a593Smuzhiyun         }
1146*4882a593Smuzhiyun     }
1147*4882a593Smuzhiyun 
1148*4882a593Smuzhiyun     if (accepted_early)
1149*4882a593Smuzhiyun         ActivateEarlyAccept(dev, ti);
1150*4882a593Smuzhiyun }
1151*4882a593Smuzhiyun 
1152*4882a593Smuzhiyun /**
1153*4882a593Smuzhiyun  * Check the oldest touch to see if it needs to be replayed to its pointer
1154*4882a593Smuzhiyun  * owner.
1155*4882a593Smuzhiyun  *
1156*4882a593Smuzhiyun  * Touch event propagation is paused if it hits a pointer listener while an
1157*4882a593Smuzhiyun  * older touch with a pointer listener is waiting on accept or reject. This
1158*4882a593Smuzhiyun  * function will restart propagation of a paused touch if needed.
1159*4882a593Smuzhiyun  *
1160*4882a593Smuzhiyun  * @param dev The device to check touches for.
1161*4882a593Smuzhiyun  */
1162*4882a593Smuzhiyun static void
CheckOldestTouch(DeviceIntPtr dev)1163*4882a593Smuzhiyun CheckOldestTouch(DeviceIntPtr dev)
1164*4882a593Smuzhiyun {
1165*4882a593Smuzhiyun     TouchPointInfoPtr oldest = FindOldestPointerEmulatedTouch(dev);
1166*4882a593Smuzhiyun 
1167*4882a593Smuzhiyun     if (oldest && oldest->listeners[0].state == LISTENER_AWAITING_BEGIN)
1168*4882a593Smuzhiyun         TouchPuntToNextOwner(dev, oldest, NULL);
1169*4882a593Smuzhiyun }
1170*4882a593Smuzhiyun 
1171*4882a593Smuzhiyun /**
1172*4882a593Smuzhiyun  * Process a touch rejection.
1173*4882a593Smuzhiyun  *
1174*4882a593Smuzhiyun  * @param sourcedev The source device of the touch sequence.
1175*4882a593Smuzhiyun  * @param ti The touchpoint info record.
1176*4882a593Smuzhiyun  * @param resource The resource of the client rejecting the touch.
1177*4882a593Smuzhiyun  * @param ev TouchOwnership event to send. Set to NULL if no event should be
1178*4882a593Smuzhiyun  *        sent.
1179*4882a593Smuzhiyun  */
1180*4882a593Smuzhiyun void
TouchRejected(DeviceIntPtr sourcedev,TouchPointInfoPtr ti,XID resource,TouchOwnershipEvent * ev)1181*4882a593Smuzhiyun TouchRejected(DeviceIntPtr sourcedev, TouchPointInfoPtr ti, XID resource,
1182*4882a593Smuzhiyun               TouchOwnershipEvent *ev)
1183*4882a593Smuzhiyun {
1184*4882a593Smuzhiyun     Bool was_owner = (resource == ti->listeners[0].listener);
1185*4882a593Smuzhiyun     int i;
1186*4882a593Smuzhiyun 
1187*4882a593Smuzhiyun     /* Send a TouchEnd event to the resource being removed, but only if they
1188*4882a593Smuzhiyun      * haven't received one yet already */
1189*4882a593Smuzhiyun     for (i = 0; i < ti->num_listeners; i++) {
1190*4882a593Smuzhiyun         if (ti->listeners[i].listener == resource) {
1191*4882a593Smuzhiyun             if (ti->listeners[i].state != LISTENER_HAS_END)
1192*4882a593Smuzhiyun                 TouchEmitTouchEnd(sourcedev, ti, TOUCH_REJECT, resource);
1193*4882a593Smuzhiyun             break;
1194*4882a593Smuzhiyun         }
1195*4882a593Smuzhiyun     }
1196*4882a593Smuzhiyun 
1197*4882a593Smuzhiyun     /* Remove the resource from the listener list, updating
1198*4882a593Smuzhiyun      * ti->num_listeners, as well as ti->num_grabs if it was a grab. */
1199*4882a593Smuzhiyun     TouchRemoveListener(ti, resource);
1200*4882a593Smuzhiyun 
1201*4882a593Smuzhiyun     /* If the current owner was removed and there are further listeners, deliver
1202*4882a593Smuzhiyun      * the TouchOwnership or TouchBegin event to the new owner. */
1203*4882a593Smuzhiyun     if (ev && ti->num_listeners > 0 && was_owner)
1204*4882a593Smuzhiyun         TouchPuntToNextOwner(sourcedev, ti, ev);
1205*4882a593Smuzhiyun     else if (ti->num_listeners == 0)
1206*4882a593Smuzhiyun         TouchEndTouch(sourcedev, ti);
1207*4882a593Smuzhiyun 
1208*4882a593Smuzhiyun     CheckOldestTouch(sourcedev);
1209*4882a593Smuzhiyun }
1210*4882a593Smuzhiyun 
1211*4882a593Smuzhiyun /**
1212*4882a593Smuzhiyun  * Processes a TouchOwnership event, indicating a grab has accepted the touch
1213*4882a593Smuzhiyun  * it currently owns, or a grab or selection has been removed.  Will generate
1214*4882a593Smuzhiyun  * and send TouchEnd events to all clients removed from the delivery list, as
1215*4882a593Smuzhiyun  * well as possibly sending the new TouchOwnership event.  May end the
1216*4882a593Smuzhiyun  * touchpoint if it is pending finish.
1217*4882a593Smuzhiyun  */
1218*4882a593Smuzhiyun static void
ProcessTouchOwnershipEvent(TouchOwnershipEvent * ev,DeviceIntPtr dev)1219*4882a593Smuzhiyun ProcessTouchOwnershipEvent(TouchOwnershipEvent *ev,
1220*4882a593Smuzhiyun                            DeviceIntPtr dev)
1221*4882a593Smuzhiyun {
1222*4882a593Smuzhiyun     TouchPointInfoPtr ti = TouchFindByClientID(dev, ev->touchid);
1223*4882a593Smuzhiyun 
1224*4882a593Smuzhiyun     if (!ti) {
1225*4882a593Smuzhiyun         DebugF("[Xi] %s: Failed to get event %d for touchpoint %d\n",
1226*4882a593Smuzhiyun                dev->name, ev->type, ev->touchid);
1227*4882a593Smuzhiyun         return;
1228*4882a593Smuzhiyun     }
1229*4882a593Smuzhiyun 
1230*4882a593Smuzhiyun     if (ev->reason == XIRejectTouch)
1231*4882a593Smuzhiyun         TouchRejected(dev, ti, ev->resource, ev);
1232*4882a593Smuzhiyun     else if (ev->reason == XIAcceptTouch) {
1233*4882a593Smuzhiyun         int i;
1234*4882a593Smuzhiyun 
1235*4882a593Smuzhiyun 
1236*4882a593Smuzhiyun         /* For pointer-emulated listeners that ungrabbed the active grab,
1237*4882a593Smuzhiyun          * the state was forced to LISTENER_HAS_END. Still go
1238*4882a593Smuzhiyun          * through the motions of ending the touch if the listener has
1239*4882a593Smuzhiyun          * already seen the end. This ensures that the touch record is ended in
1240*4882a593Smuzhiyun          * the server.
1241*4882a593Smuzhiyun          */
1242*4882a593Smuzhiyun         if (ti->listeners[0].state == LISTENER_HAS_END)
1243*4882a593Smuzhiyun             TouchEmitTouchEnd(dev, ti, TOUCH_ACCEPT, ti->listeners[0].listener);
1244*4882a593Smuzhiyun 
1245*4882a593Smuzhiyun         /* The touch owner has accepted the touch.  Send TouchEnd events to
1246*4882a593Smuzhiyun          * everyone else, and truncate the list of listeners. */
1247*4882a593Smuzhiyun         for (i = 1; i < ti->num_listeners; i++)
1248*4882a593Smuzhiyun             TouchEmitTouchEnd(dev, ti, TOUCH_ACCEPT, ti->listeners[i].listener);
1249*4882a593Smuzhiyun 
1250*4882a593Smuzhiyun         while (ti->num_listeners > 1)
1251*4882a593Smuzhiyun             TouchRemoveListener(ti, ti->listeners[1].listener);
1252*4882a593Smuzhiyun         /* Owner accepted after receiving end */
1253*4882a593Smuzhiyun         if (ti->listeners[0].state == LISTENER_HAS_END)
1254*4882a593Smuzhiyun             TouchEndTouch(dev, ti);
1255*4882a593Smuzhiyun         else
1256*4882a593Smuzhiyun             ti->listeners[0].state = LISTENER_HAS_ACCEPTED;
1257*4882a593Smuzhiyun     }
1258*4882a593Smuzhiyun     else {  /* this is the very first ownership event for a grab */
1259*4882a593Smuzhiyun         DeliverTouchEvents(dev, ti, (InternalEvent *) ev, ev->resource);
1260*4882a593Smuzhiyun     }
1261*4882a593Smuzhiyun }
1262*4882a593Smuzhiyun 
1263*4882a593Smuzhiyun /**
1264*4882a593Smuzhiyun  * Copy the event's valuator information into the touchpoint, we may need
1265*4882a593Smuzhiyun  * this for emulated TouchEnd events.
1266*4882a593Smuzhiyun  */
1267*4882a593Smuzhiyun static void
TouchCopyValuatorData(DeviceEvent * ev,TouchPointInfoPtr ti)1268*4882a593Smuzhiyun TouchCopyValuatorData(DeviceEvent *ev, TouchPointInfoPtr ti)
1269*4882a593Smuzhiyun {
1270*4882a593Smuzhiyun     int i;
1271*4882a593Smuzhiyun 
1272*4882a593Smuzhiyun     for (i = 0; i < ARRAY_SIZE(ev->valuators.data); i++)
1273*4882a593Smuzhiyun         if (BitIsOn(ev->valuators.mask, i))
1274*4882a593Smuzhiyun             valuator_mask_set_double(ti->valuators, i, ev->valuators.data[i]);
1275*4882a593Smuzhiyun }
1276*4882a593Smuzhiyun 
1277*4882a593Smuzhiyun /**
1278*4882a593Smuzhiyun  * Given a touch event and a potential listener, retrieve info needed for
1279*4882a593Smuzhiyun  * processing the event.
1280*4882a593Smuzhiyun  *
1281*4882a593Smuzhiyun  * @param dev The device generating the touch event.
1282*4882a593Smuzhiyun  * @param ti The touch point info record for the touch event.
1283*4882a593Smuzhiyun  * @param ev The touch event to process.
1284*4882a593Smuzhiyun  * @param listener The touch event listener that may receive the touch event.
1285*4882a593Smuzhiyun  * @param[out] client The client that should receive the touch event.
1286*4882a593Smuzhiyun  * @param[out] win The window to deliver the event on.
1287*4882a593Smuzhiyun  * @param[out] grab The grab to deliver the event through, if any.
1288*4882a593Smuzhiyun  * @param[out] mask The XI 2.x event mask of the grab or selection, if any.
1289*4882a593Smuzhiyun  * @return TRUE if an event should be delivered to the listener, FALSE
1290*4882a593Smuzhiyun  *         otherwise.
1291*4882a593Smuzhiyun  */
1292*4882a593Smuzhiyun static Bool
RetrieveTouchDeliveryData(DeviceIntPtr dev,TouchPointInfoPtr ti,InternalEvent * ev,TouchListener * listener,ClientPtr * client,WindowPtr * win,GrabPtr * grab,XI2Mask ** mask)1293*4882a593Smuzhiyun RetrieveTouchDeliveryData(DeviceIntPtr dev, TouchPointInfoPtr ti,
1294*4882a593Smuzhiyun                           InternalEvent *ev, TouchListener * listener,
1295*4882a593Smuzhiyun                           ClientPtr *client, WindowPtr *win, GrabPtr *grab,
1296*4882a593Smuzhiyun                           XI2Mask **mask)
1297*4882a593Smuzhiyun {
1298*4882a593Smuzhiyun     int rc;
1299*4882a593Smuzhiyun     InputClients *iclients = NULL;
1300*4882a593Smuzhiyun     *mask = NULL;
1301*4882a593Smuzhiyun 
1302*4882a593Smuzhiyun     if (listener->type == LISTENER_GRAB ||
1303*4882a593Smuzhiyun         listener->type == LISTENER_POINTER_GRAB) {
1304*4882a593Smuzhiyun         *grab = listener->grab;
1305*4882a593Smuzhiyun 
1306*4882a593Smuzhiyun         BUG_RETURN_VAL(!*grab, FALSE);
1307*4882a593Smuzhiyun 
1308*4882a593Smuzhiyun         *client = rClient(*grab);
1309*4882a593Smuzhiyun         *win = (*grab)->window;
1310*4882a593Smuzhiyun         *mask = (*grab)->xi2mask;
1311*4882a593Smuzhiyun     }
1312*4882a593Smuzhiyun     else {
1313*4882a593Smuzhiyun         rc = dixLookupResourceByType((void **) win, listener->listener,
1314*4882a593Smuzhiyun                                      listener->resource_type,
1315*4882a593Smuzhiyun                                      serverClient, DixSendAccess);
1316*4882a593Smuzhiyun         if (rc != Success)
1317*4882a593Smuzhiyun             return FALSE;
1318*4882a593Smuzhiyun 
1319*4882a593Smuzhiyun         if (listener->level == XI2) {
1320*4882a593Smuzhiyun             int evtype;
1321*4882a593Smuzhiyun 
1322*4882a593Smuzhiyun             if (ti->emulate_pointer &&
1323*4882a593Smuzhiyun                 listener->type == LISTENER_POINTER_REGULAR)
1324*4882a593Smuzhiyun                 evtype = GetXI2Type(TouchGetPointerEventType(ev));
1325*4882a593Smuzhiyun             else
1326*4882a593Smuzhiyun                 evtype = GetXI2Type(ev->any.type);
1327*4882a593Smuzhiyun 
1328*4882a593Smuzhiyun             nt_list_for_each_entry(iclients,
1329*4882a593Smuzhiyun                                    wOtherInputMasks(*win)->inputClients, next)
1330*4882a593Smuzhiyun                 if (xi2mask_isset(iclients->xi2mask, dev, evtype))
1331*4882a593Smuzhiyun                 break;
1332*4882a593Smuzhiyun 
1333*4882a593Smuzhiyun             BUG_RETURN_VAL(!iclients, FALSE);
1334*4882a593Smuzhiyun 
1335*4882a593Smuzhiyun             *mask = iclients->xi2mask;
1336*4882a593Smuzhiyun             *client = rClient(iclients);
1337*4882a593Smuzhiyun         }
1338*4882a593Smuzhiyun         else if (listener->level == XI) {
1339*4882a593Smuzhiyun             int xi_type = GetXIType(TouchGetPointerEventType(ev));
1340*4882a593Smuzhiyun             Mask xi_filter = event_get_filter_from_type(dev, xi_type);
1341*4882a593Smuzhiyun 
1342*4882a593Smuzhiyun             nt_list_for_each_entry(iclients,
1343*4882a593Smuzhiyun                                    wOtherInputMasks(*win)->inputClients, next)
1344*4882a593Smuzhiyun                 if (iclients->mask[dev->id] & xi_filter)
1345*4882a593Smuzhiyun                 break;
1346*4882a593Smuzhiyun             BUG_RETURN_VAL(!iclients, FALSE);
1347*4882a593Smuzhiyun 
1348*4882a593Smuzhiyun             *client = rClient(iclients);
1349*4882a593Smuzhiyun         }
1350*4882a593Smuzhiyun         else {
1351*4882a593Smuzhiyun             int coretype = GetCoreType(TouchGetPointerEventType(ev));
1352*4882a593Smuzhiyun             Mask core_filter = event_get_filter_from_type(dev, coretype);
1353*4882a593Smuzhiyun             OtherClients *oclients;
1354*4882a593Smuzhiyun 
1355*4882a593Smuzhiyun             /* all others */
1356*4882a593Smuzhiyun             nt_list_for_each_entry(oclients,
1357*4882a593Smuzhiyun                                    (OtherClients *) wOtherClients(*win), next)
1358*4882a593Smuzhiyun                 if (oclients->mask & core_filter)
1359*4882a593Smuzhiyun                     break;
1360*4882a593Smuzhiyun 
1361*4882a593Smuzhiyun             /* if owner selected, oclients is NULL */
1362*4882a593Smuzhiyun             *client = oclients ? rClient(oclients) : wClient(*win);
1363*4882a593Smuzhiyun         }
1364*4882a593Smuzhiyun 
1365*4882a593Smuzhiyun         *grab = NULL;
1366*4882a593Smuzhiyun     }
1367*4882a593Smuzhiyun 
1368*4882a593Smuzhiyun     return TRUE;
1369*4882a593Smuzhiyun }
1370*4882a593Smuzhiyun 
1371*4882a593Smuzhiyun static int
DeliverTouchEmulatedEvent(DeviceIntPtr dev,TouchPointInfoPtr ti,InternalEvent * ev,TouchListener * listener,ClientPtr client,WindowPtr win,GrabPtr grab,XI2Mask * xi2mask)1372*4882a593Smuzhiyun DeliverTouchEmulatedEvent(DeviceIntPtr dev, TouchPointInfoPtr ti,
1373*4882a593Smuzhiyun                           InternalEvent *ev, TouchListener * listener,
1374*4882a593Smuzhiyun                           ClientPtr client, WindowPtr win, GrabPtr grab,
1375*4882a593Smuzhiyun                           XI2Mask *xi2mask)
1376*4882a593Smuzhiyun {
1377*4882a593Smuzhiyun     InternalEvent motion, button;
1378*4882a593Smuzhiyun     InternalEvent *ptrev = &motion;
1379*4882a593Smuzhiyun     int nevents;
1380*4882a593Smuzhiyun     DeviceIntPtr kbd;
1381*4882a593Smuzhiyun 
1382*4882a593Smuzhiyun     /* There may be a pointer grab on the device */
1383*4882a593Smuzhiyun     if (!grab) {
1384*4882a593Smuzhiyun         grab = dev->deviceGrab.grab;
1385*4882a593Smuzhiyun         if (grab) {
1386*4882a593Smuzhiyun             win = grab->window;
1387*4882a593Smuzhiyun             xi2mask = grab->xi2mask;
1388*4882a593Smuzhiyun             client = rClient(grab);
1389*4882a593Smuzhiyun         }
1390*4882a593Smuzhiyun     }
1391*4882a593Smuzhiyun 
1392*4882a593Smuzhiyun     /* We don't deliver pointer events to non-owners */
1393*4882a593Smuzhiyun     if (!TouchResourceIsOwner(ti, listener->listener))
1394*4882a593Smuzhiyun         return !Success;
1395*4882a593Smuzhiyun 
1396*4882a593Smuzhiyun     if (!ti->emulate_pointer)
1397*4882a593Smuzhiyun         return !Success;
1398*4882a593Smuzhiyun 
1399*4882a593Smuzhiyun     nevents = TouchConvertToPointerEvent(ev, &motion, &button);
1400*4882a593Smuzhiyun     BUG_RETURN_VAL(nevents == 0, BadValue);
1401*4882a593Smuzhiyun 
1402*4882a593Smuzhiyun     if (nevents > 1)
1403*4882a593Smuzhiyun         ptrev = &button;
1404*4882a593Smuzhiyun 
1405*4882a593Smuzhiyun     kbd = GetMaster(dev, KEYBOARD_OR_FLOAT);
1406*4882a593Smuzhiyun     event_set_state(dev, kbd, &ptrev->device_event);
1407*4882a593Smuzhiyun     ptrev->device_event.corestate = event_get_corestate(dev, kbd);
1408*4882a593Smuzhiyun 
1409*4882a593Smuzhiyun     if (grab) {
1410*4882a593Smuzhiyun         /* this side-steps the usual activation mechanisms, but... */
1411*4882a593Smuzhiyun         if (ev->any.type == ET_TouchBegin && !dev->deviceGrab.grab)
1412*4882a593Smuzhiyun             ActivatePassiveGrab(dev, grab, ptrev, ev);  /* also delivers the event */
1413*4882a593Smuzhiyun         else {
1414*4882a593Smuzhiyun             int deliveries = 0;
1415*4882a593Smuzhiyun 
1416*4882a593Smuzhiyun             /* 'grab' is the passive grab, but if the grab isn't active,
1417*4882a593Smuzhiyun              * don't deliver */
1418*4882a593Smuzhiyun             if (!dev->deviceGrab.grab)
1419*4882a593Smuzhiyun                 return !Success;
1420*4882a593Smuzhiyun 
1421*4882a593Smuzhiyun             if (grab->ownerEvents) {
1422*4882a593Smuzhiyun                 WindowPtr focus = NullWindow;
1423*4882a593Smuzhiyun                 WindowPtr sprite_win = DeepestSpriteWin(dev->spriteInfo->sprite);
1424*4882a593Smuzhiyun 
1425*4882a593Smuzhiyun                 deliveries = DeliverDeviceEvents(sprite_win, ptrev, grab, focus, dev);
1426*4882a593Smuzhiyun             }
1427*4882a593Smuzhiyun 
1428*4882a593Smuzhiyun             if (!deliveries)
1429*4882a593Smuzhiyun                 deliveries = DeliverOneGrabbedEvent(ptrev, dev, grab->grabtype);
1430*4882a593Smuzhiyun 
1431*4882a593Smuzhiyun             /* We must accept the touch sequence once a pointer listener has
1432*4882a593Smuzhiyun              * received one event past ButtonPress. */
1433*4882a593Smuzhiyun             if (deliveries && ev->any.type != ET_TouchBegin &&
1434*4882a593Smuzhiyun                 !(ev->device_event.flags & TOUCH_CLIENT_ID))
1435*4882a593Smuzhiyun                 TouchListenerAcceptReject(dev, ti, 0, XIAcceptTouch);
1436*4882a593Smuzhiyun 
1437*4882a593Smuzhiyun             if (ev->any.type == ET_TouchEnd &&
1438*4882a593Smuzhiyun                 ti->num_listeners == 1 &&
1439*4882a593Smuzhiyun                 !dev->button->buttonsDown &&
1440*4882a593Smuzhiyun                 dev->deviceGrab.fromPassiveGrab && GrabIsPointerGrab(grab)) {
1441*4882a593Smuzhiyun                 (*dev->deviceGrab.DeactivateGrab) (dev);
1442*4882a593Smuzhiyun                 CheckOldestTouch(dev);
1443*4882a593Smuzhiyun                 return Success;
1444*4882a593Smuzhiyun             }
1445*4882a593Smuzhiyun         }
1446*4882a593Smuzhiyun     }
1447*4882a593Smuzhiyun     else {
1448*4882a593Smuzhiyun         GrabPtr devgrab = dev->deviceGrab.grab;
1449*4882a593Smuzhiyun         WindowPtr sprite_win = DeepestSpriteWin(dev->spriteInfo->sprite);
1450*4882a593Smuzhiyun 
1451*4882a593Smuzhiyun         DeliverDeviceEvents(sprite_win, ptrev, grab, win, dev);
1452*4882a593Smuzhiyun         /* FIXME: bad hack
1453*4882a593Smuzhiyun          * Implicit passive grab activated in response to this event. Store
1454*4882a593Smuzhiyun          * the event.
1455*4882a593Smuzhiyun          */
1456*4882a593Smuzhiyun         if (!devgrab && dev->deviceGrab.grab && dev->deviceGrab.implicitGrab) {
1457*4882a593Smuzhiyun             TouchListener *l;
1458*4882a593Smuzhiyun             GrabPtr g;
1459*4882a593Smuzhiyun 
1460*4882a593Smuzhiyun             devgrab = dev->deviceGrab.grab;
1461*4882a593Smuzhiyun             g = AllocGrab(devgrab);
1462*4882a593Smuzhiyun             BUG_WARN(!g);
1463*4882a593Smuzhiyun 
1464*4882a593Smuzhiyun             *dev->deviceGrab.sync.event = ev->device_event;
1465*4882a593Smuzhiyun 
1466*4882a593Smuzhiyun             /* The listener array has a sequence of grabs and then one event
1467*4882a593Smuzhiyun              * selection. Implicit grab activation occurs through delivering an
1468*4882a593Smuzhiyun              * event selection. Thus, we update the last listener in the array.
1469*4882a593Smuzhiyun              */
1470*4882a593Smuzhiyun             l = &ti->listeners[ti->num_listeners - 1];
1471*4882a593Smuzhiyun             l->listener = g->resource;
1472*4882a593Smuzhiyun             l->grab = g;
1473*4882a593Smuzhiyun             //l->resource_type = RT_NONE;
1474*4882a593Smuzhiyun 
1475*4882a593Smuzhiyun             if (devgrab->grabtype != XI2 || devgrab->type != XI_TouchBegin)
1476*4882a593Smuzhiyun                 l->type = LISTENER_POINTER_GRAB;
1477*4882a593Smuzhiyun             else
1478*4882a593Smuzhiyun                 l->type = LISTENER_GRAB;
1479*4882a593Smuzhiyun         }
1480*4882a593Smuzhiyun 
1481*4882a593Smuzhiyun     }
1482*4882a593Smuzhiyun     if (ev->any.type == ET_TouchBegin)
1483*4882a593Smuzhiyun         listener->state = LISTENER_IS_OWNER;
1484*4882a593Smuzhiyun     else if (ev->any.type == ET_TouchEnd)
1485*4882a593Smuzhiyun         listener->state = LISTENER_HAS_END;
1486*4882a593Smuzhiyun 
1487*4882a593Smuzhiyun     return Success;
1488*4882a593Smuzhiyun }
1489*4882a593Smuzhiyun 
1490*4882a593Smuzhiyun static void
DeliverEmulatedMotionEvent(DeviceIntPtr dev,TouchPointInfoPtr ti,InternalEvent * ev)1491*4882a593Smuzhiyun DeliverEmulatedMotionEvent(DeviceIntPtr dev, TouchPointInfoPtr ti,
1492*4882a593Smuzhiyun                            InternalEvent *ev)
1493*4882a593Smuzhiyun {
1494*4882a593Smuzhiyun     DeviceEvent motion;
1495*4882a593Smuzhiyun 
1496*4882a593Smuzhiyun     if (ti->num_listeners) {
1497*4882a593Smuzhiyun         ClientPtr client;
1498*4882a593Smuzhiyun         WindowPtr win;
1499*4882a593Smuzhiyun         GrabPtr grab;
1500*4882a593Smuzhiyun         XI2Mask *mask;
1501*4882a593Smuzhiyun 
1502*4882a593Smuzhiyun         if (ti->listeners[0].type != LISTENER_POINTER_REGULAR &&
1503*4882a593Smuzhiyun             ti->listeners[0].type != LISTENER_POINTER_GRAB)
1504*4882a593Smuzhiyun             return;
1505*4882a593Smuzhiyun 
1506*4882a593Smuzhiyun         motion = ev->device_event;
1507*4882a593Smuzhiyun         motion.type = ET_TouchUpdate;
1508*4882a593Smuzhiyun         motion.detail.button = 0;
1509*4882a593Smuzhiyun 
1510*4882a593Smuzhiyun         if (!RetrieveTouchDeliveryData(dev, ti, (InternalEvent*)&motion,
1511*4882a593Smuzhiyun                                        &ti->listeners[0], &client, &win, &grab,
1512*4882a593Smuzhiyun                                        &mask))
1513*4882a593Smuzhiyun             return;
1514*4882a593Smuzhiyun 
1515*4882a593Smuzhiyun         DeliverTouchEmulatedEvent(dev, ti, (InternalEvent*)&motion, &ti->listeners[0], client,
1516*4882a593Smuzhiyun                                   win, grab, mask);
1517*4882a593Smuzhiyun     }
1518*4882a593Smuzhiyun     else {
1519*4882a593Smuzhiyun         InternalEvent button;
1520*4882a593Smuzhiyun         int converted;
1521*4882a593Smuzhiyun 
1522*4882a593Smuzhiyun         converted = TouchConvertToPointerEvent(ev, (InternalEvent*)&motion, &button);
1523*4882a593Smuzhiyun 
1524*4882a593Smuzhiyun         BUG_WARN(converted == 0);
1525*4882a593Smuzhiyun         if (converted)
1526*4882a593Smuzhiyun             ProcessOtherEvent((InternalEvent*)&motion, dev);
1527*4882a593Smuzhiyun     }
1528*4882a593Smuzhiyun }
1529*4882a593Smuzhiyun 
1530*4882a593Smuzhiyun /**
1531*4882a593Smuzhiyun  * Processes and delivers a TouchBegin, TouchUpdate, or a
1532*4882a593Smuzhiyun  * TouchEnd event.
1533*4882a593Smuzhiyun  *
1534*4882a593Smuzhiyun  * Due to having rather different delivery semantics (see the Xi 2.2 protocol
1535*4882a593Smuzhiyun  * spec for more information), this implements its own grab and event-selection
1536*4882a593Smuzhiyun  * delivery logic.
1537*4882a593Smuzhiyun  */
1538*4882a593Smuzhiyun static void
ProcessTouchEvent(InternalEvent * ev,DeviceIntPtr dev)1539*4882a593Smuzhiyun ProcessTouchEvent(InternalEvent *ev, DeviceIntPtr dev)
1540*4882a593Smuzhiyun {
1541*4882a593Smuzhiyun     TouchClassPtr t = dev->touch;
1542*4882a593Smuzhiyun     TouchPointInfoPtr ti;
1543*4882a593Smuzhiyun     uint32_t touchid;
1544*4882a593Smuzhiyun     int type = ev->any.type;
1545*4882a593Smuzhiyun     int emulate_pointer = ! !(ev->device_event.flags & TOUCH_POINTER_EMULATED);
1546*4882a593Smuzhiyun     DeviceIntPtr kbd;
1547*4882a593Smuzhiyun 
1548*4882a593Smuzhiyun     if (!t)
1549*4882a593Smuzhiyun         return;
1550*4882a593Smuzhiyun 
1551*4882a593Smuzhiyun     touchid = ev->device_event.touchid;
1552*4882a593Smuzhiyun 
1553*4882a593Smuzhiyun     if (type == ET_TouchBegin && !(ev->device_event.flags & TOUCH_REPLAYING)) {
1554*4882a593Smuzhiyun         ti = TouchBeginTouch(dev, ev->device_event.sourceid, touchid,
1555*4882a593Smuzhiyun                              emulate_pointer);
1556*4882a593Smuzhiyun     }
1557*4882a593Smuzhiyun     else
1558*4882a593Smuzhiyun         ti = TouchFindByClientID(dev, touchid);
1559*4882a593Smuzhiyun 
1560*4882a593Smuzhiyun     /* Active pointer grab */
1561*4882a593Smuzhiyun     if (emulate_pointer && dev->deviceGrab.grab && !dev->deviceGrab.fromPassiveGrab &&
1562*4882a593Smuzhiyun         (dev->deviceGrab.grab->grabtype == CORE ||
1563*4882a593Smuzhiyun          dev->deviceGrab.grab->grabtype == XI ||
1564*4882a593Smuzhiyun          !xi2mask_isset(dev->deviceGrab.grab->xi2mask, dev, XI_TouchBegin)))
1565*4882a593Smuzhiyun     {
1566*4882a593Smuzhiyun         /* Active pointer grab on touch point and we get a TouchEnd - claim this
1567*4882a593Smuzhiyun          * touchpoint accepted, otherwise clients waiting for ownership will
1568*4882a593Smuzhiyun          * wait on this touchpoint until this client ungrabs, or the cows come
1569*4882a593Smuzhiyun          * home, whichever is earlier */
1570*4882a593Smuzhiyun         if (ti && type == ET_TouchEnd)
1571*4882a593Smuzhiyun             TouchListenerAcceptReject(dev, ti, 0, XIAcceptTouch);
1572*4882a593Smuzhiyun         else if (!ti && type != ET_TouchBegin) {
1573*4882a593Smuzhiyun             /* Under the following circumstances we create a new touch record for an
1574*4882a593Smuzhiyun              * existing touch:
1575*4882a593Smuzhiyun              *
1576*4882a593Smuzhiyun              * - The touch may be pointer emulated
1577*4882a593Smuzhiyun              * - An explicit grab is active on the device
1578*4882a593Smuzhiyun              * - The grab is a pointer grab
1579*4882a593Smuzhiyun              *
1580*4882a593Smuzhiyun              * This allows for an explicit grab to receive pointer events for an already
1581*4882a593Smuzhiyun              * active touch.
1582*4882a593Smuzhiyun              */
1583*4882a593Smuzhiyun             ti = TouchBeginTouch(dev, ev->device_event.sourceid, touchid,
1584*4882a593Smuzhiyun                                  emulate_pointer);
1585*4882a593Smuzhiyun             if (!ti) {
1586*4882a593Smuzhiyun                 DebugF("[Xi] %s: Failed to create new dix record for explicitly "
1587*4882a593Smuzhiyun                        "grabbed touchpoint %d\n",
1588*4882a593Smuzhiyun                        dev->name, touchid);
1589*4882a593Smuzhiyun                 return;
1590*4882a593Smuzhiyun             }
1591*4882a593Smuzhiyun 
1592*4882a593Smuzhiyun             TouchBuildSprite(dev, ti, ev);
1593*4882a593Smuzhiyun             TouchSetupListeners(dev, ti, ev);
1594*4882a593Smuzhiyun         }
1595*4882a593Smuzhiyun     }
1596*4882a593Smuzhiyun 
1597*4882a593Smuzhiyun     if (!ti) {
1598*4882a593Smuzhiyun         DebugF("[Xi] %s: Failed to get event %d for touchpoint %d\n",
1599*4882a593Smuzhiyun                dev->name, type, touchid);
1600*4882a593Smuzhiyun         goto out;
1601*4882a593Smuzhiyun     }
1602*4882a593Smuzhiyun 
1603*4882a593Smuzhiyun     /* if emulate_pointer is set, emulate the motion event right
1604*4882a593Smuzhiyun      * here, so we can ignore it for button event emulation. TouchUpdate
1605*4882a593Smuzhiyun      * events which _only_ emulate motion just work normally */
1606*4882a593Smuzhiyun     if (emulate_pointer && ev->any.type != ET_TouchUpdate)
1607*4882a593Smuzhiyun         DeliverEmulatedMotionEvent(dev, ti, ev);
1608*4882a593Smuzhiyun 
1609*4882a593Smuzhiyun     if (emulate_pointer && IsMaster(dev))
1610*4882a593Smuzhiyun         CheckMotion(&ev->device_event, dev);
1611*4882a593Smuzhiyun 
1612*4882a593Smuzhiyun     kbd = GetMaster(dev, KEYBOARD_OR_FLOAT);
1613*4882a593Smuzhiyun     event_set_state(NULL, kbd, &ev->device_event);
1614*4882a593Smuzhiyun     ev->device_event.corestate = event_get_corestate(NULL, kbd);
1615*4882a593Smuzhiyun 
1616*4882a593Smuzhiyun     /* Make sure we have a valid window trace for event delivery; must be
1617*4882a593Smuzhiyun      * called after event type mutation. Touch end events are always processed
1618*4882a593Smuzhiyun      * in order to end touch records. */
1619*4882a593Smuzhiyun     /* FIXME: check this */
1620*4882a593Smuzhiyun     if ((type == ET_TouchBegin &&
1621*4882a593Smuzhiyun          !(ev->device_event.flags & TOUCH_REPLAYING) &&
1622*4882a593Smuzhiyun          !TouchBuildSprite(dev, ti, ev)) ||
1623*4882a593Smuzhiyun         (type != ET_TouchEnd && ti->sprite.spriteTraceGood == 0))
1624*4882a593Smuzhiyun         return;
1625*4882a593Smuzhiyun 
1626*4882a593Smuzhiyun     TouchCopyValuatorData(&ev->device_event, ti);
1627*4882a593Smuzhiyun     /* WARNING: the event type may change to TouchUpdate in
1628*4882a593Smuzhiyun      * DeliverTouchEvents if a TouchEnd was delivered to a grabbing
1629*4882a593Smuzhiyun      * owner */
1630*4882a593Smuzhiyun     DeliverTouchEvents(dev, ti, ev, ev->device_event.resource);
1631*4882a593Smuzhiyun     if (ev->any.type == ET_TouchEnd)
1632*4882a593Smuzhiyun         TouchEndTouch(dev, ti);
1633*4882a593Smuzhiyun 
1634*4882a593Smuzhiyun  out:
1635*4882a593Smuzhiyun     if (emulate_pointer)
1636*4882a593Smuzhiyun         UpdateDeviceState(dev, &ev->device_event);
1637*4882a593Smuzhiyun }
1638*4882a593Smuzhiyun 
1639*4882a593Smuzhiyun static void
ProcessBarrierEvent(InternalEvent * e,DeviceIntPtr dev)1640*4882a593Smuzhiyun ProcessBarrierEvent(InternalEvent *e, DeviceIntPtr dev)
1641*4882a593Smuzhiyun {
1642*4882a593Smuzhiyun     Mask filter;
1643*4882a593Smuzhiyun     WindowPtr pWin;
1644*4882a593Smuzhiyun     BarrierEvent *be = &e->barrier_event;
1645*4882a593Smuzhiyun     xEvent *ev;
1646*4882a593Smuzhiyun     int rc;
1647*4882a593Smuzhiyun     GrabPtr grab = dev->deviceGrab.grab;
1648*4882a593Smuzhiyun 
1649*4882a593Smuzhiyun     if (!IsMaster(dev))
1650*4882a593Smuzhiyun         return;
1651*4882a593Smuzhiyun 
1652*4882a593Smuzhiyun     if (dixLookupWindow(&pWin, be->window, serverClient, DixReadAccess) != Success)
1653*4882a593Smuzhiyun         return;
1654*4882a593Smuzhiyun 
1655*4882a593Smuzhiyun     if (grab)
1656*4882a593Smuzhiyun         be->flags |= XIBarrierDeviceIsGrabbed;
1657*4882a593Smuzhiyun 
1658*4882a593Smuzhiyun     rc = EventToXI2(e, &ev);
1659*4882a593Smuzhiyun     if (rc != Success) {
1660*4882a593Smuzhiyun         ErrorF("[Xi] event conversion from %s failed with code %d\n", __func__, rc);
1661*4882a593Smuzhiyun         return;
1662*4882a593Smuzhiyun     }
1663*4882a593Smuzhiyun 
1664*4882a593Smuzhiyun     /* A client has a grab, deliver to this client if the grab_window is the
1665*4882a593Smuzhiyun        barrier window.
1666*4882a593Smuzhiyun 
1667*4882a593Smuzhiyun        Otherwise, deliver normally to the client.
1668*4882a593Smuzhiyun      */
1669*4882a593Smuzhiyun     if (grab &&
1670*4882a593Smuzhiyun         CLIENT_ID(be->barrierid) == CLIENT_ID(grab->resource) &&
1671*4882a593Smuzhiyun         grab->window->drawable.id == be->window) {
1672*4882a593Smuzhiyun         DeliverGrabbedEvent(e, dev, FALSE);
1673*4882a593Smuzhiyun     } else {
1674*4882a593Smuzhiyun         filter = GetEventFilter(dev, ev);
1675*4882a593Smuzhiyun 
1676*4882a593Smuzhiyun         DeliverEventsToWindow(dev, pWin, ev, 1,
1677*4882a593Smuzhiyun                               filter, NullGrab);
1678*4882a593Smuzhiyun     }
1679*4882a593Smuzhiyun     free(ev);
1680*4882a593Smuzhiyun }
1681*4882a593Smuzhiyun 
1682*4882a593Smuzhiyun /**
1683*4882a593Smuzhiyun  * Process DeviceEvents and DeviceChangedEvents.
1684*4882a593Smuzhiyun  */
1685*4882a593Smuzhiyun static void
ProcessDeviceEvent(InternalEvent * ev,DeviceIntPtr device)1686*4882a593Smuzhiyun ProcessDeviceEvent(InternalEvent *ev, DeviceIntPtr device)
1687*4882a593Smuzhiyun {
1688*4882a593Smuzhiyun     GrabPtr grab;
1689*4882a593Smuzhiyun     Bool deactivateDeviceGrab = FALSE;
1690*4882a593Smuzhiyun     int key = 0, rootX, rootY;
1691*4882a593Smuzhiyun     ButtonClassPtr b;
1692*4882a593Smuzhiyun     int ret = 0;
1693*4882a593Smuzhiyun     int corestate;
1694*4882a593Smuzhiyun     DeviceIntPtr mouse = NULL, kbd = NULL;
1695*4882a593Smuzhiyun     DeviceEvent *event = &ev->device_event;
1696*4882a593Smuzhiyun 
1697*4882a593Smuzhiyun     if (IsPointerDevice(device)) {
1698*4882a593Smuzhiyun         kbd = GetMaster(device, KEYBOARD_OR_FLOAT);
1699*4882a593Smuzhiyun         mouse = device;
1700*4882a593Smuzhiyun         if (!kbd->key)          /* can happen with floating SDs */
1701*4882a593Smuzhiyun             kbd = NULL;
1702*4882a593Smuzhiyun     }
1703*4882a593Smuzhiyun     else {
1704*4882a593Smuzhiyun         mouse = GetMaster(device, POINTER_OR_FLOAT);
1705*4882a593Smuzhiyun         kbd = device;
1706*4882a593Smuzhiyun         if (!mouse->valuator || !mouse->button) /* may be float. SDs */
1707*4882a593Smuzhiyun             mouse = NULL;
1708*4882a593Smuzhiyun     }
1709*4882a593Smuzhiyun 
1710*4882a593Smuzhiyun     corestate = event_get_corestate(mouse, kbd);
1711*4882a593Smuzhiyun     event_set_state(mouse, kbd, event);
1712*4882a593Smuzhiyun 
1713*4882a593Smuzhiyun     ret = UpdateDeviceState(device, event);
1714*4882a593Smuzhiyun     if (ret == DONT_PROCESS)
1715*4882a593Smuzhiyun         return;
1716*4882a593Smuzhiyun 
1717*4882a593Smuzhiyun     b = device->button;
1718*4882a593Smuzhiyun 
1719*4882a593Smuzhiyun     if (IsMaster(device) || IsFloating(device))
1720*4882a593Smuzhiyun         CheckMotion(event, device);
1721*4882a593Smuzhiyun 
1722*4882a593Smuzhiyun     switch (event->type) {
1723*4882a593Smuzhiyun     case ET_Motion:
1724*4882a593Smuzhiyun     case ET_ButtonPress:
1725*4882a593Smuzhiyun     case ET_ButtonRelease:
1726*4882a593Smuzhiyun     case ET_KeyPress:
1727*4882a593Smuzhiyun     case ET_KeyRelease:
1728*4882a593Smuzhiyun     case ET_ProximityIn:
1729*4882a593Smuzhiyun     case ET_ProximityOut:
1730*4882a593Smuzhiyun         GetSpritePosition(device, &rootX, &rootY);
1731*4882a593Smuzhiyun         event->root_x = rootX;
1732*4882a593Smuzhiyun         event->root_y = rootY;
1733*4882a593Smuzhiyun         NoticeEventTime((InternalEvent *) event, device);
1734*4882a593Smuzhiyun         event->corestate = corestate;
1735*4882a593Smuzhiyun         key = event->detail.key;
1736*4882a593Smuzhiyun         break;
1737*4882a593Smuzhiyun     default:
1738*4882a593Smuzhiyun         break;
1739*4882a593Smuzhiyun     }
1740*4882a593Smuzhiyun 
1741*4882a593Smuzhiyun     /* send KeyPress and KeyRelease events to XACE plugins */
1742*4882a593Smuzhiyun     if (XaceHookIsSet(XACE_KEY_AVAIL) &&
1743*4882a593Smuzhiyun             (event->type == ET_KeyPress || event->type == ET_KeyRelease)) {
1744*4882a593Smuzhiyun         xEvent *core;
1745*4882a593Smuzhiyun         int count;
1746*4882a593Smuzhiyun 
1747*4882a593Smuzhiyun         if (EventToCore(ev, &core, &count) == Success && count > 0) {
1748*4882a593Smuzhiyun             XaceHook(XACE_KEY_AVAIL, core, device, 0);
1749*4882a593Smuzhiyun             free(core);
1750*4882a593Smuzhiyun         }
1751*4882a593Smuzhiyun     }
1752*4882a593Smuzhiyun 
1753*4882a593Smuzhiyun     if (DeviceEventCallback && !syncEvents.playingEvents) {
1754*4882a593Smuzhiyun         DeviceEventInfoRec eventinfo;
1755*4882a593Smuzhiyun         SpritePtr pSprite = device->spriteInfo->sprite;
1756*4882a593Smuzhiyun 
1757*4882a593Smuzhiyun         /* see comment in EnqueueEvents regarding the next three lines */
1758*4882a593Smuzhiyun         if (ev->any.type == ET_Motion)
1759*4882a593Smuzhiyun             ev->device_event.root = pSprite->hotPhys.pScreen->root->drawable.id;
1760*4882a593Smuzhiyun 
1761*4882a593Smuzhiyun         eventinfo.device = device;
1762*4882a593Smuzhiyun         eventinfo.event = ev;
1763*4882a593Smuzhiyun         CallCallbacks(&DeviceEventCallback, (void *) &eventinfo);
1764*4882a593Smuzhiyun     }
1765*4882a593Smuzhiyun 
1766*4882a593Smuzhiyun     grab = device->deviceGrab.grab;
1767*4882a593Smuzhiyun 
1768*4882a593Smuzhiyun     switch (event->type) {
1769*4882a593Smuzhiyun     case ET_KeyPress:
1770*4882a593Smuzhiyun         /* Don't deliver focus events (e.g. from KeymapNotify when running
1771*4882a593Smuzhiyun          * nested) to clients. */
1772*4882a593Smuzhiyun         if (event->source_type == EVENT_SOURCE_FOCUS)
1773*4882a593Smuzhiyun             return;
1774*4882a593Smuzhiyun         if (!grab && CheckDeviceGrabs(device, event, 0))
1775*4882a593Smuzhiyun             return;
1776*4882a593Smuzhiyun         break;
1777*4882a593Smuzhiyun     case ET_KeyRelease:
1778*4882a593Smuzhiyun         if (grab && device->deviceGrab.fromPassiveGrab &&
1779*4882a593Smuzhiyun             (key == device->deviceGrab.activatingKey) &&
1780*4882a593Smuzhiyun             GrabIsKeyboardGrab(device->deviceGrab.grab))
1781*4882a593Smuzhiyun             deactivateDeviceGrab = TRUE;
1782*4882a593Smuzhiyun         break;
1783*4882a593Smuzhiyun     case ET_ButtonPress:
1784*4882a593Smuzhiyun         if (b->map[key] == 0)   /* there's no button 0 */
1785*4882a593Smuzhiyun             return;
1786*4882a593Smuzhiyun         event->detail.button = b->map[key];
1787*4882a593Smuzhiyun         if (!grab && CheckDeviceGrabs(device, event, 0)) {
1788*4882a593Smuzhiyun             /* if a passive grab was activated, the event has been sent
1789*4882a593Smuzhiyun              * already */
1790*4882a593Smuzhiyun             return;
1791*4882a593Smuzhiyun         }
1792*4882a593Smuzhiyun         break;
1793*4882a593Smuzhiyun     case ET_ButtonRelease:
1794*4882a593Smuzhiyun         if (b->map[key] == 0)   /* there's no button 0 */
1795*4882a593Smuzhiyun             return;
1796*4882a593Smuzhiyun         event->detail.button = b->map[key];
1797*4882a593Smuzhiyun         if (grab && !b->buttonsDown &&
1798*4882a593Smuzhiyun             device->deviceGrab.fromPassiveGrab &&
1799*4882a593Smuzhiyun             GrabIsPointerGrab(device->deviceGrab.grab))
1800*4882a593Smuzhiyun             deactivateDeviceGrab = TRUE;
1801*4882a593Smuzhiyun     default:
1802*4882a593Smuzhiyun         break;
1803*4882a593Smuzhiyun     }
1804*4882a593Smuzhiyun 
1805*4882a593Smuzhiyun     /* Don't deliver focus events (e.g. from KeymapNotify when running
1806*4882a593Smuzhiyun      * nested) to clients. */
1807*4882a593Smuzhiyun     if (event->source_type != EVENT_SOURCE_FOCUS) {
1808*4882a593Smuzhiyun         if (grab)
1809*4882a593Smuzhiyun             DeliverGrabbedEvent((InternalEvent *) event, device,
1810*4882a593Smuzhiyun                                 deactivateDeviceGrab);
1811*4882a593Smuzhiyun         else if (device->focus && !IsPointerEvent(ev))
1812*4882a593Smuzhiyun             DeliverFocusedEvent(device, (InternalEvent *) event,
1813*4882a593Smuzhiyun                                 GetSpriteWindow(device));
1814*4882a593Smuzhiyun         else
1815*4882a593Smuzhiyun             DeliverDeviceEvents(GetSpriteWindow(device), (InternalEvent *) event,
1816*4882a593Smuzhiyun                                 NullGrab, NullWindow, device);
1817*4882a593Smuzhiyun     }
1818*4882a593Smuzhiyun 
1819*4882a593Smuzhiyun     if (deactivateDeviceGrab == TRUE) {
1820*4882a593Smuzhiyun         (*device->deviceGrab.DeactivateGrab) (device);
1821*4882a593Smuzhiyun 
1822*4882a593Smuzhiyun         if (!IsMaster (device) && !IsFloating (device)) {
1823*4882a593Smuzhiyun             int flags, num_events = 0;
1824*4882a593Smuzhiyun             InternalEvent dce;
1825*4882a593Smuzhiyun 
1826*4882a593Smuzhiyun             flags = (IsPointerDevice (device)) ?
1827*4882a593Smuzhiyun                 DEVCHANGE_POINTER_EVENT : DEVCHANGE_KEYBOARD_EVENT;
1828*4882a593Smuzhiyun             UpdateFromMaster (&dce, device, flags, &num_events);
1829*4882a593Smuzhiyun             BUG_WARN(num_events > 1);
1830*4882a593Smuzhiyun 
1831*4882a593Smuzhiyun             if (num_events == 1)
1832*4882a593Smuzhiyun                 ChangeMasterDeviceClasses(GetMaster (device, MASTER_ATTACHED),
1833*4882a593Smuzhiyun                                           &dce.changed_event);
1834*4882a593Smuzhiyun         }
1835*4882a593Smuzhiyun 
1836*4882a593Smuzhiyun     }
1837*4882a593Smuzhiyun 
1838*4882a593Smuzhiyun     event->detail.key = key;
1839*4882a593Smuzhiyun }
1840*4882a593Smuzhiyun 
1841*4882a593Smuzhiyun /**
1842*4882a593Smuzhiyun  * Main device event processing function.
1843*4882a593Smuzhiyun  * Called from when processing the events from the event queue.
1844*4882a593Smuzhiyun  *
1845*4882a593Smuzhiyun  */
1846*4882a593Smuzhiyun void
ProcessOtherEvent(InternalEvent * ev,DeviceIntPtr device)1847*4882a593Smuzhiyun ProcessOtherEvent(InternalEvent *ev, DeviceIntPtr device)
1848*4882a593Smuzhiyun {
1849*4882a593Smuzhiyun     verify_internal_event(ev);
1850*4882a593Smuzhiyun 
1851*4882a593Smuzhiyun     switch (ev->any.type) {
1852*4882a593Smuzhiyun     case ET_RawKeyPress:
1853*4882a593Smuzhiyun     case ET_RawKeyRelease:
1854*4882a593Smuzhiyun     case ET_RawButtonPress:
1855*4882a593Smuzhiyun     case ET_RawButtonRelease:
1856*4882a593Smuzhiyun     case ET_RawMotion:
1857*4882a593Smuzhiyun     case ET_RawTouchBegin:
1858*4882a593Smuzhiyun     case ET_RawTouchUpdate:
1859*4882a593Smuzhiyun     case ET_RawTouchEnd:
1860*4882a593Smuzhiyun         DeliverRawEvent(&ev->raw_event, device);
1861*4882a593Smuzhiyun         break;
1862*4882a593Smuzhiyun     case ET_TouchBegin:
1863*4882a593Smuzhiyun     case ET_TouchUpdate:
1864*4882a593Smuzhiyun     case ET_TouchEnd:
1865*4882a593Smuzhiyun         ProcessTouchEvent(ev, device);
1866*4882a593Smuzhiyun         break;
1867*4882a593Smuzhiyun     case ET_TouchOwnership:
1868*4882a593Smuzhiyun         /* TouchOwnership events are handled separately from the rest, as they
1869*4882a593Smuzhiyun          * have more complex semantics. */
1870*4882a593Smuzhiyun         ProcessTouchOwnershipEvent(&ev->touch_ownership_event, device);
1871*4882a593Smuzhiyun         break;
1872*4882a593Smuzhiyun     case ET_BarrierHit:
1873*4882a593Smuzhiyun     case ET_BarrierLeave:
1874*4882a593Smuzhiyun         ProcessBarrierEvent(ev, device);
1875*4882a593Smuzhiyun         break;
1876*4882a593Smuzhiyun     default:
1877*4882a593Smuzhiyun         ProcessDeviceEvent(ev, device);
1878*4882a593Smuzhiyun         break;
1879*4882a593Smuzhiyun     }
1880*4882a593Smuzhiyun }
1881*4882a593Smuzhiyun 
1882*4882a593Smuzhiyun static int
DeliverTouchBeginEvent(DeviceIntPtr dev,TouchPointInfoPtr ti,InternalEvent * ev,TouchListener * listener,ClientPtr client,WindowPtr win,GrabPtr grab,XI2Mask * xi2mask)1883*4882a593Smuzhiyun DeliverTouchBeginEvent(DeviceIntPtr dev, TouchPointInfoPtr ti,
1884*4882a593Smuzhiyun                        InternalEvent *ev, TouchListener * listener,
1885*4882a593Smuzhiyun                        ClientPtr client, WindowPtr win, GrabPtr grab,
1886*4882a593Smuzhiyun                        XI2Mask *xi2mask)
1887*4882a593Smuzhiyun {
1888*4882a593Smuzhiyun     enum TouchListenerState state;
1889*4882a593Smuzhiyun     int rc = Success;
1890*4882a593Smuzhiyun     Bool has_ownershipmask;
1891*4882a593Smuzhiyun 
1892*4882a593Smuzhiyun     if (listener->type == LISTENER_POINTER_REGULAR ||
1893*4882a593Smuzhiyun         listener->type == LISTENER_POINTER_GRAB) {
1894*4882a593Smuzhiyun         rc = DeliverTouchEmulatedEvent(dev, ti, ev, listener, client, win,
1895*4882a593Smuzhiyun                                        grab, xi2mask);
1896*4882a593Smuzhiyun         if (rc == Success) {
1897*4882a593Smuzhiyun             listener->state = LISTENER_IS_OWNER;
1898*4882a593Smuzhiyun             /* async grabs cannot replay, so automatically accept this touch */
1899*4882a593Smuzhiyun             if (listener->type == LISTENER_POINTER_GRAB &&
1900*4882a593Smuzhiyun                 dev->deviceGrab.grab &&
1901*4882a593Smuzhiyun                 dev->deviceGrab.fromPassiveGrab &&
1902*4882a593Smuzhiyun                 dev->deviceGrab.grab->pointerMode == GrabModeAsync)
1903*4882a593Smuzhiyun                 ActivateEarlyAccept(dev, ti);
1904*4882a593Smuzhiyun         }
1905*4882a593Smuzhiyun         goto out;
1906*4882a593Smuzhiyun     }
1907*4882a593Smuzhiyun 
1908*4882a593Smuzhiyun     has_ownershipmask = xi2mask_isset(xi2mask, dev, XI_TouchOwnership);
1909*4882a593Smuzhiyun 
1910*4882a593Smuzhiyun     if (TouchResourceIsOwner(ti, listener->listener) || has_ownershipmask)
1911*4882a593Smuzhiyun         rc = DeliverOneTouchEvent(client, dev, ti, grab, win, ev);
1912*4882a593Smuzhiyun     if (!TouchResourceIsOwner(ti, listener->listener)) {
1913*4882a593Smuzhiyun         if (has_ownershipmask)
1914*4882a593Smuzhiyun             state = LISTENER_AWAITING_OWNER;
1915*4882a593Smuzhiyun         else
1916*4882a593Smuzhiyun             state = LISTENER_AWAITING_BEGIN;
1917*4882a593Smuzhiyun     }
1918*4882a593Smuzhiyun     else {
1919*4882a593Smuzhiyun         if (has_ownershipmask)
1920*4882a593Smuzhiyun             TouchSendOwnershipEvent(dev, ti, 0, listener->listener);
1921*4882a593Smuzhiyun 
1922*4882a593Smuzhiyun         if (listener->type == LISTENER_REGULAR)
1923*4882a593Smuzhiyun             state = LISTENER_HAS_ACCEPTED;
1924*4882a593Smuzhiyun         else
1925*4882a593Smuzhiyun             state = LISTENER_IS_OWNER;
1926*4882a593Smuzhiyun     }
1927*4882a593Smuzhiyun     listener->state = state;
1928*4882a593Smuzhiyun 
1929*4882a593Smuzhiyun  out:
1930*4882a593Smuzhiyun     return rc;
1931*4882a593Smuzhiyun }
1932*4882a593Smuzhiyun 
1933*4882a593Smuzhiyun static int
DeliverTouchEndEvent(DeviceIntPtr dev,TouchPointInfoPtr ti,InternalEvent * ev,TouchListener * listener,ClientPtr client,WindowPtr win,GrabPtr grab,XI2Mask * xi2mask)1934*4882a593Smuzhiyun DeliverTouchEndEvent(DeviceIntPtr dev, TouchPointInfoPtr ti, InternalEvent *ev,
1935*4882a593Smuzhiyun                      TouchListener * listener, ClientPtr client,
1936*4882a593Smuzhiyun                      WindowPtr win, GrabPtr grab, XI2Mask *xi2mask)
1937*4882a593Smuzhiyun {
1938*4882a593Smuzhiyun     int rc = Success;
1939*4882a593Smuzhiyun 
1940*4882a593Smuzhiyun     if (listener->type == LISTENER_POINTER_REGULAR ||
1941*4882a593Smuzhiyun         listener->type == LISTENER_POINTER_GRAB) {
1942*4882a593Smuzhiyun         /* Note: If the active grab was ungrabbed, we already changed the
1943*4882a593Smuzhiyun          * state to LISTENER_HAS_END but still get here. So we mustn't
1944*4882a593Smuzhiyun          * actually send the event.
1945*4882a593Smuzhiyun          * This is part two of the hack in DeactivatePointerGrab
1946*4882a593Smuzhiyun          */
1947*4882a593Smuzhiyun         if (listener->state != LISTENER_HAS_END) {
1948*4882a593Smuzhiyun             rc = DeliverTouchEmulatedEvent(dev, ti, ev, listener, client, win,
1949*4882a593Smuzhiyun                                            grab, xi2mask);
1950*4882a593Smuzhiyun 
1951*4882a593Smuzhiyun              /* Once we send a TouchEnd to a legacy listener, we're already well
1952*4882a593Smuzhiyun               * past the accepting/rejecting stage (can only happen on
1953*4882a593Smuzhiyun               * GrabModeSync + replay. This listener now gets the end event,
1954*4882a593Smuzhiyun               * and we can continue.
1955*4882a593Smuzhiyun               */
1956*4882a593Smuzhiyun             if (rc == Success)
1957*4882a593Smuzhiyun                 listener->state = LISTENER_HAS_END;
1958*4882a593Smuzhiyun         }
1959*4882a593Smuzhiyun         goto out;
1960*4882a593Smuzhiyun     }
1961*4882a593Smuzhiyun 
1962*4882a593Smuzhiyun     /* A client is waiting for the begin, don't give it a TouchEnd */
1963*4882a593Smuzhiyun     if (listener->state == LISTENER_AWAITING_BEGIN) {
1964*4882a593Smuzhiyun         listener->state = LISTENER_HAS_END;
1965*4882a593Smuzhiyun         goto out;
1966*4882a593Smuzhiyun     }
1967*4882a593Smuzhiyun 
1968*4882a593Smuzhiyun     /* Event in response to reject */
1969*4882a593Smuzhiyun     if (ev->device_event.flags & TOUCH_REJECT ||
1970*4882a593Smuzhiyun         (ev->device_event.flags & TOUCH_ACCEPT && !TouchResourceIsOwner(ti, listener->listener))) {
1971*4882a593Smuzhiyun         /* Touch has been rejected, or accepted by its owner which is not this listener */
1972*4882a593Smuzhiyun         if (listener->state != LISTENER_HAS_END)
1973*4882a593Smuzhiyun             rc = DeliverOneTouchEvent(client, dev, ti, grab, win, ev);
1974*4882a593Smuzhiyun         listener->state = LISTENER_HAS_END;
1975*4882a593Smuzhiyun     }
1976*4882a593Smuzhiyun     else if (TouchResourceIsOwner(ti, listener->listener)) {
1977*4882a593Smuzhiyun         Bool normal_end = !(ev->device_event.flags & TOUCH_ACCEPT);
1978*4882a593Smuzhiyun 
1979*4882a593Smuzhiyun         /* FIXME: what about early acceptance */
1980*4882a593Smuzhiyun         if (normal_end && listener->state != LISTENER_HAS_END)
1981*4882a593Smuzhiyun             rc = DeliverOneTouchEvent(client, dev, ti, grab, win, ev);
1982*4882a593Smuzhiyun 
1983*4882a593Smuzhiyun         if ((ti->num_listeners > 1 ||
1984*4882a593Smuzhiyun              (ti->num_grabs > 0 && listener->state != LISTENER_HAS_ACCEPTED)) &&
1985*4882a593Smuzhiyun             (ev->device_event.flags & (TOUCH_ACCEPT | TOUCH_REJECT)) == 0) {
1986*4882a593Smuzhiyun             ev->any.type = ET_TouchUpdate;
1987*4882a593Smuzhiyun             ev->device_event.flags |= TOUCH_PENDING_END;
1988*4882a593Smuzhiyun             ti->pending_finish = TRUE;
1989*4882a593Smuzhiyun 
1990*4882a593Smuzhiyun             if (normal_end && listener->state != LISTENER_HAS_END)
1991*4882a593Smuzhiyun                 rc = DeliverOneTouchEvent(client, dev, ti, grab, win, ev);
1992*4882a593Smuzhiyun         }
1993*4882a593Smuzhiyun 
1994*4882a593Smuzhiyun         if (normal_end)
1995*4882a593Smuzhiyun             listener->state = LISTENER_HAS_END;
1996*4882a593Smuzhiyun     }
1997*4882a593Smuzhiyun 
1998*4882a593Smuzhiyun  out:
1999*4882a593Smuzhiyun     return rc;
2000*4882a593Smuzhiyun }
2001*4882a593Smuzhiyun 
2002*4882a593Smuzhiyun static int
DeliverTouchEvent(DeviceIntPtr dev,TouchPointInfoPtr ti,InternalEvent * ev,TouchListener * listener,ClientPtr client,WindowPtr win,GrabPtr grab,XI2Mask * xi2mask)2003*4882a593Smuzhiyun DeliverTouchEvent(DeviceIntPtr dev, TouchPointInfoPtr ti, InternalEvent *ev,
2004*4882a593Smuzhiyun                   TouchListener * listener, ClientPtr client,
2005*4882a593Smuzhiyun                   WindowPtr win, GrabPtr grab, XI2Mask *xi2mask)
2006*4882a593Smuzhiyun {
2007*4882a593Smuzhiyun     Bool has_ownershipmask = FALSE;
2008*4882a593Smuzhiyun     int rc = Success;
2009*4882a593Smuzhiyun 
2010*4882a593Smuzhiyun     if (xi2mask)
2011*4882a593Smuzhiyun         has_ownershipmask = xi2mask_isset(xi2mask, dev, XI_TouchOwnership);
2012*4882a593Smuzhiyun 
2013*4882a593Smuzhiyun     if (ev->any.type == ET_TouchOwnership) {
2014*4882a593Smuzhiyun         ev->touch_ownership_event.deviceid = dev->id;
2015*4882a593Smuzhiyun         if (!TouchResourceIsOwner(ti, listener->listener))
2016*4882a593Smuzhiyun             goto out;
2017*4882a593Smuzhiyun         rc = DeliverOneTouchEvent(client, dev, ti, grab, win, ev);
2018*4882a593Smuzhiyun         listener->state = LISTENER_IS_OWNER;
2019*4882a593Smuzhiyun     }
2020*4882a593Smuzhiyun     else
2021*4882a593Smuzhiyun         ev->device_event.deviceid = dev->id;
2022*4882a593Smuzhiyun 
2023*4882a593Smuzhiyun     if (ev->any.type == ET_TouchBegin) {
2024*4882a593Smuzhiyun         rc = DeliverTouchBeginEvent(dev, ti, ev, listener, client, win, grab,
2025*4882a593Smuzhiyun                                     xi2mask);
2026*4882a593Smuzhiyun     }
2027*4882a593Smuzhiyun     else if (ev->any.type == ET_TouchUpdate) {
2028*4882a593Smuzhiyun         if (listener->type == LISTENER_POINTER_REGULAR ||
2029*4882a593Smuzhiyun             listener->type == LISTENER_POINTER_GRAB)
2030*4882a593Smuzhiyun             DeliverTouchEmulatedEvent(dev, ti, ev, listener, client, win, grab,
2031*4882a593Smuzhiyun                                       xi2mask);
2032*4882a593Smuzhiyun         else if (TouchResourceIsOwner(ti, listener->listener) ||
2033*4882a593Smuzhiyun                  has_ownershipmask)
2034*4882a593Smuzhiyun             rc = DeliverOneTouchEvent(client, dev, ti, grab, win, ev);
2035*4882a593Smuzhiyun     }
2036*4882a593Smuzhiyun     else if (ev->any.type == ET_TouchEnd)
2037*4882a593Smuzhiyun         rc = DeliverTouchEndEvent(dev, ti, ev, listener, client, win, grab,
2038*4882a593Smuzhiyun                                   xi2mask);
2039*4882a593Smuzhiyun 
2040*4882a593Smuzhiyun  out:
2041*4882a593Smuzhiyun     return rc;
2042*4882a593Smuzhiyun }
2043*4882a593Smuzhiyun 
2044*4882a593Smuzhiyun /**
2045*4882a593Smuzhiyun  * Delivers a touch events to all interested clients.  For TouchBegin events,
2046*4882a593Smuzhiyun  * will update ti->listeners, ti->num_listeners, and ti->num_grabs.
2047*4882a593Smuzhiyun  * May also mutate ev (type and flags) upon successful delivery.  If
2048*4882a593Smuzhiyun  * @resource is non-zero, will only attempt delivery to the owner of that
2049*4882a593Smuzhiyun  * resource.
2050*4882a593Smuzhiyun  *
2051*4882a593Smuzhiyun  * @return TRUE if the event was delivered at least once, FALSE otherwise
2052*4882a593Smuzhiyun  */
2053*4882a593Smuzhiyun void
DeliverTouchEvents(DeviceIntPtr dev,TouchPointInfoPtr ti,InternalEvent * ev,XID resource)2054*4882a593Smuzhiyun DeliverTouchEvents(DeviceIntPtr dev, TouchPointInfoPtr ti,
2055*4882a593Smuzhiyun                    InternalEvent *ev, XID resource)
2056*4882a593Smuzhiyun {
2057*4882a593Smuzhiyun     int i;
2058*4882a593Smuzhiyun 
2059*4882a593Smuzhiyun     if (ev->any.type == ET_TouchBegin &&
2060*4882a593Smuzhiyun         !(ev->device_event.flags & (TOUCH_CLIENT_ID | TOUCH_REPLAYING)))
2061*4882a593Smuzhiyun         TouchSetupListeners(dev, ti, ev);
2062*4882a593Smuzhiyun 
2063*4882a593Smuzhiyun     TouchEventHistoryPush(ti, &ev->device_event);
2064*4882a593Smuzhiyun 
2065*4882a593Smuzhiyun     for (i = 0; i < ti->num_listeners; i++) {
2066*4882a593Smuzhiyun         GrabPtr grab = NULL;
2067*4882a593Smuzhiyun         ClientPtr client;
2068*4882a593Smuzhiyun         WindowPtr win;
2069*4882a593Smuzhiyun         XI2Mask *mask;
2070*4882a593Smuzhiyun         TouchListener *listener = &ti->listeners[i];
2071*4882a593Smuzhiyun 
2072*4882a593Smuzhiyun         if (resource && listener->listener != resource)
2073*4882a593Smuzhiyun             continue;
2074*4882a593Smuzhiyun 
2075*4882a593Smuzhiyun         if (!RetrieveTouchDeliveryData(dev, ti, ev, listener, &client, &win,
2076*4882a593Smuzhiyun                                        &grab, &mask))
2077*4882a593Smuzhiyun             continue;
2078*4882a593Smuzhiyun 
2079*4882a593Smuzhiyun         DeliverTouchEvent(dev, ti, ev, listener, client, win, grab, mask);
2080*4882a593Smuzhiyun     }
2081*4882a593Smuzhiyun }
2082*4882a593Smuzhiyun 
2083*4882a593Smuzhiyun int
InitProximityClassDeviceStruct(DeviceIntPtr dev)2084*4882a593Smuzhiyun InitProximityClassDeviceStruct(DeviceIntPtr dev)
2085*4882a593Smuzhiyun {
2086*4882a593Smuzhiyun     ProximityClassPtr proxc;
2087*4882a593Smuzhiyun 
2088*4882a593Smuzhiyun     BUG_RETURN_VAL(dev == NULL, FALSE);
2089*4882a593Smuzhiyun     BUG_RETURN_VAL(dev->proximity != NULL, FALSE);
2090*4882a593Smuzhiyun 
2091*4882a593Smuzhiyun     proxc = (ProximityClassPtr) malloc(sizeof(ProximityClassRec));
2092*4882a593Smuzhiyun     if (!proxc)
2093*4882a593Smuzhiyun         return FALSE;
2094*4882a593Smuzhiyun     proxc->sourceid = dev->id;
2095*4882a593Smuzhiyun     proxc->in_proximity = TRUE;
2096*4882a593Smuzhiyun     dev->proximity = proxc;
2097*4882a593Smuzhiyun     return TRUE;
2098*4882a593Smuzhiyun }
2099*4882a593Smuzhiyun 
2100*4882a593Smuzhiyun /**
2101*4882a593Smuzhiyun  * Initialise the device's valuators. The memory must already be allocated,
2102*4882a593Smuzhiyun  * this function merely inits the matching axis (specified through axnum) to
2103*4882a593Smuzhiyun  * sane values.
2104*4882a593Smuzhiyun  *
2105*4882a593Smuzhiyun  * It is a condition that (minval < maxval).
2106*4882a593Smuzhiyun  *
2107*4882a593Smuzhiyun  * @see InitValuatorClassDeviceStruct
2108*4882a593Smuzhiyun  */
2109*4882a593Smuzhiyun Bool
InitValuatorAxisStruct(DeviceIntPtr dev,int axnum,Atom label,int minval,int maxval,int resolution,int min_res,int max_res,int mode)2110*4882a593Smuzhiyun InitValuatorAxisStruct(DeviceIntPtr dev, int axnum, Atom label, int minval,
2111*4882a593Smuzhiyun                        int maxval, int resolution, int min_res, int max_res,
2112*4882a593Smuzhiyun                        int mode)
2113*4882a593Smuzhiyun {
2114*4882a593Smuzhiyun     AxisInfoPtr ax;
2115*4882a593Smuzhiyun 
2116*4882a593Smuzhiyun     BUG_RETURN_VAL(dev == NULL, FALSE);
2117*4882a593Smuzhiyun     BUG_RETURN_VAL(dev->valuator == NULL, FALSE);
2118*4882a593Smuzhiyun     BUG_RETURN_VAL(axnum >= dev->valuator->numAxes, FALSE);
2119*4882a593Smuzhiyun     BUG_RETURN_VAL(minval > maxval && mode == Absolute, FALSE);
2120*4882a593Smuzhiyun 
2121*4882a593Smuzhiyun     ax = dev->valuator->axes + axnum;
2122*4882a593Smuzhiyun 
2123*4882a593Smuzhiyun     ax->min_value = minval;
2124*4882a593Smuzhiyun     ax->max_value = maxval;
2125*4882a593Smuzhiyun     ax->resolution = resolution;
2126*4882a593Smuzhiyun     ax->min_resolution = min_res;
2127*4882a593Smuzhiyun     ax->max_resolution = max_res;
2128*4882a593Smuzhiyun     ax->label = label;
2129*4882a593Smuzhiyun     ax->mode = mode;
2130*4882a593Smuzhiyun 
2131*4882a593Smuzhiyun     if (mode & OutOfProximity)
2132*4882a593Smuzhiyun         dev->proximity->in_proximity = FALSE;
2133*4882a593Smuzhiyun 
2134*4882a593Smuzhiyun     return SetScrollValuator(dev, axnum, SCROLL_TYPE_NONE, 0, SCROLL_FLAG_NONE);
2135*4882a593Smuzhiyun }
2136*4882a593Smuzhiyun 
2137*4882a593Smuzhiyun /**
2138*4882a593Smuzhiyun  * Set the given axis number as a scrolling valuator.
2139*4882a593Smuzhiyun  */
2140*4882a593Smuzhiyun Bool
SetScrollValuator(DeviceIntPtr dev,int axnum,enum ScrollType type,double increment,int flags)2141*4882a593Smuzhiyun SetScrollValuator(DeviceIntPtr dev, int axnum, enum ScrollType type,
2142*4882a593Smuzhiyun                   double increment, int flags)
2143*4882a593Smuzhiyun {
2144*4882a593Smuzhiyun     AxisInfoPtr ax;
2145*4882a593Smuzhiyun     int *current_ax;
2146*4882a593Smuzhiyun     InternalEvent dce;
2147*4882a593Smuzhiyun     DeviceIntPtr master;
2148*4882a593Smuzhiyun 
2149*4882a593Smuzhiyun     BUG_RETURN_VAL(dev == NULL, FALSE);
2150*4882a593Smuzhiyun     BUG_RETURN_VAL(dev->valuator == NULL, FALSE);
2151*4882a593Smuzhiyun     BUG_RETURN_VAL(axnum >= dev->valuator->numAxes, FALSE);
2152*4882a593Smuzhiyun 
2153*4882a593Smuzhiyun     switch (type) {
2154*4882a593Smuzhiyun     case SCROLL_TYPE_VERTICAL:
2155*4882a593Smuzhiyun         current_ax = &dev->valuator->v_scroll_axis;
2156*4882a593Smuzhiyun         break;
2157*4882a593Smuzhiyun     case SCROLL_TYPE_HORIZONTAL:
2158*4882a593Smuzhiyun         current_ax = &dev->valuator->h_scroll_axis;
2159*4882a593Smuzhiyun         break;
2160*4882a593Smuzhiyun     case SCROLL_TYPE_NONE:
2161*4882a593Smuzhiyun         ax = &dev->valuator->axes[axnum];
2162*4882a593Smuzhiyun         ax->scroll.type = type;
2163*4882a593Smuzhiyun         return TRUE;
2164*4882a593Smuzhiyun     default:
2165*4882a593Smuzhiyun         return FALSE;
2166*4882a593Smuzhiyun     }
2167*4882a593Smuzhiyun 
2168*4882a593Smuzhiyun     if (increment == 0.0)
2169*4882a593Smuzhiyun         return FALSE;
2170*4882a593Smuzhiyun 
2171*4882a593Smuzhiyun     if (*current_ax != -1 && axnum != *current_ax) {
2172*4882a593Smuzhiyun         ax = &dev->valuator->axes[*current_ax];
2173*4882a593Smuzhiyun         if (ax->scroll.type == type &&
2174*4882a593Smuzhiyun             (flags & SCROLL_FLAG_PREFERRED) &&
2175*4882a593Smuzhiyun             (ax->scroll.flags & SCROLL_FLAG_PREFERRED))
2176*4882a593Smuzhiyun             return FALSE;
2177*4882a593Smuzhiyun     }
2178*4882a593Smuzhiyun     *current_ax = axnum;
2179*4882a593Smuzhiyun 
2180*4882a593Smuzhiyun     ax = &dev->valuator->axes[axnum];
2181*4882a593Smuzhiyun     ax->scroll.type = type;
2182*4882a593Smuzhiyun     ax->scroll.increment = increment;
2183*4882a593Smuzhiyun     ax->scroll.flags = flags;
2184*4882a593Smuzhiyun 
2185*4882a593Smuzhiyun     master = GetMaster(dev, MASTER_ATTACHED);
2186*4882a593Smuzhiyun     CreateClassesChangedEvent(&dce, master, dev,
2187*4882a593Smuzhiyun                               DEVCHANGE_POINTER_EVENT |
2188*4882a593Smuzhiyun                               DEVCHANGE_DEVICE_CHANGE);
2189*4882a593Smuzhiyun     XISendDeviceChangedEvent(dev, &dce.changed_event);
2190*4882a593Smuzhiyun 
2191*4882a593Smuzhiyun     /* if the current slave is us, update the master. If not, we'll update
2192*4882a593Smuzhiyun      * whenever the next slave switch happens anyway. CMDC sends the event
2193*4882a593Smuzhiyun      * for us */
2194*4882a593Smuzhiyun     if (master && master->lastSlave == dev)
2195*4882a593Smuzhiyun         ChangeMasterDeviceClasses(master, &dce.changed_event);
2196*4882a593Smuzhiyun 
2197*4882a593Smuzhiyun     return TRUE;
2198*4882a593Smuzhiyun }
2199*4882a593Smuzhiyun 
2200*4882a593Smuzhiyun int
CheckGrabValues(ClientPtr client,GrabParameters * param)2201*4882a593Smuzhiyun CheckGrabValues(ClientPtr client, GrabParameters *param)
2202*4882a593Smuzhiyun {
2203*4882a593Smuzhiyun     if (param->grabtype != CORE &&
2204*4882a593Smuzhiyun         param->grabtype != XI && param->grabtype != XI2) {
2205*4882a593Smuzhiyun         ErrorF("[Xi] grabtype is invalid. This is a bug.\n");
2206*4882a593Smuzhiyun         return BadImplementation;
2207*4882a593Smuzhiyun     }
2208*4882a593Smuzhiyun 
2209*4882a593Smuzhiyun     if ((param->this_device_mode != GrabModeSync) &&
2210*4882a593Smuzhiyun         (param->this_device_mode != GrabModeAsync) &&
2211*4882a593Smuzhiyun         (param->this_device_mode != XIGrabModeTouch)) {
2212*4882a593Smuzhiyun         client->errorValue = param->this_device_mode;
2213*4882a593Smuzhiyun         return BadValue;
2214*4882a593Smuzhiyun     }
2215*4882a593Smuzhiyun     if ((param->other_devices_mode != GrabModeSync) &&
2216*4882a593Smuzhiyun         (param->other_devices_mode != GrabModeAsync) &&
2217*4882a593Smuzhiyun         (param->other_devices_mode != XIGrabModeTouch)) {
2218*4882a593Smuzhiyun         client->errorValue = param->other_devices_mode;
2219*4882a593Smuzhiyun         return BadValue;
2220*4882a593Smuzhiyun     }
2221*4882a593Smuzhiyun 
2222*4882a593Smuzhiyun     if (param->modifiers != AnyModifier &&
2223*4882a593Smuzhiyun         param->modifiers != XIAnyModifier &&
2224*4882a593Smuzhiyun         (param->modifiers & ~AllModifiersMask)) {
2225*4882a593Smuzhiyun         client->errorValue = param->modifiers;
2226*4882a593Smuzhiyun         return BadValue;
2227*4882a593Smuzhiyun     }
2228*4882a593Smuzhiyun 
2229*4882a593Smuzhiyun     if ((param->ownerEvents != xFalse) && (param->ownerEvents != xTrue)) {
2230*4882a593Smuzhiyun         client->errorValue = param->ownerEvents;
2231*4882a593Smuzhiyun         return BadValue;
2232*4882a593Smuzhiyun     }
2233*4882a593Smuzhiyun     return Success;
2234*4882a593Smuzhiyun }
2235*4882a593Smuzhiyun 
2236*4882a593Smuzhiyun int
GrabButton(ClientPtr client,DeviceIntPtr dev,DeviceIntPtr modifier_device,int button,GrabParameters * param,enum InputLevel grabtype,GrabMask * mask)2237*4882a593Smuzhiyun GrabButton(ClientPtr client, DeviceIntPtr dev, DeviceIntPtr modifier_device,
2238*4882a593Smuzhiyun            int button, GrabParameters *param, enum InputLevel grabtype,
2239*4882a593Smuzhiyun            GrabMask *mask)
2240*4882a593Smuzhiyun {
2241*4882a593Smuzhiyun     WindowPtr pWin, confineTo;
2242*4882a593Smuzhiyun     CursorPtr cursor;
2243*4882a593Smuzhiyun     GrabPtr grab;
2244*4882a593Smuzhiyun     int rc, type = -1;
2245*4882a593Smuzhiyun     Mask access_mode = DixGrabAccess;
2246*4882a593Smuzhiyun 
2247*4882a593Smuzhiyun     rc = CheckGrabValues(client, param);
2248*4882a593Smuzhiyun     if (rc != Success)
2249*4882a593Smuzhiyun         return rc;
2250*4882a593Smuzhiyun     if (param->confineTo == None)
2251*4882a593Smuzhiyun         confineTo = NullWindow;
2252*4882a593Smuzhiyun     else {
2253*4882a593Smuzhiyun         rc = dixLookupWindow(&confineTo, param->confineTo, client,
2254*4882a593Smuzhiyun                              DixSetAttrAccess);
2255*4882a593Smuzhiyun         if (rc != Success)
2256*4882a593Smuzhiyun             return rc;
2257*4882a593Smuzhiyun     }
2258*4882a593Smuzhiyun     if (param->cursor == None)
2259*4882a593Smuzhiyun         cursor = NullCursor;
2260*4882a593Smuzhiyun     else {
2261*4882a593Smuzhiyun         rc = dixLookupResourceByType((void **) &cursor, param->cursor,
2262*4882a593Smuzhiyun                                      RT_CURSOR, client, DixUseAccess);
2263*4882a593Smuzhiyun         if (rc != Success) {
2264*4882a593Smuzhiyun             client->errorValue = param->cursor;
2265*4882a593Smuzhiyun             return rc;
2266*4882a593Smuzhiyun         }
2267*4882a593Smuzhiyun         access_mode |= DixForceAccess;
2268*4882a593Smuzhiyun     }
2269*4882a593Smuzhiyun     if (param->this_device_mode == GrabModeSync ||
2270*4882a593Smuzhiyun         param->other_devices_mode == GrabModeSync)
2271*4882a593Smuzhiyun         access_mode |= DixFreezeAccess;
2272*4882a593Smuzhiyun     rc = XaceHook(XACE_DEVICE_ACCESS, client, dev, access_mode);
2273*4882a593Smuzhiyun     if (rc != Success)
2274*4882a593Smuzhiyun         return rc;
2275*4882a593Smuzhiyun     rc = dixLookupWindow(&pWin, param->grabWindow, client, DixSetAttrAccess);
2276*4882a593Smuzhiyun     if (rc != Success)
2277*4882a593Smuzhiyun         return rc;
2278*4882a593Smuzhiyun 
2279*4882a593Smuzhiyun     if (grabtype == XI)
2280*4882a593Smuzhiyun         type = DeviceButtonPress;
2281*4882a593Smuzhiyun     else if (grabtype == XI2)
2282*4882a593Smuzhiyun         type = XI_ButtonPress;
2283*4882a593Smuzhiyun 
2284*4882a593Smuzhiyun     grab = CreateGrab(client->index, dev, modifier_device, pWin, grabtype,
2285*4882a593Smuzhiyun                       mask, param, type, button, confineTo, cursor);
2286*4882a593Smuzhiyun     if (!grab)
2287*4882a593Smuzhiyun         return BadAlloc;
2288*4882a593Smuzhiyun     return AddPassiveGrabToList(client, grab);
2289*4882a593Smuzhiyun }
2290*4882a593Smuzhiyun 
2291*4882a593Smuzhiyun /**
2292*4882a593Smuzhiyun  * Grab the given key.
2293*4882a593Smuzhiyun  */
2294*4882a593Smuzhiyun int
GrabKey(ClientPtr client,DeviceIntPtr dev,DeviceIntPtr modifier_device,int key,GrabParameters * param,enum InputLevel grabtype,GrabMask * mask)2295*4882a593Smuzhiyun GrabKey(ClientPtr client, DeviceIntPtr dev, DeviceIntPtr modifier_device,
2296*4882a593Smuzhiyun         int key, GrabParameters *param, enum InputLevel grabtype,
2297*4882a593Smuzhiyun         GrabMask *mask)
2298*4882a593Smuzhiyun {
2299*4882a593Smuzhiyun     WindowPtr pWin;
2300*4882a593Smuzhiyun     GrabPtr grab;
2301*4882a593Smuzhiyun     KeyClassPtr k = dev->key;
2302*4882a593Smuzhiyun     Mask access_mode = DixGrabAccess;
2303*4882a593Smuzhiyun     int rc, type = -1;
2304*4882a593Smuzhiyun 
2305*4882a593Smuzhiyun     rc = CheckGrabValues(client, param);
2306*4882a593Smuzhiyun     if (rc != Success)
2307*4882a593Smuzhiyun         return rc;
2308*4882a593Smuzhiyun     if ((dev->id != XIAllDevices && dev->id != XIAllMasterDevices) && k == NULL)
2309*4882a593Smuzhiyun         return BadMatch;
2310*4882a593Smuzhiyun     if (grabtype == XI) {
2311*4882a593Smuzhiyun         if ((key > k->xkbInfo->desc->max_key_code ||
2312*4882a593Smuzhiyun              key < k->xkbInfo->desc->min_key_code)
2313*4882a593Smuzhiyun             && (key != AnyKey)) {
2314*4882a593Smuzhiyun             client->errorValue = key;
2315*4882a593Smuzhiyun             return BadValue;
2316*4882a593Smuzhiyun         }
2317*4882a593Smuzhiyun         type = DeviceKeyPress;
2318*4882a593Smuzhiyun     }
2319*4882a593Smuzhiyun     else if (grabtype == XI2)
2320*4882a593Smuzhiyun         type = XI_KeyPress;
2321*4882a593Smuzhiyun 
2322*4882a593Smuzhiyun     rc = dixLookupWindow(&pWin, param->grabWindow, client, DixSetAttrAccess);
2323*4882a593Smuzhiyun     if (rc != Success)
2324*4882a593Smuzhiyun         return rc;
2325*4882a593Smuzhiyun     if (param->this_device_mode == GrabModeSync ||
2326*4882a593Smuzhiyun         param->other_devices_mode == GrabModeSync)
2327*4882a593Smuzhiyun         access_mode |= DixFreezeAccess;
2328*4882a593Smuzhiyun     rc = XaceHook(XACE_DEVICE_ACCESS, client, dev, access_mode);
2329*4882a593Smuzhiyun     if (rc != Success)
2330*4882a593Smuzhiyun         return rc;
2331*4882a593Smuzhiyun 
2332*4882a593Smuzhiyun     grab = CreateGrab(client->index, dev, modifier_device, pWin, grabtype,
2333*4882a593Smuzhiyun                       mask, param, type, key, NULL, NULL);
2334*4882a593Smuzhiyun     if (!grab)
2335*4882a593Smuzhiyun         return BadAlloc;
2336*4882a593Smuzhiyun     return AddPassiveGrabToList(client, grab);
2337*4882a593Smuzhiyun }
2338*4882a593Smuzhiyun 
2339*4882a593Smuzhiyun /* Enter/FocusIn grab */
2340*4882a593Smuzhiyun int
GrabWindow(ClientPtr client,DeviceIntPtr dev,int type,GrabParameters * param,GrabMask * mask)2341*4882a593Smuzhiyun GrabWindow(ClientPtr client, DeviceIntPtr dev, int type,
2342*4882a593Smuzhiyun            GrabParameters *param, GrabMask *mask)
2343*4882a593Smuzhiyun {
2344*4882a593Smuzhiyun     WindowPtr pWin;
2345*4882a593Smuzhiyun     CursorPtr cursor;
2346*4882a593Smuzhiyun     GrabPtr grab;
2347*4882a593Smuzhiyun     Mask access_mode = DixGrabAccess;
2348*4882a593Smuzhiyun     int rc;
2349*4882a593Smuzhiyun 
2350*4882a593Smuzhiyun     rc = CheckGrabValues(client, param);
2351*4882a593Smuzhiyun     if (rc != Success)
2352*4882a593Smuzhiyun         return rc;
2353*4882a593Smuzhiyun 
2354*4882a593Smuzhiyun     rc = dixLookupWindow(&pWin, param->grabWindow, client, DixSetAttrAccess);
2355*4882a593Smuzhiyun     if (rc != Success)
2356*4882a593Smuzhiyun         return rc;
2357*4882a593Smuzhiyun     if (param->cursor == None)
2358*4882a593Smuzhiyun         cursor = NullCursor;
2359*4882a593Smuzhiyun     else {
2360*4882a593Smuzhiyun         rc = dixLookupResourceByType((void **) &cursor, param->cursor,
2361*4882a593Smuzhiyun                                      RT_CURSOR, client, DixUseAccess);
2362*4882a593Smuzhiyun         if (rc != Success) {
2363*4882a593Smuzhiyun             client->errorValue = param->cursor;
2364*4882a593Smuzhiyun             return rc;
2365*4882a593Smuzhiyun         }
2366*4882a593Smuzhiyun         access_mode |= DixForceAccess;
2367*4882a593Smuzhiyun     }
2368*4882a593Smuzhiyun     if (param->this_device_mode == GrabModeSync ||
2369*4882a593Smuzhiyun         param->other_devices_mode == GrabModeSync)
2370*4882a593Smuzhiyun         access_mode |= DixFreezeAccess;
2371*4882a593Smuzhiyun     rc = XaceHook(XACE_DEVICE_ACCESS, client, dev, access_mode);
2372*4882a593Smuzhiyun     if (rc != Success)
2373*4882a593Smuzhiyun         return rc;
2374*4882a593Smuzhiyun 
2375*4882a593Smuzhiyun     grab = CreateGrab(client->index, dev, dev, pWin, XI2,
2376*4882a593Smuzhiyun                       mask, param,
2377*4882a593Smuzhiyun                       (type == XIGrabtypeEnter) ? XI_Enter : XI_FocusIn, 0,
2378*4882a593Smuzhiyun                       NULL, cursor);
2379*4882a593Smuzhiyun 
2380*4882a593Smuzhiyun     if (!grab)
2381*4882a593Smuzhiyun         return BadAlloc;
2382*4882a593Smuzhiyun 
2383*4882a593Smuzhiyun     return AddPassiveGrabToList(client, grab);
2384*4882a593Smuzhiyun }
2385*4882a593Smuzhiyun 
2386*4882a593Smuzhiyun /* Touch grab */
2387*4882a593Smuzhiyun int
GrabTouch(ClientPtr client,DeviceIntPtr dev,DeviceIntPtr mod_dev,GrabParameters * param,GrabMask * mask)2388*4882a593Smuzhiyun GrabTouch(ClientPtr client, DeviceIntPtr dev, DeviceIntPtr mod_dev,
2389*4882a593Smuzhiyun           GrabParameters *param, GrabMask *mask)
2390*4882a593Smuzhiyun {
2391*4882a593Smuzhiyun     WindowPtr pWin;
2392*4882a593Smuzhiyun     GrabPtr grab;
2393*4882a593Smuzhiyun     int rc;
2394*4882a593Smuzhiyun 
2395*4882a593Smuzhiyun     rc = CheckGrabValues(client, param);
2396*4882a593Smuzhiyun     if (rc != Success)
2397*4882a593Smuzhiyun         return rc;
2398*4882a593Smuzhiyun 
2399*4882a593Smuzhiyun     rc = dixLookupWindow(&pWin, param->grabWindow, client, DixSetAttrAccess);
2400*4882a593Smuzhiyun     if (rc != Success)
2401*4882a593Smuzhiyun         return rc;
2402*4882a593Smuzhiyun     rc = XaceHook(XACE_DEVICE_ACCESS, client, dev, DixGrabAccess);
2403*4882a593Smuzhiyun     if (rc != Success)
2404*4882a593Smuzhiyun         return rc;
2405*4882a593Smuzhiyun 
2406*4882a593Smuzhiyun     grab = CreateGrab(client->index, dev, mod_dev, pWin, XI2,
2407*4882a593Smuzhiyun                       mask, param, XI_TouchBegin, 0, NullWindow, NullCursor);
2408*4882a593Smuzhiyun     if (!grab)
2409*4882a593Smuzhiyun         return BadAlloc;
2410*4882a593Smuzhiyun 
2411*4882a593Smuzhiyun     return AddPassiveGrabToList(client, grab);
2412*4882a593Smuzhiyun }
2413*4882a593Smuzhiyun 
2414*4882a593Smuzhiyun int
SelectForWindow(DeviceIntPtr dev,WindowPtr pWin,ClientPtr client,Mask mask,Mask exclusivemasks)2415*4882a593Smuzhiyun SelectForWindow(DeviceIntPtr dev, WindowPtr pWin, ClientPtr client,
2416*4882a593Smuzhiyun                 Mask mask, Mask exclusivemasks)
2417*4882a593Smuzhiyun {
2418*4882a593Smuzhiyun     int mskidx = dev->id;
2419*4882a593Smuzhiyun     int i, ret;
2420*4882a593Smuzhiyun     Mask check;
2421*4882a593Smuzhiyun     InputClientsPtr others;
2422*4882a593Smuzhiyun 
2423*4882a593Smuzhiyun     check = (mask & exclusivemasks);
2424*4882a593Smuzhiyun     if (wOtherInputMasks(pWin)) {
2425*4882a593Smuzhiyun         if (check & wOtherInputMasks(pWin)->inputEvents[mskidx]) {
2426*4882a593Smuzhiyun             /* It is illegal for two different clients to select on any of
2427*4882a593Smuzhiyun              * the events for maskcheck. However, it is OK, for some client
2428*4882a593Smuzhiyun              * to continue selecting on one of those events.
2429*4882a593Smuzhiyun              */
2430*4882a593Smuzhiyun             for (others = wOtherInputMasks(pWin)->inputClients; others;
2431*4882a593Smuzhiyun                  others = others->next) {
2432*4882a593Smuzhiyun                 if (!SameClient(others, client) && (check &
2433*4882a593Smuzhiyun                                                     others->mask[mskidx]))
2434*4882a593Smuzhiyun                     return BadAccess;
2435*4882a593Smuzhiyun             }
2436*4882a593Smuzhiyun         }
2437*4882a593Smuzhiyun         for (others = wOtherInputMasks(pWin)->inputClients; others;
2438*4882a593Smuzhiyun              others = others->next) {
2439*4882a593Smuzhiyun             if (SameClient(others, client)) {
2440*4882a593Smuzhiyun                 check = others->mask[mskidx];
2441*4882a593Smuzhiyun                 others->mask[mskidx] = mask;
2442*4882a593Smuzhiyun                 if (mask == 0) {
2443*4882a593Smuzhiyun                     for (i = 0; i < EMASKSIZE; i++)
2444*4882a593Smuzhiyun                         if (i != mskidx && others->mask[i] != 0)
2445*4882a593Smuzhiyun                             break;
2446*4882a593Smuzhiyun                     if (i == EMASKSIZE) {
2447*4882a593Smuzhiyun                         RecalculateDeviceDeliverableEvents(pWin);
2448*4882a593Smuzhiyun                         if (ShouldFreeInputMasks(pWin, FALSE))
2449*4882a593Smuzhiyun                             FreeResource(others->resource, RT_NONE);
2450*4882a593Smuzhiyun                         return Success;
2451*4882a593Smuzhiyun                     }
2452*4882a593Smuzhiyun                 }
2453*4882a593Smuzhiyun                 goto maskSet;
2454*4882a593Smuzhiyun             }
2455*4882a593Smuzhiyun         }
2456*4882a593Smuzhiyun     }
2457*4882a593Smuzhiyun     check = 0;
2458*4882a593Smuzhiyun     if ((ret = AddExtensionClient(pWin, client, mask, mskidx)) != Success)
2459*4882a593Smuzhiyun         return ret;
2460*4882a593Smuzhiyun  maskSet:
2461*4882a593Smuzhiyun     if (dev->valuator)
2462*4882a593Smuzhiyun         if ((dev->valuator->motionHintWindow == pWin) &&
2463*4882a593Smuzhiyun             (mask & DevicePointerMotionHintMask) &&
2464*4882a593Smuzhiyun             !(check & DevicePointerMotionHintMask) && !dev->deviceGrab.grab)
2465*4882a593Smuzhiyun             dev->valuator->motionHintWindow = NullWindow;
2466*4882a593Smuzhiyun     RecalculateDeviceDeliverableEvents(pWin);
2467*4882a593Smuzhiyun     return Success;
2468*4882a593Smuzhiyun }
2469*4882a593Smuzhiyun 
2470*4882a593Smuzhiyun static void
FreeInputClient(InputClientsPtr * other)2471*4882a593Smuzhiyun FreeInputClient(InputClientsPtr * other)
2472*4882a593Smuzhiyun {
2473*4882a593Smuzhiyun     xi2mask_free(&(*other)->xi2mask);
2474*4882a593Smuzhiyun     free(*other);
2475*4882a593Smuzhiyun     *other = NULL;
2476*4882a593Smuzhiyun }
2477*4882a593Smuzhiyun 
2478*4882a593Smuzhiyun static InputClientsPtr
AllocInputClient(void)2479*4882a593Smuzhiyun AllocInputClient(void)
2480*4882a593Smuzhiyun {
2481*4882a593Smuzhiyun     return calloc(1, sizeof(InputClients));
2482*4882a593Smuzhiyun }
2483*4882a593Smuzhiyun 
2484*4882a593Smuzhiyun int
AddExtensionClient(WindowPtr pWin,ClientPtr client,Mask mask,int mskidx)2485*4882a593Smuzhiyun AddExtensionClient(WindowPtr pWin, ClientPtr client, Mask mask, int mskidx)
2486*4882a593Smuzhiyun {
2487*4882a593Smuzhiyun     InputClientsPtr others;
2488*4882a593Smuzhiyun 
2489*4882a593Smuzhiyun     if (!pWin->optional && !MakeWindowOptional(pWin))
2490*4882a593Smuzhiyun         return BadAlloc;
2491*4882a593Smuzhiyun     others = AllocInputClient();
2492*4882a593Smuzhiyun     if (!others)
2493*4882a593Smuzhiyun         return BadAlloc;
2494*4882a593Smuzhiyun     if (!pWin->optional->inputMasks && !MakeInputMasks(pWin))
2495*4882a593Smuzhiyun         goto bail;
2496*4882a593Smuzhiyun     others->xi2mask = xi2mask_new();
2497*4882a593Smuzhiyun     if (!others->xi2mask)
2498*4882a593Smuzhiyun         goto bail;
2499*4882a593Smuzhiyun     others->mask[mskidx] = mask;
2500*4882a593Smuzhiyun     others->resource = FakeClientID(client->index);
2501*4882a593Smuzhiyun     others->next = pWin->optional->inputMasks->inputClients;
2502*4882a593Smuzhiyun     pWin->optional->inputMasks->inputClients = others;
2503*4882a593Smuzhiyun     if (!AddResource(others->resource, RT_INPUTCLIENT, (void *) pWin))
2504*4882a593Smuzhiyun         goto bail;
2505*4882a593Smuzhiyun     return Success;
2506*4882a593Smuzhiyun 
2507*4882a593Smuzhiyun  bail:
2508*4882a593Smuzhiyun     FreeInputClient(&others);
2509*4882a593Smuzhiyun     return BadAlloc;
2510*4882a593Smuzhiyun }
2511*4882a593Smuzhiyun 
2512*4882a593Smuzhiyun static Bool
MakeInputMasks(WindowPtr pWin)2513*4882a593Smuzhiyun MakeInputMasks(WindowPtr pWin)
2514*4882a593Smuzhiyun {
2515*4882a593Smuzhiyun     struct _OtherInputMasks *imasks;
2516*4882a593Smuzhiyun 
2517*4882a593Smuzhiyun     imasks = calloc(1, sizeof(struct _OtherInputMasks));
2518*4882a593Smuzhiyun     if (!imasks)
2519*4882a593Smuzhiyun         return FALSE;
2520*4882a593Smuzhiyun     imasks->xi2mask = xi2mask_new();
2521*4882a593Smuzhiyun     if (!imasks->xi2mask) {
2522*4882a593Smuzhiyun         free(imasks);
2523*4882a593Smuzhiyun         return FALSE;
2524*4882a593Smuzhiyun     }
2525*4882a593Smuzhiyun     pWin->optional->inputMasks = imasks;
2526*4882a593Smuzhiyun     return TRUE;
2527*4882a593Smuzhiyun }
2528*4882a593Smuzhiyun 
2529*4882a593Smuzhiyun static void
FreeInputMask(OtherInputMasks ** imask)2530*4882a593Smuzhiyun FreeInputMask(OtherInputMasks ** imask)
2531*4882a593Smuzhiyun {
2532*4882a593Smuzhiyun     xi2mask_free(&(*imask)->xi2mask);
2533*4882a593Smuzhiyun     free(*imask);
2534*4882a593Smuzhiyun     *imask = NULL;
2535*4882a593Smuzhiyun }
2536*4882a593Smuzhiyun 
2537*4882a593Smuzhiyun void
RecalculateDeviceDeliverableEvents(WindowPtr pWin)2538*4882a593Smuzhiyun RecalculateDeviceDeliverableEvents(WindowPtr pWin)
2539*4882a593Smuzhiyun {
2540*4882a593Smuzhiyun     InputClientsPtr others;
2541*4882a593Smuzhiyun     struct _OtherInputMasks *inputMasks;        /* default: NULL */
2542*4882a593Smuzhiyun     WindowPtr pChild, tmp;
2543*4882a593Smuzhiyun     int i;
2544*4882a593Smuzhiyun 
2545*4882a593Smuzhiyun     pChild = pWin;
2546*4882a593Smuzhiyun     while (1) {
2547*4882a593Smuzhiyun         if ((inputMasks = wOtherInputMasks(pChild)) != 0) {
2548*4882a593Smuzhiyun             xi2mask_zero(inputMasks->xi2mask, -1);
2549*4882a593Smuzhiyun             for (others = inputMasks->inputClients; others;
2550*4882a593Smuzhiyun                  others = others->next) {
2551*4882a593Smuzhiyun                 for (i = 0; i < EMASKSIZE; i++)
2552*4882a593Smuzhiyun                     inputMasks->inputEvents[i] |= others->mask[i];
2553*4882a593Smuzhiyun                 xi2mask_merge(inputMasks->xi2mask, others->xi2mask);
2554*4882a593Smuzhiyun             }
2555*4882a593Smuzhiyun             for (i = 0; i < EMASKSIZE; i++)
2556*4882a593Smuzhiyun                 inputMasks->deliverableEvents[i] = inputMasks->inputEvents[i];
2557*4882a593Smuzhiyun             for (tmp = pChild->parent; tmp; tmp = tmp->parent)
2558*4882a593Smuzhiyun                 if (wOtherInputMasks(tmp))
2559*4882a593Smuzhiyun                     for (i = 0; i < EMASKSIZE; i++)
2560*4882a593Smuzhiyun                         inputMasks->deliverableEvents[i] |=
2561*4882a593Smuzhiyun                             (wOtherInputMasks(tmp)->deliverableEvents[i]
2562*4882a593Smuzhiyun                              & ~inputMasks->dontPropagateMask[i] &
2563*4882a593Smuzhiyun                              PropagateMask[i]);
2564*4882a593Smuzhiyun         }
2565*4882a593Smuzhiyun         if (pChild->firstChild) {
2566*4882a593Smuzhiyun             pChild = pChild->firstChild;
2567*4882a593Smuzhiyun             continue;
2568*4882a593Smuzhiyun         }
2569*4882a593Smuzhiyun         while (!pChild->nextSib && (pChild != pWin))
2570*4882a593Smuzhiyun             pChild = pChild->parent;
2571*4882a593Smuzhiyun         if (pChild == pWin)
2572*4882a593Smuzhiyun             break;
2573*4882a593Smuzhiyun         pChild = pChild->nextSib;
2574*4882a593Smuzhiyun     }
2575*4882a593Smuzhiyun }
2576*4882a593Smuzhiyun 
2577*4882a593Smuzhiyun int
InputClientGone(WindowPtr pWin,XID id)2578*4882a593Smuzhiyun InputClientGone(WindowPtr pWin, XID id)
2579*4882a593Smuzhiyun {
2580*4882a593Smuzhiyun     InputClientsPtr other, prev;
2581*4882a593Smuzhiyun 
2582*4882a593Smuzhiyun     if (!wOtherInputMasks(pWin))
2583*4882a593Smuzhiyun         return Success;
2584*4882a593Smuzhiyun     prev = 0;
2585*4882a593Smuzhiyun     for (other = wOtherInputMasks(pWin)->inputClients; other;
2586*4882a593Smuzhiyun          other = other->next) {
2587*4882a593Smuzhiyun         if (other->resource == id) {
2588*4882a593Smuzhiyun             if (prev) {
2589*4882a593Smuzhiyun                 prev->next = other->next;
2590*4882a593Smuzhiyun                 FreeInputClient(&other);
2591*4882a593Smuzhiyun             }
2592*4882a593Smuzhiyun             else if (!(other->next)) {
2593*4882a593Smuzhiyun                 if (ShouldFreeInputMasks(pWin, TRUE)) {
2594*4882a593Smuzhiyun                     OtherInputMasks *mask = wOtherInputMasks(pWin);
2595*4882a593Smuzhiyun 
2596*4882a593Smuzhiyun                     mask->inputClients = other->next;
2597*4882a593Smuzhiyun                     FreeInputMask(&mask);
2598*4882a593Smuzhiyun                     pWin->optional->inputMasks = (OtherInputMasks *) NULL;
2599*4882a593Smuzhiyun                     CheckWindowOptionalNeed(pWin);
2600*4882a593Smuzhiyun                     FreeInputClient(&other);
2601*4882a593Smuzhiyun                 }
2602*4882a593Smuzhiyun                 else {
2603*4882a593Smuzhiyun                     other->resource = FakeClientID(0);
2604*4882a593Smuzhiyun                     if (!AddResource(other->resource, RT_INPUTCLIENT,
2605*4882a593Smuzhiyun                                      (void *) pWin))
2606*4882a593Smuzhiyun                         return BadAlloc;
2607*4882a593Smuzhiyun                 }
2608*4882a593Smuzhiyun             }
2609*4882a593Smuzhiyun             else {
2610*4882a593Smuzhiyun                 wOtherInputMasks(pWin)->inputClients = other->next;
2611*4882a593Smuzhiyun                 FreeInputClient(&other);
2612*4882a593Smuzhiyun             }
2613*4882a593Smuzhiyun             RecalculateDeviceDeliverableEvents(pWin);
2614*4882a593Smuzhiyun             return Success;
2615*4882a593Smuzhiyun         }
2616*4882a593Smuzhiyun         prev = other;
2617*4882a593Smuzhiyun     }
2618*4882a593Smuzhiyun     FatalError("client not on device event list");
2619*4882a593Smuzhiyun }
2620*4882a593Smuzhiyun 
2621*4882a593Smuzhiyun /**
2622*4882a593Smuzhiyun  * Search for window in each touch trace for each device. Remove the window
2623*4882a593Smuzhiyun  * and all its subwindows from the trace when found. The initial window
2624*4882a593Smuzhiyun  * order is preserved.
2625*4882a593Smuzhiyun  */
2626*4882a593Smuzhiyun void
WindowGone(WindowPtr win)2627*4882a593Smuzhiyun WindowGone(WindowPtr win)
2628*4882a593Smuzhiyun {
2629*4882a593Smuzhiyun     DeviceIntPtr dev;
2630*4882a593Smuzhiyun 
2631*4882a593Smuzhiyun     for (dev = inputInfo.devices; dev; dev = dev->next) {
2632*4882a593Smuzhiyun         TouchClassPtr t = dev->touch;
2633*4882a593Smuzhiyun         int i;
2634*4882a593Smuzhiyun 
2635*4882a593Smuzhiyun         if (!t)
2636*4882a593Smuzhiyun             continue;
2637*4882a593Smuzhiyun 
2638*4882a593Smuzhiyun         for (i = 0; i < t->num_touches; i++) {
2639*4882a593Smuzhiyun             SpritePtr sprite = &t->touches[i].sprite;
2640*4882a593Smuzhiyun             int j;
2641*4882a593Smuzhiyun 
2642*4882a593Smuzhiyun             for (j = 0; j < sprite->spriteTraceGood; j++) {
2643*4882a593Smuzhiyun                 if (sprite->spriteTrace[j] == win) {
2644*4882a593Smuzhiyun                     sprite->spriteTraceGood = j;
2645*4882a593Smuzhiyun                     break;
2646*4882a593Smuzhiyun                 }
2647*4882a593Smuzhiyun             }
2648*4882a593Smuzhiyun         }
2649*4882a593Smuzhiyun     }
2650*4882a593Smuzhiyun }
2651*4882a593Smuzhiyun 
2652*4882a593Smuzhiyun int
SendEvent(ClientPtr client,DeviceIntPtr d,Window dest,Bool propagate,xEvent * ev,Mask mask,int count)2653*4882a593Smuzhiyun SendEvent(ClientPtr client, DeviceIntPtr d, Window dest, Bool propagate,
2654*4882a593Smuzhiyun           xEvent *ev, Mask mask, int count)
2655*4882a593Smuzhiyun {
2656*4882a593Smuzhiyun     WindowPtr pWin;
2657*4882a593Smuzhiyun     WindowPtr effectiveFocus = NullWindow;      /* only set if dest==InputFocus */
2658*4882a593Smuzhiyun     WindowPtr spriteWin = GetSpriteWindow(d);
2659*4882a593Smuzhiyun 
2660*4882a593Smuzhiyun     if (dest == PointerWindow)
2661*4882a593Smuzhiyun         pWin = spriteWin;
2662*4882a593Smuzhiyun     else if (dest == InputFocus) {
2663*4882a593Smuzhiyun         WindowPtr inputFocus;
2664*4882a593Smuzhiyun 
2665*4882a593Smuzhiyun         if (!d->focus)
2666*4882a593Smuzhiyun             inputFocus = spriteWin;
2667*4882a593Smuzhiyun         else
2668*4882a593Smuzhiyun             inputFocus = d->focus->win;
2669*4882a593Smuzhiyun 
2670*4882a593Smuzhiyun         if (inputFocus == FollowKeyboardWin)
2671*4882a593Smuzhiyun             inputFocus = inputInfo.keyboard->focus->win;
2672*4882a593Smuzhiyun 
2673*4882a593Smuzhiyun         if (inputFocus == NoneWin)
2674*4882a593Smuzhiyun             return Success;
2675*4882a593Smuzhiyun 
2676*4882a593Smuzhiyun         /* If the input focus is PointerRootWin, send the event to where
2677*4882a593Smuzhiyun          * the pointer is if possible, then perhaps propogate up to root. */
2678*4882a593Smuzhiyun         if (inputFocus == PointerRootWin)
2679*4882a593Smuzhiyun             inputFocus = GetCurrentRootWindow(d);
2680*4882a593Smuzhiyun 
2681*4882a593Smuzhiyun         if (IsParent(inputFocus, spriteWin)) {
2682*4882a593Smuzhiyun             effectiveFocus = inputFocus;
2683*4882a593Smuzhiyun             pWin = spriteWin;
2684*4882a593Smuzhiyun         }
2685*4882a593Smuzhiyun         else
2686*4882a593Smuzhiyun             effectiveFocus = pWin = inputFocus;
2687*4882a593Smuzhiyun     }
2688*4882a593Smuzhiyun     else
2689*4882a593Smuzhiyun         dixLookupWindow(&pWin, dest, client, DixSendAccess);
2690*4882a593Smuzhiyun     if (!pWin)
2691*4882a593Smuzhiyun         return BadWindow;
2692*4882a593Smuzhiyun     if ((propagate != xFalse) && (propagate != xTrue)) {
2693*4882a593Smuzhiyun         client->errorValue = propagate;
2694*4882a593Smuzhiyun         return BadValue;
2695*4882a593Smuzhiyun     }
2696*4882a593Smuzhiyun     ev->u.u.type |= 0x80;
2697*4882a593Smuzhiyun     if (propagate) {
2698*4882a593Smuzhiyun         for (; pWin; pWin = pWin->parent) {
2699*4882a593Smuzhiyun             if (DeliverEventsToWindow(d, pWin, ev, count, mask, NullGrab))
2700*4882a593Smuzhiyun                 return Success;
2701*4882a593Smuzhiyun             if (pWin == effectiveFocus)
2702*4882a593Smuzhiyun                 return Success;
2703*4882a593Smuzhiyun             if (wOtherInputMasks(pWin))
2704*4882a593Smuzhiyun                 mask &= ~wOtherInputMasks(pWin)->dontPropagateMask[d->id];
2705*4882a593Smuzhiyun             if (!mask)
2706*4882a593Smuzhiyun                 break;
2707*4882a593Smuzhiyun         }
2708*4882a593Smuzhiyun     }
2709*4882a593Smuzhiyun     else if (!XaceHook(XACE_SEND_ACCESS, client, NULL, pWin, ev, count))
2710*4882a593Smuzhiyun         DeliverEventsToWindow(d, pWin, ev, count, mask, NullGrab);
2711*4882a593Smuzhiyun     return Success;
2712*4882a593Smuzhiyun }
2713*4882a593Smuzhiyun 
2714*4882a593Smuzhiyun int
SetButtonMapping(ClientPtr client,DeviceIntPtr dev,int nElts,BYTE * map)2715*4882a593Smuzhiyun SetButtonMapping(ClientPtr client, DeviceIntPtr dev, int nElts, BYTE * map)
2716*4882a593Smuzhiyun {
2717*4882a593Smuzhiyun     int i;
2718*4882a593Smuzhiyun     ButtonClassPtr b = dev->button;
2719*4882a593Smuzhiyun 
2720*4882a593Smuzhiyun     if (b == NULL)
2721*4882a593Smuzhiyun         return BadMatch;
2722*4882a593Smuzhiyun 
2723*4882a593Smuzhiyun     if (nElts != b->numButtons) {
2724*4882a593Smuzhiyun         client->errorValue = nElts;
2725*4882a593Smuzhiyun         return BadValue;
2726*4882a593Smuzhiyun     }
2727*4882a593Smuzhiyun     if (BadDeviceMap(&map[0], nElts, 1, 255, &client->errorValue))
2728*4882a593Smuzhiyun         return BadValue;
2729*4882a593Smuzhiyun     for (i = 0; i < nElts; i++)
2730*4882a593Smuzhiyun         if ((b->map[i + 1] != map[i]) && BitIsOn(b->down, i + 1))
2731*4882a593Smuzhiyun             return MappingBusy;
2732*4882a593Smuzhiyun     for (i = 0; i < nElts; i++)
2733*4882a593Smuzhiyun         b->map[i + 1] = map[i];
2734*4882a593Smuzhiyun     return Success;
2735*4882a593Smuzhiyun }
2736*4882a593Smuzhiyun 
2737*4882a593Smuzhiyun int
ChangeKeyMapping(ClientPtr client,DeviceIntPtr dev,unsigned len,int type,KeyCode firstKeyCode,CARD8 keyCodes,CARD8 keySymsPerKeyCode,KeySym * map)2738*4882a593Smuzhiyun ChangeKeyMapping(ClientPtr client,
2739*4882a593Smuzhiyun                  DeviceIntPtr dev,
2740*4882a593Smuzhiyun                  unsigned len,
2741*4882a593Smuzhiyun                  int type,
2742*4882a593Smuzhiyun                  KeyCode firstKeyCode,
2743*4882a593Smuzhiyun                  CARD8 keyCodes, CARD8 keySymsPerKeyCode, KeySym * map)
2744*4882a593Smuzhiyun {
2745*4882a593Smuzhiyun     KeySymsRec keysyms;
2746*4882a593Smuzhiyun     KeyClassPtr k = dev->key;
2747*4882a593Smuzhiyun 
2748*4882a593Smuzhiyun     if (k == NULL)
2749*4882a593Smuzhiyun         return BadMatch;
2750*4882a593Smuzhiyun 
2751*4882a593Smuzhiyun     if (len != (keyCodes * keySymsPerKeyCode))
2752*4882a593Smuzhiyun         return BadLength;
2753*4882a593Smuzhiyun 
2754*4882a593Smuzhiyun     if ((firstKeyCode < k->xkbInfo->desc->min_key_code) ||
2755*4882a593Smuzhiyun         (firstKeyCode + keyCodes - 1 > k->xkbInfo->desc->max_key_code)) {
2756*4882a593Smuzhiyun         client->errorValue = firstKeyCode;
2757*4882a593Smuzhiyun         return BadValue;
2758*4882a593Smuzhiyun     }
2759*4882a593Smuzhiyun     if (keySymsPerKeyCode == 0) {
2760*4882a593Smuzhiyun         client->errorValue = 0;
2761*4882a593Smuzhiyun         return BadValue;
2762*4882a593Smuzhiyun     }
2763*4882a593Smuzhiyun     keysyms.minKeyCode = firstKeyCode;
2764*4882a593Smuzhiyun     keysyms.maxKeyCode = firstKeyCode + keyCodes - 1;
2765*4882a593Smuzhiyun     keysyms.mapWidth = keySymsPerKeyCode;
2766*4882a593Smuzhiyun     keysyms.map = map;
2767*4882a593Smuzhiyun 
2768*4882a593Smuzhiyun     XkbApplyMappingChange(dev, &keysyms, firstKeyCode, keyCodes, NULL,
2769*4882a593Smuzhiyun                           serverClient);
2770*4882a593Smuzhiyun 
2771*4882a593Smuzhiyun     return Success;
2772*4882a593Smuzhiyun }
2773*4882a593Smuzhiyun 
2774*4882a593Smuzhiyun static void
DeleteDeviceFromAnyExtEvents(WindowPtr pWin,DeviceIntPtr dev)2775*4882a593Smuzhiyun DeleteDeviceFromAnyExtEvents(WindowPtr pWin, DeviceIntPtr dev)
2776*4882a593Smuzhiyun {
2777*4882a593Smuzhiyun     WindowPtr parent;
2778*4882a593Smuzhiyun 
2779*4882a593Smuzhiyun     /* Deactivate any grabs performed on this window, before making
2780*4882a593Smuzhiyun      * any input focus changes.
2781*4882a593Smuzhiyun      * Deactivating a device grab should cause focus events. */
2782*4882a593Smuzhiyun 
2783*4882a593Smuzhiyun     if (dev->deviceGrab.grab && (dev->deviceGrab.grab->window == pWin))
2784*4882a593Smuzhiyun         (*dev->deviceGrab.DeactivateGrab) (dev);
2785*4882a593Smuzhiyun 
2786*4882a593Smuzhiyun     /* If the focus window is a root window (ie. has no parent)
2787*4882a593Smuzhiyun      * then don't delete the focus from it. */
2788*4882a593Smuzhiyun 
2789*4882a593Smuzhiyun     if (dev->focus && (pWin == dev->focus->win) && (pWin->parent != NullWindow)) {
2790*4882a593Smuzhiyun         int focusEventMode = NotifyNormal;
2791*4882a593Smuzhiyun 
2792*4882a593Smuzhiyun         /* If a grab is in progress, then alter the mode of focus events. */
2793*4882a593Smuzhiyun 
2794*4882a593Smuzhiyun         if (dev->deviceGrab.grab)
2795*4882a593Smuzhiyun             focusEventMode = NotifyWhileGrabbed;
2796*4882a593Smuzhiyun 
2797*4882a593Smuzhiyun         switch (dev->focus->revert) {
2798*4882a593Smuzhiyun         case RevertToNone:
2799*4882a593Smuzhiyun             if (!ActivateFocusInGrab(dev, pWin, NoneWin))
2800*4882a593Smuzhiyun                 DoFocusEvents(dev, pWin, NoneWin, focusEventMode);
2801*4882a593Smuzhiyun             dev->focus->win = NoneWin;
2802*4882a593Smuzhiyun             dev->focus->traceGood = 0;
2803*4882a593Smuzhiyun             break;
2804*4882a593Smuzhiyun         case RevertToParent:
2805*4882a593Smuzhiyun             parent = pWin;
2806*4882a593Smuzhiyun             do {
2807*4882a593Smuzhiyun                 parent = parent->parent;
2808*4882a593Smuzhiyun                 dev->focus->traceGood--;
2809*4882a593Smuzhiyun             }
2810*4882a593Smuzhiyun             while (!parent->realized);
2811*4882a593Smuzhiyun             if (!ActivateFocusInGrab(dev, pWin, parent))
2812*4882a593Smuzhiyun                 DoFocusEvents(dev, pWin, parent, focusEventMode);
2813*4882a593Smuzhiyun             dev->focus->win = parent;
2814*4882a593Smuzhiyun             dev->focus->revert = RevertToNone;
2815*4882a593Smuzhiyun             break;
2816*4882a593Smuzhiyun         case RevertToPointerRoot:
2817*4882a593Smuzhiyun             if (!ActivateFocusInGrab(dev, pWin, PointerRootWin))
2818*4882a593Smuzhiyun                 DoFocusEvents(dev, pWin, PointerRootWin, focusEventMode);
2819*4882a593Smuzhiyun             dev->focus->win = PointerRootWin;
2820*4882a593Smuzhiyun             dev->focus->traceGood = 0;
2821*4882a593Smuzhiyun             break;
2822*4882a593Smuzhiyun         case RevertToFollowKeyboard:
2823*4882a593Smuzhiyun         {
2824*4882a593Smuzhiyun             DeviceIntPtr kbd = GetMaster(dev, MASTER_KEYBOARD);
2825*4882a593Smuzhiyun 
2826*4882a593Smuzhiyun             if (!kbd || (kbd == dev && kbd != inputInfo.keyboard))
2827*4882a593Smuzhiyun                 kbd = inputInfo.keyboard;
2828*4882a593Smuzhiyun             if (kbd->focus->win) {
2829*4882a593Smuzhiyun                 if (!ActivateFocusInGrab(dev, pWin, kbd->focus->win))
2830*4882a593Smuzhiyun                     DoFocusEvents(dev, pWin, kbd->focus->win, focusEventMode);
2831*4882a593Smuzhiyun                 dev->focus->win = FollowKeyboardWin;
2832*4882a593Smuzhiyun                 dev->focus->traceGood = 0;
2833*4882a593Smuzhiyun             }
2834*4882a593Smuzhiyun             else {
2835*4882a593Smuzhiyun                 if (!ActivateFocusInGrab(dev, pWin, NoneWin))
2836*4882a593Smuzhiyun                     DoFocusEvents(dev, pWin, NoneWin, focusEventMode);
2837*4882a593Smuzhiyun                 dev->focus->win = NoneWin;
2838*4882a593Smuzhiyun                 dev->focus->traceGood = 0;
2839*4882a593Smuzhiyun             }
2840*4882a593Smuzhiyun         }
2841*4882a593Smuzhiyun             break;
2842*4882a593Smuzhiyun         }
2843*4882a593Smuzhiyun     }
2844*4882a593Smuzhiyun 
2845*4882a593Smuzhiyun     if (dev->valuator)
2846*4882a593Smuzhiyun         if (dev->valuator->motionHintWindow == pWin)
2847*4882a593Smuzhiyun             dev->valuator->motionHintWindow = NullWindow;
2848*4882a593Smuzhiyun }
2849*4882a593Smuzhiyun 
2850*4882a593Smuzhiyun void
DeleteWindowFromAnyExtEvents(WindowPtr pWin,Bool freeResources)2851*4882a593Smuzhiyun DeleteWindowFromAnyExtEvents(WindowPtr pWin, Bool freeResources)
2852*4882a593Smuzhiyun {
2853*4882a593Smuzhiyun     int i;
2854*4882a593Smuzhiyun     DeviceIntPtr dev;
2855*4882a593Smuzhiyun     InputClientsPtr ic;
2856*4882a593Smuzhiyun     struct _OtherInputMasks *inputMasks;
2857*4882a593Smuzhiyun 
2858*4882a593Smuzhiyun     for (dev = inputInfo.devices; dev; dev = dev->next) {
2859*4882a593Smuzhiyun         DeleteDeviceFromAnyExtEvents(pWin, dev);
2860*4882a593Smuzhiyun     }
2861*4882a593Smuzhiyun 
2862*4882a593Smuzhiyun     for (dev = inputInfo.off_devices; dev; dev = dev->next)
2863*4882a593Smuzhiyun         DeleteDeviceFromAnyExtEvents(pWin, dev);
2864*4882a593Smuzhiyun 
2865*4882a593Smuzhiyun     if (freeResources)
2866*4882a593Smuzhiyun         while ((inputMasks = wOtherInputMasks(pWin)) != 0) {
2867*4882a593Smuzhiyun             ic = inputMasks->inputClients;
2868*4882a593Smuzhiyun             for (i = 0; i < EMASKSIZE; i++)
2869*4882a593Smuzhiyun                 inputMasks->dontPropagateMask[i] = 0;
2870*4882a593Smuzhiyun             FreeResource(ic->resource, RT_NONE);
2871*4882a593Smuzhiyun         }
2872*4882a593Smuzhiyun }
2873*4882a593Smuzhiyun 
2874*4882a593Smuzhiyun int
MaybeSendDeviceMotionNotifyHint(deviceKeyButtonPointer * pEvents,Mask mask)2875*4882a593Smuzhiyun MaybeSendDeviceMotionNotifyHint(deviceKeyButtonPointer *pEvents, Mask mask)
2876*4882a593Smuzhiyun {
2877*4882a593Smuzhiyun     DeviceIntPtr dev;
2878*4882a593Smuzhiyun 
2879*4882a593Smuzhiyun     dixLookupDevice(&dev, pEvents->deviceid & DEVICE_BITS, serverClient,
2880*4882a593Smuzhiyun                     DixReadAccess);
2881*4882a593Smuzhiyun     if (!dev)
2882*4882a593Smuzhiyun         return 0;
2883*4882a593Smuzhiyun 
2884*4882a593Smuzhiyun     if (pEvents->type == DeviceMotionNotify) {
2885*4882a593Smuzhiyun         if (mask & DevicePointerMotionHintMask) {
2886*4882a593Smuzhiyun             if (WID(dev->valuator->motionHintWindow) == pEvents->event) {
2887*4882a593Smuzhiyun                 return 1;       /* don't send, but pretend we did */
2888*4882a593Smuzhiyun             }
2889*4882a593Smuzhiyun             pEvents->detail = NotifyHint;
2890*4882a593Smuzhiyun         }
2891*4882a593Smuzhiyun         else {
2892*4882a593Smuzhiyun             pEvents->detail = NotifyNormal;
2893*4882a593Smuzhiyun         }
2894*4882a593Smuzhiyun     }
2895*4882a593Smuzhiyun     return 0;
2896*4882a593Smuzhiyun }
2897*4882a593Smuzhiyun 
2898*4882a593Smuzhiyun void
CheckDeviceGrabAndHintWindow(WindowPtr pWin,int type,deviceKeyButtonPointer * xE,GrabPtr grab,ClientPtr client,Mask deliveryMask)2899*4882a593Smuzhiyun CheckDeviceGrabAndHintWindow(WindowPtr pWin, int type,
2900*4882a593Smuzhiyun                              deviceKeyButtonPointer *xE, GrabPtr grab,
2901*4882a593Smuzhiyun                              ClientPtr client, Mask deliveryMask)
2902*4882a593Smuzhiyun {
2903*4882a593Smuzhiyun     DeviceIntPtr dev;
2904*4882a593Smuzhiyun 
2905*4882a593Smuzhiyun     dixLookupDevice(&dev, xE->deviceid & DEVICE_BITS, serverClient,
2906*4882a593Smuzhiyun                     DixGrabAccess);
2907*4882a593Smuzhiyun     if (!dev)
2908*4882a593Smuzhiyun         return;
2909*4882a593Smuzhiyun 
2910*4882a593Smuzhiyun     if (type == DeviceMotionNotify)
2911*4882a593Smuzhiyun         dev->valuator->motionHintWindow = pWin;
2912*4882a593Smuzhiyun     else if ((type == DeviceButtonPress) && (!grab) &&
2913*4882a593Smuzhiyun              (deliveryMask & DeviceButtonGrabMask)) {
2914*4882a593Smuzhiyun         GrabPtr tempGrab;
2915*4882a593Smuzhiyun 
2916*4882a593Smuzhiyun         tempGrab = AllocGrab(NULL);
2917*4882a593Smuzhiyun         if (!tempGrab)
2918*4882a593Smuzhiyun             return;
2919*4882a593Smuzhiyun 
2920*4882a593Smuzhiyun         tempGrab->device = dev;
2921*4882a593Smuzhiyun         tempGrab->resource = client->clientAsMask;
2922*4882a593Smuzhiyun         tempGrab->window = pWin;
2923*4882a593Smuzhiyun         tempGrab->ownerEvents =
2924*4882a593Smuzhiyun             (deliveryMask & DeviceOwnerGrabButtonMask) ? TRUE : FALSE;
2925*4882a593Smuzhiyun         tempGrab->eventMask = deliveryMask;
2926*4882a593Smuzhiyun         tempGrab->keyboardMode = GrabModeAsync;
2927*4882a593Smuzhiyun         tempGrab->pointerMode = GrabModeAsync;
2928*4882a593Smuzhiyun         tempGrab->confineTo = NullWindow;
2929*4882a593Smuzhiyun         tempGrab->cursor = NullCursor;
2930*4882a593Smuzhiyun         tempGrab->next = NULL;
2931*4882a593Smuzhiyun         (*dev->deviceGrab.ActivateGrab) (dev, tempGrab, currentTime, TRUE);
2932*4882a593Smuzhiyun         FreeGrab(tempGrab);
2933*4882a593Smuzhiyun     }
2934*4882a593Smuzhiyun }
2935*4882a593Smuzhiyun 
2936*4882a593Smuzhiyun static Mask
DeviceEventMaskForClient(DeviceIntPtr dev,WindowPtr pWin,ClientPtr client)2937*4882a593Smuzhiyun DeviceEventMaskForClient(DeviceIntPtr dev, WindowPtr pWin, ClientPtr client)
2938*4882a593Smuzhiyun {
2939*4882a593Smuzhiyun     InputClientsPtr other;
2940*4882a593Smuzhiyun 
2941*4882a593Smuzhiyun     if (!wOtherInputMasks(pWin))
2942*4882a593Smuzhiyun         return 0;
2943*4882a593Smuzhiyun     for (other = wOtherInputMasks(pWin)->inputClients; other;
2944*4882a593Smuzhiyun          other = other->next) {
2945*4882a593Smuzhiyun         if (SameClient(other, client))
2946*4882a593Smuzhiyun             return other->mask[dev->id];
2947*4882a593Smuzhiyun     }
2948*4882a593Smuzhiyun     return 0;
2949*4882a593Smuzhiyun }
2950*4882a593Smuzhiyun 
2951*4882a593Smuzhiyun void
MaybeStopDeviceHint(DeviceIntPtr dev,ClientPtr client)2952*4882a593Smuzhiyun MaybeStopDeviceHint(DeviceIntPtr dev, ClientPtr client)
2953*4882a593Smuzhiyun {
2954*4882a593Smuzhiyun     WindowPtr pWin;
2955*4882a593Smuzhiyun     GrabPtr grab = dev->deviceGrab.grab;
2956*4882a593Smuzhiyun 
2957*4882a593Smuzhiyun     pWin = dev->valuator->motionHintWindow;
2958*4882a593Smuzhiyun 
2959*4882a593Smuzhiyun     if ((grab && SameClient(grab, client) &&
2960*4882a593Smuzhiyun          ((grab->eventMask & DevicePointerMotionHintMask) ||
2961*4882a593Smuzhiyun           (grab->ownerEvents &&
2962*4882a593Smuzhiyun            (DeviceEventMaskForClient(dev, pWin, client) &
2963*4882a593Smuzhiyun             DevicePointerMotionHintMask)))) ||
2964*4882a593Smuzhiyun         (!grab &&
2965*4882a593Smuzhiyun          (DeviceEventMaskForClient(dev, pWin, client) &
2966*4882a593Smuzhiyun           DevicePointerMotionHintMask)))
2967*4882a593Smuzhiyun         dev->valuator->motionHintWindow = NullWindow;
2968*4882a593Smuzhiyun }
2969*4882a593Smuzhiyun 
2970*4882a593Smuzhiyun int
DeviceEventSuppressForWindow(WindowPtr pWin,ClientPtr client,Mask mask,int maskndx)2971*4882a593Smuzhiyun DeviceEventSuppressForWindow(WindowPtr pWin, ClientPtr client, Mask mask,
2972*4882a593Smuzhiyun                              int maskndx)
2973*4882a593Smuzhiyun {
2974*4882a593Smuzhiyun     struct _OtherInputMasks *inputMasks = wOtherInputMasks(pWin);
2975*4882a593Smuzhiyun 
2976*4882a593Smuzhiyun     if (mask & ~PropagateMask[maskndx]) {
2977*4882a593Smuzhiyun         client->errorValue = mask;
2978*4882a593Smuzhiyun         return BadValue;
2979*4882a593Smuzhiyun     }
2980*4882a593Smuzhiyun 
2981*4882a593Smuzhiyun     if (mask == 0) {
2982*4882a593Smuzhiyun         if (inputMasks)
2983*4882a593Smuzhiyun             inputMasks->dontPropagateMask[maskndx] = mask;
2984*4882a593Smuzhiyun     }
2985*4882a593Smuzhiyun     else {
2986*4882a593Smuzhiyun         if (!inputMasks)
2987*4882a593Smuzhiyun             AddExtensionClient(pWin, client, 0, 0);
2988*4882a593Smuzhiyun         inputMasks = wOtherInputMasks(pWin);
2989*4882a593Smuzhiyun         inputMasks->dontPropagateMask[maskndx] = mask;
2990*4882a593Smuzhiyun     }
2991*4882a593Smuzhiyun     RecalculateDeviceDeliverableEvents(pWin);
2992*4882a593Smuzhiyun     if (ShouldFreeInputMasks(pWin, FALSE))
2993*4882a593Smuzhiyun         FreeResource(inputMasks->inputClients->resource, RT_NONE);
2994*4882a593Smuzhiyun     return Success;
2995*4882a593Smuzhiyun }
2996*4882a593Smuzhiyun 
2997*4882a593Smuzhiyun Bool
ShouldFreeInputMasks(WindowPtr pWin,Bool ignoreSelectedEvents)2998*4882a593Smuzhiyun ShouldFreeInputMasks(WindowPtr pWin, Bool ignoreSelectedEvents)
2999*4882a593Smuzhiyun {
3000*4882a593Smuzhiyun     int i;
3001*4882a593Smuzhiyun     Mask allInputEventMasks = 0;
3002*4882a593Smuzhiyun     struct _OtherInputMasks *inputMasks = wOtherInputMasks(pWin);
3003*4882a593Smuzhiyun 
3004*4882a593Smuzhiyun     for (i = 0; i < EMASKSIZE; i++)
3005*4882a593Smuzhiyun         allInputEventMasks |= inputMasks->dontPropagateMask[i];
3006*4882a593Smuzhiyun     if (!ignoreSelectedEvents)
3007*4882a593Smuzhiyun         for (i = 0; i < EMASKSIZE; i++)
3008*4882a593Smuzhiyun             allInputEventMasks |= inputMasks->inputEvents[i];
3009*4882a593Smuzhiyun     if (allInputEventMasks == 0)
3010*4882a593Smuzhiyun         return TRUE;
3011*4882a593Smuzhiyun     else
3012*4882a593Smuzhiyun         return FALSE;
3013*4882a593Smuzhiyun }
3014*4882a593Smuzhiyun 
3015*4882a593Smuzhiyun /***********************************************************************
3016*4882a593Smuzhiyun  *
3017*4882a593Smuzhiyun  * Walk through the window tree, finding all clients that want to know
3018*4882a593Smuzhiyun  * about the Event.
3019*4882a593Smuzhiyun  *
3020*4882a593Smuzhiyun  */
3021*4882a593Smuzhiyun 
3022*4882a593Smuzhiyun static void
FindInterestedChildren(DeviceIntPtr dev,WindowPtr p1,Mask mask,xEvent * ev,int count)3023*4882a593Smuzhiyun FindInterestedChildren(DeviceIntPtr dev, WindowPtr p1, Mask mask,
3024*4882a593Smuzhiyun                        xEvent *ev, int count)
3025*4882a593Smuzhiyun {
3026*4882a593Smuzhiyun     WindowPtr p2;
3027*4882a593Smuzhiyun 
3028*4882a593Smuzhiyun     while (p1) {
3029*4882a593Smuzhiyun         p2 = p1->firstChild;
3030*4882a593Smuzhiyun         DeliverEventsToWindow(dev, p1, ev, count, mask, NullGrab);
3031*4882a593Smuzhiyun         FindInterestedChildren(dev, p2, mask, ev, count);
3032*4882a593Smuzhiyun         p1 = p1->nextSib;
3033*4882a593Smuzhiyun     }
3034*4882a593Smuzhiyun }
3035*4882a593Smuzhiyun 
3036*4882a593Smuzhiyun /***********************************************************************
3037*4882a593Smuzhiyun  *
3038*4882a593Smuzhiyun  * Send an event to interested clients in all windows on all screens.
3039*4882a593Smuzhiyun  *
3040*4882a593Smuzhiyun  */
3041*4882a593Smuzhiyun 
3042*4882a593Smuzhiyun void
SendEventToAllWindows(DeviceIntPtr dev,Mask mask,xEvent * ev,int count)3043*4882a593Smuzhiyun SendEventToAllWindows(DeviceIntPtr dev, Mask mask, xEvent *ev, int count)
3044*4882a593Smuzhiyun {
3045*4882a593Smuzhiyun     int i;
3046*4882a593Smuzhiyun     WindowPtr pWin, p1;
3047*4882a593Smuzhiyun 
3048*4882a593Smuzhiyun     for (i = 0; i < screenInfo.numScreens; i++) {
3049*4882a593Smuzhiyun         pWin = screenInfo.screens[i]->root;
3050*4882a593Smuzhiyun         if (!pWin)
3051*4882a593Smuzhiyun             continue;
3052*4882a593Smuzhiyun         DeliverEventsToWindow(dev, pWin, ev, count, mask, NullGrab);
3053*4882a593Smuzhiyun         p1 = pWin->firstChild;
3054*4882a593Smuzhiyun         FindInterestedChildren(dev, p1, mask, ev, count);
3055*4882a593Smuzhiyun     }
3056*4882a593Smuzhiyun }
3057*4882a593Smuzhiyun 
3058*4882a593Smuzhiyun /**
3059*4882a593Smuzhiyun  * Set the XI2 mask for the given client on the given window.
3060*4882a593Smuzhiyun  * @param dev The device to set the mask for.
3061*4882a593Smuzhiyun  * @param win The window to set the mask on.
3062*4882a593Smuzhiyun  * @param client The client setting the mask.
3063*4882a593Smuzhiyun  * @param len Number of bytes in mask.
3064*4882a593Smuzhiyun  * @param mask Event mask in the form of (1 << eventtype)
3065*4882a593Smuzhiyun  */
3066*4882a593Smuzhiyun int
XISetEventMask(DeviceIntPtr dev,WindowPtr win,ClientPtr client,unsigned int len,unsigned char * mask)3067*4882a593Smuzhiyun XISetEventMask(DeviceIntPtr dev, WindowPtr win, ClientPtr client,
3068*4882a593Smuzhiyun                unsigned int len, unsigned char *mask)
3069*4882a593Smuzhiyun {
3070*4882a593Smuzhiyun     OtherInputMasks *masks;
3071*4882a593Smuzhiyun     InputClientsPtr others = NULL;
3072*4882a593Smuzhiyun 
3073*4882a593Smuzhiyun     masks = wOtherInputMasks(win);
3074*4882a593Smuzhiyun     if (masks) {
3075*4882a593Smuzhiyun         for (others = wOtherInputMasks(win)->inputClients; others;
3076*4882a593Smuzhiyun              others = others->next) {
3077*4882a593Smuzhiyun             if (SameClient(others, client)) {
3078*4882a593Smuzhiyun                 xi2mask_zero(others->xi2mask, dev->id);
3079*4882a593Smuzhiyun                 break;
3080*4882a593Smuzhiyun             }
3081*4882a593Smuzhiyun         }
3082*4882a593Smuzhiyun     }
3083*4882a593Smuzhiyun 
3084*4882a593Smuzhiyun     if (len && !others) {
3085*4882a593Smuzhiyun         if (AddExtensionClient(win, client, 0, 0) != Success)
3086*4882a593Smuzhiyun             return BadAlloc;
3087*4882a593Smuzhiyun         others = wOtherInputMasks(win)->inputClients;
3088*4882a593Smuzhiyun     }
3089*4882a593Smuzhiyun 
3090*4882a593Smuzhiyun     if (others) {
3091*4882a593Smuzhiyun         xi2mask_zero(others->xi2mask, dev->id);
3092*4882a593Smuzhiyun         len = min(len, xi2mask_mask_size(others->xi2mask));
3093*4882a593Smuzhiyun     }
3094*4882a593Smuzhiyun 
3095*4882a593Smuzhiyun     if (len) {
3096*4882a593Smuzhiyun         xi2mask_set_one_mask(others->xi2mask, dev->id, mask, len);
3097*4882a593Smuzhiyun     }
3098*4882a593Smuzhiyun 
3099*4882a593Smuzhiyun     RecalculateDeviceDeliverableEvents(win);
3100*4882a593Smuzhiyun 
3101*4882a593Smuzhiyun     return Success;
3102*4882a593Smuzhiyun }
3103