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