blob: 0a34e45a565cfee9050dbe7aed16f736d33b67ba [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>
Jeff Brown93fa9b32011-06-14 17:09:25 -070034#include <utils/KeyedVector.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
36#include <linux/input.h>
Jeff Brown93fa9b32011-06-14 17:09:25 -070037#include <sys/epoll.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070039/* Convenience constants. */
40
41#define BTN_FIRST 0x100 // first button scancode
42#define BTN_LAST 0x15f // last button scancode
43
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044namespace android {
45
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046/*
Jeff Brown6d0fec22010-07-23 21:28:06 -070047 * A raw event as retrieved from the EventHub.
48 */
49struct RawEvent {
50 nsecs_t when;
51 int32_t deviceId;
52 int32_t type;
53 int32_t scanCode;
54 int32_t keyCode;
55 int32_t value;
56 uint32_t flags;
57};
58
59/* Describes an absolute axis. */
60struct RawAbsoluteAxisInfo {
61 bool valid; // true if the information is valid, false otherwise
62
63 int32_t minValue; // minimum value
64 int32_t maxValue; // maximum value
65 int32_t flat; // center flat position, eg. flat == 8 means center is between -8 and 8
66 int32_t fuzz; // error tolerance, eg. fuzz == 4 means value is +/- 4 due to noise
Jeff Brownb3a2d132011-06-12 18:14:50 -070067 int32_t resolution; // resolution in units per mm or radians per mm
Jeff Brown6d0fec22010-07-23 21:28:06 -070068
Jeff Brown8d608662010-08-30 03:02:23 -070069 inline void clear() {
70 valid = false;
71 minValue = 0;
72 maxValue = 0;
73 flat = 0;
74 fuzz = 0;
Jeff Brownb3a2d132011-06-12 18:14:50 -070075 resolution = 0;
Jeff Brown8d608662010-08-30 03:02:23 -070076 }
Jeff Brown6d0fec22010-07-23 21:28:06 -070077};
78
79/*
Jeff Brownc5ed5912010-07-14 18:48:53 -070080 * Input device classes.
81 */
82enum {
Jeff Browncb1404e2011-01-15 18:14:15 -080083 /* The input device is a keyboard or has buttons. */
Jeff Brownc5ed5912010-07-14 18:48:53 -070084 INPUT_DEVICE_CLASS_KEYBOARD = 0x00000001,
85
86 /* The input device is an alpha-numeric keyboard (not just a dial pad). */
87 INPUT_DEVICE_CLASS_ALPHAKEY = 0x00000002,
88
Jeff Brown58a2da82011-01-25 16:02:22 -080089 /* The input device is a touchscreen or a touchpad (either single-touch or multi-touch). */
90 INPUT_DEVICE_CLASS_TOUCH = 0x00000004,
Jeff Brownc5ed5912010-07-14 18:48:53 -070091
Jeff Brown83c09682010-12-23 17:50:18 -080092 /* The input device is a cursor device such as a trackball or mouse. */
93 INPUT_DEVICE_CLASS_CURSOR = 0x00000008,
Jeff Brownc5ed5912010-07-14 18:48:53 -070094
95 /* The input device is a multi-touch touchscreen. */
Jeff Brown58a2da82011-01-25 16:02:22 -080096 INPUT_DEVICE_CLASS_TOUCH_MT = 0x00000010,
Jeff Brownc5ed5912010-07-14 18:48:53 -070097
Jeff Browndc1ab4b2010-09-14 18:03:38 -070098 /* The input device is a directional pad (implies keyboard, has DPAD keys). */
Jeff Brownc5ed5912010-07-14 18:48:53 -070099 INPUT_DEVICE_CLASS_DPAD = 0x00000020,
100
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700101 /* The input device is a gamepad (implies keyboard, has BUTTON keys). */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700102 INPUT_DEVICE_CLASS_GAMEPAD = 0x00000040,
103
104 /* The input device has switches. */
105 INPUT_DEVICE_CLASS_SWITCH = 0x00000080,
Jeff Browncb1404e2011-01-15 18:14:15 -0800106
107 /* The input device is a joystick (implies gamepad, has joystick absolute axes). */
108 INPUT_DEVICE_CLASS_JOYSTICK = 0x00000100,
Jeff Brown56194eb2011-03-02 19:23:13 -0800109
110 /* The input device is external (not built-in). */
111 INPUT_DEVICE_CLASS_EXTERNAL = 0x80000000,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700112};
113
114/*
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700115 * Grand Central Station for events.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 *
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700117 * The event hub aggregates input events received across all known input
118 * devices on the system, including devices that may be emulated by the simulator
119 * environment. In addition, the event hub generates fake input events to indicate
120 * when devices are added or removed.
121 *
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800122 * The event hub provides a stream of input events (via the getEvent function).
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700123 * It also supports querying the current actual state of input devices such as identifying
124 * which keys are currently down. Finally, the event hub keeps track of the capabilities of
125 * 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 -0800126 */
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700127class EventHubInterface : public virtual RefBase {
128protected:
129 EventHubInterface() { }
130 virtual ~EventHubInterface() { }
131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132public:
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700133 // Synthetic raw event type codes produced when devices are added or removed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 enum {
Jeff Brown7342bb92010-10-01 18:55:43 -0700135 // Sent when a device is added.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 DEVICE_ADDED = 0x10000000,
Jeff Brown7342bb92010-10-01 18:55:43 -0700137 // Sent when a device is removed.
138 DEVICE_REMOVED = 0x20000000,
139 // Sent when all added/removed devices from the most recent scan have been reported.
140 // This event is always sent at least once.
141 FINISHED_DEVICE_SCAN = 0x30000000,
Jeff Brownb7198742011-03-18 18:14:26 -0700142
143 FIRST_SYNTHETIC_EVENT = DEVICE_ADDED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 };
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700145
146 virtual uint32_t getDeviceClasses(int32_t deviceId) const = 0;
147
148 virtual String8 getDeviceName(int32_t deviceId) const = 0;
149
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800150 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const = 0;
151
Jeff Brown6d0fec22010-07-23 21:28:06 -0700152 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
153 RawAbsoluteAxisInfo* outAxisInfo) const = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700154
Jeff Browncc0c1592011-02-19 05:07:28 -0800155 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const = 0;
156
Jeff Brown80fd47c2011-05-24 01:07:44 -0700157 virtual bool hasInputProperty(int32_t deviceId, int property) const = 0;
158
Jeff Brown6f2fba42011-02-19 01:08:02 -0800159 virtual status_t mapKey(int32_t deviceId, int scancode,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700160 int32_t* outKeycode, uint32_t* outFlags) const = 0;
161
Jeff Brown6f2fba42011-02-19 01:08:02 -0800162 virtual status_t mapAxis(int32_t deviceId, int scancode,
Jeff Brown3a22fa02011-03-04 13:07:49 -0800163 AxisInfo* outAxisInfo) const = 0;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800164
Jeff Brown1a84fd12011-06-02 01:26:32 -0700165 // Sets devices that are excluded from opening.
166 // This can be used to ignore input devices for sensors.
167 virtual void setExcludedDevices(const Vector<String8>& devices) = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700168
169 /*
Jeff Brownb7198742011-03-18 18:14:26 -0700170 * Wait for events to become available and returns them.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700171 * 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).
Jeff Brownaa3855d2011-03-17 01:34:19 -0700175 *
176 * The timeout is advisory only. If the device is asleep, it will not wake just to
177 * service the timeout.
178 *
Jeff Brownb7198742011-03-18 18:14:26 -0700179 * Returns the number of events obtained, or 0 if the timeout expired.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700180 */
Jeff Brownb7198742011-03-18 18:14:26 -0700181 virtual size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700182
183 /*
184 * Query current input state.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700185 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700186 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0;
187 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0;
188 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700189
190 /*
191 * Examine key input devices for specific framework keycode support
192 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700193 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700194 uint8_t* outFlags) const = 0;
Jeff Brownf2f487182010-10-01 17:46:21 -0700195
Jeff Brown497a92c2010-09-12 17:55:08 -0700196 virtual bool hasLed(int32_t deviceId, int32_t led) const = 0;
197 virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0;
198
Jeff Brown90655042010-12-02 13:50:46 -0800199 virtual void getVirtualKeyDefinitions(int32_t deviceId,
200 Vector<VirtualKeyDefinition>& outVirtualKeys) const = 0;
201
Jeff Brown93fa9b32011-06-14 17:09:25 -0700202 /* Requests the EventHub to reopen all input devices on the next call to getEvents(). */
203 virtual void requestReopenDevices() = 0;
204
205 /* Wakes up getEvents() if it is blocked on a read. */
206 virtual void wake() = 0;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700207
Jeff Brownf2f487182010-10-01 17:46:21 -0700208 virtual void dump(String8& dump) = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700209};
210
211class EventHub : public EventHubInterface
212{
213public:
214 EventHub();
215
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700216 virtual uint32_t getDeviceClasses(int32_t deviceId) const;
Jeff Brown497a92c2010-09-12 17:55:08 -0700217
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700218 virtual String8 getDeviceName(int32_t deviceId) const;
Jeff Brown497a92c2010-09-12 17:55:08 -0700219
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800220 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const;
221
Jeff Brown6d0fec22010-07-23 21:28:06 -0700222 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
223 RawAbsoluteAxisInfo* outAxisInfo) const;
224
Jeff Browncc0c1592011-02-19 05:07:28 -0800225 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const;
226
Jeff Brown80fd47c2011-05-24 01:07:44 -0700227 virtual bool hasInputProperty(int32_t deviceId, int property) const;
228
Jeff Brown6f2fba42011-02-19 01:08:02 -0800229 virtual status_t mapKey(int32_t deviceId, int scancode,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700230 int32_t* outKeycode, uint32_t* outFlags) const;
231
Jeff Brown6f2fba42011-02-19 01:08:02 -0800232 virtual status_t mapAxis(int32_t deviceId, int scancode,
Jeff Brown3a22fa02011-03-04 13:07:49 -0800233 AxisInfo* outAxisInfo) const;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800234
Jeff Brown1a84fd12011-06-02 01:26:32 -0700235 virtual void setExcludedDevices(const Vector<String8>& devices);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700236
Jeff Brown6d0fec22010-07-23 21:28:06 -0700237 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const;
238 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const;
239 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700240
Jeff Brown6d0fec22010-07-23 21:28:06 -0700241 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
242 const int32_t* keyCodes, uint8_t* outFlags) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243
Jeff Brownb7198742011-03-18 18:14:26 -0700244 virtual size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400245
Jeff Brown497a92c2010-09-12 17:55:08 -0700246 virtual bool hasLed(int32_t deviceId, int32_t led) const;
247 virtual void setLedState(int32_t deviceId, int32_t led, bool on);
248
Jeff Brown90655042010-12-02 13:50:46 -0800249 virtual void getVirtualKeyDefinitions(int32_t deviceId,
250 Vector<VirtualKeyDefinition>& outVirtualKeys) const;
251
Jeff Brown93fa9b32011-06-14 17:09:25 -0700252 virtual void requestReopenDevices();
253
254 virtual void wake();
Jeff Brown1a84fd12011-06-02 01:26:32 -0700255
Jeff Brownf2f487182010-10-01 17:46:21 -0700256 virtual void dump(String8& dump);
257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258protected:
259 virtual ~EventHub();
Jeff Brown93fa9b32011-06-14 17:09:25 -0700260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261private:
Jeff Brown90655042010-12-02 13:50:46 -0800262 struct Device {
263 Device* next;
264
265 int fd;
266 const int32_t id;
267 const String8 path;
268 const InputDeviceIdentifier identifier;
269
270 uint32_t classes;
Jeff Brown93fa9b32011-06-14 17:09:25 -0700271
272 uint8_t keyBitmask[(KEY_MAX + 1) / 8];
273 uint8_t absBitmask[(ABS_MAX + 1) / 8];
274 uint8_t relBitmask[(REL_MAX + 1) / 8];
275 uint8_t swBitmask[(SW_MAX + 1) / 8];
276 uint8_t ledBitmask[(LED_MAX + 1) / 8];
277 uint8_t propBitmask[(INPUT_PROP_MAX + 1) / 8];
278
Jeff Brown90655042010-12-02 13:50:46 -0800279 String8 configurationFile;
280 PropertyMap* configuration;
281 VirtualKeyMap* virtualKeyMap;
282 KeyMap keyMap;
283
284 Device(int fd, int32_t id, const String8& path, const InputDeviceIdentifier& identifier);
285 ~Device();
286
287 void close();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 };
289
Jeff Brown93fa9b32011-06-14 17:09:25 -0700290 status_t openDeviceLocked(const char *devicePath);
291 status_t closeDeviceByPathLocked(const char *devicePath);
292
293 void closeDeviceLocked(Device* device);
294 void closeAllDevicesLocked();
295
296 status_t scanDirLocked(const char *dirname);
297 void scanDevicesLocked();
298 status_t readNotifyLocked();
299
Jeff Brown90655042010-12-02 13:50:46 -0800300 Device* getDeviceLocked(int32_t deviceId) const;
Jeff Brown93fa9b32011-06-14 17:09:25 -0700301 Device* getDeviceByPathLocked(const char* devicePath) const;
302
Jeff Brown90655042010-12-02 13:50:46 -0800303 bool hasKeycodeLocked(Device* device, int keycode) const;
Jeff Brown497a92c2010-09-12 17:55:08 -0700304
Jeff Brown90655042010-12-02 13:50:46 -0800305 int32_t getScanCodeStateLocked(Device* device, int32_t scanCode) const;
306 int32_t getKeyCodeStateLocked(Device* device, int32_t keyCode) const;
307 int32_t getSwitchStateLocked(Device* device, int32_t sw) const;
308 bool markSupportedKeyCodesLocked(Device* device, size_t numCodes,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700309 const int32_t* keyCodes, uint8_t* outFlags) const;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700310
Jeff Brown93fa9b32011-06-14 17:09:25 -0700311 void loadConfigurationLocked(Device* device);
312 status_t loadVirtualKeyMapLocked(Device* device);
313 status_t loadKeyMapLocked(Device* device);
314 void setKeyboardPropertiesLocked(Device* device, bool builtInKeyboard);
315 void clearKeyboardPropertiesLocked(Device* device, bool builtInKeyboard);
Jeff Brown497a92c2010-09-12 17:55:08 -0700316
Jeff Brown93fa9b32011-06-14 17:09:25 -0700317 bool isExternalDeviceLocked(Device* device);
Jeff Brown56194eb2011-03-02 19:23:13 -0800318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 // Protect all internal state.
Jeff Brown90655042010-12-02 13:50:46 -0800320 mutable Mutex mLock;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400321
Jeff Brown90655042010-12-02 13:50:46 -0800322 // The actual id of the built-in keyboard, or -1 if none.
323 // EventHub remaps the built-in keyboard to id 0 externally as required by the API.
324 int32_t mBuiltInKeyboardId;
325
326 int32_t mNextDeviceId;
327
Jeff Brown93fa9b32011-06-14 17:09:25 -0700328 KeyedVector<int32_t, Device*> mDevices;
Jeff Brown90655042010-12-02 13:50:46 -0800329
330 Device *mOpeningDevices;
331 Device *mClosingDevices;
332
Jeff Brown90655042010-12-02 13:50:46 -0800333 bool mNeedToSendFinishedDeviceScan;
Jeff Brown93fa9b32011-06-14 17:09:25 -0700334 bool mNeedToReopenDevices;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700335 bool mNeedToScanDevices;
336 Vector<String8> mExcludedDevices;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400337
Jeff Brown93fa9b32011-06-14 17:09:25 -0700338 int mEpollFd;
339 int mINotifyFd;
340 int mWakeReadPipeFd;
341 int mWakeWritePipeFd;
Jeff Browncc2e7172010-08-17 16:48:25 -0700342
Jeff Brown93fa9b32011-06-14 17:09:25 -0700343 // Ids used for epoll notifications not associated with devices.
344 static const uint32_t EPOLL_ID_INOTIFY = 0x80000001;
345 static const uint32_t EPOLL_ID_WAKE = 0x80000002;
346
347 // Epoll FD list size hint.
348 static const int EPOLL_SIZE_HINT = 8;
349
350 // Maximum number of signalled FDs to handle at a time.
351 static const int EPOLL_MAX_EVENTS = 16;
352
353 // The array of pending epoll events and the index of the next event to be handled.
354 struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS];
355 size_t mPendingEventCount;
356 size_t mPendingEventIndex;
357 bool mPendingINotify;
Jeff Brownb7198742011-03-18 18:14:26 -0700358
359 // Set to the number of CPUs.
360 int32_t mNumCpus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361};
362
363}; // namespace android
364
365#endif // _RUNTIME_EVENT_HUB_H