• 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 "input_event.h"
17 #include <cassert>
18 #include <chrono>
19 
20 namespace OHOS {
21 namespace MMI {
22 namespace {
23     int64_t g_nextEventId = 1;
24 }
25 
InputEvent(int32_t eventType)26 InputEvent::InputEvent(int32_t eventType) : eventType_(eventType)
27 {
28     Reset();
29 }
30 
InputEvent(const InputEvent & other)31 InputEvent::InputEvent(const InputEvent& other)
32     : eventType_(other.eventType_), id_(other.id_), actionTime_(other.actionTime_),
33     action_(other.action_), actionStartTime_(other.actionStartTime_),
34     deviceId_(other.deviceId_), targetDisplayId_(other.targetDisplayId_),
35     targetWindowId_(other.targetWindowId_), agentWindowId_(other.agentWindowId_),
36     bitwise_(other.bitwise_), processedCallback_(other.processedCallback_)
37 {}
38 
~InputEvent()39 InputEvent::~InputEvent() {}
40 
Reset()41 void InputEvent::Reset()
42 {
43     struct timespec ts = { 0, 0 };
44     if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
45         actionTime_ = 0;
46     }
47     id_ = DEFALUTID;
48     int32_t conversionStep = 1000000;
49     uint64_t nowTime = (ts.tv_sec * static_cast<uint64_t>(1e3)) + (ts.tv_nsec / conversionStep);
50     int32_t actionTime = static_cast<int32_t>(nowTime);
51     actionTime_ = actionTime;
52     action_ = ACTION_UNKNOWN;
53     actionStartTime_ = actionTime_;
54     deviceId_ = DEFALUTID;
55     targetDisplayId_ = DEFALUTID;
56     targetWindowId_ = DEFALUTID;
57     agentWindowId_ = DEFALUTID;
58     bitwise_ = EVENT_FLAG_NONE;
59 }
60 
Create()61 std::shared_ptr<InputEvent> InputEvent::Create()
62 {
63     return std::shared_ptr<InputEvent>(new InputEvent(InputEvent::EVENT_TYPE_BASE));
64 }
65 
GetId() const66 int32_t InputEvent::GetId() const
67 {
68     return id_;
69 }
70 
SetId(int32_t id)71 void InputEvent::SetId(int32_t id)
72 {
73     id_ = id;
74 }
75 
UpdateId()76 void InputEvent::UpdateId()
77 {
78     id_ = g_nextEventId++;
79 }
80 
GetActionTime() const81 int64_t InputEvent::GetActionTime() const
82 {
83     return actionTime_;
84 }
85 
SetActionTime(int64_t actionTime)86 void InputEvent::SetActionTime(int64_t actionTime)
87 {
88     actionTime_ = actionTime;
89 }
90 
GetAction() const91 int32_t InputEvent::GetAction() const
92 {
93     return action_;
94 }
95 
SetAction(int32_t action)96 void InputEvent::SetAction(int32_t action)
97 {
98     action_ = action;
99 }
100 
GetActionStartTime() const101 int64_t InputEvent::GetActionStartTime() const
102 {
103     return actionStartTime_;
104 }
105 
SetActionStartTime(int64_t actionStartTime)106 void InputEvent::SetActionStartTime(int64_t actionStartTime)
107 {
108     actionStartTime_ = actionStartTime;
109 }
110 
GetDeviceId() const111 int32_t InputEvent::GetDeviceId() const
112 {
113     return deviceId_;
114 }
115 
SetDeviceId(int32_t deviceId)116 void InputEvent::SetDeviceId(int32_t deviceId)
117 {
118     deviceId_ = deviceId;
119 }
120 
GetTargetDisplayId() const121 int32_t InputEvent::GetTargetDisplayId() const
122 {
123     return targetDisplayId_;
124 }
125 
SetTargetDisplayId(int32_t displayId)126 void InputEvent::SetTargetDisplayId(int32_t displayId)
127 {
128     targetDisplayId_ = displayId;
129 }
130 
GetAgentWindowId() const131 int32_t InputEvent::GetAgentWindowId() const
132 {
133     return agentWindowId_;
134 }
135 
SetAgentWindowId(int32_t windowId)136 void InputEvent::SetAgentWindowId(int32_t windowId)
137 {
138     agentWindowId_ = windowId;
139 }
140 
GetTargetWindowId() const141 int32_t InputEvent::GetTargetWindowId() const
142 {
143     return targetWindowId_;
144 }
145 
SetTargetWindowId(int32_t windowId)146 void InputEvent::SetTargetWindowId(int32_t windowId)
147 {
148     targetWindowId_ = windowId;
149 }
150 
GetEventType() const151 int32_t InputEvent::GetEventType() const
152 {
153     return eventType_;
154 }
155 
DumpEventType() const156 const char* InputEvent::DumpEventType() const
157 {
158     switch (eventType_) {
159         case InputEvent::EVENT_TYPE_BASE:
160             return "base";
161         case InputEvent::EVENT_TYPE_KEY:
162             return "key";
163         case InputEvent::EVENT_TYPE_POINTER:
164             return "pointer";
165         case InputEvent::EVENT_TYPE_AXIS:
166             return "axis";
167         default:
168             break;
169     }
170     return "unknown";
171 }
172 
GetFlag() const173 uint32_t InputEvent::GetFlag() const
174 {
175     return bitwise_;
176 }
177 
HasFlag(uint32_t flag)178 bool InputEvent::HasFlag(uint32_t flag)
179 {
180     return (bitwise_ & flag) != 0;
181 }
182 
AddFlag(uint32_t flag)183 void InputEvent::AddFlag(uint32_t flag)
184 {
185     bitwise_ |= flag;
186 }
187 
ClearFlag()188 void InputEvent::ClearFlag()
189 {
190     bitwise_ = EVENT_FLAG_NONE;
191 }
192 
SetProcessedCallback(std::function<void (int32_t)> callback)193 void InputEvent::SetProcessedCallback(std::function<void(int32_t)> callback)
194 {
195     processedCallback_ = callback;
196 }
197 
MarkProcessed()198 void InputEvent::MarkProcessed()
199 {
200     if (!processedCallback_) {
201         return;
202     }
203     auto func = processedCallback_;
204     processedCallback_ = std::function<void(int32_t)>();
205     func(id_);
206 }
207 
WriteToParcel(Parcel & out) const208 bool InputEvent::WriteToParcel(Parcel &out) const
209 {
210     if (!out.WriteInt32(eventType_)) {
211         return false;
212     }
213 
214     if (!out.WriteInt32(id_)) {
215         return false;
216     }
217 
218     if (!out.WriteInt64(actionTime_)) {
219         return false;
220     }
221 
222     if (!out.WriteInt32(action_)) {
223         return false;
224     }
225 
226     if (!out.WriteInt64(actionStartTime_)) {
227         return false;
228     }
229 
230     if (!out.WriteInt32(deviceId_)) {
231         return false;
232     }
233 
234     if (!out.WriteInt32(targetDisplayId_)) {
235         return false;
236     }
237 
238     if (!out.WriteInt32(targetWindowId_)) {
239         return false;
240     }
241 
242     if (!out.WriteInt32(agentWindowId_)) {
243         return false;
244     }
245 
246     if (!out.WriteUint32(bitwise_)) {
247         return false;
248     }
249 
250     return true;
251 }
252 
ReadFromParcel(Parcel & in)253 bool InputEvent::ReadFromParcel(Parcel &in)
254 {
255     if (!in.ReadInt32(eventType_)) {
256         return false;
257     }
258 
259     if (!in.ReadInt32(id_)) {
260         return false;
261     }
262 
263     if (!in.ReadInt64(actionTime_)) {
264         return false;
265     }
266 
267     if (!in.ReadInt32(action_)) {
268         return false;
269     }
270 
271     if (!in.ReadInt64(actionStartTime_)) {
272         return false;
273     }
274 
275     if (!in.ReadInt32(deviceId_)) {
276         return false;
277     }
278 
279     if (!in.ReadInt32(targetDisplayId_)) {
280         return false;
281     }
282 
283     if (!in.ReadInt32(targetWindowId_)) {
284         return false;
285     }
286 
287     if (!in.ReadInt32(agentWindowId_)) {
288         return false;
289     }
290 
291     if (!in.ReadUint32(bitwise_)) {
292         return false;
293     }
294 
295     return true;
296 }
297 } // namespace MMI
298 } // namespace OHOS
299