The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -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 | |
| 17 | // |
| 18 | #ifndef _RUNTIME_EVENT_HUB_H |
| 19 | #define _RUNTIME_EVENT_HUB_H |
| 20 | |
Jeff Brown | 9d3b1a4 | 2013-07-01 19:07:15 -0700 | [diff] [blame] | 21 | #include <input/Input.h> |
| 22 | #include <input/InputDevice.h> |
| 23 | #include <input/Keyboard.h> |
| 24 | #include <input/KeyLayoutMap.h> |
| 25 | #include <input/KeyCharacterMap.h> |
| 26 | #include <input/VirtualKeyMap.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | #include <utils/String8.h> |
| 28 | #include <utils/threads.h> |
Mathias Agopian | 3b4062e | 2009-05-31 19:13:00 -0700 | [diff] [blame] | 29 | #include <utils/Log.h> |
| 30 | #include <utils/threads.h> |
| 31 | #include <utils/List.h> |
| 32 | #include <utils/Errors.h> |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 33 | #include <utils/PropertyMap.h> |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 34 | #include <utils/Vector.h> |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 35 | #include <utils/KeyedVector.h> |
Michael Wright | ac6c78b | 2013-07-17 13:21:45 -0700 | [diff] [blame] | 36 | #include <utils/BitSet.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | |
| 38 | #include <linux/input.h> |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 39 | #include <sys/epoll.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 41 | /* Convenience constants. */ |
| 42 | |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 43 | #define BTN_FIRST 0x100 // first button code |
| 44 | #define BTN_LAST 0x15f // last button code |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 45 | |
Jeff Brown | 4dac901 | 2013-04-10 01:03:19 -0700 | [diff] [blame] | 46 | /* |
| 47 | * These constants are used privately in Android to pass raw timestamps |
| 48 | * through evdev from uinput device drivers because there is currently no |
| 49 | * other way to transfer this information. The evdev driver automatically |
| 50 | * timestamps all input events with the time they were posted and clobbers |
| 51 | * whatever information was passed in. |
| 52 | * |
| 53 | * For the purposes of this hack, the timestamp is specified in the |
| 54 | * CLOCK_MONOTONIC timebase and is split into two EV_MSC events specifying |
| 55 | * seconds and microseconds. |
| 56 | */ |
| 57 | #define MSC_ANDROID_TIME_SEC 0x6 |
| 58 | #define MSC_ANDROID_TIME_USEC 0x7 |
| 59 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | namespace android { |
| 61 | |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 62 | enum { |
| 63 | // Device id of a special "virtual" keyboard that is always present. |
| 64 | VIRTUAL_KEYBOARD_ID = -1, |
| 65 | // Device id of the "built-in" keyboard if there is one. |
| 66 | BUILT_IN_KEYBOARD_ID = 0, |
| 67 | }; |
| 68 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | /* |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 70 | * A raw event as retrieved from the EventHub. |
| 71 | */ |
| 72 | struct RawEvent { |
| 73 | nsecs_t when; |
| 74 | int32_t deviceId; |
| 75 | int32_t type; |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 76 | int32_t code; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 77 | int32_t value; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | /* Describes an absolute axis. */ |
| 81 | struct RawAbsoluteAxisInfo { |
| 82 | bool valid; // true if the information is valid, false otherwise |
| 83 | |
| 84 | int32_t minValue; // minimum value |
| 85 | int32_t maxValue; // maximum value |
| 86 | int32_t flat; // center flat position, eg. flat == 8 means center is between -8 and 8 |
| 87 | int32_t fuzz; // error tolerance, eg. fuzz == 4 means value is +/- 4 due to noise |
Jeff Brown | b3a2d13 | 2011-06-12 18:14:50 -0700 | [diff] [blame] | 88 | int32_t resolution; // resolution in units per mm or radians per mm |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 89 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 90 | inline void clear() { |
| 91 | valid = false; |
| 92 | minValue = 0; |
| 93 | maxValue = 0; |
| 94 | flat = 0; |
| 95 | fuzz = 0; |
Jeff Brown | b3a2d13 | 2011-06-12 18:14:50 -0700 | [diff] [blame] | 96 | resolution = 0; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 97 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | /* |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 101 | * Input device classes. |
| 102 | */ |
| 103 | enum { |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 104 | /* The input device is a keyboard or has buttons. */ |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 105 | INPUT_DEVICE_CLASS_KEYBOARD = 0x00000001, |
| 106 | |
| 107 | /* The input device is an alpha-numeric keyboard (not just a dial pad). */ |
| 108 | INPUT_DEVICE_CLASS_ALPHAKEY = 0x00000002, |
| 109 | |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 110 | /* The input device is a touchscreen or a touchpad (either single-touch or multi-touch). */ |
| 111 | INPUT_DEVICE_CLASS_TOUCH = 0x00000004, |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 112 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 113 | /* The input device is a cursor device such as a trackball or mouse. */ |
| 114 | INPUT_DEVICE_CLASS_CURSOR = 0x00000008, |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 115 | |
| 116 | /* The input device is a multi-touch touchscreen. */ |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 117 | INPUT_DEVICE_CLASS_TOUCH_MT = 0x00000010, |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 118 | |
Jeff Brown | dc1ab4b | 2010-09-14 18:03:38 -0700 | [diff] [blame] | 119 | /* The input device is a directional pad (implies keyboard, has DPAD keys). */ |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 120 | INPUT_DEVICE_CLASS_DPAD = 0x00000020, |
| 121 | |
Jeff Brown | dc1ab4b | 2010-09-14 18:03:38 -0700 | [diff] [blame] | 122 | /* The input device is a gamepad (implies keyboard, has BUTTON keys). */ |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 123 | INPUT_DEVICE_CLASS_GAMEPAD = 0x00000040, |
| 124 | |
| 125 | /* The input device has switches. */ |
| 126 | INPUT_DEVICE_CLASS_SWITCH = 0x00000080, |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 127 | |
| 128 | /* The input device is a joystick (implies gamepad, has joystick absolute axes). */ |
| 129 | INPUT_DEVICE_CLASS_JOYSTICK = 0x00000100, |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 130 | |
Jeff Brown | a47425a | 2012-04-13 04:09:27 -0700 | [diff] [blame] | 131 | /* The input device has a vibrator (supports FF_RUMBLE). */ |
| 132 | INPUT_DEVICE_CLASS_VIBRATOR = 0x00000200, |
| 133 | |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 134 | /* The input device is virtual (not a real device, not part of UI configuration). */ |
| 135 | INPUT_DEVICE_CLASS_VIRTUAL = 0x40000000, |
| 136 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 137 | /* The input device is external (not built-in). */ |
| 138 | INPUT_DEVICE_CLASS_EXTERNAL = 0x80000000, |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | /* |
Jeff Brown | 9ee285a | 2011-08-31 12:56:34 -0700 | [diff] [blame] | 142 | * Gets the class that owns an axis, in cases where multiple classes might claim |
| 143 | * the same axis for different purposes. |
| 144 | */ |
| 145 | extern uint32_t getAbsAxisUsage(int32_t axis, uint32_t deviceClasses); |
| 146 | |
| 147 | /* |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 148 | * Grand Central Station for events. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | * |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 150 | * The event hub aggregates input events received across all known input |
| 151 | * devices on the system, including devices that may be emulated by the simulator |
| 152 | * environment. In addition, the event hub generates fake input events to indicate |
| 153 | * when devices are added or removed. |
| 154 | * |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 155 | * The event hub provides a stream of input events (via the getEvent function). |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 156 | * It also supports querying the current actual state of input devices such as identifying |
| 157 | * which keys are currently down. Finally, the event hub keeps track of the capabilities of |
| 158 | * individual input devices, such as their class and the set of key codes that they support. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | */ |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 160 | class EventHubInterface : public virtual RefBase { |
| 161 | protected: |
| 162 | EventHubInterface() { } |
| 163 | virtual ~EventHubInterface() { } |
| 164 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | public: |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 166 | // Synthetic raw event type codes produced when devices are added or removed. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | enum { |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 168 | // Sent when a device is added. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | DEVICE_ADDED = 0x10000000, |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 170 | // Sent when a device is removed. |
| 171 | DEVICE_REMOVED = 0x20000000, |
| 172 | // Sent when all added/removed devices from the most recent scan have been reported. |
| 173 | // This event is always sent at least once. |
| 174 | FINISHED_DEVICE_SCAN = 0x30000000, |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 175 | |
| 176 | FIRST_SYNTHETIC_EVENT = DEVICE_ADDED, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | }; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 178 | |
| 179 | virtual uint32_t getDeviceClasses(int32_t deviceId) const = 0; |
| 180 | |
Jeff Brown | e38fdfa | 2012-04-06 14:51:01 -0700 | [diff] [blame] | 181 | virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const = 0; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 182 | |
Michael Wright | ac6c78b | 2013-07-17 13:21:45 -0700 | [diff] [blame] | 183 | virtual int32_t getDeviceControllerNumber(int32_t deviceId) const = 0; |
| 184 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 185 | virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const = 0; |
| 186 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 187 | virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis, |
| 188 | RawAbsoluteAxisInfo* outAxisInfo) const = 0; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 189 | |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 190 | virtual bool hasRelativeAxis(int32_t deviceId, int axis) const = 0; |
| 191 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 192 | virtual bool hasInputProperty(int32_t deviceId, int property) const = 0; |
| 193 | |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 194 | virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 195 | int32_t* outKeycode, uint32_t* outFlags) const = 0; |
| 196 | |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 197 | virtual status_t mapAxis(int32_t deviceId, int32_t scanCode, |
Jeff Brown | 3a22fa0 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 198 | AxisInfo* outAxisInfo) const = 0; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 199 | |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 200 | // Sets devices that are excluded from opening. |
| 201 | // This can be used to ignore input devices for sensors. |
| 202 | virtual void setExcludedDevices(const Vector<String8>& devices) = 0; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 203 | |
| 204 | /* |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 205 | * Wait for events to become available and returns them. |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 206 | * After returning, the EventHub holds onto a wake lock until the next call to getEvent. |
| 207 | * This ensures that the device will not go to sleep while the event is being processed. |
| 208 | * If the device needs to remain awake longer than that, then the caller is responsible |
| 209 | * for taking care of it (say, by poking the power manager user activity timer). |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 210 | * |
| 211 | * The timeout is advisory only. If the device is asleep, it will not wake just to |
| 212 | * service the timeout. |
| 213 | * |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 214 | * Returns the number of events obtained, or 0 if the timeout expired. |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 215 | */ |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 216 | virtual size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) = 0; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 217 | |
| 218 | /* |
| 219 | * Query current input state. |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 220 | */ |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 221 | virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0; |
| 222 | virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0; |
| 223 | virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0; |
Jeff Brown | 2717eff | 2011-06-30 23:53:07 -0700 | [diff] [blame] | 224 | virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis, |
| 225 | int32_t* outValue) const = 0; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 226 | |
| 227 | /* |
| 228 | * Examine key input devices for specific framework keycode support |
| 229 | */ |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 230 | virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 231 | uint8_t* outFlags) const = 0; |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 232 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 233 | virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const = 0; |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 234 | virtual bool hasLed(int32_t deviceId, int32_t led) const = 0; |
| 235 | virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0; |
| 236 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 237 | virtual void getVirtualKeyDefinitions(int32_t deviceId, |
| 238 | Vector<VirtualKeyDefinition>& outVirtualKeys) const = 0; |
| 239 | |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 240 | virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const = 0; |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 241 | virtual bool setKeyboardLayoutOverlay(int32_t deviceId, const sp<KeyCharacterMap>& map) = 0; |
Jeff Brown | 1e08fe9 | 2011-11-15 17:48:10 -0800 | [diff] [blame] | 242 | |
Jeff Brown | a47425a | 2012-04-13 04:09:27 -0700 | [diff] [blame] | 243 | /* Control the vibrator. */ |
| 244 | virtual void vibrate(int32_t deviceId, nsecs_t duration) = 0; |
| 245 | virtual void cancelVibrate(int32_t deviceId) = 0; |
| 246 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 247 | /* Requests the EventHub to reopen all input devices on the next call to getEvents(). */ |
| 248 | virtual void requestReopenDevices() = 0; |
| 249 | |
| 250 | /* Wakes up getEvents() if it is blocked on a read. */ |
| 251 | virtual void wake() = 0; |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 252 | |
Jeff Brown | 89ef072 | 2011-08-10 16:25:21 -0700 | [diff] [blame] | 253 | /* Dump EventHub state to a string. */ |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 254 | virtual void dump(String8& dump) = 0; |
Jeff Brown | 89ef072 | 2011-08-10 16:25:21 -0700 | [diff] [blame] | 255 | |
| 256 | /* Called by the heatbeat to ensures that the reader has not deadlocked. */ |
| 257 | virtual void monitor() = 0; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 258 | }; |
| 259 | |
| 260 | class EventHub : public EventHubInterface |
| 261 | { |
| 262 | public: |
| 263 | EventHub(); |
| 264 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 265 | virtual uint32_t getDeviceClasses(int32_t deviceId) const; |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 266 | |
Jeff Brown | e38fdfa | 2012-04-06 14:51:01 -0700 | [diff] [blame] | 267 | virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const; |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 268 | |
Michael Wright | ac6c78b | 2013-07-17 13:21:45 -0700 | [diff] [blame] | 269 | virtual int32_t getDeviceControllerNumber(int32_t deviceId) const; |
| 270 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 271 | virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const; |
| 272 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 273 | virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis, |
| 274 | RawAbsoluteAxisInfo* outAxisInfo) const; |
| 275 | |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 276 | virtual bool hasRelativeAxis(int32_t deviceId, int axis) const; |
| 277 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 278 | virtual bool hasInputProperty(int32_t deviceId, int property) const; |
| 279 | |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 280 | virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 281 | int32_t* outKeycode, uint32_t* outFlags) const; |
| 282 | |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 283 | virtual status_t mapAxis(int32_t deviceId, int32_t scanCode, |
Jeff Brown | 3a22fa0 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 284 | AxisInfo* outAxisInfo) const; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 285 | |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 286 | virtual void setExcludedDevices(const Vector<String8>& devices); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 287 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 288 | virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const; |
| 289 | virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const; |
| 290 | virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const; |
Jeff Brown | 2717eff | 2011-06-30 23:53:07 -0700 | [diff] [blame] | 291 | virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t* outValue) const; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 292 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 293 | virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, |
| 294 | const int32_t* keyCodes, uint8_t* outFlags) const; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 295 | |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 296 | virtual size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize); |
Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 297 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 298 | virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const; |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 299 | virtual bool hasLed(int32_t deviceId, int32_t led) const; |
| 300 | virtual void setLedState(int32_t deviceId, int32_t led, bool on); |
| 301 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 302 | virtual void getVirtualKeyDefinitions(int32_t deviceId, |
| 303 | Vector<VirtualKeyDefinition>& outVirtualKeys) const; |
| 304 | |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 305 | virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const; |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 306 | virtual bool setKeyboardLayoutOverlay(int32_t deviceId, const sp<KeyCharacterMap>& map); |
Jeff Brown | 1e08fe9 | 2011-11-15 17:48:10 -0800 | [diff] [blame] | 307 | |
Jeff Brown | a47425a | 2012-04-13 04:09:27 -0700 | [diff] [blame] | 308 | virtual void vibrate(int32_t deviceId, nsecs_t duration); |
| 309 | virtual void cancelVibrate(int32_t deviceId); |
| 310 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 311 | virtual void requestReopenDevices(); |
| 312 | |
| 313 | virtual void wake(); |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 314 | |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 315 | virtual void dump(String8& dump); |
Jeff Brown | 89ef072 | 2011-08-10 16:25:21 -0700 | [diff] [blame] | 316 | virtual void monitor(); |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 317 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | protected: |
| 319 | virtual ~EventHub(); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 320 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 321 | private: |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 322 | struct Device { |
| 323 | Device* next; |
| 324 | |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 325 | int fd; // may be -1 if device is virtual |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 326 | const int32_t id; |
| 327 | const String8 path; |
| 328 | const InputDeviceIdentifier identifier; |
| 329 | |
| 330 | uint32_t classes; |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 331 | |
| 332 | uint8_t keyBitmask[(KEY_MAX + 1) / 8]; |
| 333 | uint8_t absBitmask[(ABS_MAX + 1) / 8]; |
| 334 | uint8_t relBitmask[(REL_MAX + 1) / 8]; |
| 335 | uint8_t swBitmask[(SW_MAX + 1) / 8]; |
| 336 | uint8_t ledBitmask[(LED_MAX + 1) / 8]; |
Jeff Brown | a47425a | 2012-04-13 04:09:27 -0700 | [diff] [blame] | 337 | uint8_t ffBitmask[(FF_MAX + 1) / 8]; |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 338 | uint8_t propBitmask[(INPUT_PROP_MAX + 1) / 8]; |
| 339 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 340 | String8 configurationFile; |
| 341 | PropertyMap* configuration; |
| 342 | VirtualKeyMap* virtualKeyMap; |
| 343 | KeyMap keyMap; |
| 344 | |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 345 | sp<KeyCharacterMap> overlayKeyMap; |
| 346 | sp<KeyCharacterMap> combinedKeyMap; |
| 347 | |
Jeff Brown | a47425a | 2012-04-13 04:09:27 -0700 | [diff] [blame] | 348 | bool ffEffectPlaying; |
| 349 | int16_t ffEffectId; // initially -1 |
| 350 | |
Michael Wright | ac6c78b | 2013-07-17 13:21:45 -0700 | [diff] [blame] | 351 | int32_t controllerNumber; |
| 352 | |
Jeff Brown | 4dac901 | 2013-04-10 01:03:19 -0700 | [diff] [blame] | 353 | int32_t timestampOverrideSec; |
| 354 | int32_t timestampOverrideUsec; |
| 355 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 356 | Device(int fd, int32_t id, const String8& path, const InputDeviceIdentifier& identifier); |
| 357 | ~Device(); |
| 358 | |
| 359 | void close(); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 360 | |
| 361 | inline bool isVirtual() const { return fd < 0; } |
Jeff Brown | 4a3862f | 2012-04-17 18:50:05 -0700 | [diff] [blame] | 362 | |
| 363 | const sp<KeyCharacterMap>& getKeyCharacterMap() const { |
| 364 | if (combinedKeyMap != NULL) { |
| 365 | return combinedKeyMap; |
| 366 | } |
| 367 | return keyMap.keyCharacterMap; |
| 368 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 369 | }; |
| 370 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 371 | status_t openDeviceLocked(const char *devicePath); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 372 | void createVirtualKeyboardLocked(); |
| 373 | void addDeviceLocked(Device* device); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 374 | |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 375 | status_t closeDeviceByPathLocked(const char *devicePath); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 376 | void closeDeviceLocked(Device* device); |
| 377 | void closeAllDevicesLocked(); |
| 378 | |
| 379 | status_t scanDirLocked(const char *dirname); |
| 380 | void scanDevicesLocked(); |
| 381 | status_t readNotifyLocked(); |
| 382 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 383 | Device* getDeviceLocked(int32_t deviceId) const; |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 384 | Device* getDeviceByPathLocked(const char* devicePath) const; |
| 385 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 386 | bool hasKeycodeLocked(Device* device, int keycode) const; |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 387 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 388 | void loadConfigurationLocked(Device* device); |
| 389 | status_t loadVirtualKeyMapLocked(Device* device); |
| 390 | status_t loadKeyMapLocked(Device* device); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 391 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 392 | bool isExternalDeviceLocked(Device* device); |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 393 | |
Michael Wright | ac6c78b | 2013-07-17 13:21:45 -0700 | [diff] [blame] | 394 | int32_t getNextControllerNumberLocked(Device* device); |
| 395 | void releaseControllerNumberLocked(Device* device); |
| 396 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 397 | // Protect all internal state. |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 398 | mutable Mutex mLock; |
Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 399 | |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 400 | // The actual id of the built-in keyboard, or NO_BUILT_IN_KEYBOARD if none. |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 401 | // EventHub remaps the built-in keyboard to id 0 externally as required by the API. |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 402 | enum { |
| 403 | // Must not conflict with any other assigned device ids, including |
| 404 | // the virtual keyboard id (-1). |
| 405 | NO_BUILT_IN_KEYBOARD = -2, |
| 406 | }; |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 407 | int32_t mBuiltInKeyboardId; |
| 408 | |
| 409 | int32_t mNextDeviceId; |
| 410 | |
Michael Wright | ac6c78b | 2013-07-17 13:21:45 -0700 | [diff] [blame] | 411 | BitSet32 mControllerNumbers; |
| 412 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 413 | KeyedVector<int32_t, Device*> mDevices; |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 414 | |
| 415 | Device *mOpeningDevices; |
| 416 | Device *mClosingDevices; |
| 417 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 418 | bool mNeedToSendFinishedDeviceScan; |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 419 | bool mNeedToReopenDevices; |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 420 | bool mNeedToScanDevices; |
| 421 | Vector<String8> mExcludedDevices; |
Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 422 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 423 | int mEpollFd; |
| 424 | int mINotifyFd; |
| 425 | int mWakeReadPipeFd; |
| 426 | int mWakeWritePipeFd; |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 427 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 428 | // Ids used for epoll notifications not associated with devices. |
| 429 | static const uint32_t EPOLL_ID_INOTIFY = 0x80000001; |
| 430 | static const uint32_t EPOLL_ID_WAKE = 0x80000002; |
| 431 | |
| 432 | // Epoll FD list size hint. |
| 433 | static const int EPOLL_SIZE_HINT = 8; |
| 434 | |
| 435 | // Maximum number of signalled FDs to handle at a time. |
| 436 | static const int EPOLL_MAX_EVENTS = 16; |
| 437 | |
| 438 | // The array of pending epoll events and the index of the next event to be handled. |
| 439 | struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS]; |
| 440 | size_t mPendingEventCount; |
| 441 | size_t mPendingEventIndex; |
| 442 | bool mPendingINotify; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 443 | }; |
| 444 | |
| 445 | }; // namespace android |
| 446 | |
| 447 | #endif // _RUNTIME_EVENT_HUB_H |