blob: d08f60e0c14624a67d3a143739c9b9094cd9215b [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 Wrightd02c5b62014-02-10 15:10:22 -0800137 /* The input device is virtual (not a real device, not part of UI configuration). */
138 INPUT_DEVICE_CLASS_VIRTUAL = 0x40000000,
139
140 /* The input device is external (not built-in). */
141 INPUT_DEVICE_CLASS_EXTERNAL = 0x80000000,
142};
143
144/*
145 * Gets the class that owns an axis, in cases where multiple classes might claim
146 * the same axis for different purposes.
147 */
148extern uint32_t getAbsAxisUsage(int32_t axis, uint32_t deviceClasses);
149
150/*
151 * Grand Central Station for events.
152 *
153 * The event hub aggregates input events received across all known input
154 * devices on the system, including devices that may be emulated by the simulator
155 * environment. In addition, the event hub generates fake input events to indicate
156 * when devices are added or removed.
157 *
158 * The event hub provides a stream of input events (via the getEvent function).
159 * It also supports querying the current actual state of input devices such as identifying
160 * which keys are currently down. Finally, the event hub keeps track of the capabilities of
161 * individual input devices, such as their class and the set of key codes that they support.
162 */
163class EventHubInterface : public virtual RefBase {
164protected:
165 EventHubInterface() { }
166 virtual ~EventHubInterface() { }
167
168public:
169 // Synthetic raw event type codes produced when devices are added or removed.
170 enum {
171 // Sent when a device is added.
172 DEVICE_ADDED = 0x10000000,
173 // Sent when a device is removed.
174 DEVICE_REMOVED = 0x20000000,
175 // Sent when all added/removed devices from the most recent scan have been reported.
176 // This event is always sent at least once.
177 FINISHED_DEVICE_SCAN = 0x30000000,
178
179 FIRST_SYNTHETIC_EVENT = DEVICE_ADDED,
180 };
181
182 virtual uint32_t getDeviceClasses(int32_t deviceId) const = 0;
183
184 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const = 0;
185
186 virtual int32_t getDeviceControllerNumber(int32_t deviceId) const = 0;
187
188 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const = 0;
189
190 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
191 RawAbsoluteAxisInfo* outAxisInfo) const = 0;
192
193 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const = 0;
194
195 virtual bool hasInputProperty(int32_t deviceId, int property) const = 0;
196
197 virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
198 int32_t* outKeycode, uint32_t* outFlags) const = 0;
199
200 virtual status_t mapAxis(int32_t deviceId, int32_t scanCode,
201 AxisInfo* outAxisInfo) const = 0;
202
203 // Sets devices that are excluded from opening.
204 // This can be used to ignore input devices for sensors.
205 virtual void setExcludedDevices(const Vector<String8>& devices) = 0;
206
207 /*
208 * Wait for events to become available and returns them.
209 * After returning, the EventHub holds onto a wake lock until the next call to getEvent.
210 * This ensures that the device will not go to sleep while the event is being processed.
211 * If the device needs to remain awake longer than that, then the caller is responsible
212 * for taking care of it (say, by poking the power manager user activity timer).
213 *
214 * The timeout is advisory only. If the device is asleep, it will not wake just to
215 * service the timeout.
216 *
217 * Returns the number of events obtained, or 0 if the timeout expired.
218 */
219 virtual size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) = 0;
220
221 /*
222 * Query current input state.
223 */
224 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0;
225 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0;
226 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0;
227 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
228 int32_t* outValue) const = 0;
229
230 /*
231 * Examine key input devices for specific framework keycode support
232 */
233 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
234 uint8_t* outFlags) const = 0;
235
236 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const = 0;
237
238 /* LED related functions expect Android LED constants, not scan codes or HID usages */
239 virtual bool hasLed(int32_t deviceId, int32_t led) const = 0;
240 virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0;
241
242 virtual void getVirtualKeyDefinitions(int32_t deviceId,
243 Vector<VirtualKeyDefinition>& outVirtualKeys) const = 0;
244
245 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const = 0;
246 virtual bool setKeyboardLayoutOverlay(int32_t deviceId, const sp<KeyCharacterMap>& map) = 0;
247
248 /* Control the vibrator. */
249 virtual void vibrate(int32_t deviceId, nsecs_t duration) = 0;
250 virtual void cancelVibrate(int32_t deviceId) = 0;
251
252 /* Requests the EventHub to reopen all input devices on the next call to getEvents(). */
253 virtual void requestReopenDevices() = 0;
254
255 /* Wakes up getEvents() if it is blocked on a read. */
256 virtual void wake() = 0;
257
258 /* Dump EventHub state to a string. */
259 virtual void dump(String8& dump) = 0;
260
261 /* Called by the heatbeat to ensures that the reader has not deadlocked. */
262 virtual void monitor() = 0;
263};
264
265class EventHub : public EventHubInterface
266{
267public:
268 EventHub();
269
270 virtual uint32_t getDeviceClasses(int32_t deviceId) const;
271
272 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const;
273
274 virtual int32_t getDeviceControllerNumber(int32_t deviceId) const;
275
276 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const;
277
278 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
279 RawAbsoluteAxisInfo* outAxisInfo) const;
280
281 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const;
282
283 virtual bool hasInputProperty(int32_t deviceId, int property) const;
284
285 virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
286 int32_t* outKeycode, uint32_t* outFlags) const;
287
288 virtual status_t mapAxis(int32_t deviceId, int32_t scanCode,
289 AxisInfo* outAxisInfo) const;
290
291 virtual void setExcludedDevices(const Vector<String8>& devices);
292
293 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const;
294 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const;
295 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const;
296 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t* outValue) const;
297
298 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
299 const int32_t* keyCodes, uint8_t* outFlags) const;
300
301 virtual size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize);
302
303 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const;
304 virtual bool hasLed(int32_t deviceId, int32_t led) const;
305 virtual void setLedState(int32_t deviceId, int32_t led, bool on);
306
307 virtual void getVirtualKeyDefinitions(int32_t deviceId,
308 Vector<VirtualKeyDefinition>& outVirtualKeys) const;
309
310 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const;
311 virtual bool setKeyboardLayoutOverlay(int32_t deviceId, const sp<KeyCharacterMap>& map);
312
313 virtual void vibrate(int32_t deviceId, nsecs_t duration);
314 virtual void cancelVibrate(int32_t deviceId);
315
316 virtual void requestReopenDevices();
317
318 virtual void wake();
319
320 virtual void dump(String8& dump);
321 virtual void monitor();
322
323protected:
324 virtual ~EventHub();
325
326private:
327 struct Device {
328 Device* next;
329
330 int fd; // may be -1 if device is virtual
331 const int32_t id;
332 const String8 path;
333 const InputDeviceIdentifier identifier;
334
335 uint32_t classes;
336
337 uint8_t keyBitmask[(KEY_MAX + 1) / 8];
338 uint8_t absBitmask[(ABS_MAX + 1) / 8];
339 uint8_t relBitmask[(REL_MAX + 1) / 8];
340 uint8_t swBitmask[(SW_MAX + 1) / 8];
341 uint8_t ledBitmask[(LED_MAX + 1) / 8];
342 uint8_t ffBitmask[(FF_MAX + 1) / 8];
343 uint8_t propBitmask[(INPUT_PROP_MAX + 1) / 8];
344
345 String8 configurationFile;
346 PropertyMap* configuration;
347 VirtualKeyMap* virtualKeyMap;
348 KeyMap keyMap;
349
350 sp<KeyCharacterMap> overlayKeyMap;
351 sp<KeyCharacterMap> combinedKeyMap;
352
353 bool ffEffectPlaying;
354 int16_t ffEffectId; // initially -1
355
356 int32_t controllerNumber;
357
358 int32_t timestampOverrideSec;
359 int32_t timestampOverrideUsec;
360
361 Device(int fd, int32_t id, const String8& path, const InputDeviceIdentifier& identifier);
362 ~Device();
363
364 void close();
365
366 inline bool isVirtual() const { return fd < 0; }
367
368 const sp<KeyCharacterMap>& getKeyCharacterMap() const {
369 if (combinedKeyMap != NULL) {
370 return combinedKeyMap;
371 }
372 return keyMap.keyCharacterMap;
373 }
374 };
375
376 status_t openDeviceLocked(const char *devicePath);
377 void createVirtualKeyboardLocked();
378 void addDeviceLocked(Device* device);
379 void assignDescriptorLocked(InputDeviceIdentifier& identifier);
380
381 status_t closeDeviceByPathLocked(const char *devicePath);
382 void closeDeviceLocked(Device* device);
383 void closeAllDevicesLocked();
384
385 status_t scanDirLocked(const char *dirname);
386 void scanDevicesLocked();
387 status_t readNotifyLocked();
388
389 Device* getDeviceByDescriptorLocked(String8& descriptor) const;
390 Device* getDeviceLocked(int32_t deviceId) const;
391 Device* getDeviceByPathLocked(const char* devicePath) const;
392
393 bool hasKeycodeLocked(Device* device, int keycode) const;
394
395 void loadConfigurationLocked(Device* device);
396 status_t loadVirtualKeyMapLocked(Device* device);
397 status_t loadKeyMapLocked(Device* device);
398
399 bool isExternalDeviceLocked(Device* device);
Tim Kilbourn063ff532015-04-08 10:26:18 -0700400 bool deviceHasMicLocked(Device* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800401
402 int32_t getNextControllerNumberLocked(Device* device);
403 void releaseControllerNumberLocked(Device* device);
404 void setLedForController(Device* device);
405
406 status_t mapLed(Device* device, int32_t led, int32_t* outScanCode) const;
407 void setLedStateLocked(Device* device, int32_t led, bool on);
408
409 // Protect all internal state.
410 mutable Mutex mLock;
411
412 // The actual id of the built-in keyboard, or NO_BUILT_IN_KEYBOARD if none.
413 // EventHub remaps the built-in keyboard to id 0 externally as required by the API.
414 enum {
415 // Must not conflict with any other assigned device ids, including
416 // the virtual keyboard id (-1).
417 NO_BUILT_IN_KEYBOARD = -2,
418 };
419 int32_t mBuiltInKeyboardId;
420
421 int32_t mNextDeviceId;
422
423 BitSet32 mControllerNumbers;
424
425 KeyedVector<int32_t, Device*> mDevices;
426
427 Device *mOpeningDevices;
428 Device *mClosingDevices;
429
430 bool mNeedToSendFinishedDeviceScan;
431 bool mNeedToReopenDevices;
432 bool mNeedToScanDevices;
433 Vector<String8> mExcludedDevices;
434
435 int mEpollFd;
436 int mINotifyFd;
437 int mWakeReadPipeFd;
438 int mWakeWritePipeFd;
439
440 // Ids used for epoll notifications not associated with devices.
441 static const uint32_t EPOLL_ID_INOTIFY = 0x80000001;
442 static const uint32_t EPOLL_ID_WAKE = 0x80000002;
443
444 // Epoll FD list size hint.
445 static const int EPOLL_SIZE_HINT = 8;
446
447 // Maximum number of signalled FDs to handle at a time.
448 static const int EPOLL_MAX_EVENTS = 16;
449
450 // The array of pending epoll events and the index of the next event to be handled.
451 struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS];
452 size_t mPendingEventCount;
453 size_t mPendingEventIndex;
454 bool mPendingINotify;
455
456 bool mUsingEpollWakeup;
457};
458
459}; // namespace android
460
461#endif // _RUNTIME_EVENT_HUB_H