blob: 9e38d0a72ade6f1ea09186e64f14bf373e810e7e [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
Michael Wrightd02c5b62014-02-10 15:10:22 -0800121 /* The input device is virtual (not a real device, not part of UI configuration). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700122 VIRTUAL = 0x40000000,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800123
124 /* The input device is external (not built-in). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700125 EXTERNAL = 0x80000000,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800126};
127
128/*
129 * Gets the class that owns an axis, in cases where multiple classes might claim
130 * the same axis for different purposes.
131 */
Chris Ye1b0c7342020-07-28 21:57:03 -0700132extern Flags<InputDeviceClass> getAbsAxisUsage(int32_t axis, Flags<InputDeviceClass> deviceClasses);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800133
134/*
135 * Grand Central Station for events.
136 *
137 * The event hub aggregates input events received across all known input
138 * devices on the system, including devices that may be emulated by the simulator
139 * environment. In addition, the event hub generates fake input events to indicate
140 * when devices are added or removed.
141 *
142 * The event hub provides a stream of input events (via the getEvent function).
143 * It also supports querying the current actual state of input devices such as identifying
144 * which keys are currently down. Finally, the event hub keeps track of the capabilities of
145 * individual input devices, such as their class and the set of key codes that they support.
146 */
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700147class EventHubInterface {
148public:
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700149 EventHubInterface() {}
150 virtual ~EventHubInterface() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800151
Michael Wrightd02c5b62014-02-10 15:10:22 -0800152 // Synthetic raw event type codes produced when devices are added or removed.
153 enum {
154 // Sent when a device is added.
155 DEVICE_ADDED = 0x10000000,
156 // Sent when a device is removed.
157 DEVICE_REMOVED = 0x20000000,
158 // Sent when all added/removed devices from the most recent scan have been reported.
159 // This event is always sent at least once.
160 FINISHED_DEVICE_SCAN = 0x30000000,
161
162 FIRST_SYNTHETIC_EVENT = DEVICE_ADDED,
163 };
164
Chris Ye1b0c7342020-07-28 21:57:03 -0700165 virtual Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800166
167 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const = 0;
168
169 virtual int32_t getDeviceControllerNumber(int32_t deviceId) const = 0;
170
171 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const = 0;
172
173 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700174 RawAbsoluteAxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800175
176 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const = 0;
177
178 virtual bool hasInputProperty(int32_t deviceId, int property) const = 0;
179
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700180 virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
181 int32_t metaState, int32_t* outKeycode, int32_t* outMetaState,
182 uint32_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800183
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700184 virtual status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800185
186 // Sets devices that are excluded from opening.
187 // This can be used to ignore input devices for sensors.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100188 virtual void setExcludedDevices(const std::vector<std::string>& devices) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800189
190 /*
191 * Wait for events to become available and returns them.
192 * After returning, the EventHub holds onto a wake lock until the next call to getEvent.
193 * This ensures that the device will not go to sleep while the event is being processed.
194 * If the device needs to remain awake longer than that, then the caller is responsible
195 * for taking care of it (say, by poking the power manager user activity timer).
196 *
197 * The timeout is advisory only. If the device is asleep, it will not wake just to
198 * service the timeout.
199 *
200 * Returns the number of events obtained, or 0 if the timeout expired.
201 */
202 virtual size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) = 0;
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800203 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800204
205 /*
206 * Query current input state.
207 */
208 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0;
209 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0;
210 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0;
211 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700212 int32_t* outValue) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800213
214 /*
215 * Examine key input devices for specific framework keycode support
216 */
217 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700218 uint8_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800219
220 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const = 0;
221
222 /* LED related functions expect Android LED constants, not scan codes or HID usages */
223 virtual bool hasLed(int32_t deviceId, int32_t led) const = 0;
224 virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0;
225
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700226 virtual void getVirtualKeyDefinitions(
227 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800228
Chris Ye3a1e4462020-08-12 10:13:15 -0700229 virtual const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const = 0;
230 virtual bool setKeyboardLayoutOverlay(int32_t deviceId,
231 std::shared_ptr<KeyCharacterMap> map) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800232
233 /* Control the vibrator. */
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000234 virtual void vibrate(int32_t deviceId, const VibrationElement& effect) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800235 virtual void cancelVibrate(int32_t deviceId) = 0;
Chris Ye87143712020-11-10 05:05:58 +0000236 virtual std::vector<int32_t> getVibratorIds(int32_t deviceId) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800237
238 /* Requests the EventHub to reopen all input devices on the next call to getEvents(). */
239 virtual void requestReopenDevices() = 0;
240
241 /* Wakes up getEvents() if it is blocked on a read. */
242 virtual void wake() = 0;
243
244 /* Dump EventHub state to a string. */
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800245 virtual void dump(std::string& dump) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800246
247 /* Called by the heatbeat to ensures that the reader has not deadlocked. */
248 virtual void monitor() = 0;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700249
250 /* Return true if the device is enabled. */
251 virtual bool isDeviceEnabled(int32_t deviceId) = 0;
252
253 /* Enable an input device */
254 virtual status_t enableDevice(int32_t deviceId) = 0;
255
256 /* Disable an input device. Closes file descriptor to that device. */
257 virtual status_t disableDevice(int32_t deviceId) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800258};
259
Chris Ye66fbac32020-07-06 20:36:43 -0700260template <std::size_t BITS>
261class BitArray {
262 /* Array element type and vector of element type. */
263 using Element = std::uint32_t;
264 /* Number of bits in each BitArray element. */
265 static constexpr size_t WIDTH = sizeof(Element) * CHAR_BIT;
266 /* Number of elements to represent a bit array of the specified size of bits. */
267 static constexpr size_t COUNT = (BITS + WIDTH - 1) / WIDTH;
268
269public:
270 /* BUFFER type declaration for BitArray */
271 using Buffer = std::array<Element, COUNT>;
272 /* To tell if a bit is set in array, it selects an element from the array, and test
273 * if the relevant bit set.
274 * Note the parameter "bit" is an index to the bit, 0 <= bit < BITS.
275 */
276 inline bool test(size_t bit) const {
277 return (bit < BITS) ? mData[bit / WIDTH].test(bit % WIDTH) : false;
278 }
279 /* Returns total number of bytes needed for the array */
280 inline size_t bytes() { return (BITS + CHAR_BIT - 1) / CHAR_BIT; }
281 /* Returns true if array contains any non-zero bit from the range defined by start and end
282 * bit index [startIndex, endIndex).
283 */
284 bool any(size_t startIndex, size_t endIndex) {
285 if (startIndex >= endIndex || startIndex > BITS || endIndex > BITS + 1) {
286 ALOGE("Invalid start/end index. start = %zu, end = %zu, total bits = %zu", startIndex,
287 endIndex, BITS);
288 return false;
289 }
290 size_t se = startIndex / WIDTH; // Start of element
291 size_t ee = endIndex / WIDTH; // End of element
292 size_t si = startIndex % WIDTH; // Start index in start element
293 size_t ei = endIndex % WIDTH; // End index in end element
294 // Need to check first unaligned bitset for any non zero bit
295 if (si > 0) {
296 size_t nBits = se == ee ? ei - si : WIDTH - si;
297 // Generate the mask of interested bit range
298 Element mask = ((1 << nBits) - 1) << si;
299 if (mData[se++].to_ulong() & mask) {
300 return true;
301 }
302 }
303 // Check whole bitset for any bit set
304 for (; se < ee; se++) {
305 if (mData[se].any()) {
306 return true;
307 }
308 }
309 // Need to check last unaligned bitset for any non zero bit
310 if (ei > 0 && se <= ee) {
311 // Generate the mask of interested bit range
312 Element mask = (1 << ei) - 1;
313 if (mData[se].to_ulong() & mask) {
314 return true;
315 }
316 }
317 return false;
318 }
319 /* Load bit array values from buffer */
320 void loadFromBuffer(const Buffer& buffer) {
321 for (size_t i = 0; i < COUNT; i++) {
322 mData[i] = std::bitset<WIDTH>(buffer[i]);
323 }
324 }
325
326private:
327 std::array<std::bitset<WIDTH>, COUNT> mData;
328};
329
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700330class EventHub : public EventHubInterface {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800331public:
332 EventHub();
333
Chris Ye989bb932020-07-04 16:18:59 -0700334 Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800335
Chris Ye989bb932020-07-04 16:18:59 -0700336 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800337
Chris Ye989bb932020-07-04 16:18:59 -0700338 int32_t getDeviceControllerNumber(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800339
Chris Ye989bb932020-07-04 16:18:59 -0700340 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800341
Chris Ye989bb932020-07-04 16:18:59 -0700342 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
343 RawAbsoluteAxisInfo* outAxisInfo) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344
Chris Ye989bb932020-07-04 16:18:59 -0700345 bool hasRelativeAxis(int32_t deviceId, int axis) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800346
Chris Ye989bb932020-07-04 16:18:59 -0700347 bool hasInputProperty(int32_t deviceId, int property) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800348
Chris Ye989bb932020-07-04 16:18:59 -0700349 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
350 int32_t* outKeycode, int32_t* outMetaState,
351 uint32_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800352
Chris Ye989bb932020-07-04 16:18:59 -0700353 status_t mapAxis(int32_t deviceId, int32_t scanCode,
354 AxisInfo* outAxisInfo) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800355
Chris Ye989bb932020-07-04 16:18:59 -0700356 void setExcludedDevices(const std::vector<std::string>& devices) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800357
Chris Ye989bb932020-07-04 16:18:59 -0700358 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override final;
359 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override final;
360 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override final;
361 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
362 int32_t* outValue) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800363
Chris Ye989bb932020-07-04 16:18:59 -0700364 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
365 uint8_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800366
Chris Ye989bb932020-07-04 16:18:59 -0700367 size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) override final;
368 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800369
Chris Ye989bb932020-07-04 16:18:59 -0700370 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override final;
371 bool hasLed(int32_t deviceId, int32_t led) const override final;
372 void setLedState(int32_t deviceId, int32_t led, bool on) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800373
Chris Ye989bb932020-07-04 16:18:59 -0700374 void getVirtualKeyDefinitions(
375 int32_t deviceId,
376 std::vector<VirtualKeyDefinition>& outVirtualKeys) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800377
Chris Ye3a1e4462020-08-12 10:13:15 -0700378 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(
379 int32_t deviceId) const override final;
380 bool setKeyboardLayoutOverlay(int32_t deviceId,
381 std::shared_ptr<KeyCharacterMap> map) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800382
Chris Ye989bb932020-07-04 16:18:59 -0700383 void vibrate(int32_t deviceId, const VibrationElement& effect) override final;
384 void cancelVibrate(int32_t deviceId) override final;
Chris Ye87143712020-11-10 05:05:58 +0000385 std::vector<int32_t> getVibratorIds(int32_t deviceId) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800386
Chris Ye989bb932020-07-04 16:18:59 -0700387 void requestReopenDevices() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800388
Chris Ye989bb932020-07-04 16:18:59 -0700389 void wake() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800390
Chris Ye989bb932020-07-04 16:18:59 -0700391 void dump(std::string& dump) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800392
Chris Ye989bb932020-07-04 16:18:59 -0700393 void monitor() override final;
394
395 bool isDeviceEnabled(int32_t deviceId) override final;
396
397 status_t enableDevice(int32_t deviceId) override final;
398
399 status_t disableDevice(int32_t deviceId) override final;
400
401 ~EventHub() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800402
403private:
404 struct Device {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700405 int fd; // may be -1 if device is closed
Michael Wrightd02c5b62014-02-10 15:10:22 -0800406 const int32_t id;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100407 const std::string path;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800408 const InputDeviceIdentifier identifier;
409
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -0800410 std::unique_ptr<TouchVideoDevice> videoDevice;
411
Chris Ye1b0c7342020-07-28 21:57:03 -0700412 Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800413
Chris Ye66fbac32020-07-06 20:36:43 -0700414 BitArray<KEY_MAX> keyBitmask;
415 BitArray<KEY_MAX> keyState;
416 BitArray<ABS_MAX> absBitmask;
417 BitArray<REL_MAX> relBitmask;
418 BitArray<SW_MAX> swBitmask;
419 BitArray<SW_MAX> swState;
420 BitArray<LED_MAX> ledBitmask;
421 BitArray<FF_MAX> ffBitmask;
422 BitArray<INPUT_PROP_MAX> propBitmask;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800423
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100424 std::string configurationFile;
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500425 std::unique_ptr<PropertyMap> configuration;
Siarhei Vishniakou3e78dec2019-02-20 16:21:46 -0600426 std::unique_ptr<VirtualKeyMap> virtualKeyMap;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800427 KeyMap keyMap;
428
Michael Wrightd02c5b62014-02-10 15:10:22 -0800429 bool ffEffectPlaying;
430 int16_t ffEffectId; // initially -1
431
432 int32_t controllerNumber;
433
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100434 Device(int fd, int32_t id, const std::string& path,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700435 const InputDeviceIdentifier& identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800436 ~Device();
437
438 void close();
439
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700440 bool enabled; // initially true
441 status_t enable();
442 status_t disable();
Chris Ye989bb932020-07-04 16:18:59 -0700443 bool hasValidFd() const;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700444 const bool isVirtual; // set if fd < 0 is passed to constructor
Michael Wrightd02c5b62014-02-10 15:10:22 -0800445
Chris Ye3a1e4462020-08-12 10:13:15 -0700446 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const;
Chris Ye66fbac32020-07-06 20:36:43 -0700447
448 template <std::size_t N>
Chris Ye989bb932020-07-04 16:18:59 -0700449 status_t readDeviceBitMask(unsigned long ioctlCode, BitArray<N>& bitArray);
Chris Ye66fbac32020-07-06 20:36:43 -0700450
Chris Ye989bb932020-07-04 16:18:59 -0700451 void configureFd();
452 bool hasKeycodeLocked(int keycode) const;
453 void loadConfigurationLocked();
454 bool loadVirtualKeyMapLocked();
455 status_t loadKeyMapLocked();
456 bool isExternalDeviceLocked();
457 bool deviceHasMicLocked();
458 void setLedForControllerLocked();
459 status_t mapLed(int32_t led, int32_t* outScanCode) const;
460 void setLedStateLocked(int32_t led, bool on);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800461 };
462
Chris Ye8594e192020-07-14 10:34:06 -0700463 status_t openDeviceLocked(const std::string& devicePath);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800464 void openVideoDeviceLocked(const std::string& devicePath);
Siarhei Vishniakouf49608d2020-08-20 19:18:21 -0500465 /**
466 * Try to associate a video device with an input device. If the association succeeds,
467 * the videoDevice is moved into the input device. 'videoDevice' will become null if this
468 * happens.
469 * Return true if the association succeeds.
470 * Return false otherwise.
471 */
472 bool tryAddVideoDevice(Device& device, std::unique_ptr<TouchVideoDevice>& videoDevice);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800473 void createVirtualKeyboardLocked();
Chris Ye989bb932020-07-04 16:18:59 -0700474 void addDeviceLocked(std::unique_ptr<Device> device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800475 void assignDescriptorLocked(InputDeviceIdentifier& identifier);
476
Chris Ye8594e192020-07-14 10:34:06 -0700477 void closeDeviceByPathLocked(const std::string& devicePath);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800478 void closeVideoDeviceByPathLocked(const std::string& devicePath);
Chris Ye989bb932020-07-04 16:18:59 -0700479 void closeDeviceLocked(Device& device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800480 void closeAllDevicesLocked();
481
Siarhei Vishniakou25920312018-12-12 15:24:44 -0800482 status_t registerFdForEpoll(int fd);
483 status_t unregisterFdFromEpoll(int fd);
Chris Ye989bb932020-07-04 16:18:59 -0700484 status_t registerDeviceForEpollLocked(Device& device);
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700485 void registerVideoDeviceForEpollLocked(const TouchVideoDevice& videoDevice);
Chris Ye989bb932020-07-04 16:18:59 -0700486 status_t unregisterDeviceFromEpollLocked(Device& device);
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700487 void unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& videoDevice);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700488
Chris Ye8594e192020-07-14 10:34:06 -0700489 status_t scanDirLocked(const std::string& dirname);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800490 status_t scanVideoDirLocked(const std::string& dirname);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800491 void scanDevicesLocked();
492 status_t readNotifyLocked();
493
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100494 Device* getDeviceByDescriptorLocked(const std::string& descriptor) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800495 Device* getDeviceLocked(int32_t deviceId) const;
Chris Ye8594e192020-07-14 10:34:06 -0700496 Device* getDeviceByPathLocked(const std::string& devicePath) const;
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700497 /**
498 * Look through all available fd's (both for input devices and for video devices),
499 * and return the device pointer.
500 */
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -0700501 Device* getDeviceByFdLocked(int fd) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800502
Chris Ye989bb932020-07-04 16:18:59 -0700503 int32_t getNextControllerNumberLocked(const std::string& name);
504 void releaseControllerNumberLocked(int32_t num);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800505
506 // Protect all internal state.
Chris Ye1c2e0892020-11-30 21:41:44 -0800507 mutable std::mutex mLock;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800508
509 // The actual id of the built-in keyboard, or NO_BUILT_IN_KEYBOARD if none.
510 // EventHub remaps the built-in keyboard to id 0 externally as required by the API.
511 enum {
512 // Must not conflict with any other assigned device ids, including
513 // the virtual keyboard id (-1).
514 NO_BUILT_IN_KEYBOARD = -2,
515 };
516 int32_t mBuiltInKeyboardId;
517
518 int32_t mNextDeviceId;
519
520 BitSet32 mControllerNumbers;
521
Chris Ye989bb932020-07-04 16:18:59 -0700522 std::unordered_map<int32_t, std::unique_ptr<Device>> mDevices;
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800523 /**
524 * Video devices that report touchscreen heatmap, but have not (yet) been paired
525 * with a specific input device. Video device discovery is independent from input device
526 * discovery, so the two types of devices could be found in any order.
527 * Ideally, video devices in this queue do not have an open fd, or at least aren't
528 * actively streaming.
529 */
530 std::vector<std::unique_ptr<TouchVideoDevice>> mUnattachedVideoDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800531
Chris Ye989bb932020-07-04 16:18:59 -0700532 std::vector<std::unique_ptr<Device>> mOpeningDevices;
533 std::vector<std::unique_ptr<Device>> mClosingDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800534
535 bool mNeedToSendFinishedDeviceScan;
536 bool mNeedToReopenDevices;
537 bool mNeedToScanDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100538 std::vector<std::string> mExcludedDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800539
540 int mEpollFd;
541 int mINotifyFd;
542 int mWakeReadPipeFd;
543 int mWakeWritePipeFd;
544
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800545 int mInputWd;
546 int mVideoWd;
547
Michael Wrightd02c5b62014-02-10 15:10:22 -0800548 // Maximum number of signalled FDs to handle at a time.
549 static const int EPOLL_MAX_EVENTS = 16;
550
551 // The array of pending epoll events and the index of the next event to be handled.
552 struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS];
553 size_t mPendingEventCount;
554 size_t mPendingEventIndex;
555 bool mPendingINotify;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800556};
557
558}; // namespace android
559
560#endif // _RUNTIME_EVENT_HUB_H