there are players now :-) every game is better with players
authorConstantin <constantin@exxxtremesys.lu>
Wed, 17 Jul 2013 20:56:28 +0000 (22:56 +0200)
committerConstantin <constantin@exxxtremesys.lu>
Wed, 17 Jul 2013 20:56:28 +0000 (22:56 +0200)
qt/multypo.cpp
qt/multypo.h
qt/multypo.pro
qt/player.cpp [new file with mode: 0644]
qt/player.h [new file with mode: 0644]

index e7a188638008266ef5112d501bfd1d46024cdc27..0c5451ece7c8be2deed7c237b6407a6938b87cfd 100644 (file)
@@ -9,13 +9,6 @@
 #include <X11/extensions/XInput2.h>
 #include <X11/Xutil.h>
 
 #include <X11/extensions/XInput2.h>
 #include <X11/Xutil.h>
 
-static QString colorToString(QColor col)
-{
-       return QString("#%1%2%3").arg(col.red(), 2, 16, QChar('0'))
-                       .arg(col.green(), 2, 16, QChar('0'))
-                       .arg(col.blue(), 2, 16, QChar('0'));
-}
-
 MultypoWindow::MultypoWindow(QWidget *parent) :
        QWidget(parent),
        xiInited(false)
 MultypoWindow::MultypoWindow(QWidget *parent) :
        QWidget(parent),
        xiInited(false)
@@ -42,16 +35,15 @@ void MultypoWindow::handleKeyPress(int device, QString string)
 {
        qDebug() << "Device" << device << "String" << string;
 
 {
        qDebug() << "Device" << device << "String" << string;
 
-       if (string == "Escape")
+       if (string == "Escape") {
                close();
                close();
+               return;
+       }
 
        if (!players.contains(device)) {
 
        if (!players.contains(device)) {
-               QLabel *label = new QLabel(this);
-               layout()->addWidget(label);
-               players[device] = label;
+               players[device] = new Player(this);
        }
        }
-       QColor textColor = QColor(Qt::blue);
-       players[device]->setText(players[device]->text() + "<span style='color:"+colorToString(textColor)+"'>"+string+"</span>");
+       players[device]->handleKey(string);
 }
 
 bool MultypoWindow::handleX11Event(XEvent *event)
 }
 
 bool MultypoWindow::handleX11Event(XEvent *event)
@@ -66,7 +58,7 @@ bool MultypoWindow::handleX11Event(XEvent *event)
                }
 
                /* Which version of XI2? We support 2.0 */
                }
 
                /* 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;
                if (XIQueryVersion(dpy, &major, &minor) == BadRequest) {
                        qCritical() << "XI2 not available. Server supports" << major << "." <<
                                                minor;
@@ -90,7 +82,7 @@ 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;
                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);
 
                }
                XIFreeDeviceInfo(devices);
 
@@ -100,7 +92,13 @@ bool MultypoWindow::handleX11Event(XEvent *event)
        }
        else if (xiInited && event->type == GenericEvent
                && event->xcookie.type == GenericEvent && event->xcookie.extension == xiOpcode
        }
        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;
                /* Handle XI event */
                XIDeviceEvent *d_ev = (XIDeviceEvent*) event->xcookie.data;
            KeyCode keycode = d_ev->detail;
index 49b70b1bdf7d079d5252c97c81743e8245809d92..de0898fd530fa335bbe2804359671cc247d21535 100644 (file)
@@ -4,7 +4,7 @@
 #include <QWidget>
 #include <QMap>
 
 #include <QWidget>
 #include <QMap>
 
-class QLabel;
+#include "player.h"
 
 class MultypoWindow : public QWidget
 {
 
 class MultypoWindow : public QWidget
 {
@@ -24,7 +24,7 @@ private:
        int xiOpcode;
        QLabel *mainLabel;
 
        int xiOpcode;
        QLabel *mainLabel;
 
-       QMap<int, QLabel*> players;
+       QMap<int, Player*> players;
 };
 
 #endif // MULTIKBD_H
 };
 
 #endif // MULTIKBD_H
index 58a81a385bf28db8a136836d585b33ef2e969817..8911a5e4473c043b2631751aeb2debc070446256 100644 (file)
@@ -13,8 +13,10 @@ TEMPLATE = app
 
 
 SOURCES += main.cpp\
 
 
 SOURCES += main.cpp\
-        multypo.cpp
+        multypo.cpp \
+    player.cpp
 
 
-HEADERS  += multypo.h
+HEADERS  += multypo.h \
+    player.h
 
 LIBS += -lX11 -lXi
 
 LIBS += -lX11 -lXi
diff --git a/qt/player.cpp b/qt/player.cpp
new file mode 100644 (file)
index 0000000..4049788
--- /dev/null
@@ -0,0 +1,28 @@
+#include "player.h"
+
+#include <QLayout>
+
+static QString colorToString(QColor col)
+{
+       return QString("#%1%2%3").arg(col.red(), 2, 16, QChar('0'))
+                       .arg(col.green(), 2, 16, QChar('0'))
+                       .arg(col.blue(), 2, 16, QChar('0'));
+}
+
+Player::Player(QWidget* parent) {
+       theLabel = new QLabel (parent);
+       parent->layout()->addWidget(theLabel);
+}
+
+void Player::handleKey(QString str) {
+       if (str.length() == 1) {
+               currentLine += str;
+       } else if (name.isNull() && str == "Return") {
+               // set name
+               name = currentLine;
+               currentLine = "";
+       } else if (str == "BackSpace") {
+               currentLine.chop(1);
+       }
+       theLabel->setText(currentLine);
+}
diff --git a/qt/player.h b/qt/player.h
new file mode 100644 (file)
index 0000000..f725069
--- /dev/null
@@ -0,0 +1,18 @@
+#ifndef PLAYER_H
+#define PLAYER_H
+
+#include <QString>
+#include <QLabel>
+
+class Player
+{
+private:
+       QString name;
+       QString currentLine;
+       QLabel* theLabel;
+public:
+       Player(QWidget* parent);
+       void handleKey(QString);
+};
+
+#endif // PLAYER_H