Qt 4.8
Public Functions | Private Slots | Private Functions | Properties | List of all members
QLinuxInputKeyboardHandler Class Reference

#include <qlinuxinput.h>

Inheritance diagram for QLinuxInputKeyboardHandler:
QObject

Public Functions

 QLinuxInputKeyboardHandler (const QString &key, const QString &specification)
 
 ~QLinuxInputKeyboardHandler ()
 
- Public Functions inherited from QObject
bool blockSignals (bool b)
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). More...
 
const QObjectListchildren () const
 Returns a list of child objects. More...
 
bool connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0)
 
bool disconnect (const QObject *receiver, const char *member=0)
 
void dumpObjectInfo ()
 Dumps information about signal connections, etc. More...
 
void dumpObjectTree ()
 Dumps a tree of children to the debug output. More...
 
QList< QByteArraydynamicPropertyNames () const
 Returns the names of all properties that were dynamically added to the object using setProperty(). More...
 
virtual bool event (QEvent *)
 This virtual function receives events to an object and should return true if the event e was recognized and processed. More...
 
virtual bool eventFilter (QObject *, QEvent *)
 Filters events if this object has been installed as an event filter for the watched object. More...
 
template<typename T >
findChild (const QString &aName=QString()) const
 Returns the child of this object that can be cast into type T and that is called name, or 0 if there is no such object. More...
 
template<typename T >
QList< T > findChildren (const QString &aName=QString()) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects. More...
 
template<typename T >
QList< T > findChildren (const QRegExp &re) const
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false. More...
 
void installEventFilter (QObject *)
 Installs an event filter filterObj on this object. More...
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false. More...
 
void killTimer (int id)
 Kills the timer with timer identifier, id. More...
 
virtual const QMetaObjectmetaObject () const
 Returns a pointer to the meta-object of this object. More...
 
void moveToThread (QThread *thread)
 Changes the thread affinity for this object and its children. More...
 
QString objectName () const
 
QObjectparent () const
 Returns a pointer to the parent object. More...
 
QVariant property (const char *name) const
 Returns the value of the object's name property. More...
 
Q_INVOKABLE QObject (QObject *parent=0)
 Constructs an object with parent object parent. More...
 
void removeEventFilter (QObject *)
 Removes an event filter object obj from this object. More...
 
void setObjectName (const QString &name)
 
void setParent (QObject *)
 Makes the object a child of parent. More...
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value. More...
 
void setUserData (uint id, QObjectUserData *data)
 
bool signalsBlocked () const
 Returns true if signals are blocked; otherwise returns false. More...
 
int startTimer (int interval)
 Starts a timer and returns a timer identifier, or returns zero if it could not start a timer. More...
 
QThreadthread () const
 Returns the thread in which the object lives. More...
 
QObjectUserDatauserData (uint id) const
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects. More...
 

Private Slots

void readKeycode ()
 

Private Functions

void switchLed (int, bool)
 

Properties

int m_fd
 
QWSLinuxInputKeyboardHandlerm_handler
 
int m_orig_kbmode
 
struct termios m_tty_attr
 
int m_tty_fd
 

Additional Inherited Members

- Public Slots inherited from QObject
void deleteLater ()
 Schedules this object for deletion. More...
 
- Signals inherited from QObject
void destroyed (QObject *=0)
 This signal is emitted immediately before the object obj is destroyed, and can not be blocked. More...
 
- Static Public Functions inherited from QObject
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 receiver object. More...
 
static bool connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 Disconnects signal in object sender from method in object receiver. More...
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static uint registerUserData ()
 
static QString tr (const char *sourceText, const char *comment=0, int n=-1)
 
static QString trUtf8 (const char *sourceText, const char *comment=0, int n=-1)
 
- Static Public Variables inherited from QObject
static const QMetaObject staticMetaObject
 This variable stores the meta-object for the class. More...
 
- Protected Functions inherited from QObject
virtual void childEvent (QChildEvent *)
 This event handler can be reimplemented in a subclass to receive child events. More...
 
virtual void connectNotify (const char *signal)
 This virtual function is called when something has been connected to signal in this object. More...
 
virtual void customEvent (QEvent *)
 This event handler can be reimplemented in a subclass to receive custom events. More...
 
virtual void disconnectNotify (const char *signal)
 This virtual function is called when something has been disconnected from signal in this object. More...
 
 QObject (QObjectPrivate &dd, QObject *parent=0)
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal. More...
 
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns 0. More...
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *)
 This event handler can be reimplemented in a subclass to receive timer events for the object. More...
 
- Protected Variables inherited from QObject
QScopedPointer< QObjectDatad_ptr
 
