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     int32_t pointerId = 10000;
450     g_mouseEvent->GetPointerItem(pointerId, item);
451     item.SetDownTime(time);
452     int32_t result = HandleMouseAction(mouseEvent, item);
453     if (result != 0) {
454         return result;
455     }
456     result = HandleMouseProperty(mouseEvent, item);
457     if (result != 0) {
458         return result;
459     }
460     g_mouseEvent->AddFlag(OHOS::MMI::InputEvent::EVENT_FLAG_SIMULATE);
461     result = OHOS::Singleton<OHOS::MMI::InputManagerImpl>::GetInstance().SimulateInputEvent(g_mouseEvent, true);
462     if ((result == INPUT_PERMISSION_DENIED) || (result == INPUT_OCCUPIED_BY_OTHER)) {
463         MMI_HILOGE("Permission denied or occupied by other");
464         return result;
465     }
466     return INPUT_SUCCESS;
467 }
468 
OH_Input_CreateMouseEvent()469 struct Input_MouseEvent* OH_Input_CreateMouseEvent()
470 {
471     CALL_DEBUG_ENTER;
472     Input_MouseEvent* mouseEvent = new (std::nothrow) Input_MouseEvent();
473     CHKPL(mouseEvent);
474     return mouseEvent;
475 }
476 
OH_Input_DestroyMouseEvent(struct Input_MouseEvent ** mouseEvent)477 void OH_Input_DestroyMouseEvent(struct Input_MouseEvent** mouseEvent)
478 {
479     CALL_DEBUG_ENTER;
480     CHKPV(mouseEvent);
481     CHKPV(*mouseEvent);
482     delete *mouseEvent;
483     *mouseEvent = nullptr;
484 }
485 
OH_Input_SetMouseEventAction(struct Input_MouseEvent * mouseEvent,int32_t action)486 void OH_Input_SetMouseEventAction(struct Input_MouseEvent* mouseEvent, int32_t action)
487 {
488     CALL_DEBUG_ENTER;
489     CHKPV(mouseEvent);
490     mouseEvent->action = action;
491 }
492 
OH_Input_GetMouseEventAction(const struct Input_MouseEvent * mouseEvent)493 int32_t OH_Input_GetMouseEventAction(const struct Input_MouseEvent* mouseEvent)
494 {
495     CALL_DEBUG_ENTER;
496     CHKPR(mouseEvent, RET_ERR);
497     return mouseEvent->action;
498 }
499 
OH_Input_SetMouseEventDisplayX(struct Input_MouseEvent * mouseEvent,int32_t displayX)500 void OH_Input_SetMouseEventDisplayX(struct Input_MouseEvent* mouseEvent, int32_t displayX)
501 {
502     CALL_DEBUG_ENTER;
503     CHKPV(mouseEvent);
504     mouseEvent->displayX = displayX;
505 }
506 
OH_Input_GetMouseEventDisplayX(const struct Input_MouseEvent * mouseEvent)507 int32_t OH_Input_GetMouseEventDisplayX(const struct Input_MouseEvent* mouseEvent)
508 {
509     CALL_DEBUG_ENTER;
510     CHKPR(mouseEvent, RET_ERR);
511     return mouseEvent->displayX;
512 }
513 
OH_Input_SetMouseEventDisplayY(struct Input_MouseEvent * mouseEvent,int32_t displayY)514 void OH_Input_SetMouseEventDisplayY(struct Input_MouseEvent* mouseEvent, int32_t displayY)
515 {
516     CALL_DEBUG_ENTER;
517     CHKPV(mouseEvent);
518     mouseEvent->displayY = displayY;
519 }
520 
OH_Input_GetMouseEventDisplayY(const struct Input_MouseEvent * mouseEvent)521 int32_t OH_Input_GetMouseEventDisplayY(const struct Input_MouseEvent* mouseEvent)
522 {
523     CALL_DEBUG_ENTER;
524     CHKPR(mouseEvent, RET_ERR);
525     return mouseEvent->displayY;
526 }
527 
OH_Input_SetMouseEventButton(struct Input_MouseEvent * mouseEvent,int32_t button)528 void OH_Input_SetMouseEventButton(struct Input_MouseEvent* mouseEvent, int32_t button)
529 {
530     CALL_DEBUG_ENTER;
531     CHKPV(mouseEvent);
532     mouseEvent->button = button;
533 }
534 
OH_Input_GetMouseEventButton(const struct Input_MouseEvent * mouseEvent)535 int32_t OH_Input_GetMouseEventButton(const struct Input_MouseEvent* mouseEvent)
536 {
537     CALL_DEBUG_ENTER;
538     CHKPR(mouseEvent, RET_ERR);
539     return mouseEvent->button;
540 }
541 
OH_Input_SetMouseEventAxisType(struct Input_MouseEvent * mouseEvent,int32_t axisType)542 void OH_Input_SetMouseEventAxisType(struct Input_MouseEvent* mouseEvent, int32_t axisType)
543 {
544     CALL_DEBUG_ENTER;
545     CHKPV(mouseEvent);
546     mouseEvent->axisType = axisType;
547 }
548 
OH_Input_GetMouseEventAxisType(const struct Input_MouseEvent * mouseEvent)549 int32_t OH_Input_GetMouseEventAxisType(const struct Input_MouseEvent* mouseEvent)
550 {
551     CALL_DEBUG_ENTER;
552     CHKPR(mouseEvent, RET_ERR);
553     return mouseEvent->axisType;
554 }
555 
OH_Input_SetMouseEventAxisValue(struct Input_MouseEvent * mouseEvent,float axisValue)556 void OH_Input_SetMouseEventAxisValue(struct Input_MouseEvent* mouseEvent, float axisValue)
557 {
558     CALL_DEBUG_ENTER;
559     CHKPV(mouseEvent);
560     mouseEvent->axisValue = axisValue;
561 }
562 
OH_Input_GetMouseEventAxisValue(const struct Input_MouseEvent * mouseEvent)563 float OH_Input_GetMouseEventAxisValue(const struct Input_MouseEvent* mouseEvent)
564 {
565     CALL_DEBUG_ENTER;
566     CHKPR(mouseEvent, RET_ERR);
567     return mouseEvent->axisValue;
568 }
569 
OH_Input_SetMouseEventActionTime(struct Input_MouseEvent * mouseEvent,int64_t actionTime)570 void OH_Input_SetMouseEventActionTime(struct Input_MouseEvent* mouseEvent, int64_t actionTime)
571 {
572     CALL_DEBUG_ENTER;
573     CHKPV(mouseEvent);
574     mouseEvent->actionTime = actionTime;
575 }
576 
OH_Input_GetMouseEventActionTime(const struct Input_MouseEvent * mouseEvent)577 int64_t OH_Input_GetMouseEventActionTime(const struct Input_MouseEvent* mouseEvent)
578 {
579     CALL_DEBUG_ENTER;
580     CHKPR(mouseEvent, RET_ERR);
581     return mouseEvent->actionTime;
582 }
583 
HandleTouchActionDown(OHOS::MMI::PointerEvent::PointerItem & item,int64_t time)584 static void HandleTouchActionDown(OHOS::MMI::PointerEvent::PointerItem &item, int64_t time)
585 {
586     auto pointIds = g_touchEvent->GetPointerIds();
587     if (pointIds.empty()) {
588         g_touchEvent->SetActionStartTime(time);
589         g_touchEvent->SetTargetDisplayId(0);
590     }
591     g_touchEvent->SetActionTime(time);
592     g_touchEvent->SetPointerAction(OHOS::MMI::PointerEvent::POINTER_ACTION_DOWN);
593     item.SetDownTime(time);
594     item.SetPressed(true);
595 }
596 
HandleTouchAction(const struct Input_TouchEvent * touchEvent,OHOS::MMI::PointerEvent::PointerItem & item)597 static int32_t HandleTouchAction(const struct Input_TouchEvent* touchEvent, OHOS::MMI::PointerEvent::PointerItem &item)
598 {
599     CALL_DEBUG_ENTER;
600     int64_t time = touchEvent->actionTime;
601     if (time < 0) {
602         time = OHOS::MMI::GetSysClockTime();
603     }
604     switch (touchEvent->action) {
605         case TOUCH_ACTION_CANCEL:{
606             g_touchEvent->SetActionTime(time);
607             g_touchEvent->SetPointerAction(OHOS::MMI::PointerEvent::POINTER_ACTION_CANCEL);
608             if (!(g_touchEvent->GetPointerItem(touchEvent->id, item))) {
609                 MMI_HILOGE("Get pointer parameter failed");
610                 return INPUT_PARAMETER_ERROR;
611             }
612             item.SetPressed(false);
613             break;
614         }
615         case TOUCH_ACTION_DOWN: {
616             HandleTouchActionDown(item, time);
617             break;
618         }
619         case TOUCH_ACTION_MOVE: {
620             g_touchEvent->SetActionTime(time);
621             g_touchEvent->SetPointerAction(OHOS::MMI::PointerEvent::POINTER_ACTION_MOVE);
622             if (!(g_touchEvent->GetPointerItem(touchEvent->id, item))) {
623                 MMI_HILOGE("Get pointer parameter failed");
624                 return INPUT_PARAMETER_ERROR;
625             }
626             break;
627         }
628         case TOUCH_ACTION_UP: {
629             g_touchEvent->SetActionTime(time);
630             g_touchEvent->SetPointerAction(OHOS::MMI::PointerEvent::POINTER_ACTION_UP);
631             if (!(g_touchEvent->GetPointerItem(touchEvent->id, item))) {
632                 MMI_HILOGE("Get pointer parameter failed");
633                 return INPUT_PARAMETER_ERROR;
634             }
635             item.SetPressed(false);
636             break;
637         }
638         default: {
639             MMI_HILOGE("action:%{public}d is invalid", touchEvent->action);
640             return INPUT_PARAMETER_ERROR;
641         }
642     }
643     return INPUT_SUCCESS;
644 }
645 
HandleTouchProperty(const struct Input_TouchEvent * touchEvent,OHOS::MMI::PointerEvent::PointerItem & item)646 static int32_t HandleTouchProperty(const struct Input_TouchEvent* touchEvent,
647     OHOS::MMI::PointerEvent::PointerItem &item)
648 {
649     CALL_DEBUG_ENTER;
650     int32_t id = touchEvent->id;
651     int32_t screenX = touchEvent->displayX;
652     int32_t screenY = touchEvent->displayY;
653     if (screenX < 0 || screenY < 0) {
654         MMI_HILOGE("touch parameter is less 0, can not process");
655         return INPUT_PARAMETER_ERROR;
656     }
657     item.SetDisplayX(screenX);
658     item.SetDisplayY(screenY);
659     item.SetPointerId(id);
660     g_touchEvent->SetPointerId(id);
661     g_touchEvent->SetSourceType(OHOS::MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
662     if (touchEvent->action == TOUCH_ACTION_DOWN) {
663         g_touchEvent->AddPointerItem(item);
664     } else if ((touchEvent->action == TOUCH_ACTION_MOVE) || (touchEvent->action == TOUCH_ACTION_UP)) {
665         g_touchEvent->UpdatePointerItem(id, item);
666     }
667     return INPUT_SUCCESS;
668 }
669 
OH_Input_InjectTouchEvent(const struct Input_TouchEvent * touchEvent)670 int32_t OH_Input_InjectTouchEvent(const struct Input_TouchEvent* touchEvent)
671 {
672     MMI_HILOGI("Input_TouchEvent injectTouchEvent");
673     CHKPR(touchEvent, INPUT_PARAMETER_ERROR);
674     CHKPR(g_touchEvent, INPUT_PARAMETER_ERROR);
675     g_touchEvent->ClearFlag();
676     OHOS::MMI::PointerEvent::PointerItem item;
677     int32_t result = HandleTouchAction(touchEvent, item);
678     if (result != 0) {
679         return INPUT_PARAMETER_ERROR;
680     }
681     result = HandleTouchProperty(touchEvent, item);
682     if (result != 0) {
683         return INPUT_PARAMETER_ERROR;
684     }
685     g_touchEvent->AddFlag(OHOS::MMI::InputEvent::EVENT_FLAG_SIMULATE);
686     OHOS::Singleton<OHOS::MMI::InputManagerImpl>::GetInstance().SimulateInputEvent(g_touchEvent, true);
687     if (touchEvent->action == TOUCH_ACTION_UP) {
688         g_touchEvent->RemovePointerItem(g_touchEvent->GetPointerId());
689         MMI_HILOGD("This touch event is up remove this finger");
690         if (g_touchEvent->GetPointerIds().empty()) {
691             MMI_HILOGD("This touch event is final finger up remove this finger");
692             g_touchEvent->Reset();
693         }
694     }
695     return INPUT_SUCCESS;
696 }
697 
OH_Input_CreateTouchEvent()698 struct Input_TouchEvent* OH_Input_CreateTouchEvent()
699 {
700     CALL_DEBUG_ENTER;
701     Input_TouchEvent* touchEvent = new (std::nothrow) Input_TouchEvent();
702     CHKPL(touchEvent);
703     return touchEvent;
704 }
705 
OH_Input_DestroyTouchEvent(struct Input_TouchEvent ** touchEvent)706 void OH_Input_DestroyTouchEvent(struct Input_TouchEvent** touchEvent)
707 {
708     CALL_DEBUG_ENTER;
709     CHKPV(touchEvent);
710     CHKPV(*touchEvent);
711     delete *touchEvent;
712     *touchEvent = nullptr;
713 }
714 
OH_Input_SetTouchEventAction(struct Input_TouchEvent * touchEvent,int32_t action)715 void OH_Input_SetTouchEventAction(struct Input_TouchEvent* touchEvent, int32_t action)
716 {
717     CALL_DEBUG_ENTER;
718     CHKPV(touchEvent);
719     touchEvent->action = action;
720 }
721 
OH_Input_GetTouchEventAction(const struct Input_TouchEvent * touchEvent)722 int32_t OH_Input_GetTouchEventAction(const struct Input_TouchEvent* touchEvent)
723 {
724     CALL_DEBUG_ENTER;
725     CHKPR(touchEvent, RET_ERR);
726     return touchEvent->action;
727 }
728 
OH_Input_SetTouchEventFingerId(struct Input_TouchEvent * touchEvent,int32_t id)729 void OH_Input_SetTouchEventFingerId(struct Input_TouchEvent* touchEvent, int32_t id)
730 {
731     CALL_DEBUG_ENTER;
732     CHKPV(touchEvent);
733     touchEvent->id = id;
734 }
735 
OH_Input_GetTouchEventFingerId(const struct Input_TouchEvent * touchEvent)736 int32_t OH_Input_GetTouchEventFingerId(const struct Input_TouchEvent* touchEvent)
737 {
738     CALL_DEBUG_ENTER;
739     CHKPR(touchEvent, RET_ERR);
740     return touchEvent->id;
741 }
742 
OH_Input_SetTouchEventDisplayX(struct Input_TouchEvent * touchEvent,int32_t displayX)743 void OH_Input_SetTouchEventDisplayX(struct Input_TouchEvent* touchEvent, int32_t displayX)
744 {
745     CALL_DEBUG_ENTER;
746     CHKPV(touchEvent);
747     touchEvent->displayX = displayX;
748 }
749 
OH_Input_GetTouchEventDisplayX(const struct Input_TouchEvent * touchEvent)750 int32_t OH_Input_GetTouchEventDisplayX(const struct Input_TouchEvent* touchEvent)
751 {
752     CALL_DEBUG_ENTER;
753     CHKPR(touchEvent, RET_ERR);
754     return touchEvent->displayX;
755 }
756 
OH_Input_SetTouchEventDisplayY(struct Input_TouchEvent * touchEvent,int32_t displayY)757 void OH_Input_SetTouchEventDisplayY(struct Input_TouchEvent* touchEvent, int32_t displayY)
758 {
759     CALL_DEBUG_ENTER;
760     CHKPV(touchEvent);
761     touchEvent->displayY = displayY;
762 }
763 
OH_Input_GetTouchEventDisplayY(const struct Input_TouchEvent * touchEvent)764 int32_t OH_Input_GetTouchEventDisplayY(const struct Input_TouchEvent* touchEvent)
765 {
766     CALL_DEBUG_ENTER;
767     CHKPR(touchEvent, RET_ERR);
768     return touchEvent->displayY;
769 }
770 
OH_Input_SetTouchEventActionTime(struct Input_TouchEvent * touchEvent,int64_t actionTime)771 void OH_Input_SetTouchEventActionTime(struct Input_TouchEvent* touchEvent, int64_t actionTime)
772 {
773     CALL_DEBUG_ENTER;
774     CHKPV(touchEvent);
775     touchEvent->actionTime = actionTime;
776 }
777 
OH_Input_GetTouchEventActionTime(const struct Input_TouchEvent * touchEvent)778 int64_t OH_Input_GetTouchEventActionTime(const struct Input_TouchEvent* touchEvent)
779 {
780     CALL_DEBUG_ENTER;
781     CHKPR(touchEvent, RET_ERR);
782     return touchEvent->actionTime;
783 }
784 
OH_Input_CancelInjection()785 void OH_Input_CancelInjection()
786 {
787     CALL_DEBUG_ENTER;
788     OHOS::Singleton<OHOS::MMI::InputManagerImpl>::GetInstance().CancelInjection();
789 }
790 
SetAxisValueByAxisEventType(std::shared_ptr<OHOS::MMI::PointerEvent> event,struct Input_AxisEvent * axisEvent,int32_t axisEventType)791 static bool SetAxisValueByAxisEventType(std::shared_ptr<OHOS::MMI::PointerEvent> event,
792     struct Input_AxisEvent *axisEvent, int32_t axisEventType)
793 {
794     CHKPR(event, false);
795     CHKPR(axisEvent, false);
796     if (axisEventType == OHOS::MMI::PointerEvent::AXIS_EVENT_TYPE_PINCH) {
797         double value = event->GetAxisValue(OHOS::MMI::PointerEvent::AXIS_TYPE_PINCH);
798         axisEvent->axisValues.insert(std::make_pair(AXIS_TYPE_PINCH, value));
799         value = event->GetAxisValue(OHOS::MMI::PointerEvent::AXIS_TYPE_ROTATE);
800         axisEvent->axisValues.insert(std::make_pair(AXIS_TYPE_ROTATE, value));
801     } else if (axisEventType == OHOS::MMI::PointerEvent::AXIS_EVENT_TYPE_SCROLL) {
802         double value = event->GetAxisValue(OHOS::MMI::PointerEvent::AXIS_TYPE_SCROLL_VERTICAL);
803         axisEvent->axisValues.insert(std::make_pair(AXIS_TYPE_SCROLL_VERTICAL, value));
804         value = event->GetAxisValue(OHOS::MMI::PointerEvent::AXIS_TYPE_SCROLL_HORIZONTAL);
805         axisEvent->axisValues.insert(std::make_pair(AXIS_TYPE_SCROLL_HORIZONTAL, value));
806     } else {
807         MMI_HILOGE("Undefined axisEventType: %{public}d", axisEventType);
808         return false;
809     }
810     axisEvent->axisEventType = axisEventType;
811     return true;
812 }
813 
IsAxisEvent(int32_t action)814 static bool IsAxisEvent(int32_t action)
815 {
816     if (action != OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_BEGIN &&
817         action != OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_UPDATE &&
818         action != OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_END) {
819         return false;
820     }
821     return true;
822 }
823 
OH_Input_CreateAxisEvent(void)824 Input_AxisEvent* OH_Input_CreateAxisEvent(void)
825 {
826     Input_AxisEvent* axisEvent = new (std::nothrow) Input_AxisEvent();
827     CHKPP(axisEvent);
828     return axisEvent;
829 }
830 
OH_Input_DestroyAxisEvent(Input_AxisEvent ** axisEvent)831 Input_Result OH_Input_DestroyAxisEvent(Input_AxisEvent** axisEvent)
832 {
833     CALL_DEBUG_ENTER;
834     if (axisEvent == nullptr || *axisEvent == nullptr) {
835         return INPUT_PARAMETER_ERROR;
836     }
837     delete *axisEvent;
838     *axisEvent = nullptr;
839     return INPUT_SUCCESS;
840 }
841 
OH_Input_SetAxisEventAction(Input_AxisEvent * axisEvent,InputEvent_AxisAction action)842 Input_Result OH_Input_SetAxisEventAction(Input_AxisEvent* axisEvent, InputEvent_AxisAction action)
843 {
844     CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
845     axisEvent->axisAction = action;
846     return INPUT_SUCCESS;
847 }
848 
OH_Input_GetAxisEventAction(const Input_AxisEvent * axisEvent,InputEvent_AxisAction * action)849 Input_Result OH_Input_GetAxisEventAction(const Input_AxisEvent* axisEvent, InputEvent_AxisAction *action)
850 {
851     CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
852     CHKPR(action, INPUT_PARAMETER_ERROR);
853     *action = InputEvent_AxisAction(axisEvent->axisAction);
854     return INPUT_SUCCESS;
855 }
856 
OH_Input_SetAxisEventDisplayX(Input_AxisEvent * axisEvent,float displayX)857 Input_Result OH_Input_SetAxisEventDisplayX(Input_AxisEvent* axisEvent, float displayX)
858 {
859     CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
860     axisEvent->displayX = displayX;
861     return INPUT_SUCCESS;
862 }
863 
OH_Input_GetAxisEventDisplayX(const Input_AxisEvent * axisEvent,float * displayX)864 Input_Result OH_Input_GetAxisEventDisplayX(const Input_AxisEvent* axisEvent, float* displayX)
865 {
866     CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
867     CHKPR(displayX, INPUT_PARAMETER_ERROR);
868     *displayX = axisEvent->displayX;
869     return INPUT_SUCCESS;
870 }
871 
OH_Input_SetAxisEventDisplayY(Input_AxisEvent * axisEvent,float displayY)872 Input_Result OH_Input_SetAxisEventDisplayY(Input_AxisEvent* axisEvent, float displayY)
873 {
874     CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
875     axisEvent->displayY = displayY;
876     return INPUT_SUCCESS;
877 }
878 
OH_Input_GetAxisEventDisplayY(const Input_AxisEvent * axisEvent,float * displayY)879 Input_Result OH_Input_GetAxisEventDisplayY(const Input_AxisEvent* axisEvent, float* displayY)
880 {
881     CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
882     CHKPR(displayY, INPUT_PARAMETER_ERROR);
883     *displayY = axisEvent->displayY;
884     return INPUT_SUCCESS;
885 }
886 
OH_Input_SetAxisEventAxisValue(Input_AxisEvent * axisEvent,InputEvent_AxisType axisType,double axisValue)887 Input_Result OH_Input_SetAxisEventAxisValue(Input_AxisEvent* axisEvent,
888     InputEvent_AxisType axisType, double axisValue)
889 {
890     CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
891     axisEvent->axisValues.emplace(axisType, axisValue);
892     return INPUT_SUCCESS;
893 }
894 
OH_Input_GetAxisEventAxisValue(const Input_AxisEvent * axisEvent,InputEvent_AxisType axisType,double * axisValue)895 Input_Result OH_Input_GetAxisEventAxisValue(const Input_AxisEvent* axisEvent,
896     InputEvent_AxisType axisType, double* axisValue)
897 {
898     CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
899     CHKPR(axisValue, INPUT_PARAMETER_ERROR);
900     auto it = axisEvent->axisValues.find(axisType);
901     if (it == axisEvent->axisValues.end()) {
902         MMI_HILOGE("There is no axis value of axisType: %{public}d in the axisEvent", axisType);
903         return INPUT_PARAMETER_ERROR;
904     }
905     *axisValue = it->second;
906     return INPUT_SUCCESS;
907 }
908 
OH_Input_SetAxisEventActionTime(Input_AxisEvent * axisEvent,int64_t actionTime)909 Input_Result OH_Input_SetAxisEventActionTime(Input_AxisEvent* axisEvent, int64_t actionTime)
910 {
911     CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
912     axisEvent->actionTime = actionTime;
913     return INPUT_SUCCESS;
914 }
915 
OH_Input_GetAxisEventActionTime(const Input_AxisEvent * axisEvent,int64_t * actionTime)916 Input_Result OH_Input_GetAxisEventActionTime(const Input_AxisEvent* axisEvent, int64_t* actionTime)
917 {
918     CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
919     CHKPR(actionTime, INPUT_PARAMETER_ERROR);
920     *actionTime = axisEvent->actionTime;
921     return INPUT_SUCCESS;
922 }
923 
OH_Input_SetAxisEventType(Input_AxisEvent * axisEvent,InputEvent_AxisEventType axisEventType)924 Input_Result OH_Input_SetAxisEventType(Input_AxisEvent* axisEvent, InputEvent_AxisEventType axisEventType)
925 {
926     CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
927     axisEvent->axisEventType = axisEventType;
928     return INPUT_SUCCESS;
929 }
930 
OH_Input_GetAxisEventType(const Input_AxisEvent * axisEvent,InputEvent_AxisEventType * axisEventType)931 Input_Result OH_Input_GetAxisEventType(const Input_AxisEvent* axisEvent, InputEvent_AxisEventType* axisEventType)
932 {
933     CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
934     CHKPR(axisEventType, INPUT_PARAMETER_ERROR);
935     *axisEventType = InputEvent_AxisEventType(axisEvent->axisEventType);
936     return INPUT_SUCCESS;
937 }
938 
OH_Input_SetAxisEventSourceType(Input_AxisEvent * axisEvent,InputEvent_SourceType sourceType)939 Input_Result OH_Input_SetAxisEventSourceType(Input_AxisEvent* axisEvent, InputEvent_SourceType sourceType)
940 {
941     CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
942     axisEvent->sourceType = sourceType;
943     return INPUT_SUCCESS;
944 }
945 
OH_Input_GetAxisEventSourceType(const Input_AxisEvent * axisEvent,InputEvent_SourceType * sourceType)946 Input_Result OH_Input_GetAxisEventSourceType(const Input_AxisEvent* axisEvent, InputEvent_SourceType* sourceType)
947 {
948     CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
949     CHKPR(sourceType, INPUT_PARAMETER_ERROR);
950     *sourceType = InputEvent_SourceType(axisEvent->sourceType);
951     return INPUT_SUCCESS;
952 }
953 
NormalizeResult(int32_t result)954 static Input_Result NormalizeResult(int32_t result)
955 {
956     if (result < RET_OK) {
957         if (result == OHOS::MMI::ERROR_NO_PERMISSION) {
958             MMI_HILOGE("Permisson denied");
959             return INPUT_PERMISSION_DENIED;
960         }
961         return INPUT_SERVICE_EXCEPTION;
962     }
963     return INPUT_SUCCESS;
964 }
965 
SetKeyEventAction(Input_KeyEvent * keyEvent,int32_t action)966 static bool SetKeyEventAction(Input_KeyEvent* keyEvent, int32_t action)
967 {
968     CHKPF(keyEvent);
969     if (action == OHOS::MMI::KeyEvent::KEY_ACTION_CANCEL) {
970         keyEvent->action = KEY_ACTION_CANCEL;
971     } else if (action == OHOS::MMI::KeyEvent::KEY_ACTION_DOWN) {
972         keyEvent->action = KEY_ACTION_DOWN;
973     } else if (action == OHOS::MMI::KeyEvent::KEY_ACTION_UP) {
974         keyEvent->action = KEY_ACTION_UP;
975     } else {
976         MMI_HILOGE("Invalid key event action");
977         return false;
978     }
979     return true;
980 }
981 
KeyEventMonitorCallback(std::shared_ptr<OHOS::MMI::KeyEvent> event)982 static void KeyEventMonitorCallback(std::shared_ptr<OHOS::MMI::KeyEvent> event)
983 {
984     CHKPV(event);
985     Input_KeyEvent* keyEvent = OH_Input_CreateKeyEvent();
986     CHKPV(keyEvent);
987     if (!SetKeyEventAction(keyEvent, event->GetKeyAction())) {
988         OH_Input_DestroyKeyEvent(&keyEvent);
989         return;
990     }
991     keyEvent->keyCode = event->GetKeyCode();
992     keyEvent->actionTime = event->GetActionTime();
993     std::lock_guard guard(g_mutex);
994     for (auto &callback : g_keyMonitorCallbacks) {
995         callback(keyEvent);
996     }
997     OH_Input_DestroyKeyEvent(&keyEvent);
998 }
999 
IsScreenCaptureWorking()1000 static bool IsScreenCaptureWorking()
1001 {
1002     CALL_DEBUG_ENTER;
1003 #ifdef PLAYER_FRAMEWORK_EXISTS
1004     int32_t pid = OHOS::IPCSkeleton::GetCallingPid();
1005     int32_t capturePid = OHOS::Media::ScreenCaptureMonitor::GetInstance()->IsScreenCaptureWorking();
1006     if (capturePid != pid) {
1007         MMI_HILOGE("Calling pid is: %{public}d, but screen capture pid is: %{public}d", pid, capturePid);
1008         return false;
1009     }
1010     return true;
1011 #else
1012     return false;
1013 #endif // PLAYER_FRAMEWORK_EXISTS
1014 }
1015 
OH_Input_AddKeyEventMonitor(Input_KeyEventCallback callback)1016 Input_Result OH_Input_AddKeyEventMonitor(Input_KeyEventCallback callback)
1017 {
1018     CALL_DEBUG_ENTER;
1019     CHKPR(callback, INPUT_PARAMETER_ERROR);
1020     if (!OHOS::MMI::PermissionHelper::GetInstance()->VerifySystemApp()) {
1021         if (!IsScreenCaptureWorking()) {
1022             MMI_HILOGE("The screen capture is not working");
1023             return INPUT_PERMISSION_DENIED;
1024         }
1025     }
1026     Input_Result retCode = INPUT_SUCCESS;
1027     std::lock_guard guard(g_mutex);
1028     if (g_keyMonitorId == INVALID_MONITOR_ID) {
1029         int32_t ret = OHOS::Singleton<OHOS::MMI::InputManagerImpl>::GetInstance().AddMonitor(KeyEventMonitorCallback);
1030         retCode = NormalizeResult(ret);
1031         if (retCode != INPUT_SUCCESS) {
1032             return retCode;
1033         }
1034         g_keyMonitorId = ret;
1035     }
1036     g_keyMonitorCallbacks.insert(callback);
1037     return retCode;
1038 }
1039 
SetTouchEventAction(Input_TouchEvent * touchEvent,int32_t action)1040 static bool SetTouchEventAction(Input_TouchEvent* touchEvent, int32_t action)
1041 {
1042     CHKPF(touchEvent);
1043     switch (action) {
1044         case OHOS::MMI::PointerEvent::POINTER_ACTION_CANCEL:
1045             touchEvent->action = TOUCH_ACTION_CANCEL;
1046             break;
1047         case OHOS::MMI::PointerEvent::POINTER_ACTION_DOWN:
1048             touchEvent->action = TOUCH_ACTION_DOWN;
1049             break;
1050         case OHOS::MMI::PointerEvent::POINTER_ACTION_MOVE:
1051             touchEvent->action = TOUCH_ACTION_MOVE;
1052             break;
1053         case OHOS::MMI::PointerEvent::POINTER_ACTION_UP:
1054             touchEvent->action = TOUCH_ACTION_UP;
1055             break;
1056         default:
1057             MMI_HILOGE("Invalid touch event action");
1058             return false;
1059     }
1060     return true;
1061 }
1062 
TouchEventMonitorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)1063 static void TouchEventMonitorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)
1064 {
1065     CHKPV(event);
1066     Input_TouchEvent* touchEvent = OH_Input_CreateTouchEvent();
1067     CHKPV(touchEvent);
1068     OHOS::MMI::PointerEvent::PointerItem item;
1069     if (!(event->GetPointerItem(event->GetPointerId(), item))) {
1070         MMI_HILOGE("Can not get pointerItem for the pointer event");
1071         OH_Input_DestroyTouchEvent(&touchEvent);
1072         return;
1073     }
1074     if (!SetTouchEventAction(touchEvent, event->GetPointerAction())) {
1075         OH_Input_DestroyTouchEvent(&touchEvent);
1076         return;
1077     }
1078     touchEvent->id = event->GetPointerId();
1079     touchEvent->displayX = item.GetDisplayX();
1080     touchEvent->displayY = item.GetDisplayY();
1081     touchEvent->actionTime = event->GetActionTime();
1082     std::lock_guard guard(g_mutex);
1083     for (auto &callback : g_touchMonitorCallbacks) {
1084         callback(touchEvent);
1085     }
1086     OH_Input_DestroyTouchEvent(&touchEvent);
1087 }
1088 
SetMouseEventAction(Input_MouseEvent * mouseEvent,int32_t action)1089 static bool SetMouseEventAction(Input_MouseEvent* mouseEvent, int32_t action)
1090 {
1091     CHKPF(mouseEvent);
1092     switch (action) {
1093         case OHOS::MMI::PointerEvent::POINTER_ACTION_CANCEL:
1094             mouseEvent->action = MOUSE_ACTION_CANCEL;
1095             break;
1096         case OHOS::MMI::PointerEvent::POINTER_ACTION_MOVE:
1097             mouseEvent->action = MOUSE_ACTION_MOVE;
1098             break;
1099         case OHOS::MMI::PointerEvent::POINTER_ACTION_BUTTON_DOWN:
1100             mouseEvent->action = MOUSE_ACTION_BUTTON_DOWN;
1101             break;
1102         case OHOS::MMI::PointerEvent::POINTER_ACTION_BUTTON_UP:
1103             mouseEvent->action = MOUSE_ACTION_BUTTON_UP;
1104             break;
1105         default:
1106             MMI_HILOGE("Invalid mouse event action");
1107             return false;
1108     }
1109     return true;
1110 }
1111 
SetMouseEventButton(Input_MouseEvent * mouseEvent,int32_t button)1112 static bool SetMouseEventButton(Input_MouseEvent* mouseEvent, int32_t button)
1113 {
1114     CHKPF(mouseEvent);
1115     switch (button) {
1116         case OHOS::MMI::PointerEvent::BUTTON_NONE:
1117             mouseEvent->button = MOUSE_BUTTON_NONE;
1118             break;
1119         case OHOS::MMI::PointerEvent::MOUSE_BUTTON_LEFT:
1120             mouseEvent->button = MOUSE_BUTTON_LEFT;
1121             break;
1122         case OHOS::MMI::PointerEvent::MOUSE_BUTTON_MIDDLE:
1123             mouseEvent->button = MOUSE_BUTTON_MIDDLE;
1124             break;
1125         case OHOS::MMI::PointerEvent::MOUSE_BUTTON_RIGHT:
1126             mouseEvent->button = MOUSE_BUTTON_RIGHT;
1127             break;
1128         case OHOS::MMI::PointerEvent::MOUSE_BUTTON_FORWARD:
1129             mouseEvent->button = MOUSE_BUTTON_FORWARD;
1130             break;
1131         case OHOS::MMI::PointerEvent::MOUSE_BUTTON_BACK:
1132             mouseEvent->button = MOUSE_BUTTON_BACK;
1133             break;
1134         default:
1135             MMI_HILOGE("Invalid mouse event button");
1136             return false;
1137     }
1138     return true;
1139 }
1140 
MouseEventMonitorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)1141 static void MouseEventMonitorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)
1142 {
1143     CHKPV(event);
1144     Input_MouseEvent* mouseEvent = OH_Input_CreateMouseEvent();
1145     CHKPV(mouseEvent);
1146     OHOS::MMI::PointerEvent::PointerItem item;
1147     if (!(event->GetPointerItem(event->GetPointerId(), item))) {
1148         MMI_HILOGE("Can not get pointerItem for the pointer event");
1149         OH_Input_DestroyMouseEvent(&mouseEvent);
1150         return;
1151     }
1152     if (!SetMouseEventAction(mouseEvent, event->GetPointerAction())) {
1153         OH_Input_DestroyMouseEvent(&mouseEvent);
1154         return;
1155     }
1156     if (!SetMouseEventButton(mouseEvent, event->GetButtonId())) {
1157         OH_Input_DestroyMouseEvent(&mouseEvent);
1158         return;
1159     }
1160     mouseEvent->displayX = item.GetDisplayX();
1161     mouseEvent->displayY = item.GetDisplayY();
1162     mouseEvent->actionTime = event->GetActionTime();
1163     std::lock_guard guard(g_mutex);
1164     for (auto &callback : g_mouseMonitorCallbacks) {
1165         callback(mouseEvent);
1166     }
1167     OH_Input_DestroyMouseEvent(&mouseEvent);
1168 }
1169 
SetAxisEventAction(Input_AxisEvent * axisEvent,int32_t action)1170 static void SetAxisEventAction(Input_AxisEvent* axisEvent, int32_t action)
1171 {
1172     CHKPV(axisEvent);
1173     switch (action) {
1174         case OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_BEGIN:
1175             axisEvent->axisAction = AXIS_ACTION_BEGIN;
1176             break;
1177         case OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_UPDATE:
1178             axisEvent->axisAction = AXIS_ACTION_UPDATE;
1179             break;
1180         case OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_END:
1181             axisEvent->axisAction = AXIS_ACTION_END;
1182             break;
1183         default:
1184             break;
1185     }
1186 }
1187 
AxisEventMonitorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)1188 static void AxisEventMonitorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)
1189 {
1190     CHKPV(event);
1191     Input_AxisEvent* axisEvent = OH_Input_CreateAxisEvent();
1192     CHKPV(axisEvent);
1193     OHOS::MMI::PointerEvent::PointerItem item;
1194     if (!(event->GetPointerItem(event->GetPointerId(), item))) {
1195         MMI_HILOGE("Can not get pointerItem for the pointer event");
1196         OH_Input_DestroyAxisEvent(&axisEvent);
1197         return;
1198     }
1199     if (!SetAxisValueByAxisEventType(event, axisEvent, event->GetAxisEventType())) {
1200         OH_Input_DestroyAxisEvent(&axisEvent);
1201         return;
1202     }
1203     SetAxisEventAction(axisEvent, event->GetPointerAction());
1204     axisEvent->displayX = item.GetDisplayX();
1205     axisEvent->displayY = item.GetDisplayY();
1206     axisEvent->actionTime = event->GetActionTime();
1207     axisEvent->sourceType = event->GetSourceType();
1208     std::lock_guard guard(g_mutex);
1209     for (auto &callback : g_axisMonitorAllCallbacks) {
1210         callback(axisEvent);
1211     }
1212     auto it = g_axisMonitorCallbacks.find(InputEvent_AxisEventType(event->GetAxisEventType()));
1213     if (it != g_axisMonitorCallbacks.end()) {
1214         for (auto &callback : it->second) {
1215             callback(axisEvent);
1216         }
1217     }
1218     OH_Input_DestroyAxisEvent(&axisEvent);
1219 }
1220 
PointerEventMonitorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)1221 static void PointerEventMonitorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)
1222 {
1223     CHKPV(event);
1224     if (event->GetSourceType() == SOURCE_TYPE_TOUCHSCREEN) {
1225         TouchEventMonitorCallback(event);
1226     } else if (event->GetSourceType() == SOURCE_TYPE_MOUSE && !IsAxisEvent(event->GetPointerAction())) {
1227         MouseEventMonitorCallback(event);
1228     } else if (IsAxisEvent(event->GetPointerAction()) && event->GetSourceType() != SOURCE_TYPE_TOUCHSCREEN) {
1229         AxisEventMonitorCallback(event);
1230     } else {
1231         MMI_HILOGE("Undefined event type");
1232     }
1233 }
1234 
AddPointerEventMonitor()1235 static Input_Result AddPointerEventMonitor()
1236 {
1237     Input_Result retCode = INPUT_SUCCESS;
1238     std::lock_guard guard(g_mutex);
1239     if (g_pointerMonitorId == INVALID_MONITOR_ID) {
1240         int32_t ret = OHOS::Singleton<OHOS::MMI::InputManagerImpl>::GetInstance().AddMonitor(
1241             PointerEventMonitorCallback);
1242         retCode = NormalizeResult(ret);
1243         if (retCode != INPUT_SUCCESS) {
1244             MMI_HILOGE("Add pointer event monitor failed.");
1245             return retCode;
1246         }
1247         g_pointerMonitorId = ret;
1248     }
1249     return retCode;
1250 }
1251 
OH_Input_AddMouseEventMonitor(Input_MouseEventCallback callback)1252 Input_Result OH_Input_AddMouseEventMonitor(Input_MouseEventCallback callback)
1253 {
1254     CALL_DEBUG_ENTER;
1255     CHKPR(callback, INPUT_PARAMETER_ERROR);
1256     if (!OHOS::MMI::PermissionHelper::GetInstance()->VerifySystemApp()) {
1257         if (!IsScreenCaptureWorking()) {
1258             MMI_HILOGE("The screen capture is not working");
1259             return INPUT_PERMISSION_DENIED;
1260         }
1261     }
1262     Input_Result ret = AddPointerEventMonitor();
1263     if (ret != INPUT_SUCCESS) {
1264         return ret;
1265     }
1266     std::lock_guard guard(g_mutex);
1267     g_mouseMonitorCallbacks.insert(callback);
1268     return INPUT_SUCCESS;
1269 }
1270 
OH_Input_AddTouchEventMonitor(Input_TouchEventCallback callback)1271 Input_Result OH_Input_AddTouchEventMonitor(Input_TouchEventCallback callback)
1272 {
1273     CALL_DEBUG_ENTER;
1274     CHKPR(callback, INPUT_PARAMETER_ERROR);
1275     if (!OHOS::MMI::PermissionHelper::GetInstance()->VerifySystemApp()) {
1276         if (!IsScreenCaptureWorking()) {
1277             MMI_HILOGE("The screen capture is not working");
1278             return INPUT_PERMISSION_DENIED;
1279         }
1280     }
1281     Input_Result ret = AddPointerEventMonitor();
1282     if (ret != INPUT_SUCCESS) {
1283         return ret;
1284     }
1285     std::lock_guard guard(g_mutex);
1286     g_touchMonitorCallbacks.insert(callback);
1287     return INPUT_SUCCESS;
1288 }
1289 
OH_Input_AddAxisEventMonitorForAll(Input_AxisEventCallback callback)1290 Input_Result OH_Input_AddAxisEventMonitorForAll(Input_AxisEventCallback callback)
1291 {
1292     CALL_DEBUG_ENTER;
1293     CHKPR(callback, INPUT_PARAMETER_ERROR);
1294     if (!OHOS::MMI::PermissionHelper::GetInstance()->VerifySystemApp()) {
1295         if (!IsScreenCaptureWorking()) {
1296             MMI_HILOGE("The screen capture is not working");
1297             return INPUT_PERMISSION_DENIED;
1298         }
1299     }
1300     Input_Result ret = AddPointerEventMonitor();
1301     if (ret != INPUT_SUCCESS) {
1302         return ret;
1303     }
1304     std::lock_guard guard(g_mutex);
1305     g_axisMonitorAllCallbacks.insert(callback);
1306     return INPUT_SUCCESS;
1307 }
1308 
OH_Input_AddAxisEventMonitor(InputEvent_AxisEventType axisEventType,Input_AxisEventCallback callback)1309 Input_Result OH_Input_AddAxisEventMonitor(InputEvent_AxisEventType axisEventType, Input_AxisEventCallback callback)
1310 {
1311     CALL_DEBUG_ENTER;
1312     CHKPR(callback, INPUT_PARAMETER_ERROR);
1313     if (!OHOS::MMI::PermissionHelper::GetInstance()->VerifySystemApp()) {
1314         if (!IsScreenCaptureWorking()) {
1315             MMI_HILOGE("The screen capture is not working");
1316             return INPUT_PERMISSION_DENIED;
1317         }
1318     }
1319     Input_Result ret = AddPointerEventMonitor();
1320     if (ret != INPUT_SUCCESS) {
1321         return ret;
1322     }
1323     std::lock_guard guard(g_mutex);
1324     auto it = g_axisMonitorCallbacks.find(axisEventType);
1325     if (it == g_axisMonitorCallbacks.end()) {
1326         std::set<Input_AxisEventCallback> callbacks;
1327         callbacks.insert(callback);
1328         g_axisMonitorCallbacks.insert(std::make_pair(axisEventType, callbacks));
1329     } else {
1330         it->second.insert(callback);
1331     }
1332     return INPUT_SUCCESS;
1333 }
1334 
OH_Input_RemoveKeyEventMonitor(Input_KeyEventCallback callback)1335 Input_Result OH_Input_RemoveKeyEventMonitor(Input_KeyEventCallback callback)
1336 {
1337     CALL_DEBUG_ENTER;
1338     CHKPR(callback, INPUT_PARAMETER_ERROR);
1339     Input_Result retCode = INPUT_SUCCESS;
1340     std::lock_guard guard(g_mutex);
1341     auto it = g_keyMonitorCallbacks.find(callback);
1342     if (it == g_keyMonitorCallbacks.end()) {
1343         return INPUT_PARAMETER_ERROR;
1344     }
1345     g_keyMonitorCallbacks.erase(it);
1346     if (g_keyMonitorCallbacks.empty()) {
1347         int32_t ret = OHOS::Singleton<OHOS::MMI::InputManagerImpl>::GetInstance().RemoveMonitor(g_keyMonitorId);
1348         retCode = NormalizeResult(ret);
1349         if (retCode != INPUT_SUCCESS) {
1350             return retCode;
1351         }
1352         g_keyMonitorId = INVALID_MONITOR_ID;
1353     }
1354     return retCode;
1355 }
1356 
IsNeedRemoveMonitor()1357 static bool IsNeedRemoveMonitor()
1358 {
1359     if (g_mouseMonitorCallbacks.empty() && g_touchMonitorCallbacks.empty() &&
1360         g_axisMonitorCallbacks.empty() && g_axisMonitorAllCallbacks.empty()) {
1361         return true;
1362     }
1363     return false;
1364 }
1365 
RemovePointerEventMonitor()1366 static Input_Result RemovePointerEventMonitor()
1367 {
1368     Input_Result retCode = INPUT_SUCCESS;
1369     if (IsNeedRemoveMonitor()) {
1370         int32_t ret = OHOS::Singleton<OHOS::MMI::InputManagerImpl>::GetInstance().RemoveMonitor(g_pointerMonitorId);
1371         retCode = NormalizeResult(ret);
1372         if (retCode != INPUT_SUCCESS) {
1373             return retCode;
1374         }
1375         g_pointerMonitorId = INVALID_MONITOR_ID;
1376     }
1377     return retCode;
1378 }
1379 
OH_Input_RemoveMouseEventMonitor(Input_MouseEventCallback callback)1380 Input_Result OH_Input_RemoveMouseEventMonitor(Input_MouseEventCallback callback)
1381 {
1382     CALL_DEBUG_ENTER;
1383     CHKPR(callback, INPUT_PARAMETER_ERROR);
1384     std::lock_guard guard(g_mutex);
1385     auto it = g_mouseMonitorCallbacks.find(callback);
1386     if (it == g_mouseMonitorCallbacks.end()) {
1387         MMI_HILOGE("The callback has not been added.");
1388         return INPUT_PARAMETER_ERROR;
1389     }
1390     g_mouseMonitorCallbacks.erase(it);
1391     return RemovePointerEventMonitor();
1392 }
1393 
OH_Input_RemoveTouchEventMonitor(Input_TouchEventCallback callback)1394 Input_Result OH_Input_RemoveTouchEventMonitor(Input_TouchEventCallback callback)
1395 {
1396     CALL_DEBUG_ENTER;
1397     CHKPR(callback, INPUT_PARAMETER_ERROR);
1398     std::lock_guard guard(g_mutex);
1399     auto it = g_touchMonitorCallbacks.find(callback);
1400     if (it == g_touchMonitorCallbacks.end()) {
1401         MMI_HILOGE("The callback has not been added.");
1402         return INPUT_PARAMETER_ERROR;
1403     }
1404     g_touchMonitorCallbacks.erase(it);
1405     return RemovePointerEventMonitor();
1406 }
1407 
OH_Input_RemoveAxisEventMonitorForAll(Input_AxisEventCallback callback)1408 Input_Result OH_Input_RemoveAxisEventMonitorForAll(Input_AxisEventCallback callback)
1409 {
1410     CALL_DEBUG_ENTER;
1411     CHKPR(callback, INPUT_PARAMETER_ERROR);
1412     std::lock_guard guard(g_mutex);
1413     auto it = g_axisMonitorAllCallbacks.find(callback);
1414     if (it == g_axisMonitorAllCallbacks.end()) {
1415         MMI_HILOGE("The callback has not been added.");
1416         return INPUT_PARAMETER_ERROR;
1417     }
1418     g_axisMonitorAllCallbacks.erase(it);
1419     return RemovePointerEventMonitor();
1420 }
1421 
OH_Input_RemoveAxisEventMonitor(InputEvent_AxisEventType axisEventType,Input_AxisEventCallback callback)1422 Input_Result OH_Input_RemoveAxisEventMonitor(InputEvent_AxisEventType axisEventType, Input_AxisEventCallback callback)
1423 {
1424     CALL_DEBUG_ENTER;
1425     CHKPR(callback, INPUT_PARAMETER_ERROR);
1426     std::lock_guard guard(g_mutex);
1427     if (g_axisMonitorCallbacks.find(axisEventType) == g_axisMonitorCallbacks.end()) {
1428         MMI_HILOGE("The axis event type has not been added.");
1429         return INPUT_PARAMETER_ERROR;
1430     }
1431     auto it = g_axisMonitorCallbacks[axisEventType].find(callback);
1432     if (it == g_axisMonitorCallbacks[axisEventType].end()) {
1433         MMI_HILOGE("The callback has not been added.");
1434         return INPUT_PARAMETER_ERROR;
1435     }
1436     g_axisMonitorCallbacks[axisEventType].erase(it);
1437     if (g_axisMonitorCallbacks[axisEventType].empty()) {
1438         g_axisMonitorCallbacks.erase(axisEventType);
1439     }
1440     return RemovePointerEventMonitor();
1441 }
1442 
KeyEventInterceptorCallback(std::shared_ptr<OHOS::MMI::KeyEvent> event)1443 static void KeyEventInterceptorCallback(std::shared_ptr<OHOS::MMI::KeyEvent> event)
1444 {
1445     CHKPV(event);
1446     Input_KeyEvent* keyEvent = OH_Input_CreateKeyEvent();
1447     CHKPV(keyEvent);
1448     if (!SetKeyEventAction(keyEvent, event->GetKeyAction())) {
1449         OH_Input_DestroyKeyEvent(&keyEvent);
1450         return;
1451     }
1452     keyEvent->keyCode = event->GetKeyCode();
1453     keyEvent->actionTime = event->GetActionTime();
1454     std::lock_guard guard(g_mutex);
1455     if (g_keyInterceptorCallback != nullptr) {
1456         g_keyInterceptorCallback(keyEvent);
1457     }
1458     OH_Input_DestroyKeyEvent(&keyEvent);
1459 }
1460 
OH_Input_AddKeyEventInterceptor(Input_KeyEventCallback callback,Input_InterceptorOptions * option)1461 Input_Result OH_Input_AddKeyEventInterceptor(Input_KeyEventCallback callback, Input_InterceptorOptions *option)
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     if (g_keyInterceptorId != INVALID_INTERCEPTOR_ID) {
1468         MMI_HILOGE("Another key event interceptor has been added");
1469         return INPUT_REPEAT_INTERCEPTOR;
1470     }
1471     g_keyInterceptor->SetCallback(KeyEventInterceptorCallback);
1472     int32_t ret = g_keyInterceptor->Start(OHOS::MMI::INTERCEPTOR_TYPE_KEY);
1473     retCode = NormalizeResult(ret);
1474     if (retCode != INPUT_SUCCESS) {
1475         MMI_HILOGE("Add key event interceptor failed.");
1476         return retCode;
1477     }
1478     g_keyInterceptorId = ret;
1479     g_keyInterceptorCallback = callback;
1480     return retCode;
1481 }
1482 
TouchEventInterceptorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)1483 static void TouchEventInterceptorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)
1484 {
1485     CHKPV(event);
1486     std::lock_guard guard(g_mutex);
1487     CHKPV(g_pointerInterceptorCallback);
1488     if (g_pointerInterceptorCallback->touchCallback == nullptr) {
1489         MMI_HILOGE("There is no callback for mouse event interceptor");
1490         return;
1491     }
1492     Input_TouchEvent* touchEvent = OH_Input_CreateTouchEvent();
1493     CHKPV(touchEvent);
1494     OHOS::MMI::PointerEvent::PointerItem item;
1495     if (!(event->GetPointerItem(event->GetPointerId(), item))) {
1496         MMI_HILOGE("Can not get pointerItem for the pointer event");
1497         OH_Input_DestroyTouchEvent(&touchEvent);
1498         return;
1499     }
1500     if (!SetTouchEventAction(touchEvent, event->GetPointerAction())) {
1501         OH_Input_DestroyTouchEvent(&touchEvent);
1502         return;
1503     }
1504     touchEvent->id = event->GetPointerId();
1505     touchEvent->displayX = item.GetDisplayX();
1506     touchEvent->displayY = item.GetDisplayY();
1507     touchEvent->actionTime = event->GetActionTime();
1508     g_pointerInterceptorCallback->touchCallback(touchEvent);
1509     OH_Input_DestroyTouchEvent(&touchEvent);
1510 }
1511 
MouseEventInterceptorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)1512 static void MouseEventInterceptorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)
1513 {
1514     CHKPV(event);
1515     std::lock_guard guard(g_mutex);
1516     CHKPV(g_pointerInterceptorCallback);
1517     if (g_pointerInterceptorCallback->mouseCallback == nullptr) {
1518         MMI_HILOGE("There is no callback for mouse event interceptor");
1519         return;
1520     }
1521     Input_MouseEvent* mouseEvent = OH_Input_CreateMouseEvent();
1522     CHKPV(mouseEvent);
1523     OHOS::MMI::PointerEvent::PointerItem item;
1524     if (!(event->GetPointerItem(event->GetPointerId(), item))) {
1525         MMI_HILOGE("Can not get pointerItem for the pointer event");
1526         OH_Input_DestroyMouseEvent(&mouseEvent);
1527         return;
1528     }
1529     if (!SetMouseEventAction(mouseEvent, event->GetPointerAction())) {
1530         OH_Input_DestroyMouseEvent(&mouseEvent);
1531         return;
1532     }
1533     if (!SetMouseEventButton(mouseEvent, event->GetButtonId())) {
1534         OH_Input_DestroyMouseEvent(&mouseEvent);
1535         return;
1536     }
1537     mouseEvent->displayX = item.GetDisplayX();
1538     mouseEvent->displayY = item.GetDisplayY();
1539     mouseEvent->actionTime = event->GetActionTime();
1540     g_pointerInterceptorCallback->mouseCallback(mouseEvent);
1541     OH_Input_DestroyMouseEvent(&mouseEvent);
1542 }
1543 
AxisEventInterceptorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)1544 static void AxisEventInterceptorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)
1545 {
1546     CHKPV(event);
1547     std::lock_guard guard(g_mutex);
1548     CHKPV(g_pointerInterceptorCallback);
1549     if (g_pointerInterceptorCallback->axisCallback == nullptr) {
1550         MMI_HILOGE("There is no callback for axis event interceptor");
1551         return;
1552     }
1553     Input_AxisEvent* axisEvent = OH_Input_CreateAxisEvent();
1554     CHKPV(axisEvent);
1555     OHOS::MMI::PointerEvent::PointerItem item;
1556     if (!(event->GetPointerItem(event->GetPointerId(), item))) {
1557         MMI_HILOGE("Can not get pointerItem for the pointer event");
1558         OH_Input_DestroyAxisEvent(&axisEvent);
1559         return;
1560     }
1561     if (!SetAxisValueByAxisEventType(event, axisEvent, event->GetAxisEventType())) {
1562         MMI_HILOGE("Fail to set axis value");
1563         OH_Input_DestroyAxisEvent(&axisEvent);
1564         return;
1565     }
1566     SetAxisEventAction(axisEvent, event->GetPointerAction());
1567     axisEvent->displayX = item.GetDisplayX();
1568     axisEvent->displayY = item.GetDisplayY();
1569     axisEvent->actionTime = event->GetActionTime();
1570     axisEvent->sourceType = event->GetSourceType();
1571     g_pointerInterceptorCallback->axisCallback(axisEvent);
1572     OH_Input_DestroyAxisEvent(&axisEvent);
1573 }
1574 
PointerEventInterceptorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)1575 static void PointerEventInterceptorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)
1576 {
1577     CHKPV(event);
1578     if (event->GetSourceType() == SOURCE_TYPE_TOUCHSCREEN) {
1579         TouchEventInterceptorCallback(event);
1580     } else if (event->GetSourceType() == SOURCE_TYPE_MOUSE && !IsAxisEvent(event->GetPointerAction())) {
1581         MouseEventInterceptorCallback(event);
1582     } else if (IsAxisEvent(event->GetPointerAction()) && event->GetSourceType() != SOURCE_TYPE_TOUCHSCREEN) {
1583         AxisEventInterceptorCallback(event);
1584     } else {
1585         MMI_HILOGE("Undefined event type");
1586     }
1587 }
1588 
OH_Input_AddInputEventInterceptor(Input_InterceptorEventCallback * callback,Input_InterceptorOptions * option)1589 Input_Result OH_Input_AddInputEventInterceptor(Input_InterceptorEventCallback *callback,
1590                                                Input_InterceptorOptions *option)
1591 {
1592     CALL_DEBUG_ENTER;
1593     CHKPR(callback, INPUT_PARAMETER_ERROR);
1594     Input_Result retCode = INPUT_SUCCESS;
1595     std::lock_guard guard(g_mutex);
1596     if (g_pointerInterceptorId != INVALID_INTERCEPTOR_ID) {
1597         MMI_HILOGE("Another interceptor for input event has been added");
1598         return INPUT_REPEAT_INTERCEPTOR;
1599     }
1600     g_pointerInterceptor->SetCallback(PointerEventInterceptorCallback);
1601     int32_t ret = g_pointerInterceptor->Start(OHOS::MMI::INTERCEPTOR_TYPE_POINTER);
1602     retCode = NormalizeResult(ret);
1603     if (retCode != INPUT_SUCCESS) {
1604         MMI_HILOGE("Add pointer event interceptor failed.");
1605         return retCode;
1606     }
1607     g_pointerInterceptorId = ret;
1608     g_pointerInterceptorCallback = callback;
1609     return retCode;
1610 }
1611 
OH_Input_RemoveKeyEventInterceptor(void)1612 Input_Result OH_Input_RemoveKeyEventInterceptor(void)
1613 {
1614     CALL_DEBUG_ENTER;
1615     Input_Result retCode = INPUT_SUCCESS;
1616     std::lock_guard guard(g_mutex);
1617     int32_t ret = g_keyInterceptor->Stop(OHOS::MMI::INTERCEPTOR_TYPE_KEY);
1618     retCode = NormalizeResult(ret);
1619     if (retCode != INPUT_SUCCESS) {
1620         MMI_HILOGE("Remove key event interceptor failed.");
1621         return retCode;
1622     }
1623     g_keyInterceptorCallback = nullptr;
1624     g_keyInterceptorId = INVALID_INTERCEPTOR_ID;
1625     return retCode;
1626 }
1627 
OH_Input_RemoveInputEventInterceptor(void)1628 Input_Result OH_Input_RemoveInputEventInterceptor(void)
1629 {
1630     CALL_DEBUG_ENTER;
1631     Input_Result retCode = INPUT_SUCCESS;
1632     std::lock_guard guard(g_mutex);
1633     int32_t ret = g_pointerInterceptor->Stop(OHOS::MMI::INTERCEPTOR_TYPE_POINTER);
1634     retCode = NormalizeResult(ret);
1635     if (retCode != INPUT_SUCCESS) {
1636         MMI_HILOGE("Remove pointer event interceptor failed.");
1637         return retCode;
1638     }
1639     g_pointerInterceptorId = INVALID_INTERCEPTOR_ID;
1640     g_pointerInterceptorCallback = nullptr;
1641     return retCode;
1642 }
1643 
OH_Input_CreateAllSystemHotkeys(int32_t count)1644 Input_Hotkey **OH_Input_CreateAllSystemHotkeys(int32_t count)
1645 {
1646     return nullptr;
1647 }
1648 
OH_Input_DestroyAllSystemHotkeys(Input_Hotkey ** hotkeys,int32_t count)1649 void OH_Input_DestroyAllSystemHotkeys(Input_Hotkey **hotkeys, int32_t count)
1650 {}
1651 
OH_Input_GetAllSystemHotkeys(Input_Hotkey ** hotkey,int32_t * count)1652 Input_Result OH_Input_GetAllSystemHotkeys(Input_Hotkey **hotkey, int32_t *count)
1653 {
1654     return INPUT_DEVICE_NOT_SUPPORTED;
1655 }
1656 
OH_Input_CreateHotkey(void)1657 Input_Hotkey* OH_Input_CreateHotkey(void)
1658 {
1659     return nullptr;
1660 }
1661 
OH_Input_DestroyHotkey(Input_Hotkey ** hotkey)1662 void OH_Input_DestroyHotkey(Input_Hotkey **hotkey)
1663 {}
1664 
OH_Input_SetPreKeys(Input_Hotkey * hotkey,int32_t * preKeys,int32_t size)1665 void OH_Input_SetPreKeys(Input_Hotkey *hotkey, int32_t *preKeys, int32_t size)
1666 {}
1667 
OH_Input_GetPreKeys(const Input_Hotkey * hotkey,int32_t ** preKeys,int32_t * preKeyCount)1668 Input_Result OH_Input_GetPreKeys(const Input_Hotkey *hotkey, int32_t **preKeys, int32_t *preKeyCount)
1669 {
1670     return INPUT_DEVICE_NOT_SUPPORTED;
1671 }
1672 
OH_Input_SetFinalKey(Input_Hotkey * hotkey,int32_t finalKey)1673 void OH_Input_SetFinalKey(Input_Hotkey *hotkey, int32_t finalKey)
1674 {}
1675 
OH_Input_GetFinalKey(const Input_Hotkey * hotkey,int32_t * finalKeyCode)1676 Input_Result OH_Input_GetFinalKey(const Input_Hotkey *hotkey, int32_t *finalKeyCode)
1677 {
1678     return INPUT_DEVICE_NOT_SUPPORTED;
1679 }
1680 
OH_Input_SetRepeat(Input_Hotkey * hotkey,bool isRepeat)1681 void OH_Input_SetRepeat(Input_Hotkey* hotkey, bool isRepeat)
1682 {}
1683 
OH_Input_GetRepeat(const Input_Hotkey * hotkey,bool * isRepeat)1684 Input_Result OH_Input_GetRepeat(const Input_Hotkey* hotkey, bool *isRepeat)
1685 {
1686     return INPUT_DEVICE_NOT_SUPPORTED;
1687 }
1688 
OH_Input_AddHotkeyMonitor(const Input_Hotkey * hotkey,Input_HotkeyCallback callback)1689 Input_Result OH_Input_AddHotkeyMonitor(const Input_Hotkey* hotkey, Input_HotkeyCallback callback)
1690 {
1691     return INPUT_DEVICE_NOT_SUPPORTED;
1692 }
1693 
OH_Input_RemoveHotkeyMonitor(const Input_Hotkey * hotkey,Input_HotkeyCallback callback)1694 Input_Result OH_Input_RemoveHotkeyMonitor(const Input_Hotkey *hotkey, Input_HotkeyCallback callback)
1695 {
1696     return INPUT_DEVICE_NOT_SUPPORTED;
1697 }
1698 
DeviceAddedCallback(int32_t deviceId,const std::string & Type)1699 static void DeviceAddedCallback(int32_t deviceId, const std::string& Type)
1700 {
1701     CALL_DEBUG_ENTER;
1702     std::lock_guard guard(g_DeviceListerCallbackMutex);
1703     for (auto listener : g_ohDeviceListenerList) {
1704         if (listener == nullptr) {
1705             MMI_HILOGE("listener is nullptr");
1706             continue;
1707         }
1708         if (listener->deviceAddedCallback == nullptr) {
1709             MMI_HILOGE("OnDeviceAdded is nullptr");
1710             continue;
1711         }
1712         listener->deviceAddedCallback(deviceId);
1713     }
1714 }
1715 
DeviceRemovedCallback(int32_t deviceId,const std::string & Type)1716 static void DeviceRemovedCallback(int32_t deviceId, const std::string& Type)
1717 {
1718     CALL_DEBUG_ENTER;
1719     std::lock_guard guard(g_DeviceListerCallbackMutex);
1720     for (auto listener : g_ohDeviceListenerList) {
1721         if (listener == nullptr) {
1722             MMI_HILOGE("listener is nullptr");
1723             continue;
1724         }
1725         if (listener->deviceRemovedCallback == nullptr) {
1726             MMI_HILOGE("OnDeviceRemoved is nullptr");
1727             continue;
1728         }
1729         listener->deviceRemovedCallback(deviceId);
1730     }
1731 }
1732 
OH_Input_RegisterDeviceListener(Input_DeviceListener * listener)1733 Input_Result OH_Input_RegisterDeviceListener(Input_DeviceListener* listener)
1734 {
1735     CALL_DEBUG_ENTER;
1736     if (listener == nullptr || listener->deviceAddedCallback == nullptr ||
1737         listener->deviceRemovedCallback == nullptr) {
1738         MMI_HILOGE("listener or callback is nullptr");
1739         return INPUT_PARAMETER_ERROR;
1740     }
1741     std::lock_guard guard(g_DeviceListerCallbackMutex);
1742     if (g_ohDeviceListenerList.empty()) {
1743         int32_t ret = OHOS::MMI::InputManager::GetInstance()->RegisterDevListener("change", g_deviceListener);
1744         g_deviceListener->SetDeviceAddedCallback(DeviceAddedCallback);
1745         g_deviceListener->SetDeviceRemovedCallback(DeviceRemovedCallback);
1746         if (ret != RET_OK) {
1747             MMI_HILOGE("RegisterDevListener fail");
1748             return INPUT_SERVICE_EXCEPTION;
1749         }
1750     }
1751     g_ohDeviceListenerList.insert(listener);
1752     return INPUT_SUCCESS;
1753 }
1754 
OH_Input_UnregisterDeviceListener(Input_DeviceListener * listener)1755 Input_Result OH_Input_UnregisterDeviceListener(Input_DeviceListener* listener)
1756 {
1757     CALL_DEBUG_ENTER;
1758     if (listener == nullptr) {
1759         MMI_HILOGE("listener is nullptr");
1760         return INPUT_PARAMETER_ERROR;
1761     }
1762     std::lock_guard guard(g_DeviceListerCallbackMutex);
1763     auto it = g_ohDeviceListenerList.find(listener);
1764     if (it == g_ohDeviceListenerList.end()) {
1765         MMI_HILOGE("listener not found");
1766         return INPUT_PARAMETER_ERROR;
1767     }
1768     g_ohDeviceListenerList.erase(it);
1769     if (g_ohDeviceListenerList.empty()) {
1770         int32_t ret = OHOS::MMI::InputManager::GetInstance()->UnregisterDevListener("change", g_deviceListener);
1771         if (ret != RET_OK) {
1772             MMI_HILOGE("UnregisterDevListener fail");
1773             return INPUT_SERVICE_EXCEPTION;
1774         }
1775     }
1776     return INPUT_SUCCESS;
1777 }
1778 
OH_Input_UnregisterDeviceListeners()1779 Input_Result OH_Input_UnregisterDeviceListeners()
1780 {
1781     CALL_DEBUG_ENTER;
1782     std::lock_guard guard(g_DeviceListerCallbackMutex);
1783     if (g_ohDeviceListenerList.empty()) {
1784         return INPUT_SUCCESS;
1785     }
1786     auto ret = OHOS::MMI::InputManager::GetInstance()->UnregisterDevListener("change", g_deviceListener);
1787     g_ohDeviceListenerList.clear();
1788     if (ret != RET_OK) {
1789         MMI_HILOGE("UnregisterDevListener fail");
1790         return INPUT_SERVICE_EXCEPTION;
1791     }
1792     return INPUT_SUCCESS;
1793 }
1794 
OH_Input_GetDeviceIds(int32_t * deviceIds,int32_t inSize,int32_t * outSize)1795 Input_Result OH_Input_GetDeviceIds(int32_t *deviceIds, int32_t inSize, int32_t *outSize)
1796 {
1797     CALL_DEBUG_ENTER;
1798     if (inSize < 0) {
1799         MMI_HILOGE("Invalid inSize:%{public}d", inSize);
1800         return INPUT_PARAMETER_ERROR;
1801     }
1802     CHKPR(deviceIds, INPUT_PARAMETER_ERROR);
1803     CHKPR(outSize, INPUT_PARAMETER_ERROR);
1804     auto nativeCallback = [&](std::vector<int32_t> &ids) {
1805         auto deviceIdslength = static_cast<int32_t>(ids.size());
1806         if (inSize > deviceIdslength) {
1807             *outSize = deviceIdslength;
1808         }
1809         if (inSize < deviceIdslength) {
1810             *outSize = inSize;
1811         }
1812         for (int32_t i = 0; i < *outSize; ++i) {
1813             *(deviceIds + i) = ids[i];
1814         }
1815     };
1816     int32_t ret = OHOS::MMI::InputManager::GetInstance()->GetDeviceIds(nativeCallback);
1817     if (ret != RET_OK) {
1818         MMI_HILOGE("GetDeviceIds fail");
1819         return INPUT_PARAMETER_ERROR;
1820     }
1821     return INPUT_SUCCESS;
1822 }
1823 
OH_Input_CreateDeviceInfo(void)1824 Input_DeviceInfo* OH_Input_CreateDeviceInfo(void)
1825 {
1826     CALL_DEBUG_ENTER;
1827     Input_DeviceInfo* deviceInfo = new (std::nothrow) Input_DeviceInfo();
1828     if (deviceInfo == nullptr) {
1829         MMI_HILOGE("deviceInfo is null");
1830         return nullptr;
1831     }
1832     return deviceInfo;
1833 }
1834 
OH_Input_DestroyDeviceInfo(Input_DeviceInfo ** deviceInfo)1835 void OH_Input_DestroyDeviceInfo(Input_DeviceInfo **deviceInfo)
1836 {
1837     CALL_DEBUG_ENTER;
1838     CHKPV(deviceInfo);
1839     CHKPV(*deviceInfo);
1840     delete *deviceInfo;
1841     *deviceInfo = nullptr;
1842 }
1843 
OH_Input_GetDevice(int32_t deviceId,Input_DeviceInfo ** deviceInfo)1844 Input_Result OH_Input_GetDevice(int32_t deviceId, Input_DeviceInfo **deviceInfo)
1845 {
1846     CALL_DEBUG_ENTER;
1847     if (deviceId < 0) {
1848         MMI_HILOGE("Invalid deviceId:%{public}d", deviceId);
1849         return INPUT_PARAMETER_ERROR;
1850     }
1851     CHKPR(*deviceInfo, INPUT_PARAMETER_ERROR);
1852     CHKPR(deviceInfo, INPUT_PARAMETER_ERROR);
1853     auto nativeCallback = [deviceInfo](std::shared_ptr<OHOS::MMI::InputDevice> device) {
1854         CHKPV(*deviceInfo);
1855         (*deviceInfo)->id = device->GetId();
1856         if (strcpy_s((*deviceInfo)->name, device->GetName().size() + 1, device->GetName().c_str()) != EOK) {
1857             MMI_HILOGE("strcpy_s error");
1858             return;
1859         }
1860         (*deviceInfo)->product = device->GetProduct();
1861         (*deviceInfo)->vendor = device->GetVendor();
1862         (*deviceInfo)->version = device->GetVersion();
1863         if (strcpy_s((*deviceInfo)->phys, device->GetPhys().size() + 1, device->GetPhys().c_str()) != EOK) {
1864             MMI_HILOGE("strcpy_s error");
1865             return;
1866         }
1867         (*deviceInfo)->ability = device->GetType();
1868     };
1869     int32_t ret = OHOS::MMI::InputManager::GetInstance()->GetDevice(deviceId, nativeCallback);
1870     if (ret != RET_OK) {
1871         MMI_HILOGE("GetDevice fail");
1872         return INPUT_PARAMETER_ERROR;
1873     }
1874     return INPUT_SUCCESS;
1875 }
1876 
OH_Input_GetKeyboardType(int32_t deviceId,int32_t * KeyboardType)1877 Input_Result OH_Input_GetKeyboardType(int32_t deviceId, int32_t *KeyboardType)
1878 {
1879     CALL_DEBUG_ENTER;
1880     if (deviceId < 0) {
1881         MMI_HILOGE("Invalid deviceId:%{public}d", deviceId);
1882         return INPUT_PARAMETER_ERROR;
1883     }
1884     CHKPR(KeyboardType, INPUT_PARAMETER_ERROR);
1885     auto nativeCallback = [KeyboardType](int32_t keyboardTypes) {
1886         *KeyboardType = keyboardTypes;
1887     };
1888     int32_t ret = OHOS::MMI::InputManager::GetInstance()->GetKeyboardType(deviceId, nativeCallback);
1889     if (ret != RET_OK) {
1890         MMI_HILOGE("GetKeyboardType fail");
1891         return INPUT_PARAMETER_ERROR;
1892     }
1893     return INPUT_SUCCESS;
1894 }
1895 
OH_Input_GetDeviceName(Input_DeviceInfo * deviceInfo,char ** name)1896 Input_Result OH_Input_GetDeviceName(Input_DeviceInfo *deviceInfo, char **name)
1897 {
1898     CALL_DEBUG_ENTER;
1899     CHKPR(deviceInfo, INPUT_PARAMETER_ERROR);
1900     CHKPR(name, INPUT_PARAMETER_ERROR);
1901     *name = deviceInfo->name;
1902     return INPUT_SUCCESS;
1903 }
1904 
1905 
OH_Input_GetDeviceAddress(Input_DeviceInfo * deviceInfo,char ** address)1906 Input_Result OH_Input_GetDeviceAddress(Input_DeviceInfo *deviceInfo, char **address)
1907 {
1908     CALL_DEBUG_ENTER;
1909     CHKPR(deviceInfo, INPUT_PARAMETER_ERROR);
1910     CHKPR(address, INPUT_PARAMETER_ERROR);
1911     *address = deviceInfo->phys;
1912     return INPUT_SUCCESS;
1913 }
1914 
OH_Input_GetDeviceId(Input_DeviceInfo * deviceInfo,int32_t * id)1915 Input_Result OH_Input_GetDeviceId(Input_DeviceInfo *deviceInfo, int32_t *id)
1916 {
1917     CALL_DEBUG_ENTER;
1918     CHKPR(deviceInfo, INPUT_PARAMETER_ERROR);
1919     CHKPR(id, INPUT_PARAMETER_ERROR);
1920     *id = deviceInfo->id;
1921     return INPUT_SUCCESS;
1922 }
1923 
OH_Input_GetCapabilities(Input_DeviceInfo * deviceInfo,int32_t * capabilities)1924 Input_Result OH_Input_GetCapabilities(Input_DeviceInfo *deviceInfo, int32_t *capabilities)
1925 {
1926     CALL_DEBUG_ENTER;
1927     CHKPR(deviceInfo, INPUT_PARAMETER_ERROR);
1928     CHKPR(capabilities, INPUT_PARAMETER_ERROR);
1929     *capabilities = deviceInfo->ability;
1930     return INPUT_SUCCESS;
1931 }
1932 
OH_Input_GetDeviceVersion(Input_DeviceInfo * deviceInfo,int32_t * version)1933 Input_Result OH_Input_GetDeviceVersion(Input_DeviceInfo *deviceInfo, int32_t *version)
1934 {
1935     CALL_DEBUG_ENTER;
1936     CHKPR(deviceInfo, INPUT_PARAMETER_ERROR);
1937     CHKPR(version, INPUT_PARAMETER_ERROR);
1938     *version = deviceInfo->version;
1939     return INPUT_SUCCESS;
1940 }
1941 
OH_Input_GetDeviceProduct(Input_DeviceInfo * deviceInfo,int32_t * product)1942 Input_Result OH_Input_GetDeviceProduct(Input_DeviceInfo *deviceInfo, int32_t *product)
1943 {
1944     CALL_DEBUG_ENTER;
1945     CHKPR(deviceInfo, INPUT_PARAMETER_ERROR);
1946     CHKPR(product, INPUT_PARAMETER_ERROR);
1947     *product = deviceInfo->product;
1948     return INPUT_SUCCESS;
1949 }
1950 
OH_Input_GetDeviceVendor(Input_DeviceInfo * deviceInfo,int32_t * vendor)1951 Input_Result OH_Input_GetDeviceVendor(Input_DeviceInfo *deviceInfo, int32_t *vendor)
1952 {
1953     CALL_DEBUG_ENTER;
1954     CHKPR(deviceInfo, INPUT_PARAMETER_ERROR);
1955     CHKPR(vendor, INPUT_PARAMETER_ERROR);
1956     *vendor = deviceInfo->vendor;
1957     return INPUT_SUCCESS;
1958 }
1959 
OH_Input_GetIntervalSinceLastInput(int64_t * intervalSinceLastInput)1960 int32_t OH_Input_GetIntervalSinceLastInput(int64_t *intervalSinceLastInput)
1961 {
1962     CALL_DEBUG_ENTER;
1963     CHKPR(intervalSinceLastInput, INPUT_PARAMETER_ERROR);
1964     int64_t interval = -1;
1965     int32_t ret = OHOS::MMI::InputManager::GetInstance()->GetIntervalSinceLastInput(interval);
1966     *intervalSinceLastInput = interval;
1967     Input_Result retCode = INPUT_SUCCESS;
1968     retCode = NormalizeResult(ret);
1969     if (retCode != INPUT_SUCCESS) {
1970         MMI_HILOGE("Get Interval Since Last Input failed");
1971         return retCode;
1972     }
1973     return INPUT_SUCCESS;
1974 }