PassivGrab.c 27.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
/* $Xorg: PassivGrab.c,v 1.5 2001/02/09 02:03:56 xorgcvs Exp $ */

/********************************************************

Copyright 1988 by Hewlett-Packard Company
Copyright 1987, 1988, 1989,1990 by Digital Equipment Corporation, Maynard, Massachusetts
Copyright 1993 by Sun Microsystems, Inc. Mountain View, CA.

Permission to use, copy, modify, and distribute this software
and its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appear in all
copies and that both that copyright notice and this permission
notice appear in supporting documentation, and that the names of
Hewlett-Packard, Digital, or Sun not be used in advertising or
publicity pertaining to distribution of the software without specific,
written prior permission.

DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

SUN DISCLAIMS ALL WARRANTIES WITH REGARD TO  THIS  SOFTWARE,
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT-
NESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SUN BE  LI-
ABLE  FOR  ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,  DATA  OR
PROFITS,  WHETHER  IN  AN  ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
THE USE OR PERFORMANCE OF THIS SOFTWARE.

********************************************************/
/* $XFree86$ */

/*

Copyright 1987, 1988, 1989, 1990, 1994, 1998  The Open Group

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.

*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "IntrinsicI.h"
#include "StringDefs.h"
#include "PassivGraI.h"

/* typedef unsigned long Mask; */
#define BITMASK(i) (((Mask)1) << ((i) & 31))
#define MASKIDX(i) ((i) >> 5)
#define MASKWORD(buf, i) buf[MASKIDX(i)]
#define BITSET(buf, i) MASKWORD(buf, i) |= BITMASK(i)
#define BITCLEAR(buf, i) MASKWORD(buf, i) &= ~BITMASK(i)
#define GETBIT(buf, i) (MASKWORD(buf, i) & BITMASK(i))
#define MasksPerDetailMask 8

#define pDisplay(grabPtr) (((grabPtr)->widget)->core.screen->display)
#define pWindow(grabPtr) (((grabPtr)->widget)->core.window)


/***************************************************************************/
/*********************** Internal Support Routines *************************/
/***************************************************************************/

/*
 * Turn off (clear) the bit in the specified detail mask which is associated
 * with the detail.
 */

static void DeleteDetailFromMask(
    Mask **ppDetailMask,
    unsigned short detail)
{
    Mask *pDetailMask = *ppDetailMask;

    if (!pDetailMask) {
	int i;
	pDetailMask = (Mask *)__XtMalloc(sizeof(Mask) * MasksPerDetailMask);
	for (i = MasksPerDetailMask; --i >= 0; )
	    pDetailMask[i] = ~0;
	*ppDetailMask = pDetailMask;
    }
    BITCLEAR((pDetailMask), detail);
}


/*
 * Make an exact copy of the specified detail mask.
 */

static Mask *CopyDetailMask(
    Mask *pOriginalDetailMask)
{
    Mask *pTempMask;
    int i;

    if (!pOriginalDetailMask)
	return NULL;

    pTempMask = (Mask *)__XtMalloc(sizeof(Mask) * MasksPerDetailMask);

    for ( i = 0; i < MasksPerDetailMask; i++)
      pTempMask[i]= pOriginalDetailMask[i];

    return pTempMask;
}


/*
 * Allocate a new grab entry, and fill in all of the fields using the
 * specified parameters.
 */

static XtServerGrabPtr CreateGrab(
    Widget	widget,
    Boolean	ownerEvents,
    Modifiers	modifiers,
    KeyCode 	keybut,
    int		pointer_mode,
    int		keyboard_mode,
    Mask	event_mask,
    Window 	confine_to,
    Cursor 	cursor,
    Boolean	need_ext)
{
    XtServerGrabPtr grab;

    if (confine_to || cursor)
	need_ext = True;
    grab = (XtServerGrabPtr)__XtMalloc(sizeof(XtServerGrabRec) +
				     (need_ext ? sizeof(XtServerGrabExtRec)
				      : 0));
    grab->next = NULL;
    grab->widget = widget;
    grab->ownerEvents = ownerEvents;
    grab->pointerMode = pointer_mode;
    grab->keyboardMode = keyboard_mode;
    grab->eventMask = event_mask;
    grab->hasExt = need_ext;
    grab->confineToIsWidgetWin = (XtWindow (widget) == confine_to);
    grab->modifiers = modifiers;
    grab->keybut = keybut;
    if (need_ext) {
	XtServerGrabExtPtr ext = GRABEXT(grab);
	ext->pModifiersMask = NULL;
	ext->pKeyButMask = NULL;
	ext->confineTo = confine_to;
	ext->cursor = cursor;
    }
    return grab;
}


