e6befe2535fd113b0ecaceb798ea04d7befda7c6
[multypo.git] / qt / multypo.cpp
1 #include "multypo.h"
2
3 #include <QX11Info>
4 #include <QDebug>
5 #include <QVBoxLayout>
6 #include <QLabel>
7 #include <QMap>
8
9 #include <X11/Xlib.h>
10 #include <X11/extensions/XInput2.h>
11 #include <X11/Xutil.h>
12
13 MultypoWindow::MultypoWindow(QWidget *parent) :
14         QWidget(parent),
15         xiInited(false)
16 {
17         /* Prepare colors */
18         setStyleSheet("background-color: black; color: green; font-size: 30pt");
19
20         /* Prepare conents */
21         setLayout(new QVBoxLayout(this));
22
23         mainLabel = new QLabel("Hit <Esc> to quit", this);
24         layout()->addWidget(mainLabel);
25
26         /* Fullscreen, no cursor */
27         setWindowState(Qt::WindowFullScreen);
28         setCursor(QCursor(Qt::BlankCursor));
29
30         words.open(stdin,QIODevice::ReadOnly);
31 }
32
33 MultypoWindow::~MultypoWindow()
34 {
35 }
36
37 void MultypoWindow::resetPlayerText() {
38         for (QMap<int, Player*>::Iterator it = players.begin(); it != players.end(); ++it) {
39                 it.value()->resetText();
40         }
41 }
42
43 void MultypoWindow::nextWord() {
44         QByteArray tmp = words.readLine();
45         QString word = QString::fromUtf8(tmp);
46         mainLabel->setText(word);
47 }
48
49 void MultypoWindow::handleKeyPress(int device, QString string)
50 {
51         qDebug() << "Device" << device << "String" << string;
52
53         if (string == "Escape") {
54                 close();
55                 return;
56         }
57
58         if (!players.contains(device)) {
59                 players[device] = new Player(this);
60         }
61         players[device]->handleKey(string);
62
63         if (gameStarted) { // ingame
64
65         } else { // name entering phase
66                 bool allHaveNames = true;
67                 for (QMap<int, Player*>::Iterator it = players.begin(); it != players.end(); ++it) {
68                         if (! it.value()->hasName()) {
69                                 allHaveNames = false;
70                                 break;
71                         }
72                 }
73                 if (!players.empty() && allHaveNames) {
74                         gameStarted = true;
75                         nextWord();
76                 }
77         }
78 }
79
80 bool MultypoWindow::handleX11Event(XEvent *event)
81 {
82         Display *dpy = x11Info().display();
83         /* Handle the first map event: We are finally visible! */
84         if (!xiInited && event->type == MapNotify) {
85                 /* XInput Extension available? */
86                 int eventID, errorID;
87                 if (!XQueryExtension(dpy, "XInputExtension", &xiOpcode, &eventID, &errorID)) {
88                    qCritical() << "X Input extension not available";
89                 }
90
91                 /* Which version of XI2? We support 2.0 */
92                 int major = 2, minor = 1;
93                 if (XIQueryVersion(dpy, &major, &minor) == BadRequest) {
94                         qCritical() << "XI2 not available. Server supports" << major << "." <<
95                                                 minor;
96                 }
97
98                 qDebug() << "System supports XI2";
99
100
101                 // Now let's listen to keypress events
102                 XIEventMask eventmask;
103                 unsigned char mask [1] = { 0 }; // will change
104                 eventmask.deviceid = XIAllMasterDevices;
105                 eventmask.mask_len = sizeof (mask); // in bytes, for whatever reason...
106                 eventmask.mask = mask;
107                 XISetMask(mask, XI_KeyPress);
108                 XISelectEvents (dpy, winId(), &eventmask, 1);
109
110                 // finally, grab all focuses
111                 int ndevices;
112                 XIDeviceInfo* devices = XIQueryDevice(dpy, XIAllMasterDevices, &ndevices);
113                 for (int i = 0; i < ndevices; ++i) {
114                         if (devices[i].use != XIMasterKeyboard) continue;
115                         qDebug() << "Found master keyboard with ID" << devices[i].deviceid;
116                         //XISetFocus(dpy, devices[i].deviceid, winId(), CurrentTime);
117                 }
118                 XIFreeDeviceInfo(devices);
119
120                 xiInited = true;
121
122                 return false;
123         }
124         else if (xiInited && event->type == GenericEvent
125                 && event->xcookie.type == GenericEvent && event->xcookie.extension == xiOpcode
126                 && event->xcookie.evtype == XI_KeyPress) { 
127                 
128                 if (XGetEventData(dpy, &event->xcookie) != True) {
129                         qCritical() << "Error getting event data";
130                         // FIXME return true;
131                 }
132                 
133                 /* Handle XI event */
134                 XIDeviceEvent *d_ev = (XIDeviceEvent*) event->xcookie.data;
135             KeyCode keycode = d_ev->detail;
136             int kbdid = d_ev->deviceid;
137
138                 if (!(d_ev->flags & XIKeyRepeat)) {
139                         int keysyms_per_keycode;
140                         KeySym keysym = XGetKeyboardMapping (dpy, keycode, 1, &keysyms_per_keycode)[0];
141                         handleKeyPress(kbdid, XKeysymToString(keysym));
142                 }
143
144                 XFreeEventData(dpy, &event->xcookie);
145
146                 return true;
147         }
148         else {
149                 return false;
150         }
151 }