• 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 
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 #include "input_device_manager.h"
30 #include "key_event_normalize.h"
31 
32 
33 #undef MMI_LOG_DOMAIN
34 #define MMI_LOG_DOMAIN MMI_LOG_SERVER
35 #undef MMI_LOG_TAG
36 #define MMI_LOG_TAG "LibinputAdapter"
37 
38 namespace OHOS {
39 namespace MMI {
40 namespace {
41 constexpr int32_t WAIT_TIME_FOR_INPUT { 10 };
42 constexpr int32_t MAX_RETRY_COUNT { 5 };
43 constexpr uint32_t KEY_CAPSLOCK { 58 };
44 
HiLogFunc(struct libinput * input,libinput_log_priority priority,const char * fmt,va_list args)45 void HiLogFunc(struct libinput* input, libinput_log_priority priority, const char* fmt, va_list args)
46 {
47     CHKPV(input);
48     CHKPV(fmt);
49     char buffer[256] = {};
50     if (vsnprintf_s(buffer, sizeof(buffer), sizeof(buffer) - 1, fmt, args) == -1) {
51         MMI_HILOGE("Call vsnprintf_s failed");
52         va_end(args);
53         return;
54     }
55     if (strstr(buffer, "LOG_LEVEL_I") != NULL) {
56         MMI_HILOGI("PrintLog_Info:%{public}s", buffer);
57     } else if (strstr(buffer, "LOG_LEVEL_D") != NULL) {
58         MMI_HILOGD("PrintLog_Info:%{public}s", buffer);
59     } else if (strstr(buffer, "LOG_LEVEL_E") != NULL) {
60         MMI_HILOGE("PrintLog_Info:%{public}s", buffer);
61     } else {
62         MMI_HILOGD("PrintLog_Info:%{public}s", buffer);
63     }
64     va_end(args);
65 }
66 } // namespace
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 = {
__anon55a7842c0202()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 = 0;
87         for (int32_t i = 0; i < MAX_RETRY_COUNT; i++) {
88             fd = open(realPath, flags);
89             if (fd >= 0) {
90                 break;
91             }
92             std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME_FOR_INPUT));
93         }
94         int32_t errNo = errno;
95         MMI_HILOGWK("Libinput .open_restricted path:%{private}s,fd:%{public}d,errno:%{public}d", path, fd, errNo);
96         return fd < 0 ? RET_ERR : fd;
97     },
98     .close_restricted = [](int32_t fd, void *user_data)
__anon55a7842c0302() 99     {
100         if (fd < 0) {
101             return;
102         }
103         MMI_HILOGI("Libinput .close_restricted fd:%{public}d", fd);
104         close(fd);
105     },
106 };
107 
Init(FunInputEvent funInputEvent)108 bool LibinputAdapter::Init(FunInputEvent funInputEvent)
109 {
110     CALL_DEBUG_ENTER;
111     CHKPF(funInputEvent);
112     funInputEvent_ = funInputEvent;
113     input_ = libinput_path_create_context(&LIBINPUT_INTERFACE, nullptr);
114     CHKPF(input_);
115     libinput_log_set_handler(input_, &HiLogFunc);
116     fd_ = libinput_get_fd(input_);
117     if (fd_ < 0) {
118         libinput_unref(input_);
119         fd_ = -1;
120         MMI_HILOGE("The fd_ is less than 0");
121         return false;
122     }
123     return hotplugDetector_.Init([this](std::string path) { OnDeviceAdded(std::move(path)); },
124         [this](std::string path) { OnDeviceRemoved(std::move(path)); });
125 }
126 
EventDispatch(int32_t fd)127 void LibinputAdapter::EventDispatch(int32_t fd)
128 {
129     CALL_DEBUG_ENTER;
130     if (fd == fd_) {
131         MMI_HILOGD("Start to libinput_dispatch");
132         if (libinput_dispatch(input_) != 0) {
133             MMI_HILOGE("Failed to dispatch libinput");
134             return;
135         }
136         OnEventHandler();
137         MMI_HILOGD("End to OnEventHandler");
138     } else if (fd == hotplugDetector_.GetFd()) {
139         hotplugDetector_.OnEvent();
140     } else {
141         MMI_HILOGE("EventDispatch() called with unknown fd:%{public}d", fd);
142     }
143 }
144 
Stop()145 void LibinputAdapter::Stop()
146 {
147     CALL_DEBUG_ENTER;
148     hotplugDetector_.Stop();
149     if (fd_ >= 0) {
150         close(fd_);
151         fd_ = -1;
152     }
153     if (input_ != nullptr) {
154         libinput_unref(input_);
155         input_ = nullptr;
156     }
157 }
158 
ProcessPendingEvents()159 void LibinputAdapter::ProcessPendingEvents()
160 {
161     OnEventHandler();
162 }
163 
OnEventHandler()164 void LibinputAdapter::OnEventHandler()
165 {
166     CALL_DEBUG_ENTER;
167     CHKPV(funInputEvent_);
168     libinput_event *event = nullptr;
169     int64_t frameTime = GetSysClockTime();
170     while ((event = libinput_get_event(input_))) {
171         MultiKeyboardSetFuncState(event);
172         funInputEvent_(event, frameTime);
173         libinput_event_destroy(event);
174     }
175     if (event == nullptr) {
176         funInputEvent_(nullptr, 0);
177     }
178 }
179 
ReloadDevice()180 void LibinputAdapter::ReloadDevice()
181 {
182     CALL_DEBUG_ENTER;
183     CHKPV(input_);
184     libinput_suspend(input_);
185     libinput_resume(input_);
186 }
187 
OnDeviceAdded(std::string path)188 void LibinputAdapter::OnDeviceAdded(std::string path)
189 {
190     CALL_DEBUG_ENTER;
191     auto pos = devices_.find(path);
192     if (pos != devices_.end()) {
193         MMI_HILOGD("Path is found");
194         return;
195     }
196     libinput_device* device = libinput_path_add_device(input_, path.c_str());
197     if (device != nullptr) {
198         devices_[std::move(path)] = libinput_device_ref(device);
199         // Libinput doesn't signal device adding event in path mode. Process manually.
200         OnEventHandler();
201     }
202 }
203 
OnDeviceRemoved(std::string path)204 void LibinputAdapter::OnDeviceRemoved(std::string path)
205 {
206     CALL_DEBUG_ENTER;
207     auto pos = devices_.find(path);
208     if (pos != devices_.end()) {
209         libinput_path_remove_device(pos->second);
210         libinput_device_unref(pos->second);
211         devices_.erase(pos);
212         // Libinput doesn't signal device removing event in path mode. Process manually.
213         OnEventHandler();
214     }
215 }
216 
MultiKeyboardSetLedState(bool oldCapsLockState)217 void LibinputAdapter::MultiKeyboardSetLedState(bool oldCapsLockState)
218 {
219     std::vector<struct libinput_device*> input_device;
220     INPUT_DEV_MGR->GetMultiKeyboardDevice(input_device);
221     for (auto it = input_device.begin(); it != input_device.end(); ++it) {
222         auto setDevice = (*it);
223         CHKPV(setDevice);
224         DeviceLedUpdate(setDevice, KeyEvent::CAPS_LOCK_FUNCTION_KEY, !oldCapsLockState);
225     }
226 }
227 
MultiKeyboardSetFuncState(libinput_event * event)228 void LibinputAdapter::MultiKeyboardSetFuncState(libinput_event* event)
229 {
230     libinput_event_type eventType = libinput_event_get_type(event);
231     if (eventType == LIBINPUT_EVENT_KEYBOARD_KEY) {
232             struct libinput_event_keyboard* keyboardEvent = libinput_event_get_keyboard_event(event);
233             CHKPV(keyboardEvent);
234             std::shared_ptr<KeyEvent> keyEvent = KeyEventHdr->GetKeyEvent();
235             if (libinput_event_keyboard_get_key_state(keyboardEvent) == LIBINPUT_KEY_STATE_PRESSED
236 			   && libinput_event_keyboard_get_key(keyboardEvent) == KEY_CAPSLOCK
237 			   && keyEvent != nullptr) {
238                 bool oldCapsLockOn = keyEvent->GetFunctionKey(MMI::KeyEvent::CAPS_LOCK_FUNCTION_KEY);
239                 MultiKeyboardSetLedState(oldCapsLockOn);
240                 keyEvent->SetFunctionKey(MMI::KeyEvent::CAPS_LOCK_FUNCTION_KEY, !oldCapsLockOn);
241             }
242     }
243 }
244 } // namespace MMI
245 } // namespace OHOS
246