blob: f864c0e5b60e34fb68a59dafe48cbc6399ec3597 [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
Michael Wright842500e2015-03-13 17:32:02 -0700160 // External stylus gets the pressure axis
Chris Ye1b0c7342020-07-28 21:57:03 -0700161 if (deviceClasses.test(InputDeviceClass::EXTERNAL_STYLUS)) {
Michael Wright842500e2015-03-13 17:32:02 -0700162 if (axis == ABS_PRESSURE) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700163 return InputDeviceClass::EXTERNAL_STYLUS;
Michael Wright842500e2015-03-13 17:32:02 -0700164 }
165 }
166
Michael Wrightd02c5b62014-02-10 15:10:22 -0800167 // Joystick devices get the rest.
Chris Ye1b0c7342020-07-28 21:57:03 -0700168 return deviceClasses & InputDeviceClass::JOYSTICK;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800169}
170
171// --- EventHub::Device ---
172
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100173EventHub::Device::Device(int fd, int32_t id, const std::string& path,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700174 const InputDeviceIdentifier& identifier)
Chris Ye989bb932020-07-04 16:18:59 -0700175 : fd(fd),
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700176 id(id),
177 path(path),
178 identifier(identifier),
179 classes(0),
180 configuration(nullptr),
181 virtualKeyMap(nullptr),
182 ffEffectPlaying(false),
183 ffEffectId(-1),
184 controllerNumber(0),
185 enabled(true),
Chris Ye66fbac32020-07-06 20:36:43 -0700186 isVirtual(fd < 0) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800187
188EventHub::Device::~Device() {
189 close();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800190}
191
192void EventHub::Device::close() {
193 if (fd >= 0) {
194 ::close(fd);
195 fd = -1;
196 }
197}
198
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700199status_t EventHub::Device::enable() {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100200 fd = open(path.c_str(), O_RDWR | O_CLOEXEC | O_NONBLOCK);
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700201 if (fd < 0) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100202 ALOGE("could not open %s, %s\n", path.c_str(), strerror(errno));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700203 return -errno;
204 }
205 enabled = true;
206 return OK;
207}
208
209status_t EventHub::Device::disable() {
210 close();
211 enabled = false;
212 return OK;
213}
214
Chris Ye989bb932020-07-04 16:18:59 -0700215bool EventHub::Device::hasValidFd() const {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700216 return !isVirtual && enabled;
217}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800218
Chris Ye3a1e4462020-08-12 10:13:15 -0700219const std::shared_ptr<KeyCharacterMap> EventHub::Device::getKeyCharacterMap() const {
Chris Ye989bb932020-07-04 16:18:59 -0700220 return keyMap.keyCharacterMap;
221}
222
223template <std::size_t N>
224status_t EventHub::Device::readDeviceBitMask(unsigned long ioctlCode, BitArray<N>& bitArray) {
225 if (!hasValidFd()) {
226 return BAD_VALUE;
227 }
228 if ((_IOC_SIZE(ioctlCode) == 0)) {
229 ioctlCode |= _IOC(0, 0, 0, bitArray.bytes());
230 }
231
232 typename BitArray<N>::Buffer buffer;
233 status_t ret = ioctl(fd, ioctlCode, buffer.data());
234 bitArray.loadFromBuffer(buffer);
235 return ret;
236}
237
238void EventHub::Device::configureFd() {
239 // Set fd parameters with ioctl, such as key repeat, suspend block, and clock type
240 if (classes.test(InputDeviceClass::KEYBOARD)) {
241 // Disable kernel key repeat since we handle it ourselves
242 unsigned int repeatRate[] = {0, 0};
243 if (ioctl(fd, EVIOCSREP, repeatRate)) {
244 ALOGW("Unable to disable kernel key repeat for %s: %s", path.c_str(), strerror(errno));
245 }
246 }
247
248 // Tell the kernel that we want to use the monotonic clock for reporting timestamps
249 // associated with input events. This is important because the input system
250 // uses the timestamps extensively and assumes they were recorded using the monotonic
251 // clock.
252 int clockId = CLOCK_MONOTONIC;
253 bool usingClockIoctl = !ioctl(fd, EVIOCSCLOCKID, &clockId);
254 ALOGI("usingClockIoctl=%s", toString(usingClockIoctl));
255}
256
257bool EventHub::Device::hasKeycodeLocked(int keycode) const {
258 if (!keyMap.haveKeyLayout()) {
259 return false;
260 }
261
262 std::vector<int32_t> scanCodes;
263 keyMap.keyLayoutMap->findScanCodesForKey(keycode, &scanCodes);
264 const size_t N = scanCodes.size();
265 for (size_t i = 0; i < N && i <= KEY_MAX; i++) {
266 int32_t sc = scanCodes[i];
267 if (sc >= 0 && sc <= KEY_MAX && keyBitmask.test(sc)) {
268 return true;
269 }
270 }
271
272 return false;
273}
274
275void EventHub::Device::loadConfigurationLocked() {
276 configurationFile =
277 getInputDeviceConfigurationFilePathByDeviceIdentifier(identifier,
278 InputDeviceConfigurationFileType::
279 CONFIGURATION);
280 if (configurationFile.empty()) {
281 ALOGD("No input device configuration file found for device '%s'.", identifier.name.c_str());
282 } else {
Siarhei Vishniakou4d9f9772020-09-02 22:28:29 -0500283 android::base::Result<std::unique_ptr<PropertyMap>> propertyMap =
284 PropertyMap::load(configurationFile.c_str());
285 if (!propertyMap.ok()) {
Chris Ye989bb932020-07-04 16:18:59 -0700286 ALOGE("Error loading input device configuration file for device '%s'. "
287 "Using default configuration.",
288 identifier.name.c_str());
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500289 } else {
Siarhei Vishniakou4d9f9772020-09-02 22:28:29 -0500290 configuration = std::move(*propertyMap);
Chris Ye989bb932020-07-04 16:18:59 -0700291 }
292 }
293}
294
295bool EventHub::Device::loadVirtualKeyMapLocked() {
296 // The virtual key map is supplied by the kernel as a system board property file.
297 std::string propPath = "/sys/board_properties/virtualkeys.";
298 propPath += identifier.getCanonicalName();
299 if (access(propPath.c_str(), R_OK)) {
300 return false;
301 }
302 virtualKeyMap = VirtualKeyMap::load(propPath);
303 return virtualKeyMap != nullptr;
304}
305
306status_t EventHub::Device::loadKeyMapLocked() {
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500307 return keyMap.load(identifier, configuration.get());
Chris Ye989bb932020-07-04 16:18:59 -0700308}
309
310bool EventHub::Device::isExternalDeviceLocked() {
311 if (configuration) {
312 bool value;
313 if (configuration->tryGetProperty(String8("device.internal"), value)) {
314 return !value;
315 }
316 }
317 return identifier.bus == BUS_USB || identifier.bus == BUS_BLUETOOTH;
318}
319
320bool EventHub::Device::deviceHasMicLocked() {
321 if (configuration) {
322 bool value;
323 if (configuration->tryGetProperty(String8("audio.mic"), value)) {
324 return value;
325 }
326 }
327 return false;
328}
329
330void EventHub::Device::setLedStateLocked(int32_t led, bool on) {
331 int32_t sc;
332 if (hasValidFd() && mapLed(led, &sc) != NAME_NOT_FOUND) {
333 struct input_event ev;
334 ev.time.tv_sec = 0;
335 ev.time.tv_usec = 0;
336 ev.type = EV_LED;
337 ev.code = sc;
338 ev.value = on ? 1 : 0;
339
340 ssize_t nWrite;
341 do {
342 nWrite = write(fd, &ev, sizeof(struct input_event));
343 } while (nWrite == -1 && errno == EINTR);
344 }
345}
346
347void EventHub::Device::setLedForControllerLocked() {
348 for (int i = 0; i < MAX_CONTROLLER_LEDS; i++) {
349 setLedStateLocked(ALED_CONTROLLER_1 + i, controllerNumber == i + 1);
350 }
351}
352
353status_t EventHub::Device::mapLed(int32_t led, int32_t* outScanCode) const {
354 if (!keyMap.haveKeyLayout()) {
355 return NAME_NOT_FOUND;
356 }
357
358 int32_t scanCode;
359 if (keyMap.keyLayoutMap->findScanCodeForLed(led, &scanCode) != NAME_NOT_FOUND) {
360 if (scanCode >= 0 && scanCode <= LED_MAX && ledBitmask.test(scanCode)) {
361 *outScanCode = scanCode;
362 return NO_ERROR;
363 }
364 }
365 return NAME_NOT_FOUND;
366}
367
Siarhei Vishniakou7a522bf2019-09-24 12:46:29 +0100368/**
369 * Get the capabilities for the current process.
370 * Crashes the system if unable to create / check / destroy the capabilities object.
371 */
372class Capabilities final {
373public:
374 explicit Capabilities() {
375 mCaps = cap_get_proc();
376 LOG_ALWAYS_FATAL_IF(mCaps == nullptr, "Could not get capabilities of the current process");
377 }
378
379 /**
380 * Check whether the current process has a specific capability
381 * in the set of effective capabilities.
382 * Return CAP_SET if the process has the requested capability
383 * Return CAP_CLEAR otherwise.
384 */
385 cap_flag_value_t checkEffectiveCapability(cap_value_t capability) {
386 cap_flag_value_t value;
387 const int result = cap_get_flag(mCaps, capability, CAP_EFFECTIVE, &value);
388 LOG_ALWAYS_FATAL_IF(result == -1, "Could not obtain the requested capability");
389 return value;
390 }
391
392 ~Capabilities() {
393 const int result = cap_free(mCaps);
394 LOG_ALWAYS_FATAL_IF(result == -1, "Could not release the capabilities structure");
395 }
396
397private:
398 cap_t mCaps;
399};
400
401static void ensureProcessCanBlockSuspend() {
402 Capabilities capabilities;
403 const bool canBlockSuspend =
404 capabilities.checkEffectiveCapability(CAP_BLOCK_SUSPEND) == CAP_SET;
405 LOG_ALWAYS_FATAL_IF(!canBlockSuspend,
406 "Input must be able to block suspend to properly process events");
407}
408
Michael Wrightd02c5b62014-02-10 15:10:22 -0800409// --- EventHub ---
410
Michael Wrightd02c5b62014-02-10 15:10:22 -0800411const int EventHub::EPOLL_MAX_EVENTS;
412
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700413EventHub::EventHub(void)
414 : mBuiltInKeyboardId(NO_BUILT_IN_KEYBOARD),
415 mNextDeviceId(1),
416 mControllerNumbers(),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800417 mNeedToSendFinishedDeviceScan(false),
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700418 mNeedToReopenDevices(false),
419 mNeedToScanDevices(true),
420 mPendingEventCount(0),
421 mPendingEventIndex(0),
422 mPendingINotify(false) {
Siarhei Vishniakou7a522bf2019-09-24 12:46:29 +0100423 ensureProcessCanBlockSuspend();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800424
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -0800425 mEpollFd = epoll_create1(EPOLL_CLOEXEC);
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800426 LOG_ALWAYS_FATAL_IF(mEpollFd < 0, "Could not create epoll instance: %s", strerror(errno));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800427
428 mINotifyFd = inotify_init();
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800429 mInputWd = inotify_add_watch(mINotifyFd, DEVICE_PATH, IN_DELETE | IN_CREATE);
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700430 LOG_ALWAYS_FATAL_IF(mInputWd < 0, "Could not register INotify for %s: %s", DEVICE_PATH,
431 strerror(errno));
Philip Quinn39b81682019-01-09 22:20:39 -0800432 if (isV4lScanningEnabled()) {
433 mVideoWd = inotify_add_watch(mINotifyFd, VIDEO_DEVICE_PATH, IN_DELETE | IN_CREATE);
434 LOG_ALWAYS_FATAL_IF(mVideoWd < 0, "Could not register INotify for %s: %s",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700435 VIDEO_DEVICE_PATH, strerror(errno));
Philip Quinn39b81682019-01-09 22:20:39 -0800436 } else {
437 mVideoWd = -1;
438 ALOGI("Video device scanning disabled");
439 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800440
Siarhei Vishniakou2d0e9482019-09-24 12:52:47 +0100441 struct epoll_event eventItem = {};
442 eventItem.events = EPOLLIN | EPOLLWAKEUP;
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -0700443 eventItem.data.fd = mINotifyFd;
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800444 int result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mINotifyFd, &eventItem);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800445 LOG_ALWAYS_FATAL_IF(result != 0, "Could not add INotify to epoll instance. errno=%d", errno);
446
447 int wakeFds[2];
448 result = pipe(wakeFds);
449 LOG_ALWAYS_FATAL_IF(result != 0, "Could not create wake pipe. errno=%d", errno);
450
451 mWakeReadPipeFd = wakeFds[0];
452 mWakeWritePipeFd = wakeFds[1];
453
454 result = fcntl(mWakeReadPipeFd, F_SETFL, O_NONBLOCK);
455 LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake read pipe non-blocking. errno=%d",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700456 errno);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800457
458 result = fcntl(mWakeWritePipeFd, F_SETFL, O_NONBLOCK);
459 LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake write pipe non-blocking. errno=%d",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700460 errno);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800461
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -0700462 eventItem.data.fd = mWakeReadPipeFd;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800463 result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeReadPipeFd, &eventItem);
464 LOG_ALWAYS_FATAL_IF(result != 0, "Could not add wake read pipe to epoll instance. errno=%d",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700465 errno);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800466}
467
468EventHub::~EventHub(void) {
469 closeAllDevicesLocked();
470
Michael Wrightd02c5b62014-02-10 15:10:22 -0800471 ::close(mEpollFd);
472 ::close(mINotifyFd);
473 ::close(mWakeReadPipeFd);
474 ::close(mWakeWritePipeFd);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800475}
476
477InputDeviceIdentifier EventHub::getDeviceIdentifier(int32_t deviceId) const {
Chris Ye87143712020-11-10 05:05:58 +0000478 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800479 Device* device = getDeviceLocked(deviceId);
Chris Ye989bb932020-07-04 16:18:59 -0700480 return device != nullptr ? device->identifier : InputDeviceIdentifier();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800481}
482
Chris Ye1b0c7342020-07-28 21:57:03 -0700483Flags<InputDeviceClass> EventHub::getDeviceClasses(int32_t deviceId) const {
Chris Ye87143712020-11-10 05:05:58 +0000484 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800485 Device* device = getDeviceLocked(deviceId);
Chris Ye989bb932020-07-04 16:18:59 -0700486 return device != nullptr ? device->classes : Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800487}
488
489int32_t EventHub::getDeviceControllerNumber(int32_t deviceId) const {
Chris Ye87143712020-11-10 05:05:58 +0000490 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800491 Device* device = getDeviceLocked(deviceId);
Chris Ye989bb932020-07-04 16:18:59 -0700492 return device != nullptr ? device->controllerNumber : 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800493}
494
495void EventHub::getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
Chris Ye87143712020-11-10 05:05:58 +0000496 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800497 Device* device = getDeviceLocked(deviceId);
Chris Ye989bb932020-07-04 16:18:59 -0700498 if (device != nullptr && device->configuration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800499 *outConfiguration = *device->configuration;
500 } else {
501 outConfiguration->clear();
502 }
503}
504
505status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700506 RawAbsoluteAxisInfo* outAxisInfo) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800507 outAxisInfo->clear();
508
509 if (axis >= 0 && axis <= ABS_MAX) {
Chris Ye87143712020-11-10 05:05:58 +0000510 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800511
512 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -0700513 if (device != nullptr && device->hasValidFd() && device->absBitmask.test(axis)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800514 struct input_absinfo info;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700515 if (ioctl(device->fd, EVIOCGABS(axis), &info)) {
516 ALOGW("Error reading absolute controller %d for device %s fd %d, errno=%d", axis,
517 device->identifier.name.c_str(), device->fd, errno);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800518 return -errno;
519 }
520
521 if (info.minimum != info.maximum) {
522 outAxisInfo->valid = true;
523 outAxisInfo->minValue = info.minimum;
524 outAxisInfo->maxValue = info.maximum;
525 outAxisInfo->flat = info.flat;
526 outAxisInfo->fuzz = info.fuzz;
527 outAxisInfo->resolution = info.resolution;
528 }
529 return OK;
530 }
531 }
532 return -1;
533}
534
535bool EventHub::hasRelativeAxis(int32_t deviceId, int axis) const {
536 if (axis >= 0 && axis <= REL_MAX) {
Chris Ye87143712020-11-10 05:05:58 +0000537 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800538 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -0700539 return device != nullptr ? device->relBitmask.test(axis) : false;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800540 }
541 return false;
542}
543
544bool EventHub::hasInputProperty(int32_t deviceId, int property) const {
Chris Ye87143712020-11-10 05:05:58 +0000545 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800546
Chris Ye989bb932020-07-04 16:18:59 -0700547 Device* device = getDeviceLocked(deviceId);
548 return property >= 0 && property <= INPUT_PROP_MAX && device != nullptr
549 ? device->propBitmask.test(property)
550 : false;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800551}
552
553int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const {
554 if (scanCode >= 0 && scanCode <= KEY_MAX) {
Chris Ye87143712020-11-10 05:05:58 +0000555 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800556
557 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -0700558 if (device != nullptr && device->hasValidFd() && device->keyBitmask.test(scanCode)) {
559 if (device->readDeviceBitMask(EVIOCGKEY(0), device->keyState) >= 0) {
560 return device->keyState.test(scanCode) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800561 }
562 }
563 }
564 return AKEY_STATE_UNKNOWN;
565}
566
567int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
Chris Ye87143712020-11-10 05:05:58 +0000568 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800569
570 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -0700571 if (device != nullptr && device->hasValidFd() && device->keyMap.haveKeyLayout()) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800572 std::vector<int32_t> scanCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800573 device->keyMap.keyLayoutMap->findScanCodesForKey(keyCode, &scanCodes);
574 if (scanCodes.size() != 0) {
Chris Ye66fbac32020-07-06 20:36:43 -0700575 if (device->readDeviceBitMask(EVIOCGKEY(0), device->keyState) >= 0) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800576 for (size_t i = 0; i < scanCodes.size(); i++) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800577 int32_t sc = scanCodes[i];
Chris Ye66fbac32020-07-06 20:36:43 -0700578 if (sc >= 0 && sc <= KEY_MAX && device->keyState.test(sc)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800579 return AKEY_STATE_DOWN;
580 }
581 }
582 return AKEY_STATE_UP;
583 }
584 }
585 }
586 return AKEY_STATE_UNKNOWN;
587}
588
589int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const {
590 if (sw >= 0 && sw <= SW_MAX) {
Chris Ye87143712020-11-10 05:05:58 +0000591 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800592
593 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -0700594 if (device != nullptr && device->hasValidFd() && device->swBitmask.test(sw)) {
595 if (device->readDeviceBitMask(EVIOCGSW(0), device->swState) >= 0) {
596 return device->swState.test(sw) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800597 }
598 }
599 }
600 return AKEY_STATE_UNKNOWN;
601}
602
603status_t EventHub::getAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t* outValue) const {
604 *outValue = 0;
605
606 if (axis >= 0 && axis <= ABS_MAX) {
Chris Ye87143712020-11-10 05:05:58 +0000607 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800608
609 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -0700610 if (device != nullptr && device->hasValidFd() && device->absBitmask.test(axis)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800611 struct input_absinfo info;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700612 if (ioctl(device->fd, EVIOCGABS(axis), &info)) {
613 ALOGW("Error reading absolute controller %d for device %s fd %d, errno=%d", axis,
614 device->identifier.name.c_str(), device->fd, errno);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800615 return -errno;
616 }
617
618 *outValue = info.value;
619 return OK;
620 }
621 }
622 return -1;
623}
624
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700625bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
626 uint8_t* outFlags) const {
Chris Ye87143712020-11-10 05:05:58 +0000627 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800628
629 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -0700630 if (device != nullptr && device->keyMap.haveKeyLayout()) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800631 std::vector<int32_t> scanCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800632 for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) {
633 scanCodes.clear();
634
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700635 status_t err = device->keyMap.keyLayoutMap->findScanCodesForKey(keyCodes[codeIndex],
636 &scanCodes);
637 if (!err) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800638 // check the possible scan codes identified by the layout map against the
639 // map of codes actually emitted by the driver
640 for (size_t sc = 0; sc < scanCodes.size(); sc++) {
Chris Ye66fbac32020-07-06 20:36:43 -0700641 if (device->keyBitmask.test(scanCodes[sc])) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800642 outFlags[codeIndex] = 1;
643 break;
644 }
645 }
646 }
647 }
648 return true;
649 }
650 return false;
651}
652
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700653status_t EventHub::mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
654 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const {
Chris Ye87143712020-11-10 05:05:58 +0000655 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800656 Device* device = getDeviceLocked(deviceId);
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700657 status_t status = NAME_NOT_FOUND;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800658
Chris Ye66fbac32020-07-06 20:36:43 -0700659 if (device != nullptr) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800660 // Check the key character map first.
Chris Ye3a1e4462020-08-12 10:13:15 -0700661 const std::shared_ptr<KeyCharacterMap> kcm = device->getKeyCharacterMap();
662 if (kcm) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800663 if (!kcm->mapKey(scanCode, usageCode, outKeycode)) {
664 *outFlags = 0;
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700665 status = NO_ERROR;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800666 }
667 }
668
669 // Check the key layout next.
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700670 if (status != NO_ERROR && device->keyMap.haveKeyLayout()) {
Siarhei Vishniakou592bac22018-11-08 19:42:38 -0800671 if (!device->keyMap.keyLayoutMap->mapKey(scanCode, usageCode, outKeycode, outFlags)) {
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700672 status = NO_ERROR;
673 }
674 }
675
676 if (status == NO_ERROR) {
Chris Ye3a1e4462020-08-12 10:13:15 -0700677 if (kcm) {
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700678 kcm->tryRemapKey(*outKeycode, metaState, outKeycode, outMetaState);
679 } else {
680 *outMetaState = metaState;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800681 }
682 }
683 }
684
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700685 if (status != NO_ERROR) {
686 *outKeycode = 0;
687 *outFlags = 0;
688 *outMetaState = metaState;
689 }
690
691 return status;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800692}
693
694status_t EventHub::mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const {
Chris Ye87143712020-11-10 05:05:58 +0000695 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800696 Device* device = getDeviceLocked(deviceId);
697
Chris Ye66fbac32020-07-06 20:36:43 -0700698 if (device != nullptr && device->keyMap.haveKeyLayout()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800699 status_t err = device->keyMap.keyLayoutMap->mapAxis(scanCode, outAxisInfo);
700 if (err == NO_ERROR) {
701 return NO_ERROR;
702 }
703 }
704
705 return NAME_NOT_FOUND;
706}
707
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100708void EventHub::setExcludedDevices(const std::vector<std::string>& devices) {
Chris Ye87143712020-11-10 05:05:58 +0000709 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800710
711 mExcludedDevices = devices;
712}
713
714bool EventHub::hasScanCode(int32_t deviceId, int32_t scanCode) const {
Chris Ye87143712020-11-10 05:05:58 +0000715 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800716 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -0700717 if (device != nullptr && scanCode >= 0 && scanCode <= KEY_MAX) {
718 return device->keyBitmask.test(scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800719 }
720 return false;
721}
722
723bool EventHub::hasLed(int32_t deviceId, int32_t led) const {
Chris Ye87143712020-11-10 05:05:58 +0000724 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800725 Device* device = getDeviceLocked(deviceId);
726 int32_t sc;
Chris Ye989bb932020-07-04 16:18:59 -0700727 if (device != nullptr && device->mapLed(led, &sc) == NO_ERROR) {
Chris Ye66fbac32020-07-06 20:36:43 -0700728 return device->ledBitmask.test(sc);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800729 }
730 return false;
731}
732
733void EventHub::setLedState(int32_t deviceId, int32_t led, bool on) {
Chris Ye87143712020-11-10 05:05:58 +0000734 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800735 Device* device = getDeviceLocked(deviceId);
Chris Ye989bb932020-07-04 16:18:59 -0700736 if (device != nullptr && device->hasValidFd()) {
737 device->setLedStateLocked(led, on);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800738 }
739}
740
741void EventHub::getVirtualKeyDefinitions(int32_t deviceId,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700742 std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800743 outVirtualKeys.clear();
744
Chris Ye87143712020-11-10 05:05:58 +0000745 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800746 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -0700747 if (device != nullptr && device->virtualKeyMap) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800748 const std::vector<VirtualKeyDefinition> virtualKeys =
749 device->virtualKeyMap->getVirtualKeys();
750 outVirtualKeys.insert(outVirtualKeys.end(), virtualKeys.begin(), virtualKeys.end());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800751 }
752}
753
Chris Ye3a1e4462020-08-12 10:13:15 -0700754const std::shared_ptr<KeyCharacterMap> EventHub::getKeyCharacterMap(int32_t deviceId) const {
Chris Ye87143712020-11-10 05:05:58 +0000755 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800756 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -0700757 if (device != nullptr) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800758 return device->getKeyCharacterMap();
759 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700760 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800761}
762
Chris Ye3a1e4462020-08-12 10:13:15 -0700763bool EventHub::setKeyboardLayoutOverlay(int32_t deviceId, std::shared_ptr<KeyCharacterMap> map) {
Chris Ye87143712020-11-10 05:05:58 +0000764 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800765 Device* device = getDeviceLocked(deviceId);
Chris Ye3a1e4462020-08-12 10:13:15 -0700766 if (device != nullptr && map != nullptr && device->keyMap.keyCharacterMap != nullptr) {
767 device->keyMap.keyCharacterMap->combine(*map);
768 device->keyMap.keyCharacterMapFile = device->keyMap.keyCharacterMap->getLoadFileName();
769 return true;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800770 }
771 return false;
772}
773
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100774static std::string generateDescriptor(InputDeviceIdentifier& identifier) {
775 std::string rawDescriptor;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700776 rawDescriptor += StringPrintf(":%04x:%04x:", identifier.vendor, identifier.product);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800777 // TODO add handling for USB devices to not uniqueify kbs that show up twice
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100778 if (!identifier.uniqueId.empty()) {
779 rawDescriptor += "uniqueId:";
780 rawDescriptor += identifier.uniqueId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800781 } else if (identifier.nonce != 0) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100782 rawDescriptor += StringPrintf("nonce:%04x", identifier.nonce);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800783 }
784
785 if (identifier.vendor == 0 && identifier.product == 0) {
786 // If we don't know the vendor and product id, then the device is probably
787 // built-in so we need to rely on other information to uniquely identify
788 // the input device. Usually we try to avoid relying on the device name or
789 // location but for built-in input device, they are unlikely to ever change.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100790 if (!identifier.name.empty()) {
791 rawDescriptor += "name:";
792 rawDescriptor += identifier.name;
793 } else if (!identifier.location.empty()) {
794 rawDescriptor += "location:";
795 rawDescriptor += identifier.location;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800796 }
797 }
798 identifier.descriptor = sha1(rawDescriptor);
799 return rawDescriptor;
800}
801
802void EventHub::assignDescriptorLocked(InputDeviceIdentifier& identifier) {
803 // Compute a device descriptor that uniquely identifies the device.
804 // The descriptor is assumed to be a stable identifier. Its value should not
805 // change between reboots, reconnections, firmware updates or new releases
806 // of Android. In practice we sometimes get devices that cannot be uniquely
807 // identified. In this case we enforce uniqueness between connected devices.
808 // Ideally, we also want the descriptor to be short and relatively opaque.
809
810 identifier.nonce = 0;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100811 std::string rawDescriptor = generateDescriptor(identifier);
812 if (identifier.uniqueId.empty()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800813 // If it didn't have a unique id check for conflicts and enforce
814 // uniqueness if necessary.
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700815 while (getDeviceByDescriptorLocked(identifier.descriptor) != nullptr) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800816 identifier.nonce++;
817 rawDescriptor = generateDescriptor(identifier);
818 }
819 }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100820 ALOGV("Created descriptor: raw=%s, cooked=%s", rawDescriptor.c_str(),
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700821 identifier.descriptor.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800822}
823
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000824void EventHub::vibrate(int32_t deviceId, const VibrationElement& element) {
Chris Ye87143712020-11-10 05:05:58 +0000825 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800826 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -0700827 if (device != nullptr && device->hasValidFd()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800828 ff_effect effect;
829 memset(&effect, 0, sizeof(effect));
830 effect.type = FF_RUMBLE;
831 effect.id = device->ffEffectId;
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000832 // evdev FF_RUMBLE effect only supports two channels of vibration.
Chris Ye6393a262020-08-04 19:41:36 -0700833 effect.u.rumble.strong_magnitude = element.getMagnitude(FF_STRONG_MAGNITUDE_CHANNEL_IDX);
834 effect.u.rumble.weak_magnitude = element.getMagnitude(FF_WEAK_MAGNITUDE_CHANNEL_IDX);
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000835 effect.replay.length = element.duration.count();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800836 effect.replay.delay = 0;
837 if (ioctl(device->fd, EVIOCSFF, &effect)) {
838 ALOGW("Could not upload force feedback effect to device %s due to error %d.",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700839 device->identifier.name.c_str(), errno);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800840 return;
841 }
842 device->ffEffectId = effect.id;
843
844 struct input_event ev;
845 ev.time.tv_sec = 0;
846 ev.time.tv_usec = 0;
847 ev.type = EV_FF;
848 ev.code = device->ffEffectId;
849 ev.value = 1;
850 if (write(device->fd, &ev, sizeof(ev)) != sizeof(ev)) {
851 ALOGW("Could not start force feedback effect on device %s due to error %d.",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700852 device->identifier.name.c_str(), errno);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800853 return;
854 }
855 device->ffEffectPlaying = true;
856 }
857}
858
859void EventHub::cancelVibrate(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +0000860 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800861 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -0700862 if (device != nullptr && device->hasValidFd()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800863 if (device->ffEffectPlaying) {
864 device->ffEffectPlaying = false;
865
866 struct input_event ev;
867 ev.time.tv_sec = 0;
868 ev.time.tv_usec = 0;
869 ev.type = EV_FF;
870 ev.code = device->ffEffectId;
871 ev.value = 0;
872 if (write(device->fd, &ev, sizeof(ev)) != sizeof(ev)) {
873 ALOGW("Could not stop force feedback effect on device %s due to error %d.",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700874 device->identifier.name.c_str(), errno);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800875 return;
876 }
877 }
878 }
879}
880
Chris Ye87143712020-11-10 05:05:58 +0000881std::vector<int32_t> EventHub::getVibratorIds(int32_t deviceId) {
882 std::scoped_lock _l(mLock);
883 std::vector<int32_t> vibrators;
884 Device* device = getDeviceLocked(deviceId);
885 if (device != nullptr && device->hasValidFd() &&
886 device->classes.test(InputDeviceClass::VIBRATOR)) {
887 vibrators.push_back(FF_STRONG_MAGNITUDE_CHANNEL_IDX);
888 vibrators.push_back(FF_WEAK_MAGNITUDE_CHANNEL_IDX);
889 }
890 return vibrators;
891}
892
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100893EventHub::Device* EventHub::getDeviceByDescriptorLocked(const std::string& descriptor) const {
Chris Ye989bb932020-07-04 16:18:59 -0700894 for (const auto& [id, device] : mDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100895 if (descriptor == device->identifier.descriptor) {
Chris Ye989bb932020-07-04 16:18:59 -0700896 return device.get();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800897 }
898 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700899 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800900}
901
902EventHub::Device* EventHub::getDeviceLocked(int32_t deviceId) const {
Prabir Pradhancae4b3a2019-02-05 18:51:32 -0800903 if (deviceId == ReservedInputDeviceId::BUILT_IN_KEYBOARD_ID) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800904 deviceId = mBuiltInKeyboardId;
905 }
Chris Ye989bb932020-07-04 16:18:59 -0700906 const auto& it = mDevices.find(deviceId);
907 return it != mDevices.end() ? it->second.get() : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800908}
909
Chris Ye8594e192020-07-14 10:34:06 -0700910EventHub::Device* EventHub::getDeviceByPathLocked(const std::string& devicePath) const {
Chris Ye989bb932020-07-04 16:18:59 -0700911 for (const auto& [id, device] : mDevices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800912 if (device->path == devicePath) {
Chris Ye989bb932020-07-04 16:18:59 -0700913 return device.get();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800914 }
915 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700916 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800917}
918
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700919/**
920 * The file descriptor could be either input device, or a video device (associated with a
921 * specific input device). Check both cases here, and return the device that this event
922 * belongs to. Caller can compare the fd's once more to determine event type.
923 * Looks through all input devices, and only attached video devices. Unattached video
924 * devices are ignored.
925 */
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -0700926EventHub::Device* EventHub::getDeviceByFdLocked(int fd) const {
Chris Ye989bb932020-07-04 16:18:59 -0700927 for (const auto& [id, device] : mDevices) {
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -0700928 if (device->fd == fd) {
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700929 // This is an input device event
Chris Ye989bb932020-07-04 16:18:59 -0700930 return device.get();
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700931 }
932 if (device->videoDevice && device->videoDevice->getFd() == fd) {
933 // This is a video device event
Chris Ye989bb932020-07-04 16:18:59 -0700934 return device.get();
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -0700935 }
936 }
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700937 // We do not check mUnattachedVideoDevices here because they should not participate in epoll,
938 // and therefore should never be looked up by fd.
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -0700939 return nullptr;
940}
941
Michael Wrightd02c5b62014-02-10 15:10:22 -0800942size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) {
943 ALOG_ASSERT(bufferSize >= 1);
944
Chris Ye87143712020-11-10 05:05:58 +0000945 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800946
947 struct input_event readBuffer[bufferSize];
948
949 RawEvent* event = buffer;
950 size_t capacity = bufferSize;
951 bool awoken = false;
952 for (;;) {
953 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
954
955 // Reopen input devices if needed.
956 if (mNeedToReopenDevices) {
957 mNeedToReopenDevices = false;
958
959 ALOGI("Reopening all input devices due to a configuration change.");
960
961 closeAllDevicesLocked();
962 mNeedToScanDevices = true;
963 break; // return to the caller before we actually rescan
964 }
965
966 // Report any devices that had last been added/removed.
Chris Ye989bb932020-07-04 16:18:59 -0700967 for (auto it = mClosingDevices.begin(); it != mClosingDevices.end();) {
968 std::unique_ptr<Device> device = std::move(*it);
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700969 ALOGV("Reporting device closed: id=%d, name=%s\n", device->id, device->path.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800970 event->when = now;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700971 event->deviceId = (device->id == mBuiltInKeyboardId)
972 ? ReservedInputDeviceId::BUILT_IN_KEYBOARD_ID
973 : device->id;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800974 event->type = DEVICE_REMOVED;
975 event += 1;
Chris Ye989bb932020-07-04 16:18:59 -0700976 it = mClosingDevices.erase(it);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800977 mNeedToSendFinishedDeviceScan = true;
978 if (--capacity == 0) {
979 break;
980 }
981 }
982
983 if (mNeedToScanDevices) {
984 mNeedToScanDevices = false;
985 scanDevicesLocked();
986 mNeedToSendFinishedDeviceScan = true;
987 }
988
Siarhei Vishniakouf49608d2020-08-20 19:18:21 -0500989 while (!mOpeningDevices.empty()) {
990 std::unique_ptr<Device> device = std::move(*mOpeningDevices.rbegin());
991 mOpeningDevices.pop_back();
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700992 ALOGV("Reporting device opened: id=%d, name=%s\n", device->id, device->path.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800993 event->when = now;
994 event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
995 event->type = DEVICE_ADDED;
996 event += 1;
Siarhei Vishniakouf49608d2020-08-20 19:18:21 -0500997
998 // Try to find a matching video device by comparing device names
999 for (auto it = mUnattachedVideoDevices.begin(); it != mUnattachedVideoDevices.end();
1000 it++) {
1001 std::unique_ptr<TouchVideoDevice>& videoDevice = *it;
1002 if (tryAddVideoDevice(*device, videoDevice)) {
1003 // videoDevice was transferred to 'device'
1004 it = mUnattachedVideoDevices.erase(it);
1005 break;
1006 }
1007 }
1008
1009 auto [dev_it, inserted] = mDevices.insert_or_assign(device->id, std::move(device));
1010 if (!inserted) {
Chris Ye989bb932020-07-04 16:18:59 -07001011 ALOGW("Device id %d exists, replaced.", device->id);
1012 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001013 mNeedToSendFinishedDeviceScan = true;
1014 if (--capacity == 0) {
1015 break;
1016 }
1017 }
1018
1019 if (mNeedToSendFinishedDeviceScan) {
1020 mNeedToSendFinishedDeviceScan = false;
1021 event->when = now;
1022 event->type = FINISHED_DEVICE_SCAN;
1023 event += 1;
1024 if (--capacity == 0) {
1025 break;
1026 }
1027 }
1028
1029 // Grab the next input event.
1030 bool deviceChanged = false;
1031 while (mPendingEventIndex < mPendingEventCount) {
1032 const struct epoll_event& eventItem = mPendingEventItems[mPendingEventIndex++];
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -07001033 if (eventItem.data.fd == mINotifyFd) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001034 if (eventItem.events & EPOLLIN) {
1035 mPendingINotify = true;
1036 } else {
1037 ALOGW("Received unexpected epoll event 0x%08x for INotify.", eventItem.events);
1038 }
1039 continue;
1040 }
1041
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -07001042 if (eventItem.data.fd == mWakeReadPipeFd) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001043 if (eventItem.events & EPOLLIN) {
1044 ALOGV("awoken after wake()");
1045 awoken = true;
Siarhei Vishniakoub4d960d2019-10-03 15:38:44 -05001046 char wakeReadBuffer[16];
Michael Wrightd02c5b62014-02-10 15:10:22 -08001047 ssize_t nRead;
1048 do {
Siarhei Vishniakoub4d960d2019-10-03 15:38:44 -05001049 nRead = read(mWakeReadPipeFd, wakeReadBuffer, sizeof(wakeReadBuffer));
1050 } while ((nRead == -1 && errno == EINTR) || nRead == sizeof(wakeReadBuffer));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001051 } else {
1052 ALOGW("Received unexpected epoll event 0x%08x for wake read pipe.",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001053 eventItem.events);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001054 }
1055 continue;
1056 }
1057
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -07001058 Device* device = getDeviceByFdLocked(eventItem.data.fd);
Chris Ye989bb932020-07-04 16:18:59 -07001059 if (device == nullptr) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001060 ALOGE("Received unexpected epoll event 0x%08x for unknown fd %d.", eventItem.events,
1061 eventItem.data.fd);
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001062 ALOG_ASSERT(!DEBUG);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001063 continue;
1064 }
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001065 if (device->videoDevice && eventItem.data.fd == device->videoDevice->getFd()) {
1066 if (eventItem.events & EPOLLIN) {
1067 size_t numFrames = device->videoDevice->readAndQueueFrames();
1068 if (numFrames == 0) {
1069 ALOGE("Received epoll event for video device %s, but could not read frame",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001070 device->videoDevice->getName().c_str());
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001071 }
1072 } else if (eventItem.events & EPOLLHUP) {
1073 // TODO(b/121395353) - consider adding EPOLLRDHUP
1074 ALOGI("Removing video device %s due to epoll hang-up event.",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001075 device->videoDevice->getName().c_str());
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001076 unregisterVideoDeviceFromEpollLocked(*device->videoDevice);
1077 device->videoDevice = nullptr;
1078 } else {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001079 ALOGW("Received unexpected epoll event 0x%08x for device %s.", eventItem.events,
1080 device->videoDevice->getName().c_str());
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001081 ALOG_ASSERT(!DEBUG);
1082 }
1083 continue;
1084 }
1085 // This must be an input event
Michael Wrightd02c5b62014-02-10 15:10:22 -08001086 if (eventItem.events & EPOLLIN) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001087 int32_t readSize =
1088 read(device->fd, readBuffer, sizeof(struct input_event) * capacity);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001089 if (readSize == 0 || (readSize < 0 && errno == ENODEV)) {
1090 // Device was removed before INotify noticed.
Mark Salyzyn5aa26b22014-06-10 13:07:44 -07001091 ALOGW("could not get event, removed? (fd: %d size: %" PRId32
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001092 " bufferSize: %zu capacity: %zu errno: %d)\n",
1093 device->fd, readSize, bufferSize, capacity, errno);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001094 deviceChanged = true;
Chris Ye989bb932020-07-04 16:18:59 -07001095 closeDeviceLocked(*device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001096 } else if (readSize < 0) {
1097 if (errno != EAGAIN && errno != EINTR) {
1098 ALOGW("could not get event (errno=%d)", errno);
1099 }
1100 } else if ((readSize % sizeof(struct input_event)) != 0) {
1101 ALOGE("could not get event (wrong size: %d)", readSize);
1102 } else {
1103 int32_t deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
1104
1105 size_t count = size_t(readSize) / sizeof(struct input_event);
1106 for (size_t i = 0; i < count; i++) {
1107 struct input_event& iev = readBuffer[i];
Siarhei Vishniakou592bac22018-11-08 19:42:38 -08001108 event->when = processEventTimestamp(iev);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001109 event->deviceId = deviceId;
1110 event->type = iev.type;
1111 event->code = iev.code;
1112 event->value = iev.value;
1113 event += 1;
1114 capacity -= 1;
1115 }
1116 if (capacity == 0) {
1117 // The result buffer is full. Reset the pending event index
1118 // so we will try to read the device again on the next iteration.
1119 mPendingEventIndex -= 1;
1120 break;
1121 }
1122 }
1123 } else if (eventItem.events & EPOLLHUP) {
1124 ALOGI("Removing device %s due to epoll hang-up event.",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001125 device->identifier.name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001126 deviceChanged = true;
Chris Ye989bb932020-07-04 16:18:59 -07001127 closeDeviceLocked(*device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001128 } else {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001129 ALOGW("Received unexpected epoll event 0x%08x for device %s.", eventItem.events,
1130 device->identifier.name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001131 }
1132 }
1133
1134 // readNotify() will modify the list of devices so this must be done after
1135 // processing all other events to ensure that we read all remaining events
1136 // before closing the devices.
1137 if (mPendingINotify && mPendingEventIndex >= mPendingEventCount) {
1138 mPendingINotify = false;
1139 readNotifyLocked();
1140 deviceChanged = true;
1141 }
1142
1143 // Report added or removed devices immediately.
1144 if (deviceChanged) {
1145 continue;
1146 }
1147
1148 // Return now if we have collected any events or if we were explicitly awoken.
1149 if (event != buffer || awoken) {
1150 break;
1151 }
1152
Siarhei Vishniakou4b5a87f2019-09-24 13:03:58 +01001153 // Poll for events.
1154 // When a device driver has pending (unread) events, it acquires
1155 // a kernel wake lock. Once the last pending event has been read, the device
1156 // driver will release the kernel wake lock, but the epoll will hold the wakelock,
1157 // since we are using EPOLLWAKEUP. The wakelock is released by the epoll when epoll_wait
1158 // is called again for the same fd that produced the event.
1159 // Thus the system can only sleep if there are no events pending or
1160 // currently being processed.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001161 //
1162 // The timeout is advisory only. If the device is asleep, it will not wake just to
1163 // service the timeout.
1164 mPendingEventIndex = 0;
1165
Siarhei Vishniakou4b5a87f2019-09-24 13:03:58 +01001166 mLock.unlock(); // release lock before poll
Michael Wrightd02c5b62014-02-10 15:10:22 -08001167
1168 int pollResult = epoll_wait(mEpollFd, mPendingEventItems, EPOLL_MAX_EVENTS, timeoutMillis);
1169
Siarhei Vishniakou4b5a87f2019-09-24 13:03:58 +01001170 mLock.lock(); // reacquire lock after poll
Michael Wrightd02c5b62014-02-10 15:10:22 -08001171
1172 if (pollResult == 0) {
1173 // Timed out.
1174 mPendingEventCount = 0;
1175 break;
1176 }
1177
1178 if (pollResult < 0) {
1179 // An error occurred.
1180 mPendingEventCount = 0;
1181
1182 // Sleep after errors to avoid locking up the system.
1183 // Hopefully the error is transient.
1184 if (errno != EINTR) {
1185 ALOGW("poll failed (errno=%d)\n", errno);
1186 usleep(100000);
1187 }
1188 } else {
1189 // Some events occurred.
1190 mPendingEventCount = size_t(pollResult);
1191 }
1192 }
1193
1194 // All done, return the number of events we read.
1195 return event - buffer;
1196}
1197
Siarhei Vishniakouadd89292018-12-13 19:23:36 -08001198std::vector<TouchVideoFrame> EventHub::getVideoFrames(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +00001199 std::scoped_lock _l(mLock);
Siarhei Vishniakouadd89292018-12-13 19:23:36 -08001200
1201 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -07001202 if (device == nullptr || !device->videoDevice) {
Siarhei Vishniakouadd89292018-12-13 19:23:36 -08001203 return {};
1204 }
1205 return device->videoDevice->consumeFrames();
1206}
1207
Michael Wrightd02c5b62014-02-10 15:10:22 -08001208void EventHub::wake() {
1209 ALOGV("wake() called");
1210
1211 ssize_t nWrite;
1212 do {
1213 nWrite = write(mWakeWritePipeFd, "W", 1);
1214 } while (nWrite == -1 && errno == EINTR);
1215
1216 if (nWrite != 1 && errno != EAGAIN) {
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001217 ALOGW("Could not write wake signal: %s", strerror(errno));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001218 }
1219}
1220
1221void EventHub::scanDevicesLocked() {
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001222 status_t result = scanDirLocked(DEVICE_PATH);
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001223 if (result < 0) {
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001224 ALOGE("scan dir failed for %s", DEVICE_PATH);
1225 }
Philip Quinn39b81682019-01-09 22:20:39 -08001226 if (isV4lScanningEnabled()) {
1227 result = scanVideoDirLocked(VIDEO_DEVICE_PATH);
1228 if (result != OK) {
1229 ALOGE("scan video dir failed for %s", VIDEO_DEVICE_PATH);
1230 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001231 }
Chris Ye989bb932020-07-04 16:18:59 -07001232 if (mDevices.find(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID) == mDevices.end()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001233 createVirtualKeyboardLocked();
1234 }
1235}
1236
1237// ----------------------------------------------------------------------------
1238
Michael Wrightd02c5b62014-02-10 15:10:22 -08001239static const int32_t GAMEPAD_KEYCODES[] = {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001240 AKEYCODE_BUTTON_A, AKEYCODE_BUTTON_B, AKEYCODE_BUTTON_C, //
1241 AKEYCODE_BUTTON_X, AKEYCODE_BUTTON_Y, AKEYCODE_BUTTON_Z, //
1242 AKEYCODE_BUTTON_L1, AKEYCODE_BUTTON_R1, //
1243 AKEYCODE_BUTTON_L2, AKEYCODE_BUTTON_R2, //
1244 AKEYCODE_BUTTON_THUMBL, AKEYCODE_BUTTON_THUMBR, //
1245 AKEYCODE_BUTTON_START, AKEYCODE_BUTTON_SELECT, AKEYCODE_BUTTON_MODE, //
Michael Wrightd02c5b62014-02-10 15:10:22 -08001246};
1247
Siarhei Vishniakou25920312018-12-12 15:24:44 -08001248status_t EventHub::registerFdForEpoll(int fd) {
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001249 // TODO(b/121395353) - consider adding EPOLLRDHUP
Siarhei Vishniakou25920312018-12-12 15:24:44 -08001250 struct epoll_event eventItem = {};
1251 eventItem.events = EPOLLIN | EPOLLWAKEUP;
1252 eventItem.data.fd = fd;
1253 if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, &eventItem)) {
1254 ALOGE("Could not add fd to epoll instance: %s", strerror(errno));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001255 return -errno;
1256 }
1257 return OK;
1258}
1259
Siarhei Vishniakou25920312018-12-12 15:24:44 -08001260status_t EventHub::unregisterFdFromEpoll(int fd) {
1261 if (epoll_ctl(mEpollFd, EPOLL_CTL_DEL, fd, nullptr)) {
1262 ALOGW("Could not remove fd from epoll instance: %s", strerror(errno));
1263 return -errno;
1264 }
1265 return OK;
1266}
1267
Chris Ye989bb932020-07-04 16:18:59 -07001268status_t EventHub::registerDeviceForEpollLocked(Device& device) {
1269 status_t result = registerFdForEpoll(device.fd);
Siarhei Vishniakou25920312018-12-12 15:24:44 -08001270 if (result != OK) {
Chris Ye989bb932020-07-04 16:18:59 -07001271 ALOGE("Could not add input device fd to epoll for device %" PRId32, device.id);
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001272 return result;
1273 }
Chris Ye989bb932020-07-04 16:18:59 -07001274 if (device.videoDevice) {
1275 registerVideoDeviceForEpollLocked(*device.videoDevice);
Siarhei Vishniakou25920312018-12-12 15:24:44 -08001276 }
1277 return result;
1278}
1279
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001280void EventHub::registerVideoDeviceForEpollLocked(const TouchVideoDevice& videoDevice) {
1281 status_t result = registerFdForEpoll(videoDevice.getFd());
1282 if (result != OK) {
1283 ALOGE("Could not add video device %s to epoll", videoDevice.getName().c_str());
1284 }
1285}
1286
Chris Ye989bb932020-07-04 16:18:59 -07001287status_t EventHub::unregisterDeviceFromEpollLocked(Device& device) {
1288 if (device.hasValidFd()) {
1289 status_t result = unregisterFdFromEpoll(device.fd);
Siarhei Vishniakou25920312018-12-12 15:24:44 -08001290 if (result != OK) {
Chris Ye989bb932020-07-04 16:18:59 -07001291 ALOGW("Could not remove input device fd from epoll for device %" PRId32, device.id);
Siarhei Vishniakou25920312018-12-12 15:24:44 -08001292 return result;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001293 }
1294 }
Chris Ye989bb932020-07-04 16:18:59 -07001295 if (device.videoDevice) {
1296 unregisterVideoDeviceFromEpollLocked(*device.videoDevice);
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001297 }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001298 return OK;
1299}
1300
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001301void EventHub::unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& videoDevice) {
1302 if (videoDevice.hasValidFd()) {
1303 status_t result = unregisterFdFromEpoll(videoDevice.getFd());
1304 if (result != OK) {
1305 ALOGW("Could not remove video device fd from epoll for device: %s",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001306 videoDevice.getName().c_str());
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001307 }
1308 }
1309}
1310
Chris Ye8594e192020-07-14 10:34:06 -07001311status_t EventHub::openDeviceLocked(const std::string& devicePath) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001312 char buffer[80];
1313
Chris Ye8594e192020-07-14 10:34:06 -07001314 ALOGV("Opening device: %s", devicePath.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001315
Chris Ye8594e192020-07-14 10:34:06 -07001316 int fd = open(devicePath.c_str(), O_RDWR | O_CLOEXEC | O_NONBLOCK);
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001317 if (fd < 0) {
Chris Ye8594e192020-07-14 10:34:06 -07001318 ALOGE("could not open %s, %s\n", devicePath.c_str(), strerror(errno));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001319 return -1;
1320 }
1321
1322 InputDeviceIdentifier identifier;
1323
1324 // Get device name.
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001325 if (ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) {
Chris Ye8594e192020-07-14 10:34:06 -07001326 ALOGE("Could not get device name for %s: %s", devicePath.c_str(), strerror(errno));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001327 } else {
1328 buffer[sizeof(buffer) - 1] = '\0';
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001329 identifier.name = buffer;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001330 }
1331
1332 // Check to see if the device is on our excluded list
1333 for (size_t i = 0; i < mExcludedDevices.size(); i++) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001334 const std::string& item = mExcludedDevices[i];
Michael Wrightd02c5b62014-02-10 15:10:22 -08001335 if (identifier.name == item) {
Chris Ye8594e192020-07-14 10:34:06 -07001336 ALOGI("ignoring event id %s driver %s\n", devicePath.c_str(), item.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001337 close(fd);
1338 return -1;
1339 }
1340 }
1341
1342 // Get device driver version.
1343 int driverVersion;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001344 if (ioctl(fd, EVIOCGVERSION, &driverVersion)) {
Chris Ye8594e192020-07-14 10:34:06 -07001345 ALOGE("could not get driver version for %s, %s\n", devicePath.c_str(), strerror(errno));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001346 close(fd);
1347 return -1;
1348 }
1349
1350 // Get device identifier.
1351 struct input_id inputId;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001352 if (ioctl(fd, EVIOCGID, &inputId)) {
Chris Ye8594e192020-07-14 10:34:06 -07001353 ALOGE("could not get device input id for %s, %s\n", devicePath.c_str(), strerror(errno));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001354 close(fd);
1355 return -1;
1356 }
1357 identifier.bus = inputId.bustype;
1358 identifier.product = inputId.product;
1359 identifier.vendor = inputId.vendor;
1360 identifier.version = inputId.version;
1361
1362 // Get device physical location.
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001363 if (ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) {
1364 // fprintf(stderr, "could not get location for %s, %s\n", devicePath, strerror(errno));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001365 } else {
1366 buffer[sizeof(buffer) - 1] = '\0';
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001367 identifier.location = buffer;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001368 }
1369
1370 // Get device unique id.
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001371 if (ioctl(fd, EVIOCGUNIQ(sizeof(buffer) - 1), &buffer) < 1) {
1372 // fprintf(stderr, "could not get idstring for %s, %s\n", devicePath, strerror(errno));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001373 } else {
1374 buffer[sizeof(buffer) - 1] = '\0';
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001375 identifier.uniqueId = buffer;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001376 }
1377
1378 // Fill in the descriptor.
1379 assignDescriptorLocked(identifier);
1380
Michael Wrightd02c5b62014-02-10 15:10:22 -08001381 // Allocate device. (The device object takes ownership of the fd at this point.)
1382 int32_t deviceId = mNextDeviceId++;
Chris Ye989bb932020-07-04 16:18:59 -07001383 std::unique_ptr<Device> device = std::make_unique<Device>(fd, deviceId, devicePath, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001384
Chris Ye8594e192020-07-14 10:34:06 -07001385 ALOGV("add device %d: %s\n", deviceId, devicePath.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001386 ALOGV(" bus: %04x\n"
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001387 " vendor %04x\n"
1388 " product %04x\n"
1389 " version %04x\n",
1390 identifier.bus, identifier.vendor, identifier.product, identifier.version);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001391 ALOGV(" name: \"%s\"\n", identifier.name.c_str());
1392 ALOGV(" location: \"%s\"\n", identifier.location.c_str());
1393 ALOGV(" unique id: \"%s\"\n", identifier.uniqueId.c_str());
1394 ALOGV(" descriptor: \"%s\"\n", identifier.descriptor.c_str());
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001395 ALOGV(" driver: v%d.%d.%d\n", driverVersion >> 16, (driverVersion >> 8) & 0xff,
1396 driverVersion & 0xff);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001397
1398 // Load the configuration file for the device.
Chris Ye989bb932020-07-04 16:18:59 -07001399 device->loadConfigurationLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001400
1401 // Figure out the kinds of events the device reports.
Chris Ye66fbac32020-07-06 20:36:43 -07001402 device->readDeviceBitMask(EVIOCGBIT(EV_KEY, 0), device->keyBitmask);
1403 device->readDeviceBitMask(EVIOCGBIT(EV_ABS, 0), device->absBitmask);
1404 device->readDeviceBitMask(EVIOCGBIT(EV_REL, 0), device->relBitmask);
1405 device->readDeviceBitMask(EVIOCGBIT(EV_SW, 0), device->swBitmask);
1406 device->readDeviceBitMask(EVIOCGBIT(EV_LED, 0), device->ledBitmask);
1407 device->readDeviceBitMask(EVIOCGBIT(EV_FF, 0), device->ffBitmask);
1408 device->readDeviceBitMask(EVIOCGPROP(0), device->propBitmask);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001409
1410 // See if this is a keyboard. Ignore everything in the button range except for
1411 // joystick and gamepad buttons which are handled like keyboards for the most part.
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001412 bool haveKeyboardKeys =
Chris Ye66fbac32020-07-06 20:36:43 -07001413 device->keyBitmask.any(0, BTN_MISC) || device->keyBitmask.any(BTN_WHEEL, KEY_MAX + 1);
1414 bool haveGamepadButtons = device->keyBitmask.any(BTN_MISC, BTN_MOUSE) ||
1415 device->keyBitmask.any(BTN_JOYSTICK, BTN_DIGI);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001416 if (haveKeyboardKeys || haveGamepadButtons) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001417 device->classes |= InputDeviceClass::KEYBOARD;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001418 }
1419
1420 // See if this is a cursor device such as a trackball or mouse.
Chris Ye66fbac32020-07-06 20:36:43 -07001421 if (device->keyBitmask.test(BTN_MOUSE) && device->relBitmask.test(REL_X) &&
1422 device->relBitmask.test(REL_Y)) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001423 device->classes |= InputDeviceClass::CURSOR;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001424 }
1425
Prashant Malani1941ff52015-08-11 18:29:28 -07001426 // See if this is a rotary encoder type device.
1427 String8 deviceType = String8();
1428 if (device->configuration &&
1429 device->configuration->tryGetProperty(String8("device.type"), deviceType)) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001430 if (!deviceType.compare(String8("rotaryEncoder"))) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001431 device->classes |= InputDeviceClass::ROTARY_ENCODER;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001432 }
Prashant Malani1941ff52015-08-11 18:29:28 -07001433 }
1434
Michael Wrightd02c5b62014-02-10 15:10:22 -08001435 // See if this is a touch pad.
1436 // Is this a new modern multi-touch driver?
Chris Ye66fbac32020-07-06 20:36:43 -07001437 if (device->absBitmask.test(ABS_MT_POSITION_X) && device->absBitmask.test(ABS_MT_POSITION_Y)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001438 // Some joysticks such as the PS3 controller report axes that conflict
1439 // with the ABS_MT range. Try to confirm that the device really is
1440 // a touch screen.
Chris Ye66fbac32020-07-06 20:36:43 -07001441 if (device->keyBitmask.test(BTN_TOUCH) || !haveGamepadButtons) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001442 device->classes |= (InputDeviceClass::TOUCH | InputDeviceClass::TOUCH_MT);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001443 }
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001444 // Is this an old style single-touch driver?
Chris Ye66fbac32020-07-06 20:36:43 -07001445 } else if (device->keyBitmask.test(BTN_TOUCH) && device->absBitmask.test(ABS_X) &&
1446 device->absBitmask.test(ABS_Y)) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001447 device->classes |= InputDeviceClass::TOUCH;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001448 // Is this a BT stylus?
Chris Ye66fbac32020-07-06 20:36:43 -07001449 } else if ((device->absBitmask.test(ABS_PRESSURE) || device->keyBitmask.test(BTN_TOUCH)) &&
1450 !device->absBitmask.test(ABS_X) && !device->absBitmask.test(ABS_Y)) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001451 device->classes |= InputDeviceClass::EXTERNAL_STYLUS;
Michael Wright842500e2015-03-13 17:32:02 -07001452 // Keyboard will try to claim some of the buttons but we really want to reserve those so we
1453 // can fuse it with the touch screen data, so just take them back. Note this means an
1454 // external stylus cannot also be a keyboard device.
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 device is a joystick.
1459 // Assumes that joysticks always have gamepad buttons in order to distinguish them
1460 // from other devices such as accelerometers that also have absolute axes.
1461 if (haveGamepadButtons) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001462 auto assumedClasses = device->classes | InputDeviceClass::JOYSTICK;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001463 for (int i = 0; i <= ABS_MAX; i++) {
Chris Ye66fbac32020-07-06 20:36:43 -07001464 if (device->absBitmask.test(i) &&
Chris Ye1b0c7342020-07-28 21:57:03 -07001465 (getAbsAxisUsage(i, assumedClasses).test(InputDeviceClass::JOYSTICK))) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001466 device->classes = assumedClasses;
1467 break;
1468 }
1469 }
1470 }
1471
1472 // Check whether this device has switches.
1473 for (int i = 0; i <= SW_MAX; i++) {
Chris Ye66fbac32020-07-06 20:36:43 -07001474 if (device->swBitmask.test(i)) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001475 device->classes |= InputDeviceClass::SWITCH;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001476 break;
1477 }
1478 }
1479
1480 // Check whether this device supports the vibrator.
Chris Ye66fbac32020-07-06 20:36:43 -07001481 if (device->ffBitmask.test(FF_RUMBLE)) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001482 device->classes |= InputDeviceClass::VIBRATOR;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001483 }
1484
1485 // Configure virtual keys.
Chris Ye1b0c7342020-07-28 21:57:03 -07001486 if ((device->classes.test(InputDeviceClass::TOUCH))) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001487 // Load the virtual keys for the touch screen, if any.
1488 // We do this now so that we can make sure to load the keymap if necessary.
Chris Ye989bb932020-07-04 16:18:59 -07001489 bool success = device->loadVirtualKeyMapLocked();
Siarhei Vishniakou3e78dec2019-02-20 16:21:46 -06001490 if (success) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001491 device->classes |= InputDeviceClass::KEYBOARD;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001492 }
1493 }
1494
1495 // Load the key map.
1496 // We need to do this for joysticks too because the key layout may specify axes.
1497 status_t keyMapStatus = NAME_NOT_FOUND;
Chris Ye1b0c7342020-07-28 21:57:03 -07001498 if (device->classes.any(InputDeviceClass::KEYBOARD | InputDeviceClass::JOYSTICK)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001499 // Load the keymap for the device.
Chris Ye989bb932020-07-04 16:18:59 -07001500 keyMapStatus = device->loadKeyMapLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001501 }
1502
1503 // Configure the keyboard, gamepad or virtual keyboard.
Chris Ye1b0c7342020-07-28 21:57:03 -07001504 if (device->classes.test(InputDeviceClass::KEYBOARD)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001505 // Register the keyboard as a built-in keyboard if it is eligible.
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001506 if (!keyMapStatus && mBuiltInKeyboardId == NO_BUILT_IN_KEYBOARD &&
Siarhei Vishniakoud549b252020-08-11 11:25:26 -05001507 isEligibleBuiltInKeyboard(device->identifier, device->configuration.get(),
1508 &device->keyMap)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001509 mBuiltInKeyboardId = device->id;
1510 }
1511
1512 // 'Q' key support = cheap test of whether this is an alpha-capable kbd
Chris Ye989bb932020-07-04 16:18:59 -07001513 if (device->hasKeycodeLocked(AKEYCODE_Q)) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001514 device->classes |= InputDeviceClass::ALPHAKEY;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001515 }
1516
1517 // See if this device has a DPAD.
Chris Ye989bb932020-07-04 16:18:59 -07001518 if (device->hasKeycodeLocked(AKEYCODE_DPAD_UP) &&
1519 device->hasKeycodeLocked(AKEYCODE_DPAD_DOWN) &&
1520 device->hasKeycodeLocked(AKEYCODE_DPAD_LEFT) &&
1521 device->hasKeycodeLocked(AKEYCODE_DPAD_RIGHT) &&
1522 device->hasKeycodeLocked(AKEYCODE_DPAD_CENTER)) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001523 device->classes |= InputDeviceClass::DPAD;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001524 }
1525
1526 // See if this device has a gamepad.
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001527 for (size_t i = 0; i < sizeof(GAMEPAD_KEYCODES) / sizeof(GAMEPAD_KEYCODES[0]); i++) {
Chris Ye989bb932020-07-04 16:18:59 -07001528 if (device->hasKeycodeLocked(GAMEPAD_KEYCODES[i])) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001529 device->classes |= InputDeviceClass::GAMEPAD;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001530 break;
1531 }
1532 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001533 }
1534
1535 // If the device isn't recognized as something we handle, don't monitor it.
Chris Ye1b0c7342020-07-28 21:57:03 -07001536 if (device->classes == Flags<InputDeviceClass>(0)) {
Chris Ye8594e192020-07-14 10:34:06 -07001537 ALOGV("Dropping device: id=%d, path='%s', name='%s'", deviceId, devicePath.c_str(),
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001538 device->identifier.name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001539 return -1;
1540 }
1541
Tim Kilbourn063ff532015-04-08 10:26:18 -07001542 // Determine whether the device has a mic.
Chris Ye989bb932020-07-04 16:18:59 -07001543 if (device->deviceHasMicLocked()) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001544 device->classes |= InputDeviceClass::MIC;
Tim Kilbourn063ff532015-04-08 10:26:18 -07001545 }
1546
Michael Wrightd02c5b62014-02-10 15:10:22 -08001547 // Determine whether the device is external or internal.
Chris Ye989bb932020-07-04 16:18:59 -07001548 if (device->isExternalDeviceLocked()) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001549 device->classes |= InputDeviceClass::EXTERNAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001550 }
1551
Chris Ye1b0c7342020-07-28 21:57:03 -07001552 if (device->classes.any(InputDeviceClass::JOYSTICK | InputDeviceClass::DPAD) &&
1553 device->classes.test(InputDeviceClass::GAMEPAD)) {
Chris Ye989bb932020-07-04 16:18:59 -07001554 device->controllerNumber = getNextControllerNumberLocked(device->identifier.name);
1555 device->setLedForControllerLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001556 }
1557
Chris Ye989bb932020-07-04 16:18:59 -07001558 if (registerDeviceForEpollLocked(*device) != OK) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001559 return -1;
1560 }
1561
Chris Ye989bb932020-07-04 16:18:59 -07001562 device->configureFd();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001563
Chris Ye1b0c7342020-07-28 21:57:03 -07001564 ALOGI("New device: id=%d, fd=%d, path='%s', name='%s', classes=%s, "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001565 "configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s, ",
Chris Ye1b0c7342020-07-28 21:57:03 -07001566 deviceId, fd, devicePath.c_str(), device->identifier.name.c_str(),
1567 device->classes.string().c_str(), device->configurationFile.c_str(),
1568 device->keyMap.keyLayoutFile.c_str(), device->keyMap.keyCharacterMapFile.c_str(),
1569 toString(mBuiltInKeyboardId == deviceId));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001570
Chris Ye989bb932020-07-04 16:18:59 -07001571 addDeviceLocked(std::move(device));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001572 return OK;
1573}
1574
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001575void EventHub::openVideoDeviceLocked(const std::string& devicePath) {
1576 std::unique_ptr<TouchVideoDevice> videoDevice = TouchVideoDevice::create(devicePath);
1577 if (!videoDevice) {
1578 ALOGE("Could not create touch video device for %s. Ignoring", devicePath.c_str());
1579 return;
1580 }
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08001581 // Transfer ownership of this video device to a matching input device
Chris Ye989bb932020-07-04 16:18:59 -07001582 for (const auto& [id, device] : mDevices) {
Siarhei Vishniakouf49608d2020-08-20 19:18:21 -05001583 if (tryAddVideoDevice(*device, videoDevice)) {
1584 return; // 'device' now owns 'videoDevice'
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08001585 }
1586 }
1587
1588 // Couldn't find a matching input device, so just add it to a temporary holding queue.
1589 // A matching input device may appear later.
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001590 ALOGI("Adding video device %s to list of unattached video devices",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001591 videoDevice->getName().c_str());
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001592 mUnattachedVideoDevices.push_back(std::move(videoDevice));
1593}
1594
Siarhei Vishniakouf49608d2020-08-20 19:18:21 -05001595bool EventHub::tryAddVideoDevice(EventHub::Device& device,
1596 std::unique_ptr<TouchVideoDevice>& videoDevice) {
1597 if (videoDevice->getName() != device.identifier.name) {
1598 return false;
1599 }
1600 device.videoDevice = std::move(videoDevice);
1601 if (device.enabled) {
1602 registerVideoDeviceForEpollLocked(*device.videoDevice);
1603 }
1604 return true;
1605}
1606
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001607bool EventHub::isDeviceEnabled(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +00001608 std::scoped_lock _l(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001609 Device* device = getDeviceLocked(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -07001610 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001611 ALOGE("Invalid device id=%" PRId32 " provided to %s", deviceId, __func__);
1612 return false;
1613 }
1614 return device->enabled;
1615}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001616
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001617status_t EventHub::enableDevice(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +00001618 std::scoped_lock _l(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001619 Device* device = getDeviceLocked(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -07001620 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001621 ALOGE("Invalid device id=%" PRId32 " provided to %s", deviceId, __func__);
1622 return BAD_VALUE;
1623 }
1624 if (device->enabled) {
1625 ALOGW("Duplicate call to %s, input device %" PRId32 " already enabled", __func__, deviceId);
1626 return OK;
1627 }
1628 status_t result = device->enable();
1629 if (result != OK) {
1630 ALOGE("Failed to enable device %" PRId32, deviceId);
1631 return result;
1632 }
1633
Chris Ye989bb932020-07-04 16:18:59 -07001634 device->configureFd();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001635
Chris Ye989bb932020-07-04 16:18:59 -07001636 return registerDeviceForEpollLocked(*device);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001637}
1638
1639status_t EventHub::disableDevice(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +00001640 std::scoped_lock _l(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001641 Device* device = getDeviceLocked(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -07001642 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001643 ALOGE("Invalid device id=%" PRId32 " provided to %s", deviceId, __func__);
1644 return BAD_VALUE;
1645 }
1646 if (!device->enabled) {
1647 ALOGW("Duplicate call to %s, input device already disabled", __func__);
1648 return OK;
1649 }
Chris Ye989bb932020-07-04 16:18:59 -07001650 unregisterDeviceFromEpollLocked(*device);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001651 return device->disable();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001652}
1653
1654void EventHub::createVirtualKeyboardLocked() {
1655 InputDeviceIdentifier identifier;
1656 identifier.name = "Virtual";
1657 identifier.uniqueId = "<virtual>";
1658 assignDescriptorLocked(identifier);
1659
Chris Ye989bb932020-07-04 16:18:59 -07001660 std::unique_ptr<Device> device =
1661 std::make_unique<Device>(-1, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, "<virtual>",
1662 identifier);
Chris Ye1b0c7342020-07-28 21:57:03 -07001663 device->classes = InputDeviceClass::KEYBOARD | InputDeviceClass::ALPHAKEY |
1664 InputDeviceClass::DPAD | InputDeviceClass::VIRTUAL;
Chris Ye989bb932020-07-04 16:18:59 -07001665 device->loadKeyMapLocked();
1666 addDeviceLocked(std::move(device));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001667}
1668
Chris Ye989bb932020-07-04 16:18:59 -07001669void EventHub::addDeviceLocked(std::unique_ptr<Device> device) {
1670 mOpeningDevices.push_back(std::move(device));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001671}
1672
Chris Ye989bb932020-07-04 16:18:59 -07001673int32_t EventHub::getNextControllerNumberLocked(const std::string& name) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001674 if (mControllerNumbers.isFull()) {
1675 ALOGI("Maximum number of controllers reached, assigning controller number 0 to device %s",
Chris Ye989bb932020-07-04 16:18:59 -07001676 name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001677 return 0;
1678 }
1679 // Since the controller number 0 is reserved for non-controllers, translate all numbers up by
1680 // one
1681 return static_cast<int32_t>(mControllerNumbers.markFirstUnmarkedBit() + 1);
1682}
1683
Chris Ye989bb932020-07-04 16:18:59 -07001684void EventHub::releaseControllerNumberLocked(int32_t num) {
1685 if (num > 0) {
1686 mControllerNumbers.clearBit(static_cast<uint32_t>(num - 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001687 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001688}
1689
Chris Ye8594e192020-07-14 10:34:06 -07001690void EventHub::closeDeviceByPathLocked(const std::string& devicePath) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001691 Device* device = getDeviceByPathLocked(devicePath);
Chris Ye989bb932020-07-04 16:18:59 -07001692 if (device != nullptr) {
1693 closeDeviceLocked(*device);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001694 return;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001695 }
Chris Ye8594e192020-07-14 10:34:06 -07001696 ALOGV("Remove device: %s not found, device may already have been removed.", devicePath.c_str());
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001697}
1698
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08001699/**
1700 * Find the video device by filename, and close it.
1701 * The video device is closed by path during an inotify event, where we don't have the
1702 * additional context about the video device fd, or the associated input device.
1703 */
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001704void EventHub::closeVideoDeviceByPathLocked(const std::string& devicePath) {
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08001705 // A video device may be owned by an existing input device, or it may be stored in
1706 // the mUnattachedVideoDevices queue. Check both locations.
Chris Ye989bb932020-07-04 16:18:59 -07001707 for (const auto& [id, device] : mDevices) {
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08001708 if (device->videoDevice && device->videoDevice->getPath() == devicePath) {
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001709 unregisterVideoDeviceFromEpollLocked(*device->videoDevice);
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08001710 device->videoDevice = nullptr;
1711 return;
1712 }
1713 }
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001714 mUnattachedVideoDevices
1715 .erase(std::remove_if(mUnattachedVideoDevices.begin(), mUnattachedVideoDevices.end(),
1716 [&devicePath](
1717 const std::unique_ptr<TouchVideoDevice>& videoDevice) {
1718 return videoDevice->getPath() == devicePath;
1719 }),
1720 mUnattachedVideoDevices.end());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001721}
1722
1723void EventHub::closeAllDevicesLocked() {
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001724 mUnattachedVideoDevices.clear();
Siarhei Vishniakoud549b252020-08-11 11:25:26 -05001725 while (!mDevices.empty()) {
1726 closeDeviceLocked(*(mDevices.begin()->second));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001727 }
1728}
1729
Chris Ye989bb932020-07-04 16:18:59 -07001730void EventHub::closeDeviceLocked(Device& device) {
1731 ALOGI("Removed device: path=%s name=%s id=%d fd=%d classes=%s", device.path.c_str(),
1732 device.identifier.name.c_str(), device.id, device.fd, device.classes.string().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001733
Chris Ye989bb932020-07-04 16:18:59 -07001734 if (device.id == mBuiltInKeyboardId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001735 ALOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this",
Chris Ye989bb932020-07-04 16:18:59 -07001736 device.path.c_str(), mBuiltInKeyboardId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001737 mBuiltInKeyboardId = NO_BUILT_IN_KEYBOARD;
1738 }
1739
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001740 unregisterDeviceFromEpollLocked(device);
Chris Ye989bb932020-07-04 16:18:59 -07001741 if (device.videoDevice) {
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001742 // This must be done after the video device is removed from epoll
Chris Ye989bb932020-07-04 16:18:59 -07001743 mUnattachedVideoDevices.push_back(std::move(device.videoDevice));
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08001744 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001745
Chris Ye989bb932020-07-04 16:18:59 -07001746 releaseControllerNumberLocked(device.controllerNumber);
Siarhei Vishniakoud549b252020-08-11 11:25:26 -05001747 device.controllerNumber = 0;
Chris Ye989bb932020-07-04 16:18:59 -07001748 device.close();
Chris Ye989bb932020-07-04 16:18:59 -07001749 mClosingDevices.push_back(std::move(mDevices[device.id]));
Siarhei Vishniakoud549b252020-08-11 11:25:26 -05001750
Chris Ye989bb932020-07-04 16:18:59 -07001751 mDevices.erase(device.id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001752}
1753
1754status_t EventHub::readNotifyLocked() {
1755 int res;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001756 char event_buf[512];
1757 int event_size;
1758 int event_pos = 0;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001759 struct inotify_event* event;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001760
1761 ALOGV("EventHub::readNotify nfd: %d\n", mINotifyFd);
1762 res = read(mINotifyFd, event_buf, sizeof(event_buf));
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001763 if (res < (int)sizeof(*event)) {
1764 if (errno == EINTR) return 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001765 ALOGW("could not get event, %s\n", strerror(errno));
1766 return -1;
1767 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001768
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001769 while (res >= (int)sizeof(*event)) {
1770 event = (struct inotify_event*)(event_buf + event_pos);
1771 if (event->len) {
Siarhei Vishniakou951f3622018-12-12 19:45:42 -08001772 if (event->wd == mInputWd) {
Chris Ye8594e192020-07-14 10:34:06 -07001773 std::string filename = std::string(DEVICE_PATH) + "/" + event->name;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001774 if (event->mask & IN_CREATE) {
Chris Ye8594e192020-07-14 10:34:06 -07001775 openDeviceLocked(filename);
Siarhei Vishniakou951f3622018-12-12 19:45:42 -08001776 } else {
1777 ALOGI("Removing device '%s' due to inotify event\n", filename.c_str());
Chris Ye8594e192020-07-14 10:34:06 -07001778 closeDeviceByPathLocked(filename);
Siarhei Vishniakou951f3622018-12-12 19:45:42 -08001779 }
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001780 } else if (event->wd == mVideoWd) {
Siarhei Vishniakou951f3622018-12-12 19:45:42 -08001781 if (isV4lTouchNode(event->name)) {
Chris Ye8594e192020-07-14 10:34:06 -07001782 std::string filename = std::string(VIDEO_DEVICE_PATH) + "/" + event->name;
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001783 if (event->mask & IN_CREATE) {
1784 openVideoDeviceLocked(filename);
1785 } else {
1786 ALOGI("Removing video device '%s' due to inotify event", filename.c_str());
1787 closeVideoDeviceByPathLocked(filename);
1788 }
Siarhei Vishniakou951f3622018-12-12 19:45:42 -08001789 }
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001790 } else {
Siarhei Vishniakou951f3622018-12-12 19:45:42 -08001791 LOG_ALWAYS_FATAL("Unexpected inotify event, wd = %i", event->wd);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001792 }
1793 }
1794 event_size = sizeof(*event) + event->len;
1795 res -= event_size;
1796 event_pos += event_size;
1797 }
1798 return 0;
1799}
1800
Chris Ye8594e192020-07-14 10:34:06 -07001801status_t EventHub::scanDirLocked(const std::string& dirname) {
1802 for (const auto& entry : std::filesystem::directory_iterator(dirname)) {
1803 openDeviceLocked(entry.path());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001804 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001805 return 0;
1806}
1807
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001808/**
1809 * Look for all dirname/v4l-touch* devices, and open them.
1810 */
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001811status_t EventHub::scanVideoDirLocked(const std::string& dirname) {
Chris Ye8594e192020-07-14 10:34:06 -07001812 for (const auto& entry : std::filesystem::directory_iterator(dirname)) {
1813 if (isV4lTouchNode(entry.path())) {
1814 ALOGI("Found touch video device %s", entry.path().c_str());
1815 openVideoDeviceLocked(entry.path());
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001816 }
1817 }
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001818 return OK;
1819}
1820
Michael Wrightd02c5b62014-02-10 15:10:22 -08001821void EventHub::requestReopenDevices() {
1822 ALOGV("requestReopenDevices() called");
1823
Chris Ye87143712020-11-10 05:05:58 +00001824 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001825 mNeedToReopenDevices = true;
1826}
1827
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001828void EventHub::dump(std::string& dump) {
1829 dump += "Event Hub State:\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001830
1831 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +00001832 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001833
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001834 dump += StringPrintf(INDENT "BuiltInKeyboardId: %d\n", mBuiltInKeyboardId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001835
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001836 dump += INDENT "Devices:\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001837
Chris Ye989bb932020-07-04 16:18:59 -07001838 for (const auto& [id, device] : mDevices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001839 if (mBuiltInKeyboardId == device->id) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001840 dump += StringPrintf(INDENT2 "%d: %s (aka device 0 - built-in keyboard)\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001841 device->id, device->identifier.name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001842 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001843 dump += StringPrintf(INDENT2 "%d: %s\n", device->id,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001844 device->identifier.name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001845 }
Chris Ye1b0c7342020-07-28 21:57:03 -07001846 dump += StringPrintf(INDENT3 "Classes: %s\n", device->classes.string().c_str());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001847 dump += StringPrintf(INDENT3 "Path: %s\n", device->path.c_str());
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001848 dump += StringPrintf(INDENT3 "Enabled: %s\n", toString(device->enabled));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001849 dump += StringPrintf(INDENT3 "Descriptor: %s\n", device->identifier.descriptor.c_str());
1850 dump += StringPrintf(INDENT3 "Location: %s\n", device->identifier.location.c_str());
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001851 dump += StringPrintf(INDENT3 "ControllerNumber: %d\n", device->controllerNumber);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001852 dump += StringPrintf(INDENT3 "UniqueId: %s\n", device->identifier.uniqueId.c_str());
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001853 dump += StringPrintf(INDENT3 "Identifier: bus=0x%04x, vendor=0x%04x, "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001854 "product=0x%04x, version=0x%04x\n",
1855 device->identifier.bus, device->identifier.vendor,
1856 device->identifier.product, device->identifier.version);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001857 dump += StringPrintf(INDENT3 "KeyLayoutFile: %s\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001858 device->keyMap.keyLayoutFile.c_str());
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001859 dump += StringPrintf(INDENT3 "KeyCharacterMapFile: %s\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001860 device->keyMap.keyCharacterMapFile.c_str());
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001861 dump += StringPrintf(INDENT3 "ConfigurationFile: %s\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001862 device->configurationFile.c_str());
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08001863 dump += INDENT3 "VideoDevice: ";
1864 if (device->videoDevice) {
1865 dump += device->videoDevice->dump() + "\n";
1866 } else {
1867 dump += "<none>\n";
1868 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001869 }
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001870
1871 dump += INDENT "Unattached video devices:\n";
1872 for (const std::unique_ptr<TouchVideoDevice>& videoDevice : mUnattachedVideoDevices) {
1873 dump += INDENT2 + videoDevice->dump() + "\n";
1874 }
1875 if (mUnattachedVideoDevices.empty()) {
1876 dump += INDENT2 "<none>\n";
1877 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001878 } // release lock
1879}
1880
1881void EventHub::monitor() {
1882 // Acquire and release the lock to ensure that the event hub has not deadlocked.
Chris Ye1c2e0892020-11-30 21:41:44 -08001883 std::unique_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001884}
1885
Michael Wrightd02c5b62014-02-10 15:10:22 -08001886}; // namespace android