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