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_manager.h"
17 
18 #include <dlfcn.h>
19 #include <unistd.h>
20 
21 #include "hdf_device_event_dispatch.h"
22 #include "mmi_log.h"
23 
24 #undef MMI_LOG_TAG
25 #define MMI_LOG_TAG "HdfDeviceEventManager"
26 
27 using namespace OHOS::HiviewDFX;
28 namespace OHOS {
29 namespace MMI {
30 namespace {
31 constexpr int32_t INPUT_PARAM_FIRST { 0 };
32 constexpr int32_t INPUT_PARAM_SECOND { 1 };
33 } // namespace
ConnectHDFInit()34 void HdfDeviceEventManager::ConnectHDFInit()
35 {
36     std::string name = "mmi-hdf";
37     inputInterface_ = IInputInterfaces::Get(true);
38     if (inputInterface_ == nullptr) {
39         MMI_HILOGE("The inputInterface_ is nullptr");
40         return;
41     }
42     thread_ = std::thread([this] { injectThread_.InjectFunc(); });
43     pthread_setname_np(thread_.native_handle(), name.c_str());
44     int32_t ret = inputInterface_->OpenInputDevice(TOUCH_DEV_ID);
45     if (ret == HDF_SUCCESS) {
46         ret = inputInterface_->GetInputDevice(TOUCH_DEV_ID, iDevInfo_);
47         if (ret != HDF_SUCCESS) {
48             MMI_HILOGE("Get input device failed");
49             return;
50         }
51         callback_ = new (std::nothrow) HdfDeviceEventDispatch(iDevInfo_.attrSet.axisInfo[ABS_MT_POSITION_X].max,
52             iDevInfo_.attrSet.axisInfo[ABS_MT_POSITION_Y].max);
53         if (callback_ == nullptr) {
54             MMI_HILOGE("The callback_ is nullptr");
55             return;
56         }
57         ret = inputInterface_->RegisterReportCallback(TOUCH_DEV_ID, callback_);
58         MMI_HILOGD("RegisterReportCallback ret:%{public}d", ret);
59     } else {
60         MMI_HILOGE("Open input device failed");
61     }
62 }
63 } // namespace MMI
64 } // namespace OHOS
65 
main()66 int32_t main() __attribute__((no_sanitize("cfi")))
67 {
68     int sleepSeconds = 1;
69     sleep(sleepSeconds);
70     OHOS::MMI::HdfDeviceEventManager iHdfDeviceEventManager;
71     iHdfDeviceEventManager.ConnectHDFInit();
72 
73     int32_t pid = getpid();
74     MMI_HILOGI("Current pid:%{public}d", pid);
75     void *notifyProcessStatus = nullptr;
76     int32_t(* notifyProcessStatusFunc)(int32_t, int32_t, int32_t) = nullptr;
77     void *libMemMgrClientHandle = dlopen("libmemmgrclient.z.so", RTLD_NOW);
78     if (!libMemMgrClientHandle) {
79         MMI_HILOGE("%{public}s, dlopen libmemmgrclient failed.", __func__);
80         goto nextStep;
81     }
82 
83     notifyProcessStatus = (dlsym(libMemMgrClientHandle, "notify_process_status"));
84     if (!notifyProcessStatus) {
85         MMI_HILOGE("%{public}s, dlsym notify_process_status failed.", __func__);
86         dlclose(libMemMgrClientHandle);
87         goto nextStep;
88     }
89     notifyProcessStatusFunc = reinterpret_cast<int32_t(*)(int32_t, int32_t, int32_t)>(notifyProcessStatus);
90     if (notifyProcessStatusFunc(pid, OHOS::MMI::INPUT_PARAM_FIRST, OHOS::MMI::INPUT_PARAM_SECOND) != 0) {
91         MMI_HILOGE("%{public}s, get device memory failed.", __func__);
92     }
93     dlclose(libMemMgrClientHandle);
94     MMI_HILOGI("notify_process_status execute end");
95 
96 nextStep:
97     MMI_HILOGI("start thread loop");
98     static std::int32_t usleepTime = 1500000;
99     while (true) {
100         usleep(usleepTime);
101     }
102     return 0;
103 }
104