Qt 4.8
qkbdlinuxinput_qws.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qkbdlinuxinput_qws.h"
43 
44 #ifndef QT_NO_QWS_KEYBOARD
45 
46 #include <QSocketNotifier>
47 #include <QStringList>
48 
49 #include <qplatformdefs.h>
50 #include <private/qcore_unix_p.h> // overrides QT_OPEN
51 
52 #include <errno.h>
53 #include <termios.h>
54 
55 #include <linux/kd.h>
56 #include <linux/input.h>
57 
59 
60 
62 {
63  Q_OBJECT
64 public:
67 
68 private:
69  void switchLed(int, bool);
70 
71 private Q_SLOTS:
72  void readKeycode();
73 
74 private:
76  int m_fd;
77  int m_tty_fd;
78  struct termios m_tty_attr;
80 };
81 
83  : QWSKeyboardHandler(device)
84 {
85  d = new QWSLinuxInputKbPrivate(this, device);
86 }
87 
89 {
90  delete d;
91 }
92 
94 {
95  return false;
96 }
97 
99  : m_handler(h), m_fd(-1), m_tty_fd(-1), m_orig_kbmode(K_XLATE)
100 {
101  setObjectName(QLatin1String("LinuxInputSubsystem Keyboard Handler"));
102 
103  QString dev = QLatin1String("/dev/input/event1");
104  int repeat_delay = -1;
105  int repeat_rate = -1;
106  int grab = 0;
107 
108  QStringList args = device.split(QLatin1Char(':'));
109  foreach (const QString &arg, args) {
110  if (arg.startsWith(QLatin1String("repeat-delay=")))
111  repeat_delay = arg.mid(13).toInt();
112  else if (arg.startsWith(QLatin1String("repeat-rate=")))
113  repeat_rate = arg.mid(12).toInt();
114  else if (arg.startsWith(QLatin1String("grab=")))
115  grab = arg.mid(5).toInt();
116  else if (arg.startsWith(QLatin1String("/dev/")))
117  dev = arg;
118  }
119 
120  m_fd = QT_OPEN(dev.toLocal8Bit().constData(), O_RDWR, 0);
121  if (m_fd >= 0) {
122  ::ioctl(m_fd, EVIOCGRAB, grab);
123  if (repeat_delay > 0 && repeat_rate > 0) {
124  int kbdrep[2] = { repeat_delay, repeat_rate };
125  ::ioctl(m_fd, EVIOCSREP, kbdrep);
126  }
127 
128  QSocketNotifier *notifier;
129  notifier = new QSocketNotifier(m_fd, QSocketNotifier::Read, this);
130  connect(notifier, SIGNAL(activated(int)), this, SLOT(readKeycode()));
131 
132  // play nice in case we are started from a shell (e.g. for debugging)
133  m_tty_fd = isatty(0) ? 0 : -1;
134 
135  if (m_tty_fd >= 0) {
136  // save tty config for restore.
137  tcgetattr(m_tty_fd, &m_tty_attr);
138 
139  struct ::termios termdata;
140  tcgetattr(m_tty_fd, &termdata);
141 
142  // record the original mode so we can restore it again in the destructor.
143  ::ioctl(m_tty_fd, KDGKBMODE, &m_orig_kbmode);
144 
145  // setting this translation mode is even needed in INPUT mode to prevent
146  // the shell from also interpreting codes, if the process has a tty
147  // attached: e.g. Ctrl+C wouldn't copy, but kill the application.
148  ::ioctl(m_tty_fd, KDSKBMODE, K_MEDIUMRAW);
149 
150  // set the tty layer to pass-through
151  termdata.c_iflag = (IGNPAR | IGNBRK) & (~PARMRK) & (~ISTRIP);
152  termdata.c_oflag = 0;
153  termdata.c_cflag = CREAD | CS8;
154  termdata.c_lflag = 0;
155  termdata.c_cc[VTIME]=0;
156  termdata.c_cc[VMIN]=1;
157  cfsetispeed(&termdata, 9600);
158  cfsetospeed(&termdata, 9600);
159  tcsetattr(m_tty_fd, TCSANOW, &termdata);
160  }
161  } else {
162  qWarning("Cannot open keyboard input device '%s': %s", qPrintable(dev), strerror(errno));
163  return;
164  }
165 }
166 
168 {
169  if (m_tty_fd >= 0) {
170  ::ioctl(m_tty_fd, KDSKBMODE, m_orig_kbmode);
171  tcsetattr(m_tty_fd, TCSANOW, &m_tty_attr);
172  }
173  if (m_fd >= 0)
174  QT_CLOSE(m_fd);
175 }
176 
177 void QWSLinuxInputKbPrivate::switchLed(int led, bool state)
178 {
179  struct ::input_event led_ie;
180  ::gettimeofday(&led_ie.time, 0);
181  led_ie.type = EV_LED;
182  led_ie.code = led;
183  led_ie.value = state;
184 
185  QT_WRITE(m_fd, &led_ie, sizeof(led_ie));
186 }
187 
189 {
190  struct ::input_event buffer[32];
191  int n = 0;
192 
193  forever {
194  n = QT_READ(m_fd, reinterpret_cast<char *>(buffer) + n, sizeof(buffer) - n);
195 
196  if (n == 0) {
197  qWarning("Got EOF from the input device.");
198  return;
199  } else if (n < 0 && (errno != EINTR && errno != EAGAIN)) {
200  qWarning("Could not read from input device: %s", strerror(errno));
201  return;
202  } else if (n % sizeof(buffer[0]) == 0) {
203  break;
204  }
205  }
206 
207  n /= sizeof(buffer[0]);
208 
209  for (int i = 0; i < n; ++i) {
210  if (buffer[i].type != EV_KEY)
211  continue;
212 
213  quint16 code = buffer[i].code;
214  qint32 value = buffer[i].value;
215 
216  if (m_handler->filterInputEvent(code, value))
217  continue;
218 
220  ka = m_handler->processKeycode(code, value != 0, value == 2);
221 
222  switch (ka) {
225  switchLed(LED_CAPSL, ka == QWSKeyboardHandler::CapsLockOn);
226  break;
227 
230  switchLed(LED_NUML, ka == QWSKeyboardHandler::NumLockOn);
231  break;
232 
235  switchLed(LED_SCROLLL, ka == QWSKeyboardHandler::ScrollLockOn);
236  break;
237 
238  default:
239  // ignore console switching and reboot
240  break;
241  }
242  }
243 }
244 
246 
247 #include "qkbdlinuxinput_qws.moc"
248 
249 #endif // QT_NO_QWS_KEYBOARD
The QWSKeyboardHandler class is a base class for keyboard drivers in Qt for Embedded Linux...
Definition: qkbd_qws.h:57
int type
Definition: qmetatype.cpp:239
QWSLinuxInputKeyboardHandler * m_handler
QWSLinuxInputKbPrivate * d
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
int qint32
Definition: qglobal.h:937
int toInt(bool *ok=0, int base=10) const
Returns the string converted to an int using base base, which is 10 by default and must be between 2 ...
Definition: qstring.cpp:6090
#define SLOT(a)
Definition: qobjectdefs.h:226
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
#define Q_SLOTS
Definition: qobjectdefs.h:71
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define QT_READ
Definition: qcore_unix_p.h:280
The QSocketNotifier class provides support for monitoring activity on a file descriptor.
void setObjectName(const QString &name)
Definition: qobject.cpp:1112
#define SIGNAL(a)
Definition: qobjectdefs.h:227
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
unsigned short quint16
Definition: qglobal.h:936
Q_CORE_EXPORT void qWarning(const char *,...)
virtual bool filterInputEvent(quint16 &input_code, qint32 &input_value)
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
KeycodeAction
This enum describes the various special actions that actual QWSKeyboardHandler implementations have t...
Definition: qkbd_qws.h:67
QByteArray toLocal8Bit() const Q_REQUIRED_RESULT
Returns the local 8-bit representation of the string as a QByteArray.
Definition: qstring.cpp:4049
#define Q_OBJECT
Definition: qobjectdefs.h:157
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
#define QT_OPEN
Definition: qcore_unix_p.h:186
QStringList split(const QString &sep, SplitBehavior behavior=KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive) const Q_REQUIRED_RESULT
Splits the string into substrings wherever sep occurs, and returns the list of those strings...
Definition: qstring.cpp:6526
#define QT_WRITE
Definition: qcore_unix_p.h:289
#define qPrintable(string)
Definition: qglobal.h:1750
KeycodeAction processKeycode(quint16 keycode, bool pressed, bool autorepeat)
Maps keycode according to a keymap and sends that key event to the Qt for Embedded Linux server appli...
Definition: qkbd_qws.cpp:521
#define O_RDWR
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
QWSLinuxInputKeyboardHandler(const QString &)
int errno
QWSLinuxInputKbPrivate(QWSLinuxInputKeyboardHandler *, const QString &)
#define QT_CLOSE
Definition: qcore_unix_p.h:304
#define forever
This macro is provided for convenience for writing infinite loops.
Definition: qglobal.h:2452