- Static Protected Variables inherited from QObject
static const QMetaObject staticQtMetaObject
 

Detailed Description

Definition at line 85 of file qlinuxinput.h.

Constructors and Destructors

◆ QLinuxInputKeyboardHandler()

QLinuxInputKeyboardHandler::QLinuxInputKeyboardHandler ( const QString key,
const QString specification 
)

Definition at line 398 of file qlinuxinput.cpp.

399  : m_handler(0), m_fd(-1), m_tty_fd(-1), m_orig_kbmode(K_XLATE)
400 {
401  setObjectName(QLatin1String("LinuxInputSubsystem Keyboard Handler"));
402 
403  QString dev = QLatin1String("/dev/input/event1");
404  int repeat_delay = -1;
405  int repeat_rate = -1;
406 
407  bool ttymode = false;
408 
409  QStringList args = specification.split(QLatin1Char(':'));
410  foreach (const QString &arg, args) {
411  if (arg.startsWith(QLatin1String("repeat-delay=")))
412  repeat_delay = arg.mid(13).toInt();
413  else if (arg.startsWith(QLatin1String("repeat-rate=")))
414  repeat_rate = arg.mid(12).toInt();
415  else if (arg.startsWith(QLatin1String("ttymode")))
416  ttymode = true;
417  else if (arg.startsWith(QLatin1String("/dev/")))
418  dev = arg;
419  }
420 
421  m_handler = new QWSLinuxInputKeyboardHandler(dev); //This is a hack to avoid copying all the QWS code
422 
423  m_fd = QT_OPEN(dev.toLocal8Bit().constData(), O_RDWR, 0);
424  if (m_fd >= 0) {
425  if (repeat_delay > 0 && repeat_rate > 0) {
426  int kbdrep[2] = { repeat_delay, repeat_rate };
427  ::ioctl(m_fd, EVIOCSREP, kbdrep);
428  }
429 
430  QSocketNotifier *notifier;
431  notifier = new QSocketNotifier(m_fd, QSocketNotifier::Read, this);
432  connect(notifier, SIGNAL(activated(int)), this, SLOT(readKeycode()));
433 
434  if (ttymode) {
435  // play nice in case we are started from a shell (e.g. for debugging)
436  m_tty_fd = isatty(0) ? 0 : -1;
437 
438  if (m_tty_fd >= 0) {
439  // save tty config for restore.
440  tcgetattr(m_tty_fd, &m_tty_attr);
441 
442  struct ::termios termdata;
443  tcgetattr(m_tty_fd, &termdata);
444 
445  // record the original mode so we can restore it again in the destructor.
446  ::ioctl(m_tty_fd, KDGKBMODE, &m_orig_kbmode);
447 
448  // setting this translation mode is even needed in INPUT mode to prevent
449  // the shell from also interpreting codes, if the process has a tty
450  // attached: e.g. Ctrl+C wouldn't copy, but kill the application.
451  ::ioctl(m_tty_fd, KDSKBMODE, K_MEDIUMRAW);
452 
453  // set the tty layer to pass-through
454  termdata.c_iflag = (IGNPAR | IGNBRK) & (~PARMRK) & (~ISTRIP);
455  termdata.c_oflag = 0;
456  termdata.c_cflag = CREAD | CS8;
457  termdata.c_lflag = 0;
458  termdata.c_cc[VTIME]=0;
459  termdata.c_cc[VMIN]=1;
460  cfsetispeed(&termdata, 9600);
461  cfsetospeed(&termdata, 9600);
462  tcsetattr(m_tty_fd, TCSANOW, &termdata);
463  }
464  }
465  } else {
466  qWarning("Cannot open keyboard input device '%s': %s", qPrintable(dev), strerror(errno));
467  return;
468  }
469 }
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
struct termios m_tty_attr
Definition: qlinuxinput.h:103
#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
The QString class provides a Unicode character string.
Definition: qstring.h:83
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
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
Q_CORE_EXPORT void qWarning(const char *,...)
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
QWSLinuxInputKeyboardHandler * m_handler
Definition: qlinuxinput.h:100
QByteArray toLocal8Bit() const Q_REQUIRED_RESULT
Returns the local 8-bit representation of the string as a QByteArray.
Definition: qstring.cpp:4049
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 qPrintable(string)
Definition: qglobal.h:1750
#define O_RDWR
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
int errno

◆ ~QLinuxInputKeyboardHandler()

QLinuxInputKeyboardHandler::~QLinuxInputKeyboardHandler ( )

Definition at line 471 of file qlinuxinput.cpp.

