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 mainLabel->setText(word);
47 typingPlayers = players.size();
50 void MultypoWindow::handleKeyPress(int device, QString string)
52 qDebug() << "Device" << device << "String" << string;
54 if (string == "Escape") {
59 if (!players.contains(device)) {
60 players[device] = new Player(this);
62 players[device]->handleKey(string);
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);
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()) {
80 if (!players.empty() && allHaveNames) {
87 bool MultypoWindow::handleX11Event(XEvent *event)
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? */
94 if (!XQueryExtension(dpy, "XInputExtension", &xiOpcode, &eventID, &errorID)) {
95 qCritical() << "X Input extension not available";
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 << "." <<
105 qDebug() << "System supports XI2";
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);
117 // finally, grab all focuses
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);
125 XIFreeDeviceInfo(devices);
131 else if (xiInited && event->type == GenericEvent
132 && event->xcookie.type == GenericEvent && event->xcookie.extension == xiOpcode
133 && event->xcookie.evtype == XI_KeyPress) {
135 if (XGetEventData(dpy, &event->xcookie) != True) {
136 qCritical() << "Error getting event data";
137 // FIXME return true;
140 /* Handle XI event */
141 XIDeviceEvent *d_ev = (XIDeviceEvent*) event->xcookie.data;
142 KeyCode keycode = d_ev->detail;
143 int kbdid = d_ev->deviceid;
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));
151 XFreeEventData(dpy, &event->xcookie);