• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "input_manager.h"
21 #include "pointer_event.h"
22 
23 #include "adapter/ohos/entrance/ace_extra_input_data.h"
24 #include "base/utils/utils.h"
25 #include "core/event/ace_events.h"
26 #include "core/event/key_event.h"
27 #include "core/pipeline/pipeline_base.h"
28 #include "adapter/ohos/entrance/ace_container.h"
29 #include "adapter/ohos/entrance/tsa_advanced_feature.h"
30 
31 namespace OHOS::Ace::Platform {
32 namespace {
33 constexpr int32_t ANGLE_0 = 0;
34 constexpr int32_t ANGLE_90 = 90;
35 constexpr int32_t ANGLE_180 = 180;
36 constexpr int32_t ANGLE_270 = 270;
37 constexpr double SIZE_DIVIDE = 2.0;
38 } // namespace
39 
GetSourceTool(int32_t orgToolType)40 SourceTool GetSourceTool(int32_t orgToolType)
41 {
42     switch (orgToolType) {
43         case OHOS::MMI::PointerEvent::TOOL_TYPE_FINGER:
44             return SourceTool::FINGER;
45         case OHOS::MMI::PointerEvent::TOOL_TYPE_PEN:
46             return SourceTool::PEN;
47         case OHOS::MMI::PointerEvent::TOOL_TYPE_RUBBER:
48             return SourceTool::RUBBER;
49         case OHOS::MMI::PointerEvent::TOOL_TYPE_BRUSH:
50             return SourceTool::BRUSH;
51         case OHOS::MMI::PointerEvent::TOOL_TYPE_PENCIL:
52             return SourceTool::PENCIL;
53         case OHOS::MMI::PointerEvent::TOOL_TYPE_AIRBRUSH:
54             return SourceTool::AIRBRUSH;
55         case OHOS::MMI::PointerEvent::TOOL_TYPE_MOUSE:
56             return SourceTool::MOUSE;
57         case OHOS::MMI::PointerEvent::TOOL_TYPE_LENS:
58             return SourceTool::LENS;
59         case OHOS::MMI::PointerEvent::TOOL_TYPE_TOUCHPAD:
60             return SourceTool::TOUCHPAD;
61         default:
62             LOGW("unknown tool type");
63             return SourceTool::UNKNOWN;
64     }
65 }
66 
ConvertTouchPoint(const MMI::PointerEvent::PointerItem & pointerItem)67 TouchPoint ConvertTouchPoint(const MMI::PointerEvent::PointerItem& pointerItem)
68 {
69     TouchPoint touchPoint;
70     // just get the max of width and height
71     touchPoint.size = std::max(pointerItem.GetWidth(), pointerItem.GetHeight()) / 2.0;
72     touchPoint.id = pointerItem.GetPointerId();
73     touchPoint.downTime = TimeStamp(std::chrono::microseconds(pointerItem.GetDownTime()));
74     touchPoint.x = pointerItem.GetWindowX();
75     touchPoint.y = pointerItem.GetWindowY();
76     touchPoint.screenX = pointerItem.GetDisplayX();
77     touchPoint.screenY = pointerItem.GetDisplayY();
78     touchPoint.isPressed = pointerItem.IsPressed();
79     touchPoint.force = static_cast<float>(pointerItem.GetPressure());
80     touchPoint.tiltX = pointerItem.GetTiltX();
81     touchPoint.tiltY = pointerItem.GetTiltY();
82     touchPoint.sourceTool = GetSourceTool(pointerItem.GetToolType());
83     touchPoint.originalId = pointerItem.GetOriginPointerId();
84     touchPoint.width = pointerItem.GetWidth();
85     touchPoint.height = pointerItem.GetHeight();
86     int32_t blobId = pointerItem.GetBlobId();
87     if (blobId < 0) {
88         touchPoint.operatingHand = 0;
89     } else {
90         touchPoint.operatingHand = static_cast<int32_t>(static_cast<uint32_t>(blobId) &
91             (OPERATING_HAND_LEFT | OPERATING_HAND_RIGHT));
92     }
93     return touchPoint;
94 }
95 
UpdateTouchEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent,TouchEvent & touchEvent)96 void UpdateTouchEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent, TouchEvent& touchEvent)
97 {
98     auto ids = pointerEvent->GetPointerIds();
99     for (auto&& id : ids) {
100         MMI::PointerEvent::PointerItem item;
101         bool ret = pointerEvent->GetPointerItem(id, item);
102         if (!ret) {
103             LOGE("get pointer item failed.");
104             continue;
105         }
106         auto touchPoint = ConvertTouchPoint(item);
107         touchPoint.CovertId();
108         touchEvent.pointers.emplace_back(std::move(touchPoint));
109     }
110     touchEvent.CovertId();
111 }
112 
GetTouchEventOriginOffset(const TouchEvent & event)113 Offset GetTouchEventOriginOffset(const TouchEvent& event)
114 {
115     auto pointerEvent = event.pointerEvent;
116     if (!pointerEvent) {
117         return Offset();
118     }
119     int32_t pointerID = pointerEvent->GetPointerId();
120     MMI::PointerEvent::PointerItem item;
121     bool ret = pointerEvent->GetPointerItem(pointerID, item);
122     if (!ret) {
123         return Offset();
124     } else {
125         return Offset(item.GetWindowX(), item.GetWindowY());
126     }
127 }
128 
GetTouchEventOriginTimeStamp(const TouchEvent & event)129 TimeStamp GetTouchEventOriginTimeStamp(const TouchEvent& event)
130 {
131     auto pointerEvent = event.pointerEvent;
132     if (!pointerEvent) {
133         return event.time;
134     }
135     std::chrono::microseconds microseconds(pointerEvent->GetActionTime());
136     TimeStamp time(microseconds);
137     return time;
138 }
139 
UpdatePressedKeyCodes(std::vector<KeyCode> & pressedKeyCodes)140 void UpdatePressedKeyCodes(std::vector<KeyCode>& pressedKeyCodes)
141 {
142     auto inputManager = MMI::InputManager::GetInstance();
143     CHECK_NULL_VOID(inputManager);
144 
145     std::vector<int32_t> pressedKeys;
146     std::map<int32_t, int32_t> specialKeysState;
147     pressedKeyCodes.clear();
148     auto ret = inputManager->GetKeyState(pressedKeys, specialKeysState);
149     if (ret == 0) {
150         for (const auto& curCode : pressedKeys) {
151             pressedKeyCodes.emplace_back(static_cast<KeyCode>(curCode));
152         }
153     }
154 }
155 
UpdateMouseEventForPen(const MMI::PointerEvent::PointerItem & pointerItem,MouseEvent & mouseEvent)156 void UpdateMouseEventForPen(const MMI::PointerEvent::PointerItem& pointerItem, MouseEvent& mouseEvent)
157 {
158     if (mouseEvent.sourceType != SourceType::TOUCH || mouseEvent.sourceTool != SourceTool::PEN) {
159         return;
160     }
161     mouseEvent.id = TOUCH_TOOL_BASE_ID + static_cast<int32_t>(mouseEvent.sourceTool);
162     // Pen use type double XY position.
163     mouseEvent.x = pointerItem.GetWindowXPos();
164     mouseEvent.y = pointerItem.GetWindowYPos();
165     mouseEvent.screenX = pointerItem.GetDisplayXPos();
166     mouseEvent.screenY = pointerItem.GetDisplayYPos();
167     mouseEvent.originalId = mouseEvent.id;
168 }
169 
SetClonedPointerEvent(const MMI::PointerEvent * pointerEvent,ArkUITouchEvent * arkUITouchEventCloned)170 void SetClonedPointerEvent(const MMI::PointerEvent* pointerEvent, ArkUITouchEvent* arkUITouchEventCloned)
171 {
172     if (pointerEvent) {
173         MMI::PointerEvent* clonedEvent = new MMI::PointerEvent(*pointerEvent);
174         arkUITouchEventCloned->rawPointerEvent = clonedEvent;
175     }
176 }
177 
SetPostPointerEvent(TouchEvent & touchEvent,ArkUITouchEvent * arkUITouchEventCloned)178 void SetPostPointerEvent(TouchEvent& touchEvent, ArkUITouchEvent* arkUITouchEventCloned)
179 {
180     MMI::PointerEvent* pointerEvent = reinterpret_cast<MMI::PointerEvent*>(arkUITouchEventCloned->rawPointerEvent);
181     if (pointerEvent) {
182         MMI::PointerEvent* clonedEvent = new MMI::PointerEvent(*pointerEvent);
183         arkUITouchEventCloned->rawPointerEvent = clonedEvent;
184     }
185     std::shared_ptr<MMI::PointerEvent> pointer(pointerEvent);
186     touchEvent.SetPointerEvent(pointer);
187 }
188 
DestroyRawPointerEvent(ArkUITouchEvent * arkUITouchEvent)189 void DestroyRawPointerEvent(ArkUITouchEvent* arkUITouchEvent)
190 {
191     MMI::PointerEvent* pointerEvent = reinterpret_cast<MMI::PointerEvent*>(arkUITouchEvent->rawPointerEvent);
192     if (pointerEvent) {
193         delete pointerEvent;
194         pointerEvent = nullptr;
195     }
196 }
197 
ConvertTouchEventFromTouchPoint(TouchPoint touchPoint)198 TouchEvent ConvertTouchEventFromTouchPoint(TouchPoint touchPoint)
199 {
200     TouchEvent event;
201     event.SetId(touchPoint.id)
202         .SetX(touchPoint.x)
203         .SetY(touchPoint.y)
204         .SetScreenX(touchPoint.screenX)
205         .SetScreenY(touchPoint.screenY)
206         .SetType(TouchType::UNKNOWN)
207         .SetPullType(TouchType::UNKNOWN)
208         .SetSize(touchPoint.size)
209         .SetForce(touchPoint.force)
210         .SetTiltX(touchPoint.tiltX)
211         .SetTiltY(touchPoint.tiltY)
212         .SetSourceType(SourceType::NONE)
213         .SetSourceTool(touchPoint.sourceTool)
214         .SetOriginalId(touchPoint.originalId)
215         .SetSourceType(SourceType::NONE)
216         .SetPressedTime(touchPoint.downTime)
217         .SetWidth(touchPoint.width)
218         .SetHeight(touchPoint.height);
219     return event;
220 }
221 
ConvertTouchEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent)222 TouchEvent ConvertTouchEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
223 {
224     int32_t pointerID = pointerEvent->GetPointerId();
225     MMI::PointerEvent::PointerItem item;
226     bool ret = pointerEvent->GetPointerItem(pointerID, item);
227     if (!ret) {
228         LOGE("get pointer item failed.");
229         return TouchEvent();
230     }
231     auto touchPoint = ConvertTouchPoint(item);
232     TouchEvent event = ConvertTouchEventFromTouchPoint(touchPoint);
233     std::chrono::microseconds microseconds(pointerEvent->GetActionTime());
234     TimeStamp time(microseconds);
235     event.SetTime(time)
236         .SetDeviceId(pointerEvent->GetDeviceId())
237         .SetTargetDisplayId(pointerEvent->GetTargetDisplayId())
238         .SetTouchEventId(pointerEvent->GetId())
239         .SetOperatingHand(touchPoint.operatingHand);
240     AceExtraInputData::ReadToTouchEvent(pointerEvent, event);
241     event.pointerEvent = pointerEvent;
242     int32_t orgDevice = pointerEvent->GetSourceType();
243     GetEventDevice(orgDevice, event);
244     int32_t orgAction = pointerEvent->GetPointerAction();
245     SetTouchEventType(orgAction, event);
246     event.isPrivacyMode = pointerEvent->HasFlag(OHOS::MMI::InputEvent::EVENT_FLAG_PRIVACY_MODE);
247     UpdateTouchEvent(pointerEvent, event);
248     if (event.sourceType == SourceType::TOUCH && event.sourceTool == SourceTool::PEN) {
249         // Pen use type double XY position.
250         event.x = item.GetWindowXPos();
251         event.y = item.GetWindowYPos();
252         event.screenX = item.GetDisplayXPos();
253         event.screenY = item.GetDisplayYPos();
254     }
255     event.pressedKeyCodes_.clear();
256     for (const auto& curCode : pointerEvent->GetPressedKeys()) {
257         event.pressedKeyCodes_.emplace_back(static_cast<KeyCode>(curCode));
258     }
259     return event;
260 }
261 
SetTouchEventType(int32_t orgAction,TouchEvent & event)262 void SetTouchEventType(int32_t orgAction, TouchEvent& event)
263 {
264     std::map<int32_t, TouchType> actionMap = {
265         { OHOS::MMI::PointerEvent::POINTER_ACTION_CANCEL, TouchType::CANCEL },
266         { OHOS::MMI::PointerEvent::POINTER_ACTION_DOWN, TouchType::DOWN },
267         { OHOS::MMI::PointerEvent::POINTER_ACTION_MOVE, TouchType::MOVE },
268         { OHOS::MMI::PointerEvent::POINTER_ACTION_UP, TouchType::UP },
269         { OHOS::MMI::PointerEvent::POINTER_ACTION_PULL_DOWN, TouchType::PULL_DOWN },
270         { OHOS::MMI::PointerEvent::POINTER_ACTION_PULL_MOVE, TouchType::PULL_MOVE },
271         { OHOS::MMI::PointerEvent::POINTER_ACTION_PULL_UP, TouchType::PULL_UP },
272         { OHOS::MMI::PointerEvent::POINTER_ACTION_PULL_IN_WINDOW, TouchType::PULL_IN_WINDOW },
273         { OHOS::MMI::PointerEvent::POINTER_ACTION_PULL_OUT_WINDOW, TouchType::PULL_OUT_WINDOW },
274         { OHOS::MMI::PointerEvent::POINTER_ACTION_HOVER_ENTER, TouchType::HOVER_ENTER },
275         { OHOS::MMI::PointerEvent::POINTER_ACTION_HOVER_MOVE, TouchType::HOVER_MOVE },
276         { OHOS::MMI::PointerEvent::POINTER_ACTION_HOVER_EXIT, TouchType::HOVER_EXIT },
277         { OHOS::MMI::PointerEvent::POINTER_ACTION_HOVER_CANCEL, TouchType::HOVER_CANCEL },
278         { OHOS::MMI::PointerEvent::POINTER_ACTION_PROXIMITY_IN, TouchType::PROXIMITY_IN },
279         { OHOS::MMI::PointerEvent::POINTER_ACTION_PROXIMITY_OUT, TouchType::PROXIMITY_OUT },
280     };
281     auto typeIter = actionMap.find(orgAction);
282     if (typeIter == actionMap.end()) {
283         TAG_LOGI(AceLogTag::ACE_INPUTKEYFLOW, "unknown touch type");
284         return;
285     }
286     event.type = typeIter->second;
287     if (typeIter->second == TouchType::PULL_DOWN || typeIter->second == TouchType::PULL_MOVE ||
288         typeIter->second == TouchType::PULL_UP || typeIter->second == TouchType::PULL_IN_WINDOW ||
289         typeIter->second == TouchType::PULL_OUT_WINDOW) {
290         event.pullType = typeIter->second;
291     }
292 }
293 
GetMouseEventAction(int32_t action,MouseEvent & events,bool isScenceBoardWindow)294 void GetMouseEventAction(int32_t action, MouseEvent& events, bool isScenceBoardWindow)
295 {
296     switch (action) {
297         case OHOS::MMI::PointerEvent::POINTER_ACTION_BUTTON_DOWN:
298             events.action = MouseAction::PRESS;
299             break;
300         case OHOS::MMI::PointerEvent::POINTER_ACTION_BUTTON_UP:
301             events.action = MouseAction::RELEASE;
302             break;
303         case OHOS::MMI::PointerEvent::POINTER_ACTION_ENTER_WINDOW:
304             events.action = MouseAction::WINDOW_ENTER;
305             break;
306         case OHOS::MMI::PointerEvent::POINTER_ACTION_LEAVE_WINDOW:
307             events.action = MouseAction::WINDOW_LEAVE;
308             break;
309         case OHOS::MMI::PointerEvent::POINTER_ACTION_MOVE:
310             events.action = MouseAction::MOVE;
311             break;
312         case OHOS::MMI::PointerEvent::POINTER_ACTION_PULL_DOWN:
313             events.action = MouseAction::PRESS;
314             events.pullAction = MouseAction::PULL_DOWN;
315             break;
316         case OHOS::MMI::PointerEvent::POINTER_ACTION_PULL_MOVE:
317             events.action = MouseAction::MOVE;
318             events.pullAction = MouseAction::PULL_MOVE;
319             break;
320         case OHOS::MMI::PointerEvent::POINTER_ACTION_PULL_IN_WINDOW:
321             events.action = MouseAction::WINDOW_ENTER;
322             events.pullAction = MouseAction::PULL_MOVE;
323             return;
324         case OHOS::MMI::PointerEvent::POINTER_ACTION_PULL_OUT_WINDOW:
325             events.action = MouseAction::WINDOW_LEAVE;
326             events.pullAction = MouseAction::PULL_MOVE;
327             return;
328         case OHOS::MMI::PointerEvent::POINTER_ACTION_PULL_UP:
329             events.action = MouseAction::RELEASE;
330             events.pullAction = MouseAction::PULL_UP;
331             break;
332         case OHOS::MMI::PointerEvent::POINTER_ACTION_CANCEL:
333             events.action = MouseAction::CANCEL;
334             break;
335         default:
336             events.action = MouseAction::NONE;
337             break;
338     }
339 }
340 
GetMouseEventButton(int32_t button)341 MouseButton GetMouseEventButton(int32_t button)
342 {
343     switch (button) {
344         case OHOS::MMI::PointerEvent::MOUSE_BUTTON_LEFT:
345             return MouseButton::LEFT_BUTTON;
346         case OHOS::MMI::PointerEvent::MOUSE_BUTTON_RIGHT:
347             return MouseButton::RIGHT_BUTTON;
348         case OHOS::MMI::PointerEvent::MOUSE_BUTTON_MIDDLE:
349             return MouseButton::MIDDLE_BUTTON;
350         case OHOS::MMI::PointerEvent::MOUSE_BUTTON_SIDE:
351             return MouseButton::BACK_BUTTON;
352         case OHOS::MMI::PointerEvent::MOUSE_BUTTON_EXTRA:
353             return MouseButton::FORWARD_BUTTON;
354         default:
355             return MouseButton::NONE_BUTTON;
356     }
357 }
358 
ConvertMouseEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent,MouseEvent & events,bool isScenceBoardWindow)359 void ConvertMouseEvent(
360     const std::shared_ptr<MMI::PointerEvent>& pointerEvent, MouseEvent& events, bool isScenceBoardWindow)
361 {
362     int32_t pointerID = pointerEvent->GetPointerId();
363     MMI::PointerEvent::PointerItem item;
364     bool ret = pointerEvent->GetPointerItem(pointerID, item);
365     if (!ret) {
366         LOGE("get pointer: %{public}d item failed.", pointerID);
367         return;
368     }
369     events.id = pointerID;
370     events.x = item.GetWindowX();
371     events.y = item.GetWindowY();
372     events.screenX = item.GetDisplayX();
373     events.screenY = item.GetDisplayY();
374     events.rawDeltaX = item.GetRawDx();
375     events.rawDeltaY = item.GetRawDy();
376     int32_t orgAction = pointerEvent->GetPointerAction();
377     GetMouseEventAction(orgAction, events, isScenceBoardWindow);
378     int32_t orgButton = pointerEvent->GetButtonId();
379     events.button = GetMouseEventButton(orgButton);
380     int32_t orgDevice = pointerEvent->GetSourceType();
381     GetEventDevice(orgDevice, events);
382     events.isPrivacyMode = pointerEvent->HasFlag(OHOS::MMI::InputEvent::EVENT_FLAG_PRIVACY_MODE);
383     events.targetDisplayId = pointerEvent->GetTargetDisplayId();
384     events.originalId = item.GetOriginPointerId();
385     events.deviceId = pointerEvent->GetDeviceId();
386 
387     std::set<int32_t> pressedSet = pointerEvent->GetPressedButtons();
388     uint32_t pressedButtons = 0;
389     if (pressedSet.find(OHOS::MMI::PointerEvent::MOUSE_BUTTON_LEFT) != pressedSet.end()) {
390         pressedButtons &= static_cast<uint32_t>(MouseButton::LEFT_BUTTON);
391     }
392     if (pressedSet.find(OHOS::MMI::PointerEvent::MOUSE_BUTTON_RIGHT) != pressedSet.end()) {
393         pressedButtons &= static_cast<uint32_t>(MouseButton::RIGHT_BUTTON);
394     }
395     if (pressedSet.find(OHOS::MMI::PointerEvent::MOUSE_BUTTON_MIDDLE) != pressedSet.end()) {
396         pressedButtons &= static_cast<uint32_t>(MouseButton::MIDDLE_BUTTON);
397     }
398     events.pressedButtons = static_cast<int32_t>(pressedButtons);
399 
400     for (const auto& pressedButton : pressedSet) {
401         events.pressedButtonsArray.emplace_back(GetMouseEventButton(pressedButton));
402     }
403     events.time = TimeStamp(std::chrono::microseconds(pointerEvent->GetActionTime()));
404     events.pointerEvent = pointerEvent;
405     events.sourceTool = GetSourceTool(item.GetToolType());
406     UpdateMouseEventForPen(item, events);
407     events.touchEventId = pointerEvent->GetId();
408     events.pressedKeyCodes_.clear();
409     for (const auto& curCode : pointerEvent->GetPressedKeys()) {
410         events.pressedKeyCodes_.emplace_back(static_cast<KeyCode>(curCode));
411     }
412 }
413 
GetAxisEventAction(int32_t action,AxisEvent & event)414 void GetAxisEventAction(int32_t action, AxisEvent& event)
415 {
416     switch (action) {
417         case OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_BEGIN:
418         case OHOS::MMI::PointerEvent::POINTER_ACTION_ROTATE_BEGIN:
419             event.action = AxisAction::BEGIN;
420             break;
421         case OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_UPDATE:
422         case OHOS::MMI::PointerEvent::POINTER_ACTION_ROTATE_UPDATE:
423             event.action = AxisAction::UPDATE;
424             break;
425         case OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_END:
426         case OHOS::MMI::PointerEvent::POINTER_ACTION_ROTATE_END:
427             event.action = AxisAction::END;
428             break;
429         default:
430             event.action = AxisAction::NONE;
431             break;
432     }
433 }
434 
GetNonPointerAxisEventAction(int32_t action,NG::FocusAxisEvent & event)435 void GetNonPointerAxisEventAction(int32_t action, NG::FocusAxisEvent& event)
436 {
437     switch (action) {
438         case OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_BEGIN:
439             event.action = AxisAction::BEGIN;
440             break;
441         case OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_UPDATE:
442             event.action = AxisAction::UPDATE;
443             break;
444         case OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_END:
445             event.action = AxisAction::END;
446             break;
447         case OHOS::MMI::PointerEvent::POINTER_ACTION_CANCEL:
448             event.action = AxisAction::CANCEL;
449             break;
450         default:
451             event.action = AxisAction::NONE;
452             break;
453     }
454 }
455 
ConvertAxisEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent,AxisEvent & event)456 void ConvertAxisEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent, AxisEvent& event)
457 {
458     int32_t pointerID = pointerEvent->GetPointerId();
459     MMI::PointerEvent::PointerItem item;
460     bool ret = pointerEvent->GetPointerItem(pointerID, item);
461     if (!ret) {
462         LOGE("get pointer: %{public}d item failed.", pointerID);
463         return;
464     }
465 
466     event.id = item.GetPointerId();
467     event.x = static_cast<float>(item.GetWindowX());
468     event.y = static_cast<float>(item.GetWindowY());
469     event.screenX = static_cast<float>(item.GetDisplayX());
470     event.screenY = static_cast<float>(item.GetDisplayY());
471     event.horizontalAxis = pointerEvent->GetAxisValue(OHOS::MMI::PointerEvent::AxisType::AXIS_TYPE_SCROLL_HORIZONTAL);
472     event.verticalAxis = pointerEvent->GetAxisValue(OHOS::MMI::PointerEvent::AxisType::AXIS_TYPE_SCROLL_VERTICAL);
473     event.pinchAxisScale = pointerEvent->GetAxisValue(OHOS::MMI::PointerEvent::AxisType::AXIS_TYPE_PINCH);
474     event.rotateAxisAngle = pointerEvent->GetAxisValue(OHOS::MMI::PointerEvent::AxisType::AXIS_TYPE_ROTATE);
475     int32_t orgAction = pointerEvent->GetPointerAction();
476     GetAxisEventAction(orgAction, event);
477     event.isRotationEvent = (orgAction >= MMI::PointerEvent::POINTER_ACTION_ROTATE_BEGIN) &&
478                             (orgAction <= MMI::PointerEvent::POINTER_ACTION_ROTATE_END);
479     int32_t orgDevice = pointerEvent->GetSourceType();
480     GetEventDevice(orgDevice, event);
481     event.sourceTool = GetSourceTool(item.GetToolType());
482     event.pointerEvent = pointerEvent;
483     event.originalId = item.GetOriginPointerId();
484     event.deviceId = pointerEvent->GetDeviceId();
485 
486     std::chrono::microseconds microseconds(pointerEvent->GetActionTime());
487     TimeStamp time(microseconds);
488     event.time = time;
489     event.touchEventId = pointerEvent->GetId();
490     event.targetDisplayId = pointerEvent->GetTargetDisplayId();
491     event.pressedCodes.clear();
492     for (const auto& curCode : pointerEvent->GetPressedKeys()) {
493         event.pressedCodes.emplace_back(static_cast<KeyCode>(curCode));
494     }
495 }
496 
ConvertKeyEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent,KeyEvent & event)497 void ConvertKeyEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent, KeyEvent& event)
498 {
499     CHECK_NULL_VOID(keyEvent);
500     event.rawKeyEvent = keyEvent;
501     event.code = static_cast<KeyCode>(keyEvent->GetKeyCode());
502     event.numLock = keyEvent->GetFunctionKey(MMI::KeyEvent::NUM_LOCK_FUNCTION_KEY);
503     event.keyIntention = static_cast<KeyIntention>(keyEvent->GetKeyIntention());
504     if (keyEvent->GetKeyAction() == OHOS::MMI::KeyEvent::KEY_ACTION_UP) {
505         event.action = KeyAction::UP;
506     } else if (keyEvent->GetKeyAction() == OHOS::MMI::KeyEvent::KEY_ACTION_DOWN) {
507         event.action = KeyAction::DOWN;
508     } else {
509         event.action = KeyAction::UNKNOWN;
510     }
511     auto keyItems = keyEvent->GetKeyItems();
512     for (auto item = keyItems.rbegin(); item != keyItems.rend(); item++) {
513         if (item->GetKeyCode() == keyEvent->GetKeyCode()) {
514             event.unicode = item->GetUnicode();
515             break;
516         } else {
517             event.unicode = 0;
518         }
519     }
520 
521     std::chrono::microseconds microseconds(keyEvent->GetActionTime());
522     TimeStamp time(microseconds);
523     event.timeStamp = time;
524     event.key.assign(MMI::KeyEvent::KeyCodeToString(keyEvent->GetKeyCode()));
525     event.deviceId = keyEvent->GetDeviceId();
526     int32_t orgDevice = keyEvent->GetSourceType();
527     event.sourceType =
528         orgDevice == MMI::PointerEvent::SOURCE_TYPE_JOYSTICK ? SourceType::JOYSTICK : SourceType::KEYBOARD;
529 #ifdef SECURITY_COMPONENT_ENABLE
530     event.enhanceData = keyEvent->GetEnhanceData();
531 #endif
532     event.pressedCodes.clear();
533     for (const auto& curCode : keyEvent->GetPressedKeys()) {
534         event.pressedCodes.emplace_back(static_cast<KeyCode>(curCode));
535     }
536     event.enableCapsLock = keyEvent->GetFunctionKey(MMI::KeyEvent::CAPS_LOCK_FUNCTION_KEY);
537 }
538 
ConvertFocusAxisEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent,NG::FocusAxisEvent & event)539 void ConvertFocusAxisEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent, NG::FocusAxisEvent& event)
540 {
541     int32_t pointerID = pointerEvent->GetPointerId();
542     MMI::PointerEvent::PointerItem item;
543     bool ret = pointerEvent->GetPointerItem(pointerID, item);
544     if (!ret) {
545         LOGE("get pointer: %{public}d item failed.", pointerID);
546         return;
547     }
548 
549     event.id = item.GetPointerId();
550     event.absXValue = pointerEvent->GetAxisValue(OHOS::MMI::PointerEvent::AxisType::AXIS_TYPE_ABS_X);
551     event.absYValue = pointerEvent->GetAxisValue(OHOS::MMI::PointerEvent::AxisType::AXIS_TYPE_ABS_Y);
552     event.absZValue = pointerEvent->GetAxisValue(OHOS::MMI::PointerEvent::AxisType::AXIS_TYPE_ABS_Z);
553     event.absRzValue = pointerEvent->GetAxisValue(OHOS::MMI::PointerEvent::AxisType::AXIS_TYPE_ABS_RZ);
554     event.absHat0XValue = pointerEvent->GetAxisValue(OHOS::MMI::PointerEvent::AxisType::AXIS_TYPE_ABS_HAT0X);
555     event.absHat0YValue = pointerEvent->GetAxisValue(OHOS::MMI::PointerEvent::AxisType::AXIS_TYPE_ABS_HAT0Y);
556     event.absBrakeValue = pointerEvent->GetAxisValue(OHOS::MMI::PointerEvent::AxisType::AXIS_TYPE_ABS_BRAKE);
557     event.absGasValue = pointerEvent->GetAxisValue(OHOS::MMI::PointerEvent::AxisType::AXIS_TYPE_ABS_GAS);
558     int32_t orgAction = pointerEvent->GetPointerAction();
559     GetNonPointerAxisEventAction(orgAction, event);
560     int32_t orgDevice = pointerEvent->GetSourceType();
561     GetEventDevice(orgDevice, event);
562     event.sourceTool = SourceTool::JOYSTICK;
563     event.pointerEvent = pointerEvent;
564     event.originalId = item.GetOriginPointerId();
565     event.deviceId = pointerEvent->GetDeviceId();
566 
567     std::chrono::microseconds microseconds(pointerEvent->GetActionTime());
568     TimeStamp time(microseconds);
569     event.time = time;
570     event.touchEventId = pointerEvent->GetId();
571     event.targetDisplayId = pointerEvent->GetTargetDisplayId();
572     event.pressedCodes.clear();
573     for (const auto& curCode : pointerEvent->GetPressedKeys()) {
574         event.pressedCodes.emplace_back(static_cast<KeyCode>(curCode));
575     }
576 }
577 
GetPointerEventAction(int32_t action,DragPointerEvent & event)578 void GetPointerEventAction(int32_t action, DragPointerEvent& event)
579 {
580     switch (action) {
581         case OHOS::MMI::PointerEvent::POINTER_ACTION_CANCEL:
582             event.action = PointerAction::CANCEL;
583             break;
584         case OHOS::MMI::PointerEvent::POINTER_ACTION_DOWN:
585             event.action = PointerAction::DOWN;
586             break;
587         case OHOS::MMI::PointerEvent::POINTER_ACTION_MOVE:
588             event.action = PointerAction::MOVE;
589             break;
590         case OHOS::MMI::PointerEvent::POINTER_ACTION_UP:
591             event.action = PointerAction::UP;
592             break;
593         case OHOS::MMI::PointerEvent::POINTER_ACTION_PULL_MOVE:
594             event.action = PointerAction::PULL_MOVE;
595             break;
596         case OHOS::MMI::PointerEvent::POINTER_ACTION_PULL_UP:
597             event.action = PointerAction::PULL_UP;
598             break;
599         case OHOS::MMI::PointerEvent::POINTER_ACTION_PULL_IN_WINDOW:
600             event.action = PointerAction::PULL_IN_WINDOW;
601             break;
602         case OHOS::MMI::PointerEvent::POINTER_ACTION_PULL_OUT_WINDOW:
603             event.action = PointerAction::PULL_OUT_WINDOW;
604             break;
605         default:
606             event.action = PointerAction::UNKNOWN;
607             break;
608     }
609 }
610 
UpdatePointerAction(std::shared_ptr<MMI::PointerEvent> & pointerEvent,const PointerAction action)611 void UpdatePointerAction(std::shared_ptr<MMI::PointerEvent>& pointerEvent, const PointerAction action)
612 {
613     if (action == PointerAction::PULL_IN_WINDOW) {
614         pointerEvent->SetPointerAction(OHOS::MMI::PointerEvent::POINTER_ACTION_PULL_IN_WINDOW);
615     }
616     if (action == PointerAction::PULL_OUT_WINDOW) {
617         pointerEvent->SetPointerAction(OHOS::MMI::PointerEvent::POINTER_ACTION_PULL_OUT_WINDOW);
618     }
619 }
620 
ConvertPointerEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent,DragPointerEvent & event)621 void ConvertPointerEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent, DragPointerEvent& event)
622 {
623     event.rawPointerEvent = pointerEvent;
624     event.pointerEventId = pointerEvent->GetId();
625     event.pointerId = pointerEvent->GetPointerId();
626     event.pullId = pointerEvent->GetPullId();
627     MMI::PointerEvent::PointerItem pointerItem;
628     pointerEvent->GetPointerItem(pointerEvent->GetPointerId(), pointerItem);
629     event.pressed = pointerItem.IsPressed();
630     event.windowX = pointerItem.GetWindowX();
631     event.windowY = pointerItem.GetWindowY();
632     event.displayX = pointerItem.GetDisplayX();
633     event.displayY = pointerItem.GetDisplayY();
634     event.size = std::max(pointerItem.GetWidth(), pointerItem.GetHeight()) / SIZE_DIVIDE;
635     event.force = static_cast<float>(pointerItem.GetPressure());
636     event.deviceId = pointerItem.GetDeviceId();
637     event.downTime = TimeStamp(std::chrono::microseconds(pointerItem.GetDownTime()));
638     event.time = TimeStamp(std::chrono::microseconds(pointerEvent->GetActionTime()));
639     event.sourceTool = GetSourceTool(pointerItem.GetToolType());
640     event.targetWindowId = pointerItem.GetTargetWindowId();
641     event.x = event.windowX;
642     event.y = event.windowY;
643     event.pressedKeyCodes_.clear();
644     for (const auto& curCode : pointerEvent->GetPressedKeys()) {
645         event.pressedKeyCodes_.emplace_back(static_cast<KeyCode>(curCode));
646     }
647     int32_t orgAction = pointerEvent->GetPointerAction();
648     GetPointerEventAction(orgAction, event);
649 }
650 
LogPointInfo(const std::shared_ptr<MMI::PointerEvent> & pointerEvent,int32_t instanceId)651 void LogPointInfo(const std::shared_ptr<MMI::PointerEvent>& pointerEvent, int32_t instanceId)
652 {
653     if (pointerEvent->GetPointerAction() == OHOS::MMI::PointerEvent::POINTER_ACTION_DOWN) {
654         auto container = Platform::AceContainer::GetContainer(instanceId);
655         if (container) {
656             auto pipelineContext = container->GetPipelineContext();
657             if (pipelineContext) {
658                 uint32_t windowId = pipelineContext->GetWindowId();
659                 TAG_LOGI(AceLogTag::ACE_INPUTTRACKING, "pointdown windowId: %{public}u", windowId);
660             }
661         }
662     }
663     if (SystemProperties::GetDebugEnabled()) {
664         TAG_LOGD(AceLogTag::ACE_DRAG, "point source: %{public}d", pointerEvent->GetSourceType());
665         auto actionId = pointerEvent->GetPointerId();
666         MMI::PointerEvent::PointerItem item;
667         if (pointerEvent->GetPointerItem(actionId, item)) {
668             TAG_LOGD(AceLogTag::ACE_DRAG,
669                 "action point info: id: %{public}d, pointerId: %{public}d, action: "
670                 "%{public}d, pressure: %{public}f, tiltX: %{public}f, tiltY: %{public}f",
671                 pointerEvent->GetId(), actionId, pointerEvent->GetPointerAction(),
672                 item.GetPressure(), item.GetTiltX(), item.GetTiltY());
673         }
674         auto ids = pointerEvent->GetPointerIds();
675         for (auto&& id : ids) {
676             MMI::PointerEvent::PointerItem item;
677             if (pointerEvent->GetPointerItem(id, item)) {
678                 TAG_LOGD(AceLogTag::ACE_UIEVENT,
679                     "all point info: id: %{public}d, x: %{public}d, y: %{public}d, isPressed: %{public}d, pressure: "
680                     "%{public}f, tiltX: %{public}f, tiltY: %{public}f",
681                     actionId, item.GetWindowX(), item.GetWindowY(), item.IsPressed(), item.GetPressure(),
682                     item.GetTiltX(), item.GetTiltY());
683             }
684         }
685     }
686 }
687 
CalculatePointerEvent(const std::shared_ptr<MMI::PointerEvent> & point,const RefPtr<NG::FrameNode> & frameNode,bool useRealtimeMatrix)688 void CalculatePointerEvent(const std::shared_ptr<MMI::PointerEvent>& point, const RefPtr<NG::FrameNode>& frameNode,
689     bool useRealtimeMatrix)
690 {
691     CHECK_NULL_VOID(point);
692     int32_t pointerId = point->GetPointerId();
693     MMI::PointerEvent::PointerItem item;
694     bool ret = point->GetPointerItem(pointerId, item);
695     if (ret) {
696         float xRelative = item.GetWindowX();
697         float yRelative = item.GetWindowY();
698         if (point->GetSourceType() == OHOS::MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN &&
699             item.GetToolType() == OHOS::MMI::PointerEvent::TOOL_TYPE_PEN) {
700             xRelative = item.GetWindowXPos();
701             yRelative = item.GetWindowYPos();
702         }
703         NG::PointF transformPoint(xRelative, yRelative);
704         NG::NGGestureRecognizer::Transform(transformPoint, frameNode, useRealtimeMatrix);
705         item.SetWindowX(static_cast<int32_t>(transformPoint.GetX()));
706         item.SetWindowY(static_cast<int32_t>(transformPoint.GetY()));
707         item.SetWindowXPos(transformPoint.GetX());
708         item.SetWindowYPos(transformPoint.GetY());
709         point->UpdatePointerItem(pointerId, item);
710     }
711 }
712 
CalculatePointerEvent(const NG::OffsetF & offsetF,const std::shared_ptr<MMI::PointerEvent> & point,const NG::VectorF & scale,int32_t udegree)713 void CalculatePointerEvent(const NG::OffsetF& offsetF, const std::shared_ptr<MMI::PointerEvent>& point,
714     const NG::VectorF& scale, int32_t udegree)
715 {
716     CHECK_NULL_VOID(point);
717     int32_t pointerId = point->GetPointerId();
718     MMI::PointerEvent::PointerItem item;
719     bool ret = point->GetPointerItem(pointerId, item);
720     if (ret) {
721         float xRelative = item.GetWindowX();
722         float yRelative = item.GetWindowY();
723         if (point->GetSourceType() == OHOS::MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN &&
724             item.GetToolType() == OHOS::MMI::PointerEvent::TOOL_TYPE_PEN) {
725             xRelative = item.GetWindowXPos();
726             yRelative = item.GetWindowYPos();
727         }
728         auto windowX = xRelative;
729         auto windowY = yRelative;
730         auto pipelineContext = PipelineBase::GetCurrentContext();
731         CHECK_NULL_VOID(pipelineContext);
732         auto displayWindowRect = pipelineContext->GetDisplayWindowRectInfo();
733         auto windowWidth = displayWindowRect.Width();
734         auto windowHeight = displayWindowRect.Height();
735         switch (udegree) {
736             case ANGLE_0:
737                 windowX = xRelative - offsetF.GetX();
738                 windowY = yRelative - offsetF.GetY();
739                 break;
740             case ANGLE_90:
741                 windowX = yRelative - offsetF.GetX();
742                 windowY = windowWidth - offsetF.GetY() - xRelative;
743                 break;
744             case ANGLE_180:
745                 windowX = windowWidth - offsetF.GetX() - xRelative;
746                 windowY = windowHeight - offsetF.GetY() - yRelative;
747                 break;
748             case ANGLE_270:
749                 windowX = windowHeight - offsetF.GetX() - yRelative;
750                 windowY = xRelative - offsetF.GetY();
751                 break;
752             default:
753                 break;
754         }
755         windowX = NearZero(scale.x) ? windowX : windowX / scale.x;
756         windowY = NearZero(scale.y) ? windowY : windowY / scale.y;
757 
758         item.SetWindowX(static_cast<int32_t>(windowX));
759         item.SetWindowY(static_cast<int32_t>(windowY));
760         item.SetWindowXPos(windowX);
761         item.SetWindowYPos(windowY);
762         point->UpdatePointerItem(pointerId, item);
763     }
764 }
765 
CalculateWindowCoordinate(const NG::OffsetF & offsetF,const std::shared_ptr<MMI::PointerEvent> & point,const NG::VectorF & scale,const int32_t udegree)766 void CalculateWindowCoordinate(const NG::OffsetF& offsetF, const std::shared_ptr<MMI::PointerEvent>& point,
767     const NG::VectorF& scale, const int32_t udegree)
768 {
769     CHECK_NULL_VOID(point);
770     auto ids = point->GetPointerIds();
771     for (auto&& id : ids) {
772         MMI::PointerEvent::PointerItem item;
773         bool ret = point->GetPointerItem(id, item);
774         if (!ret) {
775             LOGE("get pointer:%{public}d item failed", id);
776             continue;
777         }
778         float xRelative = item.GetDisplayX();
779         float yRelative = item.GetDisplayY();
780         if (point->GetSourceType() == OHOS::MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN &&
781             item.GetToolType() == OHOS::MMI::PointerEvent::TOOL_TYPE_PEN) {
782             xRelative = item.GetDisplayXPos();
783             yRelative = item.GetDisplayYPos();
784         }
785         float windowX = xRelative;
786         float windowY = yRelative;
787         int32_t deviceWidth = SystemProperties::GetDevicePhysicalWidth();
788         int32_t deviceHeight = SystemProperties::GetDevicePhysicalHeight();
789 
790         if (udegree == ANGLE_0) {
791             windowX = xRelative - offsetF.GetX();
792             windowY = yRelative - offsetF.GetY();
793         }
794         if (udegree == ANGLE_90) {
795             windowX = yRelative - offsetF.GetX();
796             windowY = deviceWidth - offsetF.GetY() - xRelative;
797         }
798         if (udegree == ANGLE_180) {
799             windowX = deviceWidth - offsetF.GetX() - xRelative;
800             windowY = deviceHeight - offsetF.GetY() - yRelative;
801         }
802         if (udegree == ANGLE_270) {
803             windowX = deviceHeight - offsetF.GetX() - yRelative;
804             windowY = xRelative - offsetF.GetY();
805         }
806 
807         windowX = NearZero(scale.x) ? windowX : windowX / scale.x;
808         windowY = NearZero(scale.y) ? windowY : windowY / scale.y;
809 
810         item.SetWindowX(static_cast<int32_t>(windowX));
811         item.SetWindowY(static_cast<int32_t>(windowY));
812         item.SetWindowXPos(windowX);
813         item.SetWindowYPos(windowY);
814         point->UpdatePointerItem(id, item);
815     }
816 }
817 } // namespace OHOS::Ace::Platform
818