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