/*
 * Free up the space occupied by a grab entry.
 */

static void FreeGrab(
    XtServerGrabPtr pGrab)
{
    if (pGrab->hasExt) {
	XtServerGrabExtPtr ext = GRABEXT(pGrab);
	if (ext->pModifiersMask)
	    XtFree((char *)ext->pModifiersMask);
	if (ext->pKeyButMask)
	    XtFree((char *)ext->pKeyButMask);
    }
    XtFree((char *)pGrab);
}

typedef struct _DetailRec {
    unsigned short 	exact;
    Mask  		*pMask;
} DetailRec, *DetailPtr;

/*
 * If the first detail is set to 'exception' and the second detail
 * is contained in the mask of the first, then TRUE is returned.
 */

static Bool IsInGrabMask(
    register DetailPtr firstDetail,
    register DetailPtr secondDetail,
    unsigned short exception)
{
    if (firstDetail->exact == exception) {
	if (!firstDetail->pMask)
	    return TRUE;

	/* (at present) never called with two non-null pMasks */
	if (secondDetail->exact == exception)
	    return FALSE;

	if (GETBIT(firstDetail->pMask, secondDetail->exact))
	    return TRUE;
    }

    return FALSE;
}


/*
 * If neither of the details is set to 'exception', and they match
 * exactly, then TRUE is returned.
 */

static Bool IdenticalExactDetails(
    unsigned short firstExact,
    unsigned short secondExact,
    unsigned short exception)
{
    if ((firstExact == exception) || (secondExact == exception))
	return FALSE;

    if (firstExact == secondExact)
	return TRUE;

    return FALSE;
}


/*
 * If the first detail is set to 'exception', and its mask has the bit
 * enabled which corresponds to the second detail, OR if neither of the
 * details is set to 'exception' and the details match exactly, then
 * TRUE is returned.
 */

static Bool DetailSupersedesSecond(
    register DetailPtr firstDetail,
    register DetailPtr secondDetail,
    unsigned short exception)
{
    if (IsInGrabMask(firstDetail, secondDetail, exception))
	return TRUE;

    if (IdenticalExactDetails(firstDetail->exact, secondDetail->exact,
			      exception))
	return TRUE;

    return FALSE;
}


/*
 * If the two grab events match exactly, or if the first grab entry
 * 'encompasses' the second grab entry, then TRUE is returned.
 */

static Bool GrabSupersedesSecond(
    register XtServerGrabPtr pFirstGrab,
    register XtServerGrabPtr pSecondGrab)
{
    DetailRec first, second;

    first.exact = pFirstGrab->modifiers;
    if (pFirstGrab->hasExt)
	first.pMask = GRABEXT(pFirstGrab)->pModifiersMask;
    else
	first.pMask = NULL;
    second.exact = pSecondGrab->modifiers;
    if (pSecondGrab->hasExt)
	second.pMask = GRABEXT(pSecondGrab)->pModifiersMask;
    else
	second.pMask = NULL;
    if (!DetailSupersedesSecond(&first, &second, (unsigned short)AnyModifier))
      return FALSE;

    first.exact = pFirstGrab->keybut;
    if (pFirstGrab->hasExt)
	first.pMask = GRABEXT(pFirstGrab)->pKeyButMask;
    else
	first.pMask = NULL;
    second.exact = pSecondGrab->keybut;
    if (pSecondGrab->hasExt)
	second.pMask = GRABEXT(pSecondGrab)->pKeyButMask;
    else
	second.pMask = NULL;
    if (DetailSupersedesSecond(&first, &second, (unsigned short)AnyKey))
      return TRUE;

    return FALSE;
}


