src/gui/embedded/qkbdlinuxinput_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 "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 
       
    58 QT_BEGIN_NAMESPACE
       
    59 
       
    60 
       
    61 class QWSLinuxInputKbPrivate : public QObject
       
    62 {
       
    63     Q_OBJECT
       
    64 public:
       
    65     QWSLinuxInputKbPrivate(QWSLinuxInputKeyboardHandler *, const QString &);
       
    66     ~QWSLinuxInputKbPrivate();
       
    67 
       
    68 private:
       
    69     void switchLed(int, bool);
       
    70 
       
    71 private Q_SLOTS:
       
    72     void readKeycode();
       
    73 
       
    74 private:
       
    75     QWSLinuxInputKeyboardHandler *m_handler;
       
    76     int                           m_fd;
       
    77     int                           m_tty_fd;
       
    78     struct termios                m_tty_attr;
       
    79     int                           m_orig_kbmode;
       
    80 };
       
    81 
       
    82 QWSLinuxInputKeyboardHandler::QWSLinuxInputKeyboardHandler(const QString &device)
       
    83     : QWSKeyboardHandler(device)
       
    84 {
       
    85     d = new QWSLinuxInputKbPrivate(this, device);
       
    86 }
       
    87 
       
    88 QWSLinuxInputKeyboardHandler::~QWSLinuxInputKeyboardHandler()
       
    89 {
       
    90     delete d;
       
    91 }
       
    92 
       
    93 bool QWSLinuxInputKeyboardHandler::filterInputEvent(quint16 &, qint32 &)
       
    94 {
       
    95     return false;
       
    96 }
       
    97 
       
    98 QWSLinuxInputKbPrivate::QWSLinuxInputKbPrivate(QWSLinuxInputKeyboardHandler *h, const QString &device)
       
    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 
       
   107     QStringList args = device.split(QLatin1Char(':'));
       
   108     foreach (const QString &arg, args) {
       
   109         if (arg.startsWith(QLatin1String("repeat-delay=")))
       
   110             repeat_delay = arg.mid(13).toInt();
       
   111         else if (arg.startsWith(QLatin1String("repeat-rate=")))
       
   112             repeat_rate = arg.mid(12).toInt();
       
   113         else if (arg.startsWith(QLatin1String("/dev/")))
       
   114             dev = arg;
       
   115     }
       
   116 
       
   117     m_fd = QT_OPEN(dev.toLocal8Bit().constData(), O_RDWR, 0);
       
   118     if (m_fd >= 0) {
       
   119         if (repeat_delay > 0 && repeat_rate > 0) {
       
   120             int kbdrep[2] = { repeat_delay, repeat_rate };
       
   121             ::ioctl(m_fd, EVIOCSREP, kbdrep);
       
   122         }
       
   123 
       
   124         QSocketNotifier *notifier;
       
   125         notifier = new QSocketNotifier(m_fd, QSocketNotifier::Read, this);
       
   126         connect(notifier, SIGNAL(activated(int)), this, SLOT(readKeycode()));
       
   127 
       
   128         // play nice in case we are started from a shell (e.g. for debugging)
       
   129         m_tty_fd = isatty(0) ? 0 : -1;
       
   130 
       
   131         if (m_tty_fd >= 0) {
       
   132             // save tty config for restore.
       
   133             tcgetattr(m_tty_fd, &m_tty_attr);
       
   134 
       
   135             struct ::termios termdata;
       
   136             tcgetattr(m_tty_fd, &termdata);
       
   137 
       
   138             // record the original mode so we can restore it again in the destructor.
       
   139             ::ioctl(m_tty_fd, KDGKBMODE, &m_orig_kbmode);
       
   140 
       
   141             // setting this tranlation mode is even needed in INPUT mode to prevent
       
   142             // the shell from also interpreting codes, if the process has a tty
       
   143             // attached: e.g. Ctrl+C wouldn't copy, but kill the application.
       
   144             ::ioctl(m_tty_fd, KDSKBMODE, K_MEDIUMRAW);
       
   145 
       
   146             // set the tty layer to pass-through
       
   147             termdata.c_iflag = (IGNPAR | IGNBRK) & (~PARMRK) & (~ISTRIP);
       
   148             termdata.c_oflag = 0;
       
   149             termdata.c_cflag = CREAD | CS8;
       
   150             termdata.c_lflag = 0;
       
   151             termdata.c_cc[VTIME]=0;
       
   152             termdata.c_cc[VMIN]=1;
       
   153             cfsetispeed(&termdata, 9600);
       
   154             cfsetospeed(&termdata, 9600);
       
   155             tcsetattr(m_tty_fd, TCSANOW, &termdata);
       
   156         }
       
   157     } else {
       
   158         qWarning("Cannot open keyboard input device '%s': %s", qPrintable(dev), strerror(errno));
       
   159         return;
       
   160     }
       
   161 }
       
   162 
       
   163 QWSLinuxInputKbPrivate::~QWSLinuxInputKbPrivate()
       
   164 {
       
   165     if (m_tty_fd >= 0) {
       
   166         ::ioctl(m_tty_fd, KDSKBMODE, m_orig_kbmode);
       
   167         tcsetattr(m_tty_fd, TCSANOW, &m_tty_attr);
       
   168     }
       
   169     if (m_fd >= 0)
       
   170         QT_CLOSE(m_fd);
       
   171 }
       
   172 
       
   173 void QWSLinuxInputKbPrivate::switchLed(int led, bool state)
       
   174 {
       
   175     struct ::input_event led_ie;
       
   176     ::gettimeofday(&led_ie.time, 0);
       
   177     led_ie.type = EV_LED;
       
   178     led_ie.code = led;
       
   179     led_ie.value = state;
       
   180 
       
   181     QT_WRITE(m_fd, &led_ie, sizeof(led_ie));
       
   182 }
       
   183 
       
   184 void QWSLinuxInputKbPrivate::readKeycode()
       
   185 {
       
   186     struct ::input_event buffer[32];
       
   187     int n = 0;
       
   188 
       
   189     forever {
       
   190         n = QT_READ(m_fd, reinterpret_cast<char *>(buffer) + n, sizeof(buffer) - n);
       
   191 
       
   192         if (n == 0) {
       
   193             qWarning("Got EOF from the input device.");
       
   194             return;
       
   195         } else if (n < 0 && (errno != EINTR && errno != EAGAIN)) {
       
   196             qWarning("Could not read from input device: %s", strerror(errno));
       
   197             return;
       
   198         } else if (n % sizeof(buffer[0]) == 0) {
       
   199             break;
       
   200         }
       
   201     }
       
   202 
       
   203     n /= sizeof(buffer[0]);
       
   204 
       
   205     for (int i = 0; i < n; ++i) {
       
   206         if (buffer[i].type != EV_KEY)
       
   207             continue;
       
   208 
       
   209         quint16 code = buffer[i].code;
       
   210         qint32 value = buffer[i].value;
       
   211 
       
   212         if (m_handler->filterInputEvent(code, value))
       
   213             continue;
       
   214 
       
   215         QWSKeyboardHandler::KeycodeAction ka;
       
   216         ka = m_handler->processKeycode(code, value != 0, value == 2);
       
   217 
       
   218         switch (ka) {
       
   219         case QWSKeyboardHandler::CapsLockOn:
       
   220         case QWSKeyboardHandler::CapsLockOff:
       
   221             switchLed(LED_CAPSL, ka == QWSKeyboardHandler::CapsLockOn);
       
   222             break;
       
   223 
       
   224         case QWSKeyboardHandler::NumLockOn:
       
   225         case QWSKeyboardHandler::NumLockOff:
       
   226             switchLed(LED_NUML, ka == QWSKeyboardHandler::NumLockOn);
       
   227             break;
       
   228 
       
   229         case QWSKeyboardHandler::ScrollLockOn:
       
   230         case QWSKeyboardHandler::ScrollLockOff:
       
   231             switchLed(LED_SCROLLL, ka == QWSKeyboardHandler::ScrollLockOn);
       
   232             break;
       
   233 
       
   234         default:
       
   235             // ignore console switching and reboot
       
   236             break;
       
   237         }
       
   238     }
       
   239 }
       
   240 
       
   241 QT_END_NAMESPACE
       
   242 
       
   243 #include "qkbdlinuxinput_qws.moc"
       
   244 
       
   245 #endif // QT_NO_QWS_KEYBOARD