Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qohosnativemouseeventshandler.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <QtCore/private/qohoscommon_p.h>
5#include <chrono>
6#include <cstdint>
7#include <functional>
8#include <optional>
9#include <qarkui/input.h>
10#include <render/qohosbatchingrequestshandler.h>
11#include <render/qohosnativemouseeventshandler.h>
12#include <vector>
13
14namespace ch = std::chrono;
15
16QT_BEGIN_NAMESPACE
17
18namespace {
19
20constexpr auto mouseMotionEventMinAgeForDrop = ch::milliseconds(20);
21
23{
24 switch (button) {
25 case ::UI_MOUSE_EVENT_BUTTON_NONE:
26 return std::nullopt;
27 case ::UI_MOUSE_EVENT_BUTTON_LEFT:
28 return Qt::LeftButton;
29 case ::UI_MOUSE_EVENT_BUTTON_RIGHT:
30 return Qt::RightButton;
31 case ::UI_MOUSE_EVENT_BUTTON_MIDDLE:
32 return Qt::MiddleButton;
33 case ::UI_MOUSE_EVENT_BUTTON_BACK:
34 return Qt::BackButton;
35 case ::UI_MOUSE_EVENT_BUTTON_FORWARD:
36 return Qt::ForwardButton;
37 }
38
39 return std::nullopt;
40};
41
43{
44 switch (action) {
45 case ::UI_MOUSE_EVENT_ACTION_UNKNOWN:
46 return std::nullopt;
47 case ::UI_MOUSE_EVENT_ACTION_PRESS:
48 return QEvent::MouseButtonPress;
49 case ::UI_MOUSE_EVENT_ACTION_RELEASE:
50 return QEvent::MouseButtonRelease;
51 case ::UI_MOUSE_EVENT_ACTION_MOVE:
52 return QEvent::MouseMove;
53 }
54
55 return std::nullopt;
56}
57
58class QOhosNativeNodeMouseInputHandler final : public std::enable_shared_from_this<QOhosNativeNodeMouseInputHandler>
59{
60public:
62 QtOhos::QThreadSafeRef<QWindow> qWindowRef,
63 QtOhos::QThreadSafeRef<QOhosInputMethodEventHandler> imEventHandlerRef);
64
65 void handleMouseEvent(QArkUi::NativeNodeMouseEvent nativeNodeMouseEvent);
66
67private:
68 struct MouseEvent
69 {
70 ch::steady_clock::time_point timestamp;
71 QOhosMouseEvent mouseEvent;
72 };
73
74 void processMouseEventsInQtThread(std::vector<MouseEvent> &&batch);
75
76 static bool mayDropMouseEvent(
77 ch::steady_clock::time_point now, const MouseEvent &event, const MouseEvent &nextEvent);
78
79 QtOhos::QThreadSafeRef<QWindow> m_qWindowRef;
80 QtOhos::QThreadSafeRef<QOhosInputMethodEventHandler> m_imEventHandlerRef;
81
82 std::function<void(std::function<void(std::vector<MouseEvent> &)>)> m_optMouseEventsHandler;
83};
84
85QOhosNativeNodeMouseInputHandler::QOhosNativeNodeMouseInputHandler(
86 QtOhos::QThreadSafeRef<QWindow> qWindowRef,
87 QtOhos::QThreadSafeRef<QOhosInputMethodEventHandler> imEventHandlerRef)
90{
91}
92
93void QOhosNativeNodeMouseInputHandler::handleMouseEvent(QArkUi::NativeNodeMouseEvent nativeNodeMouseEvent)
94{
95 auto eventType = tryMapNativeNodeMouseActionToQt(nativeNodeMouseEvent.action);
96 if (!eventType.has_value()) {
97 qOhosPrintfDebug(
98 "%s: got unsupported action in mouse event (%d), ignoring",
99 Q_FUNC_INFO, nativeNodeMouseEvent.action);
100 return;
101 }
102
103 QOhosMouseEvent mouseEvent = {
104 .targetWindow = {},
105 .timestampMs = nativeNodeMouseEvent.timestampMs,
106 .localPosition = nativeNodeMouseEvent.localPosition,
107 .globalPosition = nativeNodeMouseEvent.globalPosition,
108 .button = tryMapNativeNodeMouseButtonToQt(nativeNodeMouseEvent.button).value_or(Qt::NoButton),
109 .eventType = eventType.value(),
110 .modifiers = nativeNodeMouseEvent.modifiers,
111 .deviceType = nativeNodeMouseEvent.deviceType,
112 };
113
114 if (!m_optMouseEventsHandler) {
115 auto weakSelf = QtOhos::makeWeakPtr(shared_from_this());
116 m_optMouseEventsHandler = makeQtOhosBatchingQtRequestsHandler<std::vector<MouseEvent>>(
117 m_imEventHandlerRef.toQObjectThreadSafeRef(),
118 [weakSelf](std::vector<MouseEvent> &&batch) {
119 auto sharedSelf = weakSelf.lock();
120 if (sharedSelf)
121 sharedSelf->processMouseEventsInQtThread(std::move(batch));
122 });
123 }
124
125 m_optMouseEventsHandler(
126 [&](std::vector<MouseEvent> &batch) {
127 auto now = std::chrono::steady_clock::now();
128 MouseEvent newEvent{now, mouseEvent};
129 if (!batch.empty() && mayDropMouseEvent(now, batch.back(), newEvent))
130 batch.pop_back();
131 batch.push_back(newEvent);
132 });
133}
134
135void QOhosNativeNodeMouseInputHandler::processMouseEventsInQtThread(std::vector<MouseEvent> &&batch)
136{
137 auto now = ch::steady_clock::now();
138
139 QWindow *targetWindow = m_qWindowRef.data();
140 if (targetWindow == nullptr) {
141 qOhosPrintfWarning(
142 "%s: QWindow reference '%s' for node not valid. Rejecting mouse events batch.",
143 Q_FUNC_INFO, m_qWindowRef.refName().c_str());
144 return;
145 }
146
147 QOhosInputMethodEventHandler *eventHandler = m_imEventHandlerRef.data();
148 if (eventHandler == nullptr) {
149 qOhosPrintfWarning(
150 "%s: Input method event handler referece '%s' for node not valid. Rejecting mouse events batch.",
151 Q_FUNC_INFO, m_qWindowRef.refName().c_str());
152 return;
153 }
154
155 for (std::size_t i = 0; i < batch.size(); ++i) {
156 if (i + 1 < batch.size() && mayDropMouseEvent(now, batch[i], batch[i + 1]))
157 continue;
158
159 auto mouseEvent = batch[i].mouseEvent;
160 mouseEvent.targetWindow = targetWindow;
161 eventHandler->onMouseEvent(mouseEvent);
162 }
163}
164
165bool QOhosNativeNodeMouseInputHandler::mayDropMouseEvent(
166 ch::steady_clock::time_point now, const MouseEvent &event, const MouseEvent &nextEvent)
167{
168 return
169 now - event.timestamp >= mouseMotionEventMinAgeForDrop
170 && event.mouseEvent.eventType == QEvent::MouseMove
171 && nextEvent.mouseEvent.eventType == QEvent::MouseMove;
172}
173
174}
175
177 QtOhos::QThreadSafeRef<QWindow> qWindowRef,
178 QtOhos::QThreadSafeRef<QOhosInputMethodEventHandler> imEventHandlerRef)
179{
180 auto mouseInputHandler = std::make_shared<QOhosNativeNodeMouseInputHandler>(qWindowRef, imEventHandlerRef);
181 return [mouseInputHandler](QArkUi::NativeNodeMouseEvent nativeNodeMouseEvent) {
182 mouseInputHandler->handleMouseEvent(nativeNodeMouseEvent);
183 };
184}
185
186QT_END_NAMESPACE
void onMouseEvent(const QOhosMouseEvent &mouseEvent)
QOhosNativeNodeMouseInputHandler(QtOhos::QThreadSafeRef< QWindow > qWindowRef, QtOhos::QThreadSafeRef< QOhosInputMethodEventHandler > imEventHandlerRef)
void handleMouseEvent(QArkUi::NativeNodeMouseEvent nativeNodeMouseEvent)
std::optional< Qt::MouseButton > tryMapNativeNodeMouseButtonToQt(std::int32_t button)
std::optional< QEvent::Type > tryMapNativeNodeMouseActionToQt(std::int32_t action)
QOhosConsumer< QArkUi::NativeNodeMouseEvent > makeQOhosNativeMouseEventsHandler(QtOhos::QThreadSafeRef< QWindow > qWindowRef, QtOhos::QThreadSafeRef< QOhosInputMethodEventHandler > imEventHandlerRef)