• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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_INTERCEPTOR_HANDLER_H
17 #define EVENT_INTERCEPTOR_HANDLER_H
18 
19 #include "i_input_event_handler.h"
20 #include "i_input_event_collection_handler.h"
21 #include "input_device.h"
22 #include "uds_session.h"
23 
24 namespace OHOS {
25 namespace MMI {
26 class EventInterceptorHandler : public IInputEventHandler {
27 public:
28     EventInterceptorHandler() = default;
29     DISALLOW_COPY_AND_MOVE(EventInterceptorHandler);
30     ~EventInterceptorHandler() = default;
31 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
32     void HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent) override;
33 #endif // OHOS_BUILD_ENABLE_KEYBOARD
34 #ifdef OHOS_BUILD_ENABLE_POINTER
35     void HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent) override;
36 #endif // OHOS_BUILD_ENABLE_POINTER
37 #ifdef OHOS_BUILD_ENABLE_TOUCH
38     void HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent) override;
39 #endif // OHOS_BUILD_ENABLE_TOUCH
40     int32_t AddInputHandler(InputHandlerType handlerType, HandleEventType eventType,
41         int32_t priority, uint32_t deviceTags, SessionPtr session);
42     void RemoveInputHandler(InputHandlerType handlerType, HandleEventType eventType,
43         int32_t priority, uint32_t deviceTags, SessionPtr session);
44 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
45     bool OnHandleEvent(std::shared_ptr<KeyEvent> keyEvent);
46 #endif // OHOS_BUILD_ENABLE_KEYBOARD
47 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
48     bool OnHandleEvent(std::shared_ptr<PointerEvent> pointerEvent);
49 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
50     void Dump(int32_t fd, const std::vector<std::string> &args);
51     static bool CheckInputDeviceSource(
52         const std::shared_ptr<PointerEvent> pointerEvent, uint32_t deviceTags);
53 
54 private:
55     void InitSessionLostCallback();
56     void OnSessionLost(SessionPtr session);
57 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
58     bool TouchPadKnuckleDoubleClickHandle(std::shared_ptr<KeyEvent> event);
59 #endif // OHOS_BUILD_ENABLE_KEYBOARD
60 private:
61     class SessionHandler {
62     public:
SessionHandler(InputHandlerType handlerType,HandleEventType eventType,int32_t priority,uint32_t deviceTags,SessionPtr session)63         SessionHandler(InputHandlerType handlerType, HandleEventType eventType, int32_t priority,
64             uint32_t deviceTags, SessionPtr session) : handlerType_(handlerType),
65             eventType_(eventType & HANDLE_EVENT_TYPE_ALL), priority_(priority), deviceTags_(deviceTags),
66             session_(session) {}
67         void SendToClient(std::shared_ptr<KeyEvent> keyEvent) const;
68         void SendToClient(std::shared_ptr<PointerEvent> pointerEvent) const;
69         InputHandlerType handlerType_ { NONE };
70         HandleEventType eventType_ { HANDLE_EVENT_TYPE_ALL };
71         int32_t priority_ { DEFUALT_INTERCEPTOR_PRIORITY };
72         uint32_t deviceTags_ { CapabilityToTags(InputDeviceCapability::INPUT_DEV_CAP_MAX) };
73         SessionPtr session_ { nullptr };
74     };
75 
76     class InterceptorCollection final : public IInputEventCollectionHandler, protected NoCopyable {
77     public:
78 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
79         bool HandleEvent(std::shared_ptr<KeyEvent> keyEvent) override;
80 #endif // OHOS_BUILD_ENABLE_KEYBOARD
81 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
82         bool HandleEvent(std::shared_ptr<PointerEvent> pointerEvent) override;
83 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
84         int32_t AddInterceptor(const SessionHandler& interceptor);
85         void RemoveInterceptor(const SessionHandler& interceptor);
86         void OnSessionLost(SessionPtr session);
87         void Dump(int32_t fd, const std::vector<std::string> &args);
88         std::list<SessionHandler> interceptors_;
89     };
90 
91 private:
92     bool sessionLostCallbackInitialized_ { false };
93     InterceptorCollection interceptors_;
94     static std::unique_ptr<std::string> keyevent_intercept_whitelist;
95 };
96 } // namespace MMI
97 } // namespace OHOS
98 #endif // EVENT_INTERCEPTOR_HANDLER_H