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