123b3eb3cSopenharmony_ci/*
223b3eb3cSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License.
523b3eb3cSopenharmony_ci * You may obtain a copy of the License at
623b3eb3cSopenharmony_ci *
723b3eb3cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
823b3eb3cSopenharmony_ci *
923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and
1323b3eb3cSopenharmony_ci * limitations under the License.
1423b3eb3cSopenharmony_ci */
1523b3eb3cSopenharmony_ci
1623b3eb3cSopenharmony_ci#include "base/input_manager/input_manager.h"
1723b3eb3cSopenharmony_ci
1823b3eb3cSopenharmony_ci#include "input_manager.h"
1923b3eb3cSopenharmony_ci
2023b3eb3cSopenharmony_ci#include "base/utils/utils.h"
2123b3eb3cSopenharmony_ci
2223b3eb3cSopenharmony_cinamespace OHOS::Ace {
2323b3eb3cSopenharmony_ci
2423b3eb3cSopenharmony_cibool InputManager::GetDeviceIds(std::vector<int32_t>& resDeviceIds)
2523b3eb3cSopenharmony_ci{
2623b3eb3cSopenharmony_ci    auto* inputManager = MMI::InputManager::GetInstance();
2723b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(inputManager, false);
2823b3eb3cSopenharmony_ci    inputManager->GetDeviceIds([&resDeviceIds](std::vector<int32_t>& deviceIds) { resDeviceIds = deviceIds; });
2923b3eb3cSopenharmony_ci    return true;
3023b3eb3cSopenharmony_ci}
3123b3eb3cSopenharmony_ci
3223b3eb3cSopenharmony_ciKeyboardType ConvertKeyboardType(int32_t type)
3323b3eb3cSopenharmony_ci{
3423b3eb3cSopenharmony_ci    switch (type) {
3523b3eb3cSopenharmony_ci        case MMI::KeyboardType::KEYBOARD_TYPE_NONE:
3623b3eb3cSopenharmony_ci            return KeyboardType::NONE;
3723b3eb3cSopenharmony_ci        case MMI::KeyboardType::KEYBOARD_TYPE_UNKNOWN:
3823b3eb3cSopenharmony_ci            return KeyboardType::UNKNOWN;
3923b3eb3cSopenharmony_ci        case MMI::KeyboardType::KEYBOARD_TYPE_ALPHABETICKEYBOARD:
4023b3eb3cSopenharmony_ci            return KeyboardType::ALPHABETICKEYBOARD;
4123b3eb3cSopenharmony_ci        case MMI::KeyboardType::KEYBOARD_TYPE_DIGITALKEYBOARD:
4223b3eb3cSopenharmony_ci            return KeyboardType::DIGITALKEYBOARD;
4323b3eb3cSopenharmony_ci        case MMI::KeyboardType::KEYBOARD_TYPE_HANDWRITINGPEN:
4423b3eb3cSopenharmony_ci            return KeyboardType::HANDWRITINGPEN;
4523b3eb3cSopenharmony_ci        case MMI::KeyboardType::KEYBOARD_TYPE_REMOTECONTROL:
4623b3eb3cSopenharmony_ci            return KeyboardType::REMOTECONTROL;
4723b3eb3cSopenharmony_ci        case MMI::KeyboardType::KEYBOARD_TYPE_MAX:
4823b3eb3cSopenharmony_ci            return KeyboardType::MAX;
4923b3eb3cSopenharmony_ci        default:
5023b3eb3cSopenharmony_ci            return KeyboardType::NONE;
5123b3eb3cSopenharmony_ci    }
5223b3eb3cSopenharmony_ci}
5323b3eb3cSopenharmony_ci
5423b3eb3cSopenharmony_ciKeyboardType InputManager::GetKeyboardType(int32_t deviceId)
5523b3eb3cSopenharmony_ci{
5623b3eb3cSopenharmony_ci    auto* inputManager = MMI::InputManager::GetInstance();
5723b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(inputManager, KeyboardType::UNKNOWN);
5823b3eb3cSopenharmony_ci    int32_t mmiKeyboardType = 0;
5923b3eb3cSopenharmony_ci    inputManager->GetKeyboardType(
6023b3eb3cSopenharmony_ci        deviceId, [&mmiKeyboardType](int32_t keyboardType) { mmiKeyboardType = keyboardType; });
6123b3eb3cSopenharmony_ci    return ConvertKeyboardType(mmiKeyboardType);
6223b3eb3cSopenharmony_ci}
6323b3eb3cSopenharmony_ci
6423b3eb3cSopenharmony_cibool InputManager::GetSystemHotkeys(std::vector<HotKey>& hotkeys)
6523b3eb3cSopenharmony_ci{
6623b3eb3cSopenharmony_ci    auto* inputManager = MMI::InputManager::GetInstance();
6723b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(inputManager, false);
6823b3eb3cSopenharmony_ci
6923b3eb3cSopenharmony_ci    std::vector<std::unique_ptr<OHOS::MMI::KeyOption>> keyOptions;
7023b3eb3cSopenharmony_ci    int32_t count = 0;
7123b3eb3cSopenharmony_ci    inputManager->GetAllSystemHotkeys(keyOptions, count);
7223b3eb3cSopenharmony_ci
7323b3eb3cSopenharmony_ci    if (keyOptions.empty()) {
7423b3eb3cSopenharmony_ci        return false;
7523b3eb3cSopenharmony_ci    }
7623b3eb3cSopenharmony_ci    for (const auto& key : keyOptions) {
7723b3eb3cSopenharmony_ci        hotkeys.push_back({ key->GetPreKeys(), key->GetFinalKey() });
7823b3eb3cSopenharmony_ci    }
7923b3eb3cSopenharmony_ci    return true;
8023b3eb3cSopenharmony_ci}
8123b3eb3cSopenharmony_ci
8223b3eb3cSopenharmony_cibool InputManager::IsKeyboardConnected()
8323b3eb3cSopenharmony_ci{
8423b3eb3cSopenharmony_ci    std::vector<int32_t> deviceIds;
8523b3eb3cSopenharmony_ci    auto resGetIds = GetDeviceIds(deviceIds);
8623b3eb3cSopenharmony_ci    if (!resGetIds) {
8723b3eb3cSopenharmony_ci        return false;
8823b3eb3cSopenharmony_ci    }
8923b3eb3cSopenharmony_ci    return std::any_of(deviceIds.begin(), deviceIds.end(),
9023b3eb3cSopenharmony_ci        [](int32_t id) { return GetKeyboardType(id) == KeyboardType::ALPHABETICKEYBOARD; });
9123b3eb3cSopenharmony_ci}
9223b3eb3cSopenharmony_ci
9323b3eb3cSopenharmony_ci} // namespace OHOS::Ace
94