The Android Open Source Project | edbf3b6 | 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 | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 21 | #include <android/input.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | #include <utils/String8.h> |
| 23 | #include <utils/threads.h> |
Mathias Agopian | e0c3220 | 2009-05-31 19:13:00 -0700 | [diff] [blame] | 24 | #include <utils/Log.h> |
| 25 | #include <utils/threads.h> |
| 26 | #include <utils/List.h> |
| 27 | #include <utils/Errors.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | |
| 29 | #include <linux/input.h> |
| 30 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 31 | /* These constants are not defined in linux/input.h but they are part of the multitouch |
| 32 | * input protocol. */ |
| 33 | |
| 34 | #define ABS_MT_TOUCH_MAJOR 0x30 /* Major axis of touching ellipse */ |
| 35 | #define ABS_MT_TOUCH_MINOR 0x31 /* Minor axis (omit if circular) */ |
| 36 | #define ABS_MT_WIDTH_MAJOR 0x32 /* Major axis of approaching ellipse */ |
| 37 | #define ABS_MT_WIDTH_MINOR 0x33 /* Minor axis (omit if circular) */ |
| 38 | #define ABS_MT_ORIENTATION 0x34 /* Ellipse orientation */ |
| 39 | #define ABS_MT_POSITION_X 0x35 /* Center X ellipse position */ |
| 40 | #define ABS_MT_POSITION_Y 0x36 /* Center Y ellipse position */ |
| 41 | #define ABS_MT_TOOL_TYPE 0x37 /* Type of touching device (finger, pen, ...) */ |
| 42 | #define ABS_MT_BLOB_ID 0x38 /* Group a set of packets as a blob */ |
| 43 | #define ABS_MT_TRACKING_ID 0x39 /* Unique ID of initiated contact */ |
| 44 | #define ABS_MT_PRESSURE 0x3a /* Pressure on contact area */ |
| 45 | |
| 46 | #define MT_TOOL_FINGER 0 /* Identifies a finger */ |
| 47 | #define MT_TOOL_PEN 1 /* Identifies a pen */ |
| 48 | |
| 49 | #define SYN_MT_REPORT 2 |
| 50 | |
| 51 | /* Convenience constants. */ |
| 52 | |
| 53 | #define BTN_FIRST 0x100 // first button scancode |
| 54 | #define BTN_LAST 0x15f // last button scancode |
| 55 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | struct pollfd; |
| 57 | |
| 58 | namespace android { |
| 59 | |
| 60 | class KeyLayoutMap; |
| 61 | |
| 62 | /* |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 63 | * A raw event as retrieved from the EventHub. |
| 64 | */ |
| 65 | struct RawEvent { |
| 66 | nsecs_t when; |
| 67 | int32_t deviceId; |
| 68 | int32_t type; |
| 69 | int32_t scanCode; |
| 70 | int32_t keyCode; |
| 71 | int32_t value; |
| 72 | uint32_t flags; |
| 73 | }; |
| 74 | |
| 75 | /* Describes an absolute axis. */ |
| 76 | struct RawAbsoluteAxisInfo { |
| 77 | bool valid; // true if the information is valid, false otherwise |
| 78 | |
| 79 | int32_t minValue; // minimum value |
| 80 | int32_t maxValue; // maximum value |
| 81 | int32_t flat; // center flat position, eg. flat == 8 means center is between -8 and 8 |
| 82 | int32_t fuzz; // error tolerance, eg. fuzz == 4 means value is +/- 4 due to noise |
| 83 | |
| 84 | inline int32_t getRange() { return maxValue - minValue; } |
| 85 | }; |
| 86 | |
| 87 | /* |
Jeff Brown | 5c1ed84 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 88 | * Input device classes. |
| 89 | */ |
| 90 | enum { |
| 91 | /* The input device is a keyboard. */ |
| 92 | INPUT_DEVICE_CLASS_KEYBOARD = 0x00000001, |
| 93 | |
| 94 | /* The input device is an alpha-numeric keyboard (not just a dial pad). */ |
| 95 | INPUT_DEVICE_CLASS_ALPHAKEY = 0x00000002, |
| 96 | |
| 97 | /* The input device is a touchscreen (either single-touch or multi-touch). */ |
| 98 | INPUT_DEVICE_CLASS_TOUCHSCREEN = 0x00000004, |
| 99 | |
| 100 | /* The input device is a trackball. */ |
| 101 | INPUT_DEVICE_CLASS_TRACKBALL = 0x00000008, |
| 102 | |
| 103 | /* The input device is a multi-touch touchscreen. */ |
| 104 | INPUT_DEVICE_CLASS_TOUCHSCREEN_MT= 0x00000010, |
| 105 | |
| 106 | /* The input device is a directional pad. */ |
| 107 | INPUT_DEVICE_CLASS_DPAD = 0x00000020, |
| 108 | |
| 109 | /* The input device is a gamepad (implies keyboard). */ |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 110 | INPUT_DEVICE_CLASS_GAMEPAD = 0x00000040, |
| 111 | |
| 112 | /* The input device has switches. */ |
| 113 | INPUT_DEVICE_CLASS_SWITCH = 0x00000080, |
Jeff Brown | 5c1ed84 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | /* |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 117 | * Grand Central Station for events. |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | * |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 119 | * The event hub aggregates input events received across all known input |
| 120 | * devices on the system, including devices that may be emulated by the simulator |
| 121 | * environment. In addition, the event hub generates fake input events to indicate |
| 122 | * when devices are added or removed. |
| 123 | * |
| 124 | * The event hub provies a stream of input events (via the getEvent function). |
| 125 | * It also supports querying the current actual state of input devices such as identifying |
| 126 | * which keys are currently down. Finally, the event hub keeps track of the capabilities of |
| 127 | * individual input devices, such as their class and the set of key codes that they support. |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | */ |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 129 | class EventHubInterface : public virtual RefBase { |
| 130 | protected: |
| 131 | EventHubInterface() { } |
| 132 | virtual ~EventHubInterface() { } |
| 133 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | public: |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 135 | // Synthetic raw event type codes produced when devices are added or removed. |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | enum { |
| 137 | DEVICE_ADDED = 0x10000000, |
| 138 | DEVICE_REMOVED = 0x20000000 |
| 139 | }; |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 140 | |
| 141 | virtual uint32_t getDeviceClasses(int32_t deviceId) const = 0; |
| 142 | |
| 143 | virtual String8 getDeviceName(int32_t deviceId) const = 0; |
| 144 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 145 | virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis, |
| 146 | RawAbsoluteAxisInfo* outAxisInfo) const = 0; |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 147 | |
| 148 | virtual status_t scancodeToKeycode(int32_t deviceId, int scancode, |
| 149 | int32_t* outKeycode, uint32_t* outFlags) const = 0; |
| 150 | |
| 151 | // exclude a particular device from opening |
| 152 | // this can be used to ignore input devices for sensors |
| 153 | virtual void addExcludedDevice(const char* deviceName) = 0; |
| 154 | |
| 155 | /* |
| 156 | * Wait for the next event to become available and return it. |
| 157 | * After returning, the EventHub holds onto a wake lock until the next call to getEvent. |
| 158 | * This ensures that the device will not go to sleep while the event is being processed. |
| 159 | * If the device needs to remain awake longer than that, then the caller is responsible |
| 160 | * for taking care of it (say, by poking the power manager user activity timer). |
| 161 | */ |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 162 | virtual bool getEvent(RawEvent* outEvent) = 0; |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 163 | |
| 164 | /* |
| 165 | * Query current input state. |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 166 | */ |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 167 | virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0; |
| 168 | virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0; |
| 169 | virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0; |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 170 | |
| 171 | /* |
| 172 | * Examine key input devices for specific framework keycode support |
| 173 | */ |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 174 | virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes, |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 175 | uint8_t* outFlags) const = 0; |
| 176 | }; |
| 177 | |
| 178 | class EventHub : public EventHubInterface |
| 179 | { |
| 180 | public: |
| 181 | EventHub(); |
| 182 | |
| 183 | status_t errorCheck() const; |
| 184 | |
| 185 | virtual uint32_t getDeviceClasses(int32_t deviceId) const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 187 | virtual String8 getDeviceName(int32_t deviceId) const; |
| 188 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 189 | virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis, |
| 190 | RawAbsoluteAxisInfo* outAxisInfo) const; |
| 191 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 192 | virtual status_t scancodeToKeycode(int32_t deviceId, int scancode, |
| 193 | int32_t* outKeycode, uint32_t* outFlags) const; |
| 194 | |
| 195 | virtual void addExcludedDevice(const char* deviceName); |
| 196 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 197 | virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const; |
| 198 | virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const; |
| 199 | virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const; |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 200 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 201 | virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, |
| 202 | const int32_t* keyCodes, uint8_t* outFlags) const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 204 | virtual bool getEvent(RawEvent* outEvent); |
Mike Lockwood | b441106 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 205 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | protected: |
| 207 | virtual ~EventHub(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | |
| 209 | private: |
| 210 | bool openPlatformInput(void); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | |
| 212 | int open_device(const char *device); |
| 213 | int close_device(const char *device); |
| 214 | int scan_dir(const char *dirname); |
| 215 | int read_notify(int nfd); |
| 216 | |
| 217 | status_t mError; |
| 218 | |
| 219 | struct device_t { |
| 220 | const int32_t id; |
| 221 | const String8 path; |
| 222 | String8 name; |
| 223 | uint32_t classes; |
| 224 | uint8_t* keyBitmask; |
| 225 | KeyLayoutMap* layoutMap; |
| 226 | String8 keylayoutFilename; |
Jens Gulin | 7dcaa58 | 2010-06-22 22:21:57 +0200 | [diff] [blame^] | 227 | int fd; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | device_t* next; |
| 229 | |
Iliyan Malchev | 34193b3 | 2009-08-06 14:50:08 -0700 | [diff] [blame] | 230 | device_t(int32_t _id, const char* _path, const char* name); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | ~device_t(); |
| 232 | }; |
| 233 | |
| 234 | device_t* getDevice(int32_t deviceId) const; |
Dianne Hackborn | c591736 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 235 | bool hasKeycode(device_t* device, int keycode) const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 237 | int32_t getScanCodeStateLocked(device_t* device, int32_t scanCode) const; |
| 238 | int32_t getKeyCodeStateLocked(device_t* device, int32_t keyCode) const; |
| 239 | int32_t getSwitchStateLocked(device_t* device, int32_t sw) const; |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 240 | bool markSupportedKeyCodesLocked(device_t* device, size_t numCodes, |
| 241 | const int32_t* keyCodes, uint8_t* outFlags) const; |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 242 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | // Protect all internal state. |
| 244 | mutable Mutex mLock; |
| 245 | |
| 246 | bool mHaveFirstKeyboard; |
Dianne Hackborn | c968c3a | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 247 | int32_t mFirstKeyboardId; // the API is that the built-in keyboard is id 0, so map it |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | |
| 249 | struct device_ent { |
| 250 | device_t* device; |
| 251 | uint32_t seq; |
| 252 | }; |
| 253 | device_ent *mDevicesById; |
| 254 | int mNumDevicesById; |
| 255 | |
| 256 | device_t *mOpeningDevices; |
| 257 | device_t *mClosingDevices; |
| 258 | |
| 259 | device_t **mDevices; |
| 260 | struct pollfd *mFDs; |
| 261 | int mFDCount; |
Mike Lockwood | b441106 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 262 | |
| 263 | bool mOpened; |
| 264 | List<String8> mExcludedDevices; |
| 265 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | // device ids that report particular switches. |
| 267 | #ifdef EV_SW |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 268 | int32_t mSwitches[SW_MAX + 1]; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 269 | #endif |
Jeff Brown | 8276307 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 270 | |
| 271 | static const int INPUT_BUFFER_SIZE = 64; |
| 272 | struct input_event mInputBufferData[INPUT_BUFFER_SIZE]; |
| 273 | int32_t mInputBufferIndex; |
| 274 | int32_t mInputBufferCount; |
| 275 | int32_t mInputDeviceIndex; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | }; |
| 277 | |
| 278 | }; // namespace android |
| 279 | |
| 280 | #endif // _RUNTIME_EVENT_HUB_H |