/*
* Copyright (c) 2005 X.Org Foundation L.L.C.
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
* 
* 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
* AUTHORS OR COPYRIGHT HOLDERS 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.
* 
* Copyright (c) Applied Testing and Technology, Inc. 1995
* All Rights Reserved.
* 
* 
* Copyright (c) 1990, 1991  X Consortium
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* 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
* X CONSORTIUM 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 X Consortium 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 X Consortium.
* 
* Copyright 1993 by the Hewlett-Packard Company.
* 
* Copyright 1990, 1991 UniSoft Group Limited.
* 
* 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, and that the names of HP, and UniSoft not be
* used in advertising or publicity pertaining to distribution of the
* software without specific, written prior permission.  HP, and UniSoft
* make no representations about the suitability of this software for any
* purpose.  It is provided "as is" without express or implied warranty.
* 
* 
* 
*/
/*
 * SYNOPSIS:
 *   void
 *   XSelectExtensionEvent(display, w, eclass, ecount)
 *   Display *display;
 *   Window w;
 *   XEventClass *eclass;
 *   int ecount;
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include	<stdlib.h>
#include	<stdio.h>
#include	<string.h>
#include	"xtest.h"
#include	"X11/Xlib.h"
#include	"X11/Xutil.h"
#include	"X11/Xresource.h"
#include	"X11/keysym.h"
#include	"tet_api.h"
#include	"xtestlib.h"
#include	"pixval.h"
#ifdef INPUTEXTENSION
#include        "X11/extensions/XInput.h"
#include        "XItest.h"
#endif

extern	Display	*Dsp;
extern	Window	Win;

extern	Window	ErrdefWindow;
extern	Drawable ErrdefDrawable;
extern	GC		ErrdefGC;
extern	Colormap ErrdefColormap;
extern	Pixmap	ErrdefPixmap;
extern	Atom	ErrdefAtom;
extern	Cursor	ErrdefCursor;
extern	Font	ErrdefFont;


#define T_XSelectExtensionEvent	1
char    *TestName = "XSelectExtensionEvent";

/*
 * Defines for different argument types
 */
#define A_DISPLAY display
#define A_WINDOW w
#define A_DRAWABLE w


/*
 * Arguments to the XSelectExtensionEvent function
 */
static Display *display;
static Window w;
static XEventClass *eclass;
static int ecount;


extern ExtDeviceInfo Devs;
extern int MinKeyCode;
extern int NumButtons;
XDevice *focusdevice;
Display *fdisplay, *client1;

void xi_cleanup()
{
      cleanup();
      client1 = NULL;
}
 
getfocusdevice()
{
int i, j, ndevices;
XInputClassInfo *ip;
XDeviceInfo *list;
XDevice *device;
int ximajor, first, err;

	fdisplay = opendisplay();
	if (!XQueryExtension (fdisplay, INAME, &ximajor, &first, &err)) {
	    untested("%s: Input extension not supported.\n", TestName);
	    return;
	    }

	list = XListInputDevices (fdisplay, &ndevices);
	for (i=0; i<ndevices; i++, list++)
	    if (list->use == IsXExtensionDevice)
		{
		device = XOpenDevice(fdisplay, list->id);
		for (j=0, ip=device->classes; j<device->num_classes; j++, ip++)
		    if (ip->input_class == FocusClass)
			{
			focusdevice = device;
			break;
			}
		}
}

int 	tet_thistest;

/*
 * Called at the beginning of each test purpose to reset the
 * arguments to their initial values
 */
static void
setargs()
{
	display = Dsp;
	w = 0;
	eclass = 0;
	ecount = 0;
}

/*
 * Set the arguments to default values for error tests
 */
static void
seterrdef()
{
	w = ErrdefWindow;
}

static void t001(){

Display *client2;
XEvent	event;
int	n;
XEventClass noextensionevent, devicefocusin;
int	type;
int 	pass = 0, fail = 0;

 	report_purpose(1);

	report_assertion("Assertion XSelectExtensionEvent-1.(B)");
	report_assertion("A call to XSelectExtensionEvent requests that the X server");
	report_assertion("report the events for window w matching event_mask.");
	report_assertion("Reason for omission: There is no known reliable test method for this assertion");

	report_strategy("Create client1.");
	report_strategy("Create window with client1.");
	report_strategy("Select no events with client1 on this window.");
	report_strategy("Create client2.");
	report_strategy("Select DeviceFocusIn events with client2 on this window.");
	report_strategy("Map window.");
	report_strategy("XSync(.., False) on both clients to ensure generated events have come in.");
	report_strategy("Verify that client1 received no events.");
	report_strategy("Verify that client2 received a single MapNotify event for this window.");
	report_strategy("Verify that client2 received no other events.");

	tpstartup();
	setargs();
/* Create client1. */
	client1 = opendisplay();
	if (client1 == (Display *) NULL) {
		delete("Can not open display");
		return;
	}
	else
		CHECK;
	getfocusdevice();
	if (!focusdevice) {
	    report("%s: Required input devices not present\n",TestName);
	    UNTESTED;
	    return;
	    }
	NoExtensionEvent (focusdevice, type, noextensionevent);
/* Create window with client1. */
	w = mkwin(client1, (XVisualInfo *) NULL, (struct area *) NULL, False);
/* Select no events with client1 on this window. */
	BASIC_STARTCALL(client1);
	XSelectExtensionEvent(client1, w, &noextensionevent, 1);
	BASIC_ENDCALL(client1, Success);
/* Create client2. */

	client2 = opendisplay();
	if (client2 == (Display *) NULL) {
		delete("Can not open display");
		return;
	}
	else
		CHECK;
/* Select MapNotify events with client2 on this window. */
	BASIC_STARTCALL(client2);
	DeviceFocusIn (focusdevice, type, devicefocusin);
	XSelectExtensionEvent(client2, w, &devicefocusin, 1);
	BASIC_ENDCALL(client2, Success);
/* Map window. */
	XSync(client1, True);
	XSync(client2, True);
	XMapWindow(client1, w);
	XSetDeviceFocus (client1, focusdevice, w, RevertToPointerRoot, CurrentTime);
/* XSync(.., False) on both clients to ensure generated events have come in. */
	XSync(client1, False);
	XSync(client2, False);
	trace("Mapping window with DeviceFocusIn selected, expecting DeviceFocusIn.");
/* Verify that client1 received no events. */
	if ((n=XPending(client1)) > 0) {
		XNextEvent(client1, &event);
		report("%d unexpected event%s (first %s) %s delivered to client1.",
			n, (n==1)?"":"s", eventname(event.type), (n==1)?"was":"were");
		FAIL;
	}
	else
		CHECK;
/* Verify that client2 received a single DeviceFocusIn event for this window. */
	if ((n=XPending(client2)) > 0) {
		XNextEvent(client2, &event);
		if (event.type != type)
		    {
		    report("%d unexpected event%s (first %s) %s delivered to client1.",
			n, (n==1)?"":"s", eventname(event.type), (n==1)?"was":"were");
		    FAIL;
		    }
		else
		    CHECK;
	}
	else
		CHECK;
/* Verify that client2 received no other events. */
	if ((n=XPending(client2)) > 0) {
		XNextEvent(client2, &event);
		report("%d unexpected event%s (first %s) %s delivered to client2.",
			n, (n==1)?"":"s", eventname(event.type), (n==1)?"was":"were");
		FAIL;
	}
	else
		CHECK;

        XSync(client1, 0);
        XSync(client2, 0);
	CHECKPASS(5);
	tpcleanup();
	pfcount(pass, fail);
}

