blob: 0b15efed12616cf2cfe94cfb14c2ee62cc350b33 [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
Prabir Pradhan48108662022-09-09 21:22:04 +000017#pragma once
Michael Wrightd02c5b62014-02-10 15:10:22 -080018
Chris Ye66fbac32020-07-06 20:36:43 -070019#include <bitset>
20#include <climits>
Dominik Laskowski2f01d772022-03-23 16:01:29 -070021#include <filesystem>
Harry Cuttsea73eaa2023-01-16 17:55:46 +000022#include <ostream>
23#include <string>
Chris Ye989bb932020-07-04 16:18:59 -070024#include <unordered_map>
Prabir Pradhan51894782022-08-23 16:29:10 +000025#include <utility>
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010026#include <vector>
27
Kim Low03ea0352020-11-06 12:45:07 -080028#include <batteryservice/BatteryService.h>
Dominik Laskowski2f01d772022-03-23 16:01:29 -070029#include <ftl/flags.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080030#include <input/Input.h>
31#include <input/InputDevice.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080032#include <input/KeyCharacterMap.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070033#include <input/KeyLayoutMap.h>
34#include <input/Keyboard.h>
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070035#include <input/PropertyMap.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080036#include <input/VirtualKeyMap.h>
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000037#include <linux/input.h>
38#include <sys/epoll.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080039#include <utils/BitSet.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070040#include <utils/Errors.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070041#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
Prabir Pradhan952e65b2022-06-23 17:49:55 +000048struct inotify_event;
49
Michael Wrightd02c5b62014-02-10 15:10:22 -080050namespace android {
51
Chris Ye3fdbfef2021-01-06 18:45:18 -080052/* Number of colors : {red, green, blue} */
53static constexpr size_t COLOR_NUM = 3;
Michael Wrightd02c5b62014-02-10 15:10:22 -080054/*
55 * A raw event as retrieved from the EventHub.
56 */
57struct RawEvent {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000058 // Time when the event happened
Michael Wrightd02c5b62014-02-10 15:10:22 -080059 nsecs_t when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000060 // Time when the event was read by EventHub. Only populated for input events.
61 // For other events (device added/removed/etc), this value is undefined and should not be read.
62 nsecs_t readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -080063 int32_t deviceId;
64 int32_t type;
65 int32_t code;
66 int32_t value;
67};
68
69/* Describes an absolute axis. */
70struct RawAbsoluteAxisInfo {
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000071 bool valid{false}; // true if the information is valid, false otherwise
Michael Wrightd02c5b62014-02-10 15:10:22 -080072
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000073 int32_t minValue{}; // minimum value
74 int32_t maxValue{}; // maximum value
75 int32_t flat{}; // center flat position, eg. flat == 8 means center is between -8 and 8
76 int32_t fuzz{}; // error tolerance, eg. fuzz == 4 means value is +/- 4 due to noise
77 int32_t resolution{}; // resolution in units per mm or radians per mm
Michael Wrightd02c5b62014-02-10 15:10:22 -080078
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000079 inline void clear() { *this = RawAbsoluteAxisInfo(); }
Michael Wrightd02c5b62014-02-10 15:10:22 -080080};
81
Harry Cuttsea73eaa2023-01-16 17:55:46 +000082std::ostream& operator<<(std::ostream& out, const RawAbsoluteAxisInfo& info);
83
Michael Wrightd02c5b62014-02-10 15:10:22 -080084/*
85 * Input device classes.
86 */
Chris Ye1b0c7342020-07-28 21:57:03 -070087enum class InputDeviceClass : uint32_t {
Michael Wrightd02c5b62014-02-10 15:10:22 -080088 /* The input device is a keyboard or has buttons. */
Chris Ye1b0c7342020-07-28 21:57:03 -070089 KEYBOARD = 0x00000001,
Michael Wrightd02c5b62014-02-10 15:10:22 -080090
91 /* The input device is an alpha-numeric keyboard (not just a dial pad). */
Chris Ye1b0c7342020-07-28 21:57:03 -070092 ALPHAKEY = 0x00000002,
Michael Wrightd02c5b62014-02-10 15:10:22 -080093
94 /* The input device is a touchscreen or a touchpad (either single-touch or multi-touch). */
Chris Ye1b0c7342020-07-28 21:57:03 -070095 TOUCH = 0x00000004,
Michael Wrightd02c5b62014-02-10 15:10:22 -080096
97 /* The input device is a cursor device such as a trackball or mouse. */
Chris Ye1b0c7342020-07-28 21:57:03 -070098 CURSOR = 0x00000008,
Michael Wrightd02c5b62014-02-10 15:10:22 -080099
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000100 /* The input device is a multi-touch touchscreen or touchpad. */
Chris Ye1b0c7342020-07-28 21:57:03 -0700101 TOUCH_MT = 0x00000010,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800102
103 /* The input device is a directional pad (implies keyboard, has DPAD keys). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700104 DPAD = 0x00000020,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800105
106 /* The input device is a gamepad (implies keyboard, has BUTTON keys). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700107 GAMEPAD = 0x00000040,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800108
109 /* The input device has switches. */
Chris Ye1b0c7342020-07-28 21:57:03 -0700110 SWITCH = 0x00000080,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800111
112 /* The input device is a joystick (implies gamepad, has joystick absolute axes). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700113 JOYSTICK = 0x00000100,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800114
115 /* The input device has a vibrator (supports FF_RUMBLE). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700116 VIBRATOR = 0x00000200,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800117
Tim Kilbourn063ff532015-04-08 10:26:18 -0700118 /* The input device has a microphone. */
Chris Ye1b0c7342020-07-28 21:57:03 -0700119 MIC = 0x00000400,
Tim Kilbourn063ff532015-04-08 10:26:18 -0700120
Michael Wright842500e2015-03-13 17:32:02 -0700121 /* The input device is an external stylus (has data we want to fuse with touch data). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700122 EXTERNAL_STYLUS = 0x00000800,
Michael Wright842500e2015-03-13 17:32:02 -0700123
Prashant Malani1941ff52015-08-11 18:29:28 -0700124 /* The input device has a rotary encoder */
Chris Ye1b0c7342020-07-28 21:57:03 -0700125 ROTARY_ENCODER = 0x00001000,
Prashant Malani1941ff52015-08-11 18:29:28 -0700126
Chris Yef59a2f42020-10-16 12:55:26 -0700127 /* The input device has a sensor like accelerometer, gyro, etc */
128 SENSOR = 0x00002000,
129
Kim Low03ea0352020-11-06 12:45:07 -0800130 /* The input device has a battery */
131 BATTERY = 0x00004000,
132
Chris Ye3fdbfef2021-01-06 18:45:18 -0800133 /* The input device has sysfs controllable lights */
134 LIGHT = 0x00008000,
135
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000136 /* The input device is a touchpad, requiring an on-screen cursor. */
137 TOUCHPAD = 0x00010000,
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,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +0000176 /* The input light has kbd_backlight name */
177 KEYBOARD_BACKLIGHT = 0x00000100,
Chris Ye3fdbfef2021-01-06 18:45:18 -0800178};
179
Chris Yee2b1e5c2021-03-10 22:45:12 -0800180enum class InputBatteryClass : uint32_t {
181 /* The input device battery has capacity node. */
182 CAPACITY = 0x00000001,
183 /* The input device battery has capacity_level node. */
184 CAPACITY_LEVEL = 0x00000002,
185 /* The input device battery has status node. */
186 STATUS = 0x00000004,
187};
188
Chris Ye3fdbfef2021-01-06 18:45:18 -0800189/* Describes a raw light. */
190struct RawLightInfo {
191 int32_t id;
192 std::string name;
193 std::optional<int32_t> maxBrightness;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700194 ftl::Flags<InputLightClass> flags;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800195 std::array<int32_t, COLOR_NUM> rgbIndex;
196 std::filesystem::path path;
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000197
198 bool operator==(const RawLightInfo&) const = default;
199 bool operator!=(const RawLightInfo&) const = default;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800200};
201
Chris Yee2b1e5c2021-03-10 22:45:12 -0800202/* Describes a raw battery. */
203struct RawBatteryInfo {
204 int32_t id;
205 std::string name;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700206 ftl::Flags<InputBatteryClass> flags;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800207 std::filesystem::path path;
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000208
209 bool operator==(const RawBatteryInfo&) const = default;
210 bool operator!=(const RawBatteryInfo&) const = default;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800211};
212
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000213/* Layout information associated with the device */
214struct RawLayoutInfo {
215 std::string languageTag;
216 std::string layoutType;
217
218 bool operator==(const RawLayoutInfo&) const = default;
219 bool operator!=(const RawLayoutInfo&) const = default;
220};
221
Michael Wrightd02c5b62014-02-10 15:10:22 -0800222/*
223 * Gets the class that owns an axis, in cases where multiple classes might claim
224 * the same axis for different purposes.
225 */
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700226extern ftl::Flags<InputDeviceClass> getAbsAxisUsage(int32_t axis,
227 ftl::Flags<InputDeviceClass> deviceClasses);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800228
229/*
230 * Grand Central Station for events.
231 *
232 * The event hub aggregates input events received across all known input
233 * devices on the system, including devices that may be emulated by the simulator
234 * environment. In addition, the event hub generates fake input events to indicate
235 * when devices are added or removed.
236 *
237 * The event hub provides a stream of input events (via the getEvent function).
238 * It also supports querying the current actual state of input devices such as identifying
239 * which keys are currently down. Finally, the event hub keeps track of the capabilities of
240 * individual input devices, such as their class and the set of key codes that they support.
241 */
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700242class EventHubInterface {
243public:
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700244 EventHubInterface() {}
245 virtual ~EventHubInterface() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800246
Michael Wrightd02c5b62014-02-10 15:10:22 -0800247 // Synthetic raw event type codes produced when devices are added or removed.
248 enum {
249 // Sent when a device is added.
250 DEVICE_ADDED = 0x10000000,
251 // Sent when a device is removed.
252 DEVICE_REMOVED = 0x20000000,
253 // Sent when all added/removed devices from the most recent scan have been reported.
254 // This event is always sent at least once.
255 FINISHED_DEVICE_SCAN = 0x30000000,
256
257 FIRST_SYNTHETIC_EVENT = DEVICE_ADDED,
258 };
259
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700260 virtual ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800261
262 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const = 0;
263
264 virtual int32_t getDeviceControllerNumber(int32_t deviceId) const = 0;
265
Harry Cuttsc34f7582023-03-07 16:23:30 +0000266 /**
267 * Get the PropertyMap for the provided EventHub device, if available.
268 * This acquires the device lock, so a copy is returned rather than the raw pointer
269 * to the device's PropertyMap. A std::nullopt may be returned if the device could
270 * not be found, or if it doesn't have any configuration.
271 */
272 virtual std::optional<PropertyMap> getConfiguration(int32_t deviceId) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800273
274 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700275 RawAbsoluteAxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800276
277 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const = 0;
278
279 virtual bool hasInputProperty(int32_t deviceId, int property) const = 0;
280
Chris Yef59a2f42020-10-16 12:55:26 -0700281 virtual bool hasMscEvent(int32_t deviceId, int mscEvent) const = 0;
282
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000283 virtual void addKeyRemapping(int32_t deviceId, int32_t fromKeyCode,
284 int32_t toKeyCode) const = 0;
285
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700286 virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
287 int32_t metaState, int32_t* outKeycode, int32_t* outMetaState,
288 uint32_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800289
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700290 virtual status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800291
292 // Sets devices that are excluded from opening.
293 // This can be used to ignore input devices for sensors.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100294 virtual void setExcludedDevices(const std::vector<std::string>& devices) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800295
296 /*
297 * Wait for events to become available and returns them.
298 * After returning, the EventHub holds onto a wake lock until the next call to getEvent.
299 * This ensures that the device will not go to sleep while the event is being processed.
300 * If the device needs to remain awake longer than that, then the caller is responsible
301 * for taking care of it (say, by poking the power manager user activity timer).
302 *
303 * The timeout is advisory only. If the device is asleep, it will not wake just to
304 * service the timeout.
305 *
306 * Returns the number of events obtained, or 0 if the timeout expired.
307 */
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700308 virtual std::vector<RawEvent> getEvents(int timeoutMillis) = 0;
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800309 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) = 0;
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000310 virtual base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
311 int32_t deviceId, int32_t absCode) const = 0;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800312 // Raw batteries are sysfs power_supply nodes we found from the EventHub device sysfs node,
313 // containing the raw info of the sysfs node structure.
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000314 virtual std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const = 0;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800315 virtual std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000316 int32_t BatteryId) const = 0;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800317
Chris Ye3fdbfef2021-01-06 18:45:18 -0800318 // Raw lights are sysfs led light nodes we found from the EventHub device sysfs node,
319 // containing the raw info of the sysfs node structure.
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000320 virtual std::vector<int32_t> getRawLightIds(int32_t deviceId) const = 0;
321 virtual std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId,
322 int32_t lightId) const = 0;
323 virtual std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const = 0;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800324 virtual void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) = 0;
325 virtual std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000326 int32_t deviceId, int32_t lightId) const = 0;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800327 virtual void setLightIntensities(int32_t deviceId, int32_t lightId,
328 std::unordered_map<LightColor, int32_t> intensities) = 0;
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000329 /* Query Layout info associated with the input device. */
330 virtual std::optional<RawLayoutInfo> getRawLayoutInfo(int32_t deviceId) const = 0;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000331 /* Query current input state. */
Michael Wrightd02c5b62014-02-10 15:10:22 -0800332 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0;
333 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0;
334 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0;
335 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700336 int32_t* outValue) const = 0;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100337 virtual int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338
339 /*
340 * Examine key input devices for specific framework keycode support
341 */
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700342 virtual bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700343 uint8_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344
345 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const = 0;
Arthur Hungcb40a002021-08-03 14:31:01 +0000346 virtual bool hasKeyCode(int32_t deviceId, int32_t keyCode) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800347
348 /* LED related functions expect Android LED constants, not scan codes or HID usages */
349 virtual bool hasLed(int32_t deviceId, int32_t led) const = 0;
350 virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0;
351
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700352 virtual void getVirtualKeyDefinitions(
353 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800354
Chris Ye3a1e4462020-08-12 10:13:15 -0700355 virtual const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const = 0;
356 virtual bool setKeyboardLayoutOverlay(int32_t deviceId,
357 std::shared_ptr<KeyCharacterMap> map) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800358
359 /* Control the vibrator. */
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000360 virtual void vibrate(int32_t deviceId, const VibrationElement& effect) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800361 virtual void cancelVibrate(int32_t deviceId) = 0;
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000362 virtual std::vector<int32_t> getVibratorIds(int32_t deviceId) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800363
Kim Low03ea0352020-11-06 12:45:07 -0800364 /* Query battery level. */
Chris Yee2b1e5c2021-03-10 22:45:12 -0800365 virtual std::optional<int32_t> getBatteryCapacity(int32_t deviceId,
366 int32_t batteryId) const = 0;
Kim Low03ea0352020-11-06 12:45:07 -0800367
368 /* Query battery status. */
Chris Yee2b1e5c2021-03-10 22:45:12 -0800369 virtual std::optional<int32_t> getBatteryStatus(int32_t deviceId, int32_t batteryId) const = 0;
Kim Low03ea0352020-11-06 12:45:07 -0800370
Michael Wrightd02c5b62014-02-10 15:10:22 -0800371 /* Requests the EventHub to reopen all input devices on the next call to getEvents(). */
372 virtual void requestReopenDevices() = 0;
373
374 /* Wakes up getEvents() if it is blocked on a read. */
375 virtual void wake() = 0;
376
377 /* Dump EventHub state to a string. */
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000378 virtual void dump(std::string& dump) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800379
380 /* Called by the heatbeat to ensures that the reader has not deadlocked. */
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000381 virtual void monitor() const = 0;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700382
383 /* Return true if the device is enabled. */
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000384 virtual bool isDeviceEnabled(int32_t deviceId) const = 0;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700385
386 /* Enable an input device */
387 virtual status_t enableDevice(int32_t deviceId) = 0;
388
389 /* Disable an input device. Closes file descriptor to that device. */
390 virtual status_t disableDevice(int32_t deviceId) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800391};
392
Chris Ye66fbac32020-07-06 20:36:43 -0700393template <std::size_t BITS>
394class BitArray {
395 /* Array element type and vector of element type. */
396 using Element = std::uint32_t;
397 /* Number of bits in each BitArray element. */
398 static constexpr size_t WIDTH = sizeof(Element) * CHAR_BIT;
399 /* Number of elements to represent a bit array of the specified size of bits. */
400 static constexpr size_t COUNT = (BITS + WIDTH - 1) / WIDTH;
401
402public:
403 /* BUFFER type declaration for BitArray */
404 using Buffer = std::array<Element, COUNT>;
405 /* To tell if a bit is set in array, it selects an element from the array, and test
406 * if the relevant bit set.
407 * Note the parameter "bit" is an index to the bit, 0 <= bit < BITS.
408 */
409 inline bool test(size_t bit) const {
410 return (bit < BITS) ? mData[bit / WIDTH].test(bit % WIDTH) : false;
411 }
412 /* Returns total number of bytes needed for the array */
413 inline size_t bytes() { return (BITS + CHAR_BIT - 1) / CHAR_BIT; }
414 /* Returns true if array contains any non-zero bit from the range defined by start and end
415 * bit index [startIndex, endIndex).
416 */
417 bool any(size_t startIndex, size_t endIndex) {
418 if (startIndex >= endIndex || startIndex > BITS || endIndex > BITS + 1) {
419 ALOGE("Invalid start/end index. start = %zu, end = %zu, total bits = %zu", startIndex,
420 endIndex, BITS);
421 return false;
422 }
423 size_t se = startIndex / WIDTH; // Start of element
424 size_t ee = endIndex / WIDTH; // End of element
425 size_t si = startIndex % WIDTH; // Start index in start element
426 size_t ei = endIndex % WIDTH; // End index in end element
427 // Need to check first unaligned bitset for any non zero bit
428 if (si > 0) {
429 size_t nBits = se == ee ? ei - si : WIDTH - si;
430 // Generate the mask of interested bit range
431 Element mask = ((1 << nBits) - 1) << si;
432 if (mData[se++].to_ulong() & mask) {
433 return true;
434 }
435 }
436 // Check whole bitset for any bit set
437 for (; se < ee; se++) {
438 if (mData[se].any()) {
439 return true;
440 }
441 }
442 // Need to check last unaligned bitset for any non zero bit
443 if (ei > 0 && se <= ee) {
444 // Generate the mask of interested bit range
445 Element mask = (1 << ei) - 1;
446 if (mData[se].to_ulong() & mask) {
447 return true;
448 }
449 }
450 return false;
451 }
452 /* Load bit array values from buffer */
453 void loadFromBuffer(const Buffer& buffer) {
454 for (size_t i = 0; i < COUNT; i++) {
455 mData[i] = std::bitset<WIDTH>(buffer[i]);
456 }
457 }
458
459private:
460 std::array<std::bitset<WIDTH>, COUNT> mData;
461};
462
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700463class EventHub : public EventHubInterface {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800464public:
465 EventHub();
466
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700467 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800468
Chris Ye989bb932020-07-04 16:18:59 -0700469 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800470
Chris Ye989bb932020-07-04 16:18:59 -0700471 int32_t getDeviceControllerNumber(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800472
Harry Cuttsc34f7582023-03-07 16:23:30 +0000473 std::optional<PropertyMap> getConfiguration(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800474
Chris Ye989bb932020-07-04 16:18:59 -0700475 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
476 RawAbsoluteAxisInfo* outAxisInfo) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800477
Chris Ye989bb932020-07-04 16:18:59 -0700478 bool hasRelativeAxis(int32_t deviceId, int axis) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800479
Chris Ye989bb932020-07-04 16:18:59 -0700480 bool hasInputProperty(int32_t deviceId, int property) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800481
Chris Yef59a2f42020-10-16 12:55:26 -0700482 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final;
483
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000484 void addKeyRemapping(int32_t deviceId, int32_t fromKeyCode,
485 int32_t toKeyCode) const override final;
486
Chris Ye989bb932020-07-04 16:18:59 -0700487 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
488 int32_t* outKeycode, int32_t* outMetaState,
489 uint32_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800490
Chris Ye989bb932020-07-04 16:18:59 -0700491 status_t mapAxis(int32_t deviceId, int32_t scanCode,
492 AxisInfo* outAxisInfo) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800493
Chris Yef59a2f42020-10-16 12:55:26 -0700494 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000495 int32_t deviceId, int32_t absCode) const override final;
Chris Yef59a2f42020-10-16 12:55:26 -0700496
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000497 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override final;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800498 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000499 int32_t BatteryId) const override final;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800500
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000501 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800502
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000503 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId,
504 int32_t lightId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800505
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000506 std::optional<int32_t> getLightBrightness(int32_t deviceId,
507 int32_t lightId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800508 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override final;
509 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000510 int32_t deviceId, int32_t lightId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800511 void setLightIntensities(int32_t deviceId, int32_t lightId,
512 std::unordered_map<LightColor, int32_t> intensities) override final;
513
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000514 std::optional<RawLayoutInfo> getRawLayoutInfo(int32_t deviceId) const override final;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000515
Chris Ye989bb932020-07-04 16:18:59 -0700516 void setExcludedDevices(const std::vector<std::string>& devices) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800517
Chris Ye989bb932020-07-04 16:18:59 -0700518 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override final;
519 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override final;
520 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override final;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100521 int32_t getKeyCodeForKeyLocation(int32_t deviceId,
522 int32_t locationKeyCode) const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700523 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
524 int32_t* outValue) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800525
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700526 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Chris Ye989bb932020-07-04 16:18:59 -0700527 uint8_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800528
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700529 std::vector<RawEvent> getEvents(int timeoutMillis) override final;
Chris Ye989bb932020-07-04 16:18:59 -0700530 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800531
Chris Ye989bb932020-07-04 16:18:59 -0700532 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override final;
Arthur Hungcb40a002021-08-03 14:31:01 +0000533 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700534 bool hasLed(int32_t deviceId, int32_t led) const override final;
535 void setLedState(int32_t deviceId, int32_t led, bool on) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800536
Chris Ye989bb932020-07-04 16:18:59 -0700537 void getVirtualKeyDefinitions(
538 int32_t deviceId,
539 std::vector<VirtualKeyDefinition>& outVirtualKeys) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800540
Chris Ye3a1e4462020-08-12 10:13:15 -0700541 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(
542 int32_t deviceId) const override final;
543 bool setKeyboardLayoutOverlay(int32_t deviceId,
544 std::shared_ptr<KeyCharacterMap> map) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800545
Chris Ye989bb932020-07-04 16:18:59 -0700546 void vibrate(int32_t deviceId, const VibrationElement& effect) override final;
547 void cancelVibrate(int32_t deviceId) override final;
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000548 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800549
Chris Ye989bb932020-07-04 16:18:59 -0700550 void requestReopenDevices() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800551
Chris Ye989bb932020-07-04 16:18:59 -0700552 void wake() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800553
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000554 void dump(std::string& dump) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800555
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000556 void monitor() const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700557
Chris Yee2b1e5c2021-03-10 22:45:12 -0800558 std::optional<int32_t> getBatteryCapacity(int32_t deviceId,
559 int32_t batteryId) const override final;
Kim Low03ea0352020-11-06 12:45:07 -0800560
Chris Yee2b1e5c2021-03-10 22:45:12 -0800561 std::optional<int32_t> getBatteryStatus(int32_t deviceId,
562 int32_t batteryId) const override final;
Kim Low03ea0352020-11-06 12:45:07 -0800563
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000564 bool isDeviceEnabled(int32_t deviceId) const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700565
566 status_t enableDevice(int32_t deviceId) override final;
567
568 status_t disableDevice(int32_t deviceId) override final;
569
570 ~EventHub() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800571
572private:
Prabir Pradhancb42b472022-08-23 16:01:19 +0000573 // Holds information about the sysfs device associated with the Device.
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700574 struct AssociatedDevice {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800575 // The sysfs root path of the misc device.
576 std::filesystem::path sysfsRootPath;
Prabir Pradhancb42b472022-08-23 16:01:19 +0000577 std::unordered_map<int32_t /*batteryId*/, RawBatteryInfo> batteryInfos;
578 std::unordered_map<int32_t /*lightId*/, RawLightInfo> lightInfos;
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000579 std::optional<RawLayoutInfo> layoutInfo;
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000580
581 bool operator==(const AssociatedDevice&) const = default;
582 bool operator!=(const AssociatedDevice&) const = default;
583 std::string dump() const;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800584 };
585
Michael Wrightd02c5b62014-02-10 15:10:22 -0800586 struct Device {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700587 int fd; // may be -1 if device is closed
Michael Wrightd02c5b62014-02-10 15:10:22 -0800588 const int32_t id;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100589 const std::string path;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800590 const InputDeviceIdentifier identifier;
591
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -0800592 std::unique_ptr<TouchVideoDevice> videoDevice;
593
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700594 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800595
Chris Ye66fbac32020-07-06 20:36:43 -0700596 BitArray<KEY_MAX> keyBitmask;
597 BitArray<KEY_MAX> keyState;
598 BitArray<ABS_MAX> absBitmask;
599 BitArray<REL_MAX> relBitmask;
600 BitArray<SW_MAX> swBitmask;
601 BitArray<SW_MAX> swState;
602 BitArray<LED_MAX> ledBitmask;
603 BitArray<FF_MAX> ffBitmask;
604 BitArray<INPUT_PROP_MAX> propBitmask;
Chris Yef59a2f42020-10-16 12:55:26 -0700605 BitArray<MSC_MAX> mscBitmask;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800606
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100607 std::string configurationFile;
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500608 std::unique_ptr<PropertyMap> configuration;
Siarhei Vishniakou3e78dec2019-02-20 16:21:46 -0600609 std::unique_ptr<VirtualKeyMap> virtualKeyMap;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800610 KeyMap keyMap;
611
Michael Wrightd02c5b62014-02-10 15:10:22 -0800612 bool ffEffectPlaying;
613 int16_t ffEffectId; // initially -1
614
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700615 // A shared_ptr of a device associated with the input device.
Prabir Pradhan51894782022-08-23 16:29:10 +0000616 // The input devices that have the same sysfs path have the same associated device.
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000617 std::shared_ptr<const AssociatedDevice> associatedDevice;
Kim Low03ea0352020-11-06 12:45:07 -0800618
Michael Wrightd02c5b62014-02-10 15:10:22 -0800619 int32_t controllerNumber;
620
Prabir Pradhancb42b472022-08-23 16:01:19 +0000621 Device(int fd, int32_t id, std::string path, InputDeviceIdentifier identifier,
622 std::shared_ptr<const AssociatedDevice> assocDev);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800623 ~Device();
624
625 void close();
626
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700627 bool enabled; // initially true
628 status_t enable();
629 status_t disable();
Chris Ye989bb932020-07-04 16:18:59 -0700630 bool hasValidFd() const;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700631 const bool isVirtual; // set if fd < 0 is passed to constructor
Michael Wrightd02c5b62014-02-10 15:10:22 -0800632
Chris Ye3a1e4462020-08-12 10:13:15 -0700633 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const;
Chris Ye66fbac32020-07-06 20:36:43 -0700634
635 template <std::size_t N>
Chris Ye989bb932020-07-04 16:18:59 -0700636 status_t readDeviceBitMask(unsigned long ioctlCode, BitArray<N>& bitArray);
Chris Ye66fbac32020-07-06 20:36:43 -0700637
Chris Ye989bb932020-07-04 16:18:59 -0700638 void configureFd();
639 bool hasKeycodeLocked(int keycode) const;
640 void loadConfigurationLocked();
641 bool loadVirtualKeyMapLocked();
642 status_t loadKeyMapLocked();
643 bool isExternalDeviceLocked();
644 bool deviceHasMicLocked();
645 void setLedForControllerLocked();
646 status_t mapLed(int32_t led, int32_t* outScanCode) const;
647 void setLedStateLocked(int32_t led, bool on);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800648 };
649
Siarhei Vishniakoua4c502a2021-02-05 00:45:20 +0000650 /**
651 * Create a new device for the provided path.
652 */
Chris Yed3fef462021-03-07 17:10:08 -0800653 void openDeviceLocked(const std::string& devicePath) REQUIRES(mLock);
654 void openVideoDeviceLocked(const std::string& devicePath) REQUIRES(mLock);
Siarhei Vishniakouf49608d2020-08-20 19:18:21 -0500655 /**
656 * Try to associate a video device with an input device. If the association succeeds,
657 * the videoDevice is moved into the input device. 'videoDevice' will become null if this
658 * happens.
659 * Return true if the association succeeds.
660 * Return false otherwise.
661 */
Chris Yed3fef462021-03-07 17:10:08 -0800662 bool tryAddVideoDeviceLocked(Device& device, std::unique_ptr<TouchVideoDevice>& videoDevice)
663 REQUIRES(mLock);
664 void createVirtualKeyboardLocked() REQUIRES(mLock);
665 void addDeviceLocked(std::unique_ptr<Device> device) REQUIRES(mLock);
666 void assignDescriptorLocked(InputDeviceIdentifier& identifier) REQUIRES(mLock);
Prabir Pradhancb42b472022-08-23 16:01:19 +0000667 std::shared_ptr<const AssociatedDevice> obtainAssociatedDeviceLocked(
668 const std::filesystem::path& devicePath) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800669
Chris Yed3fef462021-03-07 17:10:08 -0800670 void closeDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock);
671 void closeVideoDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock);
672 void closeDeviceLocked(Device& device) REQUIRES(mLock);
673 void closeAllDevicesLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800674
Siarhei Vishniakou25920312018-12-12 15:24:44 -0800675 status_t registerFdForEpoll(int fd);
676 status_t unregisterFdFromEpoll(int fd);
Chris Yed3fef462021-03-07 17:10:08 -0800677 status_t registerDeviceForEpollLocked(Device& device) REQUIRES(mLock);
678 void registerVideoDeviceForEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock);
679 status_t unregisterDeviceFromEpollLocked(Device& device) REQUIRES(mLock);
680 void unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700681
Chris Yed3fef462021-03-07 17:10:08 -0800682 status_t scanDirLocked(const std::string& dirname) REQUIRES(mLock);
683 status_t scanVideoDirLocked(const std::string& dirname) REQUIRES(mLock);
684 void scanDevicesLocked() REQUIRES(mLock);
Prabir Pradhan952e65b2022-06-23 17:49:55 +0000685 base::Result<void> readNotifyLocked() REQUIRES(mLock);
686 void handleNotifyEventLocked(const inotify_event&) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800687
Chris Yed3fef462021-03-07 17:10:08 -0800688 Device* getDeviceLocked(int32_t deviceId) const REQUIRES(mLock);
689 Device* getDeviceByPathLocked(const std::string& devicePath) const REQUIRES(mLock);
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700690 /**
691 * Look through all available fd's (both for input devices and for video devices),
692 * and return the device pointer.
693 */
Chris Yed3fef462021-03-07 17:10:08 -0800694 Device* getDeviceByFdLocked(int fd) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800695
Chris Yed3fef462021-03-07 17:10:08 -0800696 int32_t getNextControllerNumberLocked(const std::string& name) REQUIRES(mLock);
Josh Bartel938632f2022-07-19 15:34:22 -0500697
698 bool hasDeviceWithDescriptorLocked(const std::string& descriptor) const REQUIRES(mLock);
699
Chris Yed3fef462021-03-07 17:10:08 -0800700 void releaseControllerNumberLocked(int32_t num) REQUIRES(mLock);
701 void reportDeviceAddedForStatisticsLocked(const InputDeviceIdentifier& identifier,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700702 ftl::Flags<InputDeviceClass> classes) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800703
Chris Yee2b1e5c2021-03-10 22:45:12 -0800704 const std::unordered_map<int32_t, RawBatteryInfo>& getBatteryInfoLocked(int32_t deviceId) const
705 REQUIRES(mLock);
706
707 const std::unordered_map<int32_t, RawLightInfo>& getLightInfoLocked(int32_t deviceId) const
708 REQUIRES(mLock);
709
Usama Arifb27c8e62021-06-03 16:44:09 +0100710 void addDeviceInputInotify();
711 void addDeviceInotify();
712
Michael Wrightd02c5b62014-02-10 15:10:22 -0800713 // Protect all internal state.
Chris Ye1c2e0892020-11-30 21:41:44 -0800714 mutable std::mutex mLock;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800715
716 // The actual id of the built-in keyboard, or NO_BUILT_IN_KEYBOARD if none.
717 // EventHub remaps the built-in keyboard to id 0 externally as required by the API.
718 enum {
719 // Must not conflict with any other assigned device ids, including
720 // the virtual keyboard id (-1).
721 NO_BUILT_IN_KEYBOARD = -2,
722 };
723 int32_t mBuiltInKeyboardId;
724
725 int32_t mNextDeviceId;
726
727 BitSet32 mControllerNumbers;
728
Chris Ye989bb932020-07-04 16:18:59 -0700729 std::unordered_map<int32_t, std::unique_ptr<Device>> mDevices;
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800730 /**
731 * Video devices that report touchscreen heatmap, but have not (yet) been paired
732 * with a specific input device. Video device discovery is independent from input device
733 * discovery, so the two types of devices could be found in any order.
734 * Ideally, video devices in this queue do not have an open fd, or at least aren't
735 * actively streaming.
736 */
737 std::vector<std::unique_ptr<TouchVideoDevice>> mUnattachedVideoDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800738
Chris Ye989bb932020-07-04 16:18:59 -0700739 std::vector<std::unique_ptr<Device>> mOpeningDevices;
740 std::vector<std::unique_ptr<Device>> mClosingDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800741
742 bool mNeedToSendFinishedDeviceScan;
743 bool mNeedToReopenDevices;
744 bool mNeedToScanDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100745 std::vector<std::string> mExcludedDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800746
747 int mEpollFd;
748 int mINotifyFd;
749 int mWakeReadPipeFd;
750 int mWakeWritePipeFd;
751
Usama Arifb27c8e62021-06-03 16:44:09 +0100752 int mDeviceInputWd;
753 int mDeviceWd = -1;
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800754
Michael Wrightd02c5b62014-02-10 15:10:22 -0800755 // Maximum number of signalled FDs to handle at a time.
756 static const int EPOLL_MAX_EVENTS = 16;
757
758 // The array of pending epoll events and the index of the next event to be handled.
759 struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS];
760 size_t mPendingEventCount;
761 size_t mPendingEventIndex;
762 bool mPendingINotify;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800763};
764
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700765} // namespace android