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
20 #include "devicestatus_define.h"
21
22 #undef LOG_TAG
23 #define LOG_TAG "InputAdapter"
24
25 namespace OHOS {
26 namespace Msdp {
27 namespace DeviceStatus {
28 namespace {
29 const std::string VIRTUAL_TRACK_PAD_NAME { "VirtualTrackpad" }; // defined in multimodalinput
30 const std::string CHANGED_TYPE { "change" };
31 }
32
AddMonitor(std::function<void (std::shared_ptr<MMI::PointerEvent>)> callback)33 int32_t InputAdapter::AddMonitor(std::function<void(std::shared_ptr<MMI::PointerEvent>)> callback)
34 {
35 int32_t monitorId = MMI::InputManager::GetInstance()->AddMonitor(callback);
36 if (monitorId < 0) {
37 FI_HILOGE("AddMonitor fail");
38 }
39 return monitorId;
40 }
41
AddMonitor(std::function<void (std::shared_ptr<MMI::KeyEvent>)> callback)42 int32_t InputAdapter::AddMonitor(std::function<void(std::shared_ptr<MMI::KeyEvent>)> callback)
43 {
44 int32_t monitorId = MMI::InputManager::GetInstance()->AddMonitor(callback);
45 if (monitorId < 0) {
46 FI_HILOGE("AddMonitor fail");
47 }
48 return monitorId;
49 }
50
AddMonitor(std::function<void (std::shared_ptr<MMI::PointerEvent>)> pointCallback,std::function<void (std::shared_ptr<MMI::KeyEvent>)> keyCallback,MMI::HandleEventType eventType)51 int32_t InputAdapter::AddMonitor(std::function<void(std::shared_ptr<MMI::PointerEvent>)> pointCallback,
52 std::function<void(std::shared_ptr<MMI::KeyEvent>)> keyCallback, MMI::HandleEventType eventType)
53 {
54 auto monitor = std::make_shared<MonitorConsumer>(pointCallback, keyCallback);
55 int32_t monitorId = MMI::InputManager::GetInstance()->AddMonitor(monitor, eventType);
56 if (monitorId < 0) {
57 FI_HILOGE("AddMonitor fail");
58 }
59 return monitorId;
60 }
61
RemoveMonitor(int32_t monitorId)62 void InputAdapter::RemoveMonitor(int32_t monitorId)
63 {
64 MMI::InputManager::GetInstance()->RemoveMonitor(monitorId);
65 }
66
AddPreMonitor(std::function<void (std::shared_ptr<MMI::PointerEvent>)> pointCallback,std::function<void (std::shared_ptr<MMI::KeyEvent>)> keyCallback,MMI::HandleEventType eventType,std::vector<int32_t> keys)67 int32_t InputAdapter::AddPreMonitor(std::function<void(std::shared_ptr<MMI::PointerEvent>)> pointCallback,
68 std::function<void(std::shared_ptr<MMI::KeyEvent>)> keyCallback,
69 MMI::HandleEventType eventType, std::vector<int32_t> keys)
70 {
71 auto monitor = std::make_shared<MonitorConsumer>(pointCallback, keyCallback);
72 int32_t preMonitorId = MMI::InputManager::GetInstance()->AddPreMonitor(monitor, eventType, keys);
73 if (preMonitorId < 0) {
74 FI_HILOGE("AddPreMonitor fail");
75 }
76 return preMonitorId;
77 }
78
RemovePreMonitor(int32_t preMonitorId)79 void InputAdapter::RemovePreMonitor(int32_t preMonitorId)
80 {
81 MMI::InputManager::GetInstance()->RemovePreMonitor(preMonitorId);
82 }
83
AddInterceptor(std::function<void (std::shared_ptr<MMI::PointerEvent>)> pointCallback)84 int32_t InputAdapter::AddInterceptor(std::function<void(std::shared_ptr<MMI::PointerEvent>)> pointCallback)
85 {
86 return AddInterceptor(pointCallback, nullptr);
87 }
88
AddInterceptor(std::function<void (std::shared_ptr<MMI::KeyEvent>)> keyCallback)89 int32_t InputAdapter::AddInterceptor(std::function<void(std::shared_ptr<MMI::KeyEvent>)> keyCallback)
90 {
91 return AddInterceptor(nullptr, keyCallback);
92 }
93
AddInterceptor(std::function<void (std::shared_ptr<MMI::PointerEvent>)> pointCallback,std::function<void (std::shared_ptr<MMI::KeyEvent>)> keyCallback)94 int32_t InputAdapter::AddInterceptor(std::function<void(std::shared_ptr<MMI::PointerEvent>)> pointCallback,
95 std::function<void(std::shared_ptr<MMI::KeyEvent>)> keyCallback)
96 {
97 uint32_t tags { 0u };
98 if (pointCallback != nullptr) {
99 tags |= MMI::CapabilityToTags(MMI::INPUT_DEV_CAP_POINTER);
100 }
101 if (keyCallback != nullptr) {
102 tags |= MMI::CapabilityToTags(MMI::INPUT_DEV_CAP_KEYBOARD);
103 }
104 if (tags == 0u) {
105 FI_HILOGE("Both interceptors are null");
106 return -1;
107 }
108 auto interceptor = std::make_shared<InterceptorConsumer>(pointCallback, keyCallback);
109 constexpr int32_t DEFAULT_PRIORITY { 499 };
110 int32_t interceptorId = MMI::InputManager::GetInstance()->AddInterceptor(interceptor, DEFAULT_PRIORITY, tags);
111 if (interceptorId < 0) {
112 FI_HILOGE("AddInterceptor fail");
113 }
114 return interceptorId;
115 }
116
RemoveInterceptor(int32_t interceptorId)117 void InputAdapter::RemoveInterceptor(int32_t interceptorId)
118 {
119 MMI::InputManager::GetInstance()->RemoveInterceptor(interceptorId);
120 }
121
AddFilter(std::function<bool (std::shared_ptr<MMI::PointerEvent>)> callback)122 int32_t InputAdapter::AddFilter(std::function<bool(std::shared_ptr<MMI::PointerEvent>)> callback)
123 {
124 constexpr int32_t DEFAULT_PRIORITY { 220 };
125 auto filter = std::make_shared<PointerFilter>(callback);
126 uint32_t tags = CapabilityToTags(MMI::INPUT_DEV_CAP_POINTER);
127 int32_t filterId = MMI::InputManager::GetInstance()->AddInputEventFilter(filter, DEFAULT_PRIORITY, tags);
128 if (filterId < 0) {
129 FI_HILOGE("AddInputEventFilter fail");
130 }
131 return filterId;
132 }
133
RemoveFilter(int32_t filterId)134 void InputAdapter::RemoveFilter(int32_t filterId)
135 {
136 MMI::InputManager::GetInstance()->RemoveInputEventFilter(filterId);
137 }
138
SetPointerVisibility(bool visible,int32_t priority)139 int32_t InputAdapter::SetPointerVisibility(bool visible, int32_t priority)
140 {
141 FI_HILOGI("Set pointer visibility, visible:%{public}s", visible ? "true" : "false");
142 return MMI::InputManager::GetInstance()->SetPointerVisible(visible, priority);
143 }
144
SetPointerLocation(int32_t x,int32_t y,int32_t displayId)145 int32_t InputAdapter::SetPointerLocation(int32_t x, int32_t y, int32_t displayId)
146 {
147 return MMI::InputManager::GetInstance()->SetPointerLocation(x, y, displayId);
148 }
149
EnableInputDevice(bool enable)150 int32_t InputAdapter::EnableInputDevice(bool enable)
151 {
152 return MMI::InputManager::GetInstance()->EnableInputDevice(enable);
153 }
154
SimulateInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent)155 void InputAdapter::SimulateInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent)
156 {
157 MMI::InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
158 }
159
SimulateInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent)160 void InputAdapter::SimulateInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent)
161 {
162 MMI::InputManager::GetInstance()->SimulateInputEvent(keyEvent);
163 }
164
AddVirtualInputDevice(std::shared_ptr<MMI::InputDevice> device,int32_t & deviceId)165 int32_t InputAdapter::AddVirtualInputDevice(std::shared_ptr<MMI::InputDevice> device, int32_t &deviceId)
166 {
167 return MMI::InputManager::GetInstance()->AddVirtualInputDevice(device, deviceId);
168 }
169
RemoveVirtualInputDevice(int32_t deviceId)170 int32_t InputAdapter::RemoveVirtualInputDevice(int32_t deviceId)
171 {
172 return MMI::InputManager::GetInstance()->RemoveVirtualInputDevice(deviceId);
173 }
174
GetPointerSpeed(int32_t & speed)175 int32_t InputAdapter::GetPointerSpeed(int32_t &speed)
176 {
177 auto ret = MMI::InputManager::GetInstance()->GetPointerSpeed(speed);
178 FI_HILOGI("Get pointerSpeed:%{public}d", speed);
179 return ret;
180 }
181
SetPointerSpeed(int32_t speed)182 int32_t InputAdapter::SetPointerSpeed(int32_t speed)
183 {
184 if (speed < 0) {
185 FI_HILOGW("Invalid pointerSpeed:%{public}d", speed);
186 return RET_ERR;
187 }
188 FI_HILOGI("Set pointerSpeed:%{public}d", speed);
189 return MMI::InputManager::GetInstance()->SetPointerSpeed(speed);
190 }
191
GetTouchPadSpeed(int32_t & speed)192 int32_t InputAdapter::GetTouchPadSpeed(int32_t &speed)
193 {
194 auto ret = MMI::InputManager::GetInstance()->GetTouchpadPointerSpeed(speed);
195 FI_HILOGI("Get TouchPad Speed:%{public}d", speed);
196 return ret;
197 }
198
SetTouchPadSpeed(int32_t speed)199 int32_t InputAdapter::SetTouchPadSpeed(int32_t speed)
200 {
201 if (speed < 0) {
202 FI_HILOGW("Invalid Touchpad Speed:%{public}d", speed);
203 return RET_ERR;
204 }
205 FI_HILOGI("Set TouchPad Speed:%{public}d", speed);
206 return MMI::InputManager::GetInstance()->SetTouchpadPointerSpeed(speed);
207 }
208
IsLocalPointerDevice(std::shared_ptr<MMI::InputDevice> device)209 bool InputAdapter::IsLocalPointerDevice(std::shared_ptr<MMI::InputDevice> device)
210 {
211 CHKPR(device, false);
212 return device->HasCapability(MMI::InputDeviceCapability::INPUT_DEV_CAP_POINTER);
213 }
214
IsVirtualTrackpad(std::shared_ptr<MMI::InputDevice> device)215 bool InputAdapter::IsVirtualTrackpad(std::shared_ptr<MMI::InputDevice> device)
216 {
217 CHKPR(device, false);
218 return device->GetName() == VIRTUAL_TRACK_PAD_NAME;
219 }
220
HasLocalPointerDevice()221 bool InputAdapter::HasLocalPointerDevice()
222 {
223 // Just used to check virtualTrackpad on Some special device.
224 std::vector<int32_t> deviceIds;
225 if (MMI::InputManager::GetInstance()->GetDeviceIds(
226 [&deviceIds](std::vector<int32_t> &ids) { deviceIds = ids; }) != RET_OK) {
227 FI_HILOGE("GetDeviceIds failed");
228 return false;
229 }
230 return std::any_of(deviceIds.begin(), deviceIds.end(), [this] (int32_t deviceId) {
231 bool isLocalPointerDevice { false };
232 auto ret = MMI::InputManager::GetInstance()->GetDevice(deviceId, [&isLocalPointerDevice, this] (
233 std::shared_ptr<MMI::InputDevice> device) {
234 if (this->IsLocalPointerDevice(device) && this->IsVirtualTrackpad(device)) {
235 isLocalPointerDevice = true;
236 }
237 });
238 if (ret != RET_OK) {
239 FI_HILOGE("GetDevice failed");
240 }
241 return isLocalPointerDevice;
242 });
243 }
244
RegisterDevListener(MMIDevListener devAddedCallback,MMIDevListener devRemovedCallback)245 int32_t InputAdapter::RegisterDevListener(MMIDevListener devAddedCallback, MMIDevListener devRemovedCallback)
246 {
247 CALL_INFO_TRACE;
248 CHKPR(devAddedCallback, RET_ERR);
249 CHKPR(devRemovedCallback, RET_ERR);
250 auto devListener = std::make_shared<DevListener>(devAddedCallback, devRemovedCallback);
251 int32_t res = MMI::InputManager::GetInstance()->RegisterDevListener(CHANGED_TYPE, devListener);
252 if (res != 0) {
253 FI_HILOGE("Register device listener failed, res:%{public}d", res);
254 }
255 return res;
256 }
257
UnregisterDevListener()258 int32_t InputAdapter::UnregisterDevListener()
259 {
260 CALL_INFO_TRACE;
261 int32_t res = MMI::InputManager::GetInstance()->UnregisterDevListener(CHANGED_TYPE);
262 if (res != 0) {
263 FI_HILOGE("Unregister device listener failed, res:%{public}d", res);
264 }
265 return res;
266 }
267
268 } // namespace DeviceStatus
269 } // namespace Msdp
270 } // namespace OHOS