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