static void t002(){

int type, this_client_count, all_clients_count;
XEventClass *this_client, *all_clients;
XEventClass noextensionevent, devicefocusin;
int 	pass = 0, fail = 0;

 	report_purpose(2);

	report_assertion("Assertion XSelectExtensionEvent-2.(B)");
	report_assertion("A call to XSelectExtensionEvent overrides the event mask");
	report_assertion("attribute set during any previous call to");
	report_assertion("XSelectExtensionEvent.");
	report_assertion("Reason for omission: There is no known reliable test method for this assertion");

	report_strategy("Create window with no events selected.");
	report_strategy("Call XGetSelectedExtensionEvents to get event mask for this window.");
	report_strategy("Verify event mask is as expected.");
	report_strategy("Call XSelectExtensionEvent to change event mask to DeviceFocusIn.");
	report_strategy("Call XGetSelectedExtensionEvents to get new event mask for this window.");
	report_strategy("Verify event mask changed as expected.");
	report_strategy("Call XSelectExtensionEvent to change event mask to NoEventMask.");
	report_strategy("Call XGetSelectedExtensionEvents to get event mask for this window.");
	report_strategy("Verify event mask is as expected.");
	report_strategy("Call XSelectExtensionEvent to change event mask to ALLEVENTS.");
	report_strategy("Call XGetSelectedExtensionEvents to get new event mask for this window.");
	report_strategy("Verify event mask changed as expected.");
	report_strategy("Call XSelectExtensionEvent to change event mask to StructureNotifyMask.");
	report_strategy("Call XGetSelectedExtensionEvents to get new event mask for this window.");
	report_strategy("Verify event mask changed as expected.");

	tpstartup();
	setargs();

/* Create window with no events selected. */
	getfocusdevice();
	client1 = NULL;
	if (!focusdevice) {
	    report("%s: Required input devices not present\n",TestName);
	    UNTESTED;
	    return;
	    }
	DeviceFocusIn (focusdevice, type, devicefocusin);
	NoExtensionEvent (focusdevice, type, noextensionevent);
	w = mkwin(display, (XVisualInfo *) NULL, (struct area *) NULL, False);
/* Call XGetSelectedExtensionEvents to get event mask for this window. */
	if (XGetSelectedExtensionEvents(display, w, &this_client_count,
		&this_client, &all_clients_count, &all_clients)) {
		delete("A call to XGetSelectedExtensionEvents failed.");
		return;
	}
	else
		CHECK;
/* Verify event mask is as expected. */
	if (this_client_count != 0) {
		delete("Unexpected event mask value.");
		return;
	}
	else
		CHECK;
/* Call XSelectExtensionEvent to change event mask to DeviceFocusIn. */
	eclass = &devicefocusin;
	ecount = 1;
	startcall(display);
	if (isdeleted())
		return;
	XSelectExtensionEvent(display, w, eclass, ecount);
	endcall(display);
	if (geterr() != Success) {
		report("Got %s, Expecting Success", errorname(geterr()));
		FAIL;
	}
/* Call XGetSelectedExtensionEvents to get new event mask for this window. */
	if (XGetSelectedExtensionEvents(display, w, &this_client_count,
		&this_client, &all_clients_count, &all_clients)) {
		delete("A call to XGetSelectedExtensionEvents failed.");
		return;
	}
	else
		CHECK;
/* Verify event mask changed as expected. */
	if (*this_client != devicefocusin) {
		report("Event mask incorrect first call.");
		FAIL;
	}
	else
		CHECK;
/* Call XChangeWindowAttributes to change event mask to NoEventMask. */
	eclass = &noextensionevent;
	ecount = 1;
	startcall(display);
	if (isdeleted())
		return;
	XSelectExtensionEvent(display, w, eclass, ecount);
	endcall(display);
	if (geterr() != Success) {
		report("Got %s, Expecting Success", errorname(geterr()));
		FAIL;
	}
/* Call XGetSelectedExtensionEvents to get event mask for this window. */
	if (XGetSelectedExtensionEvents(display, w, &this_client_count,
		&this_client, &all_clients_count, &all_clients)) {
		delete("A call to XGetSelectedExtensionEvents failed.");
		return;
	}
	else
		CHECK;
/* Verify event mask is as expected. */
	if (this_client_count != 0) {
		delete("Unexpected (non-empty) event mask value after XSelectExtensionEvent.");
		return;
	}
	else
		CHECK;
/* Call XSelectExtensionEvent to change event mask to ALLEVENTS. */
	eclass = &devicefocusin;
	ecount = 1;
	startcall(display);
	if (isdeleted())
		return;
	XSelectExtensionEvent(display, w, eclass, ecount);
	endcall(display);
	if (geterr() != Success) {
		report("Got %s, Expecting Success", errorname(geterr()));
		FAIL;
	}
/* Call XGetSelectedExtensionEvents to get new event mask for this window. */
	if (XGetSelectedExtensionEvents(display, w, &this_client_count,
		&this_client, &all_clients_count, &all_clients)) {
		delete("A call to XGetSelectedExtensionEvents failed.");
		return;
	}
	else
		CHECK;
/* Verify event mask changed as expected. */
	if (*this_client != devicefocusin) {
		report("Event mask incorrect after second call.");
		FAIL;
	}
	else
		CHECK;
/* Call XSelectExtensionEvent to change event mask to StructureNotifyMask. */
	eclass = &devicefocusin;
	ecount = 1;
	startcall(display);
	if (isdeleted())
		return;
	XSelectExtensionEvent(display, w, eclass, ecount);
	endcall(display);
	if (geterr() != Success) {
		report("Got %s, Expecting Success", errorname(geterr()));
		FAIL;
	}
/* Call XGetSelectedExtensionEvents to get new event mask for this window. */
	if (XGetSelectedExtensionEvents(display, w, &this_client_count,
		&this_client, &all_clients_count, &all_clients)) {
		delete("A call to XGetSelectedExtensionEvents failed.");
		return;
	}
	else
		CHECK;
/* Verify event mask changed as expected. */
	if (*this_client != devicefocusin) {
		report("Event mask incorrect after third call.");
		FAIL;
	}
	else
		CHECK;

	CHECKPASS(10);

	tpcleanup();
	pfcount(pass, fail);
}

