• 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 <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 #if defined(ANDROID_PLATFORM) || defined(IOS_PLATFORM)
283     return false;
284 #endif
285     WRITEINT32(out, eventType_);
286     WRITEINT32(out, id_);
287     WRITEINT64(out, actionTime_);
288     WRITEUINT64(out, sensorInputTime_);
289     WRITEINT32(out, action_);
290     WRITEINT64(out, actionStartTime_);
291     WRITEINT32(out, deviceId_);
292     WRITEINT32(out, targetDisplayId_);
293     WRITEINT32(out, targetWindowId_);
294     WRITEINT32(out, agentWindowId_);
295     WRITEUINT32(out, bitwise_);
296     WRITEBOOL(out, markEnabled_);
297     if (extraData_ && extraDataLength_ != 0) {
298         WRITEUINT32(out, extraDataLength_);
299         WRITEBUFFER(out, (void *)extraData_.get(), extraDataLength_);
300     } else {
301         WRITEUINT32(out, 0);
302     }
303     return true;
304 }
305 
ReadFromParcel(Parcel & in)306 bool InputEvent::ReadFromParcel(Parcel &in)
307 {
308 #if defined(ANDROID_PLATFORM) || defined(IOS_PLATFORM)
309     return false;
310 #endif
311     READINT32(in, eventType_);
312     READINT32(in, id_);
313     READINT64(in, actionTime_);
314     READUINT64(in, sensorInputTime_);
315     READINT32(in, action_);
316     READINT64(in, actionStartTime_);
317     READINT32(in, deviceId_);
318     READINT32(in, targetDisplayId_);
319     READINT32(in, targetWindowId_);
320     READINT32(in, agentWindowId_);
321     READUINT32(in, bitwise_);
322     READBOOL(in, markEnabled_);
323     READUINT32(in, extraDataLength_);
324 
325     if (extraDataLength_ == 0) {
326         return true;
327     }
328     if (extraDataLength_ > DATA_LENGTH_LIMIT) {
329         extraDataLength_ = 0;
330         return false;
331     }
332     const uint8_t *buffer = in.ReadBuffer(extraDataLength_);
333     std::shared_ptr<uint8_t[]> sp(new uint8_t[extraDataLength_], [](uint8_t* ptr) { delete[] ptr; });
334     if ((buffer == nullptr) || (sp == nullptr)) {
335         extraDataLength_ = 0;
336         return false;
337     }
338     std::copy(buffer, buffer + extraDataLength_, sp.get());
339     extraData_ = sp;
340     return true;
341 }
342 
ActionToShortStr(int32_t action)343 std::string_view InputEvent::ActionToShortStr(int32_t action)
344 {
345     switch (action) {
346         case InputEvent::ACTION_CANCEL:
347             return "B:C:";
348         case InputEvent::ACTION_UNKNOWN:
349             return "B:UK:";
350         default:
351             return "B:?:";
352     }
353 }
354 
355 struct LogTraceKey {
356     int64_t traceId;
357     int32_t action;
358     int32_t evtType;
359 };
360 
361 thread_local std::vector<LogTraceKey> g_traceIds;
362 thread_local std::unordered_map<int64_t, size_t> g_traceIdToIdx;
363 thread_local std::string g_traceStr;
364 
Action2Str(int32_t eventType,int32_t action)365 std::string_view Action2Str(int32_t eventType, int32_t action)
366 {
367     switch (eventType) {
368         case InputEvent::EVENT_TYPE_KEY: {
369             return KeyEvent::ActionToShortStr(action);
370         }
371         case InputEvent::EVENT_TYPE_POINTER:
372         case InputEvent::EVENT_TYPE_FINGERPRINT: {
373             return PointerEvent::ActionToShortStr(action);
374         }
375         case InputEvent::EVENT_TYPE_AXIS: {
376             return AxisEvent::ActionToShortStr(action);
377         }
378         case InputEvent::EVENT_TYPE_BASE: {
379             return InputEvent::ActionToShortStr(action);
380         }
381         default: {
382             return "?:?:";
383         }
384     }
385 }
386 
RefreshTraceStr()387 void RefreshTraceStr()
388 {
389     g_traceStr.clear();
390     for (auto item = g_traceIds.begin(); item < g_traceIds.end(); ++item) {
391         if (item->traceId == -1) {
392             continue;
393         }
394         if (item != g_traceIds.begin()) {
395             g_traceStr += "/";
396         }
397         g_traceStr += Action2Str(item->evtType, item->action);
398         g_traceStr += std::to_string(item->traceId);
399     }
400 }
401 
StartLogTraceId(int64_t traceId,int32_t eventType,int32_t action)402 void StartLogTraceId(int64_t traceId, int32_t eventType, int32_t action)
403 {
404     if (traceId == -1) {
405         return;
406     }
407     auto iter = g_traceIdToIdx.find(traceId);
408     if (iter == g_traceIdToIdx.end()) {
409         g_traceIds.push_back({traceId, action, eventType});
410         g_traceIdToIdx.emplace(traceId, g_traceIds.size() - 1);
411         std::string currentTraceStr(Action2Str(eventType, action));
412         currentTraceStr += std::to_string(traceId);
413         if (g_traceIds.size() == 1) {
414             g_traceStr = currentTraceStr;
415         } else {
416             g_traceStr += "/" + currentTraceStr;
417         }
418         return;
419     }
420     if (g_traceIds.size() <= iter->second) {
421         return;
422     }
423     LogTraceKey &old = g_traceIds.at(iter->second);
424     if (old.evtType != eventType || old.action != action) {
425         old.evtType = eventType;
426         old.action = action;
427         RefreshTraceStr();
428     }
429 };
430 
EndLogTraceId(int64_t id)431 void EndLogTraceId(int64_t id)
432 {
433     auto iter = g_traceIdToIdx.find(id);
434     if (iter == g_traceIdToIdx.end()) {
435         return;
436     }
437     size_t idx = iter->second;
438     g_traceIdToIdx.erase(iter);
439     size_t idCount = g_traceIds.size();
440     if (idCount <= idx) {
441         return;
442     }
443 
444     if (idCount == idx + 1) {
445         g_traceIds.pop_back();
446         while (!g_traceIds.empty() && g_traceIds.back().traceId == -1) {
447             g_traceIds.pop_back();
448         }
449     } else {
450         // can't erase it, erase it will make the index of other elem changed.
451         LogTraceKey &toDelete = g_traceIds.at(idx);
452         toDelete.traceId = -1;
453     }
454     RefreshTraceStr();
455 }
456 
FormatLogTrace()457 const char *FormatLogTrace()
458 {
459     return g_traceStr.c_str();
460 }
461 
462 
ResetLogTrace()463 void ResetLogTrace()
464 {
465     g_traceIds.clear();
466     g_traceIdToIdx.clear();
467     g_traceStr.clear();
468 }
469 
LogTracer(int64_t traceId,int32_t evtType,int32_t action)470 LogTracer::LogTracer(int64_t traceId, int32_t evtType, int32_t action)
471 {
472     traceId_ = traceId;
473     StartLogTraceId(traceId, evtType, action);
474 }
475 
~LogTracer()476 LogTracer::~LogTracer()
477 {
478     EndLogTraceId(traceId_);
479 }
480 
LogTracer()481 LogTracer::LogTracer()
482 {
483     traceId_ = -1;
484 }
485 
LogTracer(LogTracer && other)486 LogTracer::LogTracer(LogTracer &&other) noexcept: traceId_(other.traceId_)
487 {
488     other.traceId_ = -1;
489 }
490 
operator =(LogTracer && other)491 LogTracer &LogTracer::operator=(LogTracer &&other) noexcept
492 {
493     if (this != &other) {
494         traceId_ = other.traceId_;
495         other.traceId_ = -1;
496     }
497     return *this;
498 }
499 
500 int32_t OHOS::MMI::EventLogHelper::infoDictCount_ = 0;
501 int32_t OHOS::MMI::EventLogHelper::debugDictCount_ = 0;
502 
503 } // namespace MMI
504 } // namespace OHOS
505