Qt 4.8
qmouselinuxtp_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 "qmouselinuxtp_qws.h"
43 
44 #ifndef QT_NO_QWS_MOUSE_LINUXTP
45 #include "qwindowsystem_qws.h"
46 #include "qsocketnotifier.h"
47 #include "qtimer.h"
48 #include "qapplication.h"
49 #include "qscreen_qws.h"
50 #include <private/qcore_unix_p.h> // overrides QT_OPEN
51 
52 #include <unistd.h>
53 #include <stdlib.h>
54 #include <stdio.h>
55 #include <sys/ioctl.h>
56 #include <sys/types.h>
57 #include <sys/stat.h>
58 #include <fcntl.h>
59 #include <errno.h>
60 #include <termios.h>
61 
63 
64 #if defined(QT_QWS_IPAQ)
65  #define QT_QWS_IPAQ_RAW
66  #define QT_QWS_SCREEN_COORDINATES
67  typedef struct {
68  unsigned short pressure;
69  unsigned short x;
70  unsigned short y;
71  unsigned short pad;
72  } TS_EVENT;
73 #elif defined(QT_QWS_EBX)
74  #define QT_QWS_EBX_RAW
75  #define QT_QWS_SCREEN_COORDINATES
76 #ifndef QT_QWS_SHARP
77  typedef struct {
78  unsigned short pressure;
79  unsigned short x;
80  unsigned short y;
81  unsigned short pad;
82  } TS_EVENT;
83  #else
84  typedef struct {
85  long y;
86  long x;
87  long pressure;
88  long long millisecs;
89  } TS_EVENT;
90  #define QT_QWS_TP_SAMPLE_SIZE 10
91  #define QT_QWS_TP_MINIMUM_SAMPLES 4
92  #define QT_QWS_TP_PRESSURE_THRESHOLD 500
93  #define QT_QWS_TP_MOVE_LIMIT 50
94  #define QT_QWS_TP_JITTER_LIMIT 2
95  #endif
96 #else // not IPAQ, not SHARP
97  typedef struct {
98  unsigned short pressure;
99  unsigned short x;
100  unsigned short y;
101  unsigned short pad;
102  } TS_EVENT;
103 #endif
104 
105 #ifndef QT_QWS_TP_SAMPLE_SIZE
106 #define QT_QWS_TP_SAMPLE_SIZE 5
107 #endif
108 
109 #ifndef QT_QWS_TP_MINIMUM_SAMPLES
110 #define QT_QWS_TP_MINIMUM_SAMPLES 5
111 #endif
112 
113 #ifndef QT_QWS_TP_PRESSURE_THRESHOLD
114 #define QT_QWS_TP_PRESSURE_THRESHOLD 1
115 #endif
116 
117 #ifndef QT_QWS_TP_MOVE_LIMIT
118 #define QT_QWS_TP_MOVE_LIMIT 100
119 #endif
120 
121 #ifndef QT_QWS_TP_JITTER_LIMIT
122 #define QT_QWS_TP_JITTER_LIMIT 2
123 #endif
124 
126 {
127  Q_OBJECT
128 public:
131 
132  void suspend();
133  void resume();
134 private:
135  static const int mouseBufSize = 2048;
136  int mouseFD;
145  int mouseIdx;
146  uchar mouseBuf[mouseBufSize];
149 
150 private slots:
151  void readMouseData();
152 };
153 
155  : QWSCalibratedMouseHandler(driver, device)
156 {
157  d = new QWSLinuxTPMouseHandlerPrivate(this, device);
158 }
159 
161 {
162  delete d;
163 }
164 
166 {
167  d->suspend();
168 }
169 
171 {
172  d->resume();
173 }
174 
176  const QString &device)
177  : samples(QT_QWS_TP_SAMPLE_SIZE), currSample(0), lastSample(0),
178  numSamples(0), skipCount(0), handler(h)
179 {
180  QString mousedev;
181  if (device.isEmpty()) {
182 #if defined(QT_QWS_IPAQ)
183 # ifdef QT_QWS_IPAQ_RAW
184  mousedev = QLatin1String("/dev/h3600_tsraw");
185 # else
186  mousedev = QLatin1String("/dev/h3600_ts");
187 # endif
188 #else
189  mousedev = QLatin1String("/dev/ts");
190 #endif
191  } else {
192  mousedev = device;
193  }
194  if ((mouseFD = QT_OPEN(mousedev.toLatin1().constData(), O_RDONLY | O_NDELAY)) < 0) {
195  qWarning("Cannot open %s (%s)", qPrintable(mousedev), strerror(errno));
196  return;
197  }
198 
200  this);
201  connect(mouseNotifier, SIGNAL(activated(int)),this, SLOT(readMouseData()));
202  waspressed=false;
203  mouseIdx = 0;
204 }
205 
207 {
208  if (mouseFD >= 0)
209  QT_CLOSE(mouseFD);
210 }
211 
213 {
214  if (mouseNotifier)
215  mouseNotifier->setEnabled(false);
216 }
217 
219 {
220  mouseIdx=0;
221  currSample=0;
222  lastSample=0;
223  numSamples=0;
224  skipCount=0;
225  if (mouseNotifier)
226  mouseNotifier->setEnabled(true);
227 }
228 
229 
231 {
232  if(!qt_screen)
233  return;
234 
235  int n;
236  do {
238  if (n > 0)
239  mouseIdx += n;
240  } while (n > 0 && mouseIdx < mouseBufSize);
241 
242  //qDebug("readMouseData()");
243 
244  TS_EVENT *data;
245  int idx = 0;
246 
247  // perhaps we shouldn't be reading EVERY SAMPLE.
248  while (mouseIdx-idx >= (int)sizeof(TS_EVENT)) {
249  uchar *mb = mouseBuf+idx;
250  data = (TS_EVENT *) mb;
251 
253 #ifdef QT_QWS_SHARP
254  samples[currSample] = QPoint(1000 - data->x, data->y);
255 #else
256  samples[currSample] = QPoint(data->x, data->y);
257 #endif
258  numSamples++;
260  int sampleCount = qMin(numSamples + 1,samples.count());
261 
262  // average the rest
263  QPoint mousePos = QPoint(0, 0);
264  QPoint totalMousePos = oldTotalMousePos;
265  totalMousePos += samples[currSample];
266  if(numSamples >= samples.count())
267  totalMousePos -= samples[lastSample];
268 
269  mousePos = totalMousePos / (sampleCount - 1);
270 #if defined(QT_QWS_SCREEN_COORDINATES)
271  mousePos = handler->transform(mousePos);
272 #endif
273  if(!waspressed)
274  oldmouse = mousePos;
275  QPoint dp = mousePos - oldmouse;
276  int dxSqr = dp.x() * dp.x();
277  int dySqr = dp.y() * dp.y();
278  if (dxSqr + dySqr < (QT_QWS_TP_MOVE_LIMIT * QT_QWS_TP_MOVE_LIMIT)) {
279  if (waspressed) {
280  if ((dxSqr + dySqr > (QT_QWS_TP_JITTER_LIMIT * QT_QWS_TP_JITTER_LIMIT)) || skipCount > 2) {
282  oldmouse = mousePos;
283  skipCount = 0;
284  } else {
285  skipCount++;
286  }
287  } else {
289  oldmouse=mousePos;
290  waspressed=true;
291  }
292 
293  // save recuring information
294  currSample++;
295  if (numSamples >= samples.count())
296  lastSample++;
297  oldTotalMousePos = totalMousePos;
298  } else {
299  numSamples--; // don't use this sample, it was bad.
300  }
301  } else {
302  // build up the average
304  currSample++;
305  }
306  if (currSample >= samples.count())
307  currSample = 0;
308  if (lastSample >= samples.count())
309  lastSample = 0;
310  } else {
311  currSample = 0;
312  lastSample = 0;
313  numSamples = 0;
314  skipCount = 0;
315  oldTotalMousePos = QPoint(0,0);
316  if (waspressed) {
318  oldmouse = QPoint(-100, -100);
319  waspressed=false;
320  }
321  }
322  idx += sizeof(TS_EVENT);
323  }
324 
325  int surplus = mouseIdx - idx;
326  for (int i = 0; i < surplus; i++)
327  mouseBuf[i] = mouseBuf[idx+i];
328  mouseIdx = surplus;
329 }
330 
332 
333 #include "qmouselinuxtp_qws.moc"
334 
335 #endif //QT_NO_QWS_MOUSE_LINUXTP
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
QWSLinuxTPMouseHandler * handler
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QWSLinuxTPMouseHandler(const QString &=QString(), const QString &=QString())
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
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
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
#define QT_QWS_TP_PRESSURE_THRESHOLD
void resume()
Implement this function to resume reading and handling mouse events, e.
The QPolygon class provides a vector of points using integer precision.
Definition: qpolygon.h:60
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.
unsigned short pressure
#define SIGNAL(a)
Definition: qobjectdefs.h:227
unsigned char uchar
Definition: qglobal.h:994
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QWSLinuxTPMouseHandlerPrivate(QWSLinuxTPMouseHandler *h, 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
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
Q_CORE_EXPORT void qWarning(const char *,...)
static const char * data(const QByteArray &arr)
#define QT_QWS_TP_MOVE_LIMIT
QByteArray toLatin1() const Q_REQUIRED_RESULT
Returns a Latin-1 representation of the string as a QByteArray.
Definition: qstring.cpp:3993
#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
unsigned short y
#define QT_QWS_TP_SAMPLE_SIZE
unsigned short pad
#define QT_OPEN
Definition: qcore_unix_p.h:186
#define QT_QWS_TP_MINIMUM_SAMPLES
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.
QWSLinuxTPMouseHandlerPrivate * d
int y() const
Returns the y coordinate of this point.
Definition: qpoint.h:131
int x() const
Returns the x coordinate of this point.
Definition: qpoint.h:128
#define slots
Definition: qobjectdefs.h:68
QImageIOHandler * handler
void suspend()
Implement this function to suspend reading and handling of mouse events, e.
#define qPrintable(string)
Definition: qglobal.h:1750
#define QT_QWS_TP_JITTER_LIMIT
unsigned short x
friend class QWSLinuxTPMouseHandlerPrivate
int errno
#define QT_CLOSE
Definition: qcore_unix_p.h:304