• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 namespace OHOS {
29 namespace Msdp {
30 namespace DeviceStatus {
31 namespace {
32 constexpr ::OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "VirtualKeyboard" };
33 constexpr int32_t MINIMUM_INTERVAL { 8 };
34 } // namespace
35 
36 VirtualKeyboard *VirtualKeyboard::device_ { nullptr };
37 
GetDevice()38 VirtualKeyboard *VirtualKeyboard::GetDevice()
39 {
40     if (device_ == nullptr) {
41         std::string node;
42         if (VirtualDevice::FindDeviceNode(VirtualKeyboardBuilder::GetDeviceName(), node)) {
43             auto vKeyboard = new (std::nothrow) VirtualKeyboard(node);
44             CHKPP(vKeyboard);
45             if (vKeyboard->IsActive()) {
46                 device_ = vKeyboard;
47             }
48         }
49     }
50     return device_;
51 }
52 
VirtualKeyboard(const std::string & name)53 VirtualKeyboard::VirtualKeyboard(const std::string &name)
54     : VirtualDevice(name)
55 {
56     VirtualDevice::SetMinimumInterval(MINIMUM_INTERVAL);
57 }
58 
Down(int32_t key)59 int32_t VirtualKeyboard::Down(int32_t key)
60 {
61     CALL_DEBUG_ENTER;
62     if (!SupportKey(key)) {
63         FI_HILOGE("Unsupported key code:%{public}d", key);
64         return RET_ERR;
65     }
66 
67     SendEvent(EV_MSC, MSC_SCAN, OBFUSCATED);
68     SendEvent(EV_KEY, key, DOWN_VALUE);
69     SendEvent(EV_SYN, SYN_REPORT, SYNC_VALUE);
70     return RET_OK;
71 }
72 
Up(int32_t key)73 int32_t VirtualKeyboard::Up(int32_t key)
74 {
75     CALL_DEBUG_ENTER;
76     if (!SupportKey(key)) {
77         FI_HILOGE("Unsupported key code:%{public}d", key);
78         return RET_ERR;
79     }
80 
81     SendEvent(EV_MSC, MSC_SCAN, OBFUSCATED);
82     SendEvent(EV_KEY, key, UP_VALUE);
83     SendEvent(EV_SYN, SYN_REPORT, SYNC_VALUE);
84     return RET_OK;
85 }
86 } // namespace DeviceStatus
87 } // namespace Msdp
88 } // namespace OHOS