blob: 2afaa85e8a14a846bd740c19a0aaf5ef2eaf606f [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
Chris Ye3fdbfef2021-01-06 18:45:18 -080050/* Number of colors : {red, green, blue} */
51static constexpr size_t COLOR_NUM = 3;
Michael Wrightd02c5b62014-02-10 15:10:22 -080052/*
53 * A raw event as retrieved from the EventHub.
54 */
55struct RawEvent {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000056 // Time when the event happened
Michael Wrightd02c5b62014-02-10 15:10:22 -080057 nsecs_t when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000058 // Time when the event was read by EventHub. Only populated for input events.
59 // For other events (device added/removed/etc), this value is undefined and should not be read.
60 nsecs_t readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -080061 int32_t deviceId;
62 int32_t type;
63 int32_t code;
64 int32_t value;
65};
66
67/* Describes an absolute axis. */
68struct RawAbsoluteAxisInfo {
69 bool valid; // true if the information is valid, false otherwise
70
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070071 int32_t minValue; // minimum value
72 int32_t maxValue; // maximum value
73 int32_t flat; // center flat position, eg. flat == 8 means center is between -8 and 8
74 int32_t fuzz; // error tolerance, eg. fuzz == 4 means value is +/- 4 due to noise
Michael Wrightd02c5b62014-02-10 15:10:22 -080075 int32_t resolution; // resolution in units per mm or radians per mm
76
77 inline void clear() {
78 valid = false;
79 minValue = 0;
80 maxValue = 0;
81 flat = 0;
82 fuzz = 0;
83 resolution = 0;
84 }
85};
86
87/*
88 * Input device classes.
89 */
Chris Ye1b0c7342020-07-28 21:57:03 -070090enum class InputDeviceClass : uint32_t {
Michael Wrightd02c5b62014-02-10 15:10:22 -080091 /* The input device is a keyboard or has buttons. */
Chris Ye1b0c7342020-07-28 21:57:03 -070092 KEYBOARD = 0x00000001,
Michael Wrightd02c5b62014-02-10 15:10:22 -080093
94 /* The input device is an alpha-numeric keyboard (not just a dial pad). */
Chris Ye1b0c7342020-07-28 21:57:03 -070095 ALPHAKEY = 0x00000002,
Michael Wrightd02c5b62014-02-10 15:10:22 -080096
97 /* The input device is a touchscreen or a touchpad (either single-touch or multi-touch). */
Chris Ye1b0c7342020-07-28 21:57:03 -070098 TOUCH = 0x00000004,
Michael Wrightd02c5b62014-02-10 15:10:22 -080099
100 /* The input device is a cursor device such as a trackball or mouse. */
Chris Ye1b0c7342020-07-28 21:57:03 -0700101 CURSOR = 0x00000008,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800102
103 /* The input device is a multi-touch touchscreen. */
Chris Ye1b0c7342020-07-28 21:57:03 -0700104 TOUCH_MT = 0x00000010,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800105
106 /* The input device is a directional pad (implies keyboard, has DPAD keys). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700107 DPAD = 0x00000020,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800108
109 /* The input device is a gamepad (implies keyboard, has BUTTON keys). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700110 GAMEPAD = 0x00000040,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800111
112 /* The input device has switches. */
Chris Ye1b0c7342020-07-28 21:57:03 -0700113 SWITCH = 0x00000080,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800114
115 /* The input device is a joystick (implies gamepad, has joystick absolute axes). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700116 JOYSTICK = 0x00000100,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800117
118 /* The input device has a vibrator (supports FF_RUMBLE). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700119 VIBRATOR = 0x00000200,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800120
Tim Kilbourn063ff532015-04-08 10:26:18 -0700121 /* The input device has a microphone. */
Chris Ye1b0c7342020-07-28 21:57:03 -0700122 MIC = 0x00000400,
Tim Kilbourn063ff532015-04-08 10:26:18 -0700123
Michael Wright842500e2015-03-13 17:32:02 -0700124 /* The input device is an external stylus (has data we want to fuse with touch data). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700125 EXTERNAL_STYLUS = 0x00000800,
Michael Wright842500e2015-03-13 17:32:02 -0700126
Prashant Malani1941ff52015-08-11 18:29:28 -0700127 /* The input device has a rotary encoder */
Chris Ye1b0c7342020-07-28 21:57:03 -0700128 ROTARY_ENCODER = 0x00001000,
Prashant Malani1941ff52015-08-11 18:29:28 -0700129
Chris Yef59a2f42020-10-16 12:55:26 -0700130 /* The input device has a sensor like accelerometer, gyro, etc */
131 SENSOR = 0x00002000,
132
Kim Low03ea0352020-11-06 12:45:07 -0800133 /* The input device has a battery */
134 BATTERY = 0x00004000,
135
Chris Ye3fdbfef2021-01-06 18:45:18 -0800136 /* The input device has sysfs controllable lights */
137 LIGHT = 0x00008000,
138
Michael Wrightd02c5b62014-02-10 15:10:22 -0800139 /* The input device is virtual (not a real device, not part of UI configuration). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700140 VIRTUAL = 0x40000000,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800141
142 /* The input device is external (not built-in). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700143 EXTERNAL = 0x80000000,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800144};
145
Chris Ye3fdbfef2021-01-06 18:45:18 -0800146enum class SysfsClass : uint32_t {
147 POWER_SUPPLY = 0,
148 LEDS = 1,
149};
150
151enum class LightColor : uint32_t {
152 RED = 0,
153 GREEN = 1,
154 BLUE = 2,
155};
156
157enum class InputLightClass : uint32_t {
158 /* The input light has brightness node. */
159 BRIGHTNESS = 0x00000001,
160 /* The input light has red name. */
161 RED = 0x00000002,
162 /* The input light has green name. */
163 GREEN = 0x00000004,
164 /* The input light has blue name. */
165 BLUE = 0x00000008,
166 /* The input light has global name. */
167 GLOBAL = 0x00000010,
168 /* The input light has multi index node. */
169 MULTI_INDEX = 0x00000020,
170 /* The input light has multi intensity node. */
171 MULTI_INTENSITY = 0x00000040,
172 /* The input light has max brightness node. */
173 MAX_BRIGHTNESS = 0x00000080,
174};
175
176/* Describes a raw light. */
177struct RawLightInfo {
178 int32_t id;
179 std::string name;
180 std::optional<int32_t> maxBrightness;
181 Flags<InputLightClass> flags;
182 std::array<int32_t, COLOR_NUM> rgbIndex;
183 std::filesystem::path path;
184};
185
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186/*
187 * Gets the class that owns an axis, in cases where multiple classes might claim
188 * the same axis for different purposes.
189 */
Chris Ye1b0c7342020-07-28 21:57:03 -0700190extern Flags<InputDeviceClass> getAbsAxisUsage(int32_t axis, Flags<InputDeviceClass> deviceClasses);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800191
192/*
193 * Grand Central Station for events.
194 *
195 * The event hub aggregates input events received across all known input
196 * devices on the system, including devices that may be emulated by the simulator
197 * environment. In addition, the event hub generates fake input events to indicate
198 * when devices are added or removed.
199 *
200 * The event hub provides a stream of input events (via the getEvent function).
201 * It also supports querying the current actual state of input devices such as identifying
202 * which keys are currently down. Finally, the event hub keeps track of the capabilities of
203 * individual input devices, such as their class and the set of key codes that they support.
204 */
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700205class EventHubInterface {
206public:
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700207 EventHubInterface() {}
208 virtual ~EventHubInterface() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800209
Michael Wrightd02c5b62014-02-10 15:10:22 -0800210 // Synthetic raw event type codes produced when devices are added or removed.
211 enum {
212 // Sent when a device is added.
213 DEVICE_ADDED = 0x10000000,
214 // Sent when a device is removed.
215 DEVICE_REMOVED = 0x20000000,
216 // Sent when all added/removed devices from the most recent scan have been reported.
217 // This event is always sent at least once.
218 FINISHED_DEVICE_SCAN = 0x30000000,
219
220 FIRST_SYNTHETIC_EVENT = DEVICE_ADDED,
221 };
222
Chris Ye1b0c7342020-07-28 21:57:03 -0700223 virtual Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800224
225 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const = 0;
226
227 virtual int32_t getDeviceControllerNumber(int32_t deviceId) const = 0;
228
229 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const = 0;
230
231 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700232 RawAbsoluteAxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800233
234 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const = 0;
235
236 virtual bool hasInputProperty(int32_t deviceId, int property) const = 0;
237
Chris Yef59a2f42020-10-16 12:55:26 -0700238 virtual bool hasMscEvent(int32_t deviceId, int mscEvent) const = 0;
239
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700240 virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
241 int32_t metaState, int32_t* outKeycode, int32_t* outMetaState,
242 uint32_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800243
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700244 virtual status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800245
246 // Sets devices that are excluded from opening.
247 // This can be used to ignore input devices for sensors.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100248 virtual void setExcludedDevices(const std::vector<std::string>& devices) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800249
250 /*
251 * Wait for events to become available and returns them.
252 * After returning, the EventHub holds onto a wake lock until the next call to getEvent.
253 * This ensures that the device will not go to sleep while the event is being processed.
254 * If the device needs to remain awake longer than that, then the caller is responsible
255 * for taking care of it (say, by poking the power manager user activity timer).
256 *
257 * The timeout is advisory only. If the device is asleep, it will not wake just to
258 * service the timeout.
259 *
260 * Returns the number of events obtained, or 0 if the timeout expired.
261 */
262 virtual size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) = 0;
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800263 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) = 0;
Chris Yef59a2f42020-10-16 12:55:26 -0700264 virtual base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
265 int32_t absCode) = 0;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800266 // Raw lights are sysfs led light nodes we found from the EventHub device sysfs node,
267 // containing the raw info of the sysfs node structure.
268 virtual const std::vector<int32_t> getRawLightIds(int32_t deviceId) = 0;
269 virtual std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) = 0;
270 virtual std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) = 0;
271 virtual void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) = 0;
272 virtual std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
273 int32_t deviceId, int32_t lightId) = 0;
274 virtual void setLightIntensities(int32_t deviceId, int32_t lightId,
275 std::unordered_map<LightColor, int32_t> intensities) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800276 /*
277 * Query current input state.
278 */
279 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0;
280 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0;
281 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0;
282 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700283 int32_t* outValue) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800284
285 /*
286 * Examine key input devices for specific framework keycode support
287 */
288 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700289 uint8_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800290
291 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const = 0;
292
293 /* LED related functions expect Android LED constants, not scan codes or HID usages */
294 virtual bool hasLed(int32_t deviceId, int32_t led) const = 0;
295 virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0;
296
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700297 virtual void getVirtualKeyDefinitions(
298 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800299
Chris Ye3a1e4462020-08-12 10:13:15 -0700300 virtual const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const = 0;
301 virtual bool setKeyboardLayoutOverlay(int32_t deviceId,
302 std::shared_ptr<KeyCharacterMap> map) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800303
304 /* Control the vibrator. */
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000305 virtual void vibrate(int32_t deviceId, const VibrationElement& effect) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800306 virtual void cancelVibrate(int32_t deviceId) = 0;
Chris Ye87143712020-11-10 05:05:58 +0000307 virtual std::vector<int32_t> getVibratorIds(int32_t deviceId) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800308
Kim Low03ea0352020-11-06 12:45:07 -0800309 /* Query battery level. */
310 virtual std::optional<int32_t> getBatteryCapacity(int32_t deviceId) const = 0;
311
312 /* Query battery status. */
313 virtual std::optional<int32_t> getBatteryStatus(int32_t deviceId) const = 0;
314
Michael Wrightd02c5b62014-02-10 15:10:22 -0800315 /* Requests the EventHub to reopen all input devices on the next call to getEvents(). */
316 virtual void requestReopenDevices() = 0;
317
318 /* Wakes up getEvents() if it is blocked on a read. */
319 virtual void wake() = 0;
320
321 /* Dump EventHub state to a string. */
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800322 virtual void dump(std::string& dump) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800323
324 /* Called by the heatbeat to ensures that the reader has not deadlocked. */
325 virtual void monitor() = 0;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700326
327 /* Return true if the device is enabled. */
328 virtual bool isDeviceEnabled(int32_t deviceId) = 0;
329
330 /* Enable an input device */
331 virtual status_t enableDevice(int32_t deviceId) = 0;
332
333 /* Disable an input device. Closes file descriptor to that device. */
334 virtual status_t disableDevice(int32_t deviceId) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800335};
336
Chris Ye66fbac32020-07-06 20:36:43 -0700337template <std::size_t BITS>
338class BitArray {
339 /* Array element type and vector of element type. */
340 using Element = std::uint32_t;
341 /* Number of bits in each BitArray element. */
342 static constexpr size_t WIDTH = sizeof(Element) * CHAR_BIT;
343 /* Number of elements to represent a bit array of the specified size of bits. */
344 static constexpr size_t COUNT = (BITS + WIDTH - 1) / WIDTH;
345
346public:
347 /* BUFFER type declaration for BitArray */
348 using Buffer = std::array<Element, COUNT>;
349 /* To tell if a bit is set in array, it selects an element from the array, and test
350 * if the relevant bit set.
351 * Note the parameter "bit" is an index to the bit, 0 <= bit < BITS.
352 */
353 inline bool test(size_t bit) const {
354 return (bit < BITS) ? mData[bit / WIDTH].test(bit % WIDTH) : false;
355 }
356 /* Returns total number of bytes needed for the array */
357 inline size_t bytes() { return (BITS + CHAR_BIT - 1) / CHAR_BIT; }
358 /* Returns true if array contains any non-zero bit from the range defined by start and end
359 * bit index [startIndex, endIndex).
360 */
361 bool any(size_t startIndex, size_t endIndex) {
362 if (startIndex >= endIndex || startIndex > BITS || endIndex > BITS + 1) {
363 ALOGE("Invalid start/end index. start = %zu, end = %zu, total bits = %zu", startIndex,
364 endIndex, BITS);
365 return false;
366 }
367 size_t se = startIndex / WIDTH; // Start of element
368 size_t ee = endIndex / WIDTH; // End of element
369 size_t si = startIndex % WIDTH; // Start index in start element
370 size_t ei = endIndex % WIDTH; // End index in end element
371 // Need to check first unaligned bitset for any non zero bit
372 if (si > 0) {
373 size_t nBits = se == ee ? ei - si : WIDTH - si;
374 // Generate the mask of interested bit range
375 Element mask = ((1 << nBits) - 1) << si;
376 if (mData[se++].to_ulong() & mask) {
377 return true;
378 }
379 }
380 // Check whole bitset for any bit set
381 for (; se < ee; se++) {
382 if (mData[se].any()) {
383 return true;
384 }
385 }
386 // Need to check last unaligned bitset for any non zero bit
387 if (ei > 0 && se <= ee) {
388 // Generate the mask of interested bit range
389 Element mask = (1 << ei) - 1;
390 if (mData[se].to_ulong() & mask) {
391 return true;
392 }
393 }
394 return false;
395 }
396 /* Load bit array values from buffer */
397 void loadFromBuffer(const Buffer& buffer) {
398 for (size_t i = 0; i < COUNT; i++) {
399 mData[i] = std::bitset<WIDTH>(buffer[i]);
400 }
401 }
402
403private:
404 std::array<std::bitset<WIDTH>, COUNT> mData;
405};
406
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700407class EventHub : public EventHubInterface {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800408public:
409 EventHub();
410
Chris Ye989bb932020-07-04 16:18:59 -0700411 Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800412
Chris Ye989bb932020-07-04 16:18:59 -0700413 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800414
Chris Ye989bb932020-07-04 16:18:59 -0700415 int32_t getDeviceControllerNumber(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800416
Chris Ye989bb932020-07-04 16:18:59 -0700417 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800418
Chris Ye989bb932020-07-04 16:18:59 -0700419 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
420 RawAbsoluteAxisInfo* outAxisInfo) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800421
Chris Ye989bb932020-07-04 16:18:59 -0700422 bool hasRelativeAxis(int32_t deviceId, int axis) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800423
Chris Ye989bb932020-07-04 16:18:59 -0700424 bool hasInputProperty(int32_t deviceId, int property) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800425
Chris Yef59a2f42020-10-16 12:55:26 -0700426 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final;
427
Chris Ye989bb932020-07-04 16:18:59 -0700428 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
429 int32_t* outKeycode, int32_t* outMetaState,
430 uint32_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800431
Chris Ye989bb932020-07-04 16:18:59 -0700432 status_t mapAxis(int32_t deviceId, int32_t scanCode,
433 AxisInfo* outAxisInfo) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800434
Chris Yef59a2f42020-10-16 12:55:26 -0700435 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
436 int32_t deviceId, int32_t absCode) override final;
437
Chris Ye3fdbfef2021-01-06 18:45:18 -0800438 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override final;
439
440 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override final;
441
442 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override final;
443 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override final;
444 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
445 int32_t deviceId, int32_t lightId) override final;
446 void setLightIntensities(int32_t deviceId, int32_t lightId,
447 std::unordered_map<LightColor, int32_t> intensities) override final;
448
Chris Ye989bb932020-07-04 16:18:59 -0700449 void setExcludedDevices(const std::vector<std::string>& devices) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800450
Chris Ye989bb932020-07-04 16:18:59 -0700451 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override final;
452 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override final;
453 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override final;
454 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
455 int32_t* outValue) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800456
Chris Ye989bb932020-07-04 16:18:59 -0700457 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
458 uint8_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800459
Chris Ye989bb932020-07-04 16:18:59 -0700460 size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) override final;
461 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800462
Chris Ye989bb932020-07-04 16:18:59 -0700463 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override final;
464 bool hasLed(int32_t deviceId, int32_t led) const override final;
465 void setLedState(int32_t deviceId, int32_t led, bool on) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800466
Chris Ye989bb932020-07-04 16:18:59 -0700467 void getVirtualKeyDefinitions(
468 int32_t deviceId,
469 std::vector<VirtualKeyDefinition>& outVirtualKeys) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800470
Chris Ye3a1e4462020-08-12 10:13:15 -0700471 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(
472 int32_t deviceId) const override final;
473 bool setKeyboardLayoutOverlay(int32_t deviceId,
474 std::shared_ptr<KeyCharacterMap> map) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800475
Chris Ye989bb932020-07-04 16:18:59 -0700476 void vibrate(int32_t deviceId, const VibrationElement& effect) override final;
477 void cancelVibrate(int32_t deviceId) override final;
Chris Ye87143712020-11-10 05:05:58 +0000478 std::vector<int32_t> getVibratorIds(int32_t deviceId) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800479
Chris Ye989bb932020-07-04 16:18:59 -0700480 void requestReopenDevices() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800481
Chris Ye989bb932020-07-04 16:18:59 -0700482 void wake() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800483
Chris Ye989bb932020-07-04 16:18:59 -0700484 void dump(std::string& dump) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800485
Chris Ye989bb932020-07-04 16:18:59 -0700486 void monitor() override final;
487
Kim Low03ea0352020-11-06 12:45:07 -0800488 std::optional<int32_t> getBatteryCapacity(int32_t deviceId) const override final;
489
490 std::optional<int32_t> getBatteryStatus(int32_t deviceId) const override final;
491
Chris Ye989bb932020-07-04 16:18:59 -0700492 bool isDeviceEnabled(int32_t deviceId) override final;
493
494 status_t enableDevice(int32_t deviceId) override final;
495
496 status_t disableDevice(int32_t deviceId) override final;
497
498 ~EventHub() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800499
500private:
501 struct Device {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700502 int fd; // may be -1 if device is closed
Michael Wrightd02c5b62014-02-10 15:10:22 -0800503 const int32_t id;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100504 const std::string path;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800505 const InputDeviceIdentifier identifier;
506
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -0800507 std::unique_ptr<TouchVideoDevice> videoDevice;
508
Chris Ye1b0c7342020-07-28 21:57:03 -0700509 Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800510
Chris Ye66fbac32020-07-06 20:36:43 -0700511 BitArray<KEY_MAX> keyBitmask;
512 BitArray<KEY_MAX> keyState;
513 BitArray<ABS_MAX> absBitmask;
514 BitArray<REL_MAX> relBitmask;
515 BitArray<SW_MAX> swBitmask;
516 BitArray<SW_MAX> swState;
517 BitArray<LED_MAX> ledBitmask;
518 BitArray<FF_MAX> ffBitmask;
519 BitArray<INPUT_PROP_MAX> propBitmask;
Chris Yef59a2f42020-10-16 12:55:26 -0700520 BitArray<MSC_MAX> mscBitmask;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800521
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100522 std::string configurationFile;
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500523 std::unique_ptr<PropertyMap> configuration;
Siarhei Vishniakou3e78dec2019-02-20 16:21:46 -0600524 std::unique_ptr<VirtualKeyMap> virtualKeyMap;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800525 KeyMap keyMap;
526
Michael Wrightd02c5b62014-02-10 15:10:22 -0800527 bool ffEffectPlaying;
528 int16_t ffEffectId; // initially -1
529
Chris Ye3fdbfef2021-01-06 18:45:18 -0800530 // The paths are invalid when they're std::nullopt
531 std::optional<std::filesystem::path> sysfsRootPath;
532 std::optional<std::filesystem::path> sysfsBatteryPath;
533 // maps from light id to light info
534 std::unordered_map<int32_t, RawLightInfo> lightInfos;
535 int32_t nextLightId;
Kim Low03ea0352020-11-06 12:45:07 -0800536
Michael Wrightd02c5b62014-02-10 15:10:22 -0800537 int32_t controllerNumber;
538
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100539 Device(int fd, int32_t id, const std::string& path,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700540 const InputDeviceIdentifier& identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800541 ~Device();
542
543 void close();
544
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700545 bool enabled; // initially true
546 status_t enable();
547 status_t disable();
Chris Ye989bb932020-07-04 16:18:59 -0700548 bool hasValidFd() const;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700549 const bool isVirtual; // set if fd < 0 is passed to constructor
Michael Wrightd02c5b62014-02-10 15:10:22 -0800550
Chris Ye3a1e4462020-08-12 10:13:15 -0700551 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const;
Chris Ye66fbac32020-07-06 20:36:43 -0700552
553 template <std::size_t N>
Chris Ye989bb932020-07-04 16:18:59 -0700554 status_t readDeviceBitMask(unsigned long ioctlCode, BitArray<N>& bitArray);
Chris Ye66fbac32020-07-06 20:36:43 -0700555
Chris Ye989bb932020-07-04 16:18:59 -0700556 void configureFd();
557 bool hasKeycodeLocked(int keycode) const;
558 void loadConfigurationLocked();
559 bool loadVirtualKeyMapLocked();
560 status_t loadKeyMapLocked();
561 bool isExternalDeviceLocked();
562 bool deviceHasMicLocked();
563 void setLedForControllerLocked();
564 status_t mapLed(int32_t led, int32_t* outScanCode) const;
565 void setLedStateLocked(int32_t led, bool on);
Chris Ye3fdbfef2021-01-06 18:45:18 -0800566 bool configureBatteryLocked();
567 bool configureLightsLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800568 };
569
Siarhei Vishniakoua4c502a2021-02-05 00:45:20 +0000570 /**
571 * Create a new device for the provided path.
572 */
Chris Yed3fef462021-03-07 17:10:08 -0800573 void openDeviceLocked(const std::string& devicePath) REQUIRES(mLock);
574 void openVideoDeviceLocked(const std::string& devicePath) REQUIRES(mLock);
Siarhei Vishniakouf49608d2020-08-20 19:18:21 -0500575 /**
576 * Try to associate a video device with an input device. If the association succeeds,
577 * the videoDevice is moved into the input device. 'videoDevice' will become null if this
578 * happens.
579 * Return true if the association succeeds.
580 * Return false otherwise.
581 */
Chris Yed3fef462021-03-07 17:10:08 -0800582 bool tryAddVideoDeviceLocked(Device& device, std::unique_ptr<TouchVideoDevice>& videoDevice)
583 REQUIRES(mLock);
584 void createVirtualKeyboardLocked() REQUIRES(mLock);
585 void addDeviceLocked(std::unique_ptr<Device> device) REQUIRES(mLock);
586 void assignDescriptorLocked(InputDeviceIdentifier& identifier) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800587
Chris Yed3fef462021-03-07 17:10:08 -0800588 void closeDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock);
589 void closeVideoDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock);
590 void closeDeviceLocked(Device& device) REQUIRES(mLock);
591 void closeAllDevicesLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800592
Siarhei Vishniakou25920312018-12-12 15:24:44 -0800593 status_t registerFdForEpoll(int fd);
594 status_t unregisterFdFromEpoll(int fd);
Chris Yed3fef462021-03-07 17:10:08 -0800595 status_t registerDeviceForEpollLocked(Device& device) REQUIRES(mLock);
596 void registerVideoDeviceForEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock);
597 status_t unregisterDeviceFromEpollLocked(Device& device) REQUIRES(mLock);
598 void unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700599
Chris Yed3fef462021-03-07 17:10:08 -0800600 status_t scanDirLocked(const std::string& dirname) REQUIRES(mLock);
601 status_t scanVideoDirLocked(const std::string& dirname) REQUIRES(mLock);
602 void scanDevicesLocked() REQUIRES(mLock);
603 status_t readNotifyLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800604
Chris Yed3fef462021-03-07 17:10:08 -0800605 Device* getDeviceByDescriptorLocked(const std::string& descriptor) const REQUIRES(mLock);
606 Device* getDeviceLocked(int32_t deviceId) const REQUIRES(mLock);
607 Device* getDeviceByPathLocked(const std::string& devicePath) const REQUIRES(mLock);
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700608 /**
609 * Look through all available fd's (both for input devices and for video devices),
610 * and return the device pointer.
611 */
Chris Yed3fef462021-03-07 17:10:08 -0800612 Device* getDeviceByFdLocked(int fd) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800613
Chris Yed3fef462021-03-07 17:10:08 -0800614 int32_t getNextControllerNumberLocked(const std::string& name) REQUIRES(mLock);
615 void releaseControllerNumberLocked(int32_t num) REQUIRES(mLock);
616 void reportDeviceAddedForStatisticsLocked(const InputDeviceIdentifier& identifier,
617 Flags<InputDeviceClass> classes) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800618
619 // Protect all internal state.
Chris Ye1c2e0892020-11-30 21:41:44 -0800620 mutable std::mutex mLock;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800621
622 // The actual id of the built-in keyboard, or NO_BUILT_IN_KEYBOARD if none.
623 // EventHub remaps the built-in keyboard to id 0 externally as required by the API.
624 enum {
625 // Must not conflict with any other assigned device ids, including
626 // the virtual keyboard id (-1).
627 NO_BUILT_IN_KEYBOARD = -2,
628 };
629 int32_t mBuiltInKeyboardId;
630
631 int32_t mNextDeviceId;
632
633 BitSet32 mControllerNumbers;
634
Chris Ye989bb932020-07-04 16:18:59 -0700635 std::unordered_map<int32_t, std::unique_ptr<Device>> mDevices;
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800636 /**
637 * Video devices that report touchscreen heatmap, but have not (yet) been paired
638 * with a specific input device. Video device discovery is independent from input device
639 * discovery, so the two types of devices could be found in any order.
640 * Ideally, video devices in this queue do not have an open fd, or at least aren't
641 * actively streaming.
642 */
643 std::vector<std::unique_ptr<TouchVideoDevice>> mUnattachedVideoDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800644
Chris Ye989bb932020-07-04 16:18:59 -0700645 std::vector<std::unique_ptr<Device>> mOpeningDevices;
646 std::vector<std::unique_ptr<Device>> mClosingDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800647
648 bool mNeedToSendFinishedDeviceScan;
649 bool mNeedToReopenDevices;
650 bool mNeedToScanDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100651 std::vector<std::string> mExcludedDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800652
653 int mEpollFd;
654 int mINotifyFd;
655 int mWakeReadPipeFd;
656 int mWakeWritePipeFd;
657
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800658 int mInputWd;
659 int mVideoWd;
660
Michael Wrightd02c5b62014-02-10 15:10:22 -0800661 // Maximum number of signalled FDs to handle at a time.
662 static const int EPOLL_MAX_EVENTS = 16;
663
664 // The array of pending epoll events and the index of the next event to be handled.
665 struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS];
666 size_t mPendingEventCount;
667 size_t mPendingEventIndex;
668 bool mPendingINotify;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800669};
670
671}; // namespace android
672
673#endif // _RUNTIME_EVENT_HUB_H