From d7bc61ac518f2b780f60e895327e21c24a4e21e9 Mon Sep 17 00:00:00 2001 From: Shawn Rutledge Date: Fri, 22 Dec 2023 21:48:59 -0700 Subject: [PATCH] wasm: Handle stylus events by generating QTabletEvents Pick-to: 6.6 Fixes: QTBUG-120327 Change-Id: I37a92b9850385712b638c30f9a43028d8134f416 Reviewed-by: Lorn Potter (cherry picked from commit bc578ec6efcf667e0be2ea5c3d68bd22135cadd0) Reviewed-by: Qt Cherry-pick Bot --- src/plugins/platforms/wasm/qwasmevent.cpp | 4 +++ src/plugins/platforms/wasm/qwasmevent.h | 4 +++ .../platforms/wasm/qwasmintegration.cpp | 2 ++ src/plugins/platforms/wasm/qwasmscreen.cpp | 10 ++++++++ src/plugins/platforms/wasm/qwasmscreen.h | 2 ++ src/plugins/platforms/wasm/qwasmwindow.cpp | 2 +- .../platforms/wasm/qwasmwindowclientarea.cpp | 25 +++++++++++++++++++ 7 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/plugins/platforms/wasm/qwasmevent.cpp b/src/plugins/platforms/wasm/qwasmevent.cpp index 98df694b552..5ee17e193b4 100644 --- a/src/plugins/platforms/wasm/qwasmevent.cpp +++ b/src/plugins/platforms/wasm/qwasmevent.cpp @@ -187,6 +187,10 @@ PointerEvent::PointerEvent(EventType type, emscripten::val event) : MouseEvent(t width = event["width"].as(); height = event["height"].as(); pressure = event["pressure"].as(); + tiltX = event["tiltX"].as(); + tiltY = event["tiltY"].as(); + tangentialPressure = event["tangentialPressure"].as(); + twist = event["twist"].as(); isPrimary = event["isPrimary"].as(); } diff --git a/src/plugins/platforms/wasm/qwasmevent.h b/src/plugins/platforms/wasm/qwasmevent.h index 610f61dd668..6ada5393e3e 100644 --- a/src/plugins/platforms/wasm/qwasmevent.h +++ b/src/plugins/platforms/wasm/qwasmevent.h @@ -221,6 +221,10 @@ struct PointerEvent : public MouseEvent PointerType pointerType; int pointerId; qreal pressure; + qreal tiltX; + qreal tiltY; + qreal tangentialPressure; + qreal twist; qreal width; qreal height; bool isPrimary; diff --git a/src/plugins/platforms/wasm/qwasmintegration.cpp b/src/plugins/platforms/wasm/qwasmintegration.cpp index 1cfbf759855..e6e6693e03a 100644 --- a/src/plugins/platforms/wasm/qwasmintegration.cpp +++ b/src/plugins/platforms/wasm/qwasmintegration.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -94,6 +95,7 @@ QWasmIntegration::QWasmIntegration() qt_set_sequence_auto_mnemonic(false); touchPoints = emscripten::val::global("navigator")["maxTouchPoints"].as(); + QWindowSystemInterfacePrivate::TabletEvent::setPlatformSynthesizesMouse(false); // Create screens for container elements. Each container element will ultimately become a // div element. Qt historically supported supplying canvas for screen elements - these elements diff --git a/src/plugins/platforms/wasm/qwasmscreen.cpp b/src/plugins/platforms/wasm/qwasmscreen.cpp index bed98b5c616..ddf8140c48d 100644 --- a/src/plugins/platforms/wasm/qwasmscreen.cpp +++ b/src/plugins/platforms/wasm/qwasmscreen.cpp @@ -92,6 +92,16 @@ QWasmScreen::QWasmScreen(const emscripten::val &containerOrCanvas) QPointingDevice::Capability::Position | QPointingDevice::Capability::Area | QPointingDevice::Capability::NormalizedPosition, 10, 0); + m_tabletDevice = std::make_unique( + "stylus", 2, QInputDevice::DeviceType::Stylus, + QPointingDevice::PointerType::Pen, + QPointingDevice::Capability::Position | QPointingDevice::Capability::Pressure + | QPointingDevice::Capability::NormalizedPosition + | QInputDevice::Capability::MouseEmulation + | QInputDevice::Capability::Hover | QInputDevice::Capability::Rotation + | QInputDevice::Capability::XTilt | QInputDevice::Capability::YTilt + | QInputDevice::Capability::TangentialPressure, + 0, 0); QWindowSystemInterface::registerInputDevice(m_touchDevice.get()); } diff --git a/src/plugins/platforms/wasm/qwasmscreen.h b/src/plugins/platforms/wasm/qwasmscreen.h index e860b5d4655..da171d3f507 100644 --- a/src/plugins/platforms/wasm/qwasmscreen.h +++ b/src/plugins/platforms/wasm/qwasmscreen.h @@ -39,6 +39,7 @@ public: QString eventTargetId() const; QString outerScreenId() const; QPointingDevice *touchDevice() { return m_touchDevice.get(); } + QPointingDevice *tabletDevice() { return m_tabletDevice.get(); } QWasmCompositor *compositor(); QWasmDeadKeySupport *deadKeySupport() { return m_deadKeySupport.get(); } @@ -82,6 +83,7 @@ private: emscripten::val m_shadowContainer; std::unique_ptr m_compositor; std::unique_ptr m_touchDevice; + std::unique_ptr m_tabletDevice; std::unique_ptr m_deadKeySupport; QRect m_geometry = QRect(0, 0, 100, 100); int m_depth = 32; diff --git a/src/plugins/platforms/wasm/qwasmwindow.cpp b/src/plugins/platforms/wasm/qwasmwindow.cpp index 7a1ab876835..249d1fdfc93 100644 --- a/src/plugins/platforms/wasm/qwasmwindow.cpp +++ b/src/plugins/platforms/wasm/qwasmwindow.cpp @@ -514,7 +514,7 @@ bool QWasmWindow::processKey(const KeyEvent &event) bool QWasmWindow::processPointer(const PointerEvent &event) { - if (event.pointerType != PointerType::Mouse) + if (event.pointerType != PointerType::Mouse && event.pointerType != PointerType::Pen) return false; switch (event.type) { diff --git a/src/plugins/platforms/wasm/qwasmwindowclientarea.cpp b/src/plugins/platforms/wasm/qwasmwindowclientarea.cpp index 047b5f432cc..cc78b8dcd58 100644 --- a/src/plugins/platforms/wasm/qwasmwindowclientarea.cpp +++ b/src/plugins/platforms/wasm/qwasmwindowclientarea.cpp @@ -105,6 +105,31 @@ bool ClientArea::deliverEvent(const PointerEvent &event) eventType, event.modifiers); } + if (event.pointerType == PointerType::Pen) { + qreal pressure; + switch (event.type) { + case EventType::PointerDown : + case EventType::PointerMove : + pressure = event.pressure; + break; + case EventType::PointerUp : + pressure = 0.0; + break; + default: + return false; + } + // Tilt in the browser is in the range +-90, but QTabletEvent only goes to +-60. + qreal xTilt = qBound(-60.0, event.tiltX, 60.0); + qreal yTilt = qBound(-60.0, event.tiltY, 60.0); + // Barrel rotation is reported as 0 to 359, but QTabletEvent wants a signed value. + qreal rotation = event.twist > 180.0 ? 360.0 - event.twist : event.twist; + return QWindowSystemInterface::handleTabletEvent( + m_window->window(), QWasmIntegration::getTimestamp(), m_screen->tabletDevice(), + m_window->window()->mapFromGlobal(targetPointClippedToScreen), + targetPointClippedToScreen, event.mouseButtons, pressure, xTilt, yTilt, + event.tangentialPressure, rotation, event.modifiers); + } + QWindowSystemInterface::TouchPoint *touchPoint; QPointF pointInTargetWindowCoords =