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
AddMonitor(std::function<void (std::shared_ptr<MMI::PointerEvent>)> callback)29 int32_t InputAdapter::AddMonitor(std::function<void(std::shared_ptr<MMI::PointerEvent>)> callback)
30 {
31 int32_t monitorId = MMI::InputManager::GetInstance()->AddMonitor(callback);
32 if (monitorId < 0) {
33 FI_HILOGE("AddMonitor fail");
34 }
35 return monitorId;
36 }
37
AddMonitor(std::function<void (std::shared_ptr<MMI::KeyEvent>)> callback)38 int32_t InputAdapter::AddMonitor(std::function<void(std::shared_ptr<MMI::KeyEvent>)> callback)
39 {
40 int32_t monitorId = MMI::InputManager::GetInstance()->AddMonitor(callback);
41 if (monitorId < 0) {
42 FI_HILOGE("AddMonitor fail");
43 }
44 return monitorId;
45 }
46
RemoveMonitor(int32_t monitorId)47 void InputAdapter::RemoveMonitor(int32_t monitorId)
48 {
49 MMI::InputManager::GetInstance()->RemoveMonitor(monitorId);
50 }
51
AddInterceptor(std::function<void (std::shared_ptr<MMI::PointerEvent>)> pointerCb)52 int32_t InputAdapter::AddInterceptor(std::function<void(std::shared_ptr<MMI::PointerEvent>)> pointerCb)
53 {
54 return AddInterceptor(pointerCb, nullptr);
55 }
56
AddInterceptor(std::function<void (std::shared_ptr<MMI::KeyEvent>)> keyCb)57 int32_t InputAdapter::AddInterceptor(std::function<void(std::shared_ptr<MMI::KeyEvent>)> keyCb)
58 {
59 return AddInterceptor(nullptr, keyCb);
60 }
61
AddInterceptor(std::function<void (std::shared_ptr<MMI::PointerEvent>)> pointerCb,std::function<void (std::shared_ptr<MMI::KeyEvent>)> keyCb)62 int32_t InputAdapter::AddInterceptor(std::function<void(std::shared_ptr<MMI::PointerEvent>)> pointerCb,
63 std::function<void(std::shared_ptr<MMI::KeyEvent>)> keyCb)
64 {
65 uint32_t tags { 0u };
66 if (pointerCb != nullptr) {
67 tags |= MMI::CapabilityToTags(MMI::INPUT_DEV_CAP_POINTER);
68 }
69 if (keyCb != nullptr) {
70 tags |= MMI::CapabilityToTags(MMI::INPUT_DEV_CAP_KEYBOARD);
71 }
72 if (tags == 0u) {
73 FI_HILOGE("Both interceptors are null");
74 return -1;
75 }
76 auto interceptor = std::make_shared<InterceptorConsumer>(pointerCb, keyCb);
77 constexpr int32_t DEFAULT_PRIORITY { 499 };
78 int32_t interceptorId = MMI::InputManager::GetInstance()->AddInterceptor(interceptor, DEFAULT_PRIORITY, tags);
79 if (interceptorId < 0) {
80 FI_HILOGE("AddInterceptor fail");
81 }
82 return interceptorId;
83 }
84
RemoveInterceptor(int32_t interceptorId)85 void InputAdapter::RemoveInterceptor(int32_t interceptorId)
86 {
87 MMI::InputManager::GetInstance()->RemoveInterceptor(interceptorId);
88 }
89
AddFilter(std::function<bool (std::shared_ptr<MMI::PointerEvent>)> callback)90 int32_t InputAdapter::AddFilter(std::function<bool(std::shared_ptr<MMI::PointerEvent>)> callback)
91 {
92 constexpr int32_t DEFAULT_PRIORITY { 220 };
93 auto filter = std::make_shared<PointerFilter>(callback);
94 uint32_t tags = CapabilityToTags(MMI::INPUT_DEV_CAP_POINTER);
95 int32_t filterId = MMI::InputManager::GetInstance()->AddInputEventFilter(filter, DEFAULT_PRIORITY, tags);
96 if (filterId < 0) {
97 FI_HILOGE("AddInputEventFilter fail");
98 }
99 return filterId;
100 }
101
RemoveFilter(int32_t filterId)102 void InputAdapter::RemoveFilter(int32_t filterId)
103 {
104 MMI::InputManager::GetInstance()->RemoveInputEventFilter(filterId);
105 }
106
SetPointerVisibility(bool visible,int32_t priority)107 int32_t InputAdapter::SetPointerVisibility(bool visible, int32_t priority)
108 {
109 FI_HILOGI("Set pointer visibility, visible:%{public}s", visible ? "true" : "false");
110 return MMI::InputManager::GetInstance()->SetPointerVisible(visible, priority);
111 }
112
SetPointerLocation(int32_t x,int32_t y)113 int32_t InputAdapter::SetPointerLocation(int32_t x, int32_t y)
114 {
115 return MMI::InputManager::GetInstance()->SetPointerLocation(x, y);
116 }
117
EnableInputDevice(bool enable)118 int32_t InputAdapter::EnableInputDevice(bool enable)
119 {
120 return MMI::InputManager::GetInstance()->EnableInputDevice(enable);
121 }
122
SimulateInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent)123 void InputAdapter::SimulateInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent)
124 {
125 MMI::InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
126 }
127
SimulateInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent)128 void InputAdapter::SimulateInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent)
129 {
130 MMI::InputManager::GetInstance()->SimulateInputEvent(keyEvent);
131 }
132
AddVirtualInputDevice(std::shared_ptr<MMI::InputDevice> device,int32_t & deviceId)133 int32_t InputAdapter::AddVirtualInputDevice(std::shared_ptr<MMI::InputDevice> device, int32_t &deviceId)
134 {
135 return MMI::InputManager::GetInstance()->AddVirtualInputDevice(device, deviceId);
136 }
137
RemoveVirtualInputDevice(int32_t deviceId)138 int32_t InputAdapter::RemoveVirtualInputDevice(int32_t deviceId)
139 {
140 return MMI::InputManager::GetInstance()->RemoveVirtualInputDevice(deviceId);
141 }
142 } // namespace DeviceStatus
143 } // namespace Msdp
144 } // namespace OHOS