472 {
473  if (m_tty_fd >= 0) {
474  ::ioctl(m_tty_fd, KDSKBMODE, m_orig_kbmode);
475  tcsetattr(m_tty_fd, TCSANOW, &m_tty_attr);
476  }
477  if (m_fd >= 0)
478  QT_CLOSE(m_fd);
479  delete m_handler;
480 }
struct termios m_tty_attr
Definition: qlinuxinput.h:103
QWSLinuxInputKeyboardHandler * m_handler
Definition: qlinuxinput.h:100
#define QT_CLOSE
Definition: qcore_unix_p.h:304

Functions

◆ readKeycode

void QLinuxInputKeyboardHandler::readKeycode ( )
privateslot

Definition at line 495 of file qlinuxinput.cpp.

Referenced by QLinuxInputKeyboardHandler().

496 {
497  struct ::input_event buffer[32];
498  int n = 0;
499 
500  forever {
501  n = QT_READ(m_fd, reinterpret_cast<char *>(buffer) + n, sizeof(buffer) - n);
502 
503  if (n == 0) {
504  qWarning("Got EOF from the input device.");
505  return;
506  } else if (n < 0 && (errno != EINTR && errno != EAGAIN)) {
507  qWarning("Could not read from input device: %s", strerror(errno));
508  return;
509  } else if (n % sizeof(buffer[0]) == 0) {
510  break;
511  }
512  }
513 
514  n /= sizeof(buffer[0]);
515 
516  for (int i = 0; i < n; ++i) {
517  if (buffer[i].type != EV_KEY)
518  continue;
519 
520  quint16 code = buffer[i].code;
521  qint32 value = buffer[i].value;
522 
523  if (m_handler->filterInputEvent(code, value))
524  continue;
525 
527  ka = m_handler->processKeycode(code, value != 0, value == 2);
528 
529  switch (ka) {
532  switchLed(LED_CAPSL, ka == QWSKeyboardHandler::CapsLockOn);
533  break;
534 
537  switchLed(LED_NUML, ka == QWSKeyboardHandler::NumLockOn);
538  break;
539 
542  switchLed(LED_SCROLLL, ka == QWSKeyboardHandler::ScrollLockOn);
543  break;
544 
545  default:
546  // ignore console switching and reboot
547  break;
548  }
549  }
550 }
int type
Definition: qmetatype.cpp:239
int qint32
Definition: qglobal.h:937
#define QT_READ
Definition: qcore_unix_p.h:280
unsigned short quint16
Definition: qglobal.h:936
Q_CORE_EXPORT void qWarning(const char *,...)
virtual bool filterInputEvent(quint16 &input_code, qint32 &input_value)
KeycodeAction
This enum describes the various special actions that actual QWSKeyboardHandler implementations have t...
Definition: qkbd_qws.h:67
QWSLinuxInputKeyboardHandler * m_handler
Definition: qlinuxinput.h:100
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
int errno
#define forever
This macro is provided for convenience for writing infinite loops.
Definition: qglobal.h:2452

◆ switchLed()

void QLinuxInputKeyboardHandler::switchLed ( int  led,
bool  state 
)
private

Definition at line 482 of file qlinuxinput.cpp.

Referenced by readKeycode().

483 {
484  struct ::input_event led_ie;
485  ::gettimeofday(&led_ie.time, 0);
486  led_ie.type = EV_LED;
487  led_ie.code = led;
488  led_ie.value = state;
489 
490  QT_WRITE(m_fd, &led_ie, sizeof(led_ie));
491 }
#define QT_WRITE
Definition: qcore_unix_p.h:289

Properties

◆ m_fd

int QLinuxInputKeyboardHandler::m_fd
private

◆ m_handler

QWSLinuxInputKeyboardHandler* QLinuxInputKeyboardHandler::m_handler
private

◆ m_orig_kbmode

int QLinuxInputKeyboardHandler::m_orig_kbmode
private

Definition at line 104 of file qlinuxinput.h.

Referenced by QLinuxInputKeyboardHandler(), and ~QLinuxInputKeyboardHandler().

◆ m_tty_attr

struct termios QLinuxInputKeyboardHandler::m_tty_attr
private

Definition at line 103 of file qlinuxinput.h.

Referenced by QLinuxInputKeyboardHandler(), and ~QLinuxInputKeyboardHandler().

◆ m_tty_fd

int QLinuxInputKeyboardHandler::m_tty_fd
private

Definition at line 102 of file qlinuxinput.h.

Referenced by QLinuxInputKeyboardHandler(), and ~QLinuxInputKeyboardHandler().


The documentation for this class was generated from the following files: