1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright © 2008 Red Hat, Inc.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Permission is hereby granted, free of charge, to any person obtaining a
5*4882a593Smuzhiyun * copy of this software and associated documentation files (the "Software"),
6*4882a593Smuzhiyun * to deal in the Software without restriction, including without limitation
7*4882a593Smuzhiyun * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*4882a593Smuzhiyun * and/or sell copies of the Software, and to permit persons to whom the
9*4882a593Smuzhiyun * Software is furnished to do so, subject to the following conditions:
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * The above copyright notice and this permission notice (including the next
12*4882a593Smuzhiyun * paragraph) shall be included in all copies or substantial portions of the
13*4882a593Smuzhiyun * Software.
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*4882a593Smuzhiyun * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*4882a593Smuzhiyun * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18*4882a593Smuzhiyun * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*4882a593Smuzhiyun * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*4882a593Smuzhiyun * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21*4882a593Smuzhiyun * DEALINGS IN THE SOFTWARE.
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun * Authors: Peter Hutterer
24*4882a593Smuzhiyun *
25*4882a593Smuzhiyun */
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #ifdef HAVE_DIX_CONFIG_H
28*4882a593Smuzhiyun #include <dix-config.h>
29*4882a593Smuzhiyun #endif
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #include <X11/X.h>
32*4882a593Smuzhiyun #include <X11/extensions/XI2.h>
33*4882a593Smuzhiyun #include <X11/extensions/XIproto.h>
34*4882a593Smuzhiyun #include <X11/extensions/XI2proto.h>
35*4882a593Smuzhiyun #include "inputstr.h"
36*4882a593Smuzhiyun #include "windowstr.h"
37*4882a593Smuzhiyun #include "scrnintstr.h"
38*4882a593Smuzhiyun #include "exglobals.h"
39*4882a593Smuzhiyun #include "enterleave.h"
40*4882a593Smuzhiyun #include "eventconvert.h"
41*4882a593Smuzhiyun #include "xkbsrv.h"
42*4882a593Smuzhiyun #include "inpututils.h"
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun /**
45*4882a593Smuzhiyun * @file
46*4882a593Smuzhiyun * This file describes the model for sending core enter/leave events and
47*4882a593Smuzhiyun * focus in/out in the case of multiple pointers/keyboard foci.
48*4882a593Smuzhiyun *
49*4882a593Smuzhiyun * Since we can't send more than one Enter or Leave/Focus in or out event per
50*4882a593Smuzhiyun * window to a core client without confusing it, this is a rather complicated
51*4882a593Smuzhiyun * approach.
52*4882a593Smuzhiyun *
53*4882a593Smuzhiyun * For a full description of the enter/leave model from a window's
54*4882a593Smuzhiyun * perspective, see
55*4882a593Smuzhiyun * http://lists.freedesktop.org/archives/xorg/2008-August/037606.html
56*4882a593Smuzhiyun *
57*4882a593Smuzhiyun * For a full description of the focus in/out model from a window's
58*4882a593Smuzhiyun * perspective, see
59*4882a593Smuzhiyun * http://lists.freedesktop.org/archives/xorg/2008-December/041740.html
60*4882a593Smuzhiyun *
61*4882a593Smuzhiyun * Additional notes:
62*4882a593Smuzhiyun * - The core protocol spec says that "In a LeaveNotify event, if a child of the
63*4882a593Smuzhiyun * event window contains the initial position of the pointer, then the child
64*4882a593Smuzhiyun * component is set to that child. Otherwise, it is None. For an EnterNotify
65*4882a593Smuzhiyun * event, if a child of the event window contains the final pointer position,
66*4882a593Smuzhiyun * then the child component is set to that child. Otherwise, it is None."
67*4882a593Smuzhiyun *
68*4882a593Smuzhiyun * By inference, this means that only NotifyVirtual or NotifyNonlinearVirtual
69*4882a593Smuzhiyun * events may have a subwindow set to other than None.
70*4882a593Smuzhiyun *
71*4882a593Smuzhiyun * - NotifyPointer events may be sent if the focus changes from window A to
72*4882a593Smuzhiyun * B. The assumption used in this model is that NotifyPointer events are only
73*4882a593Smuzhiyun * sent for the pointer paired with the keyboard that is involved in the focus
74*4882a593Smuzhiyun * events. For example, if F(W) changes because of keyboard 2, then
75*4882a593Smuzhiyun * NotifyPointer events are only sent for pointer 2.
76*4882a593Smuzhiyun */
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun static WindowPtr PointerWindows[MAXDEVICES];
79*4882a593Smuzhiyun static WindowPtr FocusWindows[MAXDEVICES];
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun /**
82*4882a593Smuzhiyun * Return TRUE if 'win' has a pointer within its boundaries, excluding child
83*4882a593Smuzhiyun * window.
84*4882a593Smuzhiyun */
85*4882a593Smuzhiyun static BOOL
HasPointer(DeviceIntPtr dev,WindowPtr win)86*4882a593Smuzhiyun HasPointer(DeviceIntPtr dev, WindowPtr win)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun int i;
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun /* FIXME: The enter/leave model does not cater for grabbed devices. For
91*4882a593Smuzhiyun * now, a quickfix: if the device about to send an enter/leave event to
92*4882a593Smuzhiyun * a window is grabbed, assume there is no pointer in that window.
93*4882a593Smuzhiyun * Fixes fdo 27804.
94*4882a593Smuzhiyun * There isn't enough beer in my fridge to fix this properly.
95*4882a593Smuzhiyun */
96*4882a593Smuzhiyun if (dev->deviceGrab.grab)
97*4882a593Smuzhiyun return FALSE;
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun for (i = 0; i < MAXDEVICES; i++)
100*4882a593Smuzhiyun if (PointerWindows[i] == win)
101*4882a593Smuzhiyun return TRUE;
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun return FALSE;
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun /**
107*4882a593Smuzhiyun * Return TRUE if at least one keyboard focus is set to 'win' (excluding
108*4882a593Smuzhiyun * descendants of win).
109*4882a593Smuzhiyun */
110*4882a593Smuzhiyun static BOOL
HasFocus(WindowPtr win)111*4882a593Smuzhiyun HasFocus(WindowPtr win)
112*4882a593Smuzhiyun {
113*4882a593Smuzhiyun int i;
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun for (i = 0; i < MAXDEVICES; i++)
116*4882a593Smuzhiyun if (FocusWindows[i] == win)
117*4882a593Smuzhiyun return TRUE;
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun return FALSE;
120*4882a593Smuzhiyun }
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun /**
123*4882a593Smuzhiyun * Return the window the device dev is currently on.
124*4882a593Smuzhiyun */
125*4882a593Smuzhiyun static WindowPtr
PointerWin(DeviceIntPtr dev)126*4882a593Smuzhiyun PointerWin(DeviceIntPtr dev)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun return PointerWindows[dev->id];
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun /**
132*4882a593Smuzhiyun * Search for the first window below 'win' that has a pointer directly within
133*4882a593Smuzhiyun * it's boundaries (excluding boundaries of its own descendants).
134*4882a593Smuzhiyun *
135*4882a593Smuzhiyun * @return The child window that has the pointer within its boundaries or
136*4882a593Smuzhiyun * NULL.
137*4882a593Smuzhiyun */
138*4882a593Smuzhiyun static WindowPtr
FirstPointerChild(WindowPtr win)139*4882a593Smuzhiyun FirstPointerChild(WindowPtr win)
140*4882a593Smuzhiyun {
141*4882a593Smuzhiyun int i;
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun for (i = 0; i < MAXDEVICES; i++) {
144*4882a593Smuzhiyun if (PointerWindows[i] && IsParent(win, PointerWindows[i]))
145*4882a593Smuzhiyun return PointerWindows[i];
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun return NULL;
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun /**
152*4882a593Smuzhiyun * Search for the first window below 'win' that has a focus directly within
153*4882a593Smuzhiyun * it's boundaries (excluding boundaries of its own descendants).
154*4882a593Smuzhiyun *
155*4882a593Smuzhiyun * @return The child window that has the pointer within its boundaries or
156*4882a593Smuzhiyun * NULL.
157*4882a593Smuzhiyun */
158*4882a593Smuzhiyun static WindowPtr
FirstFocusChild(WindowPtr win)159*4882a593Smuzhiyun FirstFocusChild(WindowPtr win)
160*4882a593Smuzhiyun {
161*4882a593Smuzhiyun int i;
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun for (i = 0; i < MAXDEVICES; i++) {
164*4882a593Smuzhiyun if (FocusWindows[i] && FocusWindows[i] != PointerRootWin &&
165*4882a593Smuzhiyun IsParent(win, FocusWindows[i]))
166*4882a593Smuzhiyun return FocusWindows[i];
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun return NULL;
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun /**
173*4882a593Smuzhiyun * Set the presence flag for dev to mark that it is now in 'win'.
174*4882a593Smuzhiyun */
175*4882a593Smuzhiyun void
EnterWindow(DeviceIntPtr dev,WindowPtr win,int mode)176*4882a593Smuzhiyun EnterWindow(DeviceIntPtr dev, WindowPtr win, int mode)
177*4882a593Smuzhiyun {
178*4882a593Smuzhiyun PointerWindows[dev->id] = win;
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun /**
182*4882a593Smuzhiyun * Unset the presence flag for dev to mark that it is not in 'win' anymore.
183*4882a593Smuzhiyun */
184*4882a593Smuzhiyun void
LeaveWindow(DeviceIntPtr dev)185*4882a593Smuzhiyun LeaveWindow(DeviceIntPtr dev)
186*4882a593Smuzhiyun {
187*4882a593Smuzhiyun PointerWindows[dev->id] = NULL;
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun /**
191*4882a593Smuzhiyun * Set the presence flag for dev to mark that it is now in 'win'.
192*4882a593Smuzhiyun */
193*4882a593Smuzhiyun void
SetFocusIn(DeviceIntPtr dev,WindowPtr win)194*4882a593Smuzhiyun SetFocusIn(DeviceIntPtr dev, WindowPtr win)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun FocusWindows[dev->id] = win;
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun /**
200*4882a593Smuzhiyun * Unset the presence flag for dev to mark that it is not in 'win' anymore.
201*4882a593Smuzhiyun */
202*4882a593Smuzhiyun void
SetFocusOut(DeviceIntPtr dev)203*4882a593Smuzhiyun SetFocusOut(DeviceIntPtr dev)
204*4882a593Smuzhiyun {
205*4882a593Smuzhiyun FocusWindows[dev->id] = NULL;
206*4882a593Smuzhiyun }
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun /**
209*4882a593Smuzhiyun * Return the common ancestor of 'a' and 'b' (if one exists).
210*4882a593Smuzhiyun * @param a A window with the same ancestor as b.
211*4882a593Smuzhiyun * @param b A window with the same ancestor as a.
212*4882a593Smuzhiyun * @return The window that is the first ancestor of both 'a' and 'b', or the
213*4882a593Smuzhiyun * NullWindow if they do not have a common ancestor.
214*4882a593Smuzhiyun */
215*4882a593Smuzhiyun static WindowPtr
CommonAncestor(WindowPtr a,WindowPtr b)216*4882a593Smuzhiyun CommonAncestor(WindowPtr a, WindowPtr b)
217*4882a593Smuzhiyun {
218*4882a593Smuzhiyun for (b = b->parent; b; b = b->parent)
219*4882a593Smuzhiyun if (IsParent(b, a))
220*4882a593Smuzhiyun return b;
221*4882a593Smuzhiyun return NullWindow;
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun /**
225*4882a593Smuzhiyun * Send enter notifies to all windows between 'ancestor' and 'child' (excluding
226*4882a593Smuzhiyun * both). Events are sent running up the window hierarchy. This function
227*4882a593Smuzhiyun * recurses.
228*4882a593Smuzhiyun */
229*4882a593Smuzhiyun static void
DeviceEnterNotifies(DeviceIntPtr dev,int sourceid,WindowPtr ancestor,WindowPtr child,int mode,int detail)230*4882a593Smuzhiyun DeviceEnterNotifies(DeviceIntPtr dev,
231*4882a593Smuzhiyun int sourceid,
232*4882a593Smuzhiyun WindowPtr ancestor, WindowPtr child, int mode, int detail)
233*4882a593Smuzhiyun {
234*4882a593Smuzhiyun WindowPtr parent = child->parent;
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun if (ancestor == parent)
237*4882a593Smuzhiyun return;
238*4882a593Smuzhiyun DeviceEnterNotifies(dev, sourceid, ancestor, parent, mode, detail);
239*4882a593Smuzhiyun DeviceEnterLeaveEvent(dev, sourceid, XI_Enter, mode, detail, parent,
240*4882a593Smuzhiyun child->drawable.id);
241*4882a593Smuzhiyun }
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun /**
244*4882a593Smuzhiyun * Send enter notifies to all windows between 'ancestor' and 'child' (excluding
245*4882a593Smuzhiyun * both). Events are sent running down the window hierarchy. This function
246*4882a593Smuzhiyun * recurses.
247*4882a593Smuzhiyun */
248*4882a593Smuzhiyun static void
CoreEnterNotifies(DeviceIntPtr dev,WindowPtr ancestor,WindowPtr child,int mode,int detail)249*4882a593Smuzhiyun CoreEnterNotifies(DeviceIntPtr dev,
250*4882a593Smuzhiyun WindowPtr ancestor, WindowPtr child, int mode, int detail)
251*4882a593Smuzhiyun {
252*4882a593Smuzhiyun WindowPtr parent = child->parent;
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun if (ancestor == parent)
255*4882a593Smuzhiyun return;
256*4882a593Smuzhiyun CoreEnterNotifies(dev, ancestor, parent, mode, detail);
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun /* Case 3:
259*4882a593Smuzhiyun A is above W, B is a descendant
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun Classically: The move generates an EnterNotify on W with a detail of
262*4882a593Smuzhiyun Virtual or NonlinearVirtual
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun MPX:
265*4882a593Smuzhiyun Case 3A: There is at least one other pointer on W itself
266*4882a593Smuzhiyun P(W) doesn't change, so the event should be suppressed
267*4882a593Smuzhiyun Case 3B: Otherwise, if there is at least one other pointer in a
268*4882a593Smuzhiyun descendant
269*4882a593Smuzhiyun P(W) stays on the same descendant, or changes to a different
270*4882a593Smuzhiyun descendant. The event should be suppressed.
271*4882a593Smuzhiyun Case 3C: Otherwise:
272*4882a593Smuzhiyun P(W) moves from a window above W to a descendant. The subwindow
273*4882a593Smuzhiyun field is set to the child containing the descendant. The detail
274*4882a593Smuzhiyun may need to be changed from Virtual to NonlinearVirtual depending
275*4882a593Smuzhiyun on the previous P(W). */
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun if (!HasPointer(dev, parent) && !FirstPointerChild(parent))
278*4882a593Smuzhiyun CoreEnterLeaveEvent(dev, EnterNotify, mode, detail, parent,
279*4882a593Smuzhiyun child->drawable.id);
280*4882a593Smuzhiyun }
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun static void
CoreLeaveNotifies(DeviceIntPtr dev,WindowPtr child,WindowPtr ancestor,int mode,int detail)283*4882a593Smuzhiyun CoreLeaveNotifies(DeviceIntPtr dev,
284*4882a593Smuzhiyun WindowPtr child, WindowPtr ancestor, int mode, int detail)
285*4882a593Smuzhiyun {
286*4882a593Smuzhiyun WindowPtr win;
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun if (ancestor == child)
289*4882a593Smuzhiyun return;
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun for (win = child->parent; win != ancestor; win = win->parent) {
292*4882a593Smuzhiyun /*Case 7:
293*4882a593Smuzhiyun A is a descendant of W, B is above W
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun Classically: A LeaveNotify is generated on W with a detail of Virtual
296*4882a593Smuzhiyun or NonlinearVirtual.
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun MPX:
299*4882a593Smuzhiyun Case 3A: There is at least one other pointer on W itself
300*4882a593Smuzhiyun P(W) doesn't change, the event should be suppressed.
301*4882a593Smuzhiyun Case 3B: Otherwise, if there is at least one other pointer in a
302*4882a593Smuzhiyun descendant
303*4882a593Smuzhiyun P(W) stays on the same descendant, or changes to a different
304*4882a593Smuzhiyun descendant. The event should be suppressed.
305*4882a593Smuzhiyun Case 3C: Otherwise:
306*4882a593Smuzhiyun P(W) changes from the descendant of W to a window above W.
307*4882a593Smuzhiyun The detail may need to be changed from Virtual to NonlinearVirtual
308*4882a593Smuzhiyun or vice-versa depending on the new P(W). */
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun /* If one window has a pointer or a child with a pointer, skip some
311*4882a593Smuzhiyun * work and exit. */
312*4882a593Smuzhiyun if (HasPointer(dev, win) || FirstPointerChild(win))
313*4882a593Smuzhiyun return;
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun CoreEnterLeaveEvent(dev, LeaveNotify, mode, detail, win,
316*4882a593Smuzhiyun child->drawable.id);
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun child = win;
319*4882a593Smuzhiyun }
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun /**
323*4882a593Smuzhiyun * Send leave notifies to all windows between 'child' and 'ancestor'.
324*4882a593Smuzhiyun * Events are sent running up the hierarchy.
325*4882a593Smuzhiyun */
326*4882a593Smuzhiyun static void
DeviceLeaveNotifies(DeviceIntPtr dev,int sourceid,WindowPtr child,WindowPtr ancestor,int mode,int detail)327*4882a593Smuzhiyun DeviceLeaveNotifies(DeviceIntPtr dev,
328*4882a593Smuzhiyun int sourceid,
329*4882a593Smuzhiyun WindowPtr child, WindowPtr ancestor, int mode, int detail)
330*4882a593Smuzhiyun {
331*4882a593Smuzhiyun WindowPtr win;
332*4882a593Smuzhiyun
333*4882a593Smuzhiyun if (ancestor == child)
334*4882a593Smuzhiyun return;
335*4882a593Smuzhiyun for (win = child->parent; win != ancestor; win = win->parent) {
336*4882a593Smuzhiyun DeviceEnterLeaveEvent(dev, sourceid, XI_Leave, mode, detail, win,
337*4882a593Smuzhiyun child->drawable.id);
338*4882a593Smuzhiyun child = win;
339*4882a593Smuzhiyun }
340*4882a593Smuzhiyun }
341*4882a593Smuzhiyun
342*4882a593Smuzhiyun /**
343*4882a593Smuzhiyun * Pointer dev moves from A to B and A neither a descendant of B nor is
344*4882a593Smuzhiyun * B a descendant of A.
345*4882a593Smuzhiyun */
346*4882a593Smuzhiyun static void
CoreEnterLeaveNonLinear(DeviceIntPtr dev,WindowPtr A,WindowPtr B,int mode)347*4882a593Smuzhiyun CoreEnterLeaveNonLinear(DeviceIntPtr dev, WindowPtr A, WindowPtr B, int mode)
348*4882a593Smuzhiyun {
349*4882a593Smuzhiyun WindowPtr X = CommonAncestor(A, B);
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun /* Case 4:
352*4882a593Smuzhiyun A is W, B is above W
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun Classically: The move generates a LeaveNotify on W with a detail of
355*4882a593Smuzhiyun Ancestor or Nonlinear
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun MPX:
358*4882a593Smuzhiyun Case 3A: There is at least one other pointer on W itself
359*4882a593Smuzhiyun P(W) doesn't change, the event should be suppressed
360*4882a593Smuzhiyun Case 3B: Otherwise, if there is at least one other pointer in a
361*4882a593Smuzhiyun descendant of W
362*4882a593Smuzhiyun P(W) changes from W to a descendant of W. The subwindow field
363*4882a593Smuzhiyun is set to the child containing the new P(W), the detail field
364*4882a593Smuzhiyun is set to Inferior
365*4882a593Smuzhiyun Case 3C: Otherwise:
366*4882a593Smuzhiyun The pointer window moves from W to a window above W.
367*4882a593Smuzhiyun The detail may need to be changed from Ancestor to Nonlinear or
368*4882a593Smuzhiyun vice versa depending on the the new P(W)
369*4882a593Smuzhiyun */
370*4882a593Smuzhiyun
371*4882a593Smuzhiyun if (!HasPointer(dev, A)) {
372*4882a593Smuzhiyun WindowPtr child = FirstPointerChild(A);
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun if (child)
375*4882a593Smuzhiyun CoreEnterLeaveEvent(dev, LeaveNotify, mode, NotifyInferior, A,
376*4882a593Smuzhiyun None);
377*4882a593Smuzhiyun else
378*4882a593Smuzhiyun CoreEnterLeaveEvent(dev, LeaveNotify, mode, NotifyNonlinear, A,
379*4882a593Smuzhiyun None);
380*4882a593Smuzhiyun }
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun CoreLeaveNotifies(dev, A, X, mode, NotifyNonlinearVirtual);
383*4882a593Smuzhiyun
384*4882a593Smuzhiyun /*
385*4882a593Smuzhiyun Case 9:
386*4882a593Smuzhiyun A is a descendant of W, B is a descendant of W
387*4882a593Smuzhiyun
388*4882a593Smuzhiyun Classically: No events are generated on W
389*4882a593Smuzhiyun MPX: The pointer window stays the same or moves to a different
390*4882a593Smuzhiyun descendant of W. No events should be generated on W.
391*4882a593Smuzhiyun
392*4882a593Smuzhiyun Therefore, no event to X.
393*4882a593Smuzhiyun */
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun CoreEnterNotifies(dev, X, B, mode, NotifyNonlinearVirtual);
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun /* Case 2:
398*4882a593Smuzhiyun A is above W, B=W
399*4882a593Smuzhiyun
400*4882a593Smuzhiyun Classically: The move generates an EnterNotify on W with a detail of
401*4882a593Smuzhiyun Ancestor or Nonlinear
402*4882a593Smuzhiyun
403*4882a593Smuzhiyun MPX:
404*4882a593Smuzhiyun Case 2A: There is at least one other pointer on W itself
405*4882a593Smuzhiyun P(W) doesn't change, so the event should be suppressed
406*4882a593Smuzhiyun Case 2B: Otherwise, if there is at least one other pointer in a
407*4882a593Smuzhiyun descendant
408*4882a593Smuzhiyun P(W) moves from a descendant to W. detail is changed to Inferior,
409*4882a593Smuzhiyun subwindow is set to the child containing the previous P(W)
410*4882a593Smuzhiyun Case 2C: Otherwise:
411*4882a593Smuzhiyun P(W) changes from a window above W to W itself.
412*4882a593Smuzhiyun The detail may need to be changed from Ancestor to Nonlinear
413*4882a593Smuzhiyun or vice-versa depending on the previous P(W). */
414*4882a593Smuzhiyun
415*4882a593Smuzhiyun if (!HasPointer(dev, B)) {
416*4882a593Smuzhiyun WindowPtr child = FirstPointerChild(B);
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun if (child)
419*4882a593Smuzhiyun CoreEnterLeaveEvent(dev, EnterNotify, mode, NotifyInferior, B,
420*4882a593Smuzhiyun None);
421*4882a593Smuzhiyun else
422*4882a593Smuzhiyun CoreEnterLeaveEvent(dev, EnterNotify, mode, NotifyNonlinear, B,
423*4882a593Smuzhiyun None);
424*4882a593Smuzhiyun }
425*4882a593Smuzhiyun }
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun /**
428*4882a593Smuzhiyun * Pointer dev moves from A to B and A is a descendant of B.
429*4882a593Smuzhiyun */
430*4882a593Smuzhiyun static void
CoreEnterLeaveToAncestor(DeviceIntPtr dev,WindowPtr A,WindowPtr B,int mode)431*4882a593Smuzhiyun CoreEnterLeaveToAncestor(DeviceIntPtr dev, WindowPtr A, WindowPtr B, int mode)
432*4882a593Smuzhiyun {
433*4882a593Smuzhiyun /* Case 4:
434*4882a593Smuzhiyun A is W, B is above W
435*4882a593Smuzhiyun
436*4882a593Smuzhiyun Classically: The move generates a LeaveNotify on W with a detail of
437*4882a593Smuzhiyun Ancestor or Nonlinear
438*4882a593Smuzhiyun
439*4882a593Smuzhiyun MPX:
440*4882a593Smuzhiyun Case 3A: There is at least one other pointer on W itself
441*4882a593Smuzhiyun P(W) doesn't change, the event should be suppressed
442*4882a593Smuzhiyun Case 3B: Otherwise, if there is at least one other pointer in a
443*4882a593Smuzhiyun descendant of W
444*4882a593Smuzhiyun P(W) changes from W to a descendant of W. The subwindow field
445*4882a593Smuzhiyun is set to the child containing the new P(W), the detail field
446*4882a593Smuzhiyun is set to Inferior
447*4882a593Smuzhiyun Case 3C: Otherwise:
448*4882a593Smuzhiyun The pointer window moves from W to a window above W.
449*4882a593Smuzhiyun The detail may need to be changed from Ancestor to Nonlinear or
450*4882a593Smuzhiyun vice versa depending on the the new P(W)
451*4882a593Smuzhiyun */
452*4882a593Smuzhiyun if (!HasPointer(dev, A)) {
453*4882a593Smuzhiyun WindowPtr child = FirstPointerChild(A);
454*4882a593Smuzhiyun
455*4882a593Smuzhiyun if (child)
456*4882a593Smuzhiyun CoreEnterLeaveEvent(dev, LeaveNotify, mode, NotifyInferior, A,
457*4882a593Smuzhiyun None);
458*4882a593Smuzhiyun else
459*4882a593Smuzhiyun CoreEnterLeaveEvent(dev, LeaveNotify, mode, NotifyAncestor, A,
460*4882a593Smuzhiyun None);
461*4882a593Smuzhiyun }
462*4882a593Smuzhiyun
463*4882a593Smuzhiyun CoreLeaveNotifies(dev, A, B, mode, NotifyVirtual);
464*4882a593Smuzhiyun
465*4882a593Smuzhiyun /* Case 8:
466*4882a593Smuzhiyun A is a descendant of W, B is W
467*4882a593Smuzhiyun
468*4882a593Smuzhiyun Classically: A EnterNotify is generated on W with a detail of
469*4882a593Smuzhiyun NotifyInferior
470*4882a593Smuzhiyun
471*4882a593Smuzhiyun MPX:
472*4882a593Smuzhiyun Case 3A: There is at least one other pointer on W itself
473*4882a593Smuzhiyun P(W) doesn't change, the event should be suppressed
474*4882a593Smuzhiyun Case 3B: Otherwise:
475*4882a593Smuzhiyun P(W) changes from a descendant to W itself. The subwindow
476*4882a593Smuzhiyun field should be set to the child containing the old P(W) <<< WRONG */
477*4882a593Smuzhiyun
478*4882a593Smuzhiyun if (!HasPointer(dev, B))
479*4882a593Smuzhiyun CoreEnterLeaveEvent(dev, EnterNotify, mode, NotifyInferior, B, None);
480*4882a593Smuzhiyun
481*4882a593Smuzhiyun }
482*4882a593Smuzhiyun
483*4882a593Smuzhiyun /**
484*4882a593Smuzhiyun * Pointer dev moves from A to B and B is a descendant of A.
485*4882a593Smuzhiyun */
486*4882a593Smuzhiyun static void
CoreEnterLeaveToDescendant(DeviceIntPtr dev,WindowPtr A,WindowPtr B,int mode)487*4882a593Smuzhiyun CoreEnterLeaveToDescendant(DeviceIntPtr dev, WindowPtr A, WindowPtr B, int mode)
488*4882a593Smuzhiyun {
489*4882a593Smuzhiyun /* Case 6:
490*4882a593Smuzhiyun A is W, B is a descendant of W
491*4882a593Smuzhiyun
492*4882a593Smuzhiyun Classically: A LeaveNotify is generated on W with a detail of
493*4882a593Smuzhiyun NotifyInferior
494*4882a593Smuzhiyun
495*4882a593Smuzhiyun MPX:
496*4882a593Smuzhiyun Case 3A: There is at least one other pointer on W itself
497*4882a593Smuzhiyun P(W) doesn't change, the event should be suppressed
498*4882a593Smuzhiyun Case 3B: Otherwise:
499*4882a593Smuzhiyun P(W) changes from W to a descendant of W. The subwindow field
500*4882a593Smuzhiyun is set to the child containing the new P(W) <<< THIS IS WRONG */
501*4882a593Smuzhiyun
502*4882a593Smuzhiyun if (!HasPointer(dev, A))
503*4882a593Smuzhiyun CoreEnterLeaveEvent(dev, LeaveNotify, mode, NotifyInferior, A, None);
504*4882a593Smuzhiyun
505*4882a593Smuzhiyun CoreEnterNotifies(dev, A, B, mode, NotifyVirtual);
506*4882a593Smuzhiyun
507*4882a593Smuzhiyun /* Case 2:
508*4882a593Smuzhiyun A is above W, B=W
509*4882a593Smuzhiyun
510*4882a593Smuzhiyun Classically: The move generates an EnterNotify on W with a detail of
511*4882a593Smuzhiyun Ancestor or Nonlinear
512*4882a593Smuzhiyun
513*4882a593Smuzhiyun MPX:
514*4882a593Smuzhiyun Case 2A: There is at least one other pointer on W itself
515*4882a593Smuzhiyun P(W) doesn't change, so the event should be suppressed
516*4882a593Smuzhiyun Case 2B: Otherwise, if there is at least one other pointer in a
517*4882a593Smuzhiyun descendant
518*4882a593Smuzhiyun P(W) moves from a descendant to W. detail is changed to Inferior,
519*4882a593Smuzhiyun subwindow is set to the child containing the previous P(W)
520*4882a593Smuzhiyun Case 2C: Otherwise:
521*4882a593Smuzhiyun P(W) changes from a window above W to W itself.
522*4882a593Smuzhiyun The detail may need to be changed from Ancestor to Nonlinear
523*4882a593Smuzhiyun or vice-versa depending on the previous P(W). */
524*4882a593Smuzhiyun
525*4882a593Smuzhiyun if (!HasPointer(dev, B)) {
526*4882a593Smuzhiyun WindowPtr child = FirstPointerChild(B);
527*4882a593Smuzhiyun
528*4882a593Smuzhiyun if (child)
529*4882a593Smuzhiyun CoreEnterLeaveEvent(dev, EnterNotify, mode, NotifyInferior, B,
530*4882a593Smuzhiyun None);
531*4882a593Smuzhiyun else
532*4882a593Smuzhiyun CoreEnterLeaveEvent(dev, EnterNotify, mode, NotifyAncestor, B,
533*4882a593Smuzhiyun None);
534*4882a593Smuzhiyun }
535*4882a593Smuzhiyun }
536*4882a593Smuzhiyun
537*4882a593Smuzhiyun static void
CoreEnterLeaveEvents(DeviceIntPtr dev,WindowPtr from,WindowPtr to,int mode)538*4882a593Smuzhiyun CoreEnterLeaveEvents(DeviceIntPtr dev, WindowPtr from, WindowPtr to, int mode)
539*4882a593Smuzhiyun {
540*4882a593Smuzhiyun if (!IsMaster(dev))
541*4882a593Smuzhiyun return;
542*4882a593Smuzhiyun
543*4882a593Smuzhiyun LeaveWindow(dev);
544*4882a593Smuzhiyun
545*4882a593Smuzhiyun if (IsParent(from, to))
546*4882a593Smuzhiyun CoreEnterLeaveToDescendant(dev, from, to, mode);
547*4882a593Smuzhiyun else if (IsParent(to, from))
548*4882a593Smuzhiyun CoreEnterLeaveToAncestor(dev, from, to, mode);
549*4882a593Smuzhiyun else
550*4882a593Smuzhiyun CoreEnterLeaveNonLinear(dev, from, to, mode);
551*4882a593Smuzhiyun
552*4882a593Smuzhiyun EnterWindow(dev, to, mode);
553*4882a593Smuzhiyun }
554*4882a593Smuzhiyun
555*4882a593Smuzhiyun static void
DeviceEnterLeaveEvents(DeviceIntPtr dev,int sourceid,WindowPtr from,WindowPtr to,int mode)556*4882a593Smuzhiyun DeviceEnterLeaveEvents(DeviceIntPtr dev,
557*4882a593Smuzhiyun int sourceid, WindowPtr from, WindowPtr to, int mode)
558*4882a593Smuzhiyun {
559*4882a593Smuzhiyun if (IsParent(from, to)) {
560*4882a593Smuzhiyun DeviceEnterLeaveEvent(dev, sourceid, XI_Leave, mode, NotifyInferior,
561*4882a593Smuzhiyun from, None);
562*4882a593Smuzhiyun DeviceEnterNotifies(dev, sourceid, from, to, mode, NotifyVirtual);
563*4882a593Smuzhiyun DeviceEnterLeaveEvent(dev, sourceid, XI_Enter, mode, NotifyAncestor, to,
564*4882a593Smuzhiyun None);
565*4882a593Smuzhiyun }
566*4882a593Smuzhiyun else if (IsParent(to, from)) {
567*4882a593Smuzhiyun DeviceEnterLeaveEvent(dev, sourceid, XI_Leave, mode, NotifyAncestor,
568*4882a593Smuzhiyun from, None);
569*4882a593Smuzhiyun DeviceLeaveNotifies(dev, sourceid, from, to, mode, NotifyVirtual);
570*4882a593Smuzhiyun DeviceEnterLeaveEvent(dev, sourceid, XI_Enter, mode, NotifyInferior, to,
571*4882a593Smuzhiyun None);
572*4882a593Smuzhiyun }
573*4882a593Smuzhiyun else { /* neither from nor to is descendent of the other */
574*4882a593Smuzhiyun WindowPtr common = CommonAncestor(to, from);
575*4882a593Smuzhiyun
576*4882a593Smuzhiyun /* common == NullWindow ==> different screens */
577*4882a593Smuzhiyun DeviceEnterLeaveEvent(dev, sourceid, XI_Leave, mode, NotifyNonlinear,
578*4882a593Smuzhiyun from, None);
579*4882a593Smuzhiyun DeviceLeaveNotifies(dev, sourceid, from, common, mode,
580*4882a593Smuzhiyun NotifyNonlinearVirtual);
581*4882a593Smuzhiyun DeviceEnterNotifies(dev, sourceid, common, to, mode,
582*4882a593Smuzhiyun NotifyNonlinearVirtual);
583*4882a593Smuzhiyun DeviceEnterLeaveEvent(dev, sourceid, XI_Enter, mode, NotifyNonlinear,
584*4882a593Smuzhiyun to, None);
585*4882a593Smuzhiyun }
586*4882a593Smuzhiyun }
587*4882a593Smuzhiyun
588*4882a593Smuzhiyun /**
589*4882a593Smuzhiyun * Figure out if enter/leave events are necessary and send them to the
590*4882a593Smuzhiyun * appropriate windows.
591*4882a593Smuzhiyun *
592*4882a593Smuzhiyun * @param fromWin Window the sprite moved out of.
593*4882a593Smuzhiyun * @param toWin Window the sprite moved into.
594*4882a593Smuzhiyun */
595*4882a593Smuzhiyun void
DoEnterLeaveEvents(DeviceIntPtr pDev,int sourceid,WindowPtr fromWin,WindowPtr toWin,int mode)596*4882a593Smuzhiyun DoEnterLeaveEvents(DeviceIntPtr pDev,
597*4882a593Smuzhiyun int sourceid, WindowPtr fromWin, WindowPtr toWin, int mode)
598*4882a593Smuzhiyun {
599*4882a593Smuzhiyun if (!IsPointerDevice(pDev))
600*4882a593Smuzhiyun return;
601*4882a593Smuzhiyun
602*4882a593Smuzhiyun if (fromWin == toWin)
603*4882a593Smuzhiyun return;
604*4882a593Smuzhiyun
605*4882a593Smuzhiyun if (mode != XINotifyPassiveGrab && mode != XINotifyPassiveUngrab)
606*4882a593Smuzhiyun CoreEnterLeaveEvents(pDev, fromWin, toWin, mode);
607*4882a593Smuzhiyun DeviceEnterLeaveEvents(pDev, sourceid, fromWin, toWin, mode);
608*4882a593Smuzhiyun }
609*4882a593Smuzhiyun
610*4882a593Smuzhiyun static void
FixDeviceValuator(DeviceIntPtr dev,deviceValuator * ev,ValuatorClassPtr v,int first)611*4882a593Smuzhiyun FixDeviceValuator(DeviceIntPtr dev, deviceValuator * ev, ValuatorClassPtr v,
612*4882a593Smuzhiyun int first)
613*4882a593Smuzhiyun {
614*4882a593Smuzhiyun int nval = v->numAxes - first;
615*4882a593Smuzhiyun
616*4882a593Smuzhiyun ev->type = DeviceValuator;
617*4882a593Smuzhiyun ev->deviceid = dev->id;
618*4882a593Smuzhiyun ev->num_valuators = nval < 3 ? nval : 3;
619*4882a593Smuzhiyun ev->first_valuator = first;
620*4882a593Smuzhiyun switch (ev->num_valuators) {
621*4882a593Smuzhiyun case 3:
622*4882a593Smuzhiyun ev->valuator2 = v->axisVal[first + 2];
623*4882a593Smuzhiyun case 2:
624*4882a593Smuzhiyun ev->valuator1 = v->axisVal[first + 1];
625*4882a593Smuzhiyun case 1:
626*4882a593Smuzhiyun ev->valuator0 = v->axisVal[first];
627*4882a593Smuzhiyun break;
628*4882a593Smuzhiyun }
629*4882a593Smuzhiyun first += ev->num_valuators;
630*4882a593Smuzhiyun }
631*4882a593Smuzhiyun
632*4882a593Smuzhiyun static void
FixDeviceStateNotify(DeviceIntPtr dev,deviceStateNotify * ev,KeyClassPtr k,ButtonClassPtr b,ValuatorClassPtr v,int first)633*4882a593Smuzhiyun FixDeviceStateNotify(DeviceIntPtr dev, deviceStateNotify * ev, KeyClassPtr k,
634*4882a593Smuzhiyun ButtonClassPtr b, ValuatorClassPtr v, int first)
635*4882a593Smuzhiyun {
636*4882a593Smuzhiyun ev->type = DeviceStateNotify;
637*4882a593Smuzhiyun ev->deviceid = dev->id;
638*4882a593Smuzhiyun ev->time = currentTime.milliseconds;
639*4882a593Smuzhiyun ev->classes_reported = 0;
640*4882a593Smuzhiyun ev->num_keys = 0;
641*4882a593Smuzhiyun ev->num_buttons = 0;
642*4882a593Smuzhiyun ev->num_valuators = 0;
643*4882a593Smuzhiyun
644*4882a593Smuzhiyun if (b) {
645*4882a593Smuzhiyun ev->classes_reported |= (1 << ButtonClass);
646*4882a593Smuzhiyun ev->num_buttons = b->numButtons;
647*4882a593Smuzhiyun memcpy((char *) ev->buttons, (char *) b->down, 4);
648*4882a593Smuzhiyun }
649*4882a593Smuzhiyun else if (k) {
650*4882a593Smuzhiyun ev->classes_reported |= (1 << KeyClass);
651*4882a593Smuzhiyun ev->num_keys = k->xkbInfo->desc->max_key_code -
652*4882a593Smuzhiyun k->xkbInfo->desc->min_key_code;
653*4882a593Smuzhiyun memmove((char *) &ev->keys[0], (char *) k->down, 4);
654*4882a593Smuzhiyun }
655*4882a593Smuzhiyun if (v) {
656*4882a593Smuzhiyun int nval = v->numAxes - first;
657*4882a593Smuzhiyun
658*4882a593Smuzhiyun ev->classes_reported |= (1 << ValuatorClass);
659*4882a593Smuzhiyun ev->classes_reported |= valuator_get_mode(dev, 0) << ModeBitsShift;
660*4882a593Smuzhiyun ev->num_valuators = nval < 3 ? nval : 3;
661*4882a593Smuzhiyun switch (ev->num_valuators) {
662*4882a593Smuzhiyun case 3:
663*4882a593Smuzhiyun ev->valuator2 = v->axisVal[first + 2];
664*4882a593Smuzhiyun case 2:
665*4882a593Smuzhiyun ev->valuator1 = v->axisVal[first + 1];
666*4882a593Smuzhiyun case 1:
667*4882a593Smuzhiyun ev->valuator0 = v->axisVal[first];
668*4882a593Smuzhiyun break;
669*4882a593Smuzhiyun }
670*4882a593Smuzhiyun }
671*4882a593Smuzhiyun }
672*4882a593Smuzhiyun
673*4882a593Smuzhiyun
674*4882a593Smuzhiyun static void
DeliverStateNotifyEvent(DeviceIntPtr dev,WindowPtr win)675*4882a593Smuzhiyun DeliverStateNotifyEvent(DeviceIntPtr dev, WindowPtr win)
676*4882a593Smuzhiyun {
677*4882a593Smuzhiyun int evcount = 1;
678*4882a593Smuzhiyun deviceStateNotify *ev, *sev;
679*4882a593Smuzhiyun deviceKeyStateNotify *kev;
680*4882a593Smuzhiyun deviceButtonStateNotify *bev;
681*4882a593Smuzhiyun
682*4882a593Smuzhiyun KeyClassPtr k;
683*4882a593Smuzhiyun ButtonClassPtr b;
684*4882a593Smuzhiyun ValuatorClassPtr v;
685*4882a593Smuzhiyun int nval = 0, nkeys = 0, nbuttons = 0, first = 0;
686*4882a593Smuzhiyun
687*4882a593Smuzhiyun if (!(wOtherInputMasks(win)) ||
688*4882a593Smuzhiyun !(wOtherInputMasks(win)->inputEvents[dev->id] & DeviceStateNotifyMask))
689*4882a593Smuzhiyun return;
690*4882a593Smuzhiyun
691*4882a593Smuzhiyun if ((b = dev->button) != NULL) {
692*4882a593Smuzhiyun nbuttons = b->numButtons;
693*4882a593Smuzhiyun if (nbuttons > 32)
694*4882a593Smuzhiyun evcount++;
695*4882a593Smuzhiyun }
696*4882a593Smuzhiyun if ((k = dev->key) != NULL) {
697*4882a593Smuzhiyun nkeys = k->xkbInfo->desc->max_key_code - k->xkbInfo->desc->min_key_code;
698*4882a593Smuzhiyun if (nkeys > 32)
699*4882a593Smuzhiyun evcount++;
700*4882a593Smuzhiyun if (nbuttons > 0) {
701*4882a593Smuzhiyun evcount++;
702*4882a593Smuzhiyun }
703*4882a593Smuzhiyun }
704*4882a593Smuzhiyun if ((v = dev->valuator) != NULL) {
705*4882a593Smuzhiyun nval = v->numAxes;
706*4882a593Smuzhiyun
707*4882a593Smuzhiyun if (nval > 3)
708*4882a593Smuzhiyun evcount++;
709*4882a593Smuzhiyun if (nval > 6) {
710*4882a593Smuzhiyun if (!(k && b))
711*4882a593Smuzhiyun evcount++;
712*4882a593Smuzhiyun if (nval > 9)
713*4882a593Smuzhiyun evcount += ((nval - 7) / 3);
714*4882a593Smuzhiyun }
715*4882a593Smuzhiyun }
716*4882a593Smuzhiyun
717*4882a593Smuzhiyun sev = ev = xallocarray(evcount, sizeof(xEvent));
718*4882a593Smuzhiyun FixDeviceStateNotify(dev, ev, NULL, NULL, NULL, first);
719*4882a593Smuzhiyun
720*4882a593Smuzhiyun if (b != NULL) {
721*4882a593Smuzhiyun FixDeviceStateNotify(dev, ev++, NULL, b, v, first);
722*4882a593Smuzhiyun first += 3;
723*4882a593Smuzhiyun nval -= 3;
724*4882a593Smuzhiyun if (nbuttons > 32) {
725*4882a593Smuzhiyun (ev - 1)->deviceid |= MORE_EVENTS;
726*4882a593Smuzhiyun bev = (deviceButtonStateNotify *) ev++;
727*4882a593Smuzhiyun bev->type = DeviceButtonStateNotify;
728*4882a593Smuzhiyun bev->deviceid = dev->id;
729*4882a593Smuzhiyun memcpy((char *) &bev->buttons[4], (char *) &b->down[4],
730*4882a593Smuzhiyun DOWN_LENGTH - 4);
731*4882a593Smuzhiyun }
732*4882a593Smuzhiyun if (nval > 0) {
733*4882a593Smuzhiyun (ev - 1)->deviceid |= MORE_EVENTS;
734*4882a593Smuzhiyun FixDeviceValuator(dev, (deviceValuator *) ev++, v, first);
735*4882a593Smuzhiyun first += 3;
736*4882a593Smuzhiyun nval -= 3;
737*4882a593Smuzhiyun }
738*4882a593Smuzhiyun }
739*4882a593Smuzhiyun
740*4882a593Smuzhiyun if (k != NULL) {
741*4882a593Smuzhiyun FixDeviceStateNotify(dev, ev++, k, NULL, v, first);
742*4882a593Smuzhiyun first += 3;
743*4882a593Smuzhiyun nval -= 3;
744*4882a593Smuzhiyun if (nkeys > 32) {
745*4882a593Smuzhiyun (ev - 1)->deviceid |= MORE_EVENTS;
746*4882a593Smuzhiyun kev = (deviceKeyStateNotify *) ev++;
747*4882a593Smuzhiyun kev->type = DeviceKeyStateNotify;
748*4882a593Smuzhiyun kev->deviceid = dev->id;
749*4882a593Smuzhiyun memmove((char *) &kev->keys[0], (char *) &k->down[4], 28);
750*4882a593Smuzhiyun }
751*4882a593Smuzhiyun if (nval > 0) {
752*4882a593Smuzhiyun (ev - 1)->deviceid |= MORE_EVENTS;
753*4882a593Smuzhiyun FixDeviceValuator(dev, (deviceValuator *) ev++, v, first);
754*4882a593Smuzhiyun first += 3;
755*4882a593Smuzhiyun nval -= 3;
756*4882a593Smuzhiyun }
757*4882a593Smuzhiyun }
758*4882a593Smuzhiyun
759*4882a593Smuzhiyun while (nval > 0) {
760*4882a593Smuzhiyun FixDeviceStateNotify(dev, ev++, NULL, NULL, v, first);
761*4882a593Smuzhiyun first += 3;
762*4882a593Smuzhiyun nval -= 3;
763*4882a593Smuzhiyun if (nval > 0) {
764*4882a593Smuzhiyun (ev - 1)->deviceid |= MORE_EVENTS;
765*4882a593Smuzhiyun FixDeviceValuator(dev, (deviceValuator *) ev++, v, first);
766*4882a593Smuzhiyun first += 3;
767*4882a593Smuzhiyun nval -= 3;
768*4882a593Smuzhiyun }
769*4882a593Smuzhiyun }
770*4882a593Smuzhiyun
771*4882a593Smuzhiyun DeliverEventsToWindow(dev, win, (xEvent *) sev, evcount,
772*4882a593Smuzhiyun DeviceStateNotifyMask, NullGrab);
773*4882a593Smuzhiyun free(sev);
774*4882a593Smuzhiyun }
775*4882a593Smuzhiyun
776*4882a593Smuzhiyun void
DeviceFocusEvent(DeviceIntPtr dev,int type,int mode,int detail,WindowPtr pWin)777*4882a593Smuzhiyun DeviceFocusEvent(DeviceIntPtr dev, int type, int mode, int detail,
778*4882a593Smuzhiyun WindowPtr pWin)
779*4882a593Smuzhiyun {
780*4882a593Smuzhiyun deviceFocus event;
781*4882a593Smuzhiyun xXIFocusInEvent *xi2event;
782*4882a593Smuzhiyun DeviceIntPtr mouse;
783*4882a593Smuzhiyun int btlen, len, i;
784*4882a593Smuzhiyun
785*4882a593Smuzhiyun mouse = IsFloating(dev) ? dev : GetMaster(dev, MASTER_POINTER);
786*4882a593Smuzhiyun
787*4882a593Smuzhiyun /* XI 2 event */
788*4882a593Smuzhiyun btlen = (mouse->button) ? bits_to_bytes(mouse->button->numButtons) : 0;
789*4882a593Smuzhiyun btlen = bytes_to_int32(btlen);
790*4882a593Smuzhiyun len = sizeof(xXIFocusInEvent) + btlen * 4;
791*4882a593Smuzhiyun
792*4882a593Smuzhiyun xi2event = calloc(1, len);
793*4882a593Smuzhiyun xi2event->type = GenericEvent;
794*4882a593Smuzhiyun xi2event->extension = IReqCode;
795*4882a593Smuzhiyun xi2event->evtype = type;
796*4882a593Smuzhiyun xi2event->length = bytes_to_int32(len - sizeof(xEvent));
797*4882a593Smuzhiyun xi2event->buttons_len = btlen;
798*4882a593Smuzhiyun xi2event->detail = detail;
799*4882a593Smuzhiyun xi2event->time = currentTime.milliseconds;
800*4882a593Smuzhiyun xi2event->deviceid = dev->id;
801*4882a593Smuzhiyun xi2event->sourceid = dev->id; /* a device doesn't change focus by itself */
802*4882a593Smuzhiyun xi2event->mode = mode;
803*4882a593Smuzhiyun xi2event->root_x = double_to_fp1616(mouse->spriteInfo->sprite->hot.x);
804*4882a593Smuzhiyun xi2event->root_y = double_to_fp1616(mouse->spriteInfo->sprite->hot.y);
805*4882a593Smuzhiyun
806*4882a593Smuzhiyun for (i = 0; mouse && mouse->button && i < mouse->button->numButtons; i++)
807*4882a593Smuzhiyun if (BitIsOn(mouse->button->down, i))
808*4882a593Smuzhiyun SetBit(&xi2event[1], mouse->button->map[i]);
809*4882a593Smuzhiyun
810*4882a593Smuzhiyun if (dev->key) {
811*4882a593Smuzhiyun xi2event->mods.base_mods = dev->key->xkbInfo->state.base_mods;
812*4882a593Smuzhiyun xi2event->mods.latched_mods = dev->key->xkbInfo->state.latched_mods;
813*4882a593Smuzhiyun xi2event->mods.locked_mods = dev->key->xkbInfo->state.locked_mods;
814*4882a593Smuzhiyun xi2event->mods.effective_mods = dev->key->xkbInfo->state.mods;
815*4882a593Smuzhiyun
816*4882a593Smuzhiyun xi2event->group.base_group = dev->key->xkbInfo->state.base_group;
817*4882a593Smuzhiyun xi2event->group.latched_group = dev->key->xkbInfo->state.latched_group;
818*4882a593Smuzhiyun xi2event->group.locked_group = dev->key->xkbInfo->state.locked_group;
819*4882a593Smuzhiyun xi2event->group.effective_group = dev->key->xkbInfo->state.group;
820*4882a593Smuzhiyun }
821*4882a593Smuzhiyun
822*4882a593Smuzhiyun FixUpEventFromWindow(dev->spriteInfo->sprite, (xEvent *) xi2event, pWin,
823*4882a593Smuzhiyun None, FALSE);
824*4882a593Smuzhiyun
825*4882a593Smuzhiyun DeliverEventsToWindow(dev, pWin, (xEvent *) xi2event, 1,
826*4882a593Smuzhiyun GetEventFilter(dev, (xEvent *) xi2event), NullGrab);
827*4882a593Smuzhiyun
828*4882a593Smuzhiyun free(xi2event);
829*4882a593Smuzhiyun
830*4882a593Smuzhiyun /* XI 1.x event */
831*4882a593Smuzhiyun event = (deviceFocus) {
832*4882a593Smuzhiyun .deviceid = dev->id,
833*4882a593Smuzhiyun .mode = mode,
834*4882a593Smuzhiyun .type = (type == XI_FocusIn) ? DeviceFocusIn : DeviceFocusOut,
835*4882a593Smuzhiyun .detail = detail,
836*4882a593Smuzhiyun .window = pWin->drawable.id,
837*4882a593Smuzhiyun .time = currentTime.milliseconds
838*4882a593Smuzhiyun };
839*4882a593Smuzhiyun
840*4882a593Smuzhiyun DeliverEventsToWindow(dev, pWin, (xEvent *) &event, 1,
841*4882a593Smuzhiyun DeviceFocusChangeMask, NullGrab);
842*4882a593Smuzhiyun
843*4882a593Smuzhiyun if (event.type == DeviceFocusIn)
844*4882a593Smuzhiyun DeliverStateNotifyEvent(dev, pWin);
845*4882a593Smuzhiyun }
846*4882a593Smuzhiyun
847*4882a593Smuzhiyun /**
848*4882a593Smuzhiyun * Send focus out events to all windows between 'child' and 'ancestor'.
849*4882a593Smuzhiyun * Events are sent running up the hierarchy.
850*4882a593Smuzhiyun */
851*4882a593Smuzhiyun static void
DeviceFocusOutEvents(DeviceIntPtr dev,WindowPtr child,WindowPtr ancestor,int mode,int detail)852*4882a593Smuzhiyun DeviceFocusOutEvents(DeviceIntPtr dev,
853*4882a593Smuzhiyun WindowPtr child, WindowPtr ancestor, int mode, int detail)
854*4882a593Smuzhiyun {
855*4882a593Smuzhiyun WindowPtr win;
856*4882a593Smuzhiyun
857*4882a593Smuzhiyun if (ancestor == child)
858*4882a593Smuzhiyun return;
859*4882a593Smuzhiyun for (win = child->parent; win != ancestor; win = win->parent)
860*4882a593Smuzhiyun DeviceFocusEvent(dev, XI_FocusOut, mode, detail, win);
861*4882a593Smuzhiyun }
862*4882a593Smuzhiyun
863*4882a593Smuzhiyun /**
864*4882a593Smuzhiyun * Send enter notifies to all windows between 'ancestor' and 'child' (excluding
865*4882a593Smuzhiyun * both). Events are sent running up the window hierarchy. This function
866*4882a593Smuzhiyun * recurses.
867*4882a593Smuzhiyun */
868*4882a593Smuzhiyun static void
DeviceFocusInEvents(DeviceIntPtr dev,WindowPtr ancestor,WindowPtr child,int mode,int detail)869*4882a593Smuzhiyun DeviceFocusInEvents(DeviceIntPtr dev,
870*4882a593Smuzhiyun WindowPtr ancestor, WindowPtr child, int mode, int detail)
871*4882a593Smuzhiyun {
872*4882a593Smuzhiyun WindowPtr parent = child->parent;
873*4882a593Smuzhiyun
874*4882a593Smuzhiyun if (ancestor == parent || !parent)
875*4882a593Smuzhiyun return;
876*4882a593Smuzhiyun DeviceFocusInEvents(dev, ancestor, parent, mode, detail);
877*4882a593Smuzhiyun DeviceFocusEvent(dev, XI_FocusIn, mode, detail, parent);
878*4882a593Smuzhiyun }
879*4882a593Smuzhiyun
880*4882a593Smuzhiyun /**
881*4882a593Smuzhiyun * Send FocusIn events to all windows between 'ancestor' and 'child' (excluding
882*4882a593Smuzhiyun * both). Events are sent running down the window hierarchy. This function
883*4882a593Smuzhiyun * recurses.
884*4882a593Smuzhiyun */
885*4882a593Smuzhiyun static void
CoreFocusInEvents(DeviceIntPtr dev,WindowPtr ancestor,WindowPtr child,int mode,int detail)886*4882a593Smuzhiyun CoreFocusInEvents(DeviceIntPtr dev,
887*4882a593Smuzhiyun WindowPtr ancestor, WindowPtr child, int mode, int detail)
888*4882a593Smuzhiyun {
889*4882a593Smuzhiyun WindowPtr parent = child->parent;
890*4882a593Smuzhiyun
891*4882a593Smuzhiyun if (ancestor == parent)
892*4882a593Smuzhiyun return;
893*4882a593Smuzhiyun CoreFocusInEvents(dev, ancestor, parent, mode, detail);
894*4882a593Smuzhiyun
895*4882a593Smuzhiyun /* Case 3:
896*4882a593Smuzhiyun A is above W, B is a descendant
897*4882a593Smuzhiyun
898*4882a593Smuzhiyun Classically: The move generates an FocusIn on W with a detail of
899*4882a593Smuzhiyun Virtual or NonlinearVirtual
900*4882a593Smuzhiyun
901*4882a593Smuzhiyun MPX:
902*4882a593Smuzhiyun Case 3A: There is at least one other focus on W itself
903*4882a593Smuzhiyun F(W) doesn't change, so the event should be suppressed
904*4882a593Smuzhiyun Case 3B: Otherwise, if there is at least one other focus in a
905*4882a593Smuzhiyun descendant
906*4882a593Smuzhiyun F(W) stays on the same descendant, or changes to a different
907*4882a593Smuzhiyun descendant. The event should be suppressed.
908*4882a593Smuzhiyun Case 3C: Otherwise:
909*4882a593Smuzhiyun F(W) moves from a window above W to a descendant. The detail may
910*4882a593Smuzhiyun need to be changed from Virtual to NonlinearVirtual depending
911*4882a593Smuzhiyun on the previous F(W). */
912*4882a593Smuzhiyun
913*4882a593Smuzhiyun if (!HasFocus(parent) && !FirstFocusChild(parent))
914*4882a593Smuzhiyun CoreFocusEvent(dev, FocusIn, mode, detail, parent);
915*4882a593Smuzhiyun }
916*4882a593Smuzhiyun
917*4882a593Smuzhiyun static void
CoreFocusOutEvents(DeviceIntPtr dev,WindowPtr child,WindowPtr ancestor,int mode,int detail)918*4882a593Smuzhiyun CoreFocusOutEvents(DeviceIntPtr dev,
919*4882a593Smuzhiyun WindowPtr child, WindowPtr ancestor, int mode, int detail)
920*4882a593Smuzhiyun {
921*4882a593Smuzhiyun WindowPtr win;
922*4882a593Smuzhiyun
923*4882a593Smuzhiyun if (ancestor == child)
924*4882a593Smuzhiyun return;
925*4882a593Smuzhiyun
926*4882a593Smuzhiyun for (win = child->parent; win != ancestor; win = win->parent) {
927*4882a593Smuzhiyun /*Case 7:
928*4882a593Smuzhiyun A is a descendant of W, B is above W
929*4882a593Smuzhiyun
930*4882a593Smuzhiyun Classically: A FocusOut is generated on W with a detail of Virtual
931*4882a593Smuzhiyun or NonlinearVirtual.
932*4882a593Smuzhiyun
933*4882a593Smuzhiyun MPX:
934*4882a593Smuzhiyun Case 3A: There is at least one other focus on W itself
935*4882a593Smuzhiyun F(W) doesn't change, the event should be suppressed.
936*4882a593Smuzhiyun Case 3B: Otherwise, if there is at least one other focus in a
937*4882a593Smuzhiyun descendant
938*4882a593Smuzhiyun F(W) stays on the same descendant, or changes to a different
939*4882a593Smuzhiyun descendant. The event should be suppressed.
940*4882a593Smuzhiyun Case 3C: Otherwise:
941*4882a593Smuzhiyun F(W) changes from the descendant of W to a window above W.
942*4882a593Smuzhiyun The detail may need to be changed from Virtual to NonlinearVirtual
943*4882a593Smuzhiyun or vice-versa depending on the new P(W). */
944*4882a593Smuzhiyun
945*4882a593Smuzhiyun /* If one window has a focus or a child with a focuspointer, skip some
946*4882a593Smuzhiyun * work and exit. */
947*4882a593Smuzhiyun if (HasFocus(win) || FirstFocusChild(win))
948*4882a593Smuzhiyun return;
949*4882a593Smuzhiyun
950*4882a593Smuzhiyun CoreFocusEvent(dev, FocusOut, mode, detail, win);
951*4882a593Smuzhiyun }
952*4882a593Smuzhiyun }
953*4882a593Smuzhiyun
954*4882a593Smuzhiyun /**
955*4882a593Smuzhiyun * Send FocusOut(NotifyPointer) events from the current pointer window (which
956*4882a593Smuzhiyun * is a descendant of pwin_parent) up to (excluding) pwin_parent.
957*4882a593Smuzhiyun *
958*4882a593Smuzhiyun * NotifyPointer events are only sent for the device paired with dev.
959*4882a593Smuzhiyun *
960*4882a593Smuzhiyun * If the current pointer window is a descendant of 'exclude' or an ancestor of
961*4882a593Smuzhiyun * 'exclude', no events are sent. If the current pointer IS 'exclude', events
962*4882a593Smuzhiyun * are sent!
963*4882a593Smuzhiyun */
964*4882a593Smuzhiyun static void
CoreFocusOutNotifyPointerEvents(DeviceIntPtr dev,WindowPtr pwin_parent,WindowPtr exclude,int mode,int inclusive)965*4882a593Smuzhiyun CoreFocusOutNotifyPointerEvents(DeviceIntPtr dev,
966*4882a593Smuzhiyun WindowPtr pwin_parent,
967*4882a593Smuzhiyun WindowPtr exclude, int mode, int inclusive)
968*4882a593Smuzhiyun {
969*4882a593Smuzhiyun WindowPtr P, stopAt;
970*4882a593Smuzhiyun
971*4882a593Smuzhiyun P = PointerWin(GetMaster(dev, POINTER_OR_FLOAT));
972*4882a593Smuzhiyun
973*4882a593Smuzhiyun if (!P)
974*4882a593Smuzhiyun return;
975*4882a593Smuzhiyun if (!IsParent(pwin_parent, P))
976*4882a593Smuzhiyun if (!(pwin_parent == P && inclusive))
977*4882a593Smuzhiyun return;
978*4882a593Smuzhiyun
979*4882a593Smuzhiyun if (exclude != None && exclude != PointerRootWin &&
980*4882a593Smuzhiyun (IsParent(exclude, P) || IsParent(P, exclude)))
981*4882a593Smuzhiyun return;
982*4882a593Smuzhiyun
983*4882a593Smuzhiyun stopAt = (inclusive) ? pwin_parent->parent : pwin_parent;
984*4882a593Smuzhiyun
985*4882a593Smuzhiyun for (; P && P != stopAt; P = P->parent)
986*4882a593Smuzhiyun CoreFocusEvent(dev, FocusOut, mode, NotifyPointer, P);
987*4882a593Smuzhiyun }
988*4882a593Smuzhiyun
989*4882a593Smuzhiyun /**
990*4882a593Smuzhiyun * DO NOT CALL DIRECTLY.
991*4882a593Smuzhiyun * Recursion helper for CoreFocusInNotifyPointerEvents.
992*4882a593Smuzhiyun */
993*4882a593Smuzhiyun static void
CoreFocusInRecurse(DeviceIntPtr dev,WindowPtr win,WindowPtr stopAt,int mode,int inclusive)994*4882a593Smuzhiyun CoreFocusInRecurse(DeviceIntPtr dev,
995*4882a593Smuzhiyun WindowPtr win, WindowPtr stopAt, int mode, int inclusive)
996*4882a593Smuzhiyun {
997*4882a593Smuzhiyun if ((!inclusive && win == stopAt) || !win)
998*4882a593Smuzhiyun return;
999*4882a593Smuzhiyun
1000*4882a593Smuzhiyun CoreFocusInRecurse(dev, win->parent, stopAt, mode, inclusive);
1001*4882a593Smuzhiyun CoreFocusEvent(dev, FocusIn, mode, NotifyPointer, win);
1002*4882a593Smuzhiyun }
1003*4882a593Smuzhiyun
1004*4882a593Smuzhiyun /**
1005*4882a593Smuzhiyun * Send FocusIn(NotifyPointer) events from pwin_parent down to
1006*4882a593Smuzhiyun * including the current pointer window (which is a descendant of pwin_parent).
1007*4882a593Smuzhiyun *
1008*4882a593Smuzhiyun * @param pwin The pointer window.
1009*4882a593Smuzhiyun * @param exclude If the pointer window is a child of 'exclude', no events are
1010*4882a593Smuzhiyun * sent.
1011*4882a593Smuzhiyun * @param inclusive If TRUE, pwin_parent will receive the event too.
1012*4882a593Smuzhiyun */
1013*4882a593Smuzhiyun static void
CoreFocusInNotifyPointerEvents(DeviceIntPtr dev,WindowPtr pwin_parent,WindowPtr exclude,int mode,int inclusive)1014*4882a593Smuzhiyun CoreFocusInNotifyPointerEvents(DeviceIntPtr dev,
1015*4882a593Smuzhiyun WindowPtr pwin_parent,
1016*4882a593Smuzhiyun WindowPtr exclude, int mode, int inclusive)
1017*4882a593Smuzhiyun {
1018*4882a593Smuzhiyun WindowPtr P;
1019*4882a593Smuzhiyun
1020*4882a593Smuzhiyun P = PointerWin(GetMaster(dev, POINTER_OR_FLOAT));
1021*4882a593Smuzhiyun
1022*4882a593Smuzhiyun if (!P || P == exclude || (pwin_parent != P && !IsParent(pwin_parent, P)))
1023*4882a593Smuzhiyun return;
1024*4882a593Smuzhiyun
1025*4882a593Smuzhiyun if (exclude != None && (IsParent(exclude, P) || IsParent(P, exclude)))
1026*4882a593Smuzhiyun return;
1027*4882a593Smuzhiyun
1028*4882a593Smuzhiyun CoreFocusInRecurse(dev, P, pwin_parent, mode, inclusive);
1029*4882a593Smuzhiyun }
1030*4882a593Smuzhiyun
1031*4882a593Smuzhiyun /**
1032*4882a593Smuzhiyun * Focus of dev moves from A to B and A neither a descendant of B nor is
1033*4882a593Smuzhiyun * B a descendant of A.
1034*4882a593Smuzhiyun */
1035*4882a593Smuzhiyun static void
CoreFocusNonLinear(DeviceIntPtr dev,WindowPtr A,WindowPtr B,int mode)1036*4882a593Smuzhiyun CoreFocusNonLinear(DeviceIntPtr dev, WindowPtr A, WindowPtr B, int mode)
1037*4882a593Smuzhiyun {
1038*4882a593Smuzhiyun WindowPtr X = CommonAncestor(A, B);
1039*4882a593Smuzhiyun
1040*4882a593Smuzhiyun /* Case 4:
1041*4882a593Smuzhiyun A is W, B is above W
1042*4882a593Smuzhiyun
1043*4882a593Smuzhiyun Classically: The change generates a FocusOut on W with a detail of
1044*4882a593Smuzhiyun Ancestor or Nonlinear
1045*4882a593Smuzhiyun
1046*4882a593Smuzhiyun MPX:
1047*4882a593Smuzhiyun Case 3A: There is at least one other focus on W itself
1048*4882a593Smuzhiyun F(W) doesn't change, the event should be suppressed
1049*4882a593Smuzhiyun Case 3B: Otherwise, if there is at least one other focus in a
1050*4882a593Smuzhiyun descendant of W
1051*4882a593Smuzhiyun F(W) changes from W to a descendant of W. The detail field
1052*4882a593Smuzhiyun is set to Inferior
1053*4882a593Smuzhiyun Case 3C: Otherwise:
1054*4882a593Smuzhiyun The focus window moves from W to a window above W.
1055*4882a593Smuzhiyun The detail may need to be changed from Ancestor to Nonlinear or
1056*4882a593Smuzhiyun vice versa depending on the the new F(W)
1057*4882a593Smuzhiyun */
1058*4882a593Smuzhiyun
1059*4882a593Smuzhiyun if (!HasFocus(A)) {
1060*4882a593Smuzhiyun WindowPtr child = FirstFocusChild(A);
1061*4882a593Smuzhiyun
1062*4882a593Smuzhiyun if (child) {
1063*4882a593Smuzhiyun /* NotifyPointer P-A unless P is child or below */
1064*4882a593Smuzhiyun CoreFocusOutNotifyPointerEvents(dev, A, child, mode, FALSE);
1065*4882a593Smuzhiyun CoreFocusEvent(dev, FocusOut, mode, NotifyInferior, A);
1066*4882a593Smuzhiyun }
1067*4882a593Smuzhiyun else {
1068*4882a593Smuzhiyun /* NotifyPointer P-A */
1069*4882a593Smuzhiyun CoreFocusOutNotifyPointerEvents(dev, A, None, mode, FALSE);
1070*4882a593Smuzhiyun CoreFocusEvent(dev, FocusOut, mode, NotifyNonlinear, A);
1071*4882a593Smuzhiyun }
1072*4882a593Smuzhiyun }
1073*4882a593Smuzhiyun
1074*4882a593Smuzhiyun CoreFocusOutEvents(dev, A, X, mode, NotifyNonlinearVirtual);
1075*4882a593Smuzhiyun
1076*4882a593Smuzhiyun /*
1077*4882a593Smuzhiyun Case 9:
1078*4882a593Smuzhiyun A is a descendant of W, B is a descendant of W
1079*4882a593Smuzhiyun
1080*4882a593Smuzhiyun Classically: No events are generated on W
1081*4882a593Smuzhiyun MPX: The focus window stays the same or moves to a different
1082*4882a593Smuzhiyun descendant of W. No events should be generated on W.
1083*4882a593Smuzhiyun
1084*4882a593Smuzhiyun Therefore, no event to X.
1085*4882a593Smuzhiyun */
1086*4882a593Smuzhiyun
1087*4882a593Smuzhiyun CoreFocusInEvents(dev, X, B, mode, NotifyNonlinearVirtual);
1088*4882a593Smuzhiyun
1089*4882a593Smuzhiyun /* Case 2:
1090*4882a593Smuzhiyun A is above W, B=W
1091*4882a593Smuzhiyun
1092*4882a593Smuzhiyun Classically: The move generates an EnterNotify on W with a detail of
1093*4882a593Smuzhiyun Ancestor or Nonlinear
1094*4882a593Smuzhiyun
1095*4882a593Smuzhiyun MPX:
1096*4882a593Smuzhiyun Case 2A: There is at least one other focus on W itself
1097*4882a593Smuzhiyun F(W) doesn't change, so the event should be suppressed
1098*4882a593Smuzhiyun Case 2B: Otherwise, if there is at least one other focus in a
1099*4882a593Smuzhiyun descendant
1100*4882a593Smuzhiyun F(W) moves from a descendant to W. detail is changed to Inferior.
1101*4882a593Smuzhiyun Case 2C: Otherwise:
1102*4882a593Smuzhiyun F(W) changes from a window above W to W itself.
1103*4882a593Smuzhiyun The detail may need to be changed from Ancestor to Nonlinear
1104*4882a593Smuzhiyun or vice-versa depending on the previous F(W). */
1105*4882a593Smuzhiyun
1106*4882a593Smuzhiyun if (!HasFocus(B)) {
1107*4882a593Smuzhiyun WindowPtr child = FirstFocusChild(B);
1108*4882a593Smuzhiyun
1109*4882a593Smuzhiyun if (child) {
1110*4882a593Smuzhiyun CoreFocusEvent(dev, FocusIn, mode, NotifyInferior, B);
1111*4882a593Smuzhiyun /* NotifyPointer B-P unless P is child or below. */
1112*4882a593Smuzhiyun CoreFocusInNotifyPointerEvents(dev, B, child, mode, FALSE);
1113*4882a593Smuzhiyun }
1114*4882a593Smuzhiyun else {
1115*4882a593Smuzhiyun CoreFocusEvent(dev, FocusIn, mode, NotifyNonlinear, B);
1116*4882a593Smuzhiyun /* NotifyPointer B-P unless P is child or below. */
1117*4882a593Smuzhiyun CoreFocusInNotifyPointerEvents(dev, B, None, mode, FALSE);
1118*4882a593Smuzhiyun }
1119*4882a593Smuzhiyun }
1120*4882a593Smuzhiyun }
1121*4882a593Smuzhiyun
1122*4882a593Smuzhiyun /**
1123*4882a593Smuzhiyun * Focus of dev moves from A to B and A is a descendant of B.
1124*4882a593Smuzhiyun */
1125*4882a593Smuzhiyun static void
CoreFocusToAncestor(DeviceIntPtr dev,WindowPtr A,WindowPtr B,int mode)1126*4882a593Smuzhiyun CoreFocusToAncestor(DeviceIntPtr dev, WindowPtr A, WindowPtr B, int mode)
1127*4882a593Smuzhiyun {
1128*4882a593Smuzhiyun /* Case 4:
1129*4882a593Smuzhiyun A is W, B is above W
1130*4882a593Smuzhiyun
1131*4882a593Smuzhiyun Classically: The change generates a FocusOut on W with a detail of
1132*4882a593Smuzhiyun Ancestor or Nonlinear
1133*4882a593Smuzhiyun
1134*4882a593Smuzhiyun MPX:
1135*4882a593Smuzhiyun Case 3A: There is at least one other focus on W itself
1136*4882a593Smuzhiyun F(W) doesn't change, the event should be suppressed
1137*4882a593Smuzhiyun Case 3B: Otherwise, if there is at least one other focus in a
1138*4882a593Smuzhiyun descendant of W
1139*4882a593Smuzhiyun F(W) changes from W to a descendant of W. The detail field
1140*4882a593Smuzhiyun is set to Inferior
1141*4882a593Smuzhiyun Case 3C: Otherwise:
1142*4882a593Smuzhiyun The focus window moves from W to a window above W.
1143*4882a593Smuzhiyun The detail may need to be changed from Ancestor to Nonlinear or
1144*4882a593Smuzhiyun vice versa depending on the the new F(W)
1145*4882a593Smuzhiyun */
1146*4882a593Smuzhiyun if (!HasFocus(A)) {
1147*4882a593Smuzhiyun WindowPtr child = FirstFocusChild(A);
1148*4882a593Smuzhiyun
1149*4882a593Smuzhiyun if (child) {
1150*4882a593Smuzhiyun /* NotifyPointer P-A unless P is child or below */
1151*4882a593Smuzhiyun CoreFocusOutNotifyPointerEvents(dev, A, child, mode, FALSE);
1152*4882a593Smuzhiyun CoreFocusEvent(dev, FocusOut, mode, NotifyInferior, A);
1153*4882a593Smuzhiyun }
1154*4882a593Smuzhiyun else
1155*4882a593Smuzhiyun CoreFocusEvent(dev, FocusOut, mode, NotifyAncestor, A);
1156*4882a593Smuzhiyun }
1157*4882a593Smuzhiyun
1158*4882a593Smuzhiyun CoreFocusOutEvents(dev, A, B, mode, NotifyVirtual);
1159*4882a593Smuzhiyun
1160*4882a593Smuzhiyun /* Case 8:
1161*4882a593Smuzhiyun A is a descendant of W, B is W
1162*4882a593Smuzhiyun
1163*4882a593Smuzhiyun Classically: A FocusOut is generated on W with a detail of
1164*4882a593Smuzhiyun NotifyInferior
1165*4882a593Smuzhiyun
1166*4882a593Smuzhiyun MPX:
1167*4882a593Smuzhiyun Case 3A: There is at least one other focus on W itself
1168*4882a593Smuzhiyun F(W) doesn't change, the event should be suppressed
1169*4882a593Smuzhiyun Case 3B: Otherwise:
1170*4882a593Smuzhiyun F(W) changes from a descendant to W itself. */
1171*4882a593Smuzhiyun
1172*4882a593Smuzhiyun if (!HasFocus(B)) {
1173*4882a593Smuzhiyun CoreFocusEvent(dev, FocusIn, mode, NotifyInferior, B);
1174*4882a593Smuzhiyun /* NotifyPointer B-P unless P is A or below. */
1175*4882a593Smuzhiyun CoreFocusInNotifyPointerEvents(dev, B, A, mode, FALSE);
1176*4882a593Smuzhiyun }
1177*4882a593Smuzhiyun }
1178*4882a593Smuzhiyun
1179*4882a593Smuzhiyun /**
1180*4882a593Smuzhiyun * Focus of dev moves from A to B and B is a descendant of A.
1181*4882a593Smuzhiyun */
1182*4882a593Smuzhiyun static void
CoreFocusToDescendant(DeviceIntPtr dev,WindowPtr A,WindowPtr B,int mode)1183*4882a593Smuzhiyun CoreFocusToDescendant(DeviceIntPtr dev, WindowPtr A, WindowPtr B, int mode)
1184*4882a593Smuzhiyun {
1185*4882a593Smuzhiyun /* Case 6:
1186*4882a593Smuzhiyun A is W, B is a descendant of W
1187*4882a593Smuzhiyun
1188*4882a593Smuzhiyun Classically: A FocusOut is generated on W with a detail of
1189*4882a593Smuzhiyun NotifyInferior
1190*4882a593Smuzhiyun
1191*4882a593Smuzhiyun MPX:
1192*4882a593Smuzhiyun Case 3A: There is at least one other focus on W itself
1193*4882a593Smuzhiyun F(W) doesn't change, the event should be suppressed
1194*4882a593Smuzhiyun Case 3B: Otherwise:
1195*4882a593Smuzhiyun F(W) changes from W to a descendant of W. */
1196*4882a593Smuzhiyun
1197*4882a593Smuzhiyun if (!HasFocus(A)) {
1198*4882a593Smuzhiyun /* NotifyPointer P-A unless P is B or below */
1199*4882a593Smuzhiyun CoreFocusOutNotifyPointerEvents(dev, A, B, mode, FALSE);
1200*4882a593Smuzhiyun CoreFocusEvent(dev, FocusOut, mode, NotifyInferior, A);
1201*4882a593Smuzhiyun }
1202*4882a593Smuzhiyun
1203*4882a593Smuzhiyun CoreFocusInEvents(dev, A, B, mode, NotifyVirtual);
1204*4882a593Smuzhiyun
1205*4882a593Smuzhiyun /* Case 2:
1206*4882a593Smuzhiyun A is above W, B=W
1207*4882a593Smuzhiyun
1208*4882a593Smuzhiyun Classically: The move generates an FocusIn on W with a detail of
1209*4882a593Smuzhiyun Ancestor or Nonlinear
1210*4882a593Smuzhiyun
1211*4882a593Smuzhiyun MPX:
1212*4882a593Smuzhiyun Case 2A: There is at least one other focus on W itself
1213*4882a593Smuzhiyun F(W) doesn't change, so the event should be suppressed
1214*4882a593Smuzhiyun Case 2B: Otherwise, if there is at least one other focus in a
1215*4882a593Smuzhiyun descendant
1216*4882a593Smuzhiyun F(W) moves from a descendant to W. detail is changed to Inferior.
1217*4882a593Smuzhiyun Case 2C: Otherwise:
1218*4882a593Smuzhiyun F(W) changes from a window above W to W itself.
1219*4882a593Smuzhiyun The detail may need to be changed from Ancestor to Nonlinear
1220*4882a593Smuzhiyun or vice-versa depending on the previous F(W). */
1221*4882a593Smuzhiyun
1222*4882a593Smuzhiyun if (!HasFocus(B)) {
1223*4882a593Smuzhiyun WindowPtr child = FirstFocusChild(B);
1224*4882a593Smuzhiyun
1225*4882a593Smuzhiyun if (child) {
1226*4882a593Smuzhiyun CoreFocusEvent(dev, FocusIn, mode, NotifyInferior, B);
1227*4882a593Smuzhiyun /* NotifyPointer B-P unless P is child or below. */
1228*4882a593Smuzhiyun CoreFocusInNotifyPointerEvents(dev, B, child, mode, FALSE);
1229*4882a593Smuzhiyun }
1230*4882a593Smuzhiyun else
1231*4882a593Smuzhiyun CoreFocusEvent(dev, FocusIn, mode, NotifyAncestor, B);
1232*4882a593Smuzhiyun }
1233*4882a593Smuzhiyun }
1234*4882a593Smuzhiyun
1235*4882a593Smuzhiyun static BOOL
HasOtherPointer(WindowPtr win,DeviceIntPtr exclude)1236*4882a593Smuzhiyun HasOtherPointer(WindowPtr win, DeviceIntPtr exclude)
1237*4882a593Smuzhiyun {
1238*4882a593Smuzhiyun int i;
1239*4882a593Smuzhiyun
1240*4882a593Smuzhiyun for (i = 0; i < MAXDEVICES; i++)
1241*4882a593Smuzhiyun if (i != exclude->id && PointerWindows[i] == win)
1242*4882a593Smuzhiyun return TRUE;
1243*4882a593Smuzhiyun
1244*4882a593Smuzhiyun return FALSE;
1245*4882a593Smuzhiyun }
1246*4882a593Smuzhiyun
1247*4882a593Smuzhiyun /**
1248*4882a593Smuzhiyun * Focus moves from PointerRoot to None or from None to PointerRoot.
1249*4882a593Smuzhiyun * Assumption: Neither A nor B are valid windows.
1250*4882a593Smuzhiyun */
1251*4882a593Smuzhiyun static void
CoreFocusPointerRootNoneSwitch(DeviceIntPtr dev,WindowPtr A,WindowPtr B,int mode)1252*4882a593Smuzhiyun CoreFocusPointerRootNoneSwitch(DeviceIntPtr dev,
1253*4882a593Smuzhiyun WindowPtr A, /* PointerRootWin or NoneWin */
1254*4882a593Smuzhiyun WindowPtr B, /* NoneWin or PointerRootWin */
1255*4882a593Smuzhiyun int mode)
1256*4882a593Smuzhiyun {
1257*4882a593Smuzhiyun WindowPtr root;
1258*4882a593Smuzhiyun int i;
1259*4882a593Smuzhiyun int nscreens = screenInfo.numScreens;
1260*4882a593Smuzhiyun
1261*4882a593Smuzhiyun #ifdef PANORAMIX
1262*4882a593Smuzhiyun if (!noPanoramiXExtension)
1263*4882a593Smuzhiyun nscreens = 1;
1264*4882a593Smuzhiyun #endif
1265*4882a593Smuzhiyun
1266*4882a593Smuzhiyun for (i = 0; i < nscreens; i++) {
1267*4882a593Smuzhiyun root = screenInfo.screens[i]->root;
1268*4882a593Smuzhiyun if (!HasOtherPointer(root, GetMaster(dev, POINTER_OR_FLOAT)) &&
1269*4882a593Smuzhiyun !FirstFocusChild(root)) {
1270*4882a593Smuzhiyun /* If pointer was on PointerRootWin and changes to NoneWin, and
1271*4882a593Smuzhiyun * the pointer paired with dev is below the current root window,
1272*4882a593Smuzhiyun * do a NotifyPointer run. */
1273*4882a593Smuzhiyun if (dev->focus && dev->focus->win == PointerRootWin &&
1274*4882a593Smuzhiyun B != PointerRootWin) {
1275*4882a593Smuzhiyun WindowPtr ptrwin = PointerWin(GetMaster(dev, POINTER_OR_FLOAT));
1276*4882a593Smuzhiyun
1277*4882a593Smuzhiyun if (ptrwin && IsParent(root, ptrwin))
1278*4882a593Smuzhiyun CoreFocusOutNotifyPointerEvents(dev, root, None, mode,
1279*4882a593Smuzhiyun TRUE);
1280*4882a593Smuzhiyun }
1281*4882a593Smuzhiyun CoreFocusEvent(dev, FocusOut, mode,
1282*4882a593Smuzhiyun A ? NotifyPointerRoot : NotifyDetailNone, root);
1283*4882a593Smuzhiyun CoreFocusEvent(dev, FocusIn, mode,
1284*4882a593Smuzhiyun B ? NotifyPointerRoot : NotifyDetailNone, root);
1285*4882a593Smuzhiyun if (B == PointerRootWin)
1286*4882a593Smuzhiyun CoreFocusInNotifyPointerEvents(dev, root, None, mode, TRUE);
1287*4882a593Smuzhiyun }
1288*4882a593Smuzhiyun
1289*4882a593Smuzhiyun }
1290*4882a593Smuzhiyun }
1291*4882a593Smuzhiyun
1292*4882a593Smuzhiyun /**
1293*4882a593Smuzhiyun * Focus moves from window A to PointerRoot or to None.
1294*4882a593Smuzhiyun * Assumption: A is a valid window and not PointerRoot or None.
1295*4882a593Smuzhiyun */
1296*4882a593Smuzhiyun static void
CoreFocusToPointerRootOrNone(DeviceIntPtr dev,WindowPtr A,WindowPtr B,int mode)1297*4882a593Smuzhiyun CoreFocusToPointerRootOrNone(DeviceIntPtr dev, WindowPtr A,
1298*4882a593Smuzhiyun WindowPtr B, /* PointerRootWin or NoneWin */
1299*4882a593Smuzhiyun int mode)
1300*4882a593Smuzhiyun {
1301*4882a593Smuzhiyun WindowPtr root;
1302*4882a593Smuzhiyun int i;
1303*4882a593Smuzhiyun int nscreens = screenInfo.numScreens;
1304*4882a593Smuzhiyun
1305*4882a593Smuzhiyun #ifdef PANORAMIX
1306*4882a593Smuzhiyun if (!noPanoramiXExtension)
1307*4882a593Smuzhiyun nscreens = 1;
1308*4882a593Smuzhiyun #endif
1309*4882a593Smuzhiyun
1310*4882a593Smuzhiyun if (!HasFocus(A)) {
1311*4882a593Smuzhiyun WindowPtr child = FirstFocusChild(A);
1312*4882a593Smuzhiyun
1313*4882a593Smuzhiyun if (child) {
1314*4882a593Smuzhiyun /* NotifyPointer P-A unless P is B or below */
1315*4882a593Smuzhiyun CoreFocusOutNotifyPointerEvents(dev, A, B, mode, FALSE);
1316*4882a593Smuzhiyun CoreFocusEvent(dev, FocusOut, mode, NotifyInferior, A);
1317*4882a593Smuzhiyun }
1318*4882a593Smuzhiyun else {
1319*4882a593Smuzhiyun /* NotifyPointer P-A */
1320*4882a593Smuzhiyun CoreFocusOutNotifyPointerEvents(dev, A, None, mode, FALSE);
1321*4882a593Smuzhiyun CoreFocusEvent(dev, FocusOut, mode, NotifyNonlinear, A);
1322*4882a593Smuzhiyun }
1323*4882a593Smuzhiyun }
1324*4882a593Smuzhiyun
1325*4882a593Smuzhiyun /* NullWindow means we include the root window */
1326*4882a593Smuzhiyun CoreFocusOutEvents(dev, A, NullWindow, mode, NotifyNonlinearVirtual);
1327*4882a593Smuzhiyun
1328*4882a593Smuzhiyun for (i = 0; i < nscreens; i++) {
1329*4882a593Smuzhiyun root = screenInfo.screens[i]->root;
1330*4882a593Smuzhiyun if (!HasFocus(root) && !FirstFocusChild(root)) {
1331*4882a593Smuzhiyun CoreFocusEvent(dev, FocusIn, mode,
1332*4882a593Smuzhiyun B ? NotifyPointerRoot : NotifyDetailNone, root);
1333*4882a593Smuzhiyun if (B == PointerRootWin)
1334*4882a593Smuzhiyun CoreFocusInNotifyPointerEvents(dev, root, None, mode, TRUE);
1335*4882a593Smuzhiyun }
1336*4882a593Smuzhiyun }
1337*4882a593Smuzhiyun }
1338*4882a593Smuzhiyun
1339*4882a593Smuzhiyun /**
1340*4882a593Smuzhiyun * Focus moves from PointerRoot or None to a window B.
1341*4882a593Smuzhiyun * Assumption: B is a valid window and not PointerRoot or None.
1342*4882a593Smuzhiyun */
1343*4882a593Smuzhiyun static void
CoreFocusFromPointerRootOrNone(DeviceIntPtr dev,WindowPtr A,WindowPtr B,int mode)1344*4882a593Smuzhiyun CoreFocusFromPointerRootOrNone(DeviceIntPtr dev,
1345*4882a593Smuzhiyun WindowPtr A, /* PointerRootWin or NoneWin */
1346*4882a593Smuzhiyun WindowPtr B, int mode)
1347*4882a593Smuzhiyun {
1348*4882a593Smuzhiyun WindowPtr root;
1349*4882a593Smuzhiyun int i;
1350*4882a593Smuzhiyun int nscreens = screenInfo.numScreens;
1351*4882a593Smuzhiyun
1352*4882a593Smuzhiyun #ifdef PANORAMIX
1353*4882a593Smuzhiyun if (!noPanoramiXExtension)
1354*4882a593Smuzhiyun nscreens = 1;
1355*4882a593Smuzhiyun #endif
1356*4882a593Smuzhiyun
1357*4882a593Smuzhiyun for (i = 0; i < nscreens; i++) {
1358*4882a593Smuzhiyun root = screenInfo.screens[i]->root;
1359*4882a593Smuzhiyun if (!HasFocus(root) && !FirstFocusChild(root)) {
1360*4882a593Smuzhiyun /* If pointer was on PointerRootWin and changes to NoneWin, and
1361*4882a593Smuzhiyun * the pointer paired with dev is below the current root window,
1362*4882a593Smuzhiyun * do a NotifyPointer run. */
1363*4882a593Smuzhiyun if (dev->focus && dev->focus->win == PointerRootWin &&
1364*4882a593Smuzhiyun B != PointerRootWin) {
1365*4882a593Smuzhiyun WindowPtr ptrwin = PointerWin(GetMaster(dev, POINTER_OR_FLOAT));
1366*4882a593Smuzhiyun
1367*4882a593Smuzhiyun if (ptrwin)
1368*4882a593Smuzhiyun CoreFocusOutNotifyPointerEvents(dev, root, None, mode,
1369*4882a593Smuzhiyun TRUE);
1370*4882a593Smuzhiyun }
1371*4882a593Smuzhiyun CoreFocusEvent(dev, FocusOut, mode,
1372*4882a593Smuzhiyun A ? NotifyPointerRoot : NotifyDetailNone, root);
1373*4882a593Smuzhiyun }
1374*4882a593Smuzhiyun }
1375*4882a593Smuzhiyun
1376*4882a593Smuzhiyun root = B; /* get B's root window */
1377*4882a593Smuzhiyun while (root->parent)
1378*4882a593Smuzhiyun root = root->parent;
1379*4882a593Smuzhiyun
1380*4882a593Smuzhiyun if (B != root) {
1381*4882a593Smuzhiyun CoreFocusEvent(dev, FocusIn, mode, NotifyNonlinearVirtual, root);
1382*4882a593Smuzhiyun CoreFocusInEvents(dev, root, B, mode, NotifyNonlinearVirtual);
1383*4882a593Smuzhiyun }
1384*4882a593Smuzhiyun
1385*4882a593Smuzhiyun if (!HasFocus(B)) {
1386*4882a593Smuzhiyun WindowPtr child = FirstFocusChild(B);
1387*4882a593Smuzhiyun
1388*4882a593Smuzhiyun if (child) {
1389*4882a593Smuzhiyun CoreFocusEvent(dev, FocusIn, mode, NotifyInferior, B);
1390*4882a593Smuzhiyun /* NotifyPointer B-P unless P is child or below. */
1391*4882a593Smuzhiyun CoreFocusInNotifyPointerEvents(dev, B, child, mode, FALSE);
1392*4882a593Smuzhiyun }
1393*4882a593Smuzhiyun else {
1394*4882a593Smuzhiyun CoreFocusEvent(dev, FocusIn, mode, NotifyNonlinear, B);
1395*4882a593Smuzhiyun /* NotifyPointer B-P unless P is child or below. */
1396*4882a593Smuzhiyun CoreFocusInNotifyPointerEvents(dev, B, None, mode, FALSE);
1397*4882a593Smuzhiyun }
1398*4882a593Smuzhiyun }
1399*4882a593Smuzhiyun
1400*4882a593Smuzhiyun }
1401*4882a593Smuzhiyun
1402*4882a593Smuzhiyun static void
CoreFocusEvents(DeviceIntPtr dev,WindowPtr from,WindowPtr to,int mode)1403*4882a593Smuzhiyun CoreFocusEvents(DeviceIntPtr dev, WindowPtr from, WindowPtr to, int mode)
1404*4882a593Smuzhiyun {
1405*4882a593Smuzhiyun if (!IsMaster(dev))
1406*4882a593Smuzhiyun return;
1407*4882a593Smuzhiyun
1408*4882a593Smuzhiyun SetFocusOut(dev);
1409*4882a593Smuzhiyun
1410*4882a593Smuzhiyun if (((to == NullWindow) || (to == PointerRootWin)) &&
1411*4882a593Smuzhiyun ((from == NullWindow) || (from == PointerRootWin)))
1412*4882a593Smuzhiyun CoreFocusPointerRootNoneSwitch(dev, from, to, mode);
1413*4882a593Smuzhiyun else if ((to == NullWindow) || (to == PointerRootWin))
1414*4882a593Smuzhiyun CoreFocusToPointerRootOrNone(dev, from, to, mode);
1415*4882a593Smuzhiyun else if ((from == NullWindow) || (from == PointerRootWin))
1416*4882a593Smuzhiyun CoreFocusFromPointerRootOrNone(dev, from, to, mode);
1417*4882a593Smuzhiyun else if (IsParent(from, to))
1418*4882a593Smuzhiyun CoreFocusToDescendant(dev, from, to, mode);
1419*4882a593Smuzhiyun else if (IsParent(to, from))
1420*4882a593Smuzhiyun CoreFocusToAncestor(dev, from, to, mode);
1421*4882a593Smuzhiyun else
1422*4882a593Smuzhiyun CoreFocusNonLinear(dev, from, to, mode);
1423*4882a593Smuzhiyun
1424*4882a593Smuzhiyun SetFocusIn(dev, to);
1425*4882a593Smuzhiyun }
1426*4882a593Smuzhiyun
1427*4882a593Smuzhiyun static void
DeviceFocusEvents(DeviceIntPtr dev,WindowPtr from,WindowPtr to,int mode)1428*4882a593Smuzhiyun DeviceFocusEvents(DeviceIntPtr dev, WindowPtr from, WindowPtr to, int mode)
1429*4882a593Smuzhiyun {
1430*4882a593Smuzhiyun int out, in; /* for holding details for to/from
1431*4882a593Smuzhiyun PointerRoot/None */
1432*4882a593Smuzhiyun int i;
1433*4882a593Smuzhiyun int nscreens = screenInfo.numScreens;
1434*4882a593Smuzhiyun SpritePtr sprite = dev->spriteInfo->sprite;
1435*4882a593Smuzhiyun
1436*4882a593Smuzhiyun if (from == to)
1437*4882a593Smuzhiyun return;
1438*4882a593Smuzhiyun out = (from == NoneWin) ? NotifyDetailNone : NotifyPointerRoot;
1439*4882a593Smuzhiyun in = (to == NoneWin) ? NotifyDetailNone : NotifyPointerRoot;
1440*4882a593Smuzhiyun /* wrong values if neither, but then not referenced */
1441*4882a593Smuzhiyun
1442*4882a593Smuzhiyun #ifdef PANORAMIX
1443*4882a593Smuzhiyun if (!noPanoramiXExtension)
1444*4882a593Smuzhiyun nscreens = 1;
1445*4882a593Smuzhiyun #endif
1446*4882a593Smuzhiyun
1447*4882a593Smuzhiyun if ((to == NullWindow) || (to == PointerRootWin)) {
1448*4882a593Smuzhiyun if ((from == NullWindow) || (from == PointerRootWin)) {
1449*4882a593Smuzhiyun if (from == PointerRootWin) {
1450*4882a593Smuzhiyun DeviceFocusEvent(dev, XI_FocusOut, mode, NotifyPointer,
1451*4882a593Smuzhiyun sprite->win);
1452*4882a593Smuzhiyun DeviceFocusOutEvents(dev, sprite->win,
1453*4882a593Smuzhiyun GetCurrentRootWindow(dev), mode,
1454*4882a593Smuzhiyun NotifyPointer);
1455*4882a593Smuzhiyun }
1456*4882a593Smuzhiyun /* Notify all the roots */
1457*4882a593Smuzhiyun for (i = 0; i < nscreens; i++)
1458*4882a593Smuzhiyun DeviceFocusEvent(dev, XI_FocusOut, mode, out,
1459*4882a593Smuzhiyun screenInfo.screens[i]->root);
1460*4882a593Smuzhiyun }
1461*4882a593Smuzhiyun else {
1462*4882a593Smuzhiyun if (IsParent(from, sprite->win)) {
1463*4882a593Smuzhiyun DeviceFocusEvent(dev, XI_FocusOut, mode, NotifyPointer,
1464*4882a593Smuzhiyun sprite->win);
1465*4882a593Smuzhiyun DeviceFocusOutEvents(dev, sprite->win, from, mode,
1466*4882a593Smuzhiyun NotifyPointer);
1467*4882a593Smuzhiyun }
1468*4882a593Smuzhiyun DeviceFocusEvent(dev, XI_FocusOut, mode, NotifyNonlinear, from);
1469*4882a593Smuzhiyun /* next call catches the root too, if the screen changed */
1470*4882a593Smuzhiyun DeviceFocusOutEvents(dev, from, NullWindow, mode,
1471*4882a593Smuzhiyun NotifyNonlinearVirtual);
1472*4882a593Smuzhiyun }
1473*4882a593Smuzhiyun /* Notify all the roots */
1474*4882a593Smuzhiyun for (i = 0; i < nscreens; i++)
1475*4882a593Smuzhiyun DeviceFocusEvent(dev, XI_FocusIn, mode, in,
1476*4882a593Smuzhiyun screenInfo.screens[i]->root);
1477*4882a593Smuzhiyun if (to == PointerRootWin) {
1478*4882a593Smuzhiyun DeviceFocusInEvents(dev, GetCurrentRootWindow(dev), sprite->win,
1479*4882a593Smuzhiyun mode, NotifyPointer);
1480*4882a593Smuzhiyun DeviceFocusEvent(dev, XI_FocusIn, mode, NotifyPointer, sprite->win);
1481*4882a593Smuzhiyun }
1482*4882a593Smuzhiyun }
1483*4882a593Smuzhiyun else {
1484*4882a593Smuzhiyun if ((from == NullWindow) || (from == PointerRootWin)) {
1485*4882a593Smuzhiyun if (from == PointerRootWin) {
1486*4882a593Smuzhiyun DeviceFocusEvent(dev, XI_FocusOut, mode, NotifyPointer,
1487*4882a593Smuzhiyun sprite->win);
1488*4882a593Smuzhiyun DeviceFocusOutEvents(dev, sprite->win,
1489*4882a593Smuzhiyun GetCurrentRootWindow(dev), mode,
1490*4882a593Smuzhiyun NotifyPointer);
1491*4882a593Smuzhiyun }
1492*4882a593Smuzhiyun for (i = 0; i < nscreens; i++)
1493*4882a593Smuzhiyun DeviceFocusEvent(dev, XI_FocusOut, mode, out,
1494*4882a593Smuzhiyun screenInfo.screens[i]->root);
1495*4882a593Smuzhiyun if (to->parent != NullWindow)
1496*4882a593Smuzhiyun DeviceFocusInEvents(dev, GetCurrentRootWindow(dev), to, mode,
1497*4882a593Smuzhiyun NotifyNonlinearVirtual);
1498*4882a593Smuzhiyun DeviceFocusEvent(dev, XI_FocusIn, mode, NotifyNonlinear, to);
1499*4882a593Smuzhiyun if (IsParent(to, sprite->win))
1500*4882a593Smuzhiyun DeviceFocusInEvents(dev, to, sprite->win, mode, NotifyPointer);
1501*4882a593Smuzhiyun }
1502*4882a593Smuzhiyun else {
1503*4882a593Smuzhiyun if (IsParent(to, from)) {
1504*4882a593Smuzhiyun DeviceFocusEvent(dev, XI_FocusOut, mode, NotifyAncestor, from);
1505*4882a593Smuzhiyun DeviceFocusOutEvents(dev, from, to, mode, NotifyVirtual);
1506*4882a593Smuzhiyun DeviceFocusEvent(dev, XI_FocusIn, mode, NotifyInferior, to);
1507*4882a593Smuzhiyun if ((IsParent(to, sprite->win)) &&
1508*4882a593Smuzhiyun (sprite->win != from) &&
1509*4882a593Smuzhiyun (!IsParent(from, sprite->win)) &&
1510*4882a593Smuzhiyun (!IsParent(sprite->win, from)))
1511*4882a593Smuzhiyun DeviceFocusInEvents(dev, to, sprite->win, mode,
1512*4882a593Smuzhiyun NotifyPointer);
1513*4882a593Smuzhiyun }
1514*4882a593Smuzhiyun else if (IsParent(from, to)) {
1515*4882a593Smuzhiyun if ((IsParent(from, sprite->win)) &&
1516*4882a593Smuzhiyun (sprite->win != from) &&
1517*4882a593Smuzhiyun (!IsParent(to, sprite->win)) &&
1518*4882a593Smuzhiyun (!IsParent(sprite->win, to))) {
1519*4882a593Smuzhiyun DeviceFocusEvent(dev, XI_FocusOut, mode, NotifyPointer,
1520*4882a593Smuzhiyun sprite->win);
1521*4882a593Smuzhiyun DeviceFocusOutEvents(dev, sprite->win, from, mode,
1522*4882a593Smuzhiyun NotifyPointer);
1523*4882a593Smuzhiyun }
1524*4882a593Smuzhiyun DeviceFocusEvent(dev, XI_FocusOut, mode, NotifyInferior, from);
1525*4882a593Smuzhiyun DeviceFocusInEvents(dev, from, to, mode, NotifyVirtual);
1526*4882a593Smuzhiyun DeviceFocusEvent(dev, XI_FocusIn, mode, NotifyAncestor, to);
1527*4882a593Smuzhiyun }
1528*4882a593Smuzhiyun else {
1529*4882a593Smuzhiyun /* neither from or to is child of other */
1530*4882a593Smuzhiyun WindowPtr common = CommonAncestor(to, from);
1531*4882a593Smuzhiyun
1532*4882a593Smuzhiyun /* common == NullWindow ==> different screens */
1533*4882a593Smuzhiyun if (IsParent(from, sprite->win))
1534*4882a593Smuzhiyun DeviceFocusOutEvents(dev, sprite->win, from, mode,
1535*4882a593Smuzhiyun NotifyPointer);
1536*4882a593Smuzhiyun DeviceFocusEvent(dev, XI_FocusOut, mode, NotifyNonlinear, from);
1537*4882a593Smuzhiyun if (from->parent != NullWindow)
1538*4882a593Smuzhiyun DeviceFocusOutEvents(dev, from, common, mode,
1539*4882a593Smuzhiyun NotifyNonlinearVirtual);
1540*4882a593Smuzhiyun if (to->parent != NullWindow)
1541*4882a593Smuzhiyun DeviceFocusInEvents(dev, common, to, mode,
1542*4882a593Smuzhiyun NotifyNonlinearVirtual);
1543*4882a593Smuzhiyun DeviceFocusEvent(dev, XI_FocusIn, mode, NotifyNonlinear, to);
1544*4882a593Smuzhiyun if (IsParent(to, sprite->win))
1545*4882a593Smuzhiyun DeviceFocusInEvents(dev, to, sprite->win, mode,
1546*4882a593Smuzhiyun NotifyPointer);
1547*4882a593Smuzhiyun }
1548*4882a593Smuzhiyun }
1549*4882a593Smuzhiyun }
1550*4882a593Smuzhiyun }
1551*4882a593Smuzhiyun
1552*4882a593Smuzhiyun /**
1553*4882a593Smuzhiyun * Figure out if focus events are necessary and send them to the
1554*4882a593Smuzhiyun * appropriate windows.
1555*4882a593Smuzhiyun *
1556*4882a593Smuzhiyun * @param from Window the focus moved out of.
1557*4882a593Smuzhiyun * @param to Window the focus moved into.
1558*4882a593Smuzhiyun */
1559*4882a593Smuzhiyun void
DoFocusEvents(DeviceIntPtr pDev,WindowPtr from,WindowPtr to,int mode)1560*4882a593Smuzhiyun DoFocusEvents(DeviceIntPtr pDev, WindowPtr from, WindowPtr to, int mode)
1561*4882a593Smuzhiyun {
1562*4882a593Smuzhiyun if (!IsKeyboardDevice(pDev))
1563*4882a593Smuzhiyun return;
1564*4882a593Smuzhiyun
1565*4882a593Smuzhiyun if (from == to && mode != NotifyGrab && mode != NotifyUngrab)
1566*4882a593Smuzhiyun return;
1567*4882a593Smuzhiyun
1568*4882a593Smuzhiyun CoreFocusEvents(pDev, from, to, mode);
1569*4882a593Smuzhiyun DeviceFocusEvents(pDev, from, to, mode);
1570*4882a593Smuzhiyun }
1571