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