Qt 4.8
qmousetslib_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 "qmousetslib_qws.h"
43 
44 #if !defined(QT_NO_QWS_MOUSE_TSLIB) || defined(QT_PLUGIN)
45 
46 #include <QtCore/qregexp.h>
47 #include <QtCore/qstringlist.h>
48 #include "qsocketnotifier.h"
49 #include "qscreen_qws.h"
50 
51 #include <tslib.h>
52 #include <errno.h>
53 
55 
56 #ifdef TSLIBMOUSEHANDLER_DEBUG
57 # include <QtCore/QDebug>
58 #endif
59 
103 {
104  Q_OBJECT
105 public:
107  const QString &device);
109 
110  void suspend();
111  void resume();
112 
114  void clearCalibration();
115 
116 private:
118  struct tsdev *dev;
121 
122  struct ts_sample lastSample;
124  int lastdx;
125  int lastdy;
126 
129 
130  bool open();
131  void close();
132  inline bool get_sample(struct ts_sample *sample);
133 
134 private slots:
135  void readMouseData();
136 };
137 
139  const QString &device)
140  : handler(h), dev(0), mouseNotifier(0), jitter_limit(3)
141 {
143  QRegExp jitterRegex(QLatin1String("^jitter_limit=(\\d+)$"));
144  int index = args.indexOf(jitterRegex);
145  if (index >= 0) {
146  jitter_limit = jitterRegex.cap(1).toInt();
147  args.removeAt(index);
148  }
149 
150  devName = args.join(QString());
151 
152  if (devName.isNull()) {
153  const char *str = getenv("TSLIB_TSDEVICE");
154  if (str)
156  }
157 
158  if (devName.isNull())
159  devName = QLatin1String("/dev/ts");
160 
161  if (!open())
162  return;
163 
164  calibrated = true;
165 
166  int fd = ts_fd(dev);
168  connect(mouseNotifier, SIGNAL(activated(int)),this, SLOT(readMouseData()));
169  resume();
170 }
171 
173 {
174  close();
175 }
176 
178 {
179  dev = ts_open(devName.toLocal8Bit().constData(), 1);
180  if (!dev) {
181  qCritical("QWSTslibMouseHandlerPrivate: ts_open() failed"
182  " with error: '%s'", strerror(errno));
183  qCritical("Please check your tslib installation!");
184  return false;
185  }
186 
187  if (ts_config(dev)) {
188  qCritical("QWSTslibMouseHandlerPrivate: ts_config() failed"
189  " with error: '%s'", strerror(errno));
190  qCritical("Please check your tslib installation!");
191  close();
192  return false;
193  }
194 
195  return true;
196 }
197 
199 {
200  if (dev)
201  ts_close(dev);
202 }
203 
205 {
206  if (mouseNotifier)
207  mouseNotifier->setEnabled(false);
208 }
209 
211 {
212  memset(&lastSample, 0, sizeof(lastSample));
213  wasPressed = false;
214  lastdx = 0;
215  lastdy = 0;
216  if (mouseNotifier)
217  mouseNotifier->setEnabled(true);
218 }
219 
220 bool QWSTslibMouseHandlerPrivate::get_sample(struct ts_sample *sample)
221 {
222  if (!calibrated)
223  return (ts_read_raw(dev, sample, 1) == 1);
224 
225  return (ts_read(dev, sample, 1) == 1);
226 }
227 
229 {
230  if (!qt_screen)
231  return;
232 
233  for(;;) {
234  struct ts_sample sample = lastSample;
235  bool pressed = wasPressed;
236 
237  // Fast return if there's no events.
238  if (!get_sample(&sample))
239  return;
240  pressed = (sample.pressure > 0);
241 
242  // Only return last sample unless there's a press/release event.
243  while (pressed == wasPressed) {
244  if (!get_sample(&sample))
245  break;
246  pressed = (sample.pressure > 0);
247  }
248 
249  // work around missing coordinates on mouse release in raw mode
250  if (!calibrated && !pressed && sample.x == 0 && sample.y == 0) {
251  sample.x = lastSample.x;
252  sample.y = lastSample.y;
253  }
254 
255  int dx = sample.x - lastSample.x;
256  int dy = sample.y - lastSample.y;
257 
258  // Remove small movements in oppsite direction
259  if (dx * lastdx < 0 && qAbs(dx) < jitter_limit) {
260  sample.x = lastSample.x;
261  dx = 0;
262  }
263  if (dy * lastdy < 0 && qAbs(dy) < jitter_limit) {
264  sample.y = lastSample.y;
265  dy = 0;
266  }
267 
268  if (wasPressed == pressed && dx == 0 && dy == 0)
269  return;
270 
271 #ifdef TSLIBMOUSEHANDLER_DEBUG
272  qDebug() << "last" << QPoint(lastSample.x, lastSample.y)
273  << "curr" << QPoint(sample.x, sample.y)
274  << "dx,dy" << QPoint(dx, dy)
275  << "ddx,ddy" << QPoint(dx*lastdx, dy*lastdy)
276  << "pressed" << wasPressed << pressed;
277 #endif
278 
279  lastSample = sample;
280  wasPressed = pressed;
281  if (dx != 0)
282  lastdx = dx;
283  if (dy != 0)
284  lastdy = dy;
285 
286  const QPoint p(sample.x, sample.y);
287  if (calibrated) {
288  // tslib should do all the translation and filtering, so we send a
289  // "raw" mouse event
290  handler->QWSMouseHandler::mouseChanged(p, pressed);
291  } else {
292  handler->sendFiltered(p, pressed);
293  }
294  }
295 }
296 
298 {
299  suspend();
300  close();
301  handler->QWSCalibratedMouseHandler::clearCalibration();
302  calibrated = false;
303  open();
304  resume();
305 }
306 
308 {
309  suspend();
310  close();
311  // default implementation writes to /etc/pointercal
312  // using the same format as the tslib linear module.
313  handler->QWSCalibratedMouseHandler::calibrate(data);
314  calibrated = true;
315  open();
316  resume();
317 }
318 
323  const QString &device)
324  : QWSCalibratedMouseHandler(driver, device)
325 {
326  d = new QWSTslibMouseHandlerPrivate(this, device);
327 }
328 
333 {
334  delete d;
335 }
336 
341 {
342  d->suspend();
343 }
344 
349 {
350  d->resume();
351 }
352 
357 {
358  d->clearCalibration();
359 }
360 
365 {
366  d->calibrate(data);
367 }
368 
370 
371 #include "qmousetslib_qws.moc"
372 
373 #endif //QT_NO_QWS_MOUSE_TSLIB
Q_GUI_EXPORT QScreen * qt_screen
Definition: qscreen_qws.cpp:69
void clearCalibration()
Reimplemented Function
static QString fromLocal8Bit(const char *, int size=-1)
Returns a QString initialized with the first size characters of the 8-bit string str.
Definition: qstring.cpp:4245
QString cap(int nth=0) const
Returns the text captured by the nth subexpression.
Definition: qregexp.cpp:4310
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
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
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
Q_DECL_CONSTEXPR T qAbs(const T &t)
Definition: qglobal.h:1201
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
The QSocketNotifier class provides support for monitoring activity on a file descriptor.
Q_CORE_EXPORT void qDebug(const char *,...)
#define SIGNAL(a)
Definition: qobjectdefs.h:227
void calibrate(const QWSPointerCalibrationData *data)
Reimplemented Function
bool sendFiltered(const QPoint &, int button)
Notifies the system of a new mouse event after applying a noise reduction filter. ...
Definition: qmouse_qws.cpp:639
int indexOf(const QRegExp &rx, int from=0) const
Returns the index position of the first exact match of rx in the list, searching forward from index p...
Definition: qstringlist.h:195
#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
void suspend()
Reimplemented Function
static const char * data(const QByteArray &arr)
The QWSPointerCalibrationData class is a container for mouse calibration data in Qt for Embedded Linu...
Definition: qmouse_qws.h:57
void resume()
Reimplemented Function
QByteArray toLocal8Bit() const Q_REQUIRED_RESULT
Returns the local 8-bit representation of the string as a QByteArray.
Definition: qstring.cpp:4049
The QWSTslibMouseHandler class implements a mouse driver for the Universal Touch Screen Library...
#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
bool isNull() const
Returns true if this string is null; otherwise returns false.
Definition: qstring.h:505
QString join(const QString &sep) const
Joins all the string list&#39;s strings into a single string with each element separated by the given sep...
Definition: qstringlist.h:162
QWSTslibMouseHandler(const QString &driver=QString(), const QString &device=QString())
QWSTslibMouseHandlerPrivate(QWSTslibMouseHandler *h, const QString &device)
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.
QWSTslibMouseHandlerPrivate * d
quint16 index
QWSTslibMouseHandler * handler
void calibrate(const QWSPointerCalibrationData *data)
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 slots
Definition: qobjectdefs.h:68
bool get_sample(struct ts_sample *sample)
friend class QWSTslibMouseHandlerPrivate
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
Q_CORE_EXPORT void qCritical(const char *,...)
int errno
void removeAt(int i)
Removes the item at index position i.
Definition: qlist.h:480