1 /*
2  * Copyright (c) 2021-2022 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 "core/event/event_convertor.h"
17 
18 namespace OHOS::Ace {
19 
SetTouchEventType(AceActionData::ActionType actionType,TouchEvent & point,std::vector<TouchEvent> & events)20 void SetTouchEventType(AceActionData::ActionType actionType, TouchEvent& point, std::vector<TouchEvent>& events)
21 {
22     switch (actionType) {
23         case AceActionData::ActionType::CANCEL:
24             point.type = TouchType::CANCEL;
25             events.push_back(point);
26             return;
27         case AceActionData::ActionType::ADD:
28         case AceActionData::ActionType::REMOVE:
29         case AceActionData::ActionType::HOVER:
30             return;
31         case AceActionData::ActionType::DOWN:
32             point.type = TouchType::DOWN;
33             events.push_back(point);
34             return;
35         case AceActionData::ActionType::MOVE:
36             point.type = TouchType::MOVE;
37             events.push_back(point);
38             return;
39         case AceActionData::ActionType::UP:
40             point.type = TouchType::UP;
41             events.push_back(point);
42             return;
43         case AceActionData::ActionType::UNKNOWN:
44         default:
45             return;
46     }
47 }
48 
UpdateTouchEvent(std::vector<TouchEvent> & events)49 void UpdateTouchEvent(std::vector<TouchEvent>& events)
50 {
51     if (events.empty()) {
52         return;
53     }
54     for (auto& event : events) {
55         TouchPoint touchPoint;
56         touchPoint.size = event.size;
57         touchPoint.id = event.id;
58         touchPoint.force = event.force;
59         touchPoint.downTime = event.time;
60         touchPoint.x = event.x;
61         touchPoint.y = event.y;
62         touchPoint.screenX = event.screenX;
63         touchPoint.screenY = event.screenY;
64         touchPoint.isPressed = (event.type == TouchType::DOWN);
65         event.pointers.emplace_back(std::move(touchPoint));
66     }
67 }
68 
ConvertMouseEvent(const std::vector<uint8_t> & data,MouseEvent & events)69 void ConvertMouseEvent(const std::vector<uint8_t>& data, MouseEvent& events)
70 {
71     const auto* mouseActionData = reinterpret_cast<const AceMouseData*>(data.data());
72     if (!mouseActionData) {
73         return;
74     }
75     std::chrono::microseconds micros(mouseActionData->timeStamp);
76     TimeStamp time(micros);
77     events.x = mouseActionData->physicalX;
78     events.y = mouseActionData->physicalY;
79     events.z = mouseActionData->physicalZ;
80     events.deltaX = mouseActionData->deltaX;
81     events.deltaY = mouseActionData->deltaY;
82     events.deltaZ = mouseActionData->deltaZ;
83     events.scrollX = mouseActionData->scrollDeltaX;
84     events.scrollY = mouseActionData->scrollDeltaY;
85     events.scrollZ = mouseActionData->scrollDeltaZ;
86     switch (mouseActionData->action) {
87         case AceMouseData::Action::PRESS:
88             events.action = MouseAction::PRESS;
89             break;
90         case AceMouseData::Action::RELEASE:
91             events.action = MouseAction::RELEASE;
92             break;
93         case AceMouseData::Action::MOVE:
94             events.action = MouseAction::MOVE;
95             break;
96         case AceMouseData::Action::HOVER_ENTER:
97             events.action = MouseAction::HOVER_ENTER;
98             break;
99         case AceMouseData::Action::HOVER_MOVE:
100             events.action = MouseAction::HOVER_MOVE;
101             break;
102         case AceMouseData::Action::HOVER_EXIT:
103             events.action = MouseAction::HOVER_EXIT;
104             break;
105         default:
106             events.action = MouseAction::NONE;
107             break;
108     }
109     switch (mouseActionData->actionButton) {
110         case AceMouseData::ActionButton::LEFT_BUTTON:
111             events.button = MouseButton::LEFT_BUTTON;
112             break;
113         case AceMouseData::ActionButton::RIGHT_BUTTON:
114             events.button = MouseButton::RIGHT_BUTTON;
115             break;
116         case AceMouseData::ActionButton::MIDDLE_BUTTON:
117             events.button = MouseButton::MIDDLE_BUTTON;
118             break;
119         case AceMouseData::ActionButton::BACK_BUTTON:
120             events.button = MouseButton::BACK_BUTTON;
121             break;
122         case AceMouseData::ActionButton::FORWARD_BUTTON:
123             events.button = MouseButton::FORWARD_BUTTON;
124             break;
125         default:
126             events.button = MouseButton::NONE_BUTTON;
127             break;
128     }
129     events.pressedButtons = static_cast<int32_t>(mouseActionData->pressedButtons);
130     events.time = time;
131     events.deviceId = mouseActionData->deviceId;
132 }
133 
134 } // namespace OHOS::Ace