| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1 | /* | 
|  | 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 Salyzyn | 5aa26b2 | 2014-06-10 13:07:44 -0700 | [diff] [blame] | 17 | #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 Vishniakou | 7a522bf | 2019-09-24 12:46:29 +0100 | [diff] [blame] | 27 | #include <sys/capability.h> | 
| Mark Salyzyn | 5aa26b2 | 2014-06-10 13:07:44 -0700 | [diff] [blame] | 28 | #include <sys/epoll.h> | 
| Mark Salyzyn | 5aa26b2 | 2014-06-10 13:07:44 -0700 | [diff] [blame] | 29 | #include <sys/inotify.h> | 
|  | 30 | #include <sys/ioctl.h> | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 31 | #include <sys/limits.h> | 
| Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 32 | #include <sys/stat.h> | 
|  | 33 | #include <sys/sysmacros.h> | 
| Mark Salyzyn | 5aa26b2 | 2014-06-10 13:07:44 -0700 | [diff] [blame] | 34 | #include <unistd.h> | 
|  | 35 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 36 | #define LOG_TAG "EventHub" | 
|  | 37 |  | 
|  | 38 | // #define LOG_NDEBUG 0 | 
| Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 39 | #include <android-base/file.h> | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 40 | #include <android-base/stringprintf.h> | 
| Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 41 | #include <android-base/strings.h> | 
| Philip Quinn | 39b8168 | 2019-01-09 22:20:39 -0800 | [diff] [blame] | 42 | #include <cutils/properties.h> | 
| Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 43 | #include <input/KeyCharacterMap.h> | 
|  | 44 | #include <input/KeyLayoutMap.h> | 
|  | 45 | #include <input/VirtualKeyMap.h> | 
| Dan Albert | 677d87e | 2014-06-16 17:31:28 -0700 | [diff] [blame] | 46 | #include <openssl/sha.h> | 
| Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 47 | #include <statslog.h> | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 48 | #include <utils/Errors.h> | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 49 | #include <utils/Log.h> | 
|  | 50 | #include <utils/Timers.h> | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 51 |  | 
| Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 52 | #include <filesystem> | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 53 | #include <regex> | 
| Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 54 |  | 
|  | 55 | #include "EventHub.h" | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 56 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 57 | #define INDENT "  " | 
|  | 58 | #define INDENT2 "    " | 
|  | 59 | #define INDENT3 "      " | 
|  | 60 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 61 | using android::base::StringPrintf; | 
| Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 62 | using namespace android::flag_operators; | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 63 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 64 | namespace android { | 
|  | 65 |  | 
| Usama Arif | 78eb975 | 2021-06-03 16:44:09 +0100 | [diff] [blame] | 66 | static const char* DEVICE_INPUT_PATH = "/dev/input"; | 
| Siarhei Vishniakou | 951f362 | 2018-12-12 19:45:42 -0800 | [diff] [blame] | 67 | // v4l2 devices go directly into /dev | 
| Usama Arif | 78eb975 | 2021-06-03 16:44:09 +0100 | [diff] [blame] | 68 | static const char* DEVICE_PATH = "/dev"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 69 |  | 
| Chris Ye | 657c2f0 | 2021-05-25 16:24:37 -0700 | [diff] [blame] | 70 | static constexpr size_t OBFUSCATED_LENGTH = 8; | 
|  | 71 |  | 
| Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 72 | static constexpr int32_t FF_STRONG_MAGNITUDE_CHANNEL_IDX = 0; | 
|  | 73 | static constexpr int32_t FF_WEAK_MAGNITUDE_CHANNEL_IDX = 1; | 
| Chris Ye | 6393a26 | 2020-08-04 19:41:36 -0700 | [diff] [blame] | 74 |  | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 75 | // Mapping for input battery class node IDs lookup. | 
|  | 76 | // https://www.kernel.org/doc/Documentation/power/power_supply_class.txt | 
|  | 77 | static const std::unordered_map<std::string, InputBatteryClass> BATTERY_CLASSES = | 
|  | 78 | {{"capacity", InputBatteryClass::CAPACITY}, | 
|  | 79 | {"capacity_level", InputBatteryClass::CAPACITY_LEVEL}, | 
|  | 80 | {"status", InputBatteryClass::STATUS}}; | 
|  | 81 |  | 
|  | 82 | // Mapping for input battery class node names lookup. | 
|  | 83 | // https://www.kernel.org/doc/Documentation/power/power_supply_class.txt | 
|  | 84 | static const std::unordered_map<InputBatteryClass, std::string> BATTERY_NODES = | 
|  | 85 | {{InputBatteryClass::CAPACITY, "capacity"}, | 
|  | 86 | {InputBatteryClass::CAPACITY_LEVEL, "capacity_level"}, | 
|  | 87 | {InputBatteryClass::STATUS, "status"}}; | 
|  | 88 |  | 
| Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 89 | // must be kept in sync with definitions in kernel /drivers/power/supply/power_supply_sysfs.c | 
|  | 90 | static const std::unordered_map<std::string, int32_t> BATTERY_STATUS = | 
|  | 91 | {{"Unknown", BATTERY_STATUS_UNKNOWN}, | 
|  | 92 | {"Charging", BATTERY_STATUS_CHARGING}, | 
|  | 93 | {"Discharging", BATTERY_STATUS_DISCHARGING}, | 
|  | 94 | {"Not charging", BATTERY_STATUS_NOT_CHARGING}, | 
|  | 95 | {"Full", BATTERY_STATUS_FULL}}; | 
|  | 96 |  | 
|  | 97 | // Mapping taken from | 
|  | 98 | // https://gitlab.freedesktop.org/upower/upower/-/blob/master/src/linux/up-device-supply.c#L484 | 
|  | 99 | static const std::unordered_map<std::string, int32_t> BATTERY_LEVEL = {{"Critical", 5}, | 
|  | 100 | {"Low", 10}, | 
|  | 101 | {"Normal", 55}, | 
|  | 102 | {"High", 70}, | 
|  | 103 | {"Full", 100}, | 
|  | 104 | {"Unknown", 50}}; | 
|  | 105 |  | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 106 | // Mapping for input led class node names lookup. | 
|  | 107 | // https://www.kernel.org/doc/html/latest/leds/leds-class.html | 
|  | 108 | static const std::unordered_map<std::string, InputLightClass> LIGHT_CLASSES = | 
|  | 109 | {{"red", InputLightClass::RED}, | 
|  | 110 | {"green", InputLightClass::GREEN}, | 
|  | 111 | {"blue", InputLightClass::BLUE}, | 
|  | 112 | {"global", InputLightClass::GLOBAL}, | 
|  | 113 | {"brightness", InputLightClass::BRIGHTNESS}, | 
|  | 114 | {"multi_index", InputLightClass::MULTI_INDEX}, | 
|  | 115 | {"multi_intensity", InputLightClass::MULTI_INTENSITY}, | 
|  | 116 | {"max_brightness", InputLightClass::MAX_BRIGHTNESS}}; | 
|  | 117 |  | 
|  | 118 | // Mapping for input multicolor led class node names. | 
|  | 119 | // https://www.kernel.org/doc/html/latest/leds/leds-class-multicolor.html | 
|  | 120 | static const std::unordered_map<InputLightClass, std::string> LIGHT_NODES = | 
|  | 121 | {{InputLightClass::BRIGHTNESS, "brightness"}, | 
|  | 122 | {InputLightClass::MULTI_INDEX, "multi_index"}, | 
|  | 123 | {InputLightClass::MULTI_INTENSITY, "multi_intensity"}}; | 
|  | 124 |  | 
|  | 125 | // Mapping for light color name and the light color | 
|  | 126 | const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED}, | 
|  | 127 | {"green", LightColor::GREEN}, | 
|  | 128 | {"blue", LightColor::BLUE}}; | 
|  | 129 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 130 | static inline const char* toString(bool value) { | 
|  | 131 | return value ? "true" : "false"; | 
|  | 132 | } | 
|  | 133 |  | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 134 | static std::string sha1(const std::string& in) { | 
| Dan Albert | 677d87e | 2014-06-16 17:31:28 -0700 | [diff] [blame] | 135 | SHA_CTX ctx; | 
|  | 136 | SHA1_Init(&ctx); | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 137 | SHA1_Update(&ctx, reinterpret_cast<const u_char*>(in.c_str()), in.size()); | 
| Dan Albert | 677d87e | 2014-06-16 17:31:28 -0700 | [diff] [blame] | 138 | u_char digest[SHA_DIGEST_LENGTH]; | 
|  | 139 | SHA1_Final(digest, &ctx); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 140 |  | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 141 | std::string out; | 
| Dan Albert | 677d87e | 2014-06-16 17:31:28 -0700 | [diff] [blame] | 142 | for (size_t i = 0; i < SHA_DIGEST_LENGTH; i++) { | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 143 | out += StringPrintf("%02x", digest[i]); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 144 | } | 
|  | 145 | return out; | 
|  | 146 | } | 
|  | 147 |  | 
| Siarhei Vishniakou | 951f362 | 2018-12-12 19:45:42 -0800 | [diff] [blame] | 148 | /** | 
|  | 149 | * Return true if name matches "v4l-touch*" | 
|  | 150 | */ | 
| Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 151 | static bool isV4lTouchNode(std::string name) { | 
|  | 152 | return name.find("v4l-touch") != std::string::npos; | 
| Siarhei Vishniakou | 951f362 | 2018-12-12 19:45:42 -0800 | [diff] [blame] | 153 | } | 
|  | 154 |  | 
| Philip Quinn | 39b8168 | 2019-01-09 22:20:39 -0800 | [diff] [blame] | 155 | /** | 
|  | 156 | * Returns true if V4L devices should be scanned. | 
|  | 157 | * | 
|  | 158 | * The system property ro.input.video_enabled can be used to control whether | 
|  | 159 | * EventHub scans and opens V4L devices. As V4L does not support multiple | 
|  | 160 | * clients, EventHub effectively blocks access to these devices when it opens | 
| Siarhei Vishniakou | 29f8849 | 2019-04-05 14:11:43 -0700 | [diff] [blame] | 161 | * them. | 
|  | 162 | * | 
|  | 163 | * Setting this to "false" would prevent any video devices from being discovered and | 
|  | 164 | * associated with input devices. | 
|  | 165 | * | 
|  | 166 | * This property can be used as follows: | 
|  | 167 | * 1. To turn off features that are dependent on video device presence. | 
|  | 168 | * 2. During testing and development, to allow other clients to read video devices | 
|  | 169 | * directly from /dev. | 
| Philip Quinn | 39b8168 | 2019-01-09 22:20:39 -0800 | [diff] [blame] | 170 | */ | 
|  | 171 | static bool isV4lScanningEnabled() { | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 172 | return property_get_bool("ro.input.video_enabled", true /* default_value */); | 
| Philip Quinn | 39b8168 | 2019-01-09 22:20:39 -0800 | [diff] [blame] | 173 | } | 
|  | 174 |  | 
| Siarhei Vishniakou | 592bac2 | 2018-11-08 19:42:38 -0800 | [diff] [blame] | 175 | static nsecs_t processEventTimestamp(const struct input_event& event) { | 
|  | 176 | // Use the time specified in the event instead of the current time | 
|  | 177 | // so that downstream code can get more accurate estimates of | 
|  | 178 | // event dispatch latency from the time the event is enqueued onto | 
|  | 179 | // the evdev client buffer. | 
|  | 180 | // | 
|  | 181 | // The event's timestamp fortuitously uses the same monotonic clock | 
|  | 182 | // time base as the rest of Android. The kernel event device driver | 
|  | 183 | // (drivers/input/evdev.c) obtains timestamps using ktime_get_ts(). | 
|  | 184 | // The systemTime(SYSTEM_TIME_MONOTONIC) function we use everywhere | 
|  | 185 | // calls clock_gettime(CLOCK_MONOTONIC) which is implemented as a | 
|  | 186 | // system call that also queries ktime_get_ts(). | 
|  | 187 |  | 
|  | 188 | const nsecs_t inputEventTime = seconds_to_nanoseconds(event.time.tv_sec) + | 
|  | 189 | microseconds_to_nanoseconds(event.time.tv_usec); | 
|  | 190 | return inputEventTime; | 
|  | 191 | } | 
|  | 192 |  | 
| Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 193 | /** | 
|  | 194 | * Returns the sysfs root path of the input device | 
|  | 195 | * | 
|  | 196 | */ | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 197 | static std::optional<std::filesystem::path> getSysfsRootPath(const char* devicePath) { | 
| Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 198 | std::error_code errorCode; | 
|  | 199 |  | 
|  | 200 | // Stat the device path to get the major and minor number of the character file | 
|  | 201 | struct stat statbuf; | 
|  | 202 | if (stat(devicePath, &statbuf) == -1) { | 
|  | 203 | ALOGE("Could not stat device %s due to error: %s.", devicePath, std::strerror(errno)); | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 204 | return std::nullopt; | 
| Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 205 | } | 
|  | 206 |  | 
|  | 207 | unsigned int major_num = major(statbuf.st_rdev); | 
|  | 208 | unsigned int minor_num = minor(statbuf.st_rdev); | 
|  | 209 |  | 
|  | 210 | // Realpath "/sys/dev/char/{major}:{minor}" to get the sysfs path to the input event | 
|  | 211 | auto sysfsPath = std::filesystem::path("/sys/dev/char/"); | 
|  | 212 | sysfsPath /= std::to_string(major_num) + ":" + std::to_string(minor_num); | 
|  | 213 | sysfsPath = std::filesystem::canonical(sysfsPath, errorCode); | 
|  | 214 |  | 
|  | 215 | // Make sure nothing went wrong in call to canonical() | 
|  | 216 | if (errorCode) { | 
|  | 217 | ALOGW("Could not run filesystem::canonical() due to error %d : %s.", errorCode.value(), | 
|  | 218 | errorCode.message().c_str()); | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 219 | return std::nullopt; | 
| Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 220 | } | 
|  | 221 |  | 
|  | 222 | // Continue to go up a directory until we reach a directory named "input" | 
|  | 223 | while (sysfsPath != "/" && sysfsPath.filename() != "input") { | 
|  | 224 | sysfsPath = sysfsPath.parent_path(); | 
|  | 225 | } | 
|  | 226 |  | 
|  | 227 | // Then go up one more and you will be at the sysfs root of the device | 
|  | 228 | sysfsPath = sysfsPath.parent_path(); | 
|  | 229 |  | 
|  | 230 | // Make sure we didn't reach root path and that directory actually exists | 
|  | 231 | if (sysfsPath == "/" || !std::filesystem::exists(sysfsPath, errorCode)) { | 
|  | 232 | if (errorCode) { | 
|  | 233 | ALOGW("Could not run filesystem::exists() due to error %d : %s.", errorCode.value(), | 
|  | 234 | errorCode.message().c_str()); | 
|  | 235 | } | 
|  | 236 |  | 
|  | 237 | // Not found | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 238 | return std::nullopt; | 
| Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 239 | } | 
|  | 240 |  | 
|  | 241 | return sysfsPath; | 
|  | 242 | } | 
|  | 243 |  | 
|  | 244 | /** | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 245 | * Returns the list of files under a specified path. | 
| Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 246 | */ | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 247 | static std::vector<std::filesystem::path> allFilesInPath(const std::filesystem::path& path) { | 
|  | 248 | std::vector<std::filesystem::path> nodes; | 
|  | 249 | std::error_code errorCode; | 
|  | 250 | auto iter = std::filesystem::directory_iterator(path, errorCode); | 
|  | 251 | while (!errorCode && iter != std::filesystem::directory_iterator()) { | 
|  | 252 | nodes.push_back(iter->path()); | 
|  | 253 | iter++; | 
|  | 254 | } | 
|  | 255 | return nodes; | 
|  | 256 | } | 
|  | 257 |  | 
|  | 258 | /** | 
|  | 259 | * Returns the list of files under a specified directory in a sysfs path. | 
|  | 260 | * Example: | 
|  | 261 | * findSysfsNodes(sysfsRootPath, SysfsClass::LEDS) will return all led nodes under "leds" directory | 
|  | 262 | * in the sysfs path. | 
|  | 263 | */ | 
|  | 264 | static std::vector<std::filesystem::path> findSysfsNodes(const std::filesystem::path& sysfsRoot, | 
|  | 265 | SysfsClass clazz) { | 
|  | 266 | std::string nodeStr = NamedEnum::string(clazz); | 
|  | 267 | std::for_each(nodeStr.begin(), nodeStr.end(), | 
|  | 268 | [](char& c) { c = std::tolower(static_cast<unsigned char>(c)); }); | 
|  | 269 | std::vector<std::filesystem::path> nodes; | 
|  | 270 | for (auto path = sysfsRoot; path != "/" && nodes.empty(); path = path.parent_path()) { | 
|  | 271 | nodes = allFilesInPath(path / nodeStr); | 
|  | 272 | } | 
|  | 273 | return nodes; | 
|  | 274 | } | 
|  | 275 |  | 
|  | 276 | static std::optional<std::array<LightColor, COLOR_NUM>> getColorIndexArray( | 
|  | 277 | std::filesystem::path path) { | 
|  | 278 | std::string indexStr; | 
|  | 279 | if (!base::ReadFileToString(path, &indexStr)) { | 
|  | 280 | return std::nullopt; | 
|  | 281 | } | 
|  | 282 |  | 
|  | 283 | // Parse the multi color LED index file, refer to kernel docs | 
|  | 284 | // leds/leds-class-multicolor.html | 
|  | 285 | std::regex indexPattern("(red|green|blue)\\s(red|green|blue)\\s(red|green|blue)[\\n]"); | 
|  | 286 | std::smatch results; | 
|  | 287 | std::array<LightColor, COLOR_NUM> colors; | 
|  | 288 | if (!std::regex_match(indexStr, results, indexPattern)) { | 
|  | 289 | return std::nullopt; | 
|  | 290 | } | 
|  | 291 |  | 
|  | 292 | for (size_t i = 1; i < results.size(); i++) { | 
|  | 293 | const auto it = LIGHT_COLORS.find(results[i].str()); | 
|  | 294 | if (it != LIGHT_COLORS.end()) { | 
|  | 295 | // intensities.emplace(it->second, 0); | 
|  | 296 | colors[i - 1] = it->second; | 
| Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 297 | } | 
|  | 298 | } | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 299 | return colors; | 
| Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 300 | } | 
|  | 301 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 302 | // --- Global Functions --- | 
|  | 303 |  | 
| Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 304 | Flags<InputDeviceClass> getAbsAxisUsage(int32_t axis, Flags<InputDeviceClass> deviceClasses) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 305 | // Touch devices get dibs on touch-related axes. | 
| Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 306 | if (deviceClasses.test(InputDeviceClass::TOUCH)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 307 | switch (axis) { | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 308 | case ABS_X: | 
|  | 309 | case ABS_Y: | 
|  | 310 | case ABS_PRESSURE: | 
|  | 311 | case ABS_TOOL_WIDTH: | 
|  | 312 | case ABS_DISTANCE: | 
|  | 313 | case ABS_TILT_X: | 
|  | 314 | case ABS_TILT_Y: | 
|  | 315 | case ABS_MT_SLOT: | 
|  | 316 | case ABS_MT_TOUCH_MAJOR: | 
|  | 317 | case ABS_MT_TOUCH_MINOR: | 
|  | 318 | case ABS_MT_WIDTH_MAJOR: | 
|  | 319 | case ABS_MT_WIDTH_MINOR: | 
|  | 320 | case ABS_MT_ORIENTATION: | 
|  | 321 | case ABS_MT_POSITION_X: | 
|  | 322 | case ABS_MT_POSITION_Y: | 
|  | 323 | case ABS_MT_TOOL_TYPE: | 
|  | 324 | case ABS_MT_BLOB_ID: | 
|  | 325 | case ABS_MT_TRACKING_ID: | 
|  | 326 | case ABS_MT_PRESSURE: | 
|  | 327 | case ABS_MT_DISTANCE: | 
| Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 328 | return InputDeviceClass::TOUCH; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 329 | } | 
|  | 330 | } | 
|  | 331 |  | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 332 | if (deviceClasses.test(InputDeviceClass::SENSOR)) { | 
|  | 333 | switch (axis) { | 
|  | 334 | case ABS_X: | 
|  | 335 | case ABS_Y: | 
|  | 336 | case ABS_Z: | 
|  | 337 | case ABS_RX: | 
|  | 338 | case ABS_RY: | 
|  | 339 | case ABS_RZ: | 
|  | 340 | return InputDeviceClass::SENSOR; | 
|  | 341 | } | 
|  | 342 | } | 
|  | 343 |  | 
| Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 344 | // External stylus gets the pressure axis | 
| Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 345 | if (deviceClasses.test(InputDeviceClass::EXTERNAL_STYLUS)) { | 
| Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 346 | if (axis == ABS_PRESSURE) { | 
| Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 347 | return InputDeviceClass::EXTERNAL_STYLUS; | 
| Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 348 | } | 
|  | 349 | } | 
|  | 350 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 351 | // Joystick devices get the rest. | 
| Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 352 | return deviceClasses & InputDeviceClass::JOYSTICK; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 353 | } | 
|  | 354 |  | 
|  | 355 | // --- EventHub::Device --- | 
|  | 356 |  | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 357 | EventHub::Device::Device(int fd, int32_t id, const std::string& path, | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 358 | const InputDeviceIdentifier& identifier) | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 359 | : fd(fd), | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 360 | id(id), | 
|  | 361 | path(path), | 
|  | 362 | identifier(identifier), | 
|  | 363 | classes(0), | 
|  | 364 | configuration(nullptr), | 
|  | 365 | virtualKeyMap(nullptr), | 
|  | 366 | ffEffectPlaying(false), | 
|  | 367 | ffEffectId(-1), | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 368 | associatedDevice(nullptr), | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 369 | controllerNumber(0), | 
|  | 370 | enabled(true), | 
| Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 371 | isVirtual(fd < 0) {} | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 372 |  | 
|  | 373 | EventHub::Device::~Device() { | 
|  | 374 | close(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 375 | } | 
|  | 376 |  | 
|  | 377 | void EventHub::Device::close() { | 
|  | 378 | if (fd >= 0) { | 
|  | 379 | ::close(fd); | 
|  | 380 | fd = -1; | 
|  | 381 | } | 
|  | 382 | } | 
|  | 383 |  | 
| Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 384 | status_t EventHub::Device::enable() { | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 385 | fd = open(path.c_str(), O_RDWR | O_CLOEXEC | O_NONBLOCK); | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 386 | if (fd < 0) { | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 387 | ALOGE("could not open %s, %s\n", path.c_str(), strerror(errno)); | 
| Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 388 | return -errno; | 
|  | 389 | } | 
|  | 390 | enabled = true; | 
|  | 391 | return OK; | 
|  | 392 | } | 
|  | 393 |  | 
|  | 394 | status_t EventHub::Device::disable() { | 
|  | 395 | close(); | 
|  | 396 | enabled = false; | 
|  | 397 | return OK; | 
|  | 398 | } | 
|  | 399 |  | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 400 | bool EventHub::Device::hasValidFd() const { | 
| Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 401 | return !isVirtual && enabled; | 
|  | 402 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 403 |  | 
| Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 404 | const std::shared_ptr<KeyCharacterMap> EventHub::Device::getKeyCharacterMap() const { | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 405 | return keyMap.keyCharacterMap; | 
|  | 406 | } | 
|  | 407 |  | 
|  | 408 | template <std::size_t N> | 
|  | 409 | status_t EventHub::Device::readDeviceBitMask(unsigned long ioctlCode, BitArray<N>& bitArray) { | 
|  | 410 | if (!hasValidFd()) { | 
|  | 411 | return BAD_VALUE; | 
|  | 412 | } | 
|  | 413 | if ((_IOC_SIZE(ioctlCode) == 0)) { | 
|  | 414 | ioctlCode |= _IOC(0, 0, 0, bitArray.bytes()); | 
|  | 415 | } | 
|  | 416 |  | 
|  | 417 | typename BitArray<N>::Buffer buffer; | 
|  | 418 | status_t ret = ioctl(fd, ioctlCode, buffer.data()); | 
|  | 419 | bitArray.loadFromBuffer(buffer); | 
|  | 420 | return ret; | 
|  | 421 | } | 
|  | 422 |  | 
|  | 423 | void EventHub::Device::configureFd() { | 
|  | 424 | // Set fd parameters with ioctl, such as key repeat, suspend block, and clock type | 
|  | 425 | if (classes.test(InputDeviceClass::KEYBOARD)) { | 
|  | 426 | // Disable kernel key repeat since we handle it ourselves | 
|  | 427 | unsigned int repeatRate[] = {0, 0}; | 
|  | 428 | if (ioctl(fd, EVIOCSREP, repeatRate)) { | 
|  | 429 | ALOGW("Unable to disable kernel key repeat for %s: %s", path.c_str(), strerror(errno)); | 
|  | 430 | } | 
|  | 431 | } | 
|  | 432 |  | 
|  | 433 | // Tell the kernel that we want to use the monotonic clock for reporting timestamps | 
|  | 434 | // associated with input events.  This is important because the input system | 
|  | 435 | // uses the timestamps extensively and assumes they were recorded using the monotonic | 
|  | 436 | // clock. | 
|  | 437 | int clockId = CLOCK_MONOTONIC; | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 438 | if (classes.test(InputDeviceClass::SENSOR)) { | 
|  | 439 | // Each new sensor event should use the same time base as | 
|  | 440 | // SystemClock.elapsedRealtimeNanos(). | 
|  | 441 | clockId = CLOCK_BOOTTIME; | 
|  | 442 | } | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 443 | bool usingClockIoctl = !ioctl(fd, EVIOCSCLOCKID, &clockId); | 
|  | 444 | ALOGI("usingClockIoctl=%s", toString(usingClockIoctl)); | 
|  | 445 | } | 
|  | 446 |  | 
|  | 447 | bool EventHub::Device::hasKeycodeLocked(int keycode) const { | 
|  | 448 | if (!keyMap.haveKeyLayout()) { | 
|  | 449 | return false; | 
|  | 450 | } | 
|  | 451 |  | 
|  | 452 | std::vector<int32_t> scanCodes; | 
|  | 453 | keyMap.keyLayoutMap->findScanCodesForKey(keycode, &scanCodes); | 
|  | 454 | const size_t N = scanCodes.size(); | 
|  | 455 | for (size_t i = 0; i < N && i <= KEY_MAX; i++) { | 
|  | 456 | int32_t sc = scanCodes[i]; | 
|  | 457 | if (sc >= 0 && sc <= KEY_MAX && keyBitmask.test(sc)) { | 
|  | 458 | return true; | 
|  | 459 | } | 
|  | 460 | } | 
|  | 461 |  | 
|  | 462 | return false; | 
|  | 463 | } | 
|  | 464 |  | 
|  | 465 | void EventHub::Device::loadConfigurationLocked() { | 
|  | 466 | configurationFile = | 
|  | 467 | getInputDeviceConfigurationFilePathByDeviceIdentifier(identifier, | 
|  | 468 | InputDeviceConfigurationFileType:: | 
|  | 469 | CONFIGURATION); | 
|  | 470 | if (configurationFile.empty()) { | 
|  | 471 | ALOGD("No input device configuration file found for device '%s'.", identifier.name.c_str()); | 
|  | 472 | } else { | 
| Siarhei Vishniakou | 4d9f977 | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 473 | android::base::Result<std::unique_ptr<PropertyMap>> propertyMap = | 
|  | 474 | PropertyMap::load(configurationFile.c_str()); | 
|  | 475 | if (!propertyMap.ok()) { | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 476 | ALOGE("Error loading input device configuration file for device '%s'.  " | 
|  | 477 | "Using default configuration.", | 
|  | 478 | identifier.name.c_str()); | 
| Siarhei Vishniakou | d549b25 | 2020-08-11 11:25:26 -0500 | [diff] [blame] | 479 | } else { | 
| Siarhei Vishniakou | 4d9f977 | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 480 | configuration = std::move(*propertyMap); | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 481 | } | 
|  | 482 | } | 
|  | 483 | } | 
|  | 484 |  | 
|  | 485 | bool EventHub::Device::loadVirtualKeyMapLocked() { | 
|  | 486 | // The virtual key map is supplied by the kernel as a system board property file. | 
|  | 487 | std::string propPath = "/sys/board_properties/virtualkeys."; | 
|  | 488 | propPath += identifier.getCanonicalName(); | 
|  | 489 | if (access(propPath.c_str(), R_OK)) { | 
|  | 490 | return false; | 
|  | 491 | } | 
|  | 492 | virtualKeyMap = VirtualKeyMap::load(propPath); | 
|  | 493 | return virtualKeyMap != nullptr; | 
|  | 494 | } | 
|  | 495 |  | 
|  | 496 | status_t EventHub::Device::loadKeyMapLocked() { | 
| Siarhei Vishniakou | d549b25 | 2020-08-11 11:25:26 -0500 | [diff] [blame] | 497 | return keyMap.load(identifier, configuration.get()); | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 498 | } | 
|  | 499 |  | 
|  | 500 | bool EventHub::Device::isExternalDeviceLocked() { | 
|  | 501 | if (configuration) { | 
|  | 502 | bool value; | 
|  | 503 | if (configuration->tryGetProperty(String8("device.internal"), value)) { | 
|  | 504 | return !value; | 
|  | 505 | } | 
|  | 506 | } | 
|  | 507 | return identifier.bus == BUS_USB || identifier.bus == BUS_BLUETOOTH; | 
|  | 508 | } | 
|  | 509 |  | 
|  | 510 | bool EventHub::Device::deviceHasMicLocked() { | 
|  | 511 | if (configuration) { | 
|  | 512 | bool value; | 
|  | 513 | if (configuration->tryGetProperty(String8("audio.mic"), value)) { | 
|  | 514 | return value; | 
|  | 515 | } | 
|  | 516 | } | 
|  | 517 | return false; | 
|  | 518 | } | 
|  | 519 |  | 
|  | 520 | void EventHub::Device::setLedStateLocked(int32_t led, bool on) { | 
|  | 521 | int32_t sc; | 
|  | 522 | if (hasValidFd() && mapLed(led, &sc) != NAME_NOT_FOUND) { | 
|  | 523 | struct input_event ev; | 
|  | 524 | ev.time.tv_sec = 0; | 
|  | 525 | ev.time.tv_usec = 0; | 
|  | 526 | ev.type = EV_LED; | 
|  | 527 | ev.code = sc; | 
|  | 528 | ev.value = on ? 1 : 0; | 
|  | 529 |  | 
|  | 530 | ssize_t nWrite; | 
|  | 531 | do { | 
|  | 532 | nWrite = write(fd, &ev, sizeof(struct input_event)); | 
|  | 533 | } while (nWrite == -1 && errno == EINTR); | 
|  | 534 | } | 
|  | 535 | } | 
|  | 536 |  | 
|  | 537 | void EventHub::Device::setLedForControllerLocked() { | 
|  | 538 | for (int i = 0; i < MAX_CONTROLLER_LEDS; i++) { | 
|  | 539 | setLedStateLocked(ALED_CONTROLLER_1 + i, controllerNumber == i + 1); | 
|  | 540 | } | 
|  | 541 | } | 
|  | 542 |  | 
|  | 543 | status_t EventHub::Device::mapLed(int32_t led, int32_t* outScanCode) const { | 
|  | 544 | if (!keyMap.haveKeyLayout()) { | 
|  | 545 | return NAME_NOT_FOUND; | 
|  | 546 | } | 
|  | 547 |  | 
|  | 548 | int32_t scanCode; | 
|  | 549 | if (keyMap.keyLayoutMap->findScanCodeForLed(led, &scanCode) != NAME_NOT_FOUND) { | 
|  | 550 | if (scanCode >= 0 && scanCode <= LED_MAX && ledBitmask.test(scanCode)) { | 
|  | 551 | *outScanCode = scanCode; | 
|  | 552 | return NO_ERROR; | 
|  | 553 | } | 
|  | 554 | } | 
|  | 555 | return NAME_NOT_FOUND; | 
|  | 556 | } | 
|  | 557 |  | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 558 | // Check the sysfs path for any input device batteries, returns true if battery found. | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 559 | bool EventHub::AssociatedDevice::configureBatteryLocked() { | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 560 | nextBatteryId = 0; | 
|  | 561 | // Check if device has any battery. | 
|  | 562 | const auto& paths = findSysfsNodes(sysfsRootPath, SysfsClass::POWER_SUPPLY); | 
|  | 563 | for (const auto& nodePath : paths) { | 
|  | 564 | RawBatteryInfo info; | 
|  | 565 | info.id = ++nextBatteryId; | 
|  | 566 | info.path = nodePath; | 
|  | 567 | info.name = nodePath.filename(); | 
|  | 568 |  | 
|  | 569 | // Scan the path for all the files | 
|  | 570 | // Refer to https://www.kernel.org/doc/Documentation/leds/leds-class.txt | 
|  | 571 | const auto& files = allFilesInPath(nodePath); | 
|  | 572 | for (const auto& file : files) { | 
|  | 573 | const auto it = BATTERY_CLASSES.find(file.filename().string()); | 
|  | 574 | if (it != BATTERY_CLASSES.end()) { | 
|  | 575 | info.flags |= it->second; | 
|  | 576 | } | 
|  | 577 | } | 
|  | 578 | batteryInfos.insert_or_assign(info.id, info); | 
|  | 579 | ALOGD("configureBatteryLocked rawBatteryId %d name %s", info.id, info.name.c_str()); | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 580 | } | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 581 | return !batteryInfos.empty(); | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 582 | } | 
|  | 583 |  | 
|  | 584 | // Check the sysfs path for any input device lights, returns true if lights found. | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 585 | bool EventHub::AssociatedDevice::configureLightsLocked() { | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 586 | nextLightId = 0; | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 587 | // Check if device has any lights. | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 588 | const auto& paths = findSysfsNodes(sysfsRootPath, SysfsClass::LEDS); | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 589 | for (const auto& nodePath : paths) { | 
|  | 590 | RawLightInfo info; | 
|  | 591 | info.id = ++nextLightId; | 
|  | 592 | info.path = nodePath; | 
|  | 593 | info.name = nodePath.filename(); | 
|  | 594 | info.maxBrightness = std::nullopt; | 
|  | 595 | size_t nameStart = info.name.rfind(":"); | 
|  | 596 | if (nameStart != std::string::npos) { | 
|  | 597 | // Trim the name to color name | 
|  | 598 | info.name = info.name.substr(nameStart + 1); | 
|  | 599 | // Set InputLightClass flag for colors | 
|  | 600 | const auto it = LIGHT_CLASSES.find(info.name); | 
|  | 601 | if (it != LIGHT_CLASSES.end()) { | 
|  | 602 | info.flags |= it->second; | 
|  | 603 | } | 
|  | 604 | } | 
|  | 605 | // Scan the path for all the files | 
|  | 606 | // Refer to https://www.kernel.org/doc/Documentation/leds/leds-class.txt | 
|  | 607 | const auto& files = allFilesInPath(nodePath); | 
|  | 608 | for (const auto& file : files) { | 
|  | 609 | const auto it = LIGHT_CLASSES.find(file.filename().string()); | 
|  | 610 | if (it != LIGHT_CLASSES.end()) { | 
|  | 611 | info.flags |= it->second; | 
|  | 612 | // If the node has maximum brightness, read it | 
|  | 613 | if (it->second == InputLightClass::MAX_BRIGHTNESS) { | 
|  | 614 | std::string str; | 
|  | 615 | if (base::ReadFileToString(file, &str)) { | 
|  | 616 | info.maxBrightness = std::stoi(str); | 
|  | 617 | } | 
|  | 618 | } | 
|  | 619 | } | 
|  | 620 | } | 
|  | 621 | lightInfos.insert_or_assign(info.id, info); | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 622 | ALOGD("configureLightsLocked rawLightId %d name %s", info.id, info.name.c_str()); | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 623 | } | 
|  | 624 | return !lightInfos.empty(); | 
|  | 625 | } | 
|  | 626 |  | 
| Siarhei Vishniakou | 7a522bf | 2019-09-24 12:46:29 +0100 | [diff] [blame] | 627 | /** | 
|  | 628 | * Get the capabilities for the current process. | 
|  | 629 | * Crashes the system if unable to create / check / destroy the capabilities object. | 
|  | 630 | */ | 
|  | 631 | class Capabilities final { | 
|  | 632 | public: | 
|  | 633 | explicit Capabilities() { | 
|  | 634 | mCaps = cap_get_proc(); | 
|  | 635 | LOG_ALWAYS_FATAL_IF(mCaps == nullptr, "Could not get capabilities of the current process"); | 
|  | 636 | } | 
|  | 637 |  | 
|  | 638 | /** | 
|  | 639 | * Check whether the current process has a specific capability | 
|  | 640 | * in the set of effective capabilities. | 
|  | 641 | * Return CAP_SET if the process has the requested capability | 
|  | 642 | * Return CAP_CLEAR otherwise. | 
|  | 643 | */ | 
|  | 644 | cap_flag_value_t checkEffectiveCapability(cap_value_t capability) { | 
|  | 645 | cap_flag_value_t value; | 
|  | 646 | const int result = cap_get_flag(mCaps, capability, CAP_EFFECTIVE, &value); | 
|  | 647 | LOG_ALWAYS_FATAL_IF(result == -1, "Could not obtain the requested capability"); | 
|  | 648 | return value; | 
|  | 649 | } | 
|  | 650 |  | 
|  | 651 | ~Capabilities() { | 
|  | 652 | const int result = cap_free(mCaps); | 
|  | 653 | LOG_ALWAYS_FATAL_IF(result == -1, "Could not release the capabilities structure"); | 
|  | 654 | } | 
|  | 655 |  | 
|  | 656 | private: | 
|  | 657 | cap_t mCaps; | 
|  | 658 | }; | 
|  | 659 |  | 
|  | 660 | static void ensureProcessCanBlockSuspend() { | 
|  | 661 | Capabilities capabilities; | 
|  | 662 | const bool canBlockSuspend = | 
|  | 663 | capabilities.checkEffectiveCapability(CAP_BLOCK_SUSPEND) == CAP_SET; | 
|  | 664 | LOG_ALWAYS_FATAL_IF(!canBlockSuspend, | 
|  | 665 | "Input must be able to block suspend to properly process events"); | 
|  | 666 | } | 
|  | 667 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 668 | // --- EventHub --- | 
|  | 669 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 670 | const int EventHub::EPOLL_MAX_EVENTS; | 
|  | 671 |  | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 672 | EventHub::EventHub(void) | 
|  | 673 | : mBuiltInKeyboardId(NO_BUILT_IN_KEYBOARD), | 
|  | 674 | mNextDeviceId(1), | 
|  | 675 | mControllerNumbers(), | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 676 | mNeedToSendFinishedDeviceScan(false), | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 677 | mNeedToReopenDevices(false), | 
|  | 678 | mNeedToScanDevices(true), | 
|  | 679 | mPendingEventCount(0), | 
|  | 680 | mPendingEventIndex(0), | 
|  | 681 | mPendingINotify(false) { | 
| Siarhei Vishniakou | 7a522bf | 2019-09-24 12:46:29 +0100 | [diff] [blame] | 682 | ensureProcessCanBlockSuspend(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 683 |  | 
| Nick Kralevich | fcf1b2b | 2018-12-15 11:59:30 -0800 | [diff] [blame] | 684 | mEpollFd = epoll_create1(EPOLL_CLOEXEC); | 
| Siarhei Vishniakou | 951f362 | 2018-12-12 19:45:42 -0800 | [diff] [blame] | 685 | LOG_ALWAYS_FATAL_IF(mEpollFd < 0, "Could not create epoll instance: %s", strerror(errno)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 686 |  | 
|  | 687 | mINotifyFd = inotify_init(); | 
| Usama Arif | 78eb975 | 2021-06-03 16:44:09 +0100 | [diff] [blame] | 688 |  | 
|  | 689 | std::error_code errorCode; | 
|  | 690 | bool isDeviceInotifyAdded = false; | 
|  | 691 | if (std::filesystem::exists(DEVICE_INPUT_PATH, errorCode)) { | 
|  | 692 | addDeviceInputInotify(); | 
| Philip Quinn | 39b8168 | 2019-01-09 22:20:39 -0800 | [diff] [blame] | 693 | } else { | 
| Usama Arif | 78eb975 | 2021-06-03 16:44:09 +0100 | [diff] [blame] | 694 | addDeviceInotify(); | 
|  | 695 | isDeviceInotifyAdded = true; | 
|  | 696 | if (errorCode) { | 
|  | 697 | ALOGW("Could not run filesystem::exists() due to error %d : %s.", errorCode.value(), | 
|  | 698 | errorCode.message().c_str()); | 
|  | 699 | } | 
|  | 700 | } | 
|  | 701 |  | 
|  | 702 | if (isV4lScanningEnabled() && !isDeviceInotifyAdded) { | 
|  | 703 | addDeviceInotify(); | 
|  | 704 | } else { | 
| Philip Quinn | 39b8168 | 2019-01-09 22:20:39 -0800 | [diff] [blame] | 705 | ALOGI("Video device scanning disabled"); | 
|  | 706 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 707 |  | 
| Siarhei Vishniakou | 2d0e948 | 2019-09-24 12:52:47 +0100 | [diff] [blame] | 708 | struct epoll_event eventItem = {}; | 
|  | 709 | eventItem.events = EPOLLIN | EPOLLWAKEUP; | 
| Siarhei Vishniakou | 4bc561c | 2018-11-02 17:41:58 -0700 | [diff] [blame] | 710 | eventItem.data.fd = mINotifyFd; | 
| Siarhei Vishniakou | 951f362 | 2018-12-12 19:45:42 -0800 | [diff] [blame] | 711 | int result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mINotifyFd, &eventItem); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 712 | LOG_ALWAYS_FATAL_IF(result != 0, "Could not add INotify to epoll instance.  errno=%d", errno); | 
|  | 713 |  | 
|  | 714 | int wakeFds[2]; | 
|  | 715 | result = pipe(wakeFds); | 
|  | 716 | LOG_ALWAYS_FATAL_IF(result != 0, "Could not create wake pipe.  errno=%d", errno); | 
|  | 717 |  | 
|  | 718 | mWakeReadPipeFd = wakeFds[0]; | 
|  | 719 | mWakeWritePipeFd = wakeFds[1]; | 
|  | 720 |  | 
|  | 721 | result = fcntl(mWakeReadPipeFd, F_SETFL, O_NONBLOCK); | 
|  | 722 | LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake read pipe non-blocking.  errno=%d", | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 723 | errno); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 724 |  | 
|  | 725 | result = fcntl(mWakeWritePipeFd, F_SETFL, O_NONBLOCK); | 
|  | 726 | LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake write pipe non-blocking.  errno=%d", | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 727 | errno); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 728 |  | 
| Siarhei Vishniakou | 4bc561c | 2018-11-02 17:41:58 -0700 | [diff] [blame] | 729 | eventItem.data.fd = mWakeReadPipeFd; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 730 | result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeReadPipeFd, &eventItem); | 
|  | 731 | LOG_ALWAYS_FATAL_IF(result != 0, "Could not add wake read pipe to epoll instance.  errno=%d", | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 732 | errno); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 733 | } | 
|  | 734 |  | 
|  | 735 | EventHub::~EventHub(void) { | 
|  | 736 | closeAllDevicesLocked(); | 
|  | 737 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 738 | ::close(mEpollFd); | 
|  | 739 | ::close(mINotifyFd); | 
|  | 740 | ::close(mWakeReadPipeFd); | 
|  | 741 | ::close(mWakeWritePipeFd); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 742 | } | 
|  | 743 |  | 
| Usama Arif | 78eb975 | 2021-06-03 16:44:09 +0100 | [diff] [blame] | 744 | /** | 
|  | 745 | * On devices that don't have any input devices (like some development boards), the /dev/input | 
|  | 746 | * directory will be absent. However, the user may still plug in an input device at a later time. | 
|  | 747 | * Add watch for contents of /dev/input only when /dev/input appears. | 
|  | 748 | */ | 
|  | 749 | void EventHub::addDeviceInputInotify() { | 
|  | 750 | mDeviceInputWd = inotify_add_watch(mINotifyFd, DEVICE_INPUT_PATH, IN_DELETE | IN_CREATE); | 
|  | 751 | LOG_ALWAYS_FATAL_IF(mDeviceInputWd < 0, "Could not register INotify for %s: %s", | 
|  | 752 | DEVICE_INPUT_PATH, strerror(errno)); | 
|  | 753 | } | 
|  | 754 |  | 
|  | 755 | void EventHub::addDeviceInotify() { | 
|  | 756 | mDeviceWd = inotify_add_watch(mINotifyFd, DEVICE_PATH, IN_DELETE | IN_CREATE); | 
|  | 757 | LOG_ALWAYS_FATAL_IF(mDeviceWd < 0, "Could not register INotify for %s: %s", DEVICE_PATH, | 
|  | 758 | strerror(errno)); | 
|  | 759 | } | 
|  | 760 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 761 | InputDeviceIdentifier EventHub::getDeviceIdentifier(int32_t deviceId) const { | 
| Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 762 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 763 | Device* device = getDeviceLocked(deviceId); | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 764 | return device != nullptr ? device->identifier : InputDeviceIdentifier(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 765 | } | 
|  | 766 |  | 
| Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 767 | Flags<InputDeviceClass> EventHub::getDeviceClasses(int32_t deviceId) const { | 
| Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 768 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 769 | Device* device = getDeviceLocked(deviceId); | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 770 | return device != nullptr ? device->classes : Flags<InputDeviceClass>(0); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 771 | } | 
|  | 772 |  | 
|  | 773 | int32_t EventHub::getDeviceControllerNumber(int32_t deviceId) const { | 
| Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 774 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 775 | Device* device = getDeviceLocked(deviceId); | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 776 | return device != nullptr ? device->controllerNumber : 0; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 777 | } | 
|  | 778 |  | 
|  | 779 | void EventHub::getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const { | 
| Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 780 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 781 | Device* device = getDeviceLocked(deviceId); | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 782 | if (device != nullptr && device->configuration) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 783 | *outConfiguration = *device->configuration; | 
|  | 784 | } else { | 
|  | 785 | outConfiguration->clear(); | 
|  | 786 | } | 
|  | 787 | } | 
|  | 788 |  | 
|  | 789 | status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis, | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 790 | RawAbsoluteAxisInfo* outAxisInfo) const { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 791 | outAxisInfo->clear(); | 
|  | 792 |  | 
|  | 793 | if (axis >= 0 && axis <= ABS_MAX) { | 
| Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 794 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 795 |  | 
|  | 796 | Device* device = getDeviceLocked(deviceId); | 
| Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 797 | if (device != nullptr && device->hasValidFd() && device->absBitmask.test(axis)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 798 | struct input_absinfo info; | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 799 | if (ioctl(device->fd, EVIOCGABS(axis), &info)) { | 
|  | 800 | ALOGW("Error reading absolute controller %d for device %s fd %d, errno=%d", axis, | 
|  | 801 | device->identifier.name.c_str(), device->fd, errno); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 802 | return -errno; | 
|  | 803 | } | 
|  | 804 |  | 
|  | 805 | if (info.minimum != info.maximum) { | 
|  | 806 | outAxisInfo->valid = true; | 
|  | 807 | outAxisInfo->minValue = info.minimum; | 
|  | 808 | outAxisInfo->maxValue = info.maximum; | 
|  | 809 | outAxisInfo->flat = info.flat; | 
|  | 810 | outAxisInfo->fuzz = info.fuzz; | 
|  | 811 | outAxisInfo->resolution = info.resolution; | 
|  | 812 | } | 
|  | 813 | return OK; | 
|  | 814 | } | 
|  | 815 | } | 
|  | 816 | return -1; | 
|  | 817 | } | 
|  | 818 |  | 
|  | 819 | bool EventHub::hasRelativeAxis(int32_t deviceId, int axis) const { | 
|  | 820 | if (axis >= 0 && axis <= REL_MAX) { | 
| Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 821 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 822 | Device* device = getDeviceLocked(deviceId); | 
| Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 823 | return device != nullptr ? device->relBitmask.test(axis) : false; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 824 | } | 
|  | 825 | return false; | 
|  | 826 | } | 
|  | 827 |  | 
|  | 828 | bool EventHub::hasInputProperty(int32_t deviceId, int property) const { | 
| Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 829 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 830 |  | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 831 | Device* device = getDeviceLocked(deviceId); | 
|  | 832 | return property >= 0 && property <= INPUT_PROP_MAX && device != nullptr | 
|  | 833 | ? device->propBitmask.test(property) | 
|  | 834 | : false; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 835 | } | 
|  | 836 |  | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 837 | bool EventHub::hasMscEvent(int32_t deviceId, int mscEvent) const { | 
|  | 838 | std::scoped_lock _l(mLock); | 
|  | 839 |  | 
|  | 840 | Device* device = getDeviceLocked(deviceId); | 
|  | 841 | return mscEvent >= 0 && mscEvent <= MSC_MAX && device != nullptr | 
|  | 842 | ? device->mscBitmask.test(mscEvent) | 
|  | 843 | : false; | 
|  | 844 | } | 
|  | 845 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 846 | int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const { | 
|  | 847 | if (scanCode >= 0 && scanCode <= KEY_MAX) { | 
| Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 848 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 849 |  | 
|  | 850 | Device* device = getDeviceLocked(deviceId); | 
| Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 851 | if (device != nullptr && device->hasValidFd() && device->keyBitmask.test(scanCode)) { | 
|  | 852 | if (device->readDeviceBitMask(EVIOCGKEY(0), device->keyState) >= 0) { | 
|  | 853 | return device->keyState.test(scanCode) ? AKEY_STATE_DOWN : AKEY_STATE_UP; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 854 | } | 
|  | 855 | } | 
|  | 856 | } | 
|  | 857 | return AKEY_STATE_UNKNOWN; | 
|  | 858 | } | 
|  | 859 |  | 
|  | 860 | int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const { | 
| Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 861 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 862 |  | 
|  | 863 | Device* device = getDeviceLocked(deviceId); | 
| Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 864 | if (device != nullptr && device->hasValidFd() && device->keyMap.haveKeyLayout()) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 865 | std::vector<int32_t> scanCodes; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 866 | device->keyMap.keyLayoutMap->findScanCodesForKey(keyCode, &scanCodes); | 
|  | 867 | if (scanCodes.size() != 0) { | 
| Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 868 | if (device->readDeviceBitMask(EVIOCGKEY(0), device->keyState) >= 0) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 869 | for (size_t i = 0; i < scanCodes.size(); i++) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 870 | int32_t sc = scanCodes[i]; | 
| Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 871 | if (sc >= 0 && sc <= KEY_MAX && device->keyState.test(sc)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 872 | return AKEY_STATE_DOWN; | 
|  | 873 | } | 
|  | 874 | } | 
|  | 875 | return AKEY_STATE_UP; | 
|  | 876 | } | 
|  | 877 | } | 
|  | 878 | } | 
|  | 879 | return AKEY_STATE_UNKNOWN; | 
|  | 880 | } | 
|  | 881 |  | 
|  | 882 | int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const { | 
|  | 883 | if (sw >= 0 && sw <= SW_MAX) { | 
| Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 884 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 885 |  | 
|  | 886 | Device* device = getDeviceLocked(deviceId); | 
| Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 887 | if (device != nullptr && device->hasValidFd() && device->swBitmask.test(sw)) { | 
|  | 888 | if (device->readDeviceBitMask(EVIOCGSW(0), device->swState) >= 0) { | 
|  | 889 | return device->swState.test(sw) ? AKEY_STATE_DOWN : AKEY_STATE_UP; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 890 | } | 
|  | 891 | } | 
|  | 892 | } | 
|  | 893 | return AKEY_STATE_UNKNOWN; | 
|  | 894 | } | 
|  | 895 |  | 
|  | 896 | status_t EventHub::getAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t* outValue) const { | 
|  | 897 | *outValue = 0; | 
|  | 898 |  | 
|  | 899 | if (axis >= 0 && axis <= ABS_MAX) { | 
| Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 900 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 901 |  | 
|  | 902 | Device* device = getDeviceLocked(deviceId); | 
| Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 903 | if (device != nullptr && device->hasValidFd() && device->absBitmask.test(axis)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 904 | struct input_absinfo info; | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 905 | if (ioctl(device->fd, EVIOCGABS(axis), &info)) { | 
|  | 906 | ALOGW("Error reading absolute controller %d for device %s fd %d, errno=%d", axis, | 
|  | 907 | device->identifier.name.c_str(), device->fd, errno); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 908 | return -errno; | 
|  | 909 | } | 
|  | 910 |  | 
|  | 911 | *outValue = info.value; | 
|  | 912 | return OK; | 
|  | 913 | } | 
|  | 914 | } | 
|  | 915 | return -1; | 
|  | 916 | } | 
|  | 917 |  | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 918 | bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes, | 
|  | 919 | uint8_t* outFlags) const { | 
| Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 920 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 921 |  | 
|  | 922 | Device* device = getDeviceLocked(deviceId); | 
| Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 923 | if (device != nullptr && device->keyMap.haveKeyLayout()) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 924 | std::vector<int32_t> scanCodes; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 925 | for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) { | 
|  | 926 | scanCodes.clear(); | 
|  | 927 |  | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 928 | status_t err = device->keyMap.keyLayoutMap->findScanCodesForKey(keyCodes[codeIndex], | 
|  | 929 | &scanCodes); | 
|  | 930 | if (!err) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 931 | // check the possible scan codes identified by the layout map against the | 
|  | 932 | // map of codes actually emitted by the driver | 
|  | 933 | for (size_t sc = 0; sc < scanCodes.size(); sc++) { | 
| Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 934 | if (device->keyBitmask.test(scanCodes[sc])) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 935 | outFlags[codeIndex] = 1; | 
|  | 936 | break; | 
|  | 937 | } | 
|  | 938 | } | 
|  | 939 | } | 
|  | 940 | } | 
|  | 941 | return true; | 
|  | 942 | } | 
|  | 943 | return false; | 
|  | 944 | } | 
|  | 945 |  | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 946 | status_t EventHub::mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState, | 
|  | 947 | int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const { | 
| Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 948 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 949 | Device* device = getDeviceLocked(deviceId); | 
| Dmitry Torokhov | 0faaa0b | 2015-09-24 13:13:55 -0700 | [diff] [blame] | 950 | status_t status = NAME_NOT_FOUND; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 951 |  | 
| Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 952 | if (device != nullptr) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 953 | // Check the key character map first. | 
| Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 954 | const std::shared_ptr<KeyCharacterMap> kcm = device->getKeyCharacterMap(); | 
|  | 955 | if (kcm) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 956 | if (!kcm->mapKey(scanCode, usageCode, outKeycode)) { | 
|  | 957 | *outFlags = 0; | 
| Dmitry Torokhov | 0faaa0b | 2015-09-24 13:13:55 -0700 | [diff] [blame] | 958 | status = NO_ERROR; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 959 | } | 
|  | 960 | } | 
|  | 961 |  | 
|  | 962 | // Check the key layout next. | 
| Dmitry Torokhov | 0faaa0b | 2015-09-24 13:13:55 -0700 | [diff] [blame] | 963 | if (status != NO_ERROR && device->keyMap.haveKeyLayout()) { | 
| Siarhei Vishniakou | 592bac2 | 2018-11-08 19:42:38 -0800 | [diff] [blame] | 964 | if (!device->keyMap.keyLayoutMap->mapKey(scanCode, usageCode, outKeycode, outFlags)) { | 
| Dmitry Torokhov | 0faaa0b | 2015-09-24 13:13:55 -0700 | [diff] [blame] | 965 | status = NO_ERROR; | 
|  | 966 | } | 
|  | 967 | } | 
|  | 968 |  | 
|  | 969 | if (status == NO_ERROR) { | 
| Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 970 | if (kcm) { | 
| Dmitry Torokhov | 0faaa0b | 2015-09-24 13:13:55 -0700 | [diff] [blame] | 971 | kcm->tryRemapKey(*outKeycode, metaState, outKeycode, outMetaState); | 
|  | 972 | } else { | 
|  | 973 | *outMetaState = metaState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 974 | } | 
|  | 975 | } | 
|  | 976 | } | 
|  | 977 |  | 
| Dmitry Torokhov | 0faaa0b | 2015-09-24 13:13:55 -0700 | [diff] [blame] | 978 | if (status != NO_ERROR) { | 
|  | 979 | *outKeycode = 0; | 
|  | 980 | *outFlags = 0; | 
|  | 981 | *outMetaState = metaState; | 
|  | 982 | } | 
|  | 983 |  | 
|  | 984 | return status; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 985 | } | 
|  | 986 |  | 
|  | 987 | status_t EventHub::mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const { | 
| Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 988 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 989 | Device* device = getDeviceLocked(deviceId); | 
|  | 990 |  | 
| Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 991 | if (device != nullptr && device->keyMap.haveKeyLayout()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 992 | status_t err = device->keyMap.keyLayoutMap->mapAxis(scanCode, outAxisInfo); | 
|  | 993 | if (err == NO_ERROR) { | 
|  | 994 | return NO_ERROR; | 
|  | 995 | } | 
|  | 996 | } | 
|  | 997 |  | 
|  | 998 | return NAME_NOT_FOUND; | 
|  | 999 | } | 
|  | 1000 |  | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1001 | base::Result<std::pair<InputDeviceSensorType, int32_t>> EventHub::mapSensor(int32_t deviceId, | 
|  | 1002 | int32_t absCode) { | 
|  | 1003 | std::scoped_lock _l(mLock); | 
|  | 1004 | Device* device = getDeviceLocked(deviceId); | 
|  | 1005 |  | 
|  | 1006 | if (device != nullptr && device->keyMap.haveKeyLayout()) { | 
|  | 1007 | return device->keyMap.keyLayoutMap->mapSensor(absCode); | 
|  | 1008 | } | 
|  | 1009 | return Errorf("Device not found or device has no key layout."); | 
|  | 1010 | } | 
|  | 1011 |  | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 1012 | // Gets the battery info map from battery ID to RawBatteryInfo of the miscellaneous device | 
|  | 1013 | // associated with the device ID. Returns an empty map if no miscellaneous device found. | 
|  | 1014 | const std::unordered_map<int32_t, RawBatteryInfo>& EventHub::getBatteryInfoLocked( | 
|  | 1015 | int32_t deviceId) const { | 
|  | 1016 | static const std::unordered_map<int32_t, RawBatteryInfo> EMPTY_BATTERY_INFO = {}; | 
|  | 1017 | Device* device = getDeviceLocked(deviceId); | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 1018 | if (device == nullptr || !device->associatedDevice) { | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 1019 | return EMPTY_BATTERY_INFO; | 
|  | 1020 | } | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 1021 | return device->associatedDevice->batteryInfos; | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 1022 | } | 
|  | 1023 |  | 
|  | 1024 | const std::vector<int32_t> EventHub::getRawBatteryIds(int32_t deviceId) { | 
|  | 1025 | std::scoped_lock _l(mLock); | 
|  | 1026 | std::vector<int32_t> batteryIds; | 
|  | 1027 |  | 
|  | 1028 | for (const auto [id, info] : getBatteryInfoLocked(deviceId)) { | 
|  | 1029 | batteryIds.push_back(id); | 
|  | 1030 | } | 
|  | 1031 |  | 
|  | 1032 | return batteryIds; | 
|  | 1033 | } | 
|  | 1034 |  | 
|  | 1035 | std::optional<RawBatteryInfo> EventHub::getRawBatteryInfo(int32_t deviceId, int32_t batteryId) { | 
|  | 1036 | std::scoped_lock _l(mLock); | 
|  | 1037 |  | 
|  | 1038 | const auto infos = getBatteryInfoLocked(deviceId); | 
|  | 1039 |  | 
|  | 1040 | auto it = infos.find(batteryId); | 
|  | 1041 | if (it != infos.end()) { | 
|  | 1042 | return it->second; | 
|  | 1043 | } | 
|  | 1044 |  | 
|  | 1045 | return std::nullopt; | 
|  | 1046 | } | 
|  | 1047 |  | 
|  | 1048 | // Gets the light info map from light ID to RawLightInfo of the miscellaneous device associated | 
|  | 1049 | // with the deivice ID. Returns an empty map if no miscellaneous device found. | 
|  | 1050 | const std::unordered_map<int32_t, RawLightInfo>& EventHub::getLightInfoLocked( | 
|  | 1051 | int32_t deviceId) const { | 
|  | 1052 | static const std::unordered_map<int32_t, RawLightInfo> EMPTY_LIGHT_INFO = {}; | 
|  | 1053 | Device* device = getDeviceLocked(deviceId); | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 1054 | if (device == nullptr || !device->associatedDevice) { | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 1055 | return EMPTY_LIGHT_INFO; | 
|  | 1056 | } | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 1057 | return device->associatedDevice->lightInfos; | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 1058 | } | 
|  | 1059 |  | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 1060 | const std::vector<int32_t> EventHub::getRawLightIds(int32_t deviceId) { | 
|  | 1061 | std::scoped_lock _l(mLock); | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 1062 | std::vector<int32_t> lightIds; | 
|  | 1063 |  | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 1064 | for (const auto [id, info] : getLightInfoLocked(deviceId)) { | 
|  | 1065 | lightIds.push_back(id); | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 1066 | } | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 1067 |  | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 1068 | return lightIds; | 
|  | 1069 | } | 
|  | 1070 |  | 
|  | 1071 | std::optional<RawLightInfo> EventHub::getRawLightInfo(int32_t deviceId, int32_t lightId) { | 
|  | 1072 | std::scoped_lock _l(mLock); | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 1073 |  | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 1074 | const auto infos = getLightInfoLocked(deviceId); | 
|  | 1075 |  | 
|  | 1076 | auto it = infos.find(lightId); | 
|  | 1077 | if (it != infos.end()) { | 
|  | 1078 | return it->second; | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 1079 | } | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 1080 |  | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 1081 | return std::nullopt; | 
|  | 1082 | } | 
|  | 1083 |  | 
|  | 1084 | std::optional<int32_t> EventHub::getLightBrightness(int32_t deviceId, int32_t lightId) { | 
|  | 1085 | std::scoped_lock _l(mLock); | 
|  | 1086 |  | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 1087 | const auto infos = getLightInfoLocked(deviceId); | 
|  | 1088 | auto it = infos.find(lightId); | 
|  | 1089 | if (it == infos.end()) { | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 1090 | return std::nullopt; | 
|  | 1091 | } | 
|  | 1092 | std::string buffer; | 
|  | 1093 | if (!base::ReadFileToString(it->second.path / LIGHT_NODES.at(InputLightClass::BRIGHTNESS), | 
|  | 1094 | &buffer)) { | 
|  | 1095 | return std::nullopt; | 
|  | 1096 | } | 
|  | 1097 | return std::stoi(buffer); | 
|  | 1098 | } | 
|  | 1099 |  | 
|  | 1100 | std::optional<std::unordered_map<LightColor, int32_t>> EventHub::getLightIntensities( | 
|  | 1101 | int32_t deviceId, int32_t lightId) { | 
|  | 1102 | std::scoped_lock _l(mLock); | 
|  | 1103 |  | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 1104 | const auto infos = getLightInfoLocked(deviceId); | 
|  | 1105 | auto lightIt = infos.find(lightId); | 
|  | 1106 | if (lightIt == infos.end()) { | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 1107 | return std::nullopt; | 
|  | 1108 | } | 
|  | 1109 |  | 
|  | 1110 | auto ret = | 
|  | 1111 | getColorIndexArray(lightIt->second.path / LIGHT_NODES.at(InputLightClass::MULTI_INDEX)); | 
|  | 1112 |  | 
|  | 1113 | if (!ret.has_value()) { | 
|  | 1114 | return std::nullopt; | 
|  | 1115 | } | 
|  | 1116 | std::array<LightColor, COLOR_NUM> colors = ret.value(); | 
|  | 1117 |  | 
|  | 1118 | std::string intensityStr; | 
|  | 1119 | if (!base::ReadFileToString(lightIt->second.path / | 
|  | 1120 | LIGHT_NODES.at(InputLightClass::MULTI_INTENSITY), | 
|  | 1121 | &intensityStr)) { | 
|  | 1122 | return std::nullopt; | 
|  | 1123 | } | 
|  | 1124 |  | 
|  | 1125 | // Intensity node outputs 3 color values | 
|  | 1126 | std::regex intensityPattern("([0-9]+)\\s([0-9]+)\\s([0-9]+)[\\n]"); | 
|  | 1127 | std::smatch results; | 
|  | 1128 |  | 
|  | 1129 | if (!std::regex_match(intensityStr, results, intensityPattern)) { | 
|  | 1130 | return std::nullopt; | 
|  | 1131 | } | 
|  | 1132 | std::unordered_map<LightColor, int32_t> intensities; | 
|  | 1133 | for (size_t i = 1; i < results.size(); i++) { | 
|  | 1134 | int value = std::stoi(results[i].str()); | 
|  | 1135 | intensities.emplace(colors[i - 1], value); | 
|  | 1136 | } | 
|  | 1137 | return intensities; | 
|  | 1138 | } | 
|  | 1139 |  | 
|  | 1140 | void EventHub::setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) { | 
|  | 1141 | std::scoped_lock _l(mLock); | 
|  | 1142 |  | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 1143 | const auto infos = getLightInfoLocked(deviceId); | 
|  | 1144 | auto lightIt = infos.find(lightId); | 
|  | 1145 | if (lightIt == infos.end()) { | 
|  | 1146 | ALOGE("%s lightId %d not found ", __func__, lightId); | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 1147 | return; | 
|  | 1148 | } | 
|  | 1149 |  | 
|  | 1150 | if (!base::WriteStringToFile(std::to_string(brightness), | 
|  | 1151 | lightIt->second.path / | 
|  | 1152 | LIGHT_NODES.at(InputLightClass::BRIGHTNESS))) { | 
|  | 1153 | ALOGE("Can not write to file, error: %s", strerror(errno)); | 
|  | 1154 | } | 
|  | 1155 | } | 
|  | 1156 |  | 
|  | 1157 | void EventHub::setLightIntensities(int32_t deviceId, int32_t lightId, | 
|  | 1158 | std::unordered_map<LightColor, int32_t> intensities) { | 
|  | 1159 | std::scoped_lock _l(mLock); | 
|  | 1160 |  | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 1161 | const auto infos = getLightInfoLocked(deviceId); | 
|  | 1162 | auto lightIt = infos.find(lightId); | 
|  | 1163 | if (lightIt == infos.end()) { | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 1164 | ALOGE("Light Id %d does not exist.", lightId); | 
|  | 1165 | return; | 
|  | 1166 | } | 
|  | 1167 |  | 
|  | 1168 | auto ret = | 
|  | 1169 | getColorIndexArray(lightIt->second.path / LIGHT_NODES.at(InputLightClass::MULTI_INDEX)); | 
|  | 1170 |  | 
|  | 1171 | if (!ret.has_value()) { | 
|  | 1172 | return; | 
|  | 1173 | } | 
|  | 1174 | std::array<LightColor, COLOR_NUM> colors = ret.value(); | 
|  | 1175 |  | 
|  | 1176 | std::string rgbStr; | 
|  | 1177 | for (size_t i = 0; i < COLOR_NUM; i++) { | 
|  | 1178 | auto it = intensities.find(colors[i]); | 
|  | 1179 | if (it != intensities.end()) { | 
|  | 1180 | rgbStr += std::to_string(it->second); | 
|  | 1181 | // Insert space between colors | 
|  | 1182 | if (i < COLOR_NUM - 1) { | 
|  | 1183 | rgbStr += " "; | 
|  | 1184 | } | 
|  | 1185 | } | 
|  | 1186 | } | 
|  | 1187 | // Append new line | 
|  | 1188 | rgbStr += "\n"; | 
|  | 1189 |  | 
|  | 1190 | if (!base::WriteStringToFile(rgbStr, | 
|  | 1191 | lightIt->second.path / | 
|  | 1192 | LIGHT_NODES.at(InputLightClass::MULTI_INTENSITY))) { | 
|  | 1193 | ALOGE("Can not write to file, error: %s", strerror(errno)); | 
|  | 1194 | } | 
|  | 1195 | } | 
|  | 1196 |  | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 1197 | void EventHub::setExcludedDevices(const std::vector<std::string>& devices) { | 
| Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 1198 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1199 |  | 
|  | 1200 | mExcludedDevices = devices; | 
|  | 1201 | } | 
|  | 1202 |  | 
|  | 1203 | bool EventHub::hasScanCode(int32_t deviceId, int32_t scanCode) const { | 
| Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 1204 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1205 | Device* device = getDeviceLocked(deviceId); | 
| Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 1206 | if (device != nullptr && scanCode >= 0 && scanCode <= KEY_MAX) { | 
|  | 1207 | return device->keyBitmask.test(scanCode); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1208 | } | 
|  | 1209 | return false; | 
|  | 1210 | } | 
|  | 1211 |  | 
|  | 1212 | bool EventHub::hasLed(int32_t deviceId, int32_t led) const { | 
| Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 1213 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1214 | Device* device = getDeviceLocked(deviceId); | 
|  | 1215 | int32_t sc; | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1216 | if (device != nullptr && device->mapLed(led, &sc) == NO_ERROR) { | 
| Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 1217 | return device->ledBitmask.test(sc); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1218 | } | 
|  | 1219 | return false; | 
|  | 1220 | } | 
|  | 1221 |  | 
|  | 1222 | void EventHub::setLedState(int32_t deviceId, int32_t led, bool on) { | 
| Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 1223 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1224 | Device* device = getDeviceLocked(deviceId); | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1225 | if (device != nullptr && device->hasValidFd()) { | 
|  | 1226 | device->setLedStateLocked(led, on); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1227 | } | 
|  | 1228 | } | 
|  | 1229 |  | 
|  | 1230 | void EventHub::getVirtualKeyDefinitions(int32_t deviceId, | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1231 | std::vector<VirtualKeyDefinition>& outVirtualKeys) const { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1232 | outVirtualKeys.clear(); | 
|  | 1233 |  | 
| Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 1234 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1235 | Device* device = getDeviceLocked(deviceId); | 
| Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 1236 | if (device != nullptr && device->virtualKeyMap) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1237 | const std::vector<VirtualKeyDefinition> virtualKeys = | 
|  | 1238 | device->virtualKeyMap->getVirtualKeys(); | 
|  | 1239 | outVirtualKeys.insert(outVirtualKeys.end(), virtualKeys.begin(), virtualKeys.end()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1240 | } | 
|  | 1241 | } | 
|  | 1242 |  | 
| Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 1243 | const std::shared_ptr<KeyCharacterMap> EventHub::getKeyCharacterMap(int32_t deviceId) const { | 
| Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 1244 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1245 | Device* device = getDeviceLocked(deviceId); | 
| Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 1246 | if (device != nullptr) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1247 | return device->getKeyCharacterMap(); | 
|  | 1248 | } | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1249 | return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1250 | } | 
|  | 1251 |  | 
| Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 1252 | bool EventHub::setKeyboardLayoutOverlay(int32_t deviceId, std::shared_ptr<KeyCharacterMap> map) { | 
| Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 1253 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1254 | Device* device = getDeviceLocked(deviceId); | 
| Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 1255 | if (device != nullptr && map != nullptr && device->keyMap.keyCharacterMap != nullptr) { | 
|  | 1256 | device->keyMap.keyCharacterMap->combine(*map); | 
|  | 1257 | device->keyMap.keyCharacterMapFile = device->keyMap.keyCharacterMap->getLoadFileName(); | 
|  | 1258 | return true; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1259 | } | 
|  | 1260 | return false; | 
|  | 1261 | } | 
|  | 1262 |  | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 1263 | static std::string generateDescriptor(InputDeviceIdentifier& identifier) { | 
|  | 1264 | std::string rawDescriptor; | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1265 | rawDescriptor += StringPrintf(":%04x:%04x:", identifier.vendor, identifier.product); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1266 | // TODO add handling for USB devices to not uniqueify kbs that show up twice | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 1267 | if (!identifier.uniqueId.empty()) { | 
|  | 1268 | rawDescriptor += "uniqueId:"; | 
|  | 1269 | rawDescriptor += identifier.uniqueId; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1270 | } else if (identifier.nonce != 0) { | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 1271 | rawDescriptor += StringPrintf("nonce:%04x", identifier.nonce); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1272 | } | 
|  | 1273 |  | 
|  | 1274 | if (identifier.vendor == 0 && identifier.product == 0) { | 
|  | 1275 | // If we don't know the vendor and product id, then the device is probably | 
|  | 1276 | // built-in so we need to rely on other information to uniquely identify | 
|  | 1277 | // the input device.  Usually we try to avoid relying on the device name or | 
|  | 1278 | // location but for built-in input device, they are unlikely to ever change. | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 1279 | if (!identifier.name.empty()) { | 
|  | 1280 | rawDescriptor += "name:"; | 
|  | 1281 | rawDescriptor += identifier.name; | 
|  | 1282 | } else if (!identifier.location.empty()) { | 
|  | 1283 | rawDescriptor += "location:"; | 
|  | 1284 | rawDescriptor += identifier.location; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1285 | } | 
|  | 1286 | } | 
|  | 1287 | identifier.descriptor = sha1(rawDescriptor); | 
|  | 1288 | return rawDescriptor; | 
|  | 1289 | } | 
|  | 1290 |  | 
|  | 1291 | void EventHub::assignDescriptorLocked(InputDeviceIdentifier& identifier) { | 
|  | 1292 | // Compute a device descriptor that uniquely identifies the device. | 
|  | 1293 | // The descriptor is assumed to be a stable identifier.  Its value should not | 
|  | 1294 | // change between reboots, reconnections, firmware updates or new releases | 
|  | 1295 | // of Android. In practice we sometimes get devices that cannot be uniquely | 
|  | 1296 | // identified. In this case we enforce uniqueness between connected devices. | 
|  | 1297 | // Ideally, we also want the descriptor to be short and relatively opaque. | 
|  | 1298 |  | 
|  | 1299 | identifier.nonce = 0; | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 1300 | std::string rawDescriptor = generateDescriptor(identifier); | 
|  | 1301 | if (identifier.uniqueId.empty()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1302 | // If it didn't have a unique id check for conflicts and enforce | 
|  | 1303 | // uniqueness if necessary. | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1304 | while (getDeviceByDescriptorLocked(identifier.descriptor) != nullptr) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1305 | identifier.nonce++; | 
|  | 1306 | rawDescriptor = generateDescriptor(identifier); | 
|  | 1307 | } | 
|  | 1308 | } | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 1309 | ALOGV("Created descriptor: raw=%s, cooked=%s", rawDescriptor.c_str(), | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1310 | identifier.descriptor.c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1311 | } | 
|  | 1312 |  | 
| Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 1313 | void EventHub::vibrate(int32_t deviceId, const VibrationElement& element) { | 
| Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 1314 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1315 | Device* device = getDeviceLocked(deviceId); | 
| Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 1316 | if (device != nullptr && device->hasValidFd()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1317 | ff_effect effect; | 
|  | 1318 | memset(&effect, 0, sizeof(effect)); | 
|  | 1319 | effect.type = FF_RUMBLE; | 
|  | 1320 | effect.id = device->ffEffectId; | 
| Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 1321 | // evdev FF_RUMBLE effect only supports two channels of vibration. | 
| Chris Ye | 6393a26 | 2020-08-04 19:41:36 -0700 | [diff] [blame] | 1322 | effect.u.rumble.strong_magnitude = element.getMagnitude(FF_STRONG_MAGNITUDE_CHANNEL_IDX); | 
|  | 1323 | effect.u.rumble.weak_magnitude = element.getMagnitude(FF_WEAK_MAGNITUDE_CHANNEL_IDX); | 
| Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 1324 | effect.replay.length = element.duration.count(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1325 | effect.replay.delay = 0; | 
|  | 1326 | if (ioctl(device->fd, EVIOCSFF, &effect)) { | 
|  | 1327 | ALOGW("Could not upload force feedback effect to device %s due to error %d.", | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1328 | device->identifier.name.c_str(), errno); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1329 | return; | 
|  | 1330 | } | 
|  | 1331 | device->ffEffectId = effect.id; | 
|  | 1332 |  | 
|  | 1333 | struct input_event ev; | 
|  | 1334 | ev.time.tv_sec = 0; | 
|  | 1335 | ev.time.tv_usec = 0; | 
|  | 1336 | ev.type = EV_FF; | 
|  | 1337 | ev.code = device->ffEffectId; | 
|  | 1338 | ev.value = 1; | 
|  | 1339 | if (write(device->fd, &ev, sizeof(ev)) != sizeof(ev)) { | 
|  | 1340 | ALOGW("Could not start force feedback effect on device %s due to error %d.", | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1341 | device->identifier.name.c_str(), errno); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1342 | return; | 
|  | 1343 | } | 
|  | 1344 | device->ffEffectPlaying = true; | 
|  | 1345 | } | 
|  | 1346 | } | 
|  | 1347 |  | 
|  | 1348 | void EventHub::cancelVibrate(int32_t deviceId) { | 
| Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 1349 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1350 | Device* device = getDeviceLocked(deviceId); | 
| Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 1351 | if (device != nullptr && device->hasValidFd()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1352 | if (device->ffEffectPlaying) { | 
|  | 1353 | device->ffEffectPlaying = false; | 
|  | 1354 |  | 
|  | 1355 | struct input_event ev; | 
|  | 1356 | ev.time.tv_sec = 0; | 
|  | 1357 | ev.time.tv_usec = 0; | 
|  | 1358 | ev.type = EV_FF; | 
|  | 1359 | ev.code = device->ffEffectId; | 
|  | 1360 | ev.value = 0; | 
|  | 1361 | if (write(device->fd, &ev, sizeof(ev)) != sizeof(ev)) { | 
|  | 1362 | ALOGW("Could not stop force feedback effect on device %s due to error %d.", | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1363 | device->identifier.name.c_str(), errno); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1364 | return; | 
|  | 1365 | } | 
|  | 1366 | } | 
|  | 1367 | } | 
|  | 1368 | } | 
|  | 1369 |  | 
| Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 1370 | std::vector<int32_t> EventHub::getVibratorIds(int32_t deviceId) { | 
|  | 1371 | std::scoped_lock _l(mLock); | 
|  | 1372 | std::vector<int32_t> vibrators; | 
|  | 1373 | Device* device = getDeviceLocked(deviceId); | 
|  | 1374 | if (device != nullptr && device->hasValidFd() && | 
|  | 1375 | device->classes.test(InputDeviceClass::VIBRATOR)) { | 
|  | 1376 | vibrators.push_back(FF_STRONG_MAGNITUDE_CHANNEL_IDX); | 
|  | 1377 | vibrators.push_back(FF_WEAK_MAGNITUDE_CHANNEL_IDX); | 
|  | 1378 | } | 
|  | 1379 | return vibrators; | 
|  | 1380 | } | 
|  | 1381 |  | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 1382 | EventHub::Device* EventHub::getDeviceByDescriptorLocked(const std::string& descriptor) const { | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1383 | for (const auto& [id, device] : mDevices) { | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 1384 | if (descriptor == device->identifier.descriptor) { | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1385 | return device.get(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1386 | } | 
|  | 1387 | } | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1388 | return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1389 | } | 
|  | 1390 |  | 
|  | 1391 | EventHub::Device* EventHub::getDeviceLocked(int32_t deviceId) const { | 
| Prabir Pradhan | cae4b3a | 2019-02-05 18:51:32 -0800 | [diff] [blame] | 1392 | if (deviceId == ReservedInputDeviceId::BUILT_IN_KEYBOARD_ID) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1393 | deviceId = mBuiltInKeyboardId; | 
|  | 1394 | } | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1395 | const auto& it = mDevices.find(deviceId); | 
|  | 1396 | return it != mDevices.end() ? it->second.get() : nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1397 | } | 
|  | 1398 |  | 
| Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 1399 | EventHub::Device* EventHub::getDeviceByPathLocked(const std::string& devicePath) const { | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1400 | for (const auto& [id, device] : mDevices) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1401 | if (device->path == devicePath) { | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1402 | return device.get(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1403 | } | 
|  | 1404 | } | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1405 | return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1406 | } | 
|  | 1407 |  | 
| Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 1408 | /** | 
|  | 1409 | * The file descriptor could be either input device, or a video device (associated with a | 
|  | 1410 | * specific input device). Check both cases here, and return the device that this event | 
|  | 1411 | * belongs to. Caller can compare the fd's once more to determine event type. | 
|  | 1412 | * Looks through all input devices, and only attached video devices. Unattached video | 
|  | 1413 | * devices are ignored. | 
|  | 1414 | */ | 
| Siarhei Vishniakou | 4bc561c | 2018-11-02 17:41:58 -0700 | [diff] [blame] | 1415 | EventHub::Device* EventHub::getDeviceByFdLocked(int fd) const { | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1416 | for (const auto& [id, device] : mDevices) { | 
| Siarhei Vishniakou | 4bc561c | 2018-11-02 17:41:58 -0700 | [diff] [blame] | 1417 | if (device->fd == fd) { | 
| Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 1418 | // This is an input device event | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1419 | return device.get(); | 
| Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 1420 | } | 
|  | 1421 | if (device->videoDevice && device->videoDevice->getFd() == fd) { | 
|  | 1422 | // This is a video device event | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1423 | return device.get(); | 
| Siarhei Vishniakou | 4bc561c | 2018-11-02 17:41:58 -0700 | [diff] [blame] | 1424 | } | 
|  | 1425 | } | 
| Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 1426 | // We do not check mUnattachedVideoDevices here because they should not participate in epoll, | 
|  | 1427 | // and therefore should never be looked up by fd. | 
| Siarhei Vishniakou | 4bc561c | 2018-11-02 17:41:58 -0700 | [diff] [blame] | 1428 | return nullptr; | 
|  | 1429 | } | 
|  | 1430 |  | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 1431 | std::optional<int32_t> EventHub::getBatteryCapacity(int32_t deviceId, int32_t batteryId) const { | 
| Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 1432 | std::scoped_lock _l(mLock); | 
| Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 1433 |  | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 1434 | const auto infos = getBatteryInfoLocked(deviceId); | 
|  | 1435 | auto it = infos.find(batteryId); | 
|  | 1436 | if (it == infos.end()) { | 
| Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 1437 | return std::nullopt; | 
|  | 1438 | } | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 1439 | std::string buffer; | 
| Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 1440 |  | 
|  | 1441 | // Some devices report battery capacity as an integer through the "capacity" file | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 1442 | if (base::ReadFileToString(it->second.path / BATTERY_NODES.at(InputBatteryClass::CAPACITY), | 
|  | 1443 | &buffer)) { | 
| Chris Ye | d193677 | 2021-02-22 10:30:40 -0800 | [diff] [blame] | 1444 | return std::stoi(base::Trim(buffer)); | 
| Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 1445 | } | 
|  | 1446 |  | 
|  | 1447 | // Other devices report capacity as an enum value POWER_SUPPLY_CAPACITY_LEVEL_XXX | 
|  | 1448 | // These values are taken from kernel source code include/linux/power_supply.h | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 1449 | if (base::ReadFileToString(it->second.path / | 
|  | 1450 | BATTERY_NODES.at(InputBatteryClass::CAPACITY_LEVEL), | 
|  | 1451 | &buffer)) { | 
| Chris Ye | d193677 | 2021-02-22 10:30:40 -0800 | [diff] [blame] | 1452 | // Remove any white space such as trailing new line | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 1453 | const auto levelIt = BATTERY_LEVEL.find(base::Trim(buffer)); | 
|  | 1454 | if (levelIt != BATTERY_LEVEL.end()) { | 
|  | 1455 | return levelIt->second; | 
| Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 1456 | } | 
|  | 1457 | } | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 1458 |  | 
| Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 1459 | return std::nullopt; | 
|  | 1460 | } | 
|  | 1461 |  | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 1462 | std::optional<int32_t> EventHub::getBatteryStatus(int32_t deviceId, int32_t batteryId) const { | 
| Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 1463 | std::scoped_lock _l(mLock); | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 1464 | const auto infos = getBatteryInfoLocked(deviceId); | 
|  | 1465 | auto it = infos.find(batteryId); | 
|  | 1466 | if (it == infos.end()) { | 
| Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 1467 | return std::nullopt; | 
|  | 1468 | } | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 1469 | std::string buffer; | 
| Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 1470 |  | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 1471 | if (!base::ReadFileToString(it->second.path / BATTERY_NODES.at(InputBatteryClass::STATUS), | 
|  | 1472 | &buffer)) { | 
| Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 1473 | ALOGE("Failed to read sysfs battery info: %s", strerror(errno)); | 
|  | 1474 | return std::nullopt; | 
|  | 1475 | } | 
|  | 1476 |  | 
| Chris Ye | d193677 | 2021-02-22 10:30:40 -0800 | [diff] [blame] | 1477 | // Remove white space like trailing new line | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 1478 | const auto statusIt = BATTERY_STATUS.find(base::Trim(buffer)); | 
|  | 1479 | if (statusIt != BATTERY_STATUS.end()) { | 
|  | 1480 | return statusIt->second; | 
| Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 1481 | } | 
|  | 1482 |  | 
|  | 1483 | return std::nullopt; | 
|  | 1484 | } | 
|  | 1485 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1486 | size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) { | 
|  | 1487 | ALOG_ASSERT(bufferSize >= 1); | 
|  | 1488 |  | 
| Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 1489 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1490 |  | 
|  | 1491 | struct input_event readBuffer[bufferSize]; | 
|  | 1492 |  | 
|  | 1493 | RawEvent* event = buffer; | 
|  | 1494 | size_t capacity = bufferSize; | 
|  | 1495 | bool awoken = false; | 
|  | 1496 | for (;;) { | 
|  | 1497 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 1498 |  | 
|  | 1499 | // Reopen input devices if needed. | 
|  | 1500 | if (mNeedToReopenDevices) { | 
|  | 1501 | mNeedToReopenDevices = false; | 
|  | 1502 |  | 
|  | 1503 | ALOGI("Reopening all input devices due to a configuration change."); | 
|  | 1504 |  | 
|  | 1505 | closeAllDevicesLocked(); | 
|  | 1506 | mNeedToScanDevices = true; | 
|  | 1507 | break; // return to the caller before we actually rescan | 
|  | 1508 | } | 
|  | 1509 |  | 
|  | 1510 | // Report any devices that had last been added/removed. | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1511 | for (auto it = mClosingDevices.begin(); it != mClosingDevices.end();) { | 
|  | 1512 | std::unique_ptr<Device> device = std::move(*it); | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1513 | ALOGV("Reporting device closed: id=%d, name=%s\n", device->id, device->path.c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1514 | event->when = now; | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1515 | event->deviceId = (device->id == mBuiltInKeyboardId) | 
|  | 1516 | ? ReservedInputDeviceId::BUILT_IN_KEYBOARD_ID | 
|  | 1517 | : device->id; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1518 | event->type = DEVICE_REMOVED; | 
|  | 1519 | event += 1; | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1520 | it = mClosingDevices.erase(it); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1521 | mNeedToSendFinishedDeviceScan = true; | 
|  | 1522 | if (--capacity == 0) { | 
|  | 1523 | break; | 
|  | 1524 | } | 
|  | 1525 | } | 
|  | 1526 |  | 
|  | 1527 | if (mNeedToScanDevices) { | 
|  | 1528 | mNeedToScanDevices = false; | 
|  | 1529 | scanDevicesLocked(); | 
|  | 1530 | mNeedToSendFinishedDeviceScan = true; | 
|  | 1531 | } | 
|  | 1532 |  | 
| Siarhei Vishniakou | f49608d | 2020-08-20 19:18:21 -0500 | [diff] [blame] | 1533 | while (!mOpeningDevices.empty()) { | 
|  | 1534 | std::unique_ptr<Device> device = std::move(*mOpeningDevices.rbegin()); | 
|  | 1535 | mOpeningDevices.pop_back(); | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1536 | ALOGV("Reporting device opened: id=%d, name=%s\n", device->id, device->path.c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1537 | event->when = now; | 
|  | 1538 | event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id; | 
|  | 1539 | event->type = DEVICE_ADDED; | 
|  | 1540 | event += 1; | 
| Siarhei Vishniakou | f49608d | 2020-08-20 19:18:21 -0500 | [diff] [blame] | 1541 |  | 
|  | 1542 | // Try to find a matching video device by comparing device names | 
|  | 1543 | for (auto it = mUnattachedVideoDevices.begin(); it != mUnattachedVideoDevices.end(); | 
|  | 1544 | it++) { | 
|  | 1545 | std::unique_ptr<TouchVideoDevice>& videoDevice = *it; | 
| Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 1546 | if (tryAddVideoDeviceLocked(*device, videoDevice)) { | 
| Siarhei Vishniakou | f49608d | 2020-08-20 19:18:21 -0500 | [diff] [blame] | 1547 | // videoDevice was transferred to 'device' | 
|  | 1548 | it = mUnattachedVideoDevices.erase(it); | 
|  | 1549 | break; | 
|  | 1550 | } | 
|  | 1551 | } | 
|  | 1552 |  | 
|  | 1553 | auto [dev_it, inserted] = mDevices.insert_or_assign(device->id, std::move(device)); | 
|  | 1554 | if (!inserted) { | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1555 | ALOGW("Device id %d exists, replaced.", device->id); | 
|  | 1556 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1557 | mNeedToSendFinishedDeviceScan = true; | 
|  | 1558 | if (--capacity == 0) { | 
|  | 1559 | break; | 
|  | 1560 | } | 
|  | 1561 | } | 
|  | 1562 |  | 
|  | 1563 | if (mNeedToSendFinishedDeviceScan) { | 
|  | 1564 | mNeedToSendFinishedDeviceScan = false; | 
|  | 1565 | event->when = now; | 
|  | 1566 | event->type = FINISHED_DEVICE_SCAN; | 
|  | 1567 | event += 1; | 
|  | 1568 | if (--capacity == 0) { | 
|  | 1569 | break; | 
|  | 1570 | } | 
|  | 1571 | } | 
|  | 1572 |  | 
|  | 1573 | // Grab the next input event. | 
|  | 1574 | bool deviceChanged = false; | 
|  | 1575 | while (mPendingEventIndex < mPendingEventCount) { | 
|  | 1576 | const struct epoll_event& eventItem = mPendingEventItems[mPendingEventIndex++]; | 
| Siarhei Vishniakou | 4bc561c | 2018-11-02 17:41:58 -0700 | [diff] [blame] | 1577 | if (eventItem.data.fd == mINotifyFd) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1578 | if (eventItem.events & EPOLLIN) { | 
|  | 1579 | mPendingINotify = true; | 
|  | 1580 | } else { | 
|  | 1581 | ALOGW("Received unexpected epoll event 0x%08x for INotify.", eventItem.events); | 
|  | 1582 | } | 
|  | 1583 | continue; | 
|  | 1584 | } | 
|  | 1585 |  | 
| Siarhei Vishniakou | 4bc561c | 2018-11-02 17:41:58 -0700 | [diff] [blame] | 1586 | if (eventItem.data.fd == mWakeReadPipeFd) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1587 | if (eventItem.events & EPOLLIN) { | 
|  | 1588 | ALOGV("awoken after wake()"); | 
|  | 1589 | awoken = true; | 
| Siarhei Vishniakou | b4d960d | 2019-10-03 15:38:44 -0500 | [diff] [blame] | 1590 | char wakeReadBuffer[16]; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1591 | ssize_t nRead; | 
|  | 1592 | do { | 
| Siarhei Vishniakou | b4d960d | 2019-10-03 15:38:44 -0500 | [diff] [blame] | 1593 | nRead = read(mWakeReadPipeFd, wakeReadBuffer, sizeof(wakeReadBuffer)); | 
|  | 1594 | } while ((nRead == -1 && errno == EINTR) || nRead == sizeof(wakeReadBuffer)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1595 | } else { | 
|  | 1596 | ALOGW("Received unexpected epoll event 0x%08x for wake read pipe.", | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1597 | eventItem.events); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1598 | } | 
|  | 1599 | continue; | 
|  | 1600 | } | 
|  | 1601 |  | 
| Siarhei Vishniakou | 4bc561c | 2018-11-02 17:41:58 -0700 | [diff] [blame] | 1602 | Device* device = getDeviceByFdLocked(eventItem.data.fd); | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1603 | if (device == nullptr) { | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1604 | ALOGE("Received unexpected epoll event 0x%08x for unknown fd %d.", eventItem.events, | 
|  | 1605 | eventItem.data.fd); | 
| Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 1606 | ALOG_ASSERT(!DEBUG); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1607 | continue; | 
|  | 1608 | } | 
| Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 1609 | if (device->videoDevice && eventItem.data.fd == device->videoDevice->getFd()) { | 
|  | 1610 | if (eventItem.events & EPOLLIN) { | 
|  | 1611 | size_t numFrames = device->videoDevice->readAndQueueFrames(); | 
|  | 1612 | if (numFrames == 0) { | 
|  | 1613 | ALOGE("Received epoll event for video device %s, but could not read frame", | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1614 | device->videoDevice->getName().c_str()); | 
| Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 1615 | } | 
|  | 1616 | } else if (eventItem.events & EPOLLHUP) { | 
|  | 1617 | // TODO(b/121395353) - consider adding EPOLLRDHUP | 
|  | 1618 | ALOGI("Removing video device %s due to epoll hang-up event.", | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1619 | device->videoDevice->getName().c_str()); | 
| Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 1620 | unregisterVideoDeviceFromEpollLocked(*device->videoDevice); | 
|  | 1621 | device->videoDevice = nullptr; | 
|  | 1622 | } else { | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1623 | ALOGW("Received unexpected epoll event 0x%08x for device %s.", eventItem.events, | 
|  | 1624 | device->videoDevice->getName().c_str()); | 
| Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 1625 | ALOG_ASSERT(!DEBUG); | 
|  | 1626 | } | 
|  | 1627 | continue; | 
|  | 1628 | } | 
|  | 1629 | // This must be an input event | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1630 | if (eventItem.events & EPOLLIN) { | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1631 | int32_t readSize = | 
|  | 1632 | read(device->fd, readBuffer, sizeof(struct input_event) * capacity); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1633 | if (readSize == 0 || (readSize < 0 && errno == ENODEV)) { | 
|  | 1634 | // Device was removed before INotify noticed. | 
| Mark Salyzyn | 5aa26b2 | 2014-06-10 13:07:44 -0700 | [diff] [blame] | 1635 | ALOGW("could not get event, removed? (fd: %d size: %" PRId32 | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1636 | " bufferSize: %zu capacity: %zu errno: %d)\n", | 
|  | 1637 | device->fd, readSize, bufferSize, capacity, errno); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1638 | deviceChanged = true; | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1639 | closeDeviceLocked(*device); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1640 | } else if (readSize < 0) { | 
|  | 1641 | if (errno != EAGAIN && errno != EINTR) { | 
|  | 1642 | ALOGW("could not get event (errno=%d)", errno); | 
|  | 1643 | } | 
|  | 1644 | } else if ((readSize % sizeof(struct input_event)) != 0) { | 
|  | 1645 | ALOGE("could not get event (wrong size: %d)", readSize); | 
|  | 1646 | } else { | 
|  | 1647 | int32_t deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id; | 
|  | 1648 |  | 
|  | 1649 | size_t count = size_t(readSize) / sizeof(struct input_event); | 
|  | 1650 | for (size_t i = 0; i < count; i++) { | 
|  | 1651 | struct input_event& iev = readBuffer[i]; | 
| Siarhei Vishniakou | 592bac2 | 2018-11-08 19:42:38 -0800 | [diff] [blame] | 1652 | event->when = processEventTimestamp(iev); | 
| Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1653 | event->readTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1654 | event->deviceId = deviceId; | 
|  | 1655 | event->type = iev.type; | 
|  | 1656 | event->code = iev.code; | 
|  | 1657 | event->value = iev.value; | 
|  | 1658 | event += 1; | 
|  | 1659 | capacity -= 1; | 
|  | 1660 | } | 
|  | 1661 | if (capacity == 0) { | 
|  | 1662 | // The result buffer is full.  Reset the pending event index | 
|  | 1663 | // so we will try to read the device again on the next iteration. | 
|  | 1664 | mPendingEventIndex -= 1; | 
|  | 1665 | break; | 
|  | 1666 | } | 
|  | 1667 | } | 
|  | 1668 | } else if (eventItem.events & EPOLLHUP) { | 
|  | 1669 | ALOGI("Removing device %s due to epoll hang-up event.", | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1670 | device->identifier.name.c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1671 | deviceChanged = true; | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1672 | closeDeviceLocked(*device); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1673 | } else { | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1674 | ALOGW("Received unexpected epoll event 0x%08x for device %s.", eventItem.events, | 
|  | 1675 | device->identifier.name.c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1676 | } | 
|  | 1677 | } | 
|  | 1678 |  | 
|  | 1679 | // readNotify() will modify the list of devices so this must be done after | 
|  | 1680 | // processing all other events to ensure that we read all remaining events | 
|  | 1681 | // before closing the devices. | 
|  | 1682 | if (mPendingINotify && mPendingEventIndex >= mPendingEventCount) { | 
|  | 1683 | mPendingINotify = false; | 
|  | 1684 | readNotifyLocked(); | 
|  | 1685 | deviceChanged = true; | 
|  | 1686 | } | 
|  | 1687 |  | 
|  | 1688 | // Report added or removed devices immediately. | 
|  | 1689 | if (deviceChanged) { | 
|  | 1690 | continue; | 
|  | 1691 | } | 
|  | 1692 |  | 
|  | 1693 | // Return now if we have collected any events or if we were explicitly awoken. | 
|  | 1694 | if (event != buffer || awoken) { | 
|  | 1695 | break; | 
|  | 1696 | } | 
|  | 1697 |  | 
| Siarhei Vishniakou | 4b5a87f | 2019-09-24 13:03:58 +0100 | [diff] [blame] | 1698 | // Poll for events. | 
|  | 1699 | // When a device driver has pending (unread) events, it acquires | 
|  | 1700 | // a kernel wake lock.  Once the last pending event has been read, the device | 
|  | 1701 | // driver will release the kernel wake lock, but the epoll will hold the wakelock, | 
|  | 1702 | // since we are using EPOLLWAKEUP. The wakelock is released by the epoll when epoll_wait | 
|  | 1703 | // is called again for the same fd that produced the event. | 
|  | 1704 | // Thus the system can only sleep if there are no events pending or | 
|  | 1705 | // currently being processed. | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1706 | // | 
|  | 1707 | // The timeout is advisory only.  If the device is asleep, it will not wake just to | 
|  | 1708 | // service the timeout. | 
|  | 1709 | mPendingEventIndex = 0; | 
|  | 1710 |  | 
| Siarhei Vishniakou | 4b5a87f | 2019-09-24 13:03:58 +0100 | [diff] [blame] | 1711 | mLock.unlock(); // release lock before poll | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1712 |  | 
|  | 1713 | int pollResult = epoll_wait(mEpollFd, mPendingEventItems, EPOLL_MAX_EVENTS, timeoutMillis); | 
|  | 1714 |  | 
| Siarhei Vishniakou | 4b5a87f | 2019-09-24 13:03:58 +0100 | [diff] [blame] | 1715 | mLock.lock(); // reacquire lock after poll | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1716 |  | 
|  | 1717 | if (pollResult == 0) { | 
|  | 1718 | // Timed out. | 
|  | 1719 | mPendingEventCount = 0; | 
|  | 1720 | break; | 
|  | 1721 | } | 
|  | 1722 |  | 
|  | 1723 | if (pollResult < 0) { | 
|  | 1724 | // An error occurred. | 
|  | 1725 | mPendingEventCount = 0; | 
|  | 1726 |  | 
|  | 1727 | // Sleep after errors to avoid locking up the system. | 
|  | 1728 | // Hopefully the error is transient. | 
|  | 1729 | if (errno != EINTR) { | 
|  | 1730 | ALOGW("poll failed (errno=%d)\n", errno); | 
|  | 1731 | usleep(100000); | 
|  | 1732 | } | 
|  | 1733 | } else { | 
|  | 1734 | // Some events occurred. | 
|  | 1735 | mPendingEventCount = size_t(pollResult); | 
|  | 1736 | } | 
|  | 1737 | } | 
|  | 1738 |  | 
|  | 1739 | // All done, return the number of events we read. | 
|  | 1740 | return event - buffer; | 
|  | 1741 | } | 
|  | 1742 |  | 
| Siarhei Vishniakou | add8929 | 2018-12-13 19:23:36 -0800 | [diff] [blame] | 1743 | std::vector<TouchVideoFrame> EventHub::getVideoFrames(int32_t deviceId) { | 
| Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 1744 | std::scoped_lock _l(mLock); | 
| Siarhei Vishniakou | add8929 | 2018-12-13 19:23:36 -0800 | [diff] [blame] | 1745 |  | 
|  | 1746 | Device* device = getDeviceLocked(deviceId); | 
| Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 1747 | if (device == nullptr || !device->videoDevice) { | 
| Siarhei Vishniakou | add8929 | 2018-12-13 19:23:36 -0800 | [diff] [blame] | 1748 | return {}; | 
|  | 1749 | } | 
|  | 1750 | return device->videoDevice->consumeFrames(); | 
|  | 1751 | } | 
|  | 1752 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1753 | void EventHub::wake() { | 
|  | 1754 | ALOGV("wake() called"); | 
|  | 1755 |  | 
|  | 1756 | ssize_t nWrite; | 
|  | 1757 | do { | 
|  | 1758 | nWrite = write(mWakeWritePipeFd, "W", 1); | 
|  | 1759 | } while (nWrite == -1 && errno == EINTR); | 
|  | 1760 |  | 
|  | 1761 | if (nWrite != 1 && errno != EAGAIN) { | 
| Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 1762 | ALOGW("Could not write wake signal: %s", strerror(errno)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1763 | } | 
|  | 1764 | } | 
|  | 1765 |  | 
|  | 1766 | void EventHub::scanDevicesLocked() { | 
| Usama Arif | 78eb975 | 2021-06-03 16:44:09 +0100 | [diff] [blame] | 1767 | status_t result; | 
|  | 1768 | std::error_code errorCode; | 
|  | 1769 |  | 
|  | 1770 | if (std::filesystem::exists(DEVICE_INPUT_PATH, errorCode)) { | 
|  | 1771 | result = scanDirLocked(DEVICE_INPUT_PATH); | 
|  | 1772 | if (result < 0) { | 
|  | 1773 | ALOGE("scan dir failed for %s", DEVICE_INPUT_PATH); | 
|  | 1774 | } | 
|  | 1775 | } else { | 
|  | 1776 | if (errorCode) { | 
|  | 1777 | ALOGW("Could not run filesystem::exists() due to error %d : %s.", errorCode.value(), | 
|  | 1778 | errorCode.message().c_str()); | 
|  | 1779 | } | 
| Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 1780 | } | 
| Philip Quinn | 39b8168 | 2019-01-09 22:20:39 -0800 | [diff] [blame] | 1781 | if (isV4lScanningEnabled()) { | 
| Usama Arif | 78eb975 | 2021-06-03 16:44:09 +0100 | [diff] [blame] | 1782 | result = scanVideoDirLocked(DEVICE_PATH); | 
| Philip Quinn | 39b8168 | 2019-01-09 22:20:39 -0800 | [diff] [blame] | 1783 | if (result != OK) { | 
| Usama Arif | 78eb975 | 2021-06-03 16:44:09 +0100 | [diff] [blame] | 1784 | ALOGE("scan video dir failed for %s", DEVICE_PATH); | 
| Philip Quinn | 39b8168 | 2019-01-09 22:20:39 -0800 | [diff] [blame] | 1785 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1786 | } | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1787 | if (mDevices.find(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID) == mDevices.end()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1788 | createVirtualKeyboardLocked(); | 
|  | 1789 | } | 
|  | 1790 | } | 
|  | 1791 |  | 
|  | 1792 | // ---------------------------------------------------------------------------- | 
|  | 1793 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1794 | static const int32_t GAMEPAD_KEYCODES[] = { | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1795 | AKEYCODE_BUTTON_A,      AKEYCODE_BUTTON_B,      AKEYCODE_BUTTON_C,    // | 
|  | 1796 | AKEYCODE_BUTTON_X,      AKEYCODE_BUTTON_Y,      AKEYCODE_BUTTON_Z,    // | 
|  | 1797 | AKEYCODE_BUTTON_L1,     AKEYCODE_BUTTON_R1,                           // | 
|  | 1798 | AKEYCODE_BUTTON_L2,     AKEYCODE_BUTTON_R2,                           // | 
|  | 1799 | AKEYCODE_BUTTON_THUMBL, AKEYCODE_BUTTON_THUMBR,                       // | 
|  | 1800 | AKEYCODE_BUTTON_START,  AKEYCODE_BUTTON_SELECT, AKEYCODE_BUTTON_MODE, // | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1801 | }; | 
|  | 1802 |  | 
| Siarhei Vishniakou | 2592031 | 2018-12-12 15:24:44 -0800 | [diff] [blame] | 1803 | status_t EventHub::registerFdForEpoll(int fd) { | 
| Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 1804 | // TODO(b/121395353) - consider adding EPOLLRDHUP | 
| Siarhei Vishniakou | 2592031 | 2018-12-12 15:24:44 -0800 | [diff] [blame] | 1805 | struct epoll_event eventItem = {}; | 
|  | 1806 | eventItem.events = EPOLLIN | EPOLLWAKEUP; | 
|  | 1807 | eventItem.data.fd = fd; | 
|  | 1808 | if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, &eventItem)) { | 
|  | 1809 | ALOGE("Could not add fd to epoll instance: %s", strerror(errno)); | 
| Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 1810 | return -errno; | 
|  | 1811 | } | 
|  | 1812 | return OK; | 
|  | 1813 | } | 
|  | 1814 |  | 
| Siarhei Vishniakou | 2592031 | 2018-12-12 15:24:44 -0800 | [diff] [blame] | 1815 | status_t EventHub::unregisterFdFromEpoll(int fd) { | 
|  | 1816 | if (epoll_ctl(mEpollFd, EPOLL_CTL_DEL, fd, nullptr)) { | 
|  | 1817 | ALOGW("Could not remove fd from epoll instance: %s", strerror(errno)); | 
|  | 1818 | return -errno; | 
|  | 1819 | } | 
|  | 1820 | return OK; | 
|  | 1821 | } | 
|  | 1822 |  | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1823 | status_t EventHub::registerDeviceForEpollLocked(Device& device) { | 
|  | 1824 | status_t result = registerFdForEpoll(device.fd); | 
| Siarhei Vishniakou | 2592031 | 2018-12-12 15:24:44 -0800 | [diff] [blame] | 1825 | if (result != OK) { | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1826 | ALOGE("Could not add input device fd to epoll for device %" PRId32, device.id); | 
| Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 1827 | return result; | 
|  | 1828 | } | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1829 | if (device.videoDevice) { | 
|  | 1830 | registerVideoDeviceForEpollLocked(*device.videoDevice); | 
| Siarhei Vishniakou | 2592031 | 2018-12-12 15:24:44 -0800 | [diff] [blame] | 1831 | } | 
|  | 1832 | return result; | 
|  | 1833 | } | 
|  | 1834 |  | 
| Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 1835 | void EventHub::registerVideoDeviceForEpollLocked(const TouchVideoDevice& videoDevice) { | 
|  | 1836 | status_t result = registerFdForEpoll(videoDevice.getFd()); | 
|  | 1837 | if (result != OK) { | 
|  | 1838 | ALOGE("Could not add video device %s to epoll", videoDevice.getName().c_str()); | 
|  | 1839 | } | 
|  | 1840 | } | 
|  | 1841 |  | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1842 | status_t EventHub::unregisterDeviceFromEpollLocked(Device& device) { | 
|  | 1843 | if (device.hasValidFd()) { | 
|  | 1844 | status_t result = unregisterFdFromEpoll(device.fd); | 
| Siarhei Vishniakou | 2592031 | 2018-12-12 15:24:44 -0800 | [diff] [blame] | 1845 | if (result != OK) { | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1846 | ALOGW("Could not remove input device fd from epoll for device %" PRId32, device.id); | 
| Siarhei Vishniakou | 2592031 | 2018-12-12 15:24:44 -0800 | [diff] [blame] | 1847 | return result; | 
| Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 1848 | } | 
|  | 1849 | } | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1850 | if (device.videoDevice) { | 
|  | 1851 | unregisterVideoDeviceFromEpollLocked(*device.videoDevice); | 
| Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 1852 | } | 
| Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 1853 | return OK; | 
|  | 1854 | } | 
|  | 1855 |  | 
| Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 1856 | void EventHub::unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& videoDevice) { | 
|  | 1857 | if (videoDevice.hasValidFd()) { | 
|  | 1858 | status_t result = unregisterFdFromEpoll(videoDevice.getFd()); | 
|  | 1859 | if (result != OK) { | 
|  | 1860 | ALOGW("Could not remove video device fd from epoll for device: %s", | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1861 | videoDevice.getName().c_str()); | 
| Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 1862 | } | 
|  | 1863 | } | 
|  | 1864 | } | 
|  | 1865 |  | 
| Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 1866 | void EventHub::reportDeviceAddedForStatisticsLocked(const InputDeviceIdentifier& identifier, | 
|  | 1867 | Flags<InputDeviceClass> classes) { | 
| Chris Ye | 657c2f0 | 2021-05-25 16:24:37 -0700 | [diff] [blame] | 1868 | SHA256_CTX ctx; | 
|  | 1869 | SHA256_Init(&ctx); | 
|  | 1870 | SHA256_Update(&ctx, reinterpret_cast<const uint8_t*>(identifier.uniqueId.c_str()), | 
|  | 1871 | identifier.uniqueId.size()); | 
|  | 1872 | std::array<uint8_t, SHA256_DIGEST_LENGTH> digest; | 
|  | 1873 | SHA256_Final(digest.data(), &ctx); | 
|  | 1874 |  | 
|  | 1875 | std::string obfuscatedId; | 
|  | 1876 | for (size_t i = 0; i < OBFUSCATED_LENGTH; i++) { | 
|  | 1877 | obfuscatedId += StringPrintf("%02x", digest[i]); | 
|  | 1878 | } | 
|  | 1879 |  | 
| Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 1880 | android::util::stats_write(android::util::INPUTDEVICE_REGISTERED, identifier.name.c_str(), | 
|  | 1881 | identifier.vendor, identifier.product, identifier.version, | 
| Chris Ye | 657c2f0 | 2021-05-25 16:24:37 -0700 | [diff] [blame] | 1882 | identifier.bus, obfuscatedId.c_str(), classes.get()); | 
| Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 1883 | } | 
|  | 1884 |  | 
| Siarhei Vishniakou | a4c502a | 2021-02-05 00:45:20 +0000 | [diff] [blame] | 1885 | void EventHub::openDeviceLocked(const std::string& devicePath) { | 
|  | 1886 | // If an input device happens to register around the time when EventHub's constructor runs, it | 
|  | 1887 | // is possible that the same input event node (for example, /dev/input/event3) will be noticed | 
|  | 1888 | // in both 'inotify' callback and also in the 'scanDirLocked' pass. To prevent duplicate devices | 
|  | 1889 | // from getting registered, ensure that this path is not already covered by an existing device. | 
|  | 1890 | for (const auto& [deviceId, device] : mDevices) { | 
|  | 1891 | if (device->path == devicePath) { | 
|  | 1892 | return; // device was already registered | 
|  | 1893 | } | 
|  | 1894 | } | 
|  | 1895 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1896 | char buffer[80]; | 
|  | 1897 |  | 
| Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 1898 | ALOGV("Opening device: %s", devicePath.c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1899 |  | 
| Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 1900 | int fd = open(devicePath.c_str(), O_RDWR | O_CLOEXEC | O_NONBLOCK); | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1901 | if (fd < 0) { | 
| Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 1902 | ALOGE("could not open %s, %s\n", devicePath.c_str(), strerror(errno)); | 
| Siarhei Vishniakou | a4c502a | 2021-02-05 00:45:20 +0000 | [diff] [blame] | 1903 | return; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1904 | } | 
|  | 1905 |  | 
|  | 1906 | InputDeviceIdentifier identifier; | 
|  | 1907 |  | 
|  | 1908 | // Get device name. | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1909 | if (ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) { | 
| Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 1910 | ALOGE("Could not get device name for %s: %s", devicePath.c_str(), strerror(errno)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1911 | } else { | 
|  | 1912 | buffer[sizeof(buffer) - 1] = '\0'; | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 1913 | identifier.name = buffer; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1914 | } | 
|  | 1915 |  | 
|  | 1916 | // Check to see if the device is on our excluded list | 
|  | 1917 | for (size_t i = 0; i < mExcludedDevices.size(); i++) { | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 1918 | const std::string& item = mExcludedDevices[i]; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1919 | if (identifier.name == item) { | 
| Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 1920 | ALOGI("ignoring event id %s driver %s\n", devicePath.c_str(), item.c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1921 | close(fd); | 
| Siarhei Vishniakou | a4c502a | 2021-02-05 00:45:20 +0000 | [diff] [blame] | 1922 | return; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1923 | } | 
|  | 1924 | } | 
|  | 1925 |  | 
|  | 1926 | // Get device driver version. | 
|  | 1927 | int driverVersion; | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1928 | if (ioctl(fd, EVIOCGVERSION, &driverVersion)) { | 
| Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 1929 | ALOGE("could not get driver version for %s, %s\n", devicePath.c_str(), strerror(errno)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1930 | close(fd); | 
| Siarhei Vishniakou | a4c502a | 2021-02-05 00:45:20 +0000 | [diff] [blame] | 1931 | return; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1932 | } | 
|  | 1933 |  | 
|  | 1934 | // Get device identifier. | 
|  | 1935 | struct input_id inputId; | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1936 | if (ioctl(fd, EVIOCGID, &inputId)) { | 
| Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 1937 | ALOGE("could not get device input id for %s, %s\n", devicePath.c_str(), strerror(errno)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1938 | close(fd); | 
| Siarhei Vishniakou | a4c502a | 2021-02-05 00:45:20 +0000 | [diff] [blame] | 1939 | return; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1940 | } | 
|  | 1941 | identifier.bus = inputId.bustype; | 
|  | 1942 | identifier.product = inputId.product; | 
|  | 1943 | identifier.vendor = inputId.vendor; | 
|  | 1944 | identifier.version = inputId.version; | 
|  | 1945 |  | 
|  | 1946 | // Get device physical location. | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1947 | if (ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) { | 
|  | 1948 | // fprintf(stderr, "could not get location for %s, %s\n", devicePath, strerror(errno)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1949 | } else { | 
|  | 1950 | buffer[sizeof(buffer) - 1] = '\0'; | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 1951 | identifier.location = buffer; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1952 | } | 
|  | 1953 |  | 
|  | 1954 | // Get device unique id. | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1955 | if (ioctl(fd, EVIOCGUNIQ(sizeof(buffer) - 1), &buffer) < 1) { | 
|  | 1956 | // fprintf(stderr, "could not get idstring for %s, %s\n", devicePath, strerror(errno)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1957 | } else { | 
|  | 1958 | buffer[sizeof(buffer) - 1] = '\0'; | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 1959 | identifier.uniqueId = buffer; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1960 | } | 
|  | 1961 |  | 
|  | 1962 | // Fill in the descriptor. | 
|  | 1963 | assignDescriptorLocked(identifier); | 
|  | 1964 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1965 | // Allocate device.  (The device object takes ownership of the fd at this point.) | 
|  | 1966 | int32_t deviceId = mNextDeviceId++; | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1967 | std::unique_ptr<Device> device = std::make_unique<Device>(fd, deviceId, devicePath, identifier); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1968 |  | 
| Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 1969 | ALOGV("add device %d: %s\n", deviceId, devicePath.c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1970 | ALOGV("  bus:        %04x\n" | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1971 | "  vendor      %04x\n" | 
|  | 1972 | "  product     %04x\n" | 
|  | 1973 | "  version     %04x\n", | 
|  | 1974 | identifier.bus, identifier.vendor, identifier.product, identifier.version); | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 1975 | ALOGV("  name:       \"%s\"\n", identifier.name.c_str()); | 
|  | 1976 | ALOGV("  location:   \"%s\"\n", identifier.location.c_str()); | 
|  | 1977 | ALOGV("  unique id:  \"%s\"\n", identifier.uniqueId.c_str()); | 
|  | 1978 | ALOGV("  descriptor: \"%s\"\n", identifier.descriptor.c_str()); | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1979 | ALOGV("  driver:     v%d.%d.%d\n", driverVersion >> 16, (driverVersion >> 8) & 0xff, | 
|  | 1980 | driverVersion & 0xff); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1981 |  | 
|  | 1982 | // Load the configuration file for the device. | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1983 | device->loadConfigurationLocked(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1984 |  | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 1985 | bool hasBattery = false; | 
|  | 1986 | bool hasLights = false; | 
|  | 1987 | // Check the sysfs root path | 
|  | 1988 | std::optional<std::filesystem::path> sysfsRootPath = getSysfsRootPath(devicePath.c_str()); | 
|  | 1989 | if (sysfsRootPath.has_value()) { | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 1990 | std::shared_ptr<AssociatedDevice> associatedDevice; | 
|  | 1991 | for (const auto& [id, dev] : mDevices) { | 
|  | 1992 | if (device->identifier.descriptor == dev->identifier.descriptor && | 
|  | 1993 | !dev->associatedDevice) { | 
|  | 1994 | associatedDevice = dev->associatedDevice; | 
|  | 1995 | } | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 1996 | } | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 1997 | if (!associatedDevice) { | 
|  | 1998 | associatedDevice = std::make_shared<AssociatedDevice>(sysfsRootPath.value()); | 
|  | 1999 | } | 
|  | 2000 | hasBattery = associatedDevice->configureBatteryLocked(); | 
|  | 2001 | hasLights = associatedDevice->configureLightsLocked(); | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 2002 |  | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 2003 | device->associatedDevice = associatedDevice; | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 2004 | } | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 2005 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2006 | // Figure out the kinds of events the device reports. | 
| Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 2007 | device->readDeviceBitMask(EVIOCGBIT(EV_KEY, 0), device->keyBitmask); | 
|  | 2008 | device->readDeviceBitMask(EVIOCGBIT(EV_ABS, 0), device->absBitmask); | 
|  | 2009 | device->readDeviceBitMask(EVIOCGBIT(EV_REL, 0), device->relBitmask); | 
|  | 2010 | device->readDeviceBitMask(EVIOCGBIT(EV_SW, 0), device->swBitmask); | 
|  | 2011 | device->readDeviceBitMask(EVIOCGBIT(EV_LED, 0), device->ledBitmask); | 
|  | 2012 | device->readDeviceBitMask(EVIOCGBIT(EV_FF, 0), device->ffBitmask); | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2013 | device->readDeviceBitMask(EVIOCGBIT(EV_MSC, 0), device->mscBitmask); | 
| Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 2014 | device->readDeviceBitMask(EVIOCGPROP(0), device->propBitmask); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2015 |  | 
|  | 2016 | // See if this is a keyboard.  Ignore everything in the button range except for | 
|  | 2017 | // joystick and gamepad buttons which are handled like keyboards for the most part. | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 2018 | bool haveKeyboardKeys = | 
| Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 2019 | device->keyBitmask.any(0, BTN_MISC) || device->keyBitmask.any(BTN_WHEEL, KEY_MAX + 1); | 
|  | 2020 | bool haveGamepadButtons = device->keyBitmask.any(BTN_MISC, BTN_MOUSE) || | 
|  | 2021 | device->keyBitmask.any(BTN_JOYSTICK, BTN_DIGI); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2022 | if (haveKeyboardKeys || haveGamepadButtons) { | 
| Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 2023 | device->classes |= InputDeviceClass::KEYBOARD; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2024 | } | 
|  | 2025 |  | 
|  | 2026 | // See if this is a cursor device such as a trackball or mouse. | 
| Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 2027 | if (device->keyBitmask.test(BTN_MOUSE) && device->relBitmask.test(REL_X) && | 
|  | 2028 | device->relBitmask.test(REL_Y)) { | 
| Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 2029 | device->classes |= InputDeviceClass::CURSOR; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2030 | } | 
|  | 2031 |  | 
| Prashant Malani | 1941ff5 | 2015-08-11 18:29:28 -0700 | [diff] [blame] | 2032 | // See if this is a rotary encoder type device. | 
|  | 2033 | String8 deviceType = String8(); | 
|  | 2034 | if (device->configuration && | 
|  | 2035 | device->configuration->tryGetProperty(String8("device.type"), deviceType)) { | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 2036 | if (!deviceType.compare(String8("rotaryEncoder"))) { | 
| Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 2037 | device->classes |= InputDeviceClass::ROTARY_ENCODER; | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 2038 | } | 
| Prashant Malani | 1941ff5 | 2015-08-11 18:29:28 -0700 | [diff] [blame] | 2039 | } | 
|  | 2040 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2041 | // See if this is a touch pad. | 
|  | 2042 | // Is this a new modern multi-touch driver? | 
| Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 2043 | if (device->absBitmask.test(ABS_MT_POSITION_X) && device->absBitmask.test(ABS_MT_POSITION_Y)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2044 | // Some joysticks such as the PS3 controller report axes that conflict | 
|  | 2045 | // with the ABS_MT range.  Try to confirm that the device really is | 
|  | 2046 | // a touch screen. | 
| Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 2047 | if (device->keyBitmask.test(BTN_TOUCH) || !haveGamepadButtons) { | 
| Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 2048 | device->classes |= (InputDeviceClass::TOUCH | InputDeviceClass::TOUCH_MT); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2049 | } | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 2050 | // Is this an old style single-touch driver? | 
| Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 2051 | } else if (device->keyBitmask.test(BTN_TOUCH) && device->absBitmask.test(ABS_X) && | 
|  | 2052 | device->absBitmask.test(ABS_Y)) { | 
| Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 2053 | device->classes |= InputDeviceClass::TOUCH; | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 2054 | // Is this a BT stylus? | 
| Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 2055 | } else if ((device->absBitmask.test(ABS_PRESSURE) || device->keyBitmask.test(BTN_TOUCH)) && | 
|  | 2056 | !device->absBitmask.test(ABS_X) && !device->absBitmask.test(ABS_Y)) { | 
| Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 2057 | device->classes |= InputDeviceClass::EXTERNAL_STYLUS; | 
| Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 2058 | // Keyboard will try to claim some of the buttons but we really want to reserve those so we | 
|  | 2059 | // can fuse it with the touch screen data, so just take them back. Note this means an | 
|  | 2060 | // external stylus cannot also be a keyboard device. | 
| Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 2061 | device->classes &= ~InputDeviceClass::KEYBOARD; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2062 | } | 
|  | 2063 |  | 
|  | 2064 | // See if this device is a joystick. | 
|  | 2065 | // Assumes that joysticks always have gamepad buttons in order to distinguish them | 
|  | 2066 | // from other devices such as accelerometers that also have absolute axes. | 
|  | 2067 | if (haveGamepadButtons) { | 
| Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 2068 | auto assumedClasses = device->classes | InputDeviceClass::JOYSTICK; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2069 | for (int i = 0; i <= ABS_MAX; i++) { | 
| Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 2070 | if (device->absBitmask.test(i) && | 
| Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 2071 | (getAbsAxisUsage(i, assumedClasses).test(InputDeviceClass::JOYSTICK))) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2072 | device->classes = assumedClasses; | 
|  | 2073 | break; | 
|  | 2074 | } | 
|  | 2075 | } | 
|  | 2076 | } | 
|  | 2077 |  | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2078 | // Check whether this device is an accelerometer. | 
|  | 2079 | if (device->propBitmask.test(INPUT_PROP_ACCELEROMETER)) { | 
|  | 2080 | device->classes |= InputDeviceClass::SENSOR; | 
|  | 2081 | } | 
|  | 2082 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2083 | // Check whether this device has switches. | 
|  | 2084 | for (int i = 0; i <= SW_MAX; i++) { | 
| Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 2085 | if (device->swBitmask.test(i)) { | 
| Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 2086 | device->classes |= InputDeviceClass::SWITCH; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2087 | break; | 
|  | 2088 | } | 
|  | 2089 | } | 
|  | 2090 |  | 
|  | 2091 | // Check whether this device supports the vibrator. | 
| Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 2092 | if (device->ffBitmask.test(FF_RUMBLE)) { | 
| Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 2093 | device->classes |= InputDeviceClass::VIBRATOR; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2094 | } | 
|  | 2095 |  | 
|  | 2096 | // Configure virtual keys. | 
| Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 2097 | if ((device->classes.test(InputDeviceClass::TOUCH))) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2098 | // Load the virtual keys for the touch screen, if any. | 
|  | 2099 | // We do this now so that we can make sure to load the keymap if necessary. | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2100 | bool success = device->loadVirtualKeyMapLocked(); | 
| Siarhei Vishniakou | 3e78dec | 2019-02-20 16:21:46 -0600 | [diff] [blame] | 2101 | if (success) { | 
| Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 2102 | device->classes |= InputDeviceClass::KEYBOARD; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2103 | } | 
|  | 2104 | } | 
|  | 2105 |  | 
|  | 2106 | // Load the key map. | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2107 | // We need to do this for joysticks too because the key layout may specify axes, and for | 
|  | 2108 | // sensor as well because the key layout may specify the axes to sensor data mapping. | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2109 | status_t keyMapStatus = NAME_NOT_FOUND; | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2110 | if (device->classes.any(InputDeviceClass::KEYBOARD | InputDeviceClass::JOYSTICK | | 
|  | 2111 | InputDeviceClass::SENSOR)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2112 | // Load the keymap for the device. | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2113 | keyMapStatus = device->loadKeyMapLocked(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2114 | } | 
|  | 2115 |  | 
|  | 2116 | // Configure the keyboard, gamepad or virtual keyboard. | 
| Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 2117 | if (device->classes.test(InputDeviceClass::KEYBOARD)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2118 | // Register the keyboard as a built-in keyboard if it is eligible. | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 2119 | if (!keyMapStatus && mBuiltInKeyboardId == NO_BUILT_IN_KEYBOARD && | 
| Siarhei Vishniakou | d549b25 | 2020-08-11 11:25:26 -0500 | [diff] [blame] | 2120 | isEligibleBuiltInKeyboard(device->identifier, device->configuration.get(), | 
|  | 2121 | &device->keyMap)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2122 | mBuiltInKeyboardId = device->id; | 
|  | 2123 | } | 
|  | 2124 |  | 
|  | 2125 | // 'Q' key support = cheap test of whether this is an alpha-capable kbd | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2126 | if (device->hasKeycodeLocked(AKEYCODE_Q)) { | 
| Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 2127 | device->classes |= InputDeviceClass::ALPHAKEY; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2128 | } | 
|  | 2129 |  | 
|  | 2130 | // See if this device has a DPAD. | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2131 | if (device->hasKeycodeLocked(AKEYCODE_DPAD_UP) && | 
|  | 2132 | device->hasKeycodeLocked(AKEYCODE_DPAD_DOWN) && | 
|  | 2133 | device->hasKeycodeLocked(AKEYCODE_DPAD_LEFT) && | 
|  | 2134 | device->hasKeycodeLocked(AKEYCODE_DPAD_RIGHT) && | 
|  | 2135 | device->hasKeycodeLocked(AKEYCODE_DPAD_CENTER)) { | 
| Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 2136 | device->classes |= InputDeviceClass::DPAD; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2137 | } | 
|  | 2138 |  | 
|  | 2139 | // See if this device has a gamepad. | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 2140 | for (size_t i = 0; i < sizeof(GAMEPAD_KEYCODES) / sizeof(GAMEPAD_KEYCODES[0]); i++) { | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2141 | if (device->hasKeycodeLocked(GAMEPAD_KEYCODES[i])) { | 
| Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 2142 | device->classes |= InputDeviceClass::GAMEPAD; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2143 | break; | 
|  | 2144 | } | 
|  | 2145 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2146 | } | 
|  | 2147 |  | 
|  | 2148 | // If the device isn't recognized as something we handle, don't monitor it. | 
| Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 2149 | if (device->classes == Flags<InputDeviceClass>(0)) { | 
| Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 2150 | ALOGV("Dropping device: id=%d, path='%s', name='%s'", deviceId, devicePath.c_str(), | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 2151 | device->identifier.name.c_str()); | 
| Siarhei Vishniakou | a4c502a | 2021-02-05 00:45:20 +0000 | [diff] [blame] | 2152 | return; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2153 | } | 
|  | 2154 |  | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 2155 | // Classify InputDeviceClass::BATTERY. | 
|  | 2156 | if (hasBattery) { | 
|  | 2157 | device->classes |= InputDeviceClass::BATTERY; | 
|  | 2158 | } | 
| Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 2159 |  | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 2160 | // Classify InputDeviceClass::LIGHT. | 
|  | 2161 | if (hasLights) { | 
|  | 2162 | device->classes |= InputDeviceClass::LIGHT; | 
| Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 2163 | } | 
|  | 2164 |  | 
| Tim Kilbourn | 063ff53 | 2015-04-08 10:26:18 -0700 | [diff] [blame] | 2165 | // Determine whether the device has a mic. | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2166 | if (device->deviceHasMicLocked()) { | 
| Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 2167 | device->classes |= InputDeviceClass::MIC; | 
| Tim Kilbourn | 063ff53 | 2015-04-08 10:26:18 -0700 | [diff] [blame] | 2168 | } | 
|  | 2169 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2170 | // Determine whether the device is external or internal. | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2171 | if (device->isExternalDeviceLocked()) { | 
| Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 2172 | device->classes |= InputDeviceClass::EXTERNAL; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2173 | } | 
|  | 2174 |  | 
| Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 2175 | if (device->classes.any(InputDeviceClass::JOYSTICK | InputDeviceClass::DPAD) && | 
|  | 2176 | device->classes.test(InputDeviceClass::GAMEPAD)) { | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2177 | device->controllerNumber = getNextControllerNumberLocked(device->identifier.name); | 
|  | 2178 | device->setLedForControllerLocked(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2179 | } | 
|  | 2180 |  | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2181 | if (registerDeviceForEpollLocked(*device) != OK) { | 
| Siarhei Vishniakou | a4c502a | 2021-02-05 00:45:20 +0000 | [diff] [blame] | 2182 | return; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2183 | } | 
|  | 2184 |  | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2185 | device->configureFd(); | 
| Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 2186 |  | 
| Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 2187 | ALOGI("New device: id=%d, fd=%d, path='%s', name='%s', classes=%s, " | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 2188 | "configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s, ", | 
| Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 2189 | deviceId, fd, devicePath.c_str(), device->identifier.name.c_str(), | 
|  | 2190 | device->classes.string().c_str(), device->configurationFile.c_str(), | 
|  | 2191 | device->keyMap.keyLayoutFile.c_str(), device->keyMap.keyCharacterMapFile.c_str(), | 
|  | 2192 | toString(mBuiltInKeyboardId == deviceId)); | 
| Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 2193 |  | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2194 | addDeviceLocked(std::move(device)); | 
| Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 2195 | } | 
|  | 2196 |  | 
| Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 2197 | void EventHub::openVideoDeviceLocked(const std::string& devicePath) { | 
|  | 2198 | std::unique_ptr<TouchVideoDevice> videoDevice = TouchVideoDevice::create(devicePath); | 
|  | 2199 | if (!videoDevice) { | 
|  | 2200 | ALOGE("Could not create touch video device for %s. Ignoring", devicePath.c_str()); | 
|  | 2201 | return; | 
|  | 2202 | } | 
| Siarhei Vishniakou | ec7854a | 2018-12-14 16:52:34 -0800 | [diff] [blame] | 2203 | // Transfer ownership of this video device to a matching input device | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2204 | for (const auto& [id, device] : mDevices) { | 
| Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 2205 | if (tryAddVideoDeviceLocked(*device, videoDevice)) { | 
| Siarhei Vishniakou | f49608d | 2020-08-20 19:18:21 -0500 | [diff] [blame] | 2206 | return; // 'device' now owns 'videoDevice' | 
| Siarhei Vishniakou | ec7854a | 2018-12-14 16:52:34 -0800 | [diff] [blame] | 2207 | } | 
|  | 2208 | } | 
|  | 2209 |  | 
|  | 2210 | // Couldn't find a matching input device, so just add it to a temporary holding queue. | 
|  | 2211 | // A matching input device may appear later. | 
| Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 2212 | ALOGI("Adding video device %s to list of unattached video devices", | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 2213 | videoDevice->getName().c_str()); | 
| Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 2214 | mUnattachedVideoDevices.push_back(std::move(videoDevice)); | 
|  | 2215 | } | 
|  | 2216 |  | 
| Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 2217 | bool EventHub::tryAddVideoDeviceLocked(EventHub::Device& device, | 
|  | 2218 | std::unique_ptr<TouchVideoDevice>& videoDevice) { | 
| Siarhei Vishniakou | f49608d | 2020-08-20 19:18:21 -0500 | [diff] [blame] | 2219 | if (videoDevice->getName() != device.identifier.name) { | 
|  | 2220 | return false; | 
|  | 2221 | } | 
|  | 2222 | device.videoDevice = std::move(videoDevice); | 
|  | 2223 | if (device.enabled) { | 
|  | 2224 | registerVideoDeviceForEpollLocked(*device.videoDevice); | 
|  | 2225 | } | 
|  | 2226 | return true; | 
|  | 2227 | } | 
|  | 2228 |  | 
| Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 2229 | bool EventHub::isDeviceEnabled(int32_t deviceId) { | 
| Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 2230 | std::scoped_lock _l(mLock); | 
| Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 2231 | Device* device = getDeviceLocked(deviceId); | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2232 | if (device == nullptr) { | 
| Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 2233 | ALOGE("Invalid device id=%" PRId32 " provided to %s", deviceId, __func__); | 
|  | 2234 | return false; | 
|  | 2235 | } | 
|  | 2236 | return device->enabled; | 
|  | 2237 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2238 |  | 
| Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 2239 | status_t EventHub::enableDevice(int32_t deviceId) { | 
| Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 2240 | std::scoped_lock _l(mLock); | 
| Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 2241 | Device* device = getDeviceLocked(deviceId); | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2242 | if (device == nullptr) { | 
| Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 2243 | ALOGE("Invalid device id=%" PRId32 " provided to %s", deviceId, __func__); | 
|  | 2244 | return BAD_VALUE; | 
|  | 2245 | } | 
|  | 2246 | if (device->enabled) { | 
|  | 2247 | ALOGW("Duplicate call to %s, input device %" PRId32 " already enabled", __func__, deviceId); | 
|  | 2248 | return OK; | 
|  | 2249 | } | 
|  | 2250 | status_t result = device->enable(); | 
|  | 2251 | if (result != OK) { | 
|  | 2252 | ALOGE("Failed to enable device %" PRId32, deviceId); | 
|  | 2253 | return result; | 
|  | 2254 | } | 
|  | 2255 |  | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2256 | device->configureFd(); | 
| Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 2257 |  | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2258 | return registerDeviceForEpollLocked(*device); | 
| Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 2259 | } | 
|  | 2260 |  | 
|  | 2261 | status_t EventHub::disableDevice(int32_t deviceId) { | 
| Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 2262 | std::scoped_lock _l(mLock); | 
| Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 2263 | Device* device = getDeviceLocked(deviceId); | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2264 | if (device == nullptr) { | 
| Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 2265 | ALOGE("Invalid device id=%" PRId32 " provided to %s", deviceId, __func__); | 
|  | 2266 | return BAD_VALUE; | 
|  | 2267 | } | 
|  | 2268 | if (!device->enabled) { | 
|  | 2269 | ALOGW("Duplicate call to %s, input device already disabled", __func__); | 
|  | 2270 | return OK; | 
|  | 2271 | } | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2272 | unregisterDeviceFromEpollLocked(*device); | 
| Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 2273 | return device->disable(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2274 | } | 
|  | 2275 |  | 
|  | 2276 | void EventHub::createVirtualKeyboardLocked() { | 
|  | 2277 | InputDeviceIdentifier identifier; | 
|  | 2278 | identifier.name = "Virtual"; | 
|  | 2279 | identifier.uniqueId = "<virtual>"; | 
|  | 2280 | assignDescriptorLocked(identifier); | 
|  | 2281 |  | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2282 | std::unique_ptr<Device> device = | 
|  | 2283 | std::make_unique<Device>(-1, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, "<virtual>", | 
|  | 2284 | identifier); | 
| Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 2285 | device->classes = InputDeviceClass::KEYBOARD | InputDeviceClass::ALPHAKEY | | 
|  | 2286 | InputDeviceClass::DPAD | InputDeviceClass::VIRTUAL; | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2287 | device->loadKeyMapLocked(); | 
|  | 2288 | addDeviceLocked(std::move(device)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2289 | } | 
|  | 2290 |  | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2291 | void EventHub::addDeviceLocked(std::unique_ptr<Device> device) { | 
| Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 2292 | reportDeviceAddedForStatisticsLocked(device->identifier, device->classes); | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2293 | mOpeningDevices.push_back(std::move(device)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2294 | } | 
|  | 2295 |  | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2296 | int32_t EventHub::getNextControllerNumberLocked(const std::string& name) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2297 | if (mControllerNumbers.isFull()) { | 
|  | 2298 | ALOGI("Maximum number of controllers reached, assigning controller number 0 to device %s", | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2299 | name.c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2300 | return 0; | 
|  | 2301 | } | 
|  | 2302 | // Since the controller number 0 is reserved for non-controllers, translate all numbers up by | 
|  | 2303 | // one | 
|  | 2304 | return static_cast<int32_t>(mControllerNumbers.markFirstUnmarkedBit() + 1); | 
|  | 2305 | } | 
|  | 2306 |  | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2307 | void EventHub::releaseControllerNumberLocked(int32_t num) { | 
|  | 2308 | if (num > 0) { | 
|  | 2309 | mControllerNumbers.clearBit(static_cast<uint32_t>(num - 1)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2310 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2311 | } | 
|  | 2312 |  | 
| Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 2313 | void EventHub::closeDeviceByPathLocked(const std::string& devicePath) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2314 | Device* device = getDeviceByPathLocked(devicePath); | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2315 | if (device != nullptr) { | 
|  | 2316 | closeDeviceLocked(*device); | 
| Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 2317 | return; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2318 | } | 
| Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 2319 | ALOGV("Remove device: %s not found, device may already have been removed.", devicePath.c_str()); | 
| Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 2320 | } | 
|  | 2321 |  | 
| Siarhei Vishniakou | ec7854a | 2018-12-14 16:52:34 -0800 | [diff] [blame] | 2322 | /** | 
|  | 2323 | * Find the video device by filename, and close it. | 
|  | 2324 | * The video device is closed by path during an inotify event, where we don't have the | 
|  | 2325 | * additional context about the video device fd, or the associated input device. | 
|  | 2326 | */ | 
| Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 2327 | void EventHub::closeVideoDeviceByPathLocked(const std::string& devicePath) { | 
| Siarhei Vishniakou | ec7854a | 2018-12-14 16:52:34 -0800 | [diff] [blame] | 2328 | // A video device may be owned by an existing input device, or it may be stored in | 
|  | 2329 | // the mUnattachedVideoDevices queue. Check both locations. | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2330 | for (const auto& [id, device] : mDevices) { | 
| Siarhei Vishniakou | ec7854a | 2018-12-14 16:52:34 -0800 | [diff] [blame] | 2331 | if (device->videoDevice && device->videoDevice->getPath() == devicePath) { | 
| Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 2332 | unregisterVideoDeviceFromEpollLocked(*device->videoDevice); | 
| Siarhei Vishniakou | ec7854a | 2018-12-14 16:52:34 -0800 | [diff] [blame] | 2333 | device->videoDevice = nullptr; | 
|  | 2334 | return; | 
|  | 2335 | } | 
|  | 2336 | } | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 2337 | mUnattachedVideoDevices | 
|  | 2338 | .erase(std::remove_if(mUnattachedVideoDevices.begin(), mUnattachedVideoDevices.end(), | 
|  | 2339 | [&devicePath]( | 
|  | 2340 | const std::unique_ptr<TouchVideoDevice>& videoDevice) { | 
|  | 2341 | return videoDevice->getPath() == devicePath; | 
|  | 2342 | }), | 
|  | 2343 | mUnattachedVideoDevices.end()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2344 | } | 
|  | 2345 |  | 
|  | 2346 | void EventHub::closeAllDevicesLocked() { | 
| Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 2347 | mUnattachedVideoDevices.clear(); | 
| Siarhei Vishniakou | d549b25 | 2020-08-11 11:25:26 -0500 | [diff] [blame] | 2348 | while (!mDevices.empty()) { | 
|  | 2349 | closeDeviceLocked(*(mDevices.begin()->second)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2350 | } | 
|  | 2351 | } | 
|  | 2352 |  | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2353 | void EventHub::closeDeviceLocked(Device& device) { | 
|  | 2354 | ALOGI("Removed device: path=%s name=%s id=%d fd=%d classes=%s", device.path.c_str(), | 
|  | 2355 | device.identifier.name.c_str(), device.id, device.fd, device.classes.string().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2356 |  | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2357 | if (device.id == mBuiltInKeyboardId) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2358 | ALOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this", | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2359 | device.path.c_str(), mBuiltInKeyboardId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2360 | mBuiltInKeyboardId = NO_BUILT_IN_KEYBOARD; | 
|  | 2361 | } | 
|  | 2362 |  | 
| Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 2363 | unregisterDeviceFromEpollLocked(device); | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2364 | if (device.videoDevice) { | 
| Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 2365 | // This must be done after the video device is removed from epoll | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2366 | mUnattachedVideoDevices.push_back(std::move(device.videoDevice)); | 
| Siarhei Vishniakou | ec7854a | 2018-12-14 16:52:34 -0800 | [diff] [blame] | 2367 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2368 |  | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2369 | releaseControllerNumberLocked(device.controllerNumber); | 
| Siarhei Vishniakou | d549b25 | 2020-08-11 11:25:26 -0500 | [diff] [blame] | 2370 | device.controllerNumber = 0; | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2371 | device.close(); | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2372 | mClosingDevices.push_back(std::move(mDevices[device.id])); | 
| Siarhei Vishniakou | d549b25 | 2020-08-11 11:25:26 -0500 | [diff] [blame] | 2373 |  | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2374 | mDevices.erase(device.id); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2375 | } | 
|  | 2376 |  | 
|  | 2377 | status_t EventHub::readNotifyLocked() { | 
|  | 2378 | int res; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2379 | char event_buf[512]; | 
|  | 2380 | int event_size; | 
|  | 2381 | int event_pos = 0; | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 2382 | struct inotify_event* event; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2383 |  | 
|  | 2384 | ALOGV("EventHub::readNotify nfd: %d\n", mINotifyFd); | 
|  | 2385 | res = read(mINotifyFd, event_buf, sizeof(event_buf)); | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 2386 | if (res < (int)sizeof(*event)) { | 
|  | 2387 | if (errno == EINTR) return 0; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2388 | ALOGW("could not get event, %s\n", strerror(errno)); | 
|  | 2389 | return -1; | 
|  | 2390 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2391 |  | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 2392 | while (res >= (int)sizeof(*event)) { | 
|  | 2393 | event = (struct inotify_event*)(event_buf + event_pos); | 
|  | 2394 | if (event->len) { | 
| Usama Arif | 78eb975 | 2021-06-03 16:44:09 +0100 | [diff] [blame] | 2395 | if (event->wd == mDeviceInputWd) { | 
|  | 2396 | std::string filename = std::string(DEVICE_INPUT_PATH) + "/" + event->name; | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 2397 | if (event->mask & IN_CREATE) { | 
| Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 2398 | openDeviceLocked(filename); | 
| Siarhei Vishniakou | 951f362 | 2018-12-12 19:45:42 -0800 | [diff] [blame] | 2399 | } else { | 
|  | 2400 | ALOGI("Removing device '%s' due to inotify event\n", filename.c_str()); | 
| Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 2401 | closeDeviceByPathLocked(filename); | 
| Siarhei Vishniakou | 951f362 | 2018-12-12 19:45:42 -0800 | [diff] [blame] | 2402 | } | 
| Usama Arif | 78eb975 | 2021-06-03 16:44:09 +0100 | [diff] [blame] | 2403 | } else if (event->wd == mDeviceWd) { | 
| Siarhei Vishniakou | 951f362 | 2018-12-12 19:45:42 -0800 | [diff] [blame] | 2404 | if (isV4lTouchNode(event->name)) { | 
| Usama Arif | 78eb975 | 2021-06-03 16:44:09 +0100 | [diff] [blame] | 2405 | std::string filename = std::string(DEVICE_PATH) + "/" + event->name; | 
| Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 2406 | if (event->mask & IN_CREATE) { | 
|  | 2407 | openVideoDeviceLocked(filename); | 
|  | 2408 | } else { | 
|  | 2409 | ALOGI("Removing video device '%s' due to inotify event", filename.c_str()); | 
|  | 2410 | closeVideoDeviceByPathLocked(filename); | 
|  | 2411 | } | 
| Usama Arif | 78eb975 | 2021-06-03 16:44:09 +0100 | [diff] [blame] | 2412 | } else if (strcmp(event->name, "input") == 0 && event->mask & IN_CREATE) { | 
|  | 2413 | addDeviceInputInotify(); | 
| Siarhei Vishniakou | 951f362 | 2018-12-12 19:45:42 -0800 | [diff] [blame] | 2414 | } | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 2415 | } else { | 
| Siarhei Vishniakou | 951f362 | 2018-12-12 19:45:42 -0800 | [diff] [blame] | 2416 | LOG_ALWAYS_FATAL("Unexpected inotify event, wd = %i", event->wd); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2417 | } | 
|  | 2418 | } | 
|  | 2419 | event_size = sizeof(*event) + event->len; | 
|  | 2420 | res -= event_size; | 
|  | 2421 | event_pos += event_size; | 
|  | 2422 | } | 
|  | 2423 | return 0; | 
|  | 2424 | } | 
|  | 2425 |  | 
| Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 2426 | status_t EventHub::scanDirLocked(const std::string& dirname) { | 
|  | 2427 | for (const auto& entry : std::filesystem::directory_iterator(dirname)) { | 
|  | 2428 | openDeviceLocked(entry.path()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2429 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2430 | return 0; | 
|  | 2431 | } | 
|  | 2432 |  | 
| Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 2433 | /** | 
|  | 2434 | * Look for all dirname/v4l-touch* devices, and open them. | 
|  | 2435 | */ | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 2436 | status_t EventHub::scanVideoDirLocked(const std::string& dirname) { | 
| Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 2437 | for (const auto& entry : std::filesystem::directory_iterator(dirname)) { | 
|  | 2438 | if (isV4lTouchNode(entry.path())) { | 
|  | 2439 | ALOGI("Found touch video device %s", entry.path().c_str()); | 
|  | 2440 | openVideoDeviceLocked(entry.path()); | 
| Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 2441 | } | 
|  | 2442 | } | 
| Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 2443 | return OK; | 
|  | 2444 | } | 
|  | 2445 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2446 | void EventHub::requestReopenDevices() { | 
|  | 2447 | ALOGV("requestReopenDevices() called"); | 
|  | 2448 |  | 
| Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 2449 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2450 | mNeedToReopenDevices = true; | 
|  | 2451 | } | 
|  | 2452 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2453 | void EventHub::dump(std::string& dump) { | 
|  | 2454 | dump += "Event Hub State:\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2455 |  | 
|  | 2456 | { // acquire lock | 
| Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 2457 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2458 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2459 | dump += StringPrintf(INDENT "BuiltInKeyboardId: %d\n", mBuiltInKeyboardId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2460 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2461 | dump += INDENT "Devices:\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2462 |  | 
| Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 2463 | for (const auto& [id, device] : mDevices) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2464 | if (mBuiltInKeyboardId == device->id) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2465 | dump += StringPrintf(INDENT2 "%d: %s (aka device 0 - built-in keyboard)\n", | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 2466 | device->id, device->identifier.name.c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2467 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2468 | dump += StringPrintf(INDENT2 "%d: %s\n", device->id, | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 2469 | device->identifier.name.c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2470 | } | 
| Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 2471 | dump += StringPrintf(INDENT3 "Classes: %s\n", device->classes.string().c_str()); | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 2472 | dump += StringPrintf(INDENT3 "Path: %s\n", device->path.c_str()); | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2473 | dump += StringPrintf(INDENT3 "Enabled: %s\n", toString(device->enabled)); | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 2474 | dump += StringPrintf(INDENT3 "Descriptor: %s\n", device->identifier.descriptor.c_str()); | 
|  | 2475 | dump += StringPrintf(INDENT3 "Location: %s\n", device->identifier.location.c_str()); | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2476 | dump += StringPrintf(INDENT3 "ControllerNumber: %d\n", device->controllerNumber); | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 2477 | dump += StringPrintf(INDENT3 "UniqueId: %s\n", device->identifier.uniqueId.c_str()); | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2478 | dump += StringPrintf(INDENT3 "Identifier: bus=0x%04x, vendor=0x%04x, " | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 2479 | "product=0x%04x, version=0x%04x\n", | 
|  | 2480 | device->identifier.bus, device->identifier.vendor, | 
|  | 2481 | device->identifier.product, device->identifier.version); | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2482 | dump += StringPrintf(INDENT3 "KeyLayoutFile: %s\n", | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 2483 | device->keyMap.keyLayoutFile.c_str()); | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2484 | dump += StringPrintf(INDENT3 "KeyCharacterMapFile: %s\n", | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 2485 | device->keyMap.keyCharacterMapFile.c_str()); | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2486 | dump += StringPrintf(INDENT3 "ConfigurationFile: %s\n", | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 2487 | device->configurationFile.c_str()); | 
| Siarhei Vishniakou | ec7854a | 2018-12-14 16:52:34 -0800 | [diff] [blame] | 2488 | dump += INDENT3 "VideoDevice: "; | 
|  | 2489 | if (device->videoDevice) { | 
|  | 2490 | dump += device->videoDevice->dump() + "\n"; | 
|  | 2491 | } else { | 
|  | 2492 | dump += "<none>\n"; | 
|  | 2493 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2494 | } | 
| Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 2495 |  | 
|  | 2496 | dump += INDENT "Unattached video devices:\n"; | 
|  | 2497 | for (const std::unique_ptr<TouchVideoDevice>& videoDevice : mUnattachedVideoDevices) { | 
|  | 2498 | dump += INDENT2 + videoDevice->dump() + "\n"; | 
|  | 2499 | } | 
|  | 2500 | if (mUnattachedVideoDevices.empty()) { | 
|  | 2501 | dump += INDENT2 "<none>\n"; | 
|  | 2502 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2503 | } // release lock | 
|  | 2504 | } | 
|  | 2505 |  | 
|  | 2506 | void EventHub::monitor() { | 
|  | 2507 | // Acquire and release the lock to ensure that the event hub has not deadlocked. | 
| Chris Ye | 1c2e089 | 2020-11-30 21:41:44 -0800 | [diff] [blame] | 2508 | std::unique_lock<std::mutex> lock(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2509 | } | 
|  | 2510 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2511 | }; // namespace android |