1 /*
2  * Copyright (c) 2021-2024 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_MONITOR_HANDLER_H
17 #define EVENT_MONITOR_HANDLER_H
18 
19 #include <mutex>
20 #include <set>
21 #include <unordered_map>
22 #include <unordered_set>
23 
24 #include "nocopyable.h"
25 
26 #include "i_input_event_collection_handler.h"
27 #include "i_input_event_handler.h"
28 #include "input_handler_type.h"
29 #include "uds_session.h"
30 #include "nap_process.h"
31 #ifdef PLAYER_FRAMEWORK_EXISTS
32 #include "input_screen_capture_monitor_listener.h"
33 #endif
34 
35 namespace OHOS {
36 namespace MMI {
37 class EventMonitorHandler final : public IInputEventHandler {
38 public:
39     EventMonitorHandler() = default;
40     DISALLOW_COPY_AND_MOVE(EventMonitorHandler);
41     ~EventMonitorHandler() override = default;
42 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
43     void HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent) override;
44 #endif // OHOS_BUILD_ENABLE_KEYBOARD
45 #ifdef OHOS_BUILD_ENABLE_POINTER
46     void HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent) override;
47 #endif // OHOS_BUILD_ENABLE_POINTER
48 #ifdef OHOS_BUILD_ENABLE_TOUCH
49     void HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent) override;
50 #endif // OHOS_BUILD_ENABLE_TOUCH
51     int32_t AddInputHandler(InputHandlerType handlerType,
52         HandleEventType eventType, std::shared_ptr<IInputEventConsumer> callback);
53     void RemoveInputHandler(InputHandlerType handlerType,
54         HandleEventType eventType, std::shared_ptr<IInputEventConsumer> callback);
55     int32_t AddInputHandler(InputHandlerType handlerType, HandleEventType eventType, SessionPtr session);
56     void RemoveInputHandler(InputHandlerType handlerType, HandleEventType eventType, SessionPtr session);
57     void MarkConsumed(int32_t eventId, SessionPtr session);
58 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
59     bool OnHandleEvent(std::shared_ptr<KeyEvent> KeyEvent);
60 #endif // OHOS_BUILD_ENABLE_KEYBOARD
61 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
62     bool OnHandleEvent(std::shared_ptr<PointerEvent> PointerEvent);
63 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
64     void Dump(int32_t fd, const std::vector<std::string> &args);
65 #ifdef PLAYER_FRAMEWORK_EXISTS
66     void RegisterScreenCaptureListener();
67     void OnScreenCaptureStarted(SessionPtr session);
68     void OnScreenCaptureFinished(SessionPtr session);
69 #endif
70 
71 private:
72     void InitSessionLostCallback();
73     void OnSessionLost(SessionPtr session);
74 
75 private:
76     class SessionHandler {
77     public:
78         SessionHandler(InputHandlerType handlerType, HandleEventType eventType,
79             SessionPtr session, std::shared_ptr<IInputEventConsumer> cb = nullptr)
80             : handlerType_(handlerType), eventType_(eventType & HANDLE_EVENT_TYPE_ALL),
81               session_(session), callback(cb) {}
SessionHandler(const SessionHandler & other)82         SessionHandler(const SessionHandler& other)
83         {
84             handlerType_ = other.handlerType_;
85             eventType_ = other.eventType_;
86             session_ = other.session_;
87             callback = other.callback;
88         }
89         void SendToClient(std::shared_ptr<KeyEvent> keyEvent, NetPacket &pkt) const;
90         void SendToClient(std::shared_ptr<PointerEvent> pointerEvent, NetPacket &pkt) const;
91         bool operator<(const SessionHandler& other) const
92         {
93             return (session_ < other.session_);
94         }
95         InputHandlerType handlerType_;
96         HandleEventType eventType_;
97         SessionPtr session_ { nullptr };
98         std::shared_ptr<IInputEventConsumer> callback {nullptr};
99     };
100 
101     class MonitorCollection : public IInputEventCollectionHandler, protected NoCopyable {
102     public:
103 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
104         virtual bool HandleEvent(std::shared_ptr<KeyEvent> KeyEvent) override;
105 #endif // OHOS_BUILD_ENABLE_KEYBOARD
106 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
107         virtual bool HandleEvent(std::shared_ptr<PointerEvent> pointerEvent) override;
108 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
109         int32_t AddMonitor(const SessionHandler& mon);
110         void RemoveMonitor(const SessionHandler& mon);
111         void MarkConsumed(int32_t eventId, SessionPtr session);
112 
113         bool HasMonitor(SessionPtr session);
114         bool HasScreenCaptureMonitor(SessionPtr session);
115         void RemoveScreenCaptureMonitor(SessionPtr session);
116         void RecoveryScreenCaptureMonitor(SessionPtr session);
117 #ifdef OHOS_BUILD_ENABLE_TOUCH
118         void UpdateConsumptionState(std::shared_ptr<PointerEvent> pointerEvent);
119 #endif // OHOS_BUILD_ENABLE_TOUCH
120 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
121         void Monitor(std::shared_ptr<PointerEvent> pointerEvent);
122 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
123         void OnSessionLost(SessionPtr session);
124         void Dump(int32_t fd, const std::vector<std::string> &args);
125         bool CheckIfNeedSendToClient(SessionHandler monitor, std::shared_ptr<PointerEvent> pointerEvent);
126         bool IsPinch(std::shared_ptr<PointerEvent> pointerEvent);
127         bool IsRotate(std::shared_ptr<PointerEvent> pointerEvent);
128         bool IsThreeFingersSwipe(std::shared_ptr<PointerEvent> pointerEvent);
129         bool IsFourFingersSwipe(std::shared_ptr<PointerEvent> pointerEvent);
130         bool IsBeginAndEnd(std::shared_ptr<PointerEvent> pointerEvent);
131         bool IsThreeFingersTap(std::shared_ptr<PointerEvent> pointerEvent);
132 #ifdef OHOS_BUILD_ENABLE_FINGERPRINT
133         bool IsFingerprint(std::shared_ptr<PointerEvent> pointerEvent);
134 #endif // OHOS_BUILD_ENABLE_FINGERPRINT
135 
136         struct ConsumptionState {
137             std::set<int32_t> eventIds_;
138             bool isMonitorConsumed_ { false };
139             std::shared_ptr<PointerEvent> lastPointerEvent_ { nullptr };
140         };
141 
142     private:
143         std::set<SessionHandler> monitors_;
144         std::map<int32_t, std::set<SessionHandler>> endScreenCaptureMonitors_;
145         std::unordered_map<int32_t, ConsumptionState> states_;
146     };
147 
148 private:
149     bool sessionLostCallbackInitialized_ { false };
150     MonitorCollection monitors_;
151 #ifdef PLAYER_FRAMEWORK_EXISTS
152     sptr<InputScreenCaptureMonitorListener> screenCaptureMonitorListener_ { nullptr };
153 #endif
154 };
155 } // namespace MMI
156 } // namespace OHOS
157 #endif // EVENT_MONITOR_HANDLER_H