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