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