• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "keyboard_inject.h"
17 
18 #include "linux/input-event-codes.h"
19 #include "mmi_log.h"
20 
21 using namespace OHOS::HiviewDFX;
22 namespace OHOS {
23 namespace MMI {
24 namespace {
25 std::shared_ptr<KeyboardInject> g_instance = nullptr;
26 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "keyBoardInject" };
27 constexpr int32_t INPUT_KEY_BACK = 2;
28 constexpr int32_t LINUX_KEY_BACK = 158;
29 } // namespace
30 std::mutex KeyboardInject::mutex_;
31 std::unique_ptr<VirtualKeyboard> g_pKeyboard = nullptr;
32 std::unique_ptr<InjectThread> KeyboardInject::injectThread_ = nullptr;
33 
KeyboardInject()34 KeyboardInject::KeyboardInject()
35 {
36     std::lock_guard<std::mutex> keyboardLock(mutex_);
37     auto it = keyCodeMap_.find(INPUT_KEY_BACK);
38     if (it == keyCodeMap_.end()) {
39         auto ret = keyCodeMap_.insert(std::make_pair(INPUT_KEY_BACK, LINUX_KEY_BACK));
40         MMI_HILOGD("ret.second:%{public}d", ret.second);
41     }
42     injectThread_ = std::make_unique<InjectThread>();
43     g_pKeyboard = std::make_unique<VirtualKeyboard>();
44     g_pKeyboard->SetUp();
45 }
46 
~KeyboardInject()47 KeyboardInject::~KeyboardInject() {}
48 
InjectKeyEvent(uint16_t code,uint32_t value) const49 void KeyboardInject::InjectKeyEvent(uint16_t code, uint32_t value) const
50 {
51     std::lock_guard<std::mutex> keyboardLock(mutex_);
52     auto it = keyCodeMap_.find(code);
53     if (it == keyCodeMap_.end()) {
54         return;
55     }
56     InjectInputEvent injectInputEvent = { injectThread_->KEYBOARD_DEVICE_ID, EV_KEY, it->second, value };
57     injectThread_->WaitFunc(injectInputEvent);
58     InjectInputEvent injectInputSync = { injectThread_->KEYBOARD_DEVICE_ID, EV_SYN, SYN_REPORT, 0 };
59     injectThread_->WaitFunc(injectInputSync);
60 }
61 } // namespace MMI
62 } // namespace OHOS
63