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 #include <iterator> 23 24 #include "input_device.h" 25 #include "input_handler_type.h" 26 #include "i_input_event_consumer.h" 27 #include "pointer_event.h" 28 29 namespace OHOS { 30 namespace MMI { 31 class InputHandlerManager { 32 public: 33 InputHandlerManager(); 34 virtual ~InputHandlerManager() = default; 35 DISALLOW_COPY_AND_MOVE(InputHandlerManager); 36 37 public: 38 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 39 void OnInputEvent(std::shared_ptr<KeyEvent> keyEvent, uint32_t deviceTags); 40 #endif // OHOS_BUILD_ENABLE_KEYBOARD 41 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) 42 void OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent, uint32_t deviceTags); 43 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH 44 #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR) 45 template<typename T> 46 bool RecoverPointerEvent(std::initializer_list<T> pointerActionEvents, T pointerActionEvent); 47 void OnConnected(); 48 void OnDisconnected(); 49 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR 50 bool HasHandler(int32_t handlerId); 51 virtual InputHandlerType GetHandlerType() const = 0; 52 HandleEventType GetEventType() const; 53 int32_t GetPriority() const; 54 uint32_t GetDeviceTags() const; 55 std::vector<int32_t> GetActionsType() const; 56 57 protected: 58 int32_t AddGestureMonitor(InputHandlerType handlerType, std::shared_ptr<IInputEventConsumer> consumer, 59 HandleEventType eventType, TouchGestureType gestureType, int32_t fingers); 60 int32_t RemoveGestureMonitor(int32_t handlerId, InputHandlerType handlerType); 61 int32_t AddHandler(InputHandlerType handlerType, std::shared_ptr<IInputEventConsumer> consumer, 62 HandleEventType eventType = HANDLE_EVENT_TYPE_KP, int32_t priority = DEFUALT_INTERCEPTOR_PRIORITY, 63 uint32_t deviceTags = CapabilityToTags(InputDeviceCapability::INPUT_DEV_CAP_MAX)); 64 int32_t RemoveHandler(int32_t handlerId, InputHandlerType IsValidHandlerType); 65 int32_t AddHandler(InputHandlerType handlerType, std::shared_ptr<IInputEventConsumer> consumer, 66 std::vector<int32_t> actions); 67 68 private: 69 struct GestureHandler { 70 TouchGestureType gestureType { TOUCH_GESTURE_TYPE_NONE }; 71 int32_t fingers { 0 }; 72 bool gestureState { false }; 73 }; 74 struct Handler { 75 int32_t handlerId_ { 0 }; 76 InputHandlerType handlerType_ { NONE }; 77 HandleEventType eventType_ { HANDLE_EVENT_TYPE_KP }; 78 int32_t priority_ { DEFUALT_INTERCEPTOR_PRIORITY }; 79 uint32_t deviceTags_ { CapabilityToTags(InputDeviceCapability::INPUT_DEV_CAP_MAX) }; 80 std::shared_ptr<IInputEventConsumer> consumer_ { nullptr }; 81 GestureHandler gestureHandler_; 82 std::vector<int32_t> actionsType_; 83 }; 84 85 private: 86 int32_t GetNextId(); CheckMonitorValid(TouchGestureType type, int32_t fingers)87 virtual bool CheckMonitorValid(TouchGestureType type, int32_t fingers) 88 { 89 return false; 90 } 91 bool IsMatchGesture(const Handler &handler, int32_t action, int32_t count); 92 int32_t AddGestureToLocal(int32_t handlerId, HandleEventType eventType, 93 TouchGestureType gestureType, int32_t fingers, std::shared_ptr<IInputEventConsumer> consumer); 94 int32_t AddLocal(int32_t handlerId, InputHandlerType handlerType, HandleEventType eventType, 95 int32_t priority, uint32_t deviceTags, std::shared_ptr<IInputEventConsumer> monitor); 96 int32_t AddLocal(int32_t handlerId, InputHandlerType handlerType, std::vector<int32_t> actionsType, 97 std::shared_ptr<IInputEventConsumer> monitor); 98 int32_t AddToServer(InputHandlerType handlerType, HandleEventType eventType, int32_t priority, 99 uint32_t deviceTags, std::vector<int32_t> actionsType = std::vector<int32_t>()); 100 bool IsNeedAddToServer(std::vector<int32_t> actionsType); 101 int32_t RemoveLocal(int32_t handlerId, InputHandlerType handlerType, uint32_t &deviceTags); 102 void UpdateAddToServerActions(); 103 int32_t RemoveLocalActions(int32_t handlerId, InputHandlerType handlerType); 104 int32_t RemoveFromServer(InputHandlerType handlerType, HandleEventType eventType, int32_t priority, 105 uint32_t deviceTags, std::vector<int32_t> actionsType = std::vector<int32_t>()); 106 107 std::shared_ptr<IInputEventConsumer> FindHandler(int32_t handlerId); 108 void OnDispatchEventProcessed(int32_t eventId, int64_t actionTime); 109 void OnDispatchEventProcessed(int32_t eventId, int64_t actionTime, bool isNeedConsume); 110 void AddMouseEventId(std::shared_ptr<PointerEvent> pointerEvent); 111 int32_t GetMonitorConsumerInfos(std::shared_ptr<PointerEvent> pointerEvent, 112 std::map<int32_t, std::shared_ptr<IInputEventConsumer>> &consumerInfos); 113 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) 114 bool CheckInputDeviceSource(const std::shared_ptr<PointerEvent> pointerEvent, uint32_t deviceTags) const; 115 void GetConsumerInfos(std::shared_ptr<PointerEvent> pointerEvent, uint32_t deviceTags, 116 std::map<int32_t, std::shared_ptr<IInputEventConsumer>> &consumerInfos); 117 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH 118 119 private: 120 std::list<Handler> interHandlers_; 121 std::map<int32_t, Handler> monitorHandlers_; 122 std::map<int32_t, Handler> actionsMonitorHandlers_; 123 std::set<int32_t> mouseEventIds_; 124 std::function<void(int32_t, int64_t)> monitorCallback_ { nullptr }; 125 std::function<void(int32_t, int64_t)> monitorCallbackConsume_ { nullptr }; 126 int32_t nextId_ { 1 }; 127 std::mutex mtxHandlers_; 128 std::shared_ptr<PointerEvent> lastPointerEvent_ { nullptr }; 129 std::vector<int32_t> addToServerActions_; 130 }; 131 } // namespace MMI 132 } // namespace OHOS 133 #endif // INPUT_HANDLER_MANAGER_H 134