blob: 686925311f1fc48d9a9523f7d424492cb0541e25 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -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
21#include <input/Input.h>
22#include <input/InputDevice.h>
23#include <input/Keyboard.h>
24#include <input/KeyLayoutMap.h>
25#include <input/KeyCharacterMap.h>
26#include <input/VirtualKeyMap.h>
27#include <utils/String8.h>
28#include <utils/threads.h>
29#include <utils/Log.h>
30#include <utils/threads.h>
31#include <utils/List.h>
32#include <utils/Errors.h>
33#include <utils/PropertyMap.h>
34#include <utils/Vector.h>
35#include <utils/KeyedVector.h>
36#include <utils/BitSet.h>
37
38#include <linux/input.h>
39#include <sys/epoll.h>
40
41/* Convenience constants. */
42
43#define BTN_FIRST 0x100 // first button code
44#define BTN_LAST 0x15f // last button code
45
46/*
47 * These constants are used privately in Android to pass raw timestamps
48 * through evdev from uinput device drivers because there is currently no
49 * other way to transfer this information. The evdev driver automatically
50 * timestamps all input events with the time they were posted and clobbers
51 * whatever information was passed in.
52 *
53 * For the purposes of this hack, the timestamp is specified in the
54 * CLOCK_MONOTONIC timebase and is split into two EV_MSC events specifying
55 * seconds and microseconds.
56 */
57#define MSC_ANDROID_TIME_SEC 0x6
58#define MSC_ANDROID_TIME_USEC 0x7
59
60namespace android {
61
62enum {
63 // Device id of a special "virtual" keyboard that is always present.
64 VIRTUAL_KEYBOARD_ID = -1,
65 // Device id of the "built-in" keyboard if there is one.
66 BUILT_IN_KEYBOARD_ID = 0,
67};
68
69/*
70 * A raw event as retrieved from the EventHub.
71 */
72struct RawEvent {
73 nsecs_t when;
74 int32_t deviceId;
75 int32_t type;
76 int32_t code;
77 int32_t value;
78};
79
80/* Describes an absolute axis. */
81struct RawAbsoluteAxisInfo {
82 bool valid; // true if the information is valid, false otherwise
83
84 int32_t minValue; // minimum value
85 int32_t maxValue; // maximum value
86 int32_t flat; // center flat position, eg. flat == 8 means center is between -8 and 8
87 int32_t fuzz; // error tolerance, eg. fuzz == 4 means value is +/- 4 due to noise
88 int32_t resolution; // resolution in units per mm or radians per mm
89
90 inline void clear() {
91 valid = false;
92 minValue = 0;
93 maxValue = 0;
94 flat = 0;
95 fuzz = 0;
96 resolution = 0;
97 }
98};
99
100/*
101 * Input device classes.
102 */
103enum {
104 /* The input device is a keyboard or has buttons. */
105 INPUT_DEVICE_CLASS_KEYBOARD = 0x00000001,
106
107 /* The input device is an alpha-numeric keyboard (not just a dial pad). */
108 INPUT_DEVICE_CLASS_ALPHAKEY = 0x00000002,
109
110 /* The input device is a touchscreen or a touchpad (either single-touch or multi-touch). */
111 INPUT_DEVICE_CLASS_TOUCH = 0x00000004,
112
113 /* The input device is a cursor device such as a trackball or mouse. */
114 INPUT_DEVICE_CLASS_CURSOR = 0x00000008,
115
116 /* The input device is a multi-touch touchscreen. */
117 INPUT_DEVICE_CLASS_TOUCH_MT = 0x00000010,
118
119 /* The input device is a directional pad (implies keyboard, has DPAD keys). */
120 INPUT_DEVICE_CLASS_DPAD = 0x00000020,
121
122 /* The input device is a gamepad (implies keyboard, has BUTTON keys). */
123 INPUT_DEVICE_CLASS_GAMEPAD = 0x00000040,
124
125 /* The input device has switches. */
126 INPUT_DEVICE_CLASS_SWITCH = 0x00000080,
127
128 /* The input device is a joystick (implies gamepad, has joystick absolute axes). */
129 INPUT_DEVICE_CLASS_JOYSTICK = 0x00000100,
130
131 /* The input device has a vibrator (supports FF_RUMBLE). */
132 INPUT_DEVICE_CLASS_VIBRATOR = 0x00000200,
133
Tim Kilbourn063ff532015-04-08 10:26:18 -0700134 /* The input device has a microphone. */
135 INPUT_DEVICE_CLASS_MIC = 0x00000400,
136
Michael Wright842500e2015-03-13 17:32:02 -0700137 /* The input device is an external stylus (has data we want to fuse with touch data). */
138 INPUT_DEVICE_CLASS_EXTERNAL_STYLUS = 0x00000800,
139
Prashant Malani1941ff52015-08-11 18:29:28 -0700140 /* The input device has a rotary encoder */
141 INPUT_DEVICE_CLASS_ROTARY_ENCODER = 0x00001000,
142
Michael Wrightd02c5b62014-02-10 15:10:22 -0800143 /* The input device is virtual (not a real device, not part of UI configuration). */
144 INPUT_DEVICE_CLASS_VIRTUAL = 0x40000000,
145
146 /* The input device is external (not built-in). */
147 INPUT_DEVICE_CLASS_EXTERNAL = 0x80000000,
148};
149
150/*
151 * Gets the class that owns an axis, in cases where multiple classes might claim
152 * the same axis for different purposes.
153 */
154extern uint32_t getAbsAxisUsage(int32_t axis, uint32_t deviceClasses);
155
156/*
157 * Grand Central Station for events.
158 *
159 * The event hub aggregates input events received across all known input
160 * devices on the system, including devices that may be emulated by the simulator
161 * environment. In addition, the event hub generates fake input events to indicate
162 * when devices are added or removed.
163 *
164 * The event hub provides a stream of input events (via the getEvent function).
165 * It also supports querying the current actual state of input devices such as identifying
166 * which keys are currently down. Finally, the event hub keeps track of the capabilities of
167 * individual input devices, such as their class and the set of key codes that they support.
168 */
169class EventHubInterface : public virtual RefBase {
170protected:
171 EventHubInterface() { }
172 virtual ~EventHubInterface() { }
173
174public:
175 // Synthetic raw event type codes produced when devices are added or removed.
176 enum {
177 // Sent when a device is added.
178 DEVICE_ADDED = 0x10000000,
179 // Sent when a device is removed.
180 DEVICE_REMOVED = 0x20000000,
181 // Sent when all added/removed devices from the most recent scan have been reported.
182 // This event is always sent at least once.
183 FINISHED_DEVICE_SCAN = 0x30000000,
184
185 FIRST_SYNTHETIC_EVENT = DEVICE_ADDED,
186 };
187
188 virtual uint32_t getDeviceClasses(int32_t deviceId) const = 0;
189
190 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const = 0;
191
192 virtual int32_t getDeviceControllerNumber(int32_t deviceId) const = 0;
193
194 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const = 0;
195
196 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
197 RawAbsoluteAxisInfo* outAxisInfo) const = 0;
198
199 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const = 0;
200
201 virtual bool hasInputProperty(int32_t deviceId, int property) const = 0;
202
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700203 virtual status_t mapKey(int32_t deviceId,
204 int32_t scanCode, int32_t usageCode, int32_t metaState,
205 int32_t* outKeycode, int32_t *outMetaState, uint32_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800206
207 virtual status_t mapAxis(int32_t deviceId, int32_t scanCode,
208 AxisInfo* outAxisInfo) const = 0;
209
210 // Sets devices that are excluded from opening.
211 // This can be used to ignore input devices for sensors.
212 virtual void setExcludedDevices(const Vector<String8>& devices) = 0;
213
214 /*
215 * Wait for events to become available and returns them.
216 * After returning, the EventHub holds onto a wake lock until the next call to getEvent.
217 * This ensures that the device will not go to sleep while the event is being processed.
218 * If the device needs to remain awake longer than that, then the caller is responsible
219 * for taking care of it (say, by poking the power manager user activity timer).
220 *
221 * The timeout is advisory only. If the device is asleep, it will not wake just to
222 * service the timeout.
223 *
224 * Returns the number of events obtained, or 0 if the timeout expired.
225 */
226 virtual size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) = 0;
227
228 /*
229 * Query current input state.
230 */
231 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0;
232 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0;
233 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0;
234 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
235 int32_t* outValue) const = 0;
236
237 /*
238 * Examine key input devices for specific framework keycode support
239 */
240 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
241 uint8_t* outFlags) const = 0;
242
243 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const = 0;
244
245 /* LED related functions expect Android LED constants, not scan codes or HID usages */
246 virtual bool hasLed(int32_t deviceId, int32_t led) const = 0;
247 virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0;
248
249 virtual void getVirtualKeyDefinitions(int32_t deviceId,
250 Vector<VirtualKeyDefinition>& outVirtualKeys) const = 0;
251
252 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const = 0;
253 virtual bool setKeyboardLayoutOverlay(int32_t deviceId, const sp<KeyCharacterMap>& map) = 0;
254
255 /* Control the vibrator. */
256 virtual void vibrate(int32_t deviceId, nsecs_t duration) = 0;
257 virtual void cancelVibrate(int32_t deviceId) = 0;
258
259 /* Requests the EventHub to reopen all input devices on the next call to getEvents(). */
260 virtual void requestReopenDevices() = 0;
261
262 /* Wakes up getEvents() if it is blocked on a read. */
263 virtual void wake() = 0;
264
265 /* Dump EventHub state to a string. */
266 virtual void dump(String8& dump) = 0;
267
268 /* Called by the heatbeat to ensures that the reader has not deadlocked. */
269 virtual void monitor() = 0;
270};
271
272class EventHub : public EventHubInterface
273{
274public:
275 EventHub();
276
277 virtual uint32_t getDeviceClasses(int32_t deviceId) const;
278
279 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const;
280
281 virtual int32_t getDeviceControllerNumber(int32_t deviceId) const;
282
283 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const;
284
285 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
286 RawAbsoluteAxisInfo* outAxisInfo) const;
287
288 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const;
289
290 virtual bool hasInputProperty(int32_t deviceId, int property) const;
291
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700292 virtual status_t mapKey(int32_t deviceId,
293 int32_t scanCode, int32_t usageCode, int32_t metaState,
294 int32_t* outKeycode, int32_t *outMetaState, uint32_t* outFlags) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800295
296 virtual status_t mapAxis(int32_t deviceId, int32_t scanCode,
297 AxisInfo* outAxisInfo) const;
298
299 virtual void setExcludedDevices(const Vector<String8>& devices);
300
301 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const;
302 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const;
303 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const;
304 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t* outValue) const;
305
306 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
307 const int32_t* keyCodes, uint8_t* outFlags) const;
308
309 virtual size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize);
310
311 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const;
312 virtual bool hasLed(int32_t deviceId, int32_t led) const;
313 virtual void setLedState(int32_t deviceId, int32_t led, bool on);
314
315 virtual void getVirtualKeyDefinitions(int32_t deviceId,
316 Vector<VirtualKeyDefinition>& outVirtualKeys) const;
317
318 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const;
319 virtual bool setKeyboardLayoutOverlay(int32_t deviceId, const sp<KeyCharacterMap>& map);
320
321 virtual void vibrate(int32_t deviceId, nsecs_t duration);
322 virtual void cancelVibrate(int32_t deviceId);
323
324 virtual void requestReopenDevices();
325
326 virtual void wake();
327
328 virtual void dump(String8& dump);
329 virtual void monitor();
330
331protected:
332 virtual ~EventHub();
333
334private:
335 struct Device {
336 Device* next;
337
338 int fd; // may be -1 if device is virtual
339 const int32_t id;
340 const String8 path;
341 const InputDeviceIdentifier identifier;
342
343 uint32_t classes;
344
345 uint8_t keyBitmask[(KEY_MAX + 1) / 8];
346 uint8_t absBitmask[(ABS_MAX + 1) / 8];
347 uint8_t relBitmask[(REL_MAX + 1) / 8];
348 uint8_t swBitmask[(SW_MAX + 1) / 8];
349 uint8_t ledBitmask[(LED_MAX + 1) / 8];
350 uint8_t ffBitmask[(FF_MAX + 1) / 8];
351 uint8_t propBitmask[(INPUT_PROP_MAX + 1) / 8];
352
353 String8 configurationFile;
354 PropertyMap* configuration;
355 VirtualKeyMap* virtualKeyMap;
356 KeyMap keyMap;
357
358 sp<KeyCharacterMap> overlayKeyMap;
359 sp<KeyCharacterMap> combinedKeyMap;
360
361 bool ffEffectPlaying;
362 int16_t ffEffectId; // initially -1
363
364 int32_t controllerNumber;
365
366 int32_t timestampOverrideSec;
367 int32_t timestampOverrideUsec;
368
369 Device(int fd, int32_t id, const String8& path, const InputDeviceIdentifier& identifier);
370 ~Device();
371
372 void close();
373
374 inline bool isVirtual() const { return fd < 0; }
375
376 const sp<KeyCharacterMap>& getKeyCharacterMap() const {
377 if (combinedKeyMap != NULL) {
378 return combinedKeyMap;
379 }
380 return keyMap.keyCharacterMap;
381 }
382 };
383
384 status_t openDeviceLocked(const char *devicePath);
385 void createVirtualKeyboardLocked();
386 void addDeviceLocked(Device* device);
387 void assignDescriptorLocked(InputDeviceIdentifier& identifier);
388
389 status_t closeDeviceByPathLocked(const char *devicePath);
390 void closeDeviceLocked(Device* device);
391 void closeAllDevicesLocked();
392
393 status_t scanDirLocked(const char *dirname);
394 void scanDevicesLocked();
395 status_t readNotifyLocked();
396
397 Device* getDeviceByDescriptorLocked(String8& descriptor) const;
398 Device* getDeviceLocked(int32_t deviceId) const;
399 Device* getDeviceByPathLocked(const char* devicePath) const;
400
401 bool hasKeycodeLocked(Device* device, int keycode) const;
402
403 void loadConfigurationLocked(Device* device);
404 status_t loadVirtualKeyMapLocked(Device* device);
405 status_t loadKeyMapLocked(Device* device);
406
407 bool isExternalDeviceLocked(Device* device);
Tim Kilbourn063ff532015-04-08 10:26:18 -0700408 bool deviceHasMicLocked(Device* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800409
410 int32_t getNextControllerNumberLocked(Device* device);
411 void releaseControllerNumberLocked(Device* device);
412 void setLedForController(Device* device);
413
414 status_t mapLed(Device* device, int32_t led, int32_t* outScanCode) const;
415 void setLedStateLocked(Device* device, int32_t led, bool on);
416
417 // Protect all internal state.
418 mutable Mutex mLock;
419
420 // The actual id of the built-in keyboard, or NO_BUILT_IN_KEYBOARD if none.
421 // EventHub remaps the built-in keyboard to id 0 externally as required by the API.
422 enum {
423 // Must not conflict with any other assigned device ids, including
424 // the virtual keyboard id (-1).
425 NO_BUILT_IN_KEYBOARD = -2,
426 };
427 int32_t mBuiltInKeyboardId;
428
429 int32_t mNextDeviceId;
430
431 BitSet32 mControllerNumbers;
432
433 KeyedVector<int32_t, Device*> mDevices;
434
435 Device *mOpeningDevices;
436 Device *mClosingDevices;
437
438 bool mNeedToSendFinishedDeviceScan;
439 bool mNeedToReopenDevices;
440 bool mNeedToScanDevices;
441 Vector<String8> mExcludedDevices;
442
443 int mEpollFd;
444 int mINotifyFd;
445 int mWakeReadPipeFd;
446 int mWakeWritePipeFd;
447
448 // Ids used for epoll notifications not associated with devices.
449 static const uint32_t EPOLL_ID_INOTIFY = 0x80000001;
450 static const uint32_t EPOLL_ID_WAKE = 0x80000002;
451
452 // Epoll FD list size hint.
453 static const int EPOLL_SIZE_HINT = 8;
454
455 // Maximum number of signalled FDs to handle at a time.
456 static const int EPOLL_MAX_EVENTS = 16;
457
458 // The array of pending epoll events and the index of the next event to be handled.
459 struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS];
460 size_t mPendingEventCount;
461 size_t mPendingEventIndex;
462 bool mPendingINotify;
463
464 bool mUsingEpollWakeup;
465};
466
467}; // namespace android
468
469#endif // _RUNTIME_EVENT_HUB_H