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 "base/input_manager/input_manager.h"
17
18 #include "input_manager.h"
19
20 #include "base/utils/utils.h"
21
22 namespace OHOS::Ace {
23
GetDeviceIds(std::vector<int32_t> & resDeviceIds)24 bool InputManager::GetDeviceIds(std::vector<int32_t>& resDeviceIds)
25 {
26 auto* inputManager = MMI::InputManager::GetInstance();
27 CHECK_NULL_RETURN(inputManager, false);
28 inputManager->GetDeviceIds([&resDeviceIds](std::vector<int32_t>& deviceIds) { resDeviceIds = deviceIds; });
29 return true;
30 }
31
CreatePointerEvent(const std::shared_ptr<const MMI::PointerEvent> & pointerEvent)32 std::shared_ptr<MMI::PointerEvent> InputManager::CreatePointerEvent(
33 const std::shared_ptr<const MMI::PointerEvent>& pointerEvent)
34 {
35 if (!pointerEvent) {
36 return nullptr;
37 }
38 return std::make_shared<MMI::PointerEvent>(*pointerEvent);
39 }
40
ConvertKeyboardType(int32_t type)41 KeyboardType ConvertKeyboardType(int32_t type)
42 {
43 switch (type) {
44 case MMI::KeyboardType::KEYBOARD_TYPE_NONE:
45 return KeyboardType::NONE;
46 case MMI::KeyboardType::KEYBOARD_TYPE_UNKNOWN:
47 return KeyboardType::UNKNOWN;
48 case MMI::KeyboardType::KEYBOARD_TYPE_ALPHABETICKEYBOARD:
49 return KeyboardType::ALPHABETICKEYBOARD;
50 case MMI::KeyboardType::KEYBOARD_TYPE_DIGITALKEYBOARD:
51 return KeyboardType::DIGITALKEYBOARD;
52 case MMI::KeyboardType::KEYBOARD_TYPE_HANDWRITINGPEN:
53 return KeyboardType::HANDWRITINGPEN;
54 case MMI::KeyboardType::KEYBOARD_TYPE_REMOTECONTROL:
55 return KeyboardType::REMOTECONTROL;
56 case MMI::KeyboardType::KEYBOARD_TYPE_MAX:
57 return KeyboardType::MAX;
58 default:
59 return KeyboardType::NONE;
60 }
61 }
62
GetKeyboardType(int32_t deviceId)63 KeyboardType InputManager::GetKeyboardType(int32_t deviceId)
64 {
65 auto* inputManager = MMI::InputManager::GetInstance();
66 CHECK_NULL_RETURN(inputManager, KeyboardType::UNKNOWN);
67 int32_t mmiKeyboardType = 0;
68 inputManager->GetKeyboardType(
69 deviceId, [&mmiKeyboardType](int32_t keyboardType) { mmiKeyboardType = keyboardType; });
70 return ConvertKeyboardType(mmiKeyboardType);
71 }
72
GetSystemHotkeys(std::vector<HotKey> & hotkeys)73 bool InputManager::GetSystemHotkeys(std::vector<HotKey>& hotkeys)
74 {
75 auto* inputManager = MMI::InputManager::GetInstance();
76 CHECK_NULL_RETURN(inputManager, false);
77
78 std::vector<std::unique_ptr<OHOS::MMI::KeyOption>> keyOptions;
79 int32_t count = 0;
80 inputManager->GetAllSystemHotkeys(keyOptions, count);
81
82 if (keyOptions.empty()) {
83 return false;
84 }
85 for (const auto& key : keyOptions) {
86 hotkeys.push_back({ key->GetPreKeys(), key->GetFinalKey() });
87 }
88 return true;
89 }
90
IsKeyboardConnected()91 bool InputManager::IsKeyboardConnected()
92 {
93 std::vector<int32_t> deviceIds;
94 auto resGetIds = GetDeviceIds(deviceIds);
95 if (!resGetIds) {
96 return false;
97 }
98 return std::any_of(deviceIds.begin(), deviceIds.end(),
99 [](int32_t id) { return GetKeyboardType(id) == KeyboardType::ALPHABETICKEYBOARD; });
100 }
101
102 } // namespace OHOS::Ace
103