Add per-device output label
[multypo.git] / qt / multypo.cpp
1 #include "multypo.h"
2
3 #include <QX11Info>
4 #include <QDebug>
5 #include <QVBoxLayout>
6 #include <QLabel>
7
8 #include <X11/Xlib.h>
9 #include <X11/extensions/XInput2.h>
10 #include <X11/Xutil.h>
11
12 MultypoWindow::MultypoWindow(QWidget *parent) :
13         QWidget(parent),
14         xiInited(false)
15 {
16         /* Prepare colors */
17         setStyleSheet("background-color: black; color: green");
18
19         /* Prepare conents */
20         setLayout(new QVBoxLayout(this));
21
22         mainLabel = new QLabel("Hit <Esc> to quit", this);
23         layout()->addWidget(mainLabel);
24
25         /* Fullscreen, no cursor */
26         setWindowState(Qt::WindowFullScreen);
27         setCursor(QCursor(Qt::BlankCursor));
28 }
29
30 MultypoWindow::~MultypoWindow()
31 {
32 }
33
34 void MultypoWindow::handleKeyPress(int device, QString string)
35 {
36         qDebug() << "Device" << device << "String" << string;
37
38         if (string == "Escape")
39                 close();
40
41         if (!players.contains(device)) {
42                 QLabel *label = new QLabel(this);
43                 layout()->addWidget(label);
44                 players[device] = label;
45         }
46         players[device]->setText(players[device]->text() + string);
47 }
48
49 bool MultypoWindow::handleX11Event(XEvent *event)
50 {
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? */
55                 int eventID, errorID;
56                 if (!XQueryExtension(dpy, "XInputExtension", &xiOpcode, &eventID, &errorID)) {
57                    qCritical() << "X Input extension not available";
58                 }
59
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 << "." <<
64                                                 minor;
65                 }
66
67                 qDebug() << "System supports XI2";
68
69
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);
78
79                 // finally, grab all focuses
80                 int ndevices;
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);
86                 }
87                 XIFreeDeviceInfo(devices);
88
89                 xiInited = true;
90
91                 return false;
92         }
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)) {
96                 /* Handle XI event */
97                 XIDeviceEvent *d_ev = (XIDeviceEvent*) event->xcookie.data;
98             KeyCode keycode = d_ev->detail;
99             int kbdid = d_ev->deviceid;
100
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));
105                 }
106
107                 XFreeEventData(dpy, &event->xcookie);
108
109                 return true;
110         }
111         else {
112                 return false;
113         }
114 }