TMgrab.c revision 1477040f
1444c061aSmrg/* $Xorg: TMgrab.c,v 1.4 2001/02/09 02:03:58 xorgcvs Exp $ */
2444c061aSmrg
3444c061aSmrg/***********************************************************
41477040fSmrgCopyright 1993 Sun Microsystems, Inc.  All rights reserved.
51477040fSmrg
61477040fSmrgPermission is hereby granted, free of charge, to any person obtaining a
71477040fSmrgcopy of this software and associated documentation files (the "Software"),
81477040fSmrgto deal in the Software without restriction, including without limitation
91477040fSmrgthe rights to use, copy, modify, merge, publish, distribute, sublicense,
101477040fSmrgand/or sell copies of the Software, and to permit persons to whom the
111477040fSmrgSoftware is furnished to do so, subject to the following conditions:
121477040fSmrg
131477040fSmrgThe above copyright notice and this permission notice (including the next
141477040fSmrgparagraph) shall be included in all copies or substantial portions of the
151477040fSmrgSoftware.
161477040fSmrg
171477040fSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
181477040fSmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
191477040fSmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
201477040fSmrgTHE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
211477040fSmrgLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
221477040fSmrgFROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
231477040fSmrgDEALINGS IN THE SOFTWARE.
241477040fSmrg
251477040fSmrgCopyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
26444c061aSmrg
27444c061aSmrg                        All Rights Reserved
28444c061aSmrg
29444c061aSmrgPermission to use, copy, modify, and distribute this software and its
30444c061aSmrgdocumentation for any purpose and without fee is hereby granted,
31444c061aSmrgprovided that the above copyright notice appear in all copies and that
32444c061aSmrgboth that copyright notice and this permission notice appear in
331477040fSmrgsupporting documentation, and that the name of Digital not be
34444c061aSmrgused in advertising or publicity pertaining to distribution of the
35444c061aSmrgsoftware without specific, written prior permission.
36444c061aSmrg
37444c061aSmrgDIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
38444c061aSmrgALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
39444c061aSmrgDIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
40444c061aSmrgANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
41444c061aSmrgWHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
42444c061aSmrgARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
43444c061aSmrgSOFTWARE.
44444c061aSmrg
45444c061aSmrg******************************************************************/
46444c061aSmrg
47444c061aSmrg/*
48444c061aSmrg
49444c061aSmrgCopyright 1987, 1988, 1998  The Open Group
50444c061aSmrg
51444c061aSmrgPermission to use, copy, modify, distribute, and sell this software and its
52444c061aSmrgdocumentation for any purpose is hereby granted without fee, provided that
53444c061aSmrgthe above copyright notice appear in all copies and that both that
54444c061aSmrgcopyright notice and this permission notice appear in supporting
55444c061aSmrgdocumentation.
56444c061aSmrg
57444c061aSmrgThe above copyright notice and this permission notice shall be included in
58444c061aSmrgall copies or substantial portions of the Software.
59444c061aSmrg
60444c061aSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
61444c061aSmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
62444c061aSmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
63444c061aSmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
64444c061aSmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
65444c061aSmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
66444c061aSmrg
67444c061aSmrgExcept as contained in this notice, the name of The Open Group shall not be
68444c061aSmrgused in advertising or otherwise to promote the sale, use or other dealings
69444c061aSmrgin this Software without prior written authorization from The Open Group.
70444c061aSmrg
71444c061aSmrg*/
72444c061aSmrg/* $XFree86: xc/lib/Xt/TMgrab.c,v 1.3 2001/12/14 19:56:30 dawes Exp $ */
73444c061aSmrg
74444c061aSmrg/*LINTLIBRARY*/
75444c061aSmrg#ifdef HAVE_CONFIG_H
76444c061aSmrg#include <config.h>
77444c061aSmrg#endif
78444c061aSmrg#include "IntrinsicI.h"
79444c061aSmrg
80444c061aSmrgtypedef struct _GrabActionRec {
81444c061aSmrg    struct _GrabActionRec* next;
82444c061aSmrg    XtActionProc action_proc;
83444c061aSmrg    Boolean owner_events;
84444c061aSmrg    unsigned int event_mask;
85444c061aSmrg    int pointer_mode, keyboard_mode;
86444c061aSmrg} GrabActionRec;
87444c061aSmrg
88444c061aSmrgstatic GrabActionRec *grabActionList = NULL;
89444c061aSmrg
90444c061aSmrgstatic void GrabAllCorrectKeys(
91444c061aSmrg    Widget widget,
92444c061aSmrg    TMTypeMatch typeMatch,
93444c061aSmrg    TMModifierMatch modMatch,
94444c061aSmrg    GrabActionRec* grabP)
95444c061aSmrg{
96444c061aSmrg    Display *dpy = XtDisplay(widget);
97444c061aSmrg    KeyCode *keycodes, *keycodeP;
98444c061aSmrg    Cardinal keycount;
99444c061aSmrg    Modifiers careOn = 0;
100444c061aSmrg    Modifiers careMask = 0;
101444c061aSmrg
102444c061aSmrg    if (modMatch->lateModifiers) {
103444c061aSmrg	Boolean resolved;
104444c061aSmrg	resolved = _XtComputeLateBindings(dpy, modMatch->lateModifiers,
105444c061aSmrg					  &careOn, &careMask);
106444c061aSmrg	if (!resolved) return;
107444c061aSmrg    }
108444c061aSmrg    careOn |= modMatch->modifiers;
109444c061aSmrg    careMask |= modMatch->modifierMask;
110444c061aSmrg
111444c061aSmrg    XtKeysymToKeycodeList(
112444c061aSmrg	    dpy,
113444c061aSmrg	    (KeySym)typeMatch->eventCode,
114444c061aSmrg	    &keycodes,
115444c061aSmrg	    &keycount
116444c061aSmrg			 );
117444c061aSmrg    if (keycount == 0) return;
118444c061aSmrg    for (keycodeP = keycodes; keycount--; keycodeP++) {
119444c061aSmrg	if (modMatch->standard) {
120444c061aSmrg	    /* find standard modifiers that produce this keysym */
121444c061aSmrg	    KeySym keysym;
122444c061aSmrg	    int std_mods, least_mod;
123444c061aSmrg	    Modifiers modifiers_return;
124444c061aSmrg	    XtTranslateKeycode( dpy, *keycodeP, (Modifiers)0,
125444c061aSmrg			        &modifiers_return, &keysym );
126444c061aSmrg	    if (careOn & modifiers_return)
127444c061aSmrg		return;
128444c061aSmrg	    if (keysym == typeMatch->eventCode) {
129444c061aSmrg		XtGrabKey(widget, *keycodeP, careOn,
130444c061aSmrg			  grabP->owner_events,
131444c061aSmrg			  grabP->pointer_mode,
132444c061aSmrg			  grabP->keyboard_mode
133444c061aSmrg			);
134444c061aSmrg		/* continue; */		/* grab all modifier combinations */
135444c061aSmrg	    }
136444c061aSmrg	    least_mod = modifiers_return & (~modifiers_return + 1);
137444c061aSmrg	    for (std_mods = modifiers_return;
138444c061aSmrg		 std_mods >= least_mod; std_mods--) {
139444c061aSmrg		Modifiers dummy;
140444c061aSmrg		 /* check all useful combinations of modifier bits */
141444c061aSmrg		if (modifiers_return & std_mods &&
142444c061aSmrg		    !(~modifiers_return & std_mods)) {
143444c061aSmrg		    XtTranslateKeycode( dpy, *keycodeP,
144444c061aSmrg					(Modifiers)std_mods,
145444c061aSmrg					&dummy, &keysym );
146444c061aSmrg		    if (keysym == typeMatch->eventCode) {
147444c061aSmrg			XtGrabKey(widget, *keycodeP,
148444c061aSmrg				  careOn | (Modifiers) std_mods,
149444c061aSmrg				  grabP->owner_events,
150444c061aSmrg				  grabP->pointer_mode,
151444c061aSmrg				  grabP->keyboard_mode
152444c061aSmrg				);
153444c061aSmrg			/* break; */	/* grab all modifier combinations */
154444c061aSmrg		    }
155444c061aSmrg		}
156444c061aSmrg	    }
157444c061aSmrg	} else /* !event->standard */ {
158444c061aSmrg	    XtGrabKey(widget, *keycodeP, careOn,
159444c061aSmrg		      grabP->owner_events,
160444c061aSmrg		      grabP->pointer_mode,
161444c061aSmrg		      grabP->keyboard_mode
162444c061aSmrg		    );
163444c061aSmrg	}
164444c061aSmrg    }
165444c061aSmrg    XtFree((char *)keycodes);
166444c061aSmrg}
167444c061aSmrg
168444c061aSmrgtypedef struct {
169444c061aSmrg    TMShortCard count;
170444c061aSmrg    Widget	widget;
171444c061aSmrg    GrabActionRec *grabP;
172444c061aSmrg}DoGrabRec;
173444c061aSmrg
174444c061aSmrgstatic Boolean DoGrab(
175444c061aSmrg    StatePtr		state,
176444c061aSmrg    XtPointer		data)
177444c061aSmrg{
178444c061aSmrg    DoGrabRec		*doGrabP = (DoGrabRec *)data;
179444c061aSmrg    GrabActionRec* 	grabP = doGrabP->grabP;
180444c061aSmrg    Widget		widget = doGrabP->widget;
181444c061aSmrg    TMShortCard		count = doGrabP->count;
182444c061aSmrg    TMShortCard		typeIndex = state->typeIndex;
183444c061aSmrg    TMShortCard		modIndex = state->modIndex;
184444c061aSmrg    ActionRec		*action;
185444c061aSmrg    TMTypeMatch		typeMatch;
186444c061aSmrg    TMModifierMatch	modMatch;
187444c061aSmrg    Modifiers		careOn = 0;
188444c061aSmrg    Modifiers		careMask = 0;
189444c061aSmrg    Boolean		resolved;
190444c061aSmrg
191444c061aSmrg    LOCK_PROCESS;
192444c061aSmrg    typeMatch = TMGetTypeMatch(typeIndex);
193444c061aSmrg    modMatch = TMGetModifierMatch(modIndex);
194444c061aSmrg
195444c061aSmrg    for (action = state->actions; action; action = action->next)
196444c061aSmrg      if (count == action->idx) break;
197444c061aSmrg    if (!action) {
198444c061aSmrg	UNLOCK_PROCESS;
199444c061aSmrg	return False;
200444c061aSmrg    }
201444c061aSmrg
202444c061aSmrg    switch (typeMatch->eventType) {
203444c061aSmrg      case ButtonPress:
204444c061aSmrg      case ButtonRelease:
205444c061aSmrg	if (modMatch->lateModifiers) {
206444c061aSmrg	    resolved = _XtComputeLateBindings(XtDisplay(widget),
207444c061aSmrg					      modMatch->lateModifiers,
208444c061aSmrg					      &careOn, &careMask);
209444c061aSmrg	    if (!resolved) break;
210444c061aSmrg	}
211444c061aSmrg	careOn |= modMatch->modifiers;
212444c061aSmrg	XtGrabButton(
213444c061aSmrg		     widget,
214444c061aSmrg		     (unsigned) typeMatch->eventCode,
215444c061aSmrg		     careOn,
216444c061aSmrg		     grabP->owner_events,
217444c061aSmrg		     grabP->event_mask,
218444c061aSmrg		     grabP->pointer_mode,
219444c061aSmrg		     grabP->keyboard_mode,
220444c061aSmrg		     None,
221444c061aSmrg		     None
222444c061aSmrg		     );
223444c061aSmrg	break;
224444c061aSmrg
225444c061aSmrg      case KeyPress:
226444c061aSmrg      case KeyRelease:
227444c061aSmrg	GrabAllCorrectKeys(widget, typeMatch, modMatch, grabP);
228444c061aSmrg	break;
229444c061aSmrg
230444c061aSmrg      case EnterNotify:
231444c061aSmrg	break;
232444c061aSmrg
233444c061aSmrg      default:
234444c061aSmrg	XtAppWarningMsg(XtWidgetToApplicationContext(widget),
235444c061aSmrg			"invalidPopup","unsupportedOperation",XtCXtToolkitError,
236444c061aSmrg			"Pop-up menu creation is only supported on Button, Key or EnterNotify events.",
237444c061aSmrg			(String *)NULL, (Cardinal *)NULL);
238444c061aSmrg	break;
239444c061aSmrg    }
240444c061aSmrg    UNLOCK_PROCESS;
241444c061aSmrg    return False;
242444c061aSmrg}
243444c061aSmrg
244444c061aSmrgvoid _XtRegisterGrabs(
245444c061aSmrg    Widget widget)
246444c061aSmrg{
247444c061aSmrg    XtTranslations 	xlations = widget->core.tm.translations;
248444c061aSmrg    TMComplexStateTree 	*stateTreePtr;
249444c061aSmrg    unsigned int 	count;
250444c061aSmrg    TMShortCard		i;
251444c061aSmrg    TMBindData   	bindData = (TMBindData) widget->core.tm.proc_table;
252444c061aSmrg    XtActionProc	*procs;
253444c061aSmrg
254444c061aSmrg    if (! XtIsRealized(widget) || widget->core.being_destroyed)
255444c061aSmrg	return;
256444c061aSmrg
257444c061aSmrg    /* walk the widget instance action bindings table looking for */
258444c061aSmrg    /* actions registered as grab actions. */
259444c061aSmrg    /* when you find one, do a grab on the triggering event */
260444c061aSmrg
261444c061aSmrg    if (xlations == NULL) return;
262444c061aSmrg    stateTreePtr = (TMComplexStateTree *) xlations->stateTreeTbl;
263444c061aSmrg    if (*stateTreePtr == NULL) return;
264444c061aSmrg    for (i = 0; i < xlations->numStateTrees; i++, stateTreePtr++) {
265444c061aSmrg	if (bindData->simple.isComplex)
266444c061aSmrg	  procs = TMGetComplexBindEntry(bindData, i)->procs;
267444c061aSmrg	else
268444c061aSmrg	  procs = TMGetSimpleBindEntry(bindData, i)->procs;
269444c061aSmrg	for (count=0; count < (*stateTreePtr)->numQuarks; count++) {
270444c061aSmrg	    GrabActionRec* grabP;
271444c061aSmrg	    DoGrabRec      doGrab;
272444c061aSmrg
273444c061aSmrg	    LOCK_PROCESS;
274444c061aSmrg	    for (grabP = grabActionList; grabP != NULL; grabP = grabP->next) {
275444c061aSmrg		if (grabP->action_proc == procs[count]) {
276444c061aSmrg		    /* we've found a "grabber" in the action table. Find the
277444c061aSmrg		     * states that call this action.  Note that if there is
278444c061aSmrg		     * more than one "grabber" in the action table, we end
279444c061aSmrg		     * up searching all of the states multiple times.
280444c061aSmrg		     */
281444c061aSmrg		    doGrab.widget = widget;
282444c061aSmrg		    doGrab.grabP = grabP;
283444c061aSmrg		    doGrab.count = count;
284444c061aSmrg		    _XtTraverseStateTree((TMStateTree)*stateTreePtr,
285444c061aSmrg					 DoGrab,
286444c061aSmrg					 (XtPointer)&doGrab);
287444c061aSmrg		}
288444c061aSmrg	    }
289444c061aSmrg	    UNLOCK_PROCESS;
290444c061aSmrg	}
291444c061aSmrg    }
292444c061aSmrg}
293444c061aSmrg
294444c061aSmrgvoid XtRegisterGrabAction(
295444c061aSmrg    XtActionProc action_proc,
296444c061aSmrg    _XtBoolean owner_events,
297444c061aSmrg    unsigned int event_mask,
298444c061aSmrg    int pointer_mode,
299444c061aSmrg    int keyboard_mode)
300444c061aSmrg{
301444c061aSmrg    GrabActionRec* actionP;
302444c061aSmrg
303444c061aSmrg    LOCK_PROCESS;
304444c061aSmrg    for (actionP = grabActionList; actionP != NULL; actionP = actionP->next) {
305444c061aSmrg	if (actionP->action_proc == action_proc) break;
306444c061aSmrg    }
307444c061aSmrg    if (actionP == NULL) {
308444c061aSmrg	actionP = XtNew(GrabActionRec);
309444c061aSmrg	actionP->action_proc = action_proc;
310444c061aSmrg	actionP->next = grabActionList;
311444c061aSmrg	grabActionList = actionP;
312444c061aSmrg    }
313444c061aSmrg#ifdef DEBUG
314444c061aSmrg    else
315444c061aSmrg	if (   actionP->owner_events != owner_events
316444c061aSmrg	    || actionP->event_mask != event_mask
317444c061aSmrg	    || actionP->pointer_mode != pointer_mode
318444c061aSmrg	    || actionP->keyboard_mode != keyboard_mode) {
319444c061aSmrg	    Cardinal n = 0;
320444c061aSmrg	    XtWarningMsg(
321444c061aSmrg		"argsReplaced", "xtRegisterGrabAction", XtCXtToolkitError,
322444c061aSmrg		"XtRegisterGrabAction called on same proc with different args",
323444c061aSmrg			 NULL, &n);
324444c061aSmrg	}
325444c061aSmrg#endif /*DEBUG*/
326444c061aSmrg
327444c061aSmrg    actionP->owner_events = owner_events;
328444c061aSmrg    actionP->event_mask = event_mask;
329444c061aSmrg    actionP->pointer_mode = pointer_mode;
330444c061aSmrg    actionP->keyboard_mode = keyboard_mode;
331444c061aSmrg    UNLOCK_PROCESS;
332444c061aSmrg}
333444c061aSmrg
334444c061aSmrg/*ARGSUSED*/
335444c061aSmrgvoid _XtGrabInitialize(
336444c061aSmrg    XtAppContext	app)
337444c061aSmrg{
338444c061aSmrg    LOCK_PROCESS;
339444c061aSmrg    if (grabActionList == NULL)
340444c061aSmrg	XtRegisterGrabAction( XtMenuPopupAction, True,
341444c061aSmrg			      (unsigned)(ButtonPressMask | ButtonReleaseMask),
342444c061aSmrg			      GrabModeAsync,
343444c061aSmrg			      GrabModeAsync
344444c061aSmrg			    );
345444c061aSmrg    UNLOCK_PROCESS;
346444c061aSmrg
347444c061aSmrg}
348