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