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