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 INPUT_HANDLER_MANAGER_GLOBAL_H 17 #define INPUT_HANDLER_MANAGER_GLOBAL_H 18 #include <mutex> 19 #include <set> 20 #include "input_handler_type.h" 21 #include "i_input_event_handler.h" 22 #include "uds_session.h" 23 #include "singleton.h" 24 25 namespace OHOS { 26 namespace MMI { 27 class InputHandlerManagerGlobal : public Singleton<OHOS::MMI::InputHandlerManagerGlobal> { 28 public: 29 int32_t AddInputHandler(int32_t handlerId, InputHandlerType handlerType, SessionPtr session); 30 void RemoveInputHandler(int32_t handlerId, InputHandlerType handlerType, SessionPtr session); 31 void MarkConsumed(int32_t handlerId, int32_t eventId, SessionPtr session); 32 bool HandleEvent(std::shared_ptr<KeyEvent> KeyEvent); 33 bool HandleEvent(std::shared_ptr<PointerEvent> PointerEvent); 34 35 private: 36 void InitSessionLostCallback(); 37 void OnSessionLost(SessionPtr session); 38 39 private: 40 struct SessionHandler { SessionHandlerSessionHandler41 SessionHandler(int32_t id, InputHandlerType handlerType, SessionPtr session) 42 : id_(id), handlerType_(handlerType), session_(session) { } 43 void SendToClient(std::shared_ptr<KeyEvent> keyEvent) const; 44 void SendToClient(std::shared_ptr<PointerEvent> pointerEvent) const; 45 bool operator<(const SessionHandler& other) const 46 { 47 if (id_ != other.id_) { 48 return (id_ < other.id_); 49 } 50 if (handlerType_ != other.handlerType_) { 51 return (handlerType_ < other.handlerType_); 52 } 53 return (session_ < other.session_); 54 } 55 int32_t id_; 56 InputHandlerType handlerType_; 57 SessionPtr session_ = nullptr; 58 }; 59 60 struct MonitorCollection : public IInputEventHandler, protected NoCopyable { 61 virtual int32_t GetPriority() const override; 62 virtual bool HandleEvent(std::shared_ptr<KeyEvent> KeyEvent) override; 63 virtual bool HandleEvent(std::shared_ptr<PointerEvent> PointerEvent) override; 64 65 int32_t AddMonitor(const SessionHandler& mon); 66 void RemoveMonitor(const SessionHandler& mon); 67 void MarkConsumed(int32_t monitorId, int32_t eventId, SessionPtr session); 68 69 bool HasMonitor(int32_t monitorId, SessionPtr session); 70 void UpdateConsumptionState(std::shared_ptr<PointerEvent> pointerEvent); 71 void Monitor(std::shared_ptr<PointerEvent> pointerEvent); 72 void OnSessionLost(SessionPtr session); 73 74 std::mutex lockMonitors_; 75 std::set<SessionHandler> monitors_; 76 std::shared_ptr<PointerEvent> lastPointerEvent_ = nullptr; 77 int32_t downEventId_ { -1 }; 78 bool monitorConsumed_ { false }; 79 }; 80 81 struct InterceptorCollection : public IInputEventHandler, protected NoCopyable { 82 virtual int32_t GetPriority() const override; 83 virtual bool HandleEvent(std::shared_ptr<KeyEvent> KeyEvent) override; 84 virtual bool HandleEvent(std::shared_ptr<PointerEvent> PointerEvent) override; 85 86 int32_t AddInterceptor(const SessionHandler& interceptor); 87 void RemoveInterceptor(const SessionHandler& interceptor); 88 void OnSessionLost(SessionPtr session); 89 90 std::mutex lockInterceptors_; 91 std::set<SessionHandler> interceptors_; 92 }; 93 94 private: 95 bool sessionLostCallbackInitialized_ { false }; 96 MonitorCollection monitors_; 97 InterceptorCollection interceptors_; 98 }; 99 } // namespace MMI 100 } // namespace OHOS 101 102 #endif // INPUT_HANDLER_MANAGER_GLOBAL_H