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