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