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