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
18 #include <cassert>
19 #include <chrono>
20
21 #include "mmi_log.h"
22
23 namespace OHOS {
24 namespace MMI {
25 namespace {
26 int64_t g_nextEventId = 1;
27 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "InputEvent" };
28 } // namespace
29
InputEvent(int32_t eventType)30 InputEvent::InputEvent(int32_t eventType) : eventType_(eventType)
31 {
32 Reset();
33 }
34
InputEvent(const InputEvent & other)35 InputEvent::InputEvent(const InputEvent& other)
36 : eventType_(other.eventType_), id_(other.id_), actionTime_(other.actionTime_),
37 action_(other.action_), actionStartTime_(other.actionStartTime_),
38 deviceId_(other.deviceId_), targetDisplayId_(other.targetDisplayId_),
39 targetWindowId_(other.targetWindowId_), agentWindowId_(other.agentWindowId_),
40 bitwise_(other.bitwise_), processedCallback_(other.processedCallback_) {}
41
~InputEvent()42 InputEvent::~InputEvent() {}
43
Reset()44 void InputEvent::Reset()
45 {
46 struct timespec ts = { 0, 0 };
47 if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
48 actionTime_ = 0;
49 }
50 id_ = -1;
51 if (!AddInt64(ts.tv_sec * 1000000, ts.tv_nsec / 1000, actionTime_)) {
52 MMI_HILOGE("The addition of actionTime_ overflows");
53 return;
54 }
55 action_ = ACTION_UNKNOWN;
56 actionStartTime_ = actionTime_;
57 deviceId_ = -1;
58 targetDisplayId_ = -1;
59 targetWindowId_ = -1;
60 agentWindowId_ = -1;
61 bitwise_ = EVENT_FLAG_NONE;
62 }
63
Create()64 std::shared_ptr<InputEvent> InputEvent::Create()
65 {
66 auto event = std::shared_ptr<InputEvent>(new (std::nothrow) InputEvent(InputEvent::EVENT_TYPE_BASE));
67 CHKPP(event);
68 return event;
69 }
70
EventTypeToString(int32_t eventType)71 const char* InputEvent::EventTypeToString(int32_t eventType)
72 {
73 switch (eventType) {
74 case InputEvent::EVENT_TYPE_BASE: {
75 return "base";
76 }
77 case InputEvent::EVENT_TYPE_KEY: {
78 return "key";
79 }
80 case InputEvent::EVENT_TYPE_POINTER: {
81 return "pointer";
82 }
83 case InputEvent::EVENT_TYPE_AXIS: {
84 return "axis";
85 }
86 default: {
87 MMI_HILOGW("Unknown EVENT_TYPE");
88 return "unknown";
89 }
90 }
91 }
92
GetId() const93 int32_t InputEvent::GetId() const
94 {
95 return id_;
96 }
97
SetId(int32_t id)98 void InputEvent::SetId(int32_t id)
99 {
100 id_ = id;
101 }
102
UpdateId()103 void InputEvent::UpdateId()
104 {
105 id_ = g_nextEventId++;
106 }
107
GetActionTime() const108 int64_t InputEvent::GetActionTime() const
109 {
110 return actionTime_;
111 }
112
SetActionTime(int64_t actionTime)113 void InputEvent::SetActionTime(int64_t actionTime)
114 {
115 actionTime_ = actionTime;
116 }
117
SetSensorInputTime(uint64_t sensorInputTime)118 void InputEvent::SetSensorInputTime(uint64_t sensorInputTime)
119 {
120 sensorInputTime_ = sensorInputTime;
121 }
122
GetSensorInputTime()123 uint64_t InputEvent::GetSensorInputTime()
124 {
125 return sensorInputTime_;
126 }
127
GetAction() const128 int32_t InputEvent::GetAction() const
129 {
130 return action_;
131 }
132
SetAction(int32_t action)133 void InputEvent::SetAction(int32_t action)
134 {
135 action_ = action;
136 }
137
GetActionStartTime() const138 int64_t InputEvent::GetActionStartTime() const
139 {
140 return actionStartTime_;
141 }
142
SetActionStartTime(int64_t actionStartTime)143 void InputEvent::SetActionStartTime(int64_t actionStartTime)
144 {
145 actionStartTime_ = actionStartTime;
146 }
147
GetDeviceId() const148 int32_t InputEvent::GetDeviceId() const
149 {
150 return deviceId_;
151 }
152
SetDeviceId(int32_t deviceId)153 void InputEvent::SetDeviceId(int32_t deviceId)
154 {
155 deviceId_ = deviceId;
156 }
157
GetTargetDisplayId() const158 int32_t InputEvent::GetTargetDisplayId() const
159 {
160 return targetDisplayId_;
161 }
162
SetTargetDisplayId(int32_t displayId)163 void InputEvent::SetTargetDisplayId(int32_t displayId)
164 {
165 targetDisplayId_ = displayId;
166 }
167
GetAgentWindowId() const168 int32_t InputEvent::GetAgentWindowId() const
169 {
170 return agentWindowId_;
171 }
172
SetAgentWindowId(int32_t windowId)173 void InputEvent::SetAgentWindowId(int32_t windowId)
174 {
175 agentWindowId_ = windowId;
176 }
177
GetTargetWindowId() const178 int32_t InputEvent::GetTargetWindowId() const
179 {
180 return targetWindowId_;
181 }
182
SetTargetWindowId(int32_t windowId)183 void InputEvent::SetTargetWindowId(int32_t windowId)
184 {
185 targetWindowId_ = windowId;
186 }
187
GetEventType() const188 int32_t InputEvent::GetEventType() const
189 {
190 return eventType_;
191 }
192
GetFlag() const193 uint32_t InputEvent::GetFlag() const
194 {
195 return bitwise_;
196 }
197
HasFlag(uint32_t flag)198 bool InputEvent::HasFlag(uint32_t flag)
199 {
200 return (bitwise_ & flag) != 0;
201 }
202
AddFlag(uint32_t flag)203 void InputEvent::AddFlag(uint32_t flag)
204 {
205 bitwise_ |= flag;
206 }
207
ClearFlag()208 void InputEvent::ClearFlag()
209 {
210 bitwise_ = EVENT_FLAG_NONE;
211 }
212
SetProcessedCallback(std::function<void (int32_t,int64_t)> callback)213 void InputEvent::SetProcessedCallback(std::function<void(int32_t, int64_t)> callback)
214 {
215 processedCallback_ = callback;
216 }
217
MarkProcessed()218 void InputEvent::MarkProcessed()
219 {
220 if (!processedCallback_) {
221 return;
222 }
223 auto func = processedCallback_;
224 processedCallback_ = std::function<void(int32_t, int64_t)>();
225 func(id_, actionTime_);
226 }
227
SetExtraData(const std::shared_ptr<const uint8_t[]> data,uint32_t length)228 void InputEvent::SetExtraData(const std::shared_ptr<const uint8_t[]> data, uint32_t length)
229 {
230 if (data && length != 0) {
231 extraData_ = data;
232 extraDataLength_ = length;
233 }
234 }
235
GetExtraData(std::shared_ptr<const uint8_t[]> & data,uint32_t & length) const236 void InputEvent::GetExtraData(std::shared_ptr<const uint8_t[]> &data, uint32_t &length) const
237 {
238 if (extraData_ && extraDataLength_ != 0) {
239 data = extraData_;
240 length = extraDataLength_;
241 } else {
242 length = 0;
243 }
244 }
245
WriteToParcel(Parcel & out) const246 bool InputEvent::WriteToParcel(Parcel &out) const
247 {
248 WRITEINT32(out, eventType_);
249 WRITEINT32(out, id_);
250 WRITEINT64(out, actionTime_);
251 WRITEUINT64(out, sensorInputTime_);
252 WRITEINT32(out, action_);
253 WRITEINT64(out, actionStartTime_);
254 WRITEINT32(out, deviceId_);
255 WRITEINT32(out, targetDisplayId_);
256 WRITEINT32(out, targetWindowId_);
257 WRITEINT32(out, agentWindowId_);
258 WRITEUINT32(out, bitwise_);
259 if (extraData_ && extraDataLength_ != 0) {
260 WRITEUINT32(out, extraDataLength_);
261 WRITEBUFFER(out, (void *)extraData_.get(), extraDataLength_);
262 } else {
263 WRITEUINT32(out, 0);
264 }
265 return true;
266 }
267
ReadFromParcel(Parcel & in)268 bool InputEvent::ReadFromParcel(Parcel &in)
269 {
270 READINT32(in, eventType_);
271 READINT32(in, id_);
272 READINT64(in, actionTime_);
273 READUINT64(in, sensorInputTime_);
274 READINT32(in, action_);
275 READINT64(in, actionStartTime_);
276 READINT32(in, deviceId_);
277 READINT32(in, targetDisplayId_);
278 READINT32(in, targetWindowId_);
279 READINT32(in, agentWindowId_);
280 READUINT32(in, bitwise_);
281 READUINT32(in, extraDataLength_);
282 if (extraDataLength_ != 0) {
283 const uint8_t *buffer = in.ReadBuffer(extraDataLength_);
284 std::shared_ptr<uint8_t[]> sp(new uint8_t[extraDataLength_], [](uint8_t* ptr) { delete[] ptr; });
285 std::copy(buffer, buffer + extraDataLength_, sp.get());
286 extraData_ = sp;
287 }
288 return true;
289 }
290 } // namespace MMI
291 } // namespace OHOS
292