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 <cassert>
17 #include <chrono>
18 
19 #include "axis_event.h"
20 #include "event_log_helper.h"
21 #include "input_event.h"
22 #include "key_event.h"
23 #include "mmi_log.h"
24 #include "pointer_event.h"
25 
26 #undef MMI_LOG_TAG
27 #define MMI_LOG_TAG "InputEvent"
28 
29 namespace OHOS {
30 namespace MMI {
31 namespace {
32 int64_t g_nextEventId = 1;
33 constexpr uint32_t DATA_LENGTH_LIMIT { 1024 }; // 1024: max length
34 } // namespace
35 
36 std::string EventLogHelper::userType_ = "";
37 std::once_flag EventLogHelper::betaFlag_;
38 
InputEvent(int32_t eventType)39 InputEvent::InputEvent(int32_t eventType) : eventType_(eventType)
40 {
41     Reset();
42 }
43 
InputEvent(const InputEvent & other)44 InputEvent::InputEvent(const InputEvent& other)
45     : eventType_(other.eventType_), id_(other.id_), actionTime_(other.actionTime_),
46       action_(other.action_), actionStartTime_(other.actionStartTime_),
47       deviceId_(other.deviceId_), targetDisplayId_(other.targetDisplayId_),
48       targetWindowId_(other.targetWindowId_), agentWindowId_(other.agentWindowId_),
49       bitwise_(other.bitwise_), markEnabled_(other.markEnabled_), processedCallback_(other.processedCallback_) {}
50 
~InputEvent()51 InputEvent::~InputEvent() {}
52 
Reset()53 void InputEvent::Reset()
54 {
55     struct timespec ts = { 0, 0 };
56     if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
57         actionTime_ = 0;
58     }
59     id_ = -1;
60     if (!AddInt64(ts.tv_sec * 1000000, ts.tv_nsec / 1000, actionTime_)) {
61         MMI_HILOGE("The addition of actionTime_ overflows");
62         return;
63     }
64     action_ = ACTION_UNKNOWN;
65     actionStartTime_ = actionTime_;
66     deviceId_ = -1;
67     targetDisplayId_ = -1;
68     targetWindowId_ = -1;
69     agentWindowId_ = -1;
70     bitwise_ = EVENT_FLAG_NONE;
71     markEnabled_ = true;
72 }
73 
Create()74 std::shared_ptr<InputEvent> InputEvent::Create()
75 {
76     auto event = std::shared_ptr<InputEvent>(new (std::nothrow) InputEvent(InputEvent::EVENT_TYPE_BASE));
77     CHKPP(event);
78     return event;
79 }
80 
EventTypeToString(int32_t eventType)81 const char* InputEvent::EventTypeToString(int32_t eventType)
82 {
83     switch (eventType) {
84         case InputEvent::EVENT_TYPE_BASE: {
85             return "base";
86         }
87         case InputEvent::EVENT_TYPE_KEY: {
88             return "key";
89         }
90         case InputEvent::EVENT_TYPE_POINTER: {
91             return "pointer";
92         }
93         case InputEvent::EVENT_TYPE_AXIS: {
94             return "axis";
95         }
96         case InputEvent::EVENT_TYPE_FINGERPRINT: {
97             return "fingerprint";
98         }
99         default: {
100             MMI_HILOGW("Unknown EVENT_TYPE");
101             return "unknown";
102         }
103     }
104 }
105 
GetId() const106 int32_t InputEvent::GetId() const
107 {
108     return id_;
109 }
110 
SetId(int32_t id)111 void InputEvent::SetId(int32_t id)
112 {
113     id_ = id;
114 }
115 
UpdateId()116 void InputEvent::UpdateId()
117 {
118     id_ = g_nextEventId++;
119 }
120 
GetActionTime() const121 int64_t InputEvent::GetActionTime() const
122 {
123     return actionTime_;
124 }
125 
SetActionTime(int64_t actionTime)126 void InputEvent::SetActionTime(int64_t actionTime)
127 {
128     actionTime_ = actionTime;
129 }
130 
SetSensorInputTime(uint64_t sensorInputTime)131 void InputEvent::SetSensorInputTime(uint64_t sensorInputTime)
132 {
133     sensorInputTime_ = sensorInputTime;
134 }
135 
GetSensorInputTime()136 uint64_t InputEvent::GetSensorInputTime()
137 {
138     return sensorInputTime_;
139 }
140 
GetAction() const141 int32_t InputEvent::GetAction() const
142 {
143     return action_;
144 }
145 
SetAction(int32_t action)146 void InputEvent::SetAction(int32_t action)
147 {
148     action_ = action;
149 }
150 
GetActionStartTime() const151 int64_t InputEvent::GetActionStartTime() const
152 {
153     return actionStartTime_;
154 }
155 
SetActionStartTime(int64_t actionStartTime)156 void InputEvent::SetActionStartTime(int64_t actionStartTime)
157 {
158     actionStartTime_ = actionStartTime;
159 }
160 
GetDeviceId() const161 int32_t InputEvent::GetDeviceId() const
162 {
163     return deviceId_;
164 }
165 
SetDeviceId(int32_t deviceId)166 void InputEvent::SetDeviceId(int32_t deviceId)
167 {
168     deviceId_ = deviceId;
169 }
170 
GetTargetDisplayId() const171 int32_t InputEvent::GetTargetDisplayId() const
172 {
173     return targetDisplayId_;
174 }
175 
SetTargetDisplayId(int32_t displayId)176 void InputEvent::SetTargetDisplayId(int32_t displayId)
177 {
178     targetDisplayId_ = displayId;
179 }
180 
GetAgentWindowId() const181 int32_t InputEvent::GetAgentWindowId() const
182 {
183     return agentWindowId_;
184 }
185 
SetAgentWindowId(int32_t windowId)186 void InputEvent::SetAgentWindowId(int32_t windowId)
187 {
188     agentWindowId_ = windowId;
189 }
190 
GetTargetWindowId() const191 int32_t InputEvent::GetTargetWindowId() const
192 {
193     return targetWindowId_;
194 }
195 
SetTargetWindowId(int32_t windowId)196 void InputEvent::SetTargetWindowId(int32_t windowId)
197 {
198     targetWindowId_ = windowId;
199 }
200 
GetEventType() const201 int32_t InputEvent::GetEventType() const
202 {
203     return eventType_;
204 }
205 
GetFlag() const206 uint32_t InputEvent::GetFlag() const
207 {
208     return bitwise_;
209 }
210 
HasFlag(uint32_t flag)211 bool InputEvent::HasFlag(uint32_t flag)
212 {
213     return (bitwise_ & flag) != 0;
214 }
215 
AddFlag(uint32_t flag)216 void InputEvent::AddFlag(uint32_t flag)
217 {
218     bitwise_ |= flag;
219 }
220 
ClearFlag()221 void InputEvent::ClearFlag()
222 {
223     bitwise_ = EVENT_FLAG_NONE;
224 }
225 
ClearFlag(uint32_t flag)226 void InputEvent::ClearFlag(uint32_t flag)
227 {
228     bitwise_ &= ~flag;
229 }
230 
IsMarkEnabled() const231 bool InputEvent::IsMarkEnabled() const
232 {
233     return markEnabled_;
234 }
235 
236 
SetMarkEnabled(bool markEnabled)237 void InputEvent::SetMarkEnabled(bool markEnabled)
238 {
239     markEnabled_ = markEnabled;
240 }
241 
242 
SetProcessedCallback(std::function<void (int32_t,int64_t)> callback)243 void InputEvent::SetProcessedCallback(std::function<void(int32_t, int64_t)> callback)
244 {
245     processedCallback_ = callback;
246 }
247 
MarkProcessed()248 void InputEvent::MarkProcessed()
249 {
250     if (!processedCallback_) {
251         return;
252     }
253     if (!markEnabled_) {
254         MMI_HILOGD("Skip MarkProcessed eventId:%{public}d, eventType:%{public}d", id_, eventType_);
255         return;
256     }
257     auto func = processedCallback_;
258     processedCallback_ = std::function<void(int32_t, int64_t)>();
259     func(id_, actionTime_);
260 }
261 
SetExtraData(const std::shared_ptr<const uint8_t[]> data,uint32_t length)262 void InputEvent::SetExtraData(const std::shared_ptr<const uint8_t[]> data, uint32_t length)
263 {
264     if (data && (length > 0) && (length <= DATA_LENGTH_LIMIT)) {
265         extraData_ = data;
266         extraDataLength_ = length;
267     }
268 }
269 
GetExtraData(std::shared_ptr<const uint8_t[]> & data,uint32_t & length) const270 void InputEvent::GetExtraData(std::shared_ptr<const uint8_t[]> &data, uint32_t &length) const
271 {
272     if (extraData_ && extraDataLength_ != 0) {
273         data = extraData_;
274         length = extraDataLength_;
275     } else {
276         length = 0;
277     }
278 }
279 
WriteToParcel(Parcel & out) const280 bool InputEvent::WriteToParcel(Parcel &out) const
281 {
282     WRITEINT32(out, eventType_);
283     WRITEINT32(out, id_);
284     WRITEINT64(out, actionTime_);
285     WRITEUINT64(out, sensorInputTime_);
286     WRITEINT32(out, action_);
287     WRITEINT64(out, actionStartTime_);
288     WRITEINT32(out, deviceId_);
289     WRITEINT32(out, targetDisplayId_);
290     WRITEINT32(out, targetWindowId_);
291     WRITEINT32(out, agentWindowId_);
292     WRITEUINT32(out, bitwise_);
293     WRITEBOOL(out, markEnabled_);
294     if (extraData_ && extraDataLength_ != 0) {
295         WRITEUINT32(out, extraDataLength_);
296         WRITEBUFFER(out, (void *)extraData_.get(), extraDataLength_);
297     } else {
298         WRITEUINT32(out, 0);
299     }
300     return true;
301 }
302 
ReadFromParcel(Parcel & in)303 bool InputEvent::ReadFromParcel(Parcel &in)
304 {
305 #if defined(ANDROID_PLATFORM) || defined(IOS_PLATFORM)
306     return false;
307 #else
308     READINT32(in, eventType_);
309     READINT32(in, id_);
310     READINT64(in, actionTime_);
311     READUINT64(in, sensorInputTime_);
312     READINT32(in, action_);
313     READINT64(in, actionStartTime_);
314     READINT32(in, deviceId_);
315     READINT32(in, targetDisplayId_);
316     READINT32(in, targetWindowId_);
317     READINT32(in, agentWindowId_);
318     READUINT32(in, bitwise_);
319     READBOOL(in, markEnabled_);
320     READUINT32(in, extraDataLength_);
321 
322     if (extraDataLength_ == 0) {
323         return true;
324     }
325     if (extraDataLength_ > DATA_LENGTH_LIMIT) {
326         extraDataLength_ = 0;
327         return false;
328     }
329     const uint8_t *buffer = in.ReadBuffer(extraDataLength_);
330     std::shared_ptr<uint8_t[]> sp(new uint8_t[extraDataLength_], [](uint8_t* ptr) { delete[] ptr; });
331     if ((buffer == nullptr) || (sp == nullptr)) {
332         extraDataLength_ = 0;
333         return false;
334     }
335     std::copy(buffer, buffer + extraDataLength_, sp.get());
336     extraData_ = sp;
337     return true;
338 #endif // defined(ANDROID_PLATFORM) || defined(IOS_PLATFORM)
339 }
340 
ActionToShortStr(int32_t action)341 std::string_view InputEvent::ActionToShortStr(int32_t action)
342 {
343     switch (action) {
344         case InputEvent::ACTION_CANCEL:
345             return "B:C:";
346         case InputEvent::ACTION_UNKNOWN:
347             return "B:UK:";
348         default:
349             return "B:?:";
350     }
351 }
352 
353 struct LogTraceKey {
354     int64_t traceId;
355     int32_t action;
356     int32_t evtType;
357 };
358 
359 thread_local std::vector<LogTraceKey> g_traceIds;
360 thread_local std::unordered_map<int64_t, size_t> g_traceIdToIdx;
361 thread_local std::string g_traceStr;
362 
Action2Str(int32_t eventType,int32_t action)363 std::string_view Action2Str(int32_t eventType, int32_t action)
364 {
365     switch (eventType) {
366         case InputEvent::EVENT_TYPE_KEY: {
367             return KeyEvent::ActionToShortStr(action);
368         }
369         case InputEvent::EVENT_TYPE_POINTER:
370         case InputEvent::EVENT_TYPE_FINGERPRINT: {
371             return PointerEvent::ActionToShortStr(action);
372         }
373         case InputEvent::EVENT_TYPE_AXIS: {
374             return AxisEvent::ActionToShortStr(action);
375         }
376         case InputEvent::EVENT_TYPE_BASE: {
377             return InputEvent::ActionToShortStr(action);
378         }
379         default: {
380             return "?:?:";
381         }
382     }
383 }
384 
RefreshTraceStr()385 void RefreshTraceStr()
386 {
387     g_traceStr.clear();
388     for (auto item = g_traceIds.begin(); item < g_traceIds.end(); ++item) {
389         if (item->traceId == -1) {
390             continue;
391         }
392         if (item != g_traceIds.begin()) {
393             g_traceStr += "/";
394         }
395         g_traceStr += Action2Str(item->evtType, item->action);
396         g_traceStr += std::to_string(item->traceId);
397     }
398 }
399 
StartLogTraceId(int64_t traceId,int32_t eventType,int32_t action)400 void StartLogTraceId(int64_t traceId, int32_t eventType, int32_t action)
401 {
402     if (traceId == -1) {
403         return;
404     }
405     auto iter = g_traceIdToIdx.find(traceId);
406     if (iter == g_traceIdToIdx.end()) {
407         g_traceIds.push_back({traceId, action, eventType});
408         g_traceIdToIdx.emplace(traceId, g_traceIds.size() - 1);
409         std::string currentTraceStr(Action2Str(eventType, action));
410         currentTraceStr += std::to_string(traceId);
411         if (g_traceIds.size() == 1) {
412             g_traceStr = currentTraceStr;
413         } else {
414             g_traceStr += "/" + currentTraceStr;
415         }
416         return;
417     }
418     if (g_traceIds.size() <= iter->second) {
419         return;
420     }
421     LogTraceKey &old = g_traceIds.at(iter->second);
422     if (old.evtType != eventType || old.action != action) {
423         old.evtType = eventType;
424         old.action = action;
425         RefreshTraceStr();
426     }
427 };
428 
EndLogTraceId(int64_t id)429 void EndLogTraceId(int64_t id)
430 {
431     auto iter = g_traceIdToIdx.find(id);
432     if (iter == g_traceIdToIdx.end()) {
433         return;
434     }
435     size_t idx = iter->second;
436     g_traceIdToIdx.erase(iter);
437     size_t idCount = g_traceIds.size();
438     if (idCount <= idx) {
439         return;
440     }
441 
442     if (idCount == idx + 1) {
443         g_traceIds.pop_back();
444         while (!g_traceIds.empty() && g_traceIds.back().traceId == -1) {
445             g_traceIds.pop_back();
446         }
447     } else {
448         // can't erase it, erase it will make the index of other elem changed.
449         LogTraceKey &toDelete = g_traceIds.at(idx);
450         toDelete.traceId = -1;
451     }
452     RefreshTraceStr();
453 }
454 
FormatLogTrace()455 const char *FormatLogTrace()
456 {
457     return g_traceStr.c_str();
458 }
459 
460 
ResetLogTrace()461 void ResetLogTrace()
462 {
463     g_traceIds.clear();
464     g_traceIdToIdx.clear();
465     g_traceStr.clear();
466 }
467 
LogTracer(int64_t traceId,int32_t evtType,int32_t action)468 LogTracer::LogTracer(int64_t traceId, int32_t evtType, int32_t action)
469 {
470     traceId_ = traceId;
471     StartLogTraceId(traceId, evtType, action);
472 }
473 
~LogTracer()474 LogTracer::~LogTracer()
475 {
476     EndLogTraceId(traceId_);
477 }
478 
LogTracer()479 LogTracer::LogTracer()
480 {
481     traceId_ = -1;
482 }
483 
LogTracer(LogTracer && other)484 LogTracer::LogTracer(LogTracer &&other) noexcept: traceId_(other.traceId_)
485 {
486     other.traceId_ = -1;
487 }
488 
operator =(LogTracer && other)489 LogTracer &LogTracer::operator=(LogTracer &&other) noexcept
490 {
491     if (this != &other) {
492         traceId_ = other.traceId_;
493         other.traceId_ = -1;
494     }
495     return *this;
496 }
497 
498 int32_t OHOS::MMI::EventLogHelper::infoDictCount_ = 0;
499 int32_t OHOS::MMI::EventLogHelper::debugDictCount_ = 0;
500 
501 } // namespace MMI
502 } // namespace OHOS
503