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