1/*
2 * Copyright (c) 2023 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 "virtual_keyboard.h"
17
18#include <cmath>
19
20#include <linux/input.h>
21
22#include "input_manager.h"
23
24#include "devicestatus_define.h"
25#include "fi_log.h"
26#include "virtual_keyboard_builder.h"
27
28#undef LOG_TAG
29#define LOG_TAG "VirtualKeyboard"
30
31namespace OHOS {
32namespace Msdp {
33namespace DeviceStatus {
34namespace {
35constexpr int32_t MINIMUM_INTERVAL { 8 };
36} // namespace
37
38std::shared_ptr<VirtualKeyboard> VirtualKeyboard::device_ { nullptr };
39
40std::shared_ptr<VirtualKeyboard>VirtualKeyboard::GetDevice()
41{
42    if (device_ == nullptr) {
43        std::string node;
44        if (VirtualDevice::FindDeviceNode(VirtualKeyboardBuilder::GetDeviceName(), node)) {
45            auto vKeyboard = std::make_shared<VirtualKeyboard>(node);
46            CHKPP(vKeyboard);
47            if (vKeyboard->IsActive()) {
48                device_ = vKeyboard;
49            }
50        }
51    }
52    return device_;
53}
54
55VirtualKeyboard::VirtualKeyboard(const std::string &name)
56    : VirtualDevice(name)
57{
58    VirtualDevice::SetMinimumInterval(MINIMUM_INTERVAL);
59}
60
61int32_t VirtualKeyboard::Down(int32_t key)
62{
63    CALL_DEBUG_ENTER;
64    if (!SupportKey(key)) {
65        FI_HILOGE("Unsupported key code:%{public}d", key);
66        return RET_ERR;
67    }
68
69    SendEvent(EV_MSC, MSC_SCAN, OBFUSCATED);
70    SendEvent(EV_KEY, key, DOWN_VALUE);
71    SendEvent(EV_SYN, SYN_REPORT, SYNC_VALUE);
72    return RET_OK;
73}
74
75int32_t VirtualKeyboard::Up(int32_t key)
76{
77    CALL_DEBUG_ENTER;
78    if (!SupportKey(key)) {
79        FI_HILOGE("Unsupported key code:%{public}d", key);
80        return RET_ERR;
81    }
82
83    SendEvent(EV_MSC, MSC_SCAN, OBFUSCATED);
84    SendEvent(EV_KEY, key, UP_VALUE);
85    SendEvent(EV_SYN, SYN_REPORT, SYNC_VALUE);
86    return RET_OK;
87}
88} // namespace DeviceStatus
89} // namespace Msdp
90} // namespace OHOS