180922886Sopenharmony_ci/*
280922886Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
380922886Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
480922886Sopenharmony_ci * you may not use this file except in compliance with the License.
580922886Sopenharmony_ci * You may obtain a copy of the License at
680922886Sopenharmony_ci *
780922886Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
880922886Sopenharmony_ci *
980922886Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1080922886Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1180922886Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1280922886Sopenharmony_ci * See the License for the specific language governing permissions and
1380922886Sopenharmony_ci * limitations under the License.
1480922886Sopenharmony_ci */
1580922886Sopenharmony_ci
1680922886Sopenharmony_ci#include "key_event_adapter.h"
1780922886Sopenharmony_ci#include "input_manager.h"
1880922886Sopenharmony_ci#include "key_option.h"
1980922886Sopenharmony_ci#include "avsession_log.h"
2080922886Sopenharmony_ci
2180922886Sopenharmony_cinamespace OHOS::AVSession {
2280922886Sopenharmony_ciKeyEventAdapter& KeyEventAdapter::GetInstance()
2380922886Sopenharmony_ci{
2480922886Sopenharmony_ci    static KeyEventAdapter keyEventAdapter;
2580922886Sopenharmony_ci    return keyEventAdapter;
2680922886Sopenharmony_ci}
2780922886Sopenharmony_ci
2880922886Sopenharmony_civoid KeyEventAdapter::SubscribeKeyEvent(const std::vector<int32_t>& keyCodes,
2980922886Sopenharmony_ci                                        const std::function<void(std::shared_ptr<MMI::KeyEvent>)>& callback)
3080922886Sopenharmony_ci{
3180922886Sopenharmony_ci    SLOGI("enter");
3280922886Sopenharmony_ci    auto* inputManager = MMI::InputManager::GetInstance();
3380922886Sopenharmony_ci    CHECK_AND_RETURN_LOG(inputManager != nullptr, "failed to get input manager");
3480922886Sopenharmony_ci
3580922886Sopenharmony_ci    for (auto keyCode : keyCodes) {
3680922886Sopenharmony_ci        auto keyOption = std::make_shared<MMI::KeyOption>();
3780922886Sopenharmony_ci        CHECK_AND_RETURN_LOG(keyOption != nullptr, "no memory");
3880922886Sopenharmony_ci        keyOption->SetFinalKey(keyCode);
3980922886Sopenharmony_ci        keyOption->SetFinalKeyDown(true);
4080922886Sopenharmony_ci        keyOption->SetFinalKeyDownDuration(0);
4180922886Sopenharmony_ci        if (inputManager->SubscribeKeyEvent(keyOption, callback) < 0) {
4280922886Sopenharmony_ci            SLOGE("keyCode=%{public}d failed", keyCode);
4380922886Sopenharmony_ci        }
4480922886Sopenharmony_ci    }
4580922886Sopenharmony_ci}
4680922886Sopenharmony_ci}