static void t003(){

Display *client2;
int tcount, acount;
XEventClass *this_client, *all_clients, dkpclass, nevclass, classes[5];
int dkp, tmp;
int 	pass = 0, fail = 0;

 	report_purpose(3);

	report_assertion("Assertion XSelectExtensionEvent-3.(B)");
	report_assertion("A call to XSelectExtensionEvent does not change the events");
	report_assertion("selected for other clients.");
	report_assertion("Reason for omission: There is no known reliable test method for this assertion");

	report_strategy("Create client1.");
	report_strategy("Create window with client1.");
	report_strategy("Select NoExtensionEvent events with client1 on this window.");
	report_strategy("Call XGetSelectedEvents to get selected events for client1 for window.");
	report_strategy("Verify events are as expected.");
	report_strategy("Create client2.");
	report_strategy("Select all events with client2 on this window.");
	report_strategy("Call XGetSelectedExtensionEvents to get selected events for client2 for window.");
	report_strategy("Verify events are as expected.");
	report_strategy("Call XGetSelectedEvents to get selected events for client1 for window.");
	report_strategy("Verify selected events have not changed.");
	report_strategy("Select DeviceKeyPress events with client1 on this window.");
	report_strategy("Call XGetSelectedEvents to get selected events for client1 for window.");
	report_strategy("Verify events are as expected.");
	report_strategy("Call XGetSelectedEents to get selected events for client2 for window.");
	report_strategy("Verify selected events have not changed.");

	tpstartup();
	setargs();
	if (!Setup_Extension_DeviceInfo(KeyMask))
	    {
	    untested("%s: No input extension key device.\n", TestName);
	    return;
	    }
	DeviceKeyPress(Devs.Key, dkp, dkpclass);
	NoExtensionEvent(Devs.Key, unused, nevclass);
	classes[0] = dkpclass;
	DeviceKeyRelease(Devs.Key, tmp, classes[1]);
	DeviceStateNotify(Devs.Key, tmp, classes[2]);
	DeviceMappingNotify(Devs.Key, tmp, classes[3]);
	ChangeDeviceNotify(Devs.Key, tmp, classes[4]);
/* Create client1. */
	client1 = opendisplay();
	if (client1 == (Display *) NULL) {
		delete("Can not open display");
		return;
	}
	else
		CHECK;
/* Create window with client1. */
	w = mkwin(client1, (XVisualInfo *) NULL, (struct area *) NULL, False);
/* Select NoEventMask events with client1 on this window. */
	BASIC_STARTCALL(client1);
	XSelectExtensionEvent(client1, w, &nevclass, 1);
	BASIC_ENDCALL(client1, Success);
/* Call XGetSelectedExtensionEvents to get event mask for client1 for window. */
	if (XGetSelectedExtensionEvents(client1, w, &tcount, &this_client, 
	    &acount, &all_clients)) {
		delete("A call to XGetSelectedExtensionEvents failed.");
		return;
	}
	else
		CHECK;
/* Verify event mask is as expected. */
	if (tcount != 0) {
		delete("Unexpected events, count %d instead of 0.", tcount);
		return;
	}
	else
		CHECK;
/* Create client2. */
	client2 = opendisplay();
	if (client2 == (Display *) NULL) {
		delete("Can not open display");
		return;
	}
	else
		CHECK;
/* Select all events with client2 on this window. */
	BASIC_STARTCALL(client2);
	XSelectExtensionEvent(client2, w, classes, 5);
	BASIC_ENDCALL(client2, Success);
/* Call XGetSelectedExtensionEvents to get event mask for client2 for window. */
	if (XGetSelectedExtensionEvents(client2, w, &tcount, &this_client, 
	    &acount, &all_clients)) {
		delete("A call to XGetSelectedExtensionEvents failed.");
		return;
	}
	else
		CHECK;
/* Verify selected events are as expected. */
	if (tcount != 5) {
		delete("Unexpected events selected.");
		return;
	}
	else
		CHECK;
/* Call XGetSelectedExtensionEvents to get event mask for client1 for window. */
	if (XGetSelectedExtensionEvents(client1, w, &tcount, &this_client, 
	    &acount, &all_clients)) {
		delete("A call to XGetSelectedExtensionEvents failed.");
		return;
	}
	else
		CHECK;
/* Verify selected events have not changed. */
	if (tcount != 0) {
		delete("Unexpected events selected.");
		return;
	}
	else
		CHECK;
/* Select DeviceKeyPress events with client1 on this window. */
	BASIC_STARTCALL(client1);
	XSelectExtensionEvent(client1, w, &dkpclass, 1);
	BASIC_ENDCALL(client1, Success);
/* Call XGetSelectedExtensionEvents to get event mask for client1 for window. */
	if (XGetSelectedExtensionEvents(client1, w, &tcount, &this_client, 
	    &acount, &all_clients)) {
		delete("A call to XGetSelectedExtensionEvents failed.");
		return;
	}
	else
		CHECK;
/* Verify selected events have not changed. */
	if (tcount != 1) {
		delete("Unexpected events selected.");
		return;
	}
	else
		CHECK;
/* Call XGetSelectedExtensionEvents to get event mask for client2 for window. */
	if (XGetSelectedExtensionEvents(client2, w, &tcount, &this_client, 
	    &acount, &all_clients)) {
		delete("A call to XGetSelectedExtensionEvents failed.");
		return;
	}
	else
		CHECK;
/* Verify selected events have not changed. */
	if (tcount != 5) {
		delete("Unexpected events selected.");
		return;
	}
	else
		CHECK;

	CHECKPASS(12);
	tpcleanup();
	pfcount(pass, fail);
}

