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