/*
 * Two grabs are considered to be matching if either of the following are true:
 *
 * 1) The two grab entries match exactly, or the first grab entry
 *    encompasses the second grab entry.
 * 2) The second grab entry encompasses the first grab entry.
 * 3) The keycodes match exactly, and one entry's modifiers encompasses
 *    the others.
 * 4) The keycode for one entry encompasses the other, and the detail
 *    for the other entry encompasses the first.
 */

static Bool GrabMatchesSecond(
    register XtServerGrabPtr pFirstGrab,
    register XtServerGrabPtr pSecondGrab)
{
    DetailRec firstD, firstM, secondD, secondM;

    if (pDisplay(pFirstGrab) != pDisplay(pSecondGrab))
	return FALSE;

    if (GrabSupersedesSecond(pFirstGrab, pSecondGrab))
	return TRUE;

    if (GrabSupersedesSecond(pSecondGrab, pFirstGrab))
	return TRUE;

    firstD.exact = pFirstGrab->keybut;
    firstM.exact = pFirstGrab->modifiers;
    if (pFirstGrab->hasExt) {
	firstD.pMask = GRABEXT(pFirstGrab)->pKeyButMask;
	firstM.pMask = GRABEXT(pFirstGrab)->pModifiersMask;
    } else {
	firstD.pMask = NULL;
	firstM.pMask = NULL;
    }
    secondD.exact = pSecondGrab->keybut;
    secondM.exact = pSecondGrab->modifiers;
    if (pSecondGrab->hasExt) {
	secondD.pMask = GRABEXT(pSecondGrab)->pKeyButMask;
	secondM.pMask = GRABEXT(pSecondGrab)->pModifiersMask;
    } else {
	secondD.pMask = NULL;
	secondM.pMask = NULL;
    }

    if (DetailSupersedesSecond(&secondD, &firstD, (unsigned short)AnyKey) &&
	DetailSupersedesSecond(&firstM, &secondM, (unsigned short)AnyModifier))
	return TRUE;

    if (DetailSupersedesSecond(&firstD, &secondD, (unsigned short)AnyKey) &&
	DetailSupersedesSecond(&secondM, &firstM, (unsigned short)AnyModifier))
	return TRUE;

    return FALSE;
}


/*
 * Delete a grab combination from the passive grab list.  Each entry will
 * be checked to see if it is affected by the grab being deleted.  This
 * may result in multiple entries being modified/deleted.
 */

