1 /*
2  * Copyright (C) 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 ACCESSIBILITY_INPUT_INTERCEPTOR_H
17 #define ACCESSIBILITY_INPUT_INTERCEPTOR_H
18 
19 #include <map>
20 #include <memory>
21 #include <vector>
22 
23 #include "accessibility_event_transmission.h"
24 #include "accessibility_mouse_key.h"
25 #include "event_handler.h"
26 #include "ffrt.h"
27 #include "i_input_event_consumer.h"
28 #include "input_manager.h"
29 #include "key_event.h"
30 #include "pointer_event.h"
31 
32 namespace OHOS {
33 namespace Accessibility {
34 class AccessibleAbilityManagerService;
35 
36 class AccessibilityInputEventConsumer : public MMI::IInputEventConsumer {
37 public:
38     AccessibilityInputEventConsumer();
39     ~AccessibilityInputEventConsumer();
40     void OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const override;
41     void OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const override;
OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent)42     void OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const override {};
43 private:
44     std::shared_ptr<AppExecFwk::EventHandler> eventHandler_ = nullptr;
45 };
46 
47 class AccessibilityInputInterceptor : public EventTransmission {
48 public:
49     // Feature flag for screen magnification.
50     static constexpr uint32_t FEATURE_SCREEN_MAGNIFICATION = 0x00000001;
51 
52     // Feature flag for touch exploration.
53     static constexpr uint32_t FEATURE_TOUCH_EXPLORATION = 0x00000002;
54 
55     // Feature flag for filtering key events.
56     static constexpr uint32_t FEATURE_FILTER_KEY_EVENTS = 0x00000004;
57 
58     // Feature flag for inject touch events.
59     static constexpr uint32_t FEATURE_INJECT_TOUCH_EVENTS = 0x00000008;
60 
61     // Feature flag for mouse autoclick.
62     static constexpr uint32_t FEATURE_MOUSE_AUTOCLICK = 0x00000010;
63 
64     // Feature flag for mouse key.
65     static constexpr uint32_t FEATURE_MOUSE_KEY = 0x00000040;
66 
67     // Feature flag for screen touch.
68     static constexpr uint32_t FEATURE_SCREEN_TOUCH = 0x00000080;
69 
70     static constexpr uint32_t PRIORITY_EVENT = 500;
71 
72     static sptr<AccessibilityInputInterceptor> GetInstance();
73     ~AccessibilityInputInterceptor();
74     void ProcessKeyEvent(std::shared_ptr<MMI::KeyEvent> event);
75     void ProcessPointerEvent(std::shared_ptr<MMI::PointerEvent> event);
76     bool OnKeyEvent(MMI::KeyEvent &event) override;
77     bool OnPointerEvent(MMI::PointerEvent &event) override;
78     void OnMoveMouse(int32_t offsetX, int32_t offsetY) override;
79     void SetAvailableFunctions(uint32_t availableFunctions);
80 
81 private:
82     AccessibilityInputInterceptor();
83     static sptr<AccessibilityInputInterceptor> instance_;
84     void CreateTransmitters();
85     void DestroyTransmitters();
86     void CreatePointerEventTransmitters();
87     void CreateKeyEventTransmitters();
88     void SetNextEventTransmitter(sptr<EventTransmission> &header, sptr<EventTransmission> &current,
89         const sptr<EventTransmission> &next);
90     void UpdateInterceptor();
91     void DestroyInterceptor();
92 
93     sptr<EventTransmission> pointerEventTransmitters_ = nullptr;
94     sptr<EventTransmission> keyEventTransmitters_ = nullptr;
95     sptr<EventTransmission> mouseKey_ = nullptr;
96     uint32_t availableFunctions_ = 0;
97     int32_t interceptorId_ = -1;
98     MMI::InputManager *inputManager_ = nullptr;
99     std::shared_ptr<AccessibilityInputEventConsumer> inputEventConsumer_ = nullptr;
100     std::shared_ptr<AppExecFwk::EventHandler> eventHandler_ = nullptr;
101     ffrt::mutex mutex_;
102 };
103 } // namespace Accessibility
104 } // namespace OHOS
105 #endif // ACCESSIBILITY_INPUT_INTERCEPTOR_H
106