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 test suite 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 #include "qnativeevents.h"
43 
QNativeInput(bool subscribe)44 QNativeInput::QNativeInput(bool subscribe)
45 {
46     if (subscribe)
47         subscribeForNativeEvents();
48 }
49 
~QNativeInput()50 QNativeInput::~QNativeInput()
51 {
52     unsubscribeForNativeEvents();
53 }
54 
notify(QNativeEvent * event)55 void QNativeInput::notify(QNativeEvent *event)
56 {
57     nativeEvent(event);
58 }
59 
nativeEvent(QNativeEvent * event)60 void QNativeInput::nativeEvent(QNativeEvent *event)
61 {
62     switch (event->id()){
63         case QNativeMouseButtonEvent::eventId:{
64             QNativeMouseButtonEvent *e = static_cast<QNativeMouseButtonEvent *>(event);
65             (e->clickCount > 0) ? nativeMousePressEvent(e) : nativeMouseReleaseEvent(e);
66             break; }
67         case QNativeMouseMoveEvent::eventId:
68             nativeMouseMoveEvent(static_cast<QNativeMouseMoveEvent *>(event));
69             break;
70         case QNativeMouseDragEvent::eventId:
71             nativeMouseDragEvent(static_cast<QNativeMouseDragEvent *>(event));
72             break;
73         case QNativeMouseWheelEvent::eventId:
74             nativeMouseWheelEvent(static_cast<QNativeMouseWheelEvent *>(event));
75             break;
76         case QNativeKeyEvent::eventId:{
77             QNativeKeyEvent *e = static_cast<QNativeKeyEvent *>(event);
78             e->press ? nativeKeyPressEvent(e) : nativeKeyReleaseEvent(e);
79             break; }
80         case QNativeModifierEvent::eventId:
81             nativeModifierEvent(static_cast<QNativeModifierEvent *>(event));
82             break;
83         default:
84             break;
85     }
86 }
87 
sendNativeEvent(const QNativeEvent & event,int pid)88 Qt::Native::Status QNativeInput::sendNativeEvent(const QNativeEvent &event, int pid)
89 {
90     switch (event.id()){
91         case QNativeMouseMoveEvent::eventId:
92             return sendNativeMouseMoveEvent(static_cast<const QNativeMouseMoveEvent &>(event));
93         case QNativeMouseButtonEvent::eventId:
94             return sendNativeMouseButtonEvent(static_cast<const QNativeMouseButtonEvent &>(event));
95         case QNativeMouseDragEvent::eventId:
96             return sendNativeMouseDragEvent(static_cast<const QNativeMouseDragEvent &>(event));
97         case QNativeMouseWheelEvent::eventId:
98             return sendNativeMouseWheelEvent(static_cast<const QNativeMouseWheelEvent &>(event));
99         case QNativeKeyEvent::eventId:
100             return sendNativeKeyEvent(static_cast<const QNativeKeyEvent &>(event), pid);
101         case QNativeModifierEvent::eventId:
102             return sendNativeModifierEvent(static_cast<const QNativeModifierEvent &>(event));
103         case QNativeEvent::eventId:
104             qWarning() << "Warning: Cannot send a pure native event. Use a sub class.";
105         default:
106             return Qt::Native::Failure;
107     }
108 }
109 
QNativeEvent(Qt::KeyboardModifiers modifiers)110 QNativeEvent::QNativeEvent(Qt::KeyboardModifiers modifiers)
111     : modifiers(modifiers){}
112 
QNativeMouseEvent(QPoint pos,Qt::KeyboardModifiers modifiers)113 QNativeMouseEvent::QNativeMouseEvent(QPoint pos, Qt::KeyboardModifiers modifiers)
114     : QNativeEvent(modifiers), globalPos(pos){}
115 
QNativeMouseMoveEvent(QPoint pos,Qt::KeyboardModifiers modifiers)116 QNativeMouseMoveEvent::QNativeMouseMoveEvent(QPoint pos, Qt::KeyboardModifiers modifiers)
117     : QNativeMouseEvent(pos, modifiers){}
118 
QNativeMouseButtonEvent(QPoint globalPos,Qt::MouseButton button,int clickCount,Qt::KeyboardModifiers modifiers)119 QNativeMouseButtonEvent::QNativeMouseButtonEvent(QPoint globalPos, Qt::MouseButton button, int clickCount, Qt::KeyboardModifiers modifiers)
120     : QNativeMouseEvent(globalPos, modifiers), button(button), clickCount(clickCount){}
121 
QNativeMouseDragEvent(QPoint globalPos,Qt::MouseButton button,Qt::KeyboardModifiers modifiers)122 QNativeMouseDragEvent::QNativeMouseDragEvent(QPoint globalPos, Qt::MouseButton button, Qt::KeyboardModifiers modifiers)
123     : QNativeMouseButtonEvent(globalPos, button, true, modifiers){}
124 
QNativeMouseWheelEvent(QPoint globalPos,int delta,Qt::KeyboardModifiers modifiers)125 QNativeMouseWheelEvent::QNativeMouseWheelEvent(QPoint globalPos, int delta, Qt::KeyboardModifiers modifiers)
126     : QNativeMouseEvent(globalPos, modifiers), delta(delta){}
127 
QNativeKeyEvent(int nativeKeyCode,bool press,Qt::KeyboardModifiers modifiers)128 QNativeKeyEvent::QNativeKeyEvent(int nativeKeyCode, bool press, Qt::KeyboardModifiers modifiers)
129     : QNativeEvent(modifiers), nativeKeyCode(nativeKeyCode), press(press), character(QChar()){}
130 
QNativeModifierEvent(Qt::KeyboardModifiers modifiers,int nativeKeyCode)131 QNativeModifierEvent::QNativeModifierEvent(Qt::KeyboardModifiers modifiers, int nativeKeyCode)
132     : QNativeEvent(modifiers), nativeKeyCode(nativeKeyCode){}
133 
QNativeKeyEvent(int nativeKeyCode,bool press,QChar character,Qt::KeyboardModifiers modifiers)134 QNativeKeyEvent::QNativeKeyEvent(int nativeKeyCode, bool press, QChar character, Qt::KeyboardModifiers modifiers)
135     : QNativeEvent(modifiers), nativeKeyCode(nativeKeyCode), press(press), character(character){}
136 
getButtonAsString(const QNativeMouseButtonEvent * e)137 static QString getButtonAsString(const QNativeMouseButtonEvent *e)
138 {
139     switch (e->button){
140         case Qt::LeftButton:
141             return "button = LeftButton";
142             break;
143         case Qt::RightButton:
144             return "button = RightButton";
145             break;
146         case Qt::MidButton:
147             return "button = MidButton";
148             break;
149         default:
150             return "button = Other";
151             break;
152     }
153 }
154 
getModifiersAsString(const QNativeEvent * e)155 static QString getModifiersAsString(const QNativeEvent *e)
156 {
157     if (e->modifiers == 0)
158         return "modifiers = none";
159 
160     QString tmp = "modifiers = ";
161     if (e->modifiers.testFlag(Qt::ShiftModifier))
162         tmp += "Shift";
163     if (e->modifiers.testFlag(Qt::ControlModifier))
164         tmp += "Control";
165     if (e->modifiers.testFlag(Qt::AltModifier))
166         tmp += "Alt";
167     if (e->modifiers.testFlag(Qt::MetaModifier))
168         tmp += "Meta";
169     return tmp;
170 }
171 
getPosAsString(QPoint pos)172 static QString getPosAsString(QPoint pos)
173 {
174     return QString("QPoint(%1, %2)").arg(pos.x()).arg(pos.y());
175 }
176 
getBoolAsString(bool b)177 static QString getBoolAsString(bool b)
178 {
179     return b ? QString("true") : QString("false");
180 }
181 
toString() const182 QString QNativeMouseMoveEvent::toString() const
183 {
184     return QString("QNativeMouseMoveEvent(globalPos = %1 %2)").arg(getPosAsString(globalPos))
185         .arg(getModifiersAsString(this));
186 }
187 
toString() const188 QString QNativeMouseButtonEvent::toString() const
189 {
190     return QString("QNativeMouseButtonEvent(globalPos = %1, %2, clickCount = %3, %4)").arg(getPosAsString(globalPos))
191         .arg(getButtonAsString(this)).arg(clickCount).arg(getModifiersAsString(this));
192 }
193 
toString() const194 QString QNativeMouseDragEvent::toString() const
195 {
196     return QString("QNativeMouseDragEvent(globalPos = %1, %2, clickCount = %3, %4)").arg(getPosAsString(globalPos))
197     .arg(getButtonAsString(this)).arg(clickCount).arg(getModifiersAsString(this));
198 }
199 
toString() const200 QString QNativeMouseWheelEvent::toString() const
201 {
202     return QString("QNativeMouseWheelEvent(globalPos = %1, delta = %2, %3)").arg(getPosAsString(globalPos))
203         .arg(delta).arg(getModifiersAsString(this));
204 }
205 
toString() const206 QString QNativeKeyEvent::toString() const
207 {
208     return QString("QNativeKeyEvent(press = %1, native key code = %2, character = %3, %4)").arg(getBoolAsString(press))
209         .arg(nativeKeyCode).arg(character.isPrint() ? character : QString("<no char>"))
210         .arg(getModifiersAsString(this));
211 }
212 
toString() const213 QString QNativeModifierEvent::toString() const
214 {
215     return QString("QNativeModifierEvent(%1, native key code = %2)").arg(getModifiersAsString(this))
216         .arg(nativeKeyCode);
217 }
218 
operator <<(QDebug d,QNativeEvent * e)219 QDebug operator<<(QDebug d, QNativeEvent *e)
220 {
221     Q_UNUSED(e);
222     return d << e->toString();
223 }
224 
operator <<(QDebug d,const QNativeEvent & e)225 QDebug operator<<(QDebug d, const QNativeEvent &e)
226 {
227     Q_UNUSED(e);
228     return d << e.toString();
229 }
230 
operator <<(QTextStream & s,QNativeEvent * e)231 QTextStream &operator<<(QTextStream &s, QNativeEvent *e)
232 {
233     return s << e->eventId << " " << e->modifiers << " QNativeEvent";
234 }
235 
operator <<(QTextStream & s,QNativeMouseEvent * e)236 QTextStream &operator<<(QTextStream &s, QNativeMouseEvent *e)
237 {
238     return s << e->eventId << " " << e->globalPos.x() << " " << e->globalPos.y() << " " << e->modifiers << " " << e->toString();
239 }
240 
operator <<(QTextStream & s,QNativeMouseMoveEvent * e)241 QTextStream &operator<<(QTextStream &s, QNativeMouseMoveEvent *e)
242 {
243     return s << e->eventId << " " << e->globalPos.x() << " " << e->globalPos.y() << " " << e->modifiers << " " << e->toString();
244 }
245 
operator <<(QTextStream & s,QNativeMouseButtonEvent * e)246 QTextStream &operator<<(QTextStream &s, QNativeMouseButtonEvent *e)
247 {
248     return s << e->eventId << " " << e->globalPos.x() << " " << e->globalPos.y() << " " << e->button
249         << " " << e->clickCount << " " << e->modifiers << " " << e->toString();
250 }
251 
operator <<(QTextStream & s,QNativeMouseDragEvent * e)252 QTextStream &operator<<(QTextStream &s, QNativeMouseDragEvent *e)
253 {
254     return s << e->eventId << " " << e->globalPos.x() << " " << e->globalPos.y() << " " << e->button << " " << e->clickCount
255         << " " << e->modifiers << " " << e->toString();
256 }
257 
operator <<(QTextStream & s,QNativeMouseWheelEvent * e)258 QTextStream &operator<<(QTextStream &s, QNativeMouseWheelEvent *e)
259 {
260     return s << e->eventId << " " << e->globalPos.x() << " " << e->globalPos.y() << " " << e->delta
261         << " " << e->modifiers << " " << e->toString();
262 }
263 
operator <<(QTextStream & s,QNativeKeyEvent * e)264 QTextStream &operator<<(QTextStream &s, QNativeKeyEvent *e)
265 {
266     return s << e->eventId << " " << e->press << " " << e->nativeKeyCode << " " << e->character
267         << " " << e->modifiers << " " << e->toString();
268 }
269 
operator <<(QTextStream & s,QNativeModifierEvent * e)270 QTextStream &operator<<(QTextStream &s, QNativeModifierEvent *e)
271 {
272     return s << e->eventId << " " << e->modifiers << " " << e->nativeKeyCode << " " << e->toString();
273 }
274 
275 
276 
277 
operator >>(QTextStream & s,QNativeMouseMoveEvent * e)278 QTextStream &operator>>(QTextStream &s, QNativeMouseMoveEvent *e)
279 {
280     // Skip reading eventId.
281     QString humanReadable;
282     int x, y, modifiers;
283     s >> x >> y >> modifiers >> humanReadable;
284     e->globalPos.setX(x);
285     e->globalPos.setY(y);
286     e->modifiers = Qt::KeyboardModifiers(modifiers);
287     return s;
288 }
289 
operator >>(QTextStream & s,QNativeMouseButtonEvent * e)290 QTextStream &operator>>(QTextStream &s, QNativeMouseButtonEvent *e)
291 {
292     // Skip reading eventId.
293     QString humanReadable;
294     int x, y, button, clickCount, modifiers;
295     s >> x >> y >> button >> clickCount >> modifiers >> humanReadable;
296     e->globalPos.setX(x);
297     e->globalPos.setY(y);
298     e->clickCount = clickCount;
299     e->modifiers = Qt::KeyboardModifiers(modifiers);
300     switch (button){
301         case 1:
302             e->button = Qt::LeftButton;
303             break;
304         case 2:
305             e->button = Qt::RightButton;
306             break;
307         case 3:
308             e->button = Qt::MidButton;
309             break;
310         default:
311             e->button = Qt::NoButton;
312             break;
313     }
314     return s;
315 }
316 
operator >>(QTextStream & s,QNativeMouseDragEvent * e)317 QTextStream &operator>>(QTextStream &s, QNativeMouseDragEvent *e)
318 {
319     // Skip reading eventId.
320     QString humanReadable;
321     int x, y, button, clickCount, modifiers;
322     s >> x >> y >> button >> clickCount >> modifiers >> humanReadable;
323     e->globalPos.setX(x);
324     e->globalPos.setY(y);
325     e->clickCount = clickCount;
326     e->modifiers = Qt::KeyboardModifiers(modifiers);
327     switch (button){
328         case 1:
329             e->button = Qt::LeftButton;
330             break;
331         case 2:
332             e->button = Qt::RightButton;
333             break;
334         case 3:
335             e->button = Qt::MidButton;
336             break;
337         default:
338             e->button = Qt::NoButton;
339             break;
340     }
341     return s;
342 }
343 
operator >>(QTextStream & s,QNativeMouseWheelEvent * e)344 QTextStream &operator>>(QTextStream &s, QNativeMouseWheelEvent *e)
345 {
346     // Skip reading eventId.
347     QString humanReadable;
348     int x, y, modifiers;
349     s >> x >> y >> e->delta >> modifiers >> humanReadable;
350     e->globalPos.setX(x);
351     e->globalPos.setY(y);
352     e->modifiers = Qt::KeyboardModifiers(modifiers);
353     return s;
354 }
355 
operator >>(QTextStream & s,QNativeKeyEvent * e)356 QTextStream &operator>>(QTextStream &s, QNativeKeyEvent *e)
357 {
358     // Skip reading eventId.
359     QString humanReadable;
360     int press, modifiers;
361     QString character;
362     s >> press >> e->nativeKeyCode >> character >> modifiers >> humanReadable;
363     e->press = bool(press);
364     e->character = character[0];
365     e->modifiers = Qt::KeyboardModifiers(modifiers);
366     return s;
367 }
368 
operator >>(QTextStream & s,QNativeModifierEvent * e)369 QTextStream &operator>>(QTextStream &s, QNativeModifierEvent *e)
370 {
371     // Skip reading eventId.
372     QString humanReadable;
373     int modifiers;
374     s >> modifiers >> e->nativeKeyCode >> humanReadable;
375     e->modifiers = Qt::KeyboardModifiers(modifiers);
376     return s;
377 }
378 
379