static void t004(){

Display *client2;
XEvent	event;
int	n;
XEventClass dkpclass;
int dkp;
int 	pass = 0, fail = 0;

 	report_purpose(4);

	report_assertion("Assertion XSelectExtensionEvent-4.(A)");
	report_assertion("When multiple clients make a call to XSelectExtensionEvent");
	report_assertion("requesting the same event on the same window and that window");
	report_assertion("is the event window for the requested event, then the event");
	report_assertion("is reported to each client.");

	report_strategy("Create client1.");
	report_strategy("Create window with client1.");
	report_strategy("Select DeviceKeyPress events with client1 on this window.");
	report_strategy("Create client2.");
	report_strategy("Select DeviceKeyPress events with client2 on this window.");
	report_strategy("Map window.");
	report_strategy("XSync(.., False) on both clients to ensure generated events have come in.");
	report_strategy("Verify that client1 received a single DeviceKeyPress event for this window.");
	report_strategy("Verify that client1 received no other events.");
	report_strategy("Verify that client2 received a single DeviceKeyPress event for this window.");
	report_strategy("Verify that client2 received no other events.");

	tpstartup();
	setargs();
	if (!Setup_Extension_DeviceInfo(KeyMask))
	    {
	    untested("%s: No input extension key device.\n", TestName);
	    return;
	    }
	if (noext(1))
	    return;
	DeviceKeyPress(Devs.Key, dkp, dkpclass);
/* Create client1. */
	client1 = opendisplay();
	if (client1 == (Display *) NULL) {
		delete("Can not open display");
		return;
	}
	else
		CHECK;
/* Create window with client1. */
	w = mkwin(client1, (XVisualInfo *) NULL, (struct area *) NULL, False);
/* Select DeviceKeyPress events with client1 on this window. */
	BASIC_STARTCALL(client1);
	XSelectExtensionEvent(client1, w, &dkpclass, 1);
	BASIC_ENDCALL(client1, Success);
/* Create client2. */
	client2 = opendisplay();
	if (client2 == (Display *) NULL) {
		delete("Can not open display");
		return;
	}
	else
		CHECK;
/* Select DeviceKeyPress events with client2 on this window. */
	BASIC_STARTCALL(client2);
	XSelectExtensionEvent(client2, w, &dkpclass, 1);
	BASIC_ENDCALL(client2, Success);
/* Map window. */
	XSync(client1, True);
	XSync(client2, True);
	XMapWindow(client1, w);
	warppointer(display, w, 1, 1);
	devicekeypress(client1, Devs.Key, MinKeyCode);
/* XSync(.., False) on both clients to ensure generated events have come in. */
	XSync(client1, False);
	XSync(client2, False);
	trace("Mapping window with DeviceKeyPress selected, expecting DeviceKeyPress.");
/* Verify that client1 received a single DeviceKeyPress event for this window.*/
	if (!XCheckTypedWindowEvent(client1, w, dkp, &event)) {
		report("Selected event was not delivered to client1.");
		FAIL;
	}
	else
		CHECK;
/* Verify that client1 received no other events. */
	if ((n=XPending(client1)) > 0) {
		XNextEvent(client1, &event);
		report("%d unexpected event%s (first %s) %s delivered to client1.",
			n, (n==1)?"":"s", eventname(event.type), (n==1)?"was":"were");
		FAIL;
	}
	else
		CHECK;
/* Verify that client1 received a single DeviceKeyPress event for this window.*/
	if (!XCheckTypedWindowEvent(client2, w, dkp, &event)) {
		report("Selected event was not delivered to client2.");
		FAIL;
	}
	else
		CHECK;
/* Verify that client2 received no other events. */
	if ((n=XPending(client2)) > 0) {
		XNextEvent(client2, &event);
		report("%d unexpected event%s (first %s) %s delivered to client2.",
			n, (n==1)?"":"s", eventname(event.type), (n==1)?"was":"were");
		FAIL;
	}
	else
		CHECK;

	devicerelkeys(Devs.Key);
	CHECKPASS(6);

	tpcleanup();
	pfcount(pass, fail);
}

static void t005(){

int dbp, dmn, dbr;
XEventClass class[3];
Window w;
int axes[2];
XEvent event;
int 	pass = 0, fail = 0;

 	report_purpose(5);

	report_assertion("Assertion XSelectExtensionEvent-5.(B)");
	report_assertion("If the DeviceButtonPress event class is specified,");
	report_assertion("automatic passive grabs are not done for the requesting");
	report_assertion("client.");
	report_assertion("Reason for omission: There is no known reliable test method for this assertion");


	tpstartup();
	setargs();
	if (noext(1))
	    return;
	if (!Setup_Extension_DeviceInfo(BtnMask))
	    {
	    untested("%s: No input extension button device.\n", TestName);
	    return;
	    }
	DeviceButtonPress(Devs.Button, dbp, class[0]);
	DeviceMotionNotify(Devs.Button, dmn, class[1]);
	DeviceButtonRelease(Devs.Button, dbr, class[2]);
	w = defwin(display);
	XSelectExtensionEvent(display, w, class, 3);
	warppointer(display, w, 1, 1);
	XSync(display, 0);
	devicebuttonpress(display, Devs.Button, Button1);
	axes[0]=1;
	SimulateDeviceMotionEvent(display, Devs.Button, False, 1, axes, 0);
	devicebuttonrel(display, Devs.Button, Button1);
	XSync(display, 0);
/* Verify that client1 received a DeviceButtonPress event for this window.*/
	if (!XCheckTypedWindowEvent(display, w, dbp, &event)) {
		report("1: Button press event was not delivered to client.");
		FAIL;
	}
	else
		CHECK;
/* Verify that client1 received a DeviceMotion event for this window.*/
	if (!XCheckTypedWindowEvent(display, w, dmn, &event)) {
		report("2: Motion event was not delivered to client.");
		FAIL;
	}
	else
		CHECK;
/* Verify that client1 received a DeviceButtonRelease event for this window.*/
	if (!XCheckTypedWindowEvent(display, w, dbr, &event)) {
		report("3: Button release event was not delivered to client.");
		FAIL;
	}
	else
		CHECK;

/* Now press the button and warp the pointer out of the window.*/
/* If no automatic grab is done, this client won't receive the other events.*/
	devicebuttonpress(display, Devs.Button, Button1);
	warppointer(display, w, 200, 200);
	SimulateDeviceMotionEvent(display, Devs.Button, False, 1, axes, 0);
	devicebuttonrel(display, Devs.Button, Button1);
	XSync(display, 0);
	if (!XCheckTypedWindowEvent(display, w, dbp, &event)) {
		report("4: Button press event was not delivered to client.");
		FAIL;
	}
	else
		CHECK;
	if (XCheckTypedWindowEvent(display, w, dmn, &event)) {
		report("5: Motion event was incorrectly delivered to client.");
		FAIL;
	}
	else
		CHECK;
	if (XCheckTypedWindowEvent(display, w, dbr, &event)) {
		report("6: Button release event was incorrectly delivered to client.");
		FAIL;
	}
	else
		CHECK;
	devicerelbuttons(Devs.Button);
	axes[0]=0;
	SimulateDeviceMotionEvent(display, Devs.Button, False, 1, axes, 0);
	XSync(display,1);
	CHECKPASS(6);

	tpcleanup();
	pfcount(pass, fail);
}

