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