static void DeleteServerGrabFromList(
    XtServerGrabPtr 	*passiveListPtr,
    XtServerGrabPtr 	pMinuendGrab)
{
    register XtServerGrabPtr *next;
    register XtServerGrabPtr grab;
    register XtServerGrabExtPtr ext;

    for (next = passiveListPtr; (grab = *next); )
    {
	if (GrabMatchesSecond(grab, pMinuendGrab) &&
	    (pDisplay(grab) == pDisplay(pMinuendGrab)))
	{
	    if (GrabSupersedesSecond(pMinuendGrab, grab))
	    {
		/*
		 * The entry being deleted encompasses the list entry,
		 * so delete the list entry.
		 */
		*next = grab->next;
		FreeGrab(grab);
		continue;
	    }

	    if (!grab->hasExt) {
		grab = (XtServerGrabPtr)
		    XtRealloc((char *)grab, (sizeof(XtServerGrabRec) +
					     sizeof(XtServerGrabExtRec)));
		*next = grab;
		grab->hasExt = True;
		ext = GRABEXT(grab);
		ext->pKeyButMask = NULL;
		ext->pModifiersMask = NULL;
		ext->confineTo = None;
		ext->cursor = None;
	    } else
		ext = GRABEXT(grab);
	    if ((grab->keybut == AnyKey) && (grab->modifiers != AnyModifier))
	    {
		/*
		 * If the list entry has the key detail of AnyKey, and
		 * a modifier detail not set to AnyModifier, then we
		 * simply need to turn off the key detail bit in the
		 * list entry's key detail mask.
		 */
		DeleteDetailFromMask(&ext->pKeyButMask, pMinuendGrab->keybut);
	    } else if ((grab->modifiers == AnyModifier) &&
		       (grab->keybut != AnyKey)) {
		/*
		 * The list entry has a specific key detail, but its
		 * modifier detail is set to AnyModifier; so, we only
		 * need to turn off the specified modifier combination
		 * in the list entry's modifier mask.
		 */
		DeleteDetailFromMask(&ext->pModifiersMask,
				     pMinuendGrab->modifiers);
	    } else if ((pMinuendGrab->keybut != AnyKey) &&
		       (pMinuendGrab->modifiers != AnyModifier)) {
		/*
		 * The list entry has a key detail of AnyKey and a
		 * modifier detail of AnyModifier; the entry being
		 * deleted has a specific key and a specific modifier
		 * combination.  Therefore, we need to mask off the
		 * keycode from the list entry, and also create a
		 * new entry for this keycode, which has a modifier
		 * mask set to AnyModifier & ~(deleted modifiers).
		 */
		XtServerGrabPtr pNewGrab;

		DeleteDetailFromMask(&ext->pKeyButMask, pMinuendGrab->keybut);
		pNewGrab = CreateGrab(grab->widget,
				      (Boolean)grab->ownerEvents,
				      (Modifiers)AnyModifier,
				      pMinuendGrab->keybut,
				      (int)grab->pointerMode,
				      (int)grab->keyboardMode,
				      (Mask)0, (Window)0, (Cursor)0, True);
		GRABEXT(pNewGrab)->pModifiersMask =
		    CopyDetailMask(ext->pModifiersMask);

		DeleteDetailFromMask(&GRABEXT(pNewGrab)->pModifiersMask,
				     pMinuendGrab->modifiers);

		pNewGrab->next = *passiveListPtr;
		*passiveListPtr = pNewGrab;
	    } else if (pMinuendGrab->keybut == AnyKey) {
		/*
		 * The list entry has keycode AnyKey and modifier
		 * AnyModifier; the entry being deleted has
		 * keycode AnyKey and specific modifiers.  So we
		 * simply need to mask off the specified modifier
		 * combination.
		 */
		DeleteDetailFromMask(&ext->pModifiersMask,
				     pMinuendGrab->modifiers);
	    } else {
		/*
		 * The list entry has keycode AnyKey and modifier
		 * AnyModifier; the entry being deleted has a
		 * specific keycode and modifier AnyModifier.  So
		 * we simply need to mask off the specified
		 * keycode.
		 */
		DeleteDetailFromMask(&ext->pKeyButMask, pMinuendGrab->keybut);
	    }
	}
	next = &(*next)->next;
    }
}

static void DestroyPassiveList(
    XtServerGrabPtr	*passiveListPtr)
{
    XtServerGrabPtr	next, grab;

    for (next = *passiveListPtr; next; ) {
	grab = next;
	next = grab->next;

	/* not necessary to explicitly ungrab key or button;
	 * window is being destroyed so server will take care of it.
	 */

	FreeGrab(grab);
    }
}


/*
 * This function is called at widget destroy time to clean up
 */
/*ARGSUSED*/
void _XtDestroyServerGrabs(
    Widget		w,
    XtPointer		closure,
    XtPointer		call_data) /* unused */
{
    XtPerWidgetInput	pwi = (XtPerWidgetInput)closure;
    XtPerDisplayInput	pdi;

    LOCK_PROCESS;
    pdi = _XtGetPerDisplayInput(XtDisplay(w));
    _XtClearAncestorCache(w);
    UNLOCK_PROCESS;

    /* Remove the active grab, if necessary */
    if ((pdi->keyboard.grabType != XtNoServerGrab) &&
	(pdi->keyboard.grab.widget == w)) {
	pdi->keyboard.grabType = XtNoServerGrab;
	pdi->activatingKey = (KeyCode)0;
    }
    if ((pdi->pointer.grabType != XtNoServerGrab) &&
	(pdi->pointer.grab.widget == w))
	pdi->pointer.grabType = XtNoServerGrab;

    DestroyPassiveList(&pwi->keyList);
    DestroyPassiveList(&pwi->ptrList);

    _XtFreePerWidgetInput(w, pwi);
}

