9 #include <X11/extensions/XInput2.h>
10 #include <X11/Xutil.h>
12 MultypoWindow::MultypoWindow(QWidget *parent) :
16 QHBoxLayout *layout = new QHBoxLayout(this);
19 mainLabel = new QLabel(this);
20 layout->addWidget(mainLabel);
22 setMinimumSize(300, 300);
25 MultypoWindow::~MultypoWindow()
29 void MultypoWindow::showEvent ( QShowEvent * )
33 Display *dpy = x11Info().display();
35 /* XInput Extension available? */
37 if (!XQueryExtension(dpy, "XInputExtension", &xiOpcode, &eventID, &errorID)) {
38 qCritical() << "X Input extension not available";
41 /* Which version of XI2? We support 2.0 */
42 int major = 2, minor = 0;
43 if (XIQueryVersion(dpy, &major, &minor) == BadRequest) {
44 qCritical() << "XI2 not available. Server supports" << major << "." <<
48 qDebug() << "System is sane";
51 // Now let's listen to keypress events
52 XIEventMask eventmask;
53 unsigned char mask [1] = { 0 }; // will change
54 eventmask.deviceid = XIAllMasterDevices;
55 eventmask.mask_len = sizeof (mask); // in bytes, for whatever reason...
56 eventmask.mask = mask;
57 XISetMask(mask, XI_KeyPress);
58 XISelectEvents (dpy, winId(), &eventmask, 1);
63 void MultypoWindow::handleKeyPress(int device, QString string)
65 qDebug() << "Device" << device << "String" << string;
66 mainLabel->setText(QString("%1 pressed on %2").arg(string).arg(device));
69 bool MultypoWindow::handleX11Event(XEvent *event)
71 if (!xiInited) return false;
72 Display *dpy = x11Info().display();
74 if (event->xcookie.type == GenericEvent && event->xcookie.extension == xiOpcode
75 && event->xcookie.evtype == XI_KeyPress && XGetEventData(dpy, &event->xcookie)) {
77 XIDeviceEvent *d_ev = (XIDeviceEvent*) event->xcookie.data;
78 KeyCode keycode = d_ev->detail;
79 int kbdid = d_ev->deviceid;
81 if (!(d_ev->flags & XIKeyRepeat)) {
82 int keysyms_per_keycode;
83 KeySym keysym = XGetKeyboardMapping (dpy, keycode, 1, &keysyms_per_keycode)[0];
84 handleKeyPress(kbdid, XKeysymToString(keysym));
87 XFreeEventData(dpy, &event->xcookie);