10 #include <X11/extensions/XInput2.h>
11 #include <X11/Xutil.h>
13 MultypoWindow::MultypoWindow(QWidget *parent) :
19 setStyleSheet("background-color: black; color: green; font-size: 45pt");
22 setLayout(new QVBoxLayout(this));
24 mainLabel = new QLabel("Namen eingeben, dann Return", this);
25 layout()->addWidget(mainLabel);
27 /* Fullscreen, no cursor */
28 setWindowState(Qt::WindowFullScreen);
29 setCursor(QCursor(Qt::BlankCursor));
31 words.open(stdin,QIODevice::ReadOnly);
34 MultypoWindow::~MultypoWindow()
38 void MultypoWindow::nextWord() {
39 QByteArray tmp = words.readLine().trimmed();
40 currentWord = QString::fromUtf8(tmp);
41 qDebug() << "New word" << currentWord;
42 if (currentWord.isEmpty()) { // game over
43 mainLabel->setText("GAME OVER");
44 for (QMap<int, Player*>::Iterator it = players.begin(); it != players.end(); ++it) {
45 it.value()->showScore();
49 for (QMap<int, Player*>::Iterator it = players.begin(); it != players.end(); ++it) {
50 it.value()->nextWord();
52 mainLabel->setText(currentWord);
57 bool MultypoWindow::allPlayersWaiting()
59 for (QMap<int, Player*>::Iterator it = players.begin(); it != players.end(); ++it) {
60 if (it.value()->getState() != Player::Waiting) return false;
65 int MultypoWindow::typingPlayers()
68 for (QMap<int, Player*>::Iterator it = players.begin(); it != players.end(); ++it) {
69 if (it.value()->getState() == Player::Typing) ++n;
74 void MultypoWindow::handleKeyPress(int device, QString string)
76 qDebug() << "Device" << device << "String" << string;
78 if (string == "Escape") {
83 if (!players.contains(device)) {
86 players[device] = new Player(this);
88 Player *player = players[device];
89 player->handleKey(string);
91 if (state == Naming) {
92 // someone's still naming (or nobody's there yet)
93 qDebug() << "checking for game started";
94 if (!players.empty() && allPlayersWaiting()) {
98 else if (state == Playing) { // all players are waiting or typing
99 qDebug() << "current player line" << player->getCurrentWord();
100 qDebug() << "current word" << currentWord;
101 if (player->getState() == Player::Typing && player->getCurrentWord() == currentWord) {
102 int points = typingPlayers()+1;
103 player->wordComplete(points);
104 QString readyString = QString("READY: %1 points").arg(points);
105 if (allPlayersWaiting()) {
112 bool MultypoWindow::handleX11Event(XEvent *event)
114 Display *dpy = x11Info().display();
115 /* Handle the first map event: We are finally visible! */
116 if (!xiInited && event->type == MapNotify) {
117 /* XInput Extension available? */
118 int eventID, errorID;
119 if (!XQueryExtension(dpy, "XInputExtension", &xiOpcode, &eventID, &errorID)) {
120 qCritical() << "X Input extension not available";
123 /* Which version of XI2? We support 2.0 */
124 int major = 2, minor = 1;
125 if (XIQueryVersion(dpy, &major, &minor) == BadRequest) {
126 qCritical() << "XI2 not available. Server supports" << major << "." <<
130 qDebug() << "System supports XI2";
133 // Now let's listen to keypress events
134 XIEventMask eventmask;
135 unsigned char mask [1] = { 0 }; // will change
136 eventmask.deviceid = XIAllMasterDevices;
137 eventmask.mask_len = sizeof (mask); // in bytes, for whatever reason...
138 eventmask.mask = mask;
139 XISetMask(mask, XI_KeyPress);
140 XISelectEvents (dpy, winId(), &eventmask, 1);
142 // finally, grab all focuses
144 XIDeviceInfo* devices = XIQueryDevice(dpy, XIAllMasterDevices, &ndevices);
145 for (int i = 0; i < ndevices; ++i) {
146 if (devices[i].use != XIMasterKeyboard) continue;
147 qDebug() << "Found master keyboard with ID" << devices[i].deviceid;
148 XISetFocus(dpy, devices[i].deviceid, winId(), CurrentTime);
150 XIFreeDeviceInfo(devices);
156 else if (xiInited && event->type == GenericEvent
157 && event->xcookie.type == GenericEvent && event->xcookie.extension == xiOpcode
158 && event->xcookie.evtype == XI_KeyPress) {
160 if (XGetEventData(dpy, &event->xcookie) != True) {
161 qCritical() << "Error getting event data";
162 // FIXME return true;
165 /* Handle XI event */
166 XIDeviceEvent *d_ev = (XIDeviceEvent*) event->xcookie.data;
167 KeyCode keycode = d_ev->detail;
168 int kbdid = d_ev->deviceid;
170 if (!(d_ev->flags & XIKeyRepeat)) {
171 int keysyms_per_keycode;
172 KeySym keysym = XGetKeyboardMapping (dpy, keycode, 1, &keysyms_per_keycode)[0];
173 handleKeyPress(kbdid, XKeysymToString(keysym));
176 XFreeEventData(dpy, &event->xcookie);