1 /* 2 * Copyright (c) 2021-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_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 "gesture_monitor_handler.h" 27 #include "i_input_event_collection_handler.h" 28 #include "i_input_event_handler.h" 29 #include "input_handler_type.h" 30 #include "uds_session.h" 31 #include "nap_process.h" 32 33 namespace OHOS { 34 namespace MMI { 35 class EventMonitorHandler final : public IInputEventHandler { 36 public: 37 EventMonitorHandler() = default; 38 DISALLOW_COPY_AND_MOVE(EventMonitorHandler); 39 ~EventMonitorHandler() override = default; 40 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 41 void HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent) override; 42 #endif // OHOS_BUILD_ENABLE_KEYBOARD 43 #ifdef OHOS_BUILD_ENABLE_POINTER 44 void HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent) override; 45 #endif // OHOS_BUILD_ENABLE_POINTER 46 #ifdef OHOS_BUILD_ENABLE_TOUCH 47 void HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent) override; 48 #endif // OHOS_BUILD_ENABLE_TOUCH 49 bool CheckHasInputHandler(HandleEventType eventType); 50 int32_t AddInputHandler(InputHandlerType handlerType, 51 HandleEventType eventType, std::shared_ptr<IInputEventConsumer> callback); 52 void RemoveInputHandler(InputHandlerType handlerType, 53 HandleEventType eventType, std::shared_ptr<IInputEventConsumer> callback); 54 int32_t AddInputHandler(InputHandlerType handlerType, HandleEventType eventType, 55 SessionPtr session, TouchGestureType gestureType = TOUCH_GESTURE_TYPE_NONE, int32_t fingers = 0); 56 void RemoveInputHandler(InputHandlerType handlerType, HandleEventType eventType, 57 SessionPtr session, TouchGestureType gestureType = TOUCH_GESTURE_TYPE_NONE, int32_t fingers = 0); 58 int32_t AddInputHandler(InputHandlerType handlerType, std::vector<int32_t> actionsType, SessionPtr session); 59 void RemoveInputHandler(InputHandlerType handlerType, std::vector<int32_t> actionsType, SessionPtr session); 60 void MarkConsumed(int32_t eventId, SessionPtr session); 61 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 62 bool OnHandleEvent(std::shared_ptr<KeyEvent> KeyEvent); 63 #endif // OHOS_BUILD_ENABLE_KEYBOARD 64 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) 65 bool OnHandleEvent(std::shared_ptr<PointerEvent> PointerEvent); 66 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH 67 void Dump(int32_t fd, const std::vector<std::string> &args); 68 #ifdef PLAYER_FRAMEWORK_EXISTS 69 void ProcessScreenCapture(int32_t pid, bool isStart); 70 #endif 71 72 private: 73 void InitSessionLostCallback(); 74 void OnSessionLost(SessionPtr session); 75 76 private: 77 class SessionHandler { 78 public: SessionHandler(InputHandlerType handlerType, HandleEventType eventType, SessionPtr session, std::shared_ptr<IInputEventConsumer> cb = nullptr)79 SessionHandler(InputHandlerType handlerType, HandleEventType eventType, 80 SessionPtr session, std::shared_ptr<IInputEventConsumer> cb = nullptr) 81 : handlerType_(handlerType), eventType_(eventType & HANDLE_EVENT_TYPE_ALL), 82 session_(session), callback_(cb) {} 83 SessionHandler(InputHandlerType handlerType, HandleEventType eventType, SessionPtr session, TouchGestureType gestureType, int32_t fingers)84 SessionHandler(InputHandlerType handlerType, HandleEventType eventType, 85 SessionPtr session, TouchGestureType gestureType, int32_t fingers) 86 : handlerType_(handlerType), eventType_(eventType & HANDLE_EVENT_TYPE_ALL), 87 session_(session) 88 { 89 gesture_.AddGestureMonitor(gestureType, fingers); 90 } 91 SessionHandler(InputHandlerType handlerType, uint32_t eventType, SessionPtr session, std::vector<int32_t> actionsType, std::shared_ptr<IInputEventConsumer> cb = nullptr)92 SessionHandler(InputHandlerType handlerType, uint32_t eventType, SessionPtr session, 93 std::vector<int32_t> actionsType, std::shared_ptr<IInputEventConsumer> cb = nullptr) 94 : handlerType_(handlerType), eventType_(eventType), session_(session), actionsType_(actionsType), 95 callback_(cb) {} 96 SessionHandler(const SessionHandler& other)97 SessionHandler(const SessionHandler& other) 98 { 99 handlerType_ = other.handlerType_; 100 eventType_ = other.eventType_; 101 session_ = other.session_; 102 callback_ = other.callback_; 103 gesture_ = other.gesture_; 104 actionsType_ = other.actionsType_; 105 } 106 107 bool Expect(std::shared_ptr<PointerEvent> pointerEvent) const; 108 void SendToClient(std::shared_ptr<KeyEvent> keyEvent, NetPacket &pkt) const; 109 void SendToClient(std::shared_ptr<PointerEvent> pointerEvent, NetPacket &pkt) const; operator <(const SessionHandler& other) const110 bool operator<(const SessionHandler& other) const 111 { 112 return (session_ < other.session_); 113 } operator ()(const GestureMonitorHandler& other)114 void operator()(const GestureMonitorHandler& other) 115 { 116 gesture_ = other; 117 } 118 119 InputHandlerType handlerType_; 120 HandleEventType eventType_; 121 SessionPtr session_ { nullptr }; 122 std::vector<int32_t> actionsType_; 123 GestureMonitorHandler gesture_; 124 std::shared_ptr<IInputEventConsumer> callback_ { nullptr }; 125 }; 126 127 class MonitorCollection : public IInputEventCollectionHandler, protected NoCopyable { 128 public: 129 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 130 virtual bool HandleEvent(std::shared_ptr<KeyEvent> KeyEvent) override; 131 #endif // OHOS_BUILD_ENABLE_KEYBOARD 132 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) 133 virtual bool HandleEvent(std::shared_ptr<PointerEvent> pointerEvent) override; 134 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH 135 bool CheckHasInputHandler(HandleEventType eventType); 136 int32_t UpdateEventTypeMonitor(const std::set<SessionHandler>::iterator &iter, 137 const SessionHandler &monitor, SessionHandler &handler, bool isFound); 138 int32_t UpdateActionsTypeMonitor(const std::set<SessionHandler>::iterator &iter, 139 const SessionHandler &monitor, bool isFound); 140 int32_t AddMonitor(const SessionHandler& mon); 141 void RemoveMonitor(const SessionHandler& mon); 142 void MarkConsumed(int32_t eventId, SessionPtr session); 143 bool IsNeedInsertToMonitors(std::vector<int32_t> actionsType); 144 145 bool HasMonitor(SessionPtr session); 146 bool HasScreenCaptureMonitor(SessionPtr session); 147 void RemoveScreenCaptureMonitor(SessionPtr session); 148 void RecoveryScreenCaptureMonitor(SessionPtr session); 149 #ifdef OHOS_BUILD_ENABLE_TOUCH 150 void UpdateConsumptionState(std::shared_ptr<PointerEvent> pointerEvent); 151 #endif // OHOS_BUILD_ENABLE_TOUCH 152 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) 153 void IsSendToClient(const SessionHandler &monitor, std::shared_ptr<PointerEvent> pointerEvent, 154 NetPacket &pkt); 155 void Monitor(std::shared_ptr<PointerEvent> pointerEvent); 156 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH 157 void OnSessionLost(SessionPtr session); 158 void Dump(int32_t fd, const std::vector<std::string> &args); 159 160 struct ConsumptionState { 161 std::set<int32_t> eventIds_; 162 bool isMonitorConsumed_ { false }; 163 std::shared_ptr<PointerEvent> lastPointerEvent_ { nullptr }; 164 }; 165 166 private: 167 std::set<SessionHandler> monitors_; 168 std::map<int32_t, std::set<SessionHandler>> endScreenCaptureMonitors_; 169 std::unordered_map<int32_t, ConsumptionState> states_; 170 std::vector<int32_t> insertToMonitorsActions_; 171 }; 172 173 private: 174 bool sessionLostCallbackInitialized_ { false }; 175 MonitorCollection monitors_; 176 }; 177 } // namespace MMI 178 } // namespace OHOS 179 #endif // EVENT_MONITOR_HANDLER_H