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