Add first stub of adapter for sending X keyboard events.
[qzeecontrol] / btconnector.h
index aaeac76..f5a3541 100644 (file)
@@ -1,3 +1,22 @@
+/*
+ *  Copyright 2012 Ruediger Gad
+ *
+ *  This file is part of QZeeControl.
+ *
+ *  QZeeControl is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  QZeeControl is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with QZeeControl.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
 #ifndef BTCONNECTOR_H
 #define BTCONNECTOR_H
 
@@ -9,15 +28,25 @@ QTM_USE_NAMESPACE
 class BtConnector : public QObject
 {
     Q_OBJECT
+
+    Q_PROPERTY(int x READ x NOTIFY xChanged)
+    Q_PROPERTY(int y READ y NOTIFY yChanged)
 public:
-    explicit BtConnector(QObject *parent = 0){qDebug("BtConnector Constructor");}
-    ~BtConnector(){if(socket != NULL) delete socket;}
+    explicit BtConnector(QObject *parent = 0){
+        _x = 0;
+        _y = 0;
+    }
+    ~BtConnector(){
+        if(socket)
+            delete socket;
+    }
 
     Q_INVOKABLE void connect(QString address, int port){
         qDebug("Trying to connect to: %s--%d", address.toUtf8().constData(), port);
 
-        if(socket != NULL)
+        if(socket)
             delete socket;
+
         socket = new QBluetoothSocket(QBluetoothSocket::RfcommSocket);
         QObject::connect(socket, SIGNAL(connected()), this, SIGNAL(connected()));
         QObject::connect(socket, SIGNAL(disconnected()), this, SIGNAL(disconnected()));
@@ -30,15 +59,19 @@ public:
         QObject::connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
     }
 
+    int x(){return _x;}
+    int y(){return _y;}
+
 public slots:
     void disconnect(){
-        if(socket == NULL)
+        if(!socket)
             return;
 
         if(socket->isOpen())
             socket->close();
 
         delete socket;
+        socket = 0;
     }
 
 signals:
@@ -47,21 +80,63 @@ signals:
     void error(QBluetoothSocket::SocketError errorCode);
 
     void stickMoved(int x, int y);
+    void buttonsChanged(bool a, bool b, bool c, bool d);
+
+    void xChanged(int);
+    void yChanged(int);
 
 private slots:
     void readData(){
-        qDebug("readData...");
+//        qDebug("readData...");
         QByteArray data = socket->readAll();
-        qDebug("read %d bytes.", data.size());
+//        qDebug("read %d bytes.", data.size());
 
+/*
         for(int i=0; i < data.size(); i++){
             qDebug("%d: %d", i, ((signed char)data.at(i)));
         }
+*/
+
+        /*
+         * Actually it seems like that the first three bytes are used for
+         * identifying the "type" of data sent. However, for now using the
+         * first seems to suffice.
+         */
+        if(data.at(0) == 5){
+            // Joystick movement
+            _x = (int)(signed char) data.at(4);
+            _y = (int)(signed char) data.at(5);
+
+            emit(xChanged(_x));
+            emit(yChanged(_y));
+            emit(stickMoved(_x, _y));
+        }else if(data.at(0) == 8){
+            // Button press
+            /*
+             * A -> 0, B -> 1, C -> 2, D ->3
+             * At index 3 to 6 (inclusive)
+             */
+
+            char buttonMap = 0;
+
+            for(int i = 3; i <= 6; i++){
+                for(int b = 0; b <= 3; b++){
+                    if(data.at(i) == b){
+                        buttonMap ^= (1 << b);
+                    }
+                }
+            }
+
+//            qDebug("Button map: %d", buttonMap);
+            emit(buttonsChanged(buttonMap & 0x01, buttonMap & 0x02, buttonMap & 0x04, buttonMap & 0x08));
+        }
     }
 
 private:
     QBluetoothSocket *socket;
 
+    int _x;
+    int _y;
 };
 
 #endif // BTCONNECTOR_H