From a599c69dc523a8f906a9c970f3dc2b78e6a843f1 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 Fixes: QTBUG-120327 Change-Id: I37a92b9850385712b638c30f9a43028d8134f416 Reviewed-by: Lorn Potter (cherry picked from commit bc578ec6efcf667e0be2ea5c3d68bd22135cadd0) Reviewed-by: Qt Cherry-pick Bot (cherry picked from commit d7bc61ac518f2b780f60e895327e21c24a4e21e9) --- 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 ec37c64b2da..52514def8ad 100644 --- a/src/plugins/platforms/wasm/qwasmevent.cpp +++ b/src/plugins/platforms/wasm/qwasmevent.cpp @@ -184,6 +184,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 3b2e5bfc52d..e59618a9be3 100644 --- a/src/plugins/platforms/wasm/qwasmevent.h +++ b/src/plugins/platforms/wasm/qwasmevent.h @@ -215,6 +215,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 b93b8bb6f9c..be391f4aff2 100644 --- a/src/plugins/platforms/wasm/qwasmintegration.cpp +++ b/src/plugins/platforms/wasm/qwasmintegration.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -95,6 +96,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 c035de4a339..3aee11f43fd 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 e0195549f98..4906907b606 100644 --- a/src/plugins/platforms/wasm/qwasmscreen.h +++ b/src/plugins/platforms/wasm/qwasmscreen.h @@ -37,6 +37,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(); } @@ -70,6 +71,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 de592bd6484..23ba913296d 100644 --- a/src/plugins/platforms/wasm/qwasmwindow.cpp +++ b/src/plugins/platforms/wasm/qwasmwindow.cpp @@ -490,7 +490,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 aa8cfeebb2e..f1f0833c9a9 100644 --- a/src/plugins/platforms/wasm/qwasmwindowclientarea.cpp +++ b/src/plugins/platforms/wasm/qwasmwindowclientarea.cpp @@ -91,6 +91,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 =