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