blob: 13364e0a404972fa08e5f95b48c1977a7ff82379 [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>
Kim Low03ea0352020-11-06 12:45:07 -080026#include <filesystem>
27
28#include <batteryservice/BatteryService.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080029#include <input/Input.h>
30#include <input/InputDevice.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080031#include <input/KeyCharacterMap.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070032#include <input/KeyLayoutMap.h>
33#include <input/Keyboard.h>
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070034#include <input/PropertyMap.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080035#include <input/VirtualKeyMap.h>
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000036#include <linux/input.h>
37#include <sys/epoll.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080038#include <utils/BitSet.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070039#include <utils/Errors.h>
40#include <utils/KeyedVector.h>
41#include <utils/List.h>
42#include <utils/Log.h>
43#include <utils/Mutex.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080044
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080045#include "TouchVideoDevice.h"
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000046#include "VibrationElement.h"
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080047
Michael Wrightd02c5b62014-02-10 15:10:22 -080048namespace android {
49
Michael Wrightd02c5b62014-02-10 15:10:22 -080050/*
51 * A raw event as retrieved from the EventHub.
52 */
53struct RawEvent {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000054 // Time when the event happened
Michael Wrightd02c5b62014-02-10 15:10:22 -080055 nsecs_t when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000056 // Time when the event was read by EventHub. Only populated for input events.
57 // For other events (device added/removed/etc), this value is undefined and should not be read.
58 nsecs_t readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -080059 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
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070069 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
Michael Wrightd02c5b62014-02-10 15:10:22 -080073 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 */
Chris Ye1b0c7342020-07-28 21:57:03 -070088enum class InputDeviceClass : uint32_t {
Michael Wrightd02c5b62014-02-10 15:10:22 -080089 /* The input device is a keyboard or has buttons. */
Chris Ye1b0c7342020-07-28 21:57:03 -070090 KEYBOARD = 0x00000001,
Michael Wrightd02c5b62014-02-10 15:10:22 -080091
92 /* The input device is an alpha-numeric keyboard (not just a dial pad). */
Chris Ye1b0c7342020-07-28 21:57:03 -070093 ALPHAKEY = 0x00000002,
Michael Wrightd02c5b62014-02-10 15:10:22 -080094
95 /* The input device is a touchscreen or a touchpad (either single-touch or multi-touch). */
Chris Ye1b0c7342020-07-28 21:57:03 -070096 TOUCH = 0x00000004,
Michael Wrightd02c5b62014-02-10 15:10:22 -080097
98 /* The input device is a cursor device such as a trackball or mouse. */
Chris Ye1b0c7342020-07-28 21:57:03 -070099 CURSOR = 0x00000008,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800100
101 /* The input device is a multi-touch touchscreen. */
Chris Ye1b0c7342020-07-28 21:57:03 -0700102 TOUCH_MT = 0x00000010,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800103
104 /* The input device is a directional pad (implies keyboard, has DPAD keys). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700105 DPAD = 0x00000020,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800106
107 /* The input device is a gamepad (implies keyboard, has BUTTON keys). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700108 GAMEPAD = 0x00000040,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800109
110 /* The input device has switches. */
Chris Ye1b0c7342020-07-28 21:57:03 -0700111 SWITCH = 0x00000080,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800112
113 /* The input device is a joystick (implies gamepad, has joystick absolute axes). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700114 JOYSTICK = 0x00000100,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800115
116 /* The input device has a vibrator (supports FF_RUMBLE). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700117 VIBRATOR = 0x00000200,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800118
Tim Kilbourn063ff532015-04-08 10:26:18 -0700119 /* The input device has a microphone. */
Chris Ye1b0c7342020-07-28 21:57:03 -0700120 MIC = 0x00000400,
Tim Kilbourn063ff532015-04-08 10:26:18 -0700121
Michael Wright842500e2015-03-13 17:32:02 -0700122 /* The input device is an external stylus (has data we want to fuse with touch data). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700123 EXTERNAL_STYLUS = 0x00000800,
Michael Wright842500e2015-03-13 17:32:02 -0700124
Prashant Malani1941ff52015-08-11 18:29:28 -0700125 /* The input device has a rotary encoder */
Chris Ye1b0c7342020-07-28 21:57:03 -0700126 ROTARY_ENCODER = 0x00001000,
Prashant Malani1941ff52015-08-11 18:29:28 -0700127
Chris Yef59a2f42020-10-16 12:55:26 -0700128 /* The input device has a sensor like accelerometer, gyro, etc */
129 SENSOR = 0x00002000,
130
Kim Low03ea0352020-11-06 12:45:07 -0800131 /* The input device has a battery */
132 BATTERY = 0x00004000,
133
Michael Wrightd02c5b62014-02-10 15:10:22 -0800134 /* The input device is virtual (not a real device, not part of UI configuration). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700135 VIRTUAL = 0x40000000,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800136
137 /* The input device is external (not built-in). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700138 EXTERNAL = 0x80000000,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800139};
140
141/*
142 * Gets the class that owns an axis, in cases where multiple classes might claim
143 * the same axis for different purposes.
144 */
Chris Ye1b0c7342020-07-28 21:57:03 -0700145extern Flags<InputDeviceClass> getAbsAxisUsage(int32_t axis, Flags<InputDeviceClass> deviceClasses);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800146
147/*
148 * Grand Central Station for events.
149 *
150 * The event hub aggregates input events received across all known input
151 * devices on the system, including devices that may be emulated by the simulator
152 * environment. In addition, the event hub generates fake input events to indicate
153 * when devices are added or removed.
154 *
155 * The event hub provides a stream of input events (via the getEvent function).
156 * It also supports querying the current actual state of input devices such as identifying
157 * which keys are currently down. Finally, the event hub keeps track of the capabilities of
158 * individual input devices, such as their class and the set of key codes that they support.
159 */
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700160class EventHubInterface {
161public:
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700162 EventHubInterface() {}
163 virtual ~EventHubInterface() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800164
Michael Wrightd02c5b62014-02-10 15:10:22 -0800165 // Synthetic raw event type codes produced when devices are added or removed.
166 enum {
167 // Sent when a device is added.
168 DEVICE_ADDED = 0x10000000,
169 // Sent when a device is removed.
170 DEVICE_REMOVED = 0x20000000,
171 // Sent when all added/removed devices from the most recent scan have been reported.
172 // This event is always sent at least once.
173 FINISHED_DEVICE_SCAN = 0x30000000,
174
175 FIRST_SYNTHETIC_EVENT = DEVICE_ADDED,
176 };
177
Chris Ye1b0c7342020-07-28 21:57:03 -0700178 virtual Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800179
180 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const = 0;
181
182 virtual int32_t getDeviceControllerNumber(int32_t deviceId) const = 0;
183
184 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const = 0;
185
186 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700187 RawAbsoluteAxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188
189 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const = 0;
190
191 virtual bool hasInputProperty(int32_t deviceId, int property) const = 0;
192
Chris Yef59a2f42020-10-16 12:55:26 -0700193 virtual bool hasMscEvent(int32_t deviceId, int mscEvent) const = 0;
194
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700195 virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
196 int32_t metaState, int32_t* outKeycode, int32_t* outMetaState,
197 uint32_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800198
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700199 virtual status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800200
201 // Sets devices that are excluded from opening.
202 // This can be used to ignore input devices for sensors.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100203 virtual void setExcludedDevices(const std::vector<std::string>& devices) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800204
205 /*
206 * Wait for events to become available and returns them.
207 * After returning, the EventHub holds onto a wake lock until the next call to getEvent.
208 * This ensures that the device will not go to sleep while the event is being processed.
209 * If the device needs to remain awake longer than that, then the caller is responsible
210 * for taking care of it (say, by poking the power manager user activity timer).
211 *
212 * The timeout is advisory only. If the device is asleep, it will not wake just to
213 * service the timeout.
214 *
215 * Returns the number of events obtained, or 0 if the timeout expired.
216 */
217 virtual size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) = 0;
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800218 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) = 0;
Chris Yef59a2f42020-10-16 12:55:26 -0700219 virtual base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
220 int32_t absCode) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800221
222 /*
223 * Query current input state.
224 */
225 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0;
226 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0;
227 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0;
228 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700229 int32_t* outValue) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800230
231 /*
232 * Examine key input devices for specific framework keycode support
233 */
234 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700235 uint8_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800236
237 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const = 0;
238
239 /* LED related functions expect Android LED constants, not scan codes or HID usages */
240 virtual bool hasLed(int32_t deviceId, int32_t led) const = 0;
241 virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0;
242
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700243 virtual void getVirtualKeyDefinitions(
244 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800245
Chris Ye3a1e4462020-08-12 10:13:15 -0700246 virtual const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const = 0;
247 virtual bool setKeyboardLayoutOverlay(int32_t deviceId,
248 std::shared_ptr<KeyCharacterMap> map) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800249
250 /* Control the vibrator. */
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000251 virtual void vibrate(int32_t deviceId, const VibrationElement& effect) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800252 virtual void cancelVibrate(int32_t deviceId) = 0;
Chris Ye87143712020-11-10 05:05:58 +0000253 virtual std::vector<int32_t> getVibratorIds(int32_t deviceId) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800254
Kim Low03ea0352020-11-06 12:45:07 -0800255 /* Query battery level. */
256 virtual std::optional<int32_t> getBatteryCapacity(int32_t deviceId) const = 0;
257
258 /* Query battery status. */
259 virtual std::optional<int32_t> getBatteryStatus(int32_t deviceId) const = 0;
260
Michael Wrightd02c5b62014-02-10 15:10:22 -0800261 /* Requests the EventHub to reopen all input devices on the next call to getEvents(). */
262 virtual void requestReopenDevices() = 0;
263
264 /* Wakes up getEvents() if it is blocked on a read. */
265 virtual void wake() = 0;
266
267 /* Dump EventHub state to a string. */
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800268 virtual void dump(std::string& dump) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800269
270 /* Called by the heatbeat to ensures that the reader has not deadlocked. */
271 virtual void monitor() = 0;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700272
273 /* Return true if the device is enabled. */
274 virtual bool isDeviceEnabled(int32_t deviceId) = 0;
275
276 /* Enable an input device */
277 virtual status_t enableDevice(int32_t deviceId) = 0;
278
279 /* Disable an input device. Closes file descriptor to that device. */
280 virtual status_t disableDevice(int32_t deviceId) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800281};
282
Chris Ye66fbac32020-07-06 20:36:43 -0700283template <std::size_t BITS>
284class BitArray {
285 /* Array element type and vector of element type. */
286 using Element = std::uint32_t;
287 /* Number of bits in each BitArray element. */
288 static constexpr size_t WIDTH = sizeof(Element) * CHAR_BIT;
289 /* Number of elements to represent a bit array of the specified size of bits. */
290 static constexpr size_t COUNT = (BITS + WIDTH - 1) / WIDTH;
291
292public:
293 /* BUFFER type declaration for BitArray */
294 using Buffer = std::array<Element, COUNT>;
295 /* To tell if a bit is set in array, it selects an element from the array, and test
296 * if the relevant bit set.
297 * Note the parameter "bit" is an index to the bit, 0 <= bit < BITS.
298 */
299 inline bool test(size_t bit) const {
300 return (bit < BITS) ? mData[bit / WIDTH].test(bit % WIDTH) : false;
301 }
302 /* Returns total number of bytes needed for the array */
303 inline size_t bytes() { return (BITS + CHAR_BIT - 1) / CHAR_BIT; }
304 /* Returns true if array contains any non-zero bit from the range defined by start and end
305 * bit index [startIndex, endIndex).
306 */
307 bool any(size_t startIndex, size_t endIndex) {
308 if (startIndex >= endIndex || startIndex > BITS || endIndex > BITS + 1) {
309 ALOGE("Invalid start/end index. start = %zu, end = %zu, total bits = %zu", startIndex,
310 endIndex, BITS);
311 return false;
312 }
313 size_t se = startIndex / WIDTH; // Start of element
314 size_t ee = endIndex / WIDTH; // End of element
315 size_t si = startIndex % WIDTH; // Start index in start element
316 size_t ei = endIndex % WIDTH; // End index in end element
317 // Need to check first unaligned bitset for any non zero bit
318 if (si > 0) {
319 size_t nBits = se == ee ? ei - si : WIDTH - si;
320 // Generate the mask of interested bit range
321 Element mask = ((1 << nBits) - 1) << si;
322 if (mData[se++].to_ulong() & mask) {
323 return true;
324 }
325 }
326 // Check whole bitset for any bit set
327 for (; se < ee; se++) {
328 if (mData[se].any()) {
329 return true;
330 }
331 }
332 // Need to check last unaligned bitset for any non zero bit
333 if (ei > 0 && se <= ee) {
334 // Generate the mask of interested bit range
335 Element mask = (1 << ei) - 1;
336 if (mData[se].to_ulong() & mask) {
337 return true;
338 }
339 }
340 return false;
341 }
342 /* Load bit array values from buffer */
343 void loadFromBuffer(const Buffer& buffer) {
344 for (size_t i = 0; i < COUNT; i++) {
345 mData[i] = std::bitset<WIDTH>(buffer[i]);
346 }
347 }
348
349private:
350 std::array<std::bitset<WIDTH>, COUNT> mData;
351};
352
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700353class EventHub : public EventHubInterface {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800354public:
355 EventHub();
356
Chris Ye989bb932020-07-04 16:18:59 -0700357 Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800358
Chris Ye989bb932020-07-04 16:18:59 -0700359 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800360
Chris Ye989bb932020-07-04 16:18:59 -0700361 int32_t getDeviceControllerNumber(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800362
Chris Ye989bb932020-07-04 16:18:59 -0700363 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800364
Chris Ye989bb932020-07-04 16:18:59 -0700365 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
366 RawAbsoluteAxisInfo* outAxisInfo) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800367
Chris Ye989bb932020-07-04 16:18:59 -0700368 bool hasRelativeAxis(int32_t deviceId, int axis) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800369
Chris Ye989bb932020-07-04 16:18:59 -0700370 bool hasInputProperty(int32_t deviceId, int property) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800371
Chris Yef59a2f42020-10-16 12:55:26 -0700372 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final;
373
Chris Ye989bb932020-07-04 16:18:59 -0700374 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
375 int32_t* outKeycode, int32_t* outMetaState,
376 uint32_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800377
Chris Ye989bb932020-07-04 16:18:59 -0700378 status_t mapAxis(int32_t deviceId, int32_t scanCode,
379 AxisInfo* outAxisInfo) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800380
Chris Yef59a2f42020-10-16 12:55:26 -0700381 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
382 int32_t deviceId, int32_t absCode) override final;
383
Chris Ye989bb932020-07-04 16:18:59 -0700384 void setExcludedDevices(const std::vector<std::string>& devices) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800385
Chris Ye989bb932020-07-04 16:18:59 -0700386 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override final;
387 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override final;
388 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override final;
389 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
390 int32_t* outValue) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800391
Chris Ye989bb932020-07-04 16:18:59 -0700392 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
393 uint8_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800394
Chris Ye989bb932020-07-04 16:18:59 -0700395 size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) override final;
396 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800397
Chris Ye989bb932020-07-04 16:18:59 -0700398 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override final;
399 bool hasLed(int32_t deviceId, int32_t led) const override final;
400 void setLedState(int32_t deviceId, int32_t led, bool on) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800401
Chris Ye989bb932020-07-04 16:18:59 -0700402 void getVirtualKeyDefinitions(
403 int32_t deviceId,
404 std::vector<VirtualKeyDefinition>& outVirtualKeys) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800405
Chris Ye3a1e4462020-08-12 10:13:15 -0700406 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(
407 int32_t deviceId) const override final;
408 bool setKeyboardLayoutOverlay(int32_t deviceId,
409 std::shared_ptr<KeyCharacterMap> map) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800410
Chris Ye989bb932020-07-04 16:18:59 -0700411 void vibrate(int32_t deviceId, const VibrationElement& effect) override final;
412 void cancelVibrate(int32_t deviceId) override final;
Chris Ye87143712020-11-10 05:05:58 +0000413 std::vector<int32_t> getVibratorIds(int32_t deviceId) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800414
Chris Ye989bb932020-07-04 16:18:59 -0700415 void requestReopenDevices() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800416
Chris Ye989bb932020-07-04 16:18:59 -0700417 void wake() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800418
Chris Ye989bb932020-07-04 16:18:59 -0700419 void dump(std::string& dump) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800420
Chris Ye989bb932020-07-04 16:18:59 -0700421 void monitor() override final;
422
Kim Low03ea0352020-11-06 12:45:07 -0800423 std::optional<int32_t> getBatteryCapacity(int32_t deviceId) const override final;
424
425 std::optional<int32_t> getBatteryStatus(int32_t deviceId) const override final;
426
Chris Ye989bb932020-07-04 16:18:59 -0700427 bool isDeviceEnabled(int32_t deviceId) override final;
428
429 status_t enableDevice(int32_t deviceId) override final;
430
431 status_t disableDevice(int32_t deviceId) override final;
432
433 ~EventHub() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800434
435private:
436 struct Device {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700437 int fd; // may be -1 if device is closed
Michael Wrightd02c5b62014-02-10 15:10:22 -0800438 const int32_t id;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100439 const std::string path;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800440 const InputDeviceIdentifier identifier;
441
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -0800442 std::unique_ptr<TouchVideoDevice> videoDevice;
443
Chris Ye1b0c7342020-07-28 21:57:03 -0700444 Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800445
Chris Ye66fbac32020-07-06 20:36:43 -0700446 BitArray<KEY_MAX> keyBitmask;
447 BitArray<KEY_MAX> keyState;
448 BitArray<ABS_MAX> absBitmask;
449 BitArray<REL_MAX> relBitmask;
450 BitArray<SW_MAX> swBitmask;
451 BitArray<SW_MAX> swState;
452 BitArray<LED_MAX> ledBitmask;
453 BitArray<FF_MAX> ffBitmask;
454 BitArray<INPUT_PROP_MAX> propBitmask;
Chris Yef59a2f42020-10-16 12:55:26 -0700455 BitArray<MSC_MAX> mscBitmask;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800456
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100457 std::string configurationFile;
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500458 std::unique_ptr<PropertyMap> configuration;
Siarhei Vishniakou3e78dec2019-02-20 16:21:46 -0600459 std::unique_ptr<VirtualKeyMap> virtualKeyMap;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800460 KeyMap keyMap;
461
Michael Wrightd02c5b62014-02-10 15:10:22 -0800462 bool ffEffectPlaying;
463 int16_t ffEffectId; // initially -1
464
Kim Low03ea0352020-11-06 12:45:07 -0800465 // The paths are invalid when .empty() returns true
466 std::filesystem::path sysfsRootPath;
467 std::filesystem::path sysfsBatteryPath;
468
Michael Wrightd02c5b62014-02-10 15:10:22 -0800469 int32_t controllerNumber;
470
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100471 Device(int fd, int32_t id, const std::string& path,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700472 const InputDeviceIdentifier& identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800473 ~Device();
474
475 void close();
476
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700477 bool enabled; // initially true
478 status_t enable();
479 status_t disable();
Chris Ye989bb932020-07-04 16:18:59 -0700480 bool hasValidFd() const;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700481 const bool isVirtual; // set if fd < 0 is passed to constructor
Michael Wrightd02c5b62014-02-10 15:10:22 -0800482
Chris Ye3a1e4462020-08-12 10:13:15 -0700483 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const;
Chris Ye66fbac32020-07-06 20:36:43 -0700484
485 template <std::size_t N>
Chris Ye989bb932020-07-04 16:18:59 -0700486 status_t readDeviceBitMask(unsigned long ioctlCode, BitArray<N>& bitArray);
Chris Ye66fbac32020-07-06 20:36:43 -0700487
Chris Ye989bb932020-07-04 16:18:59 -0700488 void configureFd();
489 bool hasKeycodeLocked(int keycode) const;
490 void loadConfigurationLocked();
491 bool loadVirtualKeyMapLocked();
492 status_t loadKeyMapLocked();
493 bool isExternalDeviceLocked();
494 bool deviceHasMicLocked();
495 void setLedForControllerLocked();
496 status_t mapLed(int32_t led, int32_t* outScanCode) const;
497 void setLedStateLocked(int32_t led, bool on);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800498 };
499
Chris Ye8594e192020-07-14 10:34:06 -0700500 status_t openDeviceLocked(const std::string& devicePath);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800501 void openVideoDeviceLocked(const std::string& devicePath);
Siarhei Vishniakouf49608d2020-08-20 19:18:21 -0500502 /**
503 * Try to associate a video device with an input device. If the association succeeds,
504 * the videoDevice is moved into the input device. 'videoDevice' will become null if this
505 * happens.
506 * Return true if the association succeeds.
507 * Return false otherwise.
508 */
509 bool tryAddVideoDevice(Device& device, std::unique_ptr<TouchVideoDevice>& videoDevice);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800510 void createVirtualKeyboardLocked();
Chris Ye989bb932020-07-04 16:18:59 -0700511 void addDeviceLocked(std::unique_ptr<Device> device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800512 void assignDescriptorLocked(InputDeviceIdentifier& identifier);
513
Chris Ye8594e192020-07-14 10:34:06 -0700514 void closeDeviceByPathLocked(const std::string& devicePath);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800515 void closeVideoDeviceByPathLocked(const std::string& devicePath);
Chris Ye989bb932020-07-04 16:18:59 -0700516 void closeDeviceLocked(Device& device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800517 void closeAllDevicesLocked();
518
Siarhei Vishniakou25920312018-12-12 15:24:44 -0800519 status_t registerFdForEpoll(int fd);
520 status_t unregisterFdFromEpoll(int fd);
Chris Ye989bb932020-07-04 16:18:59 -0700521 status_t registerDeviceForEpollLocked(Device& device);
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700522 void registerVideoDeviceForEpollLocked(const TouchVideoDevice& videoDevice);
Chris Ye989bb932020-07-04 16:18:59 -0700523 status_t unregisterDeviceFromEpollLocked(Device& device);
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700524 void unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& videoDevice);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700525
Chris Ye8594e192020-07-14 10:34:06 -0700526 status_t scanDirLocked(const std::string& dirname);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800527 status_t scanVideoDirLocked(const std::string& dirname);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800528 void scanDevicesLocked();
529 status_t readNotifyLocked();
530
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100531 Device* getDeviceByDescriptorLocked(const std::string& descriptor) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800532 Device* getDeviceLocked(int32_t deviceId) const;
Chris Ye8594e192020-07-14 10:34:06 -0700533 Device* getDeviceByPathLocked(const std::string& devicePath) const;
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700534 /**
535 * Look through all available fd's (both for input devices and for video devices),
536 * and return the device pointer.
537 */
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -0700538 Device* getDeviceByFdLocked(int fd) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800539
Chris Ye989bb932020-07-04 16:18:59 -0700540 int32_t getNextControllerNumberLocked(const std::string& name);
541 void releaseControllerNumberLocked(int32_t num);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800542
543 // Protect all internal state.
Chris Ye1c2e0892020-11-30 21:41:44 -0800544 mutable std::mutex mLock;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800545
546 // The actual id of the built-in keyboard, or NO_BUILT_IN_KEYBOARD if none.
547 // EventHub remaps the built-in keyboard to id 0 externally as required by the API.
548 enum {
549 // Must not conflict with any other assigned device ids, including
550 // the virtual keyboard id (-1).
551 NO_BUILT_IN_KEYBOARD = -2,
552 };
553 int32_t mBuiltInKeyboardId;
554
555 int32_t mNextDeviceId;
556
557 BitSet32 mControllerNumbers;
558
Chris Ye989bb932020-07-04 16:18:59 -0700559 std::unordered_map<int32_t, std::unique_ptr<Device>> mDevices;
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800560 /**
561 * Video devices that report touchscreen heatmap, but have not (yet) been paired
562 * with a specific input device. Video device discovery is independent from input device
563 * discovery, so the two types of devices could be found in any order.
564 * Ideally, video devices in this queue do not have an open fd, or at least aren't
565 * actively streaming.
566 */
567 std::vector<std::unique_ptr<TouchVideoDevice>> mUnattachedVideoDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800568
Chris Ye989bb932020-07-04 16:18:59 -0700569 std::vector<std::unique_ptr<Device>> mOpeningDevices;
570 std::vector<std::unique_ptr<Device>> mClosingDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800571
572 bool mNeedToSendFinishedDeviceScan;
573 bool mNeedToReopenDevices;
574 bool mNeedToScanDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100575 std::vector<std::string> mExcludedDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800576
577 int mEpollFd;
578 int mINotifyFd;
579 int mWakeReadPipeFd;
580 int mWakeWritePipeFd;
581
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800582 int mInputWd;
583 int mVideoWd;
584
Michael Wrightd02c5b62014-02-10 15:10:22 -0800585 // Maximum number of signalled FDs to handle at a time.
586 static const int EPOLL_MAX_EVENTS = 16;
587
588 // The array of pending epoll events and the index of the next event to be handled.
589 struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS];
590 size_t mPendingEventCount;
591 size_t mPendingEventIndex;
592 bool mPendingINotify;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800593};
594
595}; // namespace android
596
597#endif // _RUNTIME_EVENT_HUB_H