9 #include <X11/extensions/XInput2.h>
10 #include <X11/Xutil.h>
12 static QString colorToString(QColor col)
14 return QString("#%1%2%3").arg(col.red(), 2, 16, QChar('0'))
15 .arg(col.green(), 2, 16, QChar('0'))
16 .arg(col.blue(), 2, 16, QChar('0'));
19 MultypoWindow::MultypoWindow(QWidget *parent) :
24 setStyleSheet("background-color: black; color: green; font-size: 30pt");
27 setLayout(new QVBoxLayout(this));
29 mainLabel = new QLabel("Hit <Esc> to quit", this);
30 layout()->addWidget(mainLabel);
32 /* Fullscreen, no cursor */
33 setWindowState(Qt::WindowFullScreen);
34 setCursor(QCursor(Qt::BlankCursor));
37 MultypoWindow::~MultypoWindow()
41 void MultypoWindow::handleKeyPress(int device, QString string)
43 qDebug() << "Device" << device << "String" << string;
45 if (string == "Escape")
48 if (!players.contains(device)) {
49 QLabel *label = new QLabel(this);
50 layout()->addWidget(label);
51 players[device] = label;
53 QColor textColor = QColor(Qt::blue);
54 players[device]->setText(players[device]->text() + "<span style='color:"+colorToString(textColor)+"'>"+string+"</span>");
57 bool MultypoWindow::handleX11Event(XEvent *event)
59 Display *dpy = x11Info().display();
60 /* Handle the first map event: We are finally visible! */
61 if (!xiInited && event->type == MapNotify) {
62 /* XInput Extension available? */
64 if (!XQueryExtension(dpy, "XInputExtension", &xiOpcode, &eventID, &errorID)) {
65 qCritical() << "X Input extension not available";
68 /* Which version of XI2? We support 2.0 */
69 int major = 2, minor = 0;
70 if (XIQueryVersion(dpy, &major, &minor) == BadRequest) {
71 qCritical() << "XI2 not available. Server supports" << major << "." <<
75 qDebug() << "System supports XI2";
78 // Now let's listen to keypress events
79 XIEventMask eventmask;
80 unsigned char mask [1] = { 0 }; // will change
81 eventmask.deviceid = XIAllMasterDevices;
82 eventmask.mask_len = sizeof (mask); // in bytes, for whatever reason...
83 eventmask.mask = mask;
84 XISetMask(mask, XI_KeyPress);
85 XISelectEvents (dpy, winId(), &eventmask, 1);
87 // finally, grab all focuses
89 XIDeviceInfo* devices = XIQueryDevice(dpy, XIAllMasterDevices, &ndevices);
90 for (int i = 0; i < ndevices; ++i) {
91 if (devices[i].use != XIMasterKeyboard) continue;
92 qDebug() << "Found master keyboard with ID" << devices[i].deviceid;
93 XISetFocus(dpy, devices[i].deviceid, winId(), CurrentTime);
95 XIFreeDeviceInfo(devices);
101 else if (xiInited && event->type == GenericEvent
102 && event->xcookie.type == GenericEvent && event->xcookie.extension == xiOpcode
103 && event->xcookie.evtype == XI_KeyPress && XGetEventData(dpy, &event->xcookie)) {
104 /* Handle XI event */
105 XIDeviceEvent *d_ev = (XIDeviceEvent*) event->xcookie.data;
106 KeyCode keycode = d_ev->detail;
107 int kbdid = d_ev->deviceid;
109 if (!(d_ev->flags & XIKeyRepeat)) {
110 int keysyms_per_keycode;
111 KeySym keysym = XGetKeyboardMapping (dpy, keycode, 1, &keysyms_per_keycode)[0];
112 handleKeyPress(kbdid, XKeysymToString(keysym));
115 XFreeEventData(dpy, &event->xcookie);