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