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