-
Notifications
You must be signed in to change notification settings - Fork 2
/
qtuiohandler.cpp
337 lines (284 loc) · 12.3 KB
/
qtuiohandler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/****************************************************************************
**
** Copyright (C) 2014 Robin Burchell <[email protected]>
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QLoggingCategory>
#include <QRect>
#include <QWindow>
#include <QGuiApplication>
#include <qpa/qwindowsysteminterface.h>
#include "qtuiocursor_p.h"
#include "qtuiohandler_p.h"
#include "qoscbundle_p.h"
QT_BEGIN_NAMESPACE
Q_LOGGING_CATEGORY(lcTuioSource, "qt.qpa.tuio.source")
Q_LOGGING_CATEGORY(lcTuioSet, "qt.qpa.tuio.set")
QTuioHandler::QTuioHandler(const QString &specification)
: m_device(new QTouchDevice) // not leaked, QTouchDevice cleans up registered devices itself
{
QStringList args = specification.split(':');
int portNumber = 3333;
int rotationAngle = 0;
bool invertx = false;
bool inverty = false;
for (int i = 0; i < args.count(); ++i) {
if (args.at(i).startsWith("udp=")) {
QString portString = args.at(i).section('=', 1, 1);
portNumber = portString.toInt();
} else if (args.at(i).startsWith("tcp=")) {
QString portString = args.at(i).section('=', 1, 1);
portNumber = portString.toInt();
qWarning() << "TCP is not yet supported. Falling back to UDP on " << portNumber;
} else if (args.at(i) == "invertx") {
invertx = true;
} else if (args.at(i) == "inverty") {
inverty = true;
} else if (args.at(i).startsWith("rotate=")) {
QString rotateArg = args.at(i).section('=', 1, 1);
int argValue = rotateArg.toInt();
switch (argValue) {
case 90:
case 180:
case 270:
rotationAngle = argValue;
default:
break;
}
}
}
if (rotationAngle)
m_transform = QTransform::fromTranslate(0.5, 0.5).rotate(rotationAngle).translate(-0.5, -0.5);
if (invertx)
m_transform *= QTransform::fromTranslate(0.5, 0.5).scale(-1.0, 1.0).translate(-0.5, -0.5);
if (inverty)
m_transform *= QTransform::fromTranslate(0.5, 0.5).scale(1.0, -1.0).translate(-0.5, -0.5);
m_device->setName("TUIO"); // TODO: multiple based on SOURCE?
m_device->setType(QTouchDevice::TouchScreen);
m_device->setCapabilities(QTouchDevice::Position |
QTouchDevice::Area |
QTouchDevice::Velocity |
QTouchDevice::NormalizedPosition);
QWindowSystemInterface::registerTouchDevice(m_device);
if (!m_socket.bind(QHostAddress::Any, portNumber)) {
qWarning() << "Failed to bind TUIO socket: " << m_socket.errorString();
return;
}
connect(&m_socket, &QUdpSocket::readyRead, this, &QTuioHandler::processPackets);
}
QTuioHandler::~QTuioHandler()
{
}
void QTuioHandler::processPackets()
{
while (m_socket.hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(m_socket.pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
qint64 size = m_socket.readDatagram(datagram.data(), datagram.size(),
&sender, &senderPort);
if (size == -1)
continue;
if (size != datagram.size())
datagram.resize(size);
QOscBundle bundle(datagram);
if (!bundle.isValid())
continue;
// "A typical TUIO bundle will contain an initial ALIVE message,
// followed by an arbitrary number of SET messages that can fit into the
// actual bundle capacity and a concluding FSEQ message. A minimal TUIO
// bundle needs to contain at least the compulsory ALIVE and FSEQ
// messages. The FSEQ frame ID is incremented for each delivered bundle,
// while redundant bundles can be marked using the frame sequence ID
// -1."
QList<QOscMessage> messages = bundle.messages();
foreach (const QOscMessage &message, messages) {
if (message.addressPattern() != "/tuio/2Dcur") {
qWarning() << "Ignoring unknown address pattern " << message.addressPattern();
continue;
}
QList<QVariant> arguments = message.arguments();
if (arguments.count() == 0) {
qWarning() << "Ignoring TUIO message with no arguments";
continue;
}
QByteArray messageType = arguments.at(0).toByteArray();
if (messageType == "source") {
process2DCurSource(message);
} else if (messageType == "alive") {
process2DCurAlive(message);
} else if (messageType == "set") {
process2DCurSet(message);
} else if (messageType == "fseq") {
process2DCurFseq(message);
} else {
qWarning() << "Ignoring unknown TUIO message type: " << messageType;
continue;
}
}
}
}
void QTuioHandler::process2DCurSource(const QOscMessage &message)
{
QList<QVariant> arguments = message.arguments();
if (arguments.count() != 2) {
qWarning() << "Ignoring malformed TUIO source message: " << arguments.count();
return;
}
if (QMetaType::Type(arguments.at(1).type()) != QMetaType::QByteArray) {
qWarning() << "Ignoring malformed TUIO source message (bad argument type)";
return;
}
qCDebug(lcTuioSource) << "Got TUIO source message from: " << arguments.at(1).toByteArray();
}
void QTuioHandler::process2DCurAlive(const QOscMessage &message)
{
QList<QVariant> arguments = message.arguments();
// delta the notified cursors that are active, against the ones we already
// know of.
//
// TBD: right now we're assuming one 2Dcur alive message corresponds to a
// new data source from the input. is this correct, or do we need to store
// changes and only process the deltas on fseq?
QMap<int, QTuioCursor> oldActiveCursors = m_activeCursors;
QMap<int, QTuioCursor> newActiveCursors;
for (int i = 1; i < arguments.count(); ++i) {
if (QMetaType::Type(arguments.at(i).type()) != QMetaType::Int) {
qWarning() << "Ignoring malformed TUIO alive message (bad argument on position" << i << arguments << ")";
return;
}
int cursorId = arguments.at(i).toInt();
if (!oldActiveCursors.contains(cursorId)) {
// newly active
QTuioCursor cursor(cursorId);
cursor.setState(Qt::TouchPointPressed);
newActiveCursors.insert(cursorId, cursor);
} else {
// we already know about it, remove it so it isn't marked as released
QTuioCursor cursor = oldActiveCursors.value(cursorId);
cursor.setState(Qt::TouchPointStationary); // position change in SET will update if needed
newActiveCursors.insert(cursorId, cursor);
oldActiveCursors.remove(cursorId);
}
}
// anything left is dead now
QMap<int, QTuioCursor>::ConstIterator it = oldActiveCursors.constBegin();
// deadCursors should be cleared from the last FSEQ now
m_deadCursors.reserve(oldActiveCursors.size());
// TODO: there could be an issue of resource exhaustion here if FSEQ isn't
// sent in a timely fashion. we should probably track message counts and
// force-flush if we get too many built up.
while (it != oldActiveCursors.constEnd()) {
m_deadCursors.append(it.value());
++it;
}
m_activeCursors = newActiveCursors;
}
void QTuioHandler::process2DCurSet(const QOscMessage &message)
{
QList<QVariant> arguments = message.arguments();
if (arguments.count() < 7) {
qWarning() << "Ignoring malformed TUIO set message with too few arguments: " << arguments.count();
return;
}
if (QMetaType::Type(arguments.at(1).type()) != QMetaType::Int ||
QMetaType::Type(arguments.at(2).type()) != QMetaType::Float ||
QMetaType::Type(arguments.at(3).type()) != QMetaType::Float ||
QMetaType::Type(arguments.at(4).type()) != QMetaType::Float ||
QMetaType::Type(arguments.at(5).type()) != QMetaType::Float ||
QMetaType::Type(arguments.at(6).type()) != QMetaType::Float
) {
qWarning() << "Ignoring malformed TUIO set message with bad types: " << arguments;
return;
}
int cursorId = arguments.at(1).toInt();
float x = arguments.at(2).toFloat();
float y = arguments.at(3).toFloat();
float vx = arguments.at(4).toFloat();
float vy = arguments.at(5).toFloat();
float acceleration = arguments.at(6).toFloat();
QMap<int, QTuioCursor>::Iterator it = m_activeCursors.find(cursorId);
if (it == m_activeCursors.end()) {
qWarning() << "Ignoring malformed TUIO set for nonexistent cursor " << cursorId;
return;
}
qCDebug(lcTuioSet) << "Processing SET for " << cursorId << " x: " << x << y << vx << vy << acceleration;
QTuioCursor &cur = *it;
cur.setX(x);
cur.setY(y);
cur.setVX(vx);
cur.setVY(vy);
cur.setAcceleration(acceleration);
}
QWindowSystemInterface::TouchPoint QTuioHandler::cursorToTouchPoint(const QTuioCursor &tc, QWindow *win)
{
QWindowSystemInterface::TouchPoint tp;
tp.id = tc.id();
tp.pressure = 1.0f;
tp.normalPosition = QPointF(tc.x(), tc.y());
if (!m_transform.isIdentity())
tp.normalPosition = m_transform.map(tp.normalPosition);
tp.state = tc.state();
tp.area = QRectF(0, 0, 1, 1);
// we map the touch to the size of the window. we do this, because frankly,
// trying to figure out which part of the screen to hit in order to press an
// element on the UI is pretty tricky when one is not using an overlay-style
// TUIO device.
//
// in the future, it might make sense to make this choice optional,
// dependent on the spec.
QPointF relPos = QPointF(win->size().width() * tp.normalPosition.x(), win->size().height() * tp.normalPosition.y());
QPointF delta = relPos - relPos.toPoint();
tp.area.moveCenter(win->mapToGlobal(relPos.toPoint()) + delta);
tp.velocity = QVector2D(win->size().width() * tc.vx(), win->size().height() * tc.vy());
return tp;
}
void QTuioHandler::process2DCurFseq(const QOscMessage &message)
{
Q_UNUSED(message); // TODO: do we need to do anything with the frame id?
QWindow *win = QGuiApplication::focusWindow();
if (!win)
return;
QList<QWindowSystemInterface::TouchPoint> tpl;
foreach (const QTuioCursor &tc, m_activeCursors) {
QWindowSystemInterface::TouchPoint tp = cursorToTouchPoint(tc, win);
tpl.append(tp);
}
foreach (const QTuioCursor &tc, m_deadCursors) {
QWindowSystemInterface::TouchPoint tp = cursorToTouchPoint(tc, win);
tp.state = Qt::TouchPointReleased;
tpl.append(tp);
}
QWindowSystemInterface::handleTouchEvent(win, m_device, tpl);
m_deadCursors.clear();
}
QT_END_NAMESPACE