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 
56 protected:
57     int32_t AddHandler(InputHandlerType handlerType, std::shared_ptr<IInputEventConsumer> consumer,
58         HandleEventType eventType = HANDLE_EVENT_TYPE_KP, int32_t priority = DEFUALT_INTERCEPTOR_PRIORITY,
59         uint32_t deviceTags = CapabilityToTags(InputDeviceCapability::INPUT_DEV_CAP_MAX));
60     int32_t RemoveHandler(int32_t handlerId, InputHandlerType IsValidHandlerType);
61 
62 private:
63     struct Handler {
64         int32_t handlerId_ { 0 };
65         InputHandlerType handlerType_ { NONE };
66         HandleEventType eventType_ { HANDLE_EVENT_TYPE_KP };
67         int32_t priority_ { DEFUALT_INTERCEPTOR_PRIORITY };
68         uint32_t deviceTags_ { CapabilityToTags(InputDeviceCapability::INPUT_DEV_CAP_MAX) };
69         std::shared_ptr<IInputEventConsumer> consumer_ { nullptr };
70     };
71 
72 private:
73     int32_t GetNextId();
74     int32_t AddLocal(int32_t handlerId, InputHandlerType handlerType, HandleEventType eventType,
75         int32_t priority, uint32_t deviceTags, std::shared_ptr<IInputEventConsumer> monitor);
76     int32_t AddToServer(InputHandlerType handlerType, HandleEventType eventType, int32_t priority,
77         uint32_t deviceTags);
78     int32_t RemoveLocal(int32_t handlerId, InputHandlerType handlerType, uint32_t &deviceTags);
79     int32_t RemoveFromServer(InputHandlerType handlerType, HandleEventType eventType, int32_t priority,
80         uint32_t deviceTags);
81 
82     std::shared_ptr<IInputEventConsumer> FindHandler(int32_t handlerId);
83     void OnDispatchEventProcessed(int32_t eventId, int64_t actionTime);
84     void OnDispatchEventProcessed(int32_t eventId, int64_t actionTime, bool isNeedConsume);
85     void AddMouseEventId(std::shared_ptr<PointerEvent> pointerEvent);
86     int32_t GetMonitorConsumerInfos(std::shared_ptr<PointerEvent> pointerEvent,
87         std::map<int32_t, std::shared_ptr<IInputEventConsumer>> &consumerInfos);
88     bool CheckIfNeedAddToConsumerInfos(const Handler &monitor, std::shared_ptr<PointerEvent> pointerEvent);
89     bool IsPinchType(std::shared_ptr<PointerEvent> pointerEvent);
90     bool IsRotateType(std::shared_ptr<PointerEvent> pointerEvent);
91     bool IsThreeFingersSwipeType(std::shared_ptr<PointerEvent> pointerEvent);
92     bool IsFourFingersSwipeType(std::shared_ptr<PointerEvent> pointerEvent);
93     bool IsBeginAndEndType(std::shared_ptr<PointerEvent> pointerEvent);
94     bool IsThreeFingersTapType(std::shared_ptr<PointerEvent> pointerEvent);
95 #ifdef OHOS_BUILD_ENABLE_FINGERPRINT
96     bool IsFingerprintType(std::shared_ptr<PointerEvent> pointerEvent);
97 #endif // OHOS_BUILD_ENABLE_FINGERPRINT
98 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
99     bool CheckInputDeviceSource(const std::shared_ptr<PointerEvent> pointerEvent, uint32_t deviceTags) const;
100     void GetConsumerInfos(std::shared_ptr<PointerEvent> pointerEvent, uint32_t deviceTags,
101         std::map<int32_t, std::shared_ptr<IInputEventConsumer>> &consumerInfos);
102 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
103 
104 private:
105     std::list<Handler> interHandlers_;
106     std::map<int32_t, Handler> monitorHandlers_;
107     std::set<int32_t> mouseEventIds_;
108     std::function<void(int32_t, int64_t)> monitorCallback_ { nullptr };
109     std::function<void(int32_t, int64_t)> monitorCallbackConsume_ { nullptr };
110     int32_t nextId_ { 1 };
111     std::mutex mtxHandlers_;
112     std::shared_ptr<PointerEvent> lastPointerEvent_ { nullptr };
113 };
114 } // namespace MMI
115 } // namespace OHOS
116 #endif // INPUT_HANDLER_MANAGER_H
117