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 "libinput_adapter.h"
17 
18 #include <cinttypes>
19 #include <climits>
20 
21 #include <dirent.h>
22 #include <fcntl.h>
23 #include <sys/epoll.h>
24 #include <unistd.h>
25 
26 #include "define_multimodal.h"
27 #include "i_input_windows_manager.h"
28 #include "util.h"
29 
30 #undef MMI_LOG_DOMAIN
31 #define MMI_LOG_DOMAIN MMI_LOG_SERVER
32 #undef MMI_LOG_TAG
33 #define MMI_LOG_TAG "LibinputAdapter"
34 
35 namespace OHOS {
36 namespace MMI {
37 namespace {
38 constexpr int32_t WAIT_TIME_FOR_INPUT { 10 };
39 constexpr int32_t MAX_RETRY_COUNT { 5 };
40 
HiLogFunc(struct libinput * input,libinput_log_priority priority,const char * fmt,va_list args)41 void HiLogFunc(struct libinput* input, libinput_log_priority priority, const char* fmt, va_list args)
42 {
43     CHKPV(input);
44     CHKPV(fmt);
45     char buffer[256] = {};
46     if (vsnprintf_s(buffer, sizeof(buffer), sizeof(buffer) - 1, fmt, args) == -1) {
47         MMI_HILOGE("Call vsnprintf_s failed");
48         va_end(args);
49         return;
50     }
51     if (strstr(buffer, "LOG_LEVEL_I") != NULL) {
52         MMI_HILOGI("PrintLog_Info:%{public}s", buffer);
53     } else if (strstr(buffer, "LOG_LEVEL_D") != NULL) {
54         MMI_HILOGD("PrintLog_Info:%{public}s", buffer);
55     } else if (strstr(buffer, "LOG_LEVEL_E") != NULL) {
56         MMI_HILOGE("PrintLog_Info:%{public}s", buffer);
57     } else {
58         MMI_HILOGD("PrintLog_Info:%{public}s", buffer);
59     }
60     va_end(args);
61 }
62 } // namespace
63 
DeviceLedUpdate(struct libinput_device * device,int32_t funcKey,bool enable)64 int32_t LibinputAdapter::DeviceLedUpdate(struct libinput_device *device, int32_t funcKey, bool enable)
65 {
66     CHKPR(device, RET_ERR);
67     return libinput_set_led_state(device, funcKey, enable);
68 }
69 
70 constexpr static libinput_interface LIBINPUT_INTERFACE = {
__anon93a813a20202()71     .open_restricted = [](const char *path, int32_t flags, void *user_data)->int32_t {
72         if (path == nullptr) {
73             MMI_HILOGWK("Input device path is nullptr");
74             return RET_ERR;
75         }
76         char realPath[PATH_MAX] = {};
77         if (realpath(path, realPath) == nullptr) {
78             std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME_FOR_INPUT));
79             MMI_HILOGWK("The error path is %{public}s", path);
80             return RET_ERR;
81         }
82         int32_t fd = 0;
83         for (int32_t i = 0; i < MAX_RETRY_COUNT; i++) {
84             fd = open(realPath, flags);
85             if (fd >= 0) {
86                 break;
87             }
88             std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME_FOR_INPUT));
89         }
90         int32_t errNo = errno;
91         MMI_HILOGWK("Libinput .open_restricted path:%{private}s,fd:%{public}d,errno:%{public}d", path, fd, errNo);
92         return fd < 0 ? RET_ERR : fd;
93     },
94     .close_restricted = [](int32_t fd, void *user_data)
__anon93a813a20302() 95     {
96         if (fd < 0) {
97             return;
98         }
99         MMI_HILOGI("Libinput .close_restricted fd:%{public}d", fd);
100         close(fd);
101     },
102 };
103 
Init(FunInputEvent funInputEvent)104 bool LibinputAdapter::Init(FunInputEvent funInputEvent)
105 {
106     CALL_DEBUG_ENTER;
107     CHKPF(funInputEvent);
108     funInputEvent_ = funInputEvent;
109     input_ = libinput_path_create_context(&LIBINPUT_INTERFACE, nullptr);
110     CHKPF(input_);
111     libinput_log_set_handler(input_, &HiLogFunc);
112     fd_ = libinput_get_fd(input_);
113     if (fd_ < 0) {
114         libinput_unref(input_);
115         fd_ = -1;
116         MMI_HILOGE("The fd_ is less than 0");
117         return false;
118     }
119     return hotplugDetector_.Init([this](std::string path) { OnDeviceAdded(std::move(path)); },
120         [this](std::string path) { OnDeviceRemoved(std::move(path)); });
121 }
122 
EventDispatch(int32_t fd)123 void LibinputAdapter::EventDispatch(int32_t fd)
124 {
125     CALL_DEBUG_ENTER;
126     if (fd == fd_) {
127         MMI_HILOGD("Start to libinput_dispatch");
128         if (libinput_dispatch(input_) != 0) {
129             MMI_HILOGE("Failed to dispatch libinput");
130             return;
131         }
132         OnEventHandler();
133         MMI_HILOGD("End to OnEventHandler");
134     } else if (fd == hotplugDetector_.GetFd()) {
135         hotplugDetector_.OnEvent();
136     } else {
137         MMI_HILOGE("EventDispatch() called with unknown fd:%{public}d", fd);
138     }
139 }
140 
Stop()141 void LibinputAdapter::Stop()
142 {
143     CALL_DEBUG_ENTER;
144     hotplugDetector_.Stop();
145     if (fd_ >= 0) {
146         close(fd_);
147         fd_ = -1;
148     }
149     if (input_ != nullptr) {
150         libinput_unref(input_);
151         input_ = nullptr;
152     }
153 }
154 
ProcessPendingEvents()155 void LibinputAdapter::ProcessPendingEvents()
156 {
157     OnEventHandler();
158 }
159 
OnEventHandler()160 void LibinputAdapter::OnEventHandler()
161 {
162     CALL_DEBUG_ENTER;
163     CHKPV(funInputEvent_);
164     libinput_event *event = nullptr;
165     int64_t frameTime = GetSysClockTime();
166     while ((event = libinput_get_event(input_))) {
167         funInputEvent_(event, frameTime);
168         libinput_event_destroy(event);
169     }
170     if (event == nullptr) {
171         funInputEvent_(nullptr, 0);
172     }
173 }
174 
ReloadDevice()175 void LibinputAdapter::ReloadDevice()
176 {
177     CALL_DEBUG_ENTER;
178     CHKPV(input_);
179     libinput_suspend(input_);
180     libinput_resume(input_);
181 }
182 
OnDeviceAdded(std::string path)183 void LibinputAdapter::OnDeviceAdded(std::string path)
184 {
185     CALL_DEBUG_ENTER;
186     auto pos = devices_.find(path);
187     if (pos != devices_.end()) {
188         MMI_HILOGD("Path is found");
189         return;
190     }
191     libinput_device* device = libinput_path_add_device(input_, path.c_str());
192     if (device != nullptr) {
193         devices_[std::move(path)] = libinput_device_ref(device);
194         // Libinput doesn't signal device adding event in path mode. Process manually.
195         OnEventHandler();
196     }
197 }
198 
OnDeviceRemoved(std::string path)199 void LibinputAdapter::OnDeviceRemoved(std::string path)
200 {
201     CALL_DEBUG_ENTER;
202     auto pos = devices_.find(path);
203     if (pos != devices_.end()) {
204         libinput_path_remove_device(pos->second);
205         libinput_device_unref(pos->second);
206         devices_.erase(pos);
207         // Libinput doesn't signal device removing event in path mode. Process manually.
208         OnEventHandler();
209     }
210 }
211 } // namespace MMI
212 } // namespace OHOS
213