• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <climits>
18 #include <cinttypes>
19 #include <dirent.h>
20 #include <fcntl.h>
21 #include <sys/epoll.h>
22 #include <unistd.h>
23 
24 #include "define_multimodal.h"
25 #include "input_windows_manager.h"
26 #include "util.h"
27 
28 
29 namespace OHOS {
30 namespace MMI {
31 namespace {
32 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "LibinputAdapter" };
33 constexpr int32_t WAIT_TIME_FOR_INPUT { 10 };
34 } // namespace
35 
HiLogFunc(struct libinput * input,libinput_log_priority priority,const char * fmt,va_list args)36 static void HiLogFunc(struct libinput* input, libinput_log_priority priority, const char* fmt, va_list args)
37 {
38     CHKPV(input);
39     char buffer[256];
40     if (vsnprintf_s(buffer, sizeof(buffer), sizeof(buffer) - 1, fmt, args) == -1) {
41         MMI_HILOGE("Call vsnprintf_s failed");
42         va_end(args);
43         return;
44     }
45     MMI_HILOGE("PrintLog_Info:%{public}s", buffer);
46     va_end(args);
47 }
48 
InitHiLogFunc(struct libinput * input)49 static void InitHiLogFunc(struct libinput* input)
50 {
51     CHKPV(input);
52     static bool initFlag = false;
53     if (initFlag) {
54         return;
55     }
56     libinput_log_set_handler(input, &HiLogFunc);
57     initFlag = true;
58 }
59 
LoginfoPackagingTool(struct libinput_event * event)60 void LibinputAdapter::LoginfoPackagingTool(struct libinput_event *event)
61 {
62     CHKPV(event);
63     auto context = libinput_event_get_context(event);
64     CHKPV(context);
65     InitHiLogFunc(context);
66 }
67 
DeviceLedUpdate(struct libinput_device * device,int32_t funcKey,bool enable)68 int32_t LibinputAdapter::DeviceLedUpdate(struct libinput_device *device, int32_t funcKey, bool enable)
69 {
70     CHKPR(device, RET_ERR);
71     return libinput_set_led_state(device, funcKey, enable);
72 }
73 
74 constexpr static libinput_interface LIBINPUT_INTERFACE = {
__anon9032244b0202()75     .open_restricted = [](const char *path, int32_t flags, void *user_data)->int32_t {
76         if (path == nullptr) {
77             MMI_HILOGWK("Input device path is nullptr");
78             return RET_ERR;
79         }
80         char realPath[PATH_MAX] = {};
81         if (realpath(path, realPath) == nullptr) {
82             std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME_FOR_INPUT));
83             MMI_HILOGWK("The error path is %{public}s", path);
84             return RET_ERR;
85         }
86         int32_t fd = open(realPath, flags);
87         int32_t errNo = errno;
88         MMI_HILOGWK("Libinput .open_restricted path:%{public}s,fd:%{public}d,errno:%{public}d", path, fd, errNo);
89         return fd < 0 ? RET_ERR : fd;
90     },
91     .close_restricted = [](int32_t fd, void *user_data)
__anon9032244b0302() 92     {
93         MMI_HILOGI("Libinput .close_restricted fd:%{public}d", fd);
94         close(fd);
95     },
96 };
97 
LibinputAdapter()98 LibinputAdapter::LibinputAdapter() {}
99 
~LibinputAdapter()100 LibinputAdapter::~LibinputAdapter() {}
101 
Init(FunInputEvent funInputEvent,const std::string & seat_id)102 bool LibinputAdapter::Init(FunInputEvent funInputEvent, const std::string& seat_id)
103 {
104     CALL_DEBUG_ENTER;
105     CHKPF(funInputEvent);
106     funInputEvent_ = funInputEvent;
107     seat_id_ = seat_id;
108     if (seat_id_.empty()) {
109         seat_id_ = DEF_SEAT_ID;
110     }
111     udev_ = udev_new();
112     CHKPF(udev_);
113     input_ = libinput_udev_create_context(&LIBINPUT_INTERFACE, nullptr, udev_);
114     CHKPF(input_);
115     int32_t rt = libinput_udev_assign_seat(input_, seat_id_.c_str());
116     if (rt != 0) {
117         libinput_unref(input_);
118         udev_unref(udev_);
119         MMI_HILOGE("The rt is not 0");
120         return false;
121     }
122     fd_ = libinput_get_fd(input_);
123     if (fd_ < 0) {
124         libinput_unref(input_);
125         udev_unref(udev_);
126         fd_ = -1;
127         MMI_HILOGE("The fd_ is less than 0");
128         return false;
129     }
130     return true;
131 }
132 
EventDispatch(struct epoll_event & ev)133 void LibinputAdapter::EventDispatch(struct epoll_event& ev)
134 {
135     CALL_DEBUG_ENTER;
136     CHKPV(ev.data.ptr);
137     auto fd = *static_cast<int*>(ev.data.ptr);
138     if ((ev.events & EPOLLERR) || (ev.events & EPOLLHUP)) {
139         MMI_HILOGF("Epoll unrecoverable error,"
140             "The service must be restarted. fd:%{public}d", fd);
141         free(ev.data.ptr);
142         ev.data.ptr = nullptr;
143         return;
144     }
145     if (libinput_dispatch(input_) != 0) {
146         MMI_HILOGE("Failed to dispatch libinput");
147         return;
148     }
149     OnEventHandler();
150 }
151 
Stop()152 void LibinputAdapter::Stop()
153 {
154     CALL_DEBUG_ENTER;
155     if (fd_ >= 0) {
156         close(fd_);
157         fd_ = -1;
158     }
159     libinput_unref(input_);
160     udev_unref(udev_);
161 }
162 
ProcessPendingEvents()163 void LibinputAdapter::ProcessPendingEvents()
164 {
165     OnEventHandler();
166 }
167 
OnEventHandler()168 void LibinputAdapter::OnEventHandler()
169 {
170     CALL_DEBUG_ENTER;
171     CHKPV(funInputEvent_);
172     libinput_event *event = nullptr;
173     while ((event = libinput_get_event(input_))) {
174         funInputEvent_(event);
175         libinput_event_destroy(event);
176     }
177 }
178 
ReloadDevice()179 void LibinputAdapter::ReloadDevice()
180 {
181     CALL_DEBUG_ENTER;
182     CHKPV(input_);
183     libinput_suspend(input_);
184     libinput_resume(input_);
185 }
186 
RetriggerHotplugEvents()187 void LibinputAdapter::RetriggerHotplugEvents()
188 {
189     CALL_INFO_TRACE;
190     DIR *pdir = opendir("/sys/class/input");
191     if (pdir == nullptr) {
192         MMI_HILOGE("Failed to open directory: \'/sys/class/input\'");
193         return;
194     }
195     for (struct dirent *pdirent = readdir(pdir); pdirent != nullptr; pdirent = readdir(pdir)) {
196         char path[PATH_MAX];
197         if (sprintf_s(path, sizeof(path), "/sys/class/input/%s/uevent", pdirent->d_name) < 0) {
198             continue;
199         }
200         MMI_HILOGD("trigger \'%{public}s\'", path);
201         FILE *fs = fopen(path, "r+");
202         if (fs == nullptr) {
203             continue;
204         }
205         if (fputs("add", fs) < 0) {
206             MMI_HILOGW("fputs error:%{public}s", strerror(errno));
207         }
208         if (fclose(fs) != 0) {
209             MMI_HILOGW("fclose error:%{public}s", strerror(errno));
210         }
211     }
212     if (closedir(pdir) != 0) {
213         MMI_HILOGW("closedir error:%{public}s", strerror(errno));
214     }
215 }
216 } // namespace MMI
217 } // namespace OHOS
218