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