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::handleKeyPress(int device, QString string)
31 qDebug() << "Device" << device << "String" << string;
32 mainLabel->setText(QString("%1 pressed on %2").arg(string).arg(device));
35 bool MultypoWindow::handleX11Event(XEvent *event)
37 Display *dpy = x11Info().display();
38 /* Handle the first map event: We are finally visible! */
39 if (!xiInited && event->type == MapNotify) {
40 /* XInput Extension available? */
42 if (!XQueryExtension(dpy, "XInputExtension", &xiOpcode, &eventID, &errorID)) {
43 qCritical() << "X Input extension not available";
46 /* Which version of XI2? We support 2.0 */
47 int major = 2, minor = 0;
48 if (XIQueryVersion(dpy, &major, &minor) == BadRequest) {
49 qCritical() << "XI2 not available. Server supports" << major << "." <<
53 qDebug() << "System supports XI2";
56 // Now let's listen to keypress events
57 XIEventMask eventmask;
58 unsigned char mask [1] = { 0 }; // will change
59 eventmask.deviceid = XIAllMasterDevices;
60 eventmask.mask_len = sizeof (mask); // in bytes, for whatever reason...
61 eventmask.mask = mask;
62 XISetMask(mask, XI_KeyPress);
63 XISelectEvents (dpy, winId(), &eventmask, 1);
65 // finally, grab all focuses
67 XIDeviceInfo* devices = XIQueryDevice(dpy, XIAllMasterDevices, &ndevices);
68 for (int i = 0; i < ndevices; ++i) {
69 if (devices[i].use != XIMasterKeyboard) continue;
70 qDebug() << "Found master keyboard with ID" << devices[i].deviceid;
71 XISetFocus(dpy, devices[i].deviceid, winId(), CurrentTime);
78 else if (xiInited && event->type == GenericEvent
79 && event->xcookie.type == GenericEvent && event->xcookie.extension == xiOpcode
80 && event->xcookie.evtype == XI_KeyPress && XGetEventData(dpy, &event->xcookie)) {
82 XIDeviceEvent *d_ev = (XIDeviceEvent*) event->xcookie.data;
83 KeyCode keycode = d_ev->detail;
84 int kbdid = d_ev->deviceid;
86 if (!(d_ev->flags & XIKeyRepeat)) {
87 int keysyms_per_keycode;
88 KeySym keysym = XGetKeyboardMapping (dpy, keycode, 1, &keysyms_per_keycode)[0];
89 handleKeyPress(kbdid, XKeysymToString(keysym));
92 XFreeEventData(dpy, &event->xcookie);