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 #include "input_adapter.h"
17 
18 #include "input_manager.h"
19 #include "i_input_event_consumer.h"
20 #include "i_input_event_filter.h"
21 
22 #include "devicestatus_define.h"
23 
24 #undef LOG_TAG
25 #define LOG_TAG "InputAdapter"
26 
27 namespace OHOS {
28 namespace Msdp {
29 namespace DeviceStatus {
30 
31 class PointerFilter : public MMI::IInputEventFilter {
32 public:
PointerFilter(std::function<bool (std::shared_ptr<MMI::PointerEvent>)> filter)33     explicit PointerFilter(std::function<bool(std::shared_ptr<MMI::PointerEvent>)> filter)
34         : filter_(filter) {}
35 
OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const36     bool OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const override
37     {
38         return (filter_ != nullptr ? filter_(pointerEvent) : false);
39     }
40 
OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const41     bool OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const override
42     {
43         return false;
44     }
45 
46 private:
47     std::function<bool(std::shared_ptr<MMI::PointerEvent>)> filter_;
48 };
49 
50 class InterceptorConsumer : public MMI::IInputEventConsumer {
51 public:
InterceptorConsumer(std::function<void (std::shared_ptr<MMI::PointerEvent>)> pointerCb,std::function<void (std::shared_ptr<MMI::KeyEvent>)> keyCb)52     InterceptorConsumer(std::function<void(std::shared_ptr<MMI::PointerEvent>)> pointerCb,
53                         std::function<void(std::shared_ptr<MMI::KeyEvent>)> keyCb)
54         : pointerCb_(pointerCb), keyCb_(keyCb) {}
55 
OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const56     void OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const override
57     {
58         if (keyCb_ != nullptr) {
59             keyCb_(keyEvent);
60         }
61     }
62 
OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const63     void OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const override
64     {
65         if (pointerCb_ != nullptr) {
66             pointerCb_(pointerEvent);
67         }
68     }
69 
OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const70     void OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const override {}
71 
72 private:
73     std::function<void(std::shared_ptr<MMI::PointerEvent>)> pointerCb_;
74     std::function<void(std::shared_ptr<MMI::KeyEvent>)> keyCb_;
75 };
76 
AddMonitor(std::function<void (std::shared_ptr<MMI::PointerEvent>)> callback)77 int32_t InputAdapter::AddMonitor(std::function<void(std::shared_ptr<MMI::PointerEvent>)> callback)
78 {
79     int32_t monitorId = MMI::InputManager::GetInstance()->AddMonitor(callback);
80     if (monitorId < 0) {
81         FI_HILOGE("AddMonitor fail");
82     }
83     return monitorId;
84 }
85 
AddMonitor(std::function<void (std::shared_ptr<MMI::KeyEvent>)> callback)86 int32_t InputAdapter::AddMonitor(std::function<void(std::shared_ptr<MMI::KeyEvent>)> callback)
87 {
88     int32_t monitorId = MMI::InputManager::GetInstance()->AddMonitor(callback);
89     if (monitorId < 0) {
90         FI_HILOGE("AddMonitor fail");
91     }
92     return monitorId;
93 }
94 
RemoveMonitor(int32_t monitorId)95 void InputAdapter::RemoveMonitor(int32_t monitorId)
96 {
97     MMI::InputManager::GetInstance()->RemoveMonitor(monitorId);
98 }
99 
AddInterceptor(std::function<void (std::shared_ptr<MMI::PointerEvent>)> pointerCb)100 int32_t InputAdapter::AddInterceptor(std::function<void(std::shared_ptr<MMI::PointerEvent>)> pointerCb)
101 {
102     return AddInterceptor(pointerCb, nullptr);
103 }
104 
AddInterceptor(std::function<void (std::shared_ptr<MMI::KeyEvent>)> keyCb)105 int32_t InputAdapter::AddInterceptor(std::function<void(std::shared_ptr<MMI::KeyEvent>)> keyCb)
106 {
107     return AddInterceptor(nullptr, keyCb);
108 }
109 
AddInterceptor(std::function<void (std::shared_ptr<MMI::PointerEvent>)> pointerCb,std::function<void (std::shared_ptr<MMI::KeyEvent>)> keyCb)110 int32_t InputAdapter::AddInterceptor(std::function<void(std::shared_ptr<MMI::PointerEvent>)> pointerCb,
111                                      std::function<void(std::shared_ptr<MMI::KeyEvent>)> keyCb)
112 {
113     uint32_t tags { 0u };
114     if (pointerCb != nullptr) {
115         tags |= MMI::CapabilityToTags(MMI::INPUT_DEV_CAP_POINTER);
116     }
117     if (keyCb != nullptr) {
118         tags |= MMI::CapabilityToTags(MMI::INPUT_DEV_CAP_KEYBOARD);
119     }
120     if (tags == 0u) {
121         FI_HILOGE("Both interceptors are null");
122         return -1;
123     }
124     auto interceptor = std::make_shared<InterceptorConsumer>(pointerCb, keyCb);
125     constexpr int32_t DEFAULT_PRIORITY { 499 };
126     int32_t interceptorId = MMI::InputManager::GetInstance()->AddInterceptor(interceptor, DEFAULT_PRIORITY, tags);
127     if (interceptorId < 0) {
128         FI_HILOGE("AddInterceptor fail");
129     }
130     return interceptorId;
131 }
132 
RemoveInterceptor(int32_t interceptorId)133 void InputAdapter::RemoveInterceptor(int32_t interceptorId)
134 {
135     MMI::InputManager::GetInstance()->RemoveInterceptor(interceptorId);
136 }
137 
AddFilter(std::function<bool (std::shared_ptr<MMI::PointerEvent>)> callback)138 int32_t InputAdapter::AddFilter(std::function<bool(std::shared_ptr<MMI::PointerEvent>)> callback)
139 {
140     constexpr int32_t DEFAULT_PRIORITY { 220 };
141     auto filter = std::make_shared<PointerFilter>(callback);
142     uint32_t tags = CapabilityToTags(MMI::INPUT_DEV_CAP_POINTER);
143     int32_t filterId = MMI::InputManager::GetInstance()->AddInputEventFilter(filter, DEFAULT_PRIORITY, tags);
144     if (filterId < 0) {
145         FI_HILOGE("AddInputEventFilter fail");
146     }
147     return filterId;
148 }
149 
RemoveFilter(int32_t filterId)150 void InputAdapter::RemoveFilter(int32_t filterId)
151 {
152     MMI::InputManager::GetInstance()->RemoveInputEventFilter(filterId);
153 }
154 
SetPointerVisibility(bool visible,int32_t priority)155 int32_t InputAdapter::SetPointerVisibility(bool visible, int32_t priority)
156 {
157     FI_HILOGI("Set pointer visibility, visible:%{public}s", visible ? "true" : "false");
158     return MMI::InputManager::GetInstance()->SetPointerVisible(visible, priority);
159 }
160 
SetPointerLocation(int32_t x,int32_t y)161 int32_t InputAdapter::SetPointerLocation(int32_t x, int32_t y)
162 {
163     return MMI::InputManager::GetInstance()->SetPointerLocation(x, y);
164 }
165 
EnableInputDevice(bool enable)166 int32_t InputAdapter::EnableInputDevice(bool enable)
167 {
168     return MMI::InputManager::GetInstance()->EnableInputDevice(enable);
169 }
170 
SimulateInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent)171 void InputAdapter::SimulateInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent)
172 {
173     MMI::InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
174 }
175 
SimulateInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent)176 void InputAdapter::SimulateInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent)
177 {
178     MMI::InputManager::GetInstance()->SimulateInputEvent(keyEvent);
179 }
180 
AddVirtualInputDevice(std::shared_ptr<MMI::InputDevice> device,int32_t & deviceId)181 int32_t InputAdapter::AddVirtualInputDevice(std::shared_ptr<MMI::InputDevice> device, int32_t &deviceId)
182 {
183     return MMI::InputManager::GetInstance()->AddVirtualInputDevice(device, deviceId);
184 }
185 
RemoveVirtualInputDevice(int32_t deviceId)186 int32_t InputAdapter::RemoveVirtualInputDevice(int32_t deviceId)
187 {
188     return MMI::InputManager::GetInstance()->RemoveVirtualInputDevice(deviceId);
189 }
190 } // namespace DeviceStatus
191 } // namespace Msdp
192 } // namespace OHOS