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