tools/qvfb/qvfbprotocol.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the tools applications of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qvfbprotocol.h"
       
    43 #include "qvfbhdr.h"
       
    44 
       
    45 #include <QDebug>
       
    46 #include <QTimer>
       
    47 
       
    48 #include <sys/types.h>
       
    49 #include <sys/stat.h>
       
    50 #include <math.h>
       
    51 #include <fcntl.h>
       
    52 #include <stdlib.h>
       
    53 #include <stdio.h>
       
    54 
       
    55 #ifdef Q_OS_UNIX
       
    56 #include <unistd.h>
       
    57 #endif
       
    58 
       
    59 #include "qvfbshmem.h"
       
    60 
       
    61 QT_BEGIN_NAMESPACE
       
    62 
       
    63 QVFbViewProtocol::QVFbViewProtocol(int display_id, QObject *parent) :
       
    64     QObject(parent), mDisplayId(display_id) { }
       
    65 
       
    66 QVFbViewProtocol::~QVFbViewProtocol() {}
       
    67 
       
    68 void QVFbViewProtocol::flushChanges() {}
       
    69 
       
    70 void QVFbViewProtocol::sendKeyboardData(QString unicode, int keycode,
       
    71                                         int modifiers, bool press, bool repeat)
       
    72 {
       
    73     if (keyHandler())
       
    74         keyHandler()->sendKeyboardData(unicode, keycode, modifiers, press, repeat);
       
    75 }
       
    76 
       
    77 void QVFbViewProtocol::sendMouseData(const QPoint &pos, int buttons, int wheel)
       
    78 {
       
    79     if (mouseHandler())
       
    80         mouseHandler()->sendMouseData(pos, buttons, wheel);
       
    81 }
       
    82 
       
    83 static int openPipe(const char *fileName)
       
    84 {
       
    85     unlink(fileName);
       
    86 
       
    87     mkfifo(fileName, 0666);
       
    88     int fd = ::open(fileName, O_RDWR | O_NDELAY);
       
    89     return fd;
       
    90 }
       
    91 
       
    92 QVFbKeyPipeProtocol::QVFbKeyPipeProtocol(int display_id)
       
    93     : QVFbKeyProtocol(display_id)
       
    94 {
       
    95     fileName = QT_VFB_KEYBOARD_PIPE(display_id);
       
    96     fd = openPipe(fileName.toLocal8Bit().constData());
       
    97 
       
    98     if (fd == -1)
       
    99 	qFatal("Cannot open keyboard pipe %s", fileName.toLocal8Bit().data());
       
   100 }
       
   101 
       
   102 QVFbKeyPipeProtocol::~QVFbKeyPipeProtocol()
       
   103 {
       
   104     sendKeyboardData(0, 0, 0, true, false); // magic die key
       
   105     ::close(fd);
       
   106     unlink(fileName.toLocal8Bit().constData());
       
   107 }
       
   108 
       
   109 void QVFbKeyPipeProtocol::sendKeyboardData(QString unicode, int keycode,
       
   110                                            int modifiers, bool press, bool repeat)
       
   111 {
       
   112     QVFbKeyData kd;
       
   113     kd.unicode = unicode[0].unicode();
       
   114     kd.keycode = keycode;
       
   115     kd.modifiers = static_cast<Qt::KeyboardModifier>(modifiers);
       
   116     kd.press = press;
       
   117     kd.repeat = repeat;
       
   118     write(fd, &kd, sizeof(QVFbKeyData));
       
   119 }
       
   120 
       
   121 QVFbMousePipe::QVFbMousePipe(int display_id)
       
   122     : QVFbMouseProtocol(display_id)
       
   123 {
       
   124     fileName = QT_VFB_MOUSE_PIPE(display_id);
       
   125     fd = openPipe(fileName.toLocal8Bit().constData());
       
   126 
       
   127     if (fd == -1)
       
   128 	qFatal("Cannot open mouse pipe %s", fileName.toLocal8Bit().data());
       
   129 }
       
   130 
       
   131 QVFbMousePipe::~QVFbMousePipe()
       
   132 {
       
   133     ::close(fd);
       
   134     unlink(fileName.toLocal8Bit().constData());
       
   135 }
       
   136 
       
   137 
       
   138 void QVFbMousePipe::sendMouseData(const QPoint &pos, int buttons, int wheel)
       
   139 {
       
   140     write(fd, &pos, sizeof(QPoint));
       
   141     write(fd, &buttons, sizeof(int));
       
   142     write(fd, &wheel, sizeof(int));
       
   143 }
       
   144 
       
   145 QVFbMouseLinuxTP::QVFbMouseLinuxTP(int display_id)
       
   146     : QObject(), QVFbMousePipe(display_id), lastPos(-1,-1)
       
   147 {
       
   148     /* the timer is needed because a real touch screen send data as long as
       
   149        there is pressure.  And the linux tp driver will filter, requiring
       
   150        a minimum of 5 samples before it even registers a press.
       
   151     */
       
   152     repeater = new QTimer(this);
       
   153     connect(repeater, SIGNAL(timeout()), this, SLOT(repeatLastPress()));
       
   154 }
       
   155 
       
   156 QVFbMouseLinuxTP::~QVFbMouseLinuxTP()
       
   157 {
       
   158 }
       
   159 
       
   160 
       
   161 void QVFbMouseLinuxTP::sendMouseData(const QPoint &pos, int buttons, int)
       
   162 {
       
   163     if (buttons & Qt::LeftButton) {
       
   164         // press
       
   165         repeater->start(5);
       
   166         writeToPipe(pos, 1);
       
   167         lastPos = pos;
       
   168     } else {
       
   169         // release
       
   170         if (lastPos == QPoint(-1,-1))
       
   171             return; /* only send one release */
       
   172         repeater->stop();
       
   173         writeToPipe(pos, 0);
       
   174         lastPos = QPoint(-1,-1);
       
   175     }
       
   176 }
       
   177 
       
   178 void QVFbMouseLinuxTP::writeToPipe(const QPoint &pos, ushort pressure)
       
   179 {
       
   180     ushort v;
       
   181     write(fd, &pressure, sizeof(ushort));
       
   182     v = pos.x();
       
   183     write(fd, &v, sizeof(ushort));
       
   184     v = pos.y();
       
   185     write(fd, &v, sizeof(ushort));
       
   186     v = 1; // pad
       
   187     write(fd, &v, sizeof(ushort));
       
   188 }
       
   189 
       
   190 void QVFbMouseLinuxTP::repeatLastPress()
       
   191 {
       
   192     writeToPipe(lastPos, 1);
       
   193 }
       
   194 
       
   195 QT_END_NAMESPACE