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_binding.h"
17
18 #include <vector>
19
20 #include "input_manager.h"
21
22 #include "devicestatus_define.h"
23 #include "input_binding_internal.h"
24
25 namespace {
26 constexpr ::OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, ::OHOS::Msdp::MSDP_DOMAIN_ID, "InputBinding" };
27 #define INPUT_MANAGER OHOS::MMI::InputManager::GetInstance()
28 } // namespace
29
DragMonitorConsumer(void (* cb)(CPointerEvent *))30 DragMonitorConsumer::DragMonitorConsumer(void (*cb)(CPointerEvent *)) : callback_(cb)
31 {
32 }
33
OnInputEvent(std::shared_ptr<OHOS::MMI::AxisEvent> axisEvent) const34 void DragMonitorConsumer::OnInputEvent(std::shared_ptr<OHOS::MMI::AxisEvent> axisEvent) const
35 {
36 }
37
OnInputEvent(std::shared_ptr<OHOS::MMI::KeyEvent> keyEvent) const38 void DragMonitorConsumer::OnInputEvent(std::shared_ptr<OHOS::MMI::KeyEvent> keyEvent) const
39 {
40 }
41
OnInputEvent(std::shared_ptr<OHOS::MMI::PointerEvent> pointerEvent) const42 void DragMonitorConsumer::OnInputEvent(std::shared_ptr<OHOS::MMI::PointerEvent> pointerEvent) const
43 {
44 CALL_DEBUG_ENTER;
45 CHKPV(pointerEvent);
46 auto cPointerEvent = new (std::nothrow) CPointerEvent(pointerEvent);
47 CHKPV(cPointerEvent);
48 CHKPV(callback_);
49 callback_(cPointerEvent);
50 }
51
CGetPointerId(const CPointerEvent * cPointerEvent)52 int32_t CGetPointerId(const CPointerEvent* cPointerEvent)
53 {
54 CALL_DEBUG_ENTER;
55 CHKPR(cPointerEvent, RET_ERR);
56 CHKPR(cPointerEvent->event, RET_ERR);
57 return cPointerEvent->event->GetPointerId();
58 }
59
CGetPointerAction(const CPointerEvent * cPointerEvent)60 int32_t CGetPointerAction(const CPointerEvent* cPointerEvent)
61 {
62 CALL_DEBUG_ENTER;
63 CHKPR(cPointerEvent, RET_ERR);
64 CHKPR(cPointerEvent->event, RET_ERR);
65 auto ret = cPointerEvent->event->GetPointerAction();
66 FI_HILOGD("action: %{public}d", ret);
67 return ret;
68 }
69
CGetTargetWindowId(const CPointerEvent * cPointerEvent)70 int32_t CGetTargetWindowId(const CPointerEvent* cPointerEvent)
71 {
72 CALL_DEBUG_ENTER;
73 CHKPR(cPointerEvent, RET_ERR);
74 CHKPR(cPointerEvent->event, RET_ERR);
75 return cPointerEvent->event->GetTargetWindowId();
76 }
77
CGetSourceType(const CPointerEvent * cPointerEvent)78 int32_t CGetSourceType(const CPointerEvent* cPointerEvent)
79 {
80 CALL_DEBUG_ENTER;
81 CHKPR(cPointerEvent, RET_ERR);
82 CHKPR(cPointerEvent->event, RET_ERR);
83 return cPointerEvent->event->GetSourceType();
84 }
85
CGetTargetDisplayId(const CPointerEvent * cPointerEvent)86 int32_t CGetTargetDisplayId(const CPointerEvent* cPointerEvent)
87 {
88 CALL_DEBUG_ENTER;
89 CHKPR(cPointerEvent, RET_ERR);
90 CHKPR(cPointerEvent->event, RET_ERR);
91 return cPointerEvent->event->GetTargetDisplayId();
92 }
93
CGetDisplayX(const CPointerEvent * cPointerEvent)94 int32_t CGetDisplayX(const CPointerEvent* cPointerEvent)
95 {
96 CALL_DEBUG_ENTER;
97 CHKPR(cPointerEvent, RET_ERR);
98 CHKPR(cPointerEvent->event, RET_ERR);
99 OHOS::MMI::PointerEvent::PointerItem pointerItem;
100 cPointerEvent->event->GetPointerItem(CGetPointerId(cPointerEvent), pointerItem);
101 return pointerItem.GetDisplayX();
102 }
103
CGetDisplayY(const CPointerEvent * cPointerEvent)104 int32_t CGetDisplayY(const CPointerEvent* cPointerEvent)
105 {
106 CALL_DEBUG_ENTER;
107 CHKPR(cPointerEvent, RET_ERR);
108 CHKPR(cPointerEvent->event, RET_ERR);
109 OHOS::MMI::PointerEvent::PointerItem pointerItem;
110 cPointerEvent->event->GetPointerItem(CGetPointerId(cPointerEvent), pointerItem);
111 return pointerItem.GetDisplayY();
112 }
113
CPointerEventAddFlag(const CPointerEvent * cPointerEvent)114 void CPointerEventAddFlag(const CPointerEvent* cPointerEvent)
115 {
116 CALL_DEBUG_ENTER;
117 CHKPV(cPointerEvent);
118 CHKPV(cPointerEvent->event);
119 cPointerEvent->event->AddFlag(OHOS::MMI::InputEvent::EVENT_FLAG_NO_INTERCEPT);
120 }
121
CKeyEventAddFlag(const CKeyEvent * cKeyEvent)122 void CKeyEventAddFlag(const CKeyEvent* cKeyEvent)
123 {
124 CALL_DEBUG_ENTER;
125 CHKPV(cKeyEvent);
126 CHKPV(cKeyEvent->event);
127 cKeyEvent->event->AddFlag(OHOS::MMI::AxisEvent::EVENT_FLAG_NO_INTERCEPT);
128 }
129
CGetDeviceId(const CPointerEvent * cPointerEvent)130 int32_t CGetDeviceId(const CPointerEvent* cPointerEvent)
131 {
132 CALL_DEBUG_ENTER;
133 CHKPR(cPointerEvent, RET_ERR);
134 CHKPR(cPointerEvent->event, RET_ERR);
135 return cPointerEvent->event->GetDeviceId();
136 }
137
CGetKeyCode(const CKeyEvent * cKeyEvent)138 int32_t CGetKeyCode(const CKeyEvent* cKeyEvent)
139 {
140 CALL_DEBUG_ENTER;
141 CHKPR(cKeyEvent, RET_ERR);
142 CHKPR(cKeyEvent->event, RET_ERR);
143 return cKeyEvent->event->GetKeyCode();
144 }
145
CAddMonitor(void (* callback)(CPointerEvent *))146 int32_t CAddMonitor(void (*callback)(CPointerEvent *))
147 {
148 CALL_DEBUG_ENTER;
149 CHKPR(callback, RET_ERR);
150 auto consumer = std::make_shared<DragMonitorConsumer>(callback);
151 auto ret = INPUT_MANAGER->AddMonitor(consumer);
152 if (ret < 0) {
153 FI_HILOGE("Failed to add monitor");
154 return RET_ERR;
155 }
156 return RET_OK;
157 }
158
CGetWindowPid(const CPointerEvent * cPointerEvent)159 int32_t CGetWindowPid(const CPointerEvent* cPointerEvent)
160 {
161 CALL_DEBUG_ENTER;
162 CHKPR(cPointerEvent, RET_ERR);
163 CHKPR(cPointerEvent->event, RET_ERR);
164 return INPUT_MANAGER->GetWindowPid(CGetTargetWindowId(cPointerEvent));
165 }
166
CGetPointerStyle(CPointerStyle * cPointerStyle)167 int32_t CGetPointerStyle(CPointerStyle* cPointerStyle)
168 {
169 CALL_DEBUG_ENTER;
170 CHKPR(cPointerStyle, RET_ERR);
171 OHOS::MMI::PointerStyle pointerStyle;
172 if (INPUT_MANAGER->GetPointerStyle(OHOS::MMI::GLOBAL_WINDOW_ID, pointerStyle) != RET_OK) {
173 FI_HILOGE("Failed to get pointer style");
174 return RET_ERR;
175 }
176
177 cPointerStyle->size = pointerStyle.size;
178 cPointerStyle->color.r = pointerStyle.color.r;
179 cPointerStyle->color.g = pointerStyle.color.g;
180 cPointerStyle->color.b = pointerStyle.color.b;
181 cPointerStyle->id = pointerStyle.id;
182 return RET_OK;
183 }
184
CAppendExtraData(CExtraData cExtraData)185 void CAppendExtraData(CExtraData cExtraData)
186 {
187 CALL_DEBUG_ENTER;
188 CHKPV(cExtraData.buffer);
189 uint8_t* uData = cExtraData.buffer;
190 CHKPV(uData);
191 OHOS::MMI::ExtraData extraData;
192 extraData.appended = cExtraData.appended;
193 extraData.pointerId = cExtraData.pointerId;
194 extraData.sourceType = cExtraData.sourceType;
195 for (size_t i = 0; i < cExtraData.bufferSize; ++i) {
196 extraData.buffer.emplace_back(*uData);
197 ++uData;
198 }
199 INPUT_MANAGER->AppendExtraData(extraData);
200 }
201
CSetPointerVisible(bool visible)202 int32_t CSetPointerVisible(bool visible)
203 {
204 CALL_DEBUG_ENTER;
205 if (INPUT_MANAGER->SetPointerVisible(visible) != RET_OK) {
206 FI_HILOGE("Failed to set pointer visible");
207 return RET_ERR;
208 }
209 return RET_OK;
210 }
211
CEnableInputDevice(bool enable)212 int32_t CEnableInputDevice(bool enable)
213 {
214 CALL_DEBUG_ENTER;
215 if (INPUT_MANAGER->EnableInputDevice(enable) != RET_OK) {
216 FI_HILOGE("Failed to enable input device");
217 return RET_ERR;
218 }
219 return RET_OK;
220 }
221
CRemoveInputEventFilter(int32_t filterId)222 int32_t CRemoveInputEventFilter(int32_t filterId)
223 {
224 CALL_DEBUG_ENTER;
225 if (INPUT_MANAGER->RemoveInputEventFilter(filterId) != RET_OK) {
226 FI_HILOGE("Failed to remove input event filter");
227 return RET_ERR;
228 }
229 return RET_OK;
230 }
231
CRemoveMonitor(int32_t monitorId)232 void CRemoveMonitor(int32_t monitorId)
233 {
234 CALL_DEBUG_ENTER;
235 INPUT_MANAGER->RemoveMonitor(monitorId);
236 }
237
CRemoveInterceptor(int32_t interceptorId)238 void CRemoveInterceptor(int32_t interceptorId)
239 {
240 CALL_DEBUG_ENTER;
241 INPUT_MANAGER->RemoveInterceptor(interceptorId);
242 }
243
CSetPointerLocation(int32_t physicalX,int32_t physicalY)244 void CSetPointerLocation(int32_t physicalX, int32_t physicalY)
245 {
246 CALL_DEBUG_ENTER;
247 INPUT_MANAGER->SetPointerLocation(physicalX, physicalY);
248 }
249
CDestroyPointerEvent(CPointerEvent * cPointerEvent)250 void CDestroyPointerEvent(CPointerEvent* cPointerEvent)
251 {
252 CALL_DEBUG_ENTER;
253 CHKPV(cPointerEvent);
254 CHKPV(cPointerEvent->event);
255 cPointerEvent->event = nullptr;
256 delete cPointerEvent;
257 }