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 {};
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 {};
40};
41
43{
44 switch (action) {
45 case ::UI_MOUSE_EVENT_ACTION_UNKNOWN:
46 return {};
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 {};
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 std::shared_ptr<QOhosHoverEventsGenerator> hoverEventsGenerator);
65
66 void handleMouseEvent(QArkUi::NativeNodeMouseEvent nativeNodeMouseEvent);
67
68private:
69 struct MouseEvent
70 {
71 ch::steady_clock::time_point timestamp;
72 QOhosMouseEvent mouseEvent;
73 };
74
75 void processMouseEventsInQtThread(std::vector<MouseEvent> &&batch);
76
77 static bool mayDropMouseEvent(
78 ch::steady_clock::time_point now, const MouseEvent &event, const MouseEvent &nextEvent);
79
80 QtOhos::QThreadSafeRef<QWindow> m_qWindowRef;
81 QtOhos::QThreadSafeRef<QOhosInputMethodEventHandler> m_imEventHandlerRef;
82 std::shared_ptr<QOhosHoverEventsGenerator> m_hoverEventsGenerator;
83
84 std::function<void(std::function<void(std::vector<MouseEvent> &)>)> m_optMouseEventsHandler;
85};
86
87QOhosNativeNodeMouseInputHandler::QOhosNativeNodeMouseInputHandler(
88 QtOhos::QThreadSafeRef<QWindow> qWindowRef,
89 QtOhos::QThreadSafeRef<QOhosInputMethodEventHandler> imEventHandlerRef,
90 std::shared_ptr<QOhosHoverEventsGenerator> hoverEventsGenerator)
94{
95}
96
97void QOhosNativeNodeMouseInputHandler::handleMouseEvent(QArkUi::NativeNodeMouseEvent nativeNodeMouseEvent)
98{
99 auto eventType = tryMapNativeNodeMouseActionToQt(nativeNodeMouseEvent.action);
100 if (!eventType.has_value()) {
101 qOhosPrintfDebug(
102 "%s: got unsupported action in mouse event (%d), ignoring",
103 Q_FUNC_INFO, nativeNodeMouseEvent.action);
104 return;
105 }
106
107 QOhosMouseEvent mouseEvent = {
108 .timestampMs = nativeNodeMouseEvent.timestampMs,
109 .localPosition = nativeNodeMouseEvent.localPosition,
110 .globalPosition = nativeNodeMouseEvent.globalPosition,
111 .button = tryMapNativeNodeMouseButtonToQt(nativeNodeMouseEvent.button).value_or(Qt::NoButton),
112 .eventType = eventType.value(),
113 .modifiers = nativeNodeMouseEvent.modifiers,
114 .deviceType = nativeNodeMouseEvent.deviceType,
115 };
116
117 m_hoverEventsGenerator->handleQOhosMouseEvent(mouseEvent);
118
119 if (!m_optMouseEventsHandler) {
120 auto weakSelf = QtOhos::makeWeakPtr(shared_from_this());
121 m_optMouseEventsHandler = makeQtOhosBatchingQtRequestsHandler<std::vector<MouseEvent>>(
122 m_imEventHandlerRef.toQObjectThreadSafeRef(),
123 [weakSelf](std::vector<MouseEvent> &&batch) {
124 auto sharedSelf = weakSelf.lock();
125 if (sharedSelf)
126 sharedSelf->processMouseEventsInQtThread(std::move(batch));
127 });
128 }
129
130 m_optMouseEventsHandler(
131 [&](std::vector<MouseEvent> &batch) {
132 auto now = std::chrono::steady_clock::now();
133 MouseEvent newEvent{now, mouseEvent};
134 if (!batch.empty() && mayDropMouseEvent(now, batch.back(), newEvent))
135 batch.pop_back();
136 batch.push_back(newEvent);
137 });
138}
139
140void QOhosNativeNodeMouseInputHandler::processMouseEventsInQtThread(std::vector<MouseEvent> &&batch)
141{
142 auto now = ch::steady_clock::now();
143
144 QWindow *targetWindow = m_qWindowRef.data();
145 if (targetWindow == nullptr) {
146 qOhosPrintfWarning(
147 "%s: QWindow reference '%s' for node not valid. Rejecting mouse events batch.",
148 Q_FUNC_INFO, m_qWindowRef.refName().c_str());
149 return;
150 }
151
152 QOhosInputMethodEventHandler *eventHandler = m_imEventHandlerRef.data();
153 if (eventHandler == nullptr) {
154 qOhosPrintfWarning(
155 "%s: Input method event handler referece '%s' for node not valid. Rejecting mouse events batch.",
156 Q_FUNC_INFO, m_qWindowRef.refName().c_str());
157 return;
158 }
159
160 for (std::size_t i = 0; i < batch.size(); ++i) {
161 if (i + 1 < batch.size() && mayDropMouseEvent(now, batch[i], batch[i + 1]))
162 continue;
163
164 auto mouseEvent = batch[i].mouseEvent;
165 mouseEvent.targetWindow = targetWindow;
166 eventHandler->onMouseEvent(mouseEvent);
167 }
168}
169
170bool QOhosNativeNodeMouseInputHandler::mayDropMouseEvent(
171 ch::steady_clock::time_point now, const MouseEvent &event, const MouseEvent &nextEvent)
172{
173 return
174 now - event.timestamp >= mouseMotionEventMinAgeForDrop
175 && event.mouseEvent.eventType == QEvent::MouseMove
176 && nextEvent.mouseEvent.eventType == QEvent::MouseMove;
177}
178
179}
180
182 QtOhos::QThreadSafeRef<QWindow> qWindowRef,
183 QtOhos::QThreadSafeRef<QOhosInputMethodEventHandler> imEventHandlerRef,
184 std::shared_ptr<QOhosHoverEventsGenerator> hoverEventsGenerator)
185{
186 auto mouseInputHandler = std::make_shared<QOhosNativeNodeMouseInputHandler>(qWindowRef, imEventHandlerRef, hoverEventsGenerator);
187 return [mouseInputHandler](QArkUi::NativeNodeMouseEvent nativeNodeMouseEvent) {
188 mouseInputHandler->handleMouseEvent(nativeNodeMouseEvent);
189 };
190}
191
192QT_END_NAMESPACE
void onMouseEvent(const QOhosMouseEvent &mouseEvent)
QOhosNativeNodeMouseInputHandler(QtOhos::QThreadSafeRef< QWindow > qWindowRef, QtOhos::QThreadSafeRef< QOhosInputMethodEventHandler > imEventHandlerRef, std::shared_ptr< QOhosHoverEventsGenerator > hoverEventsGenerator)
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, std::shared_ptr< QOhosHoverEventsGenerator > hoverEventsGenerator)