static void t006(){

int dbp, dmn, dbr;
XEventClass class[4];
Window w;
int axes[2];
XEvent event;
int 	pass = 0, fail = 0;

 	report_purpose(6);

	report_assertion("Assertion XSelectExtensionEvent-6.(B)");
	report_assertion("If the DeviceButtonPressGrab event class is specified in");
	report_assertion("addition to the DeviceButtonPress event class, automatic");
	report_assertion("passive grabs are done for the requesting client.");
	report_assertion("Reason for omission: There is no known reliable test method for this assertion");


	tpstartup();
	setargs();
	if (noext(1))
	    return;
	if (!Setup_Extension_DeviceInfo(BtnMask))
	    {
	    untested("%s: No input extension button device.\n", TestName);
	    return;
	    }
	DeviceButtonPress(Devs.Button, dbp, class[0]);
	DeviceMotionNotify(Devs.Button, dmn, class[1]);
	DeviceButtonRelease(Devs.Button, dbr, class[2]);
	DeviceButtonPressGrab(Devs.Button, unused, class[3]);
	w = defwin(display);
	XSelectExtensionEvent(display, w, class, 4);
	warppointer(display, w, 1, 1);
	devicebuttonpress(display, Devs.Button, Button1);
	axes[0]=1;
	SimulateDeviceMotionEvent(display, Devs.Button, False, 1, axes, 0);
	devicebuttonrel(display, Devs.Button, Button1);
	XSync(display, 0);
/* Verify that client1 received a DeviceButtonPress event for this window.*/
	if (!XCheckTypedWindowEvent(display, w, dbp, &event)) {
		report("Button event was not delivered to client.");
		FAIL;
	}
	else
		CHECK;
/* Verify that client1 received a DeviceMotion event for this window.*/
	if (!XCheckTypedWindowEvent(display, w, dmn, &event)) {
		report("Motion event was not delivered to client.");
		FAIL;
	}
	else
		CHECK;
/* Verify that client1 received a DeviceButtonRelease event for this window.*/
	if (!XCheckTypedWindowEvent(display, w, dbr, &event)) {
		report("Button event was not delivered to client.");
		FAIL;
	}
	else
		CHECK;

/* Now press the button and warp the pointer out of the window.*/
/* If no automatic grab is done, this client won't receive the other events.*/
	devicebuttonpress(display, Devs.Button, Button1);
	warppointer(display, w, 200, 200);
	SimulateDeviceMotionEvent(display, Devs.Button, False, 1, axes, 0);
	devicebuttonrel(display, Devs.Button, Button1);
	XSync(display, 0);
	if (!XCheckTypedWindowEvent(display, w, dbp, &event)) {
		report("Button event was not delivered to client.");
		FAIL;
	}
	else
		CHECK;
	if (!XCheckTypedWindowEvent(display, w, dmn, &event)) {
		report("Motion event was not delivered to client.");
		FAIL;
	}
	else
		CHECK;
	if (!XCheckTypedWindowEvent(display, w, dbr, &event)) {
		report("Button event was not delivered to client.");
		FAIL;
	}
	else
		CHECK;
	devicerelbuttons(Devs.Button);
	axes[0]=0;
	SimulateDeviceMotionEvent(display, Devs.Button, False, 1, axes, 0);
	XSync(display,1);
	CHECKPASS(6);

	tpcleanup();
	pfcount(pass, fail);
}

static void t007(){

int dbp, dmn, dbr;
XEventClass class[4];
Window w;
int axes[2];
XEvent event;
int 	pass = 0, fail = 0;

 	report_purpose(7);

	report_assertion("Assertion XSelectExtensionEvent-7.(B)");
	report_assertion("If the DeviceButtonPressGrab event class is specified in");
	report_assertion("addition to the DeviceButtonPress event class, automatic");
	report_assertion("passive grabs activate with owner_events equal to False.");
	report_assertion("Reason for omission: There is no known reliable test method for this assertion");


	tpstartup();
	setargs();
	if (noext(1))
	    return;
	if (!Setup_Extension_DeviceInfo(BtnMask))
	    {
	    untested("%s: No input extension button device.\n", TestName);
	    return;
	    }
	DeviceButtonPress(Devs.Button, dbp, class[0]);
	DeviceMotionNotify(Devs.Button, dmn, class[1]);
	DeviceButtonRelease(Devs.Button, dbr, class[2]);
	DeviceButtonPressGrab(Devs.Button, unused, class[3]);
	w = defwin(display);
	XSelectExtensionEvent(display, w, class, 4);
	XSync(display,0);

/* Press the button and warp the pointer out of the window.*/
/* If no automatic grab is done, this client won't receive the other events.*/
	warppointer(display, w, 1, 1);
	devicebuttonpress(display, Devs.Button, Button1);
	XSync(display,0);
	sleep(5);
	warppointer(display, w, 200, 200);
	axes[0]=1;
	SimulateDeviceMotionEvent(display, Devs.Button, False, 1, axes, 0);
	devicebuttonrel(display, Devs.Button, Button1);
	XSync(display, 0);
	if (!XCheckTypedWindowEvent(display, w, dbp, &event)) {
		report("Button event was not delivered to client.");
		FAIL;
	}
	else
		{
		XDeviceButtonEvent *bev = (XDeviceButtonEvent *) &event;
		if (bev->window == w)
		    CHECK;
		else
		    {
		    report("Button event reported with wrong window.");
		    FAIL;
		    }
		}
	if (!XCheckTypedWindowEvent(display, w, dmn, &event)) {
		report("Motion event was not delivered to client.");
		FAIL;
	}
	else
		{
		XDeviceMotionEvent *mev = (XDeviceMotionEvent *) &event;
		if (mev->window == w)
		    CHECK;
		else
		    {
		    report("Motion event reported with wrong window.");
		    FAIL;
		    }
		}
	if (!XCheckTypedWindowEvent(display, w, dbr, &event)) {
		report("Button event was not delivered to client.");
		FAIL;
	}
	else
		{
		XDeviceButtonEvent *bev = (XDeviceButtonEvent *) &event;
		if (bev->window == w)
		    CHECK;
		else
		    {
		    report("Button event reported with wrong window.");
		    FAIL;
		    }
		}
	devicerelbuttons(Devs.Button);
	axes[0]=0;
	SimulateDeviceMotionEvent(display, Devs.Button, False, 1, axes, 0);
	XSync(display,1);
	CHECKPASS(3);

	tpcleanup();
	pfcount(pass, fail);
}

