0c5451ece7c8be2deed7c237b6407a6938b87cfd
[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; font-size: 30pt");
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                 return;
41         }
42
43         if (!players.contains(device)) {
44                 players[device] = new Player(this);
45         }
46         players[device]->handleKey(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 = 1;
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) { 
96                 
97                 if (XGetEventData(dpy, &event->xcookie) != True) {
98                         qCritical() << "Error getting event data";
99                         // FIXME return true;
100                 }
101                 
102                 /* Handle XI event */
103                 XIDeviceEvent *d_ev = (XIDeviceEvent*) event->xcookie.data;
104             KeyCode keycode = d_ev->detail;
105             int kbdid = d_ev->deviceid;
106
107                 if (!(d_ev->flags & XIKeyRepeat)) {
108                         int keysyms_per_keycode;
109                         KeySym keysym = XGetKeyboardMapping (dpy, keycode, 1, &keysyms_per_keycode)[0];
110                         handleKeyPress(kbdid, XKeysymToString(keysym));
111                 }
112
113                 XFreeEventData(dpy, &event->xcookie);
114
115                 return true;
116         }
117         else {
118                 return false;
119         }
120 }