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