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