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 MMI_HILOGD("PrintLog_Info:%{public}s", buffer);
52 va_end(args);
53 }
54 } // namespace
55
DeviceLedUpdate(struct libinput_device * device,int32_t funcKey,bool enable)56 int32_t LibinputAdapter::DeviceLedUpdate(struct libinput_device *device, int32_t funcKey, bool enable)
57 {
58 CHKPR(device, RET_ERR);
59 return libinput_set_led_state(device, funcKey, enable);
60 }
61
62 constexpr static libinput_interface LIBINPUT_INTERFACE = {
__anon4eda756a0202()63 .open_restricted = [](const char *path, int32_t flags, void *user_data)->int32_t {
64 if (path == nullptr) {
65 MMI_HILOGWK("Input device path is nullptr");
66 return RET_ERR;
67 }
68 char realPath[PATH_MAX] = {};
69 if (realpath(path, realPath) == nullptr) {
70 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME_FOR_INPUT));
71 MMI_HILOGWK("The error path is %{public}s", path);
72 return RET_ERR;
73 }
74 int32_t fd = 0;
75 for (int32_t i = 0; i < MAX_RETRY_COUNT; i++) {
76 fd = open(realPath, flags);
77 if (fd >= 0) {
78 break;
79 }
80 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME_FOR_INPUT));
81 }
82 int32_t errNo = errno;
83 MMI_HILOGWK("Libinput .open_restricted path:%{private}s,fd:%{public}d,errno:%{public}d", path, fd, errNo);
84 return fd < 0 ? RET_ERR : fd;
85 },
86 .close_restricted = [](int32_t fd, void *user_data)
__anon4eda756a0302() 87 {
88 if (fd < 0) {
89 return;
90 }
91 MMI_HILOGI("Libinput .close_restricted fd:%{public}d", fd);
92 close(fd);
93 },
94 };
95
Init(FunInputEvent funInputEvent)96 bool LibinputAdapter::Init(FunInputEvent funInputEvent)
97 {
98 CALL_DEBUG_ENTER;
99 CHKPF(funInputEvent);
100 funInputEvent_ = funInputEvent;
101 input_ = libinput_path_create_context(&LIBINPUT_INTERFACE, nullptr);
102 CHKPF(input_);
103 libinput_log_set_handler(input_, &HiLogFunc);
104 fd_ = libinput_get_fd(input_);
105 if (fd_ < 0) {
106 libinput_unref(input_);
107 fd_ = -1;
108 MMI_HILOGE("The fd_ is less than 0");
109 return false;
110 }
111 return hotplugDetector_.Init([this](std::string path) { OnDeviceAdded(std::move(path)); },
112 [this](std::string path) { OnDeviceRemoved(std::move(path)); });
113 }
114
EventDispatch(int32_t fd)115 void LibinputAdapter::EventDispatch(int32_t fd)
116 {
117 CALL_DEBUG_ENTER;
118 if (fd == fd_) {
119 MMI_HILOGD("Start to libinput_dispatch");
120 if (libinput_dispatch(input_) != 0) {
121 MMI_HILOGE("Failed to dispatch libinput");
122 return;
123 }
124 OnEventHandler();
125 MMI_HILOGD("End to OnEventHandler");
126 } else if (fd == hotplugDetector_.GetFd()) {
127 hotplugDetector_.OnEvent();
128 } else {
129 MMI_HILOGE("EventDispatch() called with unknown fd:%{public}d", fd);
130 }
131 }
132
Stop()133 void LibinputAdapter::Stop()
134 {
135 CALL_DEBUG_ENTER;
136 hotplugDetector_.Stop();
137 if (fd_ >= 0) {
138 close(fd_);
139 fd_ = -1;
140 }
141 if (input_ != nullptr) {
142 libinput_unref(input_);
143 input_ = nullptr;
144 }
145 }
146
ProcessPendingEvents()147 void LibinputAdapter::ProcessPendingEvents()
148 {
149 OnEventHandler();
150 }
151
OnEventHandler()152 void LibinputAdapter::OnEventHandler()
153 {
154 CALL_DEBUG_ENTER;
155 CHKPV(funInputEvent_);
156 libinput_event *event = nullptr;
157 int64_t frameTime = GetSysClockTime();
158 while ((event = libinput_get_event(input_))) {
159 funInputEvent_(event, frameTime);
160 libinput_event_destroy(event);
161 }
162 if (event == nullptr) {
163 funInputEvent_(nullptr, 0);
164 }
165 }
166
ReloadDevice()167 void LibinputAdapter::ReloadDevice()
168 {
169 CALL_DEBUG_ENTER;
170 CHKPV(input_);
171 libinput_suspend(input_);
172 libinput_resume(input_);
173 }
174
OnDeviceAdded(std::string path)175 void LibinputAdapter::OnDeviceAdded(std::string path)
176 {
177 CALL_DEBUG_ENTER;
178 auto pos = devices_.find(path);
179 if (pos != devices_.end()) {
180 MMI_HILOGD("Path is found");
181 return;
182 }
183 libinput_device* device = libinput_path_add_device(input_, path.c_str());
184 if (device != nullptr) {
185 devices_[std::move(path)] = libinput_device_ref(device);
186 // Libinput doesn't signal device adding event in path mode. Process manually.
187 OnEventHandler();
188 }
189 }
190
OnDeviceRemoved(std::string path)191 void LibinputAdapter::OnDeviceRemoved(std::string path)
192 {
193 CALL_DEBUG_ENTER;
194 auto pos = devices_.find(path);
195 if (pos != devices_.end()) {
196 libinput_path_remove_device(pos->second);
197 libinput_device_unref(pos->second);
198 devices_.erase(pos);
199 // Libinput doesn't signal device removing event in path mode. Process manually.
200 OnEventHandler();
201 }
202 }
203 } // namespace MMI
204 } // namespace OHOS
205