1 /* 2 * Copyright (c) 2025 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_PRE_MONITOR_HANDLER_H 17 #define EVENT_PRE_MONITOR_HANDLER_H 18 19 #include "i_input_event_collection_handler.h" 20 #include "i_input_event_handler.h" 21 #include "uds_session.h" 22 23 namespace OHOS { 24 namespace MMI { 25 class EventPreMonitorHandler final : public IInputEventHandler { 26 public: 27 EventPreMonitorHandler() = default; 28 DISALLOW_COPY_AND_MOVE(EventPreMonitorHandler); 29 ~EventPreMonitorHandler() override = default; 30 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 31 void HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent) override; 32 #endif // OHOS_BUILD_ENABLE_KEYBOARD 33 #ifdef OHOS_BUILD_ENABLE_POINTER 34 void HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent) override; 35 #endif // OHOS_BUILD_ENABLE_POINTER 36 #ifdef OHOS_BUILD_ENABLE_TOUCH 37 void HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent) override; 38 #endif // OHOS_BUILD_ENABLE_TOUCH 39 40 int32_t AddInputHandler(SessionPtr sess, int32_t handlerId, HandleEventType eventType, std::vector<int32_t> keys); 41 void RemoveInputHandler(SessionPtr sess, int32_t handlerId); 42 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 43 bool OnHandleEvent(std::shared_ptr<KeyEvent> KeyEvent); 44 #endif // OHOS_BUILD_ENABLE_KEYBOARD 45 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) 46 bool OnHandleEvent(std::shared_ptr<PointerEvent> PointerEvent); 47 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH 48 void Dump(int32_t fd, const std::vector<std::string> &args); 49 50 private: 51 void InitSessionLostCallback(); 52 void OnSessionLost(SessionPtr session); 53 54 private: 55 class SessionHandler { 56 public: SessionHandler(SessionPtr session,int32_t handlerId,HandleEventType eventType,std::vector<int32_t> keys)57 SessionHandler(SessionPtr session, int32_t handlerId, HandleEventType eventType, std::vector<int32_t> keys) 58 : session_(session), handlerId_(handlerId), eventType_(eventType), keys_(keys) 59 {} 60 SessionHandler(const SessionHandler & other)61 SessionHandler(const SessionHandler& other) 62 { 63 session_ = other.session_; 64 handlerId_ = other.handlerId_; 65 eventType_ = other.eventType_; 66 keys_ = other.keys_; 67 } 68 69 void SendToClient(std::shared_ptr<KeyEvent> keyEvent, NetPacket &pkt, int32_t handlerId) const; 70 bool operator<(const SessionHandler& other) const 71 { 72 return (session_ < other.session_); 73 } 74 75 SessionPtr session_ { nullptr }; 76 int32_t handlerId_; 77 HandleEventType eventType_; 78 std::shared_ptr<IInputEventConsumer> callback_ { nullptr }; 79 std::vector<int32_t> keys_; 80 }; 81 82 class MonitorCollection : public IInputEventCollectionHandler, protected NoCopyable { 83 public: 84 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 85 virtual bool HandleEvent(std::shared_ptr<KeyEvent> KeyEvent) override; 86 #endif // OHOS_BUILD_ENABLE_KEYBOARD 87 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) 88 virtual bool HandleEvent(std::shared_ptr<PointerEvent> pointerEvent) override; 89 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH 90 int32_t AddMonitor(const std::shared_ptr<SessionHandler> monitor, std::vector<int32_t> keys); 91 void RemoveMonitor(SessionPtr sess, int32_t handlerId); 92 bool IsEqualsKeys(std::vector<int32_t> newKeys, std::vector<int32_t> oldKeys); 93 94 void OnSessionLost(SessionPtr session); 95 void Dump(int32_t fd, const std::vector<std::string> &args); 96 97 private: 98 std::map<std::vector<int32_t>, std::list<std::shared_ptr<SessionHandler>>> sessionHandlers_; 99 }; 100 101 private: 102 bool sessionLostCallbackInitialized_ { false }; 103 MonitorCollection monitors_; 104 }; 105 } // namespace MMI 106 } // namespace OHOS 107 #endif // EVENT_PRE_MONITOR_HANDLER_H