• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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_PRE_MONITOR_HANDLER_H
17 #define EVENT_PRE_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 
31 namespace OHOS {
32 namespace MMI {
33 class EventPreMonitorHandler final : public IInputEventHandler {
34 public:
35     EventPreMonitorHandler() = default;
36     DISALLOW_COPY_AND_MOVE(EventPreMonitorHandler);
37     ~EventPreMonitorHandler() override = default;
38 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
39     void HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent) override;
40 #endif // OHOS_BUILD_ENABLE_KEYBOARD
41 #ifdef OHOS_BUILD_ENABLE_POINTER
42     void HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent) override;
43 #endif // OHOS_BUILD_ENABLE_POINTER
44 #ifdef OHOS_BUILD_ENABLE_TOUCH
45     void HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent) override;
46 #endif // OHOS_BUILD_ENABLE_TOUCH
47 
48     int32_t AddInputHandler(SessionPtr sess, int32_t handlerId, HandleEventType eventType, std::vector<int32_t> keys);
49     void RemoveInputHandler(SessionPtr sess, int32_t handlerId);
50 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
51     bool OnHandleEvent(std::shared_ptr<KeyEvent> KeyEvent);
52 #endif // OHOS_BUILD_ENABLE_KEYBOARD
53 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
54     bool OnHandleEvent(std::shared_ptr<PointerEvent> PointerEvent);
55 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
56     void Dump(int32_t fd, const std::vector<std::string> &args);
57 
58 private:
59     void InitSessionLostCallback();
60     void OnSessionLost(SessionPtr session);
61 
62 private:
63     class SessionHandler {
64     public:
SessionHandler(SessionPtr session,int32_t handlerId,HandleEventType eventType,std::vector<int32_t> keys)65         SessionHandler(SessionPtr session, int32_t handlerId, HandleEventType eventType, std::vector<int32_t> keys)
66             : session_(session), handlerId_(handlerId), eventType_(eventType), keys_(keys)
67         {}
68 
SessionHandler(const SessionHandler & other)69         SessionHandler(const SessionHandler& other)
70         {
71             session_ = other.session_;
72             handlerId_ = other.handlerId_;
73             eventType_ = other.eventType_;
74             keys_ = other.keys_;
75         }
76 
77         void SendToClient(std::shared_ptr<KeyEvent> keyEvent, NetPacket &pkt, int32_t handlerId) const;
78         bool operator<(const SessionHandler& other) const
79         {
80             return (session_ < other.session_);
81         }
82 
83         SessionPtr session_ { nullptr };
84         int32_t handlerId_;
85         HandleEventType eventType_;
86         std::shared_ptr<IInputEventConsumer> callback_ { nullptr };
87         std::vector<int32_t> keys_;
88     };
89 
90     class MonitorCollection : public IInputEventCollectionHandler, protected NoCopyable {
91     public:
92 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
93         virtual bool HandleEvent(std::shared_ptr<KeyEvent> KeyEvent) override;
94 #endif // OHOS_BUILD_ENABLE_KEYBOARD
95 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
96         virtual bool HandleEvent(std::shared_ptr<PointerEvent> pointerEvent) override;
97 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
98         int32_t AddMonitor(const std::shared_ptr<SessionHandler> monitor, std::vector<int32_t> keys);
99         void RemoveMonitor(SessionPtr sess, int32_t handlerId);
100         bool IsEqualsKeys(std::vector<int32_t> newKeys, std::vector<int32_t> oldKeys);
101 
102         void OnSessionLost(SessionPtr session);
103         void Dump(int32_t fd, const std::vector<std::string> &args);
104 
105     private:
106         std::map<std::vector<int32_t>, std::list<std::shared_ptr<SessionHandler>>> sessionHandlers_;
107     };
108 
109 private:
110     bool sessionLostCallbackInitialized_ { false };
111     MonitorCollection monitors_;
112 };
113 } // namespace MMI
114 } // namespace OHOS
115 #endif // EVENT_PRE_MONITOR_HANDLER_H