static void t008(){

int dbp, dmn, dbr;
XEventClass class[5];
Window w;
int axes[2];
XEvent event;
int 	pass = 0, fail = 0;

 	report_purpose(8);

	report_assertion("Assertion XSelectExtensionEvent-8.(B)");
	report_assertion("If the DeviceOwnerGrabButton event class is specified in");
	report_assertion("addition to the DeviceButtonPress and DeviceButtonPressGrab");
	report_assertion("event classes, automatic passive grabs activate with");
	report_assertion("owner_events equal to True. requesting client.");
	report_assertion("Reason for omission: There is no known reliable test method for this assertion");


	tpstartup();
	setargs();
	if (noext(1))
	    return;
	if (!Setup_Extension_DeviceInfo(BtnMask))
	    {
	    untested("%s: No input extension button device.\n", TestName);
	    return;
	    }
	DeviceButtonPress(Devs.Button, dbp, class[0]);
	DeviceMotionNotify(Devs.Button, dmn, class[1]);
	DeviceButtonRelease(Devs.Button, dbr, class[2]);
	DeviceButtonPressGrab(Devs.Button, unused, class[3]);
	DeviceOwnerGrabButton(Devs.Button, unused, class[4]);
	w = defwin(display);
	XSelectExtensionEvent(display, w, class, 5);

/* Press the button and warp the pointer out of the window.*/
/* If owner_events is True, these events will still be reported to the 
   grab window, because they would not otherwise be reported. (No select
   has been done on the root window. */
	warppointer(display, w, 1, 1);
	devicebuttonpress(display, Devs.Button, Button1);
	warppointer(display, w, 200, 200);
	axes[0]=1;
	SimulateDeviceMotionEvent(display, Devs.Button, False, 1, axes, 0);
	devicebuttonrel(display, Devs.Button, Button1);
	XSync(display, 0);
	if (!XCheckTypedWindowEvent(display, w, dbp, &event)) {
		report("Button event was not delivered to client.");
		FAIL;
	}
	else
	    CHECK;
	if (!XCheckTypedWindowEvent(display, w, dmn, &event)) {
		report("Motion event was not delivered to client.");
		FAIL;
	}
	else
	    CHECK;
	if (!XCheckTypedWindowEvent(display, w, dbr, &event)) {
		report("Button event was not delivered to client.");
		FAIL;
	}
	else
	    CHECK;

/* Now select the events on the root window and repeat the process */
/* Events should now be reported with respect to the root window.  */
	XSelectExtensionEvent(display, RootWindow(display,0), class, 5);
	warppointer(display, w, 1, 1);
	devicebuttonpress(display, Devs.Button, Button1);
	warppointer(display, w, 200, 200);
	SimulateDeviceMotionEvent(display, Devs.Button, False, 1, axes, 0);
	devicebuttonrel(display, Devs.Button, Button1);
	XSync(display, 0);
	if (!XCheckTypedWindowEvent(display, w, dbp, &event)) {
		report("Button event was incorrectly delivered to client.");
		FAIL;
	}
	else
	    CHECK;
	if (XCheckTypedWindowEvent(display, w, dmn, &event)) {
		report("Motion event was incorrectly delivered to client.");
		FAIL;
	}
	else
	    CHECK;
	if (XCheckTypedWindowEvent(display, w, dbr, &event)) {
		report("Button event was incorrectly delivered to client.");
		FAIL;
	}
	else
	    CHECK;

	if (XCheckTypedWindowEvent(display, RootWindow(display,0), dbp, &event)) {
		report("Button event was incorrectly delivered to client.");
		FAIL;
	}
	else
	    CHECK;
	if (!XCheckTypedWindowEvent(display, RootWindow(display,0), dmn, &event)) {
		report("Motion event was not delivered to client.");
		FAIL;
	}
	else
	    CHECK;
	if (!XCheckTypedWindowEvent(display, RootWindow(display,0), dbr, &event)) {
		report("Button event was not delivered to client.");
		FAIL;
	}
	else
	    CHECK;

	devicerelbuttons(Devs.Button);
	axes[0]=0;
	SimulateDeviceMotionEvent(display, Devs.Button, False, 1, axes, 0);
	XSync(display,1);
	CHECKPASS(9);

	tpcleanup();
	pfcount(pass, fail);
}

