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 "hdf_device_event_dispatch.h"
17 
18 #include <memory>
19 
20 #include "mmi_log.h"
21 #include "virtual_touch_screen.h"
22 
23 #undef MMI_LOG_TAG
24 #define MMI_LOG_TAG "HdfDeviceEventDispatch"
25 
26 using namespace OHOS::HiviewDFX;
27 namespace OHOS {
28 namespace MMI {
29 std::unique_ptr<VirtualTouchScreen> g_pTouchScreen = nullptr;
30 InjectThread HdfDeviceEventDispatch::injectThread_;
31 
HdfDeviceEventDispatch(const uint32_t maxX,const uint32_t maxY)32 HdfDeviceEventDispatch::HdfDeviceEventDispatch(const uint32_t maxX, const uint32_t maxY)
33 {
34     g_pTouchScreen = std::make_unique<VirtualTouchScreen>(maxX, maxY);
35     g_pTouchScreen->SetUp();
36 }
37 
EventPkgCallback(const std::vector<EventPackage> & pkgs,uint32_t devIndex)38 int32_t HdfDeviceEventDispatch::EventPkgCallback(const std::vector<EventPackage> &pkgs, uint32_t devIndex)
39 {
40     if (pkgs.empty()) {
41         MMI_HILOGE("The pkgs is empty");
42         return HDF_FAILURE;
43     }
44 
45     for (const auto &item : pkgs) {
46         if ((item.type == 0) && (item.code == 0) && (item.value == 0)) {
47             InjectInputEvent injectInputSync = { injectThread_.TOUCH_SCREEN_DEVICE_ID, 0, SYN_MT_REPORT, 0 };
48             injectThread_.WaitFunc(injectInputSync);
49         }
50         InjectInputEvent injectInputEvent = {
51             injectThread_.TOUCH_SCREEN_DEVICE_ID,
52             item.type,
53             item.code,
54             item.value
55         };
56         injectThread_.WaitFunc(injectInputEvent);
57     }
58     return HDF_SUCCESS;
59 }
60 
HotPlugCallback(const HotPlugEvent & event)61 int32_t HdfDeviceEventDispatch::HotPlugCallback(const HotPlugEvent &event)
62 {
63     return HDF_SUCCESS;
64 }
65 } // namespace MMI
66 } // namespace OHOS