/*
 * If the incoming event is on the passive grab list, then activate
 * the grab.  The grab will remain in effect until the key is released.
 */

XtServerGrabPtr _XtCheckServerGrabsOnWidget (
    XEvent 		*event,
    Widget		widget,
    _XtBoolean		isKeyboard)
{
    register XtServerGrabPtr grab;
    XtServerGrabRec 	tempGrab;
    XtServerGrabPtr	*passiveListPtr;
    XtPerWidgetInput	pwi;

    LOCK_PROCESS;
    pwi = _XtGetPerWidgetInput(widget, FALSE);
    UNLOCK_PROCESS;
    if (!pwi)
	return (XtServerGrabPtr)NULL;
    if (isKeyboard)
	passiveListPtr = &pwi->keyList;
    else
	passiveListPtr = &pwi->ptrList;

    /*
     * if either there is no entry in the context manager or the entry
     * is empty, or the keyboard is grabed, then no work to be done
     */
    if (!*passiveListPtr)
	return (XtServerGrabPtr)NULL;

    /* Take only the lower thirteen bits as modifier state.  The X Keyboard
     * Extension may be representing keyboard group state in two upper bits.
     */
    tempGrab.widget = widget;
    tempGrab.keybut = event->xkey.keycode; /* also xbutton.button */
    tempGrab.modifiers = event->xkey.state & 0x1FFF; /*also xbutton.state*/
    tempGrab.hasExt = False;

    for (grab = *passiveListPtr; grab; grab = grab->next) {
	if (GrabMatchesSecond(&tempGrab, grab))
	    return (grab);
    }
    return (XtServerGrabPtr)NULL;
}

/*
 * This handler is needed to guarantee that we see releases on passive
 * button grabs for widgets that haven't selected for button release.
 */

/*ARGSUSED*/
static void  ActiveHandler (
    Widget 		widget,
    XtPointer		pdi,
    XEvent 		*event,
    Boolean		*cont)
{
    /* nothing */
}


/*
 *	MakeGrab
 */
static void  MakeGrab(
    XtServerGrabPtr	grab,
    XtServerGrabPtr	*passiveListPtr,
    Boolean		isKeyboard,
    XtPerDisplayInput	pdi,
    XtPerWidgetInput	pwi)
{
    if (!isKeyboard && !pwi->active_handler_added) {
	XtAddEventHandler(grab->widget, ButtonReleaseMask, FALSE,
			  ActiveHandler, (XtPointer)pdi);
	pwi->active_handler_added = TRUE;
    }

    if (isKeyboard) {
	XGrabKey(pDisplay(grab),
		 grab->keybut, grab->modifiers,
		 pWindow(grab), grab->ownerEvents,
		 grab->pointerMode, grab->keyboardMode);
    } else {
	Window confineTo = None;
	Cursor cursor = None;

	if (grab->hasExt) {
	    if (grab->confineToIsWidgetWin)
		confineTo = XtWindow (grab->widget);
	    else
		confineTo = GRABEXT(grab)->confineTo;
	    cursor = GRABEXT(grab)->cursor;
	}
	XGrabButton(pDisplay(grab),
		    grab->keybut, grab->modifiers,
		    pWindow(grab), grab->ownerEvents, grab->eventMask,
		    grab->pointerMode, grab->keyboardMode,
		    confineTo, cursor);
    }

    /* Add the new grab entry to the passive key grab list */
    grab->next = *passiveListPtr;
    *passiveListPtr = grab;
}

