Git init
[framework/uifw/xorg/lib/libxt.git] / src / EventUtil.c
1 /*
2
3 Copyright (c) 1993, Oracle and/or its affiliates. All rights reserved.
4
5 Permission is hereby granted, free of charge, to any person obtaining a
6 copy of this software and associated documentation files (the "Software"),
7 to deal in the Software without restriction, including without limitation
8 the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 and/or sell copies of the Software, and to permit persons to whom the
10 Software is furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice (including the next
13 paragraph) shall be included in all copies or substantial portions of the
14 Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23
24 */
25 /********************************************************
26
27 Copyright 1988 by Hewlett-Packard Company
28 Copyright 1987, 1988, 1989 by Digital Equipment Corporation, Maynard, Massachusetts
29
30 Permission to use, copy, modify, and distribute this software
31 and its documentation for any purpose and without fee is hereby
32 granted, provided that the above copyright notice appear in all
33 copies and that both that copyright notice and this permission
34 notice appear in supporting documentation, and that the names of
35 Hewlett-Packard or Digital not be used in advertising or
36 publicity pertaining to distribution of the software without specific,
37 written prior permission.
38
39 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
40 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
41 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
42 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
43 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
44 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
45 SOFTWARE.
46
47 ********************************************************/
48
49 /*
50
51 Copyright 1987, 1988, 1989, 1998  The Open Group
52
53 Permission to use, copy, modify, distribute, and sell this software and its
54 documentation for any purpose is hereby granted without fee, provided that
55 the above copyright notice appear in all copies and that both that
56 copyright notice and this permission notice appear in supporting
57 documentation.
58
59 The above copyright notice and this permission notice shall be included in
60 all copies or substantial portions of the Software.
61
62 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
63 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
64 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
65 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
66 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
67 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
68
69 Except as contained in this notice, the name of The Open Group shall not be
70 used in advertising or otherwise to promote the sale, use or other dealings
71 in this Software without prior written authorization from The Open Group.
72
73 */
74
75 #ifdef HAVE_CONFIG_H
76 #include <config.h>
77 #endif
78 #include "IntrinsicI.h"
79 #include "PassivGraI.h"
80 #include "StringDefs.h"
81 #include "EventI.h"
82
83 static XContext         perWidgetInputContext = 0;
84
85 void _XtFreePerWidgetInput(
86     Widget              w,
87     XtPerWidgetInput    pwi)
88 {
89     LOCK_PROCESS;
90     XDeleteContext(XtDisplay(w),
91                    (Window)w,
92                    perWidgetInputContext);
93
94     XtFree((char *)pwi);
95     UNLOCK_PROCESS;
96 }
97
98 /*
99  * This routine gets the passive list associated with the widget
100  * from the context manager.
101  */
102 XtPerWidgetInput _XtGetPerWidgetInput(
103     Widget      widget,
104     _XtBoolean  create)
105 {
106     XtPerWidgetInput    pwi = NULL;
107     Display             *dpy = widget->core.screen->display;
108
109     LOCK_PROCESS;
110     if (! perWidgetInputContext)
111       perWidgetInputContext = XUniqueContext();
112
113     if (XFindContext(dpy,
114                      (Window)widget,
115                      perWidgetInputContext,
116                      (XPointer *)&pwi) &&
117         create)
118       {
119           pwi = (XtPerWidgetInput)
120             __XtMalloc((unsigned) sizeof(XtPerWidgetInputRec));
121
122           pwi->focusKid = NULL;
123           pwi->queryEventDescendant = NULL;
124           pwi->focalPoint = XtUnrelated;
125           pwi->keyList =
126             pwi->ptrList = NULL;
127
128           pwi->haveFocus =
129               pwi->map_handler_added =
130                   pwi->realize_handler_added =
131                       pwi->active_handler_added = FALSE;
132
133           XtAddCallback(widget, XtNdestroyCallback,
134                         _XtDestroyServerGrabs, (XtPointer)pwi);
135
136           (void) XSaveContext(dpy,
137                               (Window)widget,
138                               perWidgetInputContext,
139                               (char *) pwi);
140       }
141     UNLOCK_PROCESS;
142     return pwi;
143 }
144
145
146 void _XtFillAncestorList(
147     Widget      **listPtr,
148     int         *maxElemsPtr,
149     int         *numElemsPtr,
150     Widget      start,
151     Widget      breakWidget)
152 {
153 #define CACHESIZE 16
154     Cardinal    i;
155     Widget      w;
156     Widget      *trace = *listPtr;
157
158     /* First time in, allocate the ancestor list */
159     if (trace == NULL)
160       {
161           trace = (Widget *) __XtMalloc(CACHESIZE * sizeof(Widget));
162           *maxElemsPtr = CACHESIZE;
163       }
164     /* First fill in the ancestor list */
165
166     trace[0] = start;
167
168     for (i = 1, w = XtParent(start);
169          w != NULL && !XtIsShell(trace[i-1]) && trace[i-1] != breakWidget;
170          w = XtParent(w), i++) {
171         if (i == (Cardinal) *maxElemsPtr) {
172             /* This should rarely happen, but if it does it'll probably
173                happen again, so grow the ancestor list */
174             *maxElemsPtr += CACHESIZE;
175             trace = (Widget *) XtRealloc((char*)trace,
176                                          sizeof(Widget) * (*maxElemsPtr));
177         }
178         trace[i] = w;
179     }
180     *listPtr = trace;
181     *numElemsPtr = i;
182 #undef CACHESIZE
183 }
184
185
186 Widget _XtFindRemapWidget(
187     XEvent      *event,
188     Widget      widget,
189     EventMask   mask,
190     XtPerDisplayInput pdi)
191 {
192     Widget              dspWidget = widget;
193
194     if (!pdi->traceDepth || !(widget == pdi->trace[0]))
195       {
196           _XtFillAncestorList(&pdi->trace, &pdi->traceMax,
197                               &pdi->traceDepth, widget, NULL);
198           pdi->focusWidget = NULL; /* invalidate the focus
199                                       cache */
200       }
201     if (mask & (KeyPressMask | KeyReleaseMask))
202           dspWidget = _XtProcessKeyboardEvent((XKeyEvent*)event, widget, pdi);
203     else if (mask &(ButtonPressMask | ButtonReleaseMask))
204           dspWidget = _XtProcessPointerEvent((XButtonEvent*)event, widget,pdi);
205
206     return dspWidget;
207 }
208
209 void _XtUngrabBadGrabs(
210     XEvent      *event,
211     Widget      widget,
212     EventMask   mask,
213     XtPerDisplayInput pdi)
214 {
215     XKeyEvent   * ke = (XKeyEvent *) event;
216
217     if (mask & (KeyPressMask | KeyReleaseMask))
218       {
219           if (IsServerGrab(pdi->keyboard.grabType) &&
220               !_XtOnGrabList(pdi->keyboard.grab.widget,
221                              pdi->grabList))
222             XtUngrabKeyboard(widget, ke->time);
223
224       }
225     else
226       {
227           if (IsServerGrab(pdi->pointer.grabType) &&
228               !_XtOnGrabList(pdi->pointer.grab.widget,
229                              pdi->grabList))
230             XtUngrabPointer(widget, ke->time);
231       }
232 }