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