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_HILOGE("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 = {
__anon76ad25890202()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)
__anon76ad25890302() 83 {
84 MMI_HILOGI("Libinput .close_restricted fd:%{public}d", fd);
85 close(fd);
86 },
87 };
88
Init(FunInputEvent funInputEvent)89 bool LibinputAdapter::Init(FunInputEvent funInputEvent)
90 {
91 CALL_DEBUG_ENTER;
92 CHKPF(funInputEvent);
93 funInputEvent_ = funInputEvent;
94 input_ = libinput_path_create_context(&LIBINPUT_INTERFACE, nullptr);
95 CHKPF(input_);
96 libinput_log_set_handler(input_, &HiLogFunc);
97 fd_ = libinput_get_fd(input_);
98 if (fd_ < 0) {
99 libinput_unref(input_);
100 fd_ = -1;
101 MMI_HILOGE("The fd_ is less than 0");
102 return false;
103 }
104 return hotplugDetector_.Init([this](std::string path) { OnDeviceAdded(std::move(path)); },
105 [this](std::string path) { OnDeviceRemoved(std::move(path)); });
106 }
107
EventDispatch(int32_t fd)108 void LibinputAdapter::EventDispatch(int32_t fd)
109 {
110 CALL_DEBUG_ENTER;
111 if (fd == fd_) {
112 MMI_HILOGD("Start to libinput_dispatch");
113 if (libinput_dispatch(input_) != 0) {
114 MMI_HILOGE("Failed to dispatch libinput");
115 return;
116 }
117 OnEventHandler();
118 MMI_HILOGD("End to OnEventHandler");
119 } else if (fd == hotplugDetector_.GetFd()) {
120 hotplugDetector_.OnEvent();
121 } else {
122 MMI_HILOGE("EventDispatch() called with unknown fd: %{public}d.", fd);
123 }
124 }
125
Stop()126 void LibinputAdapter::Stop()
127 {
128 CALL_DEBUG_ENTER;
129 hotplugDetector_.Stop();
130 if (fd_ >= 0) {
131 close(fd_);
132 fd_ = -1;
133 }
134 if (input_ != nullptr) {
135 libinput_unref(input_);
136 input_ = nullptr;
137 }
138 }
139
ProcessPendingEvents()140 void LibinputAdapter::ProcessPendingEvents()
141 {
142 OnEventHandler();
143 }
144
OnEventHandler()145 void LibinputAdapter::OnEventHandler()
146 {
147 CALL_DEBUG_ENTER;
148 CHKPV(funInputEvent_);
149 libinput_event *event = nullptr;
150 while ((event = libinput_get_event(input_))) {
151 funInputEvent_(event);
152 libinput_event_destroy(event);
153 }
154 }
155
ReloadDevice()156 void LibinputAdapter::ReloadDevice()
157 {
158 CALL_DEBUG_ENTER;
159 CHKPV(input_);
160 libinput_suspend(input_);
161 libinput_resume(input_);
162 }
163
OnDeviceAdded(std::string path)164 void LibinputAdapter::OnDeviceAdded(std::string path)
165 {
166 CALL_DEBUG_ENTER;
167 libinput_device* device = libinput_path_add_device(input_, path.c_str());
168 if (device != nullptr) {
169 devices_[std::move(path)] = libinput_device_ref(device);
170 // Libinput doesn't signal device adding event in path mode. Process manually.
171 OnEventHandler();
172 }
173 }
174
OnDeviceRemoved(std::string path)175 void LibinputAdapter::OnDeviceRemoved(std::string path)
176 {
177 CALL_DEBUG_ENTER;
178 auto pos = devices_.find(path);
179 if (pos != devices_.end()) {
180 libinput_path_remove_device(pos->second);
181 libinput_device_unref(pos->second);
182 devices_.erase(pos);
183 // Libinput doesn't signal device removing event in path mode. Process manually.
184 OnEventHandler();
185 }
186 }
187 } // namespace MMI
188 } // namespace OHOS
189