1 /*
2  * Copyright (c) 2023 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 INPUT_ADAPTER_H
17 #define INPUT_ADAPTER_H
18 
19 #include "nocopyable.h"
20 
21 #include "i_input_adapter.h"
22 
23 #include "i_input_event_consumer.h"
24 #include "i_input_event_filter.h"
25 namespace OHOS {
26 namespace Msdp {
27 namespace DeviceStatus {
28 class InputAdapter final : public IInputAdapter {
29 public:
30     InputAdapter() = default;
31     ~InputAdapter() = default;
32     DISALLOW_COPY_AND_MOVE(InputAdapter);
33 
34     int32_t AddMonitor(std::function<void(std::shared_ptr<MMI::PointerEvent>)> callback) override;
35     int32_t AddMonitor(std::function<void(std::shared_ptr<MMI::KeyEvent>)> callback) override;
36     void RemoveMonitor(int32_t monitorId) override;
37 
38     int32_t AddInterceptor(std::function<void(std::shared_ptr<MMI::PointerEvent>)> pointerCb) override;
39     int32_t AddInterceptor(std::function<void(std::shared_ptr<MMI::KeyEvent>)> keyCb) override;
40     int32_t AddInterceptor(std::function<void(std::shared_ptr<MMI::PointerEvent>)> pointerCb,
41                            std::function<void(std::shared_ptr<MMI::KeyEvent>)> keyCb) override;
42     void RemoveInterceptor(int32_t interceptorId) override;
43 
44     int32_t AddFilter(std::function<bool(std::shared_ptr<MMI::PointerEvent>)> callback) override;
45     void RemoveFilter(int32_t filterId) override;
46 
47     int32_t SetPointerVisibility(bool visible, int32_t priority = 0) override;
48     int32_t SetPointerLocation(int32_t x, int32_t y) override;
49     int32_t EnableInputDevice(bool enable) override;
50 
51     void SimulateInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) override;
52     void SimulateInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) override;
53     int32_t AddVirtualInputDevice(std::shared_ptr<MMI::InputDevice> device, int32_t &deviceId) override;
54     int32_t RemoveVirtualInputDevice(int32_t deviceId) override;
55 };
56 
57 class PointerFilter : public MMI::IInputEventFilter {
58 public:
PointerFilter(std::function<bool (std::shared_ptr<MMI::PointerEvent>)> filter)59     explicit PointerFilter(std::function<bool(std::shared_ptr<MMI::PointerEvent>)> filter)
60         : filter_(filter) {}
61 
OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent)62     bool OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const override
63     {
64         return (filter_ != nullptr ? filter_(pointerEvent) : false);
65     }
66 
OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent)67     bool OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const override
68     {
69         return false;
70     }
71 
72 private:
73     std::function<bool(std::shared_ptr<MMI::PointerEvent>)> filter_;
74 };
75 
76 class InterceptorConsumer : public MMI::IInputEventConsumer {
77 public:
InterceptorConsumer(std::function<void (std::shared_ptr<MMI::PointerEvent>)> pointerCb,std::function<void (std::shared_ptr<MMI::KeyEvent>)> keyCb)78     InterceptorConsumer(std::function<void(std::shared_ptr<MMI::PointerEvent>)> pointerCb,
79                         std::function<void(std::shared_ptr<MMI::KeyEvent>)> keyCb)
80         : pointerCb_(pointerCb), keyCb_(keyCb) {}
81 
OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent)82     void OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const override
83     {
84         if (keyCb_ != nullptr) {
85             keyCb_(keyEvent);
86         }
87     }
88 
OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent)89     void OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const override
90     {
91         if (pointerCb_ != nullptr) {
92             pointerCb_(pointerEvent);
93         }
94     }
95 
OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent)96     void OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const override {}
97 
98 private:
99     std::function<void(std::shared_ptr<MMI::PointerEvent>)> pointerCb_;
100     std::function<void(std::shared_ptr<MMI::KeyEvent>)> keyCb_;
101 };
102 } // namespace DeviceStatus
103 } // namespace Msdp
104 } // namespace OHOS
105 #endif // INPUT_ADAPTER_H