static void MakeGrabs(
    XtServerGrabPtr	*passiveListPtr,
    Boolean		isKeyboard,
    XtPerDisplayInput	pdi)
{
    XtServerGrabPtr	next = *passiveListPtr;
    XtServerGrabPtr	grab;
    XtPerWidgetInput	pwi;
    /*
     * make MakeGrab build a new list that has had the merge
     * processing done on it. Start with an empty list
     * (passiveListPtr).
     */
    LOCK_PROCESS;
    *passiveListPtr = NULL;
    while (next)
      {
	  grab = next;
	  next = grab->next;
	  pwi = _XtGetPerWidgetInput(grab->widget, FALSE);
	  MakeGrab(grab, passiveListPtr, isKeyboard, pdi, pwi);
      }
    UNLOCK_PROCESS;
}

/*
 * This function is the event handler attached to the associated widget
 * when grabs need to be added, but the widget is not yet realized.  When
 * it is first mapped, this handler will be invoked, and it will add all
 * needed grabs.
 */

/*ARGSUSED*/
static void  RealizeHandler (
    Widget 		widget,
    XtPointer		closure,
    XEvent 		*event,	/* unused */
    Boolean		*cont)	/* unused */
{
    XtPerWidgetInput	pwi = (XtPerWidgetInput)closure;
    XtPerDisplayInput	pdi;

    LOCK_PROCESS;
    pdi = _XtGetPerDisplayInput(XtDisplay(widget));
    UNLOCK_PROCESS;
    MakeGrabs(&pwi->keyList, KEYBOARD, pdi);
    MakeGrabs(&pwi->ptrList, POINTER, pdi);

    XtRemoveEventHandler(widget, XtAllEvents, True,
			 RealizeHandler, (XtPointer)pwi);
    pwi->realize_handler_added = FALSE;
}

/***************************************************************************/
/**************************** Global Routines ******************************/
/***************************************************************************/


/*
 * Routine used by an application to set up a passive grab for a key/modifier
 * combination.
 */

static
void GrabKeyOrButton (
    Widget	widget,
    KeyCode	keyOrButton,
    Modifiers	modifiers,
    Boolean	owner_events,
    int 	pointer_mode,
    int 	keyboard_mode,
    Mask	event_mask,
    Window 	confine_to,
    Cursor 	cursor,
    Boolean	isKeyboard)
{
    XtServerGrabPtr	*passiveListPtr;
    XtServerGrabPtr 	newGrab;
    XtPerWidgetInput	pwi;
    XtPerDisplayInput	pdi;


    XtCheckSubclass(widget, coreWidgetClass, "in XtGrabKey or XtGrabButton");
    LOCK_PROCESS;
    pwi = _XtGetPerWidgetInput(widget, TRUE);
    if (isKeyboard)
      passiveListPtr = &pwi->keyList;
    else
      passiveListPtr = &pwi->ptrList;
    pdi = _XtGetPerDisplayInput(XtDisplay(widget));
    UNLOCK_PROCESS;
    newGrab = CreateGrab(widget, owner_events, modifiers,
			 keyOrButton, pointer_mode, keyboard_mode,
			 event_mask, confine_to, cursor, False);
    /*
     *  if the widget is realized then process the entry into the grab
     * list. else if the list is empty (i.e. first time) then add the
     * event handler. then add the raw entry to the list for processing
     * in the handler at realize time.
     */
    if (XtIsRealized(widget))
      MakeGrab(newGrab, passiveListPtr, isKeyboard, pdi, pwi);
    else {
	if (!pwi->realize_handler_added)
	    {
		XtAddEventHandler(widget, StructureNotifyMask, FALSE,
				  RealizeHandler,
				  (XtPointer)pwi);
		pwi->realize_handler_added = TRUE;
	    }

	while (*passiveListPtr)
	    passiveListPtr = &(*passiveListPtr)->next;
	*passiveListPtr = newGrab;
    }
}


