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
GetAction() const118 int32_t InputEvent::GetAction() const
119 {
120 return action_;
121 }
122
SetAction(int32_t action)123 void InputEvent::SetAction(int32_t action)
124 {
125 action_ = action;
126 }
127
GetActionStartTime() const128 int64_t InputEvent::GetActionStartTime() const
129 {
130 return actionStartTime_;
131 }
132
SetActionStartTime(int64_t actionStartTime)133 void InputEvent::SetActionStartTime(int64_t actionStartTime)
134 {
135 actionStartTime_ = actionStartTime;
136 }
137
GetDeviceId() const138 int32_t InputEvent::GetDeviceId() const
139 {
140 return deviceId_;
141 }
142
SetDeviceId(int32_t deviceId)143 void InputEvent::SetDeviceId(int32_t deviceId)
144 {
145 deviceId_ = deviceId;
146 }
147
GetTargetDisplayId() const148 int32_t InputEvent::GetTargetDisplayId() const
149 {
150 return targetDisplayId_;
151 }
152
SetTargetDisplayId(int32_t displayId)153 void InputEvent::SetTargetDisplayId(int32_t displayId)
154 {
155 targetDisplayId_ = displayId;
156 }
157
GetAgentWindowId() const158 int32_t InputEvent::GetAgentWindowId() const
159 {
160 return agentWindowId_;
161 }
162
SetAgentWindowId(int32_t windowId)163 void InputEvent::SetAgentWindowId(int32_t windowId)
164 {
165 agentWindowId_ = windowId;
166 }
167
GetTargetWindowId() const168 int32_t InputEvent::GetTargetWindowId() const
169 {
170 return targetWindowId_;
171 }
172
SetTargetWindowId(int32_t windowId)173 void InputEvent::SetTargetWindowId(int32_t windowId)
174 {
175 targetWindowId_ = windowId;
176 }
177
GetEventType() const178 int32_t InputEvent::GetEventType() const
179 {
180 return eventType_;
181 }
182
GetFlag() const183 uint32_t InputEvent::GetFlag() const
184 {
185 return bitwise_;
186 }
187
HasFlag(uint32_t flag)188 bool InputEvent::HasFlag(uint32_t flag)
189 {
190 return (bitwise_ & flag) != 0;
191 }
192
AddFlag(uint32_t flag)193 void InputEvent::AddFlag(uint32_t flag)
194 {
195 bitwise_ |= flag;
196 }
197
ClearFlag()198 void InputEvent::ClearFlag()
199 {
200 bitwise_ = EVENT_FLAG_NONE;
201 }
202
SetProcessedCallback(std::function<void (int32_t)> callback)203 void InputEvent::SetProcessedCallback(std::function<void(int32_t)> callback)
204 {
205 processedCallback_ = callback;
206 }
207
MarkProcessed()208 void InputEvent::MarkProcessed()
209 {
210 if (!processedCallback_) {
211 return;
212 }
213 auto func = processedCallback_;
214 processedCallback_ = std::function<void(int32_t)>();
215 func(id_);
216 }
217
WriteToParcel(Parcel & out) const218 bool InputEvent::WriteToParcel(Parcel &out) const
219 {
220 WRITEINT32(out, eventType_);
221
222 WRITEINT32(out, id_);
223
224 WRITEINT64(out, actionTime_);
225
226 WRITEINT32(out, action_);
227
228 WRITEINT64(out, actionStartTime_);
229
230 WRITEINT32(out, deviceId_);
231
232 WRITEINT32(out, targetDisplayId_);
233
234 WRITEINT32(out, targetWindowId_);
235
236 WRITEINT32(out, agentWindowId_);
237
238 WRITEUINT32(out, bitwise_);
239
240 return true;
241 }
242
ReadFromParcel(Parcel & in)243 bool InputEvent::ReadFromParcel(Parcel &in)
244 {
245 READINT32(in, eventType_);
246
247 READINT32(in, id_);
248
249 READINT64(in, actionTime_);
250
251 READINT32(in, action_);
252
253 READINT64(in, actionStartTime_);
254
255 READINT32(in, deviceId_);
256
257 READINT32(in, targetDisplayId_);
258
259 READINT32(in, targetWindowId_);
260
261 READINT32(in, agentWindowId_);
262
263 READUINT32(in, bitwise_);
264
265 return true;
266 }
267 } // namespace MMI
268 } // namespace OHOS
269