summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLorn Potter <lorn.potter@gmail.com>2018-06-19 10:53:08 +1000
committerLorn Potter <lorn.potter@gmail.com>2018-06-20 19:30:51 +0000
commit2d0ecb48a8b6b6932789bf3bbeec817231a60ac7 (patch)
tree562550fd233b54d2db19eba904539ce51f4a2891
parent17c450bb9a18c2db70da86436554236cc95429f1 (diff)
wasm: add touch event support
Task-number: QTBUG-68347 Change-Id: Icafdaa1ddb9f724abe730302c7a07aa5b74a64b7 Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
-rw-r--r--src/plugins/platforms/html5/qhtml5eventtranslator.cpp71
-rw-r--r--src/plugins/platforms/html5/qhtml5eventtranslator.h5
2 files changed, 75 insertions, 1 deletions
diff --git a/src/plugins/platforms/html5/qhtml5eventtranslator.cpp b/src/plugins/platforms/html5/qhtml5eventtranslator.cpp
index b6113f3fdb..1fc40fdd9f 100644
--- a/src/plugins/platforms/html5/qhtml5eventtranslator.cpp
+++ b/src/plugins/platforms/html5/qhtml5eventtranslator.cpp
@@ -37,6 +37,9 @@
#include <qpa/qwindowsysteminterface.h>
#include <QCoreApplication>
#include <QtGlobal>
+#include <QObject>
+
+#include <QtCore/qdeadlinetimer.h>
#include <iostream>
@@ -63,6 +66,16 @@ QHtml5EventTranslator::QHtml5EventTranslator(QObject *parent)
emscripten_set_wheel_callback(0, (void *)this, 1, &wheel_cb);
+ touchDevice = new QTouchDevice;
+ touchDevice->setType(QTouchDevice::TouchScreen);
+ touchDevice->setCapabilities(QTouchDevice::Position | QTouchDevice::Area | QTouchDevice::NormalizedPosition);
+ QWindowSystemInterface::registerTouchDevice(touchDevice);
+
+ emscripten_set_touchstart_callback("#canvas", (void *)this, 1, &touchCallback);
+ emscripten_set_touchend_callback("#canvas", (void *)this, 1, &touchCallback);
+ emscripten_set_touchmove_callback("#canvas", (void *)this, 1, &touchCallback);
+ emscripten_set_touchcancel_callback("#canvas", (void *)this, 1, &touchCallback);
+
// The Platform Detect: expand coverage and move as needed
enum Platform {
GenericPlatform,
@@ -442,7 +455,63 @@ int QHtml5EventTranslator::wheel_cb(int eventType, const EmscriptenWheelEvent *w
if (wheelEvent->deltaX != 0) pixelDelta.setX(wheelEvent->deltaX * scrollFactor);
QWindowSystemInterface::handleWheelEvent(window2, timestamp, localPoint, globalPoint, QPoint(), pixelDelta, modifiers);
+}
+
+int QHtml5EventTranslator::touchCallback(int eventType, const EmscriptenTouchEvent *touchEvent, void *userData)
+{
+ QList<QWindowSystemInterface::TouchPoint> touchPointList;
+ touchPointList.reserve(touchEvent->numTouches);
+ QWindow *window2;
+
+ for (int i = 0; i < touchEvent->numTouches; i++) {
+
+ const EmscriptenTouchPoint *touches = &touchEvent->touches[i];
+
+ QPoint point(touches->canvasX, touches->canvasY);
+ window2 = QHtml5Integration::get()->compositor()->windowAt(point, 5);
+
+ QWindowSystemInterface::TouchPoint touchPoint;
+
+ auto cX = point.x();
+ auto cY = point.y();
+ touchPoint.area = QRect(0, 0, 8, 8);
+ touchPoint.area.moveCenter(QPointF(cX,cY)); // simulate area
+
+ touchPoint.id = touches->identifier;
+ touchPoint.normalPosition = QPointF(cX / window2->width(), cY / window2->height());
+
+ switch (eventType) {
+ case EMSCRIPTEN_EVENT_TOUCHSTART:
+ touchPoint.state = Qt::TouchPointPressed;
+ break;
+ case EMSCRIPTEN_EVENT_TOUCHEND:
+ touchPoint.state = Qt::TouchPointReleased;
+ break;
+ case EMSCRIPTEN_EVENT_TOUCHMOVE:
+ touchPoint.state = Qt::TouchPointMoved;
+ break;
+ default:
+ Q_UNREACHABLE();
+ }
+ touchPointList.append(touchPoint);
+ }
+
+ QHtml5EventTranslator *html5EventTranslator = (QHtml5EventTranslator*)userData;
+ QFlags<Qt::KeyboardModifier> keyModifier = translatKeyModifier(touchEvent);
+
+ if (eventType != EMSCRIPTEN_EVENT_TOUCHCANCEL)
+ QWindowSystemInterface::handleTouchEvent<QWindowSystemInterface::SynchronousDelivery>(window2, html5EventTranslator->getTimestamp(), html5EventTranslator->touchDevice, touchPointList, keyModifier);
+ else
+ QWindowSystemInterface::handleTouchCancelEvent(window2, html5EventTranslator->getTimestamp(), html5EventTranslator->touchDevice, keyModifier);
+
+ QCoreApplication::processEvents();
+ return 1;
+}
+
+quint64 QHtml5EventTranslator::getTimestamp()
+{
+ return QDeadlineTimer::current().deadlineNSecs() / 1000;
}
- QT_END_NAMESPACE
+QT_END_NAMESPACE
diff --git a/src/plugins/platforms/html5/qhtml5eventtranslator.h b/src/plugins/platforms/html5/qhtml5eventtranslator.h
index d9a3b9039a..9821b41630 100644
--- a/src/plugins/platforms/html5/qhtml5eventtranslator.h
+++ b/src/plugins/platforms/html5/qhtml5eventtranslator.h
@@ -35,6 +35,7 @@
#include <QtCore/QPoint>
#include <emscripten/html5.h>
#include "qhtml5window.h"
+#include <QTouchDevice>
QT_BEGIN_NAMESPACE
@@ -178,6 +179,8 @@ public:
static int focus_cb(int eventType, const EmscriptenFocusEvent *focusEvent, void *userData);
static int wheel_cb(int eventType, const EmscriptenWheelEvent *wheelEvent, void *userData);
+ static int touchCallback(int eventType, const EmscriptenTouchEvent *ev, void *userData);
+
void processEvents();
Q_SIGNALS:
@@ -200,6 +203,8 @@ private:
QHtml5Window::ResizeMode resizeMode;
QPoint resizePoint;
QRect resizeStartRect;
+ QTouchDevice* touchDevice;
+ quint64 getTimestamp();
};
QT_END_NAMESPACE