blob: ca3361976fa407c3adb802b0b9d94055c3741bf8 [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 Brown80fd47c2011-05-24 01:07:44 -070037/* These constants are not defined in linux/input.h in the version of the kernel
38 * headers currently provided with Bionic. */
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070039
Jeff Brown80fd47c2011-05-24 01:07:44 -070040#define EVIOCGPROP(len) _IOC(_IOC_READ, 'E', 0x09, len)
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070041
Jeff Brown80fd47c2011-05-24 01:07:44 -070042#define INPUT_PROP_POINTER 0x00
43#define INPUT_PROP_DIRECT 0x01
44#define INPUT_PROP_BUTTONPAD 0x02
45#define INPUT_PROP_SEMI_MT 0x03
46#define INPUT_PROP_MAX 0x1f
47#define INPUT_PROP_CNT (INPUT_PROP_MAX + 1)
48
49#define ABS_MT_SLOT 0x2f
50#define ABS_MT_TOUCH_MAJOR 0x30
51#define ABS_MT_TOUCH_MINOR 0x31
52#define ABS_MT_WIDTH_MAJOR 0x32
53#define ABS_MT_WIDTH_MINOR 0x33
54#define ABS_MT_ORIENTATION 0x34
55#define ABS_MT_POSITION_X 0x35
56#define ABS_MT_POSITION_Y 0x36
57#define ABS_MT_TOOL_TYPE 0x37
58#define ABS_MT_BLOB_ID 0x38
59#define ABS_MT_TRACKING_ID 0x39
60#define ABS_MT_PRESSURE 0x3a
61#define ABS_MT_DISTANCE 0x3b
62
63#define MT_TOOL_FINGER 0
64#define MT_TOOL_PEN 1
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070065
66#define SYN_MT_REPORT 2
Jeff Brown80fd47c2011-05-24 01:07:44 -070067#define SYN_DROPPED 3
68
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070069
70/* Convenience constants. */
71
72#define BTN_FIRST 0x100 // first button scancode
73#define BTN_LAST 0x15f // last button scancode
74
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075struct pollfd;
76
77namespace android {
78
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079/*
Jeff Brown6d0fec22010-07-23 21:28:06 -070080 * A raw event as retrieved from the EventHub.
81 */
82struct RawEvent {
83 nsecs_t when;
84 int32_t deviceId;
85 int32_t type;
86 int32_t scanCode;
87 int32_t keyCode;
88 int32_t value;
89 uint32_t flags;
90};
91
92/* Describes an absolute axis. */
93struct RawAbsoluteAxisInfo {
94 bool valid; // true if the information is valid, false otherwise
95
96 int32_t minValue; // minimum value
97 int32_t maxValue; // maximum value
98 int32_t flat; // center flat position, eg. flat == 8 means center is between -8 and 8
99 int32_t fuzz; // error tolerance, eg. fuzz == 4 means value is +/- 4 due to noise
100
Jeff Brown8d608662010-08-30 03:02:23 -0700101 inline void clear() {
102 valid = false;
103 minValue = 0;
104 maxValue = 0;
105 flat = 0;
106 fuzz = 0;
107 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700108};
109
110/*
Jeff Brownc5ed5912010-07-14 18:48:53 -0700111 * Input device classes.
112 */
113enum {
Jeff Browncb1404e2011-01-15 18:14:15 -0800114 /* The input device is a keyboard or has buttons. */
Jeff Brownc5ed5912010-07-14 18:48:53 -0700115 INPUT_DEVICE_CLASS_KEYBOARD = 0x00000001,
116
117 /* The input device is an alpha-numeric keyboard (not just a dial pad). */
118 INPUT_DEVICE_CLASS_ALPHAKEY = 0x00000002,
119
Jeff Brown58a2da82011-01-25 16:02:22 -0800120 /* The input device is a touchscreen or a touchpad (either single-touch or multi-touch). */
121 INPUT_DEVICE_CLASS_TOUCH = 0x00000004,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700122
Jeff Brown83c09682010-12-23 17:50:18 -0800123 /* The input device is a cursor device such as a trackball or mouse. */
124 INPUT_DEVICE_CLASS_CURSOR = 0x00000008,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700125
126 /* The input device is a multi-touch touchscreen. */
Jeff Brown58a2da82011-01-25 16:02:22 -0800127 INPUT_DEVICE_CLASS_TOUCH_MT = 0x00000010,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700128
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700129 /* The input device is a directional pad (implies keyboard, has DPAD keys). */
Jeff Brownc5ed5912010-07-14 18:48:53 -0700130 INPUT_DEVICE_CLASS_DPAD = 0x00000020,
131
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700132 /* The input device is a gamepad (implies keyboard, has BUTTON keys). */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700133 INPUT_DEVICE_CLASS_GAMEPAD = 0x00000040,
134
135 /* The input device has switches. */
136 INPUT_DEVICE_CLASS_SWITCH = 0x00000080,
Jeff Browncb1404e2011-01-15 18:14:15 -0800137
138 /* The input device is a joystick (implies gamepad, has joystick absolute axes). */
139 INPUT_DEVICE_CLASS_JOYSTICK = 0x00000100,
Jeff Brown56194eb2011-03-02 19:23:13 -0800140
141 /* The input device is external (not built-in). */
142 INPUT_DEVICE_CLASS_EXTERNAL = 0x80000000,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700143};
144
145/*
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700146 * Grand Central Station for events.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 *
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700148 * The event hub aggregates input events received across all known input
149 * devices on the system, including devices that may be emulated by the simulator
150 * environment. In addition, the event hub generates fake input events to indicate
151 * when devices are added or removed.
152 *
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800153 * The event hub provides a stream of input events (via the getEvent function).
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700154 * It also supports querying the current actual state of input devices such as identifying
155 * which keys are currently down. Finally, the event hub keeps track of the capabilities of
156 * 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 -0800157 */
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700158class EventHubInterface : public virtual RefBase {
159protected:
160 EventHubInterface() { }
161 virtual ~EventHubInterface() { }
162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163public:
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700164 // Synthetic raw event type codes produced when devices are added or removed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 enum {
Jeff Brown7342bb92010-10-01 18:55:43 -0700166 // Sent when a device is added.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 DEVICE_ADDED = 0x10000000,
Jeff Brown7342bb92010-10-01 18:55:43 -0700168 // Sent when a device is removed.
169 DEVICE_REMOVED = 0x20000000,
170 // Sent when all added/removed devices from the most recent scan have been reported.
171 // This event is always sent at least once.
172 FINISHED_DEVICE_SCAN = 0x30000000,
Jeff Brownb7198742011-03-18 18:14:26 -0700173
174 FIRST_SYNTHETIC_EVENT = DEVICE_ADDED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 };
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700176
177 virtual uint32_t getDeviceClasses(int32_t deviceId) const = 0;
178
179 virtual String8 getDeviceName(int32_t deviceId) const = 0;
180
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800181 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const = 0;
182
Jeff Brown6d0fec22010-07-23 21:28:06 -0700183 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
184 RawAbsoluteAxisInfo* outAxisInfo) const = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700185
Jeff Browncc0c1592011-02-19 05:07:28 -0800186 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const = 0;
187
Jeff Brown80fd47c2011-05-24 01:07:44 -0700188 virtual bool hasInputProperty(int32_t deviceId, int property) const = 0;
189
Jeff Brown6f2fba42011-02-19 01:08:02 -0800190 virtual status_t mapKey(int32_t deviceId, int scancode,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700191 int32_t* outKeycode, uint32_t* outFlags) const = 0;
192
Jeff Brown6f2fba42011-02-19 01:08:02 -0800193 virtual status_t mapAxis(int32_t deviceId, int scancode,
Jeff Brown3a22fa02011-03-04 13:07:49 -0800194 AxisInfo* outAxisInfo) const = 0;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800195
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700196 // exclude a particular device from opening
197 // this can be used to ignore input devices for sensors
198 virtual void addExcludedDevice(const char* deviceName) = 0;
199
200 /*
Jeff Brownb7198742011-03-18 18:14:26 -0700201 * Wait for events to become available and returns them.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700202 * After returning, the EventHub holds onto a wake lock until the next call to getEvent.
203 * This ensures that the device will not go to sleep while the event is being processed.
204 * If the device needs to remain awake longer than that, then the caller is responsible
205 * for taking care of it (say, by poking the power manager user activity timer).
Jeff Brownaa3855d2011-03-17 01:34:19 -0700206 *
207 * The timeout is advisory only. If the device is asleep, it will not wake just to
208 * service the timeout.
209 *
Jeff Brownb7198742011-03-18 18:14:26 -0700210 * Returns the number of events obtained, or 0 if the timeout expired.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700211 */
Jeff Brownb7198742011-03-18 18:14:26 -0700212 virtual size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700213
214 /*
215 * Query current input state.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700216 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700217 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0;
218 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0;
219 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700220
221 /*
222 * Examine key input devices for specific framework keycode support
223 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700224 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700225 uint8_t* outFlags) const = 0;
Jeff Brownf2f487182010-10-01 17:46:21 -0700226
Jeff Brown497a92c2010-09-12 17:55:08 -0700227 virtual bool hasLed(int32_t deviceId, int32_t led) const = 0;
228 virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0;
229
Jeff Brown90655042010-12-02 13:50:46 -0800230 virtual void getVirtualKeyDefinitions(int32_t deviceId,
231 Vector<VirtualKeyDefinition>& outVirtualKeys) const = 0;
232
Jeff Brownf2f487182010-10-01 17:46:21 -0700233 virtual void dump(String8& dump) = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700234};
235
236class EventHub : public EventHubInterface
237{
238public:
239 EventHub();
240
241 status_t errorCheck() const;
242
243 virtual uint32_t getDeviceClasses(int32_t deviceId) const;
Jeff Brown497a92c2010-09-12 17:55:08 -0700244
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700245 virtual String8 getDeviceName(int32_t deviceId) const;
Jeff Brown497a92c2010-09-12 17:55:08 -0700246
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800247 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const;
248
Jeff Brown6d0fec22010-07-23 21:28:06 -0700249 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
250 RawAbsoluteAxisInfo* outAxisInfo) const;
251
Jeff Browncc0c1592011-02-19 05:07:28 -0800252 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const;
253
Jeff Brown80fd47c2011-05-24 01:07:44 -0700254 virtual bool hasInputProperty(int32_t deviceId, int property) const;
255
Jeff Brown6f2fba42011-02-19 01:08:02 -0800256 virtual status_t mapKey(int32_t deviceId, int scancode,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700257 int32_t* outKeycode, uint32_t* outFlags) const;
258
Jeff Brown6f2fba42011-02-19 01:08:02 -0800259 virtual status_t mapAxis(int32_t deviceId, int scancode,
Jeff Brown3a22fa02011-03-04 13:07:49 -0800260 AxisInfo* outAxisInfo) const;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800261
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700262 virtual void addExcludedDevice(const char* deviceName);
263
Jeff Brown6d0fec22010-07-23 21:28:06 -0700264 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const;
265 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const;
266 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700267
Jeff Brown6d0fec22010-07-23 21:28:06 -0700268 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
269 const int32_t* keyCodes, uint8_t* outFlags) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270
Jeff Brownb7198742011-03-18 18:14:26 -0700271 virtual size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400272
Jeff Brown497a92c2010-09-12 17:55:08 -0700273 virtual bool hasLed(int32_t deviceId, int32_t led) const;
274 virtual void setLedState(int32_t deviceId, int32_t led, bool on);
275
Jeff Brown90655042010-12-02 13:50:46 -0800276 virtual void getVirtualKeyDefinitions(int32_t deviceId,
277 Vector<VirtualKeyDefinition>& outVirtualKeys) const;
278
Jeff Brownf2f487182010-10-01 17:46:21 -0700279 virtual void dump(String8& dump);
280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281protected:
282 virtual ~EventHub();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283
284private:
285 bool openPlatformInput(void);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286
Jeff Brown90655042010-12-02 13:50:46 -0800287 int openDevice(const char *devicePath);
288 int closeDevice(const char *devicePath);
Jeff Brown33bbfd22011-02-24 20:55:35 -0800289 int closeDeviceAtIndexLocked(int index);
Jeff Brown7342bb92010-10-01 18:55:43 -0700290 int scanDir(const char *dirname);
291 int readNotify(int nfd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292
293 status_t mError;
294
Jeff Brown90655042010-12-02 13:50:46 -0800295 struct Device {
296 Device* next;
297
298 int fd;
299 const int32_t id;
300 const String8 path;
301 const InputDeviceIdentifier identifier;
302
303 uint32_t classes;
304 uint8_t* keyBitmask;
Jeff Browncc0c1592011-02-19 05:07:28 -0800305 uint8_t* relBitmask;
Jeff Brown80fd47c2011-05-24 01:07:44 -0700306 uint8_t* propBitmask;
Jeff Brown90655042010-12-02 13:50:46 -0800307 String8 configurationFile;
308 PropertyMap* configuration;
309 VirtualKeyMap* virtualKeyMap;
310 KeyMap keyMap;
311
312 Device(int fd, int32_t id, const String8& path, const InputDeviceIdentifier& identifier);
313 ~Device();
314
315 void close();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 };
317
Jeff Brown90655042010-12-02 13:50:46 -0800318 Device* getDeviceLocked(int32_t deviceId) const;
319 bool hasKeycodeLocked(Device* device, int keycode) const;
Jeff Brown497a92c2010-09-12 17:55:08 -0700320
Jeff Brown90655042010-12-02 13:50:46 -0800321 int32_t getScanCodeStateLocked(Device* device, int32_t scanCode) const;
322 int32_t getKeyCodeStateLocked(Device* device, int32_t keyCode) const;
323 int32_t getSwitchStateLocked(Device* device, int32_t sw) const;
324 bool markSupportedKeyCodesLocked(Device* device, size_t numCodes,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700325 const int32_t* keyCodes, uint8_t* outFlags) const;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700326
Jeff Brown90655042010-12-02 13:50:46 -0800327 void loadConfiguration(Device* device);
328 status_t loadVirtualKeyMap(Device* device);
329 status_t loadKeyMap(Device* device);
330 void setKeyboardProperties(Device* device, bool builtInKeyboard);
331 void clearKeyboardProperties(Device* device, bool builtInKeyboard);
Jeff Brown497a92c2010-09-12 17:55:08 -0700332
Jeff Brown56194eb2011-03-02 19:23:13 -0800333 bool isExternalDevice(Device* device);
334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 // Protect all internal state.
Jeff Brown90655042010-12-02 13:50:46 -0800336 mutable Mutex mLock;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400337
Jeff Brown90655042010-12-02 13:50:46 -0800338 // The actual id of the built-in keyboard, or -1 if none.
339 // EventHub remaps the built-in keyboard to id 0 externally as required by the API.
340 int32_t mBuiltInKeyboardId;
341
342 int32_t mNextDeviceId;
343
344 // Parallel arrays of fds and devices.
345 // First index is reserved for inotify.
346 Vector<struct pollfd> mFds;
347 Vector<Device*> mDevices;
348
349 Device *mOpeningDevices;
350 Device *mClosingDevices;
351
352 bool mOpened;
353 bool mNeedToSendFinishedDeviceScan;
354 List<String8> mExcludedDevices;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 // device ids that report particular switches.
Jeff Brown90655042010-12-02 13:50:46 -0800357 int32_t mSwitches[SW_MAX + 1];
Jeff Browncc2e7172010-08-17 16:48:25 -0700358
Jeff Brownb7198742011-03-18 18:14:26 -0700359 // The index of the next file descriptor that needs to be read.
Jeff Brown90655042010-12-02 13:50:46 -0800360 size_t mInputFdIndex;
Jeff Brownb7198742011-03-18 18:14:26 -0700361
362 // Set to the number of CPUs.
363 int32_t mNumCpus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364};
365
366}; // namespace android
367
368#endif // _RUNTIME_EVENT_HUB_H