Qt 4.8
qmouselinuxinput_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 plugins 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 "qmouselinuxinput_qws.h"
43 
44 #include <QScreen>
45 #include <QSocketNotifier>
46 #include <QStringList>
47 
48 #include <qplatformdefs.h>
49 #include <private/qcore_unix_p.h> // overrides QT_OPEN
50 
51 #include <errno.h>
52 
53 #include <linux/input.h>
54 
56 
57 
59 {
60  Q_OBJECT
61 public:
64 
65  void enable(bool on);
66 
67 private Q_SLOTS:
68  void readMouseData();
69 
70 private:
73  int m_fd;
74  int m_x, m_y;
75  int m_buttons;
76 };
77 
80 {
81  d = new QWSLinuxInputMousePrivate(this, device);
82 }
83 
85 {
86  delete d;
87 }
88 
90 {
91  d->enable(false);
92 }
93 
95 {
96  d->enable(true);
97 }
98 
100  : m_handler(h), m_notify(0), m_x(0), m_y(0), m_buttons(0)
101 {
102  setObjectName(QLatin1String("LinuxInputSubsystem Mouse Handler"));
103 
104  QString dev = QLatin1String("/dev/input/event0");
105  int grab = 0;
106 
107  QStringList args = device.split(QLatin1Char(':'));
108  foreach (const QString &arg, args) {
109  if (arg.startsWith(QLatin1String("grab=")))
110  grab = arg.mid(5).toInt();
111  else if (arg.startsWith(QLatin1String("/dev/")))
112  dev = arg;
113  }
114 
115  m_fd = QT_OPEN(dev.toLocal8Bit().constData(), O_RDONLY | O_NDELAY, 0);
116  if (m_fd >= 0) {
117  ::ioctl(m_fd, EVIOCGRAB, grab);
119  connect(m_notify, SIGNAL(activated(int)), this, SLOT(readMouseData()));
120  } else {
121  qWarning("Cannot open mouse input device '%s': %s", qPrintable(dev), strerror(errno));
122  return;
123  }
124 }
125 
127 {
128  if (m_fd >= 0)
129  QT_CLOSE(m_fd);
130 }
131 
133 {
134  if (m_notify)
135  m_notify->setEnabled(on);
136 }
137 
139 {
140  if (!qt_screen)
141  return;
142 
143  struct ::input_event buffer[32];
144  int n = 0;
145 
146  forever {
147  int bytesRead = QT_READ(m_fd, reinterpret_cast<char *>(buffer) + n, sizeof(buffer) - n);
148  if (bytesRead == 0) {
149  qWarning("Got EOF from the input device.");
150  return;
151  }
152  if (bytesRead == -1) {
153  if (errno != EAGAIN)
154  qWarning("Could not read from input device: %s", strerror(errno));
155  break;
156  }
157 
158  n += bytesRead;
159  if (n % sizeof(buffer[0]) == 0)
160  break;
161  }
162  n /= sizeof(buffer[0]);
163 
164  for (int i = 0; i < n; ++i) {
165  struct ::input_event *data = &buffer[i];
166 
167  bool unknown = false;
168  if (data->type == EV_ABS) {
169  if (data->code == ABS_X) {
170  m_x = data->value;
171  } else if (data->code == ABS_Y) {
172  m_y = data->value;
173  } else {
174  unknown = true;
175  }
176  } else if (data->type == EV_REL) {
177  if (data->code == REL_X) {
178  m_x += data->value;
179  } else if (data->code == REL_Y) {
180  m_y += data->value;
181  } else {
182  unknown = true;
183  }
184  } else if (data->type == EV_KEY && data->code == BTN_TOUCH) {
185  m_buttons = data->value ? Qt::LeftButton : 0;
186  } else if (data->type == EV_KEY) {
187  int button = 0;
188  switch (data->code) {
189  case BTN_LEFT: button = Qt::LeftButton; break;
190  case BTN_MIDDLE: button = Qt::MidButton; break;
191  case BTN_RIGHT: button = Qt::RightButton; break;
192  }
193  if (data->value)
194  m_buttons |= button;
195  else
196  m_buttons &= ~button;
197  } else if (data->type == EV_SYN && data->code == SYN_REPORT) {
198  QPoint pos(m_x, m_y);
199  pos = m_handler->transform(pos);
200  m_handler->limitToScreen(pos);
202  } else if (data->type == EV_MSC && data->code == MSC_SCAN) {
203  // kernel encountered an unmapped key - just ignore it
204  continue;
205  } else {
206  unknown = true;
207  }
208  if (unknown) {
209  qWarning("unknown mouse event type=%x, code=%x, value=%x", data->type, data->code, data->value);
210  }
211  }
212 }
213 
215 
216 #include "qmouselinuxinput_qws.moc"
QPoint transform(const QPoint &)
Transforms the given position from device coordinates to screen coordinates, and returns the transfor...
Definition: qmouse_qws.cpp:589
Q_GUI_EXPORT QScreen * qt_screen
Definition: qscreen_qws.cpp:69
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void suspend()
Implement this function to suspend reading and handling of mouse events, e.
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
The QWSCalibratedMouseHandler class provides mouse calibration and noise reduction in Qt for Embedded...
Definition: qmouse_qws.h:92
#define SLOT(a)
Definition: qobjectdefs.h:226
#define O_RDONLY
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
QWSLinuxInputMouseHandler * m_handler
#define Q_SLOTS
Definition: qobjectdefs.h:71
The QString class provides a Unicode character string.
Definition: qstring.h:83
void mouseChanged(const QPoint &pos, int bstate, int wheel=0)
Notifies the system of a new mouse event.
Definition: qmouse_qws.cpp:285
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
QWSLinuxInputMousePrivate * d
#define SIGNAL(a)
Definition: qobjectdefs.h:227
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QWSLinuxInputMousePrivate(QWSLinuxInputMouseHandler *, const QString &)
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
void resume()
Implement this function to resume reading and handling mouse events, e.
Q_CORE_EXPORT void qWarning(const char *,...)
static const char * data(const QByteArray &arr)
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
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
QWSLinuxInputMouseHandler(const QString &)
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
void setEnabled(bool)
If enable is true, the notifier is enabled; otherwise the notifier is disabled.
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
void limitToScreen(QPoint &pt)
Ensures that the given position is within the screen&#39;s boundaries, changing the position if necessary...
Definition: qmouse_qws.cpp:248
#define qPrintable(string)
Definition: qglobal.h:1750
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
int errno
#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