1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "adapter/ohos/entrance/mmi_event_convertor.h"
17
18 #include <memory>
19
20 #include "pointer_event.h"
21
22 #include "base/utils/utils.h"
23
24 namespace OHOS::Ace::Platform {
25
GetSourceTool(int32_t orgToolType)26 SourceTool GetSourceTool(int32_t orgToolType)
27 {
28 switch (orgToolType) {
29 case OHOS::MMI::PointerEvent::TOOL_TYPE_FINGER:
30 return SourceTool::FINGER;
31 case OHOS::MMI::PointerEvent::TOOL_TYPE_PEN:
32 return SourceTool::PEN;
33 case OHOS::MMI::PointerEvent::TOOL_TYPE_RUBBER:
34 return SourceTool::RUBBER;
35 case OHOS::MMI::PointerEvent::TOOL_TYPE_BRUSH:
36 return SourceTool::BRUSH;
37 case OHOS::MMI::PointerEvent::TOOL_TYPE_PENCIL:
38 return SourceTool::PENCIL;
39 case OHOS::MMI::PointerEvent::TOOL_TYPE_AIRBRUSH:
40 return SourceTool::AIRBRUSH;
41 case OHOS::MMI::PointerEvent::TOOL_TYPE_MOUSE:
42 return SourceTool::MOUSE;
43 case OHOS::MMI::PointerEvent::TOOL_TYPE_LENS:
44 return SourceTool::LENS;
45 case OHOS::MMI::PointerEvent::TOOL_TYPE_TOUCHPAD:
46 return SourceTool::TOUCHPAD;
47 default:
48 LOGW("unknown tool type");
49 return SourceTool::UNKNOWN;
50 }
51 }
52
ConvertTouchPoint(const MMI::PointerEvent::PointerItem & pointerItem)53 TouchPoint ConvertTouchPoint(const MMI::PointerEvent::PointerItem& pointerItem)
54 {
55 TouchPoint touchPoint;
56 // just get the max of width and height
57 touchPoint.size = std::max(pointerItem.GetWidth(), pointerItem.GetHeight()) / 2.0;
58 touchPoint.id = pointerItem.GetPointerId();
59 touchPoint.downTime = TimeStamp(std::chrono::microseconds(pointerItem.GetDownTime()));
60 touchPoint.x = pointerItem.GetWindowX();
61 touchPoint.y = pointerItem.GetWindowY();
62 touchPoint.screenX = pointerItem.GetDisplayX();
63 touchPoint.screenY = pointerItem.GetDisplayY();
64 touchPoint.isPressed = pointerItem.IsPressed();
65 touchPoint.force = static_cast<float>(pointerItem.GetPressure());
66 touchPoint.tiltX = pointerItem.GetTiltX();
67 touchPoint.tiltY = pointerItem.GetTiltY();
68 touchPoint.sourceTool = GetSourceTool(pointerItem.GetToolType());
69 return touchPoint;
70 }
71
UpdateTouchEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent,TouchEvent & touchEvent)72 void UpdateTouchEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent, TouchEvent& touchEvent)
73 {
74 auto ids = pointerEvent->GetPointerIds();
75 for (auto&& id : ids) {
76 MMI::PointerEvent::PointerItem item;
77 bool ret = pointerEvent->GetPointerItem(id, item);
78 if (!ret) {
79 LOGE("get pointer item failed.");
80 continue;
81 }
82 auto touchPoint = ConvertTouchPoint(item);
83 touchEvent.pointers.emplace_back(std::move(touchPoint));
84 }
85 touchEvent.CovertId();
86 }
87
ConvertTouchEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent)88 TouchEvent ConvertTouchEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
89 {
90 int32_t pointerID = pointerEvent->GetPointerId();
91 MMI::PointerEvent::PointerItem item;
92 bool ret = pointerEvent->GetPointerItem(pointerID, item);
93 if (!ret) {
94 LOGE("get pointer item failed.");
95 return TouchEvent();
96 }
97 auto touchPoint = ConvertTouchPoint(item);
98 std::chrono::microseconds microseconds(pointerEvent->GetActionTime());
99 TimeStamp time(microseconds);
100 TouchEvent event { touchPoint.id, touchPoint.x, touchPoint.y, touchPoint.screenX, touchPoint.screenY,
101 TouchType::UNKNOWN, TouchType::UNKNOWN, time, touchPoint.size, touchPoint.force, touchPoint.tiltX,
102 touchPoint.tiltY, pointerEvent->GetDeviceId(), pointerEvent->GetTargetDisplayId(), SourceType::NONE,
103 touchPoint.sourceTool };
104 event.pointerEvent = pointerEvent;
105 #ifdef SECURITY_COMPONENT_ENABLE
106 event.enhanceData = pointerEvent->GetEnhanceData();
107 #endif
108 int32_t orgDevice = pointerEvent->GetSourceType();
109 GetEventDevice(orgDevice, event);
110 int32_t orgAction = pointerEvent->GetPointerAction();
111 switch (orgAction) {
112 case OHOS::MMI::PointerEvent::POINTER_ACTION_CANCEL:
113 event.type = TouchType::CANCEL;
114 break;
115 case OHOS::MMI::PointerEvent::POINTER_ACTION_DOWN:
116 event.type = TouchType::DOWN;
117 break;
118 case OHOS::MMI::PointerEvent::POINTER_ACTION_MOVE:
119 event.type = TouchType::MOVE;
120 break;
121 case OHOS::MMI::PointerEvent::POINTER_ACTION_UP:
122 event.type = TouchType::UP;
123 break;
124 case OHOS::MMI::PointerEvent::POINTER_ACTION_PULL_DOWN:
125 event.type = TouchType::PULL_DOWN;
126 event.pullType = TouchType::PULL_DOWN;
127 break;
128 case OHOS::MMI::PointerEvent::POINTER_ACTION_PULL_MOVE:
129 event.type = TouchType::PULL_MOVE;
130 event.pullType = TouchType::PULL_MOVE;
131 break;
132 case OHOS::MMI::PointerEvent::POINTER_ACTION_PULL_UP:
133 event.type = TouchType::PULL_UP;
134 event.pullType = TouchType::PULL_UP;
135 break;
136 case OHOS::MMI::PointerEvent::POINTER_ACTION_PULL_IN_WINDOW:
137 event.type = TouchType::PULL_IN_WINDOW;
138 event.pullType = TouchType::PULL_IN_WINDOW;
139 break;
140 case OHOS::MMI::PointerEvent::POINTER_ACTION_PULL_OUT_WINDOW:
141 event.type = TouchType::PULL_OUT_WINDOW;
142 event.pullType = TouchType::PULL_OUT_WINDOW;
143 break;
144 default:
145 LOGW("unknown type");
146 break;
147 }
148 UpdateTouchEvent(pointerEvent, event);
149 return event;
150 }
151
GetMouseEventAction(int32_t action,MouseEvent & events,bool isScenceBoardWindow)152 void GetMouseEventAction(int32_t action, MouseEvent& events, bool isScenceBoardWindow)
153 {
154 switch (action) {
155 case OHOS::MMI::PointerEvent::POINTER_ACTION_BUTTON_DOWN:
156 events.action = MouseAction::PRESS;
157 break;
158 case OHOS::MMI::PointerEvent::POINTER_ACTION_BUTTON_UP:
159 events.action = MouseAction::RELEASE;
160 break;
161 case OHOS::MMI::PointerEvent::POINTER_ACTION_ENTER_WINDOW:
162 events.action = MouseAction::WINDOW_ENTER;
163 break;
164 case OHOS::MMI::PointerEvent::POINTER_ACTION_LEAVE_WINDOW:
165 events.action = MouseAction::WINDOW_LEAVE;
166 break;
167 case OHOS::MMI::PointerEvent::POINTER_ACTION_MOVE:
168 events.action = MouseAction::MOVE;
169 break;
170 #ifdef ENABLE_DRAG_FRAMEWORK
171 case OHOS::MMI::PointerEvent::POINTER_ACTION_PULL_DOWN:
172 events.action = MouseAction::PRESS;
173 if (isScenceBoardWindow) {
174 events.pullAction = MouseAction::PULL_DOWN;
175 }
176 break;
177 case OHOS::MMI::PointerEvent::POINTER_ACTION_PULL_MOVE:
178 events.action = MouseAction::MOVE;
179 if (isScenceBoardWindow) {
180 events.pullAction = MouseAction::PULL_MOVE;
181 }
182 break;
183 case OHOS::MMI::PointerEvent::POINTER_ACTION_PULL_UP:
184 events.action = MouseAction::RELEASE;
185 if (isScenceBoardWindow) {
186 events.pullAction = MouseAction::PULL_UP;
187 }
188 break;
189 #endif // ENABLE_DRAG_FRAMEWORK
190 default:
191 events.action = MouseAction::NONE;
192 break;
193 }
194 }
195
GetMouseEventButton(int32_t button,MouseEvent & events)196 void GetMouseEventButton(int32_t button, MouseEvent& events)
197 {
198 switch (button) {
199 case OHOS::MMI::PointerEvent::MOUSE_BUTTON_LEFT:
200 events.button = MouseButton::LEFT_BUTTON;
201 break;
202 case OHOS::MMI::PointerEvent::MOUSE_BUTTON_RIGHT:
203 events.button = MouseButton::RIGHT_BUTTON;
204 break;
205 case OHOS::MMI::PointerEvent::MOUSE_BUTTON_MIDDLE:
206 events.button = MouseButton::MIDDLE_BUTTON;
207 break;
208 case OHOS::MMI::PointerEvent::MOUSE_BUTTON_SIDE:
209 events.button = MouseButton::BACK_BUTTON;
210 break;
211 case OHOS::MMI::PointerEvent::MOUSE_BUTTON_EXTRA:
212 events.button = MouseButton::FORWARD_BUTTON;
213 break;
214 default:
215 events.button = MouseButton::NONE_BUTTON;
216 break;
217 }
218 }
219
ConvertMouseEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent,MouseEvent & events,bool isScenceBoardWindow)220 void ConvertMouseEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent,
221 MouseEvent& events, bool isScenceBoardWindow)
222 {
223 int32_t pointerID = pointerEvent->GetPointerId();
224 MMI::PointerEvent::PointerItem item;
225 bool ret = pointerEvent->GetPointerItem(pointerID, item);
226 if (!ret) {
227 LOGE("get pointer: %{public}d item failed.", pointerID);
228 return;
229 }
230 events.id = pointerID;
231 events.x = item.GetWindowX();
232 events.y = item.GetWindowY();
233 events.screenX = item.GetDisplayX();
234 events.screenY = item.GetDisplayY();
235 int32_t orgAction = pointerEvent->GetPointerAction();
236 GetMouseEventAction(orgAction, events, isScenceBoardWindow);
237 int32_t orgButton = pointerEvent->GetButtonId();
238 GetMouseEventButton(orgButton, events);
239 int32_t orgDevice = pointerEvent->GetSourceType();
240 GetEventDevice(orgDevice, events);
241 events.targetDisplayId = pointerEvent->GetTargetDisplayId();
242
243 std::set<int32_t> pressedSet = pointerEvent->GetPressedButtons();
244 uint32_t pressedButtons = 0;
245 if (pressedSet.find(OHOS::MMI::PointerEvent::MOUSE_BUTTON_LEFT) != pressedSet.end()) {
246 pressedButtons &= static_cast<uint32_t>(MouseButton::LEFT_BUTTON);
247 }
248 if (pressedSet.find(OHOS::MMI::PointerEvent::MOUSE_BUTTON_RIGHT) != pressedSet.end()) {
249 pressedButtons &= static_cast<uint32_t>(MouseButton::RIGHT_BUTTON);
250 }
251 if (pressedSet.find(OHOS::MMI::PointerEvent::MOUSE_BUTTON_MIDDLE) != pressedSet.end()) {
252 pressedButtons &= static_cast<uint32_t>(MouseButton::MIDDLE_BUTTON);
253 }
254 events.pressedButtons = static_cast<int32_t>(pressedButtons);
255
256 std::chrono::microseconds microseconds(pointerEvent->GetActionTime());
257 TimeStamp time(microseconds);
258 events.time = time;
259 events.pointerEvent = pointerEvent;
260 #ifdef SECURITY_COMPONENT_ENABLE
261 events.enhanceData = pointerEvent->GetEnhanceData();
262 #endif
263 LOGD("ConvertMouseEvent: id: %{public}d (x,y): (%{public}f,%{public}f). Button: %{public}d. Action: %{public}d. "
264 "DeviceType: %{public}d. PressedButton: %{public}d. Time: %{public}lld",
265 events.id, events.x, events.y, events.button, events.action, events.sourceType, events.pressedButtons,
266 (long long)pointerEvent->GetActionTime());
267 }
268
GetAxisEventAction(int32_t action,AxisEvent & event)269 void GetAxisEventAction(int32_t action, AxisEvent& event)
270 {
271 switch (action) {
272 case OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_BEGIN:
273 event.action = AxisAction::BEGIN;
274 break;
275 case OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_UPDATE:
276 event.action = AxisAction::UPDATE;
277 break;
278 case OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_END:
279 event.action = AxisAction::END;
280 break;
281 default:
282 event.action = AxisAction::NONE;
283 break;
284 }
285 }
286
ConvertAxisEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent,AxisEvent & event)287 void ConvertAxisEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent, AxisEvent& event)
288 {
289 int32_t pointerID = pointerEvent->GetPointerId();
290 MMI::PointerEvent::PointerItem item;
291 bool ret = pointerEvent->GetPointerItem(pointerID, item);
292 if (!ret) {
293 LOGE("get pointer: %{public}d item failed.", pointerID);
294 return;
295 }
296
297 event.id = item.GetPointerId();
298 event.x = static_cast<float>(item.GetWindowX());
299 event.y = static_cast<float>(item.GetWindowY());
300 event.screenX = static_cast<float>(item.GetDisplayX());
301 event.screenY = static_cast<float>(item.GetDisplayY());
302 event.horizontalAxis = pointerEvent->GetAxisValue(OHOS::MMI::PointerEvent::AxisType::AXIS_TYPE_SCROLL_HORIZONTAL);
303 event.verticalAxis = pointerEvent->GetAxisValue(OHOS::MMI::PointerEvent::AxisType::AXIS_TYPE_SCROLL_VERTICAL);
304 event.pinchAxisScale = pointerEvent->GetAxisValue(OHOS::MMI::PointerEvent::AxisType::AXIS_TYPE_PINCH);
305 int32_t orgAction = pointerEvent->GetPointerAction();
306 GetAxisEventAction(orgAction, event);
307 int32_t orgDevice = pointerEvent->GetSourceType();
308 GetEventDevice(orgDevice, event);
309 event.sourceTool = GetSourceTool(item.GetToolType());
310 event.pointerEvent = pointerEvent;
311
312 std::chrono::microseconds microseconds(pointerEvent->GetActionTime());
313 TimeStamp time(microseconds);
314 event.time = time;
315 LOGD("ConvertAxisEvent: id: %{public}d, (x,y): (%{public}f,%{public}f). HorizontalAxis: %{public}f. VerticalAxis: "
316 "%{public}f. "
317 "Action: %{public}d. SourceType: %{public}d. ToolType: %{public}d. Time: %{public}lld",
318 event.id, event.x, event.y, event.horizontalAxis, event.verticalAxis, event.action, event.sourceType,
319 event.sourceTool, (long long)pointerEvent->GetActionTime());
320 }
321
ConvertKeyEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent,KeyEvent & event)322 void ConvertKeyEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent, KeyEvent& event)
323 {
324 event.rawKeyEvent = keyEvent;
325 event.code = static_cast<KeyCode>(keyEvent->GetKeyCode());
326 event.keyIntention = static_cast<KeyIntention>(keyEvent->GetKeyIntention());
327 if (keyEvent->GetKeyAction() == OHOS::MMI::KeyEvent::KEY_ACTION_UP) {
328 event.action = KeyAction::UP;
329 } else if (keyEvent->GetKeyAction() == OHOS::MMI::KeyEvent::KEY_ACTION_DOWN) {
330 event.action = KeyAction::DOWN;
331 } else {
332 event.action = KeyAction::UNKNOWN;
333 }
334 std::chrono::microseconds microseconds(keyEvent->GetActionTime());
335 TimeStamp time(microseconds);
336 event.timeStamp = time;
337 event.key = MMI::KeyEvent::KeyCodeToString(keyEvent->GetKeyCode());
338 event.deviceId = keyEvent->GetDeviceId();
339 event.sourceType = SourceType::KEYBOARD;
340 std::string pressedKeyStr = "Pressed Keys: ";
341 for (const auto& curCode : keyEvent->GetPressedKeys()) {
342 pressedKeyStr += (std::to_string(curCode) + " ");
343 event.pressedCodes.emplace_back(static_cast<KeyCode>(curCode));
344 }
345 LOGD("ConvertKeyEvent: KeyCode: %{private}d. KeyAction: %{public}d. PressedCodes: %{private}s. Time: %{public}lld",
346 event.code, event.action, pressedKeyStr.c_str(), (long long)(keyEvent->GetActionTime()));
347 }
348
LogPointInfo(const std::shared_ptr<MMI::PointerEvent> & pointerEvent)349 void LogPointInfo(const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
350 {
351 if (SystemProperties::GetDebugEnabled()) {
352 LOGI("point source: %{public}d", pointerEvent->GetSourceType());
353 auto actionId = pointerEvent->GetPointerId();
354 MMI::PointerEvent::PointerItem item;
355 if (pointerEvent->GetPointerItem(actionId, item)) {
356 LOGI("action point info: id: %{public}d, x: %{public}d, y: %{public}d, action: %{public}d, pressure: "
357 "%{public}f, tiltX: %{public}f, tiltY: %{public}f",
358 actionId, item.GetWindowX(), item.GetWindowY(), pointerEvent->GetPointerAction(), item.GetPressure(),
359 item.GetTiltX(), item.GetTiltY());
360 }
361 auto ids = pointerEvent->GetPointerIds();
362 for (auto&& id : ids) {
363 MMI::PointerEvent::PointerItem item;
364 if (pointerEvent->GetPointerItem(id, item)) {
365 LOGI("all point info: id: %{public}d, x: %{public}d, y: %{public}d, isPressed: %{public}d, pressure: "
366 "%{public}f, tiltX: %{public}f, tiltY: %{public}f",
367 actionId, item.GetWindowX(), item.GetWindowY(), item.IsPressed(), item.GetPressure(),
368 item.GetTiltX(), item.GetTiltY());
369 }
370 }
371 }
372 }
373
CalculatePointerEvent(const NG::OffsetF & offsetF,const std::shared_ptr<MMI::PointerEvent> & point,const NG::VectorF & scale)374 void CalculatePointerEvent(
375 const NG::OffsetF& offsetF, const std::shared_ptr<MMI::PointerEvent>& point, const NG::VectorF& scale)
376 {
377 CHECK_NULL_VOID(point);
378 int32_t pointerId = point->GetPointerId();
379 MMI::PointerEvent::PointerItem item;
380 bool ret = point->GetPointerItem(pointerId, item);
381 if (ret) {
382 float xRelative = item.GetWindowX() - offsetF.GetX();
383 float yRelative = item.GetWindowY() - offsetF.GetY();
384 float xBeforeScale = NearZero(scale.x) ? xRelative : xRelative / scale.x;
385 float yBeforeScale = NearZero(scale.y) ? yRelative : yRelative / scale.y;
386
387 item.SetWindowX(static_cast<int32_t>(xBeforeScale));
388 item.SetWindowY(static_cast<int32_t>(yBeforeScale));
389 point->UpdatePointerItem(pointerId, item);
390 }
391 }
392
CalculateWindowCoordinate(const NG::OffsetF & offsetF,const std::shared_ptr<MMI::PointerEvent> & point,const NG::VectorF & scale)393 void CalculateWindowCoordinate(
394 const NG::OffsetF& offsetF, const std::shared_ptr<MMI::PointerEvent>& point, const NG::VectorF& scale)
395 {
396 CHECK_NULL_VOID(point);
397 int32_t pointerId = point->GetPointerId();
398 MMI::PointerEvent::PointerItem item;
399 bool ret = point->GetPointerItem(pointerId, item);
400 if (ret) {
401 float xRelative = item.GetDisplayX() - offsetF.GetX();
402 float yRelative = item.GetDisplayY() - offsetF.GetY();
403 float windowX = NearZero(scale.x) ? xRelative : xRelative / scale.x;
404 float windowY = NearZero(scale.y) ? yRelative : yRelative / scale.y;
405
406 item.SetWindowX(static_cast<int32_t>(windowX));
407 item.SetWindowY(static_cast<int32_t>(windowY));
408 point->UpdatePointerItem(pointerId, item);
409 }
410 }
411 } // namespace OHOS::Ace::Platform
412