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 | 5c1ed84 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 63 | * Input device classes. |
| 64 | */ |
| 65 | enum { |
| 66 | /* The input device is a keyboard. */ |
| 67 | INPUT_DEVICE_CLASS_KEYBOARD = 0x00000001, |
| 68 | |
| 69 | /* The input device is an alpha-numeric keyboard (not just a dial pad). */ |
| 70 | INPUT_DEVICE_CLASS_ALPHAKEY = 0x00000002, |
| 71 | |
| 72 | /* The input device is a touchscreen (either single-touch or multi-touch). */ |
| 73 | INPUT_DEVICE_CLASS_TOUCHSCREEN = 0x00000004, |
| 74 | |
| 75 | /* The input device is a trackball. */ |
| 76 | INPUT_DEVICE_CLASS_TRACKBALL = 0x00000008, |
| 77 | |
| 78 | /* The input device is a multi-touch touchscreen. */ |
| 79 | INPUT_DEVICE_CLASS_TOUCHSCREEN_MT= 0x00000010, |
| 80 | |
| 81 | /* The input device is a directional pad. */ |
| 82 | INPUT_DEVICE_CLASS_DPAD = 0x00000020, |
| 83 | |
| 84 | /* The input device is a gamepad (implies keyboard). */ |
| 85 | INPUT_DEVICE_CLASS_GAMEPAD = 0x00000040 |
| 86 | }; |
| 87 | |
| 88 | /* |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 89 | * Grand Central Station for events. |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | * |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 91 | * The event hub aggregates input events received across all known input |
| 92 | * devices on the system, including devices that may be emulated by the simulator |
| 93 | * environment. In addition, the event hub generates fake input events to indicate |
| 94 | * when devices are added or removed. |
| 95 | * |
| 96 | * The event hub provies a stream of input events (via the getEvent function). |
| 97 | * It also supports querying the current actual state of input devices such as identifying |
| 98 | * which keys are currently down. Finally, the event hub keeps track of the capabilities of |
| 99 | * 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] | 100 | */ |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 101 | class EventHubInterface : public virtual RefBase { |
| 102 | protected: |
| 103 | EventHubInterface() { } |
| 104 | virtual ~EventHubInterface() { } |
| 105 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | public: |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 107 | // 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] | 108 | enum { |
| 109 | DEVICE_ADDED = 0x10000000, |
| 110 | DEVICE_REMOVED = 0x20000000 |
| 111 | }; |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 112 | |
| 113 | virtual uint32_t getDeviceClasses(int32_t deviceId) const = 0; |
| 114 | |
| 115 | virtual String8 getDeviceName(int32_t deviceId) const = 0; |
| 116 | |
| 117 | virtual int getAbsoluteInfo(int32_t deviceId, int axis, int *outMinValue, |
| 118 | int* outMaxValue, int* outFlat, int* outFuzz) const = 0; |
| 119 | |
| 120 | virtual status_t scancodeToKeycode(int32_t deviceId, int scancode, |
| 121 | int32_t* outKeycode, uint32_t* outFlags) const = 0; |
| 122 | |
| 123 | // exclude a particular device from opening |
| 124 | // this can be used to ignore input devices for sensors |
| 125 | virtual void addExcludedDevice(const char* deviceName) = 0; |
| 126 | |
| 127 | /* |
| 128 | * Wait for the next event to become available and return it. |
| 129 | * After returning, the EventHub holds onto a wake lock until the next call to getEvent. |
| 130 | * This ensures that the device will not go to sleep while the event is being processed. |
| 131 | * If the device needs to remain awake longer than that, then the caller is responsible |
| 132 | * for taking care of it (say, by poking the power manager user activity timer). |
| 133 | */ |
| 134 | virtual bool getEvent(int32_t* outDeviceId, int32_t* outType, |
| 135 | int32_t* outScancode, int32_t* outKeycode, uint32_t *outFlags, |
| 136 | int32_t* outValue, nsecs_t* outWhen) = 0; |
| 137 | |
| 138 | /* |
| 139 | * Query current input state. |
| 140 | * deviceId may be -1 to search for the device automatically, filtered by class. |
| 141 | * deviceClasses may be -1 to ignore device class while searching. |
| 142 | */ |
| 143 | virtual int32_t getScanCodeState(int32_t deviceId, int32_t deviceClasses, |
| 144 | int32_t scanCode) const = 0; |
| 145 | virtual int32_t getKeyCodeState(int32_t deviceId, int32_t deviceClasses, |
| 146 | int32_t keyCode) const = 0; |
| 147 | virtual int32_t getSwitchState(int32_t deviceId, int32_t deviceClasses, |
| 148 | int32_t sw) const = 0; |
| 149 | |
| 150 | /* |
| 151 | * Examine key input devices for specific framework keycode support |
| 152 | */ |
| 153 | virtual bool hasKeys(size_t numCodes, const int32_t* keyCodes, |
| 154 | uint8_t* outFlags) const = 0; |
| 155 | }; |
| 156 | |
| 157 | class EventHub : public EventHubInterface |
| 158 | { |
| 159 | public: |
| 160 | EventHub(); |
| 161 | |
| 162 | status_t errorCheck() const; |
| 163 | |
| 164 | virtual uint32_t getDeviceClasses(int32_t deviceId) const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 166 | virtual String8 getDeviceName(int32_t deviceId) const; |
| 167 | |
| 168 | virtual int getAbsoluteInfo(int32_t deviceId, int axis, int *outMinValue, |
| 169 | int* outMaxValue, int* outFlat, int* outFuzz) const; |
| 170 | |
| 171 | virtual status_t scancodeToKeycode(int32_t deviceId, int scancode, |
| 172 | int32_t* outKeycode, uint32_t* outFlags) const; |
| 173 | |
| 174 | virtual void addExcludedDevice(const char* deviceName); |
| 175 | |
| 176 | virtual int32_t getScanCodeState(int32_t deviceId, int32_t deviceClasses, |
| 177 | int32_t scanCode) const; |
| 178 | virtual int32_t getKeyCodeState(int32_t deviceId, int32_t deviceClasses, |
| 179 | int32_t keyCode) const; |
| 180 | virtual int32_t getSwitchState(int32_t deviceId, int32_t deviceClasses, |
| 181 | int32_t sw) const; |
| 182 | |
| 183 | virtual bool hasKeys(size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | |
| 185 | virtual bool getEvent(int32_t* outDeviceId, int32_t* outType, |
| 186 | int32_t* outScancode, int32_t* outKeycode, uint32_t *outFlags, |
| 187 | int32_t* outValue, nsecs_t* outWhen); |
Mike Lockwood | b441106 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 188 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | protected: |
| 190 | virtual ~EventHub(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | |
| 192 | private: |
| 193 | bool openPlatformInput(void); |
| 194 | int32_t convertDeviceKey_TI_P2(int code); |
| 195 | |
| 196 | int open_device(const char *device); |
| 197 | int close_device(const char *device); |
| 198 | int scan_dir(const char *dirname); |
| 199 | int read_notify(int nfd); |
| 200 | |
| 201 | status_t mError; |
| 202 | |
| 203 | struct device_t { |
| 204 | const int32_t id; |
| 205 | const String8 path; |
| 206 | String8 name; |
| 207 | uint32_t classes; |
| 208 | uint8_t* keyBitmask; |
| 209 | KeyLayoutMap* layoutMap; |
| 210 | String8 keylayoutFilename; |
| 211 | device_t* next; |
| 212 | |
Iliyan Malchev | 34193b3 | 2009-08-06 14:50:08 -0700 | [diff] [blame] | 213 | 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] | 214 | ~device_t(); |
| 215 | }; |
| 216 | |
| 217 | device_t* getDevice(int32_t deviceId) const; |
Dianne Hackborn | c591736 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 218 | bool hasKeycode(device_t* device, int keycode) const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 220 | int32_t getScanCodeStateLocked(device_t* device, int32_t scanCode) const; |
| 221 | int32_t getKeyCodeStateLocked(device_t* device, int32_t keyCode) const; |
| 222 | int32_t getSwitchStateLocked(device_t* device, int32_t sw) const; |
| 223 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | // Protect all internal state. |
| 225 | mutable Mutex mLock; |
| 226 | |
| 227 | bool mHaveFirstKeyboard; |
Dianne Hackborn | c968c3a | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 228 | 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] | 229 | |
| 230 | struct device_ent { |
| 231 | device_t* device; |
| 232 | uint32_t seq; |
| 233 | }; |
| 234 | device_ent *mDevicesById; |
| 235 | int mNumDevicesById; |
| 236 | |
| 237 | device_t *mOpeningDevices; |
| 238 | device_t *mClosingDevices; |
| 239 | |
| 240 | device_t **mDevices; |
| 241 | struct pollfd *mFDs; |
| 242 | int mFDCount; |
Mike Lockwood | b441106 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 243 | |
| 244 | bool mOpened; |
| 245 | List<String8> mExcludedDevices; |
| 246 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | // device ids that report particular switches. |
| 248 | #ifdef EV_SW |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 249 | int32_t mSwitches[SW_MAX + 1]; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | #endif |
| 251 | }; |
| 252 | |
| 253 | }; // namespace android |
| 254 | |
| 255 | #endif // _RUNTIME_EVENT_HUB_H |