1 /*
2 * Copyright (c) 2024-2025 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 "securec.h"
19
20 #include "event_log_helper.h"
21 #include "input_manager.h"
22 #include "input_manager_impl.h"
23 #include "oh_input_device_listener.h"
24 #include "oh_input_interceptor.h"
25 #include "oh_key_code.h"
26 #include "permission_helper.h"
27 #include "pointer_event_ndk.h"
28 #ifdef PLAYER_FRAMEWORK_EXISTS
29 #include "screen_capture_monitor.h"
30 #include "ipc_skeleton.h"
31 #endif // PLAYER_FRAMEWORK_EXISTS
32
33 #undef MMI_LOG_TAG
34 #define MMI_LOG_TAG "OHInputManager"
35
36 struct Input_KeyState {
37 int32_t keyCode;
38 int32_t keyState;
39 int32_t keySwitch;
40 };
41
42 struct Input_KeyEvent {
43 int32_t action;
44 int32_t keyCode;
45 int64_t actionTime { -1 };
46 int32_t windowId { -1 };
47 int32_t displayId { -1 };
48 };
49
50 struct Input_MouseEvent {
51 int32_t action;
52 int32_t displayX;
53 int32_t displayY;
54 int32_t globalX { INT32_MAX };
55 int32_t globalY { INT32_MAX };
56 int32_t button { -1 };
57 int32_t axisType { -1 };
58 float axisValue { 0.0f };
59 int64_t actionTime { -1 };
60 int32_t windowId { -1 };
61 int32_t displayId { -1 };
62 };
63
64 struct Input_TouchEvent {
65 int32_t action;
66 int32_t id;
67 int32_t displayX;
68 int32_t displayY;
69 int32_t globalX { INT32_MAX };
70 int32_t globalY { INT32_MAX };
71 int64_t actionTime { -1 };
72 int32_t windowId { -1 };
73 int32_t displayId { -1 };
74 };
75
76 struct Input_AxisEvent {
77 int32_t axisAction;
78 float displayX;
79 float displayY;
80 int32_t globalX { INT32_MAX };
81 int32_t globalY { INT32_MAX };
82 std::map<int32_t, double> axisValues;
83 int64_t actionTime { -1 };
84 int32_t sourceType;
85 int32_t axisEventType { -1 };
86 int32_t windowId { -1 };
87 int32_t displayId { -1 };
88 };
89
90 struct Input_HotkeyInfo {
91 int32_t subscribeId;
92 std::string hotkeyId;
93 Input_HotkeyCallback callback { nullptr };
94 std::shared_ptr<OHOS::MMI::KeyOption> keyOption { nullptr };
95 };
96
97 struct Input_Hotkey {
98 std::set<int32_t> preKeys {};
99 int32_t finalKey { -1 };
100 bool isRepeat { true };
101 };
102
103 constexpr int32_t SIZE_ARRAY = 64;
104 struct Input_DeviceInfo {
105 int32_t id { -1 };
106 char name[SIZE_ARRAY] {};
107 int32_t ability { -1 };
108 int32_t product { -1 };
109 int32_t vendor { -1 };
110 int32_t version { -1 };
111 char phys[SIZE_ARRAY] {};
112 };
113
114 typedef std::map<std::string, std::list<Input_HotkeyInfo *>> Callbacks;
115 static Callbacks g_callbacks = {};
116 static std::mutex g_CallBacksMutex;
117 static constexpr size_t PRE_KEYS_SIZE { 4 };
118 static constexpr size_t KEYS_SIZE { 3 };
119 static std::mutex g_hotkeyCountsMutex;
120 static std::unordered_map<Input_Hotkey**, int32_t> g_hotkeyCounts;
121 static constexpr int32_t INVALID_MONITOR_ID = -1;
122 static constexpr int32_t INVALID_INTERCEPTOR_ID = -1;
123 static constexpr int32_t OCCUPIED_BY_SYSTEM = -3;
124 static constexpr int32_t OCCUPIED_BY_OTHER = -4;
125 static constexpr int32_t SIMULATE_POINTER_EVENT_START_ID { 30000 };
126 static std::shared_ptr<OHOS::MMI::KeyEvent> g_keyEvent = OHOS::MMI::KeyEvent::Create();
127 static std::shared_ptr<OHOS::MMI::PointerEvent> g_mouseEvent = OHOS::MMI::PointerEvent::Create();
128 static std::shared_ptr<OHOS::MMI::PointerEvent> g_touchEvent = OHOS::MMI::PointerEvent::Create();
129 static const std::set<int32_t> g_keyCodeValueSet = {
130 KEYCODE_FN, KEYCODE_DPAD_UP, KEYCODE_DPAD_DOWN, KEYCODE_DPAD_LEFT, KEYCODE_DPAD_RIGHT, KEYCODE_ALT_LEFT,
131 KEYCODE_ALT_RIGHT, KEYCODE_SHIFT_LEFT, KEYCODE_SHIFT_RIGHT, KEYCODE_TAB, KEYCODE_ENTER, KEYCODE_DEL, KEYCODE_MENU,
132 KEYCODE_PAGE_UP, KEYCODE_PAGE_DOWN, KEYCODE_ESCAPE, KEYCODE_FORWARD_DEL, KEYCODE_CTRL_LEFT, KEYCODE_CTRL_RIGHT,
133 KEYCODE_CAPS_LOCK, KEYCODE_SCROLL_LOCK, KEYCODE_META_LEFT, KEYCODE_META_RIGHT, KEYCODE_SYSRQ, KEYCODE_BREAK,
134 KEYCODE_MOVE_HOME, KEYCODE_MOVE_END, KEYCODE_INSERT, KEYCODE_F1, KEYCODE_F2, KEYCODE_F3, KEYCODE_F4, KEYCODE_F5,
135 KEYCODE_F6, KEYCODE_F7, KEYCODE_F8, KEYCODE_F9, KEYCODE_F10, KEYCODE_F11, KEYCODE_F12, KEYCODE_NUM_LOCK
136 };
137 static std::set<Input_KeyEventCallback> g_keyMonitorCallbacks;
138 static std::set<Input_MouseEventCallback> g_mouseMonitorCallbacks;
139 static std::set<Input_TouchEventCallback> g_touchMonitorCallbacks;
140 static std::set<Input_AxisEventCallback> g_axisMonitorAllCallbacks;
141 static std::set<Input_DeviceListener*> g_ohDeviceListenerList;
142 static std::map<InputEvent_AxisEventType, std::set<Input_AxisEventCallback>> g_axisMonitorCallbacks;
143 static Input_KeyEventCallback g_keyInterceptorCallback = nullptr;
144 static struct Input_InterceptorEventCallback *g_pointerInterceptorCallback = nullptr;
145 static std::shared_ptr<OHOS::MMI::OHInputInterceptor> g_pointerInterceptor =
146 std::make_shared<OHOS::MMI::OHInputInterceptor>();
147 static std::shared_ptr<OHOS::MMI::OHInputInterceptor> g_keyInterceptor =
148 std::make_shared<OHOS::MMI::OHInputInterceptor>();
149 static std::shared_ptr<OHOS::MMI::OHInputDeviceListener> g_deviceListener =
150 std::make_shared<OHOS::MMI::OHInputDeviceListener>();
151 static std::mutex g_DeviceListerCallbackMutex;
152 static std::mutex g_mutex;
153 static int32_t g_keyMonitorId = INVALID_MONITOR_ID;
154 static int32_t g_pointerMonitorId = INVALID_MONITOR_ID;
155 static int32_t g_keyInterceptorId = INVALID_INTERCEPTOR_ID;
156 static int32_t g_pointerInterceptorId = INVALID_INTERCEPTOR_ID;
157 static int32_t UNKNOWN_MAX_TOUCH_POINTS { -1 };
158
159 static const std::vector<int32_t> g_pressKeyCodes = {
160 OHOS::MMI::KeyEvent::KEYCODE_ALT_LEFT,
161 OHOS::MMI::KeyEvent::KEYCODE_ALT_RIGHT,
162 OHOS::MMI::KeyEvent::KEYCODE_SHIFT_LEFT,
163 OHOS::MMI::KeyEvent::KEYCODE_SHIFT_RIGHT,
164 OHOS::MMI::KeyEvent::KEYCODE_CTRL_LEFT,
165 OHOS::MMI::KeyEvent::KEYCODE_CTRL_RIGHT
166 };
167 static const std::vector<int32_t> g_finalKeyCodes = {
168 OHOS::MMI::KeyEvent::KEYCODE_ALT_LEFT,
169 OHOS::MMI::KeyEvent::KEYCODE_ALT_RIGHT,
170 OHOS::MMI::KeyEvent::KEYCODE_SHIFT_LEFT,
171 OHOS::MMI::KeyEvent::KEYCODE_SHIFT_RIGHT,
172 OHOS::MMI::KeyEvent::KEYCODE_CTRL_LEFT,
173 OHOS::MMI::KeyEvent::KEYCODE_CTRL_RIGHT,
174 OHOS::MMI::KeyEvent::KEYCODE_META_LEFT,
175 OHOS::MMI::KeyEvent::KEYCODE_META_RIGHT
176 };
177 using OHOS::MMI::AUTHORIZE_QUERY_STATE;
178
OH_Input_GetKeyState(struct Input_KeyState * keyState)179 Input_Result OH_Input_GetKeyState(struct Input_KeyState* keyState)
180 {
181 CALL_DEBUG_ENTER;
182 CHKPR(keyState, INPUT_PARAMETER_ERROR);
183 if (keyState->keyCode < 0 || keyState->keyCode > KEYCODE_NUMPAD_RIGHT_PAREN) {
184 if (!OHOS::MMI::EventLogHelper::IsBetaVersion()) {
185 MMI_HILOGE("Invaild");
186 } else {
187 MMI_HILOGE("Invaild");
188 }
189 return INPUT_PARAMETER_ERROR;
190 }
191 if (g_keyCodeValueSet.find(keyState->keyCode) == g_keyCodeValueSet.end()) {
192 MMI_HILOGE("code is not within the query range:%{private}d", keyState->keyCode);
193 return INPUT_PARAMETER_ERROR;
194 }
195 std::vector<int32_t> pressedKeys;
196 std::map<int32_t, int32_t> specialKeysState;
197 OHOS::MMI::InputManager::GetInstance()->GetKeyState(pressedKeys, specialKeysState);
198 auto iter = std::find(pressedKeys.begin(), pressedKeys.end(), keyState->keyCode);
199 if (iter != pressedKeys.end()) {
200 keyState->keyState = KEY_PRESSED;
201 } else {
202 keyState->keyState = KEY_RELEASED;
203 }
204 auto itr = specialKeysState.find(keyState->keyCode);
205 if (itr != specialKeysState.end()) {
206 if (itr->second == 0) {
207 keyState->keySwitch = KEY_SWITCH_OFF;
208 } else {
209 keyState->keySwitch = KEY_SWITCH_ON;
210 }
211 } else {
212 keyState->keySwitch = KEY_DEFAULT;
213 }
214 return INPUT_SUCCESS;
215 }
216
OH_Input_CreateKeyState()217 struct Input_KeyState* OH_Input_CreateKeyState()
218 {
219 Input_KeyState* keyState = new (std::nothrow) Input_KeyState();
220 CHKPL(keyState);
221 return keyState;
222 }
223
OH_Input_DestroyKeyState(struct Input_KeyState ** keyState)224 void OH_Input_DestroyKeyState(struct Input_KeyState** keyState)
225 {
226 CALL_DEBUG_ENTER;
227 CHKPV(keyState);
228 CHKPV(*keyState);
229 delete *keyState;
230 *keyState = nullptr;
231 }
232
OH_Input_SetKeyCode(struct Input_KeyState * keyState,int32_t keyCode)233 void OH_Input_SetKeyCode(struct Input_KeyState* keyState, int32_t keyCode)
234 {
235 CHKPV(keyState);
236 if (keyCode < 0 || keyState->keyCode > KEYCODE_NUMPAD_RIGHT_PAREN) {
237 if (!OHOS::MMI::EventLogHelper::IsBetaVersion()) {
238 MMI_HILOGE("Invaild");
239 } else {
240 MMI_HILOGE("Invaild");
241 }
242 return;
243 }
244 keyState->keyCode = keyCode;
245 }
246
OH_Input_GetKeyCode(const struct Input_KeyState * keyState)247 int32_t OH_Input_GetKeyCode(const struct Input_KeyState* keyState)
248 {
249 CHKPR(keyState, KEYCODE_UNKNOWN);
250 return keyState->keyCode;
251 }
252
OH_Input_SetKeyPressed(struct Input_KeyState * keyState,int32_t keyAction)253 void OH_Input_SetKeyPressed(struct Input_KeyState* keyState, int32_t keyAction)
254 {
255 CHKPV(keyState);
256 keyState->keyState = keyAction;
257 }
258
OH_Input_GetKeyPressed(const struct Input_KeyState * keyState)259 int32_t OH_Input_GetKeyPressed(const struct Input_KeyState* keyState)
260 {
261 CHKPR(keyState, KEY_DEFAULT);
262 return keyState->keyState;
263 }
264
OH_Input_SetKeySwitch(struct Input_KeyState * keyState,int32_t keySwitch)265 void OH_Input_SetKeySwitch(struct Input_KeyState* keyState, int32_t keySwitch)
266 {
267 CHKPV(keyState);
268 keyState->keySwitch = keySwitch;
269 }
270
OH_Input_GetKeySwitch(const struct Input_KeyState * keyState)271 int32_t OH_Input_GetKeySwitch(const struct Input_KeyState* keyState)
272 {
273 CHKPR(keyState, KEY_DEFAULT);
274 return keyState->keySwitch;
275 }
276
HandleKeyAction(const struct Input_KeyEvent * keyEvent,OHOS::MMI::KeyEvent::KeyItem & item)277 static void HandleKeyAction(const struct Input_KeyEvent* keyEvent, OHOS::MMI::KeyEvent::KeyItem &item)
278 {
279 if (keyEvent->action == KEY_ACTION_DOWN) {
280 g_keyEvent->AddPressedKeyItems(item);
281 }
282 if (keyEvent->action == KEY_ACTION_UP) {
283 std::optional<OHOS::MMI::KeyEvent::KeyItem> pressedKeyItem = g_keyEvent->GetKeyItem(keyEvent->keyCode);
284 if (pressedKeyItem) {
285 item.SetDownTime(pressedKeyItem->GetDownTime());
286 } else if (!OHOS::MMI::EventLogHelper::IsBetaVersion()) {
287 MMI_HILOGW("Find pressed key failed");
288 } else {
289 MMI_HILOGW("Find pressed key failed");
290 }
291 g_keyEvent->RemoveReleasedKeyItems(item);
292 g_keyEvent->AddPressedKeyItems(item);
293 }
294 }
295
OH_Input_InjectKeyEvent(const struct Input_KeyEvent * keyEvent)296 int32_t OH_Input_InjectKeyEvent(const struct Input_KeyEvent* keyEvent)
297 {
298 MMI_HILOGI("Input_KeyEvent injectEvent");
299 CHKPR(keyEvent, INPUT_PARAMETER_ERROR);
300 if (keyEvent->keyCode < 0) {
301 if (!OHOS::MMI::EventLogHelper::IsBetaVersion()) {
302 MMI_HILOGE("code is less 0, can not process");
303 } else {
304 MMI_HILOGE("code is less 0, can not process");
305 }
306 return INPUT_PARAMETER_ERROR;
307 }
308 CHKPR(g_keyEvent, INPUT_PARAMETER_ERROR);
309 g_keyEvent->ClearFlag();
310 if (g_keyEvent->GetAction() == OHOS::MMI::KeyEvent::KEY_ACTION_UP) {
311 std::optional<OHOS::MMI::KeyEvent::KeyItem> preUpKeyItem = g_keyEvent->GetKeyItem();
312 if (preUpKeyItem) {
313 g_keyEvent->RemoveReleasedKeyItems(*preUpKeyItem);
314 } else {
315 MMI_HILOGE("The preUpKeyItem is nullopt");
316 }
317 }
318 int64_t time = keyEvent->actionTime;
319 if (time < 0) {
320 time = OHOS::MMI::GetSysClockTime();
321 }
322 g_keyEvent->SetActionTime(time);
323 g_keyEvent->SetRepeat(true);
324 g_keyEvent->SetKeyCode(keyEvent->keyCode);
325 bool isKeyPressed = false;
326 if (keyEvent->action == KEY_ACTION_DOWN) {
327 g_keyEvent->SetAction(OHOS::MMI::KeyEvent::KEY_ACTION_DOWN);
328 g_keyEvent->SetKeyAction(OHOS::MMI::KeyEvent::KEY_ACTION_DOWN);
329 isKeyPressed = true;
330 } else if (keyEvent->action == KEY_ACTION_UP) {
331 g_keyEvent->SetAction(OHOS::MMI::KeyEvent::KEY_ACTION_UP);
332 g_keyEvent->SetKeyAction(OHOS::MMI::KeyEvent::KEY_ACTION_UP);
333 isKeyPressed = false;
334 }
335 OHOS::MMI::KeyEvent::KeyItem item;
336 item.SetDownTime(time);
337 item.SetKeyCode(keyEvent->keyCode);
338 item.SetPressed(isKeyPressed);
339 HandleKeyAction(keyEvent, item);
340 g_keyEvent->AddFlag(OHOS::MMI::InputEvent::EVENT_FLAG_SIMULATE);
341 OHOS::Singleton<OHOS::MMI::InputManagerImpl>::GetInstance().SimulateInputEvent(g_keyEvent, true);
342 return INPUT_SUCCESS;
343 }
344
OH_Input_CreateKeyEvent()345 struct Input_KeyEvent* OH_Input_CreateKeyEvent()
346 {
347 Input_KeyEvent* keyEvent = new (std::nothrow) Input_KeyEvent();
348 CHKPL(keyEvent);
349 return keyEvent;
350 }
351
OH_Input_DestroyKeyEvent(struct Input_KeyEvent ** keyEvent)352 void OH_Input_DestroyKeyEvent(struct Input_KeyEvent** keyEvent)
353 {
354 CALL_DEBUG_ENTER;
355 CHKPV(keyEvent);
356 CHKPV(*keyEvent);
357 delete *keyEvent;
358 *keyEvent = nullptr;
359 }
360
OH_Input_SetKeyEventAction(struct Input_KeyEvent * keyEvent,int32_t action)361 void OH_Input_SetKeyEventAction(struct Input_KeyEvent* keyEvent, int32_t action)
362 {
363 CHKPV(keyEvent);
364 keyEvent->action = action;
365 }
366
OH_Input_GetKeyEventAction(const struct Input_KeyEvent * keyEvent)367 int32_t OH_Input_GetKeyEventAction(const struct Input_KeyEvent* keyEvent)
368 {
369 CHKPR(keyEvent, RET_ERR);
370 return keyEvent->action;
371 }
372
OH_Input_SetKeyEventKeyCode(struct Input_KeyEvent * keyEvent,int32_t keyCode)373 void OH_Input_SetKeyEventKeyCode(struct Input_KeyEvent* keyEvent, int32_t keyCode)
374 {
375 CHKPV(keyEvent);
376 keyEvent->keyCode = keyCode;
377 }
378
OH_Input_GetKeyEventKeyCode(const struct Input_KeyEvent * keyEvent)379 int32_t OH_Input_GetKeyEventKeyCode(const struct Input_KeyEvent* keyEvent)
380 {
381 CHKPR(keyEvent, KEYCODE_UNKNOWN);
382 return keyEvent->keyCode;
383 }
384
OH_Input_SetKeyEventActionTime(struct Input_KeyEvent * keyEvent,int64_t actionTime)385 void OH_Input_SetKeyEventActionTime(struct Input_KeyEvent* keyEvent, int64_t actionTime)
386 {
387 CHKPV(keyEvent);
388 keyEvent->actionTime = actionTime;
389 }
390
OH_Input_GetKeyEventActionTime(const struct Input_KeyEvent * keyEvent)391 int64_t OH_Input_GetKeyEventActionTime(const struct Input_KeyEvent* keyEvent)
392 {
393 CHKPR(keyEvent, RET_ERR);
394 return keyEvent->actionTime;
395 }
396
OH_Input_SetKeyEventWindowId(struct Input_KeyEvent * keyEvent,int32_t windowId)397 void OH_Input_SetKeyEventWindowId(struct Input_KeyEvent* keyEvent, int32_t windowId)
398 {
399 CHKPV(keyEvent);
400 keyEvent->windowId = windowId;
401 }
402
OH_Input_GetKeyEventWindowId(const struct Input_KeyEvent * keyEvent)403 int32_t OH_Input_GetKeyEventWindowId(const struct Input_KeyEvent* keyEvent)
404 {
405 CHKPR(keyEvent, RET_ERR);
406 return keyEvent->windowId;
407 }
408
OH_Input_SetKeyEventDisplayId(struct Input_KeyEvent * keyEvent,int32_t displayId)409 void OH_Input_SetKeyEventDisplayId(struct Input_KeyEvent* keyEvent, int32_t displayId)
410 {
411 CHKPV(keyEvent);
412 keyEvent->displayId = displayId;
413 }
414
OH_Input_GetKeyEventDisplayId(const struct Input_KeyEvent * keyEvent)415 int32_t OH_Input_GetKeyEventDisplayId(const struct Input_KeyEvent* keyEvent)
416 {
417 CHKPR(keyEvent, RET_ERR);
418 return keyEvent->displayId;
419 }
420
HandleMouseButton(const struct Input_MouseEvent * mouseEvent)421 static int32_t HandleMouseButton(const struct Input_MouseEvent* mouseEvent)
422 {
423 int32_t button = mouseEvent->button;
424 switch (button) {
425 case MOUSE_BUTTON_NONE: {
426 button = OHOS::MMI::PointerEvent::BUTTON_NONE;
427 break;
428 }
429 case MOUSE_BUTTON_LEFT: {
430 button = OHOS::MMI::PointerEvent::MOUSE_BUTTON_LEFT;
431 break;
432 }
433 case MOUSE_BUTTON_MIDDLE: {
434 button = OHOS::MMI::PointerEvent::MOUSE_BUTTON_MIDDLE;
435 break;
436 }
437 case MOUSE_BUTTON_RIGHT: {
438 button = OHOS::MMI::PointerEvent::MOUSE_BUTTON_RIGHT;
439 break;
440 }
441 case MOUSE_BUTTON_FORWARD: {
442 button = OHOS::MMI::PointerEvent::MOUSE_BUTTON_FORWARD;
443 break;
444 }
445 case MOUSE_BUTTON_BACK: {
446 button = OHOS::MMI::PointerEvent::MOUSE_BUTTON_BACK;
447 break;
448 }
449 default: {
450 MMI_HILOGE("button:%{public}d is invalid", button);
451 return INPUT_PARAMETER_ERROR;
452 }
453 }
454 if (mouseEvent->action == MOUSE_ACTION_BUTTON_DOWN) {
455 g_mouseEvent->SetButtonPressed(button);
456 } else if (mouseEvent->action == MOUSE_ACTION_BUTTON_UP) {
457 g_mouseEvent->DeleteReleaseButton(button);
458 }
459 g_mouseEvent->SetButtonId(button);
460 return INPUT_SUCCESS;
461 }
462
HandleMouseAction(const struct Input_MouseEvent * mouseEvent,OHOS::MMI::PointerEvent::PointerItem & item)463 static int32_t HandleMouseAction(const struct Input_MouseEvent* mouseEvent, OHOS::MMI::PointerEvent::PointerItem &item)
464 {
465 switch (mouseEvent->action) {
466 case MOUSE_ACTION_CANCEL:
467 g_mouseEvent->SetPointerAction(OHOS::MMI::PointerEvent::POINTER_ACTION_CANCEL);
468 break;
469 case MOUSE_ACTION_MOVE:
470 g_mouseEvent->SetPointerAction(OHOS::MMI::PointerEvent::POINTER_ACTION_MOVE);
471 break;
472 case MOUSE_ACTION_BUTTON_DOWN:
473 g_mouseEvent->SetPointerAction(OHOS::MMI::PointerEvent::POINTER_ACTION_BUTTON_DOWN);
474 item.SetPressed(true);
475 break;
476 case MOUSE_ACTION_BUTTON_UP:
477 g_mouseEvent->SetPointerAction(OHOS::MMI::PointerEvent::POINTER_ACTION_BUTTON_UP);
478 item.SetPressed(false);
479 break;
480 case MOUSE_ACTION_AXIS_BEGIN:
481 g_mouseEvent->SetPointerAction(OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_BEGIN);
482 break;
483 case MOUSE_ACTION_AXIS_UPDATE:
484 g_mouseEvent->SetPointerAction(OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_UPDATE);
485 break;
486 case MOUSE_ACTION_AXIS_END:
487 g_mouseEvent->SetPointerAction(OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_END);
488 break;
489 default:
490 MMI_HILOGE("The action:%{public}d is invalid", mouseEvent->action);
491 return INPUT_PARAMETER_ERROR;
492 }
493 if (mouseEvent->axisType == MOUSE_AXIS_SCROLL_VERTICAL) {
494 g_mouseEvent->SetAxisValue(OHOS::MMI::PointerEvent::AXIS_TYPE_SCROLL_VERTICAL, mouseEvent->axisValue);
495 }
496 if (mouseEvent->axisType == MOUSE_AXIS_SCROLL_HORIZONTAL) {
497 g_mouseEvent->SetAxisValue(OHOS::MMI::PointerEvent::AXIS_TYPE_SCROLL_HORIZONTAL, mouseEvent->axisValue);
498 }
499 return HandleMouseButton(mouseEvent);
500 }
501
HandleMouseProperty(const struct Input_MouseEvent * mouseEvent,OHOS::MMI::PointerEvent::PointerItem & item)502 static int32_t HandleMouseProperty(const struct Input_MouseEvent* mouseEvent,
503 OHOS::MMI::PointerEvent::PointerItem &item)
504 {
505 int32_t screenX = mouseEvent->displayX;
506 int32_t screenY = mouseEvent->displayY;
507 g_mouseEvent->SetSourceType(OHOS::MMI::PointerEvent::SOURCE_TYPE_MOUSE);
508 item.SetPointerId(0);
509 item.SetDisplayX(screenX);
510 item.SetDisplayY(screenY);
511 item.SetDisplayXPos(screenX);
512 item.SetDisplayYPos(screenY);
513 int32_t globalX = mouseEvent->globalX;
514 int32_t globalY = mouseEvent->globalY;
515 if (globalX != INT32_MAX && globalY != INT32_MAX) {
516 item.SetGlobalX(globalX);
517 item.SetGlobalY(globalY);
518 } else {
519 item.SetGlobalX(DBL_MAX);
520 item.SetGlobalY(DBL_MAX);
521 }
522 g_mouseEvent->SetPointerId(0);
523 g_mouseEvent->UpdatePointerItem(g_mouseEvent->GetPointerId(), item);
524 return INPUT_SUCCESS;
525 }
526
OH_Input_InjectMouseEvent(const struct Input_MouseEvent * mouseEvent)527 int32_t OH_Input_InjectMouseEvent(const struct Input_MouseEvent* mouseEvent)
528 {
529 MMI_HILOGI("Input_MouseEvent injectEvent");
530 CHKPR(mouseEvent, INPUT_PARAMETER_ERROR);
531 CHKPR(g_mouseEvent, INPUT_PARAMETER_ERROR);
532 g_mouseEvent->ClearFlag();
533 g_mouseEvent->ClearAxisValue();
534 if (mouseEvent->displayId <= 0) {
535 g_mouseEvent->SetTargetDisplayId(0);
536 } else {
537 MMI_HILOGI("{%{public}d}", mouseEvent->displayId);
538 g_mouseEvent->SetTargetDisplayId(mouseEvent->displayId);
539 }
540 int64_t time = mouseEvent->actionTime;
541 if (time < 0) {
542 time = OHOS::MMI::GetSysClockTime();
543 }
544 g_mouseEvent->SetActionTime(time);
545 OHOS::MMI::PointerEvent::PointerItem item;
546 int32_t pointerId = 10000;
547 g_mouseEvent->GetPointerItem(pointerId, item);
548 item.SetDownTime(time);
549 int32_t result = HandleMouseAction(mouseEvent, item);
550 if (result != 0) {
551 return result;
552 }
553 result = HandleMouseProperty(mouseEvent, item);
554 if (result != 0) {
555 return result;
556 }
557 g_mouseEvent->AddFlag(OHOS::MMI::InputEvent::EVENT_FLAG_SIMULATE);
558 result = OHOS::Singleton<OHOS::MMI::InputManagerImpl>::GetInstance().SimulateInputEvent(g_mouseEvent,
559 true, PointerEvent::DISPLAY_COORDINATE);
560 if ((result == INPUT_PERMISSION_DENIED) || (result == INPUT_OCCUPIED_BY_OTHER)) {
561 MMI_HILOGE("Permission denied or occupied by other");
562 return result;
563 }
564 return INPUT_SUCCESS;
565 }
566
OH_Input_InjectMouseEventGlobal(const struct Input_MouseEvent * mouseEvent)567 int32_t OH_Input_InjectMouseEventGlobal(const struct Input_MouseEvent* mouseEvent)
568 {
569 MMI_HILOGD("Input_MouseEvent global");
570 CHKPR(mouseEvent, INPUT_PARAMETER_ERROR);
571 CHKPR(g_mouseEvent, INPUT_PARAMETER_ERROR);
572 g_mouseEvent->ClearFlag();
573 g_mouseEvent->ClearAxisValue();
574 g_mouseEvent->SetTargetDisplayId(0);
575 int64_t time = mouseEvent->actionTime;
576 if (time < 0) {
577 time = OHOS::MMI::GetSysClockTime();
578 }
579 g_mouseEvent->SetActionTime(time);
580 OHOS::MMI::PointerEvent::PointerItem item;
581 int32_t pointerId = 10000;
582 g_mouseEvent->GetPointerItem(pointerId, item);
583 item.SetDownTime(time);
584 int32_t result = HandleMouseAction(mouseEvent, item);
585 if (result != 0) {
586 return result;
587 }
588 result = HandleMouseProperty(mouseEvent, item);
589 if (result != 0) {
590 return result;
591 }
592 if (!item.IsValidGlobalXY()) {
593 return INPUT_PARAMETER_ERROR;
594 }
595 g_mouseEvent->AddFlag(OHOS::MMI::InputEvent::EVENT_FLAG_SIMULATE);
596 result = OHOS::Singleton<OHOS::MMI::InputManagerImpl>::GetInstance().SimulateInputEvent(g_mouseEvent,
597 true, PointerEvent::GLOBAL_COORDINATE);
598 if ((result == INPUT_PERMISSION_DENIED) || (result == INPUT_OCCUPIED_BY_OTHER)) {
599 MMI_HILOGE("Permission denied or occupied by other");
600 return INPUT_PERMISSION_DENIED;
601 }
602 return INPUT_SUCCESS;
603 }
604
OH_Input_CreateMouseEvent()605 struct Input_MouseEvent* OH_Input_CreateMouseEvent()
606 {
607 CALL_DEBUG_ENTER;
608 Input_MouseEvent* mouseEvent = new (std::nothrow) Input_MouseEvent();
609 CHKPL(mouseEvent);
610 return mouseEvent;
611 }
612
OH_Input_DestroyMouseEvent(struct Input_MouseEvent ** mouseEvent)613 void OH_Input_DestroyMouseEvent(struct Input_MouseEvent** mouseEvent)
614 {
615 CALL_DEBUG_ENTER;
616 CHKPV(mouseEvent);
617 CHKPV(*mouseEvent);
618 delete *mouseEvent;
619 *mouseEvent = nullptr;
620 }
621
OH_Input_SetMouseEventAction(struct Input_MouseEvent * mouseEvent,int32_t action)622 void OH_Input_SetMouseEventAction(struct Input_MouseEvent* mouseEvent, int32_t action)
623 {
624 CALL_DEBUG_ENTER;
625 CHKPV(mouseEvent);
626 mouseEvent->action = action;
627 }
628
OH_Input_GetMouseEventAction(const struct Input_MouseEvent * mouseEvent)629 int32_t OH_Input_GetMouseEventAction(const struct Input_MouseEvent* mouseEvent)
630 {
631 CALL_DEBUG_ENTER;
632 CHKPR(mouseEvent, RET_ERR);
633 return mouseEvent->action;
634 }
635
OH_Input_SetMouseEventDisplayX(struct Input_MouseEvent * mouseEvent,int32_t displayX)636 void OH_Input_SetMouseEventDisplayX(struct Input_MouseEvent* mouseEvent, int32_t displayX)
637 {
638 CALL_DEBUG_ENTER;
639 CHKPV(mouseEvent);
640 mouseEvent->displayX = displayX;
641 }
642
OH_Input_GetMouseEventDisplayX(const struct Input_MouseEvent * mouseEvent)643 int32_t OH_Input_GetMouseEventDisplayX(const struct Input_MouseEvent* mouseEvent)
644 {
645 CALL_DEBUG_ENTER;
646 CHKPR(mouseEvent, RET_ERR);
647 return mouseEvent->displayX;
648 }
649
OH_Input_SetMouseEventDisplayY(struct Input_MouseEvent * mouseEvent,int32_t displayY)650 void OH_Input_SetMouseEventDisplayY(struct Input_MouseEvent* mouseEvent, int32_t displayY)
651 {
652 CALL_DEBUG_ENTER;
653 CHKPV(mouseEvent);
654 mouseEvent->displayY = displayY;
655 }
656
OH_Input_GetMouseEventDisplayY(const struct Input_MouseEvent * mouseEvent)657 int32_t OH_Input_GetMouseEventDisplayY(const struct Input_MouseEvent* mouseEvent)
658 {
659 CALL_DEBUG_ENTER;
660 CHKPR(mouseEvent, RET_ERR);
661 return mouseEvent->displayY;
662 }
663
OH_Input_SetMouseEventButton(struct Input_MouseEvent * mouseEvent,int32_t button)664 void OH_Input_SetMouseEventButton(struct Input_MouseEvent* mouseEvent, int32_t button)
665 {
666 CALL_DEBUG_ENTER;
667 CHKPV(mouseEvent);
668 mouseEvent->button = button;
669 }
670
OH_Input_GetMouseEventButton(const struct Input_MouseEvent * mouseEvent)671 int32_t OH_Input_GetMouseEventButton(const struct Input_MouseEvent* mouseEvent)
672 {
673 CALL_DEBUG_ENTER;
674 CHKPR(mouseEvent, RET_ERR);
675 return mouseEvent->button;
676 }
677
OH_Input_SetMouseEventAxisType(struct Input_MouseEvent * mouseEvent,int32_t axisType)678 void OH_Input_SetMouseEventAxisType(struct Input_MouseEvent* mouseEvent, int32_t axisType)
679 {
680 CALL_DEBUG_ENTER;
681 CHKPV(mouseEvent);
682 mouseEvent->axisType = axisType;
683 }
684
OH_Input_GetMouseEventAxisType(const struct Input_MouseEvent * mouseEvent)685 int32_t OH_Input_GetMouseEventAxisType(const struct Input_MouseEvent* mouseEvent)
686 {
687 CALL_DEBUG_ENTER;
688 CHKPR(mouseEvent, RET_ERR);
689 return mouseEvent->axisType;
690 }
691
OH_Input_SetMouseEventAxisValue(struct Input_MouseEvent * mouseEvent,float axisValue)692 void OH_Input_SetMouseEventAxisValue(struct Input_MouseEvent* mouseEvent, float axisValue)
693 {
694 CALL_DEBUG_ENTER;
695 CHKPV(mouseEvent);
696 mouseEvent->axisValue = axisValue;
697 }
698
OH_Input_GetMouseEventAxisValue(const struct Input_MouseEvent * mouseEvent)699 float OH_Input_GetMouseEventAxisValue(const struct Input_MouseEvent* mouseEvent)
700 {
701 CALL_DEBUG_ENTER;
702 CHKPR(mouseEvent, RET_ERR);
703 return mouseEvent->axisValue;
704 }
705
OH_Input_SetMouseEventActionTime(struct Input_MouseEvent * mouseEvent,int64_t actionTime)706 void OH_Input_SetMouseEventActionTime(struct Input_MouseEvent* mouseEvent, int64_t actionTime)
707 {
708 CALL_DEBUG_ENTER;
709 CHKPV(mouseEvent);
710 mouseEvent->actionTime = actionTime;
711 }
712
OH_Input_GetMouseEventActionTime(const struct Input_MouseEvent * mouseEvent)713 int64_t OH_Input_GetMouseEventActionTime(const struct Input_MouseEvent* mouseEvent)
714 {
715 CALL_DEBUG_ENTER;
716 CHKPR(mouseEvent, RET_ERR);
717 return mouseEvent->actionTime;
718 }
719
OH_Input_SetMouseEventWindowId(struct Input_MouseEvent * mouseEvent,int32_t windowId)720 void OH_Input_SetMouseEventWindowId(struct Input_MouseEvent* mouseEvent, int32_t windowId)
721 {
722 CALL_DEBUG_ENTER;
723 CHKPV(mouseEvent);
724 mouseEvent->windowId = windowId;
725 }
726
OH_Input_GetMouseEventWindowId(const struct Input_MouseEvent * mouseEvent)727 int32_t OH_Input_GetMouseEventWindowId(const struct Input_MouseEvent* mouseEvent)
728 {
729 CALL_DEBUG_ENTER;
730 CHKPR(mouseEvent, RET_ERR);
731 return mouseEvent->windowId;
732 }
733
OH_Input_SetMouseEventDisplayId(struct Input_MouseEvent * mouseEvent,int32_t displayId)734 void OH_Input_SetMouseEventDisplayId(struct Input_MouseEvent* mouseEvent, int32_t displayId)
735 {
736 CALL_DEBUG_ENTER;
737 CHKPV(mouseEvent);
738 mouseEvent->displayId = displayId;
739 }
740
OH_Input_GetMouseEventDisplayId(const struct Input_MouseEvent * mouseEvent)741 int32_t OH_Input_GetMouseEventDisplayId(const struct Input_MouseEvent* mouseEvent)
742 {
743 CALL_DEBUG_ENTER;
744 CHKPR(mouseEvent, RET_ERR);
745 return mouseEvent->displayId;
746 }
747
OH_Input_SetMouseEventGlobalX(struct Input_MouseEvent * mouseEvent,int32_t globalX)748 void OH_Input_SetMouseEventGlobalX(struct Input_MouseEvent* mouseEvent, int32_t globalX)
749 {
750 CALL_DEBUG_ENTER;
751 CHKPV(mouseEvent);
752 mouseEvent->globalX = globalX;
753 }
754
OH_Input_GetMouseEventGlobalX(const struct Input_MouseEvent * mouseEvent)755 int32_t OH_Input_GetMouseEventGlobalX(const struct Input_MouseEvent* mouseEvent)
756 {
757 CALL_DEBUG_ENTER;
758 CHKPR(mouseEvent, INT32_MAX);
759 return mouseEvent->globalX;
760 }
761
OH_Input_SetMouseEventGlobalY(struct Input_MouseEvent * mouseEvent,int32_t globalY)762 void OH_Input_SetMouseEventGlobalY(struct Input_MouseEvent* mouseEvent, int32_t globalY)
763 {
764 CALL_DEBUG_ENTER;
765 CHKPV(mouseEvent);
766 mouseEvent->globalY = globalY;
767 }
768
OH_Input_GetMouseEventGlobalY(const struct Input_MouseEvent * mouseEvent)769 int32_t OH_Input_GetMouseEventGlobalY(const struct Input_MouseEvent* mouseEvent)
770 {
771 CALL_DEBUG_ENTER;
772 CHKPR(mouseEvent, INT32_MAX);
773 return mouseEvent->globalY;
774 }
775
HandleTouchActionDown(OHOS::MMI::PointerEvent::PointerItem & item,int64_t time)776 static void HandleTouchActionDown(OHOS::MMI::PointerEvent::PointerItem &item, int64_t time)
777 {
778 auto pointIds = g_touchEvent->GetPointerIds();
779 if (pointIds.empty()) {
780 g_touchEvent->SetActionStartTime(time);
781 g_touchEvent->SetTargetDisplayId(0);
782 }
783 g_touchEvent->SetActionTime(time);
784 g_touchEvent->SetPointerAction(OHOS::MMI::PointerEvent::POINTER_ACTION_DOWN);
785 item.SetDownTime(time);
786 item.SetPressed(true);
787 }
788
HandleTouchAction(const struct Input_TouchEvent * touchEvent,OHOS::MMI::PointerEvent::PointerItem & item)789 static int32_t HandleTouchAction(const struct Input_TouchEvent* touchEvent, OHOS::MMI::PointerEvent::PointerItem &item)
790 {
791 CALL_DEBUG_ENTER;
792 int64_t time = touchEvent->actionTime;
793 if (time < 0) {
794 time = OHOS::MMI::GetSysClockTime();
795 }
796 switch (touchEvent->action) {
797 case TOUCH_ACTION_CANCEL:{
798 g_touchEvent->SetActionTime(time);
799 g_touchEvent->SetPointerAction(OHOS::MMI::PointerEvent::POINTER_ACTION_CANCEL);
800 if (!(g_touchEvent->GetPointerItem(touchEvent->id, item))) {
801 MMI_HILOGE("Get pointer parameter failed");
802 return INPUT_PARAMETER_ERROR;
803 }
804 item.SetPressed(false);
805 break;
806 }
807 case TOUCH_ACTION_DOWN: {
808 HandleTouchActionDown(item, time);
809 break;
810 }
811 case TOUCH_ACTION_MOVE: {
812 g_touchEvent->SetActionTime(time);
813 g_touchEvent->SetPointerAction(OHOS::MMI::PointerEvent::POINTER_ACTION_MOVE);
814 if (!(g_touchEvent->GetPointerItem(touchEvent->id, item))) {
815 MMI_HILOGE("Get pointer parameter failed");
816 return INPUT_PARAMETER_ERROR;
817 }
818 break;
819 }
820 case TOUCH_ACTION_UP: {
821 g_touchEvent->SetActionTime(time);
822 g_touchEvent->SetPointerAction(OHOS::MMI::PointerEvent::POINTER_ACTION_UP);
823 if (!(g_touchEvent->GetPointerItem(touchEvent->id, item))) {
824 MMI_HILOGE("Get pointer parameter failed");
825 return INPUT_PARAMETER_ERROR;
826 }
827 item.SetPressed(false);
828 break;
829 }
830 default: {
831 MMI_HILOGE("action:%{public}d is invalid", touchEvent->action);
832 return INPUT_PARAMETER_ERROR;
833 }
834 }
835 return INPUT_SUCCESS;
836 }
837
HandleTouchProperty(const struct Input_TouchEvent * touchEvent,OHOS::MMI::PointerEvent::PointerItem & item,int32_t useCoordinate)838 static int32_t HandleTouchProperty(const struct Input_TouchEvent* touchEvent,
839 OHOS::MMI::PointerEvent::PointerItem &item, int32_t useCoordinate)
840 {
841 CALL_DEBUG_ENTER;
842 int32_t id = touchEvent->id;
843 int32_t screenX = touchEvent->displayX;
844 int32_t screenY = touchEvent->displayY;
845 if (useCoordinate == PointerEvent::DISPLAY_COORDINATE && (screenX < 0 || screenY < 0)) {
846 MMI_HILOGE("touch parameter is less 0, can not process");
847 return INPUT_PARAMETER_ERROR;
848 }
849 item.SetDisplayX(screenX);
850 item.SetDisplayY(screenY);
851 item.SetDisplayXPos(screenX);
852 item.SetDisplayYPos(screenY);
853 int32_t globalX = touchEvent->globalX;
854 int32_t globalY = touchEvent->globalY;
855 if (globalX != INT32_MAX && globalY != INT32_MAX) {
856 item.SetGlobalX(globalX);
857 item.SetGlobalY(globalY);
858 } else {
859 item.SetGlobalX(DBL_MAX);
860 item.SetGlobalY(DBL_MAX);
861 }
862 item.SetPointerId(id);
863 g_touchEvent->SetPointerId(id);
864 g_touchEvent->SetSourceType(OHOS::MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
865 if (touchEvent->action == TOUCH_ACTION_DOWN) {
866 g_touchEvent->AddPointerItem(item);
867 } else if ((touchEvent->action == TOUCH_ACTION_MOVE) || (touchEvent->action == TOUCH_ACTION_UP)) {
868 g_touchEvent->UpdatePointerItem(id, item);
869 }
870 return INPUT_SUCCESS;
871 }
872
OH_Input_InjectTouchEvent(const struct Input_TouchEvent * touchEvent)873 int32_t OH_Input_InjectTouchEvent(const struct Input_TouchEvent* touchEvent)
874 {
875 MMI_HILOGI("Input_TouchEvent injectTouchEvent");
876 CHKPR(touchEvent, INPUT_PARAMETER_ERROR);
877 CHKPR(g_touchEvent, INPUT_PARAMETER_ERROR);
878 g_touchEvent->ClearFlag();
879 OHOS::MMI::PointerEvent::PointerItem item;
880 int32_t result = HandleTouchAction(touchEvent, item);
881 if (result != 0) {
882 return INPUT_PARAMETER_ERROR;
883 }
884 result = HandleTouchProperty(touchEvent, item, PointerEvent::DISPLAY_COORDINATE);
885 if (result != 0) {
886 return INPUT_PARAMETER_ERROR;
887 }
888 g_touchEvent->AddFlag(OHOS::MMI::InputEvent::EVENT_FLAG_SIMULATE);
889 OHOS::Singleton<OHOS::MMI::InputManagerImpl>::GetInstance().SimulateInputEvent(g_touchEvent, true,
890 PointerEvent::DISPLAY_COORDINATE);
891 if (touchEvent->action == TOUCH_ACTION_UP) {
892 g_touchEvent->RemovePointerItem(g_touchEvent->GetPointerId());
893 MMI_HILOGD("This touch event is up remove this finger");
894 if (g_touchEvent->GetPointerIds().empty()) {
895 MMI_HILOGD("This touch event is final finger up remove this finger");
896 g_touchEvent->Reset();
897 }
898 }
899 return INPUT_SUCCESS;
900 }
901
OH_Input_InjectTouchEventGlobal(const struct Input_TouchEvent * touchEvent)902 int32_t OH_Input_InjectTouchEventGlobal(const struct Input_TouchEvent* touchEvent)
903 {
904 MMI_HILOGD("injectTouchEvent global");
905 CHKPR(touchEvent, INPUT_PARAMETER_ERROR);
906 CHKPR(g_touchEvent, INPUT_PARAMETER_ERROR);
907 g_touchEvent->ClearFlag();
908 OHOS::MMI::PointerEvent::PointerItem item;
909 int32_t result = HandleTouchAction(touchEvent, item);
910 if (result != 0) {
911 return INPUT_PARAMETER_ERROR;
912 }
913 result = HandleTouchProperty(touchEvent, item, PointerEvent::GLOBAL_COORDINATE);
914 if (result != 0) {
915 return INPUT_PARAMETER_ERROR;
916 }
917 if (!item.IsValidGlobalXY()) {
918 return INPUT_PARAMETER_ERROR;
919 }
920 g_touchEvent->AddFlag(OHOS::MMI::InputEvent::EVENT_FLAG_SIMULATE);
921 result = OHOS::Singleton<OHOS::MMI::InputManagerImpl>::GetInstance().SimulateInputEvent(g_touchEvent, true,
922 PointerEvent::GLOBAL_COORDINATE);
923 if (touchEvent->action == TOUCH_ACTION_UP) {
924 g_touchEvent->RemovePointerItem(g_touchEvent->GetPointerId());
925 MMI_HILOGD("This touch event is up remove this finger");
926 if (g_touchEvent->GetPointerIds().empty()) {
927 MMI_HILOGD("This touch event is final finger up remove this finger");
928 g_touchEvent->Reset();
929 }
930 }
931 if ((result == INPUT_PERMISSION_DENIED) || (result == INPUT_OCCUPIED_BY_OTHER)) {
932 MMI_HILOGE("Permission denied or occupied by other");
933 return INPUT_PERMISSION_DENIED;
934 }
935 return INPUT_SUCCESS;
936 }
937
OH_Input_CreateTouchEvent()938 struct Input_TouchEvent* OH_Input_CreateTouchEvent()
939 {
940 CALL_DEBUG_ENTER;
941 Input_TouchEvent* touchEvent = new (std::nothrow) Input_TouchEvent();
942 CHKPL(touchEvent);
943 return touchEvent;
944 }
945
OH_Input_DestroyTouchEvent(struct Input_TouchEvent ** touchEvent)946 void OH_Input_DestroyTouchEvent(struct Input_TouchEvent** touchEvent)
947 {
948 CALL_DEBUG_ENTER;
949 CHKPV(touchEvent);
950 CHKPV(*touchEvent);
951 delete *touchEvent;
952 *touchEvent = nullptr;
953 }
954
OH_Input_SetTouchEventAction(struct Input_TouchEvent * touchEvent,int32_t action)955 void OH_Input_SetTouchEventAction(struct Input_TouchEvent* touchEvent, int32_t action)
956 {
957 CALL_DEBUG_ENTER;
958 CHKPV(touchEvent);
959 touchEvent->action = action;
960 }
961
OH_Input_GetTouchEventAction(const struct Input_TouchEvent * touchEvent)962 int32_t OH_Input_GetTouchEventAction(const struct Input_TouchEvent* touchEvent)
963 {
964 CALL_DEBUG_ENTER;
965 CHKPR(touchEvent, RET_ERR);
966 return touchEvent->action;
967 }
968
OH_Input_SetTouchEventFingerId(struct Input_TouchEvent * touchEvent,int32_t id)969 void OH_Input_SetTouchEventFingerId(struct Input_TouchEvent* touchEvent, int32_t id)
970 {
971 CALL_DEBUG_ENTER;
972 CHKPV(touchEvent);
973 touchEvent->id = id;
974 }
975
OH_Input_GetTouchEventFingerId(const struct Input_TouchEvent * touchEvent)976 int32_t OH_Input_GetTouchEventFingerId(const struct Input_TouchEvent* touchEvent)
977 {
978 CALL_DEBUG_ENTER;
979 CHKPR(touchEvent, RET_ERR);
980 return touchEvent->id;
981 }
982
OH_Input_SetTouchEventDisplayX(struct Input_TouchEvent * touchEvent,int32_t displayX)983 void OH_Input_SetTouchEventDisplayX(struct Input_TouchEvent* touchEvent, int32_t displayX)
984 {
985 CALL_DEBUG_ENTER;
986 CHKPV(touchEvent);
987 touchEvent->displayX = displayX;
988 }
989
OH_Input_GetTouchEventDisplayX(const struct Input_TouchEvent * touchEvent)990 int32_t OH_Input_GetTouchEventDisplayX(const struct Input_TouchEvent* touchEvent)
991 {
992 CALL_DEBUG_ENTER;
993 CHKPR(touchEvent, RET_ERR);
994 return touchEvent->displayX;
995 }
996
OH_Input_SetTouchEventDisplayY(struct Input_TouchEvent * touchEvent,int32_t displayY)997 void OH_Input_SetTouchEventDisplayY(struct Input_TouchEvent* touchEvent, int32_t displayY)
998 {
999 CALL_DEBUG_ENTER;
1000 CHKPV(touchEvent);
1001 touchEvent->displayY = displayY;
1002 }
1003
OH_Input_GetTouchEventDisplayY(const struct Input_TouchEvent * touchEvent)1004 int32_t OH_Input_GetTouchEventDisplayY(const struct Input_TouchEvent* touchEvent)
1005 {
1006 CALL_DEBUG_ENTER;
1007 CHKPR(touchEvent, RET_ERR);
1008 return touchEvent->displayY;
1009 }
1010
OH_Input_SetTouchEventActionTime(struct Input_TouchEvent * touchEvent,int64_t actionTime)1011 void OH_Input_SetTouchEventActionTime(struct Input_TouchEvent* touchEvent, int64_t actionTime)
1012 {
1013 CALL_DEBUG_ENTER;
1014 CHKPV(touchEvent);
1015 touchEvent->actionTime = actionTime;
1016 }
1017
OH_Input_GetTouchEventActionTime(const struct Input_TouchEvent * touchEvent)1018 int64_t OH_Input_GetTouchEventActionTime(const struct Input_TouchEvent* touchEvent)
1019 {
1020 CALL_DEBUG_ENTER;
1021 CHKPR(touchEvent, RET_ERR);
1022 return touchEvent->actionTime;
1023 }
1024
OH_Input_SetTouchEventWindowId(struct Input_TouchEvent * touchEvent,int32_t windowId)1025 void OH_Input_SetTouchEventWindowId(struct Input_TouchEvent* touchEvent, int32_t windowId)
1026 {
1027 CALL_DEBUG_ENTER;
1028 CHKPV(touchEvent);
1029 touchEvent->windowId = windowId;
1030 }
1031
OH_Input_GetTouchEventWindowId(const struct Input_TouchEvent * touchEvent)1032 int32_t OH_Input_GetTouchEventWindowId(const struct Input_TouchEvent* touchEvent)
1033 {
1034 CALL_DEBUG_ENTER;
1035 CHKPR(touchEvent, RET_ERR);
1036 return touchEvent->windowId;
1037 }
1038
OH_Input_SetTouchEventDisplayId(struct Input_TouchEvent * touchEvent,int32_t displayId)1039 void OH_Input_SetTouchEventDisplayId(struct Input_TouchEvent* touchEvent, int32_t displayId)
1040 {
1041 CALL_DEBUG_ENTER;
1042 CHKPV(touchEvent);
1043 touchEvent->displayId = displayId;
1044 }
1045
OH_Input_GetTouchEventDisplayId(const struct Input_TouchEvent * touchEvent)1046 int32_t OH_Input_GetTouchEventDisplayId(const struct Input_TouchEvent* touchEvent)
1047 {
1048 CALL_DEBUG_ENTER;
1049 CHKPR(touchEvent, RET_ERR);
1050 return touchEvent->displayId;
1051 }
1052
OH_Input_SetTouchEventGlobalX(struct Input_TouchEvent * touchEvent,int32_t globalX)1053 void OH_Input_SetTouchEventGlobalX(struct Input_TouchEvent* touchEvent, int32_t globalX)
1054 {
1055 CALL_DEBUG_ENTER;
1056 CHKPV(touchEvent);
1057 touchEvent->globalX = globalX;
1058 }
1059
OH_Input_GetTouchEventGlobalX(const struct Input_TouchEvent * touchEvent)1060 int32_t OH_Input_GetTouchEventGlobalX(const struct Input_TouchEvent* touchEvent)
1061 {
1062 CALL_DEBUG_ENTER;
1063 CHKPR(touchEvent, INT32_MAX);
1064 return touchEvent->globalX;
1065 }
1066
OH_Input_SetTouchEventGlobalY(struct Input_TouchEvent * touchEvent,int32_t globalY)1067 void OH_Input_SetTouchEventGlobalY(struct Input_TouchEvent* touchEvent, int32_t globalY)
1068 {
1069 CALL_DEBUG_ENTER;
1070 CHKPV(touchEvent);
1071 touchEvent->globalY = globalY;
1072 }
1073
OH_Input_GetTouchEventGlobalY(const struct Input_TouchEvent * touchEvent)1074 int32_t OH_Input_GetTouchEventGlobalY(const struct Input_TouchEvent* touchEvent)
1075 {
1076 CALL_DEBUG_ENTER;
1077 CHKPR(touchEvent, INT32_MAX);
1078 return touchEvent->globalY;
1079 }
1080
OH_Input_CancelInjection()1081 void OH_Input_CancelInjection()
1082 {
1083 CALL_DEBUG_ENTER;
1084 OHOS::Singleton<OHOS::MMI::InputManagerImpl>::GetInstance().CancelInjection();
1085 }
1086
SetAxisValueByAxisEventType(std::shared_ptr<OHOS::MMI::PointerEvent> event,struct Input_AxisEvent * axisEvent,int32_t axisEventType)1087 static bool SetAxisValueByAxisEventType(std::shared_ptr<OHOS::MMI::PointerEvent> event,
1088 struct Input_AxisEvent *axisEvent, int32_t axisEventType)
1089 {
1090 CHKPF(event);
1091 CHKPF(axisEvent);
1092 if (axisEventType == OHOS::MMI::PointerEvent::AXIS_EVENT_TYPE_PINCH) {
1093 double value = event->GetAxisValue(OHOS::MMI::PointerEvent::AXIS_TYPE_PINCH);
1094 axisEvent->axisValues.insert(std::make_pair(AXIS_TYPE_PINCH, value));
1095 value = event->GetAxisValue(OHOS::MMI::PointerEvent::AXIS_TYPE_ROTATE);
1096 axisEvent->axisValues.insert(std::make_pair(AXIS_TYPE_ROTATE, value));
1097 } else if (axisEventType == OHOS::MMI::PointerEvent::AXIS_EVENT_TYPE_SCROLL) {
1098 double value = event->GetAxisValue(OHOS::MMI::PointerEvent::AXIS_TYPE_SCROLL_VERTICAL);
1099 axisEvent->axisValues.insert(std::make_pair(AXIS_TYPE_SCROLL_VERTICAL, value));
1100 value = event->GetAxisValue(OHOS::MMI::PointerEvent::AXIS_TYPE_SCROLL_HORIZONTAL);
1101 axisEvent->axisValues.insert(std::make_pair(AXIS_TYPE_SCROLL_HORIZONTAL, value));
1102 } else {
1103 MMI_HILOGE("Undefined axisEventType:%{public}d", axisEventType);
1104 return false;
1105 }
1106 axisEvent->axisEventType = axisEventType;
1107 return true;
1108 }
1109
IsAxisEvent(int32_t action)1110 static bool IsAxisEvent(int32_t action)
1111 {
1112 if (action != OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_BEGIN &&
1113 action != OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_UPDATE &&
1114 action != OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_END) {
1115 return false;
1116 }
1117 return true;
1118 }
1119
OH_Input_CreateAxisEvent(void)1120 Input_AxisEvent* OH_Input_CreateAxisEvent(void)
1121 {
1122 Input_AxisEvent* axisEvent = new (std::nothrow) Input_AxisEvent();
1123 CHKPP(axisEvent);
1124 return axisEvent;
1125 }
1126
OH_Input_DestroyAxisEvent(Input_AxisEvent ** axisEvent)1127 Input_Result OH_Input_DestroyAxisEvent(Input_AxisEvent** axisEvent)
1128 {
1129 CALL_DEBUG_ENTER;
1130 if (axisEvent == nullptr || *axisEvent == nullptr) {
1131 return INPUT_PARAMETER_ERROR;
1132 }
1133 delete *axisEvent;
1134 *axisEvent = nullptr;
1135 return INPUT_SUCCESS;
1136 }
1137
OH_Input_SetAxisEventAction(Input_AxisEvent * axisEvent,InputEvent_AxisAction action)1138 Input_Result OH_Input_SetAxisEventAction(Input_AxisEvent* axisEvent, InputEvent_AxisAction action)
1139 {
1140 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
1141 axisEvent->axisAction = action;
1142 return INPUT_SUCCESS;
1143 }
1144
OH_Input_GetAxisEventAction(const Input_AxisEvent * axisEvent,InputEvent_AxisAction * action)1145 Input_Result OH_Input_GetAxisEventAction(const Input_AxisEvent* axisEvent, InputEvent_AxisAction *action)
1146 {
1147 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
1148 CHKPR(action, INPUT_PARAMETER_ERROR);
1149 *action = InputEvent_AxisAction(axisEvent->axisAction);
1150 return INPUT_SUCCESS;
1151 }
1152
OH_Input_SetAxisEventDisplayX(Input_AxisEvent * axisEvent,float displayX)1153 Input_Result OH_Input_SetAxisEventDisplayX(Input_AxisEvent* axisEvent, float displayX)
1154 {
1155 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
1156 axisEvent->displayX = displayX;
1157 return INPUT_SUCCESS;
1158 }
1159
OH_Input_GetAxisEventDisplayX(const Input_AxisEvent * axisEvent,float * displayX)1160 Input_Result OH_Input_GetAxisEventDisplayX(const Input_AxisEvent* axisEvent, float* displayX)
1161 {
1162 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
1163 CHKPR(displayX, INPUT_PARAMETER_ERROR);
1164 *displayX = axisEvent->displayX;
1165 return INPUT_SUCCESS;
1166 }
1167
OH_Input_SetAxisEventDisplayY(Input_AxisEvent * axisEvent,float displayY)1168 Input_Result OH_Input_SetAxisEventDisplayY(Input_AxisEvent* axisEvent, float displayY)
1169 {
1170 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
1171 axisEvent->displayY = displayY;
1172 return INPUT_SUCCESS;
1173 }
1174
OH_Input_GetAxisEventDisplayY(const Input_AxisEvent * axisEvent,float * displayY)1175 Input_Result OH_Input_GetAxisEventDisplayY(const Input_AxisEvent* axisEvent, float* displayY)
1176 {
1177 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
1178 CHKPR(displayY, INPUT_PARAMETER_ERROR);
1179 *displayY = axisEvent->displayY;
1180 return INPUT_SUCCESS;
1181 }
1182
OH_Input_SetAxisEventAxisValue(Input_AxisEvent * axisEvent,InputEvent_AxisType axisType,double axisValue)1183 Input_Result OH_Input_SetAxisEventAxisValue(Input_AxisEvent* axisEvent,
1184 InputEvent_AxisType axisType, double axisValue)
1185 {
1186 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
1187 axisEvent->axisValues.emplace(axisType, axisValue);
1188 return INPUT_SUCCESS;
1189 }
1190
OH_Input_GetAxisEventAxisValue(const Input_AxisEvent * axisEvent,InputEvent_AxisType axisType,double * axisValue)1191 Input_Result OH_Input_GetAxisEventAxisValue(const Input_AxisEvent* axisEvent,
1192 InputEvent_AxisType axisType, double* axisValue)
1193 {
1194 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
1195 CHKPR(axisValue, INPUT_PARAMETER_ERROR);
1196 auto it = axisEvent->axisValues.find(axisType);
1197 if (it == axisEvent->axisValues.end()) {
1198 MMI_HILOGE("There is no axis value of axisType:%{public}d in the axisEvent", axisType);
1199 return INPUT_PARAMETER_ERROR;
1200 }
1201 *axisValue = it->second;
1202 return INPUT_SUCCESS;
1203 }
1204
OH_Input_SetAxisEventActionTime(Input_AxisEvent * axisEvent,int64_t actionTime)1205 Input_Result OH_Input_SetAxisEventActionTime(Input_AxisEvent* axisEvent, int64_t actionTime)
1206 {
1207 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
1208 axisEvent->actionTime = actionTime;
1209 return INPUT_SUCCESS;
1210 }
1211
OH_Input_GetAxisEventActionTime(const Input_AxisEvent * axisEvent,int64_t * actionTime)1212 Input_Result OH_Input_GetAxisEventActionTime(const Input_AxisEvent* axisEvent, int64_t* actionTime)
1213 {
1214 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
1215 CHKPR(actionTime, INPUT_PARAMETER_ERROR);
1216 *actionTime = axisEvent->actionTime;
1217 return INPUT_SUCCESS;
1218 }
1219
OH_Input_SetAxisEventType(Input_AxisEvent * axisEvent,InputEvent_AxisEventType axisEventType)1220 Input_Result OH_Input_SetAxisEventType(Input_AxisEvent* axisEvent, InputEvent_AxisEventType axisEventType)
1221 {
1222 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
1223 axisEvent->axisEventType = axisEventType;
1224 return INPUT_SUCCESS;
1225 }
1226
OH_Input_GetAxisEventType(const Input_AxisEvent * axisEvent,InputEvent_AxisEventType * axisEventType)1227 Input_Result OH_Input_GetAxisEventType(const Input_AxisEvent* axisEvent, InputEvent_AxisEventType* axisEventType)
1228 {
1229 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
1230 CHKPR(axisEventType, INPUT_PARAMETER_ERROR);
1231 *axisEventType = InputEvent_AxisEventType(axisEvent->axisEventType);
1232 return INPUT_SUCCESS;
1233 }
1234
OH_Input_SetAxisEventSourceType(Input_AxisEvent * axisEvent,InputEvent_SourceType sourceType)1235 Input_Result OH_Input_SetAxisEventSourceType(Input_AxisEvent* axisEvent, InputEvent_SourceType sourceType)
1236 {
1237 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
1238 axisEvent->sourceType = sourceType;
1239 return INPUT_SUCCESS;
1240 }
1241
OH_Input_GetAxisEventSourceType(const Input_AxisEvent * axisEvent,InputEvent_SourceType * sourceType)1242 Input_Result OH_Input_GetAxisEventSourceType(const Input_AxisEvent* axisEvent, InputEvent_SourceType* sourceType)
1243 {
1244 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
1245 CHKPR(sourceType, INPUT_PARAMETER_ERROR);
1246 *sourceType = InputEvent_SourceType(axisEvent->sourceType);
1247 return INPUT_SUCCESS;
1248 }
1249
OH_Input_SetAxisEventWindowId(Input_AxisEvent * axisEvent,int32_t windowId)1250 Input_Result OH_Input_SetAxisEventWindowId(Input_AxisEvent* axisEvent, int32_t windowId)
1251 {
1252 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
1253 axisEvent->windowId = windowId;
1254 return INPUT_SUCCESS;
1255 }
1256
OH_Input_GetAxisEventWindowId(const Input_AxisEvent * axisEvent,int32_t * windowId)1257 Input_Result OH_Input_GetAxisEventWindowId(const Input_AxisEvent* axisEvent, int32_t* windowId)
1258 {
1259 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
1260 CHKPR(windowId, INPUT_PARAMETER_ERROR);
1261 *windowId = axisEvent->windowId;
1262 return INPUT_SUCCESS;
1263 }
1264
OH_Input_SetAxisEventDisplayId(Input_AxisEvent * axisEvent,int32_t displayId)1265 Input_Result OH_Input_SetAxisEventDisplayId(Input_AxisEvent* axisEvent, int32_t displayId)
1266 {
1267 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
1268 axisEvent->displayId = displayId;
1269 return INPUT_SUCCESS;
1270 }
1271
OH_Input_GetAxisEventDisplayId(const Input_AxisEvent * axisEvent,int32_t * displayId)1272 Input_Result OH_Input_GetAxisEventDisplayId(const Input_AxisEvent* axisEvent, int32_t* displayId)
1273 {
1274 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
1275 CHKPR(displayId, INPUT_PARAMETER_ERROR);
1276 *displayId = axisEvent->displayId;
1277 return INPUT_SUCCESS;
1278 }
1279
OH_Input_SetAxisEventGlobalX(struct Input_AxisEvent * axisEvent,int32_t globalX)1280 Input_Result OH_Input_SetAxisEventGlobalX(struct Input_AxisEvent* axisEvent, int32_t globalX)
1281 {
1282 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
1283 axisEvent->globalX = globalX;
1284 return INPUT_SUCCESS;
1285 }
1286
OH_Input_GetAxisEventGlobalX(const Input_AxisEvent * axisEvent,int32_t * globalX)1287 Input_Result OH_Input_GetAxisEventGlobalX(const Input_AxisEvent* axisEvent, int32_t* globalX)
1288 {
1289 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
1290 CHKPR(globalX, INPUT_PARAMETER_ERROR);
1291 *globalX = axisEvent->globalX;
1292 return INPUT_SUCCESS;
1293 }
1294
OH_Input_SetAxisEventGlobalY(struct Input_AxisEvent * axisEvent,int32_t globalY)1295 Input_Result OH_Input_SetAxisEventGlobalY(struct Input_AxisEvent* axisEvent, int32_t globalY)
1296 {
1297 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
1298 axisEvent->globalY = globalY;
1299 return INPUT_SUCCESS;
1300 }
1301
OH_Input_GetAxisEventGlobalY(const Input_AxisEvent * axisEvent,int32_t * globalY)1302 Input_Result OH_Input_GetAxisEventGlobalY(const Input_AxisEvent* axisEvent, int32_t* globalY)
1303 {
1304 CHKPR(axisEvent, INPUT_PARAMETER_ERROR);
1305 CHKPR(globalY, INPUT_PARAMETER_ERROR);
1306 *globalY = axisEvent->globalY;
1307 return INPUT_SUCCESS;
1308 }
1309
NormalizeResult(int32_t result)1310 static Input_Result NormalizeResult(int32_t result)
1311 {
1312 if (result < RET_OK) {
1313 if (result == OHOS::MMI::ERROR_NO_PERMISSION) {
1314 MMI_HILOGE("Permisson denied");
1315 return INPUT_PERMISSION_DENIED;
1316 }
1317 return INPUT_SERVICE_EXCEPTION;
1318 }
1319 return INPUT_SUCCESS;
1320 }
1321
SetKeyEventAction(Input_KeyEvent * keyEvent,int32_t action)1322 static bool SetKeyEventAction(Input_KeyEvent* keyEvent, int32_t action)
1323 {
1324 CHKPF(keyEvent);
1325 if (action == OHOS::MMI::KeyEvent::KEY_ACTION_CANCEL) {
1326 keyEvent->action = KEY_ACTION_CANCEL;
1327 } else if (action == OHOS::MMI::KeyEvent::KEY_ACTION_DOWN) {
1328 keyEvent->action = KEY_ACTION_DOWN;
1329 } else if (action == OHOS::MMI::KeyEvent::KEY_ACTION_UP) {
1330 keyEvent->action = KEY_ACTION_UP;
1331 } else {
1332 MMI_HILOGE("Invalid key event action");
1333 return false;
1334 }
1335 return true;
1336 }
1337
KeyEventMonitorCallback(std::shared_ptr<OHOS::MMI::KeyEvent> event)1338 static void KeyEventMonitorCallback(std::shared_ptr<OHOS::MMI::KeyEvent> event)
1339 {
1340 CHKPV(event);
1341 Input_KeyEvent* keyEvent = OH_Input_CreateKeyEvent();
1342 CHKPV(keyEvent);
1343 if (!SetKeyEventAction(keyEvent, event->GetKeyAction())) {
1344 OH_Input_DestroyKeyEvent(&keyEvent);
1345 return;
1346 }
1347 keyEvent->keyCode = event->GetKeyCode();
1348 keyEvent->actionTime = event->GetActionTime();
1349 keyEvent->windowId = event->GetTargetWindowId();
1350 keyEvent->displayId = event->GetTargetDisplayId();
1351 std::lock_guard guard(g_mutex);
1352 for (auto &callback : g_keyMonitorCallbacks) {
1353 callback(keyEvent);
1354 }
1355 OH_Input_DestroyKeyEvent(&keyEvent);
1356 }
1357
IsScreenCaptureWorking()1358 static bool IsScreenCaptureWorking()
1359 {
1360 CALL_DEBUG_ENTER;
1361 #ifdef PLAYER_FRAMEWORK_EXISTS
1362 int32_t pid = OHOS::IPCSkeleton::GetCallingPid();
1363 std::list<int32_t> pidList = OHOS::Media::ScreenCaptureMonitor::GetInstance()->IsScreenCaptureWorking();
1364 for (const auto &capturePid : pidList) {
1365 MMI_HILOGI("Current screen capture work pid %{public}d ", capturePid);
1366 if (capturePid == pid) {
1367 return true;
1368 } else {
1369 MMI_HILOGE("Calling pid is:%{public}d, but screen capture pid is:%{public}d", pid, capturePid);
1370 }
1371 }
1372 return false;
1373 #else
1374 return false;
1375 #endif // PLAYER_FRAMEWORK_EXISTS
1376 }
1377
OH_Input_AddKeyEventMonitor(Input_KeyEventCallback callback)1378 Input_Result OH_Input_AddKeyEventMonitor(Input_KeyEventCallback callback)
1379 {
1380 CALL_DEBUG_ENTER;
1381 CHKPR(callback, INPUT_PARAMETER_ERROR);
1382 if (!OHOS::MMI::PermissionHelper::GetInstance()->VerifySystemApp()) {
1383 if (!IsScreenCaptureWorking()) {
1384 MMI_HILOGE("The screen capture is not working");
1385 return INPUT_PERMISSION_DENIED;
1386 }
1387 }
1388 Input_Result retCode = INPUT_SUCCESS;
1389 std::lock_guard guard(g_mutex);
1390 if (g_keyMonitorId == INVALID_MONITOR_ID) {
1391 int32_t ret = OHOS::Singleton<OHOS::MMI::InputManagerImpl>::GetInstance().AddMonitor(KeyEventMonitorCallback);
1392 retCode = NormalizeResult(ret);
1393 if (retCode != INPUT_SUCCESS) {
1394 return retCode;
1395 }
1396 g_keyMonitorId = ret;
1397 }
1398 g_keyMonitorCallbacks.insert(callback);
1399 return retCode;
1400 }
1401
SetTouchEventAction(Input_TouchEvent * touchEvent,int32_t action)1402 static bool SetTouchEventAction(Input_TouchEvent* touchEvent, int32_t action)
1403 {
1404 CHKPF(touchEvent);
1405 switch (action) {
1406 case OHOS::MMI::PointerEvent::POINTER_ACTION_CANCEL:
1407 touchEvent->action = TOUCH_ACTION_CANCEL;
1408 break;
1409 case OHOS::MMI::PointerEvent::POINTER_ACTION_DOWN:
1410 touchEvent->action = TOUCH_ACTION_DOWN;
1411 break;
1412 case OHOS::MMI::PointerEvent::POINTER_ACTION_MOVE:
1413 touchEvent->action = TOUCH_ACTION_MOVE;
1414 break;
1415 case OHOS::MMI::PointerEvent::POINTER_ACTION_UP:
1416 touchEvent->action = TOUCH_ACTION_UP;
1417 break;
1418 default:
1419 MMI_HILOGE("Invalid touch event action");
1420 return false;
1421 }
1422 return true;
1423 }
1424
TouchEventMonitorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)1425 static void TouchEventMonitorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)
1426 {
1427 CHKPV(event);
1428 Input_TouchEvent* touchEvent = OH_Input_CreateTouchEvent();
1429 CHKPV(touchEvent);
1430 OHOS::MMI::PointerEvent::PointerItem item;
1431 if (!(event->GetPointerItem(event->GetPointerId(), item))) {
1432 MMI_HILOGE("Can not get pointerItem for the pointer event");
1433 OH_Input_DestroyTouchEvent(&touchEvent);
1434 return;
1435 }
1436 if (!SetTouchEventAction(touchEvent, event->GetPointerAction())) {
1437 OH_Input_DestroyTouchEvent(&touchEvent);
1438 return;
1439 }
1440 touchEvent->id = event->GetPointerId();
1441 touchEvent->displayX = item.GetDisplayX();
1442 touchEvent->displayY = item.GetDisplayY();
1443 touchEvent->globalX = item.GetGlobalX();
1444 touchEvent->globalY = item.GetGlobalY();
1445 touchEvent->actionTime = event->GetActionTime();
1446 touchEvent->windowId = event->GetTargetWindowId();
1447 touchEvent->displayId = event->GetTargetDisplayId();
1448 std::lock_guard guard(g_mutex);
1449 for (auto &callback : g_touchMonitorCallbacks) {
1450 callback(touchEvent);
1451 }
1452 OH_Input_DestroyTouchEvent(&touchEvent);
1453 }
1454
SetMouseEventAction(Input_MouseEvent * mouseEvent,int32_t action)1455 static bool SetMouseEventAction(Input_MouseEvent* mouseEvent, int32_t action)
1456 {
1457 CHKPF(mouseEvent);
1458 switch (action) {
1459 case OHOS::MMI::PointerEvent::POINTER_ACTION_CANCEL:
1460 mouseEvent->action = MOUSE_ACTION_CANCEL;
1461 break;
1462 case OHOS::MMI::PointerEvent::POINTER_ACTION_MOVE:
1463 mouseEvent->action = MOUSE_ACTION_MOVE;
1464 break;
1465 case OHOS::MMI::PointerEvent::POINTER_ACTION_BUTTON_DOWN:
1466 mouseEvent->action = MOUSE_ACTION_BUTTON_DOWN;
1467 break;
1468 case OHOS::MMI::PointerEvent::POINTER_ACTION_BUTTON_UP:
1469 mouseEvent->action = MOUSE_ACTION_BUTTON_UP;
1470 break;
1471 default:
1472 MMI_HILOGE("Invalid mouse event action");
1473 return false;
1474 }
1475 return true;
1476 }
1477
SetMouseEventButton(Input_MouseEvent * mouseEvent,int32_t button)1478 static bool SetMouseEventButton(Input_MouseEvent* mouseEvent, int32_t button)
1479 {
1480 CHKPF(mouseEvent);
1481 switch (button) {
1482 case OHOS::MMI::PointerEvent::BUTTON_NONE:
1483 mouseEvent->button = MOUSE_BUTTON_NONE;
1484 break;
1485 case OHOS::MMI::PointerEvent::MOUSE_BUTTON_LEFT:
1486 mouseEvent->button = MOUSE_BUTTON_LEFT;
1487 break;
1488 case OHOS::MMI::PointerEvent::MOUSE_BUTTON_MIDDLE:
1489 mouseEvent->button = MOUSE_BUTTON_MIDDLE;
1490 break;
1491 case OHOS::MMI::PointerEvent::MOUSE_BUTTON_RIGHT:
1492 mouseEvent->button = MOUSE_BUTTON_RIGHT;
1493 break;
1494 case OHOS::MMI::PointerEvent::MOUSE_BUTTON_FORWARD:
1495 mouseEvent->button = MOUSE_BUTTON_FORWARD;
1496 break;
1497 case OHOS::MMI::PointerEvent::MOUSE_BUTTON_BACK:
1498 mouseEvent->button = MOUSE_BUTTON_BACK;
1499 break;
1500 default:
1501 MMI_HILOGE("Invalid mouse event button");
1502 return false;
1503 }
1504 return true;
1505 }
1506
MouseEventMonitorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)1507 static void MouseEventMonitorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)
1508 {
1509 CHKPV(event);
1510 Input_MouseEvent* mouseEvent = OH_Input_CreateMouseEvent();
1511 CHKPV(mouseEvent);
1512 OHOS::MMI::PointerEvent::PointerItem item;
1513 if (!(event->GetPointerItem(event->GetPointerId(), item))) {
1514 MMI_HILOGE("Can not get pointerItem for the pointer event");
1515 OH_Input_DestroyMouseEvent(&mouseEvent);
1516 return;
1517 }
1518 if (!SetMouseEventAction(mouseEvent, event->GetPointerAction())) {
1519 OH_Input_DestroyMouseEvent(&mouseEvent);
1520 return;
1521 }
1522 if (!SetMouseEventButton(mouseEvent, event->GetButtonId())) {
1523 OH_Input_DestroyMouseEvent(&mouseEvent);
1524 return;
1525 }
1526 mouseEvent->displayX = item.GetDisplayX();
1527 mouseEvent->displayY = item.GetDisplayY();
1528 mouseEvent->globalX = item.GetGlobalX();
1529 mouseEvent->globalY = item.GetGlobalY();
1530 mouseEvent->actionTime = event->GetActionTime();
1531 mouseEvent->windowId = event->GetTargetWindowId();
1532 mouseEvent->displayId = event->GetTargetDisplayId();
1533 std::lock_guard guard(g_mutex);
1534 for (auto &callback : g_mouseMonitorCallbacks) {
1535 callback(mouseEvent);
1536 }
1537 OH_Input_DestroyMouseEvent(&mouseEvent);
1538 }
1539
SetAxisEventAction(Input_AxisEvent * axisEvent,int32_t action)1540 static void SetAxisEventAction(Input_AxisEvent* axisEvent, int32_t action)
1541 {
1542 CHKPV(axisEvent);
1543 switch (action) {
1544 case OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_BEGIN:
1545 axisEvent->axisAction = AXIS_ACTION_BEGIN;
1546 break;
1547 case OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_UPDATE:
1548 axisEvent->axisAction = AXIS_ACTION_UPDATE;
1549 break;
1550 case OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_END:
1551 axisEvent->axisAction = AXIS_ACTION_END;
1552 break;
1553 default:
1554 break;
1555 }
1556 }
1557
AxisEventMonitorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)1558 static void AxisEventMonitorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)
1559 {
1560 CHKPV(event);
1561 Input_AxisEvent* axisEvent = OH_Input_CreateAxisEvent();
1562 CHKPV(axisEvent);
1563 OHOS::MMI::PointerEvent::PointerItem item;
1564 if (!(event->GetPointerItem(event->GetPointerId(), item))) {
1565 MMI_HILOGE("Can not get pointerItem for the pointer event");
1566 OH_Input_DestroyAxisEvent(&axisEvent);
1567 return;
1568 }
1569 if (!SetAxisValueByAxisEventType(event, axisEvent, event->GetAxisEventType())) {
1570 OH_Input_DestroyAxisEvent(&axisEvent);
1571 return;
1572 }
1573 SetAxisEventAction(axisEvent, event->GetPointerAction());
1574 axisEvent->displayX = item.GetDisplayX();
1575 axisEvent->displayY = item.GetDisplayY();
1576 axisEvent->globalX = item.GetGlobalX();
1577 axisEvent->globalY = item.GetGlobalY();
1578 axisEvent->actionTime = event->GetActionTime();
1579 axisEvent->sourceType = event->GetSourceType();
1580 axisEvent->windowId = event->GetTargetWindowId();
1581 axisEvent->displayId = event->GetTargetDisplayId();
1582 std::lock_guard guard(g_mutex);
1583 for (auto &callback : g_axisMonitorAllCallbacks) {
1584 callback(axisEvent);
1585 }
1586 auto it = g_axisMonitorCallbacks.find(InputEvent_AxisEventType(event->GetAxisEventType()));
1587 if (it != g_axisMonitorCallbacks.end()) {
1588 for (auto &callback : it->second) {
1589 callback(axisEvent);
1590 }
1591 }
1592 OH_Input_DestroyAxisEvent(&axisEvent);
1593 }
1594
PointerEventMonitorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)1595 static void PointerEventMonitorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)
1596 {
1597 CHKPV(event);
1598 if (event->GetSourceType() == SOURCE_TYPE_TOUCHSCREEN) {
1599 TouchEventMonitorCallback(event);
1600 } else if (event->GetSourceType() == SOURCE_TYPE_MOUSE && !IsAxisEvent(event->GetPointerAction())) {
1601 MouseEventMonitorCallback(event);
1602 } else if (IsAxisEvent(event->GetPointerAction()) && event->GetSourceType() != SOURCE_TYPE_TOUCHSCREEN) {
1603 AxisEventMonitorCallback(event);
1604 } else {
1605 MMI_HILOGE("Undefined event type");
1606 }
1607 }
1608
AddPointerEventMonitor()1609 static Input_Result AddPointerEventMonitor()
1610 {
1611 Input_Result retCode = INPUT_SUCCESS;
1612 std::lock_guard guard(g_mutex);
1613 if (g_pointerMonitorId == INVALID_MONITOR_ID) {
1614 int32_t ret = OHOS::Singleton<OHOS::MMI::InputManagerImpl>::GetInstance().AddMonitor(
1615 PointerEventMonitorCallback);
1616 retCode = NormalizeResult(ret);
1617 if (retCode != INPUT_SUCCESS) {
1618 MMI_HILOGE("Add pointer event monitor failed");
1619 return retCode;
1620 }
1621 g_pointerMonitorId = ret;
1622 }
1623 return retCode;
1624 }
1625
OH_Input_AddMouseEventMonitor(Input_MouseEventCallback callback)1626 Input_Result OH_Input_AddMouseEventMonitor(Input_MouseEventCallback callback)
1627 {
1628 CALL_DEBUG_ENTER;
1629 CHKPR(callback, INPUT_PARAMETER_ERROR);
1630 if (!OHOS::MMI::PermissionHelper::GetInstance()->VerifySystemApp()) {
1631 if (!IsScreenCaptureWorking()) {
1632 MMI_HILOGE("The screen capture is not working");
1633 return INPUT_PERMISSION_DENIED;
1634 }
1635 }
1636 Input_Result ret = AddPointerEventMonitor();
1637 if (ret != INPUT_SUCCESS) {
1638 return ret;
1639 }
1640 std::lock_guard guard(g_mutex);
1641 g_mouseMonitorCallbacks.insert(callback);
1642 return INPUT_SUCCESS;
1643 }
1644
OH_Input_AddTouchEventMonitor(Input_TouchEventCallback callback)1645 Input_Result OH_Input_AddTouchEventMonitor(Input_TouchEventCallback callback)
1646 {
1647 CALL_DEBUG_ENTER;
1648 CHKPR(callback, INPUT_PARAMETER_ERROR);
1649 if (!OHOS::MMI::PermissionHelper::GetInstance()->VerifySystemApp()) {
1650 if (!IsScreenCaptureWorking()) {
1651 MMI_HILOGE("The screen capture is not working");
1652 return INPUT_PERMISSION_DENIED;
1653 }
1654 }
1655 Input_Result ret = AddPointerEventMonitor();
1656 if (ret != INPUT_SUCCESS) {
1657 return ret;
1658 }
1659 std::lock_guard guard(g_mutex);
1660 g_touchMonitorCallbacks.insert(callback);
1661 return INPUT_SUCCESS;
1662 }
1663
OH_Input_AddAxisEventMonitorForAll(Input_AxisEventCallback callback)1664 Input_Result OH_Input_AddAxisEventMonitorForAll(Input_AxisEventCallback callback)
1665 {
1666 CALL_DEBUG_ENTER;
1667 CHKPR(callback, INPUT_PARAMETER_ERROR);
1668 if (!OHOS::MMI::PermissionHelper::GetInstance()->VerifySystemApp()) {
1669 if (!IsScreenCaptureWorking()) {
1670 MMI_HILOGE("The screen capture is not working");
1671 return INPUT_PERMISSION_DENIED;
1672 }
1673 }
1674 Input_Result ret = AddPointerEventMonitor();
1675 if (ret != INPUT_SUCCESS) {
1676 return ret;
1677 }
1678 std::lock_guard guard(g_mutex);
1679 g_axisMonitorAllCallbacks.insert(callback);
1680 return INPUT_SUCCESS;
1681 }
1682
OH_Input_AddAxisEventMonitor(InputEvent_AxisEventType axisEventType,Input_AxisEventCallback callback)1683 Input_Result OH_Input_AddAxisEventMonitor(InputEvent_AxisEventType axisEventType, Input_AxisEventCallback callback)
1684 {
1685 CALL_DEBUG_ENTER;
1686 CHKPR(callback, INPUT_PARAMETER_ERROR);
1687 if (!OHOS::MMI::PermissionHelper::GetInstance()->VerifySystemApp()) {
1688 if (!IsScreenCaptureWorking()) {
1689 MMI_HILOGE("The screen capture is not working");
1690 return INPUT_PERMISSION_DENIED;
1691 }
1692 }
1693 Input_Result ret = AddPointerEventMonitor();
1694 if (ret != INPUT_SUCCESS) {
1695 return ret;
1696 }
1697 std::lock_guard guard(g_mutex);
1698 auto it = g_axisMonitorCallbacks.find(axisEventType);
1699 if (it == g_axisMonitorCallbacks.end()) {
1700 std::set<Input_AxisEventCallback> callbacks;
1701 callbacks.insert(callback);
1702 g_axisMonitorCallbacks.insert(std::make_pair(axisEventType, callbacks));
1703 } else {
1704 it->second.insert(callback);
1705 }
1706 return INPUT_SUCCESS;
1707 }
1708
OH_Input_RemoveKeyEventMonitor(Input_KeyEventCallback callback)1709 Input_Result OH_Input_RemoveKeyEventMonitor(Input_KeyEventCallback callback)
1710 {
1711 CALL_DEBUG_ENTER;
1712 CHKPR(callback, INPUT_PARAMETER_ERROR);
1713 Input_Result retCode = INPUT_SUCCESS;
1714 std::lock_guard guard(g_mutex);
1715 auto it = g_keyMonitorCallbacks.find(callback);
1716 if (it == g_keyMonitorCallbacks.end()) {
1717 return INPUT_PARAMETER_ERROR;
1718 }
1719 g_keyMonitorCallbacks.erase(it);
1720 if (g_keyMonitorCallbacks.empty()) {
1721 int32_t ret = OHOS::Singleton<OHOS::MMI::InputManagerImpl>::GetInstance().RemoveMonitor(g_keyMonitorId);
1722 retCode = NormalizeResult(ret);
1723 if (retCode != INPUT_SUCCESS) {
1724 return retCode;
1725 }
1726 g_keyMonitorId = INVALID_MONITOR_ID;
1727 }
1728 return retCode;
1729 }
1730
IsNeedRemoveMonitor()1731 static bool IsNeedRemoveMonitor()
1732 {
1733 if (g_mouseMonitorCallbacks.empty() && g_touchMonitorCallbacks.empty() &&
1734 g_axisMonitorCallbacks.empty() && g_axisMonitorAllCallbacks.empty()) {
1735 return true;
1736 }
1737 return false;
1738 }
1739
RemovePointerEventMonitor()1740 static Input_Result RemovePointerEventMonitor()
1741 {
1742 Input_Result retCode = INPUT_SUCCESS;
1743 if (IsNeedRemoveMonitor()) {
1744 int32_t ret = OHOS::Singleton<OHOS::MMI::InputManagerImpl>::GetInstance().RemoveMonitor(g_pointerMonitorId);
1745 retCode = NormalizeResult(ret);
1746 if (retCode != INPUT_SUCCESS) {
1747 return retCode;
1748 }
1749 g_pointerMonitorId = INVALID_MONITOR_ID;
1750 }
1751 return retCode;
1752 }
1753
OH_Input_RemoveMouseEventMonitor(Input_MouseEventCallback callback)1754 Input_Result OH_Input_RemoveMouseEventMonitor(Input_MouseEventCallback callback)
1755 {
1756 CALL_DEBUG_ENTER;
1757 CHKPR(callback, INPUT_PARAMETER_ERROR);
1758 std::lock_guard guard(g_mutex);
1759 auto it = g_mouseMonitorCallbacks.find(callback);
1760 if (it == g_mouseMonitorCallbacks.end()) {
1761 MMI_HILOGE("The callback has not been added");
1762 return INPUT_PARAMETER_ERROR;
1763 }
1764 g_mouseMonitorCallbacks.erase(it);
1765 return RemovePointerEventMonitor();
1766 }
1767
OH_Input_RemoveTouchEventMonitor(Input_TouchEventCallback callback)1768 Input_Result OH_Input_RemoveTouchEventMonitor(Input_TouchEventCallback callback)
1769 {
1770 CALL_DEBUG_ENTER;
1771 CHKPR(callback, INPUT_PARAMETER_ERROR);
1772 std::lock_guard guard(g_mutex);
1773 auto it = g_touchMonitorCallbacks.find(callback);
1774 if (it == g_touchMonitorCallbacks.end()) {
1775 MMI_HILOGE("The callback has not been added.");
1776 return INPUT_PARAMETER_ERROR;
1777 }
1778 g_touchMonitorCallbacks.erase(it);
1779 return RemovePointerEventMonitor();
1780 }
1781
OH_Input_RemoveAxisEventMonitorForAll(Input_AxisEventCallback callback)1782 Input_Result OH_Input_RemoveAxisEventMonitorForAll(Input_AxisEventCallback callback)
1783 {
1784 CALL_DEBUG_ENTER;
1785 CHKPR(callback, INPUT_PARAMETER_ERROR);
1786 std::lock_guard guard(g_mutex);
1787 auto it = g_axisMonitorAllCallbacks.find(callback);
1788 if (it == g_axisMonitorAllCallbacks.end()) {
1789 MMI_HILOGE("The callback has not been added.");
1790 return INPUT_PARAMETER_ERROR;
1791 }
1792 g_axisMonitorAllCallbacks.erase(it);
1793 return RemovePointerEventMonitor();
1794 }
1795
OH_Input_RemoveAxisEventMonitor(InputEvent_AxisEventType axisEventType,Input_AxisEventCallback callback)1796 Input_Result OH_Input_RemoveAxisEventMonitor(InputEvent_AxisEventType axisEventType, Input_AxisEventCallback callback)
1797 {
1798 CALL_DEBUG_ENTER;
1799 CHKPR(callback, INPUT_PARAMETER_ERROR);
1800 std::lock_guard guard(g_mutex);
1801 if (g_axisMonitorCallbacks.find(axisEventType) == g_axisMonitorCallbacks.end()) {
1802 MMI_HILOGE("The axis event type has not been added");
1803 return INPUT_PARAMETER_ERROR;
1804 }
1805 auto it = g_axisMonitorCallbacks[axisEventType].find(callback);
1806 if (it == g_axisMonitorCallbacks[axisEventType].end()) {
1807 MMI_HILOGE("The callback has not been added");
1808 return INPUT_PARAMETER_ERROR;
1809 }
1810 g_axisMonitorCallbacks[axisEventType].erase(it);
1811 if (g_axisMonitorCallbacks[axisEventType].empty()) {
1812 g_axisMonitorCallbacks.erase(axisEventType);
1813 }
1814 return RemovePointerEventMonitor();
1815 }
1816
KeyEventInterceptorCallback(std::shared_ptr<OHOS::MMI::KeyEvent> event)1817 static void KeyEventInterceptorCallback(std::shared_ptr<OHOS::MMI::KeyEvent> event)
1818 {
1819 CHKPV(event);
1820 Input_KeyEvent* keyEvent = OH_Input_CreateKeyEvent();
1821 CHKPV(keyEvent);
1822 if (!SetKeyEventAction(keyEvent, event->GetKeyAction())) {
1823 OH_Input_DestroyKeyEvent(&keyEvent);
1824 return;
1825 }
1826 keyEvent->keyCode = event->GetKeyCode();
1827 keyEvent->actionTime = event->GetActionTime();
1828 keyEvent->windowId = event->GetTargetWindowId();
1829 keyEvent->displayId = event->GetTargetDisplayId();
1830 std::lock_guard guard(g_mutex);
1831 if (g_keyInterceptorCallback != nullptr) {
1832 g_keyInterceptorCallback(keyEvent);
1833 }
1834 OH_Input_DestroyKeyEvent(&keyEvent);
1835 }
1836
OH_Input_AddKeyEventInterceptor(Input_KeyEventCallback callback,Input_InterceptorOptions * option)1837 Input_Result OH_Input_AddKeyEventInterceptor(Input_KeyEventCallback callback, Input_InterceptorOptions *option)
1838 {
1839 CALL_DEBUG_ENTER;
1840 CHKPR(callback, INPUT_PARAMETER_ERROR);
1841 Input_Result retCode = INPUT_SUCCESS;
1842 std::lock_guard guard(g_mutex);
1843 if (g_keyInterceptorId != INVALID_INTERCEPTOR_ID) {
1844 MMI_HILOGE("Another key event interceptor has been added");
1845 return INPUT_REPEAT_INTERCEPTOR;
1846 }
1847 CHKPR(g_keyInterceptor, INPUT_PARAMETER_ERROR);
1848 g_keyInterceptor->SetCallback(KeyEventInterceptorCallback);
1849 int32_t ret = g_keyInterceptor->Start(OHOS::MMI::INTERCEPTOR_TYPE_KEY);
1850 retCode = NormalizeResult(ret);
1851 if (retCode != INPUT_SUCCESS) {
1852 MMI_HILOGE("Add key event interceptor failed.");
1853 return retCode;
1854 }
1855 g_keyInterceptorId = ret;
1856 g_keyInterceptorCallback = callback;
1857 return retCode;
1858 }
1859
TouchEventInterceptorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)1860 static void TouchEventInterceptorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)
1861 {
1862 CHKPV(event);
1863 std::lock_guard guard(g_mutex);
1864 CHKPV(g_pointerInterceptorCallback);
1865 if (g_pointerInterceptorCallback->touchCallback == nullptr) {
1866 MMI_HILOGE("There is no callback for mouse event interceptor");
1867 return;
1868 }
1869 Input_TouchEvent* touchEvent = OH_Input_CreateTouchEvent();
1870 CHKPV(touchEvent);
1871 OHOS::MMI::PointerEvent::PointerItem item;
1872 if (!(event->GetPointerItem(event->GetPointerId(), item))) {
1873 MMI_HILOGE("Can not get pointerItem for the pointer event");
1874 OH_Input_DestroyTouchEvent(&touchEvent);
1875 return;
1876 }
1877 if (!SetTouchEventAction(touchEvent, event->GetPointerAction())) {
1878 OH_Input_DestroyTouchEvent(&touchEvent);
1879 return;
1880 }
1881 touchEvent->id = event->GetPointerId();
1882 touchEvent->displayX = item.GetDisplayX();
1883 touchEvent->displayY = item.GetDisplayY();
1884 touchEvent->globalX = item.GetGlobalX();
1885 touchEvent->globalY = item.GetGlobalY();
1886 touchEvent->actionTime = event->GetActionTime();
1887 touchEvent->windowId = event->GetTargetWindowId();
1888 touchEvent->displayId = event->GetTargetDisplayId();
1889 g_pointerInterceptorCallback->touchCallback(touchEvent);
1890 OH_Input_DestroyTouchEvent(&touchEvent);
1891 }
1892
MouseEventInterceptorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)1893 static void MouseEventInterceptorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)
1894 {
1895 CHKPV(event);
1896 std::lock_guard guard(g_mutex);
1897 CHKPV(g_pointerInterceptorCallback);
1898 if (g_pointerInterceptorCallback->mouseCallback == nullptr) {
1899 MMI_HILOGE("There is no callback for mouse event interceptor");
1900 return;
1901 }
1902 Input_MouseEvent* mouseEvent = OH_Input_CreateMouseEvent();
1903 CHKPV(mouseEvent);
1904 OHOS::MMI::PointerEvent::PointerItem item;
1905 if (!(event->GetPointerItem(event->GetPointerId(), item))) {
1906 MMI_HILOGE("Can not get pointerItem for the pointer event");
1907 OH_Input_DestroyMouseEvent(&mouseEvent);
1908 return;
1909 }
1910 if (!SetMouseEventAction(mouseEvent, event->GetPointerAction())) {
1911 OH_Input_DestroyMouseEvent(&mouseEvent);
1912 return;
1913 }
1914 if (!SetMouseEventButton(mouseEvent, event->GetButtonId())) {
1915 OH_Input_DestroyMouseEvent(&mouseEvent);
1916 return;
1917 }
1918 mouseEvent->displayX = item.GetDisplayX();
1919 mouseEvent->displayY = item.GetDisplayY();
1920 mouseEvent->globalX = item.GetGlobalX();
1921 mouseEvent->globalY = item.GetGlobalY();
1922 mouseEvent->actionTime = event->GetActionTime();
1923 mouseEvent->windowId = event->GetTargetWindowId();
1924 mouseEvent->displayId = event->GetTargetDisplayId();
1925 g_pointerInterceptorCallback->mouseCallback(mouseEvent);
1926 OH_Input_DestroyMouseEvent(&mouseEvent);
1927 }
1928
AxisEventInterceptorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)1929 static void AxisEventInterceptorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)
1930 {
1931 CHKPV(event);
1932 std::lock_guard guard(g_mutex);
1933 CHKPV(g_pointerInterceptorCallback);
1934 if (g_pointerInterceptorCallback->axisCallback == nullptr) {
1935 MMI_HILOGE("There is no callback for axis event interceptor");
1936 return;
1937 }
1938 Input_AxisEvent* axisEvent = OH_Input_CreateAxisEvent();
1939 CHKPV(axisEvent);
1940 OHOS::MMI::PointerEvent::PointerItem item;
1941 if (!(event->GetPointerItem(event->GetPointerId(), item))) {
1942 MMI_HILOGE("Can not get pointerItem for the pointer event");
1943 OH_Input_DestroyAxisEvent(&axisEvent);
1944 return;
1945 }
1946 if (!SetAxisValueByAxisEventType(event, axisEvent, event->GetAxisEventType())) {
1947 MMI_HILOGE("Fail to set axis value");
1948 OH_Input_DestroyAxisEvent(&axisEvent);
1949 return;
1950 }
1951 SetAxisEventAction(axisEvent, event->GetPointerAction());
1952 axisEvent->displayX = item.GetDisplayX();
1953 axisEvent->displayY = item.GetDisplayY();
1954 axisEvent->globalX = item.GetGlobalX();
1955 axisEvent->globalY = item.GetGlobalY();
1956 axisEvent->actionTime = event->GetActionTime();
1957 axisEvent->sourceType = event->GetSourceType();
1958 axisEvent->windowId = event->GetTargetWindowId();
1959 axisEvent->displayId = event->GetTargetDisplayId();
1960 g_pointerInterceptorCallback->axisCallback(axisEvent);
1961 OH_Input_DestroyAxisEvent(&axisEvent);
1962 }
1963
PointerEventInterceptorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)1964 static void PointerEventInterceptorCallback(std::shared_ptr<OHOS::MMI::PointerEvent> event)
1965 {
1966 CHKPV(event);
1967 if (event->GetSourceType() == SOURCE_TYPE_TOUCHSCREEN) {
1968 TouchEventInterceptorCallback(event);
1969 } else if (event->GetSourceType() == SOURCE_TYPE_MOUSE && !IsAxisEvent(event->GetPointerAction())) {
1970 MouseEventInterceptorCallback(event);
1971 } else if (IsAxisEvent(event->GetPointerAction()) && event->GetSourceType() != SOURCE_TYPE_TOUCHSCREEN) {
1972 AxisEventInterceptorCallback(event);
1973 } else {
1974 MMI_HILOGE("Undefined event type");
1975 }
1976 }
1977
OH_Input_AddInputEventInterceptor(Input_InterceptorEventCallback * callback,Input_InterceptorOptions * option)1978 Input_Result OH_Input_AddInputEventInterceptor(Input_InterceptorEventCallback *callback,
1979 Input_InterceptorOptions *option)
1980 {
1981 CALL_DEBUG_ENTER;
1982 CHKPR(callback, INPUT_PARAMETER_ERROR);
1983 Input_Result retCode = INPUT_SUCCESS;
1984 std::lock_guard guard(g_mutex);
1985 if (g_pointerInterceptorId != INVALID_INTERCEPTOR_ID) {
1986 MMI_HILOGE("Another interceptor for input event has been added");
1987 return INPUT_REPEAT_INTERCEPTOR;
1988 }
1989 g_pointerInterceptor->SetCallback(PointerEventInterceptorCallback);
1990 int32_t ret = g_pointerInterceptor->Start(OHOS::MMI::INTERCEPTOR_TYPE_POINTER);
1991 retCode = NormalizeResult(ret);
1992 if (retCode != INPUT_SUCCESS) {
1993 MMI_HILOGE("Add pointer event interceptor failed.");
1994 return retCode;
1995 }
1996 g_pointerInterceptorId = ret;
1997 g_pointerInterceptorCallback = callback;
1998 return retCode;
1999 }
2000
OH_Input_RemoveKeyEventInterceptor(void)2001 Input_Result OH_Input_RemoveKeyEventInterceptor(void)
2002 {
2003 CALL_DEBUG_ENTER;
2004 CHKPR(g_keyInterceptor, INPUT_PARAMETER_ERROR);
2005 Input_Result retCode = INPUT_SUCCESS;
2006 std::lock_guard guard(g_mutex);
2007 int32_t ret = g_keyInterceptor->Stop(OHOS::MMI::INTERCEPTOR_TYPE_KEY);
2008 retCode = NormalizeResult(ret);
2009 if (retCode != INPUT_SUCCESS) {
2010 MMI_HILOGE("Remove key event interceptor failed.");
2011 return retCode;
2012 }
2013 g_keyInterceptorCallback = nullptr;
2014 g_keyInterceptorId = INVALID_INTERCEPTOR_ID;
2015 return retCode;
2016 }
2017
OH_Input_RemoveInputEventInterceptor(void)2018 Input_Result OH_Input_RemoveInputEventInterceptor(void)
2019 {
2020 CALL_DEBUG_ENTER;
2021 Input_Result retCode = INPUT_SUCCESS;
2022 std::lock_guard guard(g_mutex);
2023 int32_t ret = g_pointerInterceptor->Stop(OHOS::MMI::INTERCEPTOR_TYPE_POINTER);
2024 retCode = NormalizeResult(ret);
2025 if (retCode != INPUT_SUCCESS) {
2026 MMI_HILOGE("Remove pointer event interceptor failed.");
2027 return retCode;
2028 }
2029 g_pointerInterceptorId = INVALID_INTERCEPTOR_ID;
2030 g_pointerInterceptorCallback = nullptr;
2031 return retCode;
2032 }
2033
OH_Input_GetIntervalSinceLastInput(int64_t * intervalSinceLastInput)2034 int32_t OH_Input_GetIntervalSinceLastInput(int64_t *intervalSinceLastInput)
2035 {
2036 CALL_DEBUG_ENTER;
2037 CHKPR(intervalSinceLastInput, INPUT_PARAMETER_ERROR);
2038 int64_t interval = -1;
2039 int32_t ret = OHOS::MMI::InputManager::GetInstance()->GetIntervalSinceLastInput(interval);
2040 *intervalSinceLastInput = interval;
2041 Input_Result retCode = INPUT_SUCCESS;
2042 retCode = NormalizeResult(ret);
2043 if (retCode != INPUT_SUCCESS) {
2044 MMI_HILOGE("Get Interval Since Last Input failed");
2045 return retCode;
2046 }
2047 return INPUT_SUCCESS;
2048 }
2049
OH_Input_CreateAllSystemHotkeys(int32_t count)2050 Input_Hotkey **OH_Input_CreateAllSystemHotkeys(int32_t count)
2051 {
2052 if (count <= 0) {
2053 MMI_HILOGE("Invalid count:%{public}d", count);
2054 return nullptr;
2055 }
2056 std::vector<std::unique_ptr<OHOS::MMI::KeyOption>> keyOptions;
2057 int32_t hotkeyCount = -1;
2058 int32_t ret = OHOS::MMI::InputManager::GetInstance()->GetAllSystemHotkeys(keyOptions, hotkeyCount);
2059 if (ret != RET_OK || hotkeyCount < 0) {
2060 MMI_HILOGE("GetAllSystemHotkeys fail");
2061 return nullptr;
2062 }
2063 if (count != hotkeyCount) {
2064 MMI_HILOGE("Parameter error");
2065 return nullptr;
2066 }
2067 auto hotkeys = new (std::nothrow)Input_Hotkey *[count];
2068 if (hotkeys == nullptr) {
2069 MMI_HILOGE("Memory allocation failed");
2070 return nullptr;
2071 }
2072 for (int32_t i = 0; i < count; ++i) {
2073 hotkeys[i] = new (std::nothrow)Input_Hotkey();
2074 if (hotkeys[i] == nullptr) {
2075 MMI_HILOGE("Memory allocation failed");
2076 for (int32_t j = 0; j < i; ++j) {
2077 delete hotkeys[j];
2078 hotkeys[j] = nullptr;
2079 }
2080 delete[] hotkeys;
2081 hotkeys = nullptr;
2082 return nullptr;
2083 }
2084 }
2085 std::lock_guard<std::mutex> lock(g_hotkeyCountsMutex);
2086 g_hotkeyCounts.insert(std::make_pair(hotkeys, count));
2087 return hotkeys;
2088 }
2089
OH_Input_DestroyAllSystemHotkeys(Input_Hotkey ** hotkeys,int32_t count)2090 void OH_Input_DestroyAllSystemHotkeys(Input_Hotkey **hotkeys, int32_t count)
2091 {
2092 std::lock_guard<std::mutex> lock(g_hotkeyCountsMutex);
2093 if (g_hotkeyCounts.find(hotkeys) != g_hotkeyCounts.end()) {
2094 if (count != g_hotkeyCounts[hotkeys]) {
2095 MMI_HILOGW("Parameter inconsistency");
2096 }
2097 for (int32_t i = 0; i < g_hotkeyCounts[hotkeys]; ++i) {
2098 if (hotkeys[i] != nullptr) {
2099 delete hotkeys[i];
2100 hotkeys[i] = nullptr;
2101 }
2102 }
2103 if (hotkeys != nullptr) {
2104 delete[] hotkeys;
2105 hotkeys = nullptr;
2106 }
2107 g_hotkeyCounts.erase(hotkeys);
2108 }
2109 }
2110
OH_Input_GetAllSystemHotkeys(Input_Hotkey ** hotkey,int32_t * count)2111 Input_Result OH_Input_GetAllSystemHotkeys(Input_Hotkey **hotkey, int32_t *count)
2112 {
2113 CALL_DEBUG_ENTER;
2114 CHKPR(count, INPUT_PARAMETER_ERROR);
2115 std::vector<std::unique_ptr<OHOS::MMI::KeyOption>> keyOptions;
2116 int32_t hotkeyCount = -1;
2117 int32_t ret = OHOS::MMI::InputManager::GetInstance()->GetAllSystemHotkeys(keyOptions, hotkeyCount);
2118 if (ret != RET_OK || hotkeyCount < 0) {
2119 MMI_HILOGE("GetAllSystemHotkeys fail");
2120 return INPUT_SERVICE_EXCEPTION;
2121 }
2122 if (hotkey == nullptr) {
2123 *count = static_cast<int32_t>(hotkeyCount);
2124 MMI_HILOGD("Hot key count:%{public}d", *count);
2125 return INPUT_SUCCESS;
2126 }
2127 if ((static_cast<int32_t>(hotkeyCount) != *count) && (*count <= 0)) {
2128 MMI_HILOGE("Count:%{public}d is invalid, should be:%{public}d", *count, hotkeyCount);
2129 return INPUT_PARAMETER_ERROR;
2130 }
2131 for (int32_t i = 0; i < hotkeyCount; ++i) {
2132 if (hotkey[i] == nullptr) {
2133 MMI_HILOGE("Hotkey is null, i:%{public}d", i);
2134 return INPUT_PARAMETER_ERROR;
2135 }
2136 hotkey[i]->preKeys = keyOptions[i]->GetPreKeys();
2137 hotkey[i]->finalKey = keyOptions[i]->GetFinalKey();
2138 }
2139 return INPUT_SUCCESS;
2140 }
2141
OH_Input_CreateHotkey(void)2142 Input_Hotkey* OH_Input_CreateHotkey(void)
2143 {
2144 CALL_DEBUG_ENTER;
2145 Input_Hotkey* hotkey = new (std::nothrow) Input_Hotkey();
2146 CHKPP(hotkey);
2147 return hotkey;
2148 }
2149
OH_Input_DestroyHotkey(Input_Hotkey ** hotkey)2150 void OH_Input_DestroyHotkey(Input_Hotkey **hotkey)
2151 {
2152 CALL_DEBUG_ENTER;
2153 CHKPV(hotkey);
2154 CHKPV(*hotkey);
2155 delete *hotkey;
2156 *hotkey = nullptr;
2157 }
2158
OH_Input_SetPreKeys(Input_Hotkey * hotkey,int32_t * preKeys,int32_t size)2159 void OH_Input_SetPreKeys(Input_Hotkey *hotkey, int32_t *preKeys, int32_t size)
2160 {
2161 CALL_DEBUG_ENTER;
2162 CHKPV(hotkey);
2163 CHKPV(preKeys);
2164 if (size <= 0) {
2165 MMI_HILOGE("PreKeys does not exist");
2166 return;
2167 }
2168
2169 for (int32_t i = 0; i < size; ++i) {
2170 hotkey->preKeys.insert(preKeys[i]);
2171 }
2172 return;
2173 }
2174
OH_Input_GetPreKeys(const Input_Hotkey * hotkey,int32_t ** preKeys,int32_t * preKeyCount)2175 Input_Result OH_Input_GetPreKeys(const Input_Hotkey *hotkey, int32_t **preKeys, int32_t *preKeyCount)
2176 {
2177 CALL_DEBUG_ENTER;
2178 CHKPR(hotkey, INPUT_PARAMETER_ERROR);
2179 CHKPR(preKeys, INPUT_PARAMETER_ERROR);
2180 CHKPR(*preKeys, INPUT_PARAMETER_ERROR);
2181 CHKPR(preKeyCount, INPUT_PARAMETER_ERROR);
2182 std::set<int32_t> preKey = hotkey->preKeys;
2183 if (preKey.empty()) {
2184 MMI_HILOGE("The pressKeys not exit");
2185 return INPUT_SERVICE_EXCEPTION;
2186 }
2187 int32_t index = 0;
2188 for (auto it = preKey.begin(); it != preKey.end(); ++it) {
2189 *preKeys[index++] = *it;
2190 }
2191 *preKeyCount = index;
2192 return INPUT_SUCCESS;
2193 }
2194
OH_Input_SetFinalKey(Input_Hotkey * hotkey,int32_t finalKey)2195 void OH_Input_SetFinalKey(Input_Hotkey *hotkey, int32_t finalKey)
2196 {
2197 CALL_DEBUG_ENTER;
2198 CHKPV(hotkey);
2199 hotkey->finalKey = finalKey;
2200 return;
2201 }
2202
OH_Input_GetFinalKey(const Input_Hotkey * hotkey,int32_t * finalKeyCode)2203 Input_Result OH_Input_GetFinalKey(const Input_Hotkey *hotkey, int32_t *finalKeyCode)
2204 {
2205 CALL_DEBUG_ENTER;
2206 CHKPR(hotkey, INPUT_PARAMETER_ERROR);
2207 CHKPR(finalKeyCode, INPUT_PARAMETER_ERROR);
2208 *finalKeyCode = hotkey->finalKey;
2209 return INPUT_SUCCESS;
2210 }
2211
OH_Input_SetRepeat(Input_Hotkey * hotkey,bool isRepeat)2212 void OH_Input_SetRepeat(Input_Hotkey* hotkey, bool isRepeat)
2213 {
2214 CALL_DEBUG_ENTER;
2215 CHKPV(hotkey);
2216 hotkey->isRepeat = isRepeat;
2217 }
2218
OH_Input_GetRepeat(const Input_Hotkey * hotkey,bool * isRepeat)2219 Input_Result OH_Input_GetRepeat(const Input_Hotkey* hotkey, bool *isRepeat)
2220 {
2221 CALL_DEBUG_ENTER;
2222 CHKPR(hotkey, INPUT_PARAMETER_ERROR);
2223 CHKPR(isRepeat, INPUT_PARAMETER_ERROR);
2224 *isRepeat = hotkey->isRepeat;
2225 return INPUT_SUCCESS;
2226 }
2227
GetHotkeyName(std::set<int32_t> preKeys,int32_t finalKey,bool isRepeat,std::shared_ptr<OHOS::MMI::KeyOption> keyOption)2228 std::string GetHotkeyName(std::set<int32_t> preKeys, int32_t finalKey, bool isRepeat,
2229 std::shared_ptr<OHOS::MMI::KeyOption> keyOption)
2230 {
2231 bool isFinalKeyDown = true;
2232 int32_t keyDownDuration = 0;
2233 keyOption->SetPreKeys(preKeys);
2234 keyOption->SetFinalKey(finalKey);
2235 keyOption->SetFinalKeyDown(isFinalKeyDown);
2236 keyOption->SetFinalKeyDownDuration(keyDownDuration);
2237 keyOption->SetRepeat(isRepeat);
2238 std::string hotkeyName;
2239 for (const auto &item : preKeys) {
2240 hotkeyName += std::to_string(item);
2241 hotkeyName += ",";
2242 }
2243 hotkeyName += std::to_string(finalKey);
2244 hotkeyName += ",";
2245 hotkeyName += std::to_string(isFinalKeyDown);
2246 hotkeyName += ",";
2247 hotkeyName += std::to_string(keyDownDuration);
2248 hotkeyName += ",";
2249 hotkeyName += std::to_string(isRepeat);
2250 return hotkeyName;
2251 }
2252
MakeHotkeyInfo(const Input_Hotkey * hotkey,Input_HotkeyInfo * hotkeyInfo,std::shared_ptr<OHOS::MMI::KeyOption> keyOption)2253 static int32_t MakeHotkeyInfo(const Input_Hotkey* hotkey, Input_HotkeyInfo* hotkeyInfo,
2254 std::shared_ptr<OHOS::MMI::KeyOption> keyOption)
2255 {
2256 CALL_DEBUG_ENTER;
2257 CHKPR(hotkey, INPUT_PARAMETER_ERROR);
2258 CHKPR(hotkeyInfo, INPUT_PARAMETER_ERROR);
2259 CHKPR(keyOption, INPUT_PARAMETER_ERROR);
2260
2261 if (hotkey->preKeys.empty()) {
2262 MMI_HILOGE("PressedKeys not found");
2263 return INPUT_PARAMETER_ERROR;
2264 }
2265 std::set<int32_t> preKeys = hotkey->preKeys;
2266 if (preKeys.size() > PRE_KEYS_SIZE) {
2267 MMI_HILOGE("PreKeys size invalid");
2268 return INPUT_PARAMETER_ERROR;
2269 }
2270 for (const auto &item : preKeys) {
2271 auto it = std::find(g_pressKeyCodes.begin(), g_pressKeyCodes.end(), item);
2272 if (it == g_pressKeyCodes.end()) {
2273 MMI_HILOGE("PreKeys is not expect");
2274 return INPUT_PARAMETER_ERROR;
2275 }
2276 }
2277
2278 int32_t finalKey = hotkey->finalKey;
2279 if (finalKey < 0) {
2280 MMI_HILOGE("FinalKey:%{private}d is less 0, can not process", finalKey);
2281 return INPUT_PARAMETER_ERROR;
2282 }
2283 auto it = std::find(g_finalKeyCodes.begin(), g_finalKeyCodes.end(), finalKey);
2284 if (it != g_finalKeyCodes.end()) {
2285 MMI_HILOGE("FinalKey is not expect");
2286 return INPUT_PARAMETER_ERROR;
2287 }
2288
2289 bool isRepeat = hotkey->isRepeat;
2290 std::string hotkeyName = GetHotkeyName(preKeys, finalKey, isRepeat, keyOption);
2291 hotkeyInfo->hotkeyId = hotkeyName;
2292 MMI_HILOGI("HotkeyId is :%{private}s", hotkeyInfo->hotkeyId.c_str());
2293 return INPUT_SUCCESS;
2294 }
2295
GetSubscribeId(Input_HotkeyInfo * hotkeyInfo)2296 static int32_t GetSubscribeId(Input_HotkeyInfo* hotkeyInfo)
2297 {
2298 CALL_DEBUG_ENTER;
2299 CHKPR(hotkeyInfo, INPUT_PARAMETER_ERROR);
2300
2301 auto it = g_callbacks.find(hotkeyInfo->hotkeyId);
2302 if (it == g_callbacks.end() || it->second.empty()) {
2303 MMI_HILOGD("The callbacks is empty");
2304 return INPUT_PARAMETER_ERROR;
2305 }
2306
2307 CHKPR(it->second.front(), INPUT_PARAMETER_ERROR);
2308 return it->second.front()->subscribeId;
2309 }
2310
AddHotkeySubscribe(Input_HotkeyInfo * hotkeyInfo)2311 static int32_t AddHotkeySubscribe(Input_HotkeyInfo* hotkeyInfo)
2312 {
2313 CALL_DEBUG_ENTER;
2314 CHKPR(hotkeyInfo, INPUT_PARAMETER_ERROR);
2315 if (g_callbacks.find(hotkeyInfo->hotkeyId) == g_callbacks.end()) {
2316 MMI_HILOGD("No callback in %{private}s", hotkeyInfo->hotkeyId.c_str());
2317 g_callbacks[hotkeyInfo->hotkeyId] = {};
2318 }
2319 auto it = g_callbacks.find(hotkeyInfo->hotkeyId);
2320 if (it != g_callbacks.end()) {
2321 for (const auto &iter: it->second) {
2322 if (iter->callback == hotkeyInfo->callback) {
2323 MMI_HILOGI("Callback already exist");
2324 return INPUT_PARAMETER_ERROR;
2325 }
2326 }
2327 }
2328 it->second.push_back(hotkeyInfo);
2329 return INPUT_SUCCESS;
2330 }
2331
CheckHotkey(std::shared_ptr<OHOS::MMI::KeyEvent> keyEvent,std::string & hotkeyEventName)2332 static bool CheckHotkey(std::shared_ptr<OHOS::MMI::KeyEvent> keyEvent, std::string &hotkeyEventName)
2333 {
2334 CALL_DEBUG_ENTER;
2335 CHKPF(keyEvent);
2336
2337 int32_t keyEventFinalKey = keyEvent->GetKeyCode();
2338 std::vector<OHOS::MMI::KeyEvent::KeyItem> items = keyEvent->GetKeyItems();
2339 if (items.size() > KEYS_SIZE || keyEvent->GetKeyAction() != OHOS::MMI::KeyEvent::KEY_ACTION_DOWN) {
2340 MMI_HILOGE("Prekeys num more than three or finalkey is not down");
2341 return false;
2342 }
2343
2344 bool isFinalKeyDown = true;
2345 int32_t keyDownDuration = 0;
2346 std::string hotkeyName;
2347 bool isRepeat = keyEvent->IsRepeat();
2348 std::set<int32_t> presskeys;
2349 for (const auto &item : items) {
2350 presskeys.insert(item.GetKeyCode());
2351 }
2352 for (const auto &item : presskeys) {
2353 if (item == keyEventFinalKey) {
2354 continue;
2355 }
2356 hotkeyName += std::to_string(item);
2357 hotkeyName += ",";
2358 }
2359 hotkeyName += std::to_string(keyEventFinalKey);
2360 hotkeyName += ",";
2361 hotkeyName += std::to_string(isFinalKeyDown);
2362 hotkeyName += ",";
2363 hotkeyName += std::to_string(keyDownDuration);
2364 hotkeyName += ",";
2365 hotkeyName += std::to_string(isRepeat);
2366 hotkeyEventName = hotkeyName;
2367 MMI_HILOGD("HotkeyEventName:%{private}s", hotkeyEventName.c_str());
2368 return true;
2369 }
2370
OnNotifyCallbackWorkResult(Input_HotkeyInfo * reportEvent)2371 static void OnNotifyCallbackWorkResult(Input_HotkeyInfo* reportEvent)
2372 {
2373 CALL_DEBUG_ENTER;
2374 CHKPV(reportEvent);
2375
2376 Input_HotkeyInfo *info = new(std::nothrow) Input_HotkeyInfo();
2377 CHKPV(info);
2378 info->keyOption = reportEvent->keyOption;
2379 if (info->keyOption == nullptr) {
2380 delete info;
2381 info = nullptr;
2382 MMI_HILOGE("KeyOption is null");
2383 return;
2384 }
2385 info->callback = reportEvent->callback;
2386 if (info->callback == nullptr) {
2387 delete info;
2388 info = nullptr;
2389 MMI_HILOGE("Callback is null");
2390 return;
2391 }
2392
2393 Input_Hotkey hotkey;
2394 hotkey.preKeys = info->keyOption->GetPreKeys();
2395 hotkey.finalKey = info->keyOption->GetFinalKey();
2396 hotkey.isRepeat = info->keyOption->IsRepeat();
2397 info->callback(&hotkey);
2398 delete info;
2399 info = nullptr;
2400 }
2401
HandleKeyEvent(std::shared_ptr<OHOS::MMI::KeyEvent> keyEvent)2402 static void HandleKeyEvent(std::shared_ptr<OHOS::MMI::KeyEvent> keyEvent)
2403 {
2404 CALL_DEBUG_ENTER;
2405 CHKPV(keyEvent);
2406 std::lock_guard guard(g_CallBacksMutex);
2407 std::string hotkeyEventName;
2408 if (CheckHotkey(keyEvent, hotkeyEventName)) {
2409 auto list = g_callbacks[hotkeyEventName];
2410 MMI_HILOGD("Callback list size:%{public}zu", list.size());
2411 for (const auto &info : list) {
2412 if (info->hotkeyId == hotkeyEventName) {
2413 OnNotifyCallbackWorkResult(info);
2414 }
2415 }
2416 }
2417 }
2418
OH_Input_AddHotkeyMonitor(const Input_Hotkey * hotkey,Input_HotkeyCallback callback)2419 Input_Result OH_Input_AddHotkeyMonitor(const Input_Hotkey* hotkey, Input_HotkeyCallback callback)
2420 {
2421 CALL_DEBUG_ENTER;
2422 CHKPR(hotkey, INPUT_PARAMETER_ERROR);
2423 CHKPR(callback, INPUT_PARAMETER_ERROR);
2424 std::lock_guard guard(g_CallBacksMutex);
2425
2426 Input_HotkeyInfo *hotkeyInfo = new (std::nothrow) Input_HotkeyInfo();
2427 CHKPR(hotkeyInfo, INPUT_PARAMETER_ERROR);
2428 auto keyOption = std::make_shared<OHOS::MMI::KeyOption>();
2429 if (MakeHotkeyInfo(hotkey, hotkeyInfo, keyOption) != INPUT_SUCCESS) {
2430 delete hotkeyInfo;
2431 hotkeyInfo = nullptr;
2432 MMI_HILOGE("MakeHotkeyInfo failed");
2433 return INPUT_PARAMETER_ERROR;
2434 }
2435 hotkeyInfo->keyOption = keyOption;
2436 hotkeyInfo->callback = callback;
2437 int32_t preSubscribeId = GetSubscribeId(hotkeyInfo);
2438 if (preSubscribeId == INPUT_PARAMETER_ERROR) {
2439 MMI_HILOGD("HotkeyId:%{private}s", hotkeyInfo->hotkeyId.c_str());
2440 int32_t subscribeId = -1;
2441 subscribeId = OHOS::MMI::InputManager::GetInstance()->SubscribeHotkey(keyOption, HandleKeyEvent);
2442 if (subscribeId == OHOS::MMI::ERROR_UNSUPPORT) {
2443 delete hotkeyInfo;
2444 hotkeyInfo = nullptr;
2445 MMI_HILOGE("SubscribeId invalid:%{public}d", subscribeId);
2446 return INPUT_DEVICE_NOT_SUPPORTED;
2447 }
2448 if (subscribeId == OCCUPIED_BY_SYSTEM) {
2449 delete hotkeyInfo;
2450 hotkeyInfo = nullptr;
2451 MMI_HILOGE("SubscribeId invalid:%{public}d", subscribeId);
2452 return INPUT_OCCUPIED_BY_SYSTEM;
2453 }
2454 if (subscribeId == OCCUPIED_BY_OTHER) {
2455 delete hotkeyInfo;
2456 hotkeyInfo = nullptr;
2457 MMI_HILOGE("SubscribeId invalid:%{public}d", subscribeId);
2458 return INPUT_OCCUPIED_BY_OTHER;
2459 }
2460 MMI_HILOGD("SubscribeId:%{public}d", subscribeId);
2461 hotkeyInfo->subscribeId = subscribeId;
2462 } else {
2463 hotkeyInfo->subscribeId = preSubscribeId;
2464 }
2465 if (AddHotkeySubscribe(hotkeyInfo) != INPUT_SUCCESS) {
2466 delete hotkeyInfo;
2467 hotkeyInfo = nullptr;
2468 MMI_HILOGE("AddHotkeySubscribe fail");
2469 return INPUT_PARAMETER_ERROR;
2470 }
2471 return INPUT_SUCCESS;
2472 }
2473
DelHotkeyMonitor(std::list<Input_HotkeyInfo * > & infos,Input_HotkeyCallback callback,int32_t & subscribeId)2474 int32_t DelHotkeyMonitor(std::list<Input_HotkeyInfo *> &infos,
2475 Input_HotkeyCallback callback, int32_t &subscribeId)
2476 {
2477 CALL_DEBUG_ENTER;
2478 CHKPR(&infos, INPUT_PARAMETER_ERROR);
2479 CHKPR(callback, INPUT_PARAMETER_ERROR);
2480
2481 auto iter = infos.begin();
2482 while (iter != infos.end()) {
2483 if (*iter == nullptr) {
2484 iter = infos.erase(iter);
2485 continue;
2486 }
2487 if (callback == nullptr) {
2488 Input_HotkeyInfo *monitorInfo = *iter;
2489 infos.erase(iter++);
2490 if (infos.empty()) {
2491 subscribeId = monitorInfo->subscribeId;
2492 }
2493 delete monitorInfo;
2494 monitorInfo = nullptr;
2495 MMI_HILOGD("Callback has been deleted, size:%{public}zu", infos.size());
2496 continue;
2497 }
2498 if ((*iter)->callback == callback) {
2499 Input_HotkeyInfo *monitorInfo = *iter;
2500 iter = infos.erase(iter);
2501 if (infos.empty()) {
2502 subscribeId = monitorInfo->subscribeId;
2503 }
2504 delete monitorInfo;
2505 monitorInfo = nullptr;
2506 MMI_HILOGD("Callback has been deleted, size:%{public}zu", infos.size());
2507 return INPUT_SUCCESS;
2508 }
2509 ++iter;
2510 }
2511 return INPUT_SUCCESS;
2512 }
2513
DelEventCallback(Input_HotkeyInfo * hotkeyInfo,int32_t & subscribeId)2514 int32_t DelEventCallback(Input_HotkeyInfo* hotkeyInfo, int32_t &subscribeId)
2515 {
2516 CALL_DEBUG_ENTER;
2517 CHKPR(hotkeyInfo, INPUT_PARAMETER_ERROR);
2518 if (g_callbacks.count(hotkeyInfo->hotkeyId) <= 0) {
2519 MMI_HILOGE("Callback doesn't exists");
2520 return INPUT_PARAMETER_ERROR;
2521 }
2522 auto &info = g_callbacks[hotkeyInfo->hotkeyId];
2523 MMI_HILOGD("HotkeyId is :%{private}s, Input_HotkeyInfo:%{public}zu", hotkeyInfo->hotkeyId.c_str(), info.size());
2524 return DelHotkeyMonitor(info, hotkeyInfo->callback, subscribeId);
2525 }
2526
OH_Input_RemoveHotkeyMonitor(const Input_Hotkey * hotkey,Input_HotkeyCallback callback)2527 Input_Result OH_Input_RemoveHotkeyMonitor(const Input_Hotkey *hotkey, Input_HotkeyCallback callback)
2528 {
2529 CALL_DEBUG_ENTER;
2530 CHKPR(hotkey, INPUT_PARAMETER_ERROR);
2531 CHKPR(callback, INPUT_PARAMETER_ERROR);
2532 std::lock_guard guard(g_CallBacksMutex);
2533
2534 Input_HotkeyInfo *hotkeyInfo = new (std::nothrow) Input_HotkeyInfo();
2535 CHKPR(hotkeyInfo, INPUT_PARAMETER_ERROR);
2536 auto keyOption = std::make_shared<OHOS::MMI::KeyOption>();
2537 if (MakeHotkeyInfo(hotkey, hotkeyInfo, keyOption) != INPUT_SUCCESS) {
2538 delete hotkeyInfo;
2539 hotkeyInfo = nullptr;
2540 MMI_HILOGE("MakeHotkeyInfo failed");
2541 return INPUT_PARAMETER_ERROR;
2542 }
2543 hotkeyInfo->callback = callback;
2544 int32_t subscribeId = -1;
2545 if (DelEventCallback(hotkeyInfo, subscribeId) != INPUT_SUCCESS) {
2546 delete hotkeyInfo;
2547 hotkeyInfo = nullptr;
2548 MMI_HILOGE("DelEventCallback failed");
2549 return INPUT_SERVICE_EXCEPTION;
2550 }
2551 MMI_HILOGD("SubscribeId:%{public}d", subscribeId);
2552 if (subscribeId >= 0) {
2553 OHOS::MMI::InputManager::GetInstance()->UnsubscribeHotkey(subscribeId);
2554 }
2555 delete hotkeyInfo;
2556 hotkeyInfo = nullptr;
2557 return INPUT_SUCCESS;
2558 }
2559
DeviceAddedCallback(int32_t deviceId,const std::string & Type)2560 static void DeviceAddedCallback(int32_t deviceId, const std::string& Type)
2561 {
2562 CALL_DEBUG_ENTER;
2563 std::lock_guard guard(g_DeviceListerCallbackMutex);
2564 for (auto listener : g_ohDeviceListenerList) {
2565 if (listener == nullptr) {
2566 MMI_HILOGE("listener is nullptr");
2567 continue;
2568 }
2569 if (listener->deviceAddedCallback == nullptr) {
2570 MMI_HILOGE("OnDeviceAdded is nullptr");
2571 continue;
2572 }
2573 listener->deviceAddedCallback(deviceId);
2574 }
2575 }
2576
DeviceRemovedCallback(int32_t deviceId,const std::string & Type)2577 static void DeviceRemovedCallback(int32_t deviceId, const std::string& Type)
2578 {
2579 CALL_DEBUG_ENTER;
2580 std::lock_guard guard(g_DeviceListerCallbackMutex);
2581 for (auto listener : g_ohDeviceListenerList) {
2582 if (listener == nullptr) {
2583 MMI_HILOGE("listener is nullptr");
2584 continue;
2585 }
2586 if (listener->deviceRemovedCallback == nullptr) {
2587 MMI_HILOGE("OnDeviceRemoved is nullptr");
2588 continue;
2589 }
2590 listener->deviceRemovedCallback(deviceId);
2591 }
2592 }
2593
OH_Input_RegisterDeviceListener(Input_DeviceListener * listener)2594 Input_Result OH_Input_RegisterDeviceListener(Input_DeviceListener* listener)
2595 {
2596 CALL_DEBUG_ENTER;
2597 if (listener == nullptr || listener->deviceAddedCallback == nullptr ||
2598 listener->deviceRemovedCallback == nullptr) {
2599 MMI_HILOGE("listener or callback is nullptr");
2600 return INPUT_PARAMETER_ERROR;
2601 }
2602 std::lock_guard guard(g_DeviceListerCallbackMutex);
2603 if (g_ohDeviceListenerList.empty()) {
2604 int32_t ret = OHOS::MMI::InputManager::GetInstance()->RegisterDevListener("change", g_deviceListener);
2605 g_deviceListener->SetDeviceAddedCallback(DeviceAddedCallback);
2606 g_deviceListener->SetDeviceRemovedCallback(DeviceRemovedCallback);
2607 if (ret != RET_OK) {
2608 MMI_HILOGE("RegisterDevListener fail");
2609 return INPUT_SERVICE_EXCEPTION;
2610 }
2611 }
2612 g_ohDeviceListenerList.insert(listener);
2613 return INPUT_SUCCESS;
2614 }
2615
OH_Input_UnregisterDeviceListener(Input_DeviceListener * listener)2616 Input_Result OH_Input_UnregisterDeviceListener(Input_DeviceListener* listener)
2617 {
2618 CALL_DEBUG_ENTER;
2619 CHKPR(listener, INPUT_PARAMETER_ERROR);
2620 std::lock_guard guard(g_DeviceListerCallbackMutex);
2621 auto it = g_ohDeviceListenerList.find(listener);
2622 if (it == g_ohDeviceListenerList.end()) {
2623 MMI_HILOGE("listener not found");
2624 return INPUT_PARAMETER_ERROR;
2625 }
2626 g_ohDeviceListenerList.erase(it);
2627 if (g_ohDeviceListenerList.empty()) {
2628 int32_t ret = OHOS::MMI::InputManager::GetInstance()->UnregisterDevListener("change", g_deviceListener);
2629 if (ret != RET_OK) {
2630 MMI_HILOGE("UnregisterDevListener fail");
2631 return INPUT_SERVICE_EXCEPTION;
2632 }
2633 }
2634 return INPUT_SUCCESS;
2635 }
2636
OH_Input_UnregisterDeviceListeners()2637 Input_Result OH_Input_UnregisterDeviceListeners()
2638 {
2639 CALL_DEBUG_ENTER;
2640 std::lock_guard guard(g_DeviceListerCallbackMutex);
2641 if (g_ohDeviceListenerList.empty()) {
2642 return INPUT_SUCCESS;
2643 }
2644 auto ret = OHOS::MMI::InputManager::GetInstance()->UnregisterDevListener("change", g_deviceListener);
2645 g_ohDeviceListenerList.clear();
2646 if (ret != RET_OK) {
2647 MMI_HILOGE("UnregisterDevListener fail");
2648 return INPUT_SERVICE_EXCEPTION;
2649 }
2650 return INPUT_SUCCESS;
2651 }
2652
OH_Input_GetDeviceIds(int32_t * deviceIds,int32_t inSize,int32_t * outSize)2653 Input_Result OH_Input_GetDeviceIds(int32_t *deviceIds, int32_t inSize, int32_t *outSize)
2654 {
2655 CALL_DEBUG_ENTER;
2656 if (inSize < 0) {
2657 MMI_HILOGE("Invalid inSize:%{public}d", inSize);
2658 return INPUT_PARAMETER_ERROR;
2659 }
2660 CHKPR(deviceIds, INPUT_PARAMETER_ERROR);
2661 CHKPR(outSize, INPUT_PARAMETER_ERROR);
2662 auto nativeCallback = [&](std::vector<int32_t> &ids) {
2663 auto deviceIdslength = static_cast<int32_t>(ids.size());
2664 if (inSize > deviceIdslength) {
2665 *outSize = deviceIdslength;
2666 }
2667 if (inSize < deviceIdslength) {
2668 *outSize = inSize;
2669 }
2670 for (int32_t i = 0; i < *outSize; ++i) {
2671 *(deviceIds + i) = ids[i];
2672 }
2673 };
2674 int32_t ret = OHOS::MMI::InputManager::GetInstance()->GetDeviceIds(nativeCallback);
2675 if (ret != RET_OK) {
2676 MMI_HILOGE("GetDeviceIds fail");
2677 return INPUT_PARAMETER_ERROR;
2678 }
2679 return INPUT_SUCCESS;
2680 }
2681
OH_Input_CreateDeviceInfo(void)2682 Input_DeviceInfo* OH_Input_CreateDeviceInfo(void)
2683 {
2684 CALL_DEBUG_ENTER;
2685 Input_DeviceInfo* deviceInfo = new (std::nothrow) Input_DeviceInfo();
2686 CHKPP(deviceInfo);
2687 return deviceInfo;
2688 }
2689
OH_Input_DestroyDeviceInfo(Input_DeviceInfo ** deviceInfo)2690 void OH_Input_DestroyDeviceInfo(Input_DeviceInfo **deviceInfo)
2691 {
2692 CALL_DEBUG_ENTER;
2693 CHKPV(deviceInfo);
2694 CHKPV(*deviceInfo);
2695 delete *deviceInfo;
2696 *deviceInfo = nullptr;
2697 }
2698
OH_Input_GetDevice(int32_t deviceId,Input_DeviceInfo ** deviceInfo)2699 Input_Result OH_Input_GetDevice(int32_t deviceId, Input_DeviceInfo **deviceInfo)
2700 {
2701 CALL_DEBUG_ENTER;
2702 if (deviceId < 0) {
2703 MMI_HILOGE("Invalid deviceId:%{public}d", deviceId);
2704 return INPUT_PARAMETER_ERROR;
2705 }
2706 CHKPR(*deviceInfo, INPUT_PARAMETER_ERROR);
2707 CHKPR(deviceInfo, INPUT_PARAMETER_ERROR);
2708 auto nativeCallback = [deviceInfo](std::shared_ptr<OHOS::MMI::InputDevice> device) {
2709 CHKPV(*deviceInfo);
2710 (*deviceInfo)->id = device->GetId();
2711 if (strcpy_s((*deviceInfo)->name, device->GetName().size() + 1, device->GetName().c_str()) != EOK) {
2712 MMI_HILOGE("strcpy_s error");
2713 return;
2714 }
2715 (*deviceInfo)->product = device->GetProduct();
2716 (*deviceInfo)->vendor = device->GetVendor();
2717 (*deviceInfo)->version = device->GetVersion();
2718 if (strcpy_s((*deviceInfo)->phys, device->GetPhys().size() + 1, device->GetPhys().c_str()) != EOK) {
2719 MMI_HILOGE("strcpy_s error");
2720 return;
2721 }
2722 (*deviceInfo)->ability = device->GetType();
2723 };
2724 int32_t ret = OHOS::MMI::InputManager::GetInstance()->GetDevice(deviceId, nativeCallback);
2725 if (ret != RET_OK) {
2726 MMI_HILOGE("GetDevice fail");
2727 return INPUT_PARAMETER_ERROR;
2728 }
2729 return INPUT_SUCCESS;
2730 }
2731
OH_Input_GetKeyboardType(int32_t deviceId,int32_t * KeyboardType)2732 Input_Result OH_Input_GetKeyboardType(int32_t deviceId, int32_t *KeyboardType)
2733 {
2734 CALL_DEBUG_ENTER;
2735 if (deviceId < 0) {
2736 MMI_HILOGE("Invalid deviceId:%{public}d", deviceId);
2737 return INPUT_PARAMETER_ERROR;
2738 }
2739 CHKPR(KeyboardType, INPUT_PARAMETER_ERROR);
2740 auto nativeCallback = [KeyboardType](int32_t keyboardTypes) {
2741 *KeyboardType = keyboardTypes;
2742 };
2743 int32_t ret = OHOS::MMI::InputManager::GetInstance()->GetKeyboardType(deviceId, nativeCallback);
2744 if (ret != RET_OK) {
2745 MMI_HILOGE("GetKeyboardType fail");
2746 return INPUT_PARAMETER_ERROR;
2747 }
2748 return INPUT_SUCCESS;
2749 }
2750
OH_Input_GetDeviceName(Input_DeviceInfo * deviceInfo,char ** name)2751 Input_Result OH_Input_GetDeviceName(Input_DeviceInfo *deviceInfo, char **name)
2752 {
2753 CALL_DEBUG_ENTER;
2754 CHKPR(deviceInfo, INPUT_PARAMETER_ERROR);
2755 CHKPR(name, INPUT_PARAMETER_ERROR);
2756 *name = deviceInfo->name;
2757 return INPUT_SUCCESS;
2758 }
2759
2760
OH_Input_GetDeviceAddress(Input_DeviceInfo * deviceInfo,char ** address)2761 Input_Result OH_Input_GetDeviceAddress(Input_DeviceInfo *deviceInfo, char **address)
2762 {
2763 CALL_DEBUG_ENTER;
2764 CHKPR(deviceInfo, INPUT_PARAMETER_ERROR);
2765 CHKPR(address, INPUT_PARAMETER_ERROR);
2766 *address = deviceInfo->phys;
2767 return INPUT_SUCCESS;
2768 }
2769
OH_Input_GetDeviceId(Input_DeviceInfo * deviceInfo,int32_t * id)2770 Input_Result OH_Input_GetDeviceId(Input_DeviceInfo *deviceInfo, int32_t *id)
2771 {
2772 CALL_DEBUG_ENTER;
2773 CHKPR(deviceInfo, INPUT_PARAMETER_ERROR);
2774 CHKPR(id, INPUT_PARAMETER_ERROR);
2775 *id = deviceInfo->id;
2776 return INPUT_SUCCESS;
2777 }
2778
OH_Input_GetCapabilities(Input_DeviceInfo * deviceInfo,int32_t * capabilities)2779 Input_Result OH_Input_GetCapabilities(Input_DeviceInfo *deviceInfo, int32_t *capabilities)
2780 {
2781 CALL_DEBUG_ENTER;
2782 CHKPR(deviceInfo, INPUT_PARAMETER_ERROR);
2783 CHKPR(capabilities, INPUT_PARAMETER_ERROR);
2784 *capabilities = deviceInfo->ability;
2785 return INPUT_SUCCESS;
2786 }
2787
OH_Input_GetDeviceVersion(Input_DeviceInfo * deviceInfo,int32_t * version)2788 Input_Result OH_Input_GetDeviceVersion(Input_DeviceInfo *deviceInfo, int32_t *version)
2789 {
2790 CALL_DEBUG_ENTER;
2791 CHKPR(deviceInfo, INPUT_PARAMETER_ERROR);
2792 CHKPR(version, INPUT_PARAMETER_ERROR);
2793 *version = deviceInfo->version;
2794 return INPUT_SUCCESS;
2795 }
2796
OH_Input_GetDeviceProduct(Input_DeviceInfo * deviceInfo,int32_t * product)2797 Input_Result OH_Input_GetDeviceProduct(Input_DeviceInfo *deviceInfo, int32_t *product)
2798 {
2799 CALL_DEBUG_ENTER;
2800 CHKPR(deviceInfo, INPUT_PARAMETER_ERROR);
2801 CHKPR(product, INPUT_PARAMETER_ERROR);
2802 *product = deviceInfo->product;
2803 return INPUT_SUCCESS;
2804 }
2805
OH_Input_GetDeviceVendor(Input_DeviceInfo * deviceInfo,int32_t * vendor)2806 Input_Result OH_Input_GetDeviceVendor(Input_DeviceInfo *deviceInfo, int32_t *vendor)
2807 {
2808 CALL_DEBUG_ENTER;
2809 CHKPR(deviceInfo, INPUT_PARAMETER_ERROR);
2810 CHKPR(vendor, INPUT_PARAMETER_ERROR);
2811 *vendor = deviceInfo->vendor;
2812 return INPUT_SUCCESS;
2813 }
2814
OH_Input_GetFunctionKeyState(int32_t keyCode,int32_t * state)2815 Input_Result OH_Input_GetFunctionKeyState(int32_t keyCode, int32_t *state)
2816 {
2817 CALL_DEBUG_ENTER;
2818 if (keyCode < 0 || keyCode != OHOS::MMI::FunctionKey::FUNCTION_KEY_CAPSLOCK) {
2819 MMI_HILOGE("Invalid code:%{private}d", keyCode);
2820 return INPUT_PARAMETER_ERROR;
2821 }
2822 CHKPR(state, INPUT_PARAMETER_ERROR);
2823 bool resultState = false;
2824 int32_t napiCode = OHOS::MMI::InputManager::GetInstance()->GetFunctionKeyState(keyCode, resultState);
2825 *state = resultState ? 1 : 0;
2826 if (napiCode == INPUT_KEYBOARD_DEVICE_NOT_EXIST) {
2827 MMI_HILOGE("GetFunctionKeyState fail, no keyboard device connected");
2828 return INPUT_KEYBOARD_DEVICE_NOT_EXIST;
2829 }
2830 return INPUT_SUCCESS;
2831 }
2832
OH_Input_QueryMaxTouchPoints(int32_t * count)2833 Input_Result OH_Input_QueryMaxTouchPoints(int32_t *count)
2834 {
2835 CHKPR(count, INPUT_PARAMETER_ERROR);
2836 auto ret = OHOS::MMI::InputManager::GetInstance()->GetMaxMultiTouchPointNum(*count);
2837 if (ret != RET_OK) {
2838 *count = UNKNOWN_MAX_TOUCH_POINTS;
2839 MMI_HILOGE("GetMaxMultiTouchPointNum fail, error:%{public}d", ret);
2840 }
2841 return INPUT_SUCCESS;
2842 }
2843
OH_Input_RequestInjection(Input_InjectAuthorizeCallback callback)2844 Input_Result OH_Input_RequestInjection(Input_InjectAuthorizeCallback callback)
2845 {
2846 CALL_DEBUG_ENTER;
2847 CHKPR(callback, INPUT_PARAMETER_ERROR);
2848 int32_t reqId = 0;
2849 int32_t status = 0;
2850 int32_t ret = OHOS::MMI::InputManager::GetInstance()->RequestInjection(status, reqId);
2851 MMI_HILOGD("RequestInjection %{public}d,%{public}d,%{public}d", ret, status, reqId);
2852 if (ret != RET_OK) {
2853 MMI_HILOGE("RequestInjection fail, error:%{public}d", ret);
2854 if (ret == OHOS::MMI::ERROR_DEVICE_NOT_SUPPORTED) {
2855 return INPUT_DEVICE_NOT_SUPPORTED;
2856 }
2857 if (ret == OHOS::MMI::ERROR_OPERATION_FREQUENT) {
2858 return INPUT_INJECTION_OPERATION_FREQUENT;
2859 }
2860 return INPUT_SERVICE_EXCEPTION;
2861 }
2862 AUTHORIZE_QUERY_STATE recvStatus = static_cast<AUTHORIZE_QUERY_STATE>(status);
2863 switch (recvStatus) {
2864 case AUTHORIZE_QUERY_STATE::OTHER_PID_IN_AUTHORIZATION_SELECTION:
2865 case AUTHORIZE_QUERY_STATE::CURRENT_PID_IN_AUTHORIZATION_SELECTION: {
2866 return INPUT_INJECTION_AUTHORIZING;
2867 }
2868 case AUTHORIZE_QUERY_STATE::OTHER_PID_AUTHORIZED: {
2869 return INPUT_INJECTION_AUTHORIZED_OTHERS;
2870 }
2871 case AUTHORIZE_QUERY_STATE::CURRENT_PID_AUTHORIZED: {
2872 return INPUT_INJECTION_AUTHORIZED;
2873 }
2874 case AUTHORIZE_QUERY_STATE::UNAUTHORIZED: {
2875 MMI_HILOGD("RequestInjection ok %{public}d", reqId);
2876 OHOS::MMI::InputManager::GetInstance()->InsertRequestInjectionCallback(reqId,
2877 [callback, reqId](int32_t status) {
2878 AUTHORIZE_QUERY_STATE callStatus = static_cast<AUTHORIZE_QUERY_STATE>(status);
2879 callback(callStatus == AUTHORIZE_QUERY_STATE::CURRENT_PID_AUTHORIZED ?
2880 Input_InjectionStatus::AUTHORIZED : Input_InjectionStatus::UNAUTHORIZED);
2881 });
2882 return INPUT_SUCCESS;
2883 }
2884 default:
2885 return INPUT_SERVICE_EXCEPTION;
2886 }
2887 }
2888
OH_Input_QueryAuthorizedStatus(Input_InjectionStatus * status)2889 Input_Result OH_Input_QueryAuthorizedStatus(Input_InjectionStatus* status)
2890 {
2891 CALL_DEBUG_ENTER;
2892 CHKPR(status, INPUT_PARAMETER_ERROR);
2893 int32_t tmpStatus = 0;
2894 int32_t ret = OHOS::MMI::InputManager::GetInstance()->QueryAuthorizedStatus(tmpStatus);
2895 MMI_HILOGD("QueryAuthorizedStatus ret:%{public}d,%{public}d", ret, tmpStatus);
2896 if (ret != RET_OK) {
2897 MMI_HILOGE("QueryAuthorizedStatus fail, error:%{public}d", ret);
2898 return INPUT_SERVICE_EXCEPTION;
2899 }
2900 AUTHORIZE_QUERY_STATE recvStatus = static_cast<AUTHORIZE_QUERY_STATE>(tmpStatus);
2901 switch (recvStatus) {
2902 case AUTHORIZE_QUERY_STATE::OTHER_PID_IN_AUTHORIZATION_SELECTION:
2903 case AUTHORIZE_QUERY_STATE::OTHER_PID_AUTHORIZED:
2904 case AUTHORIZE_QUERY_STATE::UNAUTHORIZED: {
2905 *status = Input_InjectionStatus::UNAUTHORIZED;
2906 break;
2907 }
2908 case AUTHORIZE_QUERY_STATE::CURRENT_PID_IN_AUTHORIZATION_SELECTION: {
2909 *status = Input_InjectionStatus::AUTHORIZING;
2910 break;
2911 }
2912 case AUTHORIZE_QUERY_STATE::CURRENT_PID_AUTHORIZED: {
2913 *status = Input_InjectionStatus::AUTHORIZED;
2914 break;
2915 }
2916 default:
2917 MMI_HILOGE("QueryAuthorizedStatus fail, %{public}d", recvStatus);
2918 return INPUT_SERVICE_EXCEPTION;
2919 }
2920 return INPUT_SUCCESS;
2921 }
2922
OH_Input_GetPointerLocation(int32_t * displayId,double * displayX,double * displayY)2923 Input_Result OH_Input_GetPointerLocation(int32_t *displayId, double *displayX, double *displayY)
2924 {
2925 CALL_DEBUG_ENTER;
2926 CHKPR(displayId, INPUT_PARAMETER_ERROR);
2927 CHKPR(displayX, INPUT_PARAMETER_ERROR);
2928 CHKPR(displayY, INPUT_PARAMETER_ERROR);
2929 int32_t tmpDisplayId = 0;
2930 double tmpX = 0.0;
2931 double tmpY = 0.0;
2932 int32_t ret = OHOS::MMI::InputManager::GetInstance()->GetPointerLocation(tmpDisplayId, tmpX, tmpY);
2933 if (ret != RET_OK) {
2934 MMI_HILOGE("Query pointer location failed, error:%{public}d", ret);
2935 if (ret == OHOS::MMI::ERROR_DEVICE_NO_POINTER) {
2936 MMI_HILOGE("The device has no pointer");
2937 return INPUT_DEVICE_NO_POINTER;
2938 }
2939 if (ret == OHOS::MMI::ERROR_APP_NOT_FOCUSED) {
2940 MMI_HILOGE("The app is not the focused app");
2941 return INPUT_APP_NOT_FOCUSED;
2942 }
2943 return INPUT_SERVICE_EXCEPTION;
2944 }
2945 *displayId = tmpDisplayId;
2946 *displayX = tmpX;
2947 *displayY = tmpY;
2948 return INPUT_SUCCESS;
2949 }
2950
TransformTouchActionDown(std::shared_ptr<OHOS::MMI::PointerEvent> pointerEvent,OHOS::MMI::PointerEvent::PointerItem & item,int64_t time)2951 static void TransformTouchActionDown(std::shared_ptr<OHOS::MMI::PointerEvent> pointerEvent,
2952 OHOS::MMI::PointerEvent::PointerItem &item, int64_t time)
2953 {
2954 CALL_INFO_TRACE;
2955 CHKPV(pointerEvent);
2956 auto pointIds = pointerEvent->GetPointerIds();
2957 if (pointIds.empty()) {
2958 pointerEvent->SetActionStartTime(time);
2959 pointerEvent->SetTargetDisplayId(0);
2960 }
2961 pointerEvent->SetActionTime(time);
2962 pointerEvent->SetPointerAction(OHOS::MMI::PointerEvent::POINTER_ACTION_DOWN);
2963 item.SetDownTime(time);
2964 item.SetPressed(true);
2965 }
2966
TransformTouchAction(const struct Input_TouchEvent * touchEvent,std::shared_ptr<OHOS::MMI::PointerEvent> pointerEvent,OHOS::MMI::PointerEvent::PointerItem & item)2967 static int32_t TransformTouchAction(const struct Input_TouchEvent *touchEvent,
2968 std::shared_ptr<OHOS::MMI::PointerEvent> pointerEvent, OHOS::MMI::PointerEvent::PointerItem &item)
2969 {
2970 CALL_INFO_TRACE;
2971 CHKPR(touchEvent, INPUT_PARAMETER_ERROR);
2972 CHKPR(pointerEvent, INPUT_PARAMETER_ERROR);
2973 int64_t time = touchEvent->actionTime;
2974 if (time < 0) {
2975 MMI_HILOGW("Invalid parameter for the actionTime");
2976 time = OHOS::MMI::GetSysClockTime();
2977 }
2978 switch (touchEvent->action) {
2979 case TOUCH_ACTION_CANCEL:{
2980 pointerEvent->SetActionTime(time);
2981 pointerEvent->SetPointerAction(OHOS::MMI::PointerEvent::POINTER_ACTION_CANCEL);
2982 item.SetPressed(false);
2983 break;
2984 }
2985 case TOUCH_ACTION_DOWN: {
2986 TransformTouchActionDown(pointerEvent, item, time);
2987 break;
2988 }
2989 case TOUCH_ACTION_MOVE: {
2990 pointerEvent->SetActionTime(time);
2991 pointerEvent->SetPointerAction(OHOS::MMI::PointerEvent::POINTER_ACTION_MOVE);
2992 break;
2993 }
2994 case TOUCH_ACTION_UP: {
2995 pointerEvent->SetActionTime(time);
2996 pointerEvent->SetPointerAction(OHOS::MMI::PointerEvent::POINTER_ACTION_UP);
2997 item.SetPressed(false);
2998 break;
2999 }
3000 default: {
3001 MMI_HILOGE("action:%{public}d is invalid", touchEvent->action);
3002 return INPUT_PARAMETER_ERROR;
3003 }
3004 }
3005 return INPUT_SUCCESS;
3006 }
3007
TransformTouchProperty(const struct Input_TouchEvent * touchEvent,std::shared_ptr<OHOS::MMI::PointerEvent> pointerEvent,OHOS::MMI::PointerEvent::PointerItem & item,int32_t windowX,int32_t windowY)3008 static int32_t TransformTouchProperty(const struct Input_TouchEvent *touchEvent,
3009 std::shared_ptr<OHOS::MMI::PointerEvent> pointerEvent,
3010 OHOS::MMI::PointerEvent::PointerItem &item, int32_t windowX, int32_t windowY)
3011 {
3012 CALL_INFO_TRACE;
3013 CHKPR(touchEvent, INPUT_PARAMETER_ERROR);
3014 CHKPR(pointerEvent, INPUT_PARAMETER_ERROR);
3015 int32_t screenX = touchEvent->displayX;
3016 int32_t screenY = touchEvent->displayY;
3017 if (screenX < 0 || screenY < 0) {
3018 MMI_HILOGE("touchEvent parameter is less 0, can not process");
3019 return INPUT_PARAMETER_ERROR;
3020 }
3021 item.SetDisplayX(screenX);
3022 item.SetDisplayY(screenY);
3023 item.SetDisplayXPos(screenX);
3024 item.SetDisplayYPos(screenY);
3025
3026 int32_t globalX = touchEvent->globalX;
3027 int32_t globalY = touchEvent->globalY;
3028 item.SetGlobalX(globalX);
3029 item.SetGlobalY(globalY);
3030 item.SetWindowX(windowX);
3031 item.SetWindowY(windowY);
3032 item.SetWindowXPos(windowX);
3033 item.SetWindowYPos(windowY);
3034
3035 int32_t id = touchEvent->id;
3036 if (id < 0) {
3037 MMI_HILOGE("displayId is less 0, can not process");
3038 return INPUT_PARAMETER_ERROR;
3039 }
3040 item.SetOriginPointerId(id);
3041 if (id < SIMULATE_POINTER_EVENT_START_ID) {
3042 item.SetPointerId(id + SIMULATE_POINTER_EVENT_START_ID);
3043 pointerEvent->SetPointerId(id + SIMULATE_POINTER_EVENT_START_ID);
3044 } else {
3045 item.SetPointerId(id);
3046 pointerEvent->SetPointerId(id);
3047 }
3048 item.SetTargetWindowId(touchEvent->windowId);
3049 pointerEvent->SetTargetDisplayId(touchEvent->displayId);
3050 pointerEvent->UpdateId();
3051 pointerEvent->SetSourceType(OHOS::MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
3052 if (touchEvent->action == TOUCH_ACTION_DOWN) {
3053 pointerEvent->AddPointerItem(item);
3054 } else if ((touchEvent->action == TOUCH_ACTION_MOVE) || (touchEvent->action == TOUCH_ACTION_UP) ||
3055 (touchEvent->action == TOUCH_ACTION_CANCEL)) {
3056 pointerEvent->UpdatePointerItem(id, item);
3057 }
3058 return INPUT_SUCCESS;
3059 }
3060
OH_Input_TouchEventToPointerEvent(Input_TouchEvent * touchEvent,int32_t windowX,int32_t windowY)3061 std::shared_ptr<OHOS::MMI::PointerEvent> OH_Input_TouchEventToPointerEvent(Input_TouchEvent *touchEvent,
3062 int32_t windowX, int32_t windowY)
3063 {
3064 CALL_INFO_TRACE;
3065 if (touchEvent == nullptr) {
3066 MMI_HILOGE("touchEvent is null");
3067 return nullptr;
3068 }
3069 if (windowX < 0 || windowY < 0) {
3070 MMI_HILOGE("window coordination is less 0");
3071 return nullptr;
3072 }
3073 std::shared_ptr<OHOS::MMI::PointerEvent> pointerEvent = OHOS::MMI::PointerEvent::Create();
3074 if (pointerEvent == nullptr) {
3075 MMI_HILOGE("pointerEventis null");
3076 return nullptr;
3077 }
3078 OHOS::MMI::PointerEvent::PointerItem item;
3079 int32_t ret = TransformTouchAction(touchEvent, pointerEvent, item);
3080 if (ret != INPUT_SUCCESS) {
3081 return nullptr;
3082 }
3083
3084 ret = TransformTouchProperty(touchEvent, pointerEvent, item, windowX, windowY);
3085 if (ret != INPUT_SUCCESS) {
3086 return nullptr;
3087 }
3088 pointerEvent->AddFlag(OHOS::MMI::InputEvent::EVENT_FLAG_SIMULATE);
3089 return pointerEvent;
3090 }