blob: 07011f5ada671d2dea102fe9bd1cafbd8a396738 [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>
Kim Low03ea0352020-11-06 12:45:07 -080032#include <sys/stat.h>
33#include <sys/sysmacros.h>
Mark Salyzyn5aa26b22014-06-10 13:07:44 -070034#include <unistd.h>
35
Michael Wrightd02c5b62014-02-10 15:10:22 -080036#define LOG_TAG "EventHub"
37
38// #define LOG_NDEBUG 0
Kim Low03ea0352020-11-06 12:45:07 -080039#include <android-base/file.h>
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080040#include <android-base/stringprintf.h>
Chris Yed3fef462021-03-07 17:10:08 -080041#include <android-base/strings.h>
Philip Quinn39b81682019-01-09 22:20:39 -080042#include <cutils/properties.h>
Chris Ye8594e192020-07-14 10:34:06 -070043#include <input/KeyCharacterMap.h>
44#include <input/KeyLayoutMap.h>
45#include <input/VirtualKeyMap.h>
Dan Albert677d87e2014-06-16 17:31:28 -070046#include <openssl/sha.h>
Chris Yed3fef462021-03-07 17:10:08 -080047#include <statslog.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070048#include <utils/Errors.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080049#include <utils/Log.h>
50#include <utils/Timers.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080051
Chris Ye8594e192020-07-14 10:34:06 -070052#include <filesystem>
Chris Ye3fdbfef2021-01-06 18:45:18 -080053#include <regex>
Chris Ye8594e192020-07-14 10:34:06 -070054
55#include "EventHub.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080056
Michael Wrightd02c5b62014-02-10 15:10:22 -080057#define INDENT " "
58#define INDENT2 " "
59#define INDENT3 " "
60
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080061using android::base::StringPrintf;
Chris Ye1b0c7342020-07-28 21:57:03 -070062using namespace android::flag_operators;
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080063
Michael Wrightd02c5b62014-02-10 15:10:22 -080064namespace android {
65
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070066static const char* DEVICE_PATH = "/dev/input";
Siarhei Vishniakou951f3622018-12-12 19:45:42 -080067// v4l2 devices go directly into /dev
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070068static const char* VIDEO_DEVICE_PATH = "/dev";
Michael Wrightd02c5b62014-02-10 15:10:22 -080069
Chris Ye87143712020-11-10 05:05:58 +000070static constexpr int32_t FF_STRONG_MAGNITUDE_CHANNEL_IDX = 0;
71static constexpr int32_t FF_WEAK_MAGNITUDE_CHANNEL_IDX = 1;
Chris Ye6393a262020-08-04 19:41:36 -070072
Kim Low03ea0352020-11-06 12:45:07 -080073// must be kept in sync with definitions in kernel /drivers/power/supply/power_supply_sysfs.c
74static const std::unordered_map<std::string, int32_t> BATTERY_STATUS =
75 {{"Unknown", BATTERY_STATUS_UNKNOWN},
76 {"Charging", BATTERY_STATUS_CHARGING},
77 {"Discharging", BATTERY_STATUS_DISCHARGING},
78 {"Not charging", BATTERY_STATUS_NOT_CHARGING},
79 {"Full", BATTERY_STATUS_FULL}};
80
81// Mapping taken from
82// https://gitlab.freedesktop.org/upower/upower/-/blob/master/src/linux/up-device-supply.c#L484
83static const std::unordered_map<std::string, int32_t> BATTERY_LEVEL = {{"Critical", 5},
84 {"Low", 10},
85 {"Normal", 55},
86 {"High", 70},
87 {"Full", 100},
88 {"Unknown", 50}};
89
Chris Ye3fdbfef2021-01-06 18:45:18 -080090// Mapping for input led class node names lookup.
91// https://www.kernel.org/doc/html/latest/leds/leds-class.html
92static const std::unordered_map<std::string, InputLightClass> LIGHT_CLASSES =
93 {{"red", InputLightClass::RED},
94 {"green", InputLightClass::GREEN},
95 {"blue", InputLightClass::BLUE},
96 {"global", InputLightClass::GLOBAL},
97 {"brightness", InputLightClass::BRIGHTNESS},
98 {"multi_index", InputLightClass::MULTI_INDEX},
99 {"multi_intensity", InputLightClass::MULTI_INTENSITY},
100 {"max_brightness", InputLightClass::MAX_BRIGHTNESS}};
101
102// Mapping for input multicolor led class node names.
103// https://www.kernel.org/doc/html/latest/leds/leds-class-multicolor.html
104static const std::unordered_map<InputLightClass, std::string> LIGHT_NODES =
105 {{InputLightClass::BRIGHTNESS, "brightness"},
106 {InputLightClass::MULTI_INDEX, "multi_index"},
107 {InputLightClass::MULTI_INTENSITY, "multi_intensity"}};
108
109// Mapping for light color name and the light color
110const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
111 {"green", LightColor::GREEN},
112 {"blue", LightColor::BLUE}};
113
Michael Wrightd02c5b62014-02-10 15:10:22 -0800114static inline const char* toString(bool value) {
115 return value ? "true" : "false";
116}
117
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100118static std::string sha1(const std::string& in) {
Dan Albert677d87e2014-06-16 17:31:28 -0700119 SHA_CTX ctx;
120 SHA1_Init(&ctx);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100121 SHA1_Update(&ctx, reinterpret_cast<const u_char*>(in.c_str()), in.size());
Dan Albert677d87e2014-06-16 17:31:28 -0700122 u_char digest[SHA_DIGEST_LENGTH];
123 SHA1_Final(digest, &ctx);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800124
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100125 std::string out;
Dan Albert677d87e2014-06-16 17:31:28 -0700126 for (size_t i = 0; i < SHA_DIGEST_LENGTH; i++) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100127 out += StringPrintf("%02x", digest[i]);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800128 }
129 return out;
130}
131
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800132/**
133 * Return true if name matches "v4l-touch*"
134 */
Chris Ye8594e192020-07-14 10:34:06 -0700135static bool isV4lTouchNode(std::string name) {
136 return name.find("v4l-touch") != std::string::npos;
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800137}
138
Philip Quinn39b81682019-01-09 22:20:39 -0800139/**
140 * Returns true if V4L devices should be scanned.
141 *
142 * The system property ro.input.video_enabled can be used to control whether
143 * EventHub scans and opens V4L devices. As V4L does not support multiple
144 * clients, EventHub effectively blocks access to these devices when it opens
Siarhei Vishniakou29f88492019-04-05 14:11:43 -0700145 * them.
146 *
147 * Setting this to "false" would prevent any video devices from being discovered and
148 * associated with input devices.
149 *
150 * This property can be used as follows:
151 * 1. To turn off features that are dependent on video device presence.
152 * 2. During testing and development, to allow other clients to read video devices
153 * directly from /dev.
Philip Quinn39b81682019-01-09 22:20:39 -0800154 */
155static bool isV4lScanningEnabled() {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700156 return property_get_bool("ro.input.video_enabled", true /* default_value */);
Philip Quinn39b81682019-01-09 22:20:39 -0800157}
158
Siarhei Vishniakou592bac22018-11-08 19:42:38 -0800159static nsecs_t processEventTimestamp(const struct input_event& event) {
160 // Use the time specified in the event instead of the current time
161 // so that downstream code can get more accurate estimates of
162 // event dispatch latency from the time the event is enqueued onto
163 // the evdev client buffer.
164 //
165 // The event's timestamp fortuitously uses the same monotonic clock
166 // time base as the rest of Android. The kernel event device driver
167 // (drivers/input/evdev.c) obtains timestamps using ktime_get_ts().
168 // The systemTime(SYSTEM_TIME_MONOTONIC) function we use everywhere
169 // calls clock_gettime(CLOCK_MONOTONIC) which is implemented as a
170 // system call that also queries ktime_get_ts().
171
172 const nsecs_t inputEventTime = seconds_to_nanoseconds(event.time.tv_sec) +
173 microseconds_to_nanoseconds(event.time.tv_usec);
174 return inputEventTime;
175}
176
Kim Low03ea0352020-11-06 12:45:07 -0800177/**
178 * Returns the sysfs root path of the input device
179 *
180 */
Chris Ye3fdbfef2021-01-06 18:45:18 -0800181static std::optional<std::filesystem::path> getSysfsRootPath(const char* devicePath) {
Kim Low03ea0352020-11-06 12:45:07 -0800182 std::error_code errorCode;
183
184 // Stat the device path to get the major and minor number of the character file
185 struct stat statbuf;
186 if (stat(devicePath, &statbuf) == -1) {
187 ALOGE("Could not stat device %s due to error: %s.", devicePath, std::strerror(errno));
Chris Ye3fdbfef2021-01-06 18:45:18 -0800188 return std::nullopt;
Kim Low03ea0352020-11-06 12:45:07 -0800189 }
190
191 unsigned int major_num = major(statbuf.st_rdev);
192 unsigned int minor_num = minor(statbuf.st_rdev);
193
194 // Realpath "/sys/dev/char/{major}:{minor}" to get the sysfs path to the input event
195 auto sysfsPath = std::filesystem::path("/sys/dev/char/");
196 sysfsPath /= std::to_string(major_num) + ":" + std::to_string(minor_num);
197 sysfsPath = std::filesystem::canonical(sysfsPath, errorCode);
198
199 // Make sure nothing went wrong in call to canonical()
200 if (errorCode) {
201 ALOGW("Could not run filesystem::canonical() due to error %d : %s.", errorCode.value(),
202 errorCode.message().c_str());
Chris Ye3fdbfef2021-01-06 18:45:18 -0800203 return std::nullopt;
Kim Low03ea0352020-11-06 12:45:07 -0800204 }
205
206 // Continue to go up a directory until we reach a directory named "input"
207 while (sysfsPath != "/" && sysfsPath.filename() != "input") {
208 sysfsPath = sysfsPath.parent_path();
209 }
210
211 // Then go up one more and you will be at the sysfs root of the device
212 sysfsPath = sysfsPath.parent_path();
213
214 // Make sure we didn't reach root path and that directory actually exists
215 if (sysfsPath == "/" || !std::filesystem::exists(sysfsPath, errorCode)) {
216 if (errorCode) {
217 ALOGW("Could not run filesystem::exists() due to error %d : %s.", errorCode.value(),
218 errorCode.message().c_str());
219 }
220
221 // Not found
Chris Ye3fdbfef2021-01-06 18:45:18 -0800222 return std::nullopt;
Kim Low03ea0352020-11-06 12:45:07 -0800223 }
224
225 return sysfsPath;
226}
227
228/**
Chris Ye3fdbfef2021-01-06 18:45:18 -0800229 * Returns the list of files under a specified path.
Kim Low03ea0352020-11-06 12:45:07 -0800230 */
Chris Ye3fdbfef2021-01-06 18:45:18 -0800231static std::vector<std::filesystem::path> allFilesInPath(const std::filesystem::path& path) {
232 std::vector<std::filesystem::path> nodes;
233 std::error_code errorCode;
234 auto iter = std::filesystem::directory_iterator(path, errorCode);
235 while (!errorCode && iter != std::filesystem::directory_iterator()) {
236 nodes.push_back(iter->path());
237 iter++;
238 }
239 return nodes;
240}
241
242/**
243 * Returns the list of files under a specified directory in a sysfs path.
244 * Example:
245 * findSysfsNodes(sysfsRootPath, SysfsClass::LEDS) will return all led nodes under "leds" directory
246 * in the sysfs path.
247 */
248static std::vector<std::filesystem::path> findSysfsNodes(const std::filesystem::path& sysfsRoot,
249 SysfsClass clazz) {
250 std::string nodeStr = NamedEnum::string(clazz);
251 std::for_each(nodeStr.begin(), nodeStr.end(),
252 [](char& c) { c = std::tolower(static_cast<unsigned char>(c)); });
253 std::vector<std::filesystem::path> nodes;
254 for (auto path = sysfsRoot; path != "/" && nodes.empty(); path = path.parent_path()) {
255 nodes = allFilesInPath(path / nodeStr);
256 }
257 return nodes;
258}
259
260static std::optional<std::array<LightColor, COLOR_NUM>> getColorIndexArray(
261 std::filesystem::path path) {
262 std::string indexStr;
263 if (!base::ReadFileToString(path, &indexStr)) {
264 return std::nullopt;
265 }
266
267 // Parse the multi color LED index file, refer to kernel docs
268 // leds/leds-class-multicolor.html
269 std::regex indexPattern("(red|green|blue)\\s(red|green|blue)\\s(red|green|blue)[\\n]");
270 std::smatch results;
271 std::array<LightColor, COLOR_NUM> colors;
272 if (!std::regex_match(indexStr, results, indexPattern)) {
273 return std::nullopt;
274 }
275
276 for (size_t i = 1; i < results.size(); i++) {
277 const auto it = LIGHT_COLORS.find(results[i].str());
278 if (it != LIGHT_COLORS.end()) {
279 // intensities.emplace(it->second, 0);
280 colors[i - 1] = it->second;
Kim Low03ea0352020-11-06 12:45:07 -0800281 }
282 }
Chris Ye3fdbfef2021-01-06 18:45:18 -0800283 return colors;
Kim Low03ea0352020-11-06 12:45:07 -0800284}
285
Michael Wrightd02c5b62014-02-10 15:10:22 -0800286// --- Global Functions ---
287
Chris Ye1b0c7342020-07-28 21:57:03 -0700288Flags<InputDeviceClass> getAbsAxisUsage(int32_t axis, Flags<InputDeviceClass> deviceClasses) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800289 // Touch devices get dibs on touch-related axes.
Chris Ye1b0c7342020-07-28 21:57:03 -0700290 if (deviceClasses.test(InputDeviceClass::TOUCH)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800291 switch (axis) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700292 case ABS_X:
293 case ABS_Y:
294 case ABS_PRESSURE:
295 case ABS_TOOL_WIDTH:
296 case ABS_DISTANCE:
297 case ABS_TILT_X:
298 case ABS_TILT_Y:
299 case ABS_MT_SLOT:
300 case ABS_MT_TOUCH_MAJOR:
301 case ABS_MT_TOUCH_MINOR:
302 case ABS_MT_WIDTH_MAJOR:
303 case ABS_MT_WIDTH_MINOR:
304 case ABS_MT_ORIENTATION:
305 case ABS_MT_POSITION_X:
306 case ABS_MT_POSITION_Y:
307 case ABS_MT_TOOL_TYPE:
308 case ABS_MT_BLOB_ID:
309 case ABS_MT_TRACKING_ID:
310 case ABS_MT_PRESSURE:
311 case ABS_MT_DISTANCE:
Chris Ye1b0c7342020-07-28 21:57:03 -0700312 return InputDeviceClass::TOUCH;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800313 }
314 }
315
Chris Yef59a2f42020-10-16 12:55:26 -0700316 if (deviceClasses.test(InputDeviceClass::SENSOR)) {
317 switch (axis) {
318 case ABS_X:
319 case ABS_Y:
320 case ABS_Z:
321 case ABS_RX:
322 case ABS_RY:
323 case ABS_RZ:
324 return InputDeviceClass::SENSOR;
325 }
326 }
327
Michael Wright842500e2015-03-13 17:32:02 -0700328 // External stylus gets the pressure axis
Chris Ye1b0c7342020-07-28 21:57:03 -0700329 if (deviceClasses.test(InputDeviceClass::EXTERNAL_STYLUS)) {
Michael Wright842500e2015-03-13 17:32:02 -0700330 if (axis == ABS_PRESSURE) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700331 return InputDeviceClass::EXTERNAL_STYLUS;
Michael Wright842500e2015-03-13 17:32:02 -0700332 }
333 }
334
Michael Wrightd02c5b62014-02-10 15:10:22 -0800335 // Joystick devices get the rest.
Chris Ye1b0c7342020-07-28 21:57:03 -0700336 return deviceClasses & InputDeviceClass::JOYSTICK;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800337}
338
339// --- EventHub::Device ---
340
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100341EventHub::Device::Device(int fd, int32_t id, const std::string& path,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700342 const InputDeviceIdentifier& identifier)
Chris Ye989bb932020-07-04 16:18:59 -0700343 : fd(fd),
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700344 id(id),
345 path(path),
346 identifier(identifier),
347 classes(0),
348 configuration(nullptr),
349 virtualKeyMap(nullptr),
350 ffEffectPlaying(false),
351 ffEffectId(-1),
Chris Ye3fdbfef2021-01-06 18:45:18 -0800352 nextLightId(0),
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700353 controllerNumber(0),
354 enabled(true),
Chris Ye66fbac32020-07-06 20:36:43 -0700355 isVirtual(fd < 0) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800356
357EventHub::Device::~Device() {
358 close();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800359}
360
361void EventHub::Device::close() {
362 if (fd >= 0) {
363 ::close(fd);
364 fd = -1;
365 }
366}
367
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700368status_t EventHub::Device::enable() {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100369 fd = open(path.c_str(), O_RDWR | O_CLOEXEC | O_NONBLOCK);
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700370 if (fd < 0) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100371 ALOGE("could not open %s, %s\n", path.c_str(), strerror(errno));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700372 return -errno;
373 }
374 enabled = true;
375 return OK;
376}
377
378status_t EventHub::Device::disable() {
379 close();
380 enabled = false;
381 return OK;
382}
383
Chris Ye989bb932020-07-04 16:18:59 -0700384bool EventHub::Device::hasValidFd() const {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700385 return !isVirtual && enabled;
386}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800387
Chris Ye3a1e4462020-08-12 10:13:15 -0700388const std::shared_ptr<KeyCharacterMap> EventHub::Device::getKeyCharacterMap() const {
Chris Ye989bb932020-07-04 16:18:59 -0700389 return keyMap.keyCharacterMap;
390}
391
392template <std::size_t N>
393status_t EventHub::Device::readDeviceBitMask(unsigned long ioctlCode, BitArray<N>& bitArray) {
394 if (!hasValidFd()) {
395 return BAD_VALUE;
396 }
397 if ((_IOC_SIZE(ioctlCode) == 0)) {
398 ioctlCode |= _IOC(0, 0, 0, bitArray.bytes());
399 }
400
401 typename BitArray<N>::Buffer buffer;
402 status_t ret = ioctl(fd, ioctlCode, buffer.data());
403 bitArray.loadFromBuffer(buffer);
404 return ret;
405}
406
407void EventHub::Device::configureFd() {
408 // Set fd parameters with ioctl, such as key repeat, suspend block, and clock type
409 if (classes.test(InputDeviceClass::KEYBOARD)) {
410 // Disable kernel key repeat since we handle it ourselves
411 unsigned int repeatRate[] = {0, 0};
412 if (ioctl(fd, EVIOCSREP, repeatRate)) {
413 ALOGW("Unable to disable kernel key repeat for %s: %s", path.c_str(), strerror(errno));
414 }
415 }
416
417 // Tell the kernel that we want to use the monotonic clock for reporting timestamps
418 // associated with input events. This is important because the input system
419 // uses the timestamps extensively and assumes they were recorded using the monotonic
420 // clock.
421 int clockId = CLOCK_MONOTONIC;
Chris Yef59a2f42020-10-16 12:55:26 -0700422 if (classes.test(InputDeviceClass::SENSOR)) {
423 // Each new sensor event should use the same time base as
424 // SystemClock.elapsedRealtimeNanos().
425 clockId = CLOCK_BOOTTIME;
426 }
Chris Ye989bb932020-07-04 16:18:59 -0700427 bool usingClockIoctl = !ioctl(fd, EVIOCSCLOCKID, &clockId);
428 ALOGI("usingClockIoctl=%s", toString(usingClockIoctl));
429}
430
431bool EventHub::Device::hasKeycodeLocked(int keycode) const {
432 if (!keyMap.haveKeyLayout()) {
433 return false;
434 }
435
436 std::vector<int32_t> scanCodes;
437 keyMap.keyLayoutMap->findScanCodesForKey(keycode, &scanCodes);
438 const size_t N = scanCodes.size();
439 for (size_t i = 0; i < N && i <= KEY_MAX; i++) {
440 int32_t sc = scanCodes[i];
441 if (sc >= 0 && sc <= KEY_MAX && keyBitmask.test(sc)) {
442 return true;
443 }
444 }
445
446 return false;
447}
448
449void EventHub::Device::loadConfigurationLocked() {
450 configurationFile =
451 getInputDeviceConfigurationFilePathByDeviceIdentifier(identifier,
452 InputDeviceConfigurationFileType::
453 CONFIGURATION);
454 if (configurationFile.empty()) {
455 ALOGD("No input device configuration file found for device '%s'.", identifier.name.c_str());
456 } else {
Siarhei Vishniakou4d9f9772020-09-02 22:28:29 -0500457 android::base::Result<std::unique_ptr<PropertyMap>> propertyMap =
458 PropertyMap::load(configurationFile.c_str());
459 if (!propertyMap.ok()) {
Chris Ye989bb932020-07-04 16:18:59 -0700460 ALOGE("Error loading input device configuration file for device '%s'. "
461 "Using default configuration.",
462 identifier.name.c_str());
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500463 } else {
Siarhei Vishniakou4d9f9772020-09-02 22:28:29 -0500464 configuration = std::move(*propertyMap);
Chris Ye989bb932020-07-04 16:18:59 -0700465 }
466 }
467}
468
469bool EventHub::Device::loadVirtualKeyMapLocked() {
470 // The virtual key map is supplied by the kernel as a system board property file.
471 std::string propPath = "/sys/board_properties/virtualkeys.";
472 propPath += identifier.getCanonicalName();
473 if (access(propPath.c_str(), R_OK)) {
474 return false;
475 }
476 virtualKeyMap = VirtualKeyMap::load(propPath);
477 return virtualKeyMap != nullptr;
478}
479
480status_t EventHub::Device::loadKeyMapLocked() {
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500481 return keyMap.load(identifier, configuration.get());
Chris Ye989bb932020-07-04 16:18:59 -0700482}
483
484bool EventHub::Device::isExternalDeviceLocked() {
485 if (configuration) {
486 bool value;
487 if (configuration->tryGetProperty(String8("device.internal"), value)) {
488 return !value;
489 }
490 }
491 return identifier.bus == BUS_USB || identifier.bus == BUS_BLUETOOTH;
492}
493
494bool EventHub::Device::deviceHasMicLocked() {
495 if (configuration) {
496 bool value;
497 if (configuration->tryGetProperty(String8("audio.mic"), value)) {
498 return value;
499 }
500 }
501 return false;
502}
503
504void EventHub::Device::setLedStateLocked(int32_t led, bool on) {
505 int32_t sc;
506 if (hasValidFd() && mapLed(led, &sc) != NAME_NOT_FOUND) {
507 struct input_event ev;
508 ev.time.tv_sec = 0;
509 ev.time.tv_usec = 0;
510 ev.type = EV_LED;
511 ev.code = sc;
512 ev.value = on ? 1 : 0;
513
514 ssize_t nWrite;
515 do {
516 nWrite = write(fd, &ev, sizeof(struct input_event));
517 } while (nWrite == -1 && errno == EINTR);
518 }
519}
520
521void EventHub::Device::setLedForControllerLocked() {
522 for (int i = 0; i < MAX_CONTROLLER_LEDS; i++) {
523 setLedStateLocked(ALED_CONTROLLER_1 + i, controllerNumber == i + 1);
524 }
525}
526
527status_t EventHub::Device::mapLed(int32_t led, int32_t* outScanCode) const {
528 if (!keyMap.haveKeyLayout()) {
529 return NAME_NOT_FOUND;
530 }
531
532 int32_t scanCode;
533 if (keyMap.keyLayoutMap->findScanCodeForLed(led, &scanCode) != NAME_NOT_FOUND) {
534 if (scanCode >= 0 && scanCode <= LED_MAX && ledBitmask.test(scanCode)) {
535 *outScanCode = scanCode;
536 return NO_ERROR;
537 }
538 }
539 return NAME_NOT_FOUND;
540}
541
Chris Ye3fdbfef2021-01-06 18:45:18 -0800542// Check the sysfs path for any input device batteries, returns true if battery found.
543bool EventHub::Device::configureBatteryLocked() {
544 if (!sysfsRootPath.has_value()) {
545 return false;
546 }
547 // Check if device has any batteries.
548 std::vector<std::filesystem::path> batteryPaths =
549 findSysfsNodes(sysfsRootPath.value(), SysfsClass::POWER_SUPPLY);
550 // We only support single battery for an input device, if multiple batteries exist only the
551 // first one is supported.
552 if (batteryPaths.empty()) {
553 // Set path to be empty
554 sysfsBatteryPath = std::nullopt;
555 return false;
556 }
557 // If a battery exists
558 sysfsBatteryPath = batteryPaths[0];
559 return true;
560}
561
562// Check the sysfs path for any input device lights, returns true if lights found.
563bool EventHub::Device::configureLightsLocked() {
564 if (!sysfsRootPath.has_value()) {
565 return false;
566 }
567 // Check if device has any lights.
568 const auto& paths = findSysfsNodes(sysfsRootPath.value(), SysfsClass::LEDS);
569 for (const auto& nodePath : paths) {
570 RawLightInfo info;
571 info.id = ++nextLightId;
572 info.path = nodePath;
573 info.name = nodePath.filename();
574 info.maxBrightness = std::nullopt;
575 size_t nameStart = info.name.rfind(":");
576 if (nameStart != std::string::npos) {
577 // Trim the name to color name
578 info.name = info.name.substr(nameStart + 1);
579 // Set InputLightClass flag for colors
580 const auto it = LIGHT_CLASSES.find(info.name);
581 if (it != LIGHT_CLASSES.end()) {
582 info.flags |= it->second;
583 }
584 }
585 // Scan the path for all the files
586 // Refer to https://www.kernel.org/doc/Documentation/leds/leds-class.txt
587 const auto& files = allFilesInPath(nodePath);
588 for (const auto& file : files) {
589 const auto it = LIGHT_CLASSES.find(file.filename().string());
590 if (it != LIGHT_CLASSES.end()) {
591 info.flags |= it->second;
592 // If the node has maximum brightness, read it
593 if (it->second == InputLightClass::MAX_BRIGHTNESS) {
594 std::string str;
595 if (base::ReadFileToString(file, &str)) {
596 info.maxBrightness = std::stoi(str);
597 }
598 }
599 }
600 }
601 lightInfos.insert_or_assign(info.id, info);
602 }
603 return !lightInfos.empty();
604}
605
Siarhei Vishniakou7a522bf2019-09-24 12:46:29 +0100606/**
607 * Get the capabilities for the current process.
608 * Crashes the system if unable to create / check / destroy the capabilities object.
609 */
610class Capabilities final {
611public:
612 explicit Capabilities() {
613 mCaps = cap_get_proc();
614 LOG_ALWAYS_FATAL_IF(mCaps == nullptr, "Could not get capabilities of the current process");
615 }
616
617 /**
618 * Check whether the current process has a specific capability
619 * in the set of effective capabilities.
620 * Return CAP_SET if the process has the requested capability
621 * Return CAP_CLEAR otherwise.
622 */
623 cap_flag_value_t checkEffectiveCapability(cap_value_t capability) {
624 cap_flag_value_t value;
625 const int result = cap_get_flag(mCaps, capability, CAP_EFFECTIVE, &value);
626 LOG_ALWAYS_FATAL_IF(result == -1, "Could not obtain the requested capability");
627 return value;
628 }
629
630 ~Capabilities() {
631 const int result = cap_free(mCaps);
632 LOG_ALWAYS_FATAL_IF(result == -1, "Could not release the capabilities structure");
633 }
634
635private:
636 cap_t mCaps;
637};
638
639static void ensureProcessCanBlockSuspend() {
640 Capabilities capabilities;
641 const bool canBlockSuspend =
642 capabilities.checkEffectiveCapability(CAP_BLOCK_SUSPEND) == CAP_SET;
643 LOG_ALWAYS_FATAL_IF(!canBlockSuspend,
644 "Input must be able to block suspend to properly process events");
645}
646
Michael Wrightd02c5b62014-02-10 15:10:22 -0800647// --- EventHub ---
648
Michael Wrightd02c5b62014-02-10 15:10:22 -0800649const int EventHub::EPOLL_MAX_EVENTS;
650
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700651EventHub::EventHub(void)
652 : mBuiltInKeyboardId(NO_BUILT_IN_KEYBOARD),
653 mNextDeviceId(1),
654 mControllerNumbers(),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800655 mNeedToSendFinishedDeviceScan(false),
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700656 mNeedToReopenDevices(false),
657 mNeedToScanDevices(true),
658 mPendingEventCount(0),
659 mPendingEventIndex(0),
660 mPendingINotify(false) {
Siarhei Vishniakou7a522bf2019-09-24 12:46:29 +0100661 ensureProcessCanBlockSuspend();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800662
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -0800663 mEpollFd = epoll_create1(EPOLL_CLOEXEC);
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800664 LOG_ALWAYS_FATAL_IF(mEpollFd < 0, "Could not create epoll instance: %s", strerror(errno));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800665
666 mINotifyFd = inotify_init();
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800667 mInputWd = inotify_add_watch(mINotifyFd, DEVICE_PATH, IN_DELETE | IN_CREATE);
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700668 LOG_ALWAYS_FATAL_IF(mInputWd < 0, "Could not register INotify for %s: %s", DEVICE_PATH,
669 strerror(errno));
Philip Quinn39b81682019-01-09 22:20:39 -0800670 if (isV4lScanningEnabled()) {
671 mVideoWd = inotify_add_watch(mINotifyFd, VIDEO_DEVICE_PATH, IN_DELETE | IN_CREATE);
672 LOG_ALWAYS_FATAL_IF(mVideoWd < 0, "Could not register INotify for %s: %s",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700673 VIDEO_DEVICE_PATH, strerror(errno));
Philip Quinn39b81682019-01-09 22:20:39 -0800674 } else {
675 mVideoWd = -1;
676 ALOGI("Video device scanning disabled");
677 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800678
Siarhei Vishniakou2d0e9482019-09-24 12:52:47 +0100679 struct epoll_event eventItem = {};
680 eventItem.events = EPOLLIN | EPOLLWAKEUP;
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -0700681 eventItem.data.fd = mINotifyFd;
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800682 int result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mINotifyFd, &eventItem);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800683 LOG_ALWAYS_FATAL_IF(result != 0, "Could not add INotify to epoll instance. errno=%d", errno);
684
685 int wakeFds[2];
686 result = pipe(wakeFds);
687 LOG_ALWAYS_FATAL_IF(result != 0, "Could not create wake pipe. errno=%d", errno);
688
689 mWakeReadPipeFd = wakeFds[0];
690 mWakeWritePipeFd = wakeFds[1];
691
692 result = fcntl(mWakeReadPipeFd, F_SETFL, O_NONBLOCK);
693 LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake read pipe non-blocking. errno=%d",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700694 errno);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800695
696 result = fcntl(mWakeWritePipeFd, F_SETFL, O_NONBLOCK);
697 LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake write pipe non-blocking. errno=%d",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700698 errno);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800699
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -0700700 eventItem.data.fd = mWakeReadPipeFd;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800701 result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeReadPipeFd, &eventItem);
702 LOG_ALWAYS_FATAL_IF(result != 0, "Could not add wake read pipe to epoll instance. errno=%d",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700703 errno);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800704}
705
706EventHub::~EventHub(void) {
707 closeAllDevicesLocked();
708
Michael Wrightd02c5b62014-02-10 15:10:22 -0800709 ::close(mEpollFd);
710 ::close(mINotifyFd);
711 ::close(mWakeReadPipeFd);
712 ::close(mWakeWritePipeFd);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800713}
714
715InputDeviceIdentifier EventHub::getDeviceIdentifier(int32_t deviceId) const {
Chris Ye87143712020-11-10 05:05:58 +0000716 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800717 Device* device = getDeviceLocked(deviceId);
Chris Ye989bb932020-07-04 16:18:59 -0700718 return device != nullptr ? device->identifier : InputDeviceIdentifier();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800719}
720
Chris Ye1b0c7342020-07-28 21:57:03 -0700721Flags<InputDeviceClass> EventHub::getDeviceClasses(int32_t deviceId) const {
Chris Ye87143712020-11-10 05:05:58 +0000722 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800723 Device* device = getDeviceLocked(deviceId);
Chris Ye989bb932020-07-04 16:18:59 -0700724 return device != nullptr ? device->classes : Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800725}
726
727int32_t EventHub::getDeviceControllerNumber(int32_t deviceId) const {
Chris Ye87143712020-11-10 05:05:58 +0000728 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800729 Device* device = getDeviceLocked(deviceId);
Chris Ye989bb932020-07-04 16:18:59 -0700730 return device != nullptr ? device->controllerNumber : 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800731}
732
733void EventHub::getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
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->configuration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800737 *outConfiguration = *device->configuration;
738 } else {
739 outConfiguration->clear();
740 }
741}
742
743status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700744 RawAbsoluteAxisInfo* outAxisInfo) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800745 outAxisInfo->clear();
746
747 if (axis >= 0 && axis <= ABS_MAX) {
Chris Ye87143712020-11-10 05:05:58 +0000748 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800749
750 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -0700751 if (device != nullptr && device->hasValidFd() && device->absBitmask.test(axis)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800752 struct input_absinfo info;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700753 if (ioctl(device->fd, EVIOCGABS(axis), &info)) {
754 ALOGW("Error reading absolute controller %d for device %s fd %d, errno=%d", axis,
755 device->identifier.name.c_str(), device->fd, errno);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800756 return -errno;
757 }
758
759 if (info.minimum != info.maximum) {
760 outAxisInfo->valid = true;
761 outAxisInfo->minValue = info.minimum;
762 outAxisInfo->maxValue = info.maximum;
763 outAxisInfo->flat = info.flat;
764 outAxisInfo->fuzz = info.fuzz;
765 outAxisInfo->resolution = info.resolution;
766 }
767 return OK;
768 }
769 }
770 return -1;
771}
772
773bool EventHub::hasRelativeAxis(int32_t deviceId, int axis) const {
774 if (axis >= 0 && axis <= REL_MAX) {
Chris Ye87143712020-11-10 05:05:58 +0000775 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800776 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -0700777 return device != nullptr ? device->relBitmask.test(axis) : false;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800778 }
779 return false;
780}
781
782bool EventHub::hasInputProperty(int32_t deviceId, int property) const {
Chris Ye87143712020-11-10 05:05:58 +0000783 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800784
Chris Ye989bb932020-07-04 16:18:59 -0700785 Device* device = getDeviceLocked(deviceId);
786 return property >= 0 && property <= INPUT_PROP_MAX && device != nullptr
787 ? device->propBitmask.test(property)
788 : false;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800789}
790
Chris Yef59a2f42020-10-16 12:55:26 -0700791bool EventHub::hasMscEvent(int32_t deviceId, int mscEvent) const {
792 std::scoped_lock _l(mLock);
793
794 Device* device = getDeviceLocked(deviceId);
795 return mscEvent >= 0 && mscEvent <= MSC_MAX && device != nullptr
796 ? device->mscBitmask.test(mscEvent)
797 : false;
798}
799
Michael Wrightd02c5b62014-02-10 15:10:22 -0800800int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const {
801 if (scanCode >= 0 && scanCode <= KEY_MAX) {
Chris Ye87143712020-11-10 05:05:58 +0000802 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800803
804 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -0700805 if (device != nullptr && device->hasValidFd() && device->keyBitmask.test(scanCode)) {
806 if (device->readDeviceBitMask(EVIOCGKEY(0), device->keyState) >= 0) {
807 return device->keyState.test(scanCode) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800808 }
809 }
810 }
811 return AKEY_STATE_UNKNOWN;
812}
813
814int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
Chris Ye87143712020-11-10 05:05:58 +0000815 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800816
817 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -0700818 if (device != nullptr && device->hasValidFd() && device->keyMap.haveKeyLayout()) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800819 std::vector<int32_t> scanCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800820 device->keyMap.keyLayoutMap->findScanCodesForKey(keyCode, &scanCodes);
821 if (scanCodes.size() != 0) {
Chris Ye66fbac32020-07-06 20:36:43 -0700822 if (device->readDeviceBitMask(EVIOCGKEY(0), device->keyState) >= 0) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800823 for (size_t i = 0; i < scanCodes.size(); i++) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800824 int32_t sc = scanCodes[i];
Chris Ye66fbac32020-07-06 20:36:43 -0700825 if (sc >= 0 && sc <= KEY_MAX && device->keyState.test(sc)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800826 return AKEY_STATE_DOWN;
827 }
828 }
829 return AKEY_STATE_UP;
830 }
831 }
832 }
833 return AKEY_STATE_UNKNOWN;
834}
835
836int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const {
837 if (sw >= 0 && sw <= SW_MAX) {
Chris Ye87143712020-11-10 05:05:58 +0000838 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800839
840 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -0700841 if (device != nullptr && device->hasValidFd() && device->swBitmask.test(sw)) {
842 if (device->readDeviceBitMask(EVIOCGSW(0), device->swState) >= 0) {
843 return device->swState.test(sw) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800844 }
845 }
846 }
847 return AKEY_STATE_UNKNOWN;
848}
849
850status_t EventHub::getAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t* outValue) const {
851 *outValue = 0;
852
853 if (axis >= 0 && axis <= ABS_MAX) {
Chris Ye87143712020-11-10 05:05:58 +0000854 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800855
856 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -0700857 if (device != nullptr && device->hasValidFd() && device->absBitmask.test(axis)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800858 struct input_absinfo info;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700859 if (ioctl(device->fd, EVIOCGABS(axis), &info)) {
860 ALOGW("Error reading absolute controller %d for device %s fd %d, errno=%d", axis,
861 device->identifier.name.c_str(), device->fd, errno);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800862 return -errno;
863 }
864
865 *outValue = info.value;
866 return OK;
867 }
868 }
869 return -1;
870}
871
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700872bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
873 uint8_t* outFlags) const {
Chris Ye87143712020-11-10 05:05:58 +0000874 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800875
876 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -0700877 if (device != nullptr && device->keyMap.haveKeyLayout()) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800878 std::vector<int32_t> scanCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800879 for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) {
880 scanCodes.clear();
881
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700882 status_t err = device->keyMap.keyLayoutMap->findScanCodesForKey(keyCodes[codeIndex],
883 &scanCodes);
884 if (!err) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800885 // check the possible scan codes identified by the layout map against the
886 // map of codes actually emitted by the driver
887 for (size_t sc = 0; sc < scanCodes.size(); sc++) {
Chris Ye66fbac32020-07-06 20:36:43 -0700888 if (device->keyBitmask.test(scanCodes[sc])) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800889 outFlags[codeIndex] = 1;
890 break;
891 }
892 }
893 }
894 }
895 return true;
896 }
897 return false;
898}
899
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700900status_t EventHub::mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
901 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const {
Chris Ye87143712020-11-10 05:05:58 +0000902 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800903 Device* device = getDeviceLocked(deviceId);
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700904 status_t status = NAME_NOT_FOUND;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800905
Chris Ye66fbac32020-07-06 20:36:43 -0700906 if (device != nullptr) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800907 // Check the key character map first.
Chris Ye3a1e4462020-08-12 10:13:15 -0700908 const std::shared_ptr<KeyCharacterMap> kcm = device->getKeyCharacterMap();
909 if (kcm) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800910 if (!kcm->mapKey(scanCode, usageCode, outKeycode)) {
911 *outFlags = 0;
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700912 status = NO_ERROR;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800913 }
914 }
915
916 // Check the key layout next.
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700917 if (status != NO_ERROR && device->keyMap.haveKeyLayout()) {
Siarhei Vishniakou592bac22018-11-08 19:42:38 -0800918 if (!device->keyMap.keyLayoutMap->mapKey(scanCode, usageCode, outKeycode, outFlags)) {
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700919 status = NO_ERROR;
920 }
921 }
922
923 if (status == NO_ERROR) {
Chris Ye3a1e4462020-08-12 10:13:15 -0700924 if (kcm) {
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700925 kcm->tryRemapKey(*outKeycode, metaState, outKeycode, outMetaState);
926 } else {
927 *outMetaState = metaState;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800928 }
929 }
930 }
931
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700932 if (status != NO_ERROR) {
933 *outKeycode = 0;
934 *outFlags = 0;
935 *outMetaState = metaState;
936 }
937
938 return status;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800939}
940
941status_t EventHub::mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const {
Chris Ye87143712020-11-10 05:05:58 +0000942 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800943 Device* device = getDeviceLocked(deviceId);
944
Chris Ye66fbac32020-07-06 20:36:43 -0700945 if (device != nullptr && device->keyMap.haveKeyLayout()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800946 status_t err = device->keyMap.keyLayoutMap->mapAxis(scanCode, outAxisInfo);
947 if (err == NO_ERROR) {
948 return NO_ERROR;
949 }
950 }
951
952 return NAME_NOT_FOUND;
953}
954
Chris Yef59a2f42020-10-16 12:55:26 -0700955base::Result<std::pair<InputDeviceSensorType, int32_t>> EventHub::mapSensor(int32_t deviceId,
956 int32_t absCode) {
957 std::scoped_lock _l(mLock);
958 Device* device = getDeviceLocked(deviceId);
959
960 if (device != nullptr && device->keyMap.haveKeyLayout()) {
961 return device->keyMap.keyLayoutMap->mapSensor(absCode);
962 }
963 return Errorf("Device not found or device has no key layout.");
964}
965
Chris Ye3fdbfef2021-01-06 18:45:18 -0800966const std::vector<int32_t> EventHub::getRawLightIds(int32_t deviceId) {
967 std::scoped_lock _l(mLock);
968 Device* device = getDeviceLocked(deviceId);
969 std::vector<int32_t> lightIds;
970
971 if (device != nullptr) {
972 for (const auto [id, info] : device->lightInfos) {
973 lightIds.push_back(id);
974 }
975 }
976 return lightIds;
977}
978
979std::optional<RawLightInfo> EventHub::getRawLightInfo(int32_t deviceId, int32_t lightId) {
980 std::scoped_lock _l(mLock);
981 Device* device = getDeviceLocked(deviceId);
982
983 if (device != nullptr) {
984 auto it = device->lightInfos.find(lightId);
985 if (it != device->lightInfos.end()) {
986 return it->second;
987 }
988 }
989 return std::nullopt;
990}
991
992std::optional<int32_t> EventHub::getLightBrightness(int32_t deviceId, int32_t lightId) {
993 std::scoped_lock _l(mLock);
994
995 Device* device = getDeviceLocked(deviceId);
996 if (device == nullptr) {
997 return std::nullopt;
998 }
999
1000 auto it = device->lightInfos.find(lightId);
1001 if (it == device->lightInfos.end()) {
1002 return std::nullopt;
1003 }
1004 std::string buffer;
1005 if (!base::ReadFileToString(it->second.path / LIGHT_NODES.at(InputLightClass::BRIGHTNESS),
1006 &buffer)) {
1007 return std::nullopt;
1008 }
1009 return std::stoi(buffer);
1010}
1011
1012std::optional<std::unordered_map<LightColor, int32_t>> EventHub::getLightIntensities(
1013 int32_t deviceId, int32_t lightId) {
1014 std::scoped_lock _l(mLock);
1015
1016 Device* device = getDeviceLocked(deviceId);
1017 if (device == nullptr) {
1018 return std::nullopt;
1019 }
1020
1021 auto lightIt = device->lightInfos.find(lightId);
1022 if (lightIt == device->lightInfos.end()) {
1023 return std::nullopt;
1024 }
1025
1026 auto ret =
1027 getColorIndexArray(lightIt->second.path / LIGHT_NODES.at(InputLightClass::MULTI_INDEX));
1028
1029 if (!ret.has_value()) {
1030 return std::nullopt;
1031 }
1032 std::array<LightColor, COLOR_NUM> colors = ret.value();
1033
1034 std::string intensityStr;
1035 if (!base::ReadFileToString(lightIt->second.path /
1036 LIGHT_NODES.at(InputLightClass::MULTI_INTENSITY),
1037 &intensityStr)) {
1038 return std::nullopt;
1039 }
1040
1041 // Intensity node outputs 3 color values
1042 std::regex intensityPattern("([0-9]+)\\s([0-9]+)\\s([0-9]+)[\\n]");
1043 std::smatch results;
1044
1045 if (!std::regex_match(intensityStr, results, intensityPattern)) {
1046 return std::nullopt;
1047 }
1048 std::unordered_map<LightColor, int32_t> intensities;
1049 for (size_t i = 1; i < results.size(); i++) {
1050 int value = std::stoi(results[i].str());
1051 intensities.emplace(colors[i - 1], value);
1052 }
1053 return intensities;
1054}
1055
1056void EventHub::setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) {
1057 std::scoped_lock _l(mLock);
1058
1059 Device* device = getDeviceLocked(deviceId);
1060 if (device == nullptr) {
1061 ALOGE("Device Id %d does not exist", deviceId);
1062 return;
1063 }
1064 auto lightIt = device->lightInfos.find(lightId);
1065 if (lightIt == device->lightInfos.end()) {
1066 ALOGE("Light Id %d does not exist.", lightId);
1067 return;
1068 }
1069
1070 if (!base::WriteStringToFile(std::to_string(brightness),
1071 lightIt->second.path /
1072 LIGHT_NODES.at(InputLightClass::BRIGHTNESS))) {
1073 ALOGE("Can not write to file, error: %s", strerror(errno));
1074 }
1075}
1076
1077void EventHub::setLightIntensities(int32_t deviceId, int32_t lightId,
1078 std::unordered_map<LightColor, int32_t> intensities) {
1079 std::scoped_lock _l(mLock);
1080
1081 Device* device = getDeviceLocked(deviceId);
1082 if (device == nullptr) {
1083 ALOGE("Device Id %d does not exist", deviceId);
1084 return;
1085 }
1086 auto lightIt = device->lightInfos.find(lightId);
1087 if (lightIt == device->lightInfos.end()) {
1088 ALOGE("Light Id %d does not exist.", lightId);
1089 return;
1090 }
1091
1092 auto ret =
1093 getColorIndexArray(lightIt->second.path / LIGHT_NODES.at(InputLightClass::MULTI_INDEX));
1094
1095 if (!ret.has_value()) {
1096 return;
1097 }
1098 std::array<LightColor, COLOR_NUM> colors = ret.value();
1099
1100 std::string rgbStr;
1101 for (size_t i = 0; i < COLOR_NUM; i++) {
1102 auto it = intensities.find(colors[i]);
1103 if (it != intensities.end()) {
1104 rgbStr += std::to_string(it->second);
1105 // Insert space between colors
1106 if (i < COLOR_NUM - 1) {
1107 rgbStr += " ";
1108 }
1109 }
1110 }
1111 // Append new line
1112 rgbStr += "\n";
1113
1114 if (!base::WriteStringToFile(rgbStr,
1115 lightIt->second.path /
1116 LIGHT_NODES.at(InputLightClass::MULTI_INTENSITY))) {
1117 ALOGE("Can not write to file, error: %s", strerror(errno));
1118 }
1119}
1120
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001121void EventHub::setExcludedDevices(const std::vector<std::string>& devices) {
Chris Ye87143712020-11-10 05:05:58 +00001122 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001123
1124 mExcludedDevices = devices;
1125}
1126
1127bool EventHub::hasScanCode(int32_t deviceId, int32_t scanCode) const {
Chris Ye87143712020-11-10 05:05:58 +00001128 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001129 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -07001130 if (device != nullptr && scanCode >= 0 && scanCode <= KEY_MAX) {
1131 return device->keyBitmask.test(scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001132 }
1133 return false;
1134}
1135
1136bool EventHub::hasLed(int32_t deviceId, int32_t led) const {
Chris Ye87143712020-11-10 05:05:58 +00001137 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001138 Device* device = getDeviceLocked(deviceId);
1139 int32_t sc;
Chris Ye989bb932020-07-04 16:18:59 -07001140 if (device != nullptr && device->mapLed(led, &sc) == NO_ERROR) {
Chris Ye66fbac32020-07-06 20:36:43 -07001141 return device->ledBitmask.test(sc);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001142 }
1143 return false;
1144}
1145
1146void EventHub::setLedState(int32_t deviceId, int32_t led, bool on) {
Chris Ye87143712020-11-10 05:05:58 +00001147 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001148 Device* device = getDeviceLocked(deviceId);
Chris Ye989bb932020-07-04 16:18:59 -07001149 if (device != nullptr && device->hasValidFd()) {
1150 device->setLedStateLocked(led, on);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001151 }
1152}
1153
1154void EventHub::getVirtualKeyDefinitions(int32_t deviceId,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001155 std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001156 outVirtualKeys.clear();
1157
Chris Ye87143712020-11-10 05:05:58 +00001158 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001159 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -07001160 if (device != nullptr && device->virtualKeyMap) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001161 const std::vector<VirtualKeyDefinition> virtualKeys =
1162 device->virtualKeyMap->getVirtualKeys();
1163 outVirtualKeys.insert(outVirtualKeys.end(), virtualKeys.begin(), virtualKeys.end());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001164 }
1165}
1166
Chris Ye3a1e4462020-08-12 10:13:15 -07001167const std::shared_ptr<KeyCharacterMap> EventHub::getKeyCharacterMap(int32_t deviceId) const {
Chris Ye87143712020-11-10 05:05:58 +00001168 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001169 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -07001170 if (device != nullptr) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001171 return device->getKeyCharacterMap();
1172 }
Yi Kong9b14ac62018-07-17 13:48:38 -07001173 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001174}
1175
Chris Ye3a1e4462020-08-12 10:13:15 -07001176bool EventHub::setKeyboardLayoutOverlay(int32_t deviceId, std::shared_ptr<KeyCharacterMap> map) {
Chris Ye87143712020-11-10 05:05:58 +00001177 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001178 Device* device = getDeviceLocked(deviceId);
Chris Ye3a1e4462020-08-12 10:13:15 -07001179 if (device != nullptr && map != nullptr && device->keyMap.keyCharacterMap != nullptr) {
1180 device->keyMap.keyCharacterMap->combine(*map);
1181 device->keyMap.keyCharacterMapFile = device->keyMap.keyCharacterMap->getLoadFileName();
1182 return true;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001183 }
1184 return false;
1185}
1186
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001187static std::string generateDescriptor(InputDeviceIdentifier& identifier) {
1188 std::string rawDescriptor;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001189 rawDescriptor += StringPrintf(":%04x:%04x:", identifier.vendor, identifier.product);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001190 // TODO add handling for USB devices to not uniqueify kbs that show up twice
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001191 if (!identifier.uniqueId.empty()) {
1192 rawDescriptor += "uniqueId:";
1193 rawDescriptor += identifier.uniqueId;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001194 } else if (identifier.nonce != 0) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001195 rawDescriptor += StringPrintf("nonce:%04x", identifier.nonce);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001196 }
1197
1198 if (identifier.vendor == 0 && identifier.product == 0) {
1199 // If we don't know the vendor and product id, then the device is probably
1200 // built-in so we need to rely on other information to uniquely identify
1201 // the input device. Usually we try to avoid relying on the device name or
1202 // location but for built-in input device, they are unlikely to ever change.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001203 if (!identifier.name.empty()) {
1204 rawDescriptor += "name:";
1205 rawDescriptor += identifier.name;
1206 } else if (!identifier.location.empty()) {
1207 rawDescriptor += "location:";
1208 rawDescriptor += identifier.location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001209 }
1210 }
1211 identifier.descriptor = sha1(rawDescriptor);
1212 return rawDescriptor;
1213}
1214
1215void EventHub::assignDescriptorLocked(InputDeviceIdentifier& identifier) {
1216 // Compute a device descriptor that uniquely identifies the device.
1217 // The descriptor is assumed to be a stable identifier. Its value should not
1218 // change between reboots, reconnections, firmware updates or new releases
1219 // of Android. In practice we sometimes get devices that cannot be uniquely
1220 // identified. In this case we enforce uniqueness between connected devices.
1221 // Ideally, we also want the descriptor to be short and relatively opaque.
1222
1223 identifier.nonce = 0;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001224 std::string rawDescriptor = generateDescriptor(identifier);
1225 if (identifier.uniqueId.empty()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001226 // If it didn't have a unique id check for conflicts and enforce
1227 // uniqueness if necessary.
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001228 while (getDeviceByDescriptorLocked(identifier.descriptor) != nullptr) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001229 identifier.nonce++;
1230 rawDescriptor = generateDescriptor(identifier);
1231 }
1232 }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001233 ALOGV("Created descriptor: raw=%s, cooked=%s", rawDescriptor.c_str(),
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001234 identifier.descriptor.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001235}
1236
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +00001237void EventHub::vibrate(int32_t deviceId, const VibrationElement& element) {
Chris Ye87143712020-11-10 05:05:58 +00001238 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001239 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -07001240 if (device != nullptr && device->hasValidFd()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001241 ff_effect effect;
1242 memset(&effect, 0, sizeof(effect));
1243 effect.type = FF_RUMBLE;
1244 effect.id = device->ffEffectId;
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +00001245 // evdev FF_RUMBLE effect only supports two channels of vibration.
Chris Ye6393a262020-08-04 19:41:36 -07001246 effect.u.rumble.strong_magnitude = element.getMagnitude(FF_STRONG_MAGNITUDE_CHANNEL_IDX);
1247 effect.u.rumble.weak_magnitude = element.getMagnitude(FF_WEAK_MAGNITUDE_CHANNEL_IDX);
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +00001248 effect.replay.length = element.duration.count();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001249 effect.replay.delay = 0;
1250 if (ioctl(device->fd, EVIOCSFF, &effect)) {
1251 ALOGW("Could not upload force feedback effect to device %s due to error %d.",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001252 device->identifier.name.c_str(), errno);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001253 return;
1254 }
1255 device->ffEffectId = effect.id;
1256
1257 struct input_event ev;
1258 ev.time.tv_sec = 0;
1259 ev.time.tv_usec = 0;
1260 ev.type = EV_FF;
1261 ev.code = device->ffEffectId;
1262 ev.value = 1;
1263 if (write(device->fd, &ev, sizeof(ev)) != sizeof(ev)) {
1264 ALOGW("Could not start force feedback effect on device %s due to error %d.",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001265 device->identifier.name.c_str(), errno);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001266 return;
1267 }
1268 device->ffEffectPlaying = true;
1269 }
1270}
1271
1272void EventHub::cancelVibrate(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +00001273 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001274 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -07001275 if (device != nullptr && device->hasValidFd()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001276 if (device->ffEffectPlaying) {
1277 device->ffEffectPlaying = false;
1278
1279 struct input_event ev;
1280 ev.time.tv_sec = 0;
1281 ev.time.tv_usec = 0;
1282 ev.type = EV_FF;
1283 ev.code = device->ffEffectId;
1284 ev.value = 0;
1285 if (write(device->fd, &ev, sizeof(ev)) != sizeof(ev)) {
1286 ALOGW("Could not stop force feedback effect on device %s due to error %d.",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001287 device->identifier.name.c_str(), errno);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001288 return;
1289 }
1290 }
1291 }
1292}
1293
Chris Ye87143712020-11-10 05:05:58 +00001294std::vector<int32_t> EventHub::getVibratorIds(int32_t deviceId) {
1295 std::scoped_lock _l(mLock);
1296 std::vector<int32_t> vibrators;
1297 Device* device = getDeviceLocked(deviceId);
1298 if (device != nullptr && device->hasValidFd() &&
1299 device->classes.test(InputDeviceClass::VIBRATOR)) {
1300 vibrators.push_back(FF_STRONG_MAGNITUDE_CHANNEL_IDX);
1301 vibrators.push_back(FF_WEAK_MAGNITUDE_CHANNEL_IDX);
1302 }
1303 return vibrators;
1304}
1305
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001306EventHub::Device* EventHub::getDeviceByDescriptorLocked(const std::string& descriptor) const {
Chris Ye989bb932020-07-04 16:18:59 -07001307 for (const auto& [id, device] : mDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001308 if (descriptor == device->identifier.descriptor) {
Chris Ye989bb932020-07-04 16:18:59 -07001309 return device.get();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001310 }
1311 }
Yi Kong9b14ac62018-07-17 13:48:38 -07001312 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001313}
1314
1315EventHub::Device* EventHub::getDeviceLocked(int32_t deviceId) const {
Prabir Pradhancae4b3a2019-02-05 18:51:32 -08001316 if (deviceId == ReservedInputDeviceId::BUILT_IN_KEYBOARD_ID) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001317 deviceId = mBuiltInKeyboardId;
1318 }
Chris Ye989bb932020-07-04 16:18:59 -07001319 const auto& it = mDevices.find(deviceId);
1320 return it != mDevices.end() ? it->second.get() : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001321}
1322
Chris Ye8594e192020-07-14 10:34:06 -07001323EventHub::Device* EventHub::getDeviceByPathLocked(const std::string& devicePath) const {
Chris Ye989bb932020-07-04 16:18:59 -07001324 for (const auto& [id, device] : mDevices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001325 if (device->path == devicePath) {
Chris Ye989bb932020-07-04 16:18:59 -07001326 return device.get();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001327 }
1328 }
Yi Kong9b14ac62018-07-17 13:48:38 -07001329 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001330}
1331
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001332/**
1333 * The file descriptor could be either input device, or a video device (associated with a
1334 * specific input device). Check both cases here, and return the device that this event
1335 * belongs to. Caller can compare the fd's once more to determine event type.
1336 * Looks through all input devices, and only attached video devices. Unattached video
1337 * devices are ignored.
1338 */
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -07001339EventHub::Device* EventHub::getDeviceByFdLocked(int fd) const {
Chris Ye989bb932020-07-04 16:18:59 -07001340 for (const auto& [id, device] : mDevices) {
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -07001341 if (device->fd == fd) {
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001342 // This is an input device event
Chris Ye989bb932020-07-04 16:18:59 -07001343 return device.get();
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001344 }
1345 if (device->videoDevice && device->videoDevice->getFd() == fd) {
1346 // This is a video device event
Chris Ye989bb932020-07-04 16:18:59 -07001347 return device.get();
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -07001348 }
1349 }
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001350 // We do not check mUnattachedVideoDevices here because they should not participate in epoll,
1351 // and therefore should never be looked up by fd.
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -07001352 return nullptr;
1353}
1354
Kim Low03ea0352020-11-06 12:45:07 -08001355std::optional<int32_t> EventHub::getBatteryCapacity(int32_t deviceId) const {
1356 std::scoped_lock _l(mLock);
1357 Device* device = getDeviceLocked(deviceId);
1358 std::string buffer;
1359
Chris Ye3fdbfef2021-01-06 18:45:18 -08001360 if (device == nullptr || !device->sysfsBatteryPath.has_value()) {
Kim Low03ea0352020-11-06 12:45:07 -08001361 return std::nullopt;
1362 }
1363
1364 // Some devices report battery capacity as an integer through the "capacity" file
Chris Ye3fdbfef2021-01-06 18:45:18 -08001365 if (base::ReadFileToString(device->sysfsBatteryPath.value() / "capacity", &buffer)) {
Chris Yed1936772021-02-22 10:30:40 -08001366 return std::stoi(base::Trim(buffer));
Kim Low03ea0352020-11-06 12:45:07 -08001367 }
1368
1369 // Other devices report capacity as an enum value POWER_SUPPLY_CAPACITY_LEVEL_XXX
1370 // These values are taken from kernel source code include/linux/power_supply.h
Chris Ye3fdbfef2021-01-06 18:45:18 -08001371 if (base::ReadFileToString(device->sysfsBatteryPath.value() / "capacity_level", &buffer)) {
Chris Yed1936772021-02-22 10:30:40 -08001372 // Remove any white space such as trailing new line
1373 const auto it = BATTERY_LEVEL.find(base::Trim(buffer));
Kim Low03ea0352020-11-06 12:45:07 -08001374 if (it != BATTERY_LEVEL.end()) {
1375 return it->second;
1376 }
1377 }
1378 return std::nullopt;
1379}
1380
1381std::optional<int32_t> EventHub::getBatteryStatus(int32_t deviceId) const {
1382 std::scoped_lock _l(mLock);
1383 Device* device = getDeviceLocked(deviceId);
1384 std::string buffer;
1385
Chris Ye3fdbfef2021-01-06 18:45:18 -08001386 if (device == nullptr || !device->sysfsBatteryPath.has_value()) {
Kim Low03ea0352020-11-06 12:45:07 -08001387 return std::nullopt;
1388 }
1389
Chris Ye3fdbfef2021-01-06 18:45:18 -08001390 if (!base::ReadFileToString(device->sysfsBatteryPath.value() / "status", &buffer)) {
Kim Low03ea0352020-11-06 12:45:07 -08001391 ALOGE("Failed to read sysfs battery info: %s", strerror(errno));
1392 return std::nullopt;
1393 }
1394
Chris Yed1936772021-02-22 10:30:40 -08001395 // Remove white space like trailing new line
1396 const auto it = BATTERY_STATUS.find(base::Trim(buffer));
Kim Low03ea0352020-11-06 12:45:07 -08001397
1398 if (it != BATTERY_STATUS.end()) {
1399 return it->second;
1400 }
1401
1402 return std::nullopt;
1403}
1404
Michael Wrightd02c5b62014-02-10 15:10:22 -08001405size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) {
1406 ALOG_ASSERT(bufferSize >= 1);
1407
Chris Ye87143712020-11-10 05:05:58 +00001408 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001409
1410 struct input_event readBuffer[bufferSize];
1411
1412 RawEvent* event = buffer;
1413 size_t capacity = bufferSize;
1414 bool awoken = false;
1415 for (;;) {
1416 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
1417
1418 // Reopen input devices if needed.
1419 if (mNeedToReopenDevices) {
1420 mNeedToReopenDevices = false;
1421
1422 ALOGI("Reopening all input devices due to a configuration change.");
1423
1424 closeAllDevicesLocked();
1425 mNeedToScanDevices = true;
1426 break; // return to the caller before we actually rescan
1427 }
1428
1429 // Report any devices that had last been added/removed.
Chris Ye989bb932020-07-04 16:18:59 -07001430 for (auto it = mClosingDevices.begin(); it != mClosingDevices.end();) {
1431 std::unique_ptr<Device> device = std::move(*it);
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001432 ALOGV("Reporting device closed: id=%d, name=%s\n", device->id, device->path.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001433 event->when = now;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001434 event->deviceId = (device->id == mBuiltInKeyboardId)
1435 ? ReservedInputDeviceId::BUILT_IN_KEYBOARD_ID
1436 : device->id;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001437 event->type = DEVICE_REMOVED;
1438 event += 1;
Chris Ye989bb932020-07-04 16:18:59 -07001439 it = mClosingDevices.erase(it);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001440 mNeedToSendFinishedDeviceScan = true;
1441 if (--capacity == 0) {
1442 break;
1443 }
1444 }
1445
1446 if (mNeedToScanDevices) {
1447 mNeedToScanDevices = false;
1448 scanDevicesLocked();
1449 mNeedToSendFinishedDeviceScan = true;
1450 }
1451
Siarhei Vishniakouf49608d2020-08-20 19:18:21 -05001452 while (!mOpeningDevices.empty()) {
1453 std::unique_ptr<Device> device = std::move(*mOpeningDevices.rbegin());
1454 mOpeningDevices.pop_back();
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001455 ALOGV("Reporting device opened: id=%d, name=%s\n", device->id, device->path.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001456 event->when = now;
1457 event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
1458 event->type = DEVICE_ADDED;
1459 event += 1;
Siarhei Vishniakouf49608d2020-08-20 19:18:21 -05001460
1461 // Try to find a matching video device by comparing device names
1462 for (auto it = mUnattachedVideoDevices.begin(); it != mUnattachedVideoDevices.end();
1463 it++) {
1464 std::unique_ptr<TouchVideoDevice>& videoDevice = *it;
Chris Yed3fef462021-03-07 17:10:08 -08001465 if (tryAddVideoDeviceLocked(*device, videoDevice)) {
Siarhei Vishniakouf49608d2020-08-20 19:18:21 -05001466 // videoDevice was transferred to 'device'
1467 it = mUnattachedVideoDevices.erase(it);
1468 break;
1469 }
1470 }
1471
1472 auto [dev_it, inserted] = mDevices.insert_or_assign(device->id, std::move(device));
1473 if (!inserted) {
Chris Ye989bb932020-07-04 16:18:59 -07001474 ALOGW("Device id %d exists, replaced.", device->id);
1475 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001476 mNeedToSendFinishedDeviceScan = true;
1477 if (--capacity == 0) {
1478 break;
1479 }
1480 }
1481
1482 if (mNeedToSendFinishedDeviceScan) {
1483 mNeedToSendFinishedDeviceScan = false;
1484 event->when = now;
1485 event->type = FINISHED_DEVICE_SCAN;
1486 event += 1;
1487 if (--capacity == 0) {
1488 break;
1489 }
1490 }
1491
1492 // Grab the next input event.
1493 bool deviceChanged = false;
1494 while (mPendingEventIndex < mPendingEventCount) {
1495 const struct epoll_event& eventItem = mPendingEventItems[mPendingEventIndex++];
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -07001496 if (eventItem.data.fd == mINotifyFd) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001497 if (eventItem.events & EPOLLIN) {
1498 mPendingINotify = true;
1499 } else {
1500 ALOGW("Received unexpected epoll event 0x%08x for INotify.", eventItem.events);
1501 }
1502 continue;
1503 }
1504
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -07001505 if (eventItem.data.fd == mWakeReadPipeFd) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001506 if (eventItem.events & EPOLLIN) {
1507 ALOGV("awoken after wake()");
1508 awoken = true;
Siarhei Vishniakoub4d960d2019-10-03 15:38:44 -05001509 char wakeReadBuffer[16];
Michael Wrightd02c5b62014-02-10 15:10:22 -08001510 ssize_t nRead;
1511 do {
Siarhei Vishniakoub4d960d2019-10-03 15:38:44 -05001512 nRead = read(mWakeReadPipeFd, wakeReadBuffer, sizeof(wakeReadBuffer));
1513 } while ((nRead == -1 && errno == EINTR) || nRead == sizeof(wakeReadBuffer));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001514 } else {
1515 ALOGW("Received unexpected epoll event 0x%08x for wake read pipe.",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001516 eventItem.events);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001517 }
1518 continue;
1519 }
1520
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -07001521 Device* device = getDeviceByFdLocked(eventItem.data.fd);
Chris Ye989bb932020-07-04 16:18:59 -07001522 if (device == nullptr) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001523 ALOGE("Received unexpected epoll event 0x%08x for unknown fd %d.", eventItem.events,
1524 eventItem.data.fd);
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001525 ALOG_ASSERT(!DEBUG);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001526 continue;
1527 }
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001528 if (device->videoDevice && eventItem.data.fd == device->videoDevice->getFd()) {
1529 if (eventItem.events & EPOLLIN) {
1530 size_t numFrames = device->videoDevice->readAndQueueFrames();
1531 if (numFrames == 0) {
1532 ALOGE("Received epoll event for video device %s, but could not read frame",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001533 device->videoDevice->getName().c_str());
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001534 }
1535 } else if (eventItem.events & EPOLLHUP) {
1536 // TODO(b/121395353) - consider adding EPOLLRDHUP
1537 ALOGI("Removing video device %s due to epoll hang-up event.",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001538 device->videoDevice->getName().c_str());
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001539 unregisterVideoDeviceFromEpollLocked(*device->videoDevice);
1540 device->videoDevice = nullptr;
1541 } else {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001542 ALOGW("Received unexpected epoll event 0x%08x for device %s.", eventItem.events,
1543 device->videoDevice->getName().c_str());
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001544 ALOG_ASSERT(!DEBUG);
1545 }
1546 continue;
1547 }
1548 // This must be an input event
Michael Wrightd02c5b62014-02-10 15:10:22 -08001549 if (eventItem.events & EPOLLIN) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001550 int32_t readSize =
1551 read(device->fd, readBuffer, sizeof(struct input_event) * capacity);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001552 if (readSize == 0 || (readSize < 0 && errno == ENODEV)) {
1553 // Device was removed before INotify noticed.
Mark Salyzyn5aa26b22014-06-10 13:07:44 -07001554 ALOGW("could not get event, removed? (fd: %d size: %" PRId32
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001555 " bufferSize: %zu capacity: %zu errno: %d)\n",
1556 device->fd, readSize, bufferSize, capacity, errno);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001557 deviceChanged = true;
Chris Ye989bb932020-07-04 16:18:59 -07001558 closeDeviceLocked(*device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001559 } else if (readSize < 0) {
1560 if (errno != EAGAIN && errno != EINTR) {
1561 ALOGW("could not get event (errno=%d)", errno);
1562 }
1563 } else if ((readSize % sizeof(struct input_event)) != 0) {
1564 ALOGE("could not get event (wrong size: %d)", readSize);
1565 } else {
1566 int32_t deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
1567
1568 size_t count = size_t(readSize) / sizeof(struct input_event);
1569 for (size_t i = 0; i < count; i++) {
1570 struct input_event& iev = readBuffer[i];
Siarhei Vishniakou592bac22018-11-08 19:42:38 -08001571 event->when = processEventTimestamp(iev);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001572 event->readTime = systemTime(SYSTEM_TIME_MONOTONIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001573 event->deviceId = deviceId;
1574 event->type = iev.type;
1575 event->code = iev.code;
1576 event->value = iev.value;
1577 event += 1;
1578 capacity -= 1;
1579 }
1580 if (capacity == 0) {
1581 // The result buffer is full. Reset the pending event index
1582 // so we will try to read the device again on the next iteration.
1583 mPendingEventIndex -= 1;
1584 break;
1585 }
1586 }
1587 } else if (eventItem.events & EPOLLHUP) {
1588 ALOGI("Removing device %s due to epoll hang-up event.",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001589 device->identifier.name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001590 deviceChanged = true;
Chris Ye989bb932020-07-04 16:18:59 -07001591 closeDeviceLocked(*device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001592 } else {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001593 ALOGW("Received unexpected epoll event 0x%08x for device %s.", eventItem.events,
1594 device->identifier.name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001595 }
1596 }
1597
1598 // readNotify() will modify the list of devices so this must be done after
1599 // processing all other events to ensure that we read all remaining events
1600 // before closing the devices.
1601 if (mPendingINotify && mPendingEventIndex >= mPendingEventCount) {
1602 mPendingINotify = false;
1603 readNotifyLocked();
1604 deviceChanged = true;
1605 }
1606
1607 // Report added or removed devices immediately.
1608 if (deviceChanged) {
1609 continue;
1610 }
1611
1612 // Return now if we have collected any events or if we were explicitly awoken.
1613 if (event != buffer || awoken) {
1614 break;
1615 }
1616
Siarhei Vishniakou4b5a87f2019-09-24 13:03:58 +01001617 // Poll for events.
1618 // When a device driver has pending (unread) events, it acquires
1619 // a kernel wake lock. Once the last pending event has been read, the device
1620 // driver will release the kernel wake lock, but the epoll will hold the wakelock,
1621 // since we are using EPOLLWAKEUP. The wakelock is released by the epoll when epoll_wait
1622 // is called again for the same fd that produced the event.
1623 // Thus the system can only sleep if there are no events pending or
1624 // currently being processed.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001625 //
1626 // The timeout is advisory only. If the device is asleep, it will not wake just to
1627 // service the timeout.
1628 mPendingEventIndex = 0;
1629
Siarhei Vishniakou4b5a87f2019-09-24 13:03:58 +01001630 mLock.unlock(); // release lock before poll
Michael Wrightd02c5b62014-02-10 15:10:22 -08001631
1632 int pollResult = epoll_wait(mEpollFd, mPendingEventItems, EPOLL_MAX_EVENTS, timeoutMillis);
1633
Siarhei Vishniakou4b5a87f2019-09-24 13:03:58 +01001634 mLock.lock(); // reacquire lock after poll
Michael Wrightd02c5b62014-02-10 15:10:22 -08001635
1636 if (pollResult == 0) {
1637 // Timed out.
1638 mPendingEventCount = 0;
1639 break;
1640 }
1641
1642 if (pollResult < 0) {
1643 // An error occurred.
1644 mPendingEventCount = 0;
1645
1646 // Sleep after errors to avoid locking up the system.
1647 // Hopefully the error is transient.
1648 if (errno != EINTR) {
1649 ALOGW("poll failed (errno=%d)\n", errno);
1650 usleep(100000);
1651 }
1652 } else {
1653 // Some events occurred.
1654 mPendingEventCount = size_t(pollResult);
1655 }
1656 }
1657
1658 // All done, return the number of events we read.
1659 return event - buffer;
1660}
1661
Siarhei Vishniakouadd89292018-12-13 19:23:36 -08001662std::vector<TouchVideoFrame> EventHub::getVideoFrames(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +00001663 std::scoped_lock _l(mLock);
Siarhei Vishniakouadd89292018-12-13 19:23:36 -08001664
1665 Device* device = getDeviceLocked(deviceId);
Chris Ye66fbac32020-07-06 20:36:43 -07001666 if (device == nullptr || !device->videoDevice) {
Siarhei Vishniakouadd89292018-12-13 19:23:36 -08001667 return {};
1668 }
1669 return device->videoDevice->consumeFrames();
1670}
1671
Michael Wrightd02c5b62014-02-10 15:10:22 -08001672void EventHub::wake() {
1673 ALOGV("wake() called");
1674
1675 ssize_t nWrite;
1676 do {
1677 nWrite = write(mWakeWritePipeFd, "W", 1);
1678 } while (nWrite == -1 && errno == EINTR);
1679
1680 if (nWrite != 1 && errno != EAGAIN) {
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001681 ALOGW("Could not write wake signal: %s", strerror(errno));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001682 }
1683}
1684
1685void EventHub::scanDevicesLocked() {
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001686 status_t result = scanDirLocked(DEVICE_PATH);
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001687 if (result < 0) {
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001688 ALOGE("scan dir failed for %s", DEVICE_PATH);
1689 }
Philip Quinn39b81682019-01-09 22:20:39 -08001690 if (isV4lScanningEnabled()) {
1691 result = scanVideoDirLocked(VIDEO_DEVICE_PATH);
1692 if (result != OK) {
1693 ALOGE("scan video dir failed for %s", VIDEO_DEVICE_PATH);
1694 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001695 }
Chris Ye989bb932020-07-04 16:18:59 -07001696 if (mDevices.find(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID) == mDevices.end()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001697 createVirtualKeyboardLocked();
1698 }
1699}
1700
1701// ----------------------------------------------------------------------------
1702
Michael Wrightd02c5b62014-02-10 15:10:22 -08001703static const int32_t GAMEPAD_KEYCODES[] = {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001704 AKEYCODE_BUTTON_A, AKEYCODE_BUTTON_B, AKEYCODE_BUTTON_C, //
1705 AKEYCODE_BUTTON_X, AKEYCODE_BUTTON_Y, AKEYCODE_BUTTON_Z, //
1706 AKEYCODE_BUTTON_L1, AKEYCODE_BUTTON_R1, //
1707 AKEYCODE_BUTTON_L2, AKEYCODE_BUTTON_R2, //
1708 AKEYCODE_BUTTON_THUMBL, AKEYCODE_BUTTON_THUMBR, //
1709 AKEYCODE_BUTTON_START, AKEYCODE_BUTTON_SELECT, AKEYCODE_BUTTON_MODE, //
Michael Wrightd02c5b62014-02-10 15:10:22 -08001710};
1711
Siarhei Vishniakou25920312018-12-12 15:24:44 -08001712status_t EventHub::registerFdForEpoll(int fd) {
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001713 // TODO(b/121395353) - consider adding EPOLLRDHUP
Siarhei Vishniakou25920312018-12-12 15:24:44 -08001714 struct epoll_event eventItem = {};
1715 eventItem.events = EPOLLIN | EPOLLWAKEUP;
1716 eventItem.data.fd = fd;
1717 if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, &eventItem)) {
1718 ALOGE("Could not add fd to epoll instance: %s", strerror(errno));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001719 return -errno;
1720 }
1721 return OK;
1722}
1723
Siarhei Vishniakou25920312018-12-12 15:24:44 -08001724status_t EventHub::unregisterFdFromEpoll(int fd) {
1725 if (epoll_ctl(mEpollFd, EPOLL_CTL_DEL, fd, nullptr)) {
1726 ALOGW("Could not remove fd from epoll instance: %s", strerror(errno));
1727 return -errno;
1728 }
1729 return OK;
1730}
1731
Chris Ye989bb932020-07-04 16:18:59 -07001732status_t EventHub::registerDeviceForEpollLocked(Device& device) {
1733 status_t result = registerFdForEpoll(device.fd);
Siarhei Vishniakou25920312018-12-12 15:24:44 -08001734 if (result != OK) {
Chris Ye989bb932020-07-04 16:18:59 -07001735 ALOGE("Could not add input device fd to epoll for device %" PRId32, device.id);
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001736 return result;
1737 }
Chris Ye989bb932020-07-04 16:18:59 -07001738 if (device.videoDevice) {
1739 registerVideoDeviceForEpollLocked(*device.videoDevice);
Siarhei Vishniakou25920312018-12-12 15:24:44 -08001740 }
1741 return result;
1742}
1743
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001744void EventHub::registerVideoDeviceForEpollLocked(const TouchVideoDevice& videoDevice) {
1745 status_t result = registerFdForEpoll(videoDevice.getFd());
1746 if (result != OK) {
1747 ALOGE("Could not add video device %s to epoll", videoDevice.getName().c_str());
1748 }
1749}
1750
Chris Ye989bb932020-07-04 16:18:59 -07001751status_t EventHub::unregisterDeviceFromEpollLocked(Device& device) {
1752 if (device.hasValidFd()) {
1753 status_t result = unregisterFdFromEpoll(device.fd);
Siarhei Vishniakou25920312018-12-12 15:24:44 -08001754 if (result != OK) {
Chris Ye989bb932020-07-04 16:18:59 -07001755 ALOGW("Could not remove input device fd from epoll for device %" PRId32, device.id);
Siarhei Vishniakou25920312018-12-12 15:24:44 -08001756 return result;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001757 }
1758 }
Chris Ye989bb932020-07-04 16:18:59 -07001759 if (device.videoDevice) {
1760 unregisterVideoDeviceFromEpollLocked(*device.videoDevice);
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001761 }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001762 return OK;
1763}
1764
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001765void EventHub::unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& videoDevice) {
1766 if (videoDevice.hasValidFd()) {
1767 status_t result = unregisterFdFromEpoll(videoDevice.getFd());
1768 if (result != OK) {
1769 ALOGW("Could not remove video device fd from epoll for device: %s",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001770 videoDevice.getName().c_str());
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001771 }
1772 }
1773}
1774
Chris Yed3fef462021-03-07 17:10:08 -08001775void EventHub::reportDeviceAddedForStatisticsLocked(const InputDeviceIdentifier& identifier,
1776 Flags<InputDeviceClass> classes) {
1777 android::util::stats_write(android::util::INPUTDEVICE_REGISTERED, identifier.name.c_str(),
1778 identifier.vendor, identifier.product, identifier.version,
1779 identifier.bus, identifier.uniqueId.c_str(), classes.get());
1780}
1781
Siarhei Vishniakoua4c502a2021-02-05 00:45:20 +00001782void EventHub::openDeviceLocked(const std::string& devicePath) {
1783 // If an input device happens to register around the time when EventHub's constructor runs, it
1784 // is possible that the same input event node (for example, /dev/input/event3) will be noticed
1785 // in both 'inotify' callback and also in the 'scanDirLocked' pass. To prevent duplicate devices
1786 // from getting registered, ensure that this path is not already covered by an existing device.
1787 for (const auto& [deviceId, device] : mDevices) {
1788 if (device->path == devicePath) {
1789 return; // device was already registered
1790 }
1791 }
1792
Michael Wrightd02c5b62014-02-10 15:10:22 -08001793 char buffer[80];
1794
Chris Ye8594e192020-07-14 10:34:06 -07001795 ALOGV("Opening device: %s", devicePath.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001796
Chris Ye8594e192020-07-14 10:34:06 -07001797 int fd = open(devicePath.c_str(), O_RDWR | O_CLOEXEC | O_NONBLOCK);
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001798 if (fd < 0) {
Chris Ye8594e192020-07-14 10:34:06 -07001799 ALOGE("could not open %s, %s\n", devicePath.c_str(), strerror(errno));
Siarhei Vishniakoua4c502a2021-02-05 00:45:20 +00001800 return;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001801 }
1802
1803 InputDeviceIdentifier identifier;
1804
1805 // Get device name.
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001806 if (ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) {
Chris Ye8594e192020-07-14 10:34:06 -07001807 ALOGE("Could not get device name for %s: %s", devicePath.c_str(), strerror(errno));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001808 } else {
1809 buffer[sizeof(buffer) - 1] = '\0';
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001810 identifier.name = buffer;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001811 }
1812
1813 // Check to see if the device is on our excluded list
1814 for (size_t i = 0; i < mExcludedDevices.size(); i++) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001815 const std::string& item = mExcludedDevices[i];
Michael Wrightd02c5b62014-02-10 15:10:22 -08001816 if (identifier.name == item) {
Chris Ye8594e192020-07-14 10:34:06 -07001817 ALOGI("ignoring event id %s driver %s\n", devicePath.c_str(), item.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001818 close(fd);
Siarhei Vishniakoua4c502a2021-02-05 00:45:20 +00001819 return;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001820 }
1821 }
1822
1823 // Get device driver version.
1824 int driverVersion;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001825 if (ioctl(fd, EVIOCGVERSION, &driverVersion)) {
Chris Ye8594e192020-07-14 10:34:06 -07001826 ALOGE("could not get driver version for %s, %s\n", devicePath.c_str(), strerror(errno));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001827 close(fd);
Siarhei Vishniakoua4c502a2021-02-05 00:45:20 +00001828 return;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001829 }
1830
1831 // Get device identifier.
1832 struct input_id inputId;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001833 if (ioctl(fd, EVIOCGID, &inputId)) {
Chris Ye8594e192020-07-14 10:34:06 -07001834 ALOGE("could not get device input id for %s, %s\n", devicePath.c_str(), strerror(errno));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001835 close(fd);
Siarhei Vishniakoua4c502a2021-02-05 00:45:20 +00001836 return;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001837 }
1838 identifier.bus = inputId.bustype;
1839 identifier.product = inputId.product;
1840 identifier.vendor = inputId.vendor;
1841 identifier.version = inputId.version;
1842
1843 // Get device physical location.
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001844 if (ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) {
1845 // fprintf(stderr, "could not get location for %s, %s\n", devicePath, strerror(errno));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001846 } else {
1847 buffer[sizeof(buffer) - 1] = '\0';
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001848 identifier.location = buffer;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001849 }
1850
1851 // Get device unique id.
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001852 if (ioctl(fd, EVIOCGUNIQ(sizeof(buffer) - 1), &buffer) < 1) {
1853 // fprintf(stderr, "could not get idstring for %s, %s\n", devicePath, strerror(errno));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001854 } else {
1855 buffer[sizeof(buffer) - 1] = '\0';
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001856 identifier.uniqueId = buffer;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001857 }
1858
1859 // Fill in the descriptor.
1860 assignDescriptorLocked(identifier);
1861
Michael Wrightd02c5b62014-02-10 15:10:22 -08001862 // Allocate device. (The device object takes ownership of the fd at this point.)
1863 int32_t deviceId = mNextDeviceId++;
Chris Ye989bb932020-07-04 16:18:59 -07001864 std::unique_ptr<Device> device = std::make_unique<Device>(fd, deviceId, devicePath, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001865
Chris Ye8594e192020-07-14 10:34:06 -07001866 ALOGV("add device %d: %s\n", deviceId, devicePath.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001867 ALOGV(" bus: %04x\n"
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001868 " vendor %04x\n"
1869 " product %04x\n"
1870 " version %04x\n",
1871 identifier.bus, identifier.vendor, identifier.product, identifier.version);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001872 ALOGV(" name: \"%s\"\n", identifier.name.c_str());
1873 ALOGV(" location: \"%s\"\n", identifier.location.c_str());
1874 ALOGV(" unique id: \"%s\"\n", identifier.uniqueId.c_str());
1875 ALOGV(" descriptor: \"%s\"\n", identifier.descriptor.c_str());
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001876 ALOGV(" driver: v%d.%d.%d\n", driverVersion >> 16, (driverVersion >> 8) & 0xff,
1877 driverVersion & 0xff);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001878
1879 // Load the configuration file for the device.
Chris Ye989bb932020-07-04 16:18:59 -07001880 device->loadConfigurationLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001881
Chris Ye3fdbfef2021-01-06 18:45:18 -08001882 // Grab the device's sysfs path
1883 device->sysfsRootPath = getSysfsRootPath(devicePath.c_str());
1884 // find related components
1885 bool hasBattery = device->configureBatteryLocked();
1886 bool hasLights = device->configureLightsLocked();
1887
Michael Wrightd02c5b62014-02-10 15:10:22 -08001888 // Figure out the kinds of events the device reports.
Chris Ye66fbac32020-07-06 20:36:43 -07001889 device->readDeviceBitMask(EVIOCGBIT(EV_KEY, 0), device->keyBitmask);
1890 device->readDeviceBitMask(EVIOCGBIT(EV_ABS, 0), device->absBitmask);
1891 device->readDeviceBitMask(EVIOCGBIT(EV_REL, 0), device->relBitmask);
1892 device->readDeviceBitMask(EVIOCGBIT(EV_SW, 0), device->swBitmask);
1893 device->readDeviceBitMask(EVIOCGBIT(EV_LED, 0), device->ledBitmask);
1894 device->readDeviceBitMask(EVIOCGBIT(EV_FF, 0), device->ffBitmask);
Chris Yef59a2f42020-10-16 12:55:26 -07001895 device->readDeviceBitMask(EVIOCGBIT(EV_MSC, 0), device->mscBitmask);
Chris Ye66fbac32020-07-06 20:36:43 -07001896 device->readDeviceBitMask(EVIOCGPROP(0), device->propBitmask);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001897
1898 // See if this is a keyboard. Ignore everything in the button range except for
1899 // joystick and gamepad buttons which are handled like keyboards for the most part.
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001900 bool haveKeyboardKeys =
Chris Ye66fbac32020-07-06 20:36:43 -07001901 device->keyBitmask.any(0, BTN_MISC) || device->keyBitmask.any(BTN_WHEEL, KEY_MAX + 1);
1902 bool haveGamepadButtons = device->keyBitmask.any(BTN_MISC, BTN_MOUSE) ||
1903 device->keyBitmask.any(BTN_JOYSTICK, BTN_DIGI);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001904 if (haveKeyboardKeys || haveGamepadButtons) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001905 device->classes |= InputDeviceClass::KEYBOARD;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001906 }
1907
1908 // See if this is a cursor device such as a trackball or mouse.
Chris Ye66fbac32020-07-06 20:36:43 -07001909 if (device->keyBitmask.test(BTN_MOUSE) && device->relBitmask.test(REL_X) &&
1910 device->relBitmask.test(REL_Y)) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001911 device->classes |= InputDeviceClass::CURSOR;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001912 }
1913
Prashant Malani1941ff52015-08-11 18:29:28 -07001914 // See if this is a rotary encoder type device.
1915 String8 deviceType = String8();
1916 if (device->configuration &&
1917 device->configuration->tryGetProperty(String8("device.type"), deviceType)) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001918 if (!deviceType.compare(String8("rotaryEncoder"))) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001919 device->classes |= InputDeviceClass::ROTARY_ENCODER;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001920 }
Prashant Malani1941ff52015-08-11 18:29:28 -07001921 }
1922
Michael Wrightd02c5b62014-02-10 15:10:22 -08001923 // See if this is a touch pad.
1924 // Is this a new modern multi-touch driver?
Chris Ye66fbac32020-07-06 20:36:43 -07001925 if (device->absBitmask.test(ABS_MT_POSITION_X) && device->absBitmask.test(ABS_MT_POSITION_Y)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001926 // Some joysticks such as the PS3 controller report axes that conflict
1927 // with the ABS_MT range. Try to confirm that the device really is
1928 // a touch screen.
Chris Ye66fbac32020-07-06 20:36:43 -07001929 if (device->keyBitmask.test(BTN_TOUCH) || !haveGamepadButtons) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001930 device->classes |= (InputDeviceClass::TOUCH | InputDeviceClass::TOUCH_MT);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001931 }
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001932 // Is this an old style single-touch driver?
Chris Ye66fbac32020-07-06 20:36:43 -07001933 } else if (device->keyBitmask.test(BTN_TOUCH) && device->absBitmask.test(ABS_X) &&
1934 device->absBitmask.test(ABS_Y)) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001935 device->classes |= InputDeviceClass::TOUCH;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001936 // Is this a BT stylus?
Chris Ye66fbac32020-07-06 20:36:43 -07001937 } else if ((device->absBitmask.test(ABS_PRESSURE) || device->keyBitmask.test(BTN_TOUCH)) &&
1938 !device->absBitmask.test(ABS_X) && !device->absBitmask.test(ABS_Y)) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001939 device->classes |= InputDeviceClass::EXTERNAL_STYLUS;
Michael Wright842500e2015-03-13 17:32:02 -07001940 // Keyboard will try to claim some of the buttons but we really want to reserve those so we
1941 // can fuse it with the touch screen data, so just take them back. Note this means an
1942 // external stylus cannot also be a keyboard device.
Chris Ye1b0c7342020-07-28 21:57:03 -07001943 device->classes &= ~InputDeviceClass::KEYBOARD;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001944 }
1945
1946 // See if this device is a joystick.
1947 // Assumes that joysticks always have gamepad buttons in order to distinguish them
1948 // from other devices such as accelerometers that also have absolute axes.
1949 if (haveGamepadButtons) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001950 auto assumedClasses = device->classes | InputDeviceClass::JOYSTICK;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001951 for (int i = 0; i <= ABS_MAX; i++) {
Chris Ye66fbac32020-07-06 20:36:43 -07001952 if (device->absBitmask.test(i) &&
Chris Ye1b0c7342020-07-28 21:57:03 -07001953 (getAbsAxisUsage(i, assumedClasses).test(InputDeviceClass::JOYSTICK))) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001954 device->classes = assumedClasses;
1955 break;
1956 }
1957 }
1958 }
1959
Chris Yef59a2f42020-10-16 12:55:26 -07001960 // Check whether this device is an accelerometer.
1961 if (device->propBitmask.test(INPUT_PROP_ACCELEROMETER)) {
1962 device->classes |= InputDeviceClass::SENSOR;
1963 }
1964
Michael Wrightd02c5b62014-02-10 15:10:22 -08001965 // Check whether this device has switches.
1966 for (int i = 0; i <= SW_MAX; i++) {
Chris Ye66fbac32020-07-06 20:36:43 -07001967 if (device->swBitmask.test(i)) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001968 device->classes |= InputDeviceClass::SWITCH;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001969 break;
1970 }
1971 }
1972
1973 // Check whether this device supports the vibrator.
Chris Ye66fbac32020-07-06 20:36:43 -07001974 if (device->ffBitmask.test(FF_RUMBLE)) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001975 device->classes |= InputDeviceClass::VIBRATOR;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001976 }
1977
1978 // Configure virtual keys.
Chris Ye1b0c7342020-07-28 21:57:03 -07001979 if ((device->classes.test(InputDeviceClass::TOUCH))) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001980 // Load the virtual keys for the touch screen, if any.
1981 // We do this now so that we can make sure to load the keymap if necessary.
Chris Ye989bb932020-07-04 16:18:59 -07001982 bool success = device->loadVirtualKeyMapLocked();
Siarhei Vishniakou3e78dec2019-02-20 16:21:46 -06001983 if (success) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001984 device->classes |= InputDeviceClass::KEYBOARD;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001985 }
1986 }
1987
1988 // Load the key map.
Chris Yef59a2f42020-10-16 12:55:26 -07001989 // We need to do this for joysticks too because the key layout may specify axes, and for
1990 // sensor as well because the key layout may specify the axes to sensor data mapping.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001991 status_t keyMapStatus = NAME_NOT_FOUND;
Chris Yef59a2f42020-10-16 12:55:26 -07001992 if (device->classes.any(InputDeviceClass::KEYBOARD | InputDeviceClass::JOYSTICK |
1993 InputDeviceClass::SENSOR)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001994 // Load the keymap for the device.
Chris Ye989bb932020-07-04 16:18:59 -07001995 keyMapStatus = device->loadKeyMapLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001996 }
1997
1998 // Configure the keyboard, gamepad or virtual keyboard.
Chris Ye1b0c7342020-07-28 21:57:03 -07001999 if (device->classes.test(InputDeviceClass::KEYBOARD)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002000 // Register the keyboard as a built-in keyboard if it is eligible.
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07002001 if (!keyMapStatus && mBuiltInKeyboardId == NO_BUILT_IN_KEYBOARD &&
Siarhei Vishniakoud549b252020-08-11 11:25:26 -05002002 isEligibleBuiltInKeyboard(device->identifier, device->configuration.get(),
2003 &device->keyMap)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002004 mBuiltInKeyboardId = device->id;
2005 }
2006
2007 // 'Q' key support = cheap test of whether this is an alpha-capable kbd
Chris Ye989bb932020-07-04 16:18:59 -07002008 if (device->hasKeycodeLocked(AKEYCODE_Q)) {
Chris Ye1b0c7342020-07-28 21:57:03 -07002009 device->classes |= InputDeviceClass::ALPHAKEY;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002010 }
2011
2012 // See if this device has a DPAD.
Chris Ye989bb932020-07-04 16:18:59 -07002013 if (device->hasKeycodeLocked(AKEYCODE_DPAD_UP) &&
2014 device->hasKeycodeLocked(AKEYCODE_DPAD_DOWN) &&
2015 device->hasKeycodeLocked(AKEYCODE_DPAD_LEFT) &&
2016 device->hasKeycodeLocked(AKEYCODE_DPAD_RIGHT) &&
2017 device->hasKeycodeLocked(AKEYCODE_DPAD_CENTER)) {
Chris Ye1b0c7342020-07-28 21:57:03 -07002018 device->classes |= InputDeviceClass::DPAD;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002019 }
2020
2021 // See if this device has a gamepad.
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07002022 for (size_t i = 0; i < sizeof(GAMEPAD_KEYCODES) / sizeof(GAMEPAD_KEYCODES[0]); i++) {
Chris Ye989bb932020-07-04 16:18:59 -07002023 if (device->hasKeycodeLocked(GAMEPAD_KEYCODES[i])) {
Chris Ye1b0c7342020-07-28 21:57:03 -07002024 device->classes |= InputDeviceClass::GAMEPAD;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002025 break;
2026 }
2027 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002028 }
2029
2030 // If the device isn't recognized as something we handle, don't monitor it.
Chris Ye1b0c7342020-07-28 21:57:03 -07002031 if (device->classes == Flags<InputDeviceClass>(0)) {
Chris Ye8594e192020-07-14 10:34:06 -07002032 ALOGV("Dropping device: id=%d, path='%s', name='%s'", deviceId, devicePath.c_str(),
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07002033 device->identifier.name.c_str());
Siarhei Vishniakoua4c502a2021-02-05 00:45:20 +00002034 return;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002035 }
2036
Chris Ye3fdbfef2021-01-06 18:45:18 -08002037 // Classify InputDeviceClass::BATTERY.
2038 if (hasBattery) {
2039 device->classes |= InputDeviceClass::BATTERY;
2040 }
Kim Low03ea0352020-11-06 12:45:07 -08002041
Chris Ye3fdbfef2021-01-06 18:45:18 -08002042 // Classify InputDeviceClass::LIGHT.
2043 if (hasLights) {
2044 device->classes |= InputDeviceClass::LIGHT;
Kim Low03ea0352020-11-06 12:45:07 -08002045 }
2046
Tim Kilbourn063ff532015-04-08 10:26:18 -07002047 // Determine whether the device has a mic.
Chris Ye989bb932020-07-04 16:18:59 -07002048 if (device->deviceHasMicLocked()) {
Chris Ye1b0c7342020-07-28 21:57:03 -07002049 device->classes |= InputDeviceClass::MIC;
Tim Kilbourn063ff532015-04-08 10:26:18 -07002050 }
2051
Michael Wrightd02c5b62014-02-10 15:10:22 -08002052 // Determine whether the device is external or internal.
Chris Ye989bb932020-07-04 16:18:59 -07002053 if (device->isExternalDeviceLocked()) {
Chris Ye1b0c7342020-07-28 21:57:03 -07002054 device->classes |= InputDeviceClass::EXTERNAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002055 }
2056
Chris Ye1b0c7342020-07-28 21:57:03 -07002057 if (device->classes.any(InputDeviceClass::JOYSTICK | InputDeviceClass::DPAD) &&
2058 device->classes.test(InputDeviceClass::GAMEPAD)) {
Chris Ye989bb932020-07-04 16:18:59 -07002059 device->controllerNumber = getNextControllerNumberLocked(device->identifier.name);
2060 device->setLedForControllerLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002061 }
2062
Chris Ye989bb932020-07-04 16:18:59 -07002063 if (registerDeviceForEpollLocked(*device) != OK) {
Siarhei Vishniakoua4c502a2021-02-05 00:45:20 +00002064 return;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002065 }
2066
Chris Ye989bb932020-07-04 16:18:59 -07002067 device->configureFd();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002068
Chris Ye1b0c7342020-07-28 21:57:03 -07002069 ALOGI("New device: id=%d, fd=%d, path='%s', name='%s', classes=%s, "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07002070 "configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s, ",
Chris Ye1b0c7342020-07-28 21:57:03 -07002071 deviceId, fd, devicePath.c_str(), device->identifier.name.c_str(),
2072 device->classes.string().c_str(), device->configurationFile.c_str(),
2073 device->keyMap.keyLayoutFile.c_str(), device->keyMap.keyCharacterMapFile.c_str(),
2074 toString(mBuiltInKeyboardId == deviceId));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002075
Chris Ye989bb932020-07-04 16:18:59 -07002076 addDeviceLocked(std::move(device));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002077}
2078
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08002079void EventHub::openVideoDeviceLocked(const std::string& devicePath) {
2080 std::unique_ptr<TouchVideoDevice> videoDevice = TouchVideoDevice::create(devicePath);
2081 if (!videoDevice) {
2082 ALOGE("Could not create touch video device for %s. Ignoring", devicePath.c_str());
2083 return;
2084 }
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08002085 // Transfer ownership of this video device to a matching input device
Chris Ye989bb932020-07-04 16:18:59 -07002086 for (const auto& [id, device] : mDevices) {
Chris Yed3fef462021-03-07 17:10:08 -08002087 if (tryAddVideoDeviceLocked(*device, videoDevice)) {
Siarhei Vishniakouf49608d2020-08-20 19:18:21 -05002088 return; // 'device' now owns 'videoDevice'
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08002089 }
2090 }
2091
2092 // Couldn't find a matching input device, so just add it to a temporary holding queue.
2093 // A matching input device may appear later.
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08002094 ALOGI("Adding video device %s to list of unattached video devices",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07002095 videoDevice->getName().c_str());
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08002096 mUnattachedVideoDevices.push_back(std::move(videoDevice));
2097}
2098
Chris Yed3fef462021-03-07 17:10:08 -08002099bool EventHub::tryAddVideoDeviceLocked(EventHub::Device& device,
2100 std::unique_ptr<TouchVideoDevice>& videoDevice) {
Siarhei Vishniakouf49608d2020-08-20 19:18:21 -05002101 if (videoDevice->getName() != device.identifier.name) {
2102 return false;
2103 }
2104 device.videoDevice = std::move(videoDevice);
2105 if (device.enabled) {
2106 registerVideoDeviceForEpollLocked(*device.videoDevice);
2107 }
2108 return true;
2109}
2110
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002111bool EventHub::isDeviceEnabled(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +00002112 std::scoped_lock _l(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002113 Device* device = getDeviceLocked(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -07002114 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002115 ALOGE("Invalid device id=%" PRId32 " provided to %s", deviceId, __func__);
2116 return false;
2117 }
2118 return device->enabled;
2119}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002120
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002121status_t EventHub::enableDevice(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +00002122 std::scoped_lock _l(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002123 Device* device = getDeviceLocked(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -07002124 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002125 ALOGE("Invalid device id=%" PRId32 " provided to %s", deviceId, __func__);
2126 return BAD_VALUE;
2127 }
2128 if (device->enabled) {
2129 ALOGW("Duplicate call to %s, input device %" PRId32 " already enabled", __func__, deviceId);
2130 return OK;
2131 }
2132 status_t result = device->enable();
2133 if (result != OK) {
2134 ALOGE("Failed to enable device %" PRId32, deviceId);
2135 return result;
2136 }
2137
Chris Ye989bb932020-07-04 16:18:59 -07002138 device->configureFd();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002139
Chris Ye989bb932020-07-04 16:18:59 -07002140 return registerDeviceForEpollLocked(*device);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002141}
2142
2143status_t EventHub::disableDevice(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +00002144 std::scoped_lock _l(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002145 Device* device = getDeviceLocked(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -07002146 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002147 ALOGE("Invalid device id=%" PRId32 " provided to %s", deviceId, __func__);
2148 return BAD_VALUE;
2149 }
2150 if (!device->enabled) {
2151 ALOGW("Duplicate call to %s, input device already disabled", __func__);
2152 return OK;
2153 }
Chris Ye989bb932020-07-04 16:18:59 -07002154 unregisterDeviceFromEpollLocked(*device);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002155 return device->disable();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002156}
2157
2158void EventHub::createVirtualKeyboardLocked() {
2159 InputDeviceIdentifier identifier;
2160 identifier.name = "Virtual";
2161 identifier.uniqueId = "<virtual>";
2162 assignDescriptorLocked(identifier);
2163
Chris Ye989bb932020-07-04 16:18:59 -07002164 std::unique_ptr<Device> device =
2165 std::make_unique<Device>(-1, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, "<virtual>",
2166 identifier);
Chris Ye1b0c7342020-07-28 21:57:03 -07002167 device->classes = InputDeviceClass::KEYBOARD | InputDeviceClass::ALPHAKEY |
2168 InputDeviceClass::DPAD | InputDeviceClass::VIRTUAL;
Chris Ye989bb932020-07-04 16:18:59 -07002169 device->loadKeyMapLocked();
2170 addDeviceLocked(std::move(device));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002171}
2172
Chris Ye989bb932020-07-04 16:18:59 -07002173void EventHub::addDeviceLocked(std::unique_ptr<Device> device) {
Chris Yed3fef462021-03-07 17:10:08 -08002174 reportDeviceAddedForStatisticsLocked(device->identifier, device->classes);
Chris Ye989bb932020-07-04 16:18:59 -07002175 mOpeningDevices.push_back(std::move(device));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002176}
2177
Chris Ye989bb932020-07-04 16:18:59 -07002178int32_t EventHub::getNextControllerNumberLocked(const std::string& name) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002179 if (mControllerNumbers.isFull()) {
2180 ALOGI("Maximum number of controllers reached, assigning controller number 0 to device %s",
Chris Ye989bb932020-07-04 16:18:59 -07002181 name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002182 return 0;
2183 }
2184 // Since the controller number 0 is reserved for non-controllers, translate all numbers up by
2185 // one
2186 return static_cast<int32_t>(mControllerNumbers.markFirstUnmarkedBit() + 1);
2187}
2188
Chris Ye989bb932020-07-04 16:18:59 -07002189void EventHub::releaseControllerNumberLocked(int32_t num) {
2190 if (num > 0) {
2191 mControllerNumbers.clearBit(static_cast<uint32_t>(num - 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002192 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002193}
2194
Chris Ye8594e192020-07-14 10:34:06 -07002195void EventHub::closeDeviceByPathLocked(const std::string& devicePath) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002196 Device* device = getDeviceByPathLocked(devicePath);
Chris Ye989bb932020-07-04 16:18:59 -07002197 if (device != nullptr) {
2198 closeDeviceLocked(*device);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08002199 return;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002200 }
Chris Ye8594e192020-07-14 10:34:06 -07002201 ALOGV("Remove device: %s not found, device may already have been removed.", devicePath.c_str());
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08002202}
2203
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08002204/**
2205 * Find the video device by filename, and close it.
2206 * The video device is closed by path during an inotify event, where we don't have the
2207 * additional context about the video device fd, or the associated input device.
2208 */
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08002209void EventHub::closeVideoDeviceByPathLocked(const std::string& devicePath) {
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08002210 // A video device may be owned by an existing input device, or it may be stored in
2211 // the mUnattachedVideoDevices queue. Check both locations.
Chris Ye989bb932020-07-04 16:18:59 -07002212 for (const auto& [id, device] : mDevices) {
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08002213 if (device->videoDevice && device->videoDevice->getPath() == devicePath) {
Siarhei Vishniakou12598682018-11-02 17:19:19 -07002214 unregisterVideoDeviceFromEpollLocked(*device->videoDevice);
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08002215 device->videoDevice = nullptr;
2216 return;
2217 }
2218 }
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07002219 mUnattachedVideoDevices
2220 .erase(std::remove_if(mUnattachedVideoDevices.begin(), mUnattachedVideoDevices.end(),
2221 [&devicePath](
2222 const std::unique_ptr<TouchVideoDevice>& videoDevice) {
2223 return videoDevice->getPath() == devicePath;
2224 }),
2225 mUnattachedVideoDevices.end());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002226}
2227
2228void EventHub::closeAllDevicesLocked() {
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08002229 mUnattachedVideoDevices.clear();
Siarhei Vishniakoud549b252020-08-11 11:25:26 -05002230 while (!mDevices.empty()) {
2231 closeDeviceLocked(*(mDevices.begin()->second));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002232 }
2233}
2234
Chris Ye989bb932020-07-04 16:18:59 -07002235void EventHub::closeDeviceLocked(Device& device) {
2236 ALOGI("Removed device: path=%s name=%s id=%d fd=%d classes=%s", device.path.c_str(),
2237 device.identifier.name.c_str(), device.id, device.fd, device.classes.string().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002238
Chris Ye989bb932020-07-04 16:18:59 -07002239 if (device.id == mBuiltInKeyboardId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002240 ALOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this",
Chris Ye989bb932020-07-04 16:18:59 -07002241 device.path.c_str(), mBuiltInKeyboardId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002242 mBuiltInKeyboardId = NO_BUILT_IN_KEYBOARD;
2243 }
2244
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002245 unregisterDeviceFromEpollLocked(device);
Chris Ye989bb932020-07-04 16:18:59 -07002246 if (device.videoDevice) {
Siarhei Vishniakou12598682018-11-02 17:19:19 -07002247 // This must be done after the video device is removed from epoll
Chris Ye989bb932020-07-04 16:18:59 -07002248 mUnattachedVideoDevices.push_back(std::move(device.videoDevice));
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08002249 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002250
Chris Ye989bb932020-07-04 16:18:59 -07002251 releaseControllerNumberLocked(device.controllerNumber);
Siarhei Vishniakoud549b252020-08-11 11:25:26 -05002252 device.controllerNumber = 0;
Chris Ye989bb932020-07-04 16:18:59 -07002253 device.close();
Chris Ye989bb932020-07-04 16:18:59 -07002254 mClosingDevices.push_back(std::move(mDevices[device.id]));
Siarhei Vishniakoud549b252020-08-11 11:25:26 -05002255
Chris Ye989bb932020-07-04 16:18:59 -07002256 mDevices.erase(device.id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002257}
2258
2259status_t EventHub::readNotifyLocked() {
2260 int res;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002261 char event_buf[512];
2262 int event_size;
2263 int event_pos = 0;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07002264 struct inotify_event* event;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002265
2266 ALOGV("EventHub::readNotify nfd: %d\n", mINotifyFd);
2267 res = read(mINotifyFd, event_buf, sizeof(event_buf));
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07002268 if (res < (int)sizeof(*event)) {
2269 if (errno == EINTR) return 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002270 ALOGW("could not get event, %s\n", strerror(errno));
2271 return -1;
2272 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002273
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07002274 while (res >= (int)sizeof(*event)) {
2275 event = (struct inotify_event*)(event_buf + event_pos);
2276 if (event->len) {
Siarhei Vishniakou951f3622018-12-12 19:45:42 -08002277 if (event->wd == mInputWd) {
Chris Ye8594e192020-07-14 10:34:06 -07002278 std::string filename = std::string(DEVICE_PATH) + "/" + event->name;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07002279 if (event->mask & IN_CREATE) {
Chris Ye8594e192020-07-14 10:34:06 -07002280 openDeviceLocked(filename);
Siarhei Vishniakou951f3622018-12-12 19:45:42 -08002281 } else {
2282 ALOGI("Removing device '%s' due to inotify event\n", filename.c_str());
Chris Ye8594e192020-07-14 10:34:06 -07002283 closeDeviceByPathLocked(filename);
Siarhei Vishniakou951f3622018-12-12 19:45:42 -08002284 }
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07002285 } else if (event->wd == mVideoWd) {
Siarhei Vishniakou951f3622018-12-12 19:45:42 -08002286 if (isV4lTouchNode(event->name)) {
Chris Ye8594e192020-07-14 10:34:06 -07002287 std::string filename = std::string(VIDEO_DEVICE_PATH) + "/" + event->name;
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08002288 if (event->mask & IN_CREATE) {
2289 openVideoDeviceLocked(filename);
2290 } else {
2291 ALOGI("Removing video device '%s' due to inotify event", filename.c_str());
2292 closeVideoDeviceByPathLocked(filename);
2293 }
Siarhei Vishniakou951f3622018-12-12 19:45:42 -08002294 }
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07002295 } else {
Siarhei Vishniakou951f3622018-12-12 19:45:42 -08002296 LOG_ALWAYS_FATAL("Unexpected inotify event, wd = %i", event->wd);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002297 }
2298 }
2299 event_size = sizeof(*event) + event->len;
2300 res -= event_size;
2301 event_pos += event_size;
2302 }
2303 return 0;
2304}
2305
Chris Ye8594e192020-07-14 10:34:06 -07002306status_t EventHub::scanDirLocked(const std::string& dirname) {
2307 for (const auto& entry : std::filesystem::directory_iterator(dirname)) {
2308 openDeviceLocked(entry.path());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002309 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002310 return 0;
2311}
2312
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08002313/**
2314 * Look for all dirname/v4l-touch* devices, and open them.
2315 */
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07002316status_t EventHub::scanVideoDirLocked(const std::string& dirname) {
Chris Ye8594e192020-07-14 10:34:06 -07002317 for (const auto& entry : std::filesystem::directory_iterator(dirname)) {
2318 if (isV4lTouchNode(entry.path())) {
2319 ALOGI("Found touch video device %s", entry.path().c_str());
2320 openVideoDeviceLocked(entry.path());
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08002321 }
2322 }
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08002323 return OK;
2324}
2325
Michael Wrightd02c5b62014-02-10 15:10:22 -08002326void EventHub::requestReopenDevices() {
2327 ALOGV("requestReopenDevices() called");
2328
Chris Ye87143712020-11-10 05:05:58 +00002329 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002330 mNeedToReopenDevices = true;
2331}
2332
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08002333void EventHub::dump(std::string& dump) {
2334 dump += "Event Hub State:\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002335
2336 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +00002337 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002338
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08002339 dump += StringPrintf(INDENT "BuiltInKeyboardId: %d\n", mBuiltInKeyboardId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002340
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08002341 dump += INDENT "Devices:\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002342
Chris Ye989bb932020-07-04 16:18:59 -07002343 for (const auto& [id, device] : mDevices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002344 if (mBuiltInKeyboardId == device->id) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08002345 dump += StringPrintf(INDENT2 "%d: %s (aka device 0 - built-in keyboard)\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07002346 device->id, device->identifier.name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002347 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08002348 dump += StringPrintf(INDENT2 "%d: %s\n", device->id,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07002349 device->identifier.name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002350 }
Chris Ye1b0c7342020-07-28 21:57:03 -07002351 dump += StringPrintf(INDENT3 "Classes: %s\n", device->classes.string().c_str());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002352 dump += StringPrintf(INDENT3 "Path: %s\n", device->path.c_str());
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08002353 dump += StringPrintf(INDENT3 "Enabled: %s\n", toString(device->enabled));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002354 dump += StringPrintf(INDENT3 "Descriptor: %s\n", device->identifier.descriptor.c_str());
2355 dump += StringPrintf(INDENT3 "Location: %s\n", device->identifier.location.c_str());
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08002356 dump += StringPrintf(INDENT3 "ControllerNumber: %d\n", device->controllerNumber);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002357 dump += StringPrintf(INDENT3 "UniqueId: %s\n", device->identifier.uniqueId.c_str());
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08002358 dump += StringPrintf(INDENT3 "Identifier: bus=0x%04x, vendor=0x%04x, "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07002359 "product=0x%04x, version=0x%04x\n",
2360 device->identifier.bus, device->identifier.vendor,
2361 device->identifier.product, device->identifier.version);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08002362 dump += StringPrintf(INDENT3 "KeyLayoutFile: %s\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07002363 device->keyMap.keyLayoutFile.c_str());
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08002364 dump += StringPrintf(INDENT3 "KeyCharacterMapFile: %s\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07002365 device->keyMap.keyCharacterMapFile.c_str());
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08002366 dump += StringPrintf(INDENT3 "ConfigurationFile: %s\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07002367 device->configurationFile.c_str());
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08002368 dump += INDENT3 "VideoDevice: ";
2369 if (device->videoDevice) {
2370 dump += device->videoDevice->dump() + "\n";
2371 } else {
2372 dump += "<none>\n";
2373 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002374 }
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08002375
2376 dump += INDENT "Unattached video devices:\n";
2377 for (const std::unique_ptr<TouchVideoDevice>& videoDevice : mUnattachedVideoDevices) {
2378 dump += INDENT2 + videoDevice->dump() + "\n";
2379 }
2380 if (mUnattachedVideoDevices.empty()) {
2381 dump += INDENT2 "<none>\n";
2382 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002383 } // release lock
2384}
2385
2386void EventHub::monitor() {
2387 // Acquire and release the lock to ensure that the event hub has not deadlocked.
Chris Ye1c2e0892020-11-30 21:41:44 -08002388 std::unique_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002389}
2390
Michael Wrightd02c5b62014-02-10 15:10:22 -08002391}; // namespace android