1 /* 2 * Copyright (c) 2022-2024 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 #ifndef EVENT_INTERCEPTOR_HANDLER_H 17 #define EVENT_INTERCEPTOR_HANDLER_H 18 19 #include "i_input_event_handler.h" 20 #include "i_input_event_collection_handler.h" 21 #include "input_device.h" 22 #include "uds_session.h" 23 24 namespace OHOS { 25 namespace MMI { 26 class EventInterceptorHandler : public IInputEventHandler { 27 public: 28 EventInterceptorHandler() = default; 29 DISALLOW_COPY_AND_MOVE(EventInterceptorHandler); 30 ~EventInterceptorHandler() = default; 31 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 32 void HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent) override; 33 #endif // OHOS_BUILD_ENABLE_KEYBOARD 34 #ifdef OHOS_BUILD_ENABLE_POINTER 35 void HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent) override; 36 #endif // OHOS_BUILD_ENABLE_POINTER 37 #ifdef OHOS_BUILD_ENABLE_TOUCH 38 void HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent) override; 39 #endif // OHOS_BUILD_ENABLE_TOUCH 40 int32_t AddInputHandler(InputHandlerType handlerType, HandleEventType eventType, 41 int32_t priority, uint32_t deviceTags, SessionPtr session); 42 void RemoveInputHandler(InputHandlerType handlerType, HandleEventType eventType, 43 int32_t priority, uint32_t deviceTags, SessionPtr session); 44 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 45 bool OnHandleEvent(std::shared_ptr<KeyEvent> keyEvent); 46 #endif // OHOS_BUILD_ENABLE_KEYBOARD 47 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) 48 bool OnHandleEvent(std::shared_ptr<PointerEvent> pointerEvent); 49 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH 50 void Dump(int32_t fd, const std::vector<std::string> &args); 51 static bool CheckInputDeviceSource( 52 const std::shared_ptr<PointerEvent> pointerEvent, uint32_t deviceTags); 53 54 private: 55 void InitSessionLostCallback(); 56 void OnSessionLost(SessionPtr session); 57 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 58 bool KeyInterceptByHostOSWhiteList(int32_t keyCode); 59 bool TouchPadKnuckleDoubleClickHandle(std::shared_ptr<KeyEvent> event); 60 #endif // OHOS_BUILD_ENABLE_KEYBOARD 61 private: 62 class SessionHandler { 63 public: SessionHandler(InputHandlerType handlerType,HandleEventType eventType,int32_t priority,uint32_t deviceTags,SessionPtr session)64 SessionHandler(InputHandlerType handlerType, HandleEventType eventType, int32_t priority, 65 uint32_t deviceTags, SessionPtr session) : handlerType_(handlerType), 66 eventType_(eventType & HANDLE_EVENT_TYPE_ALL), priority_(priority), deviceTags_(deviceTags), 67 session_(session) {} 68 void SendToClient(std::shared_ptr<KeyEvent> keyEvent) const; 69 void SendToClient(std::shared_ptr<PointerEvent> pointerEvent) const; 70 InputHandlerType handlerType_ { NONE }; 71 HandleEventType eventType_ { HANDLE_EVENT_TYPE_ALL }; 72 int32_t priority_ { DEFUALT_INTERCEPTOR_PRIORITY }; 73 uint32_t deviceTags_ { CapabilityToTags(InputDeviceCapability::INPUT_DEV_CAP_MAX) }; 74 SessionPtr session_ { nullptr }; 75 }; 76 77 class InterceptorCollection final : public IInputEventCollectionHandler, protected NoCopyable { 78 public: 79 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 80 bool HandleEvent(std::shared_ptr<KeyEvent> keyEvent) override; 81 #endif // OHOS_BUILD_ENABLE_KEYBOARD 82 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) 83 bool HandleEvent(std::shared_ptr<PointerEvent> pointerEvent) override; 84 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH 85 int32_t AddInterceptor(const SessionHandler& interceptor); 86 void RemoveInterceptor(const SessionHandler& interceptor); 87 void OnSessionLost(SessionPtr session); 88 void Dump(int32_t fd, const std::vector<std::string> &args); 89 std::list<SessionHandler> interceptors_; 90 }; 91 92 private: 93 bool sessionLostCallbackInitialized_ { false }; 94 InterceptorCollection interceptors_; 95 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 96 std::unique_ptr<std::string> keyevent_intercept_whitelist{nullptr}; 97 #endif // OHOS_BUILD_ENABLE_KEYBOARD 98 }; 99 } // namespace MMI 100 } // namespace OHOS 101 #endif // EVENT_INTERCEPTOR_HANDLER_H