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 | |
Prabir Pradhan | 4810866 | 2022-09-09 21:22:04 +0000 | [diff] [blame] | 17 | #pragma once |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 18 | |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 19 | #include <bitset> |
| 20 | #include <climits> |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 21 | #include <filesystem> |
Prabir Pradhan | 955b613 | 2023-07-14 19:44:34 +0000 | [diff] [blame] | 22 | #include <functional> |
| 23 | #include <map> |
Harry Cutts | ea73eaa | 2023-01-16 17:55:46 +0000 | [diff] [blame] | 24 | #include <ostream> |
| 25 | #include <string> |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 26 | #include <unordered_map> |
Prabir Pradhan | 5189478 | 2022-08-23 16:29:10 +0000 | [diff] [blame] | 27 | #include <utility> |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 28 | #include <vector> |
| 29 | |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 30 | #include <batteryservice/BatteryService.h> |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 31 | #include <ftl/flags.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 32 | #include <input/Input.h> |
| 33 | #include <input/InputDevice.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 34 | #include <input/KeyCharacterMap.h> |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 35 | #include <input/KeyLayoutMap.h> |
| 36 | #include <input/Keyboard.h> |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 37 | #include <input/PropertyMap.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 38 | #include <input/VirtualKeyMap.h> |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 39 | #include <linux/input.h> |
| 40 | #include <sys/epoll.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 41 | #include <utils/BitSet.h> |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 42 | #include <utils/Errors.h> |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 43 | #include <utils/List.h> |
| 44 | #include <utils/Log.h> |
| 45 | #include <utils/Mutex.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 46 | |
Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 47 | #include "TouchVideoDevice.h" |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 48 | #include "VibrationElement.h" |
Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 49 | |
Prabir Pradhan | 952e65b | 2022-06-23 17:49:55 +0000 | [diff] [blame] | 50 | struct inotify_event; |
| 51 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 52 | namespace android { |
| 53 | |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 54 | /* Number of colors : {red, green, blue} */ |
| 55 | static constexpr size_t COLOR_NUM = 3; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 56 | /* |
| 57 | * A raw event as retrieved from the EventHub. |
| 58 | */ |
| 59 | struct RawEvent { |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 60 | // Time when the event happened |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 61 | nsecs_t when; |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 62 | // Time when the event was read by EventHub. Only populated for input events. |
| 63 | // For other events (device added/removed/etc), this value is undefined and should not be read. |
| 64 | nsecs_t readTime; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 65 | int32_t deviceId; |
| 66 | int32_t type; |
| 67 | int32_t code; |
| 68 | int32_t value; |
| 69 | }; |
| 70 | |
| 71 | /* Describes an absolute axis. */ |
| 72 | struct RawAbsoluteAxisInfo { |
Prabir Pradhan | d6ccedb | 2022-09-27 21:04:06 +0000 | [diff] [blame] | 73 | bool valid{false}; // true if the information is valid, false otherwise |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 74 | |
Prabir Pradhan | d6ccedb | 2022-09-27 21:04:06 +0000 | [diff] [blame] | 75 | int32_t minValue{}; // minimum value |
| 76 | int32_t maxValue{}; // maximum value |
| 77 | int32_t flat{}; // center flat position, eg. flat == 8 means center is between -8 and 8 |
| 78 | int32_t fuzz{}; // error tolerance, eg. fuzz == 4 means value is +/- 4 due to noise |
| 79 | int32_t resolution{}; // resolution in units per mm or radians per mm |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 80 | |
Prabir Pradhan | d6ccedb | 2022-09-27 21:04:06 +0000 | [diff] [blame] | 81 | inline void clear() { *this = RawAbsoluteAxisInfo(); } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 82 | }; |
| 83 | |
Harry Cutts | ea73eaa | 2023-01-16 17:55:46 +0000 | [diff] [blame] | 84 | std::ostream& operator<<(std::ostream& out, const RawAbsoluteAxisInfo& info); |
| 85 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 86 | /* |
| 87 | * Input device classes. |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 88 | * |
| 89 | * These classes are duplicated in rust side here: /frameworks/native/libs/input/rust/input.rs. |
| 90 | * If any new classes are added, we need to add them in rust input side too. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 91 | */ |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 92 | enum class InputDeviceClass : uint32_t { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 93 | /* The input device is a keyboard or has buttons. */ |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 94 | KEYBOARD = android::os::IInputConstants::DEVICE_CLASS_KEYBOARD, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 95 | |
| 96 | /* The input device is an alpha-numeric keyboard (not just a dial pad). */ |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 97 | ALPHAKEY = android::os::IInputConstants::DEVICE_CLASS_ALPHAKEY, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 98 | |
| 99 | /* The input device is a touchscreen or a touchpad (either single-touch or multi-touch). */ |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 100 | TOUCH = android::os::IInputConstants::DEVICE_CLASS_TOUCH, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 101 | |
| 102 | /* The input device is a cursor device such as a trackball or mouse. */ |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 103 | CURSOR = android::os::IInputConstants::DEVICE_CLASS_CURSOR, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 104 | |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 105 | /* The input device is a multi-touch touchscreen or touchpad. */ |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 106 | TOUCH_MT = android::os::IInputConstants::DEVICE_CLASS_TOUCH_MT, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 107 | |
| 108 | /* The input device is a directional pad (implies keyboard, has DPAD keys). */ |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 109 | DPAD = android::os::IInputConstants::DEVICE_CLASS_DPAD, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 110 | |
| 111 | /* The input device is a gamepad (implies keyboard, has BUTTON keys). */ |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 112 | GAMEPAD = android::os::IInputConstants::DEVICE_CLASS_GAMEPAD, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 113 | |
| 114 | /* The input device has switches. */ |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 115 | SWITCH = android::os::IInputConstants::DEVICE_CLASS_SWITCH, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 116 | |
| 117 | /* The input device is a joystick (implies gamepad, has joystick absolute axes). */ |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 118 | JOYSTICK = android::os::IInputConstants::DEVICE_CLASS_JOYSTICK, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 119 | |
| 120 | /* The input device has a vibrator (supports FF_RUMBLE). */ |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 121 | VIBRATOR = android::os::IInputConstants::DEVICE_CLASS_VIBRATOR, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 122 | |
Tim Kilbourn | 063ff53 | 2015-04-08 10:26:18 -0700 | [diff] [blame] | 123 | /* The input device has a microphone. */ |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 124 | MIC = android::os::IInputConstants::DEVICE_CLASS_MIC, |
Tim Kilbourn | 063ff53 | 2015-04-08 10:26:18 -0700 | [diff] [blame] | 125 | |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 126 | /* The input device is an external stylus (has data we want to fuse with touch data). */ |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 127 | EXTERNAL_STYLUS = android::os::IInputConstants::DEVICE_CLASS_EXTERNAL_STYLUS, |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 128 | |
Prashant Malani | 1941ff5 | 2015-08-11 18:29:28 -0700 | [diff] [blame] | 129 | /* The input device has a rotary encoder */ |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 130 | ROTARY_ENCODER = android::os::IInputConstants::DEVICE_CLASS_ROTARY_ENCODER, |
Prashant Malani | 1941ff5 | 2015-08-11 18:29:28 -0700 | [diff] [blame] | 131 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 132 | /* The input device has a sensor like accelerometer, gyro, etc */ |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 133 | SENSOR = android::os::IInputConstants::DEVICE_CLASS_SENSOR, |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 134 | |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 135 | /* The input device has a battery */ |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 136 | BATTERY = android::os::IInputConstants::DEVICE_CLASS_BATTERY, |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 137 | |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 138 | /* The input device has sysfs controllable lights */ |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 139 | LIGHT = android::os::IInputConstants::DEVICE_CLASS_LIGHT, |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 140 | |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 141 | /* The input device is a touchpad, requiring an on-screen cursor. */ |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 142 | TOUCHPAD = android::os::IInputConstants::DEVICE_CLASS_TOUCHPAD, |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 143 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 144 | /* The input device is virtual (not a real device, not part of UI configuration). */ |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 145 | VIRTUAL = android::os::IInputConstants::DEVICE_CLASS_VIRTUAL, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 146 | |
| 147 | /* The input device is external (not built-in). */ |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 148 | EXTERNAL = android::os::IInputConstants::DEVICE_CLASS_EXTERNAL, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 149 | }; |
| 150 | |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 151 | enum class SysfsClass : uint32_t { |
| 152 | POWER_SUPPLY = 0, |
| 153 | LEDS = 1, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 154 | |
| 155 | ftl_last = LEDS |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | enum class LightColor : uint32_t { |
| 159 | RED = 0, |
| 160 | GREEN = 1, |
| 161 | BLUE = 2, |
| 162 | }; |
| 163 | |
| 164 | enum class InputLightClass : uint32_t { |
| 165 | /* The input light has brightness node. */ |
| 166 | BRIGHTNESS = 0x00000001, |
| 167 | /* The input light has red name. */ |
| 168 | RED = 0x00000002, |
| 169 | /* The input light has green name. */ |
| 170 | GREEN = 0x00000004, |
| 171 | /* The input light has blue name. */ |
| 172 | BLUE = 0x00000008, |
| 173 | /* The input light has global name. */ |
| 174 | GLOBAL = 0x00000010, |
| 175 | /* The input light has multi index node. */ |
| 176 | MULTI_INDEX = 0x00000020, |
| 177 | /* The input light has multi intensity node. */ |
| 178 | MULTI_INTENSITY = 0x00000040, |
| 179 | /* The input light has max brightness node. */ |
| 180 | MAX_BRIGHTNESS = 0x00000080, |
Vaibhav Devmurari | 82b37d6 | 2022-09-12 13:36:48 +0000 | [diff] [blame] | 181 | /* The input light has kbd_backlight name */ |
| 182 | KEYBOARD_BACKLIGHT = 0x00000100, |
DingYong | 99f2c3c | 2023-12-20 15:46:06 +0800 | [diff] [blame] | 183 | /* The input light has mic_mute name */ |
| 184 | KEYBOARD_MIC_MUTE = 0x00000200, |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 185 | }; |
| 186 | |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 187 | enum class InputBatteryClass : uint32_t { |
| 188 | /* The input device battery has capacity node. */ |
| 189 | CAPACITY = 0x00000001, |
| 190 | /* The input device battery has capacity_level node. */ |
| 191 | CAPACITY_LEVEL = 0x00000002, |
| 192 | /* The input device battery has status node. */ |
| 193 | STATUS = 0x00000004, |
| 194 | }; |
| 195 | |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 196 | /* Describes a raw light. */ |
| 197 | struct RawLightInfo { |
| 198 | int32_t id; |
| 199 | std::string name; |
| 200 | std::optional<int32_t> maxBrightness; |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 201 | ftl::Flags<InputLightClass> flags; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 202 | std::array<int32_t, COLOR_NUM> rgbIndex; |
| 203 | std::filesystem::path path; |
Prabir Pradhan | edeec3b | 2022-08-26 22:33:55 +0000 | [diff] [blame] | 204 | |
| 205 | bool operator==(const RawLightInfo&) const = default; |
| 206 | bool operator!=(const RawLightInfo&) const = default; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 207 | }; |
| 208 | |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 209 | /* Describes a raw battery. */ |
| 210 | struct RawBatteryInfo { |
| 211 | int32_t id; |
| 212 | std::string name; |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 213 | ftl::Flags<InputBatteryClass> flags; |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 214 | std::filesystem::path path; |
Prabir Pradhan | edeec3b | 2022-08-26 22:33:55 +0000 | [diff] [blame] | 215 | |
| 216 | bool operator==(const RawBatteryInfo&) const = default; |
| 217 | bool operator!=(const RawBatteryInfo&) const = default; |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 218 | }; |
| 219 | |
Vaibhav Devmurari | 7fb4113 | 2023-01-02 13:30:26 +0000 | [diff] [blame] | 220 | /* Layout information associated with the device */ |
| 221 | struct RawLayoutInfo { |
| 222 | std::string languageTag; |
| 223 | std::string layoutType; |
| 224 | |
| 225 | bool operator==(const RawLayoutInfo&) const = default; |
| 226 | bool operator!=(const RawLayoutInfo&) const = default; |
| 227 | }; |
| 228 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 229 | /* |
| 230 | * Gets the class that owns an axis, in cases where multiple classes might claim |
| 231 | * the same axis for different purposes. |
| 232 | */ |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 233 | extern ftl::Flags<InputDeviceClass> getAbsAxisUsage(int32_t axis, |
| 234 | ftl::Flags<InputDeviceClass> deviceClasses); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 235 | |
| 236 | /* |
| 237 | * Grand Central Station for events. |
| 238 | * |
| 239 | * The event hub aggregates input events received across all known input |
| 240 | * devices on the system, including devices that may be emulated by the simulator |
| 241 | * environment. In addition, the event hub generates fake input events to indicate |
| 242 | * when devices are added or removed. |
| 243 | * |
| 244 | * The event hub provides a stream of input events (via the getEvent function). |
| 245 | * It also supports querying the current actual state of input devices such as identifying |
| 246 | * which keys are currently down. Finally, the event hub keeps track of the capabilities of |
| 247 | * individual input devices, such as their class and the set of key codes that they support. |
| 248 | */ |
Siarhei Vishniakou | 3bc7e09 | 2019-07-24 17:43:30 -0700 | [diff] [blame] | 249 | class EventHubInterface { |
| 250 | public: |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 251 | EventHubInterface() {} |
| 252 | virtual ~EventHubInterface() {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 253 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 254 | // Synthetic raw event type codes produced when devices are added or removed. |
| 255 | enum { |
| 256 | // Sent when a device is added. |
| 257 | DEVICE_ADDED = 0x10000000, |
| 258 | // Sent when a device is removed. |
| 259 | DEVICE_REMOVED = 0x20000000, |
| 260 | // Sent when all added/removed devices from the most recent scan have been reported. |
| 261 | // This event is always sent at least once. |
| 262 | FINISHED_DEVICE_SCAN = 0x30000000, |
| 263 | |
| 264 | FIRST_SYNTHETIC_EVENT = DEVICE_ADDED, |
| 265 | }; |
| 266 | |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 267 | virtual ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 268 | |
| 269 | virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const = 0; |
| 270 | |
| 271 | virtual int32_t getDeviceControllerNumber(int32_t deviceId) const = 0; |
| 272 | |
Harry Cutts | c34f758 | 2023-03-07 16:23:30 +0000 | [diff] [blame] | 273 | /** |
| 274 | * Get the PropertyMap for the provided EventHub device, if available. |
| 275 | * This acquires the device lock, so a copy is returned rather than the raw pointer |
| 276 | * to the device's PropertyMap. A std::nullopt may be returned if the device could |
| 277 | * not be found, or if it doesn't have any configuration. |
| 278 | */ |
| 279 | virtual std::optional<PropertyMap> getConfiguration(int32_t deviceId) const = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 280 | |
| 281 | virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis, |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 282 | RawAbsoluteAxisInfo* outAxisInfo) const = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 283 | |
| 284 | virtual bool hasRelativeAxis(int32_t deviceId, int axis) const = 0; |
| 285 | |
| 286 | virtual bool hasInputProperty(int32_t deviceId, int property) const = 0; |
| 287 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 288 | virtual bool hasMscEvent(int32_t deviceId, int mscEvent) const = 0; |
| 289 | |
Vaibhav Devmurari | cbba14c | 2022-10-10 16:54:49 +0000 | [diff] [blame] | 290 | virtual void addKeyRemapping(int32_t deviceId, int32_t fromKeyCode, |
| 291 | int32_t toKeyCode) const = 0; |
| 292 | |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 293 | virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, |
| 294 | int32_t metaState, int32_t* outKeycode, int32_t* outMetaState, |
| 295 | uint32_t* outFlags) const = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 296 | |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 297 | virtual status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 298 | |
| 299 | // Sets devices that are excluded from opening. |
| 300 | // This can be used to ignore input devices for sensors. |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 301 | virtual void setExcludedDevices(const std::vector<std::string>& devices) = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 302 | |
| 303 | /* |
| 304 | * Wait for events to become available and returns them. |
| 305 | * After returning, the EventHub holds onto a wake lock until the next call to getEvent. |
| 306 | * This ensures that the device will not go to sleep while the event is being processed. |
| 307 | * If the device needs to remain awake longer than that, then the caller is responsible |
| 308 | * for taking care of it (say, by poking the power manager user activity timer). |
| 309 | * |
| 310 | * The timeout is advisory only. If the device is asleep, it will not wake just to |
| 311 | * service the timeout. |
| 312 | * |
| 313 | * Returns the number of events obtained, or 0 if the timeout expired. |
| 314 | */ |
Siarhei Vishniakou | 7b3ea0b | 2022-09-16 14:23:20 -0700 | [diff] [blame] | 315 | virtual std::vector<RawEvent> getEvents(int timeoutMillis) = 0; |
Siarhei Vishniakou | add8929 | 2018-12-13 19:23:36 -0800 | [diff] [blame] | 316 | virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) = 0; |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 317 | virtual base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor( |
| 318 | int32_t deviceId, int32_t absCode) const = 0; |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 319 | // Raw batteries are sysfs power_supply nodes we found from the EventHub device sysfs node, |
| 320 | // containing the raw info of the sysfs node structure. |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 321 | virtual std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const = 0; |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 322 | virtual std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 323 | int32_t BatteryId) const = 0; |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 324 | |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 325 | // Raw lights are sysfs led light nodes we found from the EventHub device sysfs node, |
| 326 | // containing the raw info of the sysfs node structure. |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 327 | virtual std::vector<int32_t> getRawLightIds(int32_t deviceId) const = 0; |
| 328 | virtual std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, |
| 329 | int32_t lightId) const = 0; |
| 330 | virtual std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const = 0; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 331 | virtual void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) = 0; |
| 332 | virtual std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities( |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 333 | int32_t deviceId, int32_t lightId) const = 0; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 334 | virtual void setLightIntensities(int32_t deviceId, int32_t lightId, |
| 335 | std::unordered_map<LightColor, int32_t> intensities) = 0; |
Vaibhav Devmurari | 7fb4113 | 2023-01-02 13:30:26 +0000 | [diff] [blame] | 336 | /* Query Layout info associated with the input device. */ |
| 337 | virtual std::optional<RawLayoutInfo> getRawLayoutInfo(int32_t deviceId) const = 0; |
Vaibhav Devmurari | dd82b8e | 2022-08-16 15:34:01 +0000 | [diff] [blame] | 338 | /* Query current input state. */ |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 339 | virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0; |
| 340 | virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0; |
| 341 | virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0; |
| 342 | virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis, |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 343 | int32_t* outValue) const = 0; |
Arpit Singh | 4b4a457 | 2023-11-24 18:19:56 +0000 | [diff] [blame] | 344 | /* Query Multi-Touch slot values for an axis. Returns error or an 1 indexed array of size |
| 345 | * (slotCount + 1). The value at the 0 index is set to queried axis. */ |
| 346 | virtual base::Result<std::vector<int32_t>> getMtSlotValues(int32_t deviceId, int32_t axis, |
| 347 | size_t slotCount) const = 0; |
Philip Junker | 4af3b3d | 2021-12-14 10:36:55 +0100 | [diff] [blame] | 348 | virtual int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 349 | |
| 350 | /* |
| 351 | * Examine key input devices for specific framework keycode support |
| 352 | */ |
Siarhei Vishniakou | 7400794 | 2022-06-13 13:57:47 -0700 | [diff] [blame] | 353 | virtual bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes, |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 354 | uint8_t* outFlags) const = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 355 | |
| 356 | virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const = 0; |
Arthur Hung | cb40a00 | 2021-08-03 14:31:01 +0000 | [diff] [blame] | 357 | virtual bool hasKeyCode(int32_t deviceId, int32_t keyCode) const = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 358 | |
| 359 | /* LED related functions expect Android LED constants, not scan codes or HID usages */ |
| 360 | virtual bool hasLed(int32_t deviceId, int32_t led) const = 0; |
| 361 | virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0; |
| 362 | |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 363 | virtual void getVirtualKeyDefinitions( |
| 364 | int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 365 | |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 366 | virtual const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const = 0; |
| 367 | virtual bool setKeyboardLayoutOverlay(int32_t deviceId, |
| 368 | std::shared_ptr<KeyCharacterMap> map) = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 369 | |
| 370 | /* Control the vibrator. */ |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 371 | virtual void vibrate(int32_t deviceId, const VibrationElement& effect) = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 372 | virtual void cancelVibrate(int32_t deviceId) = 0; |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 373 | virtual std::vector<int32_t> getVibratorIds(int32_t deviceId) const = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 374 | |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 375 | /* Query battery level. */ |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 376 | virtual std::optional<int32_t> getBatteryCapacity(int32_t deviceId, |
| 377 | int32_t batteryId) const = 0; |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 378 | |
| 379 | /* Query battery status. */ |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 380 | virtual std::optional<int32_t> getBatteryStatus(int32_t deviceId, int32_t batteryId) const = 0; |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 381 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 382 | /* Requests the EventHub to reopen all input devices on the next call to getEvents(). */ |
| 383 | virtual void requestReopenDevices() = 0; |
| 384 | |
| 385 | /* Wakes up getEvents() if it is blocked on a read. */ |
| 386 | virtual void wake() = 0; |
| 387 | |
| 388 | /* Dump EventHub state to a string. */ |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 389 | virtual void dump(std::string& dump) const = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 390 | |
| 391 | /* Called by the heatbeat to ensures that the reader has not deadlocked. */ |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 392 | virtual void monitor() const = 0; |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 393 | |
| 394 | /* Return true if the device is enabled. */ |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 395 | virtual bool isDeviceEnabled(int32_t deviceId) const = 0; |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 396 | |
| 397 | /* Enable an input device */ |
| 398 | virtual status_t enableDevice(int32_t deviceId) = 0; |
| 399 | |
| 400 | /* Disable an input device. Closes file descriptor to that device. */ |
| 401 | virtual status_t disableDevice(int32_t deviceId) = 0; |
Vaibhav Devmurari | 5fc7d85 | 2023-03-17 18:43:33 +0000 | [diff] [blame] | 402 | |
| 403 | /* Sysfs node changed. Reopen the Eventhub device if any new Peripheral like Light, Battery, |
| 404 | * etc. is detected. */ |
| 405 | virtual void sysfsNodeChanged(const std::string& sysfsNodePath) = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 406 | }; |
| 407 | |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 408 | template <std::size_t BITS> |
| 409 | class BitArray { |
| 410 | /* Array element type and vector of element type. */ |
| 411 | using Element = std::uint32_t; |
| 412 | /* Number of bits in each BitArray element. */ |
| 413 | static constexpr size_t WIDTH = sizeof(Element) * CHAR_BIT; |
| 414 | /* Number of elements to represent a bit array of the specified size of bits. */ |
| 415 | static constexpr size_t COUNT = (BITS + WIDTH - 1) / WIDTH; |
| 416 | |
| 417 | public: |
| 418 | /* BUFFER type declaration for BitArray */ |
| 419 | using Buffer = std::array<Element, COUNT>; |
| 420 | /* To tell if a bit is set in array, it selects an element from the array, and test |
| 421 | * if the relevant bit set. |
| 422 | * Note the parameter "bit" is an index to the bit, 0 <= bit < BITS. |
| 423 | */ |
| 424 | inline bool test(size_t bit) const { |
Prabir Pradhan | 9762ac9 | 2023-07-13 21:45:12 +0000 | [diff] [blame] | 425 | return (bit < BITS) && mData[bit / WIDTH].test(bit % WIDTH); |
| 426 | } |
| 427 | /* Sets the given bit in the bit array to given value. |
| 428 | * Returns true if the given bit is a valid index and thus was set successfully. |
| 429 | */ |
| 430 | inline bool set(size_t bit, bool value) { |
| 431 | if (bit >= BITS) { |
| 432 | return false; |
| 433 | } |
| 434 | mData[bit / WIDTH].set(bit % WIDTH, value); |
| 435 | return true; |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 436 | } |
| 437 | /* Returns total number of bytes needed for the array */ |
| 438 | inline size_t bytes() { return (BITS + CHAR_BIT - 1) / CHAR_BIT; } |
| 439 | /* Returns true if array contains any non-zero bit from the range defined by start and end |
| 440 | * bit index [startIndex, endIndex). |
| 441 | */ |
| 442 | bool any(size_t startIndex, size_t endIndex) { |
| 443 | if (startIndex >= endIndex || startIndex > BITS || endIndex > BITS + 1) { |
| 444 | ALOGE("Invalid start/end index. start = %zu, end = %zu, total bits = %zu", startIndex, |
| 445 | endIndex, BITS); |
| 446 | return false; |
| 447 | } |
| 448 | size_t se = startIndex / WIDTH; // Start of element |
| 449 | size_t ee = endIndex / WIDTH; // End of element |
| 450 | size_t si = startIndex % WIDTH; // Start index in start element |
| 451 | size_t ei = endIndex % WIDTH; // End index in end element |
| 452 | // Need to check first unaligned bitset for any non zero bit |
| 453 | if (si > 0) { |
| 454 | size_t nBits = se == ee ? ei - si : WIDTH - si; |
| 455 | // Generate the mask of interested bit range |
| 456 | Element mask = ((1 << nBits) - 1) << si; |
| 457 | if (mData[se++].to_ulong() & mask) { |
| 458 | return true; |
| 459 | } |
| 460 | } |
| 461 | // Check whole bitset for any bit set |
| 462 | for (; se < ee; se++) { |
| 463 | if (mData[se].any()) { |
| 464 | return true; |
| 465 | } |
| 466 | } |
| 467 | // Need to check last unaligned bitset for any non zero bit |
| 468 | if (ei > 0 && se <= ee) { |
| 469 | // Generate the mask of interested bit range |
| 470 | Element mask = (1 << ei) - 1; |
| 471 | if (mData[se].to_ulong() & mask) { |
| 472 | return true; |
| 473 | } |
| 474 | } |
| 475 | return false; |
| 476 | } |
| 477 | /* Load bit array values from buffer */ |
| 478 | void loadFromBuffer(const Buffer& buffer) { |
| 479 | for (size_t i = 0; i < COUNT; i++) { |
| 480 | mData[i] = std::bitset<WIDTH>(buffer[i]); |
| 481 | } |
| 482 | } |
Prabir Pradhan | 955b613 | 2023-07-14 19:44:34 +0000 | [diff] [blame] | 483 | /* Dump the indices in the bit array that are set. */ |
| 484 | inline std::string dumpSetIndices(std::string separator, |
| 485 | std::function<std::string(size_t /*index*/)> format) { |
| 486 | std::string dmp; |
| 487 | for (size_t i = 0; i < BITS; i++) { |
| 488 | if (test(i)) { |
| 489 | if (!dmp.empty()) { |
| 490 | dmp += separator; |
| 491 | } |
| 492 | dmp += format(i); |
| 493 | } |
| 494 | } |
| 495 | return dmp.empty() ? "<none>" : dmp; |
| 496 | } |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 497 | |
| 498 | private: |
| 499 | std::array<std::bitset<WIDTH>, COUNT> mData; |
| 500 | }; |
| 501 | |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 502 | class EventHub : public EventHubInterface { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 503 | public: |
| 504 | EventHub(); |
| 505 | |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 506 | ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 507 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 508 | InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 509 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 510 | int32_t getDeviceControllerNumber(int32_t deviceId) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 511 | |
Harry Cutts | c34f758 | 2023-03-07 16:23:30 +0000 | [diff] [blame] | 512 | std::optional<PropertyMap> getConfiguration(int32_t deviceId) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 513 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 514 | status_t getAbsoluteAxisInfo(int32_t deviceId, int axis, |
| 515 | RawAbsoluteAxisInfo* outAxisInfo) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 516 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 517 | bool hasRelativeAxis(int32_t deviceId, int axis) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 518 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 519 | bool hasInputProperty(int32_t deviceId, int property) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 520 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 521 | bool hasMscEvent(int32_t deviceId, int mscEvent) const override final; |
| 522 | |
Vaibhav Devmurari | cbba14c | 2022-10-10 16:54:49 +0000 | [diff] [blame] | 523 | void addKeyRemapping(int32_t deviceId, int32_t fromKeyCode, |
| 524 | int32_t toKeyCode) const override final; |
| 525 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 526 | status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState, |
| 527 | int32_t* outKeycode, int32_t* outMetaState, |
| 528 | uint32_t* outFlags) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 529 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 530 | status_t mapAxis(int32_t deviceId, int32_t scanCode, |
| 531 | AxisInfo* outAxisInfo) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 532 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 533 | base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor( |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 534 | int32_t deviceId, int32_t absCode) const override final; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 535 | |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 536 | std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override final; |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 537 | std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 538 | int32_t BatteryId) const override final; |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 539 | |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 540 | std::vector<int32_t> getRawLightIds(int32_t deviceId) const override final; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 541 | |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 542 | std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, |
| 543 | int32_t lightId) const override final; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 544 | |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 545 | std::optional<int32_t> getLightBrightness(int32_t deviceId, |
| 546 | int32_t lightId) const override final; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 547 | void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override final; |
| 548 | std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities( |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 549 | int32_t deviceId, int32_t lightId) const override final; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 550 | void setLightIntensities(int32_t deviceId, int32_t lightId, |
| 551 | std::unordered_map<LightColor, int32_t> intensities) override final; |
| 552 | |
Vaibhav Devmurari | 7fb4113 | 2023-01-02 13:30:26 +0000 | [diff] [blame] | 553 | std::optional<RawLayoutInfo> getRawLayoutInfo(int32_t deviceId) const override final; |
Vaibhav Devmurari | dd82b8e | 2022-08-16 15:34:01 +0000 | [diff] [blame] | 554 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 555 | void setExcludedDevices(const std::vector<std::string>& devices) override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 556 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 557 | int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override final; |
| 558 | int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override final; |
| 559 | int32_t getSwitchState(int32_t deviceId, int32_t sw) const override final; |
Philip Junker | 4af3b3d | 2021-12-14 10:36:55 +0100 | [diff] [blame] | 560 | int32_t getKeyCodeForKeyLocation(int32_t deviceId, |
| 561 | int32_t locationKeyCode) const override final; |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 562 | status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis, |
| 563 | int32_t* outValue) const override final; |
Arpit Singh | 4b4a457 | 2023-11-24 18:19:56 +0000 | [diff] [blame] | 564 | base::Result<std::vector<int32_t>> getMtSlotValues(int32_t deviceId, int32_t axis, |
| 565 | size_t slotCount) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 566 | |
Siarhei Vishniakou | 7400794 | 2022-06-13 13:57:47 -0700 | [diff] [blame] | 567 | bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes, |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 568 | uint8_t* outFlags) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 569 | |
Siarhei Vishniakou | 7b3ea0b | 2022-09-16 14:23:20 -0700 | [diff] [blame] | 570 | std::vector<RawEvent> getEvents(int timeoutMillis) override final; |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 571 | std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 572 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 573 | bool hasScanCode(int32_t deviceId, int32_t scanCode) const override final; |
Arthur Hung | cb40a00 | 2021-08-03 14:31:01 +0000 | [diff] [blame] | 574 | bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override final; |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 575 | bool hasLed(int32_t deviceId, int32_t led) const override final; |
| 576 | void setLedState(int32_t deviceId, int32_t led, bool on) override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 577 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 578 | void getVirtualKeyDefinitions( |
| 579 | int32_t deviceId, |
| 580 | std::vector<VirtualKeyDefinition>& outVirtualKeys) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 581 | |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 582 | const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap( |
| 583 | int32_t deviceId) const override final; |
| 584 | bool setKeyboardLayoutOverlay(int32_t deviceId, |
| 585 | std::shared_ptr<KeyCharacterMap> map) override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 586 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 587 | void vibrate(int32_t deviceId, const VibrationElement& effect) override final; |
| 588 | void cancelVibrate(int32_t deviceId) override final; |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 589 | std::vector<int32_t> getVibratorIds(int32_t deviceId) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 590 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 591 | void requestReopenDevices() override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 592 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 593 | void wake() override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 594 | |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 595 | void dump(std::string& dump) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 596 | |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 597 | void monitor() const override final; |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 598 | |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 599 | std::optional<int32_t> getBatteryCapacity(int32_t deviceId, |
| 600 | int32_t batteryId) const override final; |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 601 | |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 602 | std::optional<int32_t> getBatteryStatus(int32_t deviceId, |
| 603 | int32_t batteryId) const override final; |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 604 | |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 605 | bool isDeviceEnabled(int32_t deviceId) const override final; |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 606 | |
| 607 | status_t enableDevice(int32_t deviceId) override final; |
| 608 | |
| 609 | status_t disableDevice(int32_t deviceId) override final; |
| 610 | |
Vaibhav Devmurari | 5fc7d85 | 2023-03-17 18:43:33 +0000 | [diff] [blame] | 611 | void sysfsNodeChanged(const std::string& sysfsNodePath) override final; |
| 612 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 613 | ~EventHub() override; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 614 | |
| 615 | private: |
Prabir Pradhan | cb42b47 | 2022-08-23 16:01:19 +0000 | [diff] [blame] | 616 | // Holds information about the sysfs device associated with the Device. |
Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 617 | struct AssociatedDevice { |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 618 | // The sysfs root path of the misc device. |
| 619 | std::filesystem::path sysfsRootPath; |
Prabir Pradhan | cb42b47 | 2022-08-23 16:01:19 +0000 | [diff] [blame] | 620 | std::unordered_map<int32_t /*batteryId*/, RawBatteryInfo> batteryInfos; |
| 621 | std::unordered_map<int32_t /*lightId*/, RawLightInfo> lightInfos; |
Vaibhav Devmurari | 7fb4113 | 2023-01-02 13:30:26 +0000 | [diff] [blame] | 622 | std::optional<RawLayoutInfo> layoutInfo; |
Prabir Pradhan | edeec3b | 2022-08-26 22:33:55 +0000 | [diff] [blame] | 623 | |
Vaibhav Devmurari | 5fc7d85 | 2023-03-17 18:43:33 +0000 | [diff] [blame] | 624 | bool isChanged() const; |
Prabir Pradhan | edeec3b | 2022-08-26 22:33:55 +0000 | [diff] [blame] | 625 | bool operator==(const AssociatedDevice&) const = default; |
| 626 | bool operator!=(const AssociatedDevice&) const = default; |
| 627 | std::string dump() const; |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 628 | }; |
| 629 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 630 | struct Device { |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 631 | int fd; // may be -1 if device is closed |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 632 | const int32_t id; |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 633 | const std::string path; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 634 | const InputDeviceIdentifier identifier; |
| 635 | |
Siarhei Vishniakou | ec7854a | 2018-12-14 16:52:34 -0800 | [diff] [blame] | 636 | std::unique_ptr<TouchVideoDevice> videoDevice; |
| 637 | |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 638 | ftl::Flags<InputDeviceClass> classes; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 639 | |
Prabir Pradhan | f44ab48 | 2023-07-20 20:46:09 +0000 | [diff] [blame] | 640 | BitArray<KEY_CNT> keyBitmask; |
| 641 | BitArray<KEY_CNT> keyState; |
| 642 | BitArray<REL_CNT> relBitmask; |
| 643 | BitArray<SW_CNT> swBitmask; |
| 644 | BitArray<SW_CNT> swState; |
| 645 | BitArray<LED_CNT> ledBitmask; |
| 646 | BitArray<FF_CNT> ffBitmask; |
| 647 | BitArray<INPUT_PROP_CNT> propBitmask; |
| 648 | BitArray<MSC_CNT> mscBitmask; |
| 649 | BitArray<ABS_CNT> absBitmask; |
Prabir Pradhan | 341d078 | 2023-07-14 19:04:10 +0000 | [diff] [blame] | 650 | struct AxisState { |
| 651 | RawAbsoluteAxisInfo info; |
| 652 | int value; |
| 653 | }; |
Prabir Pradhan | 955b613 | 2023-07-14 19:44:34 +0000 | [diff] [blame] | 654 | std::map<int /*axis*/, AxisState> absState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 655 | |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 656 | std::string configurationFile; |
Siarhei Vishniakou | d549b25 | 2020-08-11 11:25:26 -0500 | [diff] [blame] | 657 | std::unique_ptr<PropertyMap> configuration; |
Siarhei Vishniakou | 3e78dec | 2019-02-20 16:21:46 -0600 | [diff] [blame] | 658 | std::unique_ptr<VirtualKeyMap> virtualKeyMap; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 659 | KeyMap keyMap; |
| 660 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 661 | bool ffEffectPlaying; |
| 662 | int16_t ffEffectId; // initially -1 |
| 663 | |
Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 664 | // A shared_ptr of a device associated with the input device. |
Prabir Pradhan | 5189478 | 2022-08-23 16:29:10 +0000 | [diff] [blame] | 665 | // The input devices that have the same sysfs path have the same associated device. |
Prabir Pradhan | edeec3b | 2022-08-26 22:33:55 +0000 | [diff] [blame] | 666 | std::shared_ptr<const AssociatedDevice> associatedDevice; |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 667 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 668 | int32_t controllerNumber; |
| 669 | |
Prabir Pradhan | cb42b47 | 2022-08-23 16:01:19 +0000 | [diff] [blame] | 670 | Device(int fd, int32_t id, std::string path, InputDeviceIdentifier identifier, |
| 671 | std::shared_ptr<const AssociatedDevice> assocDev); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 672 | ~Device(); |
| 673 | |
| 674 | void close(); |
| 675 | |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 676 | bool enabled; // initially true |
| 677 | status_t enable(); |
| 678 | status_t disable(); |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 679 | bool hasValidFd() const; |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 680 | const bool isVirtual; // set if fd < 0 is passed to constructor |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 681 | |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 682 | const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const; |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 683 | |
| 684 | template <std::size_t N> |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 685 | status_t readDeviceBitMask(unsigned long ioctlCode, BitArray<N>& bitArray); |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 686 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 687 | void configureFd(); |
Prabir Pradhan | 341d078 | 2023-07-14 19:04:10 +0000 | [diff] [blame] | 688 | void populateAbsoluteAxisStates(); |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 689 | bool hasKeycodeLocked(int keycode) const; |
| 690 | void loadConfigurationLocked(); |
| 691 | bool loadVirtualKeyMapLocked(); |
| 692 | status_t loadKeyMapLocked(); |
| 693 | bool isExternalDeviceLocked(); |
| 694 | bool deviceHasMicLocked(); |
| 695 | void setLedForControllerLocked(); |
| 696 | status_t mapLed(int32_t led, int32_t* outScanCode) const; |
| 697 | void setLedStateLocked(int32_t led, bool on); |
Prabir Pradhan | 9762ac9 | 2023-07-13 21:45:12 +0000 | [diff] [blame] | 698 | |
| 699 | bool currentFrameDropped; |
| 700 | void trackInputEvent(const struct input_event& event); |
| 701 | void readDeviceState(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 702 | }; |
| 703 | |
Siarhei Vishniakou | a4c502a | 2021-02-05 00:45:20 +0000 | [diff] [blame] | 704 | /** |
| 705 | * Create a new device for the provided path. |
| 706 | */ |
Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 707 | void openDeviceLocked(const std::string& devicePath) REQUIRES(mLock); |
| 708 | void openVideoDeviceLocked(const std::string& devicePath) REQUIRES(mLock); |
Siarhei Vishniakou | f49608d | 2020-08-20 19:18:21 -0500 | [diff] [blame] | 709 | /** |
| 710 | * Try to associate a video device with an input device. If the association succeeds, |
| 711 | * the videoDevice is moved into the input device. 'videoDevice' will become null if this |
| 712 | * happens. |
| 713 | * Return true if the association succeeds. |
| 714 | * Return false otherwise. |
| 715 | */ |
Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 716 | bool tryAddVideoDeviceLocked(Device& device, std::unique_ptr<TouchVideoDevice>& videoDevice) |
| 717 | REQUIRES(mLock); |
| 718 | void createVirtualKeyboardLocked() REQUIRES(mLock); |
| 719 | void addDeviceLocked(std::unique_ptr<Device> device) REQUIRES(mLock); |
| 720 | void assignDescriptorLocked(InputDeviceIdentifier& identifier) REQUIRES(mLock); |
Prabir Pradhan | cb42b47 | 2022-08-23 16:01:19 +0000 | [diff] [blame] | 721 | std::shared_ptr<const AssociatedDevice> obtainAssociatedDeviceLocked( |
| 722 | const std::filesystem::path& devicePath) const REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 723 | |
Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 724 | void closeDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock); |
| 725 | void closeVideoDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock); |
| 726 | void closeDeviceLocked(Device& device) REQUIRES(mLock); |
| 727 | void closeAllDevicesLocked() REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 728 | |
Siarhei Vishniakou | 2592031 | 2018-12-12 15:24:44 -0800 | [diff] [blame] | 729 | status_t registerFdForEpoll(int fd); |
| 730 | status_t unregisterFdFromEpoll(int fd); |
Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 731 | status_t registerDeviceForEpollLocked(Device& device) REQUIRES(mLock); |
| 732 | void registerVideoDeviceForEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock); |
| 733 | status_t unregisterDeviceFromEpollLocked(Device& device) REQUIRES(mLock); |
| 734 | void unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock); |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 735 | |
Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 736 | status_t scanDirLocked(const std::string& dirname) REQUIRES(mLock); |
| 737 | status_t scanVideoDirLocked(const std::string& dirname) REQUIRES(mLock); |
| 738 | void scanDevicesLocked() REQUIRES(mLock); |
Prabir Pradhan | 952e65b | 2022-06-23 17:49:55 +0000 | [diff] [blame] | 739 | base::Result<void> readNotifyLocked() REQUIRES(mLock); |
| 740 | void handleNotifyEventLocked(const inotify_event&) REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 741 | |
Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 742 | Device* getDeviceLocked(int32_t deviceId) const REQUIRES(mLock); |
| 743 | Device* getDeviceByPathLocked(const std::string& devicePath) const REQUIRES(mLock); |
Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 744 | /** |
| 745 | * Look through all available fd's (both for input devices and for video devices), |
| 746 | * and return the device pointer. |
| 747 | */ |
Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 748 | Device* getDeviceByFdLocked(int fd) const REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 749 | |
Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 750 | int32_t getNextControllerNumberLocked(const std::string& name) REQUIRES(mLock); |
Josh Bartel | 938632f | 2022-07-19 15:34:22 -0500 | [diff] [blame] | 751 | |
| 752 | bool hasDeviceWithDescriptorLocked(const std::string& descriptor) const REQUIRES(mLock); |
| 753 | |
Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 754 | void releaseControllerNumberLocked(int32_t num) REQUIRES(mLock); |
| 755 | void reportDeviceAddedForStatisticsLocked(const InputDeviceIdentifier& identifier, |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 756 | ftl::Flags<InputDeviceClass> classes) REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 757 | |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 758 | const std::unordered_map<int32_t, RawBatteryInfo>& getBatteryInfoLocked(int32_t deviceId) const |
| 759 | REQUIRES(mLock); |
| 760 | |
| 761 | const std::unordered_map<int32_t, RawLightInfo>& getLightInfoLocked(int32_t deviceId) const |
| 762 | REQUIRES(mLock); |
| 763 | |
Usama Arif | b27c8e6 | 2021-06-03 16:44:09 +0100 | [diff] [blame] | 764 | void addDeviceInputInotify(); |
| 765 | void addDeviceInotify(); |
| 766 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 767 | // Protect all internal state. |
Chris Ye | 1c2e089 | 2020-11-30 21:41:44 -0800 | [diff] [blame] | 768 | mutable std::mutex mLock; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 769 | |
| 770 | // The actual id of the built-in keyboard, or NO_BUILT_IN_KEYBOARD if none. |
| 771 | // EventHub remaps the built-in keyboard to id 0 externally as required by the API. |
| 772 | enum { |
| 773 | // Must not conflict with any other assigned device ids, including |
| 774 | // the virtual keyboard id (-1). |
| 775 | NO_BUILT_IN_KEYBOARD = -2, |
| 776 | }; |
| 777 | int32_t mBuiltInKeyboardId; |
| 778 | |
| 779 | int32_t mNextDeviceId; |
| 780 | |
| 781 | BitSet32 mControllerNumbers; |
| 782 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 783 | std::unordered_map<int32_t, std::unique_ptr<Device>> mDevices; |
Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 784 | /** |
| 785 | * Video devices that report touchscreen heatmap, but have not (yet) been paired |
| 786 | * with a specific input device. Video device discovery is independent from input device |
| 787 | * discovery, so the two types of devices could be found in any order. |
| 788 | * Ideally, video devices in this queue do not have an open fd, or at least aren't |
| 789 | * actively streaming. |
| 790 | */ |
| 791 | std::vector<std::unique_ptr<TouchVideoDevice>> mUnattachedVideoDevices; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 792 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 793 | std::vector<std::unique_ptr<Device>> mOpeningDevices; |
| 794 | std::vector<std::unique_ptr<Device>> mClosingDevices; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 795 | |
| 796 | bool mNeedToSendFinishedDeviceScan; |
| 797 | bool mNeedToReopenDevices; |
| 798 | bool mNeedToScanDevices; |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 799 | std::vector<std::string> mExcludedDevices; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 800 | |
| 801 | int mEpollFd; |
| 802 | int mINotifyFd; |
| 803 | int mWakeReadPipeFd; |
| 804 | int mWakeWritePipeFd; |
| 805 | |
Usama Arif | b27c8e6 | 2021-06-03 16:44:09 +0100 | [diff] [blame] | 806 | int mDeviceInputWd; |
| 807 | int mDeviceWd = -1; |
Siarhei Vishniakou | 951f362 | 2018-12-12 19:45:42 -0800 | [diff] [blame] | 808 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 809 | // Maximum number of signalled FDs to handle at a time. |
| 810 | static const int EPOLL_MAX_EVENTS = 16; |
| 811 | |
| 812 | // The array of pending epoll events and the index of the next event to be handled. |
| 813 | struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS]; |
| 814 | size_t mPendingEventCount; |
| 815 | size_t mPendingEventIndex; |
| 816 | bool mPendingINotify; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 817 | }; |
| 818 | |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 819 | } // namespace android |