X-Git-Url: https://git.ralfj.de/multypo.git/blobdiff_plain/1d03d11fb0aa8e3418597e7c1ab38f2b28d48924..0712b0f435879a9fc49f8b814f7b3b388da88441:/qt/multypo.cpp diff --git a/qt/multypo.cpp b/qt/multypo.cpp index 6e461e3..1667c0f 100644 --- a/qt/multypo.cpp +++ b/qt/multypo.cpp @@ -2,6 +2,9 @@ #include #include +#include +#include +#include #include #include @@ -11,64 +14,142 @@ MultypoWindow::MultypoWindow(QWidget *parent) : QWidget(parent), xiInited(false) { - + /* Prepare colors */ + setStyleSheet("background-color: black; color: green; font-size: 30pt"); + + /* Prepare conents */ + setLayout(new QVBoxLayout(this)); + + mainLabel = new QLabel("Hit to quit", this); + layout()->addWidget(mainLabel); + + /* Fullscreen, no cursor */ + setWindowState(Qt::WindowFullScreen); + setCursor(QCursor(Qt::BlankCursor)); + + words.open(stdin,QIODevice::ReadOnly); } MultypoWindow::~MultypoWindow() { } -void MultypoWindow::showEvent ( QShowEvent * ) -{ - if (xiInited) return; - - Display *dpy = x11Info().display(); - - /* XInput Extension available? */ - int eventID, errorID; - if (!XQueryExtension(dpy, "XInputExtension", &xiOpcode, &eventID, &errorID)) { - qCritical() << "X Input extension not available"; +void MultypoWindow::resetPlayerText() { + for (QMap::Iterator it = players.begin(); it != players.end(); ++it) { + it.value()->resetText(); } +} - /* Which version of XI2? We support 2.0 */ - int major = 2, minor = 0; - if (XIQueryVersion(dpy, &major, &minor) == BadRequest) { - qCritical() << "XI2 not available. Server supports" << major << "." << - minor; +void MultypoWindow::nextWord() { + QByteArray tmp = words.readLine().trimmed(); + QString word = QString::fromUtf8(tmp); + if (word.isEmpty()) { // game over + qDebug() << "TODO: implement gameover"; + exit (0); + } else { + resetPlayerText(); + mainLabel->setText(word); + typingPlayers = players.size(); } - - qDebug() << "System is sane"; - - - // Now let's listen to keypress events - XIEventMask eventmask; - unsigned char mask [1] = { 0 }; // will change - eventmask.deviceid = XIAllMasterDevices; - eventmask.mask_len = sizeof (mask); // in bytes, for whatever reason... - eventmask.mask = mask; - XISetMask(mask, XI_KeyPress); - XISelectEvents (dpy, winId(), &eventmask, 1); - - xiInited = true; - - /*activateWindow(); - raise(); - setFocus(Qt::OtherFocusReason);*/ } void MultypoWindow::handleKeyPress(int device, QString string) { qDebug() << "Device" << device << "String" << string; + + if (string == "Escape") { + close(); + return; + } + + if (!players.contains(device)) { + players[device] = new Player(this); + } + players[device]->handleKey(string); + + if (gameStarted) { // ingame + qDebug() << players[device]->getCurrentLine(); + qDebug() << mainLabel->text(); + if (players[device]->getCurrentLine() == mainLabel->text()) { + players[device]->score += typingPlayers; + QString readyString = QString("READY: %1 points").arg(typingPlayers); + players[device]->setWaiting(readyString); + qDebug() << "typingPlayers " << typingPlayers; + typingPlayers--; + qDebug() << "typingPlayers " << typingPlayers; + if (typingPlayers <= 0) { + nextWord(); + } + } + } else { // name entering phase + bool allHaveNames = true; + for (QMap::Iterator it = players.begin(); it != players.end(); ++it) { + if (! it.value()->hasName()) { + allHaveNames = false; + break; + } + } + if (!players.empty() && allHaveNames) { + gameStarted = true; + nextWord(); + } + } } bool MultypoWindow::handleX11Event(XEvent *event) { - if (!xiInited) return false; Display *dpy = x11Info().display(); + /* Handle the first map event: We are finally visible! */ + if (!xiInited && event->type == MapNotify) { + /* XInput Extension available? */ + int eventID, errorID; + if (!XQueryExtension(dpy, "XInputExtension", &xiOpcode, &eventID, &errorID)) { + qCritical() << "X Input extension not available"; + } - if (event->xcookie.type == GenericEvent && event->xcookie.extension == xiOpcode - && event->xcookie.evtype == XI_KeyPress && XGetEventData(dpy, &event->xcookie)) { + /* Which version of XI2? We support 2.0 */ + int major = 2, minor = 1; + if (XIQueryVersion(dpy, &major, &minor) == BadRequest) { + qCritical() << "XI2 not available. Server supports" << major << "." << + minor; + } + qDebug() << "System supports XI2"; + + + // Now let's listen to keypress events + XIEventMask eventmask; + unsigned char mask [1] = { 0 }; // will change + eventmask.deviceid = XIAllMasterDevices; + eventmask.mask_len = sizeof (mask); // in bytes, for whatever reason... + eventmask.mask = mask; + XISetMask(mask, XI_KeyPress); + XISelectEvents (dpy, winId(), &eventmask, 1); + + // finally, grab all focuses + int ndevices; + XIDeviceInfo* devices = XIQueryDevice(dpy, XIAllMasterDevices, &ndevices); + for (int i = 0; i < ndevices; ++i) { + if (devices[i].use != XIMasterKeyboard) continue; + qDebug() << "Found master keyboard with ID" << devices[i].deviceid; + XISetFocus(dpy, devices[i].deviceid, winId(), CurrentTime); + } + XIFreeDeviceInfo(devices); + + xiInited = true; + + return false; + } + else if (xiInited && event->type == GenericEvent + && event->xcookie.type == GenericEvent && event->xcookie.extension == xiOpcode + && event->xcookie.evtype == XI_KeyPress) { + + if (XGetEventData(dpy, &event->xcookie) != True) { + qCritical() << "Error getting event data"; + // FIXME return true; + } + + /* Handle XI event */ XIDeviceEvent *d_ev = (XIDeviceEvent*) event->xcookie.data; KeyCode keycode = d_ev->detail; int kbdid = d_ev->deviceid; @@ -83,6 +164,7 @@ bool MultypoWindow::handleX11Event(XEvent *event) return true; } - - return false; + else { + return false; + } }