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