static void t009(){

XEventClass dbmclasses[6];
int dbm1;
int axes[2];
XEvent event;
int 	pass = 0, fail = 0;

 	report_purpose(9);

	report_assertion("Assertion XSelectExtensionEvent-9.(B)");
	report_assertion("If a call to XSelectExtensionEvent is made with event");
	report_assertion("classes Button1Motion through Button5Motion, motion events");
	report_assertion("will be received from that device only when the specified");
	report_assertion("button is down.");
	report_assertion("Reason for omission: There is no known reliable test method for this assertion");


	tpstartup();
	setargs();

	if (noext(1))
	    return;
	if (!Setup_Extension_DeviceInfo(BtnMask))
	    {
	    untested("%s: No input extension button device.\n", TestName);
	    return;
	    }
	DeviceMotionNotify(Devs.Button, dbm1, dbmclasses[0]);
	DeviceButton1Motion(Devs.Button, dbm1, dbmclasses[0]);
	DeviceButton2Motion(Devs.Button, unused, dbmclasses[1]);
	DeviceButton3Motion(Devs.Button, unused, dbmclasses[2]);
	DeviceButton4Motion(Devs.Button, unused, dbmclasses[3]);
	DeviceButton5Motion(Devs.Button, unused, dbmclasses[4]);
	DeviceButtonMotion(Devs.Button, unused, dbmclasses[5]);
	w = defwin(display);
	XSetDeviceFocus(display, Devs.Button, w, RevertToPointerRoot, 
	    CurrentTime);
	XSync(display,0);
	eclass = dbmclasses;
	ecount = 1;
	startcall(display);
	if (isdeleted())
		return;
	XSelectExtensionEvent(display, w, eclass, ecount);
	endcall(display);
	if (geterr() != Success) {
		report("Got %s, Expecting Success", errorname(geterr()));
		FAIL;
	}
	axes[0]=1;
	SimulateDeviceMotionEvent(display, Devs.Button, False, 1, axes, 0);
	XSync(display,0);
	if (XCheckTypedWindowEvent(display, w, dbm1, &event)) {
		report("Event was incorrectly delivered to client.");
		FAIL;
	}
	else
	    CHECK;
	devicebuttonpress(display, Devs.Button, Button1);
	SimulateDeviceMotionEvent(display, Devs.Button, False, 1, axes, 0);
	XSync(display,0);
	if (!XCheckTypedWindowEvent(display, w, dbm1, &event)) {
		report("Event was not delivered to client.");
		FAIL;
	}
	else
	    CHECK;

	if (NumButtons > 1) {
	    devicebuttonpress(display, Devs.Button, Button2);
	    SimulateDeviceMotionEvent(display, Devs.Button, False, 1, axes, 0);
	    XSync(display,0);
	    if (!XCheckTypedWindowEvent(display, w, dbm1, &event)) {
		report("Event was not delivered to client.");
		FAIL;
	    }
	    else
	        CHECK;
	}
	else {
	    report("%s can't be completely tested because the device doesn't have enough buttons\n",TestName);
	    UNTESTED;
	    return;
	    }

	devicebuttonrel(display, Devs.Button, Button1);
	SimulateDeviceMotionEvent(display, Devs.Button, False, 1, axes, 0);
	XSync(display,0);
	if (XCheckTypedWindowEvent(display, w, dbm1, &event)) {
		report("Event was incorrectly delivered to client.");
		FAIL;
	}
	else
	    CHECK;

	eclass = &dbmclasses[1];
	startcall(display);
	if (isdeleted())
		return;
	XSelectExtensionEvent(display, w, eclass, ecount);
	endcall(display);
	if (geterr() != Success) {
		report("Got %s, Expecting Success", errorname(geterr()));
		FAIL;
	}
	SimulateDeviceMotionEvent(display, Devs.Button, False, 1, axes, 0);
	XSync(display,0);
	if (!XCheckTypedWindowEvent(display, w, dbm1, &event)) {
		report("Event was not delivered to client.");
		FAIL;
	}
	else
	    CHECK;

	if (NumButtons > 2) {
	    devicebuttonpress(display, Devs.Button, Button3);
	    SimulateDeviceMotionEvent(display, Devs.Button, False, 1, axes, 0);
	    XSync(display,0);
	    if (!XCheckTypedWindowEvent(display, w, dbm1, &event)) {
		report("Event was not delivered to client.");
		FAIL;
	    }
	    else
	        CHECK;
	}
	else {
	    report("%s can't be completely tested because the device doesn't have enough buttons\n",TestName);
	    UNTESTED;
	    return;
	    }

	devicebuttonrel(display, Devs.Button, Button2);
	SimulateDeviceMotionEvent(display, Devs.Button, False, 1, axes, 0);
	XSync(display,0);
	if (XCheckTypedWindowEvent(display, w, dbm1, &event)) {
		report("Event was incorrectly delivered to client.");
		FAIL;
	}
	else
	    CHECK;

	eclass = &dbmclasses[2];
	startcall(display);
	if (isdeleted())
		return;
	XSelectExtensionEvent(display, w, eclass, ecount);
	endcall(display);
	if (geterr() != Success) {
		report("Got %s, Expecting Success", errorname(geterr()));
		FAIL;
	}
	SimulateDeviceMotionEvent(display, Devs.Button, False, 1, axes, 0);
	XSync(display,0);
	if (!XCheckTypedWindowEvent(display, w, dbm1, &event)) {
		report("Event was not delivered to client.");
		FAIL;
	}
	else
	    CHECK;


	if (NumButtons > 3) {
	    devicebuttonpress(display, Devs.Button, Button4);
	    SimulateDeviceMotionEvent(display, Devs.Button, False, 1, axes, 0);
	    XSync(display,0);
	    if (!XCheckTypedWindowEvent(display, w, dbm1, &event)) {
		report("Event was not delivered to client.");
		FAIL;
	    }
	    else
	        CHECK;
	}
	else {
	    report("%s can't be completely tested because the device doesn't have enough buttons\n",TestName);
	    UNTESTED;
	    return;
	    }

	devicebuttonrel(display, Devs.Button, Button3);
	SimulateDeviceMotionEvent(display, Devs.Button, False, 1, axes, 0);
	XSync(display,0);
	if (XCheckTypedWindowEvent(display, w, dbm1, &event)) {
		report("Event was incorrectly delivered to client.");
		FAIL;
	}
	else
	    CHECK;

	eclass = &dbmclasses[3];
	startcall(display);
	if (isdeleted())
		return;
	XSelectExtensionEvent(display, w, eclass, ecount);
	endcall(display);
	if (geterr() != Success) {
		report("Got %s, Expecting Success", errorname(geterr()));
		FAIL;
	}
	SimulateDeviceMotionEvent(display, Devs.Button, False, 1, axes, 0);
	XSync(display,0);
	if (!XCheckTypedWindowEvent(display, w, dbm1, &event)) {
		report("Event was not delivered to client.");
		FAIL;
	}
	else
	    CHECK;

	if (NumButtons > 4) {
	    devicebuttonpress(display, Devs.Button, Button5);
	    SimulateDeviceMotionEvent(display, Devs.Button, False, 1, axes, 0);
	    XSync(display,0);
	    if (!XCheckTypedWindowEvent(display, w, dbm1, &event)) {
		report("Event was not delivered to client.");
		FAIL;
	    }
	    else
	        CHECK;
	}
	else {
	    report("%s can't be completely tested because the device doesn't have enough buttons\n",TestName);
	    UNTESTED;
	    return;
	    }

	devicebuttonrel(display, Devs.Button, Button4);
	SimulateDeviceMotionEvent(display, Devs.Button, False, 1, axes, 0);
	XSync(display,0);
	if (XCheckTypedWindowEvent(display, w, dbm1, &event)) {
		report("Event was incorrectly delivered to client.");
		FAIL;
	}
	else
	    CHECK;

	eclass = &dbmclasses[4];
	startcall(display);
	if (isdeleted())
		return;
	XSelectExtensionEvent(display, w, eclass, ecount);
	endcall(display);
	if (geterr() != Success) {
		report("Got %s, Expecting Success", errorname(geterr()));
		FAIL;
	}
	SimulateDeviceMotionEvent(display, Devs.Button, False, 1, axes, 0);
	XSync(display,0);
	if (!XCheckTypedWindowEvent(display, w, dbm1, &event)) {
		report("Event was not delivered to client.");
		FAIL;
	}
	else
	    CHECK;

	eclass = &dbmclasses[5];
	startcall(display);
	if (isdeleted())
		return;
	XSelectExtensionEvent(display, w, eclass, ecount);
	endcall(display);
	if (geterr() != Success) {
		report("Got %s, Expecting Success", errorname(geterr()));
		FAIL;
	}
	SimulateDeviceMotionEvent(display, Devs.Button, False, 1, axes, 0);
	XSync(display,0);
	if (!XCheckTypedWindowEvent(display, w, dbm1, &event)) {
		report("Event was not delivered to client.");
		FAIL;
	}
	else
	    CHECK;
	devicerelbuttons(Devs.Button);
	axes[0]=0;
	SimulateDeviceMotionEvent(display, Devs.Button, False, 1, axes, 0);
	XSync(display,1);
	CHECKPASS(15);

	tpcleanup();
	pfcount(pass, fail);
}

static void t010(){

XEventClass classes[3];
int dmn;
int i, axes[2];
XEvent event;
int 	pass = 0, fail = 0;

 	report_purpose(10);

	report_assertion("Assertion XSelectExtensionEvent-10.(B)");
	report_assertion("A call to XSelectExtensionEvent specifying");
	report_assertion("devicemotionnotify and devicemotionnotifyhint classes will");
	report_assertion("result in only one devicemotionnotify event being sent,");
	report_assertion("until a button is pressed on that device.");
	report_assertion("Reason for omission: There is no known reliable test method for this assertion");


	tpstartup();
	setargs();
	if (noext(1))
	    return;
	if (!Setup_Extension_DeviceInfo(BtnMask))
	    {
	    untested("%s: No input extension Button device.\n", TestName);
	    return;
	    }
	DeviceMotionNotify(Devs.Button, dmn, classes[0]);
	DevicePointerMotionHint(Devs.Button, unused, classes[1]);
	w = defwin(display);
	XSetDeviceFocus(display, Devs.Button, w, RevertToPointerRoot, 
	    CurrentTime);
	XSync(display,0);
	eclass = classes;
	ecount = 2;
	startcall(display);
	if (isdeleted())
		return;
	XSelectExtensionEvent(display, w, eclass, ecount);
	endcall(display);
	if (geterr() != Success) {
		report("Got %s, Expecting Success", errorname(geterr()));
		FAIL;
	}
	axes[0]=1;
	for(i=0; i<5; i++)
	    SimulateDeviceMotionEvent(display, Devs.Button, False, 1, axes, 0);
	XSync(display,0);
	if (XPending(display) > 1)
	    {
	    report("Too many events sent to client.");
	    FAIL;
	    }
	else
	    CHECK;
	if (!XCheckTypedWindowEvent(display, w, dmn, &event)) {
		report("Initial event was not delivered to client.");
		FAIL;
	}
	else
	    CHECK;
	for(i=0; i<5; i++)
	    SimulateDeviceMotionEvent(display, Devs.Button, False, 1, axes, 0);
	XSync(display,0);
	if (XCheckTypedWindowEvent(display, w, dmn, &event)) {
		report("Event was incorrectly delivered to client.");
		FAIL;
	}
	else
	    CHECK;
	devicebuttonpress(display, Devs.Button, Button1);
	for(i=0; i<5; i++)
	    SimulateDeviceMotionEvent(display, Devs.Button, False, 1, axes, 0);
	devicebuttonrel(display, Devs.Button, Button1);
	XSync(display,0);
	if (XPending(display) > 1)
	    {
	    report("Too many events sent to client.");
	    FAIL;
	    }
	else
	    CHECK;
	if (!XCheckTypedWindowEvent(display, w, dmn, &event)) {
		report("Event was not delivered to client after button.");
		FAIL;
	}
	else
	    CHECK;
	devicerelbuttons(Devs.Button);
	axes[0]=0;
	SimulateDeviceMotionEvent(display, Devs.Button, False, 1, axes, 0);
	XSync(display,1);
	CHECKPASS(5);

	tpcleanup();
	pfcount(pass, fail);
}

