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