blob: 2cea01776aa43758360f9f82465c4724ea2e0201 [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
Michael Wrightd02c5b62014-02-10 15:10:22 -080017#ifndef _RUNTIME_EVENT_HUB_H
18#define _RUNTIME_EVENT_HUB_H
19
Chris Ye66fbac32020-07-06 20:36:43 -070020#include <bitset>
21#include <climits>
Chris Ye989bb932020-07-04 16:18:59 -070022#include <unordered_map>
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010023#include <vector>
24
Chris Ye1b0c7342020-07-28 21:57:03 -070025#include <input/Flags.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080026#include <input/Input.h>
27#include <input/InputDevice.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080028#include <input/KeyCharacterMap.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070029#include <input/KeyLayoutMap.h>
30#include <input/Keyboard.h>
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070031#include <input/PropertyMap.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080032#include <input/VirtualKeyMap.h>
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000033#include <linux/input.h>
34#include <sys/epoll.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080035#include <utils/BitSet.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070036#include <utils/Errors.h>
37#include <utils/KeyedVector.h>
38#include <utils/List.h>
39#include <utils/Log.h>
40#include <utils/Mutex.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080041
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080042#include "TouchVideoDevice.h"
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000043#include "VibrationElement.h"
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080044
Michael Wrightd02c5b62014-02-10 15:10:22 -080045namespace android {
46
Michael Wrightd02c5b62014-02-10 15:10:22 -080047/*
48 * A raw event as retrieved from the EventHub.
49 */
50struct RawEvent {
51 nsecs_t when;
52 int32_t deviceId;
53 int32_t type;
54 int32_t code;
55 int32_t value;
56};
57
58/* Describes an absolute axis. */
59struct RawAbsoluteAxisInfo {
60 bool valid; // true if the information is valid, false otherwise
61
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070062 int32_t minValue; // minimum value
63 int32_t maxValue; // maximum value
64 int32_t flat; // center flat position, eg. flat == 8 means center is between -8 and 8
65 int32_t fuzz; // error tolerance, eg. fuzz == 4 means value is +/- 4 due to noise
Michael Wrightd02c5b62014-02-10 15:10:22 -080066 int32_t resolution; // resolution in units per mm or radians per mm
67
68 inline void clear() {
69 valid = false;
70 minValue = 0;
71 maxValue = 0;
72 flat = 0;
73 fuzz = 0;
74 resolution = 0;
75 }
76};
77
78/*
79 * Input device classes.
80 */
Chris Ye1b0c7342020-07-28 21:57:03 -070081enum class InputDeviceClass : uint32_t {
Michael Wrightd02c5b62014-02-10 15:10:22 -080082 /* The input device is a keyboard or has buttons. */
Chris Ye1b0c7342020-07-28 21:57:03 -070083 KEYBOARD = 0x00000001,
Michael Wrightd02c5b62014-02-10 15:10:22 -080084
85 /* The input device is an alpha-numeric keyboard (not just a dial pad). */
Chris Ye1b0c7342020-07-28 21:57:03 -070086 ALPHAKEY = 0x00000002,
Michael Wrightd02c5b62014-02-10 15:10:22 -080087
88 /* The input device is a touchscreen or a touchpad (either single-touch or multi-touch). */
Chris Ye1b0c7342020-07-28 21:57:03 -070089 TOUCH = 0x00000004,
Michael Wrightd02c5b62014-02-10 15:10:22 -080090
91 /* The input device is a cursor device such as a trackball or mouse. */
Chris Ye1b0c7342020-07-28 21:57:03 -070092 CURSOR = 0x00000008,
Michael Wrightd02c5b62014-02-10 15:10:22 -080093
94 /* The input device is a multi-touch touchscreen. */
Chris Ye1b0c7342020-07-28 21:57:03 -070095 TOUCH_MT = 0x00000010,
Michael Wrightd02c5b62014-02-10 15:10:22 -080096
97 /* The input device is a directional pad (implies keyboard, has DPAD keys). */
Chris Ye1b0c7342020-07-28 21:57:03 -070098 DPAD = 0x00000020,
Michael Wrightd02c5b62014-02-10 15:10:22 -080099
100 /* The input device is a gamepad (implies keyboard, has BUTTON keys). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700101 GAMEPAD = 0x00000040,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800102
103 /* The input device has switches. */
Chris Ye1b0c7342020-07-28 21:57:03 -0700104 SWITCH = 0x00000080,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800105
106 /* The input device is a joystick (implies gamepad, has joystick absolute axes). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700107 JOYSTICK = 0x00000100,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800108
109 /* The input device has a vibrator (supports FF_RUMBLE). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700110 VIBRATOR = 0x00000200,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800111
Tim Kilbourn063ff532015-04-08 10:26:18 -0700112 /* The input device has a microphone. */
Chris Ye1b0c7342020-07-28 21:57:03 -0700113 MIC = 0x00000400,
Tim Kilbourn063ff532015-04-08 10:26:18 -0700114
Michael Wright842500e2015-03-13 17:32:02 -0700115 /* The input device is an external stylus (has data we want to fuse with touch data). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700116 EXTERNAL_STYLUS = 0x00000800,
Michael Wright842500e2015-03-13 17:32:02 -0700117
Prashant Malani1941ff52015-08-11 18:29:28 -0700118 /* The input device has a rotary encoder */
Chris Ye1b0c7342020-07-28 21:57:03 -0700119 ROTARY_ENCODER = 0x00001000,
Prashant Malani1941ff52015-08-11 18:29:28 -0700120
Chris Yef59a2f42020-10-16 12:55:26 -0700121 /* The input device has a sensor like accelerometer, gyro, etc */
122 SENSOR = 0x00002000,
123
Michael Wrightd02c5b62014-02-10 15:10:22 -0800124 /* The input device is virtual (not a real device, not part of UI configuration). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700125 VIRTUAL = 0x40000000,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800126
127 /* The input device is external (not built-in). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700128 EXTERNAL = 0x80000000,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800129};
130
131/*
132 * Gets the class that owns an axis, in cases where multiple classes might claim
133 * the same axis for different purposes.
134 */
Chris Ye1b0c7342020-07-28 21:57:03 -0700135extern Flags<InputDeviceClass> getAbsAxisUsage(int32_t axis, Flags<InputDeviceClass> deviceClasses);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800136
137/*
138 * Grand Central Station for events.
139 *
140 * The event hub aggregates input events received across all known input
141 * devices on the system, including devices that may be emulated by the simulator
142 * environment. In addition, the event hub generates fake input events to indicate
143 * when devices are added or removed.
144 *
145 * The event hub provides a stream of input events (via the getEvent function).
146 * It also supports querying the current actual state of input devices such as identifying
147 * which keys are currently down. Finally, the event hub keeps track of the capabilities of
148 * individual input devices, such as their class and the set of key codes that they support.
149 */
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700150class EventHubInterface {
151public:
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700152 EventHubInterface() {}
153 virtual ~EventHubInterface() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154
Michael Wrightd02c5b62014-02-10 15:10:22 -0800155 // Synthetic raw event type codes produced when devices are added or removed.
156 enum {
157 // Sent when a device is added.
158 DEVICE_ADDED = 0x10000000,
159 // Sent when a device is removed.
160 DEVICE_REMOVED = 0x20000000,
161 // Sent when all added/removed devices from the most recent scan have been reported.
162 // This event is always sent at least once.
163 FINISHED_DEVICE_SCAN = 0x30000000,
164
165 FIRST_SYNTHETIC_EVENT = DEVICE_ADDED,
166 };
167
Chris Ye1b0c7342020-07-28 21:57:03 -0700168 virtual Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800169
170 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const = 0;
171
172 virtual int32_t getDeviceControllerNumber(int32_t deviceId) const = 0;
173
174 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const = 0;
175
176 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700177 RawAbsoluteAxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800178
179 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const = 0;
180
181 virtual bool hasInputProperty(int32_t deviceId, int property) const = 0;
182
Chris Yef59a2f42020-10-16 12:55:26 -0700183 virtual bool hasMscEvent(int32_t deviceId, int mscEvent) const = 0;
184
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700185 virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
186 int32_t metaState, int32_t* outKeycode, int32_t* outMetaState,
187 uint32_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700189 virtual status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800190
191 // Sets devices that are excluded from opening.
192 // This can be used to ignore input devices for sensors.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100193 virtual void setExcludedDevices(const std::vector<std::string>& devices) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800194
195 /*
196 * Wait for events to become available and returns them.
197 * After returning, the EventHub holds onto a wake lock until the next call to getEvent.
198 * This ensures that the device will not go to sleep while the event is being processed.
199 * If the device needs to remain awake longer than that, then the caller is responsible
200 * for taking care of it (say, by poking the power manager user activity timer).
201 *
202 * The timeout is advisory only. If the device is asleep, it will not wake just to
203 * service the timeout.
204 *
205 * Returns the number of events obtained, or 0 if the timeout expired.
206 */
207 virtual size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) = 0;
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800208 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) = 0;
Chris Yef59a2f42020-10-16 12:55:26 -0700209 virtual base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
210 int32_t absCode) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800211
212 /*
213 * Query current input state.
214 */
215 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0;
216 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0;
217 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0;
218 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700219 int32_t* outValue) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800220
221 /*
222 * Examine key input devices for specific framework keycode support
223 */
224 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700225 uint8_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800226
227 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const = 0;
228
229 /* LED related functions expect Android LED constants, not scan codes or HID usages */
230 virtual bool hasLed(int32_t deviceId, int32_t led) const = 0;
231 virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0;
232
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700233 virtual void getVirtualKeyDefinitions(
234 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800235
Chris Ye3a1e4462020-08-12 10:13:15 -0700236 virtual const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const = 0;
237 virtual bool setKeyboardLayoutOverlay(int32_t deviceId,
238 std::shared_ptr<KeyCharacterMap> map) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800239
240 /* Control the vibrator. */
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000241 virtual void vibrate(int32_t deviceId, const VibrationElement& effect) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800242 virtual void cancelVibrate(int32_t deviceId) = 0;
Chris Ye87143712020-11-10 05:05:58 +0000243 virtual std::vector<int32_t> getVibratorIds(int32_t deviceId) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800244
245 /* Requests the EventHub to reopen all input devices on the next call to getEvents(). */
246 virtual void requestReopenDevices() = 0;
247
248 /* Wakes up getEvents() if it is blocked on a read. */
249 virtual void wake() = 0;
250
251 /* Dump EventHub state to a string. */
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800252 virtual void dump(std::string& dump) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800253
254 /* Called by the heatbeat to ensures that the reader has not deadlocked. */
255 virtual void monitor() = 0;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700256
257 /* Return true if the device is enabled. */
258 virtual bool isDeviceEnabled(int32_t deviceId) = 0;
259
260 /* Enable an input device */
261 virtual status_t enableDevice(int32_t deviceId) = 0;
262
263 /* Disable an input device. Closes file descriptor to that device. */
264 virtual status_t disableDevice(int32_t deviceId) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800265};
266
Chris Ye66fbac32020-07-06 20:36:43 -0700267template <std::size_t BITS>
268class BitArray {
269 /* Array element type and vector of element type. */
270 using Element = std::uint32_t;
271 /* Number of bits in each BitArray element. */
272 static constexpr size_t WIDTH = sizeof(Element) * CHAR_BIT;
273 /* Number of elements to represent a bit array of the specified size of bits. */
274 static constexpr size_t COUNT = (BITS + WIDTH - 1) / WIDTH;
275
276public:
277 /* BUFFER type declaration for BitArray */
278 using Buffer = std::array<Element, COUNT>;
279 /* To tell if a bit is set in array, it selects an element from the array, and test
280 * if the relevant bit set.
281 * Note the parameter "bit" is an index to the bit, 0 <= bit < BITS.
282 */
283 inline bool test(size_t bit) const {
284 return (bit < BITS) ? mData[bit / WIDTH].test(bit % WIDTH) : false;
285 }
286 /* Returns total number of bytes needed for the array */
287 inline size_t bytes() { return (BITS + CHAR_BIT - 1) / CHAR_BIT; }
288 /* Returns true if array contains any non-zero bit from the range defined by start and end
289 * bit index [startIndex, endIndex).
290 */
291 bool any(size_t startIndex, size_t endIndex) {
292 if (startIndex >= endIndex || startIndex > BITS || endIndex > BITS + 1) {
293 ALOGE("Invalid start/end index. start = %zu, end = %zu, total bits = %zu", startIndex,
294 endIndex, BITS);
295 return false;
296 }
297 size_t se = startIndex / WIDTH; // Start of element
298 size_t ee = endIndex / WIDTH; // End of element
299 size_t si = startIndex % WIDTH; // Start index in start element
300 size_t ei = endIndex % WIDTH; // End index in end element
301 // Need to check first unaligned bitset for any non zero bit
302 if (si > 0) {
303 size_t nBits = se == ee ? ei - si : WIDTH - si;
304 // Generate the mask of interested bit range
305 Element mask = ((1 << nBits) - 1) << si;
306 if (mData[se++].to_ulong() & mask) {
307 return true;
308 }
309 }
310 // Check whole bitset for any bit set
311 for (; se < ee; se++) {
312 if (mData[se].any()) {
313 return true;
314 }
315 }
316 // Need to check last unaligned bitset for any non zero bit
317 if (ei > 0 && se <= ee) {
318 // Generate the mask of interested bit range
319 Element mask = (1 << ei) - 1;
320 if (mData[se].to_ulong() & mask) {
321 return true;
322 }
323 }
324 return false;
325 }
326 /* Load bit array values from buffer */
327 void loadFromBuffer(const Buffer& buffer) {
328 for (size_t i = 0; i < COUNT; i++) {
329 mData[i] = std::bitset<WIDTH>(buffer[i]);
330 }
331 }
332
333private:
334 std::array<std::bitset<WIDTH>, COUNT> mData;
335};
336
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700337class EventHub : public EventHubInterface {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338public:
339 EventHub();
340
Chris Ye989bb932020-07-04 16:18:59 -0700341 Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800342
Chris Ye989bb932020-07-04 16:18:59 -0700343 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344
Chris Ye989bb932020-07-04 16:18:59 -0700345 int32_t getDeviceControllerNumber(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800346
Chris Ye989bb932020-07-04 16:18:59 -0700347 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800348
Chris Ye989bb932020-07-04 16:18:59 -0700349 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
350 RawAbsoluteAxisInfo* outAxisInfo) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351
Chris Ye989bb932020-07-04 16:18:59 -0700352 bool hasRelativeAxis(int32_t deviceId, int axis) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800353
Chris Ye989bb932020-07-04 16:18:59 -0700354 bool hasInputProperty(int32_t deviceId, int property) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800355
Chris Yef59a2f42020-10-16 12:55:26 -0700356 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final;
357
Chris Ye989bb932020-07-04 16:18:59 -0700358 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
359 int32_t* outKeycode, int32_t* outMetaState,
360 uint32_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800361
Chris Ye989bb932020-07-04 16:18:59 -0700362 status_t mapAxis(int32_t deviceId, int32_t scanCode,
363 AxisInfo* outAxisInfo) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800364
Chris Yef59a2f42020-10-16 12:55:26 -0700365 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
366 int32_t deviceId, int32_t absCode) override final;
367
Chris Ye989bb932020-07-04 16:18:59 -0700368 void setExcludedDevices(const std::vector<std::string>& devices) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800369
Chris Ye989bb932020-07-04 16:18:59 -0700370 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override final;
371 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override final;
372 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override final;
373 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
374 int32_t* outValue) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800375
Chris Ye989bb932020-07-04 16:18:59 -0700376 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
377 uint8_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800378
Chris Ye989bb932020-07-04 16:18:59 -0700379 size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) override final;
380 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800381
Chris Ye989bb932020-07-04 16:18:59 -0700382 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override final;
383 bool hasLed(int32_t deviceId, int32_t led) const override final;
384 void setLedState(int32_t deviceId, int32_t led, bool on) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800385
Chris Ye989bb932020-07-04 16:18:59 -0700386 void getVirtualKeyDefinitions(
387 int32_t deviceId,
388 std::vector<VirtualKeyDefinition>& outVirtualKeys) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800389
Chris Ye3a1e4462020-08-12 10:13:15 -0700390 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(
391 int32_t deviceId) const override final;
392 bool setKeyboardLayoutOverlay(int32_t deviceId,
393 std::shared_ptr<KeyCharacterMap> map) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800394
Chris Ye989bb932020-07-04 16:18:59 -0700395 void vibrate(int32_t deviceId, const VibrationElement& effect) override final;
396 void cancelVibrate(int32_t deviceId) override final;
Chris Ye87143712020-11-10 05:05:58 +0000397 std::vector<int32_t> getVibratorIds(int32_t deviceId) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800398
Chris Ye989bb932020-07-04 16:18:59 -0700399 void requestReopenDevices() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800400
Chris Ye989bb932020-07-04 16:18:59 -0700401 void wake() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800402
Chris Ye989bb932020-07-04 16:18:59 -0700403 void dump(std::string& dump) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800404
Chris Ye989bb932020-07-04 16:18:59 -0700405 void monitor() override final;
406
407 bool isDeviceEnabled(int32_t deviceId) override final;
408
409 status_t enableDevice(int32_t deviceId) override final;
410
411 status_t disableDevice(int32_t deviceId) override final;
412
413 ~EventHub() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800414
415private:
416 struct Device {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700417 int fd; // may be -1 if device is closed
Michael Wrightd02c5b62014-02-10 15:10:22 -0800418 const int32_t id;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100419 const std::string path;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800420 const InputDeviceIdentifier identifier;
421
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -0800422 std::unique_ptr<TouchVideoDevice> videoDevice;
423
Chris Ye1b0c7342020-07-28 21:57:03 -0700424 Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800425
Chris Ye66fbac32020-07-06 20:36:43 -0700426 BitArray<KEY_MAX> keyBitmask;
427 BitArray<KEY_MAX> keyState;
428 BitArray<ABS_MAX> absBitmask;
429 BitArray<REL_MAX> relBitmask;
430 BitArray<SW_MAX> swBitmask;
431 BitArray<SW_MAX> swState;
432 BitArray<LED_MAX> ledBitmask;
433 BitArray<FF_MAX> ffBitmask;
434 BitArray<INPUT_PROP_MAX> propBitmask;
Chris Yef59a2f42020-10-16 12:55:26 -0700435 BitArray<MSC_MAX> mscBitmask;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800436
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100437 std::string configurationFile;
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500438 std::unique_ptr<PropertyMap> configuration;
Siarhei Vishniakou3e78dec2019-02-20 16:21:46 -0600439 std::unique_ptr<VirtualKeyMap> virtualKeyMap;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800440 KeyMap keyMap;
441
Michael Wrightd02c5b62014-02-10 15:10:22 -0800442 bool ffEffectPlaying;
443 int16_t ffEffectId; // initially -1
444
445 int32_t controllerNumber;
446
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100447 Device(int fd, int32_t id, const std::string& path,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700448 const InputDeviceIdentifier& identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800449 ~Device();
450
451 void close();
452
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700453 bool enabled; // initially true
454 status_t enable();
455 status_t disable();
Chris Ye989bb932020-07-04 16:18:59 -0700456 bool hasValidFd() const;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700457 const bool isVirtual; // set if fd < 0 is passed to constructor
Michael Wrightd02c5b62014-02-10 15:10:22 -0800458
Chris Ye3a1e4462020-08-12 10:13:15 -0700459 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const;
Chris Ye66fbac32020-07-06 20:36:43 -0700460
461 template <std::size_t N>
Chris Ye989bb932020-07-04 16:18:59 -0700462 status_t readDeviceBitMask(unsigned long ioctlCode, BitArray<N>& bitArray);
Chris Ye66fbac32020-07-06 20:36:43 -0700463
Chris Ye989bb932020-07-04 16:18:59 -0700464 void configureFd();
465 bool hasKeycodeLocked(int keycode) const;
466 void loadConfigurationLocked();
467 bool loadVirtualKeyMapLocked();
468 status_t loadKeyMapLocked();
469 bool isExternalDeviceLocked();
470 bool deviceHasMicLocked();
471 void setLedForControllerLocked();
472 status_t mapLed(int32_t led, int32_t* outScanCode) const;
473 void setLedStateLocked(int32_t led, bool on);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800474 };
475
Chris Ye8594e192020-07-14 10:34:06 -0700476 status_t openDeviceLocked(const std::string& devicePath);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800477 void openVideoDeviceLocked(const std::string& devicePath);
Siarhei Vishniakouf49608d2020-08-20 19:18:21 -0500478 /**
479 * Try to associate a video device with an input device. If the association succeeds,
480 * the videoDevice is moved into the input device. 'videoDevice' will become null if this
481 * happens.
482 * Return true if the association succeeds.
483 * Return false otherwise.
484 */
485 bool tryAddVideoDevice(Device& device, std::unique_ptr<TouchVideoDevice>& videoDevice);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800486 void createVirtualKeyboardLocked();
Chris Ye989bb932020-07-04 16:18:59 -0700487 void addDeviceLocked(std::unique_ptr<Device> device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800488 void assignDescriptorLocked(InputDeviceIdentifier& identifier);
489
Chris Ye8594e192020-07-14 10:34:06 -0700490 void closeDeviceByPathLocked(const std::string& devicePath);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800491 void closeVideoDeviceByPathLocked(const std::string& devicePath);
Chris Ye989bb932020-07-04 16:18:59 -0700492 void closeDeviceLocked(Device& device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800493 void closeAllDevicesLocked();
494
Siarhei Vishniakou25920312018-12-12 15:24:44 -0800495 status_t registerFdForEpoll(int fd);
496 status_t unregisterFdFromEpoll(int fd);
Chris Ye989bb932020-07-04 16:18:59 -0700497 status_t registerDeviceForEpollLocked(Device& device);
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700498 void registerVideoDeviceForEpollLocked(const TouchVideoDevice& videoDevice);
Chris Ye989bb932020-07-04 16:18:59 -0700499 status_t unregisterDeviceFromEpollLocked(Device& device);
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700500 void unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& videoDevice);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700501
Chris Ye8594e192020-07-14 10:34:06 -0700502 status_t scanDirLocked(const std::string& dirname);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800503 status_t scanVideoDirLocked(const std::string& dirname);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800504 void scanDevicesLocked();
505 status_t readNotifyLocked();
506
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100507 Device* getDeviceByDescriptorLocked(const std::string& descriptor) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800508 Device* getDeviceLocked(int32_t deviceId) const;
Chris Ye8594e192020-07-14 10:34:06 -0700509 Device* getDeviceByPathLocked(const std::string& devicePath) const;
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700510 /**
511 * Look through all available fd's (both for input devices and for video devices),
512 * and return the device pointer.
513 */
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -0700514 Device* getDeviceByFdLocked(int fd) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800515
Chris Ye989bb932020-07-04 16:18:59 -0700516 int32_t getNextControllerNumberLocked(const std::string& name);
517 void releaseControllerNumberLocked(int32_t num);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800518
519 // Protect all internal state.
Chris Ye1c2e0892020-11-30 21:41:44 -0800520 mutable std::mutex mLock;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800521
522 // The actual id of the built-in keyboard, or NO_BUILT_IN_KEYBOARD if none.
523 // EventHub remaps the built-in keyboard to id 0 externally as required by the API.
524 enum {
525 // Must not conflict with any other assigned device ids, including
526 // the virtual keyboard id (-1).
527 NO_BUILT_IN_KEYBOARD = -2,
528 };
529 int32_t mBuiltInKeyboardId;
530
531 int32_t mNextDeviceId;
532
533 BitSet32 mControllerNumbers;
534
Chris Ye989bb932020-07-04 16:18:59 -0700535 std::unordered_map<int32_t, std::unique_ptr<Device>> mDevices;
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800536 /**
537 * Video devices that report touchscreen heatmap, but have not (yet) been paired
538 * with a specific input device. Video device discovery is independent from input device
539 * discovery, so the two types of devices could be found in any order.
540 * Ideally, video devices in this queue do not have an open fd, or at least aren't
541 * actively streaming.
542 */
543 std::vector<std::unique_ptr<TouchVideoDevice>> mUnattachedVideoDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800544
Chris Ye989bb932020-07-04 16:18:59 -0700545 std::vector<std::unique_ptr<Device>> mOpeningDevices;
546 std::vector<std::unique_ptr<Device>> mClosingDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800547
548 bool mNeedToSendFinishedDeviceScan;
549 bool mNeedToReopenDevices;
550 bool mNeedToScanDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100551 std::vector<std::string> mExcludedDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800552
553 int mEpollFd;
554 int mINotifyFd;
555 int mWakeReadPipeFd;
556 int mWakeWritePipeFd;
557
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800558 int mInputWd;
559 int mVideoWd;
560
Michael Wrightd02c5b62014-02-10 15:10:22 -0800561 // Maximum number of signalled FDs to handle at a time.
562 static const int EPOLL_MAX_EVENTS = 16;
563
564 // The array of pending epoll events and the index of the next event to be handled.
565 struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS];
566 size_t mPendingEventCount;
567 size_t mPendingEventIndex;
568 bool mPendingINotify;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800569};
570
571}; // namespace android
572
573#endif // _RUNTIME_EVENT_HUB_H