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