there are players now :-) every game is better with players
[multypo.git] / qt / multypo.cpp
index 638114cde667c80b15703d107d4589e3e4ee3bd9..0c5451ece7c8be2deed7c237b6407a6938b87cfd 100644 (file)
@@ -2,7 +2,7 @@
 
 #include <QX11Info>
 #include <QDebug>
-#include <QHBoxLayout>
+#include <QVBoxLayout>
 #include <QLabel>
 
 #include <X11/Xlib.h>
@@ -13,13 +13,18 @@ MultypoWindow::MultypoWindow(QWidget *parent) :
        QWidget(parent),
        xiInited(false)
 {
-       QHBoxLayout *layout = new QHBoxLayout(this);
-       setLayout(layout);
+       /* Prepare colors */
+       setStyleSheet("background-color: black; color: green; font-size: 30pt");
 
-       mainLabel = new QLabel(this);
-       layout->addWidget(mainLabel);
+       /* Prepare conents */
+       setLayout(new QVBoxLayout(this));
 
-       setMinimumSize(300, 300);
+       mainLabel = new QLabel("Hit <Esc> to quit", this);
+       layout()->addWidget(mainLabel);
+
+       /* Fullscreen, no cursor */
+       setWindowState(Qt::WindowFullScreen);
+       setCursor(QCursor(Qt::BlankCursor));
 }
 
 MultypoWindow::~MultypoWindow()
@@ -29,7 +34,16 @@ MultypoWindow::~MultypoWindow()
 void MultypoWindow::handleKeyPress(int device, QString string)
 {
        qDebug() << "Device" << device << "String" << string;
-       mainLabel->setText(QString("%1 pressed on %2").arg(string).arg(device));
+
+       if (string == "Escape") {
+               close();
+               return;
+       }
+
+       if (!players.contains(device)) {
+               players[device] = new Player(this);
+       }
+       players[device]->handleKey(string);
 }
 
 bool MultypoWindow::handleX11Event(XEvent *event)
@@ -44,7 +58,7 @@ bool MultypoWindow::handleX11Event(XEvent *event)
                }
 
                /* Which version of XI2? We support 2.0 */
-               int major = 2, minor = 0;
+               int major = 2, minor = 1;
                if (XIQueryVersion(dpy, &major, &minor) == BadRequest) {
                        qCritical() << "XI2 not available. Server supports" << major << "." <<
                                                minor;
@@ -68,8 +82,9 @@ bool MultypoWindow::handleX11Event(XEvent *event)
                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);
+                       //XISetFocus(dpy, devices[i].deviceid, winId(), CurrentTime);
                }
+               XIFreeDeviceInfo(devices);
 
                xiInited = true;
 
@@ -77,7 +92,13 @@ bool MultypoWindow::handleX11Event(XEvent *event)
        }
        else if (xiInited && event->type == GenericEvent
                && event->xcookie.type == GenericEvent && event->xcookie.extension == xiOpcode
-               && event->xcookie.evtype == XI_KeyPress && XGetEventData(dpy, &event->xcookie)) {
+               && 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;