10 #include <X11/extensions/XInput2.h>
11 #include <X11/Xutil.h>
13 MultypoWindow::MultypoWindow(QWidget *parent) :
18 setStyleSheet("background-color: black; color: green; font-size: 30pt");
21 setLayout(new QVBoxLayout(this));
23 mainLabel = new QLabel("Hit <Esc> to quit", this);
24 layout()->addWidget(mainLabel);
26 /* Fullscreen, no cursor */
27 setWindowState(Qt::WindowFullScreen);
28 setCursor(QCursor(Qt::BlankCursor));
30 words.open(stdin,QIODevice::ReadOnly);
33 MultypoWindow::~MultypoWindow()
37 void MultypoWindow::resetPlayerText() {
38 for (QMap<int, Player*>::Iterator it = players.begin(); it != players.end(); ++it) {
39 it.value()->resetText();
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);
54 mainLabel->setText(word);
55 typingPlayers = players.size();
59 void MultypoWindow::handleKeyPress(int device, QString string)
61 qDebug() << "Device" << device << "String" << string;
63 if (string == "Escape") {
68 if (!players.contains(device)) {
69 players[device] = new Player(this);
71 players[device]->handleKey(string);
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;
82 qDebug() << "typingPlayers " << typingPlayers;
83 if (typingPlayers <= 0) {
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()) {
95 if (!players.empty() && allHaveNames) {
102 bool MultypoWindow::handleX11Event(XEvent *event)
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";
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 << "." <<
120 qDebug() << "System supports XI2";
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);
132 // finally, grab all focuses
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);
140 XIFreeDeviceInfo(devices);
146 else if (xiInited && event->type == GenericEvent
147 && event->xcookie.type == GenericEvent && event->xcookie.extension == xiOpcode
148 && event->xcookie.evtype == XI_KeyPress) {
150 if (XGetEventData(dpy, &event->xcookie) != True) {
151 qCritical() << "Error getting event data";
152 // FIXME return true;
155 /* Handle XI event */
156 XIDeviceEvent *d_ev = (XIDeviceEvent*) event->xcookie.data;
157 KeyCode keycode = d_ev->detail;
158 int kbdid = d_ev->deviceid;
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));
166 XFreeEventData(dpy, &event->xcookie);