blob: d077d0e4246ea40460f320a26d80e8ce23acbff6 [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 Brown46b9ac0a2010-04-22 18:58:52 -070021#include <android/input.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022#include <utils/String8.h>
23#include <utils/threads.h>
Mathias Agopian3b4062e2009-05-31 19:13:00 -070024#include <utils/Log.h>
25#include <utils/threads.h>
26#include <utils/List.h>
27#include <utils/Errors.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
29#include <linux/input.h>
30
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070031/* 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 Project9066cfe2009-03-03 19:31:44 -080056struct pollfd;
57
58namespace android {
59
60class KeyLayoutMap;
61
62/*
Jeff Brown6d0fec22010-07-23 21:28:06 -070063 * A raw event as retrieved from the EventHub.
64 */
65struct 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. */
76struct 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; }
Jeff Brown8d608662010-08-30 03:02:23 -070085
86 inline void clear() {
87 valid = false;
88 minValue = 0;
89 maxValue = 0;
90 flat = 0;
91 fuzz = 0;
92 }
Jeff Brown6d0fec22010-07-23 21:28:06 -070093};
94
95/*
Jeff Brownc5ed5912010-07-14 18:48:53 -070096 * Input device classes.
97 */
98enum {
99 /* The input device is a keyboard. */
100 INPUT_DEVICE_CLASS_KEYBOARD = 0x00000001,
101
102 /* The input device is an alpha-numeric keyboard (not just a dial pad). */
103 INPUT_DEVICE_CLASS_ALPHAKEY = 0x00000002,
104
105 /* The input device is a touchscreen (either single-touch or multi-touch). */
106 INPUT_DEVICE_CLASS_TOUCHSCREEN = 0x00000004,
107
108 /* The input device is a trackball. */
109 INPUT_DEVICE_CLASS_TRACKBALL = 0x00000008,
110
111 /* The input device is a multi-touch touchscreen. */
112 INPUT_DEVICE_CLASS_TOUCHSCREEN_MT= 0x00000010,
113
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700114 /* The input device is a directional pad (implies keyboard, has DPAD keys). */
Jeff Brownc5ed5912010-07-14 18:48:53 -0700115 INPUT_DEVICE_CLASS_DPAD = 0x00000020,
116
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700117 /* The input device is a gamepad (implies keyboard, has BUTTON keys). */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700118 INPUT_DEVICE_CLASS_GAMEPAD = 0x00000040,
119
120 /* The input device has switches. */
121 INPUT_DEVICE_CLASS_SWITCH = 0x00000080,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700122};
123
124/*
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700125 * Grand Central Station for events.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 *
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700127 * The event hub aggregates input events received across all known input
128 * devices on the system, including devices that may be emulated by the simulator
129 * environment. In addition, the event hub generates fake input events to indicate
130 * when devices are added or removed.
131 *
132 * The event hub provies a stream of input events (via the getEvent function).
133 * It also supports querying the current actual state of input devices such as identifying
134 * which keys are currently down. Finally, the event hub keeps track of the capabilities of
135 * 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 -0800136 */
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700137class EventHubInterface : public virtual RefBase {
138protected:
139 EventHubInterface() { }
140 virtual ~EventHubInterface() { }
141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142public:
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700143 // Synthetic raw event type codes produced when devices are added or removed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 enum {
145 DEVICE_ADDED = 0x10000000,
146 DEVICE_REMOVED = 0x20000000
147 };
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700148
149 virtual uint32_t getDeviceClasses(int32_t deviceId) const = 0;
150
151 virtual String8 getDeviceName(int32_t deviceId) const = 0;
152
Jeff Brown6d0fec22010-07-23 21:28:06 -0700153 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
154 RawAbsoluteAxisInfo* outAxisInfo) const = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700155
156 virtual status_t scancodeToKeycode(int32_t deviceId, int scancode,
157 int32_t* outKeycode, uint32_t* outFlags) const = 0;
158
159 // exclude a particular device from opening
160 // this can be used to ignore input devices for sensors
161 virtual void addExcludedDevice(const char* deviceName) = 0;
162
163 /*
164 * Wait for the next event to become available and return it.
165 * After returning, the EventHub holds onto a wake lock until the next call to getEvent.
166 * This ensures that the device will not go to sleep while the event is being processed.
167 * If the device needs to remain awake longer than that, then the caller is responsible
168 * for taking care of it (say, by poking the power manager user activity timer).
169 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700170 virtual bool getEvent(RawEvent* outEvent) = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700171
172 /*
173 * Query current input state.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700174 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700175 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0;
176 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0;
177 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700178
179 /*
180 * Examine key input devices for specific framework keycode support
181 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700182 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700183 uint8_t* outFlags) const = 0;
Jeff Brownf2f487182010-10-01 17:46:21 -0700184
185 virtual void dump(String8& dump) = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700186};
187
188class EventHub : public EventHubInterface
189{
190public:
191 EventHub();
192
193 status_t errorCheck() const;
194
195 virtual uint32_t getDeviceClasses(int32_t deviceId) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700197 virtual String8 getDeviceName(int32_t deviceId) const;
198
Jeff Brown6d0fec22010-07-23 21:28:06 -0700199 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
200 RawAbsoluteAxisInfo* outAxisInfo) const;
201
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700202 virtual status_t scancodeToKeycode(int32_t deviceId, int scancode,
203 int32_t* outKeycode, uint32_t* outFlags) const;
204
205 virtual void addExcludedDevice(const char* deviceName);
206
Jeff Brown6d0fec22010-07-23 21:28:06 -0700207 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const;
208 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const;
209 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700210
Jeff Brown6d0fec22010-07-23 21:28:06 -0700211 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
212 const int32_t* keyCodes, uint8_t* outFlags) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213
Jeff Brown6d0fec22010-07-23 21:28:06 -0700214 virtual bool getEvent(RawEvent* outEvent);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400215
Jeff Brownf2f487182010-10-01 17:46:21 -0700216 virtual void dump(String8& dump);
217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218protected:
219 virtual ~EventHub();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220
221private:
222 bool openPlatformInput(void);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223
224 int open_device(const char *device);
225 int close_device(const char *device);
226 int scan_dir(const char *dirname);
227 int read_notify(int nfd);
228
229 status_t mError;
230
231 struct device_t {
232 const int32_t id;
233 const String8 path;
234 String8 name;
235 uint32_t classes;
236 uint8_t* keyBitmask;
237 KeyLayoutMap* layoutMap;
238 String8 keylayoutFilename;
Jens Gulinc4554b92010-06-22 22:21:57 +0200239 int fd;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 device_t* next;
241
Iliyan Malchevfc2ebc42009-08-06 14:50:08 -0700242 device_t(int32_t _id, const char* _path, const char* name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 ~device_t();
244 };
245
Jeff Brownf2f487182010-10-01 17:46:21 -0700246 device_t* getDeviceLocked(int32_t deviceId) const;
247 bool hasKeycodeLocked(device_t* device, int keycode) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700249 int32_t getScanCodeStateLocked(device_t* device, int32_t scanCode) const;
250 int32_t getKeyCodeStateLocked(device_t* device, int32_t keyCode) const;
251 int32_t getSwitchStateLocked(device_t* device, int32_t sw) const;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700252 bool markSupportedKeyCodesLocked(device_t* device, size_t numCodes,
253 const int32_t* keyCodes, uint8_t* outFlags) const;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 // Protect all internal state.
256 mutable Mutex mLock;
257
258 bool mHaveFirstKeyboard;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700259 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 -0800260
261 struct device_ent {
262 device_t* device;
263 uint32_t seq;
264 };
265 device_ent *mDevicesById;
266 int mNumDevicesById;
267
268 device_t *mOpeningDevices;
269 device_t *mClosingDevices;
270
271 device_t **mDevices;
272 struct pollfd *mFDs;
273 int mFDCount;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400274
275 bool mOpened;
276 List<String8> mExcludedDevices;
277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 // device ids that report particular switches.
279#ifdef EV_SW
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700280 int32_t mSwitches[SW_MAX + 1];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281#endif
Jeff Browncc2e7172010-08-17 16:48:25 -0700282
283 static const int INPUT_BUFFER_SIZE = 64;
284 struct input_event mInputBufferData[INPUT_BUFFER_SIZE];
285 int32_t mInputBufferIndex;
286 int32_t mInputBufferCount;
287 int32_t mInputDeviceIndex;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288};
289
290}; // namespace android
291
292#endif // _RUNTIME_EVENT_HUB_H