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 #ifndef EVENT_MONITOR_HANDLER_H 17 #define EVENT_MONITOR_HANDLER_H 18 19 #include <mutex> 20 #include <set> 21 #include <unordered_map> 22 #include <unordered_set> 23 24 #include "nocopyable.h" 25 26 #include "i_input_event_handler.h" 27 #include "i_input_event_collection_handler.h" 28 #include "input_handler_type.h" 29 #include "uds_session.h" 30 31 namespace OHOS { 32 namespace MMI { 33 class EventMonitorHandler : public IInputEventHandler { 34 public: 35 EventMonitorHandler() = default; 36 DISALLOW_COPY_AND_MOVE(EventMonitorHandler); 37 ~EventMonitorHandler() = default; 38 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 39 void HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent) override; 40 #endif // OHOS_BUILD_ENABLE_KEYBOARD 41 #ifdef OHOS_BUILD_ENABLE_POINTER 42 void HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent) override; 43 #endif // OHOS_BUILD_ENABLE_POINTER 44 #ifdef OHOS_BUILD_ENABLE_TOUCH 45 void HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent) override; 46 #endif // OHOS_BUILD_ENABLE_TOUCH 47 int32_t AddInputHandler(InputHandlerType handlerType, HandleEventType eventType, SessionPtr session); 48 void RemoveInputHandler(InputHandlerType handlerType, HandleEventType eventType, SessionPtr session); 49 void MarkConsumed(int32_t eventId, SessionPtr session); 50 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 51 bool OnHandleEvent(std::shared_ptr<KeyEvent> KeyEvent); 52 #endif // OHOS_BUILD_ENABLE_KEYBOARD 53 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) 54 bool OnHandleEvent(std::shared_ptr<PointerEvent> PointerEvent); 55 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH 56 void Dump(int32_t fd, const std::vector<std::string> &args); 57 58 private: 59 void InitSessionLostCallback(); 60 void OnSessionLost(SessionPtr session); 61 62 private: 63 class SessionHandler { 64 public: SessionHandler(InputHandlerType handlerType,HandleEventType eventType,SessionPtr session)65 SessionHandler(InputHandlerType handlerType, HandleEventType eventType, SessionPtr session) 66 : handlerType_(handlerType), eventType_(eventType & HANDLE_EVENT_TYPE_ALL), 67 session_(session) {} 68 void SendToClient(std::shared_ptr<KeyEvent> keyEvent) const; 69 void SendToClient(std::shared_ptr<PointerEvent> pointerEvent) const; 70 bool operator<(const SessionHandler& other) const 71 { 72 return (session_ < other.session_); 73 } 74 InputHandlerType handlerType_; 75 HandleEventType eventType_; 76 SessionPtr session_ { nullptr }; 77 }; 78 79 class MonitorCollection : public IInputEventCollectionHandler, protected NoCopyable { 80 public: 81 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 82 virtual 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 virtual bool HandleEvent(std::shared_ptr<PointerEvent> pointerEvent) override; 86 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH 87 int32_t AddMonitor(const SessionHandler& mon); 88 void RemoveMonitor(const SessionHandler& mon); 89 void MarkConsumed(int32_t eventId, SessionPtr session); 90 91 bool HasMonitor(SessionPtr session); 92 #ifdef OHOS_BUILD_ENABLE_TOUCH 93 void UpdateConsumptionState(std::shared_ptr<PointerEvent> pointerEvent); 94 #endif // OHOS_BUILD_ENABLE_TOUCH 95 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) 96 void Monitor(std::shared_ptr<PointerEvent> pointerEvent); 97 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH 98 void OnSessionLost(SessionPtr session); 99 void Dump(int32_t fd, const std::vector<std::string> &args); 100 101 struct ConsumptionState { 102 std::unordered_set<int32_t> eventIds_; 103 bool isMonitorConsumed_ { false }; 104 std::shared_ptr<PointerEvent> lastPointerEvent_ { nullptr }; 105 }; 106 107 private: 108 std::set<SessionHandler> monitors_; 109 std::unordered_map<int32_t, ConsumptionState> states_; 110 }; 111 112 private: 113 bool sessionLostCallbackInitialized_ { false }; 114 MonitorCollection monitors_; 115 }; 116 } // namespace MMI 117 } // namespace OHOS 118 #endif // EVENT_MONITOR_HANDLER_H