Ejemplo n.º 1
0
int Run(Display* pDisp, UI* pUI, FORMATTYPE fmt, const char* pszDeviceName)
{
	int nRtn;
	XDevice* pDev;
	XDeviceInfoPtr pDevInfo;
	int nEventListCnt = 0;
	XEventClass eventList[32];
	XEventClass cls;

	/* get the device by name */
	pDevInfo = GetDevice(pDisp,pszDeviceName);
	if (!pDevInfo)
	{
		fprintf(stderr,"Unable to find input device '%s'\n",pszDeviceName);
		return 1;
	}

	/* open device */
	pDev = XOpenDevice(pDisp,pDevInfo->id);
	if (!pDev)
	{
		fprintf(stderr,"Unable to open input device '%s'\n",pszDeviceName);
		return 1;
	}

	/* key events */
	DeviceKeyPress(pDev,gnInputEvent[INPUTEVENT_KEY_PRESS],cls);
	if (cls) eventList[nEventListCnt++] = cls;
	DeviceKeyRelease(pDev,gnInputEvent[INPUTEVENT_KEY_RELEASE],cls);
	if (cls) eventList[nEventListCnt++] = cls;

	/* focus events */
	DeviceFocusIn(pDev,gnInputEvent[INPUTEVENT_FOCUS_IN],cls);
	if (cls) eventList[nEventListCnt++] = cls;
	DeviceFocusOut(pDev,gnInputEvent[INPUTEVENT_FOCUS_OUT],cls);
	if (cls) eventList[nEventListCnt++] = cls;

	/* button events */
	DeviceButtonPress(pDev,gnInputEvent[INPUTEVENT_BTN_PRESS],cls);
	if (cls) eventList[nEventListCnt++] = cls;
	DeviceButtonRelease(pDev,gnInputEvent[INPUTEVENT_BTN_RELEASE],cls);
	if (cls) eventList[nEventListCnt++] = cls;

	/* proximity events */
	ProximityIn(pDev,gnInputEvent[INPUTEVENT_PROXIMITY_IN],cls);
	if (cls) eventList[nEventListCnt++] = cls;
	ProximityOut(pDev,gnInputEvent[INPUTEVENT_PROXIMITY_OUT],cls);
	if (cls) eventList[nEventListCnt++] = cls;

	/* motion events */
	DeviceMotionNotify(pDev,gnInputEvent[INPUTEVENT_MOTION_NOTIFY],cls);
	if (cls) eventList[nEventListCnt++] = cls;

	/* device state */
	DeviceStateNotify(pDev,gnInputEvent[INPUTEVENT_DEVICE_STATE_NOTIFY],cls);
	if (cls) eventList[nEventListCnt++] = cls;
	DeviceMappingNotify(pDev,
			gnInputEvent[INPUTEVENT_DEVICE_MAPPING_NOTIFY],cls);
	if (cls) eventList[nEventListCnt++] = cls;
	ChangeDeviceNotify(pDev,gnInputEvent[INPUTEVENT_CHANGE_DEVICE_NOTIFY],cls);
	if (cls) eventList[nEventListCnt++] = cls;

#if 0
	/* this cuts the motion data down - not sure if this is useful */
	DevicePointerMotionHint(pDev,
			gnInputEvent[INPUTEVENT_DEVICE_POINTER_MOTION_HINT],cls);
	if (cls) eventList[nEventListCnt++] = cls;
#endif

	/* button motion */
	DeviceButtonMotion(pDev,
			gnInputEvent[INPUTEVENT_DEVICE_BUTTON_MOTION],cls);
	if (cls) eventList[nEventListCnt++] = cls;
	DeviceButton1Motion(pDev,
			gnInputEvent[INPUTEVENT_DEVICE_BUTTON1_MOTION],cls);
	if (cls) eventList[nEventListCnt++] = cls;
	DeviceButton2Motion(pDev,
			gnInputEvent[INPUTEVENT_DEVICE_BUTTON2_MOTION],cls);
	if (cls) eventList[nEventListCnt++] = cls;
	DeviceButton3Motion(pDev,
			gnInputEvent[INPUTEVENT_DEVICE_BUTTON3_MOTION],cls);
	if (cls) eventList[nEventListCnt++] = cls;
	DeviceButton4Motion(pDev,
			gnInputEvent[INPUTEVENT_DEVICE_BUTTON4_MOTION],cls);
	if (cls) eventList[nEventListCnt++] = cls;
	DeviceButton5Motion(pDev,
			gnInputEvent[INPUTEVENT_DEVICE_BUTTON5_MOTION],cls);
	if (cls) eventList[nEventListCnt++] = cls;

	/* specify which events to report */
	/* XSelectInput(pDisp,wnd,0x00FFFFFF ^ PointerMotionHintMask); */
	/* XSelectExtensionEvent(pDisp,wnd,eventList,nEventListCnt); */

	/* grab device - work whether pointer is in active window or not */
	XGrabDevice(pDisp,pDev,DefaultRootWindow(pDisp),
			0, /* no owner events */
			nEventListCnt, eventList, /* events */
			GrabModeAsync, /* don't queue, give me whatever you got */
			GrabModeAsync, /* same */
			CurrentTime);

	/* fire up the UI */
	if ((nRtn=pUI->Init()) != 0)
		fprintf(stderr,"failed to initialize UI\n");
	else
	{
		if ((nRtn=pUI->Run(pDisp,pDevInfo,fmt)) != 0)
			fprintf(stderr,"failed to run UI\n");
		pUI->Term();
	}

	XUngrabDevice(pDisp,pDev,CurrentTime);
	XFree(pDev);
	XCloseDisplay(pDisp);

	return nRtn;
}
Ejemplo n.º 2
0
static PyObject * xextdev_grab(PyObject *self, PyObject *args)
{
	XEventClass cls;
	const char *devname;
	int devid;
	XAnyClassPtr pClass;
	int j;
	XEventClass eventList[32];
	int nEventListCnt = 0;

	if (!display) {
		ASSERTCOND(display = XOpenDisplay(NULL),"open display");

		/* polling this seems to be the only way 
		* 	 * to do a timed wait on X events */
		x11_fd = ConnectionNumber(display);

		xdevList = (XDeviceInfoPtr) XListInputDevices(display, &devcount);
		if (!xdevList) {
			fprintf(stderr,"Failed to get input device list\n");
			return Py_BuildValue("i", 0);
		}
	}

	if (!PyArg_ParseTuple(args, "i", &devid))
		return Py_BuildValue("i", 0);

	ASSERTCOND(devid<devcount,
		   "Tried to grab non-existing device id %d (max %d)",
		   devid,devcount);

	xdevInfo = xdevList + devid;
	devname = xdevInfo->name;

	val = NULL;

	pClass = xdevInfo->inputclassinfo;
	for (j=0; j<xdevInfo->num_classes; ++j)
	{
		switch (pClass->class) {
		case ValuatorClass:
			val = (XValuatorInfoPtr)pClass;
			break;
		}
		pClass = (XAnyClassPtr)((char*)pClass + pClass->length);
	}

	ASSERTCOND(val,"xinputextdev: Unable to get valuator "
		   "information of '%s'\n",devname);

	/* open device */
	ASSERTCOND(xdev = XOpenDevice(display,xdevInfo->id),
		   "xinputextdev: Unable to open "
		   "input device '%s'\n",devname);

	/* button events */
	DeviceButtonPress(xdev,inputEventTypes[INPUTEVENT_BTN_PRESS],cls);
	if (cls) eventList[nEventListCnt++] = cls;
	DeviceButtonRelease(xdev,inputEventTypes[INPUTEVENT_BTN_RELEASE],cls);
	if (cls) eventList[nEventListCnt++] = cls;

	/* motion events */
	DeviceMotionNotify(xdev,inputEventTypes[INPUTEVENT_MOTION_NOTIFY],cls);
	if (cls) eventList[nEventListCnt++] = cls;

	/* proximity events */
	ProximityOut(xdev,inputEventTypes[INPUTEVENT_PROXIMITY_OUT],cls);
	if (cls) eventList[nEventListCnt++] = cls;
	ProximityIn(xdev,inputEventTypes[INPUTEVENT_PROXIMITY_IN],cls);
	if (cls) eventList[nEventListCnt++] = cls;

	/* grab device */
	int err = XGrabDevice(display,xdev,DefaultRootWindow(display),
			      0,nEventListCnt,eventList,
			      GrabModeAsync,GrabModeAsync,CurrentTime);

	if (err == AlreadyGrabbed)
		fprintf(stderr,"xinputextdev: Grab error: AlreadyGrabbed\n");
	else if (err == GrabNotViewable)
		fprintf(stderr, "xinputextdev: Grab error: GrabNotViewable\n");
	else if (err == GrabFrozen)
		fprintf(stderr, "xinputextdev: Grab error: GrabFrozen\n");
	else {
		printf("xinputextdev: Device '%s' grabbed.\n",devname);
		return Py_BuildValue("[i,i]",
				     val->axes[0].max_value,
				     val->axes[1].max_value);
	}

	return Py_BuildValue("i", 0);
}