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