static
void   UngrabKeyOrButton (
    Widget	widget,
    int		keyOrButton,
    Modifiers	modifiers,
    Boolean	isKeyboard)
{
    XtServerGrabRec 	tempGrab;
    XtPerWidgetInput	pwi;

    XtCheckSubclass(widget, coreWidgetClass,
		    "in XtUngrabKey or XtUngrabButton");

    /* Build a temporary grab list entry */
    tempGrab.widget = widget;
    tempGrab.modifiers = modifiers;
    tempGrab.keybut = keyOrButton;
    tempGrab.hasExt = False;

    LOCK_PROCESS;
    pwi = _XtGetPerWidgetInput(widget, FALSE);
    UNLOCK_PROCESS;
    /*
     * if there is no entry in the context manager then somethings wrong
     */
    if (!pwi)
      {
	  XtAppWarningMsg(XtWidgetToApplicationContext(widget),
		       "invalidGrab", "ungrabKeyOrButton", XtCXtToolkitError,
		       "Attempt to remove nonexistent passive grab",
		       (String *)NULL, (Cardinal *)NULL);
	  return;
      }

    if (XtIsRealized(widget))
      {
	  if (isKeyboard)
	    XUngrabKey(widget->core.screen->display,
		       keyOrButton, (unsigned int)modifiers,
		       widget->core.window);
	  else
	    XUngrabButton(widget->core.screen->display,
			  keyOrButton, (unsigned int)modifiers,
			  widget->core.window);
      }


    /* Delete all entries which are encompassed by the specified grab. */
    DeleteServerGrabFromList(isKeyboard ? &pwi->keyList : &pwi->ptrList,
			     &tempGrab);
}

void  XtGrabKey (
    Widget	widget,
    _XtKeyCode	keycode,
    Modifiers	modifiers,
    _XtBoolean	owner_events,
    int 	pointer_mode,
    int 	keyboard_mode)
{
    WIDGET_TO_APPCON(widget);

    LOCK_APP(app);
    GrabKeyOrButton(widget, (KeyCode)keycode, modifiers, owner_events,
		    pointer_mode, keyboard_mode,
		    (Mask)0, (Window)None, (Cursor)None, KEYBOARD);
    UNLOCK_APP(app);
}

void  XtGrabButton(
    Widget	widget,
    int		button,
    Modifiers	modifiers,
    _XtBoolean	owner_events,
    unsigned int event_mask,
    int 	pointer_mode,
    int 	keyboard_mode,
    Window 	confine_to,
    Cursor 	cursor)
{
    WIDGET_TO_APPCON(widget);

    LOCK_APP(app);
    GrabKeyOrButton(widget, (KeyCode)button, modifiers, owner_events,
		    pointer_mode, keyboard_mode,
		    (Mask)event_mask, confine_to, cursor, POINTER);
    UNLOCK_APP(app);
}


/*
 * Routine used by an application to clear a passive grab for a key/modifier
 * combination.
 */

void   XtUngrabKey (
    Widget	widget,
    _XtKeyCode	keycode,
    Modifiers	modifiers)
{
    WIDGET_TO_APPCON(widget);

    LOCK_APP(app);
    UngrabKeyOrButton(widget, (int)keycode, modifiers, KEYBOARD);
    UNLOCK_APP(app);
}

void   XtUngrabButton (
    Widget	widget,
    unsigned int button,
    Modifiers	modifiers)
{
    WIDGET_TO_APPCON(widget);

    LOCK_APP(app);
    UngrabKeyOrButton(widget, (KeyCode)button, modifiers, POINTER);
    UNLOCK_APP(app);
}

/*
 * Active grab of Device. clear any client side grabs so we dont lock
 */
