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