1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
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 The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/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 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file. Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
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
54 QT_BEGIN_NAMESPACE
55
56 class QIntMouseListenThread;
57
58 class QIntMousePrivate : public QObject
59 {
60 Q_OBJECT
61 friend class QIntMouseListenTaskThread;
62 Q_SIGNALS:
63 void mouseDataAvailable(int x, int y, int buttons);
64 public:
65 QIntMousePrivate(QIntMouseHandler *handler);
66 ~QIntMousePrivate();
dataReady(int x,int y,int buttons)67 void dataReady(int x, int y, int buttons) { emit mouseDataAvailable(x, y, buttons); }
68 bool calibrated;
69 bool waitforread;
70 bool suspended;
71 QIntMouseListenThread *mousethread;
72 private:
73 QIntMouseHandler *handler;
74 };
75
76 class QIntMouseListenThread : public QThread
77 {
78 protected:
79 QIntMousePrivate *imp;
80 bool loop;
81 QList<HIDHandle> handleList;
82 QList<Activity> actList;
83 Semaphore loopsem;
84 public:
QIntMouseListenThread(QIntMousePrivate * im)85 QIntMouseListenThread(QIntMousePrivate *im) : QThread(), imp(im) {};
~QIntMouseListenThread()86 ~QIntMouseListenThread() {};
87 void run();
88 bool setup(QString driverName, uint32_t index);
stoploop()89 void stoploop() { loop = false; ReleaseSemaphore(loopsem); };
90 };
91
QIntMouseHandler(const QString & driver,const QString & device)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
~QIntMouseHandler()105 QIntMouseHandler::~QIntMouseHandler()
106 {
107 disconnect(d, SIGNAL(mouseDataAvailable(int, int, int)), this, SLOT(readMouseData(int, int, int)));
108 delete d;
109 }
110
resume()111 void QIntMouseHandler::resume()
112 {
113 d->suspended = true;
114 }
115
suspend()116 void QIntMouseHandler::suspend()
117 {
118 d->suspended = false;
119 }
120
readMouseData(int x,int y,int buttons)121 void QIntMouseHandler::readMouseData(int x, int y, int buttons)
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
clearCalibration()136 void QIntMouseHandler::clearCalibration()
137 {
138 QWSCalibratedMouseHandler::clearCalibration();
139 }
140
calibrate(const QWSPointerCalibrationData * data)141 void QIntMouseHandler::calibrate(const QWSPointerCalibrationData *data)
142 {
143 QWSCalibratedMouseHandler::calibrate(data);
144 }
145
setup(QString driverName,uint32_t index)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
run(void)180 void QIntMouseListenThread::run(void)
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
QIntMousePrivate(QIntMouseHandler * handler)264 QIntMousePrivate::QIntMousePrivate(QIntMouseHandler *handler) : QObject()
265 {
266 this->handler = handler;
267 suspended = false;
268 mousethread = new QIntMouseListenThread(this);
269 }
270
~QIntMousePrivate()271 QIntMousePrivate::~QIntMousePrivate()
272 {
273 mousethread->stoploop();
274 mousethread->wait();
275 delete mousethread;
276 }
277
278 QT_END_NAMESPACE
279
280 #include "qmouseintegrity_qws.moc"
281
282 #endif // QT_NO_QWS_MOUSE_INTEGRITY
283