blob: 20612c767c9f5cde5faf817ae6598f14c05a1a3f [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;
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000391
392 /* Sysfs node changed. Reopen the Eventhub device if any new Peripheral like Light, Battery,
393 * etc. is detected. */
394 virtual void sysfsNodeChanged(const std::string& sysfsNodePath) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800395};
396
Chris Ye66fbac32020-07-06 20:36:43 -0700397template <std::size_t BITS>
398class BitArray {
399 /* Array element type and vector of element type. */
400 using Element = std::uint32_t;
401 /* Number of bits in each BitArray element. */
402 static constexpr size_t WIDTH = sizeof(Element) * CHAR_BIT;
403 /* Number of elements to represent a bit array of the specified size of bits. */
404 static constexpr size_t COUNT = (BITS + WIDTH - 1) / WIDTH;
405
406public:
407 /* BUFFER type declaration for BitArray */
408 using Buffer = std::array<Element, COUNT>;
409 /* To tell if a bit is set in array, it selects an element from the array, and test
410 * if the relevant bit set.
411 * Note the parameter "bit" is an index to the bit, 0 <= bit < BITS.
412 */
413 inline bool test(size_t bit) const {
414 return (bit < BITS) ? mData[bit / WIDTH].test(bit % WIDTH) : false;
415 }
416 /* Returns total number of bytes needed for the array */
417 inline size_t bytes() { return (BITS + CHAR_BIT - 1) / CHAR_BIT; }
418 /* Returns true if array contains any non-zero bit from the range defined by start and end
419 * bit index [startIndex, endIndex).
420 */
421 bool any(size_t startIndex, size_t endIndex) {
422 if (startIndex >= endIndex || startIndex > BITS || endIndex > BITS + 1) {
423 ALOGE("Invalid start/end index. start = %zu, end = %zu, total bits = %zu", startIndex,
424 endIndex, BITS);
425 return false;
426 }
427 size_t se = startIndex / WIDTH; // Start of element
428 size_t ee = endIndex / WIDTH; // End of element
429 size_t si = startIndex % WIDTH; // Start index in start element
430 size_t ei = endIndex % WIDTH; // End index in end element
431 // Need to check first unaligned bitset for any non zero bit
432 if (si > 0) {
433 size_t nBits = se == ee ? ei - si : WIDTH - si;
434 // Generate the mask of interested bit range
435 Element mask = ((1 << nBits) - 1) << si;
436 if (mData[se++].to_ulong() & mask) {
437 return true;
438 }
439 }
440 // Check whole bitset for any bit set
441 for (; se < ee; se++) {
442 if (mData[se].any()) {
443 return true;
444 }
445 }
446 // Need to check last unaligned bitset for any non zero bit
447 if (ei > 0 && se <= ee) {
448 // Generate the mask of interested bit range
449 Element mask = (1 << ei) - 1;
450 if (mData[se].to_ulong() & mask) {
451 return true;
452 }
453 }
454 return false;
455 }
456 /* Load bit array values from buffer */
457 void loadFromBuffer(const Buffer& buffer) {
458 for (size_t i = 0; i < COUNT; i++) {
459 mData[i] = std::bitset<WIDTH>(buffer[i]);
460 }
461 }
462
463private:
464 std::array<std::bitset<WIDTH>, COUNT> mData;
465};
466
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700467class EventHub : public EventHubInterface {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800468public:
469 EventHub();
470
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700471 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800472
Chris Ye989bb932020-07-04 16:18:59 -0700473 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800474
Chris Ye989bb932020-07-04 16:18:59 -0700475 int32_t getDeviceControllerNumber(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800476
Harry Cuttsc34f7582023-03-07 16:23:30 +0000477 std::optional<PropertyMap> getConfiguration(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800478
Chris Ye989bb932020-07-04 16:18:59 -0700479 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
480 RawAbsoluteAxisInfo* outAxisInfo) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800481
Chris Ye989bb932020-07-04 16:18:59 -0700482 bool hasRelativeAxis(int32_t deviceId, int axis) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800483
Chris Ye989bb932020-07-04 16:18:59 -0700484 bool hasInputProperty(int32_t deviceId, int property) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800485
Chris Yef59a2f42020-10-16 12:55:26 -0700486 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final;
487
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000488 void addKeyRemapping(int32_t deviceId, int32_t fromKeyCode,
489 int32_t toKeyCode) const override final;
490
Chris Ye989bb932020-07-04 16:18:59 -0700491 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
492 int32_t* outKeycode, int32_t* outMetaState,
493 uint32_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800494
Chris Ye989bb932020-07-04 16:18:59 -0700495 status_t mapAxis(int32_t deviceId, int32_t scanCode,
496 AxisInfo* outAxisInfo) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800497
Chris Yef59a2f42020-10-16 12:55:26 -0700498 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000499 int32_t deviceId, int32_t absCode) const override final;
Chris Yef59a2f42020-10-16 12:55:26 -0700500
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000501 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override final;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800502 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000503 int32_t BatteryId) const override final;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800504
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000505 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800506
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000507 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId,
508 int32_t lightId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800509
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000510 std::optional<int32_t> getLightBrightness(int32_t deviceId,
511 int32_t lightId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800512 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override final;
513 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000514 int32_t deviceId, int32_t lightId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800515 void setLightIntensities(int32_t deviceId, int32_t lightId,
516 std::unordered_map<LightColor, int32_t> intensities) override final;
517
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000518 std::optional<RawLayoutInfo> getRawLayoutInfo(int32_t deviceId) const override final;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000519
Chris Ye989bb932020-07-04 16:18:59 -0700520 void setExcludedDevices(const std::vector<std::string>& devices) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800521
Chris Ye989bb932020-07-04 16:18:59 -0700522 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override final;
523 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override final;
524 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override final;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100525 int32_t getKeyCodeForKeyLocation(int32_t deviceId,
526 int32_t locationKeyCode) const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700527 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
528 int32_t* outValue) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800529
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700530 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Chris Ye989bb932020-07-04 16:18:59 -0700531 uint8_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800532
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700533 std::vector<RawEvent> getEvents(int timeoutMillis) override final;
Chris Ye989bb932020-07-04 16:18:59 -0700534 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800535
Chris Ye989bb932020-07-04 16:18:59 -0700536 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override final;
Arthur Hungcb40a002021-08-03 14:31:01 +0000537 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700538 bool hasLed(int32_t deviceId, int32_t led) const override final;
539 void setLedState(int32_t deviceId, int32_t led, bool on) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800540
Chris Ye989bb932020-07-04 16:18:59 -0700541 void getVirtualKeyDefinitions(
542 int32_t deviceId,
543 std::vector<VirtualKeyDefinition>& outVirtualKeys) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800544
Chris Ye3a1e4462020-08-12 10:13:15 -0700545 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(
546 int32_t deviceId) const override final;
547 bool setKeyboardLayoutOverlay(int32_t deviceId,
548 std::shared_ptr<KeyCharacterMap> map) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800549
Chris Ye989bb932020-07-04 16:18:59 -0700550 void vibrate(int32_t deviceId, const VibrationElement& effect) override final;
551 void cancelVibrate(int32_t deviceId) override final;
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000552 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800553
Chris Ye989bb932020-07-04 16:18:59 -0700554 void requestReopenDevices() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800555
Chris Ye989bb932020-07-04 16:18:59 -0700556 void wake() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800557
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000558 void dump(std::string& dump) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800559
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000560 void monitor() const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700561
Chris Yee2b1e5c2021-03-10 22:45:12 -0800562 std::optional<int32_t> getBatteryCapacity(int32_t deviceId,
563 int32_t batteryId) const override final;
Kim Low03ea0352020-11-06 12:45:07 -0800564
Chris Yee2b1e5c2021-03-10 22:45:12 -0800565 std::optional<int32_t> getBatteryStatus(int32_t deviceId,
566 int32_t batteryId) const override final;
Kim Low03ea0352020-11-06 12:45:07 -0800567
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000568 bool isDeviceEnabled(int32_t deviceId) const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700569
570 status_t enableDevice(int32_t deviceId) override final;
571
572 status_t disableDevice(int32_t deviceId) override final;
573
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000574 void sysfsNodeChanged(const std::string& sysfsNodePath) override final;
575
Chris Ye989bb932020-07-04 16:18:59 -0700576 ~EventHub() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800577
578private:
Prabir Pradhancb42b472022-08-23 16:01:19 +0000579 // Holds information about the sysfs device associated with the Device.
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700580 struct AssociatedDevice {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800581 // The sysfs root path of the misc device.
582 std::filesystem::path sysfsRootPath;
Prabir Pradhancb42b472022-08-23 16:01:19 +0000583 std::unordered_map<int32_t /*batteryId*/, RawBatteryInfo> batteryInfos;
584 std::unordered_map<int32_t /*lightId*/, RawLightInfo> lightInfos;
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000585 std::optional<RawLayoutInfo> layoutInfo;
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000586
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000587 bool isChanged() const;
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000588 bool operator==(const AssociatedDevice&) const = default;
589 bool operator!=(const AssociatedDevice&) const = default;
590 std::string dump() const;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800591 };
592
Michael Wrightd02c5b62014-02-10 15:10:22 -0800593 struct Device {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700594 int fd; // may be -1 if device is closed
Michael Wrightd02c5b62014-02-10 15:10:22 -0800595 const int32_t id;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100596 const std::string path;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800597 const InputDeviceIdentifier identifier;
598
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -0800599 std::unique_ptr<TouchVideoDevice> videoDevice;
600
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700601 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800602
Chris Ye66fbac32020-07-06 20:36:43 -0700603 BitArray<KEY_MAX> keyBitmask;
604 BitArray<KEY_MAX> keyState;
605 BitArray<ABS_MAX> absBitmask;
606 BitArray<REL_MAX> relBitmask;
607 BitArray<SW_MAX> swBitmask;
608 BitArray<SW_MAX> swState;
609 BitArray<LED_MAX> ledBitmask;
610 BitArray<FF_MAX> ffBitmask;
611 BitArray<INPUT_PROP_MAX> propBitmask;
Chris Yef59a2f42020-10-16 12:55:26 -0700612 BitArray<MSC_MAX> mscBitmask;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800613
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100614 std::string configurationFile;
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500615 std::unique_ptr<PropertyMap> configuration;
Siarhei Vishniakou3e78dec2019-02-20 16:21:46 -0600616 std::unique_ptr<VirtualKeyMap> virtualKeyMap;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800617 KeyMap keyMap;
618
Michael Wrightd02c5b62014-02-10 15:10:22 -0800619 bool ffEffectPlaying;
620 int16_t ffEffectId; // initially -1
621
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700622 // A shared_ptr of a device associated with the input device.
Prabir Pradhan51894782022-08-23 16:29:10 +0000623 // The input devices that have the same sysfs path have the same associated device.
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000624 std::shared_ptr<const AssociatedDevice> associatedDevice;
Kim Low03ea0352020-11-06 12:45:07 -0800625
Michael Wrightd02c5b62014-02-10 15:10:22 -0800626 int32_t controllerNumber;
627
Prabir Pradhancb42b472022-08-23 16:01:19 +0000628 Device(int fd, int32_t id, std::string path, InputDeviceIdentifier identifier,
629 std::shared_ptr<const AssociatedDevice> assocDev);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800630 ~Device();
631
632 void close();
633
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700634 bool enabled; // initially true
635 status_t enable();
636 status_t disable();
Chris Ye989bb932020-07-04 16:18:59 -0700637 bool hasValidFd() const;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700638 const bool isVirtual; // set if fd < 0 is passed to constructor
Michael Wrightd02c5b62014-02-10 15:10:22 -0800639
Chris Ye3a1e4462020-08-12 10:13:15 -0700640 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const;
Chris Ye66fbac32020-07-06 20:36:43 -0700641
642 template <std::size_t N>
Chris Ye989bb932020-07-04 16:18:59 -0700643 status_t readDeviceBitMask(unsigned long ioctlCode, BitArray<N>& bitArray);
Chris Ye66fbac32020-07-06 20:36:43 -0700644
Chris Ye989bb932020-07-04 16:18:59 -0700645 void configureFd();
646 bool hasKeycodeLocked(int keycode) const;
647 void loadConfigurationLocked();
648 bool loadVirtualKeyMapLocked();
649 status_t loadKeyMapLocked();
650 bool isExternalDeviceLocked();
651 bool deviceHasMicLocked();
652 void setLedForControllerLocked();
653 status_t mapLed(int32_t led, int32_t* outScanCode) const;
654 void setLedStateLocked(int32_t led, bool on);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800655 };
656
Siarhei Vishniakoua4c502a2021-02-05 00:45:20 +0000657 /**
658 * Create a new device for the provided path.
659 */
Chris Yed3fef462021-03-07 17:10:08 -0800660 void openDeviceLocked(const std::string& devicePath) REQUIRES(mLock);
661 void openVideoDeviceLocked(const std::string& devicePath) REQUIRES(mLock);
Siarhei Vishniakouf49608d2020-08-20 19:18:21 -0500662 /**
663 * Try to associate a video device with an input device. If the association succeeds,
664 * the videoDevice is moved into the input device. 'videoDevice' will become null if this
665 * happens.
666 * Return true if the association succeeds.
667 * Return false otherwise.
668 */
Chris Yed3fef462021-03-07 17:10:08 -0800669 bool tryAddVideoDeviceLocked(Device& device, std::unique_ptr<TouchVideoDevice>& videoDevice)
670 REQUIRES(mLock);
671 void createVirtualKeyboardLocked() REQUIRES(mLock);
672 void addDeviceLocked(std::unique_ptr<Device> device) REQUIRES(mLock);
673 void assignDescriptorLocked(InputDeviceIdentifier& identifier) REQUIRES(mLock);
Prabir Pradhancb42b472022-08-23 16:01:19 +0000674 std::shared_ptr<const AssociatedDevice> obtainAssociatedDeviceLocked(
675 const std::filesystem::path& devicePath) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800676
Chris Yed3fef462021-03-07 17:10:08 -0800677 void closeDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock);
678 void closeVideoDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock);
679 void closeDeviceLocked(Device& device) REQUIRES(mLock);
680 void closeAllDevicesLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800681
Siarhei Vishniakou25920312018-12-12 15:24:44 -0800682 status_t registerFdForEpoll(int fd);
683 status_t unregisterFdFromEpoll(int fd);
Chris Yed3fef462021-03-07 17:10:08 -0800684 status_t registerDeviceForEpollLocked(Device& device) REQUIRES(mLock);
685 void registerVideoDeviceForEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock);
686 status_t unregisterDeviceFromEpollLocked(Device& device) REQUIRES(mLock);
687 void unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700688
Chris Yed3fef462021-03-07 17:10:08 -0800689 status_t scanDirLocked(const std::string& dirname) REQUIRES(mLock);
690 status_t scanVideoDirLocked(const std::string& dirname) REQUIRES(mLock);
691 void scanDevicesLocked() REQUIRES(mLock);
Prabir Pradhan952e65b2022-06-23 17:49:55 +0000692 base::Result<void> readNotifyLocked() REQUIRES(mLock);
693 void handleNotifyEventLocked(const inotify_event&) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800694
Chris Yed3fef462021-03-07 17:10:08 -0800695 Device* getDeviceLocked(int32_t deviceId) const REQUIRES(mLock);
696 Device* getDeviceByPathLocked(const std::string& devicePath) const REQUIRES(mLock);
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700697 /**
698 * Look through all available fd's (both for input devices and for video devices),
699 * and return the device pointer.
700 */
Chris Yed3fef462021-03-07 17:10:08 -0800701 Device* getDeviceByFdLocked(int fd) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800702
Chris Yed3fef462021-03-07 17:10:08 -0800703 int32_t getNextControllerNumberLocked(const std::string& name) REQUIRES(mLock);
Josh Bartel938632f2022-07-19 15:34:22 -0500704
705 bool hasDeviceWithDescriptorLocked(const std::string& descriptor) const REQUIRES(mLock);
706
Chris Yed3fef462021-03-07 17:10:08 -0800707 void releaseControllerNumberLocked(int32_t num) REQUIRES(mLock);
708 void reportDeviceAddedForStatisticsLocked(const InputDeviceIdentifier& identifier,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700709 ftl::Flags<InputDeviceClass> classes) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800710
Chris Yee2b1e5c2021-03-10 22:45:12 -0800711 const std::unordered_map<int32_t, RawBatteryInfo>& getBatteryInfoLocked(int32_t deviceId) const
712 REQUIRES(mLock);
713
714 const std::unordered_map<int32_t, RawLightInfo>& getLightInfoLocked(int32_t deviceId) const
715 REQUIRES(mLock);
716
Usama Arifb27c8e62021-06-03 16:44:09 +0100717 void addDeviceInputInotify();
718 void addDeviceInotify();
719
Michael Wrightd02c5b62014-02-10 15:10:22 -0800720 // Protect all internal state.
Chris Ye1c2e0892020-11-30 21:41:44 -0800721 mutable std::mutex mLock;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800722
723 // The actual id of the built-in keyboard, or NO_BUILT_IN_KEYBOARD if none.
724 // EventHub remaps the built-in keyboard to id 0 externally as required by the API.
725 enum {
726 // Must not conflict with any other assigned device ids, including
727 // the virtual keyboard id (-1).
728 NO_BUILT_IN_KEYBOARD = -2,
729 };
730 int32_t mBuiltInKeyboardId;
731
732 int32_t mNextDeviceId;
733
734 BitSet32 mControllerNumbers;
735
Chris Ye989bb932020-07-04 16:18:59 -0700736 std::unordered_map<int32_t, std::unique_ptr<Device>> mDevices;
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800737 /**
738 * Video devices that report touchscreen heatmap, but have not (yet) been paired
739 * with a specific input device. Video device discovery is independent from input device
740 * discovery, so the two types of devices could be found in any order.
741 * Ideally, video devices in this queue do not have an open fd, or at least aren't
742 * actively streaming.
743 */
744 std::vector<std::unique_ptr<TouchVideoDevice>> mUnattachedVideoDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800745
Chris Ye989bb932020-07-04 16:18:59 -0700746 std::vector<std::unique_ptr<Device>> mOpeningDevices;
747 std::vector<std::unique_ptr<Device>> mClosingDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800748
749 bool mNeedToSendFinishedDeviceScan;
750 bool mNeedToReopenDevices;
751 bool mNeedToScanDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100752 std::vector<std::string> mExcludedDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800753
754 int mEpollFd;
755 int mINotifyFd;
756 int mWakeReadPipeFd;
757 int mWakeWritePipeFd;
758
Usama Arifb27c8e62021-06-03 16:44:09 +0100759 int mDeviceInputWd;
760 int mDeviceWd = -1;
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800761
Michael Wrightd02c5b62014-02-10 15:10:22 -0800762 // Maximum number of signalled FDs to handle at a time.
763 static const int EPOLL_MAX_EVENTS = 16;
764
765 // The array of pending epoll events and the index of the next event to be handled.
766 struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS];
767 size_t mPendingEventCount;
768 size_t mPendingEventIndex;
769 bool mPendingINotify;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800770};
771
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700772} // namespace android