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_handler.h"
17
18 #include <cinttypes>
19 #include <cstdio>
20 #include <cstring>
21 #include <functional>
22 #include <vector>
23
24 #include <sys/stat.h>
25 #include <unistd.h>
26
27 #include "libinput.h"
28 #include "key_command_handler.h"
29 #include "timer_manager.h"
30 #include "util.h"
31
32 namespace OHOS {
33 namespace MMI {
34 namespace {
35 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "InputEventHandler" };
36 constexpr int32_t MT_TOOL_PALM = 2;
37 } // namespace
38
InputEventHandler()39 InputEventHandler::InputEventHandler()
40 {
41 udsServer_ = nullptr;
42 }
43
~InputEventHandler()44 InputEventHandler::~InputEventHandler() {}
45
Init(UDSServer & udsServer)46 void InputEventHandler::Init(UDSServer& udsServer)
47 {
48 udsServer_ = &udsServer;
49 BuildInputHandlerChain();
50 }
51
OnEvent(void * event,int64_t frameTime)52 void InputEventHandler::OnEvent(void *event, int64_t frameTime)
53 {
54 CHKPV(eventNormalizeHandler_);
55 if (event == nullptr) {
56 eventNormalizeHandler_->HandleEvent(nullptr, frameTime);
57 return;
58 }
59
60 CHKPV(event);
61 idSeed_ += 1;
62 const uint64_t maxUInt64 = (std::numeric_limits<uint64_t>::max)() - 1;
63 if (idSeed_ >= maxUInt64) {
64 MMI_HILOGE("The value is flipped. id:%{public}" PRId64, idSeed_);
65 idSeed_ = 1;
66 }
67
68 auto *lpEvent = static_cast<libinput_event *>(event);
69 CHKPV(lpEvent);
70 int32_t eventType = libinput_event_get_type(lpEvent);
71 int64_t beginTime = GetSysClockTime();
72 MMI_HILOGD("Event reporting. id:%{public}" PRId64 ",tid:%{public}" PRId64 ",eventType:%{public}d,"
73 "beginTime:%{public}" PRId64, idSeed_, GetThisThreadId(), eventType, beginTime);
74 if (IsTouchpadMistouch(lpEvent)) {
75 return;
76 }
77 eventNormalizeHandler_->HandleEvent(lpEvent, frameTime);
78 int64_t endTime = GetSysClockTime();
79 int64_t lostTime = endTime - beginTime;
80 MMI_HILOGD("Event handling completed. id:%{public}" PRId64 ",endTime:%{public}" PRId64
81 ",lostTime:%{public}" PRId64, idSeed_, endTime, lostTime);
82 }
83
IsTouchpadMistouch(libinput_event * event)84 bool InputEventHandler::IsTouchpadMistouch(libinput_event* event)
85 {
86 CHKPF(event);
87 auto touchpad = libinput_event_get_touchpad_event(event);
88 if (touchpad != nullptr) {
89 int32_t toolType = libinput_event_touchpad_get_tool_type(touchpad);
90 if (toolType == MT_TOOL_PALM) {
91 MMI_HILOGD("touchpad event is palm");
92 return false;
93 }
94 }
95
96 auto type = libinput_event_get_type(event);
97 if (type == LIBINPUT_EVENT_POINTER_BUTTON_TOUCHPAD) {
98 MMI_HILOGD("touchpad event is button");
99 return false;
100 }
101
102 if (type == LIBINPUT_EVENT_POINTER_TAP) {
103 auto isTapMistouch = IsTouchpadTapMistouch(event);
104 return isTapMistouch;
105 }
106
107 if (type == LIBINPUT_EVENT_KEYBOARD_KEY) {
108 isTyping_ = true;
109 if (TimerMgr->IsExist(timerId_)) {
110 TimerMgr->ResetTimer(timerId_);
111 } else {
112 static constexpr int32_t timeout = 500;
113 std::weak_ptr<InputEventHandler> weakPtr = shared_from_this();
114 timerId_ = TimerMgr->AddTimer(timeout, 1, [weakPtr]() {
115 CALL_DEBUG_ENTER;
116 auto sharedPtr = weakPtr.lock();
117 CHKPV(sharedPtr);
118 MMI_HILOGD("mistouch timer:%{public}d", sharedPtr->timerId_);
119 sharedPtr->timerId_ = -1;
120 sharedPtr->isTyping_ = false;
121 });
122 }
123 }
124 if (isTyping_ && (type == LIBINPUT_EVENT_POINTER_MOTION_TOUCHPAD || type == LIBINPUT_EVENT_TOUCHPAD_MOTION)) {
125 MMI_HILOGD("The touchpad event is mistouch");
126 return true;
127 }
128 return false;
129 }
130
IsTouchpadTapMistouch(libinput_event * event)131 bool InputEventHandler::IsTouchpadTapMistouch(libinput_event* event)
132 {
133 CHKPF(event);
134 auto data = libinput_event_get_pointer_event(event);
135 CHKPF(data);
136 auto state = libinput_event_pointer_get_button_state(data);
137 if (state == LIBINPUT_BUTTON_STATE_PRESSED) {
138 if (isTyping_) {
139 isTapMistouch_ = true;
140 MMI_HILOGD("The tapPressed event is mistouch");
141 return true;
142 }
143 }
144 if (state == LIBINPUT_BUTTON_STATE_RELEASED) {
145 if (isTapMistouch_) {
146 isTapMistouch_ = false;
147 MMI_HILOGD("The tapReleased event is mistouch");
148 return true;
149 }
150 }
151 return false;
152 }
153
BuildInputHandlerChain()154 int32_t InputEventHandler::BuildInputHandlerChain()
155 {
156 eventNormalizeHandler_ = std::make_shared<EventNormalizeHandler>();
157 #if !defined(OHOS_BUILD_ENABLE_KEYBOARD) && !defined(OHOS_BUILD_ENABLE_POINTER) && !defined(OHOS_BUILD_ENABLE_TOUCH)
158 return RET_OK;
159 #endif // !OHOS_BUILD_ENABLE_KEYBOARD && !OHOS_BUILD_ENABLE_POINTER && !OHOS_BUILD_ENABLE_TOUCH
160
161 std::shared_ptr<IInputEventHandler> handler = eventNormalizeHandler_;
162 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
163 eventFilterHandler_ = std::make_shared<EventFilterHandler>();
164 handler->SetNext(eventFilterHandler_);
165 handler = eventFilterHandler_;
166 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
167
168 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
169 eventInterceptorHandler_ = std::make_shared<EventInterceptorHandler>();
170 handler->SetNext(eventInterceptorHandler_);
171 handler = eventInterceptorHandler_;
172 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR
173
174 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
175 #ifdef OHOS_BUILD_ENABLE_COMBINATION_KEY
176 eventKeyCommandHandler_ = std::make_shared<KeyCommandHandler>();
177 handler->SetNext(eventKeyCommandHandler_);
178 handler = eventKeyCommandHandler_;
179 #endif // OHOS_BUILD_ENABLE_COMBINATION_KEY
180 eventSubscriberHandler_ = std::make_shared<KeySubscriberHandler>();
181 handler->SetNext(eventSubscriberHandler_);
182 handler = eventSubscriberHandler_;
183 #endif // OHOS_BUILD_ENABLE_KEYBOARD
184 #ifdef OHOS_BUILD_ENABLE_SWITCH
185 switchEventSubscriberHandler_ = std::make_shared<SwitchSubscriberHandler>();
186 handler->SetNext(switchEventSubscriberHandler_);
187 handler = switchEventSubscriberHandler_;
188 #endif // OHOS_BUILD_ENABLE_SWITCH
189 #ifdef OHOS_BUILD_ENABLE_MONITOR
190 eventMonitorHandler_ = std::make_shared<EventMonitorHandler>();
191 handler->SetNext(eventMonitorHandler_);
192 handler = eventMonitorHandler_;
193 #endif // OHOS_BUILD_ENABLE_MONITOR
194 eventDispatchHandler_ = std::make_shared<EventDispatchHandler>();
195 handler->SetNext(eventDispatchHandler_);
196 return RET_OK;
197 }
198
GetUDSServer() const199 UDSServer* InputEventHandler::GetUDSServer() const
200 {
201 return udsServer_;
202 }
203
GetEventNormalizeHandler() const204 std::shared_ptr<EventNormalizeHandler> InputEventHandler::GetEventNormalizeHandler() const
205 {
206 return eventNormalizeHandler_;
207 }
208
GetInterceptorHandler() const209 std::shared_ptr<EventInterceptorHandler> InputEventHandler::GetInterceptorHandler() const
210 {
211 return eventInterceptorHandler_;
212 }
213
GetSubscriberHandler() const214 std::shared_ptr<KeySubscriberHandler> InputEventHandler::GetSubscriberHandler() const
215 {
216 return eventSubscriberHandler_;
217 }
218
GetSwitchSubscriberHandler() const219 std::shared_ptr<SwitchSubscriberHandler> InputEventHandler::GetSwitchSubscriberHandler() const
220 {
221 return switchEventSubscriberHandler_;
222 }
223
GetKeyCommandHandler() const224 std::shared_ptr<KeyCommandHandler> InputEventHandler::GetKeyCommandHandler() const
225 {
226 return eventKeyCommandHandler_;
227 }
228
GetMonitorHandler() const229 std::shared_ptr<EventMonitorHandler> InputEventHandler::GetMonitorHandler() const
230 {
231 return eventMonitorHandler_;
232 }
233
GetFilterHandler() const234 std::shared_ptr<EventFilterHandler> InputEventHandler::GetFilterHandler() const
235 {
236 return eventFilterHandler_;
237 }
238
GetEventDispatchHandler() const239 std::shared_ptr<EventDispatchHandler> InputEventHandler::GetEventDispatchHandler() const
240 {
241 return eventDispatchHandler_;
242 }
243 } // namespace MMI
244 } // namespace OHOS