src/gui/embedded/qkbdum_qws.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 QtGui module 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 "qkbdum_qws.h"
       
    43 #include "qvfbhdr.h"
       
    44 
       
    45 #if !defined(QT_NO_QWS_KEYBOARD) && !defined(QT_NO_QWS_KBD_UM)
       
    46 
       
    47 #include <stdlib.h>
       
    48 #include <sys/types.h>
       
    49 #include <sys/stat.h>
       
    50 #include <unistd.h>
       
    51 #include <fcntl.h>
       
    52 #include <errno.h>
       
    53 
       
    54 #include <qstring.h>
       
    55 #include <qwindowsystem_qws.h>
       
    56 #include <qsocketnotifier.h>
       
    57 #include "qplatformdefs.h"
       
    58 
       
    59 QT_BEGIN_NAMESPACE
       
    60 
       
    61 class QWSUmKeyboardHandlerPrivate : public QObject
       
    62 {
       
    63     Q_OBJECT
       
    64 
       
    65 public:
       
    66     QWSUmKeyboardHandlerPrivate(const QString&);
       
    67     ~QWSUmKeyboardHandlerPrivate();
       
    68 
       
    69 private slots:
       
    70     void readKeyboardData();
       
    71 
       
    72 private:
       
    73     int kbdFD;
       
    74     int kbdIdx;
       
    75     const int kbdBufferLen;
       
    76     unsigned char *kbdBuffer;
       
    77     QSocketNotifier *notifier;
       
    78 };
       
    79 
       
    80 QWSUmKeyboardHandlerPrivate::QWSUmKeyboardHandlerPrivate(const QString &device)
       
    81     : kbdFD(-1), kbdIdx(0), kbdBufferLen(sizeof(QVFbKeyData)*5)
       
    82 {
       
    83     kbdBuffer = new unsigned char [kbdBufferLen];
       
    84 
       
    85     if ((kbdFD = QT_OPEN((const char *)device.toLocal8Bit(), O_RDONLY | O_NDELAY, 0)) < 0) {
       
    86         qDebug("Cannot open %s (%s)", (const char *)device.toLocal8Bit(),
       
    87         strerror(errno));
       
    88     } else {
       
    89         // Clear pending input
       
    90         char buf[2];
       
    91         while (QT_READ(kbdFD, buf, 1) > 0) { }
       
    92 
       
    93         notifier = new QSocketNotifier(kbdFD, QSocketNotifier::Read, this);
       
    94         connect(notifier, SIGNAL(activated(int)),this, SLOT(readKeyboardData()));
       
    95     }
       
    96 }
       
    97 
       
    98 QWSUmKeyboardHandlerPrivate::~QWSUmKeyboardHandlerPrivate()
       
    99 {
       
   100     if (kbdFD >= 0)
       
   101         QT_CLOSE(kbdFD);
       
   102     delete [] kbdBuffer;
       
   103 }
       
   104 
       
   105 
       
   106 void QWSUmKeyboardHandlerPrivate::readKeyboardData()
       
   107 {
       
   108     int n;
       
   109     do {
       
   110         n  = QT_READ(kbdFD, kbdBuffer+kbdIdx, kbdBufferLen - kbdIdx);
       
   111         if (n > 0)
       
   112             kbdIdx += n;
       
   113     } while (n > 0);
       
   114 
       
   115     int idx = 0;
       
   116     while (kbdIdx - idx >= (int)sizeof(QVFbKeyData)) {
       
   117         QVFbKeyData *kd = (QVFbKeyData *)(kbdBuffer + idx);
       
   118         // Qtopia Key filters must still work.
       
   119         QWSServer::processKeyEvent(kd->unicode, kd->keycode, kd->modifiers, kd->press, kd->repeat);
       
   120         idx += sizeof(QVFbKeyData);
       
   121     }
       
   122 
       
   123     int surplus = kbdIdx - idx;
       
   124     for (int i = 0; i < surplus; i++)
       
   125         kbdBuffer[i] = kbdBuffer[idx+i];
       
   126     kbdIdx = surplus;
       
   127 }
       
   128 
       
   129 QWSUmKeyboardHandler::QWSUmKeyboardHandler(const QString &device)
       
   130     : QWSKeyboardHandler()
       
   131 {
       
   132     d = new QWSUmKeyboardHandlerPrivate(device);
       
   133 }
       
   134 
       
   135 QWSUmKeyboardHandler::~QWSUmKeyboardHandler()
       
   136 {
       
   137     delete d;
       
   138 }
       
   139 
       
   140 QT_END_NAMESPACE
       
   141 
       
   142 #include "qkbdum_qws.moc"
       
   143 
       
   144 #endif // QT_NO_QWS_KEYBOARD && QT_NO_QWS_KBD_UM