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