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