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 
ConvertKeyboardType(int32_t type)32 KeyboardType ConvertKeyboardType(int32_t type)
33 {
34     switch (type) {
35         case MMI::KeyboardType::KEYBOARD_TYPE_NONE:
36             return KeyboardType::NONE;
37         case MMI::KeyboardType::KEYBOARD_TYPE_UNKNOWN:
38             return KeyboardType::UNKNOWN;
39         case MMI::KeyboardType::KEYBOARD_TYPE_ALPHABETICKEYBOARD:
40             return KeyboardType::ALPHABETICKEYBOARD;
41         case MMI::KeyboardType::KEYBOARD_TYPE_DIGITALKEYBOARD:
42             return KeyboardType::DIGITALKEYBOARD;
43         case MMI::KeyboardType::KEYBOARD_TYPE_HANDWRITINGPEN:
44             return KeyboardType::HANDWRITINGPEN;
45         case MMI::KeyboardType::KEYBOARD_TYPE_REMOTECONTROL:
46             return KeyboardType::REMOTECONTROL;
47         case MMI::KeyboardType::KEYBOARD_TYPE_MAX:
48             return KeyboardType::MAX;
49         default:
50             return KeyboardType::NONE;
51     }
52 }
53 
GetKeyboardType(int32_t deviceId)54 KeyboardType InputManager::GetKeyboardType(int32_t deviceId)
55 {
56     auto* inputManager = MMI::InputManager::GetInstance();
57     CHECK_NULL_RETURN(inputManager, KeyboardType::UNKNOWN);
58     int32_t mmiKeyboardType = 0;
59     inputManager->GetKeyboardType(
60         deviceId, [&mmiKeyboardType](int32_t keyboardType) { mmiKeyboardType = keyboardType; });
61     return ConvertKeyboardType(mmiKeyboardType);
62 }
63 
GetSystemHotkeys(std::vector<HotKey>& hotkeys)64 bool InputManager::GetSystemHotkeys(std::vector<HotKey>& hotkeys)
65 {
66     auto* inputManager = MMI::InputManager::GetInstance();
67     CHECK_NULL_RETURN(inputManager, false);
68 
69     std::vector<std::unique_ptr<OHOS::MMI::KeyOption>> keyOptions;
70     int32_t count = 0;
71     inputManager->GetAllSystemHotkeys(keyOptions, count);
72 
73     if (keyOptions.empty()) {
74         return false;
75     }
76     for (const auto& key : keyOptions) {
77         hotkeys.push_back({ key->GetPreKeys(), key->GetFinalKey() });
78     }
79     return true;
80 }
81 
IsKeyboardConnected()82 bool InputManager::IsKeyboardConnected()
83 {
84     std::vector<int32_t> deviceIds;
85     auto resGetIds = GetDeviceIds(deviceIds);
86     if (!resGetIds) {
87         return false;
88     }
89     return std::any_of(deviceIds.begin(), deviceIds.end(),
90         [](int32_t id) { return GetKeyboardType(id) == KeyboardType::ALPHABETICKEYBOARD; });
91 }
92 
93 } // namespace OHOS::Ace
94