static PyObject *xextdev_poll(PyObject *self, PyObject *args) { fd_set in_fds; XEvent event; struct timeval tv; int gotsome = FALSE; FD_ZERO(&in_fds); FD_SET(x11_fd, &in_fds); tv.tv_sec = 1; tv.tv_usec = 0; PyObject *list = PyList_New(0); while(!term) { if (XCheckIfEvent(display,&event,predicate,NULL)) { do { PyObject *py = process_event(&event); if (py) { PyList_Append(list,py); gotsome = TRUE; } } while (XCheckIfEvent(display,&event,predicate,NULL)); if (gotsome) break; } Py_BEGIN_ALLOW_THREADS select(x11_fd+1, &in_fds, 0, 0, &tv); tv.tv_sec = 1; tv.tv_usec = 0; Py_END_ALLOW_THREADS } if (term) { fprintf(stderr,"Ungrabbing device...\n"); XUngrabDevice(display,xdev,CurrentTime); //while (XPending(display)) // XNextEvent(display,&event); XCloseDisplay(display); display = NULL; } return list; }
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; }