Change output color per label
[multypo.git] / qt / multypo.cpp
index a5d81375048e331c225eeb45add07f89dfc4fab7..e7a188638008266ef5112d501bfd1d46024cdc27 100644 (file)
 
 #include <QX11Info>
 #include <QDebug>
-#include <QHBoxLayout>
+#include <QVBoxLayout>
 #include <QLabel>
 
 #include <X11/Xlib.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)
 {
-       QHBoxLayout *layout = new QHBoxLayout(this);
-       setLayout(layout);
+       /* Prepare colors */
+       setStyleSheet("background-color: black; color: green; font-size: 30pt");
+
+       /* Prepare conents */
+       setLayout(new QVBoxLayout(this));
 
-       mainLabel = new QLabel(this);
-       layout->addWidget(mainLabel);
+       mainLabel = new QLabel("Hit <Esc> to quit", this);
+       layout()->addWidget(mainLabel);
 
-       setMinimumSize(300, 300);
+       /* Fullscreen, no cursor */
+       setWindowState(Qt::WindowFullScreen);
+       setCursor(QCursor(Qt::BlankCursor));
 }
 
 MultypoWindow::~MultypoWindow()
 {
 }
 
-void MultypoWindow::showEvent ( QShowEvent * )
+void MultypoWindow::handleKeyPress(int device, QString string)
 {
-       if (xiInited) return;
-
-       Display *dpy = x11Info().display();
+       qDebug() << "Device" << device << "String" << string;
 
-       /* XInput Extension available? */
-       int eventID, errorID;
-       if (!XQueryExtension(dpy, "XInputExtension", &xiOpcode, &eventID, &errorID)) {
-          qCritical() << "X Input extension not available";
-       }
+       if (string == "Escape")
+               close();
 
-       /* 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;
+       if (!players.contains(device)) {
+               QLabel *label = new QLabel(this);
+               layout()->addWidget(label);
+               players[device] = label;
        }
-
-       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;
-}
-
-void MultypoWindow::handleKeyPress(int device, QString string)
-{
-       qDebug() << "Device" << device << "String" << string;
-       mainLabel->setText(QString("%1 pressed on %2").arg(string).arg(device));
+       QColor textColor = QColor(Qt::blue);
+       players[device]->setText(players[device]->text() + "<span style='color:"+colorToString(textColor)+"'>"+string+"</span>");
 }
 
 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 = 0;
+               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 && XGetEventData(dpy, &event->xcookie)) {
+               /* Handle XI event */
                XIDeviceEvent *d_ev = (XIDeviceEvent*) event->xcookie.data;
            KeyCode keycode = d_ev->detail;
            int kbdid = d_ev->deviceid;
@@ -88,6 +116,7 @@ bool MultypoWindow::handleX11Event(XEvent *event)
 
                return true;
        }
-
-       return false;
+       else {
+               return false;
+       }
 }