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