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 qDebug() << "TODO: implement gameover";
51 mainLabel->setText(word);
52 typingPlayers = players.size();
56 void MultypoWindow::handleKeyPress(int device, QString string)
58 qDebug() << "Device" << device << "String" << string;
60 if (string == "Escape") {
65 if (!players.contains(device)) {
66 players[device] = new Player(this);
68 players[device]->handleKey(string);
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;
79 qDebug() << "typingPlayers " << typingPlayers;
80 if (typingPlayers <= 0) {
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()) {
92 if (!players.empty() && allHaveNames) {
99 bool MultypoWindow::handleX11Event(XEvent *event)
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";
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 << "." <<
117 qDebug() << "System supports XI2";
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);
129 // finally, grab all focuses
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);
137 XIFreeDeviceInfo(devices);
143 else if (xiInited && event->type == GenericEvent
144 && event->xcookie.type == GenericEvent && event->xcookie.extension == xiOpcode
145 && event->xcookie.evtype == XI_KeyPress) {
147 if (XGetEventData(dpy, &event->xcookie) != True) {
148 qCritical() << "Error getting event data";
149 // FIXME return true;
152 /* Handle XI event */
153 XIDeviceEvent *d_ev = (XIDeviceEvent*) event->xcookie.data;
154 KeyCode keycode = d_ev->detail;
155 int kbdid = d_ev->deviceid;
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));
163 XFreeEventData(dpy, &event->xcookie);