static void t011(){

int dfi;
XEventClass dfic;
int 	pass = 0, fail = 0;

 	report_purpose(11);

	report_assertion("Assertion XSelectExtensionEvent-11.(B)");
	report_assertion("A call to XSelectExtensionEvent specifying an invalid");
	report_assertion("window id results in a BadWindow error.");
	report_assertion("Reason for omission: There is no known reliable test method for this assertion");


	tpstartup();
	setargs();
	if (!Setup_Extension_DeviceInfo(BtnMask))
	    {
	    untested("%s: No input extension Button device.\n", TestName);
	    return;
	    }
	DeviceFocusIn(Devs.Button, dfi, dfic);
	w = 0;
	eclass = &dfic;
	ecount = 1;
	startcall(display);
	if (isdeleted())
		return;
	XSelectExtensionEvent(display, w, eclass, ecount);
	endcall(display);
	if (geterr() != BadWindow) {
		report("Got %s, Expecting BadWindow", errorname(geterr()));
		FAIL;
	}
	if (geterr()==BadWindow)
	    PASS;
	else
	    FAIL;

	tpcleanup();
	pfcount(pass, fail);
}

/* Including from file Mdefcode.tmc.2640 */
/* End of included file Mdefcode.tmc.2640 */

static void t012(){

Display *client1;
Display *client2;
XEventClass dbpgclass;
int 	pass = 0, fail = 0;

 	report_purpose(12);

	report_assertion("Assertion XSelectExtensionEvent-12.(B)");
	report_assertion("If XSelectExtensionEvent is called with an eventclass of");
	report_assertion("DeviceButtonPressGrab specifying a window for which another");
	report_assertion("client has already selected DeviceButtonPressGrab, a");
	report_assertion("BadAccess error will result.");
	report_assertion("Reason for omission: There is no known reliable test method for this assertion");

	report_strategy("Call XSelectExtensionEvent with an eventclass of DeviceButtonPressGrab specifying a ");
	report_strategy("window for which another client has already specified DeviceButtonPressGrab.");

	tpstartup();
	setargs();
	if (!Setup_Extension_DeviceInfo(BtnMask))
	    {
	    untested("%s: No input extension button device.\n", TestName);
	    return;
	    }
/* Create client1. */
	client1 = opendisplay();
	if (client1 == (Display *) NULL) {
		delete("Can not open display");
		return;
	}
	else
		CHECK;
/* Create window with client1. */
	w = mkwin(client1, (XVisualInfo *) NULL, (struct area *) NULL, False);
/* Select DeviceButtonPressGrab class with client1 on this window. */
	DeviceButtonPressGrab(Devs.Button, unused, dbpgclass);
	BASIC_STARTCALL(client1);
	eclass = &dbpgclass;
	ecount = 1;
	XSelectExtensionEvent(client1, w, &dbpgclass, 1);
	BASIC_ENDCALL(client1, Success);
/* Create client2. */
	client2 = opendisplay();
	if (client2 == (Display *) NULL) {
		delete("Can not open display");
		return;
	}
	else
		CHECK;
/* Select DeviceButtonPressGrab class with client2 on this window. */
	BASIC_STARTCALL(client2);
	XSelectExtensionEvent(client2, w, &dbpgclass, 1);
	BASIC_ENDCALL(client2, BadAccess);
/* Verify that a BadAccess error was generated. */
	if (geterr() != BadAccess) {
		report("A call to XSelectExtensionEvent did not generate BadAccess error");
		FAIL;
	}
	else
		CHECK;

	CHECKPASS(3);
	tpcleanup();
	pfcount(pass, fail);
}

static void t013(){

int badclass;
XEventClass bogus;
int 	pass = 0, fail = 0;

 	report_purpose(13);

	report_assertion("Assertion XSelectExtensionEvent-13.(B)");
	report_assertion("A call to XSelectExtensionEvent specifying an invalid");
	report_assertion("eventclass results in a BadClass error.");
	report_assertion("Reason for omission: There is no known reliable test method for this assertion");

	report_strategy("Call XSelectExtensionEvent with an eventclass of DeviceButtonPressGrab specifying a ");
	report_strategy("window for which another client has already specified DeviceButtonPressGrab.");

	tpstartup();
	setargs();
	if (!Setup_Extension_DeviceInfo(BtnMask))
	    {
	    untested("%s: No input extension button device.\n", TestName);
	    return;
	    }
	BadClass(display, badclass);
	w = 0;
	bogus = -1;
	eclass = &bogus;
	ecount = 1;
	w = mkwin(display, (XVisualInfo *) NULL, (struct area *) NULL, False);
	startcall(display);
	if (isdeleted())
		return;
	XSelectExtensionEvent(display, w, eclass, ecount);
	endcall(display);
	if (geterr() != badclass) {
		report("Got %s, Expecting badclass", errorname(geterr()));
		FAIL;
	}
	if (geterr()==badclass)
	    PASS;
	else
	    FAIL;

	tpcleanup();
	pfcount(pass, fail);
}

/* Including from file Mdefcode.tmc.2640 */
/* End of included file Mdefcode.tmc.2640 */

/* End of Test Cases */


struct tet_testlist tet_testlist[] = {
	{ t001, 1 },
	{ t002, 2 },
	{ t003, 3 },
	{ t004, 4 },
	{ t005, 5 },
	{ t006, 6 },
	{ t007, 7 },
	{ t008, 8 },
	{ t009, 9 },
	{ t010, 10 },
	{ t011, 11 },
	{ t012, 12 },
	{ t013, 13 },
	{ NULL, 0 }
};

int 	ntests = sizeof(tet_testlist)/sizeof(struct tet_testlist)-1;

void	(*tet_startup)() = startup;
void	(*tet_cleanup)() = xi_cleanup;
