blob: 6c798a64babb01d244ffa0a2998896c8a8de0ba7 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-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 Browne839a582010-04-22 18:58:52 -070021#include <android/input.h>
Jeff Browna3477c82010-11-10 16:03:06 -080022#include <ui/Keyboard.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023#include <utils/String8.h>
24#include <utils/threads.h>
Mathias Agopiane0c32202009-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>
Jeff Brown66888372010-11-29 17:37:49 -080029#include <utils/PropertyMap.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030
31#include <linux/input.h>
32
Jeff Browne839a582010-04-22 18:58:52 -070033/* These constants are not defined in linux/input.h but they are part of the multitouch
34 * input protocol. */
35
36#define ABS_MT_TOUCH_MAJOR 0x30 /* Major axis of touching ellipse */
37#define ABS_MT_TOUCH_MINOR 0x31 /* Minor axis (omit if circular) */
38#define ABS_MT_WIDTH_MAJOR 0x32 /* Major axis of approaching ellipse */
39#define ABS_MT_WIDTH_MINOR 0x33 /* Minor axis (omit if circular) */
40#define ABS_MT_ORIENTATION 0x34 /* Ellipse orientation */
41#define ABS_MT_POSITION_X 0x35 /* Center X ellipse position */
42#define ABS_MT_POSITION_Y 0x36 /* Center Y ellipse position */
43#define ABS_MT_TOOL_TYPE 0x37 /* Type of touching device (finger, pen, ...) */
44#define ABS_MT_BLOB_ID 0x38 /* Group a set of packets as a blob */
45#define ABS_MT_TRACKING_ID 0x39 /* Unique ID of initiated contact */
46#define ABS_MT_PRESSURE 0x3a /* Pressure on contact area */
47
48#define MT_TOOL_FINGER 0 /* Identifies a finger */
49#define MT_TOOL_PEN 1 /* Identifies a pen */
50
51#define SYN_MT_REPORT 2
52
53/* Convenience constants. */
54
55#define BTN_FIRST 0x100 // first button scancode
56#define BTN_LAST 0x15f // last button scancode
57
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058struct pollfd;
59
60namespace android {
61
62class KeyLayoutMap;
63
64/*
Jeff Browne57e8952010-07-23 21:28:06 -070065 * A raw event as retrieved from the EventHub.
66 */
67struct RawEvent {
68 nsecs_t when;
69 int32_t deviceId;
70 int32_t type;
71 int32_t scanCode;
72 int32_t keyCode;
73 int32_t value;
74 uint32_t flags;
75};
76
77/* Describes an absolute axis. */
78struct RawAbsoluteAxisInfo {
79 bool valid; // true if the information is valid, false otherwise
80
81 int32_t minValue; // minimum value
82 int32_t maxValue; // maximum value
83 int32_t flat; // center flat position, eg. flat == 8 means center is between -8 and 8
84 int32_t fuzz; // error tolerance, eg. fuzz == 4 means value is +/- 4 due to noise
85
86 inline int32_t getRange() { return maxValue - minValue; }
Jeff Brown38a7fab2010-08-30 03:02:23 -070087
88 inline void clear() {
89 valid = false;
90 minValue = 0;
91 maxValue = 0;
92 flat = 0;
93 fuzz = 0;
94 }
Jeff Browne57e8952010-07-23 21:28:06 -070095};
96
97/*
Jeff Brown5c1ed842010-07-14 18:48:53 -070098 * Input device classes.
99 */
100enum {
101 /* The input device is a keyboard. */
102 INPUT_DEVICE_CLASS_KEYBOARD = 0x00000001,
103
104 /* The input device is an alpha-numeric keyboard (not just a dial pad). */
105 INPUT_DEVICE_CLASS_ALPHAKEY = 0x00000002,
106
107 /* The input device is a touchscreen (either single-touch or multi-touch). */
108 INPUT_DEVICE_CLASS_TOUCHSCREEN = 0x00000004,
109
110 /* The input device is a trackball. */
111 INPUT_DEVICE_CLASS_TRACKBALL = 0x00000008,
112
113 /* The input device is a multi-touch touchscreen. */
114 INPUT_DEVICE_CLASS_TOUCHSCREEN_MT= 0x00000010,
115
Jeff Brownb5764e02010-09-14 18:03:38 -0700116 /* The input device is a directional pad (implies keyboard, has DPAD keys). */
Jeff Brown5c1ed842010-07-14 18:48:53 -0700117 INPUT_DEVICE_CLASS_DPAD = 0x00000020,
118
Jeff Brownb5764e02010-09-14 18:03:38 -0700119 /* The input device is a gamepad (implies keyboard, has BUTTON keys). */
Jeff Browne57e8952010-07-23 21:28:06 -0700120 INPUT_DEVICE_CLASS_GAMEPAD = 0x00000040,
121
122 /* The input device has switches. */
123 INPUT_DEVICE_CLASS_SWITCH = 0x00000080,
Jeff Brown5c1ed842010-07-14 18:48:53 -0700124};
125
126/*
Jeff Browne839a582010-04-22 18:58:52 -0700127 * Grand Central Station for events.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128 *
Jeff Browne839a582010-04-22 18:58:52 -0700129 * The event hub aggregates input events received across all known input
130 * devices on the system, including devices that may be emulated by the simulator
131 * environment. In addition, the event hub generates fake input events to indicate
132 * when devices are added or removed.
133 *
134 * The event hub provies a stream of input events (via the getEvent function).
135 * It also supports querying the current actual state of input devices such as identifying
136 * which keys are currently down. Finally, the event hub keeps track of the capabilities of
137 * individual input devices, such as their class and the set of key codes that they support.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138 */
Jeff Browne839a582010-04-22 18:58:52 -0700139class EventHubInterface : public virtual RefBase {
140protected:
141 EventHubInterface() { }
142 virtual ~EventHubInterface() { }
143
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144public:
Jeff Browne839a582010-04-22 18:58:52 -0700145 // Synthetic raw event type codes produced when devices are added or removed.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146 enum {
Jeff Brown1ad00e92010-10-01 18:55:43 -0700147 // Sent when a device is added.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800148 DEVICE_ADDED = 0x10000000,
Jeff Brown1ad00e92010-10-01 18:55:43 -0700149 // Sent when a device is removed.
150 DEVICE_REMOVED = 0x20000000,
151 // Sent when all added/removed devices from the most recent scan have been reported.
152 // This event is always sent at least once.
153 FINISHED_DEVICE_SCAN = 0x30000000,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154 };
Jeff Browne839a582010-04-22 18:58:52 -0700155
156 virtual uint32_t getDeviceClasses(int32_t deviceId) const = 0;
157
158 virtual String8 getDeviceName(int32_t deviceId) const = 0;
159
Jeff Brown66888372010-11-29 17:37:49 -0800160 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const = 0;
161
Jeff Browne57e8952010-07-23 21:28:06 -0700162 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
163 RawAbsoluteAxisInfo* outAxisInfo) const = 0;
Jeff Browne839a582010-04-22 18:58:52 -0700164
165 virtual status_t scancodeToKeycode(int32_t deviceId, int scancode,
166 int32_t* outKeycode, uint32_t* outFlags) const = 0;
167
168 // exclude a particular device from opening
169 // this can be used to ignore input devices for sensors
170 virtual void addExcludedDevice(const char* deviceName) = 0;
171
172 /*
173 * Wait for the next event to become available and return it.
174 * After returning, the EventHub holds onto a wake lock until the next call to getEvent.
175 * This ensures that the device will not go to sleep while the event is being processed.
176 * If the device needs to remain awake longer than that, then the caller is responsible
177 * for taking care of it (say, by poking the power manager user activity timer).
178 */
Jeff Browne57e8952010-07-23 21:28:06 -0700179 virtual bool getEvent(RawEvent* outEvent) = 0;
Jeff Browne839a582010-04-22 18:58:52 -0700180
181 /*
182 * Query current input state.
Jeff Browne839a582010-04-22 18:58:52 -0700183 */
Jeff Browne57e8952010-07-23 21:28:06 -0700184 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0;
185 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0;
186 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0;
Jeff Browne839a582010-04-22 18:58:52 -0700187
188 /*
189 * Examine key input devices for specific framework keycode support
190 */
Jeff Browne57e8952010-07-23 21:28:06 -0700191 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
Jeff Browne839a582010-04-22 18:58:52 -0700192 uint8_t* outFlags) const = 0;
Jeff Brown2806e382010-10-01 17:46:21 -0700193
Jeff Brown6a817e22010-09-12 17:55:08 -0700194 virtual bool hasLed(int32_t deviceId, int32_t led) const = 0;
195 virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0;
196
Jeff Brown2806e382010-10-01 17:46:21 -0700197 virtual void dump(String8& dump) = 0;
Jeff Browne839a582010-04-22 18:58:52 -0700198};
199
200class EventHub : public EventHubInterface
201{
202public:
203 EventHub();
204
205 status_t errorCheck() const;
206
207 virtual uint32_t getDeviceClasses(int32_t deviceId) const;
Jeff Brown6a817e22010-09-12 17:55:08 -0700208
Jeff Browne839a582010-04-22 18:58:52 -0700209 virtual String8 getDeviceName(int32_t deviceId) const;
Jeff Brown6a817e22010-09-12 17:55:08 -0700210
Jeff Brown66888372010-11-29 17:37:49 -0800211 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const;
212
Jeff Browne57e8952010-07-23 21:28:06 -0700213 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
214 RawAbsoluteAxisInfo* outAxisInfo) const;
215
Jeff Browne839a582010-04-22 18:58:52 -0700216 virtual status_t scancodeToKeycode(int32_t deviceId, int scancode,
217 int32_t* outKeycode, uint32_t* outFlags) const;
218
219 virtual void addExcludedDevice(const char* deviceName);
220
Jeff Browne57e8952010-07-23 21:28:06 -0700221 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const;
222 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const;
223 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const;
Jeff Browne839a582010-04-22 18:58:52 -0700224
Jeff Browne57e8952010-07-23 21:28:06 -0700225 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
226 const int32_t* keyCodes, uint8_t* outFlags) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227
Jeff Browne57e8952010-07-23 21:28:06 -0700228 virtual bool getEvent(RawEvent* outEvent);
Mike Lockwoodb4411062009-07-16 11:11:18 -0400229
Jeff Brown6a817e22010-09-12 17:55:08 -0700230 virtual bool hasLed(int32_t deviceId, int32_t led) const;
231 virtual void setLedState(int32_t deviceId, int32_t led, bool on);
232
Jeff Brown2806e382010-10-01 17:46:21 -0700233 virtual void dump(String8& dump);
234
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235protected:
236 virtual ~EventHub();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237
238private:
239 bool openPlatformInput(void);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240
Jeff Brown1ad00e92010-10-01 18:55:43 -0700241 int openDevice(const char *device);
242 int closeDevice(const char *device);
243 int scanDir(const char *dirname);
244 int readNotify(int nfd);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800245
246 status_t mError;
247
248 struct device_t {
249 const int32_t id;
250 const String8 path;
251 String8 name;
252 uint32_t classes;
253 uint8_t* keyBitmask;
254 KeyLayoutMap* layoutMap;
Jeff Brown66888372010-11-29 17:37:49 -0800255 String8 configurationFile;
256 PropertyMap* configuration;
Jeff Browna3477c82010-11-10 16:03:06 -0800257 KeyMapInfo keyMapInfo;
Jens Gulin7dcaa582010-06-22 22:21:57 +0200258 int fd;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259 device_t* next;
260
Iliyan Malchev34193b32009-08-06 14:50:08 -0700261 device_t(int32_t _id, const char* _path, const char* name);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262 ~device_t();
263 };
264
Jeff Brown2806e382010-10-01 17:46:21 -0700265 device_t* getDeviceLocked(int32_t deviceId) const;
266 bool hasKeycodeLocked(device_t* device, int keycode) const;
Jeff Brown6a817e22010-09-12 17:55:08 -0700267
Jeff Browne839a582010-04-22 18:58:52 -0700268 int32_t getScanCodeStateLocked(device_t* device, int32_t scanCode) const;
269 int32_t getKeyCodeStateLocked(device_t* device, int32_t keyCode) const;
270 int32_t getSwitchStateLocked(device_t* device, int32_t sw) const;
Jeff Browne57e8952010-07-23 21:28:06 -0700271 bool markSupportedKeyCodesLocked(device_t* device, size_t numCodes,
272 const int32_t* keyCodes, uint8_t* outFlags) const;
Jeff Browne839a582010-04-22 18:58:52 -0700273
Jeff Brown66888372010-11-29 17:37:49 -0800274 void loadConfiguration(device_t* device);
Jeff Brown6a817e22010-09-12 17:55:08 -0700275 void configureKeyMap(device_t* device);
Jeff Brown6a817e22010-09-12 17:55:08 -0700276 void setKeyboardProperties(device_t* device, bool firstKeyboard);
277 void clearKeyboardProperties(device_t* device, bool firstKeyboard);
278
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800279 // Protect all internal state.
280 mutable Mutex mLock;
281
282 bool mHaveFirstKeyboard;
Dianne Hackbornc968c3a2009-07-14 12:06:54 -0700283 int32_t mFirstKeyboardId; // the API is that the built-in keyboard is id 0, so map it
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800284
285 struct device_ent {
286 device_t* device;
287 uint32_t seq;
288 };
289 device_ent *mDevicesById;
290 int mNumDevicesById;
291
292 device_t *mOpeningDevices;
293 device_t *mClosingDevices;
294
295 device_t **mDevices;
296 struct pollfd *mFDs;
297 int mFDCount;
Mike Lockwoodb4411062009-07-16 11:11:18 -0400298
299 bool mOpened;
Jeff Brown1ad00e92010-10-01 18:55:43 -0700300 bool mNeedToSendFinishedDeviceScan;
Mike Lockwoodb4411062009-07-16 11:11:18 -0400301 List<String8> mExcludedDevices;
302
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303 // device ids that report particular switches.
304#ifdef EV_SW
Jeff Browne839a582010-04-22 18:58:52 -0700305 int32_t mSwitches[SW_MAX + 1];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800306#endif
Jeff Brown82763072010-08-17 16:48:25 -0700307
308 static const int INPUT_BUFFER_SIZE = 64;
309 struct input_event mInputBufferData[INPUT_BUFFER_SIZE];
310 int32_t mInputBufferIndex;
311 int32_t mInputBufferCount;
312 int32_t mInputDeviceIndex;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800313};
314
315}; // namespace android
316
317#endif // _RUNTIME_EVENT_HUB_H