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 "inject_thread.h"
17 
18 #include <sys/prctl.h>
19 
20 namespace OHOS {
21 namespace MMI {
22 std::mutex InjectThread::mutex_;
23 std::condition_variable InjectThread::conditionVariable_;
24 std::vector<InjectInputEvent> InjectThread::injectQueue_;
25 
InjectFunc() const26 void InjectThread::InjectFunc() const
27 {
28     prctl(PR_SET_NAME, "mmi-inject");
29     std::unique_lock<std::mutex> uniqueLock(mutex_);
30     while (true) {
31         conditionVariable_.wait(uniqueLock);
32         while (injectQueue_.size() > 0) {
33             if (injectQueue_[0].deviceId == TOUCH_SCREEN_DEVICE_ID) {
34                 g_pTouchScreen->EmitEvent(injectQueue_[0].type, injectQueue_[0].code, injectQueue_[0].value);
35             } else if (injectQueue_[0].deviceId == KEYBOARD_DEVICE_ID) {
36                 g_pKeyboard->EmitEvent(injectQueue_[0].type, injectQueue_[0].code, injectQueue_[0].value);
37             }
38             injectQueue_.erase(injectQueue_.begin());
39         }
40     }
41 }
42 
WaitFunc(InjectInputEvent injectInputEvent) const43 void InjectThread::WaitFunc(InjectInputEvent injectInputEvent) const
44 {
45     std::lock_guard<std::mutex> lockGuard(mutex_);
46     injectQueue_.push_back(injectInputEvent);
47     conditionVariable_.notify_one();
48 }
49 } // namespace MMI
50 } // namespace OHOS