1 /* 2 * Copyright (c) 2021-2023 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_H 17 #define INPUT_HANDLER_MANAGER_H 18 19 #include <limits> 20 #include <map> 21 #include <mutex> 22 23 #include "input_device.h" 24 #include "input_handler_type.h" 25 #include "i_input_event_consumer.h" 26 #include "pointer_event.h" 27 28 namespace OHOS { 29 namespace MMI { 30 class InputHandlerManager { 31 public: 32 InputHandlerManager(); 33 virtual ~InputHandlerManager() = default; 34 DISALLOW_COPY_AND_MOVE(InputHandlerManager); 35 36 public: 37 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 38 void OnInputEvent(std::shared_ptr<KeyEvent> keyEvent, uint32_t deviceTags); 39 #endif // OHOS_BUILD_ENABLE_KEYBOARD 40 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) 41 void OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent, uint32_t deviceTags); 42 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH 43 #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR) 44 void OnConnected(); 45 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR 46 bool HasHandler(int32_t handlerId); 47 virtual InputHandlerType GetHandlerType() const = 0; 48 HandleEventType GetEventType() const; 49 int32_t GetPriority() const; 50 uint32_t GetDeviceTags() const; 51 52 protected: 53 int32_t AddHandler(InputHandlerType handlerType, std::shared_ptr<IInputEventConsumer> consumer, 54 HandleEventType eventType = HANDLE_EVENT_TYPE_ALL, int32_t priority = DEFUALT_INTERCEPTOR_PRIORITY, 55 uint32_t deviceTags = CapabilityToTags(InputDeviceCapability::INPUT_DEV_CAP_MAX)); 56 void RemoveHandler(int32_t handlerId, InputHandlerType IsValidHandlerType); 57 58 private: 59 struct Handler { 60 int32_t handlerId_ { 0 }; 61 InputHandlerType handlerType_ { NONE }; 62 HandleEventType eventType_ { HANDLE_EVENT_TYPE_ALL }; 63 int32_t priority_ { DEFUALT_INTERCEPTOR_PRIORITY }; 64 uint32_t deviceTags_ { CapabilityToTags(InputDeviceCapability::INPUT_DEV_CAP_MAX) }; 65 std::shared_ptr<IInputEventConsumer> consumer_ { nullptr }; 66 }; 67 68 private: 69 int32_t GetNextId(); 70 int32_t AddLocal(int32_t handlerId, InputHandlerType handlerType, HandleEventType eventType, 71 int32_t priority, uint32_t deviceTags, std::shared_ptr<IInputEventConsumer> monitor); 72 int32_t AddToServer(InputHandlerType handlerType, HandleEventType eventType, int32_t priority, 73 uint32_t deviceTags); 74 int32_t RemoveLocal(int32_t handlerId, InputHandlerType handlerType, uint32_t &deviceTags); 75 void RemoveFromServer(InputHandlerType handlerType, HandleEventType eventType, int32_t priority, 76 uint32_t deviceTags); 77 78 std::shared_ptr<IInputEventConsumer> FindHandler(int32_t handlerId); 79 void OnDispatchEventProcessed(int32_t eventId, int64_t actionTime); 80 void AddMouseEventId(std::shared_ptr<PointerEvent> pointerEvent); 81 void AddProcessedEventId(std::shared_ptr<PointerEvent> pointerEvent, int32_t consumerCount); 82 int32_t GetMonitorConsumerInfos(std::shared_ptr<PointerEvent> pointerEvent, 83 std::map<int32_t, std::shared_ptr<IInputEventConsumer>> &consumerInfos); 84 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) 85 bool CheckInputDeviceSource(const std::shared_ptr<PointerEvent> pointerEvent, uint32_t deviceTags) const; 86 void GetConsumerInfos(std::shared_ptr<PointerEvent> pointerEvent, uint32_t deviceTags, 87 std::map<int32_t, std::shared_ptr<IInputEventConsumer>> &consumerInfos); 88 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH 89 90 private: 91 std::list<Handler> interHandlers_; 92 std::map<int32_t, Handler> monitorHandlers_; 93 std::map<int32_t, int32_t> processedEvents_; 94 std::set<int32_t> mouseEventIds_; 95 std::function<void(int32_t, int64_t)> monitorCallback_ { nullptr }; 96 int32_t nextId_ { 1 }; 97 std::mutex mtxHandlers_; 98 }; 99 } // namespace MMI 100 } // namespace OHOS 101 #endif // INPUT_HANDLER_MANAGER_H 102