blob: b97ff909d63d914b6fabc59a7f2efe72201b067e [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mark Salyzyn5aa26b22014-06-10 13:07:44 -070017#include <assert.h>
18#include <dirent.h>
19#include <errno.h>
20#include <fcntl.h>
21#include <inttypes.h>
22#include <memory.h>
23#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
Siarhei Vishniakou7a522bf2019-09-24 12:46:29 +010027#include <sys/capability.h>
Mark Salyzyn5aa26b22014-06-10 13:07:44 -070028#include <sys/epoll.h>
Mark Salyzyn5aa26b22014-06-10 13:07:44 -070029#include <sys/inotify.h>
30#include <sys/ioctl.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070031#include <sys/limits.h>
Mark Salyzyn5aa26b22014-06-10 13:07:44 -070032#include <unistd.h>
33
Michael Wrightd02c5b62014-02-10 15:10:22 -080034#define LOG_TAG "EventHub"
35
36// #define LOG_NDEBUG 0
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080037#include <android-base/stringprintf.h>
Philip Quinn39b81682019-01-09 22:20:39 -080038#include <cutils/properties.h>
Chris Ye8594e192020-07-14 10:34:06 -070039#include <input/KeyCharacterMap.h>
40#include <input/KeyLayoutMap.h>
41#include <input/VirtualKeyMap.h>
Dan Albert677d87e2014-06-16 17:31:28 -070042#include <openssl/sha.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070043#include <utils/Errors.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080044#include <utils/Log.h>
45#include <utils/Timers.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080046
Chris Ye8594e192020-07-14 10:34:06 -070047#include <filesystem>
48
49#include "EventHub.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080050
Michael Wrightd02c5b62014-02-10 15:10:22 -080051#define INDENT " "
52#define INDENT2 " "
53#define INDENT3 " "
54
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080055using android::base::StringPrintf;
Chris Ye1b0c7342020-07-28 21:57:03 -070056using namespace android::flag_operators;
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080057
Michael Wrightd02c5b62014-02-10 15:10:22 -080058namespace android {
59
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070060static const char* DEVICE_PATH = "/dev/input";
Siarhei Vishniakou951f3622018-12-12 19:45:42 -080061// v4l2 devices go directly into /dev
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070062static const char* VIDEO_DEVICE_PATH = "/dev";
Michael Wrightd02c5b62014-02-10 15:10:22 -080063
Chris Ye87143712020-11-10 05:05:58 +000064static constexpr int32_t FF_STRONG_MAGNITUDE_CHANNEL_IDX = 0;
65static constexpr int32_t FF_WEAK_MAGNITUDE_CHANNEL_IDX = 1;
Chris Ye6393a262020-08-04 19:41:36 -070066
Michael Wrightd02c5b62014-02-10 15:10:22 -080067static inline const char* toString(bool value) {
68 return value ? "true" : "false";
69}
70
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010071static std::string sha1(const std::string& in) {
Dan Albert677d87e2014-06-16 17:31:28 -070072 SHA_CTX ctx;
73 SHA1_Init(&ctx);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010074 SHA1_Update(&ctx, reinterpret_cast<const u_char*>(in.c_str()), in.size());
Dan Albert677d87e2014-06-16 17:31:28 -070075 u_char digest[SHA_DIGEST_LENGTH];
76 SHA1_Final(digest, &ctx);
Michael Wrightd02c5b62014-02-10 15:10:22 -080077
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010078 std::string out;
Dan Albert677d87e2014-06-16 17:31:28 -070079 for (size_t i = 0; i < SHA_DIGEST_LENGTH; i++) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010080 out += StringPrintf("%02x", digest[i]);
Michael Wrightd02c5b62014-02-10 15:10:22 -080081 }
82 return out;
83}
84
Siarhei Vishniakou951f3622018-12-12 19:45:42 -080085/**
86 * Return true if name matches "v4l-touch*"
87 */
Chris Ye8594e192020-07-14 10:34:06 -070088static bool isV4lTouchNode(std::string name) {
89 return name.find("v4l-touch") != std::string::npos;
Siarhei Vishniakou951f3622018-12-12 19:45:42 -080090}
91
Philip Quinn39b81682019-01-09 22:20:39 -080092/**
93 * Returns true if V4L devices should be scanned.
94 *
95 * The system property ro.input.video_enabled can be used to control whether
96 * EventHub scans and opens V4L devices. As V4L does not support multiple
97 * clients, EventHub effectively blocks access to these devices when it opens
Siarhei Vishniakou29f88492019-04-05 14:11:43 -070098 * them.
99 *
100 * Setting this to "false" would prevent any video devices from being discovered and
101 * associated with input devices.
102 *
103 * This property can be used as follows:
104 * 1. To turn off features that are dependent on video device presence.
105 * 2. During testing and development, to allow other clients to read video devices
106 * directly from /dev.
Philip Quinn39b81682019-01-09 22:20:39 -0800107 */
108static bool isV4lScanningEnabled() {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700109 return property_get_bool("ro.input.video_enabled", true /* default_value */);
Philip Quinn39b81682019-01-09 22:20:39 -0800110}
111
Siarhei Vishniakou592bac22018-11-08 19:42:38 -0800112static nsecs_t processEventTimestamp(const struct input_event& event) {
113 // Use the time specified in the event instead of the current time
114 // so that downstream code can get more accurate estimates of
115 // event dispatch latency from the time the event is enqueued onto
116 // the evdev client buffer.
117 //
118 // The event's timestamp fortuitously uses the same monotonic clock
119 // time base as the rest of Android. The kernel event device driver
120 // (drivers/input/evdev.c) obtains timestamps using ktime_get_ts().
121 // The systemTime(SYSTEM_TIME_MONOTONIC) function we use everywhere
122 // calls clock_gettime(CLOCK_MONOTONIC) which is implemented as a
123 // system call that also queries ktime_get_ts().
124
125 const nsecs_t inputEventTime = seconds_to_nanoseconds(event.time.tv_sec) +
126 microseconds_to_nanoseconds(event.time.tv_usec);
127 return inputEventTime;
128}
129
Michael Wrightd02c5b62014-02-10 15:10:22 -0800130// --- Global Functions ---
131
Chris Ye1b0c7342020-07-28 21:57:03 -0700132Flags<InputDeviceClass> getAbsAxisUsage(int32_t axis, Flags<InputDeviceClass> deviceClasses) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800133 // Touch devices get dibs on touch-related axes.
Chris Ye1b0c7342020-07-28 21:57:03 -0700134 if (deviceClasses.test(InputDeviceClass::TOUCH)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800135 switch (axis) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700136 case ABS_X:
137 case ABS_Y:
138 case ABS_PRESSURE:
139 case ABS_TOOL_WIDTH:
140 case ABS_DISTANCE:
141 case ABS_TILT_X:
142 case ABS_TILT_Y:
143 case ABS_MT_SLOT:
144 case ABS_MT_TOUCH_MAJOR:
145 case ABS_MT_TOUCH_MINOR:
146 case ABS_MT_WIDTH_MAJOR:
147 case ABS_MT_WIDTH_MINOR:
148 case ABS_MT_ORIENTATION:
149 case ABS_MT_POSITION_X:
150 case ABS_MT_POSITION_Y:
151 case ABS_MT_TOOL_TYPE:
152 case ABS_MT_BLOB_ID:
153 case ABS_MT_TRACKING_ID:
154 case ABS_MT_PRESSURE:
155 case ABS_MT_DISTANCE:
Chris Ye1b0c7342020-07-28 21:57:03 -0700156 return InputDeviceClass::TOUCH;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800157 }
158 }
159
Chris Yef59a2f42020-10-16 12:55:26 -0700160 if (deviceClasses.test(InputDeviceClass::SENSOR)) {
161 switch (axis) {
162 case ABS_X:
163 case ABS_Y:
164 case ABS_Z:
165 case ABS_RX:
166 case ABS_RY:
167 case ABS_RZ:
168 return InputDeviceClass::SENSOR;
169 }
170 }
171
Michael Wright842500e2015-03-13 17:32:02 -0700172 // External stylus gets the pressure axis
Chris Ye1b0c7342020-07-28 21:57:03 -0700173 if (deviceClasses.test(InputDeviceClass::EXTERNAL_STYLUS)) {
Michael Wright842500e2015-03-13 17:32:02 -0700174 if (axis == ABS_PRESSURE) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700175 return InputDeviceClass::EXTERNAL_STYLUS;
Michael Wright842500e2015-03-13 17:32:02 -0700176 }
177 }
178
Michael Wrightd02c5b62014-02-10 15:10:22 -0800179 // Joystick devices get the rest.
Chris Ye1b0c7342020-07-28 21:57:03 -0700180 return deviceClasses & InputDeviceClass::JOYSTICK;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800181}
182
183// --- EventHub::Device ---
184
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100185EventHub::Device::Device(int fd, int32_t id, const std::string& path,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700186 const InputDeviceIdentifier& identifier)
Chris Ye989bb932020-07-04 16:18:59 -0700187 : fd(fd),
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700188 id(id),
189 path(path),
190 identifier(identifier),
191 classes(0),
192 configuration(nullptr),
193 virtualKeyMap(nullptr),
194 ffEffectPlaying(false),
195 ffEffectId(-1),
196 controllerNumber(0),
197 enabled(true),
Chris Ye66fbac32020-07-06 20:36:43 -0700198 isVirtual(fd < 0) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800199
200EventHub::Device::~Device() {
201 close();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800202}
203
204void EventHub::Device::close() {
205 if (fd >= 0) {
206 ::close(fd);
207 fd = -1;
208 }
209}
210
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700211status_t EventHub::Device::enable() {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100212 fd = open(path.c_str(), O_RDWR | O_CLOEXEC | O_NONBLOCK);
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700213 if (fd < 0) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100214 ALOGE("could not open %s, %s\n", path.c_str(), strerror(errno));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700215 return -errno;
216 }
217 enabled = true;
218 return OK;
219}
220
221status_t EventHub::Device::disable() {
222 close();
223 enabled = false;
224 return OK;
225}
226
Chris Ye989bb932020-07-04 16:18:59 -0700227bool EventHub::Device::hasValidFd() const {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700228 return !isVirtual && enabled;
229}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800230
Chris Ye3a1e4462020-08-12 10:13:15 -0700231const std::shared_ptr<KeyCharacterMap> EventHub::Device::getKeyCharacterMap() const {
Chris Ye989bb932020-07-04 16:18:59 -0700232 return keyMap.keyCharacterMap;
233}
234
235template <std::size_t N>
236status_t EventHub::Device::readDeviceBitMask(unsigned long ioctlCode, BitArray<N>& bitArray) {
237 if (!hasValidFd()) {
238 return BAD_VALUE;
239 }
240 if ((_IOC_SIZE(ioctlCode) == 0)) {
241 ioctlCode |= _IOC(0, 0, 0, bitArray.bytes());
242 }
243
244 typename BitArray<N>::Buffer buffer;
245 status_t ret = ioctl(fd, ioctlCode, buffer.data());
246 bitArray.loadFromBuffer(buffer);
247 return ret;
248}
249
250void EventHub::Device::configureFd() {
251 // Set fd parameters with ioctl, such as key repeat, suspend block, and clock type
252 if (classes.test(InputDeviceClass::KEYBOARD)) {
253 // Disable kernel key repeat since we handle it ourselves
254 unsigned int repeatRate[] = {0, 0};
255 if (ioctl(fd, EVIOCSREP, repeatRate)) {
256 ALOGW("Unable to disable kernel key repeat for %s: %s", path.c_str(), strerror(errno));
257 }
258 }
259
260 // Tell the kernel that we want to use the monotonic clock for reporting timestamps
261 // associated with input events. This is important because the input system
262 // uses the timestamps extensively and assumes they were recorded using the monotonic
263 // clock.
264 int clockId = CLOCK_MONOTONIC;
Chris Yef59a2f42020-10-16 12:55:26 -0700265 if (classes.test(InputDeviceClass::SENSOR)) {
266 // Each new sensor event should use the same time base as
267 // SystemClock.elapsedRealtimeNanos().
268 clockId = CLOCK_BOOTTIME;
269 }
Chris Ye989bb932020-07-04 16:18:59 -0700270 bool usingClockIoctl = !ioctl(fd, EVIOCSCLOCKID, &clockId);
271 ALOGI("usingClockIoctl=%s", toString(usingClockIoctl));
272}
273
274bool EventHub::Device::hasKeycodeLocked(int keycode) const {
275 if (!keyMap.haveKeyLayout()) {
276 return false;
277 }
278
279 std::vector<int32_t> scanCodes;
280 keyMap.keyLayoutMap->findScanCodesForKey(keycode, &scanCodes);
281 const size_t N = scanCodes.size();
282 for (size_t i = 0; i < N && i <= KEY_MAX; i++) {
283 int32_t sc = scanCodes[i];
284 if (sc >= 0 && sc <= KEY_MAX && keyBitmask.test(sc)) {
285 return true;
286 }
287 }
288
289 return false;
290}
291
292void EventHub::Device::loadConfigurationLocked() {
293 configurationFile =
294 getInputDeviceConfigurationFilePathByDeviceIdentifier(identifier,
295 InputDeviceConfigurationFileType::
296 CONFIGURATION);
297 if (configurationFile.empty()) {
298 ALOGD("No input device configuration file found for device '%s'.", identifier.name.c_str());
299 } else {
Siarhei Vishniakou4d9f9772020-09-02 22:28:29 -0500300 android::base::Result<std::unique_ptr<PropertyMap>> propertyMap =
301 PropertyMap::load(configurationFile.c_str());
302 if (!propertyMap.ok()) {
Chris Ye989bb932020-07-04 16:18:59 -0700303 ALOGE("Error loading input device configuration file for device '%s'. "
304 "Using default configuration.",
305 identifier.name.c_str());
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500306 } else {
Siarhei Vishniakou4d9f9772020-09-02 22:28:29 -0500307 configuration = std::move(*propertyMap);
Chris Ye989bb932020-07-04 16:18:59 -0700308 }
309 }
310}
311
312bool EventHub::Device::loadVirtualKeyMapLocked() {
313 // The virtual key map is supplied by the kernel as a system board property file.
314 std::string propPath = "/sys/board_properties/virtualkeys.";
315 propPath += identifier.getCanonicalName();
316 if (access(propPath.c_str(), R_OK)) {
317 return false;
318 }
319 virtualKeyMap = VirtualKeyMap::load(propPath);
320 return virtualKeyMap != nullptr;
321}
322
323status_t EventHub::Device::loadKeyMapLocked() {
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500324 return keyMap.load(identifier, configuration.get());
Chris Ye989bb932020-07-04 16:18:59 -0700325}
326
327bool EventHub::Device::isExternalDeviceLocked() {
328 if (configuration) {
329 bool value;
330 if (configuration->tryGetProperty(String8("device.internal"), value)) {
331 return !value;
332 }
333 }
334 return identifier.bus == BUS_USB || identifier.bus == BUS_BLUETOOTH;
335}
336
337bool EventHub::Device::deviceHasMicLocked() {
338 if (configuration) {
339 bool value;
340 if (configuration->tryGetProperty(String8("audio.mic"), value)) {
341 return value;
342 }
343 }
344 return false;
345}
346
347void EventHub::Device::setLedStateLocked(int32_t led, bool on) {
348 int32_t sc;
349 if (hasValidFd() && mapLed(led, &sc) != NAME_NOT_FOUND) {
350 struct input_event ev;
351 ev.time.tv_sec = 0;
352 ev.time.tv_usec = 0;
353 ev.type = EV_LED;
354 ev.code = sc;
355 ev.value = on ? 1 : 0;
356
357 ssize_t nWrite;
358 do {
359 nWrite = write(fd, &ev, sizeof(struct input_event));
360 } while (nWrite == -1 && errno == EINTR);
361 }
362}
363
364void EventHub::Device::setLedForControllerLocked() {
365 for (int i = 0; i < MAX_CONTROLLER_LEDS; i++) {
366 setLedStateLocked(ALED_CONTROLLER_1 + i, controllerNumber == i + 1);
367 }
368}
369
370status_t EventHub::Device::mapLed(int32_t led, int32_t* outScanCode) const {
371 if (!keyMap.haveKeyLayout()) {
372 return NAME_NOT_FOUND;
373 }
374
375 int32_t scanCode;
376 if (keyMap.keyLayoutMap->findScanCodeForLed(led, &scanCode) != NAME_NOT_FOUND) {
377 if (scanCode >= 0 && scanCode <= LED_MAX && ledBitmask.test(scanCode)) {
378 *outScanCode = scanCode;
379 return NO_ERROR;
380 }
381 }
382 return NAME_NOT_FOUND;
383}
384
Siarhei Vishniakou7a522bf2019-09-24 12:46:29 +0100385/**
386 * Get the capabilities for the current process.
387 * Crashes the system if unable to create / check / destroy the capabilities object.
388 */
389class Capabilities final {
390public:
391 explicit Capabilities() {
392 mCaps = cap_get_proc();
393 LOG_ALWAYS_FATAL_IF(mCaps == nullptr, "Could not get capabilities of the current process");
394 }
395
396 /**
397 * Check whether the current process has a specific capability
398 * in the set of effective capabilities.
399 * Return CAP_SET if the process has the requested capability
400 * Return CAP_CLEAR otherwise.
401 */
402 cap_flag_value_t checkEffectiveCapability(cap_value_t capability) {
403 cap_flag_value_t value;
404 const int result = cap_get_flag(mCaps, capability, CAP_EFFECTIVE, &value);
405 LOG_ALWAYS_FATAL_IF(result == -1, "Could not obtain the requested capability");
406 return value;
407 }
408
409 ~Capabilities() {
410 const int result = cap_free(mCaps);
411 LOG_ALWAYS_FATAL_IF(result == -1, "Could not release the capabilities structure");
412 }
413
414private:
415 cap_t mCaps;
416};
417
418static void ensureProcessCanBlockSuspend() {
419 Capabilities capabilities;
420 const bool canBlockSuspend =
421 capabilities.checkEffectiveCapability(CAP_BLOCK_SUSPEND) == CAP_SET;
422 LOG_ALWAYS_FATAL_IF(!canBlockSuspend,
423 "Input must be able to block suspend to properly process events");
424}
425
Michael Wrightd02c5b62014-02-10 15:10:22 -0800426// --- EventHub ---
427
Michael Wrightd02c5b62014-02-10 15:10:22 -0800428const int EventHub::EPOLL_MAX_EVENTS;
429
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700430EventHub::EventHub(void)
431 : mBuiltInKeyboardId(NO_BUILT_IN_KEYBOARD),
432 mNextDeviceId(1),
433 mControllerNumbers(),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800434 mNeedToSendFinishedDeviceScan(false),
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700435 mNeedToReopenDevices(false),
436 mNeedToScanDevices(true),
437 mPendingEventCount(0),
438 mPendingEventIndex(0),
439 mPendingINotify(false) {
Siarhei Vishniakou7a522bf2019-09-24 12:46:29 +0100440 ensureProcessCanBlockSuspend();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800441
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -0800442 mEpollFd = epoll_create1(EPOLL_CLOEXEC);
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800443 LOG_ALWAYS_FATAL_IF(mEpollFd < 0, "Could not create epoll instance: %s", strerror(errno));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800444
445 mINotifyFd = inotify_init();
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800446 mInputWd = inotify_add_watch(mINotifyFd, DEVICE_PATH, IN_DELETE | IN_CREATE);
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700447 LOG_ALWAYS_FATAL_IF(mInputWd < 0, "Could not register INotify for %s: %s", DEVICE_PATH,
448 strerror(errno));
Philip Quinn39b81682019-01-09 22:20:39 -0800449 if (isV4lScanningEnabled()) {
450 mVideoWd = inotify_add_watch(mINotifyFd, VIDEO_DEVICE_PATH, IN_DELETE | IN_CREATE);
451 LOG_ALWAYS_FATAL_IF(mVideoWd < 0, "Could not register INotify for %s: %s",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700452 VIDEO_DEVICE_PATH, strerror(errno));
Philip Quinn39b81682019-01-09 22:20:39 -0800453 } else {
454 mVideoWd = -1;
455 ALOGI("Video device scanning disabled");
456 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800457
Siarhei Vishniakou2d0e9482019-09-24 12:52:47 +0100458 struct epoll_event eventItem = {};
459 eventItem.events = EPOLLIN | EPOLLWAKEUP;
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -0700460 eventItem.data.fd = mINotifyFd;
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800461 int result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mINotifyFd, &eventItem);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800462 LOG_ALWAYS_FATAL_IF(result != 0, "Could not add INotify to epoll instance. errno=%d", errno);
463
464 int wakeFds[2];
465 result = pipe(wakeFds);
466 LOG_ALWAYS_FATAL_IF(result != 0, "Could not create wake pipe. errno=%d", errno);
467
468 mWakeReadPipeFd = wakeFds[0];
469 mWakeWritePipeFd = wakeFds[1];
470
471 result = fcntl(mWakeReadPipeFd, F_SETFL, O_NONBLOCK);
472 LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake read pipe non-blocking. errno=%d",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700473 errno);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800474
475 result = fcntl(mWakeWritePipeFd, F_SETFL, O_NONBLOCK);
476 LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake write pipe non-blocking. errno=%d",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700477 errno);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800478
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -0700479 eventItem.data.fd = mWakeReadPipeFd;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800480 result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeReadPipeFd, &eventItem);
481 LOG_ALWAYS_FATAL_IF(result != 0, "Could not add wake read pipe to epoll instance. errno=%d",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700482 errno);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800483}
484
485EventHub::~EventHub(void) {
486 closeAllDevicesLocked();
487
Michael Wrightd02c5b62014-02-10 15:10:22 -0800488 ::close(mEpollFd);
489 ::close(mINotifyFd);
490 ::close(mWakeReadPipeFd);
491 ::close(mWakeWritePipeFd);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800492}
493
494InputDeviceIdentifier EventHub::getDeviceIdentifier(int32_t deviceId) const {
Chris Ye87143712020-11-10 05:05:58 +0000495 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800496 Device* device = getDeviceLocked(deviceId);
Chris Ye989bb932020-07-04 16:18:59 -0700497 return device != nullptr ? device->identifier : InputDeviceIdentifier();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800498}
499
Chris Ye1b0c7342020-07-28 21:57:03 -0700500Flags<InputDeviceClass> EventHub::getDeviceClasses(int32_t deviceId) const {
Chris Ye87143712020-11-10 05:05:58 +0000501 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800502 Device* device = getDeviceLocked(deviceId);
Chris Ye989bb932020-07-04 16:18:59 -0700503 return device != nullptr ? device->classes : Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800504}
505
506int32_t EventHub::getDeviceControllerNumber(int32_t deviceId) const {
Chris Ye87143712020-11-10 05:05:58 +0000507 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800508 Device* device = getDeviceLocked(deviceId);
Chris Ye989bb932020-07-04 16:18:59 -0700509 return device != nullptr ? device->controllerNumber : 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800510}
511
512void EventHub::getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
Chris Ye87143712020-11-10 05:05:58 +0000513 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800514 Device* device = getDeviceLocked(deviceId);
Chris Ye989bb932020-07-04 16:18:59 -0700515 if (device != nullptr && device->configuration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800516 *outConfiguration = *device->configuration;
517 } else {
518 outConfiguration->clear();
519 }
520}
521
522status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700523 RawAbsoluteAxisInfo* outAxisInfo) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800524 outAxisInfo->clear();
525
526 if (axis >= 0 && axis <= ABS_MAX) {
Chris Ye87143712020-11-10 05:05:58 +0000527 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800528
529 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -0700530 if (device != nullptr && device->hasValidFd() && device->absBitmask.test(axis)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800531 struct input_absinfo info;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700532 if (ioctl(device->fd, EVIOCGABS(axis), &info)) {
533 ALOGW("Error reading absolute controller %d for device %s fd %d, errno=%d", axis,
534 device->identifier.name.c_str(), device->fd, errno);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800535 return -errno;
536 }
537
538 if (info.minimum != info.maximum) {
539 outAxisInfo->valid = true;
540 outAxisInfo->minValue = info.minimum;
541 outAxisInfo->maxValue = info.maximum;
542 outAxisInfo->flat = info.flat;
543 outAxisInfo->fuzz = info.fuzz;
544 outAxisInfo->resolution = info.resolution;
545 }
546 return OK;
547 }
548 }
549 return -1;
550}
551
552bool EventHub::hasRelativeAxis(int32_t deviceId, int axis) const {
553 if (axis >= 0 && axis <= REL_MAX) {
Chris Ye87143712020-11-10 05:05:58 +0000554 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800555 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -0700556 return device != nullptr ? device->relBitmask.test(axis) : false;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800557 }
558 return false;
559}
560
561bool EventHub::hasInputProperty(int32_t deviceId, int property) const {
Chris Ye87143712020-11-10 05:05:58 +0000562 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800563
Chris Ye989bb932020-07-04 16:18:59 -0700564 Device* device = getDeviceLocked(deviceId);
565 return property >= 0 && property <= INPUT_PROP_MAX && device != nullptr
566 ? device->propBitmask.test(property)
567 : false;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800568}
569
Chris Yef59a2f42020-10-16 12:55:26 -0700570bool EventHub::hasMscEvent(int32_t deviceId, int mscEvent) const {
571 std::scoped_lock _l(mLock);
572
573 Device* device = getDeviceLocked(deviceId);
574 return mscEvent >= 0 && mscEvent <= MSC_MAX && device != nullptr
575 ? device->mscBitmask.test(mscEvent)
576 : false;
577}
578
Michael Wrightd02c5b62014-02-10 15:10:22 -0800579int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const {
580 if (scanCode >= 0 && scanCode <= KEY_MAX) {
Chris Ye87143712020-11-10 05:05:58 +0000581 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800582
583 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -0700584 if (device != nullptr && device->hasValidFd() && device->keyBitmask.test(scanCode)) {
585 if (device->readDeviceBitMask(EVIOCGKEY(0), device->keyState) >= 0) {
586 return device->keyState.test(scanCode) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800587 }
588 }
589 }
590 return AKEY_STATE_UNKNOWN;
591}
592
593int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
Chris Ye87143712020-11-10 05:05:58 +0000594 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800595
596 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -0700597 if (device != nullptr && device->hasValidFd() && device->keyMap.haveKeyLayout()) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800598 std::vector<int32_t> scanCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800599 device->keyMap.keyLayoutMap->findScanCodesForKey(keyCode, &scanCodes);
600 if (scanCodes.size() != 0) {
Chris Ye66fbac32020-07-06 20:36:43 -0700601 if (device->readDeviceBitMask(EVIOCGKEY(0), device->keyState) >= 0) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800602 for (size_t i = 0; i < scanCodes.size(); i++) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800603 int32_t sc = scanCodes[i];
Chris Ye66fbac32020-07-06 20:36:43 -0700604 if (sc >= 0 && sc <= KEY_MAX && device->keyState.test(sc)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800605 return AKEY_STATE_DOWN;
606 }
607 }
608 return AKEY_STATE_UP;
609 }
610 }
611 }
612 return AKEY_STATE_UNKNOWN;
613}
614
615int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const {
616 if (sw >= 0 && sw <= SW_MAX) {
Chris Ye87143712020-11-10 05:05:58 +0000617 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800618
619 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -0700620 if (device != nullptr && device->hasValidFd() && device->swBitmask.test(sw)) {
621 if (device->readDeviceBitMask(EVIOCGSW(0), device->swState) >= 0) {
622 return device->swState.test(sw) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800623 }
624 }
625 }
626 return AKEY_STATE_UNKNOWN;
627}
628
629status_t EventHub::getAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t* outValue) const {
630 *outValue = 0;
631
632 if (axis >= 0 && axis <= ABS_MAX) {
Chris Ye87143712020-11-10 05:05:58 +0000633 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800634
635 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -0700636 if (device != nullptr && device->hasValidFd() && device->absBitmask.test(axis)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800637 struct input_absinfo info;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700638 if (ioctl(device->fd, EVIOCGABS(axis), &info)) {
639 ALOGW("Error reading absolute controller %d for device %s fd %d, errno=%d", axis,
640 device->identifier.name.c_str(), device->fd, errno);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800641 return -errno;
642 }
643
644 *outValue = info.value;
645 return OK;
646 }
647 }
648 return -1;
649}
650
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700651bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
652 uint8_t* outFlags) const {
Chris Ye87143712020-11-10 05:05:58 +0000653 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800654
655 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -0700656 if (device != nullptr && device->keyMap.haveKeyLayout()) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800657 std::vector<int32_t> scanCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800658 for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) {
659 scanCodes.clear();
660
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700661 status_t err = device->keyMap.keyLayoutMap->findScanCodesForKey(keyCodes[codeIndex],
662 &scanCodes);
663 if (!err) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800664 // check the possible scan codes identified by the layout map against the
665 // map of codes actually emitted by the driver
666 for (size_t sc = 0; sc < scanCodes.size(); sc++) {
Chris Ye66fbac32020-07-06 20:36:43 -0700667 if (device->keyBitmask.test(scanCodes[sc])) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800668 outFlags[codeIndex] = 1;
669 break;
670 }
671 }
672 }
673 }
674 return true;
675 }
676 return false;
677}
678
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700679status_t EventHub::mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
680 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const {
Chris Ye87143712020-11-10 05:05:58 +0000681 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800682 Device* device = getDeviceLocked(deviceId);
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700683 status_t status = NAME_NOT_FOUND;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800684
Chris Ye66fbac32020-07-06 20:36:43 -0700685 if (device != nullptr) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800686 // Check the key character map first.
Chris Ye3a1e4462020-08-12 10:13:15 -0700687 const std::shared_ptr<KeyCharacterMap> kcm = device->getKeyCharacterMap();
688 if (kcm) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800689 if (!kcm->mapKey(scanCode, usageCode, outKeycode)) {
690 *outFlags = 0;
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700691 status = NO_ERROR;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800692 }
693 }
694
695 // Check the key layout next.
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700696 if (status != NO_ERROR && device->keyMap.haveKeyLayout()) {
Siarhei Vishniakou592bac22018-11-08 19:42:38 -0800697 if (!device->keyMap.keyLayoutMap->mapKey(scanCode, usageCode, outKeycode, outFlags)) {
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700698 status = NO_ERROR;
699 }
700 }
701
702 if (status == NO_ERROR) {
Chris Ye3a1e4462020-08-12 10:13:15 -0700703 if (kcm) {
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700704 kcm->tryRemapKey(*outKeycode, metaState, outKeycode, outMetaState);
705 } else {
706 *outMetaState = metaState;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800707 }
708 }
709 }
710
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700711 if (status != NO_ERROR) {
712 *outKeycode = 0;
713 *outFlags = 0;
714 *outMetaState = metaState;
715 }
716
717 return status;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800718}
719
720status_t EventHub::mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const {
Chris Ye87143712020-11-10 05:05:58 +0000721 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800722 Device* device = getDeviceLocked(deviceId);
723
Chris Ye66fbac32020-07-06 20:36:43 -0700724 if (device != nullptr && device->keyMap.haveKeyLayout()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800725 status_t err = device->keyMap.keyLayoutMap->mapAxis(scanCode, outAxisInfo);
726 if (err == NO_ERROR) {
727 return NO_ERROR;
728 }
729 }
730
731 return NAME_NOT_FOUND;
732}
733
Chris Yef59a2f42020-10-16 12:55:26 -0700734base::Result<std::pair<InputDeviceSensorType, int32_t>> EventHub::mapSensor(int32_t deviceId,
735 int32_t absCode) {
736 std::scoped_lock _l(mLock);
737 Device* device = getDeviceLocked(deviceId);
738
739 if (device != nullptr && device->keyMap.haveKeyLayout()) {
740 return device->keyMap.keyLayoutMap->mapSensor(absCode);
741 }
742 return Errorf("Device not found or device has no key layout.");
743}
744
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100745void EventHub::setExcludedDevices(const std::vector<std::string>& devices) {
Chris Ye87143712020-11-10 05:05:58 +0000746 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800747
748 mExcludedDevices = devices;
749}
750
751bool EventHub::hasScanCode(int32_t deviceId, int32_t scanCode) const {
Chris Ye87143712020-11-10 05:05:58 +0000752 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800753 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -0700754 if (device != nullptr && scanCode >= 0 && scanCode <= KEY_MAX) {
755 return device->keyBitmask.test(scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800756 }
757 return false;
758}
759
760bool EventHub::hasLed(int32_t deviceId, int32_t led) const {
Chris Ye87143712020-11-10 05:05:58 +0000761 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800762 Device* device = getDeviceLocked(deviceId);
763 int32_t sc;
Chris Ye989bb932020-07-04 16:18:59 -0700764 if (device != nullptr && device->mapLed(led, &sc) == NO_ERROR) {
Chris Ye66fbac32020-07-06 20:36:43 -0700765 return device->ledBitmask.test(sc);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800766 }
767 return false;
768}
769
770void EventHub::setLedState(int32_t deviceId, int32_t led, bool on) {
Chris Ye87143712020-11-10 05:05:58 +0000771 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800772 Device* device = getDeviceLocked(deviceId);
Chris Ye989bb932020-07-04 16:18:59 -0700773 if (device != nullptr && device->hasValidFd()) {
774 device->setLedStateLocked(led, on);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800775 }
776}
777
778void EventHub::getVirtualKeyDefinitions(int32_t deviceId,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700779 std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800780 outVirtualKeys.clear();
781
Chris Ye87143712020-11-10 05:05:58 +0000782 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800783 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -0700784 if (device != nullptr && device->virtualKeyMap) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800785 const std::vector<VirtualKeyDefinition> virtualKeys =
786 device->virtualKeyMap->getVirtualKeys();
787 outVirtualKeys.insert(outVirtualKeys.end(), virtualKeys.begin(), virtualKeys.end());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800788 }
789}
790
Chris Ye3a1e4462020-08-12 10:13:15 -0700791const std::shared_ptr<KeyCharacterMap> EventHub::getKeyCharacterMap(int32_t deviceId) const {
Chris Ye87143712020-11-10 05:05:58 +0000792 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800793 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -0700794 if (device != nullptr) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800795 return device->getKeyCharacterMap();
796 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700797 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800798}
799
Chris Ye3a1e4462020-08-12 10:13:15 -0700800bool EventHub::setKeyboardLayoutOverlay(int32_t deviceId, std::shared_ptr<KeyCharacterMap> map) {
Chris Ye87143712020-11-10 05:05:58 +0000801 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800802 Device* device = getDeviceLocked(deviceId);
Chris Ye3a1e4462020-08-12 10:13:15 -0700803 if (device != nullptr && map != nullptr && device->keyMap.keyCharacterMap != nullptr) {
804 device->keyMap.keyCharacterMap->combine(*map);
805 device->keyMap.keyCharacterMapFile = device->keyMap.keyCharacterMap->getLoadFileName();
806 return true;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800807 }
808 return false;
809}
810
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100811static std::string generateDescriptor(InputDeviceIdentifier& identifier) {
812 std::string rawDescriptor;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700813 rawDescriptor += StringPrintf(":%04x:%04x:", identifier.vendor, identifier.product);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800814 // TODO add handling for USB devices to not uniqueify kbs that show up twice
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100815 if (!identifier.uniqueId.empty()) {
816 rawDescriptor += "uniqueId:";
817 rawDescriptor += identifier.uniqueId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800818 } else if (identifier.nonce != 0) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100819 rawDescriptor += StringPrintf("nonce:%04x", identifier.nonce);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800820 }
821
822 if (identifier.vendor == 0 && identifier.product == 0) {
823 // If we don't know the vendor and product id, then the device is probably
824 // built-in so we need to rely on other information to uniquely identify
825 // the input device. Usually we try to avoid relying on the device name or
826 // location but for built-in input device, they are unlikely to ever change.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100827 if (!identifier.name.empty()) {
828 rawDescriptor += "name:";
829 rawDescriptor += identifier.name;
830 } else if (!identifier.location.empty()) {
831 rawDescriptor += "location:";
832 rawDescriptor += identifier.location;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800833 }
834 }
835 identifier.descriptor = sha1(rawDescriptor);
836 return rawDescriptor;
837}
838
839void EventHub::assignDescriptorLocked(InputDeviceIdentifier& identifier) {
840 // Compute a device descriptor that uniquely identifies the device.
841 // The descriptor is assumed to be a stable identifier. Its value should not
842 // change between reboots, reconnections, firmware updates or new releases
843 // of Android. In practice we sometimes get devices that cannot be uniquely
844 // identified. In this case we enforce uniqueness between connected devices.
845 // Ideally, we also want the descriptor to be short and relatively opaque.
846
847 identifier.nonce = 0;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100848 std::string rawDescriptor = generateDescriptor(identifier);
849 if (identifier.uniqueId.empty()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800850 // If it didn't have a unique id check for conflicts and enforce
851 // uniqueness if necessary.
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700852 while (getDeviceByDescriptorLocked(identifier.descriptor) != nullptr) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800853 identifier.nonce++;
854 rawDescriptor = generateDescriptor(identifier);
855 }
856 }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100857 ALOGV("Created descriptor: raw=%s, cooked=%s", rawDescriptor.c_str(),
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700858 identifier.descriptor.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800859}
860
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000861void EventHub::vibrate(int32_t deviceId, const VibrationElement& element) {
Chris Ye87143712020-11-10 05:05:58 +0000862 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800863 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -0700864 if (device != nullptr && device->hasValidFd()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800865 ff_effect effect;
866 memset(&effect, 0, sizeof(effect));
867 effect.type = FF_RUMBLE;
868 effect.id = device->ffEffectId;
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000869 // evdev FF_RUMBLE effect only supports two channels of vibration.
Chris Ye6393a262020-08-04 19:41:36 -0700870 effect.u.rumble.strong_magnitude = element.getMagnitude(FF_STRONG_MAGNITUDE_CHANNEL_IDX);
871 effect.u.rumble.weak_magnitude = element.getMagnitude(FF_WEAK_MAGNITUDE_CHANNEL_IDX);
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000872 effect.replay.length = element.duration.count();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800873 effect.replay.delay = 0;
874 if (ioctl(device->fd, EVIOCSFF, &effect)) {
875 ALOGW("Could not upload force feedback effect to device %s due to error %d.",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700876 device->identifier.name.c_str(), errno);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800877 return;
878 }
879 device->ffEffectId = effect.id;
880
881 struct input_event ev;
882 ev.time.tv_sec = 0;
883 ev.time.tv_usec = 0;
884 ev.type = EV_FF;
885 ev.code = device->ffEffectId;
886 ev.value = 1;
887 if (write(device->fd, &ev, sizeof(ev)) != sizeof(ev)) {
888 ALOGW("Could not start force feedback effect on device %s due to error %d.",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700889 device->identifier.name.c_str(), errno);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800890 return;
891 }
892 device->ffEffectPlaying = true;
893 }
894}
895
896void EventHub::cancelVibrate(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +0000897 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800898 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -0700899 if (device != nullptr && device->hasValidFd()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800900 if (device->ffEffectPlaying) {
901 device->ffEffectPlaying = false;
902
903 struct input_event ev;
904 ev.time.tv_sec = 0;
905 ev.time.tv_usec = 0;
906 ev.type = EV_FF;
907 ev.code = device->ffEffectId;
908 ev.value = 0;
909 if (write(device->fd, &ev, sizeof(ev)) != sizeof(ev)) {
910 ALOGW("Could not stop force feedback effect on device %s due to error %d.",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700911 device->identifier.name.c_str(), errno);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800912 return;
913 }
914 }
915 }
916}
917
Chris Ye87143712020-11-10 05:05:58 +0000918std::vector<int32_t> EventHub::getVibratorIds(int32_t deviceId) {
919 std::scoped_lock _l(mLock);
920 std::vector<int32_t> vibrators;
921 Device* device = getDeviceLocked(deviceId);
922 if (device != nullptr && device->hasValidFd() &&
923 device->classes.test(InputDeviceClass::VIBRATOR)) {
924 vibrators.push_back(FF_STRONG_MAGNITUDE_CHANNEL_IDX);
925 vibrators.push_back(FF_WEAK_MAGNITUDE_CHANNEL_IDX);
926 }
927 return vibrators;
928}
929
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100930EventHub::Device* EventHub::getDeviceByDescriptorLocked(const std::string& descriptor) const {
Chris Ye989bb932020-07-04 16:18:59 -0700931 for (const auto& [id, device] : mDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100932 if (descriptor == device->identifier.descriptor) {
Chris Ye989bb932020-07-04 16:18:59 -0700933 return device.get();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800934 }
935 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700936 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800937}
938
939EventHub::Device* EventHub::getDeviceLocked(int32_t deviceId) const {
Prabir Pradhancae4b3a2019-02-05 18:51:32 -0800940 if (deviceId == ReservedInputDeviceId::BUILT_IN_KEYBOARD_ID) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800941 deviceId = mBuiltInKeyboardId;
942 }
Chris Ye989bb932020-07-04 16:18:59 -0700943 const auto& it = mDevices.find(deviceId);
944 return it != mDevices.end() ? it->second.get() : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800945}
946
Chris Ye8594e192020-07-14 10:34:06 -0700947EventHub::Device* EventHub::getDeviceByPathLocked(const std::string& devicePath) const {
Chris Ye989bb932020-07-04 16:18:59 -0700948 for (const auto& [id, device] : mDevices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800949 if (device->path == devicePath) {
Chris Ye989bb932020-07-04 16:18:59 -0700950 return device.get();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800951 }
952 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700953 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800954}
955
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700956/**
957 * The file descriptor could be either input device, or a video device (associated with a
958 * specific input device). Check both cases here, and return the device that this event
959 * belongs to. Caller can compare the fd's once more to determine event type.
960 * Looks through all input devices, and only attached video devices. Unattached video
961 * devices are ignored.
962 */
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -0700963EventHub::Device* EventHub::getDeviceByFdLocked(int fd) const {
Chris Ye989bb932020-07-04 16:18:59 -0700964 for (const auto& [id, device] : mDevices) {
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -0700965 if (device->fd == fd) {
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700966 // This is an input device event
Chris Ye989bb932020-07-04 16:18:59 -0700967 return device.get();
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700968 }
969 if (device->videoDevice && device->videoDevice->getFd() == fd) {
970 // This is a video device event
Chris Ye989bb932020-07-04 16:18:59 -0700971 return device.get();
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -0700972 }
973 }
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700974 // We do not check mUnattachedVideoDevices here because they should not participate in epoll,
975 // and therefore should never be looked up by fd.
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -0700976 return nullptr;
977}
978
Michael Wrightd02c5b62014-02-10 15:10:22 -0800979size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) {
980 ALOG_ASSERT(bufferSize >= 1);
981
Chris Ye87143712020-11-10 05:05:58 +0000982 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800983
984 struct input_event readBuffer[bufferSize];
985
986 RawEvent* event = buffer;
987 size_t capacity = bufferSize;
988 bool awoken = false;
989 for (;;) {
990 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
991
992 // Reopen input devices if needed.
993 if (mNeedToReopenDevices) {
994 mNeedToReopenDevices = false;
995
996 ALOGI("Reopening all input devices due to a configuration change.");
997
998 closeAllDevicesLocked();
999 mNeedToScanDevices = true;
1000 break; // return to the caller before we actually rescan
1001 }
1002
1003 // Report any devices that had last been added/removed.
Chris Ye989bb932020-07-04 16:18:59 -07001004 for (auto it = mClosingDevices.begin(); it != mClosingDevices.end();) {
1005 std::unique_ptr<Device> device = std::move(*it);
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001006 ALOGV("Reporting device closed: id=%d, name=%s\n", device->id, device->path.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001007 event->when = now;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001008 event->deviceId = (device->id == mBuiltInKeyboardId)
1009 ? ReservedInputDeviceId::BUILT_IN_KEYBOARD_ID
1010 : device->id;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001011 event->type = DEVICE_REMOVED;
1012 event += 1;
Chris Ye989bb932020-07-04 16:18:59 -07001013 it = mClosingDevices.erase(it);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001014 mNeedToSendFinishedDeviceScan = true;
1015 if (--capacity == 0) {
1016 break;
1017 }
1018 }
1019
1020 if (mNeedToScanDevices) {
1021 mNeedToScanDevices = false;
1022 scanDevicesLocked();
1023 mNeedToSendFinishedDeviceScan = true;
1024 }
1025
Siarhei Vishniakouf49608d2020-08-20 19:18:21 -05001026 while (!mOpeningDevices.empty()) {
1027 std::unique_ptr<Device> device = std::move(*mOpeningDevices.rbegin());
1028 mOpeningDevices.pop_back();
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001029 ALOGV("Reporting device opened: id=%d, name=%s\n", device->id, device->path.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001030 event->when = now;
1031 event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
1032 event->type = DEVICE_ADDED;
1033 event += 1;
Siarhei Vishniakouf49608d2020-08-20 19:18:21 -05001034
1035 // Try to find a matching video device by comparing device names
1036 for (auto it = mUnattachedVideoDevices.begin(); it != mUnattachedVideoDevices.end();
1037 it++) {
1038 std::unique_ptr<TouchVideoDevice>& videoDevice = *it;
1039 if (tryAddVideoDevice(*device, videoDevice)) {
1040 // videoDevice was transferred to 'device'
1041 it = mUnattachedVideoDevices.erase(it);
1042 break;
1043 }
1044 }
1045
1046 auto [dev_it, inserted] = mDevices.insert_or_assign(device->id, std::move(device));
1047 if (!inserted) {
Chris Ye989bb932020-07-04 16:18:59 -07001048 ALOGW("Device id %d exists, replaced.", device->id);
1049 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001050 mNeedToSendFinishedDeviceScan = true;
1051 if (--capacity == 0) {
1052 break;
1053 }
1054 }
1055
1056 if (mNeedToSendFinishedDeviceScan) {
1057 mNeedToSendFinishedDeviceScan = false;
1058 event->when = now;
1059 event->type = FINISHED_DEVICE_SCAN;
1060 event += 1;
1061 if (--capacity == 0) {
1062 break;
1063 }
1064 }
1065
1066 // Grab the next input event.
1067 bool deviceChanged = false;
1068 while (mPendingEventIndex < mPendingEventCount) {
1069 const struct epoll_event& eventItem = mPendingEventItems[mPendingEventIndex++];
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -07001070 if (eventItem.data.fd == mINotifyFd) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001071 if (eventItem.events & EPOLLIN) {
1072 mPendingINotify = true;
1073 } else {
1074 ALOGW("Received unexpected epoll event 0x%08x for INotify.", eventItem.events);
1075 }
1076 continue;
1077 }
1078
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -07001079 if (eventItem.data.fd == mWakeReadPipeFd) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001080 if (eventItem.events & EPOLLIN) {
1081 ALOGV("awoken after wake()");
1082 awoken = true;
Siarhei Vishniakoub4d960d2019-10-03 15:38:44 -05001083 char wakeReadBuffer[16];
Michael Wrightd02c5b62014-02-10 15:10:22 -08001084 ssize_t nRead;
1085 do {
Siarhei Vishniakoub4d960d2019-10-03 15:38:44 -05001086 nRead = read(mWakeReadPipeFd, wakeReadBuffer, sizeof(wakeReadBuffer));
1087 } while ((nRead == -1 && errno == EINTR) || nRead == sizeof(wakeReadBuffer));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001088 } else {
1089 ALOGW("Received unexpected epoll event 0x%08x for wake read pipe.",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001090 eventItem.events);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001091 }
1092 continue;
1093 }
1094
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -07001095 Device* device = getDeviceByFdLocked(eventItem.data.fd);
Chris Ye989bb932020-07-04 16:18:59 -07001096 if (device == nullptr) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001097 ALOGE("Received unexpected epoll event 0x%08x for unknown fd %d.", eventItem.events,
1098 eventItem.data.fd);
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001099 ALOG_ASSERT(!DEBUG);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001100 continue;
1101 }
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001102 if (device->videoDevice && eventItem.data.fd == device->videoDevice->getFd()) {
1103 if (eventItem.events & EPOLLIN) {
1104 size_t numFrames = device->videoDevice->readAndQueueFrames();
1105 if (numFrames == 0) {
1106 ALOGE("Received epoll event for video device %s, but could not read frame",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001107 device->videoDevice->getName().c_str());
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001108 }
1109 } else if (eventItem.events & EPOLLHUP) {
1110 // TODO(b/121395353) - consider adding EPOLLRDHUP
1111 ALOGI("Removing video device %s due to epoll hang-up event.",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001112 device->videoDevice->getName().c_str());
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001113 unregisterVideoDeviceFromEpollLocked(*device->videoDevice);
1114 device->videoDevice = nullptr;
1115 } else {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001116 ALOGW("Received unexpected epoll event 0x%08x for device %s.", eventItem.events,
1117 device->videoDevice->getName().c_str());
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001118 ALOG_ASSERT(!DEBUG);
1119 }
1120 continue;
1121 }
1122 // This must be an input event
Michael Wrightd02c5b62014-02-10 15:10:22 -08001123 if (eventItem.events & EPOLLIN) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001124 int32_t readSize =
1125 read(device->fd, readBuffer, sizeof(struct input_event) * capacity);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001126 if (readSize == 0 || (readSize < 0 && errno == ENODEV)) {
1127 // Device was removed before INotify noticed.
Mark Salyzyn5aa26b22014-06-10 13:07:44 -07001128 ALOGW("could not get event, removed? (fd: %d size: %" PRId32
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001129 " bufferSize: %zu capacity: %zu errno: %d)\n",
1130 device->fd, readSize, bufferSize, capacity, errno);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001131 deviceChanged = true;
Chris Ye989bb932020-07-04 16:18:59 -07001132 closeDeviceLocked(*device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001133 } else if (readSize < 0) {
1134 if (errno != EAGAIN && errno != EINTR) {
1135 ALOGW("could not get event (errno=%d)", errno);
1136 }
1137 } else if ((readSize % sizeof(struct input_event)) != 0) {
1138 ALOGE("could not get event (wrong size: %d)", readSize);
1139 } else {
1140 int32_t deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
1141
1142 size_t count = size_t(readSize) / sizeof(struct input_event);
1143 for (size_t i = 0; i < count; i++) {
1144 struct input_event& iev = readBuffer[i];
Siarhei Vishniakou592bac22018-11-08 19:42:38 -08001145 event->when = processEventTimestamp(iev);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001146 event->deviceId = deviceId;
1147 event->type = iev.type;
1148 event->code = iev.code;
1149 event->value = iev.value;
1150 event += 1;
1151 capacity -= 1;
1152 }
1153 if (capacity == 0) {
1154 // The result buffer is full. Reset the pending event index
1155 // so we will try to read the device again on the next iteration.
1156 mPendingEventIndex -= 1;
1157 break;
1158 }
1159 }
1160 } else if (eventItem.events & EPOLLHUP) {
1161 ALOGI("Removing device %s due to epoll hang-up event.",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001162 device->identifier.name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001163 deviceChanged = true;
Chris Ye989bb932020-07-04 16:18:59 -07001164 closeDeviceLocked(*device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001165 } else {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001166 ALOGW("Received unexpected epoll event 0x%08x for device %s.", eventItem.events,
1167 device->identifier.name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001168 }
1169 }
1170
1171 // readNotify() will modify the list of devices so this must be done after
1172 // processing all other events to ensure that we read all remaining events
1173 // before closing the devices.
1174 if (mPendingINotify && mPendingEventIndex >= mPendingEventCount) {
1175 mPendingINotify = false;
1176 readNotifyLocked();
1177 deviceChanged = true;
1178 }
1179
1180 // Report added or removed devices immediately.
1181 if (deviceChanged) {
1182 continue;
1183 }
1184
1185 // Return now if we have collected any events or if we were explicitly awoken.
1186 if (event != buffer || awoken) {
1187 break;
1188 }
1189
Siarhei Vishniakou4b5a87f2019-09-24 13:03:58 +01001190 // Poll for events.
1191 // When a device driver has pending (unread) events, it acquires
1192 // a kernel wake lock. Once the last pending event has been read, the device
1193 // driver will release the kernel wake lock, but the epoll will hold the wakelock,
1194 // since we are using EPOLLWAKEUP. The wakelock is released by the epoll when epoll_wait
1195 // is called again for the same fd that produced the event.
1196 // Thus the system can only sleep if there are no events pending or
1197 // currently being processed.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001198 //
1199 // The timeout is advisory only. If the device is asleep, it will not wake just to
1200 // service the timeout.
1201 mPendingEventIndex = 0;
1202
Siarhei Vishniakou4b5a87f2019-09-24 13:03:58 +01001203 mLock.unlock(); // release lock before poll
Michael Wrightd02c5b62014-02-10 15:10:22 -08001204
1205 int pollResult = epoll_wait(mEpollFd, mPendingEventItems, EPOLL_MAX_EVENTS, timeoutMillis);
1206
Siarhei Vishniakou4b5a87f2019-09-24 13:03:58 +01001207 mLock.lock(); // reacquire lock after poll
Michael Wrightd02c5b62014-02-10 15:10:22 -08001208
1209 if (pollResult == 0) {
1210 // Timed out.
1211 mPendingEventCount = 0;
1212 break;
1213 }
1214
1215 if (pollResult < 0) {
1216 // An error occurred.
1217 mPendingEventCount = 0;
1218
1219 // Sleep after errors to avoid locking up the system.
1220 // Hopefully the error is transient.
1221 if (errno != EINTR) {
1222 ALOGW("poll failed (errno=%d)\n", errno);
1223 usleep(100000);
1224 }
1225 } else {
1226 // Some events occurred.
1227 mPendingEventCount = size_t(pollResult);
1228 }
1229 }
1230
1231 // All done, return the number of events we read.
1232 return event - buffer;
1233}
1234
Siarhei Vishniakouadd89292018-12-13 19:23:36 -08001235std::vector<TouchVideoFrame> EventHub::getVideoFrames(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +00001236 std::scoped_lock _l(mLock);
Siarhei Vishniakouadd89292018-12-13 19:23:36 -08001237
1238 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -07001239 if (device == nullptr || !device->videoDevice) {
Siarhei Vishniakouadd89292018-12-13 19:23:36 -08001240 return {};
1241 }
1242 return device->videoDevice->consumeFrames();
1243}
1244
Michael Wrightd02c5b62014-02-10 15:10:22 -08001245void EventHub::wake() {
1246 ALOGV("wake() called");
1247
1248 ssize_t nWrite;
1249 do {
1250 nWrite = write(mWakeWritePipeFd, "W", 1);
1251 } while (nWrite == -1 && errno == EINTR);
1252
1253 if (nWrite != 1 && errno != EAGAIN) {
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001254 ALOGW("Could not write wake signal: %s", strerror(errno));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001255 }
1256}
1257
1258void EventHub::scanDevicesLocked() {
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001259 status_t result = scanDirLocked(DEVICE_PATH);
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001260 if (result < 0) {
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001261 ALOGE("scan dir failed for %s", DEVICE_PATH);
1262 }
Philip Quinn39b81682019-01-09 22:20:39 -08001263 if (isV4lScanningEnabled()) {
1264 result = scanVideoDirLocked(VIDEO_DEVICE_PATH);
1265 if (result != OK) {
1266 ALOGE("scan video dir failed for %s", VIDEO_DEVICE_PATH);
1267 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001268 }
Chris Ye989bb932020-07-04 16:18:59 -07001269 if (mDevices.find(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID) == mDevices.end()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001270 createVirtualKeyboardLocked();
1271 }
1272}
1273
1274// ----------------------------------------------------------------------------
1275
Michael Wrightd02c5b62014-02-10 15:10:22 -08001276static const int32_t GAMEPAD_KEYCODES[] = {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001277 AKEYCODE_BUTTON_A, AKEYCODE_BUTTON_B, AKEYCODE_BUTTON_C, //
1278 AKEYCODE_BUTTON_X, AKEYCODE_BUTTON_Y, AKEYCODE_BUTTON_Z, //
1279 AKEYCODE_BUTTON_L1, AKEYCODE_BUTTON_R1, //
1280 AKEYCODE_BUTTON_L2, AKEYCODE_BUTTON_R2, //
1281 AKEYCODE_BUTTON_THUMBL, AKEYCODE_BUTTON_THUMBR, //
1282 AKEYCODE_BUTTON_START, AKEYCODE_BUTTON_SELECT, AKEYCODE_BUTTON_MODE, //
Michael Wrightd02c5b62014-02-10 15:10:22 -08001283};
1284
Siarhei Vishniakou25920312018-12-12 15:24:44 -08001285status_t EventHub::registerFdForEpoll(int fd) {
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001286 // TODO(b/121395353) - consider adding EPOLLRDHUP
Siarhei Vishniakou25920312018-12-12 15:24:44 -08001287 struct epoll_event eventItem = {};
1288 eventItem.events = EPOLLIN | EPOLLWAKEUP;
1289 eventItem.data.fd = fd;
1290 if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, &eventItem)) {
1291 ALOGE("Could not add fd to epoll instance: %s", strerror(errno));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001292 return -errno;
1293 }
1294 return OK;
1295}
1296
Siarhei Vishniakou25920312018-12-12 15:24:44 -08001297status_t EventHub::unregisterFdFromEpoll(int fd) {
1298 if (epoll_ctl(mEpollFd, EPOLL_CTL_DEL, fd, nullptr)) {
1299 ALOGW("Could not remove fd from epoll instance: %s", strerror(errno));
1300 return -errno;
1301 }
1302 return OK;
1303}
1304
Chris Ye989bb932020-07-04 16:18:59 -07001305status_t EventHub::registerDeviceForEpollLocked(Device& device) {
1306 status_t result = registerFdForEpoll(device.fd);
Siarhei Vishniakou25920312018-12-12 15:24:44 -08001307 if (result != OK) {
Chris Ye989bb932020-07-04 16:18:59 -07001308 ALOGE("Could not add input device fd to epoll for device %" PRId32, device.id);
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001309 return result;
1310 }
Chris Ye989bb932020-07-04 16:18:59 -07001311 if (device.videoDevice) {
1312 registerVideoDeviceForEpollLocked(*device.videoDevice);
Siarhei Vishniakou25920312018-12-12 15:24:44 -08001313 }
1314 return result;
1315}
1316
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001317void EventHub::registerVideoDeviceForEpollLocked(const TouchVideoDevice& videoDevice) {
1318 status_t result = registerFdForEpoll(videoDevice.getFd());
1319 if (result != OK) {
1320 ALOGE("Could not add video device %s to epoll", videoDevice.getName().c_str());
1321 }
1322}
1323
Chris Ye989bb932020-07-04 16:18:59 -07001324status_t EventHub::unregisterDeviceFromEpollLocked(Device& device) {
1325 if (device.hasValidFd()) {
1326 status_t result = unregisterFdFromEpoll(device.fd);
Siarhei Vishniakou25920312018-12-12 15:24:44 -08001327 if (result != OK) {
Chris Ye989bb932020-07-04 16:18:59 -07001328 ALOGW("Could not remove input device fd from epoll for device %" PRId32, device.id);
Siarhei Vishniakou25920312018-12-12 15:24:44 -08001329 return result;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001330 }
1331 }
Chris Ye989bb932020-07-04 16:18:59 -07001332 if (device.videoDevice) {
1333 unregisterVideoDeviceFromEpollLocked(*device.videoDevice);
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001334 }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001335 return OK;
1336}
1337
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001338void EventHub::unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& videoDevice) {
1339 if (videoDevice.hasValidFd()) {
1340 status_t result = unregisterFdFromEpoll(videoDevice.getFd());
1341 if (result != OK) {
1342 ALOGW("Could not remove video device fd from epoll for device: %s",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001343 videoDevice.getName().c_str());
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001344 }
1345 }
1346}
1347
Chris Ye8594e192020-07-14 10:34:06 -07001348status_t EventHub::openDeviceLocked(const std::string& devicePath) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001349 char buffer[80];
1350
Chris Ye8594e192020-07-14 10:34:06 -07001351 ALOGV("Opening device: %s", devicePath.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001352
Chris Ye8594e192020-07-14 10:34:06 -07001353 int fd = open(devicePath.c_str(), O_RDWR | O_CLOEXEC | O_NONBLOCK);
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001354 if (fd < 0) {
Chris Ye8594e192020-07-14 10:34:06 -07001355 ALOGE("could not open %s, %s\n", devicePath.c_str(), strerror(errno));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001356 return -1;
1357 }
1358
1359 InputDeviceIdentifier identifier;
1360
1361 // Get device name.
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001362 if (ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) {
Chris Ye8594e192020-07-14 10:34:06 -07001363 ALOGE("Could not get device name for %s: %s", devicePath.c_str(), strerror(errno));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001364 } else {
1365 buffer[sizeof(buffer) - 1] = '\0';
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001366 identifier.name = buffer;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001367 }
1368
1369 // Check to see if the device is on our excluded list
1370 for (size_t i = 0; i < mExcludedDevices.size(); i++) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001371 const std::string& item = mExcludedDevices[i];
Michael Wrightd02c5b62014-02-10 15:10:22 -08001372 if (identifier.name == item) {
Chris Ye8594e192020-07-14 10:34:06 -07001373 ALOGI("ignoring event id %s driver %s\n", devicePath.c_str(), item.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001374 close(fd);
1375 return -1;
1376 }
1377 }
1378
1379 // Get device driver version.
1380 int driverVersion;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001381 if (ioctl(fd, EVIOCGVERSION, &driverVersion)) {
Chris Ye8594e192020-07-14 10:34:06 -07001382 ALOGE("could not get driver version for %s, %s\n", devicePath.c_str(), strerror(errno));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001383 close(fd);
1384 return -1;
1385 }
1386
1387 // Get device identifier.
1388 struct input_id inputId;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001389 if (ioctl(fd, EVIOCGID, &inputId)) {
Chris Ye8594e192020-07-14 10:34:06 -07001390 ALOGE("could not get device input id for %s, %s\n", devicePath.c_str(), strerror(errno));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001391 close(fd);
1392 return -1;
1393 }
1394 identifier.bus = inputId.bustype;
1395 identifier.product = inputId.product;
1396 identifier.vendor = inputId.vendor;
1397 identifier.version = inputId.version;
1398
1399 // Get device physical location.
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001400 if (ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) {
1401 // fprintf(stderr, "could not get location for %s, %s\n", devicePath, strerror(errno));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001402 } else {
1403 buffer[sizeof(buffer) - 1] = '\0';
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001404 identifier.location = buffer;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001405 }
1406
1407 // Get device unique id.
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001408 if (ioctl(fd, EVIOCGUNIQ(sizeof(buffer) - 1), &buffer) < 1) {
1409 // fprintf(stderr, "could not get idstring for %s, %s\n", devicePath, strerror(errno));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001410 } else {
1411 buffer[sizeof(buffer) - 1] = '\0';
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001412 identifier.uniqueId = buffer;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001413 }
1414
1415 // Fill in the descriptor.
1416 assignDescriptorLocked(identifier);
1417
Michael Wrightd02c5b62014-02-10 15:10:22 -08001418 // Allocate device. (The device object takes ownership of the fd at this point.)
1419 int32_t deviceId = mNextDeviceId++;
Chris Ye989bb932020-07-04 16:18:59 -07001420 std::unique_ptr<Device> device = std::make_unique<Device>(fd, deviceId, devicePath, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001421
Chris Ye8594e192020-07-14 10:34:06 -07001422 ALOGV("add device %d: %s\n", deviceId, devicePath.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001423 ALOGV(" bus: %04x\n"
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001424 " vendor %04x\n"
1425 " product %04x\n"
1426 " version %04x\n",
1427 identifier.bus, identifier.vendor, identifier.product, identifier.version);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001428 ALOGV(" name: \"%s\"\n", identifier.name.c_str());
1429 ALOGV(" location: \"%s\"\n", identifier.location.c_str());
1430 ALOGV(" unique id: \"%s\"\n", identifier.uniqueId.c_str());
1431 ALOGV(" descriptor: \"%s\"\n", identifier.descriptor.c_str());
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001432 ALOGV(" driver: v%d.%d.%d\n", driverVersion >> 16, (driverVersion >> 8) & 0xff,
1433 driverVersion & 0xff);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001434
1435 // Load the configuration file for the device.
Chris Ye989bb932020-07-04 16:18:59 -07001436 device->loadConfigurationLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001437
1438 // Figure out the kinds of events the device reports.
Chris Ye66fbac32020-07-06 20:36:43 -07001439 device->readDeviceBitMask(EVIOCGBIT(EV_KEY, 0), device->keyBitmask);
1440 device->readDeviceBitMask(EVIOCGBIT(EV_ABS, 0), device->absBitmask);
1441 device->readDeviceBitMask(EVIOCGBIT(EV_REL, 0), device->relBitmask);
1442 device->readDeviceBitMask(EVIOCGBIT(EV_SW, 0), device->swBitmask);
1443 device->readDeviceBitMask(EVIOCGBIT(EV_LED, 0), device->ledBitmask);
1444 device->readDeviceBitMask(EVIOCGBIT(EV_FF, 0), device->ffBitmask);
Chris Yef59a2f42020-10-16 12:55:26 -07001445 device->readDeviceBitMask(EVIOCGBIT(EV_MSC, 0), device->mscBitmask);
Chris Ye66fbac32020-07-06 20:36:43 -07001446 device->readDeviceBitMask(EVIOCGPROP(0), device->propBitmask);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001447
1448 // See if this is a keyboard. Ignore everything in the button range except for
1449 // joystick and gamepad buttons which are handled like keyboards for the most part.
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001450 bool haveKeyboardKeys =
Chris Ye66fbac32020-07-06 20:36:43 -07001451 device->keyBitmask.any(0, BTN_MISC) || device->keyBitmask.any(BTN_WHEEL, KEY_MAX + 1);
1452 bool haveGamepadButtons = device->keyBitmask.any(BTN_MISC, BTN_MOUSE) ||
1453 device->keyBitmask.any(BTN_JOYSTICK, BTN_DIGI);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001454 if (haveKeyboardKeys || haveGamepadButtons) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001455 device->classes |= InputDeviceClass::KEYBOARD;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001456 }
1457
1458 // See if this is a cursor device such as a trackball or mouse.
Chris Ye66fbac32020-07-06 20:36:43 -07001459 if (device->keyBitmask.test(BTN_MOUSE) && device->relBitmask.test(REL_X) &&
1460 device->relBitmask.test(REL_Y)) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001461 device->classes |= InputDeviceClass::CURSOR;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001462 }
1463
Prashant Malani1941ff52015-08-11 18:29:28 -07001464 // See if this is a rotary encoder type device.
1465 String8 deviceType = String8();
1466 if (device->configuration &&
1467 device->configuration->tryGetProperty(String8("device.type"), deviceType)) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001468 if (!deviceType.compare(String8("rotaryEncoder"))) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001469 device->classes |= InputDeviceClass::ROTARY_ENCODER;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001470 }
Prashant Malani1941ff52015-08-11 18:29:28 -07001471 }
1472
Michael Wrightd02c5b62014-02-10 15:10:22 -08001473 // See if this is a touch pad.
1474 // Is this a new modern multi-touch driver?
Chris Ye66fbac32020-07-06 20:36:43 -07001475 if (device->absBitmask.test(ABS_MT_POSITION_X) && device->absBitmask.test(ABS_MT_POSITION_Y)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001476 // Some joysticks such as the PS3 controller report axes that conflict
1477 // with the ABS_MT range. Try to confirm that the device really is
1478 // a touch screen.
Chris Ye66fbac32020-07-06 20:36:43 -07001479 if (device->keyBitmask.test(BTN_TOUCH) || !haveGamepadButtons) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001480 device->classes |= (InputDeviceClass::TOUCH | InputDeviceClass::TOUCH_MT);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001481 }
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001482 // Is this an old style single-touch driver?
Chris Ye66fbac32020-07-06 20:36:43 -07001483 } else if (device->keyBitmask.test(BTN_TOUCH) && device->absBitmask.test(ABS_X) &&
1484 device->absBitmask.test(ABS_Y)) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001485 device->classes |= InputDeviceClass::TOUCH;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001486 // Is this a BT stylus?
Chris Ye66fbac32020-07-06 20:36:43 -07001487 } else if ((device->absBitmask.test(ABS_PRESSURE) || device->keyBitmask.test(BTN_TOUCH)) &&
1488 !device->absBitmask.test(ABS_X) && !device->absBitmask.test(ABS_Y)) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001489 device->classes |= InputDeviceClass::EXTERNAL_STYLUS;
Michael Wright842500e2015-03-13 17:32:02 -07001490 // Keyboard will try to claim some of the buttons but we really want to reserve those so we
1491 // can fuse it with the touch screen data, so just take them back. Note this means an
1492 // external stylus cannot also be a keyboard device.
Chris Ye1b0c7342020-07-28 21:57:03 -07001493 device->classes &= ~InputDeviceClass::KEYBOARD;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001494 }
1495
1496 // See if this device is a joystick.
1497 // Assumes that joysticks always have gamepad buttons in order to distinguish them
1498 // from other devices such as accelerometers that also have absolute axes.
1499 if (haveGamepadButtons) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001500 auto assumedClasses = device->classes | InputDeviceClass::JOYSTICK;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001501 for (int i = 0; i <= ABS_MAX; i++) {
Chris Ye66fbac32020-07-06 20:36:43 -07001502 if (device->absBitmask.test(i) &&
Chris Ye1b0c7342020-07-28 21:57:03 -07001503 (getAbsAxisUsage(i, assumedClasses).test(InputDeviceClass::JOYSTICK))) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001504 device->classes = assumedClasses;
1505 break;
1506 }
1507 }
1508 }
1509
Chris Yef59a2f42020-10-16 12:55:26 -07001510 // Check whether this device is an accelerometer.
1511 if (device->propBitmask.test(INPUT_PROP_ACCELEROMETER)) {
1512 device->classes |= InputDeviceClass::SENSOR;
1513 }
1514
Michael Wrightd02c5b62014-02-10 15:10:22 -08001515 // Check whether this device has switches.
1516 for (int i = 0; i <= SW_MAX; i++) {
Chris Ye66fbac32020-07-06 20:36:43 -07001517 if (device->swBitmask.test(i)) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001518 device->classes |= InputDeviceClass::SWITCH;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001519 break;
1520 }
1521 }
1522
1523 // Check whether this device supports the vibrator.
Chris Ye66fbac32020-07-06 20:36:43 -07001524 if (device->ffBitmask.test(FF_RUMBLE)) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001525 device->classes |= InputDeviceClass::VIBRATOR;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001526 }
1527
1528 // Configure virtual keys.
Chris Ye1b0c7342020-07-28 21:57:03 -07001529 if ((device->classes.test(InputDeviceClass::TOUCH))) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001530 // Load the virtual keys for the touch screen, if any.
1531 // We do this now so that we can make sure to load the keymap if necessary.
Chris Ye989bb932020-07-04 16:18:59 -07001532 bool success = device->loadVirtualKeyMapLocked();
Siarhei Vishniakou3e78dec2019-02-20 16:21:46 -06001533 if (success) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001534 device->classes |= InputDeviceClass::KEYBOARD;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001535 }
1536 }
1537
1538 // Load the key map.
Chris Yef59a2f42020-10-16 12:55:26 -07001539 // We need to do this for joysticks too because the key layout may specify axes, and for
1540 // sensor as well because the key layout may specify the axes to sensor data mapping.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001541 status_t keyMapStatus = NAME_NOT_FOUND;
Chris Yef59a2f42020-10-16 12:55:26 -07001542 if (device->classes.any(InputDeviceClass::KEYBOARD | InputDeviceClass::JOYSTICK |
1543 InputDeviceClass::SENSOR)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001544 // Load the keymap for the device.
Chris Ye989bb932020-07-04 16:18:59 -07001545 keyMapStatus = device->loadKeyMapLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001546 }
1547
1548 // Configure the keyboard, gamepad or virtual keyboard.
Chris Ye1b0c7342020-07-28 21:57:03 -07001549 if (device->classes.test(InputDeviceClass::KEYBOARD)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001550 // Register the keyboard as a built-in keyboard if it is eligible.
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001551 if (!keyMapStatus && mBuiltInKeyboardId == NO_BUILT_IN_KEYBOARD &&
Siarhei Vishniakoud549b252020-08-11 11:25:26 -05001552 isEligibleBuiltInKeyboard(device->identifier, device->configuration.get(),
1553 &device->keyMap)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001554 mBuiltInKeyboardId = device->id;
1555 }
1556
1557 // 'Q' key support = cheap test of whether this is an alpha-capable kbd
Chris Ye989bb932020-07-04 16:18:59 -07001558 if (device->hasKeycodeLocked(AKEYCODE_Q)) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001559 device->classes |= InputDeviceClass::ALPHAKEY;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001560 }
1561
1562 // See if this device has a DPAD.
Chris Ye989bb932020-07-04 16:18:59 -07001563 if (device->hasKeycodeLocked(AKEYCODE_DPAD_UP) &&
1564 device->hasKeycodeLocked(AKEYCODE_DPAD_DOWN) &&
1565 device->hasKeycodeLocked(AKEYCODE_DPAD_LEFT) &&
1566 device->hasKeycodeLocked(AKEYCODE_DPAD_RIGHT) &&
1567 device->hasKeycodeLocked(AKEYCODE_DPAD_CENTER)) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001568 device->classes |= InputDeviceClass::DPAD;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001569 }
1570
1571 // See if this device has a gamepad.
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001572 for (size_t i = 0; i < sizeof(GAMEPAD_KEYCODES) / sizeof(GAMEPAD_KEYCODES[0]); i++) {
Chris Ye989bb932020-07-04 16:18:59 -07001573 if (device->hasKeycodeLocked(GAMEPAD_KEYCODES[i])) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001574 device->classes |= InputDeviceClass::GAMEPAD;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001575 break;
1576 }
1577 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001578 }
1579
1580 // If the device isn't recognized as something we handle, don't monitor it.
Chris Ye1b0c7342020-07-28 21:57:03 -07001581 if (device->classes == Flags<InputDeviceClass>(0)) {
Chris Ye8594e192020-07-14 10:34:06 -07001582 ALOGV("Dropping device: id=%d, path='%s', name='%s'", deviceId, devicePath.c_str(),
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001583 device->identifier.name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001584 return -1;
1585 }
1586
Tim Kilbourn063ff532015-04-08 10:26:18 -07001587 // Determine whether the device has a mic.
Chris Ye989bb932020-07-04 16:18:59 -07001588 if (device->deviceHasMicLocked()) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001589 device->classes |= InputDeviceClass::MIC;
Tim Kilbourn063ff532015-04-08 10:26:18 -07001590 }
1591
Michael Wrightd02c5b62014-02-10 15:10:22 -08001592 // Determine whether the device is external or internal.
Chris Ye989bb932020-07-04 16:18:59 -07001593 if (device->isExternalDeviceLocked()) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001594 device->classes |= InputDeviceClass::EXTERNAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001595 }
1596
Chris Ye1b0c7342020-07-28 21:57:03 -07001597 if (device->classes.any(InputDeviceClass::JOYSTICK | InputDeviceClass::DPAD) &&
1598 device->classes.test(InputDeviceClass::GAMEPAD)) {
Chris Ye989bb932020-07-04 16:18:59 -07001599 device->controllerNumber = getNextControllerNumberLocked(device->identifier.name);
1600 device->setLedForControllerLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001601 }
1602
Chris Ye989bb932020-07-04 16:18:59 -07001603 if (registerDeviceForEpollLocked(*device) != OK) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001604 return -1;
1605 }
1606
Chris Ye989bb932020-07-04 16:18:59 -07001607 device->configureFd();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001608
Chris Ye1b0c7342020-07-28 21:57:03 -07001609 ALOGI("New device: id=%d, fd=%d, path='%s', name='%s', classes=%s, "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001610 "configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s, ",
Chris Ye1b0c7342020-07-28 21:57:03 -07001611 deviceId, fd, devicePath.c_str(), device->identifier.name.c_str(),
1612 device->classes.string().c_str(), device->configurationFile.c_str(),
1613 device->keyMap.keyLayoutFile.c_str(), device->keyMap.keyCharacterMapFile.c_str(),
1614 toString(mBuiltInKeyboardId == deviceId));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001615
Chris Ye989bb932020-07-04 16:18:59 -07001616 addDeviceLocked(std::move(device));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001617 return OK;
1618}
1619
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001620void EventHub::openVideoDeviceLocked(const std::string& devicePath) {
1621 std::unique_ptr<TouchVideoDevice> videoDevice = TouchVideoDevice::create(devicePath);
1622 if (!videoDevice) {
1623 ALOGE("Could not create touch video device for %s. Ignoring", devicePath.c_str());
1624 return;
1625 }
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08001626 // Transfer ownership of this video device to a matching input device
Chris Ye989bb932020-07-04 16:18:59 -07001627 for (const auto& [id, device] : mDevices) {
Siarhei Vishniakouf49608d2020-08-20 19:18:21 -05001628 if (tryAddVideoDevice(*device, videoDevice)) {
1629 return; // 'device' now owns 'videoDevice'
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08001630 }
1631 }
1632
1633 // Couldn't find a matching input device, so just add it to a temporary holding queue.
1634 // A matching input device may appear later.
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001635 ALOGI("Adding video device %s to list of unattached video devices",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001636 videoDevice->getName().c_str());
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001637 mUnattachedVideoDevices.push_back(std::move(videoDevice));
1638}
1639
Siarhei Vishniakouf49608d2020-08-20 19:18:21 -05001640bool EventHub::tryAddVideoDevice(EventHub::Device& device,
1641 std::unique_ptr<TouchVideoDevice>& videoDevice) {
1642 if (videoDevice->getName() != device.identifier.name) {
1643 return false;
1644 }
1645 device.videoDevice = std::move(videoDevice);
1646 if (device.enabled) {
1647 registerVideoDeviceForEpollLocked(*device.videoDevice);
1648 }
1649 return true;
1650}
1651
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001652bool EventHub::isDeviceEnabled(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +00001653 std::scoped_lock _l(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001654 Device* device = getDeviceLocked(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -07001655 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001656 ALOGE("Invalid device id=%" PRId32 " provided to %s", deviceId, __func__);
1657 return false;
1658 }
1659 return device->enabled;
1660}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001661
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001662status_t EventHub::enableDevice(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +00001663 std::scoped_lock _l(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001664 Device* device = getDeviceLocked(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -07001665 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001666 ALOGE("Invalid device id=%" PRId32 " provided to %s", deviceId, __func__);
1667 return BAD_VALUE;
1668 }
1669 if (device->enabled) {
1670 ALOGW("Duplicate call to %s, input device %" PRId32 " already enabled", __func__, deviceId);
1671 return OK;
1672 }
1673 status_t result = device->enable();
1674 if (result != OK) {
1675 ALOGE("Failed to enable device %" PRId32, deviceId);
1676 return result;
1677 }
1678
Chris Ye989bb932020-07-04 16:18:59 -07001679 device->configureFd();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001680
Chris Ye989bb932020-07-04 16:18:59 -07001681 return registerDeviceForEpollLocked(*device);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001682}
1683
1684status_t EventHub::disableDevice(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +00001685 std::scoped_lock _l(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001686 Device* device = getDeviceLocked(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -07001687 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001688 ALOGE("Invalid device id=%" PRId32 " provided to %s", deviceId, __func__);
1689 return BAD_VALUE;
1690 }
1691 if (!device->enabled) {
1692 ALOGW("Duplicate call to %s, input device already disabled", __func__);
1693 return OK;
1694 }
Chris Ye989bb932020-07-04 16:18:59 -07001695 unregisterDeviceFromEpollLocked(*device);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001696 return device->disable();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001697}
1698
1699void EventHub::createVirtualKeyboardLocked() {
1700 InputDeviceIdentifier identifier;
1701 identifier.name = "Virtual";
1702 identifier.uniqueId = "<virtual>";
1703 assignDescriptorLocked(identifier);
1704
Chris Ye989bb932020-07-04 16:18:59 -07001705 std::unique_ptr<Device> device =
1706 std::make_unique<Device>(-1, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, "<virtual>",
1707 identifier);
Chris Ye1b0c7342020-07-28 21:57:03 -07001708 device->classes = InputDeviceClass::KEYBOARD | InputDeviceClass::ALPHAKEY |
1709 InputDeviceClass::DPAD | InputDeviceClass::VIRTUAL;
Chris Ye989bb932020-07-04 16:18:59 -07001710 device->loadKeyMapLocked();
1711 addDeviceLocked(std::move(device));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001712}
1713
Chris Ye989bb932020-07-04 16:18:59 -07001714void EventHub::addDeviceLocked(std::unique_ptr<Device> device) {
1715 mOpeningDevices.push_back(std::move(device));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001716}
1717
Chris Ye989bb932020-07-04 16:18:59 -07001718int32_t EventHub::getNextControllerNumberLocked(const std::string& name) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001719 if (mControllerNumbers.isFull()) {
1720 ALOGI("Maximum number of controllers reached, assigning controller number 0 to device %s",
Chris Ye989bb932020-07-04 16:18:59 -07001721 name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001722 return 0;
1723 }
1724 // Since the controller number 0 is reserved for non-controllers, translate all numbers up by
1725 // one
1726 return static_cast<int32_t>(mControllerNumbers.markFirstUnmarkedBit() + 1);
1727}
1728
Chris Ye989bb932020-07-04 16:18:59 -07001729void EventHub::releaseControllerNumberLocked(int32_t num) {
1730 if (num > 0) {
1731 mControllerNumbers.clearBit(static_cast<uint32_t>(num - 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001732 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001733}
1734
Chris Ye8594e192020-07-14 10:34:06 -07001735void EventHub::closeDeviceByPathLocked(const std::string& devicePath) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001736 Device* device = getDeviceByPathLocked(devicePath);
Chris Ye989bb932020-07-04 16:18:59 -07001737 if (device != nullptr) {
1738 closeDeviceLocked(*device);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001739 return;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001740 }
Chris Ye8594e192020-07-14 10:34:06 -07001741 ALOGV("Remove device: %s not found, device may already have been removed.", devicePath.c_str());
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001742}
1743
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08001744/**
1745 * Find the video device by filename, and close it.
1746 * The video device is closed by path during an inotify event, where we don't have the
1747 * additional context about the video device fd, or the associated input device.
1748 */
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001749void EventHub::closeVideoDeviceByPathLocked(const std::string& devicePath) {
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08001750 // A video device may be owned by an existing input device, or it may be stored in
1751 // the mUnattachedVideoDevices queue. Check both locations.
Chris Ye989bb932020-07-04 16:18:59 -07001752 for (const auto& [id, device] : mDevices) {
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08001753 if (device->videoDevice && device->videoDevice->getPath() == devicePath) {
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001754 unregisterVideoDeviceFromEpollLocked(*device->videoDevice);
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08001755 device->videoDevice = nullptr;
1756 return;
1757 }
1758 }
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001759 mUnattachedVideoDevices
1760 .erase(std::remove_if(mUnattachedVideoDevices.begin(), mUnattachedVideoDevices.end(),
1761 [&devicePath](
1762 const std::unique_ptr<TouchVideoDevice>& videoDevice) {
1763 return videoDevice->getPath() == devicePath;
1764 }),
1765 mUnattachedVideoDevices.end());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001766}
1767
1768void EventHub::closeAllDevicesLocked() {
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001769 mUnattachedVideoDevices.clear();
Siarhei Vishniakoud549b252020-08-11 11:25:26 -05001770 while (!mDevices.empty()) {
1771 closeDeviceLocked(*(mDevices.begin()->second));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001772 }
1773}
1774
Chris Ye989bb932020-07-04 16:18:59 -07001775void EventHub::closeDeviceLocked(Device& device) {
1776 ALOGI("Removed device: path=%s name=%s id=%d fd=%d classes=%s", device.path.c_str(),
1777 device.identifier.name.c_str(), device.id, device.fd, device.classes.string().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001778
Chris Ye989bb932020-07-04 16:18:59 -07001779 if (device.id == mBuiltInKeyboardId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001780 ALOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this",
Chris Ye989bb932020-07-04 16:18:59 -07001781 device.path.c_str(), mBuiltInKeyboardId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001782 mBuiltInKeyboardId = NO_BUILT_IN_KEYBOARD;
1783 }
1784
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001785 unregisterDeviceFromEpollLocked(device);
Chris Ye989bb932020-07-04 16:18:59 -07001786 if (device.videoDevice) {
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001787 // This must be done after the video device is removed from epoll
Chris Ye989bb932020-07-04 16:18:59 -07001788 mUnattachedVideoDevices.push_back(std::move(device.videoDevice));
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08001789 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001790
Chris Ye989bb932020-07-04 16:18:59 -07001791 releaseControllerNumberLocked(device.controllerNumber);
Siarhei Vishniakoud549b252020-08-11 11:25:26 -05001792 device.controllerNumber = 0;
Chris Ye989bb932020-07-04 16:18:59 -07001793 device.close();
Chris Ye989bb932020-07-04 16:18:59 -07001794 mClosingDevices.push_back(std::move(mDevices[device.id]));
Siarhei Vishniakoud549b252020-08-11 11:25:26 -05001795
Chris Ye989bb932020-07-04 16:18:59 -07001796 mDevices.erase(device.id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001797}
1798
1799status_t EventHub::readNotifyLocked() {
1800 int res;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001801 char event_buf[512];
1802 int event_size;
1803 int event_pos = 0;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001804 struct inotify_event* event;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001805
1806 ALOGV("EventHub::readNotify nfd: %d\n", mINotifyFd);
1807 res = read(mINotifyFd, event_buf, sizeof(event_buf));
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001808 if (res < (int)sizeof(*event)) {
1809 if (errno == EINTR) return 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001810 ALOGW("could not get event, %s\n", strerror(errno));
1811 return -1;
1812 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001813
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001814 while (res >= (int)sizeof(*event)) {
1815 event = (struct inotify_event*)(event_buf + event_pos);
1816 if (event->len) {
Siarhei Vishniakou951f3622018-12-12 19:45:42 -08001817 if (event->wd == mInputWd) {
Chris Ye8594e192020-07-14 10:34:06 -07001818 std::string filename = std::string(DEVICE_PATH) + "/" + event->name;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001819 if (event->mask & IN_CREATE) {
Chris Ye8594e192020-07-14 10:34:06 -07001820 openDeviceLocked(filename);
Siarhei Vishniakou951f3622018-12-12 19:45:42 -08001821 } else {
1822 ALOGI("Removing device '%s' due to inotify event\n", filename.c_str());
Chris Ye8594e192020-07-14 10:34:06 -07001823 closeDeviceByPathLocked(filename);
Siarhei Vishniakou951f3622018-12-12 19:45:42 -08001824 }
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001825 } else if (event->wd == mVideoWd) {
Siarhei Vishniakou951f3622018-12-12 19:45:42 -08001826 if (isV4lTouchNode(event->name)) {
Chris Ye8594e192020-07-14 10:34:06 -07001827 std::string filename = std::string(VIDEO_DEVICE_PATH) + "/" + event->name;
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001828 if (event->mask & IN_CREATE) {
1829 openVideoDeviceLocked(filename);
1830 } else {
1831 ALOGI("Removing video device '%s' due to inotify event", filename.c_str());
1832 closeVideoDeviceByPathLocked(filename);
1833 }
Siarhei Vishniakou951f3622018-12-12 19:45:42 -08001834 }
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001835 } else {
Siarhei Vishniakou951f3622018-12-12 19:45:42 -08001836 LOG_ALWAYS_FATAL("Unexpected inotify event, wd = %i", event->wd);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001837 }
1838 }
1839 event_size = sizeof(*event) + event->len;
1840 res -= event_size;
1841 event_pos += event_size;
1842 }
1843 return 0;
1844}
1845
Chris Ye8594e192020-07-14 10:34:06 -07001846status_t EventHub::scanDirLocked(const std::string& dirname) {
1847 for (const auto& entry : std::filesystem::directory_iterator(dirname)) {
1848 openDeviceLocked(entry.path());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001849 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001850 return 0;
1851}
1852
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001853/**
1854 * Look for all dirname/v4l-touch* devices, and open them.
1855 */
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001856status_t EventHub::scanVideoDirLocked(const std::string& dirname) {
Chris Ye8594e192020-07-14 10:34:06 -07001857 for (const auto& entry : std::filesystem::directory_iterator(dirname)) {
1858 if (isV4lTouchNode(entry.path())) {
1859 ALOGI("Found touch video device %s", entry.path().c_str());
1860 openVideoDeviceLocked(entry.path());
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001861 }
1862 }
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001863 return OK;
1864}
1865
Michael Wrightd02c5b62014-02-10 15:10:22 -08001866void EventHub::requestReopenDevices() {
1867 ALOGV("requestReopenDevices() called");
1868
Chris Ye87143712020-11-10 05:05:58 +00001869 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001870 mNeedToReopenDevices = true;
1871}
1872
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001873void EventHub::dump(std::string& dump) {
1874 dump += "Event Hub State:\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001875
1876 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +00001877 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001878
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001879 dump += StringPrintf(INDENT "BuiltInKeyboardId: %d\n", mBuiltInKeyboardId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001880
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001881 dump += INDENT "Devices:\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001882
Chris Ye989bb932020-07-04 16:18:59 -07001883 for (const auto& [id, device] : mDevices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001884 if (mBuiltInKeyboardId == device->id) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001885 dump += StringPrintf(INDENT2 "%d: %s (aka device 0 - built-in keyboard)\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001886 device->id, device->identifier.name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001887 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001888 dump += StringPrintf(INDENT2 "%d: %s\n", device->id,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001889 device->identifier.name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001890 }
Chris Ye1b0c7342020-07-28 21:57:03 -07001891 dump += StringPrintf(INDENT3 "Classes: %s\n", device->classes.string().c_str());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001892 dump += StringPrintf(INDENT3 "Path: %s\n", device->path.c_str());
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001893 dump += StringPrintf(INDENT3 "Enabled: %s\n", toString(device->enabled));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001894 dump += StringPrintf(INDENT3 "Descriptor: %s\n", device->identifier.descriptor.c_str());
1895 dump += StringPrintf(INDENT3 "Location: %s\n", device->identifier.location.c_str());
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001896 dump += StringPrintf(INDENT3 "ControllerNumber: %d\n", device->controllerNumber);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001897 dump += StringPrintf(INDENT3 "UniqueId: %s\n", device->identifier.uniqueId.c_str());
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001898 dump += StringPrintf(INDENT3 "Identifier: bus=0x%04x, vendor=0x%04x, "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001899 "product=0x%04x, version=0x%04x\n",
1900 device->identifier.bus, device->identifier.vendor,
1901 device->identifier.product, device->identifier.version);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001902 dump += StringPrintf(INDENT3 "KeyLayoutFile: %s\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001903 device->keyMap.keyLayoutFile.c_str());
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001904 dump += StringPrintf(INDENT3 "KeyCharacterMapFile: %s\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001905 device->keyMap.keyCharacterMapFile.c_str());
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001906 dump += StringPrintf(INDENT3 "ConfigurationFile: %s\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001907 device->configurationFile.c_str());
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08001908 dump += INDENT3 "VideoDevice: ";
1909 if (device->videoDevice) {
1910 dump += device->videoDevice->dump() + "\n";
1911 } else {
1912 dump += "<none>\n";
1913 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001914 }
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001915
1916 dump += INDENT "Unattached video devices:\n";
1917 for (const std::unique_ptr<TouchVideoDevice>& videoDevice : mUnattachedVideoDevices) {
1918 dump += INDENT2 + videoDevice->dump() + "\n";
1919 }
1920 if (mUnattachedVideoDevices.empty()) {
1921 dump += INDENT2 "<none>\n";
1922 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001923 } // release lock
1924}
1925
1926void EventHub::monitor() {
1927 // Acquire and release the lock to ensure that the event hub has not deadlocked.
Chris Ye1c2e0892020-11-30 21:41:44 -08001928 std::unique_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001929}
1930
Michael Wrightd02c5b62014-02-10 15:10:22 -08001931}; // namespace android