static int GrabDevice (
    Widget	widget,
    Boolean	owner_events,
    int 	pointer_mode,
    int 	keyboard_mode,
    Mask	event_mask,
    Window 	confine_to,
    Cursor 	cursor,
    Time	time,
    Boolean	isKeyboard)
{
    XtPerDisplayInput	pdi;
    int			returnVal;

    XtCheckSubclass(widget, coreWidgetClass,
		    "in XtGrabKeyboard or XtGrabPointer");
    if (!XtIsRealized(widget))
	return GrabNotViewable;
    LOCK_PROCESS;
    pdi = _XtGetPerDisplayInput(XtDisplay(widget));
    UNLOCK_PROCESS;
    if (!isKeyboard)
      returnVal = XGrabPointer(XtDisplay(widget), XtWindow(widget),
			       owner_events, event_mask,
			       pointer_mode, keyboard_mode,
			       confine_to, cursor, time);
    else
      returnVal = XGrabKeyboard(XtDisplay(widget), XtWindow(widget),
				owner_events, pointer_mode,
				keyboard_mode, time);

    if (returnVal == GrabSuccess) {
	  XtDevice		device;

	  device = isKeyboard ? &pdi->keyboard : &pdi->pointer;
	  /* fill in the server grab rec */
	  device->grab.widget = widget;
	  device->grab.modifiers = 0;
	  device->grab.keybut = 0;
	  device->grab.ownerEvents = owner_events;
	  device->grab.pointerMode = pointer_mode;
	  device->grab.keyboardMode = keyboard_mode;
	  device->grab.hasExt = False;
	  device->grabType = XtActiveServerGrab;
	  pdi->activatingKey = (KeyCode)0;
      }
    return returnVal;
}

static void   UngrabDevice(
    Widget	widget,
    Time	time,
    Boolean	isKeyboard)
{
    XtPerDisplayInput pdi;
    XtDevice device;

    LOCK_PROCESS;
    pdi = _XtGetPerDisplayInput(XtDisplay(widget));
    UNLOCK_PROCESS;
    device = isKeyboard ? &pdi->keyboard : &pdi->pointer;
    XtCheckSubclass(widget, coreWidgetClass,
		    "in XtUngrabKeyboard or XtUngrabPointer");

    if (device->grabType != XtNoServerGrab) {

	if (device->grabType != XtPseudoPassiveServerGrab
	    && XtIsRealized(widget)) {
	    if (isKeyboard)
		XUngrabKeyboard(XtDisplay(widget), time);
	    else
		XUngrabPointer(XtDisplay(widget), time);
	}
	device->grabType = XtNoServerGrab;
	pdi->activatingKey = (KeyCode)0;
    }
}


/*
 * Active grab of keyboard. clear any client side grabs so we dont lock
 */
int XtGrabKeyboard (
    Widget	widget,
    _XtBoolean	owner_events,
    int 	pointer_mode,
    int 	keyboard_mode,
    Time	time)
{
    int retval;
    WIDGET_TO_APPCON(widget);

    LOCK_APP(app);
    retval = GrabDevice (widget, owner_events,
			pointer_mode, keyboard_mode,
			(Mask)0, (Window)None, (Cursor)None, time, KEYBOARD);
    UNLOCK_APP(app);
    return retval;
}


/*
 * Ungrab the keyboard
 */

void   XtUngrabKeyboard(
    Widget	widget,
    Time	time)
{
    WIDGET_TO_APPCON(widget);

    LOCK_APP(app);
    UngrabDevice(widget, time, KEYBOARD);
    UNLOCK_APP(app);
}




/*
 * grab the pointer
 */
int XtGrabPointer (
    Widget	widget,
    _XtBoolean	owner_events,
    unsigned int event_mask,
    int 	pointer_mode,
    int 	keyboard_mode,
    Window 	confine_to,
    Cursor 	cursor,
    Time	time)
{
    int retval;
    WIDGET_TO_APPCON(widget);

    LOCK_APP(app);
    retval = GrabDevice (widget, owner_events,
			pointer_mode, keyboard_mode,
			(Mask)event_mask, confine_to,
			cursor, time, POINTER);
    UNLOCK_APP(app);
    return retval;
}


/*
 * Ungrab the pointer
 */

void   XtUngrabPointer(
    Widget	widget,
    Time	time)
{
    WIDGET_TO_APPCON(widget);

    LOCK_APP(app);
    UngrabDevice(widget, time, POINTER);
    UNLOCK_APP(app);
}


void _XtRegisterPassiveGrabs (
    Widget	widget)
{
    XtPerWidgetInput	pwi = _XtGetPerWidgetInput (widget, FALSE);

    if (pwi != NULL && !pwi->realize_handler_added) {
	XtAddEventHandler(widget, StructureNotifyMask, FALSE,
			  RealizeHandler,
			  (XtPointer)pwi);
	pwi->realize_handler_added = TRUE;
    }
}