8dea3e025abeea2b8ca1aa2aaa25dbd7d64d741e
[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().trimmed();
45         QString word = QString::fromUtf8(tmp);
46         if (word.isEmpty()) { // game over
47                 mainLabel->setText("GAME OVER");
48                 for (QMap<int, Player*>::Iterator it = players.begin(); it != players.end(); ++it) {
49                         QString tmp = QString ("Spieler %1 hat %2 Punkte.").arg(it.value()->getName()).arg(it.value()->score);
50                         it.value()->setWaiting(tmp);
51                 }
52         } else {
53                 resetPlayerText();
54                 mainLabel->setText(word);
55                 typingPlayers = players.size();
56         }
57 }
58
59 void MultypoWindow::handleKeyPress(int device, QString string)
60 {
61         qDebug() << "Device" << device << "String" << string;
62
63         if (string == "Escape") {
64                 close();
65                 return;
66         }
67
68         if (!players.contains(device)) {
69                 players[device] = new Player(this);
70         }
71         players[device]->handleKey(string);
72
73         if (gameStarted) { // ingame
74                 qDebug() << players[device]->getCurrentLine();
75                 qDebug() << mainLabel->text();
76                 if (players[device]->getCurrentLine() == mainLabel->text()) {
77                         players[device]->score += typingPlayers;
78                         QString readyString = QString("READY: %1 points").arg(typingPlayers);
79                         players[device]->setWaiting(readyString);
80                         qDebug() << "typingPlayers " << typingPlayers;
81                         typingPlayers--;
82                         qDebug() << "typingPlayers " << typingPlayers;
83                         if (typingPlayers <= 0) {
84                                 nextWord();
85                         }
86                 }
87         } else { // name entering phase
88                 bool allHaveNames = true;
89                 for (QMap<int, Player*>::Iterator it = players.begin(); it != players.end(); ++it) {
90                         if (! it.value()->hasName()) {
91                                 allHaveNames = false;
92                                 break;
93                         }
94                 }
95                 if (!players.empty() && allHaveNames) {
96                         gameStarted = true;
97                         nextWord();
98                 }
99         }
100 }
101
102 bool MultypoWindow::handleX11Event(XEvent *event)
103 {
104         Display *dpy = x11Info().display();
105         /* Handle the first map event: We are finally visible! */
106         if (!xiInited && event->type == MapNotify) {
107                 /* XInput Extension available? */
108                 int eventID, errorID;
109                 if (!XQueryExtension(dpy, "XInputExtension", &xiOpcode, &eventID, &errorID)) {
110                    qCritical() << "X Input extension not available";
111                 }
112
113                 /* Which version of XI2? We support 2.0 */
114                 int major = 2, minor = 1;
115                 if (XIQueryVersion(dpy, &major, &minor) == BadRequest) {
116                         qCritical() << "XI2 not available. Server supports" << major << "." <<
117                                                 minor;
118                 }
119
120                 qDebug() << "System supports XI2";
121
122
123                 // Now let's listen to keypress events
124                 XIEventMask eventmask;
125                 unsigned char mask [1] = { 0 }; // will change
126                 eventmask.deviceid = XIAllMasterDevices;
127                 eventmask.mask_len = sizeof (mask); // in bytes, for whatever reason...
128                 eventmask.mask = mask;
129                 XISetMask(mask, XI_KeyPress);
130                 XISelectEvents (dpy, winId(), &eventmask, 1);
131
132                 // finally, grab all focuses
133                 int ndevices;
134                 XIDeviceInfo* devices = XIQueryDevice(dpy, XIAllMasterDevices, &ndevices);
135                 for (int i = 0; i < ndevices; ++i) {
136                         if (devices[i].use != XIMasterKeyboard) continue;
137                         qDebug() << "Found master keyboard with ID" << devices[i].deviceid;
138                         XISetFocus(dpy, devices[i].deviceid, winId(), CurrentTime);
139                 }
140                 XIFreeDeviceInfo(devices);
141
142                 xiInited = true;
143
144                 return false;
145         }
146         else if (xiInited && event->type == GenericEvent
147                 && event->xcookie.type == GenericEvent && event->xcookie.extension == xiOpcode
148                 && event->xcookie.evtype == XI_KeyPress) { 
149                 
150                 if (XGetEventData(dpy, &event->xcookie) != True) {
151                         qCritical() << "Error getting event data";
152                         // FIXME return true;
153                 }
154                 
155                 /* Handle XI event */
156                 XIDeviceEvent *d_ev = (XIDeviceEvent*) event->xcookie.data;
157             KeyCode keycode = d_ev->detail;
158             int kbdid = d_ev->deviceid;
159
160                 if (!(d_ev->flags & XIKeyRepeat)) {
161                         int keysyms_per_keycode;
162                         KeySym keysym = XGetKeyboardMapping (dpy, keycode, 1, &keysyms_per_keycode)[0];
163                         handleKeyPress(kbdid, XKeysymToString(keysym));
164                 }
165
166                 XFreeEventData(dpy, &event->xcookie);
167
168                 return true;
169         }
170         else {
171                 return false;
172         }
173 }