blob: 4d26a9565efd11c5511320496ed656dc71916d81 [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 Brown90655042010-12-02 13:50:46 -080021#include <ui/Input.h>
Jeff Brown6b53e8d2010-11-10 16:03:06 -080022#include <ui/Keyboard.h>
Jeff Brown90655042010-12-02 13:50:46 -080023#include <ui/KeyLayoutMap.h>
24#include <ui/KeyCharacterMap.h>
25#include <ui/VirtualKeyMap.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026#include <utils/String8.h>
27#include <utils/threads.h>
Mathias Agopian3b4062e2009-05-31 19:13:00 -070028#include <utils/Log.h>
29#include <utils/threads.h>
30#include <utils/List.h>
31#include <utils/Errors.h>
Jeff Brown47e6b1b2010-11-29 17:37:49 -080032#include <utils/PropertyMap.h>
Jeff Brown90655042010-12-02 13:50:46 -080033#include <utils/Vector.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034
35#include <linux/input.h>
36
Jeff Brown46b9ac02010-04-22 18:58:52 -070037/* These constants are not defined in linux/input.h but they are part of the multitouch
38 * input protocol. */
39
40#define ABS_MT_TOUCH_MAJOR 0x30 /* Major axis of touching ellipse */
41#define ABS_MT_TOUCH_MINOR 0x31 /* Minor axis (omit if circular) */
42#define ABS_MT_WIDTH_MAJOR 0x32 /* Major axis of approaching ellipse */
43#define ABS_MT_WIDTH_MINOR 0x33 /* Minor axis (omit if circular) */
44#define ABS_MT_ORIENTATION 0x34 /* Ellipse orientation */
45#define ABS_MT_POSITION_X 0x35 /* Center X ellipse position */
46#define ABS_MT_POSITION_Y 0x36 /* Center Y ellipse position */
47#define ABS_MT_TOOL_TYPE 0x37 /* Type of touching device (finger, pen, ...) */
48#define ABS_MT_BLOB_ID 0x38 /* Group a set of packets as a blob */
49#define ABS_MT_TRACKING_ID 0x39 /* Unique ID of initiated contact */
50#define ABS_MT_PRESSURE 0x3a /* Pressure on contact area */
51
52#define MT_TOOL_FINGER 0 /* Identifies a finger */
53#define MT_TOOL_PEN 1 /* Identifies a pen */
54
55#define SYN_MT_REPORT 2
56
57/* Convenience constants. */
58
59#define BTN_FIRST 0x100 // first button scancode
60#define BTN_LAST 0x15f // last button scancode
61
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062struct pollfd;
63
64namespace android {
65
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066/*
Jeff Brown6d0fec22010-07-23 21:28:06 -070067 * A raw event as retrieved from the EventHub.
68 */
69struct RawEvent {
70 nsecs_t when;
71 int32_t deviceId;
72 int32_t type;
73 int32_t scanCode;
74 int32_t keyCode;
75 int32_t value;
76 uint32_t flags;
77};
78
79/* Describes an absolute axis. */
80struct RawAbsoluteAxisInfo {
81 bool valid; // true if the information is valid, false otherwise
82
83 int32_t minValue; // minimum value
84 int32_t maxValue; // maximum value
85 int32_t flat; // center flat position, eg. flat == 8 means center is between -8 and 8
86 int32_t fuzz; // error tolerance, eg. fuzz == 4 means value is +/- 4 due to noise
87
Jeff Brown8d608662010-08-30 03:02:23 -070088 inline void clear() {
89 valid = false;
90 minValue = 0;
91 maxValue = 0;
92 flat = 0;
93 fuzz = 0;
94 }
Jeff Brown6d0fec22010-07-23 21:28:06 -070095};
96
97/*
Jeff Brownc5ed5912010-07-14 18:48:53 -070098 * Input device classes.
99 */
100enum {
Jeff Browncb1404e2011-01-15 18:14:15 -0800101 /* The input device is a keyboard or has buttons. */
Jeff Brownc5ed5912010-07-14 18:48:53 -0700102 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
Jeff Brown58a2da82011-01-25 16:02:22 -0800107 /* The input device is a touchscreen or a touchpad (either single-touch or multi-touch). */
108 INPUT_DEVICE_CLASS_TOUCH = 0x00000004,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700109
Jeff Brown83c09682010-12-23 17:50:18 -0800110 /* The input device is a cursor device such as a trackball or mouse. */
111 INPUT_DEVICE_CLASS_CURSOR = 0x00000008,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700112
113 /* The input device is a multi-touch touchscreen. */
Jeff Brown58a2da82011-01-25 16:02:22 -0800114 INPUT_DEVICE_CLASS_TOUCH_MT = 0x00000010,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700115
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700116 /* The input device is a directional pad (implies keyboard, has DPAD keys). */
Jeff Brownc5ed5912010-07-14 18:48:53 -0700117 INPUT_DEVICE_CLASS_DPAD = 0x00000020,
118
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700119 /* The input device is a gamepad (implies keyboard, has BUTTON keys). */
Jeff Brown6d0fec22010-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 Browncb1404e2011-01-15 18:14:15 -0800124
125 /* The input device is a joystick (implies gamepad, has joystick absolute axes). */
126 INPUT_DEVICE_CLASS_JOYSTICK = 0x00000100,
Jeff Brown56194eb2011-03-02 19:23:13 -0800127
128 /* The input device is external (not built-in). */
129 INPUT_DEVICE_CLASS_EXTERNAL = 0x80000000,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700130};
131
132/*
Jeff Brown46b9ac02010-04-22 18:58:52 -0700133 * Grand Central Station for events.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 *
Jeff Brown46b9ac02010-04-22 18:58:52 -0700135 * The event hub aggregates input events received across all known input
136 * devices on the system, including devices that may be emulated by the simulator
137 * environment. In addition, the event hub generates fake input events to indicate
138 * when devices are added or removed.
139 *
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800140 * The event hub provides a stream of input events (via the getEvent function).
Jeff Brown46b9ac02010-04-22 18:58:52 -0700141 * It also supports querying the current actual state of input devices such as identifying
142 * which keys are currently down. Finally, the event hub keeps track of the capabilities of
143 * 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 -0800144 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700145class EventHubInterface : public virtual RefBase {
146protected:
147 EventHubInterface() { }
148 virtual ~EventHubInterface() { }
149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150public:
Jeff Brown46b9ac02010-04-22 18:58:52 -0700151 // Synthetic raw event type codes produced when devices are added or removed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 enum {
Jeff Brown7342bb92010-10-01 18:55:43 -0700153 // Sent when a device is added.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 DEVICE_ADDED = 0x10000000,
Jeff Brown7342bb92010-10-01 18:55:43 -0700155 // Sent when a device is removed.
156 DEVICE_REMOVED = 0x20000000,
157 // Sent when all added/removed devices from the most recent scan have been reported.
158 // This event is always sent at least once.
159 FINISHED_DEVICE_SCAN = 0x30000000,
Jeff Brownb7198742011-03-18 18:14:26 -0700160
161 FIRST_SYNTHETIC_EVENT = DEVICE_ADDED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 };
Jeff Brown46b9ac02010-04-22 18:58:52 -0700163
164 virtual uint32_t getDeviceClasses(int32_t deviceId) const = 0;
165
166 virtual String8 getDeviceName(int32_t deviceId) const = 0;
167
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800168 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const = 0;
169
Jeff Brown6d0fec22010-07-23 21:28:06 -0700170 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
171 RawAbsoluteAxisInfo* outAxisInfo) const = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700172
Jeff Browncc0c1592011-02-19 05:07:28 -0800173 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const = 0;
174
Jeff Brown6f2fba42011-02-19 01:08:02 -0800175 virtual status_t mapKey(int32_t deviceId, int scancode,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700176 int32_t* outKeycode, uint32_t* outFlags) const = 0;
177
Jeff Brown6f2fba42011-02-19 01:08:02 -0800178 virtual status_t mapAxis(int32_t deviceId, int scancode,
Jeff Brown3a22fa02011-03-04 13:07:49 -0800179 AxisInfo* outAxisInfo) const = 0;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800180
Jeff Brown46b9ac02010-04-22 18:58:52 -0700181 // exclude a particular device from opening
182 // this can be used to ignore input devices for sensors
183 virtual void addExcludedDevice(const char* deviceName) = 0;
184
185 /*
Jeff Brownb7198742011-03-18 18:14:26 -0700186 * Wait for events to become available and returns them.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700187 * After returning, the EventHub holds onto a wake lock until the next call to getEvent.
188 * This ensures that the device will not go to sleep while the event is being processed.
189 * If the device needs to remain awake longer than that, then the caller is responsible
190 * for taking care of it (say, by poking the power manager user activity timer).
Jeff Brownaa3855d2011-03-17 01:34:19 -0700191 *
192 * The timeout is advisory only. If the device is asleep, it will not wake just to
193 * service the timeout.
194 *
Jeff Brownb7198742011-03-18 18:14:26 -0700195 * Returns the number of events obtained, or 0 if the timeout expired.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700196 */
Jeff Brownb7198742011-03-18 18:14:26 -0700197 virtual size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700198
199 /*
200 * Query current input state.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700201 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700202 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0;
203 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0;
204 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700205
206 /*
207 * Examine key input devices for specific framework keycode support
208 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700209 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700210 uint8_t* outFlags) const = 0;
Jeff Brownf2f48712010-10-01 17:46:21 -0700211
Jeff Brown497a92c2010-09-12 17:55:08 -0700212 virtual bool hasLed(int32_t deviceId, int32_t led) const = 0;
213 virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0;
214
Jeff Brown90655042010-12-02 13:50:46 -0800215 virtual void getVirtualKeyDefinitions(int32_t deviceId,
216 Vector<VirtualKeyDefinition>& outVirtualKeys) const = 0;
217
Jeff Brownf2f48712010-10-01 17:46:21 -0700218 virtual void dump(String8& dump) = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700219};
220
221class EventHub : public EventHubInterface
222{
223public:
224 EventHub();
225
226 status_t errorCheck() const;
227
228 virtual uint32_t getDeviceClasses(int32_t deviceId) const;
Jeff Brown497a92c2010-09-12 17:55:08 -0700229
Jeff Brown46b9ac02010-04-22 18:58:52 -0700230 virtual String8 getDeviceName(int32_t deviceId) const;
Jeff Brown497a92c2010-09-12 17:55:08 -0700231
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800232 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const;
233
Jeff Brown6d0fec22010-07-23 21:28:06 -0700234 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
235 RawAbsoluteAxisInfo* outAxisInfo) const;
236
Jeff Browncc0c1592011-02-19 05:07:28 -0800237 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const;
238
Jeff Brown6f2fba42011-02-19 01:08:02 -0800239 virtual status_t mapKey(int32_t deviceId, int scancode,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700240 int32_t* outKeycode, uint32_t* outFlags) const;
241
Jeff Brown6f2fba42011-02-19 01:08:02 -0800242 virtual status_t mapAxis(int32_t deviceId, int scancode,
Jeff Brown3a22fa02011-03-04 13:07:49 -0800243 AxisInfo* outAxisInfo) const;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800244
Jeff Brown46b9ac02010-04-22 18:58:52 -0700245 virtual void addExcludedDevice(const char* deviceName);
246
Jeff Brown6d0fec22010-07-23 21:28:06 -0700247 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const;
248 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const;
249 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700250
Jeff Brown6d0fec22010-07-23 21:28:06 -0700251 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
252 const int32_t* keyCodes, uint8_t* outFlags) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253
Jeff Brownb7198742011-03-18 18:14:26 -0700254 virtual size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400255
Jeff Brown497a92c2010-09-12 17:55:08 -0700256 virtual bool hasLed(int32_t deviceId, int32_t led) const;
257 virtual void setLedState(int32_t deviceId, int32_t led, bool on);
258
Jeff Brown90655042010-12-02 13:50:46 -0800259 virtual void getVirtualKeyDefinitions(int32_t deviceId,
260 Vector<VirtualKeyDefinition>& outVirtualKeys) const;
261
Jeff Brownf2f48712010-10-01 17:46:21 -0700262 virtual void dump(String8& dump);
263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264protected:
265 virtual ~EventHub();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266
267private:
268 bool openPlatformInput(void);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269
Jeff Brown90655042010-12-02 13:50:46 -0800270 int openDevice(const char *devicePath);
271 int closeDevice(const char *devicePath);
Jeff Brown33bbfd22011-02-24 20:55:35 -0800272 int closeDeviceAtIndexLocked(int index);
Jeff Brown7342bb92010-10-01 18:55:43 -0700273 int scanDir(const char *dirname);
274 int readNotify(int nfd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275
276 status_t mError;
277
Jeff Brown90655042010-12-02 13:50:46 -0800278 struct Device {
279 Device* next;
280
281 int fd;
282 const int32_t id;
283 const String8 path;
284 const InputDeviceIdentifier identifier;
285
286 uint32_t classes;
287 uint8_t* keyBitmask;
Jeff Browncc0c1592011-02-19 05:07:28 -0800288 uint8_t* relBitmask;
Jeff Brown90655042010-12-02 13:50:46 -0800289 String8 configurationFile;
290 PropertyMap* configuration;
291 VirtualKeyMap* virtualKeyMap;
292 KeyMap keyMap;
293
294 Device(int fd, int32_t id, const String8& path, const InputDeviceIdentifier& identifier);
295 ~Device();
296
297 void close();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 };
299
Jeff Brown90655042010-12-02 13:50:46 -0800300 Device* getDeviceLocked(int32_t deviceId) const;
301 bool hasKeycodeLocked(Device* device, int keycode) const;
Jeff Brown497a92c2010-09-12 17:55:08 -0700302
Jeff Brown90655042010-12-02 13:50:46 -0800303 int32_t getScanCodeStateLocked(Device* device, int32_t scanCode) const;
304 int32_t getKeyCodeStateLocked(Device* device, int32_t keyCode) const;
305 int32_t getSwitchStateLocked(Device* device, int32_t sw) const;
306 bool markSupportedKeyCodesLocked(Device* device, size_t numCodes,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700307 const int32_t* keyCodes, uint8_t* outFlags) const;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700308
Jeff Brown90655042010-12-02 13:50:46 -0800309 void loadConfiguration(Device* device);
310 status_t loadVirtualKeyMap(Device* device);
311 status_t loadKeyMap(Device* device);
312 void setKeyboardProperties(Device* device, bool builtInKeyboard);
313 void clearKeyboardProperties(Device* device, bool builtInKeyboard);
Jeff Brown497a92c2010-09-12 17:55:08 -0700314
Jeff Brown56194eb2011-03-02 19:23:13 -0800315 bool isExternalDevice(Device* device);
316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 // Protect all internal state.
Jeff Brown90655042010-12-02 13:50:46 -0800318 mutable Mutex mLock;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400319
Jeff Brown90655042010-12-02 13:50:46 -0800320 // The actual id of the built-in keyboard, or -1 if none.
321 // EventHub remaps the built-in keyboard to id 0 externally as required by the API.
322 int32_t mBuiltInKeyboardId;
323
324 int32_t mNextDeviceId;
325
326 // Parallel arrays of fds and devices.
327 // First index is reserved for inotify.
328 Vector<struct pollfd> mFds;
329 Vector<Device*> mDevices;
330
331 Device *mOpeningDevices;
332 Device *mClosingDevices;
333
334 bool mOpened;
335 bool mNeedToSendFinishedDeviceScan;
336 List<String8> mExcludedDevices;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 // device ids that report particular switches.
Jeff Brown90655042010-12-02 13:50:46 -0800339 int32_t mSwitches[SW_MAX + 1];
Jeff Browncc2e7172010-08-17 16:48:25 -0700340
Jeff Brownb7198742011-03-18 18:14:26 -0700341 // The index of the next file descriptor that needs to be read.
Jeff Brown90655042010-12-02 13:50:46 -0800342 size_t mInputFdIndex;
Jeff Brownb7198742011-03-18 18:14:26 -0700343
344 // Set to the number of CPUs.
345 int32_t mNumCpus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346};
347
348}; // namespace android
349
350#endif // _RUNTIME_EVENT_HUB_H