blob: 5839b4c41c3df0599be7049cf99036ad51c883b1 [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 Cutts207674d2024-06-06 18:53:41 +000024#include <optional>
Harry Cuttsea73eaa2023-01-16 17:55:46 +000025#include <ostream>
26#include <string>
Chris Ye989bb932020-07-04 16:18:59 -070027#include <unordered_map>
Prabir Pradhan51894782022-08-23 16:29:10 +000028#include <utility>
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010029#include <vector>
30
Kim Low03ea0352020-11-06 12:45:07 -080031#include <batteryservice/BatteryService.h>
Dominik Laskowski2f01d772022-03-23 16:01:29 -070032#include <ftl/flags.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080033#include <input/Input.h>
34#include <input/InputDevice.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080035#include <input/KeyCharacterMap.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070036#include <input/KeyLayoutMap.h>
37#include <input/Keyboard.h>
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070038#include <input/PropertyMap.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080039#include <input/VirtualKeyMap.h>
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000040#include <linux/input.h>
41#include <sys/epoll.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080042#include <utils/BitSet.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070043#include <utils/Errors.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070044#include <utils/List.h>
45#include <utils/Log.h>
46#include <utils/Mutex.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080047
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080048#include "TouchVideoDevice.h"
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000049#include "VibrationElement.h"
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080050
Prabir Pradhan952e65b2022-06-23 17:49:55 +000051struct inotify_event;
52
Michael Wrightd02c5b62014-02-10 15:10:22 -080053namespace android {
54
Chris Ye3fdbfef2021-01-06 18:45:18 -080055/* Number of colors : {red, green, blue} */
56static constexpr size_t COLOR_NUM = 3;
Michael Wrightd02c5b62014-02-10 15:10:22 -080057/*
58 * A raw event as retrieved from the EventHub.
59 */
60struct RawEvent {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000061 // Time when the event happened
Michael Wrightd02c5b62014-02-10 15:10:22 -080062 nsecs_t when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000063 // Time when the event was read by EventHub. Only populated for input events.
64 // For other events (device added/removed/etc), this value is undefined and should not be read.
65 nsecs_t readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -080066 int32_t deviceId;
67 int32_t type;
68 int32_t code;
69 int32_t value;
70};
71
72/* Describes an absolute axis. */
73struct RawAbsoluteAxisInfo {
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000074 int32_t minValue{}; // minimum value
75 int32_t maxValue{}; // maximum value
76 int32_t flat{}; // center flat position, eg. flat == 8 means center is between -8 and 8
77 int32_t fuzz{}; // error tolerance, eg. fuzz == 4 means value is +/- 4 due to noise
78 int32_t resolution{}; // resolution in units per mm or radians per mm
Michael Wrightd02c5b62014-02-10 15:10:22 -080079};
80
Prabir Pradhan132f21c2024-07-25 16:48:30 +000081std::ostream& operator<<(std::ostream& out, const std::optional<RawAbsoluteAxisInfo>& info);
Harry Cuttsea73eaa2023-01-16 17:55:46 +000082
Michael Wrightd02c5b62014-02-10 15:10:22 -080083/*
84 * Input device classes.
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +000085 *
86 * These classes are duplicated in rust side here: /frameworks/native/libs/input/rust/input.rs.
87 * If any new classes are added, we need to add them in rust input side too.
Michael Wrightd02c5b62014-02-10 15:10:22 -080088 */
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. */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +000091 KEYBOARD = android::os::IInputConstants::DEVICE_CLASS_KEYBOARD,
Michael Wrightd02c5b62014-02-10 15:10:22 -080092
93 /* The input device is an alpha-numeric keyboard (not just a dial pad). */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +000094 ALPHAKEY = android::os::IInputConstants::DEVICE_CLASS_ALPHAKEY,
Michael Wrightd02c5b62014-02-10 15:10:22 -080095
96 /* The input device is a touchscreen or a touchpad (either single-touch or multi-touch). */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +000097 TOUCH = android::os::IInputConstants::DEVICE_CLASS_TOUCH,
Michael Wrightd02c5b62014-02-10 15:10:22 -080098
99 /* The input device is a cursor device such as a trackball or mouse. */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000100 CURSOR = android::os::IInputConstants::DEVICE_CLASS_CURSOR,
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. */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000103 TOUCH_MT = android::os::IInputConstants::DEVICE_CLASS_TOUCH_MT,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800104
105 /* The input device is a directional pad (implies keyboard, has DPAD keys). */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000106 DPAD = android::os::IInputConstants::DEVICE_CLASS_DPAD,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800107
108 /* The input device is a gamepad (implies keyboard, has BUTTON keys). */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000109 GAMEPAD = android::os::IInputConstants::DEVICE_CLASS_GAMEPAD,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800110
111 /* The input device has switches. */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000112 SWITCH = android::os::IInputConstants::DEVICE_CLASS_SWITCH,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800113
114 /* The input device is a joystick (implies gamepad, has joystick absolute axes). */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000115 JOYSTICK = android::os::IInputConstants::DEVICE_CLASS_JOYSTICK,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800116
117 /* The input device has a vibrator (supports FF_RUMBLE). */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000118 VIBRATOR = android::os::IInputConstants::DEVICE_CLASS_VIBRATOR,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800119
Tim Kilbourn063ff532015-04-08 10:26:18 -0700120 /* The input device has a microphone. */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000121 MIC = android::os::IInputConstants::DEVICE_CLASS_MIC,
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). */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000124 EXTERNAL_STYLUS = android::os::IInputConstants::DEVICE_CLASS_EXTERNAL_STYLUS,
Michael Wright842500e2015-03-13 17:32:02 -0700125
Prashant Malani1941ff52015-08-11 18:29:28 -0700126 /* The input device has a rotary encoder */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000127 ROTARY_ENCODER = android::os::IInputConstants::DEVICE_CLASS_ROTARY_ENCODER,
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 */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000130 SENSOR = android::os::IInputConstants::DEVICE_CLASS_SENSOR,
Chris Yef59a2f42020-10-16 12:55:26 -0700131
Kim Low03ea0352020-11-06 12:45:07 -0800132 /* The input device has a battery */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000133 BATTERY = android::os::IInputConstants::DEVICE_CLASS_BATTERY,
Kim Low03ea0352020-11-06 12:45:07 -0800134
Chris Ye3fdbfef2021-01-06 18:45:18 -0800135 /* The input device has sysfs controllable lights */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000136 LIGHT = android::os::IInputConstants::DEVICE_CLASS_LIGHT,
Chris Ye3fdbfef2021-01-06 18:45:18 -0800137
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000138 /* The input device is a touchpad, requiring an on-screen cursor. */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000139 TOUCHPAD = android::os::IInputConstants::DEVICE_CLASS_TOUCHPAD,
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000140
Michael Wrightd02c5b62014-02-10 15:10:22 -0800141 /* The input device is virtual (not a real device, not part of UI configuration). */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000142 VIRTUAL = android::os::IInputConstants::DEVICE_CLASS_VIRTUAL,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800143
144 /* The input device is external (not built-in). */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000145 EXTERNAL = android::os::IInputConstants::DEVICE_CLASS_EXTERNAL,
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,
DingYong99f2c3c2023-12-20 15:46:06 +0800180 /* The input light has mic_mute name */
181 KEYBOARD_MIC_MUTE = 0x00000200,
DingYongffe24cc2024-08-15 08:24:02 +0000182 /* The input light has mute name */
183 KEYBOARD_VOLUME_MUTE = 0x00000400,
Chris Ye3fdbfef2021-01-06 18:45:18 -0800184};
185
Chris Yee2b1e5c2021-03-10 22:45:12 -0800186enum class InputBatteryClass : uint32_t {
187 /* The input device battery has capacity node. */
188 CAPACITY = 0x00000001,
189 /* The input device battery has capacity_level node. */
190 CAPACITY_LEVEL = 0x00000002,
191 /* The input device battery has status node. */
192 STATUS = 0x00000004,
193};
194
Chris Ye3fdbfef2021-01-06 18:45:18 -0800195/* Describes a raw light. */
196struct RawLightInfo {
197 int32_t id;
198 std::string name;
199 std::optional<int32_t> maxBrightness;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700200 ftl::Flags<InputLightClass> flags;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800201 std::array<int32_t, COLOR_NUM> rgbIndex;
202 std::filesystem::path path;
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000203
204 bool operator==(const RawLightInfo&) const = default;
205 bool operator!=(const RawLightInfo&) const = default;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800206};
207
Chris Yee2b1e5c2021-03-10 22:45:12 -0800208/* Describes a raw battery. */
209struct RawBatteryInfo {
210 int32_t id;
211 std::string name;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700212 ftl::Flags<InputBatteryClass> flags;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800213 std::filesystem::path path;
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000214
215 bool operator==(const RawBatteryInfo&) const = default;
216 bool operator!=(const RawBatteryInfo&) const = default;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800217};
218
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000219/* Layout information associated with the device */
220struct RawLayoutInfo {
221 std::string languageTag;
222 std::string layoutType;
223
224 bool operator==(const RawLayoutInfo&) const = default;
225 bool operator!=(const RawLayoutInfo&) const = default;
226};
227
Michael Wrightd02c5b62014-02-10 15:10:22 -0800228/*
229 * Gets the class that owns an axis, in cases where multiple classes might claim
230 * the same axis for different purposes.
231 */
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700232extern ftl::Flags<InputDeviceClass> getAbsAxisUsage(int32_t axis,
233 ftl::Flags<InputDeviceClass> deviceClasses);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800234
235/*
236 * Grand Central Station for events.
237 *
238 * The event hub aggregates input events received across all known input
239 * devices on the system, including devices that may be emulated by the simulator
240 * environment. In addition, the event hub generates fake input events to indicate
241 * when devices are added or removed.
242 *
243 * The event hub provides a stream of input events (via the getEvent function).
244 * It also supports querying the current actual state of input devices such as identifying
245 * which keys are currently down. Finally, the event hub keeps track of the capabilities of
246 * individual input devices, such as their class and the set of key codes that they support.
247 */
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700248class EventHubInterface {
249public:
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700250 EventHubInterface() {}
251 virtual ~EventHubInterface() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800252
Michael Wrightd02c5b62014-02-10 15:10:22 -0800253 // Synthetic raw event type codes produced when devices are added or removed.
254 enum {
255 // Sent when a device is added.
256 DEVICE_ADDED = 0x10000000,
257 // Sent when a device is removed.
258 DEVICE_REMOVED = 0x20000000,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259
260 FIRST_SYNTHETIC_EVENT = DEVICE_ADDED,
261 };
262
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700263 virtual ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800264
265 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const = 0;
266
267 virtual int32_t getDeviceControllerNumber(int32_t deviceId) const = 0;
268
Harry Cuttsc34f7582023-03-07 16:23:30 +0000269 /**
270 * Get the PropertyMap for the provided EventHub device, if available.
271 * This acquires the device lock, so a copy is returned rather than the raw pointer
272 * to the device's PropertyMap. A std::nullopt may be returned if the device could
273 * not be found, or if it doesn't have any configuration.
274 */
275 virtual std::optional<PropertyMap> getConfiguration(int32_t deviceId) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800276
Harry Cutts207674d2024-06-06 18:53:41 +0000277 virtual std::optional<RawAbsoluteAxisInfo> getAbsoluteAxisInfo(int32_t deviceId,
278 int axis) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800279
280 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const = 0;
281
282 virtual bool hasInputProperty(int32_t deviceId, int property) const = 0;
283
Chris Yef59a2f42020-10-16 12:55:26 -0700284 virtual bool hasMscEvent(int32_t deviceId, int mscEvent) const = 0;
285
Linnan Lie5657f22024-09-13 21:54:37 +0800286 virtual void setKeyRemapping(int32_t deviceId,
287 const std::map<int32_t, int32_t>& keyRemapping) const = 0;
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000288
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700289 virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
290 int32_t metaState, int32_t* outKeycode, int32_t* outMetaState,
291 uint32_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800292
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700293 virtual status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800294
295 // Sets devices that are excluded from opening.
296 // This can be used to ignore input devices for sensors.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100297 virtual void setExcludedDevices(const std::vector<std::string>& devices) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800298
299 /*
300 * Wait for events to become available and returns them.
301 * After returning, the EventHub holds onto a wake lock until the next call to getEvent.
302 * This ensures that the device will not go to sleep while the event is being processed.
303 * If the device needs to remain awake longer than that, then the caller is responsible
304 * for taking care of it (say, by poking the power manager user activity timer).
305 *
306 * The timeout is advisory only. If the device is asleep, it will not wake just to
307 * service the timeout.
308 *
309 * Returns the number of events obtained, or 0 if the timeout expired.
310 */
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700311 virtual std::vector<RawEvent> getEvents(int timeoutMillis) = 0;
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800312 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) = 0;
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000313 virtual base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
314 int32_t deviceId, int32_t absCode) const = 0;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800315 // Raw batteries are sysfs power_supply nodes we found from the EventHub device sysfs node,
316 // containing the raw info of the sysfs node structure.
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000317 virtual std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const = 0;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800318 virtual std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000319 int32_t BatteryId) const = 0;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800320
Chris Ye3fdbfef2021-01-06 18:45:18 -0800321 // Raw lights are sysfs led light nodes we found from the EventHub device sysfs node,
322 // containing the raw info of the sysfs node structure.
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000323 virtual std::vector<int32_t> getRawLightIds(int32_t deviceId) const = 0;
324 virtual std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId,
325 int32_t lightId) const = 0;
326 virtual std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const = 0;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800327 virtual void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) = 0;
328 virtual std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000329 int32_t deviceId, int32_t lightId) const = 0;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800330 virtual void setLightIntensities(int32_t deviceId, int32_t lightId,
331 std::unordered_map<LightColor, int32_t> intensities) = 0;
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000332 /* Query Layout info associated with the input device. */
333 virtual std::optional<RawLayoutInfo> getRawLayoutInfo(int32_t deviceId) const = 0;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000334 /* Query current input state. */
Michael Wrightd02c5b62014-02-10 15:10:22 -0800335 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0;
336 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0;
337 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0;
Harry Cuttse2c5e202024-06-21 17:08:48 +0000338 virtual std::optional<int32_t> getAbsoluteAxisValue(int32_t deviceId, int32_t axis) 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;
Yanye Li81a590c2024-10-22 19:25:54 +0000401
402 /* Set whether the given input device can wake up the kernel from sleep
403 * when it generates input events. By default, usually only internal (built-in)
404 * input devices can wake the kernel from sleep. For an external input device
405 * that supports remote wakeup to be able to wake the kernel, this must be called
406 * after each time the device is connected/added. */
407 virtual bool setKernelWakeEnabled(int32_t deviceId, bool enabled) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800408};
409
Chris Ye66fbac32020-07-06 20:36:43 -0700410template <std::size_t BITS>
411class BitArray {
412 /* Array element type and vector of element type. */
413 using Element = std::uint32_t;
414 /* Number of bits in each BitArray element. */
415 static constexpr size_t WIDTH = sizeof(Element) * CHAR_BIT;
416 /* Number of elements to represent a bit array of the specified size of bits. */
417 static constexpr size_t COUNT = (BITS + WIDTH - 1) / WIDTH;
418
419public:
420 /* BUFFER type declaration for BitArray */
421 using Buffer = std::array<Element, COUNT>;
422 /* To tell if a bit is set in array, it selects an element from the array, and test
423 * if the relevant bit set.
424 * Note the parameter "bit" is an index to the bit, 0 <= bit < BITS.
425 */
426 inline bool test(size_t bit) const {
Prabir Pradhan9762ac92023-07-13 21:45:12 +0000427 return (bit < BITS) && mData[bit / WIDTH].test(bit % WIDTH);
428 }
429 /* Sets the given bit in the bit array to given value.
430 * Returns true if the given bit is a valid index and thus was set successfully.
431 */
432 inline bool set(size_t bit, bool value) {
433 if (bit >= BITS) {
434 return false;
435 }
436 mData[bit / WIDTH].set(bit % WIDTH, value);
437 return true;
Chris Ye66fbac32020-07-06 20:36:43 -0700438 }
439 /* Returns total number of bytes needed for the array */
440 inline size_t bytes() { return (BITS + CHAR_BIT - 1) / CHAR_BIT; }
441 /* Returns true if array contains any non-zero bit from the range defined by start and end
442 * bit index [startIndex, endIndex).
443 */
444 bool any(size_t startIndex, size_t endIndex) {
445 if (startIndex >= endIndex || startIndex > BITS || endIndex > BITS + 1) {
446 ALOGE("Invalid start/end index. start = %zu, end = %zu, total bits = %zu", startIndex,
447 endIndex, BITS);
448 return false;
449 }
450 size_t se = startIndex / WIDTH; // Start of element
451 size_t ee = endIndex / WIDTH; // End of element
452 size_t si = startIndex % WIDTH; // Start index in start element
453 size_t ei = endIndex % WIDTH; // End index in end element
454 // Need to check first unaligned bitset for any non zero bit
455 if (si > 0) {
456 size_t nBits = se == ee ? ei - si : WIDTH - si;
457 // Generate the mask of interested bit range
458 Element mask = ((1 << nBits) - 1) << si;
459 if (mData[se++].to_ulong() & mask) {
460 return true;
461 }
462 }
463 // Check whole bitset for any bit set
464 for (; se < ee; se++) {
465 if (mData[se].any()) {
466 return true;
467 }
468 }
469 // Need to check last unaligned bitset for any non zero bit
470 if (ei > 0 && se <= ee) {
471 // Generate the mask of interested bit range
472 Element mask = (1 << ei) - 1;
473 if (mData[se].to_ulong() & mask) {
474 return true;
475 }
476 }
477 return false;
478 }
479 /* Load bit array values from buffer */
480 void loadFromBuffer(const Buffer& buffer) {
481 for (size_t i = 0; i < COUNT; i++) {
482 mData[i] = std::bitset<WIDTH>(buffer[i]);
483 }
484 }
Prabir Pradhan955b6132023-07-14 19:44:34 +0000485 /* Dump the indices in the bit array that are set. */
486 inline std::string dumpSetIndices(std::string separator,
487 std::function<std::string(size_t /*index*/)> format) {
488 std::string dmp;
489 for (size_t i = 0; i < BITS; i++) {
490 if (test(i)) {
491 if (!dmp.empty()) {
492 dmp += separator;
493 }
494 dmp += format(i);
495 }
496 }
497 return dmp.empty() ? "<none>" : dmp;
498 }
Chris Ye66fbac32020-07-06 20:36:43 -0700499
500private:
501 std::array<std::bitset<WIDTH>, COUNT> mData;
502};
503
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700504class EventHub : public EventHubInterface {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800505public:
506 EventHub();
507
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700508 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800509
Chris Ye989bb932020-07-04 16:18:59 -0700510 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800511
Chris Ye989bb932020-07-04 16:18:59 -0700512 int32_t getDeviceControllerNumber(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800513
Harry Cuttsc34f7582023-03-07 16:23:30 +0000514 std::optional<PropertyMap> getConfiguration(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800515
Harry Cutts207674d2024-06-06 18:53:41 +0000516 std::optional<RawAbsoluteAxisInfo> getAbsoluteAxisInfo(int32_t deviceId,
517 int axis) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800518
Chris Ye989bb932020-07-04 16:18:59 -0700519 bool hasRelativeAxis(int32_t deviceId, int axis) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800520
Chris Ye989bb932020-07-04 16:18:59 -0700521 bool hasInputProperty(int32_t deviceId, int property) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800522
Chris Yef59a2f42020-10-16 12:55:26 -0700523 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final;
524
Linnan Lie5657f22024-09-13 21:54:37 +0800525 void setKeyRemapping(int32_t deviceId,
526 const std::map<int32_t, int32_t>& keyRemapping) const override final;
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000527
Chris Ye989bb932020-07-04 16:18:59 -0700528 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
529 int32_t* outKeycode, int32_t* outMetaState,
530 uint32_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800531
Chris Ye989bb932020-07-04 16:18:59 -0700532 status_t mapAxis(int32_t deviceId, int32_t scanCode,
533 AxisInfo* outAxisInfo) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800534
Chris Yef59a2f42020-10-16 12:55:26 -0700535 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000536 int32_t deviceId, int32_t absCode) const override final;
Chris Yef59a2f42020-10-16 12:55:26 -0700537
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000538 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override final;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800539 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000540 int32_t BatteryId) const override final;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800541
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000542 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800543
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000544 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId,
545 int32_t lightId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800546
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000547 std::optional<int32_t> getLightBrightness(int32_t deviceId,
548 int32_t lightId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800549 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override final;
550 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000551 int32_t deviceId, int32_t lightId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800552 void setLightIntensities(int32_t deviceId, int32_t lightId,
553 std::unordered_map<LightColor, int32_t> intensities) override final;
554
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000555 std::optional<RawLayoutInfo> getRawLayoutInfo(int32_t deviceId) const override final;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000556
Chris Ye989bb932020-07-04 16:18:59 -0700557 void setExcludedDevices(const std::vector<std::string>& devices) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800558
Chris Ye989bb932020-07-04 16:18:59 -0700559 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override final;
560 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override final;
561 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override final;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100562 int32_t getKeyCodeForKeyLocation(int32_t deviceId,
563 int32_t locationKeyCode) const override final;
Harry Cuttse2c5e202024-06-21 17:08:48 +0000564 std::optional<int32_t> getAbsoluteAxisValue(int32_t deviceId,
565 int32_t axis) const override final;
Arpit Singh4b4a4572023-11-24 18:19:56 +0000566 base::Result<std::vector<int32_t>> getMtSlotValues(int32_t deviceId, int32_t axis,
567 size_t slotCount) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800568
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700569 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Chris Ye989bb932020-07-04 16:18:59 -0700570 uint8_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800571
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700572 std::vector<RawEvent> getEvents(int timeoutMillis) override final;
Chris Ye989bb932020-07-04 16:18:59 -0700573 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800574
Chris Ye989bb932020-07-04 16:18:59 -0700575 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override final;
Arthur Hungcb40a002021-08-03 14:31:01 +0000576 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700577 bool hasLed(int32_t deviceId, int32_t led) const override final;
578 void setLedState(int32_t deviceId, int32_t led, bool on) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800579
Chris Ye989bb932020-07-04 16:18:59 -0700580 void getVirtualKeyDefinitions(
581 int32_t deviceId,
582 std::vector<VirtualKeyDefinition>& outVirtualKeys) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800583
Chris Ye3a1e4462020-08-12 10:13:15 -0700584 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(
585 int32_t deviceId) const override final;
586 bool setKeyboardLayoutOverlay(int32_t deviceId,
587 std::shared_ptr<KeyCharacterMap> map) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800588
Chris Ye989bb932020-07-04 16:18:59 -0700589 void vibrate(int32_t deviceId, const VibrationElement& effect) override final;
590 void cancelVibrate(int32_t deviceId) override final;
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000591 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800592
Chris Ye989bb932020-07-04 16:18:59 -0700593 void requestReopenDevices() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800594
Chris Ye989bb932020-07-04 16:18:59 -0700595 void wake() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000597 void dump(std::string& dump) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800598
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000599 void monitor() const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700600
Chris Yee2b1e5c2021-03-10 22:45:12 -0800601 std::optional<int32_t> getBatteryCapacity(int32_t deviceId,
602 int32_t batteryId) const override final;
Kim Low03ea0352020-11-06 12:45:07 -0800603
Chris Yee2b1e5c2021-03-10 22:45:12 -0800604 std::optional<int32_t> getBatteryStatus(int32_t deviceId,
605 int32_t batteryId) const override final;
Kim Low03ea0352020-11-06 12:45:07 -0800606
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000607 bool isDeviceEnabled(int32_t deviceId) const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700608
609 status_t enableDevice(int32_t deviceId) override final;
610
611 status_t disableDevice(int32_t deviceId) override final;
612
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000613 void sysfsNodeChanged(const std::string& sysfsNodePath) override final;
614
Yanye Li81a590c2024-10-22 19:25:54 +0000615 bool setKernelWakeEnabled(int32_t deviceId, bool enabled) override final;
616
Chris Ye989bb932020-07-04 16:18:59 -0700617 ~EventHub() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800618
619private:
Prabir Pradhancb42b472022-08-23 16:01:19 +0000620 // Holds information about the sysfs device associated with the Device.
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700621 struct AssociatedDevice {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800622 // The sysfs root path of the misc device.
623 std::filesystem::path sysfsRootPath;
Prabir Pradhancb42b472022-08-23 16:01:19 +0000624 std::unordered_map<int32_t /*batteryId*/, RawBatteryInfo> batteryInfos;
625 std::unordered_map<int32_t /*lightId*/, RawLightInfo> lightInfos;
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000626 std::optional<RawLayoutInfo> layoutInfo;
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000627
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000628 bool isChanged() const;
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000629 bool operator==(const AssociatedDevice&) const = default;
630 bool operator!=(const AssociatedDevice&) const = default;
631 std::string dump() const;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800632 };
633
Michael Wrightd02c5b62014-02-10 15:10:22 -0800634 struct Device {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700635 int fd; // may be -1 if device is closed
Michael Wrightd02c5b62014-02-10 15:10:22 -0800636 const int32_t id;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100637 const std::string path;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800638 const InputDeviceIdentifier identifier;
639
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -0800640 std::unique_ptr<TouchVideoDevice> videoDevice;
641
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700642 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800643
Prabir Pradhanf44ab482023-07-20 20:46:09 +0000644 BitArray<KEY_CNT> keyBitmask;
645 BitArray<KEY_CNT> keyState;
646 BitArray<REL_CNT> relBitmask;
647 BitArray<SW_CNT> swBitmask;
648 BitArray<SW_CNT> swState;
649 BitArray<LED_CNT> ledBitmask;
650 BitArray<FF_CNT> ffBitmask;
651 BitArray<INPUT_PROP_CNT> propBitmask;
652 BitArray<MSC_CNT> mscBitmask;
653 BitArray<ABS_CNT> absBitmask;
Prabir Pradhan341d0782023-07-14 19:04:10 +0000654 struct AxisState {
655 RawAbsoluteAxisInfo info;
656 int value;
657 };
Prabir Pradhan955b6132023-07-14 19:44:34 +0000658 std::map<int /*axis*/, AxisState> absState;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800659
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100660 std::string configurationFile;
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500661 std::unique_ptr<PropertyMap> configuration;
Siarhei Vishniakou3e78dec2019-02-20 16:21:46 -0600662 std::unique_ptr<VirtualKeyMap> virtualKeyMap;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800663 KeyMap keyMap;
664
Michael Wrightd02c5b62014-02-10 15:10:22 -0800665 bool ffEffectPlaying;
666 int16_t ffEffectId; // initially -1
667
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700668 // A shared_ptr of a device associated with the input device.
Prabir Pradhan51894782022-08-23 16:29:10 +0000669 // The input devices that have the same sysfs path have the same associated device.
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000670 std::shared_ptr<const AssociatedDevice> associatedDevice;
Kim Low03ea0352020-11-06 12:45:07 -0800671
Michael Wrightd02c5b62014-02-10 15:10:22 -0800672 int32_t controllerNumber;
673
Prabir Pradhancb42b472022-08-23 16:01:19 +0000674 Device(int fd, int32_t id, std::string path, InputDeviceIdentifier identifier,
675 std::shared_ptr<const AssociatedDevice> assocDev);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800676 ~Device();
677
678 void close();
679
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700680 bool enabled; // initially true
681 status_t enable();
682 status_t disable();
Chris Ye989bb932020-07-04 16:18:59 -0700683 bool hasValidFd() const;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700684 const bool isVirtual; // set if fd < 0 is passed to constructor
Michael Wrightd02c5b62014-02-10 15:10:22 -0800685
Chris Ye3a1e4462020-08-12 10:13:15 -0700686 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const;
Chris Ye66fbac32020-07-06 20:36:43 -0700687
688 template <std::size_t N>
Chris Ye989bb932020-07-04 16:18:59 -0700689 status_t readDeviceBitMask(unsigned long ioctlCode, BitArray<N>& bitArray);
Chris Ye66fbac32020-07-06 20:36:43 -0700690
Chris Ye989bb932020-07-04 16:18:59 -0700691 void configureFd();
Prabir Pradhan341d0782023-07-14 19:04:10 +0000692 void populateAbsoluteAxisStates();
Chris Ye989bb932020-07-04 16:18:59 -0700693 bool hasKeycodeLocked(int keycode) const;
Vaibhav Devmurari5a71e882024-09-30 15:54:41 +0000694 bool hasKeycodeInternalLocked(int keycode) const;
Chris Ye989bb932020-07-04 16:18:59 -0700695 void loadConfigurationLocked();
696 bool loadVirtualKeyMapLocked();
697 status_t loadKeyMapLocked();
698 bool isExternalDeviceLocked();
699 bool deviceHasMicLocked();
700 void setLedForControllerLocked();
701 status_t mapLed(int32_t led, int32_t* outScanCode) const;
702 void setLedStateLocked(int32_t led, bool on);
Prabir Pradhan9762ac92023-07-13 21:45:12 +0000703
704 bool currentFrameDropped;
705 void trackInputEvent(const struct input_event& event);
706 void readDeviceState();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800707 };
708
Siarhei Vishniakoua4c502a2021-02-05 00:45:20 +0000709 /**
710 * Create a new device for the provided path.
711 */
Chris Yed3fef462021-03-07 17:10:08 -0800712 void openDeviceLocked(const std::string& devicePath) REQUIRES(mLock);
713 void openVideoDeviceLocked(const std::string& devicePath) REQUIRES(mLock);
Siarhei Vishniakouf49608d2020-08-20 19:18:21 -0500714 /**
715 * Try to associate a video device with an input device. If the association succeeds,
716 * the videoDevice is moved into the input device. 'videoDevice' will become null if this
717 * happens.
718 * Return true if the association succeeds.
719 * Return false otherwise.
720 */
Chris Yed3fef462021-03-07 17:10:08 -0800721 bool tryAddVideoDeviceLocked(Device& device, std::unique_ptr<TouchVideoDevice>& videoDevice)
722 REQUIRES(mLock);
723 void createVirtualKeyboardLocked() REQUIRES(mLock);
724 void addDeviceLocked(std::unique_ptr<Device> device) REQUIRES(mLock);
725 void assignDescriptorLocked(InputDeviceIdentifier& identifier) REQUIRES(mLock);
Prabir Pradhancb42b472022-08-23 16:01:19 +0000726 std::shared_ptr<const AssociatedDevice> obtainAssociatedDeviceLocked(
727 const std::filesystem::path& devicePath) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800728
Chris Yed3fef462021-03-07 17:10:08 -0800729 void closeDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock);
730 void closeVideoDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock);
731 void closeDeviceLocked(Device& device) REQUIRES(mLock);
732 void closeAllDevicesLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800733
Siarhei Vishniakou25920312018-12-12 15:24:44 -0800734 status_t registerFdForEpoll(int fd);
735 status_t unregisterFdFromEpoll(int fd);
Chris Yed3fef462021-03-07 17:10:08 -0800736 status_t registerDeviceForEpollLocked(Device& device) REQUIRES(mLock);
737 void registerVideoDeviceForEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock);
738 status_t unregisterDeviceFromEpollLocked(Device& device) REQUIRES(mLock);
739 void unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700740
Chris Yed3fef462021-03-07 17:10:08 -0800741 status_t scanDirLocked(const std::string& dirname) REQUIRES(mLock);
742 status_t scanVideoDirLocked(const std::string& dirname) REQUIRES(mLock);
743 void scanDevicesLocked() REQUIRES(mLock);
Prabir Pradhan952e65b2022-06-23 17:49:55 +0000744 base::Result<void> readNotifyLocked() REQUIRES(mLock);
745 void handleNotifyEventLocked(const inotify_event&) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800746
Chris Yed3fef462021-03-07 17:10:08 -0800747 Device* getDeviceLocked(int32_t deviceId) const REQUIRES(mLock);
748 Device* getDeviceByPathLocked(const std::string& devicePath) const REQUIRES(mLock);
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700749 /**
750 * Look through all available fd's (both for input devices and for video devices),
751 * and return the device pointer.
752 */
Chris Yed3fef462021-03-07 17:10:08 -0800753 Device* getDeviceByFdLocked(int fd) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800754
Chris Yed3fef462021-03-07 17:10:08 -0800755 int32_t getNextControllerNumberLocked(const std::string& name) REQUIRES(mLock);
Josh Bartel938632f2022-07-19 15:34:22 -0500756
757 bool hasDeviceWithDescriptorLocked(const std::string& descriptor) const REQUIRES(mLock);
758
Chris Yed3fef462021-03-07 17:10:08 -0800759 void releaseControllerNumberLocked(int32_t num) REQUIRES(mLock);
760 void reportDeviceAddedForStatisticsLocked(const InputDeviceIdentifier& identifier,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700761 ftl::Flags<InputDeviceClass> classes) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800762
Chris Yee2b1e5c2021-03-10 22:45:12 -0800763 const std::unordered_map<int32_t, RawBatteryInfo>& getBatteryInfoLocked(int32_t deviceId) const
764 REQUIRES(mLock);
765
766 const std::unordered_map<int32_t, RawLightInfo>& getLightInfoLocked(int32_t deviceId) const
767 REQUIRES(mLock);
768
Usama Arifb27c8e62021-06-03 16:44:09 +0100769 void addDeviceInputInotify();
770 void addDeviceInotify();
771
Michael Wrightd02c5b62014-02-10 15:10:22 -0800772 // Protect all internal state.
Chris Ye1c2e0892020-11-30 21:41:44 -0800773 mutable std::mutex mLock;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800774
775 // The actual id of the built-in keyboard, or NO_BUILT_IN_KEYBOARD if none.
776 // EventHub remaps the built-in keyboard to id 0 externally as required by the API.
777 enum {
778 // Must not conflict with any other assigned device ids, including
779 // the virtual keyboard id (-1).
780 NO_BUILT_IN_KEYBOARD = -2,
781 };
782 int32_t mBuiltInKeyboardId;
783
784 int32_t mNextDeviceId;
785
786 BitSet32 mControllerNumbers;
787
Chris Ye989bb932020-07-04 16:18:59 -0700788 std::unordered_map<int32_t, std::unique_ptr<Device>> mDevices;
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800789 /**
790 * Video devices that report touchscreen heatmap, but have not (yet) been paired
791 * with a specific input device. Video device discovery is independent from input device
792 * discovery, so the two types of devices could be found in any order.
793 * Ideally, video devices in this queue do not have an open fd, or at least aren't
794 * actively streaming.
795 */
796 std::vector<std::unique_ptr<TouchVideoDevice>> mUnattachedVideoDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800797
Chris Ye989bb932020-07-04 16:18:59 -0700798 std::vector<std::unique_ptr<Device>> mOpeningDevices;
799 std::vector<std::unique_ptr<Device>> mClosingDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800800
Michael Wrightd02c5b62014-02-10 15:10:22 -0800801 bool mNeedToReopenDevices;
802 bool mNeedToScanDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100803 std::vector<std::string> mExcludedDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800804
805 int mEpollFd;
806 int mINotifyFd;
807 int mWakeReadPipeFd;
808 int mWakeWritePipeFd;
809
Usama Arifb27c8e62021-06-03 16:44:09 +0100810 int mDeviceInputWd;
811 int mDeviceWd = -1;
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800812
Michael Wrightd02c5b62014-02-10 15:10:22 -0800813 // Maximum number of signalled FDs to handle at a time.
814 static const int EPOLL_MAX_EVENTS = 16;
815
816 // The array of pending epoll events and the index of the next event to be handled.
817 struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS];
818 size_t mPendingEventCount;
819 size_t mPendingEventIndex;
820 bool mPendingINotify;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800821};
822
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700823} // namespace android