blob: f4dc536cfbd30d8e62748044de709f323f4b3692 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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 Brown46b9ac02010-04-22 18:58:52 -070021#include <android/input.h>
Jeff Brown6b53e8d2010-11-10 16:03:06 -080022#include <ui/Keyboard.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023#include <utils/String8.h>
24#include <utils/threads.h>
Mathias Agopian3b4062e2009-05-31 19:13:00 -070025#include <utils/Log.h>
26#include <utils/threads.h>
27#include <utils/List.h>
28#include <utils/Errors.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029
30#include <linux/input.h>
31
Jeff Brown46b9ac02010-04-22 18:58:52 -070032/* These constants are not defined in linux/input.h but they are part of the multitouch
33 * input protocol. */
34
35#define ABS_MT_TOUCH_MAJOR 0x30 /* Major axis of touching ellipse */
36#define ABS_MT_TOUCH_MINOR 0x31 /* Minor axis (omit if circular) */
37#define ABS_MT_WIDTH_MAJOR 0x32 /* Major axis of approaching ellipse */
38#define ABS_MT_WIDTH_MINOR 0x33 /* Minor axis (omit if circular) */
39#define ABS_MT_ORIENTATION 0x34 /* Ellipse orientation */
40#define ABS_MT_POSITION_X 0x35 /* Center X ellipse position */
41#define ABS_MT_POSITION_Y 0x36 /* Center Y ellipse position */
42#define ABS_MT_TOOL_TYPE 0x37 /* Type of touching device (finger, pen, ...) */
43#define ABS_MT_BLOB_ID 0x38 /* Group a set of packets as a blob */
44#define ABS_MT_TRACKING_ID 0x39 /* Unique ID of initiated contact */
45#define ABS_MT_PRESSURE 0x3a /* Pressure on contact area */
46
47#define MT_TOOL_FINGER 0 /* Identifies a finger */
48#define MT_TOOL_PEN 1 /* Identifies a pen */
49
50#define SYN_MT_REPORT 2
51
52/* Convenience constants. */
53
54#define BTN_FIRST 0x100 // first button scancode
55#define BTN_LAST 0x15f // last button scancode
56
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057struct pollfd;
58
59namespace android {
60
61class KeyLayoutMap;
62
63/*
Jeff Brown6d0fec22010-07-23 21:28:06 -070064 * A raw event as retrieved from the EventHub.
65 */
66struct RawEvent {
67 nsecs_t when;
68 int32_t deviceId;
69 int32_t type;
70 int32_t scanCode;
71 int32_t keyCode;
72 int32_t value;
73 uint32_t flags;
74};
75
76/* Describes an absolute axis. */
77struct RawAbsoluteAxisInfo {
78 bool valid; // true if the information is valid, false otherwise
79
80 int32_t minValue; // minimum value
81 int32_t maxValue; // maximum value
82 int32_t flat; // center flat position, eg. flat == 8 means center is between -8 and 8
83 int32_t fuzz; // error tolerance, eg. fuzz == 4 means value is +/- 4 due to noise
84
85 inline int32_t getRange() { return maxValue - minValue; }
Jeff Brown8d608662010-08-30 03:02:23 -070086
87 inline void clear() {
88 valid = false;
89 minValue = 0;
90 maxValue = 0;
91 flat = 0;
92 fuzz = 0;
93 }
Jeff Brown6d0fec22010-07-23 21:28:06 -070094};
95
96/*
Jeff Brownc5ed5912010-07-14 18:48:53 -070097 * Input device classes.
98 */
99enum {
100 /* The input device is a keyboard. */
101 INPUT_DEVICE_CLASS_KEYBOARD = 0x00000001,
102
103 /* The input device is an alpha-numeric keyboard (not just a dial pad). */
104 INPUT_DEVICE_CLASS_ALPHAKEY = 0x00000002,
105
106 /* The input device is a touchscreen (either single-touch or multi-touch). */
107 INPUT_DEVICE_CLASS_TOUCHSCREEN = 0x00000004,
108
109 /* The input device is a trackball. */
110 INPUT_DEVICE_CLASS_TRACKBALL = 0x00000008,
111
112 /* The input device is a multi-touch touchscreen. */
113 INPUT_DEVICE_CLASS_TOUCHSCREEN_MT= 0x00000010,
114
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700115 /* The input device is a directional pad (implies keyboard, has DPAD keys). */
Jeff Brownc5ed5912010-07-14 18:48:53 -0700116 INPUT_DEVICE_CLASS_DPAD = 0x00000020,
117
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700118 /* The input device is a gamepad (implies keyboard, has BUTTON keys). */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700119 INPUT_DEVICE_CLASS_GAMEPAD = 0x00000040,
120
121 /* The input device has switches. */
122 INPUT_DEVICE_CLASS_SWITCH = 0x00000080,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700123};
124
125/*
Jeff Brown46b9ac02010-04-22 18:58:52 -0700126 * Grand Central Station for events.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 *
Jeff Brown46b9ac02010-04-22 18:58:52 -0700128 * The event hub aggregates input events received across all known input
129 * devices on the system, including devices that may be emulated by the simulator
130 * environment. In addition, the event hub generates fake input events to indicate
131 * when devices are added or removed.
132 *
133 * The event hub provies a stream of input events (via the getEvent function).
134 * It also supports querying the current actual state of input devices such as identifying
135 * which keys are currently down. Finally, the event hub keeps track of the capabilities of
136 * individual input devices, such as their class and the set of key codes that they support.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700138class EventHubInterface : public virtual RefBase {
139protected:
140 EventHubInterface() { }
141 virtual ~EventHubInterface() { }
142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143public:
Jeff Brown46b9ac02010-04-22 18:58:52 -0700144 // Synthetic raw event type codes produced when devices are added or removed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 enum {
Jeff Brown7342bb92010-10-01 18:55:43 -0700146 // Sent when a device is added.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 DEVICE_ADDED = 0x10000000,
Jeff Brown7342bb92010-10-01 18:55:43 -0700148 // Sent when a device is removed.
149 DEVICE_REMOVED = 0x20000000,
150 // Sent when all added/removed devices from the most recent scan have been reported.
151 // This event is always sent at least once.
152 FINISHED_DEVICE_SCAN = 0x30000000,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 };
Jeff Brown46b9ac02010-04-22 18:58:52 -0700154
155 virtual uint32_t getDeviceClasses(int32_t deviceId) const = 0;
156
157 virtual String8 getDeviceName(int32_t deviceId) const = 0;
158
Jeff Brown6d0fec22010-07-23 21:28:06 -0700159 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
160 RawAbsoluteAxisInfo* outAxisInfo) const = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700161
162 virtual status_t scancodeToKeycode(int32_t deviceId, int scancode,
163 int32_t* outKeycode, uint32_t* outFlags) const = 0;
164
165 // exclude a particular device from opening
166 // this can be used to ignore input devices for sensors
167 virtual void addExcludedDevice(const char* deviceName) = 0;
168
169 /*
170 * Wait for the next event to become available and return it.
171 * After returning, the EventHub holds onto a wake lock until the next call to getEvent.
172 * This ensures that the device will not go to sleep while the event is being processed.
173 * If the device needs to remain awake longer than that, then the caller is responsible
174 * for taking care of it (say, by poking the power manager user activity timer).
175 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700176 virtual bool getEvent(RawEvent* outEvent) = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700177
178 /*
179 * Query current input state.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700180 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700181 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0;
182 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0;
183 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700184
185 /*
186 * Examine key input devices for specific framework keycode support
187 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700188 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700189 uint8_t* outFlags) const = 0;
Jeff Brownf2f48712010-10-01 17:46:21 -0700190
Jeff Brown497a92c2010-09-12 17:55:08 -0700191 virtual bool hasLed(int32_t deviceId, int32_t led) const = 0;
192 virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0;
193
Jeff Brownf2f48712010-10-01 17:46:21 -0700194 virtual void dump(String8& dump) = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700195};
196
197class EventHub : public EventHubInterface
198{
199public:
200 EventHub();
201
202 status_t errorCheck() const;
203
204 virtual uint32_t getDeviceClasses(int32_t deviceId) const;
Jeff Brown497a92c2010-09-12 17:55:08 -0700205
Jeff Brown46b9ac02010-04-22 18:58:52 -0700206 virtual String8 getDeviceName(int32_t deviceId) const;
Jeff Brown497a92c2010-09-12 17:55:08 -0700207
Jeff Brown6d0fec22010-07-23 21:28:06 -0700208 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
209 RawAbsoluteAxisInfo* outAxisInfo) const;
210
Jeff Brown46b9ac02010-04-22 18:58:52 -0700211 virtual status_t scancodeToKeycode(int32_t deviceId, int scancode,
212 int32_t* outKeycode, uint32_t* outFlags) const;
213
214 virtual void addExcludedDevice(const char* deviceName);
215
Jeff Brown6d0fec22010-07-23 21:28:06 -0700216 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const;
217 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const;
218 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700219
Jeff Brown6d0fec22010-07-23 21:28:06 -0700220 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
221 const int32_t* keyCodes, uint8_t* outFlags) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222
Jeff Brown6d0fec22010-07-23 21:28:06 -0700223 virtual bool getEvent(RawEvent* outEvent);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400224
Jeff Brown497a92c2010-09-12 17:55:08 -0700225 virtual bool hasLed(int32_t deviceId, int32_t led) const;
226 virtual void setLedState(int32_t deviceId, int32_t led, bool on);
227
Jeff Brownf2f48712010-10-01 17:46:21 -0700228 virtual void dump(String8& dump);
229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230protected:
231 virtual ~EventHub();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232
233private:
234 bool openPlatformInput(void);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235
Jeff Brown7342bb92010-10-01 18:55:43 -0700236 int openDevice(const char *device);
237 int closeDevice(const char *device);
238 int scanDir(const char *dirname);
239 int readNotify(int nfd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240
241 status_t mError;
242
243 struct device_t {
244 const int32_t id;
245 const String8 path;
246 String8 name;
247 uint32_t classes;
248 uint8_t* keyBitmask;
249 KeyLayoutMap* layoutMap;
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800250 KeyMapInfo keyMapInfo;
Jens Gulinc4554b92010-06-22 22:21:57 +0200251 int fd;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 device_t* next;
253
Iliyan Malchevfc2ebc42009-08-06 14:50:08 -0700254 device_t(int32_t _id, const char* _path, const char* name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 ~device_t();
256 };
257
Jeff Brownf2f48712010-10-01 17:46:21 -0700258 device_t* getDeviceLocked(int32_t deviceId) const;
259 bool hasKeycodeLocked(device_t* device, int keycode) const;
Jeff Brown497a92c2010-09-12 17:55:08 -0700260
Jeff Brown46b9ac02010-04-22 18:58:52 -0700261 int32_t getScanCodeStateLocked(device_t* device, int32_t scanCode) const;
262 int32_t getKeyCodeStateLocked(device_t* device, int32_t keyCode) const;
263 int32_t getSwitchStateLocked(device_t* device, int32_t sw) const;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700264 bool markSupportedKeyCodesLocked(device_t* device, size_t numCodes,
265 const int32_t* keyCodes, uint8_t* outFlags) const;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700266
Jeff Brown497a92c2010-09-12 17:55:08 -0700267 void configureKeyMap(device_t* device);
Jeff Brown497a92c2010-09-12 17:55:08 -0700268 void setKeyboardProperties(device_t* device, bool firstKeyboard);
269 void clearKeyboardProperties(device_t* device, bool firstKeyboard);
270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 // Protect all internal state.
272 mutable Mutex mLock;
273
274 bool mHaveFirstKeyboard;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700275 int32_t mFirstKeyboardId; // the API is that the built-in keyboard is id 0, so map it
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276
277 struct device_ent {
278 device_t* device;
279 uint32_t seq;
280 };
281 device_ent *mDevicesById;
282 int mNumDevicesById;
283
284 device_t *mOpeningDevices;
285 device_t *mClosingDevices;
286
287 device_t **mDevices;
288 struct pollfd *mFDs;
289 int mFDCount;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400290
291 bool mOpened;
Jeff Brown7342bb92010-10-01 18:55:43 -0700292 bool mNeedToSendFinishedDeviceScan;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400293 List<String8> mExcludedDevices;
294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 // device ids that report particular switches.
296#ifdef EV_SW
Jeff Brown46b9ac02010-04-22 18:58:52 -0700297 int32_t mSwitches[SW_MAX + 1];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298#endif
Jeff Browncc2e7172010-08-17 16:48:25 -0700299
300 static const int INPUT_BUFFER_SIZE = 64;
301 struct input_event mInputBufferData[INPUT_BUFFER_SIZE];
302 int32_t mInputBufferIndex;
303 int32_t mInputBufferCount;
304 int32_t mInputDeviceIndex;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305};
306
307}; // namespace android
308
309#endif // _RUNTIME_EVENT_HUB_H