blob: a7e06756d027087679dc2ef83f0672a8d6f75979 [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>
Prabir Pradhan955b6132023-07-14 19:44:34 +000022#include <functional>
23#include <map>
Harry Cuttsea73eaa2023-01-16 17:55:46 +000024#include <ostream>
25#include <string>
Chris Ye989bb932020-07-04 16:18:59 -070026#include <unordered_map>
Prabir Pradhan51894782022-08-23 16:29:10 +000027#include <utility>
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010028#include <vector>
29
Kim Low03ea0352020-11-06 12:45:07 -080030#include <batteryservice/BatteryService.h>
Dominik Laskowski2f01d772022-03-23 16:01:29 -070031#include <ftl/flags.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080032#include <input/Input.h>
33#include <input/InputDevice.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080034#include <input/KeyCharacterMap.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070035#include <input/KeyLayoutMap.h>
36#include <input/Keyboard.h>
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070037#include <input/PropertyMap.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080038#include <input/VirtualKeyMap.h>
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000039#include <linux/input.h>
40#include <sys/epoll.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080041#include <utils/BitSet.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070042#include <utils/Errors.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070043#include <utils/List.h>
44#include <utils/Log.h>
45#include <utils/Mutex.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080046
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080047#include "TouchVideoDevice.h"
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000048#include "VibrationElement.h"
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080049
Prabir Pradhan952e65b2022-06-23 17:49:55 +000050struct inotify_event;
51
Michael Wrightd02c5b62014-02-10 15:10:22 -080052namespace android {
53
Chris Ye3fdbfef2021-01-06 18:45:18 -080054/* Number of colors : {red, green, blue} */
55static constexpr size_t COLOR_NUM = 3;
Michael Wrightd02c5b62014-02-10 15:10:22 -080056/*
57 * A raw event as retrieved from the EventHub.
58 */
59struct RawEvent {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000060 // Time when the event happened
Michael Wrightd02c5b62014-02-10 15:10:22 -080061 nsecs_t when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000062 // Time when the event was read by EventHub. Only populated for input events.
63 // For other events (device added/removed/etc), this value is undefined and should not be read.
64 nsecs_t readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -080065 int32_t deviceId;
66 int32_t type;
67 int32_t code;
68 int32_t value;
69};
70
71/* Describes an absolute axis. */
72struct RawAbsoluteAxisInfo {
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000073 bool valid{false}; // true if the information is valid, false otherwise
Michael Wrightd02c5b62014-02-10 15:10:22 -080074
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000075 int32_t minValue{}; // minimum value
76 int32_t maxValue{}; // maximum value
77 int32_t flat{}; // center flat position, eg. flat == 8 means center is between -8 and 8
78 int32_t fuzz{}; // error tolerance, eg. fuzz == 4 means value is +/- 4 due to noise
79 int32_t resolution{}; // resolution in units per mm or radians per mm
Michael Wrightd02c5b62014-02-10 15:10:22 -080080
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000081 inline void clear() { *this = RawAbsoluteAxisInfo(); }
Michael Wrightd02c5b62014-02-10 15:10:22 -080082};
83
Harry Cuttsea73eaa2023-01-16 17:55:46 +000084std::ostream& operator<<(std::ostream& out, const RawAbsoluteAxisInfo& info);
85
Michael Wrightd02c5b62014-02-10 15:10:22 -080086/*
87 * Input device classes.
88 */
Chris Ye1b0c7342020-07-28 21:57:03 -070089enum class InputDeviceClass : uint32_t {
Michael Wrightd02c5b62014-02-10 15:10:22 -080090 /* The input device is a keyboard or has buttons. */
Chris Ye1b0c7342020-07-28 21:57:03 -070091 KEYBOARD = 0x00000001,
Michael Wrightd02c5b62014-02-10 15:10:22 -080092
93 /* The input device is an alpha-numeric keyboard (not just a dial pad). */
Chris Ye1b0c7342020-07-28 21:57:03 -070094 ALPHAKEY = 0x00000002,
Michael Wrightd02c5b62014-02-10 15:10:22 -080095
96 /* The input device is a touchscreen or a touchpad (either single-touch or multi-touch). */
Chris Ye1b0c7342020-07-28 21:57:03 -070097 TOUCH = 0x00000004,
Michael Wrightd02c5b62014-02-10 15:10:22 -080098
99 /* The input device is a cursor device such as a trackball or mouse. */
Chris Ye1b0c7342020-07-28 21:57:03 -0700100 CURSOR = 0x00000008,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800101
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000102 /* The input device is a multi-touch touchscreen or touchpad. */
Chris Ye1b0c7342020-07-28 21:57:03 -0700103 TOUCH_MT = 0x00000010,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800104
105 /* The input device is a directional pad (implies keyboard, has DPAD keys). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700106 DPAD = 0x00000020,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800107
108 /* The input device is a gamepad (implies keyboard, has BUTTON keys). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700109 GAMEPAD = 0x00000040,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800110
111 /* The input device has switches. */
Chris Ye1b0c7342020-07-28 21:57:03 -0700112 SWITCH = 0x00000080,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800113
114 /* The input device is a joystick (implies gamepad, has joystick absolute axes). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700115 JOYSTICK = 0x00000100,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800116
117 /* The input device has a vibrator (supports FF_RUMBLE). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700118 VIBRATOR = 0x00000200,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800119
Tim Kilbourn063ff532015-04-08 10:26:18 -0700120 /* The input device has a microphone. */
Chris Ye1b0c7342020-07-28 21:57:03 -0700121 MIC = 0x00000400,
Tim Kilbourn063ff532015-04-08 10:26:18 -0700122
Michael Wright842500e2015-03-13 17:32:02 -0700123 /* The input device is an external stylus (has data we want to fuse with touch data). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700124 EXTERNAL_STYLUS = 0x00000800,
Michael Wright842500e2015-03-13 17:32:02 -0700125
Prashant Malani1941ff52015-08-11 18:29:28 -0700126 /* The input device has a rotary encoder */
Chris Ye1b0c7342020-07-28 21:57:03 -0700127 ROTARY_ENCODER = 0x00001000,
Prashant Malani1941ff52015-08-11 18:29:28 -0700128
Chris Yef59a2f42020-10-16 12:55:26 -0700129 /* The input device has a sensor like accelerometer, gyro, etc */
130 SENSOR = 0x00002000,
131
Kim Low03ea0352020-11-06 12:45:07 -0800132 /* The input device has a battery */
133 BATTERY = 0x00004000,
134
Chris Ye3fdbfef2021-01-06 18:45:18 -0800135 /* The input device has sysfs controllable lights */
136 LIGHT = 0x00008000,
137
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000138 /* The input device is a touchpad, requiring an on-screen cursor. */
139 TOUCHPAD = 0x00010000,
140
Michael Wrightd02c5b62014-02-10 15:10:22 -0800141 /* The input device is virtual (not a real device, not part of UI configuration). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700142 VIRTUAL = 0x40000000,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800143
144 /* The input device is external (not built-in). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700145 EXTERNAL = 0x80000000,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800146};
147
Chris Ye3fdbfef2021-01-06 18:45:18 -0800148enum class SysfsClass : uint32_t {
149 POWER_SUPPLY = 0,
150 LEDS = 1,
Dominik Laskowski75788452021-02-09 18:51:25 -0800151
152 ftl_last = LEDS
Chris Ye3fdbfef2021-01-06 18:45:18 -0800153};
154
155enum class LightColor : uint32_t {
156 RED = 0,
157 GREEN = 1,
158 BLUE = 2,
159};
160
161enum class InputLightClass : uint32_t {
162 /* The input light has brightness node. */
163 BRIGHTNESS = 0x00000001,
164 /* The input light has red name. */
165 RED = 0x00000002,
166 /* The input light has green name. */
167 GREEN = 0x00000004,
168 /* The input light has blue name. */
169 BLUE = 0x00000008,
170 /* The input light has global name. */
171 GLOBAL = 0x00000010,
172 /* The input light has multi index node. */
173 MULTI_INDEX = 0x00000020,
174 /* The input light has multi intensity node. */
175 MULTI_INTENSITY = 0x00000040,
176 /* The input light has max brightness node. */
177 MAX_BRIGHTNESS = 0x00000080,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +0000178 /* The input light has kbd_backlight name */
179 KEYBOARD_BACKLIGHT = 0x00000100,
Chris Ye3fdbfef2021-01-06 18:45:18 -0800180};
181
Chris Yee2b1e5c2021-03-10 22:45:12 -0800182enum class InputBatteryClass : uint32_t {
183 /* The input device battery has capacity node. */
184 CAPACITY = 0x00000001,
185 /* The input device battery has capacity_level node. */
186 CAPACITY_LEVEL = 0x00000002,
187 /* The input device battery has status node. */
188 STATUS = 0x00000004,
189};
190
Chris Ye3fdbfef2021-01-06 18:45:18 -0800191/* Describes a raw light. */
192struct RawLightInfo {
193 int32_t id;
194 std::string name;
195 std::optional<int32_t> maxBrightness;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700196 ftl::Flags<InputLightClass> flags;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800197 std::array<int32_t, COLOR_NUM> rgbIndex;
198 std::filesystem::path path;
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000199
200 bool operator==(const RawLightInfo&) const = default;
201 bool operator!=(const RawLightInfo&) const = default;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800202};
203
Chris Yee2b1e5c2021-03-10 22:45:12 -0800204/* Describes a raw battery. */
205struct RawBatteryInfo {
206 int32_t id;
207 std::string name;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700208 ftl::Flags<InputBatteryClass> flags;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800209 std::filesystem::path path;
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000210
211 bool operator==(const RawBatteryInfo&) const = default;
212 bool operator!=(const RawBatteryInfo&) const = default;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800213};
214
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000215/* Layout information associated with the device */
216struct RawLayoutInfo {
217 std::string languageTag;
218 std::string layoutType;
219
220 bool operator==(const RawLayoutInfo&) const = default;
221 bool operator!=(const RawLayoutInfo&) const = default;
222};
223
Michael Wrightd02c5b62014-02-10 15:10:22 -0800224/*
225 * Gets the class that owns an axis, in cases where multiple classes might claim
226 * the same axis for different purposes.
227 */
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700228extern ftl::Flags<InputDeviceClass> getAbsAxisUsage(int32_t axis,
229 ftl::Flags<InputDeviceClass> deviceClasses);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800230
231/*
232 * Grand Central Station for events.
233 *
234 * The event hub aggregates input events received across all known input
235 * devices on the system, including devices that may be emulated by the simulator
236 * environment. In addition, the event hub generates fake input events to indicate
237 * when devices are added or removed.
238 *
239 * The event hub provides a stream of input events (via the getEvent function).
240 * It also supports querying the current actual state of input devices such as identifying
241 * which keys are currently down. Finally, the event hub keeps track of the capabilities of
242 * individual input devices, such as their class and the set of key codes that they support.
243 */
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700244class EventHubInterface {
245public:
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700246 EventHubInterface() {}
247 virtual ~EventHubInterface() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800248
Michael Wrightd02c5b62014-02-10 15:10:22 -0800249 // Synthetic raw event type codes produced when devices are added or removed.
250 enum {
251 // Sent when a device is added.
252 DEVICE_ADDED = 0x10000000,
253 // Sent when a device is removed.
254 DEVICE_REMOVED = 0x20000000,
255 // Sent when all added/removed devices from the most recent scan have been reported.
256 // This event is always sent at least once.
257 FINISHED_DEVICE_SCAN = 0x30000000,
258
259 FIRST_SYNTHETIC_EVENT = DEVICE_ADDED,
260 };
261
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700262 virtual ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800263
264 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const = 0;
265
266 virtual int32_t getDeviceControllerNumber(int32_t deviceId) const = 0;
267
Harry Cuttsc34f7582023-03-07 16:23:30 +0000268 /**
269 * Get the PropertyMap for the provided EventHub device, if available.
270 * This acquires the device lock, so a copy is returned rather than the raw pointer
271 * to the device's PropertyMap. A std::nullopt may be returned if the device could
272 * not be found, or if it doesn't have any configuration.
273 */
274 virtual std::optional<PropertyMap> getConfiguration(int32_t deviceId) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800275
276 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700277 RawAbsoluteAxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800278
279 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const = 0;
280
281 virtual bool hasInputProperty(int32_t deviceId, int property) const = 0;
282
Chris Yef59a2f42020-10-16 12:55:26 -0700283 virtual bool hasMscEvent(int32_t deviceId, int mscEvent) const = 0;
284
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000285 virtual void addKeyRemapping(int32_t deviceId, int32_t fromKeyCode,
286 int32_t toKeyCode) const = 0;
287
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700288 virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
289 int32_t metaState, int32_t* outKeycode, int32_t* outMetaState,
290 uint32_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800291
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700292 virtual status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800293
294 // Sets devices that are excluded from opening.
295 // This can be used to ignore input devices for sensors.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100296 virtual void setExcludedDevices(const std::vector<std::string>& devices) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800297
298 /*
299 * Wait for events to become available and returns them.
300 * After returning, the EventHub holds onto a wake lock until the next call to getEvent.
301 * This ensures that the device will not go to sleep while the event is being processed.
302 * If the device needs to remain awake longer than that, then the caller is responsible
303 * for taking care of it (say, by poking the power manager user activity timer).
304 *
305 * The timeout is advisory only. If the device is asleep, it will not wake just to
306 * service the timeout.
307 *
308 * Returns the number of events obtained, or 0 if the timeout expired.
309 */
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700310 virtual std::vector<RawEvent> getEvents(int timeoutMillis) = 0;
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800311 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) = 0;
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000312 virtual base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
313 int32_t deviceId, int32_t absCode) const = 0;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800314 // Raw batteries are sysfs power_supply nodes we found from the EventHub device sysfs node,
315 // containing the raw info of the sysfs node structure.
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000316 virtual std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const = 0;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800317 virtual std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000318 int32_t BatteryId) const = 0;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800319
Chris Ye3fdbfef2021-01-06 18:45:18 -0800320 // Raw lights are sysfs led light nodes we found from the EventHub device sysfs node,
321 // containing the raw info of the sysfs node structure.
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000322 virtual std::vector<int32_t> getRawLightIds(int32_t deviceId) const = 0;
323 virtual std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId,
324 int32_t lightId) const = 0;
325 virtual std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const = 0;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800326 virtual void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) = 0;
327 virtual std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000328 int32_t deviceId, int32_t lightId) const = 0;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800329 virtual void setLightIntensities(int32_t deviceId, int32_t lightId,
330 std::unordered_map<LightColor, int32_t> intensities) = 0;
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000331 /* Query Layout info associated with the input device. */
332 virtual std::optional<RawLayoutInfo> getRawLayoutInfo(int32_t deviceId) const = 0;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000333 /* Query current input state. */
Michael Wrightd02c5b62014-02-10 15:10:22 -0800334 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0;
335 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0;
336 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0;
337 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700338 int32_t* outValue) const = 0;
Arpit Singh4b4a4572023-11-24 18:19:56 +0000339 /* Query Multi-Touch slot values for an axis. Returns error or an 1 indexed array of size
340 * (slotCount + 1). The value at the 0 index is set to queried axis. */
341 virtual base::Result<std::vector<int32_t>> getMtSlotValues(int32_t deviceId, int32_t axis,
342 size_t slotCount) const = 0;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100343 virtual int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344
345 /*
346 * Examine key input devices for specific framework keycode support
347 */
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700348 virtual bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700349 uint8_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800350
351 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const = 0;
Arthur Hungcb40a002021-08-03 14:31:01 +0000352 virtual bool hasKeyCode(int32_t deviceId, int32_t keyCode) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800353
354 /* LED related functions expect Android LED constants, not scan codes or HID usages */
355 virtual bool hasLed(int32_t deviceId, int32_t led) const = 0;
356 virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0;
357
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700358 virtual void getVirtualKeyDefinitions(
359 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800360
Chris Ye3a1e4462020-08-12 10:13:15 -0700361 virtual const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const = 0;
362 virtual bool setKeyboardLayoutOverlay(int32_t deviceId,
363 std::shared_ptr<KeyCharacterMap> map) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800364
365 /* Control the vibrator. */
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000366 virtual void vibrate(int32_t deviceId, const VibrationElement& effect) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800367 virtual void cancelVibrate(int32_t deviceId) = 0;
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000368 virtual std::vector<int32_t> getVibratorIds(int32_t deviceId) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800369
Kim Low03ea0352020-11-06 12:45:07 -0800370 /* Query battery level. */
Chris Yee2b1e5c2021-03-10 22:45:12 -0800371 virtual std::optional<int32_t> getBatteryCapacity(int32_t deviceId,
372 int32_t batteryId) const = 0;
Kim Low03ea0352020-11-06 12:45:07 -0800373
374 /* Query battery status. */
Chris Yee2b1e5c2021-03-10 22:45:12 -0800375 virtual std::optional<int32_t> getBatteryStatus(int32_t deviceId, int32_t batteryId) const = 0;
Kim Low03ea0352020-11-06 12:45:07 -0800376
Michael Wrightd02c5b62014-02-10 15:10:22 -0800377 /* Requests the EventHub to reopen all input devices on the next call to getEvents(). */
378 virtual void requestReopenDevices() = 0;
379
380 /* Wakes up getEvents() if it is blocked on a read. */
381 virtual void wake() = 0;
382
383 /* Dump EventHub state to a string. */
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000384 virtual void dump(std::string& dump) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800385
386 /* Called by the heatbeat to ensures that the reader has not deadlocked. */
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000387 virtual void monitor() const = 0;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700388
389 /* Return true if the device is enabled. */
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000390 virtual bool isDeviceEnabled(int32_t deviceId) const = 0;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700391
392 /* Enable an input device */
393 virtual status_t enableDevice(int32_t deviceId) = 0;
394
395 /* Disable an input device. Closes file descriptor to that device. */
396 virtual status_t disableDevice(int32_t deviceId) = 0;
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000397
398 /* Sysfs node changed. Reopen the Eventhub device if any new Peripheral like Light, Battery,
399 * etc. is detected. */
400 virtual void sysfsNodeChanged(const std::string& sysfsNodePath) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800401};
402
Chris Ye66fbac32020-07-06 20:36:43 -0700403template <std::size_t BITS>
404class BitArray {
405 /* Array element type and vector of element type. */
406 using Element = std::uint32_t;
407 /* Number of bits in each BitArray element. */
408 static constexpr size_t WIDTH = sizeof(Element) * CHAR_BIT;
409 /* Number of elements to represent a bit array of the specified size of bits. */
410 static constexpr size_t COUNT = (BITS + WIDTH - 1) / WIDTH;
411
412public:
413 /* BUFFER type declaration for BitArray */
414 using Buffer = std::array<Element, COUNT>;
415 /* To tell if a bit is set in array, it selects an element from the array, and test
416 * if the relevant bit set.
417 * Note the parameter "bit" is an index to the bit, 0 <= bit < BITS.
418 */
419 inline bool test(size_t bit) const {
Prabir Pradhan9762ac92023-07-13 21:45:12 +0000420 return (bit < BITS) && mData[bit / WIDTH].test(bit % WIDTH);
421 }
422 /* Sets the given bit in the bit array to given value.
423 * Returns true if the given bit is a valid index and thus was set successfully.
424 */
425 inline bool set(size_t bit, bool value) {
426 if (bit >= BITS) {
427 return false;
428 }
429 mData[bit / WIDTH].set(bit % WIDTH, value);
430 return true;
Chris Ye66fbac32020-07-06 20:36:43 -0700431 }
432 /* Returns total number of bytes needed for the array */
433 inline size_t bytes() { return (BITS + CHAR_BIT - 1) / CHAR_BIT; }
434 /* Returns true if array contains any non-zero bit from the range defined by start and end
435 * bit index [startIndex, endIndex).
436 */
437 bool any(size_t startIndex, size_t endIndex) {
438 if (startIndex >= endIndex || startIndex > BITS || endIndex > BITS + 1) {
439 ALOGE("Invalid start/end index. start = %zu, end = %zu, total bits = %zu", startIndex,
440 endIndex, BITS);
441 return false;
442 }
443 size_t se = startIndex / WIDTH; // Start of element
444 size_t ee = endIndex / WIDTH; // End of element
445 size_t si = startIndex % WIDTH; // Start index in start element
446 size_t ei = endIndex % WIDTH; // End index in end element
447 // Need to check first unaligned bitset for any non zero bit
448 if (si > 0) {
449 size_t nBits = se == ee ? ei - si : WIDTH - si;
450 // Generate the mask of interested bit range
451 Element mask = ((1 << nBits) - 1) << si;
452 if (mData[se++].to_ulong() & mask) {
453 return true;
454 }
455 }
456 // Check whole bitset for any bit set
457 for (; se < ee; se++) {
458 if (mData[se].any()) {
459 return true;
460 }
461 }
462 // Need to check last unaligned bitset for any non zero bit
463 if (ei > 0 && se <= ee) {
464 // Generate the mask of interested bit range
465 Element mask = (1 << ei) - 1;
466 if (mData[se].to_ulong() & mask) {
467 return true;
468 }
469 }
470 return false;
471 }
472 /* Load bit array values from buffer */
473 void loadFromBuffer(const Buffer& buffer) {
474 for (size_t i = 0; i < COUNT; i++) {
475 mData[i] = std::bitset<WIDTH>(buffer[i]);
476 }
477 }
Prabir Pradhan955b6132023-07-14 19:44:34 +0000478 /* Dump the indices in the bit array that are set. */
479 inline std::string dumpSetIndices(std::string separator,
480 std::function<std::string(size_t /*index*/)> format) {
481 std::string dmp;
482 for (size_t i = 0; i < BITS; i++) {
483 if (test(i)) {
484 if (!dmp.empty()) {
485 dmp += separator;
486 }
487 dmp += format(i);
488 }
489 }
490 return dmp.empty() ? "<none>" : dmp;
491 }
Chris Ye66fbac32020-07-06 20:36:43 -0700492
493private:
494 std::array<std::bitset<WIDTH>, COUNT> mData;
495};
496
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700497class EventHub : public EventHubInterface {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800498public:
499 EventHub();
500
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700501 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800502
Chris Ye989bb932020-07-04 16:18:59 -0700503 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800504
Chris Ye989bb932020-07-04 16:18:59 -0700505 int32_t getDeviceControllerNumber(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800506
Harry Cuttsc34f7582023-03-07 16:23:30 +0000507 std::optional<PropertyMap> getConfiguration(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800508
Chris Ye989bb932020-07-04 16:18:59 -0700509 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
510 RawAbsoluteAxisInfo* outAxisInfo) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800511
Chris Ye989bb932020-07-04 16:18:59 -0700512 bool hasRelativeAxis(int32_t deviceId, int axis) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800513
Chris Ye989bb932020-07-04 16:18:59 -0700514 bool hasInputProperty(int32_t deviceId, int property) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800515
Chris Yef59a2f42020-10-16 12:55:26 -0700516 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final;
517
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000518 void addKeyRemapping(int32_t deviceId, int32_t fromKeyCode,
519 int32_t toKeyCode) const override final;
520
Chris Ye989bb932020-07-04 16:18:59 -0700521 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
522 int32_t* outKeycode, int32_t* outMetaState,
523 uint32_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800524
Chris Ye989bb932020-07-04 16:18:59 -0700525 status_t mapAxis(int32_t deviceId, int32_t scanCode,
526 AxisInfo* outAxisInfo) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800527
Chris Yef59a2f42020-10-16 12:55:26 -0700528 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000529 int32_t deviceId, int32_t absCode) const override final;
Chris Yef59a2f42020-10-16 12:55:26 -0700530
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000531 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override final;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800532 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000533 int32_t BatteryId) const override final;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800534
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000535 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800536
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000537 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId,
538 int32_t lightId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800539
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000540 std::optional<int32_t> getLightBrightness(int32_t deviceId,
541 int32_t lightId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800542 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override final;
543 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000544 int32_t deviceId, int32_t lightId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800545 void setLightIntensities(int32_t deviceId, int32_t lightId,
546 std::unordered_map<LightColor, int32_t> intensities) override final;
547
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000548 std::optional<RawLayoutInfo> getRawLayoutInfo(int32_t deviceId) const override final;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000549
Chris Ye989bb932020-07-04 16:18:59 -0700550 void setExcludedDevices(const std::vector<std::string>& devices) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800551
Chris Ye989bb932020-07-04 16:18:59 -0700552 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override final;
553 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override final;
554 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override final;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100555 int32_t getKeyCodeForKeyLocation(int32_t deviceId,
556 int32_t locationKeyCode) const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700557 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
558 int32_t* outValue) const override final;
Arpit Singh4b4a4572023-11-24 18:19:56 +0000559 base::Result<std::vector<int32_t>> getMtSlotValues(int32_t deviceId, int32_t axis,
560 size_t slotCount) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800561
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700562 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Chris Ye989bb932020-07-04 16:18:59 -0700563 uint8_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800564
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700565 std::vector<RawEvent> getEvents(int timeoutMillis) override final;
Chris Ye989bb932020-07-04 16:18:59 -0700566 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800567
Chris Ye989bb932020-07-04 16:18:59 -0700568 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override final;
Arthur Hungcb40a002021-08-03 14:31:01 +0000569 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700570 bool hasLed(int32_t deviceId, int32_t led) const override final;
571 void setLedState(int32_t deviceId, int32_t led, bool on) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800572
Chris Ye989bb932020-07-04 16:18:59 -0700573 void getVirtualKeyDefinitions(
574 int32_t deviceId,
575 std::vector<VirtualKeyDefinition>& outVirtualKeys) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800576
Chris Ye3a1e4462020-08-12 10:13:15 -0700577 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(
578 int32_t deviceId) const override final;
579 bool setKeyboardLayoutOverlay(int32_t deviceId,
580 std::shared_ptr<KeyCharacterMap> map) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800581
Chris Ye989bb932020-07-04 16:18:59 -0700582 void vibrate(int32_t deviceId, const VibrationElement& effect) override final;
583 void cancelVibrate(int32_t deviceId) override final;
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000584 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800585
Chris Ye989bb932020-07-04 16:18:59 -0700586 void requestReopenDevices() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800587
Chris Ye989bb932020-07-04 16:18:59 -0700588 void wake() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800589
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000590 void dump(std::string& dump) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800591
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000592 void monitor() const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700593
Chris Yee2b1e5c2021-03-10 22:45:12 -0800594 std::optional<int32_t> getBatteryCapacity(int32_t deviceId,
595 int32_t batteryId) const override final;
Kim Low03ea0352020-11-06 12:45:07 -0800596
Chris Yee2b1e5c2021-03-10 22:45:12 -0800597 std::optional<int32_t> getBatteryStatus(int32_t deviceId,
598 int32_t batteryId) const override final;
Kim Low03ea0352020-11-06 12:45:07 -0800599
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000600 bool isDeviceEnabled(int32_t deviceId) const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700601
602 status_t enableDevice(int32_t deviceId) override final;
603
604 status_t disableDevice(int32_t deviceId) override final;
605
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000606 void sysfsNodeChanged(const std::string& sysfsNodePath) override final;
607
Chris Ye989bb932020-07-04 16:18:59 -0700608 ~EventHub() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800609
610private:
Prabir Pradhancb42b472022-08-23 16:01:19 +0000611 // Holds information about the sysfs device associated with the Device.
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700612 struct AssociatedDevice {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800613 // The sysfs root path of the misc device.
614 std::filesystem::path sysfsRootPath;
Prabir Pradhancb42b472022-08-23 16:01:19 +0000615 std::unordered_map<int32_t /*batteryId*/, RawBatteryInfo> batteryInfos;
616 std::unordered_map<int32_t /*lightId*/, RawLightInfo> lightInfos;
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000617 std::optional<RawLayoutInfo> layoutInfo;
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000618
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000619 bool isChanged() const;
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000620 bool operator==(const AssociatedDevice&) const = default;
621 bool operator!=(const AssociatedDevice&) const = default;
622 std::string dump() const;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800623 };
624
Michael Wrightd02c5b62014-02-10 15:10:22 -0800625 struct Device {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700626 int fd; // may be -1 if device is closed
Michael Wrightd02c5b62014-02-10 15:10:22 -0800627 const int32_t id;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100628 const std::string path;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800629 const InputDeviceIdentifier identifier;
630
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -0800631 std::unique_ptr<TouchVideoDevice> videoDevice;
632
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700633 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800634
Prabir Pradhanf44ab482023-07-20 20:46:09 +0000635 BitArray<KEY_CNT> keyBitmask;
636 BitArray<KEY_CNT> keyState;
637 BitArray<REL_CNT> relBitmask;
638 BitArray<SW_CNT> swBitmask;
639 BitArray<SW_CNT> swState;
640 BitArray<LED_CNT> ledBitmask;
641 BitArray<FF_CNT> ffBitmask;
642 BitArray<INPUT_PROP_CNT> propBitmask;
643 BitArray<MSC_CNT> mscBitmask;
644 BitArray<ABS_CNT> absBitmask;
Prabir Pradhan341d0782023-07-14 19:04:10 +0000645 struct AxisState {
646 RawAbsoluteAxisInfo info;
647 int value;
648 };
Prabir Pradhan955b6132023-07-14 19:44:34 +0000649 std::map<int /*axis*/, AxisState> absState;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800650
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100651 std::string configurationFile;
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500652 std::unique_ptr<PropertyMap> configuration;
Siarhei Vishniakou3e78dec2019-02-20 16:21:46 -0600653 std::unique_ptr<VirtualKeyMap> virtualKeyMap;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800654 KeyMap keyMap;
655
Michael Wrightd02c5b62014-02-10 15:10:22 -0800656 bool ffEffectPlaying;
657 int16_t ffEffectId; // initially -1
658
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700659 // A shared_ptr of a device associated with the input device.
Prabir Pradhan51894782022-08-23 16:29:10 +0000660 // The input devices that have the same sysfs path have the same associated device.
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000661 std::shared_ptr<const AssociatedDevice> associatedDevice;
Kim Low03ea0352020-11-06 12:45:07 -0800662
Michael Wrightd02c5b62014-02-10 15:10:22 -0800663 int32_t controllerNumber;
664
Prabir Pradhancb42b472022-08-23 16:01:19 +0000665 Device(int fd, int32_t id, std::string path, InputDeviceIdentifier identifier,
666 std::shared_ptr<const AssociatedDevice> assocDev);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800667 ~Device();
668
669 void close();
670
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700671 bool enabled; // initially true
672 status_t enable();
673 status_t disable();
Chris Ye989bb932020-07-04 16:18:59 -0700674 bool hasValidFd() const;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700675 const bool isVirtual; // set if fd < 0 is passed to constructor
Michael Wrightd02c5b62014-02-10 15:10:22 -0800676
Chris Ye3a1e4462020-08-12 10:13:15 -0700677 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const;
Chris Ye66fbac32020-07-06 20:36:43 -0700678
679 template <std::size_t N>
Chris Ye989bb932020-07-04 16:18:59 -0700680 status_t readDeviceBitMask(unsigned long ioctlCode, BitArray<N>& bitArray);
Chris Ye66fbac32020-07-06 20:36:43 -0700681
Chris Ye989bb932020-07-04 16:18:59 -0700682 void configureFd();
Prabir Pradhan341d0782023-07-14 19:04:10 +0000683 void populateAbsoluteAxisStates();
Chris Ye989bb932020-07-04 16:18:59 -0700684 bool hasKeycodeLocked(int keycode) const;
685 void loadConfigurationLocked();
686 bool loadVirtualKeyMapLocked();
687 status_t loadKeyMapLocked();
688 bool isExternalDeviceLocked();
689 bool deviceHasMicLocked();
690 void setLedForControllerLocked();
691 status_t mapLed(int32_t led, int32_t* outScanCode) const;
692 void setLedStateLocked(int32_t led, bool on);
Prabir Pradhan9762ac92023-07-13 21:45:12 +0000693
694 bool currentFrameDropped;
695 void trackInputEvent(const struct input_event& event);
696 void readDeviceState();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800697 };
698
Siarhei Vishniakoua4c502a2021-02-05 00:45:20 +0000699 /**
700 * Create a new device for the provided path.
701 */
Chris Yed3fef462021-03-07 17:10:08 -0800702 void openDeviceLocked(const std::string& devicePath) REQUIRES(mLock);
703 void openVideoDeviceLocked(const std::string& devicePath) REQUIRES(mLock);
Siarhei Vishniakouf49608d2020-08-20 19:18:21 -0500704 /**
705 * Try to associate a video device with an input device. If the association succeeds,
706 * the videoDevice is moved into the input device. 'videoDevice' will become null if this
707 * happens.
708 * Return true if the association succeeds.
709 * Return false otherwise.
710 */
Chris Yed3fef462021-03-07 17:10:08 -0800711 bool tryAddVideoDeviceLocked(Device& device, std::unique_ptr<TouchVideoDevice>& videoDevice)
712 REQUIRES(mLock);
713 void createVirtualKeyboardLocked() REQUIRES(mLock);
714 void addDeviceLocked(std::unique_ptr<Device> device) REQUIRES(mLock);
715 void assignDescriptorLocked(InputDeviceIdentifier& identifier) REQUIRES(mLock);
Prabir Pradhancb42b472022-08-23 16:01:19 +0000716 std::shared_ptr<const AssociatedDevice> obtainAssociatedDeviceLocked(
717 const std::filesystem::path& devicePath) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800718
Chris Yed3fef462021-03-07 17:10:08 -0800719 void closeDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock);
720 void closeVideoDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock);
721 void closeDeviceLocked(Device& device) REQUIRES(mLock);
722 void closeAllDevicesLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800723
Siarhei Vishniakou25920312018-12-12 15:24:44 -0800724 status_t registerFdForEpoll(int fd);
725 status_t unregisterFdFromEpoll(int fd);
Chris Yed3fef462021-03-07 17:10:08 -0800726 status_t registerDeviceForEpollLocked(Device& device) REQUIRES(mLock);
727 void registerVideoDeviceForEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock);
728 status_t unregisterDeviceFromEpollLocked(Device& device) REQUIRES(mLock);
729 void unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700730
Chris Yed3fef462021-03-07 17:10:08 -0800731 status_t scanDirLocked(const std::string& dirname) REQUIRES(mLock);
732 status_t scanVideoDirLocked(const std::string& dirname) REQUIRES(mLock);
733 void scanDevicesLocked() REQUIRES(mLock);
Prabir Pradhan952e65b2022-06-23 17:49:55 +0000734 base::Result<void> readNotifyLocked() REQUIRES(mLock);
735 void handleNotifyEventLocked(const inotify_event&) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800736
Chris Yed3fef462021-03-07 17:10:08 -0800737 Device* getDeviceLocked(int32_t deviceId) const REQUIRES(mLock);
738 Device* getDeviceByPathLocked(const std::string& devicePath) const REQUIRES(mLock);
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700739 /**
740 * Look through all available fd's (both for input devices and for video devices),
741 * and return the device pointer.
742 */
Chris Yed3fef462021-03-07 17:10:08 -0800743 Device* getDeviceByFdLocked(int fd) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800744
Chris Yed3fef462021-03-07 17:10:08 -0800745 int32_t getNextControllerNumberLocked(const std::string& name) REQUIRES(mLock);
Josh Bartel938632f2022-07-19 15:34:22 -0500746
747 bool hasDeviceWithDescriptorLocked(const std::string& descriptor) const REQUIRES(mLock);
748
Chris Yed3fef462021-03-07 17:10:08 -0800749 void releaseControllerNumberLocked(int32_t num) REQUIRES(mLock);
750 void reportDeviceAddedForStatisticsLocked(const InputDeviceIdentifier& identifier,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700751 ftl::Flags<InputDeviceClass> classes) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800752
Chris Yee2b1e5c2021-03-10 22:45:12 -0800753 const std::unordered_map<int32_t, RawBatteryInfo>& getBatteryInfoLocked(int32_t deviceId) const
754 REQUIRES(mLock);
755
756 const std::unordered_map<int32_t, RawLightInfo>& getLightInfoLocked(int32_t deviceId) const
757 REQUIRES(mLock);
758
Usama Arifb27c8e62021-06-03 16:44:09 +0100759 void addDeviceInputInotify();
760 void addDeviceInotify();
761
Michael Wrightd02c5b62014-02-10 15:10:22 -0800762 // Protect all internal state.
Chris Ye1c2e0892020-11-30 21:41:44 -0800763 mutable std::mutex mLock;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800764
765 // The actual id of the built-in keyboard, or NO_BUILT_IN_KEYBOARD if none.
766 // EventHub remaps the built-in keyboard to id 0 externally as required by the API.
767 enum {
768 // Must not conflict with any other assigned device ids, including
769 // the virtual keyboard id (-1).
770 NO_BUILT_IN_KEYBOARD = -2,
771 };
772 int32_t mBuiltInKeyboardId;
773
774 int32_t mNextDeviceId;
775
776 BitSet32 mControllerNumbers;
777
Chris Ye989bb932020-07-04 16:18:59 -0700778 std::unordered_map<int32_t, std::unique_ptr<Device>> mDevices;
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800779 /**
780 * Video devices that report touchscreen heatmap, but have not (yet) been paired
781 * with a specific input device. Video device discovery is independent from input device
782 * discovery, so the two types of devices could be found in any order.
783 * Ideally, video devices in this queue do not have an open fd, or at least aren't
784 * actively streaming.
785 */
786 std::vector<std::unique_ptr<TouchVideoDevice>> mUnattachedVideoDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800787
Chris Ye989bb932020-07-04 16:18:59 -0700788 std::vector<std::unique_ptr<Device>> mOpeningDevices;
789 std::vector<std::unique_ptr<Device>> mClosingDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800790
791 bool mNeedToSendFinishedDeviceScan;
792 bool mNeedToReopenDevices;
793 bool mNeedToScanDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100794 std::vector<std::string> mExcludedDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800795
796 int mEpollFd;
797 int mINotifyFd;
798 int mWakeReadPipeFd;
799 int mWakeWritePipeFd;
800
Usama Arifb27c8e62021-06-03 16:44:09 +0100801 int mDeviceInputWd;
802 int mDeviceWd = -1;
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800803
Michael Wrightd02c5b62014-02-10 15:10:22 -0800804 // Maximum number of signalled FDs to handle at a time.
805 static const int EPOLL_MAX_EVENTS = 16;
806
807 // The array of pending epoll events and the index of the next event to be handled.
808 struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS];
809 size_t mPendingEventCount;
810 size_t mPendingEventIndex;
811 bool mPendingINotify;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800812};
813
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700814} // namespace android