9 #include <X11/extensions/XInput2.h>
10 #include <X11/Xutil.h>
12 MultypoWindow::MultypoWindow(QWidget *parent) :
17 setStyleSheet("background-color: black; color: green");
20 setLayout(new QVBoxLayout(this));
22 mainLabel = new QLabel("Hit <Esc> to quit", this);
23 layout()->addWidget(mainLabel);
25 /* Fullscreen, no cursor */
26 setWindowState(Qt::WindowFullScreen);
27 setCursor(QCursor(Qt::BlankCursor));
30 MultypoWindow::~MultypoWindow()
34 void MultypoWindow::handleKeyPress(int device, QString string)
36 qDebug() << "Device" << device << "String" << string;
38 if (string == "Escape")
41 if (!players.contains(device)) {
42 QLabel *label = new QLabel(this);
43 layout()->addWidget(label);
44 players[device] = label;
46 players[device]->setText(players[device]->text() + string);
49 bool MultypoWindow::handleX11Event(XEvent *event)
51 Display *dpy = x11Info().display();
52 /* Handle the first map event: We are finally visible! */
53 if (!xiInited && event->type == MapNotify) {
54 /* XInput Extension available? */
56 if (!XQueryExtension(dpy, "XInputExtension", &xiOpcode, &eventID, &errorID)) {
57 qCritical() << "X Input extension not available";
60 /* Which version of XI2? We support 2.0 */
61 int major = 2, minor = 0;
62 if (XIQueryVersion(dpy, &major, &minor) == BadRequest) {
63 qCritical() << "XI2 not available. Server supports" << major << "." <<
67 qDebug() << "System supports XI2";
70 // Now let's listen to keypress events
71 XIEventMask eventmask;
72 unsigned char mask [1] = { 0 }; // will change
73 eventmask.deviceid = XIAllMasterDevices;
74 eventmask.mask_len = sizeof (mask); // in bytes, for whatever reason...
75 eventmask.mask = mask;
76 XISetMask(mask, XI_KeyPress);
77 XISelectEvents (dpy, winId(), &eventmask, 1);
79 // finally, grab all focuses
81 XIDeviceInfo* devices = XIQueryDevice(dpy, XIAllMasterDevices, &ndevices);
82 for (int i = 0; i < ndevices; ++i) {
83 if (devices[i].use != XIMasterKeyboard) continue;
84 qDebug() << "Found master keyboard with ID" << devices[i].deviceid;
85 XISetFocus(dpy, devices[i].deviceid, winId(), CurrentTime);
87 XIFreeDeviceInfo(devices);
93 else if (xiInited && event->type == GenericEvent
94 && event->xcookie.type == GenericEvent && event->xcookie.extension == xiOpcode
95 && event->xcookie.evtype == XI_KeyPress && XGetEventData(dpy, &event->xcookie)) {
97 XIDeviceEvent *d_ev = (XIDeviceEvent*) event->xcookie.data;
98 KeyCode keycode = d_ev->detail;
99 int kbdid = d_ev->deviceid;
101 if (!(d_ev->flags & XIKeyRepeat)) {
102 int keysyms_per_keycode;
103 KeySym keysym = XGetKeyboardMapping (dpy, keycode, 1, &keysyms_per_keycode)[0];
104 handleKeyPress(kbdid, XKeysymToString(keysym));
107 XFreeEventData(dpy, &event->xcookie);