1 /*
2 * Copyright (c) 2024 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 "oh_input_manager.h"
17
18 #include "error_multimodal.h"
19 #include "event_log_helper.h"
20 #include "input_manager.h"
21 #include "input_manager_impl.h"
22 #include "key_event.h"
23 #include "mmi_log.h"
24 #include "oh_axis_type.h"
25 #include "oh_input_device_listener.h"
26 #include "oh_input_interceptor.h"
27 #include "oh_key_code.h"
28 #include "permission_helper.h"
29 #ifdef PLAYER_FRAMEWORK_EXISTS
30 #include "screen_capture_monitor.h"
31 #include "ipc_skeleton.h"
32 #endif // PLAYER_FRAMEWORK_EXISTS
33
34 #undef MMI_LOG_TAG
35 #define MMI_LOG_TAG "OHInputManager"
36
37 struct Input_KeyState {
38 int32_t keyCode;
39 int32_t keyState;
40 int32_t keySwitch;
41 };
42
43 struct Input_KeyEvent {
44 int32_t action;
45 int32_t keyCode;
46 int64_t actionTime { -1 };
47 int32_t windowId { -1 };
48 int32_t displayId { -1 };
49 };
50
51 struct Input_MouseEvent {
52 int32_t action;
53 int32_t displayX;
54 int32_t displayY;
55 int32_t button { -1 };
56 int32_t axisType { -1 };
57 float axisValue { 0.0f };
58 int64_t actionTime { -1 };
59 int32_t windowId { -1 };
60 int32_t displayId { -1 };
61 };
62
63 struct Input_TouchEvent {
64 int32_t action;
65 int32_t id;
66 int32_t displayX;
67 int32_t displayY;
68 int64_t actionTime { -1 };
69 int32_t windowId { -1 };
70 int32_t displayId { -1 };
71 };
72
73 struct Input_AxisEvent {
74 int32_t axisAction;
75 float displayX;
76 float displayY;
77 std::map<int32_t, double> axisValues;
78 int64_t actionTime { -1 };
79 int32_t sourceType;
80 int32_t axisEventType { -1 };
81 int32_t windowId { -1 };
82 int32_t displayId { -1 };
83 };
84
85 constexpr int32_t SIZE_ARRAY = 64;
86 struct Input_DeviceInfo {
87 int32_t id { -1 };
88 char name[SIZE_ARRAY] {};
89 int32_t ability { -1 };
90 int32_t product { -1 };
91 int32_t vendor { -1 };
92 int32_t version { -1 };
93 char phys[SIZE_ARRAY] {};
94 };
95
96 static constexpr int32_t INVALID_MONITOR_ID = -1;
97 static constexpr int32_t INVALID_INTERCEPTOR_ID = -1;
98 static std::shared_ptr<OHOS::MMI::KeyEvent> g_keyEvent = OHOS::MMI::KeyEvent::Create();
99 static std::shared_ptr<OHOS::MMI::PointerEvent> g_mouseEvent = OHOS::MMI::PointerEvent::Create();
100 static std::shared_ptr<OHOS::MMI::PointerEvent> g_touchEvent = OHOS::MMI::PointerEvent::Create();
101 static const std::set<int32_t> g_keyCodeValueSet = {
102 KEYCODE_FN, KEYCODE_DPAD_UP, KEYCODE_DPAD_DOWN, KEYCODE_DPAD_LEFT, KEYCODE_DPAD_RIGHT, KEYCODE_ALT_LEFT,
103 KEYCODE_ALT_RIGHT, KEYCODE_SHIFT_LEFT, KEYCODE_SHIFT_RIGHT, KEYCODE_TAB, KEYCODE_ENTER, KEYCODE_DEL, KEYCODE_MENU,
104 KEYCODE_PAGE_UP, KEYCODE_PAGE_DOWN, KEYCODE_ESCAPE, KEYCODE_FORWARD_DEL, KEYCODE_CTRL_LEFT, KEYCODE_CTRL_RIGHT,
105 KEYCODE_CAPS_LOCK, KEYCODE_SCROLL_LOCK, KEYCODE_META_LEFT, KEYCODE_META_RIGHT, KEYCODE_SYSRQ, KEYCODE_BREAK,
106 KEYCODE_MOVE_HOME, KEYCODE_MOVE_END, KEYCODE_INSERT, KEYCODE_F1, KEYCODE_F2, KEYCODE_F3, KEYCODE_F4, KEYCODE_F5,
107 KEYCODE_F6, KEYCODE_F7, KEYCODE_F8, KEYCODE_F9, KEYCODE_F10, KEYCODE_F11, KEYCODE_F12, KEYCODE_NUM_LOCK
108 };
109 static std::set<Input_KeyEventCallback> g_keyMonitorCallbacks;
110 static std::set<Input_MouseEventCallback> g_mouseMonitorCallbacks;
111 static std::set<Input_TouchEventCallback> g_touchMonitorCallbacks;
112 static std::set<Input_AxisEventCallback> g_axisMonitorAllCallbacks;
113 static std::set<Input_DeviceListener*> g_ohDeviceListenerList;
114 static std::map<InputEvent_AxisEventType, std::set<Input_AxisEventCallback>> g_axisMonitorCallbacks;
115 static Input_KeyEventCallback g_keyInterceptorCallback = nullptr;
116 static struct Input_InterceptorEventCallback *g_pointerInterceptorCallback = nullptr;
117 static std::shared_ptr<OHOS::MMI::OHInputInterceptor> g_pointerInterceptor =
118 std::make_shared<OHOS::MMI::OHInputInterceptor>();
119 static std::shared_ptr<OHOS::MMI::OHInputInterceptor> g_keyInterceptor =
120 std::make_shared<OHOS::MMI::OHInputInterceptor>();
121 static std::shared_ptr<OHOS::MMI::OHInputDeviceListener> g_deviceListener =
122 std::make_shared<OHOS::MMI::OHInputDeviceListener>();
123 static std::mutex g_DeviceListerCallbackMutex;
124 static std::mutex g_mutex;
125 static int32_t g_keyMonitorId = INVALID_MONITOR_ID;
126 static int32_t g_pointerMonitorId = INVALID_MONITOR_ID;
127 static int32_t g_keyInterceptorId = INVALID_INTERCEPTOR_ID;
128 static int32_t g_pointerInterceptorId = INVALID_INTERCEPTOR_ID;
129
OH_Input_GetKeyState(struct Input_KeyState * keyState)130 Input_Result OH_Input_GetKeyState(struct Input_KeyState* keyState)
131 {
132 CALL_DEBUG_ENTER;
133 CHKPR(keyState, INPUT_PARAMETER_ERROR);
134 if (keyState->keyCode < 0 || keyState->keyCode > KEYCODE_NUMPAD_RIGHT_PAREN) {
135 if (!OHOS::MMI::EventLogHelper::IsBetaVersion()) {
136 MMI_HILOGE("keyCode is invalid");
137 } else {
138 MMI_HILOGE("keyCode is invalid");
139 }
140 return INPUT_PARAMETER_ERROR;
141 }
142 if (g_keyCodeValueSet.find(keyState->keyCode) == g_keyCodeValueSet.end()) {
143 MMI_HILOGE("keyCode is not within the query range, keyCode:%{public}d", keyState->keyCode);
144 return INPUT_PARAMETER_ERROR;
145 }
146 std::vector<int32_t> pressedKeys;
147 std::map<int32_t, int32_t> specialKeysState;
148 OHOS::MMI::InputManager::GetInstance()->GetKeyState(pressedKeys, specialKeysState);
149 auto iter = std::find(pressedKeys.begin(), pressedKeys.end(), keyState->keyCode);
150 if (iter != pressedKeys.end()) {
151 keyState->keyState = KEY_PRESSED;
152 } else {
153 keyState->keyState = KEY_RELEASED;
154 }
155 auto itr = specialKeysState.find(keyState->keyCode);
156 if (itr != specialKeysState.end()) {
157 if (itr->second == 0) {
158 keyState->keySwitch = KEY_SWITCH_OFF;
159 } else {
160 keyState->keySwitch = KEY_SWITCH_ON;
161 }
162 } else {
163 keyState->keySwitch = KEY_DEFAULT;
164 }
165 return INPUT_SUCCESS;
166 }
167
OH_Input_CreateKeyState()168 struct Input_KeyState* OH_Input_CreateKeyState()
169 {
170 Input_KeyState* keyState = new (std::nothrow) Input_KeyState();
171 CHKPL(keyState);
172 return keyState;
173 }
174
OH_Input_DestroyKeyState(struct Input_KeyState ** keyState)175 void OH_Input_DestroyKeyState(struct Input_KeyState** keyState)
176 {
177 CALL_DEBUG_ENTER;
178 CHKPV(keyState);
179 CHKPV(*keyState);
180 delete *keyState;
181 *keyState = nullptr;
182 }
183
OH_Input_SetKeyCode(struct Input_KeyState * keyState,int32_t keyCode)184 void OH_Input_SetKeyCode(struct Input_KeyState* keyState, int32_t keyCode)
185 {
186 CHKPV(keyState);
187 if (keyCode < 0 || keyState->keyCode > KEYCODE_NUMPAD_RIGHT_PAREN) {
188 if (!OHOS::MMI::EventLogHelper::IsBetaVersion()) {
189 MMI_HILOGE("keyCode is invalid");
190 } else {
191 MMI_HILOGE("keyCode is invalid");
192 }
193 return;
194 }
195 keyState->keyCode = keyCode;
196 }
197
OH_Input_GetKeyCode(const struct Input_KeyState * keyState)198 int32_t OH_Input_GetKeyCode(const struct Input_KeyState* keyState)
199 {
200 CHKPR(keyState, KEYCODE_UNKNOWN);
201 return keyState->keyCode;
202 }
203
OH_Input_SetKeyPressed(struct Input_KeyState * keyState,int32_t keyAction)204 void OH_Input_SetKeyPressed(struct Input_KeyState* keyState, int32_t keyAction)
205 {
206 CHKPV(keyState);
207 keyState->keyState = keyAction;
208 }
209
OH_Input_GetKeyPressed(const struct Input_KeyState * keyState)210 int32_t OH_Input_GetKeyPressed(const struct Input_KeyState* keyState)
211 {
212 CHKPR(keyState, KEY_DEFAULT);
213 return keyState->keyState;
214 }
215
OH_Input_SetKeySwitch(struct Input_KeyState * keyState,int32_t keySwitch)216 void OH_Input_SetKeySwitch(struct Input_KeyState* keyState, int32_t keySwitch)
217 {
218 CHKPV(keyState);
219 keyState->keySwitch = keySwitch;
220 }
221
OH_Input_GetKeySwitch(const struct Input_KeyState * keyState)222 int32_t OH_Input_GetKeySwitch(const struct Input_KeyState* keyState)
223 {
224 CHKPR(keyState, KEY_DEFAULT);
225 return keyState->keySwitch;
226 }
227
HandleKeyAction(const struct Input_KeyEvent * keyEvent,OHOS::MMI::KeyEvent::KeyItem & item)228 static void HandleKeyAction(const struct Input_KeyEvent* keyEvent, OHOS::MMI::KeyEvent::KeyItem &item)
229 {
230 if (keyEvent->action == KEY_ACTION_DOWN) {
231 g_keyEvent->AddPressedKeyItems(item);
232 }
233 if (keyEvent->action == KEY_ACTION_UP) {
234 std::optional<OHOS::MMI::KeyEvent::KeyItem> pressedKeyItem = g_keyEvent->GetKeyItem(keyEvent->keyCode);
235 if (pressedKeyItem) {
236 item.SetDownTime(pressedKeyItem->GetDownTime());
237 } else if (!OHOS::MMI::EventLogHelper::IsBetaVersion()) {
238 MMI_HILOGW("Find pressed key failed");
239 } else {
240 MMI_HILOGW("Find pressed key failed");
241 }
242 g_keyEvent->RemoveReleasedKeyItems(item);
243 g_keyEvent->AddPressedKeyItems(item);
244 }
245 }
246
OH_Input_InjectKeyEvent(const struct Input_KeyEvent * keyEvent)247 int32_t OH_Input_InjectKeyEvent(const struct Input_KeyEvent* keyEvent)
248 {
249 MMI_HILOGI("Input_KeyEvent injectEvent");
250 CHKPR(keyEvent, INPUT_PARAMETER_ERROR);
251 if (keyEvent->keyCode < 0) {
252 if (!OHOS::MMI::EventLogHelper::IsBetaVersion()) {
253 MMI_HILOGE("keyCode is less 0, can not process");
254 } else {
255 MMI_HILOGE("keyCode is less 0, can not process");
256 }
257 return INPUT_PARAMETER_ERROR;
258 }
259 CHKPR(g_keyEvent, INPUT_PARAMETER_ERROR);
260 g_keyEvent->ClearFlag();
261 if (g_keyEvent->GetAction() == OHOS::MMI::KeyEvent::KEY_ACTION_UP) {
262 std::optional<OHOS::MMI::KeyEvent::KeyItem> preUpKeyItem = g_keyEvent->GetKeyItem();
263 if (preUpKeyItem) {
264 g_keyEvent->RemoveReleasedKeyItems(*preUpKeyItem);
265 } else {
266 MMI_HILOGE("The preUpKeyItem is nullopt");
267 }
268 }
269 int64_t time = keyEvent->actionTime;
270 if (time < 0) {
271 time = OHOS::MMI::GetSysClockTime();
272 }
273 g_keyEvent->SetActionTime(time);
274 g_keyEvent->SetRepeat(true);
275 g_keyEvent->SetKeyCode(keyEvent->keyCode);
276 bool isKeyPressed = false;
277 if (keyEvent->action == KEY_ACTION_DOWN) {
278 g_keyEvent->SetAction(OHOS::MMI::KeyEvent::KEY_ACTION_DOWN);
279 g_keyEvent->SetKeyAction(OHOS::MMI::KeyEvent::KEY_ACTION_DOWN);
280 isKeyPressed = true;
281 } else if (keyEvent->action == KEY_ACTION_UP) {
282 g_keyEvent->SetAction(OHOS::MMI::KeyEvent::KEY_ACTION_UP);
283 g_keyEvent->SetKeyAction(OHOS::MMI::KeyEvent::KEY_ACTION_UP);
284 isKeyPressed = false;
285 }
286 OHOS::MMI::KeyEvent::KeyItem item;
287 item.SetDownTime(time);
288 item.SetKeyCode(keyEvent->keyCode);
289 item.SetPressed(isKeyPressed);
290 HandleKeyAction(keyEvent, item);
291 g_keyEvent->AddFlag(OHOS::MMI::InputEvent::EVENT_FLAG_SIMULATE);
292 OHOS::Singleton<OHOS::MMI::InputManagerImpl>::GetInstance().SimulateInputEvent(g_keyEvent, true);
293 return INPUT_SUCCESS;
294 }
295
OH_Input_CreateKeyEvent()296 struct Input_KeyEvent* OH_Input_CreateKeyEvent()
297 {
298 Input_KeyEvent* keyEvent = new (std::nothrow) Input_KeyEvent();
299 CHKPL(keyEvent);
300 return keyEvent;
301 }
302
OH_Input_DestroyKeyEvent(struct Input_KeyEvent ** keyEvent)303 void OH_Input_DestroyKeyEvent(struct Input_KeyEvent** keyEvent)
304 {
305 CALL_DEBUG_ENTER;
306 CHKPV(keyEvent);
307 CHKPV(*keyEvent);
308 delete *keyEvent;
309 *keyEvent = nullptr;
310 }
311
OH_Input_SetKeyEventAction(struct Input_KeyEvent * keyEvent,int32_t action)312 void OH_Input_SetKeyEventAction(struct Input_KeyEvent* keyEvent, int32_t action)
313 {
314 CHKPV(keyEvent);
315 keyEvent->action = action;
316 }
317
OH_Input_GetKeyEventAction(const struct Input_KeyEvent * keyEvent)318 int32_t OH_Input_GetKeyEventAction(const struct Input_KeyEvent* keyEvent)
319 {
320 CHKPR(keyEvent, RET_ERR);
321 return keyEvent->action;
322 }
323
OH_Input_SetKeyEventKeyCode(struct Input_KeyEvent * keyEvent,int32_t keyCode)324 void OH_Input_SetKeyEventKeyCode(struct Input_KeyEvent* keyEvent, int32_t keyCode)
325 {
326 CHKPV(keyEvent);
327 keyEvent->keyCode = keyCode;
328 }
329
OH_Input_GetKeyEventKeyCode(const struct Input_KeyEvent * keyEvent)330 int32_t OH_Input_GetKeyEventKeyCode(const struct Input_KeyEvent* keyEvent)
331 {
332 CHKPR(keyEvent, KEYCODE_UNKNOWN);
333 return keyEvent->keyCode;
334 }
335
OH_Input_SetKeyEventActionTime(struct Input_KeyEvent * keyEvent,int64_t actionTime)336 void OH_Input_SetKeyEventActionTime(struct Input_KeyEvent* keyEvent, int64_t actionTime)
337 {
338 CHKPV(keyEvent);
339 keyEvent->actionTime = actionTime;
340 }
341
OH_Input_GetKeyEventActionTime(const struct Input_KeyEvent * keyEvent)342 int64_t OH_Input_GetKeyEventActionTime(const struct Input_KeyEvent* keyEvent)
343 {
344 CHKPR(keyEvent, RET_ERR);
345 return keyEvent->actionTime;
346 }
347
OH_Input_SetKeyEventWindowId(struct Input_KeyEvent * keyEvent,int32_t windowId)348 void OH_Input_SetKeyEventWindowId(struct Input_KeyEvent* keyEvent, int32_t windowId)
349 {
350 CHKPV(keyEvent);
351 keyEvent->windowId = windowId;
352 }
353
OH_Input_GetKeyEventWindowId(const struct Input_KeyEvent * keyEvent)354 int32_t OH_Input_GetKeyEventWindowId(const struct Input_KeyEvent* keyEvent)
355 {
356 CHKPR(keyEvent, RET_ERR);
357 return keyEvent->windowId;
358 }
359
OH_Input_SetKeyEventDisplayId(struct Input_KeyEvent * keyEvent,int32_t displayId)360 void OH_Input_SetKeyEventDisplayId(struct Input_KeyEvent* keyEvent, int32_t displayId)
361 {
362 CHKPV(keyEvent);
363 keyEvent->displayId = displayId;
364 }
365
OH_Input_GetKeyEventDisplayId(const struct Input_KeyEvent * keyEvent)366 int32_t OH_Input_GetKeyEventDisplayId(const struct Input_KeyEvent* keyEvent)
367 {
368 CHKPR(keyEvent, RET_ERR);
369 return keyEvent->displayId;
370 }
371
HandleMouseButton(const struct Input_MouseEvent * mouseEvent)372 static int32_t HandleMouseButton(const struct Input_MouseEvent* mouseEvent)
373 {
374 int32_t button = mouseEvent->button;
375 switch (button) {
376 case MOUSE_BUTTON_NONE: {
377 button = OHOS::MMI::PointerEvent::BUTTON_NONE;
378 break;
379 }
380 case MOUSE_BUTTON_LEFT: {
381 button = OHOS::MMI::PointerEvent::MOUSE_BUTTON_LEFT;
382 break;
383 }
384 case MOUSE_BUTTON_MIDDLE: {
385 button = OHOS::MMI::PointerEvent::MOUSE_BUTTON_MIDDLE;
386 break;
387 }
388 case MOUSE_BUTTON_RIGHT: {
389 button = OHOS::MMI::PointerEvent::MOUSE_BUTTON_RIGHT;
390 break;
391 }
392 case MOUSE_BUTTON_FORWARD: {
393 button = OHOS::MMI::PointerEvent::MOUSE_BUTTON_FORWARD;
394 break;
395 }
396 case MOUSE_BUTTON_BACK: {
397 button = OHOS::MMI::PointerEvent::MOUSE_BUTTON_BACK;
398 break;
399 }
400 default: {
401 MMI_HILOGE("button:%{public}d is invalid", button);
402 return INPUT_PARAMETER_ERROR;
403 }
404 }
405 if (mouseEvent->action == MOUSE_ACTION_BUTTON_DOWN) {
406 g_mouseEvent->SetButtonPressed(button);
407 } else if (mouseEvent->action == MOUSE_ACTION_BUTTON_UP) {
408 g_mouseEvent->DeleteReleaseButton(button);
409 }
410 g_mouseEvent->SetButtonId(button);
411 return INPUT_SUCCESS;
412 }
413
HandleMouseAction(const struct Input_MouseEvent * mouseEvent,OHOS::MMI::PointerEvent::PointerItem & item)414 static int32_t HandleMouseAction(const struct Input_MouseEvent* mouseEvent, OHOS::MMI::PointerEvent::PointerItem &item)
415 {
416 switch (mouseEvent->action) {
417 case MOUSE_ACTION_CANCEL:
418 g_mouseEvent->SetPointerAction(OHOS::MMI::PointerEvent::POINTER_ACTION_CANCEL);
419 break;
420 case MOUSE_ACTION_MOVE:
421 g_mouseEvent->SetPointerAction(OHOS::MMI::PointerEvent::POINTER_ACTION_MOVE);
422 break;
423 case MOUSE_ACTION_BUTTON_DOWN:
424 g_mouseEvent->SetPointerAction(OHOS::MMI::PointerEvent::POINTER_ACTION_BUTTON_DOWN);
425 item.SetPressed(true);
426 break;
427 case MOUSE_ACTION_BUTTON_UP:
428 g_mouseEvent->SetPointerAction(OHOS::MMI::PointerEvent::POINTER_ACTION_BUTTON_UP);
429 item.SetPressed(false);
430 break;
431 case MOUSE_ACTION_AXIS_BEGIN:
432 g_mouseEvent->SetPointerAction(OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_BEGIN);
433 break;
434 case MOUSE_ACTION_AXIS_UPDATE:
435 g_mouseEvent->SetPointerAction(OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_UPDATE);
436 break;
437 case MOUSE_ACTION_AXIS_END:
438 g_mouseEvent->SetPointerAction(OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_END);
439 break;
440 default:
441 MMI_HILOGE("action:%{public}d is invalid", mouseEvent->action);
442 return INPUT_PARAMETER_ERROR;
443 }
444 if (mouseEvent->axisType == MOUSE_AXIS_SCROLL_VERTICAL) {
445 g_mouseEvent->SetAxisValue(OHOS::MMI::PointerEvent::AXIS_TYPE_SCROLL_VERTICAL, mouseEvent->axisValue);
446 }
447 if (mouseEvent->axisType == MOUSE_AXIS_SCROLL_HORIZONTAL) {
448 g_mouseEvent->SetAxisValue(OHOS::MMI::PointerEvent::AXIS_TYPE_SCROLL_HORIZONTAL, mouseEvent->axisValue);
449 }
450 return HandleMouseButton(mouseEvent);
451 }
452
HandleMouseProperty(const struct Input_MouseEvent * mouseEvent,OHOS::MMI::PointerEvent::PointerItem & item)453 static int32_t HandleMouseProperty(const struct Input_MouseEvent* mouseEvent,
454 OHOS::MMI::PointerEvent::PointerItem &item)
455 {
456 int32_t screenX = mouseEvent->displayX;
457 int32_t screenY = mouseEvent->displayY;
458 g_mouseEvent->SetSourceType(OHOS::MMI::PointerEvent::SOURCE_TYPE_MOUSE);
459 item.SetPointerId(0);
460 item.SetDisplayX(screenX);
461 item.SetDisplayY(screenY);
462 g_mouseEvent->SetPointerId(0);
463 g_mouseEvent->UpdatePointerItem(g_mouseEvent->GetPointerId(), item);
464 return INPUT_SUCCESS;
465 }
466
OH_Input_InjectMouseEvent(const struct Input_MouseEvent * mouseEvent)467 int32_t OH_Input_InjectMouseEvent(const struct Input_MouseEvent* mouseEvent)
468 {
469 MMI_HILOGI("Input_MouseEvent injectEvent");
470 CHKPR(mouseEvent, INPUT_PARAMETER_ERROR);
471 CHKPR(g_mouseEvent, INPUT_PARAMETER_ERROR);
472 g_mouseEvent->ClearFlag();
473 g_mouseEvent->ClearAxisValue();
474 g_mouseEvent->SetTargetDisplayId(0);
475 int64_t time = mouseEvent->actionTime;
476 if (time < 0) {
477 time = OHOS::MMI::GetSysClockTime();
478 }
479 g_mouseEvent->SetActionTime(time);
480 OHOS::MMI::PointerEvent::PointerItem item;
481 int32_t pointerId = 10000;
482 g_mouseEvent->GetPointerItem(pointerId, item);
483 item.SetDownTime(time);
484 int32_t result = HandleMouseAction(mouseEvent, item);
485 if (result != 0) {
486 return result;
487 }
488 result = HandleMouseProperty(mouseEvent, item);
489 if (result != 0) {
490 return result;
491 }
492 g_mouseEvent->AddFlag(OHOS::MMI::InputEvent::EVENT_FLAG_SIMULATE);
493 result = OHOS::Singleton<OHOS::MMI::InputManagerImpl>::GetInstance().SimulateInputEvent(g_mouseEvent, true);
494 if ((result == INPUT_PERMISSION_DENIED) || (result == INPUT_OCCUPIED_BY_OTHER)) {
495 MMI_HILOGE("Permission denied or occupied by other");
496 return result;
497 }
498 return INPUT_SUCCESS;
499 }
500
OH_Input_CreateMouseEvent()501 struct Input_MouseEvent* OH_Input_CreateMouseEvent()
502 {
503 CALL_DEBUG_ENTER;
504 Input_MouseEvent* mouseEvent = new (std::nothrow) Input_MouseEvent();
505 CHKPL(mouseEvent);
506 return mouseEvent;
507 }
508
OH_Input_DestroyMouseEvent(struct Input_MouseEvent ** mouseEvent)509 void OH_Input_DestroyMouseEvent(struct Input_MouseEvent** mouseEvent)
510 {
511 CALL_DEBUG_ENTER;
512 CHKPV(mouseEvent);
513 CHKPV(*mouseEvent);
514 delete *mouseEvent;
515 *mouseEvent = nullptr;
516 }
517
OH_Input_SetMouseEventAction(struct Input_MouseEvent * mouseEvent,int32_t action)518 void OH_Input_SetMouseEventAction(struct Input_MouseEvent* mouseEvent, int32_t action)
519 {
520 CALL_DEBUG_ENTER;
521 CHKPV(mouseEvent);
522 mouseEvent->action = action;
523 }
524
OH_Input_GetMouseEventAction(const struct Input_MouseEvent * mouseEvent)525 int32_t OH_Input_GetMouseEventAction(const struct Input_MouseEvent* mouseEvent)
526 {
527 CALL_DEBUG_ENTER;
528 CHKPR(mouseEvent, RET_ERR);
529 return mouseEvent->action;
530 }
531
OH_Input_SetMouseEventDisplayX(struct Input_MouseEvent * mouseEvent,int32_t displayX)532 void OH_Input_SetMouseEventDisplayX(struct Input_MouseEvent* mouseEvent, int32_t displayX)
533 {
534 CALL_DEBUG_ENTER;
535 CHKPV(mouseEvent);
536 mouseEvent->displayX = displayX;
537 }
538
OH_Input_GetMouseEventDisplayX(const struct Input_MouseEvent * mouseEvent)539 int32_t OH_Input_GetMouseEventDisplayX(const struct Input_MouseEvent* mouseEvent)
540 {
541 CALL_DEBUG_ENTER;
542 CHKPR(mouseEvent, RET_ERR);
543 return mouseEvent->displayX;
544 }
545
OH_Input_SetMouseEventDisplayY(struct Input_MouseEvent * mouseEvent,int32_t displayY)546 void OH_Input_SetMouseEventDisplayY(struct Input_MouseEvent* mouseEvent, int32_t displayY)
547 {
548 CALL_DEBUG_ENTER;
549 CHKPV(mouseEvent);
550 mouseEvent->displayY = displayY;
551 }
552
OH_Input_GetMouseEventDisplayY(const struct Input_MouseEvent * mouseEvent)553 int32_t OH_Input_GetMouseEventDisplayY(const struct Input_MouseEvent* mouseEvent)
554 {
555 CALL_DEBUG_ENTER;
556 CHKPR(mouseEvent, RET_ERR);
557 return mouseEvent->displayY;
558 }
559
OH_Input_SetMouseEventButton(struct Input_MouseEvent * mouseEvent,int32_t button)560 void OH_Input_SetMouseEventButton(struct Input_MouseEvent* mouseEvent, int32_t button)
561 {
562 CALL_DEBUG_ENTER;
563 CHKPV(mouseEvent);
564 mouseEvent->button = button;
565 }
566
OH_Input_GetMouseEventButton(const struct Input_MouseEvent * mouseEvent)567 int32_t OH_Input_GetMouseEventButton(const struct Input_MouseEvent* mouseEvent)
568 {
569 CALL_DEBUG_ENTER;
570 CHKPR(mouseEvent, RET_ERR);
571 return mouseEvent->button;
572 }
573
OH_Input_SetMouseEventAxisType(struct Input_MouseEvent * mouseEvent,int32_t axisType)574 void OH_Input_SetMouseEventAxisType(struct Input_MouseEvent* mouseEvent, int32_t axisType)
575 {
576 CALL_DEBUG_ENTER;
577 CHKPV(mouseEvent);
578 mouseEvent->axisType = axisType;
579 }
580
OH_Input_GetMouseEventAxisType(const struct Input_MouseEvent * mouseEvent)581 int32_t OH_Input_GetMouseEventAxisType(const struct Input_MouseEvent* mouseEvent)
582 {
583 CALL_DEBUG_ENTER;
584 CHKPR(mouseEvent, RET_ERR);
585 return mouseEvent->axisType;
586 }
587
OH_Input_SetMouseEventAxisValue(struct Input_MouseEvent * mouseEvent,float axisValue)588 void OH_Input_SetMouseEventAxisValue(struct Input_MouseEvent* mouseEvent, float axisValue)
589 {
590 CALL_DEBUG_ENTER;
591 CHKPV(mouseEvent);
592 mouseEvent->axisValue = axisValue;
593 }
594
OH_Input_GetMouseEventAxisValue(const struct Input_MouseEvent * mouseEvent)595 float OH_Input_GetMouseEventAxisValue(const struct Input_MouseEvent* mouseEvent)
596 {
597 CALL_DEBUG_ENTER;
598 CHKPR(mouseEvent, RET_ERR);
599 return mouseEvent->axisValue;
600 }
601
OH_Input_SetMouseEventActionTime(struct Input_MouseEvent * mouseEvent,int64_t actionTime)602 void OH_Input_SetMouseEventActionTime(struct Input_MouseEvent* mouseEvent, int64_t actionTime)
603 {
604 CALL_DEBUG_ENTER;
605 CHKPV(mouseEvent);
606 mouseEvent->actionTime = actionTime;
607 }
608
OH_Input_GetMouseEventActionTime(const struct Input_MouseEvent * mouseEvent)609 int64_t OH_Input_GetMouseEventActionTime(const struct Input_MouseEvent* mouseEvent)
610 {
611 CALL_DEBUG_ENTER;
612 CHKPR(mouseEvent, RET_ERR);
613 return mouseEvent->actionTime;
614 }
615
OH_Input_SetMouseEventWindowId(struct Input_MouseEvent * mouseEvent,int32_t windowId)616 void OH_Input_SetMouseEventWindowId(struct Input_MouseEvent* mouseEvent, int32_t windowId)
617 {
618 CALL_DEBUG_ENTER;
619 CHKPV(mouseEvent);
620 mouseEvent->windowId = windowId;
621 }
622
OH_Input_GetMouseEventWindowId(const struct Input_MouseEvent * mouseEvent)623 int32_t OH_Input_GetMouseEventWindowId(const struct Input_MouseEvent* mouseEvent)
624 {
625 CALL_DEBUG_ENTER;
626 CHKPR(mouseEvent, RET_ERR);
627 return mouseEvent->windowId;
628 }
629
OH_Input_SetMouseEventDisplayId(struct Input_MouseEvent * mouseEvent,int32_t displayId)630 void OH_Input_SetMouseEventDisplayId(struct Input_MouseEvent* mouseEvent, int32_t displayId)
631 {
632 CALL_DEBUG_ENTER;
633 CHKPV(mouseEvent);
634 mouseEvent->displayId = displayId;
635 }
636
OH_Input_GetMouseEventDisplayId(const struct Input_MouseEvent * mouseEvent)637 int32_t OH_Input_GetMouseEventDisplayId(const struct Input_MouseEvent* mouseEvent)
638 {
639 CALL_DEBUG_ENTER;
640 CHKPR(mouseEvent, RET_ERR);
641 return mouseEvent->displayId;
642 }
643
HandleTouchActionDown(OHOS::MMI::PointerEvent::PointerItem & item,int64_t time)644 static void HandleTouchActionDown(OHOS::MMI::PointerEvent::PointerItem &item, int64_t time)
645 {
646 auto pointIds = g_touchEvent->GetPointerIds();
647 if (pointIds.empty()) {
648 g_touchEvent->SetActionStartTime(time);
649 g_touchEvent->SetTargetDisplayId(0);
650 }
651 g_touchEvent->SetActionTime(time);
652 g_touchEvent->SetPointerAction(OHOS::MMI::PointerEvent::POINTER_ACTION_DOWN);
653 item.SetDownTime(time);
654 item.SetPressed(true);
655 }
656
HandleTouchAction(const struct Input_TouchEvent * touchEvent,OHOS::MMI::PointerEvent::PointerItem & item)657 static int32_t HandleTouchAction(const struct Input_TouchEvent* touchEvent, OHOS::MMI::PointerEvent::PointerItem &item)
658 {
659 CALL_DEBUG_ENTER;
660 int64_t time = touchEvent->actionTime;
661 if (time < 0) {
662 time = OHOS::MMI::GetSysClockTime();
663 }
664 switch (touchEvent->action) {
665 case TOUCH_ACTION_CANCEL:{
666 g_touchEvent->SetActionTime(time);
667 g_touchEvent->SetPointerAction(OHOS::MMI::PointerEvent::POINTER_ACTION_CANCEL);
668 if (!(g_touchEvent->GetPointerItem(touchEvent->id, item))) {
669 MMI_HILOGE("Get pointer parameter failed");
670 return INPUT_PARAMETER_ERROR;
671 }
672 item.SetPressed(false);
673 break;
674 }
675 case TOUCH_ACTION_DOWN: {
676 HandleTouchActionDown(item, time);
677 break;
678 }
679 case TOUCH_ACTION_MOVE: {
680 g_touchEvent->SetActionTime(time);
681 g_touchEvent->SetPointerAction(OHOS::MMI::PointerEvent::POINTER_ACTION_MOVE);
682 if (!(g_touchEvent->GetPointerItem(touchEvent->id, item))) {
683 MMI_HILOGE("Get pointer parameter failed");
684 return INPUT_PARAMETER_ERROR;
685 }
686 break;
687 }
688 case TOUCH_ACTION_UP: {
689 g_touchEvent->SetActionTime(time);
690 g_touchEvent->SetPointerAction(OHOS::MMI::PointerEvent::POINTER_ACTION_UP);
691 if (!(g_touchEvent->GetPointerItem(touchEvent->id, item))) {
692 MMI_HILOGE("Get pointer parameter failed");
693 return INPUT_PARAMETER_ERROR;
694 }
695 item.SetPressed(false);
696 break;
697 }
698 default: {
699 MMI_HILOGE("action:%{public}d is invalid", touchEvent->action);
700 return INPUT_PARAMETER_ERROR;
701 }
702 }
703 return INPUT_SUCCESS;
704 }
705
HandleTouchProperty(const struct Input_TouchEvent * touchEvent,OHOS::MMI::PointerEvent::PointerItem & item)706 static int32_t HandleTouchProperty(const struct Input_TouchEvent* touchEvent,
707 OHOS::MMI::PointerEvent::PointerItem &item)
708 {
709 CALL_DEBUG_ENTER;
710 int32_t id = touchEvent->id;
711 int32_t screenX = touchEvent->displayX;
712 int32_t screenY = touchEvent->displayY;
713 if (screenX < 0 || screenY < 0) {
714 MMI_HILOGE("touch parameter is less 0, can not process");
715 return INPUT_PARAMETER_ERROR;
716 }
717 item.SetDisplayX(screenX);
718 item.SetDisplayY(screenY);
719 item.SetPointerId(id);
720 g_touchEvent->SetPointerId(id);
721 g_touchEvent->SetSourceType(OHOS::MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
722 if (touchEvent->action == TOUCH_ACTION_DOWN) {
723 g_touchEvent->AddPointerItem(item);
724 } else if ((touchEvent->action == TOUCH_ACTION_MOVE) || (touchEvent->action == TOUCH_ACTION_UP)) {
725 g_touchEvent->UpdatePointerItem(id, item);
726 }
727 return INPUT_SUCCESS;
728 }
729
OH_Input_InjectTouchEvent(const struct Input_TouchEvent * touchEvent)730 int32_t OH_Input_InjectTouchEvent(const struct Input_TouchEvent* touchEvent)
731 {
732 MMI_HILOGI("Input_TouchEvent injectTouchEvent");
733 CHKPR(touchEvent, INPUT_PARAMETER_ERROR);
734 CHKPR(g_touchEvent, INPUT_PARAMETER_ERROR);
735 g_touchEvent->ClearFlag();
736 OHOS::MMI::PointerEvent::PointerItem item;
737 int32_t result = HandleTouchAction(touchEvent, item);
738 if (result != 0) {
739 return INPUT_PARAMETER_ERROR;
740 }
741 result = HandleTouchProperty(touchEvent, item);
742 if (result != 0) {
743 return INPUT_PARAMETER_ERROR;
744 }
745 g_touchEvent->AddFlag(OHOS::MMI::InputEvent::EVENT_FLAG_SIMULATE);
746 OHOS::Singleton<OHOS::MMI::InputManagerImpl>::GetInstance().SimulateInputEvent(g_touchEvent, true);
747 if (touchEvent->action == TOUCH_ACTION_UP) {
748 g_touchEvent->RemovePointerItem(g_touchEvent->GetPointerId());
749 MMI_HILOGD("This touch event is up remove this finger");
750 if (g_touchEvent->GetPointerIds().empty()) {
751 MMI_HILOGD("This touch event is final finger up remove this finger");
752 g_touchEvent->Reset();
753 }
754 }
755 return INPUT_SUCCESS;
756 }
757
OH_Input_CreateTouchEvent()758 struct Input_TouchEvent* OH_Input_CreateTouchEvent()
759 {
760 CALL_DEBUG_ENTER;
761 Input_TouchEvent* touchEvent = new (std::nothrow) Input_TouchEvent();
762 CHKPL(touchEvent);
763 return touchEvent;
764 }
765
OH_Input_DestroyTouchEvent(struct Input_TouchEvent ** touchEvent)766 void OH_Input_DestroyTouchEvent(struct Input_TouchEvent** touchEvent)
767 {
768 CALL_DEBUG_ENTER;
769 CHKPV(touchEvent);
770 CHKPV(*touchEvent);
771 delete *touchEvent;
772 *touchEvent = nullptr;
773 }
774
OH_Input_SetTouchEventAction(struct Input_TouchEvent * touchEvent,int32_t action)775 void OH_Input_SetTouchEventAction(struct Input_TouchEvent* touchEvent, int32_t action)
776 {
777 CALL_DEBUG_ENTER;
778 CHKPV(touchEvent);
779 touchEvent->action = action;
780 }
781
OH_Input_GetTouchEventAction(const struct Input_TouchEvent * touchEvent)782 int32_t OH_Input_GetTouchEventAction(const struct Input_TouchEvent* touchEvent)
783 {
784 CALL_DEBUG_ENTER;
785 CHKPR(touchEvent, RET_ERR);
786 return touchEvent->action;
787 }
788
OH_Input_SetTouchEventFingerId(struct Input_TouchEvent * touchEvent,int32_t id)789 void OH_Input_SetTouchEventFingerId(struct Input_TouchEvent* touchEvent, int32_t id)
790 {
791 CALL_DEBUG_ENTER;
792 CHKPV(touchEvent);
793 touchEvent->id = id;
794 }
795
OH_Input_GetTouchEventFingerId(const struct Input_TouchEvent * touchEvent)796 int32_t OH_Input_GetTouchEventFingerId(const struct Input_TouchEvent* touchEvent)
797 {
798 CALL_DEBUG_ENTER;
799 CHKPR(touchEvent, RET_ERR);
800 return touchEvent->id;
801 }
802
OH_Input_SetTouchEventDisplayX(struct Input_TouchEvent * touchEvent,int32_t displayX)803 void OH_Input_SetTouchEventDisplayX(struct Input_TouchEvent* touchEvent, int32_t displayX)
804 {
805 CALL_DEBUG_ENTER;
806 CHKPV(touchEvent);
807 touchEvent->displayX = displayX;
808 }
809
OH_Input_GetTouchEventDisplayX(const struct Input_TouchEvent * touchEvent)810 int32_t OH_Input_GetTouchEventDisplayX(const struct Input_TouchEvent* touchEvent)
811 {
812 CALL_DEBUG_ENTER;
813 CHKPR(touchEvent, RET_ERR);
814 return touchEvent->displayX;
815 }
816
OH_Input_SetTouchEventDisplayY(struct Input_TouchEvent * touchEvent,int32_t displayY)817 void OH_Input_SetTouchEventDisplayY(struct Input_TouchEvent* touchEvent, int32_t displayY)
818 {
819 CALL_DEBUG_ENTER;
820 CHKPV(touchEvent);
821 touchEvent->displayY = displayY;
822 }
823
OH_Input_GetTouchEventDisplayY(const struct Input_TouchEvent * touchEvent)824 int32_t OH_Input_GetTouchEventDisplayY(const struct Input_TouchEvent* touchEvent)
825 {
826 CALL_DEBUG_ENTER;
827 CHKPR(touchEvent, RET_ERR);
828 return touchEvent->displayY;
829 }
830
OH_Input_SetTouchEventActionTime(struct Input_TouchEvent * touchEvent,int64_t actionTime)831 void OH_Input_SetTouchEventActionTime(struct Input_TouchEvent* touchEvent, int64_t actionTime)
832 {
833 CALL_DEBUG_ENTER;
834 CHKPV(touchEvent);
835 touchEvent->actionTime = actionTime;
836 }
837
OH_Input_GetTouchEventActionTime(const struct Input_TouchEvent * touchEvent)838 int64_t OH_Input_GetTouchEventActionTime(const struct Input_TouchEvent* touchEvent)
839 {
840 CALL_DEBUG_ENTER;
841 CHKPR(touchEvent, RET_ERR);
842 return touchEvent->actionTime;
843 }
844
OH_Input_SetTouchEventWindowId(struct Input_TouchEvent * touchEvent,int32_t windowId)845 void OH_Input_SetTouchEventWindowId(struct Input_TouchEvent* touchEvent, int32_t windowId)
846 {
847 CALL_DEBUG_ENTER;
848 CHKPV(touchEvent);
849 touchEvent->windowId = windowId;
850 }
851
OH_Input_GetTouchEventWindowId(const struct Input_TouchEvent * touchEvent)852 int32_t OH_Input_GetTouchEventWindowId(const struct Input_TouchEvent* touchEvent)
853 {
854 CALL_DEBUG_ENTER;
855 CHKPR(touchEvent, RET_ERR);
856 return touchEvent->windowId;
857 }
858
OH_Input_SetTouchEventDisplayId(struct Input_TouchEvent * touchEvent,int32_t displayId)859 void OH_Input_SetTouchEventDisplayId(struct Input_TouchEvent* touchEvent, int32_t displayId)
860 {
861 CALL_DEBUG_ENTER;
862 CHKPV(touchEvent);
863 touchEvent->displayId = displayId;
864 }
865
OH_Input_GetTouchEventDisplayId(const struct Input_TouchEvent * touchEvent)866 int32_t OH_Input_GetTouchEventDisplayId(const struct Input_TouchEvent* touchEvent)
867 {
868 CALL_DEBUG_ENTER;
869 CHKPR(touchEvent, RET_ERR);
870 return touchEvent->displayId;
871 }
872
OH_Input_CancelInjection()873 void OH_Input_CancelInjection()
874 {
875 CALL_DEBUG_ENTER;
876 OHOS::Singleton<OHOS::MMI::InputManagerImpl>::GetInstance().CancelInjection();
877 }
878
SetAxisValueByAxisEventType(std::shared_ptr<OHOS::MMI::PointerEvent> event,struct Input_AxisEvent * axisEvent,int32_t axisEventType)879 static bool SetAxisValueByAxisEventType(std::shared_ptr<OHOS::MMI::PointerEvent> event,
880 struct Input_AxisEvent *axisEvent, int32_t axisEventType)
881 {
882 CHKPR(event, false);
883 CHKPR(axisEvent, false);
884 if (axisEventType == OHOS::MMI::PointerEvent::AXIS_EVENT_TYPE_PINCH) {
885 double value = event->GetAxisValue(OHOS::MMI::PointerEvent::AXIS_TYPE_PINCH);
886 axisEvent->axisValues.insert(std::make_pair(AXIS_TYPE_PINCH, value));
887 value = event->GetAxisValue(OHOS::MMI::PointerEvent::AXIS_TYPE_ROTATE);
888 axisEvent->axisValues.insert(std::make_pair(AXIS_TYPE_ROTATE, value));
889 } else if (axisEventType == OHOS::MMI::PointerEvent::AXIS_EVENT_TYPE_SCROLL) {
890 double value = event->GetAxisValue(OHOS::MMI::PointerEvent::AXIS_TYPE_SCROLL_VERTICAL);
891 axisEvent->axisValues.insert(std::make_pair(AXIS_TYPE_SCROLL_VERTICAL, value));
892 value = event->GetAxisValue(OHOS::MMI::PointerEvent::AXIS_TYPE_SCROLL_HORIZONTAL);
893 axisEvent->axisValues.insert(std::make_pair(AXIS_TYPE_SCROLL_HORIZONTAL, value));
894 } else {
895 MMI_HILOGE("Undefined axisEventType: %{public}d", axisEventType);
896 return false;
897 }
898 axisEvent->axisEventType = axisEventType;
899 return true;
900 }
901
IsAxisEvent(int32_t action)902 static bool IsAxisEvent(int32_t action)
903 {
904 if (action != OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_BEGIN &&
905 action != OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_UPDATE &&
906 action != OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_END) {
907 return false;
908 }
909 return true;
910 }
911
OH_Input_CreateAxisEvent(void)912 Input_AxisEvent* OH_Input_CreateAxisEvent(void)
913 {
914 Input_AxisEvent* axisEvent = new (std::nothrow) Input_AxisEvent();
915 CHKPP(axisEvent);
916 return axisEvent;
917 }
918
OH_Input_DestroyAxisEvent(Input_AxisEvent ** axisEvent)919 Input_Result OH_Input_DestroyAxisEvent(Input_AxisEvent** axisEvent)
920 {
921 CALL_DEBUG_ENTER;
922 if (axisEvent == nullptr || *axisEvent == nullptr) {
923 return INPUT_PARAMETER_ERROR;
924 }
925 delete *axisEvent;
926 *axisEvent = nullptr;
927 return INPUT_SUCCESS;
928 }
929
OH_Input_SetAxisEventAction(Input_AxisEvent * axisEvent,InputEvent_AxisAction action)930 Input_Result OH_Input_SetAxisEventAction(Input_AxisEvent* axisEvent, InputEvent_AxisAction action)
931 {
932 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
933 axisEvent->axisAction = action;
934 return INPUT_SUCCESS;
935 }
936
OH_Input_GetAxisEventAction(const Input_AxisEvent * axisEvent,InputEvent_AxisAction * action)937 Input_Result OH_Input_GetAxisEventAction(const Input_AxisEvent* axisEvent, InputEvent_AxisAction *action)
938 {
939 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
940 CHKPR(action, INPUT_PARAMETER_ERROR);
941 *action = InputEvent_AxisAction(axisEvent->axisAction);
942 return INPUT_SUCCESS;
943 }
944
OH_Input_SetAxisEventDisplayX(Input_AxisEvent * axisEvent,float displayX)945 Input_Result OH_Input_SetAxisEventDisplayX(Input_AxisEvent* axisEvent, float displayX)
946 {
947 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
948 axisEvent->displayX = displayX;
949 return INPUT_SUCCESS;
950 }
951
OH_Input_GetAxisEventDisplayX(const Input_AxisEvent * axisEvent,float * displayX)952 Input_Result OH_Input_GetAxisEventDisplayX(const Input_AxisEvent* axisEvent, float* displayX)
953 {
954 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
955 CHKPR(displayX, INPUT_PARAMETER_ERROR);
956 *displayX = axisEvent->displayX;
957 return INPUT_SUCCESS;
958 }
959
OH_Input_SetAxisEventDisplayY(Input_AxisEvent * axisEvent,float displayY)960 Input_Result OH_Input_SetAxisEventDisplayY(Input_AxisEvent* axisEvent, float displayY)
961 {
962 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
963 axisEvent->displayY = displayY;
964 return INPUT_SUCCESS;
965 }
966
OH_Input_GetAxisEventDisplayY(const Input_AxisEvent * axisEvent,float * displayY)967 Input_Result OH_Input_GetAxisEventDisplayY(const Input_AxisEvent* axisEvent, float* displayY)
968 {
969 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
970 CHKPR(displayY, INPUT_PARAMETER_ERROR);
971 *displayY = axisEvent->displayY;
972 return INPUT_SUCCESS;
973 }
974
OH_Input_SetAxisEventAxisValue(Input_AxisEvent * axisEvent,InputEvent_AxisType axisType,double axisValue)975 Input_Result OH_Input_SetAxisEventAxisValue(Input_AxisEvent* axisEvent,
976 InputEvent_AxisType axisType, double axisValue)
977 {
978 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
979 axisEvent->axisValues.emplace(axisType, axisValue);
980 return INPUT_SUCCESS;
981 }
982
OH_Input_GetAxisEventAxisValue(const Input_AxisEvent * axisEvent,InputEvent_AxisType axisType,double * axisValue)983 Input_Result OH_Input_GetAxisEventAxisValue(const Input_AxisEvent* axisEvent,
984 InputEvent_AxisType axisType, double* axisValue)
985 {
986 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
987 CHKPR(axisValue, INPUT_PARAMETER_ERROR);
988 auto it = axisEvent->axisValues.find(axisType);
989 if (it == axisEvent->axisValues.end()) {
990 MMI_HILOGE("There is no axis value of axisType: %{public}d in the axisEvent", axisType);
991 return INPUT_PARAMETER_ERROR;
992 }
993 *axisValue = it->second;
994 return INPUT_SUCCESS;
995 }
996
OH_Input_SetAxisEventActionTime(Input_AxisEvent * axisEvent,int64_t actionTime)997 Input_Result OH_Input_SetAxisEventActionTime(Input_AxisEvent* axisEvent, int64_t actionTime)
998 {
999 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
1000 axisEvent->actionTime = actionTime;
1001 return INPUT_SUCCESS;
1002 }
1003
OH_Input_GetAxisEventActionTime(const Input_AxisEvent * axisEvent,int64_t * actionTime)1004 Input_Result OH_Input_GetAxisEventActionTime(const Input_AxisEvent* axisEvent, int64_t* actionTime)
1005 {
1006 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
1007 CHKPR(actionTime, INPUT_PARAMETER_ERROR);
1008 *actionTime = axisEvent->actionTime;
1009 return INPUT_SUCCESS;
1010 }
1011
OH_Input_SetAxisEventType(Input_AxisEvent * axisEvent,InputEvent_AxisEventType axisEventType)1012 Input_Result OH_Input_SetAxisEventType(Input_AxisEvent* axisEvent, InputEvent_AxisEventType axisEventType)
1013 {
1014 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
1015 axisEvent->axisEventType = axisEventType;
1016 return INPUT_SUCCESS;
1017 }
1018
OH_Input_GetAxisEventType(const Input_AxisEvent * axisEvent,InputEvent_AxisEventType * axisEventType)1019 Input_Result OH_Input_GetAxisEventType(const Input_AxisEvent* axisEvent, InputEvent_AxisEventType* axisEventType)
1020 {
1021 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
1022 CHKPR(axisEventType, INPUT_PARAMETER_ERROR);
1023 *axisEventType = InputEvent_AxisEventType(axisEvent->axisEventType);
1024 return INPUT_SUCCESS;
1025 }
1026
OH_Input_SetAxisEventSourceType(Input_AxisEvent * axisEvent,InputEvent_SourceType sourceType)1027 Input_Result OH_Input_SetAxisEventSourceType(Input_AxisEvent* axisEvent, InputEvent_SourceType sourceType)
1028 {
1029 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
1030 axisEvent->sourceType = sourceType;
1031 return INPUT_SUCCESS;
1032 }
1033
OH_Input_GetAxisEventSourceType(const Input_AxisEvent * axisEvent,InputEvent_SourceType * sourceType)1034 Input_Result OH_Input_GetAxisEventSourceType(const Input_AxisEvent* axisEvent, InputEvent_SourceType* sourceType)
1035 {
1036 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
1037 CHKPR(sourceType, INPUT_PARAMETER_ERROR);
1038 *sourceType = InputEvent_SourceType(axisEvent->sourceType);
1039 return INPUT_SUCCESS;
1040 }
1041
OH_Input_SetAxisEventWindowId(Input_AxisEvent * axisEvent,int32_t windowId)1042 Input_Result OH_Input_SetAxisEventWindowId(Input_AxisEvent* axisEvent, int32_t windowId)
1043 {
1044 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
1045 axisEvent->windowId = windowId;
1046 return INPUT_SUCCESS;
1047 }
1048
OH_Input_GetAxisEventWindowId(const Input_AxisEvent * axisEvent,int32_t * windowId)1049 Input_Result OH_Input_GetAxisEventWindowId(const Input_AxisEvent* axisEvent, int32_t* windowId)
1050 {
1051 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
1052 CHKPR(windowId, INPUT_PARAMETER_ERROR);
1053 *windowId = axisEvent->windowId;
1054 return INPUT_SUCCESS;
1055 }
1056
OH_Input_SetAxisEventDisplayId(Input_AxisEvent * axisEvent,int32_t displayId)1057 Input_Result OH_Input_SetAxisEventDisplayId(Input_AxisEvent* axisEvent, int32_t displayId)
1058 {
1059 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
1060 axisEvent->displayId = displayId;
1061 return INPUT_SUCCESS;
1062 }
1063
OH_Input_GetAxisEventDisplayId(const Input_AxisEvent * axisEvent,int32_t * displayId)1064 Input_Result OH_Input_GetAxisEventDisplayId(const Input_AxisEvent* axisEvent, int32_t* displayId)
1065 {
1066 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
1067 CHKPR(displayId, INPUT_PARAMETER_ERROR);
1068 *displayId = axisEvent->displayId;
1069 return INPUT_SUCCESS;
1070 }
1071
NormalizeResult(int32_t result)1072 static Input_Result NormalizeResult(int32_t result)
1073 {
1074 if (result < RET_OK) {
1075 if (result == OHOS::MMI::ERROR_NO_PERMISSION) {
1076 MMI_HILOGE("Permisson denied");
1077 return INPUT_PERMISSION_DENIED;
1078 }
1079 return INPUT_SERVICE_EXCEPTION;
1080 }
1081 return INPUT_SUCCESS;
1082 }
1083
SetKeyEventAction(Input_KeyEvent * keyEvent,int32_t action)1084 static bool SetKeyEventAction(Input_KeyEvent* keyEvent, int32_t action)
1085 {
1086 CHKPF(keyEvent);
1087 if (action == OHOS::MMI::KeyEvent::KEY_ACTION_CANCEL) {
1088 keyEvent->action = KEY_ACTION_CANCEL;
1089 } else if (action == OHOS::MMI::KeyEvent::KEY_ACTION_DOWN) {
1090 keyEvent->action = KEY_ACTION_DOWN;
1091 } else if (action == OHOS::MMI::KeyEvent::KEY_ACTION_UP) {
1092 keyEvent->action = KEY_ACTION_UP;
1093 } else {
1094 MMI_HILOGE("Invalid key event action");
1095 return false;
1096 }
1097 return true;
1098 }
1099
KeyEventMonitorCallback(std::shared_ptr<OHOS::MMI::KeyEvent> event)1100 static void KeyEventMonitorCallback(std::shared_ptr<OHOS::MMI::KeyEvent> event)
1101 {
1102 CHKPV(event);
1103 Input_KeyEvent* keyEvent = OH_Input_CreateKeyEvent();
1104 CHKPV(keyEvent);
1105 if (!SetKeyEventAction(keyEvent, event->GetKeyAction())) {
1106 OH_Input_DestroyKeyEvent(&keyEvent);
1107 return;
1108 }
1109 keyEvent->keyCode = event->GetKeyCode();
1110 keyEvent->actionTime = event->GetActionTime();
1111 keyEvent->windowId = event->GetTargetWindowId();
1112 keyEvent->displayId = event->GetTargetDisplayId();
1113 std::lock_guard guard(g_mutex);
1114 for (auto &callback : g_keyMonitorCallbacks) {
1115 callback(keyEvent);
1116 }
1117 OH_Input_DestroyKeyEvent(&keyEvent);
1118 }
1119
IsScreenCaptureWorking()1120 static bool IsScreenCaptureWorking()
1121 {
1122 CALL_DEBUG_ENTER;
1123 #ifdef PLAYER_FRAMEWORK_EXISTS
1124 int32_t pid = OHOS::IPCSkeleton::GetCallingPid();
1125 int32_t capturePid = OHOS::Media::ScreenCaptureMonitor::GetInstance()->IsScreenCaptureWorking();
1126 if (capturePid != pid) {
1127 MMI_HILOGE("Calling pid is: %{public}d, but screen capture pid is: %{public}d", pid, capturePid);
1128 return false;
1129 }
1130 return true;
1131 #else
1132 return false;
1133 #endif // PLAYER_FRAMEWORK_EXISTS
1134 }
1135
OH_Input_AddKeyEventMonitor(Input_KeyEventCallback callback)1136 Input_Result OH_Input_AddKeyEventMonitor(Input_KeyEventCallback callback)
1137 {
1138 CALL_DEBUG_ENTER;
1139 CHKPR(callback, INPUT_PARAMETER_ERROR);
1140 if (!OHOS::MMI::PermissionHelper::GetInstance()->VerifySystemApp()) {
1141 if (!IsScreenCaptureWorking()) {
1142 MMI_HILOGE("The screen capture is not working");
1143 return INPUT_PERMISSION_DENIED;
1144 }
1145 }
1146 Input_Result retCode = INPUT_SUCCESS;
1147 std::lock_guard guard(g_mutex);
1148 if (g_keyMonitorId == INVALID_MONITOR_ID) {
1149 int32_t ret = OHOS::Singleton<OHOS::MMI::InputManagerImpl>::GetInstance().AddMonitor(KeyEventMonitorCallback);
1150 retCode = NormalizeResult(ret);
1151 if (retCode != INPUT_SUCCESS) {
1152 return retCode;
1153 }
1154 g_keyMonitorId = ret;
1155 }
1156 g_keyMonitorCallbacks.insert(callback);
1157 return retCode;
1158 }
1159
SetTouchEventAction(Input_TouchEvent * touchEvent,int32_t action)1160 static bool SetTouchEventAction(Input_TouchEvent* touchEvent, int32_t action)
1161 {
1162 CHKPF(touchEvent);
1163 switch (action) {
1164 case OHOS::MMI::PointerEvent::POINTER_ACTION_CANCEL:
1165 touchEvent->action = TOUCH_ACTION_CANCEL;
1166 break;
1167 case OHOS::MMI::PointerEvent::POINTER_ACTION_DOWN:
1168 touchEvent->action = TOUCH_ACTION_DOWN;
1169 break;
1170 case OHOS::MMI::PointerEvent::POINTER_ACTION_MOVE:
1171 touchEvent->action = TOUCH_ACTION_MOVE;
1172 break;
1173 case OHOS::MMI::PointerEvent::POINTER_ACTION_UP:
1174 touchEvent->action = TOUCH_ACTION_UP;
1175 break;
1176 default:
1177 MMI_HILOGE("Invalid touch event action");
1178 return false;
1179 }
1180 return true;
1181 }
1182
TouchEventMonitorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)1183 static void TouchEventMonitorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)
1184 {
1185 CHKPV(event);
1186 Input_TouchEvent* touchEvent = OH_Input_CreateTouchEvent();
1187 CHKPV(touchEvent);
1188 OHOS::MMI::PointerEvent::PointerItem item;
1189 if (!(event->GetPointerItem(event->GetPointerId(), item))) {
1190 MMI_HILOGE("Can not get pointerItem for the pointer event");
1191 OH_Input_DestroyTouchEvent(&touchEvent);
1192 return;
1193 }
1194 if (!SetTouchEventAction(touchEvent, event->GetPointerAction())) {
1195 OH_Input_DestroyTouchEvent(&touchEvent);
1196 return;
1197 }
1198 touchEvent->id = event->GetPointerId();
1199 touchEvent->displayX = item.GetDisplayX();
1200 touchEvent->displayY = item.GetDisplayY();
1201 touchEvent->actionTime = event->GetActionTime();
1202 touchEvent->windowId = event->GetTargetWindowId();
1203 touchEvent->displayId = event->GetTargetDisplayId();
1204 std::lock_guard guard(g_mutex);
1205 for (auto &callback : g_touchMonitorCallbacks) {
1206 callback(touchEvent);
1207 }
1208 OH_Input_DestroyTouchEvent(&touchEvent);
1209 }
1210
SetMouseEventAction(Input_MouseEvent * mouseEvent,int32_t action)1211 static bool SetMouseEventAction(Input_MouseEvent* mouseEvent, int32_t action)
1212 {
1213 CHKPF(mouseEvent);
1214 switch (action) {
1215 case OHOS::MMI::PointerEvent::POINTER_ACTION_CANCEL:
1216 mouseEvent->action = MOUSE_ACTION_CANCEL;
1217 break;
1218 case OHOS::MMI::PointerEvent::POINTER_ACTION_MOVE:
1219 mouseEvent->action = MOUSE_ACTION_MOVE;
1220 break;
1221 case OHOS::MMI::PointerEvent::POINTER_ACTION_BUTTON_DOWN:
1222 mouseEvent->action = MOUSE_ACTION_BUTTON_DOWN;
1223 break;
1224 case OHOS::MMI::PointerEvent::POINTER_ACTION_BUTTON_UP:
1225 mouseEvent->action = MOUSE_ACTION_BUTTON_UP;
1226 break;
1227 default:
1228 MMI_HILOGE("Invalid mouse event action");
1229 return false;
1230 }
1231 return true;
1232 }
1233
SetMouseEventButton(Input_MouseEvent * mouseEvent,int32_t button)1234 static bool SetMouseEventButton(Input_MouseEvent* mouseEvent, int32_t button)
1235 {
1236 CHKPF(mouseEvent);
1237 switch (button) {
1238 case OHOS::MMI::PointerEvent::BUTTON_NONE:
1239 mouseEvent->button = MOUSE_BUTTON_NONE;
1240 break;
1241 case OHOS::MMI::PointerEvent::MOUSE_BUTTON_LEFT:
1242 mouseEvent->button = MOUSE_BUTTON_LEFT;
1243 break;
1244 case OHOS::MMI::PointerEvent::MOUSE_BUTTON_MIDDLE:
1245 mouseEvent->button = MOUSE_BUTTON_MIDDLE;
1246 break;
1247 case OHOS::MMI::PointerEvent::MOUSE_BUTTON_RIGHT:
1248 mouseEvent->button = MOUSE_BUTTON_RIGHT;
1249 break;
1250 case OHOS::MMI::PointerEvent::MOUSE_BUTTON_FORWARD:
1251 mouseEvent->button = MOUSE_BUTTON_FORWARD;
1252 break;
1253 case OHOS::MMI::PointerEvent::MOUSE_BUTTON_BACK:
1254 mouseEvent->button = MOUSE_BUTTON_BACK;
1255 break;
1256 default:
1257 MMI_HILOGE("Invalid mouse event button");
1258 return false;
1259 }
1260 return true;
1261 }
1262
MouseEventMonitorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)1263 static void MouseEventMonitorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)
1264 {
1265 CHKPV(event);
1266 Input_MouseEvent* mouseEvent = OH_Input_CreateMouseEvent();
1267 CHKPV(mouseEvent);
1268 OHOS::MMI::PointerEvent::PointerItem item;
1269 if (!(event->GetPointerItem(event->GetPointerId(), item))) {
1270 MMI_HILOGE("Can not get pointerItem for the pointer event");
1271 OH_Input_DestroyMouseEvent(&mouseEvent);
1272 return;
1273 }
1274 if (!SetMouseEventAction(mouseEvent, event->GetPointerAction())) {
1275 OH_Input_DestroyMouseEvent(&mouseEvent);
1276 return;
1277 }
1278 if (!SetMouseEventButton(mouseEvent, event->GetButtonId())) {
1279 OH_Input_DestroyMouseEvent(&mouseEvent);
1280 return;
1281 }
1282 mouseEvent->displayX = item.GetDisplayX();
1283 mouseEvent->displayY = item.GetDisplayY();
1284 mouseEvent->actionTime = event->GetActionTime();
1285 mouseEvent->windowId = event->GetTargetWindowId();
1286 mouseEvent->displayId = event->GetTargetDisplayId();
1287 std::lock_guard guard(g_mutex);
1288 for (auto &callback : g_mouseMonitorCallbacks) {
1289 callback(mouseEvent);
1290 }
1291 OH_Input_DestroyMouseEvent(&mouseEvent);
1292 }
1293
SetAxisEventAction(Input_AxisEvent * axisEvent,int32_t action)1294 static void SetAxisEventAction(Input_AxisEvent* axisEvent, int32_t action)
1295 {
1296 CHKPV(axisEvent);
1297 switch (action) {
1298 case OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_BEGIN:
1299 axisEvent->axisAction = AXIS_ACTION_BEGIN;
1300 break;
1301 case OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_UPDATE:
1302 axisEvent->axisAction = AXIS_ACTION_UPDATE;
1303 break;
1304 case OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_END:
1305 axisEvent->axisAction = AXIS_ACTION_END;
1306 break;
1307 default:
1308 break;
1309 }
1310 }
1311
AxisEventMonitorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)1312 static void AxisEventMonitorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)
1313 {
1314 CHKPV(event);
1315 Input_AxisEvent* axisEvent = OH_Input_CreateAxisEvent();
1316 CHKPV(axisEvent);
1317 OHOS::MMI::PointerEvent::PointerItem item;
1318 if (!(event->GetPointerItem(event->GetPointerId(), item))) {
1319 MMI_HILOGE("Can not get pointerItem for the pointer event");
1320 OH_Input_DestroyAxisEvent(&axisEvent);
1321 return;
1322 }
1323 if (!SetAxisValueByAxisEventType(event, axisEvent, event->GetAxisEventType())) {
1324 OH_Input_DestroyAxisEvent(&axisEvent);
1325 return;
1326 }
1327 SetAxisEventAction(axisEvent, event->GetPointerAction());
1328 axisEvent->displayX = item.GetDisplayX();
1329 axisEvent->displayY = item.GetDisplayY();
1330 axisEvent->actionTime = event->GetActionTime();
1331 axisEvent->sourceType = event->GetSourceType();
1332 axisEvent->windowId = event->GetTargetWindowId();
1333 axisEvent->displayId = event->GetTargetDisplayId();
1334 std::lock_guard guard(g_mutex);
1335 for (auto &callback : g_axisMonitorAllCallbacks) {
1336 callback(axisEvent);
1337 }
1338 auto it = g_axisMonitorCallbacks.find(InputEvent_AxisEventType(event->GetAxisEventType()));
1339 if (it != g_axisMonitorCallbacks.end()) {
1340 for (auto &callback : it->second) {
1341 callback(axisEvent);
1342 }
1343 }
1344 OH_Input_DestroyAxisEvent(&axisEvent);
1345 }
1346
PointerEventMonitorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)1347 static void PointerEventMonitorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)
1348 {
1349 CHKPV(event);
1350 if (event->GetSourceType() == SOURCE_TYPE_TOUCHSCREEN) {
1351 TouchEventMonitorCallback(event);
1352 } else if (event->GetSourceType() == SOURCE_TYPE_MOUSE && !IsAxisEvent(event->GetPointerAction())) {
1353 MouseEventMonitorCallback(event);
1354 } else if (IsAxisEvent(event->GetPointerAction()) && event->GetSourceType() != SOURCE_TYPE_TOUCHSCREEN) {
1355 AxisEventMonitorCallback(event);
1356 } else {
1357 MMI_HILOGE("Undefined event type");
1358 }
1359 }
1360
AddPointerEventMonitor()1361 static Input_Result AddPointerEventMonitor()
1362 {
1363 Input_Result retCode = INPUT_SUCCESS;
1364 std::lock_guard guard(g_mutex);
1365 if (g_pointerMonitorId == INVALID_MONITOR_ID) {
1366 int32_t ret = OHOS::Singleton<OHOS::MMI::InputManagerImpl>::GetInstance().AddMonitor(
1367 PointerEventMonitorCallback);
1368 retCode = NormalizeResult(ret);
1369 if (retCode != INPUT_SUCCESS) {
1370 MMI_HILOGE("Add pointer event monitor failed.");
1371 return retCode;
1372 }
1373 g_pointerMonitorId = ret;
1374 }
1375 return retCode;
1376 }
1377
OH_Input_AddMouseEventMonitor(Input_MouseEventCallback callback)1378 Input_Result OH_Input_AddMouseEventMonitor(Input_MouseEventCallback callback)
1379 {
1380 CALL_DEBUG_ENTER;
1381 CHKPR(callback, INPUT_PARAMETER_ERROR);
1382 if (!OHOS::MMI::PermissionHelper::GetInstance()->VerifySystemApp()) {
1383 if (!IsScreenCaptureWorking()) {
1384 MMI_HILOGE("The screen capture is not working");
1385 return INPUT_PERMISSION_DENIED;
1386 }
1387 }
1388 Input_Result ret = AddPointerEventMonitor();
1389 if (ret != INPUT_SUCCESS) {
1390 return ret;
1391 }
1392 std::lock_guard guard(g_mutex);
1393 g_mouseMonitorCallbacks.insert(callback);
1394 return INPUT_SUCCESS;
1395 }
1396
OH_Input_AddTouchEventMonitor(Input_TouchEventCallback callback)1397 Input_Result OH_Input_AddTouchEventMonitor(Input_TouchEventCallback callback)
1398 {
1399 CALL_DEBUG_ENTER;
1400 CHKPR(callback, INPUT_PARAMETER_ERROR);
1401 if (!OHOS::MMI::PermissionHelper::GetInstance()->VerifySystemApp()) {
1402 if (!IsScreenCaptureWorking()) {
1403 MMI_HILOGE("The screen capture is not working");
1404 return INPUT_PERMISSION_DENIED;
1405 }
1406 }
1407 Input_Result ret = AddPointerEventMonitor();
1408 if (ret != INPUT_SUCCESS) {
1409 return ret;
1410 }
1411 std::lock_guard guard(g_mutex);
1412 g_touchMonitorCallbacks.insert(callback);
1413 return INPUT_SUCCESS;
1414 }
1415
OH_Input_AddAxisEventMonitorForAll(Input_AxisEventCallback callback)1416 Input_Result OH_Input_AddAxisEventMonitorForAll(Input_AxisEventCallback callback)
1417 {
1418 CALL_DEBUG_ENTER;
1419 CHKPR(callback, INPUT_PARAMETER_ERROR);
1420 if (!OHOS::MMI::PermissionHelper::GetInstance()->VerifySystemApp()) {
1421 if (!IsScreenCaptureWorking()) {
1422 MMI_HILOGE("The screen capture is not working");
1423 return INPUT_PERMISSION_DENIED;
1424 }
1425 }
1426 Input_Result ret = AddPointerEventMonitor();
1427 if (ret != INPUT_SUCCESS) {
1428 return ret;
1429 }
1430 std::lock_guard guard(g_mutex);
1431 g_axisMonitorAllCallbacks.insert(callback);
1432 return INPUT_SUCCESS;
1433 }
1434
OH_Input_AddAxisEventMonitor(InputEvent_AxisEventType axisEventType,Input_AxisEventCallback callback)1435 Input_Result OH_Input_AddAxisEventMonitor(InputEvent_AxisEventType axisEventType, Input_AxisEventCallback callback)
1436 {
1437 CALL_DEBUG_ENTER;
1438 CHKPR(callback, INPUT_PARAMETER_ERROR);
1439 if (!OHOS::MMI::PermissionHelper::GetInstance()->VerifySystemApp()) {
1440 if (!IsScreenCaptureWorking()) {
1441 MMI_HILOGE("The screen capture is not working");
1442 return INPUT_PERMISSION_DENIED;
1443 }
1444 }
1445 Input_Result ret = AddPointerEventMonitor();
1446 if (ret != INPUT_SUCCESS) {
1447 return ret;
1448 }
1449 std::lock_guard guard(g_mutex);
1450 auto it = g_axisMonitorCallbacks.find(axisEventType);
1451 if (it == g_axisMonitorCallbacks.end()) {
1452 std::set<Input_AxisEventCallback> callbacks;
1453 callbacks.insert(callback);
1454 g_axisMonitorCallbacks.insert(std::make_pair(axisEventType, callbacks));
1455 } else {
1456 it->second.insert(callback);
1457 }
1458 return INPUT_SUCCESS;
1459 }
1460
OH_Input_RemoveKeyEventMonitor(Input_KeyEventCallback callback)1461 Input_Result OH_Input_RemoveKeyEventMonitor(Input_KeyEventCallback callback)
1462 {
1463 CALL_DEBUG_ENTER;
1464 CHKPR(callback, INPUT_PARAMETER_ERROR);
1465 Input_Result retCode = INPUT_SUCCESS;
1466 std::lock_guard guard(g_mutex);
1467 auto it = g_keyMonitorCallbacks.find(callback);
1468 if (it == g_keyMonitorCallbacks.end()) {
1469 return INPUT_PARAMETER_ERROR;
1470 }
1471 g_keyMonitorCallbacks.erase(it);
1472 if (g_keyMonitorCallbacks.empty()) {
1473 int32_t ret = OHOS::Singleton<OHOS::MMI::InputManagerImpl>::GetInstance().RemoveMonitor(g_keyMonitorId);
1474 retCode = NormalizeResult(ret);
1475 if (retCode != INPUT_SUCCESS) {
1476 return retCode;
1477 }
1478 g_keyMonitorId = INVALID_MONITOR_ID;
1479 }
1480 return retCode;
1481 }
1482
IsNeedRemoveMonitor()1483 static bool IsNeedRemoveMonitor()
1484 {
1485 if (g_mouseMonitorCallbacks.empty() && g_touchMonitorCallbacks.empty() &&
1486 g_axisMonitorCallbacks.empty() && g_axisMonitorAllCallbacks.empty()) {
1487 return true;
1488 }
1489 return false;
1490 }
1491
RemovePointerEventMonitor()1492 static Input_Result RemovePointerEventMonitor()
1493 {
1494 Input_Result retCode = INPUT_SUCCESS;
1495 if (IsNeedRemoveMonitor()) {
1496 int32_t ret = OHOS::Singleton<OHOS::MMI::InputManagerImpl>::GetInstance().RemoveMonitor(g_pointerMonitorId);
1497 retCode = NormalizeResult(ret);
1498 if (retCode != INPUT_SUCCESS) {
1499 return retCode;
1500 }
1501 g_pointerMonitorId = INVALID_MONITOR_ID;
1502 }
1503 return retCode;
1504 }
1505
OH_Input_RemoveMouseEventMonitor(Input_MouseEventCallback callback)1506 Input_Result OH_Input_RemoveMouseEventMonitor(Input_MouseEventCallback callback)
1507 {
1508 CALL_DEBUG_ENTER;
1509 CHKPR(callback, INPUT_PARAMETER_ERROR);
1510 std::lock_guard guard(g_mutex);
1511 auto it = g_mouseMonitorCallbacks.find(callback);
1512 if (it == g_mouseMonitorCallbacks.end()) {
1513 MMI_HILOGE("The callback has not been added.");
1514 return INPUT_PARAMETER_ERROR;
1515 }
1516 g_mouseMonitorCallbacks.erase(it);
1517 return RemovePointerEventMonitor();
1518 }
1519
OH_Input_RemoveTouchEventMonitor(Input_TouchEventCallback callback)1520 Input_Result OH_Input_RemoveTouchEventMonitor(Input_TouchEventCallback callback)
1521 {
1522 CALL_DEBUG_ENTER;
1523 CHKPR(callback, INPUT_PARAMETER_ERROR);
1524 std::lock_guard guard(g_mutex);
1525 auto it = g_touchMonitorCallbacks.find(callback);
1526 if (it == g_touchMonitorCallbacks.end()) {
1527 MMI_HILOGE("The callback has not been added.");
1528 return INPUT_PARAMETER_ERROR;
1529 }
1530 g_touchMonitorCallbacks.erase(it);
1531 return RemovePointerEventMonitor();
1532 }
1533
OH_Input_RemoveAxisEventMonitorForAll(Input_AxisEventCallback callback)1534 Input_Result OH_Input_RemoveAxisEventMonitorForAll(Input_AxisEventCallback callback)
1535 {
1536 CALL_DEBUG_ENTER;
1537 CHKPR(callback, INPUT_PARAMETER_ERROR);
1538 std::lock_guard guard(g_mutex);
1539 auto it = g_axisMonitorAllCallbacks.find(callback);
1540 if (it == g_axisMonitorAllCallbacks.end()) {
1541 MMI_HILOGE("The callback has not been added.");
1542 return INPUT_PARAMETER_ERROR;
1543 }
1544 g_axisMonitorAllCallbacks.erase(it);
1545 return RemovePointerEventMonitor();
1546 }
1547
OH_Input_RemoveAxisEventMonitor(InputEvent_AxisEventType axisEventType,Input_AxisEventCallback callback)1548 Input_Result OH_Input_RemoveAxisEventMonitor(InputEvent_AxisEventType axisEventType, Input_AxisEventCallback callback)
1549 {
1550 CALL_DEBUG_ENTER;
1551 CHKPR(callback, INPUT_PARAMETER_ERROR);
1552 std::lock_guard guard(g_mutex);
1553 if (g_axisMonitorCallbacks.find(axisEventType) == g_axisMonitorCallbacks.end()) {
1554 MMI_HILOGE("The axis event type has not been added.");
1555 return INPUT_PARAMETER_ERROR;
1556 }
1557 auto it = g_axisMonitorCallbacks[axisEventType].find(callback);
1558 if (it == g_axisMonitorCallbacks[axisEventType].end()) {
1559 MMI_HILOGE("The callback has not been added.");
1560 return INPUT_PARAMETER_ERROR;
1561 }
1562 g_axisMonitorCallbacks[axisEventType].erase(it);
1563 if (g_axisMonitorCallbacks[axisEventType].empty()) {
1564 g_axisMonitorCallbacks.erase(axisEventType);
1565 }
1566 return RemovePointerEventMonitor();
1567 }
1568
KeyEventInterceptorCallback(std::shared_ptr<OHOS::MMI::KeyEvent> event)1569 static void KeyEventInterceptorCallback(std::shared_ptr<OHOS::MMI::KeyEvent> event)
1570 {
1571 CHKPV(event);
1572 Input_KeyEvent* keyEvent = OH_Input_CreateKeyEvent();
1573 CHKPV(keyEvent);
1574 if (!SetKeyEventAction(keyEvent, event->GetKeyAction())) {
1575 OH_Input_DestroyKeyEvent(&keyEvent);
1576 return;
1577 }
1578 keyEvent->keyCode = event->GetKeyCode();
1579 keyEvent->actionTime = event->GetActionTime();
1580 keyEvent->windowId = event->GetTargetWindowId();
1581 keyEvent->displayId = event->GetTargetDisplayId();
1582 std::lock_guard guard(g_mutex);
1583 if (g_keyInterceptorCallback != nullptr) {
1584 g_keyInterceptorCallback(keyEvent);
1585 }
1586 OH_Input_DestroyKeyEvent(&keyEvent);
1587 }
1588
OH_Input_AddKeyEventInterceptor(Input_KeyEventCallback callback,Input_InterceptorOptions * option)1589 Input_Result OH_Input_AddKeyEventInterceptor(Input_KeyEventCallback callback, Input_InterceptorOptions *option)
1590 {
1591 CALL_DEBUG_ENTER;
1592 CHKPR(callback, INPUT_PARAMETER_ERROR);
1593 Input_Result retCode = INPUT_SUCCESS;
1594 std::lock_guard guard(g_mutex);
1595 if (g_keyInterceptorId != INVALID_INTERCEPTOR_ID) {
1596 MMI_HILOGE("Another key event interceptor has been added");
1597 return INPUT_REPEAT_INTERCEPTOR;
1598 }
1599 g_keyInterceptor->SetCallback(KeyEventInterceptorCallback);
1600 int32_t ret = g_keyInterceptor->Start(OHOS::MMI::INTERCEPTOR_TYPE_KEY);
1601 retCode = NormalizeResult(ret);
1602 if (retCode != INPUT_SUCCESS) {
1603 MMI_HILOGE("Add key event interceptor failed.");
1604 return retCode;
1605 }
1606 g_keyInterceptorId = ret;
1607 g_keyInterceptorCallback = callback;
1608 return retCode;
1609 }
1610
TouchEventInterceptorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)1611 static void TouchEventInterceptorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)
1612 {
1613 CHKPV(event);
1614 std::lock_guard guard(g_mutex);
1615 CHKPV(g_pointerInterceptorCallback);
1616 if (g_pointerInterceptorCallback->touchCallback == nullptr) {
1617 MMI_HILOGE("There is no callback for mouse event interceptor");
1618 return;
1619 }
1620 Input_TouchEvent* touchEvent = OH_Input_CreateTouchEvent();
1621 CHKPV(touchEvent);
1622 OHOS::MMI::PointerEvent::PointerItem item;
1623 if (!(event->GetPointerItem(event->GetPointerId(), item))) {
1624 MMI_HILOGE("Can not get pointerItem for the pointer event");
1625 OH_Input_DestroyTouchEvent(&touchEvent);
1626 return;
1627 }
1628 if (!SetTouchEventAction(touchEvent, event->GetPointerAction())) {
1629 OH_Input_DestroyTouchEvent(&touchEvent);
1630 return;
1631 }
1632 touchEvent->id = event->GetPointerId();
1633 touchEvent->displayX = item.GetDisplayX();
1634 touchEvent->displayY = item.GetDisplayY();
1635 touchEvent->actionTime = event->GetActionTime();
1636 touchEvent->windowId = event->GetTargetWindowId();
1637 touchEvent->displayId = event->GetTargetDisplayId();
1638 g_pointerInterceptorCallback->touchCallback(touchEvent);
1639 OH_Input_DestroyTouchEvent(&touchEvent);
1640 }
1641
MouseEventInterceptorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)1642 static void MouseEventInterceptorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)
1643 {
1644 CHKPV(event);
1645 std::lock_guard guard(g_mutex);
1646 CHKPV(g_pointerInterceptorCallback);
1647 if (g_pointerInterceptorCallback->mouseCallback == nullptr) {
1648 MMI_HILOGE("There is no callback for mouse event interceptor");
1649 return;
1650 }
1651 Input_MouseEvent* mouseEvent = OH_Input_CreateMouseEvent();
1652 CHKPV(mouseEvent);
1653 OHOS::MMI::PointerEvent::PointerItem item;
1654 if (!(event->GetPointerItem(event->GetPointerId(), item))) {
1655 MMI_HILOGE("Can not get pointerItem for the pointer event");
1656 OH_Input_DestroyMouseEvent(&mouseEvent);
1657 return;
1658 }
1659 if (!SetMouseEventAction(mouseEvent, event->GetPointerAction())) {
1660 OH_Input_DestroyMouseEvent(&mouseEvent);
1661 return;
1662 }
1663 if (!SetMouseEventButton(mouseEvent, event->GetButtonId())) {
1664 OH_Input_DestroyMouseEvent(&mouseEvent);
1665 return;
1666 }
1667 mouseEvent->displayX = item.GetDisplayX();
1668 mouseEvent->displayY = item.GetDisplayY();
1669 mouseEvent->actionTime = event->GetActionTime();
1670 mouseEvent->windowId = event->GetTargetWindowId();
1671 mouseEvent->displayId = event->GetTargetDisplayId();
1672 g_pointerInterceptorCallback->mouseCallback(mouseEvent);
1673 OH_Input_DestroyMouseEvent(&mouseEvent);
1674 }
1675
AxisEventInterceptorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)1676 static void AxisEventInterceptorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)
1677 {
1678 CHKPV(event);
1679 std::lock_guard guard(g_mutex);
1680 CHKPV(g_pointerInterceptorCallback);
1681 if (g_pointerInterceptorCallback->axisCallback == nullptr) {
1682 MMI_HILOGE("There is no callback for axis event interceptor");
1683 return;
1684 }
1685 Input_AxisEvent* axisEvent = OH_Input_CreateAxisEvent();
1686 CHKPV(axisEvent);
1687 OHOS::MMI::PointerEvent::PointerItem item;
1688 if (!(event->GetPointerItem(event->GetPointerId(), item))) {
1689 MMI_HILOGE("Can not get pointerItem for the pointer event");
1690 OH_Input_DestroyAxisEvent(&axisEvent);
1691 return;
1692 }
1693 if (!SetAxisValueByAxisEventType(event, axisEvent, event->GetAxisEventType())) {
1694 MMI_HILOGE("Fail to set axis value");
1695 OH_Input_DestroyAxisEvent(&axisEvent);
1696 return;
1697 }
1698 SetAxisEventAction(axisEvent, event->GetPointerAction());
1699 axisEvent->displayX = item.GetDisplayX();
1700 axisEvent->displayY = item.GetDisplayY();
1701 axisEvent->actionTime = event->GetActionTime();
1702 axisEvent->sourceType = event->GetSourceType();
1703 axisEvent->windowId = event->GetTargetWindowId();
1704 axisEvent->displayId = event->GetTargetDisplayId();
1705 g_pointerInterceptorCallback->axisCallback(axisEvent);
1706 OH_Input_DestroyAxisEvent(&axisEvent);
1707 }
1708
PointerEventInterceptorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)1709 static void PointerEventInterceptorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)
1710 {
1711 CHKPV(event);
1712 if (event->GetSourceType() == SOURCE_TYPE_TOUCHSCREEN) {
1713 TouchEventInterceptorCallback(event);
1714 } else if (event->GetSourceType() == SOURCE_TYPE_MOUSE && !IsAxisEvent(event->GetPointerAction())) {
1715 MouseEventInterceptorCallback(event);
1716 } else if (IsAxisEvent(event->GetPointerAction()) && event->GetSourceType() != SOURCE_TYPE_TOUCHSCREEN) {
1717 AxisEventInterceptorCallback(event);
1718 } else {
1719 MMI_HILOGE("Undefined event type");
1720 }
1721 }
1722
OH_Input_AddInputEventInterceptor(Input_InterceptorEventCallback * callback,Input_InterceptorOptions * option)1723 Input_Result OH_Input_AddInputEventInterceptor(Input_InterceptorEventCallback *callback,
1724 Input_InterceptorOptions *option)
1725 {
1726 CALL_DEBUG_ENTER;
1727 CHKPR(callback, INPUT_PARAMETER_ERROR);
1728 Input_Result retCode = INPUT_SUCCESS;
1729 std::lock_guard guard(g_mutex);
1730 if (g_pointerInterceptorId != INVALID_INTERCEPTOR_ID) {
1731 MMI_HILOGE("Another interceptor for input event has been added");
1732 return INPUT_REPEAT_INTERCEPTOR;
1733 }
1734 g_pointerInterceptor->SetCallback(PointerEventInterceptorCallback);
1735 int32_t ret = g_pointerInterceptor->Start(OHOS::MMI::INTERCEPTOR_TYPE_POINTER);
1736 retCode = NormalizeResult(ret);
1737 if (retCode != INPUT_SUCCESS) {
1738 MMI_HILOGE("Add pointer event interceptor failed.");
1739 return retCode;
1740 }
1741 g_pointerInterceptorId = ret;
1742 g_pointerInterceptorCallback = callback;
1743 return retCode;
1744 }
1745
OH_Input_RemoveKeyEventInterceptor(void)1746 Input_Result OH_Input_RemoveKeyEventInterceptor(void)
1747 {
1748 CALL_DEBUG_ENTER;
1749 Input_Result retCode = INPUT_SUCCESS;
1750 std::lock_guard guard(g_mutex);
1751 int32_t ret = g_keyInterceptor->Stop(OHOS::MMI::INTERCEPTOR_TYPE_KEY);
1752 retCode = NormalizeResult(ret);
1753 if (retCode != INPUT_SUCCESS) {
1754 MMI_HILOGE("Remove key event interceptor failed.");
1755 return retCode;
1756 }
1757 g_keyInterceptorCallback = nullptr;
1758 g_keyInterceptorId = INVALID_INTERCEPTOR_ID;
1759 return retCode;
1760 }
1761
OH_Input_RemoveInputEventInterceptor(void)1762 Input_Result OH_Input_RemoveInputEventInterceptor(void)
1763 {
1764 CALL_DEBUG_ENTER;
1765 Input_Result retCode = INPUT_SUCCESS;
1766 std::lock_guard guard(g_mutex);
1767 int32_t ret = g_pointerInterceptor->Stop(OHOS::MMI::INTERCEPTOR_TYPE_POINTER);
1768 retCode = NormalizeResult(ret);
1769 if (retCode != INPUT_SUCCESS) {
1770 MMI_HILOGE("Remove pointer event interceptor failed.");
1771 return retCode;
1772 }
1773 g_pointerInterceptorId = INVALID_INTERCEPTOR_ID;
1774 g_pointerInterceptorCallback = nullptr;
1775 return retCode;
1776 }
1777
OH_Input_CreateAllSystemHotkeys(int32_t count)1778 Input_Hotkey **OH_Input_CreateAllSystemHotkeys(int32_t count)
1779 {
1780 return nullptr;
1781 }
1782
OH_Input_DestroyAllSystemHotkeys(Input_Hotkey ** hotkeys,int32_t count)1783 void OH_Input_DestroyAllSystemHotkeys(Input_Hotkey **hotkeys, int32_t count)
1784 {}
1785
OH_Input_GetAllSystemHotkeys(Input_Hotkey ** hotkey,int32_t * count)1786 Input_Result OH_Input_GetAllSystemHotkeys(Input_Hotkey **hotkey, int32_t *count)
1787 {
1788 return INPUT_DEVICE_NOT_SUPPORTED;
1789 }
1790
OH_Input_CreateHotkey(void)1791 Input_Hotkey* OH_Input_CreateHotkey(void)
1792 {
1793 return nullptr;
1794 }
1795
OH_Input_DestroyHotkey(Input_Hotkey ** hotkey)1796 void OH_Input_DestroyHotkey(Input_Hotkey **hotkey)
1797 {}
1798
OH_Input_SetPreKeys(Input_Hotkey * hotkey,int32_t * preKeys,int32_t size)1799 void OH_Input_SetPreKeys(Input_Hotkey *hotkey, int32_t *preKeys, int32_t size)
1800 {}
1801
OH_Input_GetPreKeys(const Input_Hotkey * hotkey,int32_t ** preKeys,int32_t * preKeyCount)1802 Input_Result OH_Input_GetPreKeys(const Input_Hotkey *hotkey, int32_t **preKeys, int32_t *preKeyCount)
1803 {
1804 return INPUT_DEVICE_NOT_SUPPORTED;
1805 }
1806
OH_Input_SetFinalKey(Input_Hotkey * hotkey,int32_t finalKey)1807 void OH_Input_SetFinalKey(Input_Hotkey *hotkey, int32_t finalKey)
1808 {}
1809
OH_Input_GetFinalKey(const Input_Hotkey * hotkey,int32_t * finalKeyCode)1810 Input_Result OH_Input_GetFinalKey(const Input_Hotkey *hotkey, int32_t *finalKeyCode)
1811 {
1812 return INPUT_DEVICE_NOT_SUPPORTED;
1813 }
1814
OH_Input_SetRepeat(Input_Hotkey * hotkey,bool isRepeat)1815 void OH_Input_SetRepeat(Input_Hotkey* hotkey, bool isRepeat)
1816 {}
1817
OH_Input_GetRepeat(const Input_Hotkey * hotkey,bool * isRepeat)1818 Input_Result OH_Input_GetRepeat(const Input_Hotkey* hotkey, bool *isRepeat)
1819 {
1820 return INPUT_DEVICE_NOT_SUPPORTED;
1821 }
1822
OH_Input_AddHotkeyMonitor(const Input_Hotkey * hotkey,Input_HotkeyCallback callback)1823 Input_Result OH_Input_AddHotkeyMonitor(const Input_Hotkey* hotkey, Input_HotkeyCallback callback)
1824 {
1825 return INPUT_DEVICE_NOT_SUPPORTED;
1826 }
1827
OH_Input_RemoveHotkeyMonitor(const Input_Hotkey * hotkey,Input_HotkeyCallback callback)1828 Input_Result OH_Input_RemoveHotkeyMonitor(const Input_Hotkey *hotkey, Input_HotkeyCallback callback)
1829 {
1830 return INPUT_DEVICE_NOT_SUPPORTED;
1831 }
1832
DeviceAddedCallback(int32_t deviceId,const std::string & Type)1833 static void DeviceAddedCallback(int32_t deviceId, const std::string& Type)
1834 {
1835 CALL_DEBUG_ENTER;
1836 std::lock_guard guard(g_DeviceListerCallbackMutex);
1837 for (auto listener : g_ohDeviceListenerList) {
1838 if (listener == nullptr) {
1839 MMI_HILOGE("listener is nullptr");
1840 continue;
1841 }
1842 if (listener->deviceAddedCallback == nullptr) {
1843 MMI_HILOGE("OnDeviceAdded is nullptr");
1844 continue;
1845 }
1846 listener->deviceAddedCallback(deviceId);
1847 }
1848 }
1849
DeviceRemovedCallback(int32_t deviceId,const std::string & Type)1850 static void DeviceRemovedCallback(int32_t deviceId, const std::string& Type)
1851 {
1852 CALL_DEBUG_ENTER;
1853 std::lock_guard guard(g_DeviceListerCallbackMutex);
1854 for (auto listener : g_ohDeviceListenerList) {
1855 if (listener == nullptr) {
1856 MMI_HILOGE("listener is nullptr");
1857 continue;
1858 }
1859 if (listener->deviceRemovedCallback == nullptr) {
1860 MMI_HILOGE("OnDeviceRemoved is nullptr");
1861 continue;
1862 }
1863 listener->deviceRemovedCallback(deviceId);
1864 }
1865 }
1866
OH_Input_RegisterDeviceListener(Input_DeviceListener * listener)1867 Input_Result OH_Input_RegisterDeviceListener(Input_DeviceListener* listener)
1868 {
1869 CALL_DEBUG_ENTER;
1870 if (listener == nullptr || listener->deviceAddedCallback == nullptr ||
1871 listener->deviceRemovedCallback == nullptr) {
1872 MMI_HILOGE("listener or callback is nullptr");
1873 return INPUT_PARAMETER_ERROR;
1874 }
1875 std::lock_guard guard(g_DeviceListerCallbackMutex);
1876 if (g_ohDeviceListenerList.empty()) {
1877 int32_t ret = OHOS::MMI::InputManager::GetInstance()->RegisterDevListener("change", g_deviceListener);
1878 g_deviceListener->SetDeviceAddedCallback(DeviceAddedCallback);
1879 g_deviceListener->SetDeviceRemovedCallback(DeviceRemovedCallback);
1880 if (ret != RET_OK) {
1881 MMI_HILOGE("RegisterDevListener fail");
1882 return INPUT_SERVICE_EXCEPTION;
1883 }
1884 }
1885 g_ohDeviceListenerList.insert(listener);
1886 return INPUT_SUCCESS;
1887 }
1888
OH_Input_UnregisterDeviceListener(Input_DeviceListener * listener)1889 Input_Result OH_Input_UnregisterDeviceListener(Input_DeviceListener* listener)
1890 {
1891 CALL_DEBUG_ENTER;
1892 if (listener == nullptr) {
1893 MMI_HILOGE("listener is nullptr");
1894 return INPUT_PARAMETER_ERROR;
1895 }
1896 std::lock_guard guard(g_DeviceListerCallbackMutex);
1897 auto it = g_ohDeviceListenerList.find(listener);
1898 if (it == g_ohDeviceListenerList.end()) {
1899 MMI_HILOGE("listener not found");
1900 return INPUT_PARAMETER_ERROR;
1901 }
1902 g_ohDeviceListenerList.erase(it);
1903 if (g_ohDeviceListenerList.empty()) {
1904 int32_t ret = OHOS::MMI::InputManager::GetInstance()->UnregisterDevListener("change", g_deviceListener);
1905 if (ret != RET_OK) {
1906 MMI_HILOGE("UnregisterDevListener fail");
1907 return INPUT_SERVICE_EXCEPTION;
1908 }
1909 }
1910 return INPUT_SUCCESS;
1911 }
1912
OH_Input_UnregisterDeviceListeners()1913 Input_Result OH_Input_UnregisterDeviceListeners()
1914 {
1915 CALL_DEBUG_ENTER;
1916 std::lock_guard guard(g_DeviceListerCallbackMutex);
1917 if (g_ohDeviceListenerList.empty()) {
1918 return INPUT_SUCCESS;
1919 }
1920 auto ret = OHOS::MMI::InputManager::GetInstance()->UnregisterDevListener("change", g_deviceListener);
1921 g_ohDeviceListenerList.clear();
1922 if (ret != RET_OK) {
1923 MMI_HILOGE("UnregisterDevListener fail");
1924 return INPUT_SERVICE_EXCEPTION;
1925 }
1926 return INPUT_SUCCESS;
1927 }
1928
OH_Input_GetDeviceIds(int32_t * deviceIds,int32_t inSize,int32_t * outSize)1929 Input_Result OH_Input_GetDeviceIds(int32_t *deviceIds, int32_t inSize, int32_t *outSize)
1930 {
1931 CALL_DEBUG_ENTER;
1932 if (inSize < 0) {
1933 MMI_HILOGE("Invalid inSize:%{public}d", inSize);
1934 return INPUT_PARAMETER_ERROR;
1935 }
1936 CHKPR(deviceIds, INPUT_PARAMETER_ERROR);
1937 CHKPR(outSize, INPUT_PARAMETER_ERROR);
1938 auto nativeCallback = [&](std::vector<int32_t> &ids) {
1939 auto deviceIdslength = static_cast<int32_t>(ids.size());
1940 if (inSize > deviceIdslength) {
1941 *outSize = deviceIdslength;
1942 }
1943 if (inSize < deviceIdslength) {
1944 *outSize = inSize;
1945 }
1946 for (int32_t i = 0; i < *outSize; ++i) {
1947 *(deviceIds + i) = ids[i];
1948 }
1949 };
1950 int32_t ret = OHOS::MMI::InputManager::GetInstance()->GetDeviceIds(nativeCallback);
1951 if (ret != RET_OK) {
1952 MMI_HILOGE("GetDeviceIds fail");
1953 return INPUT_PARAMETER_ERROR;
1954 }
1955 return INPUT_SUCCESS;
1956 }
1957
OH_Input_CreateDeviceInfo(void)1958 Input_DeviceInfo* OH_Input_CreateDeviceInfo(void)
1959 {
1960 CALL_DEBUG_ENTER;
1961 Input_DeviceInfo* deviceInfo = new (std::nothrow) Input_DeviceInfo();
1962 if (deviceInfo == nullptr) {
1963 MMI_HILOGE("deviceInfo is null");
1964 return nullptr;
1965 }
1966 return deviceInfo;
1967 }
1968
OH_Input_DestroyDeviceInfo(Input_DeviceInfo ** deviceInfo)1969 void OH_Input_DestroyDeviceInfo(Input_DeviceInfo **deviceInfo)
1970 {
1971 CALL_DEBUG_ENTER;
1972 CHKPV(deviceInfo);
1973 CHKPV(*deviceInfo);
1974 delete *deviceInfo;
1975 *deviceInfo = nullptr;
1976 }
1977
OH_Input_GetDevice(int32_t deviceId,Input_DeviceInfo ** deviceInfo)1978 Input_Result OH_Input_GetDevice(int32_t deviceId, Input_DeviceInfo **deviceInfo)
1979 {
1980 CALL_DEBUG_ENTER;
1981 if (deviceId < 0) {
1982 MMI_HILOGE("Invalid deviceId:%{public}d", deviceId);
1983 return INPUT_PARAMETER_ERROR;
1984 }
1985 CHKPR(*deviceInfo, INPUT_PARAMETER_ERROR);
1986 CHKPR(deviceInfo, INPUT_PARAMETER_ERROR);
1987 auto nativeCallback = [deviceInfo](std::shared_ptr<OHOS::MMI::InputDevice> device) {
1988 CHKPV(*deviceInfo);
1989 (*deviceInfo)->id = device->GetId();
1990 if (strcpy_s((*deviceInfo)->name, device->GetName().size() + 1, device->GetName().c_str()) != EOK) {
1991 MMI_HILOGE("strcpy_s error");
1992 return;
1993 }
1994 (*deviceInfo)->product = device->GetProduct();
1995 (*deviceInfo)->vendor = device->GetVendor();
1996 (*deviceInfo)->version = device->GetVersion();
1997 if (strcpy_s((*deviceInfo)->phys, device->GetPhys().size() + 1, device->GetPhys().c_str()) != EOK) {
1998 MMI_HILOGE("strcpy_s error");
1999 return;
2000 }
2001 (*deviceInfo)->ability = device->GetType();
2002 };
2003 int32_t ret = OHOS::MMI::InputManager::GetInstance()->GetDevice(deviceId, nativeCallback);
2004 if (ret != RET_OK) {
2005 MMI_HILOGE("GetDevice fail");
2006 return INPUT_PARAMETER_ERROR;
2007 }
2008 return INPUT_SUCCESS;
2009 }
2010
OH_Input_GetKeyboardType(int32_t deviceId,int32_t * KeyboardType)2011 Input_Result OH_Input_GetKeyboardType(int32_t deviceId, int32_t *KeyboardType)
2012 {
2013 CALL_DEBUG_ENTER;
2014 if (deviceId < 0) {
2015 MMI_HILOGE("Invalid deviceId:%{public}d", deviceId);
2016 return INPUT_PARAMETER_ERROR;
2017 }
2018 CHKPR(KeyboardType, INPUT_PARAMETER_ERROR);
2019 auto nativeCallback = [KeyboardType](int32_t keyboardTypes) {
2020 *KeyboardType = keyboardTypes;
2021 };
2022 int32_t ret = OHOS::MMI::InputManager::GetInstance()->GetKeyboardType(deviceId, nativeCallback);
2023 if (ret != RET_OK) {
2024 MMI_HILOGE("GetKeyboardType fail");
2025 return INPUT_PARAMETER_ERROR;
2026 }
2027 return INPUT_SUCCESS;
2028 }
2029
OH_Input_GetDeviceName(Input_DeviceInfo * deviceInfo,char ** name)2030 Input_Result OH_Input_GetDeviceName(Input_DeviceInfo *deviceInfo, char **name)
2031 {
2032 CALL_DEBUG_ENTER;
2033 CHKPR(deviceInfo, INPUT_PARAMETER_ERROR);
2034 CHKPR(name, INPUT_PARAMETER_ERROR);
2035 *name = deviceInfo->name;
2036 return INPUT_SUCCESS;
2037 }
2038
2039
OH_Input_GetDeviceAddress(Input_DeviceInfo * deviceInfo,char ** address)2040 Input_Result OH_Input_GetDeviceAddress(Input_DeviceInfo *deviceInfo, char **address)
2041 {
2042 CALL_DEBUG_ENTER;
2043 CHKPR(deviceInfo, INPUT_PARAMETER_ERROR);
2044 CHKPR(address, INPUT_PARAMETER_ERROR);
2045 *address = deviceInfo->phys;
2046 return INPUT_SUCCESS;
2047 }
2048
OH_Input_GetDeviceId(Input_DeviceInfo * deviceInfo,int32_t * id)2049 Input_Result OH_Input_GetDeviceId(Input_DeviceInfo *deviceInfo, int32_t *id)
2050 {
2051 CALL_DEBUG_ENTER;
2052 CHKPR(deviceInfo, INPUT_PARAMETER_ERROR);
2053 CHKPR(id, INPUT_PARAMETER_ERROR);
2054 *id = deviceInfo->id;
2055 return INPUT_SUCCESS;
2056 }
2057
OH_Input_GetCapabilities(Input_DeviceInfo * deviceInfo,int32_t * capabilities)2058 Input_Result OH_Input_GetCapabilities(Input_DeviceInfo *deviceInfo, int32_t *capabilities)
2059 {
2060 CALL_DEBUG_ENTER;
2061 CHKPR(deviceInfo, INPUT_PARAMETER_ERROR);
2062 CHKPR(capabilities, INPUT_PARAMETER_ERROR);
2063 *capabilities = deviceInfo->ability;
2064 return INPUT_SUCCESS;
2065 }
2066
OH_Input_GetDeviceVersion(Input_DeviceInfo * deviceInfo,int32_t * version)2067 Input_Result OH_Input_GetDeviceVersion(Input_DeviceInfo *deviceInfo, int32_t *version)
2068 {
2069 CALL_DEBUG_ENTER;
2070 CHKPR(deviceInfo, INPUT_PARAMETER_ERROR);
2071 CHKPR(version, INPUT_PARAMETER_ERROR);
2072 *version = deviceInfo->version;
2073 return INPUT_SUCCESS;
2074 }
2075
OH_Input_GetDeviceProduct(Input_DeviceInfo * deviceInfo,int32_t * product)2076 Input_Result OH_Input_GetDeviceProduct(Input_DeviceInfo *deviceInfo, int32_t *product)
2077 {
2078 CALL_DEBUG_ENTER;
2079 CHKPR(deviceInfo, INPUT_PARAMETER_ERROR);
2080 CHKPR(product, INPUT_PARAMETER_ERROR);
2081 *product = deviceInfo->product;
2082 return INPUT_SUCCESS;
2083 }
2084
OH_Input_GetDeviceVendor(Input_DeviceInfo * deviceInfo,int32_t * vendor)2085 Input_Result OH_Input_GetDeviceVendor(Input_DeviceInfo *deviceInfo, int32_t *vendor)
2086 {
2087 CALL_DEBUG_ENTER;
2088 CHKPR(deviceInfo, INPUT_PARAMETER_ERROR);
2089 CHKPR(vendor, INPUT_PARAMETER_ERROR);
2090 *vendor = deviceInfo->vendor;
2091 return INPUT_SUCCESS;
2092 }
2093
OH_Input_GetFunctionKeyState(int32_t keyCode,int32_t * state)2094 Input_Result OH_Input_GetFunctionKeyState(int32_t keyCode, int32_t *state)
2095 {
2096 CALL_DEBUG_ENTER;
2097 if (keyCode < 0 || keyCode != OHOS::MMI::FunctionKey::FUNCTION_KEY_CAPSLOCK) {
2098 MMI_HILOGE("Invalid keycode:%{public}d", keyCode);
2099 return INPUT_PARAMETER_ERROR;
2100 }
2101 CHKPR(state, INPUT_PARAMETER_ERROR);
2102 bool resultState = false;
2103 int32_t napiCode = OHOS::MMI::InputManager::GetInstance()->GetFunctionKeyState(keyCode, resultState);
2104 *state = resultState ? 1 : 0;
2105 if (napiCode == INPUT_DEVICE_NOT_EXIST) {
2106 MMI_HILOGE("GetFunctionKeyState fail, no keyboard device connected");
2107 return INPUT_DEVICE_NOT_EXIST;
2108 }
2109 return INPUT_SUCCESS;
2110 }
2111
OH_Input_GetIntervalSinceLastInput(int64_t * intervalSinceLastInput)2112 int32_t OH_Input_GetIntervalSinceLastInput(int64_t *intervalSinceLastInput)
2113 {
2114 CALL_DEBUG_ENTER;
2115 CHKPR(intervalSinceLastInput, INPUT_PARAMETER_ERROR);
2116 int64_t interval = -1;
2117 int32_t ret = OHOS::MMI::InputManager::GetInstance()->GetIntervalSinceLastInput(interval);
2118 *intervalSinceLastInput = interval;
2119 Input_Result retCode = INPUT_SUCCESS;
2120 retCode = NormalizeResult(ret);
2121 if (retCode != INPUT_SUCCESS) {
2122 MMI_HILOGE("Get Interval Since Last Input failed");
2123 return retCode;
2124 }
2125 return INPUT_SUCCESS;
2126 }