Qt 4.8
qmouseintegrity_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 #ifndef QT_NO_QWS_MOUSE_INTEGRITY
43 
44 #include "qmouseintegrity_qws.h"
45 #include <qwindowsystem_qws.h>
46 #include <qapplication.h>
47 #include <qtimer.h>
48 #include <INTEGRITY.h>
49 
50 #include <QThread>
51 
52 #include <device/hiddriver.h>
53 
55 
57 
58 class QIntMousePrivate : public QObject
59 {
60  Q_OBJECT
62 Q_SIGNALS:
63  void mouseDataAvailable(int x, int y, int buttons);
64 public:
67  void dataReady(int x, int y, int buttons) { emit mouseDataAvailable(x, y, buttons); }
68  bool calibrated;
70  bool suspended;
72 private:
74 };
75 
77 {
78 protected:
80  bool loop;
83  Semaphore loopsem;
84 public:
87  void run();
88  bool setup(QString driverName, uint32_t index);
89  void stoploop() { loop = false; ReleaseSemaphore(loopsem); };
90 };
91 
92 QIntMouseHandler::QIntMouseHandler(const QString &driver, const QString &device)
93  : QObject(), QWSCalibratedMouseHandler(driver, device)
94 {
95  QPoint test(1,1);
96  d = new QIntMousePrivate(this);
97  connect(d, SIGNAL(mouseDataAvailable(int, int, int)), this, SLOT(readMouseData(int, int, int)));
98 
99  d->calibrated = (test != transform(test));
100 
101  d->mousethread->setup(QString(), 0);
102  d->mousethread->start();
103 }
104 
106 {
107  disconnect(d, SIGNAL(mouseDataAvailable(int, int, int)), this, SLOT(readMouseData(int, int, int)));
108  delete d;
109 }
110 
112 {
113  d->suspended = true;
114 }
115 
117 {
118  d->suspended = false;
119 }
120 
122 {
123  d->waitforread = false;
124  if (d->suspended)
125  return;
126  if (d->calibrated)
127  sendFiltered(QPoint(x, y), buttons);
128  else {
129  QPoint pos;
130  pos = transform(QPoint(x, y));
131  limitToScreen(pos);
132  mouseChanged(pos, buttons, 0);
133  }
134 }
135 
137 {
139 }
140 
142 {
144 }
145 
146 bool QIntMouseListenThread::setup(QString driverName, uint32_t index)
147 {
148  int i;
149  int devices;
150  Error driverError, deviceError;
151  HIDDriver *driver;
152  HIDHandle handle;
153  /* FIXME : take a list of driver names/indexes for setup */
154  devices = 0;
155  i = 0;
156  do {
157  driverError = gh_hid_get_driver(i, &driver);
158  if (driverError == Success) {
159  int j = 0;
160  do {
161  deviceError = gh_hid_init_device(driver, j, &handle);
162  if (deviceError == Success) {
163  int32_t type;
164  /* only accept pointing devices */
165  deviceError = gh_hid_get_setting(handle, HID_SETTING_CAPABILITIES, &type);
166  if ((deviceError == Success) && (type & HID_TYPE_AXIS)) {
167  handleList.append(handle);
168  devices++;
169  } else
170  gh_hid_close(handle);
171  j++;
172  }
173  } while (deviceError == Success);
174  i++;
175  }
176  } while (driverError == Success);
177  return (devices > 0);
178 }
179 
181 {
182  Value id;
183  HIDEvent event;
184  Activity loopact;
185  QPoint currentpos(0,0);
186  Qt::MouseButtons currentbutton = Qt::NoButton;
187  Qt::KeyboardModifiers keymod;
188 
189  /* first create all Activities for the main loop.
190  * We couldn't do this in setup() because this Task is different */
191  Activity act;
192  int i = 0;
193  foreach (HIDHandle h, handleList) {
194  CheckSuccess(CreateActivity(CurrentTask(), 2, false, i, &act));
195  actList.append(act);
196  i++;
197  CheckSuccess(gh_hid_async_wait_for_event(h, act));
198  }
199 
200  /* setup a Semaphore used to watch for a request for exit */
201  CheckSuccess(CreateSemaphore(0, &loopsem));
202  CheckSuccess(CreateActivity(CurrentTask(), 2, false, 0, &loopact));
203  CheckSuccess(AsynchronousReceive(loopact, (Object)loopsem, NULL));
204 
205  loop = true;
206  do {
207  uint32_t num_events = 1;
208 
209  WaitForActivity(&id);
210  if (loop) {
211  while (gh_hid_get_event(handleList.at(id), &event, &num_events) == Success) {
212  if (event.type == HID_TYPE_AXIS) {
213  switch (event.index) {
214  case HID_AXIS_ABSX:
215  currentpos.setX(event.value);
216  break;
217  case HID_AXIS_ABSY:
218  currentpos.setY(event.value);
219  break;
220  case HID_AXIS_RELX:
221  currentpos.setX(currentpos.x() + event.value);
222  break;
223  case HID_AXIS_RELY:
224  currentpos.setY(currentpos.y() + event.value);
225  break;
226  default:
227  break;
228  }
229  } else if (event.type == HID_TYPE_KEY) {
230  switch (event.index) {
231  case HID_BUTTON_LEFT:
232  if (event.value)
233  currentbutton |= Qt::LeftButton;
234  else
235  currentbutton &= ~Qt::LeftButton;
236  break;
237  case HID_BUTTON_MIDDLE:
238  if (event.value)
239  currentbutton |= Qt::MiddleButton;
240  else
241  currentbutton &= ~Qt::MiddleButton;
242  break;
243  case HID_BUTTON_RIGHT:
244  if (event.value)
245  currentbutton |= Qt::RightButton;
246  else
247  currentbutton &= ~Qt::RightButton;
248  break;
249  }
250  } else if (event.type == HID_TYPE_SYNC) {
251  /* sync events are sent by mouses and not by keyboards.
252  * this should probably be changed */
253  imp->dataReady(currentpos.x(), currentpos.y(), currentbutton);
254  //QWindowSystemInterface::handleMouseEvent(0, currentpos, currentpos, currentbutton);
255  }
256  }
257  CheckSuccess(gh_hid_async_wait_for_event(handleList.at(id), actList.at(id)));
258  }
259  } while (loop);
260  CloseSemaphore(loopsem);
261  QThread::exit(0);
262 }
263 
265 {
266  this->handler = handler;
267  suspended = false;
269 }
270 
272 {
274  mousethread->wait();
275  delete mousethread;
276 }
277 
279 
280 #include "qmouseintegrity_qws.moc"
281 
282 #endif // QT_NO_QWS_MOUSE_INTEGRITY
QPoint transform(const QPoint &)
Transforms the given position from device coordinates to screen coordinates, and returns the transfor...
Definition: qmouse_qws.cpp:589
Error
Definition: qaudio.h:58
void clearCalibration()
Clears the current calibration, i.e., makes the mouse driver return mouse events in raw device coordi...
int type
Definition: qmetatype.cpp:239
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void mouseDataAvailable(int x, int y, int buttons)
The QWSCalibratedMouseHandler class provides mouse calibration and noise reduction in Qt for Embedded...
Definition: qmouse_qws.h:92
void readMouseData(int x, int y, int buttons)
bool setup(QString driverName, uint32_t index)
#define SLOT(a)
Definition: qobjectdefs.h:226
static Qt::MouseButtons buttons
QIntMousePrivate * d
virtual void calibrate(const QWSPointerCalibrationData *)
Updates the calibration parameters based on coordinate mapping of the given data. ...
Definition: qmouse_qws.cpp:526
The QString class provides a Unicode character string.
Definition: qstring.h:83
const QPoint & pos() const
Returns the current mouse position.
Definition: qmouse_qws.h:82
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
virtual bool event(QEvent *)
This virtual function receives events to an object and should return true if the event e was recogniz...
Definition: qobject.cpp:1200
#define Q_SIGNALS
Definition: qobjectdefs.h:72
virtual void clearCalibration()
Clears the current calibration, i.e., makes the mouse driver return mouse events in raw device coordi...
Definition: qmouse_qws.cpp:416
#define SIGNAL(a)
Definition: qobjectdefs.h:227
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
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QIntMouseListenThread * mousethread
QIntMouseHandler * handler
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
#define emit
Definition: qobjectdefs.h:76
void run()
The starting point for the thread.
static const char * data(const QByteArray &arr)
QIntMouseHandler(const QString &driver=QString(), const QString &device=QString())
The QWSPointerCalibrationData class is a container for mouse calibration data in Qt for Embedded Linu...
Definition: qmouse_qws.h:57
#define Q_OBJECT
Definition: qobjectdefs.h:157
void dataReady(int x, int y, int buttons)
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.
Definition: qobject.cpp:2895
void setY(int y)
Sets the y coordinate of this point to the given y coordinate.
Definition: qpoint.h:137
void start(Priority=InheritPriority)
Begins execution of the thread by calling run().
bool wait(unsigned long time=ULONG_MAX)
Blocks the thread until either of these conditions is met:
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
Definition: qnamespace.h:54
int y() const
Returns the y coordinate of this point.
Definition: qpoint.h:131
quint16 index
QIntMouseListenThread(QIntMousePrivate *im)
QFuture< T > run(Function function,...)
QList< HIDHandle > handleList
int x() const
Returns the x coordinate of this point.
Definition: qpoint.h:128
void resume()
Implement this function to resume reading and handling mouse events, e.
QImageIOHandler * handler
QIntMousePrivate(QIntMouseHandler *handler)
friend class QIntMouseListenTaskThread
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
void suspend()
Implement this function to suspend reading and handling of mouse events, e.
void setX(int x)
Sets the x coordinate of this point to the given x coordinate.
Definition: qpoint.h:134
The QThread class provides a platform-independent way to manage threads.
Definition: qthread.h:59
void calibrate(const QWSPointerCalibrationData *data)
Updates the calibration parameters based on coordinate mapping of the given data. ...
void exit(int retcode=0)
Tells the thread&#39;s event loop to exit with a return code.
Definition: qthread.cpp:592
static void setup()
Definition: qtextcodec.cpp:718