blob: 4733ecb323db6883f085e3e2d4df183bb8ba838d [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>
Dominik Laskowski2f01d772022-03-23 16:01:29 -070022#include <filesystem>
Chris Ye989bb932020-07-04 16:18:59 -070023#include <unordered_map>
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010024#include <vector>
25
Kim Low03ea0352020-11-06 12:45:07 -080026#include <batteryservice/BatteryService.h>
Dominik Laskowski2f01d772022-03-23 16:01:29 -070027#include <ftl/flags.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080028#include <input/Input.h>
29#include <input/InputDevice.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080030#include <input/KeyCharacterMap.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070031#include <input/KeyLayoutMap.h>
32#include <input/Keyboard.h>
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070033#include <input/PropertyMap.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080034#include <input/VirtualKeyMap.h>
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000035#include <linux/input.h>
36#include <sys/epoll.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080037#include <utils/BitSet.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070038#include <utils/Errors.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070039#include <utils/List.h>
40#include <utils/Log.h>
41#include <utils/Mutex.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080042
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080043#include "TouchVideoDevice.h"
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000044#include "VibrationElement.h"
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080045
Prabir Pradhan952e65b2022-06-23 17:49:55 +000046struct inotify_event;
47
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,
Dominik Laskowski75788452021-02-09 18:51:25 -0800149
150 ftl_last = LEDS
Chris Ye3fdbfef2021-01-06 18:45:18 -0800151};
152
153enum class LightColor : uint32_t {
154 RED = 0,
155 GREEN = 1,
156 BLUE = 2,
157};
158
159enum class InputLightClass : uint32_t {
160 /* The input light has brightness node. */
161 BRIGHTNESS = 0x00000001,
162 /* The input light has red name. */
163 RED = 0x00000002,
164 /* The input light has green name. */
165 GREEN = 0x00000004,
166 /* The input light has blue name. */
167 BLUE = 0x00000008,
168 /* The input light has global name. */
169 GLOBAL = 0x00000010,
170 /* The input light has multi index node. */
171 MULTI_INDEX = 0x00000020,
172 /* The input light has multi intensity node. */
173 MULTI_INTENSITY = 0x00000040,
174 /* The input light has max brightness node. */
175 MAX_BRIGHTNESS = 0x00000080,
176};
177
Chris Yee2b1e5c2021-03-10 22:45:12 -0800178enum class InputBatteryClass : uint32_t {
179 /* The input device battery has capacity node. */
180 CAPACITY = 0x00000001,
181 /* The input device battery has capacity_level node. */
182 CAPACITY_LEVEL = 0x00000002,
183 /* The input device battery has status node. */
184 STATUS = 0x00000004,
185};
186
Chris Ye3fdbfef2021-01-06 18:45:18 -0800187/* Describes a raw light. */
188struct RawLightInfo {
189 int32_t id;
190 std::string name;
191 std::optional<int32_t> maxBrightness;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700192 ftl::Flags<InputLightClass> flags;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800193 std::array<int32_t, COLOR_NUM> rgbIndex;
194 std::filesystem::path path;
195};
196
Chris Yee2b1e5c2021-03-10 22:45:12 -0800197/* Describes a raw battery. */
198struct RawBatteryInfo {
199 int32_t id;
200 std::string name;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700201 ftl::Flags<InputBatteryClass> flags;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800202 std::filesystem::path path;
203};
204
Michael Wrightd02c5b62014-02-10 15:10:22 -0800205/*
206 * Gets the class that owns an axis, in cases where multiple classes might claim
207 * the same axis for different purposes.
208 */
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700209extern ftl::Flags<InputDeviceClass> getAbsAxisUsage(int32_t axis,
210 ftl::Flags<InputDeviceClass> deviceClasses);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800211
212/*
213 * Grand Central Station for events.
214 *
215 * The event hub aggregates input events received across all known input
216 * devices on the system, including devices that may be emulated by the simulator
217 * environment. In addition, the event hub generates fake input events to indicate
218 * when devices are added or removed.
219 *
220 * The event hub provides a stream of input events (via the getEvent function).
221 * It also supports querying the current actual state of input devices such as identifying
222 * which keys are currently down. Finally, the event hub keeps track of the capabilities of
223 * individual input devices, such as their class and the set of key codes that they support.
224 */
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700225class EventHubInterface {
226public:
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700227 EventHubInterface() {}
228 virtual ~EventHubInterface() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800229
Michael Wrightd02c5b62014-02-10 15:10:22 -0800230 // Synthetic raw event type codes produced when devices are added or removed.
231 enum {
232 // Sent when a device is added.
233 DEVICE_ADDED = 0x10000000,
234 // Sent when a device is removed.
235 DEVICE_REMOVED = 0x20000000,
236 // Sent when all added/removed devices from the most recent scan have been reported.
237 // This event is always sent at least once.
238 FINISHED_DEVICE_SCAN = 0x30000000,
239
240 FIRST_SYNTHETIC_EVENT = DEVICE_ADDED,
241 };
242
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700243 virtual ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800244
245 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const = 0;
246
247 virtual int32_t getDeviceControllerNumber(int32_t deviceId) const = 0;
248
249 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const = 0;
250
251 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700252 RawAbsoluteAxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800253
254 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const = 0;
255
256 virtual bool hasInputProperty(int32_t deviceId, int property) const = 0;
257
Chris Yef59a2f42020-10-16 12:55:26 -0700258 virtual bool hasMscEvent(int32_t deviceId, int mscEvent) const = 0;
259
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700260 virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
261 int32_t metaState, int32_t* outKeycode, int32_t* outMetaState,
262 uint32_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800263
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700264 virtual status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800265
266 // Sets devices that are excluded from opening.
267 // This can be used to ignore input devices for sensors.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100268 virtual void setExcludedDevices(const std::vector<std::string>& devices) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800269
270 /*
271 * Wait for events to become available and returns them.
272 * After returning, the EventHub holds onto a wake lock until the next call to getEvent.
273 * This ensures that the device will not go to sleep while the event is being processed.
274 * If the device needs to remain awake longer than that, then the caller is responsible
275 * for taking care of it (say, by poking the power manager user activity timer).
276 *
277 * The timeout is advisory only. If the device is asleep, it will not wake just to
278 * service the timeout.
279 *
280 * Returns the number of events obtained, or 0 if the timeout expired.
281 */
282 virtual size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) = 0;
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800283 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) = 0;
Chris Yef59a2f42020-10-16 12:55:26 -0700284 virtual base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
285 int32_t absCode) = 0;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800286 // Raw batteries are sysfs power_supply nodes we found from the EventHub device sysfs node,
287 // containing the raw info of the sysfs node structure.
288 virtual const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) = 0;
289 virtual std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
290 int32_t BatteryId) = 0;
291
Chris Ye3fdbfef2021-01-06 18:45:18 -0800292 // Raw lights are sysfs led light nodes we found from the EventHub device sysfs node,
293 // containing the raw info of the sysfs node structure.
294 virtual const std::vector<int32_t> getRawLightIds(int32_t deviceId) = 0;
295 virtual std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) = 0;
296 virtual std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) = 0;
297 virtual void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) = 0;
298 virtual std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
299 int32_t deviceId, int32_t lightId) = 0;
300 virtual void setLightIntensities(int32_t deviceId, int32_t lightId,
301 std::unordered_map<LightColor, int32_t> intensities) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800302 /*
303 * Query current input state.
304 */
305 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0;
306 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0;
307 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0;
308 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700309 int32_t* outValue) const = 0;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100310 virtual int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800311
312 /*
313 * Examine key input devices for specific framework keycode support
314 */
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700315 virtual bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700316 uint8_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800317
318 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const = 0;
Arthur Hungcb40a002021-08-03 14:31:01 +0000319 virtual bool hasKeyCode(int32_t deviceId, int32_t keyCode) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800320
321 /* LED related functions expect Android LED constants, not scan codes or HID usages */
322 virtual bool hasLed(int32_t deviceId, int32_t led) const = 0;
323 virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0;
324
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700325 virtual void getVirtualKeyDefinitions(
326 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800327
Chris Ye3a1e4462020-08-12 10:13:15 -0700328 virtual const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const = 0;
329 virtual bool setKeyboardLayoutOverlay(int32_t deviceId,
330 std::shared_ptr<KeyCharacterMap> map) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800331
332 /* Control the vibrator. */
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000333 virtual void vibrate(int32_t deviceId, const VibrationElement& effect) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800334 virtual void cancelVibrate(int32_t deviceId) = 0;
Chris Ye87143712020-11-10 05:05:58 +0000335 virtual std::vector<int32_t> getVibratorIds(int32_t deviceId) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800336
Kim Low03ea0352020-11-06 12:45:07 -0800337 /* Query battery level. */
Chris Yee2b1e5c2021-03-10 22:45:12 -0800338 virtual std::optional<int32_t> getBatteryCapacity(int32_t deviceId,
339 int32_t batteryId) const = 0;
Kim Low03ea0352020-11-06 12:45:07 -0800340
341 /* Query battery status. */
Chris Yee2b1e5c2021-03-10 22:45:12 -0800342 virtual std::optional<int32_t> getBatteryStatus(int32_t deviceId, int32_t batteryId) const = 0;
Kim Low03ea0352020-11-06 12:45:07 -0800343
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344 /* Requests the EventHub to reopen all input devices on the next call to getEvents(). */
345 virtual void requestReopenDevices() = 0;
346
347 /* Wakes up getEvents() if it is blocked on a read. */
348 virtual void wake() = 0;
349
350 /* Dump EventHub state to a string. */
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800351 virtual void dump(std::string& dump) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800352
353 /* Called by the heatbeat to ensures that the reader has not deadlocked. */
354 virtual void monitor() = 0;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700355
356 /* Return true if the device is enabled. */
357 virtual bool isDeviceEnabled(int32_t deviceId) = 0;
358
359 /* Enable an input device */
360 virtual status_t enableDevice(int32_t deviceId) = 0;
361
362 /* Disable an input device. Closes file descriptor to that device. */
363 virtual status_t disableDevice(int32_t deviceId) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800364};
365
Chris Ye66fbac32020-07-06 20:36:43 -0700366template <std::size_t BITS>
367class BitArray {
368 /* Array element type and vector of element type. */
369 using Element = std::uint32_t;
370 /* Number of bits in each BitArray element. */
371 static constexpr size_t WIDTH = sizeof(Element) * CHAR_BIT;
372 /* Number of elements to represent a bit array of the specified size of bits. */
373 static constexpr size_t COUNT = (BITS + WIDTH - 1) / WIDTH;
374
375public:
376 /* BUFFER type declaration for BitArray */
377 using Buffer = std::array<Element, COUNT>;
378 /* To tell if a bit is set in array, it selects an element from the array, and test
379 * if the relevant bit set.
380 * Note the parameter "bit" is an index to the bit, 0 <= bit < BITS.
381 */
382 inline bool test(size_t bit) const {
383 return (bit < BITS) ? mData[bit / WIDTH].test(bit % WIDTH) : false;
384 }
385 /* Returns total number of bytes needed for the array */
386 inline size_t bytes() { return (BITS + CHAR_BIT - 1) / CHAR_BIT; }
387 /* Returns true if array contains any non-zero bit from the range defined by start and end
388 * bit index [startIndex, endIndex).
389 */
390 bool any(size_t startIndex, size_t endIndex) {
391 if (startIndex >= endIndex || startIndex > BITS || endIndex > BITS + 1) {
392 ALOGE("Invalid start/end index. start = %zu, end = %zu, total bits = %zu", startIndex,
393 endIndex, BITS);
394 return false;
395 }
396 size_t se = startIndex / WIDTH; // Start of element
397 size_t ee = endIndex / WIDTH; // End of element
398 size_t si = startIndex % WIDTH; // Start index in start element
399 size_t ei = endIndex % WIDTH; // End index in end element
400 // Need to check first unaligned bitset for any non zero bit
401 if (si > 0) {
402 size_t nBits = se == ee ? ei - si : WIDTH - si;
403 // Generate the mask of interested bit range
404 Element mask = ((1 << nBits) - 1) << si;
405 if (mData[se++].to_ulong() & mask) {
406 return true;
407 }
408 }
409 // Check whole bitset for any bit set
410 for (; se < ee; se++) {
411 if (mData[se].any()) {
412 return true;
413 }
414 }
415 // Need to check last unaligned bitset for any non zero bit
416 if (ei > 0 && se <= ee) {
417 // Generate the mask of interested bit range
418 Element mask = (1 << ei) - 1;
419 if (mData[se].to_ulong() & mask) {
420 return true;
421 }
422 }
423 return false;
424 }
425 /* Load bit array values from buffer */
426 void loadFromBuffer(const Buffer& buffer) {
427 for (size_t i = 0; i < COUNT; i++) {
428 mData[i] = std::bitset<WIDTH>(buffer[i]);
429 }
430 }
431
432private:
433 std::array<std::bitset<WIDTH>, COUNT> mData;
434};
435
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700436class EventHub : public EventHubInterface {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800437public:
438 EventHub();
439
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700440 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800441
Chris Ye989bb932020-07-04 16:18:59 -0700442 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800443
Chris Ye989bb932020-07-04 16:18:59 -0700444 int32_t getDeviceControllerNumber(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800445
Chris Ye989bb932020-07-04 16:18:59 -0700446 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800447
Chris Ye989bb932020-07-04 16:18:59 -0700448 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
449 RawAbsoluteAxisInfo* outAxisInfo) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800450
Chris Ye989bb932020-07-04 16:18:59 -0700451 bool hasRelativeAxis(int32_t deviceId, int axis) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800452
Chris Ye989bb932020-07-04 16:18:59 -0700453 bool hasInputProperty(int32_t deviceId, int property) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800454
Chris Yef59a2f42020-10-16 12:55:26 -0700455 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final;
456
Chris Ye989bb932020-07-04 16:18:59 -0700457 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
458 int32_t* outKeycode, int32_t* outMetaState,
459 uint32_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800460
Chris Ye989bb932020-07-04 16:18:59 -0700461 status_t mapAxis(int32_t deviceId, int32_t scanCode,
462 AxisInfo* outAxisInfo) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800463
Chris Yef59a2f42020-10-16 12:55:26 -0700464 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
465 int32_t deviceId, int32_t absCode) override final;
466
Chris Yee2b1e5c2021-03-10 22:45:12 -0800467 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) override final;
468 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
469 int32_t BatteryId) override final;
470
Chris Ye3fdbfef2021-01-06 18:45:18 -0800471 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override final;
472
473 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override final;
474
475 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override final;
476 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override final;
477 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
478 int32_t deviceId, int32_t lightId) override final;
479 void setLightIntensities(int32_t deviceId, int32_t lightId,
480 std::unordered_map<LightColor, int32_t> intensities) override final;
481
Chris Ye989bb932020-07-04 16:18:59 -0700482 void setExcludedDevices(const std::vector<std::string>& devices) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800483
Chris Ye989bb932020-07-04 16:18:59 -0700484 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override final;
485 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override final;
486 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override final;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100487 int32_t getKeyCodeForKeyLocation(int32_t deviceId,
488 int32_t locationKeyCode) const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700489 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
490 int32_t* outValue) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800491
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700492 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Chris Ye989bb932020-07-04 16:18:59 -0700493 uint8_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800494
Chris Ye989bb932020-07-04 16:18:59 -0700495 size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) override final;
496 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800497
Chris Ye989bb932020-07-04 16:18:59 -0700498 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override final;
Arthur Hungcb40a002021-08-03 14:31:01 +0000499 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700500 bool hasLed(int32_t deviceId, int32_t led) const override final;
501 void setLedState(int32_t deviceId, int32_t led, bool on) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800502
Chris Ye989bb932020-07-04 16:18:59 -0700503 void getVirtualKeyDefinitions(
504 int32_t deviceId,
505 std::vector<VirtualKeyDefinition>& outVirtualKeys) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800506
Chris Ye3a1e4462020-08-12 10:13:15 -0700507 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(
508 int32_t deviceId) const override final;
509 bool setKeyboardLayoutOverlay(int32_t deviceId,
510 std::shared_ptr<KeyCharacterMap> map) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800511
Chris Ye989bb932020-07-04 16:18:59 -0700512 void vibrate(int32_t deviceId, const VibrationElement& effect) override final;
513 void cancelVibrate(int32_t deviceId) override final;
Chris Ye87143712020-11-10 05:05:58 +0000514 std::vector<int32_t> getVibratorIds(int32_t deviceId) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800515
Chris Ye989bb932020-07-04 16:18:59 -0700516 void requestReopenDevices() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800517
Chris Ye989bb932020-07-04 16:18:59 -0700518 void wake() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800519
Chris Ye989bb932020-07-04 16:18:59 -0700520 void dump(std::string& dump) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800521
Chris Ye989bb932020-07-04 16:18:59 -0700522 void monitor() override final;
523
Chris Yee2b1e5c2021-03-10 22:45:12 -0800524 std::optional<int32_t> getBatteryCapacity(int32_t deviceId,
525 int32_t batteryId) const override final;
Kim Low03ea0352020-11-06 12:45:07 -0800526
Chris Yee2b1e5c2021-03-10 22:45:12 -0800527 std::optional<int32_t> getBatteryStatus(int32_t deviceId,
528 int32_t batteryId) const override final;
Kim Low03ea0352020-11-06 12:45:07 -0800529
Chris Ye989bb932020-07-04 16:18:59 -0700530 bool isDeviceEnabled(int32_t deviceId) override final;
531
532 status_t enableDevice(int32_t deviceId) override final;
533
534 status_t disableDevice(int32_t deviceId) override final;
535
536 ~EventHub() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800537
538private:
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700539 struct AssociatedDevice {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800540 // The device descriptor from evdev device the misc device associated with.
541 std::string descriptor;
542 // The sysfs root path of the misc device.
543 std::filesystem::path sysfsRootPath;
544
545 int32_t nextBatteryId;
546 int32_t nextLightId;
547 std::unordered_map<int32_t, RawBatteryInfo> batteryInfos;
548 std::unordered_map<int32_t, RawLightInfo> lightInfos;
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700549 explicit AssociatedDevice(std::filesystem::path sysfsRootPath)
Chris Yee2b1e5c2021-03-10 22:45:12 -0800550 : sysfsRootPath(sysfsRootPath), nextBatteryId(0), nextLightId(0) {}
551 bool configureBatteryLocked();
552 bool configureLightsLocked();
553 };
554
Michael Wrightd02c5b62014-02-10 15:10:22 -0800555 struct Device {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700556 int fd; // may be -1 if device is closed
Michael Wrightd02c5b62014-02-10 15:10:22 -0800557 const int32_t id;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100558 const std::string path;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800559 const InputDeviceIdentifier identifier;
560
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -0800561 std::unique_ptr<TouchVideoDevice> videoDevice;
562
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700563 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800564
Chris Ye66fbac32020-07-06 20:36:43 -0700565 BitArray<KEY_MAX> keyBitmask;
566 BitArray<KEY_MAX> keyState;
567 BitArray<ABS_MAX> absBitmask;
568 BitArray<REL_MAX> relBitmask;
569 BitArray<SW_MAX> swBitmask;
570 BitArray<SW_MAX> swState;
571 BitArray<LED_MAX> ledBitmask;
572 BitArray<FF_MAX> ffBitmask;
573 BitArray<INPUT_PROP_MAX> propBitmask;
Chris Yef59a2f42020-10-16 12:55:26 -0700574 BitArray<MSC_MAX> mscBitmask;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800575
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100576 std::string configurationFile;
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500577 std::unique_ptr<PropertyMap> configuration;
Siarhei Vishniakou3e78dec2019-02-20 16:21:46 -0600578 std::unique_ptr<VirtualKeyMap> virtualKeyMap;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800579 KeyMap keyMap;
580
Michael Wrightd02c5b62014-02-10 15:10:22 -0800581 bool ffEffectPlaying;
582 int16_t ffEffectId; // initially -1
583
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700584 // A shared_ptr of a device associated with the input device.
585 // The input devices with same descriptor has the same associated device.
586 std::shared_ptr<AssociatedDevice> associatedDevice;
Kim Low03ea0352020-11-06 12:45:07 -0800587
Michael Wrightd02c5b62014-02-10 15:10:22 -0800588 int32_t controllerNumber;
589
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100590 Device(int fd, int32_t id, const std::string& path,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700591 const InputDeviceIdentifier& identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800592 ~Device();
593
594 void close();
595
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700596 bool enabled; // initially true
597 status_t enable();
598 status_t disable();
Chris Ye989bb932020-07-04 16:18:59 -0700599 bool hasValidFd() const;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700600 const bool isVirtual; // set if fd < 0 is passed to constructor
Michael Wrightd02c5b62014-02-10 15:10:22 -0800601
Chris Ye3a1e4462020-08-12 10:13:15 -0700602 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const;
Chris Ye66fbac32020-07-06 20:36:43 -0700603
604 template <std::size_t N>
Chris Ye989bb932020-07-04 16:18:59 -0700605 status_t readDeviceBitMask(unsigned long ioctlCode, BitArray<N>& bitArray);
Chris Ye66fbac32020-07-06 20:36:43 -0700606
Chris Ye989bb932020-07-04 16:18:59 -0700607 void configureFd();
608 bool hasKeycodeLocked(int keycode) const;
609 void loadConfigurationLocked();
610 bool loadVirtualKeyMapLocked();
611 status_t loadKeyMapLocked();
612 bool isExternalDeviceLocked();
613 bool deviceHasMicLocked();
614 void setLedForControllerLocked();
615 status_t mapLed(int32_t led, int32_t* outScanCode) const;
616 void setLedStateLocked(int32_t led, bool on);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800617 };
618
Siarhei Vishniakoua4c502a2021-02-05 00:45:20 +0000619 /**
620 * Create a new device for the provided path.
621 */
Chris Yed3fef462021-03-07 17:10:08 -0800622 void openDeviceLocked(const std::string& devicePath) REQUIRES(mLock);
623 void openVideoDeviceLocked(const std::string& devicePath) REQUIRES(mLock);
Siarhei Vishniakouf49608d2020-08-20 19:18:21 -0500624 /**
625 * Try to associate a video device with an input device. If the association succeeds,
626 * the videoDevice is moved into the input device. 'videoDevice' will become null if this
627 * happens.
628 * Return true if the association succeeds.
629 * Return false otherwise.
630 */
Chris Yed3fef462021-03-07 17:10:08 -0800631 bool tryAddVideoDeviceLocked(Device& device, std::unique_ptr<TouchVideoDevice>& videoDevice)
632 REQUIRES(mLock);
633 void createVirtualKeyboardLocked() REQUIRES(mLock);
634 void addDeviceLocked(std::unique_ptr<Device> device) REQUIRES(mLock);
635 void assignDescriptorLocked(InputDeviceIdentifier& identifier) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800636
Chris Yed3fef462021-03-07 17:10:08 -0800637 void closeDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock);
638 void closeVideoDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock);
639 void closeDeviceLocked(Device& device) REQUIRES(mLock);
640 void closeAllDevicesLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800641
Siarhei Vishniakou25920312018-12-12 15:24:44 -0800642 status_t registerFdForEpoll(int fd);
643 status_t unregisterFdFromEpoll(int fd);
Chris Yed3fef462021-03-07 17:10:08 -0800644 status_t registerDeviceForEpollLocked(Device& device) REQUIRES(mLock);
645 void registerVideoDeviceForEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock);
646 status_t unregisterDeviceFromEpollLocked(Device& device) REQUIRES(mLock);
647 void unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700648
Chris Yed3fef462021-03-07 17:10:08 -0800649 status_t scanDirLocked(const std::string& dirname) REQUIRES(mLock);
650 status_t scanVideoDirLocked(const std::string& dirname) REQUIRES(mLock);
651 void scanDevicesLocked() REQUIRES(mLock);
Prabir Pradhan952e65b2022-06-23 17:49:55 +0000652 base::Result<void> readNotifyLocked() REQUIRES(mLock);
653 void handleNotifyEventLocked(const inotify_event&) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800654
Chris Yed3fef462021-03-07 17:10:08 -0800655 Device* getDeviceLocked(int32_t deviceId) const REQUIRES(mLock);
656 Device* getDeviceByPathLocked(const std::string& devicePath) const REQUIRES(mLock);
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700657 /**
658 * Look through all available fd's (both for input devices and for video devices),
659 * and return the device pointer.
660 */
Chris Yed3fef462021-03-07 17:10:08 -0800661 Device* getDeviceByFdLocked(int fd) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800662
Chris Yed3fef462021-03-07 17:10:08 -0800663 int32_t getNextControllerNumberLocked(const std::string& name) REQUIRES(mLock);
Josh Bartel938632f2022-07-19 15:34:22 -0500664
665 bool hasDeviceWithDescriptorLocked(const std::string& descriptor) const REQUIRES(mLock);
666
Chris Yed3fef462021-03-07 17:10:08 -0800667 void releaseControllerNumberLocked(int32_t num) REQUIRES(mLock);
668 void reportDeviceAddedForStatisticsLocked(const InputDeviceIdentifier& identifier,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700669 ftl::Flags<InputDeviceClass> classes) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800670
Chris Yee2b1e5c2021-03-10 22:45:12 -0800671 const std::unordered_map<int32_t, RawBatteryInfo>& getBatteryInfoLocked(int32_t deviceId) const
672 REQUIRES(mLock);
673
674 const std::unordered_map<int32_t, RawLightInfo>& getLightInfoLocked(int32_t deviceId) const
675 REQUIRES(mLock);
676
Usama Arifb27c8e62021-06-03 16:44:09 +0100677 void addDeviceInputInotify();
678 void addDeviceInotify();
679
Michael Wrightd02c5b62014-02-10 15:10:22 -0800680 // Protect all internal state.
Chris Ye1c2e0892020-11-30 21:41:44 -0800681 mutable std::mutex mLock;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800682
683 // The actual id of the built-in keyboard, or NO_BUILT_IN_KEYBOARD if none.
684 // EventHub remaps the built-in keyboard to id 0 externally as required by the API.
685 enum {
686 // Must not conflict with any other assigned device ids, including
687 // the virtual keyboard id (-1).
688 NO_BUILT_IN_KEYBOARD = -2,
689 };
690 int32_t mBuiltInKeyboardId;
691
692 int32_t mNextDeviceId;
693
694 BitSet32 mControllerNumbers;
695
Chris Ye989bb932020-07-04 16:18:59 -0700696 std::unordered_map<int32_t, std::unique_ptr<Device>> mDevices;
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800697 /**
698 * Video devices that report touchscreen heatmap, but have not (yet) been paired
699 * with a specific input device. Video device discovery is independent from input device
700 * discovery, so the two types of devices could be found in any order.
701 * Ideally, video devices in this queue do not have an open fd, or at least aren't
702 * actively streaming.
703 */
704 std::vector<std::unique_ptr<TouchVideoDevice>> mUnattachedVideoDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800705
Chris Ye989bb932020-07-04 16:18:59 -0700706 std::vector<std::unique_ptr<Device>> mOpeningDevices;
707 std::vector<std::unique_ptr<Device>> mClosingDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800708
709 bool mNeedToSendFinishedDeviceScan;
710 bool mNeedToReopenDevices;
711 bool mNeedToScanDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100712 std::vector<std::string> mExcludedDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800713
714 int mEpollFd;
715 int mINotifyFd;
716 int mWakeReadPipeFd;
717 int mWakeWritePipeFd;
718
Usama Arifb27c8e62021-06-03 16:44:09 +0100719 int mDeviceInputWd;
720 int mDeviceWd = -1;
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800721
Michael Wrightd02c5b62014-02-10 15:10:22 -0800722 // Maximum number of signalled FDs to handle at a time.
723 static const int EPOLL_MAX_EVENTS = 16;
724
725 // The array of pending epoll events and the index of the next event to be handled.
726 struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS];
727 size_t mPendingEventCount;
728 size_t mPendingEventIndex;
729 bool mPendingINotify;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800730};
731
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700732} // namespace android
Michael Wrightd02c5b62014-02-10 15:10:22 -0800733
734#endif // _RUNTIME_EVENT_HUB_H