blob: 7cf584df78113a44ad065448e94fbb9df782fa05 [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.
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +000088 *
89 * These classes are duplicated in rust side here: /frameworks/native/libs/input/rust/input.rs.
90 * If any new classes are added, we need to add them in rust input side too.
Michael Wrightd02c5b62014-02-10 15:10:22 -080091 */
Chris Ye1b0c7342020-07-28 21:57:03 -070092enum class InputDeviceClass : uint32_t {
Michael Wrightd02c5b62014-02-10 15:10:22 -080093 /* The input device is a keyboard or has buttons. */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +000094 KEYBOARD = android::os::IInputConstants::DEVICE_CLASS_KEYBOARD,
Michael Wrightd02c5b62014-02-10 15:10:22 -080095
96 /* The input device is an alpha-numeric keyboard (not just a dial pad). */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +000097 ALPHAKEY = android::os::IInputConstants::DEVICE_CLASS_ALPHAKEY,
Michael Wrightd02c5b62014-02-10 15:10:22 -080098
99 /* The input device is a touchscreen or a touchpad (either single-touch or multi-touch). */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000100 TOUCH = android::os::IInputConstants::DEVICE_CLASS_TOUCH,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800101
102 /* The input device is a cursor device such as a trackball or mouse. */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000103 CURSOR = android::os::IInputConstants::DEVICE_CLASS_CURSOR,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800104
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000105 /* The input device is a multi-touch touchscreen or touchpad. */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000106 TOUCH_MT = android::os::IInputConstants::DEVICE_CLASS_TOUCH_MT,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800107
108 /* The input device is a directional pad (implies keyboard, has DPAD keys). */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000109 DPAD = android::os::IInputConstants::DEVICE_CLASS_DPAD,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800110
111 /* The input device is a gamepad (implies keyboard, has BUTTON keys). */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000112 GAMEPAD = android::os::IInputConstants::DEVICE_CLASS_GAMEPAD,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800113
114 /* The input device has switches. */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000115 SWITCH = android::os::IInputConstants::DEVICE_CLASS_SWITCH,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800116
117 /* The input device is a joystick (implies gamepad, has joystick absolute axes). */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000118 JOYSTICK = android::os::IInputConstants::DEVICE_CLASS_JOYSTICK,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800119
120 /* The input device has a vibrator (supports FF_RUMBLE). */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000121 VIBRATOR = android::os::IInputConstants::DEVICE_CLASS_VIBRATOR,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800122
Tim Kilbourn063ff532015-04-08 10:26:18 -0700123 /* The input device has a microphone. */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000124 MIC = android::os::IInputConstants::DEVICE_CLASS_MIC,
Tim Kilbourn063ff532015-04-08 10:26:18 -0700125
Michael Wright842500e2015-03-13 17:32:02 -0700126 /* The input device is an external stylus (has data we want to fuse with touch data). */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000127 EXTERNAL_STYLUS = android::os::IInputConstants::DEVICE_CLASS_EXTERNAL_STYLUS,
Michael Wright842500e2015-03-13 17:32:02 -0700128
Prashant Malani1941ff52015-08-11 18:29:28 -0700129 /* The input device has a rotary encoder */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000130 ROTARY_ENCODER = android::os::IInputConstants::DEVICE_CLASS_ROTARY_ENCODER,
Prashant Malani1941ff52015-08-11 18:29:28 -0700131
Chris Yef59a2f42020-10-16 12:55:26 -0700132 /* The input device has a sensor like accelerometer, gyro, etc */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000133 SENSOR = android::os::IInputConstants::DEVICE_CLASS_SENSOR,
Chris Yef59a2f42020-10-16 12:55:26 -0700134
Kim Low03ea0352020-11-06 12:45:07 -0800135 /* The input device has a battery */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000136 BATTERY = android::os::IInputConstants::DEVICE_CLASS_BATTERY,
Kim Low03ea0352020-11-06 12:45:07 -0800137
Chris Ye3fdbfef2021-01-06 18:45:18 -0800138 /* The input device has sysfs controllable lights */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000139 LIGHT = android::os::IInputConstants::DEVICE_CLASS_LIGHT,
Chris Ye3fdbfef2021-01-06 18:45:18 -0800140
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000141 /* The input device is a touchpad, requiring an on-screen cursor. */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000142 TOUCHPAD = android::os::IInputConstants::DEVICE_CLASS_TOUCHPAD,
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000143
Michael Wrightd02c5b62014-02-10 15:10:22 -0800144 /* The input device is virtual (not a real device, not part of UI configuration). */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000145 VIRTUAL = android::os::IInputConstants::DEVICE_CLASS_VIRTUAL,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800146
147 /* The input device is external (not built-in). */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000148 EXTERNAL = android::os::IInputConstants::DEVICE_CLASS_EXTERNAL,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800149};
150
Chris Ye3fdbfef2021-01-06 18:45:18 -0800151enum class SysfsClass : uint32_t {
152 POWER_SUPPLY = 0,
153 LEDS = 1,
Dominik Laskowski75788452021-02-09 18:51:25 -0800154
155 ftl_last = LEDS
Chris Ye3fdbfef2021-01-06 18:45:18 -0800156};
157
158enum class LightColor : uint32_t {
159 RED = 0,
160 GREEN = 1,
161 BLUE = 2,
162};
163
164enum class InputLightClass : uint32_t {
165 /* The input light has brightness node. */
166 BRIGHTNESS = 0x00000001,
167 /* The input light has red name. */
168 RED = 0x00000002,
169 /* The input light has green name. */
170 GREEN = 0x00000004,
171 /* The input light has blue name. */
172 BLUE = 0x00000008,
173 /* The input light has global name. */
174 GLOBAL = 0x00000010,
175 /* The input light has multi index node. */
176 MULTI_INDEX = 0x00000020,
177 /* The input light has multi intensity node. */
178 MULTI_INTENSITY = 0x00000040,
179 /* The input light has max brightness node. */
180 MAX_BRIGHTNESS = 0x00000080,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +0000181 /* The input light has kbd_backlight name */
182 KEYBOARD_BACKLIGHT = 0x00000100,
DingYong99f2c3c2023-12-20 15:46:06 +0800183 /* The input light has mic_mute name */
184 KEYBOARD_MIC_MUTE = 0x00000200,
Chris Ye3fdbfef2021-01-06 18:45:18 -0800185};
186
Chris Yee2b1e5c2021-03-10 22:45:12 -0800187enum class InputBatteryClass : uint32_t {
188 /* The input device battery has capacity node. */
189 CAPACITY = 0x00000001,
190 /* The input device battery has capacity_level node. */
191 CAPACITY_LEVEL = 0x00000002,
192 /* The input device battery has status node. */
193 STATUS = 0x00000004,
194};
195
Chris Ye3fdbfef2021-01-06 18:45:18 -0800196/* Describes a raw light. */
197struct RawLightInfo {
198 int32_t id;
199 std::string name;
200 std::optional<int32_t> maxBrightness;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700201 ftl::Flags<InputLightClass> flags;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800202 std::array<int32_t, COLOR_NUM> rgbIndex;
203 std::filesystem::path path;
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000204
205 bool operator==(const RawLightInfo&) const = default;
206 bool operator!=(const RawLightInfo&) const = default;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800207};
208
Chris Yee2b1e5c2021-03-10 22:45:12 -0800209/* Describes a raw battery. */
210struct RawBatteryInfo {
211 int32_t id;
212 std::string name;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700213 ftl::Flags<InputBatteryClass> flags;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800214 std::filesystem::path path;
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000215
216 bool operator==(const RawBatteryInfo&) const = default;
217 bool operator!=(const RawBatteryInfo&) const = default;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800218};
219
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000220/* Layout information associated with the device */
221struct RawLayoutInfo {
222 std::string languageTag;
223 std::string layoutType;
224
225 bool operator==(const RawLayoutInfo&) const = default;
226 bool operator!=(const RawLayoutInfo&) const = default;
227};
228
Michael Wrightd02c5b62014-02-10 15:10:22 -0800229/*
230 * Gets the class that owns an axis, in cases where multiple classes might claim
231 * the same axis for different purposes.
232 */
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700233extern ftl::Flags<InputDeviceClass> getAbsAxisUsage(int32_t axis,
234 ftl::Flags<InputDeviceClass> deviceClasses);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800235
236/*
237 * Grand Central Station for events.
238 *
239 * The event hub aggregates input events received across all known input
240 * devices on the system, including devices that may be emulated by the simulator
241 * environment. In addition, the event hub generates fake input events to indicate
242 * when devices are added or removed.
243 *
244 * The event hub provides a stream of input events (via the getEvent function).
245 * It also supports querying the current actual state of input devices such as identifying
246 * which keys are currently down. Finally, the event hub keeps track of the capabilities of
247 * individual input devices, such as their class and the set of key codes that they support.
248 */
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700249class EventHubInterface {
250public:
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700251 EventHubInterface() {}
252 virtual ~EventHubInterface() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800253
Michael Wrightd02c5b62014-02-10 15:10:22 -0800254 // Synthetic raw event type codes produced when devices are added or removed.
255 enum {
256 // Sent when a device is added.
257 DEVICE_ADDED = 0x10000000,
258 // Sent when a device is removed.
259 DEVICE_REMOVED = 0x20000000,
260 // Sent when all added/removed devices from the most recent scan have been reported.
261 // This event is always sent at least once.
262 FINISHED_DEVICE_SCAN = 0x30000000,
263
264 FIRST_SYNTHETIC_EVENT = DEVICE_ADDED,
265 };
266
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700267 virtual ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800268
269 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const = 0;
270
271 virtual int32_t getDeviceControllerNumber(int32_t deviceId) const = 0;
272
Harry Cuttsc34f7582023-03-07 16:23:30 +0000273 /**
274 * Get the PropertyMap for the provided EventHub device, if available.
275 * This acquires the device lock, so a copy is returned rather than the raw pointer
276 * to the device's PropertyMap. A std::nullopt may be returned if the device could
277 * not be found, or if it doesn't have any configuration.
278 */
279 virtual std::optional<PropertyMap> getConfiguration(int32_t deviceId) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800280
281 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700282 RawAbsoluteAxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800283
284 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const = 0;
285
286 virtual bool hasInputProperty(int32_t deviceId, int property) const = 0;
287
Chris Yef59a2f42020-10-16 12:55:26 -0700288 virtual bool hasMscEvent(int32_t deviceId, int mscEvent) const = 0;
289
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000290 virtual void addKeyRemapping(int32_t deviceId, int32_t fromKeyCode,
291 int32_t toKeyCode) const = 0;
292
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700293 virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
294 int32_t metaState, int32_t* outKeycode, int32_t* outMetaState,
295 uint32_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800296
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700297 virtual status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800298
299 // Sets devices that are excluded from opening.
300 // This can be used to ignore input devices for sensors.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100301 virtual void setExcludedDevices(const std::vector<std::string>& devices) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800302
303 /*
304 * Wait for events to become available and returns them.
305 * After returning, the EventHub holds onto a wake lock until the next call to getEvent.
306 * This ensures that the device will not go to sleep while the event is being processed.
307 * If the device needs to remain awake longer than that, then the caller is responsible
308 * for taking care of it (say, by poking the power manager user activity timer).
309 *
310 * The timeout is advisory only. If the device is asleep, it will not wake just to
311 * service the timeout.
312 *
313 * Returns the number of events obtained, or 0 if the timeout expired.
314 */
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700315 virtual std::vector<RawEvent> getEvents(int timeoutMillis) = 0;
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800316 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) = 0;
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000317 virtual base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
318 int32_t deviceId, int32_t absCode) const = 0;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800319 // Raw batteries are sysfs power_supply nodes we found from the EventHub device sysfs node,
320 // containing the raw info of the sysfs node structure.
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000321 virtual std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const = 0;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800322 virtual std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000323 int32_t BatteryId) const = 0;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800324
Chris Ye3fdbfef2021-01-06 18:45:18 -0800325 // Raw lights are sysfs led light nodes we found from the EventHub device sysfs node,
326 // containing the raw info of the sysfs node structure.
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000327 virtual std::vector<int32_t> getRawLightIds(int32_t deviceId) const = 0;
328 virtual std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId,
329 int32_t lightId) const = 0;
330 virtual std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const = 0;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800331 virtual void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) = 0;
332 virtual std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000333 int32_t deviceId, int32_t lightId) const = 0;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800334 virtual void setLightIntensities(int32_t deviceId, int32_t lightId,
335 std::unordered_map<LightColor, int32_t> intensities) = 0;
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000336 /* Query Layout info associated with the input device. */
337 virtual std::optional<RawLayoutInfo> getRawLayoutInfo(int32_t deviceId) const = 0;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000338 /* Query current input state. */
Michael Wrightd02c5b62014-02-10 15:10:22 -0800339 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0;
340 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0;
341 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0;
342 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700343 int32_t* outValue) const = 0;
Arpit Singh4b4a4572023-11-24 18:19:56 +0000344 /* Query Multi-Touch slot values for an axis. Returns error or an 1 indexed array of size
345 * (slotCount + 1). The value at the 0 index is set to queried axis. */
346 virtual base::Result<std::vector<int32_t>> getMtSlotValues(int32_t deviceId, int32_t axis,
347 size_t slotCount) const = 0;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100348 virtual int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800349
350 /*
351 * Examine key input devices for specific framework keycode support
352 */
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700353 virtual bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700354 uint8_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800355
356 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const = 0;
Arthur Hungcb40a002021-08-03 14:31:01 +0000357 virtual bool hasKeyCode(int32_t deviceId, int32_t keyCode) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800358
359 /* LED related functions expect Android LED constants, not scan codes or HID usages */
360 virtual bool hasLed(int32_t deviceId, int32_t led) const = 0;
361 virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0;
362
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700363 virtual void getVirtualKeyDefinitions(
364 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800365
Chris Ye3a1e4462020-08-12 10:13:15 -0700366 virtual const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const = 0;
367 virtual bool setKeyboardLayoutOverlay(int32_t deviceId,
368 std::shared_ptr<KeyCharacterMap> map) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800369
370 /* Control the vibrator. */
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000371 virtual void vibrate(int32_t deviceId, const VibrationElement& effect) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800372 virtual void cancelVibrate(int32_t deviceId) = 0;
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000373 virtual std::vector<int32_t> getVibratorIds(int32_t deviceId) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800374
Kim Low03ea0352020-11-06 12:45:07 -0800375 /* Query battery level. */
Chris Yee2b1e5c2021-03-10 22:45:12 -0800376 virtual std::optional<int32_t> getBatteryCapacity(int32_t deviceId,
377 int32_t batteryId) const = 0;
Kim Low03ea0352020-11-06 12:45:07 -0800378
379 /* Query battery status. */
Chris Yee2b1e5c2021-03-10 22:45:12 -0800380 virtual std::optional<int32_t> getBatteryStatus(int32_t deviceId, int32_t batteryId) const = 0;
Kim Low03ea0352020-11-06 12:45:07 -0800381
Michael Wrightd02c5b62014-02-10 15:10:22 -0800382 /* Requests the EventHub to reopen all input devices on the next call to getEvents(). */
383 virtual void requestReopenDevices() = 0;
384
385 /* Wakes up getEvents() if it is blocked on a read. */
386 virtual void wake() = 0;
387
388 /* Dump EventHub state to a string. */
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000389 virtual void dump(std::string& dump) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800390
391 /* Called by the heatbeat to ensures that the reader has not deadlocked. */
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000392 virtual void monitor() const = 0;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700393
394 /* Return true if the device is enabled. */
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000395 virtual bool isDeviceEnabled(int32_t deviceId) const = 0;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700396
397 /* Enable an input device */
398 virtual status_t enableDevice(int32_t deviceId) = 0;
399
400 /* Disable an input device. Closes file descriptor to that device. */
401 virtual status_t disableDevice(int32_t deviceId) = 0;
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000402
403 /* Sysfs node changed. Reopen the Eventhub device if any new Peripheral like Light, Battery,
404 * etc. is detected. */
405 virtual void sysfsNodeChanged(const std::string& sysfsNodePath) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800406};
407
Chris Ye66fbac32020-07-06 20:36:43 -0700408template <std::size_t BITS>
409class BitArray {
410 /* Array element type and vector of element type. */
411 using Element = std::uint32_t;
412 /* Number of bits in each BitArray element. */
413 static constexpr size_t WIDTH = sizeof(Element) * CHAR_BIT;
414 /* Number of elements to represent a bit array of the specified size of bits. */
415 static constexpr size_t COUNT = (BITS + WIDTH - 1) / WIDTH;
416
417public:
418 /* BUFFER type declaration for BitArray */
419 using Buffer = std::array<Element, COUNT>;
420 /* To tell if a bit is set in array, it selects an element from the array, and test
421 * if the relevant bit set.
422 * Note the parameter "bit" is an index to the bit, 0 <= bit < BITS.
423 */
424 inline bool test(size_t bit) const {
Prabir Pradhan9762ac92023-07-13 21:45:12 +0000425 return (bit < BITS) && mData[bit / WIDTH].test(bit % WIDTH);
426 }
427 /* Sets the given bit in the bit array to given value.
428 * Returns true if the given bit is a valid index and thus was set successfully.
429 */
430 inline bool set(size_t bit, bool value) {
431 if (bit >= BITS) {
432 return false;
433 }
434 mData[bit / WIDTH].set(bit % WIDTH, value);
435 return true;
Chris Ye66fbac32020-07-06 20:36:43 -0700436 }
437 /* Returns total number of bytes needed for the array */
438 inline size_t bytes() { return (BITS + CHAR_BIT - 1) / CHAR_BIT; }
439 /* Returns true if array contains any non-zero bit from the range defined by start and end
440 * bit index [startIndex, endIndex).
441 */
442 bool any(size_t startIndex, size_t endIndex) {
443 if (startIndex >= endIndex || startIndex > BITS || endIndex > BITS + 1) {
444 ALOGE("Invalid start/end index. start = %zu, end = %zu, total bits = %zu", startIndex,
445 endIndex, BITS);
446 return false;
447 }
448 size_t se = startIndex / WIDTH; // Start of element
449 size_t ee = endIndex / WIDTH; // End of element
450 size_t si = startIndex % WIDTH; // Start index in start element
451 size_t ei = endIndex % WIDTH; // End index in end element
452 // Need to check first unaligned bitset for any non zero bit
453 if (si > 0) {
454 size_t nBits = se == ee ? ei - si : WIDTH - si;
455 // Generate the mask of interested bit range
456 Element mask = ((1 << nBits) - 1) << si;
457 if (mData[se++].to_ulong() & mask) {
458 return true;
459 }
460 }
461 // Check whole bitset for any bit set
462 for (; se < ee; se++) {
463 if (mData[se].any()) {
464 return true;
465 }
466 }
467 // Need to check last unaligned bitset for any non zero bit
468 if (ei > 0 && se <= ee) {
469 // Generate the mask of interested bit range
470 Element mask = (1 << ei) - 1;
471 if (mData[se].to_ulong() & mask) {
472 return true;
473 }
474 }
475 return false;
476 }
477 /* Load bit array values from buffer */
478 void loadFromBuffer(const Buffer& buffer) {
479 for (size_t i = 0; i < COUNT; i++) {
480 mData[i] = std::bitset<WIDTH>(buffer[i]);
481 }
482 }
Prabir Pradhan955b6132023-07-14 19:44:34 +0000483 /* Dump the indices in the bit array that are set. */
484 inline std::string dumpSetIndices(std::string separator,
485 std::function<std::string(size_t /*index*/)> format) {
486 std::string dmp;
487 for (size_t i = 0; i < BITS; i++) {
488 if (test(i)) {
489 if (!dmp.empty()) {
490 dmp += separator;
491 }
492 dmp += format(i);
493 }
494 }
495 return dmp.empty() ? "<none>" : dmp;
496 }
Chris Ye66fbac32020-07-06 20:36:43 -0700497
498private:
499 std::array<std::bitset<WIDTH>, COUNT> mData;
500};
501
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700502class EventHub : public EventHubInterface {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800503public:
504 EventHub();
505
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700506 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800507
Chris Ye989bb932020-07-04 16:18:59 -0700508 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800509
Chris Ye989bb932020-07-04 16:18:59 -0700510 int32_t getDeviceControllerNumber(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800511
Harry Cuttsc34f7582023-03-07 16:23:30 +0000512 std::optional<PropertyMap> getConfiguration(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800513
Chris Ye989bb932020-07-04 16:18:59 -0700514 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
515 RawAbsoluteAxisInfo* outAxisInfo) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800516
Chris Ye989bb932020-07-04 16:18:59 -0700517 bool hasRelativeAxis(int32_t deviceId, int axis) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800518
Chris Ye989bb932020-07-04 16:18:59 -0700519 bool hasInputProperty(int32_t deviceId, int property) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800520
Chris Yef59a2f42020-10-16 12:55:26 -0700521 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final;
522
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000523 void addKeyRemapping(int32_t deviceId, int32_t fromKeyCode,
524 int32_t toKeyCode) const override final;
525
Chris Ye989bb932020-07-04 16:18:59 -0700526 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
527 int32_t* outKeycode, int32_t* outMetaState,
528 uint32_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800529
Chris Ye989bb932020-07-04 16:18:59 -0700530 status_t mapAxis(int32_t deviceId, int32_t scanCode,
531 AxisInfo* outAxisInfo) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800532
Chris Yef59a2f42020-10-16 12:55:26 -0700533 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000534 int32_t deviceId, int32_t absCode) const override final;
Chris Yef59a2f42020-10-16 12:55:26 -0700535
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000536 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override final;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800537 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000538 int32_t BatteryId) const override final;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800539
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000540 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800541
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000542 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId,
543 int32_t lightId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800544
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000545 std::optional<int32_t> getLightBrightness(int32_t deviceId,
546 int32_t lightId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800547 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override final;
548 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000549 int32_t deviceId, int32_t lightId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800550 void setLightIntensities(int32_t deviceId, int32_t lightId,
551 std::unordered_map<LightColor, int32_t> intensities) override final;
552
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000553 std::optional<RawLayoutInfo> getRawLayoutInfo(int32_t deviceId) const override final;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000554
Chris Ye989bb932020-07-04 16:18:59 -0700555 void setExcludedDevices(const std::vector<std::string>& devices) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800556
Chris Ye989bb932020-07-04 16:18:59 -0700557 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override final;
558 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override final;
559 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override final;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100560 int32_t getKeyCodeForKeyLocation(int32_t deviceId,
561 int32_t locationKeyCode) const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700562 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
563 int32_t* outValue) const override final;
Arpit Singh4b4a4572023-11-24 18:19:56 +0000564 base::Result<std::vector<int32_t>> getMtSlotValues(int32_t deviceId, int32_t axis,
565 size_t slotCount) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800566
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700567 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Chris Ye989bb932020-07-04 16:18:59 -0700568 uint8_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800569
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700570 std::vector<RawEvent> getEvents(int timeoutMillis) override final;
Chris Ye989bb932020-07-04 16:18:59 -0700571 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800572
Chris Ye989bb932020-07-04 16:18:59 -0700573 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override final;
Arthur Hungcb40a002021-08-03 14:31:01 +0000574 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700575 bool hasLed(int32_t deviceId, int32_t led) const override final;
576 void setLedState(int32_t deviceId, int32_t led, bool on) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800577
Chris Ye989bb932020-07-04 16:18:59 -0700578 void getVirtualKeyDefinitions(
579 int32_t deviceId,
580 std::vector<VirtualKeyDefinition>& outVirtualKeys) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800581
Chris Ye3a1e4462020-08-12 10:13:15 -0700582 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(
583 int32_t deviceId) const override final;
584 bool setKeyboardLayoutOverlay(int32_t deviceId,
585 std::shared_ptr<KeyCharacterMap> map) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800586
Chris Ye989bb932020-07-04 16:18:59 -0700587 void vibrate(int32_t deviceId, const VibrationElement& effect) override final;
588 void cancelVibrate(int32_t deviceId) override final;
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000589 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800590
Chris Ye989bb932020-07-04 16:18:59 -0700591 void requestReopenDevices() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800592
Chris Ye989bb932020-07-04 16:18:59 -0700593 void wake() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800594
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000595 void dump(std::string& dump) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000597 void monitor() const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700598
Chris Yee2b1e5c2021-03-10 22:45:12 -0800599 std::optional<int32_t> getBatteryCapacity(int32_t deviceId,
600 int32_t batteryId) const override final;
Kim Low03ea0352020-11-06 12:45:07 -0800601
Chris Yee2b1e5c2021-03-10 22:45:12 -0800602 std::optional<int32_t> getBatteryStatus(int32_t deviceId,
603 int32_t batteryId) const override final;
Kim Low03ea0352020-11-06 12:45:07 -0800604
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000605 bool isDeviceEnabled(int32_t deviceId) const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700606
607 status_t enableDevice(int32_t deviceId) override final;
608
609 status_t disableDevice(int32_t deviceId) override final;
610
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000611 void sysfsNodeChanged(const std::string& sysfsNodePath) override final;
612
Chris Ye989bb932020-07-04 16:18:59 -0700613 ~EventHub() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800614
615private:
Prabir Pradhancb42b472022-08-23 16:01:19 +0000616 // Holds information about the sysfs device associated with the Device.
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700617 struct AssociatedDevice {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800618 // The sysfs root path of the misc device.
619 std::filesystem::path sysfsRootPath;
Prabir Pradhancb42b472022-08-23 16:01:19 +0000620 std::unordered_map<int32_t /*batteryId*/, RawBatteryInfo> batteryInfos;
621 std::unordered_map<int32_t /*lightId*/, RawLightInfo> lightInfos;
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000622 std::optional<RawLayoutInfo> layoutInfo;
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000623
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000624 bool isChanged() const;
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000625 bool operator==(const AssociatedDevice&) const = default;
626 bool operator!=(const AssociatedDevice&) const = default;
627 std::string dump() const;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800628 };
629
Michael Wrightd02c5b62014-02-10 15:10:22 -0800630 struct Device {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700631 int fd; // may be -1 if device is closed
Michael Wrightd02c5b62014-02-10 15:10:22 -0800632 const int32_t id;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100633 const std::string path;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800634 const InputDeviceIdentifier identifier;
635
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -0800636 std::unique_ptr<TouchVideoDevice> videoDevice;
637
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700638 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800639
Prabir Pradhanf44ab482023-07-20 20:46:09 +0000640 BitArray<KEY_CNT> keyBitmask;
641 BitArray<KEY_CNT> keyState;
642 BitArray<REL_CNT> relBitmask;
643 BitArray<SW_CNT> swBitmask;
644 BitArray<SW_CNT> swState;
645 BitArray<LED_CNT> ledBitmask;
646 BitArray<FF_CNT> ffBitmask;
647 BitArray<INPUT_PROP_CNT> propBitmask;
648 BitArray<MSC_CNT> mscBitmask;
649 BitArray<ABS_CNT> absBitmask;
Prabir Pradhan341d0782023-07-14 19:04:10 +0000650 struct AxisState {
651 RawAbsoluteAxisInfo info;
652 int value;
653 };
Prabir Pradhan955b6132023-07-14 19:44:34 +0000654 std::map<int /*axis*/, AxisState> absState;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800655
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100656 std::string configurationFile;
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500657 std::unique_ptr<PropertyMap> configuration;
Siarhei Vishniakou3e78dec2019-02-20 16:21:46 -0600658 std::unique_ptr<VirtualKeyMap> virtualKeyMap;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800659 KeyMap keyMap;
660
Michael Wrightd02c5b62014-02-10 15:10:22 -0800661 bool ffEffectPlaying;
662 int16_t ffEffectId; // initially -1
663
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700664 // A shared_ptr of a device associated with the input device.
Prabir Pradhan51894782022-08-23 16:29:10 +0000665 // The input devices that have the same sysfs path have the same associated device.
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000666 std::shared_ptr<const AssociatedDevice> associatedDevice;
Kim Low03ea0352020-11-06 12:45:07 -0800667
Michael Wrightd02c5b62014-02-10 15:10:22 -0800668 int32_t controllerNumber;
669
Prabir Pradhancb42b472022-08-23 16:01:19 +0000670 Device(int fd, int32_t id, std::string path, InputDeviceIdentifier identifier,
671 std::shared_ptr<const AssociatedDevice> assocDev);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800672 ~Device();
673
674 void close();
675
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700676 bool enabled; // initially true
677 status_t enable();
678 status_t disable();
Chris Ye989bb932020-07-04 16:18:59 -0700679 bool hasValidFd() const;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700680 const bool isVirtual; // set if fd < 0 is passed to constructor
Michael Wrightd02c5b62014-02-10 15:10:22 -0800681
Chris Ye3a1e4462020-08-12 10:13:15 -0700682 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const;
Chris Ye66fbac32020-07-06 20:36:43 -0700683
684 template <std::size_t N>
Chris Ye989bb932020-07-04 16:18:59 -0700685 status_t readDeviceBitMask(unsigned long ioctlCode, BitArray<N>& bitArray);
Chris Ye66fbac32020-07-06 20:36:43 -0700686
Chris Ye989bb932020-07-04 16:18:59 -0700687 void configureFd();
Prabir Pradhan341d0782023-07-14 19:04:10 +0000688 void populateAbsoluteAxisStates();
Chris Ye989bb932020-07-04 16:18:59 -0700689 bool hasKeycodeLocked(int keycode) const;
690 void loadConfigurationLocked();
691 bool loadVirtualKeyMapLocked();
692 status_t loadKeyMapLocked();
693 bool isExternalDeviceLocked();
694 bool deviceHasMicLocked();
695 void setLedForControllerLocked();
696 status_t mapLed(int32_t led, int32_t* outScanCode) const;
697 void setLedStateLocked(int32_t led, bool on);
Prabir Pradhan9762ac92023-07-13 21:45:12 +0000698
699 bool currentFrameDropped;
700 void trackInputEvent(const struct input_event& event);
701 void readDeviceState();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800702 };
703
Siarhei Vishniakoua4c502a2021-02-05 00:45:20 +0000704 /**
705 * Create a new device for the provided path.
706 */
Chris Yed3fef462021-03-07 17:10:08 -0800707 void openDeviceLocked(const std::string& devicePath) REQUIRES(mLock);
708 void openVideoDeviceLocked(const std::string& devicePath) REQUIRES(mLock);
Siarhei Vishniakouf49608d2020-08-20 19:18:21 -0500709 /**
710 * Try to associate a video device with an input device. If the association succeeds,
711 * the videoDevice is moved into the input device. 'videoDevice' will become null if this
712 * happens.
713 * Return true if the association succeeds.
714 * Return false otherwise.
715 */
Chris Yed3fef462021-03-07 17:10:08 -0800716 bool tryAddVideoDeviceLocked(Device& device, std::unique_ptr<TouchVideoDevice>& videoDevice)
717 REQUIRES(mLock);
718 void createVirtualKeyboardLocked() REQUIRES(mLock);
719 void addDeviceLocked(std::unique_ptr<Device> device) REQUIRES(mLock);
720 void assignDescriptorLocked(InputDeviceIdentifier& identifier) REQUIRES(mLock);
Prabir Pradhancb42b472022-08-23 16:01:19 +0000721 std::shared_ptr<const AssociatedDevice> obtainAssociatedDeviceLocked(
722 const std::filesystem::path& devicePath) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800723
Chris Yed3fef462021-03-07 17:10:08 -0800724 void closeDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock);
725 void closeVideoDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock);
726 void closeDeviceLocked(Device& device) REQUIRES(mLock);
727 void closeAllDevicesLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800728
Siarhei Vishniakou25920312018-12-12 15:24:44 -0800729 status_t registerFdForEpoll(int fd);
730 status_t unregisterFdFromEpoll(int fd);
Chris Yed3fef462021-03-07 17:10:08 -0800731 status_t registerDeviceForEpollLocked(Device& device) REQUIRES(mLock);
732 void registerVideoDeviceForEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock);
733 status_t unregisterDeviceFromEpollLocked(Device& device) REQUIRES(mLock);
734 void unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700735
Chris Yed3fef462021-03-07 17:10:08 -0800736 status_t scanDirLocked(const std::string& dirname) REQUIRES(mLock);
737 status_t scanVideoDirLocked(const std::string& dirname) REQUIRES(mLock);
738 void scanDevicesLocked() REQUIRES(mLock);
Prabir Pradhan952e65b2022-06-23 17:49:55 +0000739 base::Result<void> readNotifyLocked() REQUIRES(mLock);
740 void handleNotifyEventLocked(const inotify_event&) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800741
Chris Yed3fef462021-03-07 17:10:08 -0800742 Device* getDeviceLocked(int32_t deviceId) const REQUIRES(mLock);
743 Device* getDeviceByPathLocked(const std::string& devicePath) const REQUIRES(mLock);
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700744 /**
745 * Look through all available fd's (both for input devices and for video devices),
746 * and return the device pointer.
747 */
Chris Yed3fef462021-03-07 17:10:08 -0800748 Device* getDeviceByFdLocked(int fd) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800749
Chris Yed3fef462021-03-07 17:10:08 -0800750 int32_t getNextControllerNumberLocked(const std::string& name) REQUIRES(mLock);
Josh Bartel938632f2022-07-19 15:34:22 -0500751
752 bool hasDeviceWithDescriptorLocked(const std::string& descriptor) const REQUIRES(mLock);
753
Chris Yed3fef462021-03-07 17:10:08 -0800754 void releaseControllerNumberLocked(int32_t num) REQUIRES(mLock);
755 void reportDeviceAddedForStatisticsLocked(const InputDeviceIdentifier& identifier,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700756 ftl::Flags<InputDeviceClass> classes) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800757
Chris Yee2b1e5c2021-03-10 22:45:12 -0800758 const std::unordered_map<int32_t, RawBatteryInfo>& getBatteryInfoLocked(int32_t deviceId) const
759 REQUIRES(mLock);
760
761 const std::unordered_map<int32_t, RawLightInfo>& getLightInfoLocked(int32_t deviceId) const
762 REQUIRES(mLock);
763
Usama Arifb27c8e62021-06-03 16:44:09 +0100764 void addDeviceInputInotify();
765 void addDeviceInotify();
766
Michael Wrightd02c5b62014-02-10 15:10:22 -0800767 // Protect all internal state.
Chris Ye1c2e0892020-11-30 21:41:44 -0800768 mutable std::mutex mLock;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800769
770 // The actual id of the built-in keyboard, or NO_BUILT_IN_KEYBOARD if none.
771 // EventHub remaps the built-in keyboard to id 0 externally as required by the API.
772 enum {
773 // Must not conflict with any other assigned device ids, including
774 // the virtual keyboard id (-1).
775 NO_BUILT_IN_KEYBOARD = -2,
776 };
777 int32_t mBuiltInKeyboardId;
778
779 int32_t mNextDeviceId;
780
781 BitSet32 mControllerNumbers;
782
Chris Ye989bb932020-07-04 16:18:59 -0700783 std::unordered_map<int32_t, std::unique_ptr<Device>> mDevices;
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800784 /**
785 * Video devices that report touchscreen heatmap, but have not (yet) been paired
786 * with a specific input device. Video device discovery is independent from input device
787 * discovery, so the two types of devices could be found in any order.
788 * Ideally, video devices in this queue do not have an open fd, or at least aren't
789 * actively streaming.
790 */
791 std::vector<std::unique_ptr<TouchVideoDevice>> mUnattachedVideoDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800792
Chris Ye989bb932020-07-04 16:18:59 -0700793 std::vector<std::unique_ptr<Device>> mOpeningDevices;
794 std::vector<std::unique_ptr<Device>> mClosingDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800795
796 bool mNeedToSendFinishedDeviceScan;
797 bool mNeedToReopenDevices;
798 bool mNeedToScanDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100799 std::vector<std::string> mExcludedDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800800
801 int mEpollFd;
802 int mINotifyFd;
803 int mWakeReadPipeFd;
804 int mWakeWritePipeFd;
805
Usama Arifb27c8e62021-06-03 16:44:09 +0100806 int mDeviceInputWd;
807 int mDeviceWd = -1;
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800808
Michael Wrightd02c5b62014-02-10 15:10:22 -0800809 // Maximum number of signalled FDs to handle at a time.
810 static const int EPOLL_MAX_EVENTS = 16;
811
812 // The array of pending epoll events and the index of the next event to be handled.
813 struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS];
814 size_t mPendingEventCount;
815 size_t mPendingEventIndex;
816 bool mPendingINotify;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800817};
818
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700819} // namespace android