1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qkbdum_qws.h"
43 
44 #if !defined(QT_NO_QWS_KEYBOARD) && !defined(QT_NO_QWS_KBD_UM)
45 
46 #include <stdlib.h>
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <unistd.h>
50 #include <fcntl.h>
51 #include <errno.h>
52 
53 #include <qstring.h>
54 #include <qwindowsystem_qws.h>
55 #include <qsocketnotifier.h>
56 #include "qplatformdefs.h"
57 #include "qvfbhdr.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 
QWSUmKeyboardHandlerPrivate(const QString & device)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 
~QWSUmKeyboardHandlerPrivate()98 QWSUmKeyboardHandlerPrivate::~QWSUmKeyboardHandlerPrivate()
99 {
100     if (kbdFD >= 0)
101         QT_CLOSE(kbdFD);
102     delete [] kbdBuffer;
103 }
104 
105 
readKeyboardData()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 
QWSUmKeyboardHandler(const QString & device)129 QWSUmKeyboardHandler::QWSUmKeyboardHandler(const QString &device)
130     : QWSKeyboardHandler()
131 {
132     d = new QWSUmKeyboardHandlerPrivate(device);
133 }
134 
~QWSUmKeyboardHandler()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
145