• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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 
32 namespace OHOS {
33 namespace MMI {
34 class EventMonitorHandler final : public IInputEventHandler {
35 public:
36     EventMonitorHandler() = default;
37     DISALLOW_COPY_AND_MOVE(EventMonitorHandler);
38     ~EventMonitorHandler() override = default;
39 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
40     void HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent) override;
41 #endif // OHOS_BUILD_ENABLE_KEYBOARD
42 #ifdef OHOS_BUILD_ENABLE_POINTER
43     void HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent) override;
44 #endif // OHOS_BUILD_ENABLE_POINTER
45 #ifdef OHOS_BUILD_ENABLE_TOUCH
46     void HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent) override;
47 #endif // OHOS_BUILD_ENABLE_TOUCH
48     int32_t AddInputHandler(InputHandlerType handlerType, HandleEventType eventType, SessionPtr session);
49     void RemoveInputHandler(InputHandlerType handlerType, HandleEventType eventType, SessionPtr session);
50     void MarkConsumed(int32_t eventId, SessionPtr session);
51 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
52     bool OnHandleEvent(std::shared_ptr<KeyEvent> KeyEvent);
53 #endif // OHOS_BUILD_ENABLE_KEYBOARD
54 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
55     bool OnHandleEvent(std::shared_ptr<PointerEvent> PointerEvent);
56 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
57     void Dump(int32_t fd, const std::vector<std::string> &args);
58 
59 private:
60     void InitSessionLostCallback();
61     void OnSessionLost(SessionPtr session);
62 
63 private:
64     class SessionHandler {
65     public:
SessionHandler(InputHandlerType handlerType,HandleEventType eventType,SessionPtr session)66         SessionHandler(InputHandlerType handlerType, HandleEventType eventType, SessionPtr session)
67             : handlerType_(handlerType), eventType_(eventType & HANDLE_EVENT_TYPE_ALL),
68               session_(session) {}
69         void SendToClient(std::shared_ptr<KeyEvent> keyEvent) const;
70         void SendToClient(std::shared_ptr<PointerEvent> pointerEvent) const;
71         bool operator<(const SessionHandler& other) const
72         {
73             return (session_ < other.session_);
74         }
75         InputHandlerType handlerType_;
76         HandleEventType eventType_;
77         SessionPtr session_ { nullptr };
78     };
79 
80     class MonitorCollection : public IInputEventCollectionHandler, protected NoCopyable {
81     public:
82 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
83         virtual bool HandleEvent(std::shared_ptr<KeyEvent> KeyEvent) override;
84 #endif // OHOS_BUILD_ENABLE_KEYBOARD
85 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
86         virtual bool HandleEvent(std::shared_ptr<PointerEvent> pointerEvent) override;
87 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
88         int32_t AddMonitor(const SessionHandler& mon);
89         void RemoveMonitor(const SessionHandler& mon);
90         void MarkConsumed(int32_t eventId, SessionPtr session);
91 
92         bool HasMonitor(SessionPtr session);
93 #ifdef OHOS_BUILD_ENABLE_TOUCH
94         void UpdateConsumptionState(std::shared_ptr<PointerEvent> pointerEvent);
95 #endif // OHOS_BUILD_ENABLE_TOUCH
96 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
97         void Monitor(std::shared_ptr<PointerEvent> pointerEvent);
98 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
99         void OnSessionLost(SessionPtr session);
100         void Dump(int32_t fd, const std::vector<std::string> &args);
101 
102     struct ConsumptionState {
103         std::set<int32_t> eventIds_;
104         bool isMonitorConsumed_ { false };
105         std::shared_ptr<PointerEvent> lastPointerEvent_ { nullptr };
106     };
107 
108     private:
109         std::set<SessionHandler> monitors_;
110         std::unordered_map<int32_t, ConsumptionState> states_;
111     };
112 
113 private:
114     bool sessionLostCallbackInitialized_ { false };
115     MonitorCollection monitors_;
116 };
117 } // namespace MMI
118 } // namespace OHOS
119 #endif // EVENT_MONITOR_HANDLER_H