blob: 0ae57e3f2a98198e4825006aa2c50dc3e5cb65a7 [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 bool valid{false}; // true if the information is valid, false otherwise
Michael Wrightd02c5b62014-02-10 15:10:22 -080075
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000076 int32_t minValue{}; // minimum value
77 int32_t maxValue{}; // maximum value
78 int32_t flat{}; // center flat position, eg. flat == 8 means center is between -8 and 8
79 int32_t fuzz{}; // error tolerance, eg. fuzz == 4 means value is +/- 4 due to noise
80 int32_t resolution{}; // resolution in units per mm or radians per mm
Michael Wrightd02c5b62014-02-10 15:10:22 -080081
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000082 inline void clear() { *this = RawAbsoluteAxisInfo(); }
Michael Wrightd02c5b62014-02-10 15:10:22 -080083};
84
Harry Cuttsea73eaa2023-01-16 17:55:46 +000085std::ostream& operator<<(std::ostream& out, const RawAbsoluteAxisInfo& info);
86
Michael Wrightd02c5b62014-02-10 15:10:22 -080087/*
88 * Input device classes.
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +000089 *
90 * These classes are duplicated in rust side here: /frameworks/native/libs/input/rust/input.rs.
91 * If any new classes are added, we need to add them in rust input side too.
Michael Wrightd02c5b62014-02-10 15:10:22 -080092 */
Chris Ye1b0c7342020-07-28 21:57:03 -070093enum class InputDeviceClass : uint32_t {
Michael Wrightd02c5b62014-02-10 15:10:22 -080094 /* The input device is a keyboard or has buttons. */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +000095 KEYBOARD = android::os::IInputConstants::DEVICE_CLASS_KEYBOARD,
Michael Wrightd02c5b62014-02-10 15:10:22 -080096
97 /* The input device is an alpha-numeric keyboard (not just a dial pad). */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +000098 ALPHAKEY = android::os::IInputConstants::DEVICE_CLASS_ALPHAKEY,
Michael Wrightd02c5b62014-02-10 15:10:22 -080099
100 /* The input device is a touchscreen or a touchpad (either single-touch or multi-touch). */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000101 TOUCH = android::os::IInputConstants::DEVICE_CLASS_TOUCH,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800102
103 /* The input device is a cursor device such as a trackball or mouse. */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000104 CURSOR = android::os::IInputConstants::DEVICE_CLASS_CURSOR,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800105
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000106 /* The input device is a multi-touch touchscreen or touchpad. */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000107 TOUCH_MT = android::os::IInputConstants::DEVICE_CLASS_TOUCH_MT,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800108
109 /* The input device is a directional pad (implies keyboard, has DPAD keys). */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000110 DPAD = android::os::IInputConstants::DEVICE_CLASS_DPAD,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800111
112 /* The input device is a gamepad (implies keyboard, has BUTTON keys). */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000113 GAMEPAD = android::os::IInputConstants::DEVICE_CLASS_GAMEPAD,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800114
115 /* The input device has switches. */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000116 SWITCH = android::os::IInputConstants::DEVICE_CLASS_SWITCH,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800117
118 /* The input device is a joystick (implies gamepad, has joystick absolute axes). */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000119 JOYSTICK = android::os::IInputConstants::DEVICE_CLASS_JOYSTICK,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800120
121 /* The input device has a vibrator (supports FF_RUMBLE). */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000122 VIBRATOR = android::os::IInputConstants::DEVICE_CLASS_VIBRATOR,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800123
Tim Kilbourn063ff532015-04-08 10:26:18 -0700124 /* The input device has a microphone. */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000125 MIC = android::os::IInputConstants::DEVICE_CLASS_MIC,
Tim Kilbourn063ff532015-04-08 10:26:18 -0700126
Michael Wright842500e2015-03-13 17:32:02 -0700127 /* The input device is an external stylus (has data we want to fuse with touch data). */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000128 EXTERNAL_STYLUS = android::os::IInputConstants::DEVICE_CLASS_EXTERNAL_STYLUS,
Michael Wright842500e2015-03-13 17:32:02 -0700129
Prashant Malani1941ff52015-08-11 18:29:28 -0700130 /* The input device has a rotary encoder */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000131 ROTARY_ENCODER = android::os::IInputConstants::DEVICE_CLASS_ROTARY_ENCODER,
Prashant Malani1941ff52015-08-11 18:29:28 -0700132
Chris Yef59a2f42020-10-16 12:55:26 -0700133 /* The input device has a sensor like accelerometer, gyro, etc */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000134 SENSOR = android::os::IInputConstants::DEVICE_CLASS_SENSOR,
Chris Yef59a2f42020-10-16 12:55:26 -0700135
Kim Low03ea0352020-11-06 12:45:07 -0800136 /* The input device has a battery */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000137 BATTERY = android::os::IInputConstants::DEVICE_CLASS_BATTERY,
Kim Low03ea0352020-11-06 12:45:07 -0800138
Chris Ye3fdbfef2021-01-06 18:45:18 -0800139 /* The input device has sysfs controllable lights */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000140 LIGHT = android::os::IInputConstants::DEVICE_CLASS_LIGHT,
Chris Ye3fdbfef2021-01-06 18:45:18 -0800141
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000142 /* The input device is a touchpad, requiring an on-screen cursor. */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000143 TOUCHPAD = android::os::IInputConstants::DEVICE_CLASS_TOUCHPAD,
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000144
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145 /* The input device is virtual (not a real device, not part of UI configuration). */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000146 VIRTUAL = android::os::IInputConstants::DEVICE_CLASS_VIRTUAL,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800147
148 /* The input device is external (not built-in). */
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000149 EXTERNAL = android::os::IInputConstants::DEVICE_CLASS_EXTERNAL,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800150};
151
Chris Ye3fdbfef2021-01-06 18:45:18 -0800152enum class SysfsClass : uint32_t {
153 POWER_SUPPLY = 0,
154 LEDS = 1,
Dominik Laskowski75788452021-02-09 18:51:25 -0800155
156 ftl_last = LEDS
Chris Ye3fdbfef2021-01-06 18:45:18 -0800157};
158
159enum class LightColor : uint32_t {
160 RED = 0,
161 GREEN = 1,
162 BLUE = 2,
163};
164
165enum class InputLightClass : uint32_t {
166 /* The input light has brightness node. */
167 BRIGHTNESS = 0x00000001,
168 /* The input light has red name. */
169 RED = 0x00000002,
170 /* The input light has green name. */
171 GREEN = 0x00000004,
172 /* The input light has blue name. */
173 BLUE = 0x00000008,
174 /* The input light has global name. */
175 GLOBAL = 0x00000010,
176 /* The input light has multi index node. */
177 MULTI_INDEX = 0x00000020,
178 /* The input light has multi intensity node. */
179 MULTI_INTENSITY = 0x00000040,
180 /* The input light has max brightness node. */
181 MAX_BRIGHTNESS = 0x00000080,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +0000182 /* The input light has kbd_backlight name */
183 KEYBOARD_BACKLIGHT = 0x00000100,
DingYong99f2c3c2023-12-20 15:46:06 +0800184 /* The input light has mic_mute name */
185 KEYBOARD_MIC_MUTE = 0x00000200,
Chris Ye3fdbfef2021-01-06 18:45:18 -0800186};
187
Chris Yee2b1e5c2021-03-10 22:45:12 -0800188enum class InputBatteryClass : uint32_t {
189 /* The input device battery has capacity node. */
190 CAPACITY = 0x00000001,
191 /* The input device battery has capacity_level node. */
192 CAPACITY_LEVEL = 0x00000002,
193 /* The input device battery has status node. */
194 STATUS = 0x00000004,
195};
196
Chris Ye3fdbfef2021-01-06 18:45:18 -0800197/* Describes a raw light. */
198struct RawLightInfo {
199 int32_t id;
200 std::string name;
201 std::optional<int32_t> maxBrightness;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700202 ftl::Flags<InputLightClass> flags;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800203 std::array<int32_t, COLOR_NUM> rgbIndex;
204 std::filesystem::path path;
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000205
206 bool operator==(const RawLightInfo&) const = default;
207 bool operator!=(const RawLightInfo&) const = default;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800208};
209
Chris Yee2b1e5c2021-03-10 22:45:12 -0800210/* Describes a raw battery. */
211struct RawBatteryInfo {
212 int32_t id;
213 std::string name;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700214 ftl::Flags<InputBatteryClass> flags;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800215 std::filesystem::path path;
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000216
217 bool operator==(const RawBatteryInfo&) const = default;
218 bool operator!=(const RawBatteryInfo&) const = default;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800219};
220
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000221/* Layout information associated with the device */
222struct RawLayoutInfo {
223 std::string languageTag;
224 std::string layoutType;
225
226 bool operator==(const RawLayoutInfo&) const = default;
227 bool operator!=(const RawLayoutInfo&) const = default;
228};
229
Michael Wrightd02c5b62014-02-10 15:10:22 -0800230/*
231 * Gets the class that owns an axis, in cases where multiple classes might claim
232 * the same axis for different purposes.
233 */
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700234extern ftl::Flags<InputDeviceClass> getAbsAxisUsage(int32_t axis,
235 ftl::Flags<InputDeviceClass> deviceClasses);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800236
237/*
238 * Grand Central Station for events.
239 *
240 * The event hub aggregates input events received across all known input
241 * devices on the system, including devices that may be emulated by the simulator
242 * environment. In addition, the event hub generates fake input events to indicate
243 * when devices are added or removed.
244 *
245 * The event hub provides a stream of input events (via the getEvent function).
246 * It also supports querying the current actual state of input devices such as identifying
247 * which keys are currently down. Finally, the event hub keeps track of the capabilities of
248 * individual input devices, such as their class and the set of key codes that they support.
249 */
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700250class EventHubInterface {
251public:
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700252 EventHubInterface() {}
253 virtual ~EventHubInterface() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800254
Michael Wrightd02c5b62014-02-10 15:10:22 -0800255 // Synthetic raw event type codes produced when devices are added or removed.
256 enum {
257 // Sent when a device is added.
258 DEVICE_ADDED = 0x10000000,
259 // Sent when a device is removed.
260 DEVICE_REMOVED = 0x20000000,
261 // Sent when all added/removed devices from the most recent scan have been reported.
262 // This event is always sent at least once.
263 FINISHED_DEVICE_SCAN = 0x30000000,
264
265 FIRST_SYNTHETIC_EVENT = DEVICE_ADDED,
266 };
267
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700268 virtual ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800269
270 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const = 0;
271
272 virtual int32_t getDeviceControllerNumber(int32_t deviceId) const = 0;
273
Harry Cuttsc34f7582023-03-07 16:23:30 +0000274 /**
275 * Get the PropertyMap for the provided EventHub device, if available.
276 * This acquires the device lock, so a copy is returned rather than the raw pointer
277 * to the device's PropertyMap. A std::nullopt may be returned if the device could
278 * not be found, or if it doesn't have any configuration.
279 */
280 virtual std::optional<PropertyMap> getConfiguration(int32_t deviceId) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800281
Harry Cutts207674d2024-06-06 18:53:41 +0000282 virtual std::optional<RawAbsoluteAxisInfo> getAbsoluteAxisInfo(int32_t deviceId,
283 int axis) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800284
285 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const = 0;
286
287 virtual bool hasInputProperty(int32_t deviceId, int property) const = 0;
288
Chris Yef59a2f42020-10-16 12:55:26 -0700289 virtual bool hasMscEvent(int32_t deviceId, int mscEvent) const = 0;
290
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000291 virtual void addKeyRemapping(int32_t deviceId, int32_t fromKeyCode,
292 int32_t toKeyCode) const = 0;
293
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700294 virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
295 int32_t metaState, int32_t* outKeycode, int32_t* outMetaState,
296 uint32_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800297
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700298 virtual status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800299
300 // Sets devices that are excluded from opening.
301 // This can be used to ignore input devices for sensors.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100302 virtual void setExcludedDevices(const std::vector<std::string>& devices) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800303
304 /*
305 * Wait for events to become available and returns them.
306 * After returning, the EventHub holds onto a wake lock until the next call to getEvent.
307 * This ensures that the device will not go to sleep while the event is being processed.
308 * If the device needs to remain awake longer than that, then the caller is responsible
309 * for taking care of it (say, by poking the power manager user activity timer).
310 *
311 * The timeout is advisory only. If the device is asleep, it will not wake just to
312 * service the timeout.
313 *
314 * Returns the number of events obtained, or 0 if the timeout expired.
315 */
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700316 virtual std::vector<RawEvent> getEvents(int timeoutMillis) = 0;
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800317 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) = 0;
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000318 virtual base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
319 int32_t deviceId, int32_t absCode) const = 0;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800320 // Raw batteries are sysfs power_supply nodes we found from the EventHub device sysfs node,
321 // containing the raw info of the sysfs node structure.
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000322 virtual std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const = 0;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800323 virtual std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000324 int32_t BatteryId) const = 0;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800325
Chris Ye3fdbfef2021-01-06 18:45:18 -0800326 // Raw lights are sysfs led light nodes we found from the EventHub device sysfs node,
327 // containing the raw info of the sysfs node structure.
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000328 virtual std::vector<int32_t> getRawLightIds(int32_t deviceId) const = 0;
329 virtual std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId,
330 int32_t lightId) const = 0;
331 virtual std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const = 0;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800332 virtual void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) = 0;
333 virtual std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000334 int32_t deviceId, int32_t lightId) const = 0;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800335 virtual void setLightIntensities(int32_t deviceId, int32_t lightId,
336 std::unordered_map<LightColor, int32_t> intensities) = 0;
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000337 /* Query Layout info associated with the input device. */
338 virtual std::optional<RawLayoutInfo> getRawLayoutInfo(int32_t deviceId) const = 0;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000339 /* Query current input state. */
Michael Wrightd02c5b62014-02-10 15:10:22 -0800340 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0;
341 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0;
342 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0;
343 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700344 int32_t* outValue) const = 0;
Arpit Singh4b4a4572023-11-24 18:19:56 +0000345 /* Query Multi-Touch slot values for an axis. Returns error or an 1 indexed array of size
346 * (slotCount + 1). The value at the 0 index is set to queried axis. */
347 virtual base::Result<std::vector<int32_t>> getMtSlotValues(int32_t deviceId, int32_t axis,
348 size_t slotCount) const = 0;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100349 virtual int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800350
351 /*
352 * Examine key input devices for specific framework keycode support
353 */
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700354 virtual bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700355 uint8_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800356
357 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const = 0;
Arthur Hungcb40a002021-08-03 14:31:01 +0000358 virtual bool hasKeyCode(int32_t deviceId, int32_t keyCode) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800359
360 /* LED related functions expect Android LED constants, not scan codes or HID usages */
361 virtual bool hasLed(int32_t deviceId, int32_t led) const = 0;
362 virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0;
363
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700364 virtual void getVirtualKeyDefinitions(
365 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800366
Chris Ye3a1e4462020-08-12 10:13:15 -0700367 virtual const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const = 0;
368 virtual bool setKeyboardLayoutOverlay(int32_t deviceId,
369 std::shared_ptr<KeyCharacterMap> map) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800370
371 /* Control the vibrator. */
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000372 virtual void vibrate(int32_t deviceId, const VibrationElement& effect) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800373 virtual void cancelVibrate(int32_t deviceId) = 0;
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000374 virtual std::vector<int32_t> getVibratorIds(int32_t deviceId) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800375
Kim Low03ea0352020-11-06 12:45:07 -0800376 /* Query battery level. */
Chris Yee2b1e5c2021-03-10 22:45:12 -0800377 virtual std::optional<int32_t> getBatteryCapacity(int32_t deviceId,
378 int32_t batteryId) const = 0;
Kim Low03ea0352020-11-06 12:45:07 -0800379
380 /* Query battery status. */
Chris Yee2b1e5c2021-03-10 22:45:12 -0800381 virtual std::optional<int32_t> getBatteryStatus(int32_t deviceId, int32_t batteryId) const = 0;
Kim Low03ea0352020-11-06 12:45:07 -0800382
Michael Wrightd02c5b62014-02-10 15:10:22 -0800383 /* Requests the EventHub to reopen all input devices on the next call to getEvents(). */
384 virtual void requestReopenDevices() = 0;
385
386 /* Wakes up getEvents() if it is blocked on a read. */
387 virtual void wake() = 0;
388
389 /* Dump EventHub state to a string. */
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000390 virtual void dump(std::string& dump) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800391
392 /* Called by the heatbeat to ensures that the reader has not deadlocked. */
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000393 virtual void monitor() const = 0;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700394
395 /* Return true if the device is enabled. */
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000396 virtual bool isDeviceEnabled(int32_t deviceId) const = 0;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700397
398 /* Enable an input device */
399 virtual status_t enableDevice(int32_t deviceId) = 0;
400
401 /* Disable an input device. Closes file descriptor to that device. */
402 virtual status_t disableDevice(int32_t deviceId) = 0;
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000403
404 /* Sysfs node changed. Reopen the Eventhub device if any new Peripheral like Light, Battery,
405 * etc. is detected. */
406 virtual void sysfsNodeChanged(const std::string& sysfsNodePath) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800407};
408
Chris Ye66fbac32020-07-06 20:36:43 -0700409template <std::size_t BITS>
410class BitArray {
411 /* Array element type and vector of element type. */
412 using Element = std::uint32_t;
413 /* Number of bits in each BitArray element. */
414 static constexpr size_t WIDTH = sizeof(Element) * CHAR_BIT;
415 /* Number of elements to represent a bit array of the specified size of bits. */
416 static constexpr size_t COUNT = (BITS + WIDTH - 1) / WIDTH;
417
418public:
419 /* BUFFER type declaration for BitArray */
420 using Buffer = std::array<Element, COUNT>;
421 /* To tell if a bit is set in array, it selects an element from the array, and test
422 * if the relevant bit set.
423 * Note the parameter "bit" is an index to the bit, 0 <= bit < BITS.
424 */
425 inline bool test(size_t bit) const {
Prabir Pradhan9762ac92023-07-13 21:45:12 +0000426 return (bit < BITS) && mData[bit / WIDTH].test(bit % WIDTH);
427 }
428 /* Sets the given bit in the bit array to given value.
429 * Returns true if the given bit is a valid index and thus was set successfully.
430 */
431 inline bool set(size_t bit, bool value) {
432 if (bit >= BITS) {
433 return false;
434 }
435 mData[bit / WIDTH].set(bit % WIDTH, value);
436 return true;
Chris Ye66fbac32020-07-06 20:36:43 -0700437 }
438 /* Returns total number of bytes needed for the array */
439 inline size_t bytes() { return (BITS + CHAR_BIT - 1) / CHAR_BIT; }
440 /* Returns true if array contains any non-zero bit from the range defined by start and end
441 * bit index [startIndex, endIndex).
442 */
443 bool any(size_t startIndex, size_t endIndex) {
444 if (startIndex >= endIndex || startIndex > BITS || endIndex > BITS + 1) {
445 ALOGE("Invalid start/end index. start = %zu, end = %zu, total bits = %zu", startIndex,
446 endIndex, BITS);
447 return false;
448 }
449 size_t se = startIndex / WIDTH; // Start of element
450 size_t ee = endIndex / WIDTH; // End of element
451 size_t si = startIndex % WIDTH; // Start index in start element
452 size_t ei = endIndex % WIDTH; // End index in end element
453 // Need to check first unaligned bitset for any non zero bit
454 if (si > 0) {
455 size_t nBits = se == ee ? ei - si : WIDTH - si;
456 // Generate the mask of interested bit range
457 Element mask = ((1 << nBits) - 1) << si;
458 if (mData[se++].to_ulong() & mask) {
459 return true;
460 }
461 }
462 // Check whole bitset for any bit set
463 for (; se < ee; se++) {
464 if (mData[se].any()) {
465 return true;
466 }
467 }
468 // Need to check last unaligned bitset for any non zero bit
469 if (ei > 0 && se <= ee) {
470 // Generate the mask of interested bit range
471 Element mask = (1 << ei) - 1;
472 if (mData[se].to_ulong() & mask) {
473 return true;
474 }
475 }
476 return false;
477 }
478 /* Load bit array values from buffer */
479 void loadFromBuffer(const Buffer& buffer) {
480 for (size_t i = 0; i < COUNT; i++) {
481 mData[i] = std::bitset<WIDTH>(buffer[i]);
482 }
483 }
Prabir Pradhan955b6132023-07-14 19:44:34 +0000484 /* Dump the indices in the bit array that are set. */
485 inline std::string dumpSetIndices(std::string separator,
486 std::function<std::string(size_t /*index*/)> format) {
487 std::string dmp;
488 for (size_t i = 0; i < BITS; i++) {
489 if (test(i)) {
490 if (!dmp.empty()) {
491 dmp += separator;
492 }
493 dmp += format(i);
494 }
495 }
496 return dmp.empty() ? "<none>" : dmp;
497 }
Chris Ye66fbac32020-07-06 20:36:43 -0700498
499private:
500 std::array<std::bitset<WIDTH>, COUNT> mData;
501};
502
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700503class EventHub : public EventHubInterface {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800504public:
505 EventHub();
506
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700507 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800508
Chris Ye989bb932020-07-04 16:18:59 -0700509 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800510
Chris Ye989bb932020-07-04 16:18:59 -0700511 int32_t getDeviceControllerNumber(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800512
Harry Cuttsc34f7582023-03-07 16:23:30 +0000513 std::optional<PropertyMap> getConfiguration(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800514
Harry Cutts207674d2024-06-06 18:53:41 +0000515 std::optional<RawAbsoluteAxisInfo> getAbsoluteAxisInfo(int32_t deviceId,
516 int axis) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800517
Chris Ye989bb932020-07-04 16:18:59 -0700518 bool hasRelativeAxis(int32_t deviceId, int axis) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800519
Chris Ye989bb932020-07-04 16:18:59 -0700520 bool hasInputProperty(int32_t deviceId, int property) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800521
Chris Yef59a2f42020-10-16 12:55:26 -0700522 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final;
523
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000524 void addKeyRemapping(int32_t deviceId, int32_t fromKeyCode,
525 int32_t toKeyCode) const override final;
526
Chris Ye989bb932020-07-04 16:18:59 -0700527 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
528 int32_t* outKeycode, int32_t* outMetaState,
529 uint32_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800530
Chris Ye989bb932020-07-04 16:18:59 -0700531 status_t mapAxis(int32_t deviceId, int32_t scanCode,
532 AxisInfo* outAxisInfo) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800533
Chris Yef59a2f42020-10-16 12:55:26 -0700534 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000535 int32_t deviceId, int32_t absCode) const override final;
Chris Yef59a2f42020-10-16 12:55:26 -0700536
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000537 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override final;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800538 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000539 int32_t BatteryId) const override final;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800540
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000541 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800542
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000543 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId,
544 int32_t lightId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800545
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000546 std::optional<int32_t> getLightBrightness(int32_t deviceId,
547 int32_t lightId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800548 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override final;
549 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000550 int32_t deviceId, int32_t lightId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800551 void setLightIntensities(int32_t deviceId, int32_t lightId,
552 std::unordered_map<LightColor, int32_t> intensities) override final;
553
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000554 std::optional<RawLayoutInfo> getRawLayoutInfo(int32_t deviceId) const override final;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000555
Chris Ye989bb932020-07-04 16:18:59 -0700556 void setExcludedDevices(const std::vector<std::string>& devices) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800557
Chris Ye989bb932020-07-04 16:18:59 -0700558 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override final;
559 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override final;
560 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override final;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100561 int32_t getKeyCodeForKeyLocation(int32_t deviceId,
562 int32_t locationKeyCode) const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700563 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
564 int32_t* outValue) const override final;
Arpit Singh4b4a4572023-11-24 18:19:56 +0000565 base::Result<std::vector<int32_t>> getMtSlotValues(int32_t deviceId, int32_t axis,
566 size_t slotCount) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800567
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700568 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Chris Ye989bb932020-07-04 16:18:59 -0700569 uint8_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800570
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700571 std::vector<RawEvent> getEvents(int timeoutMillis) override final;
Chris Ye989bb932020-07-04 16:18:59 -0700572 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800573
Chris Ye989bb932020-07-04 16:18:59 -0700574 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override final;
Arthur Hungcb40a002021-08-03 14:31:01 +0000575 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700576 bool hasLed(int32_t deviceId, int32_t led) const override final;
577 void setLedState(int32_t deviceId, int32_t led, bool on) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800578
Chris Ye989bb932020-07-04 16:18:59 -0700579 void getVirtualKeyDefinitions(
580 int32_t deviceId,
581 std::vector<VirtualKeyDefinition>& outVirtualKeys) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800582
Chris Ye3a1e4462020-08-12 10:13:15 -0700583 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(
584 int32_t deviceId) const override final;
585 bool setKeyboardLayoutOverlay(int32_t deviceId,
586 std::shared_ptr<KeyCharacterMap> map) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800587
Chris Ye989bb932020-07-04 16:18:59 -0700588 void vibrate(int32_t deviceId, const VibrationElement& effect) override final;
589 void cancelVibrate(int32_t deviceId) override final;
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000590 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800591
Chris Ye989bb932020-07-04 16:18:59 -0700592 void requestReopenDevices() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800593
Chris Ye989bb932020-07-04 16:18:59 -0700594 void wake() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800595
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000596 void dump(std::string& dump) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800597
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000598 void monitor() const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700599
Chris Yee2b1e5c2021-03-10 22:45:12 -0800600 std::optional<int32_t> getBatteryCapacity(int32_t deviceId,
601 int32_t batteryId) const override final;
Kim Low03ea0352020-11-06 12:45:07 -0800602
Chris Yee2b1e5c2021-03-10 22:45:12 -0800603 std::optional<int32_t> getBatteryStatus(int32_t deviceId,
604 int32_t batteryId) const override final;
Kim Low03ea0352020-11-06 12:45:07 -0800605
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000606 bool isDeviceEnabled(int32_t deviceId) const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700607
608 status_t enableDevice(int32_t deviceId) override final;
609
610 status_t disableDevice(int32_t deviceId) override final;
611
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000612 void sysfsNodeChanged(const std::string& sysfsNodePath) override final;
613
Chris Ye989bb932020-07-04 16:18:59 -0700614 ~EventHub() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800615
616private:
Prabir Pradhancb42b472022-08-23 16:01:19 +0000617 // Holds information about the sysfs device associated with the Device.
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700618 struct AssociatedDevice {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800619 // The sysfs root path of the misc device.
620 std::filesystem::path sysfsRootPath;
Prabir Pradhancb42b472022-08-23 16:01:19 +0000621 std::unordered_map<int32_t /*batteryId*/, RawBatteryInfo> batteryInfos;
622 std::unordered_map<int32_t /*lightId*/, RawLightInfo> lightInfos;
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000623 std::optional<RawLayoutInfo> layoutInfo;
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000624
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000625 bool isChanged() const;
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000626 bool operator==(const AssociatedDevice&) const = default;
627 bool operator!=(const AssociatedDevice&) const = default;
628 std::string dump() const;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800629 };
630
Michael Wrightd02c5b62014-02-10 15:10:22 -0800631 struct Device {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700632 int fd; // may be -1 if device is closed
Michael Wrightd02c5b62014-02-10 15:10:22 -0800633 const int32_t id;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100634 const std::string path;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800635 const InputDeviceIdentifier identifier;
636
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -0800637 std::unique_ptr<TouchVideoDevice> videoDevice;
638
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700639 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800640
Prabir Pradhanf44ab482023-07-20 20:46:09 +0000641 BitArray<KEY_CNT> keyBitmask;
642 BitArray<KEY_CNT> keyState;
643 BitArray<REL_CNT> relBitmask;
644 BitArray<SW_CNT> swBitmask;
645 BitArray<SW_CNT> swState;
646 BitArray<LED_CNT> ledBitmask;
647 BitArray<FF_CNT> ffBitmask;
648 BitArray<INPUT_PROP_CNT> propBitmask;
649 BitArray<MSC_CNT> mscBitmask;
650 BitArray<ABS_CNT> absBitmask;
Prabir Pradhan341d0782023-07-14 19:04:10 +0000651 struct AxisState {
652 RawAbsoluteAxisInfo info;
653 int value;
654 };
Prabir Pradhan955b6132023-07-14 19:44:34 +0000655 std::map<int /*axis*/, AxisState> absState;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800656
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100657 std::string configurationFile;
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500658 std::unique_ptr<PropertyMap> configuration;
Siarhei Vishniakou3e78dec2019-02-20 16:21:46 -0600659 std::unique_ptr<VirtualKeyMap> virtualKeyMap;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800660 KeyMap keyMap;
661
Michael Wrightd02c5b62014-02-10 15:10:22 -0800662 bool ffEffectPlaying;
663 int16_t ffEffectId; // initially -1
664
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700665 // A shared_ptr of a device associated with the input device.
Prabir Pradhan51894782022-08-23 16:29:10 +0000666 // The input devices that have the same sysfs path have the same associated device.
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000667 std::shared_ptr<const AssociatedDevice> associatedDevice;
Kim Low03ea0352020-11-06 12:45:07 -0800668
Michael Wrightd02c5b62014-02-10 15:10:22 -0800669 int32_t controllerNumber;
670
Prabir Pradhancb42b472022-08-23 16:01:19 +0000671 Device(int fd, int32_t id, std::string path, InputDeviceIdentifier identifier,
672 std::shared_ptr<const AssociatedDevice> assocDev);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800673 ~Device();
674
675 void close();
676
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700677 bool enabled; // initially true
678 status_t enable();
679 status_t disable();
Chris Ye989bb932020-07-04 16:18:59 -0700680 bool hasValidFd() const;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700681 const bool isVirtual; // set if fd < 0 is passed to constructor
Michael Wrightd02c5b62014-02-10 15:10:22 -0800682
Chris Ye3a1e4462020-08-12 10:13:15 -0700683 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const;
Chris Ye66fbac32020-07-06 20:36:43 -0700684
685 template <std::size_t N>
Chris Ye989bb932020-07-04 16:18:59 -0700686 status_t readDeviceBitMask(unsigned long ioctlCode, BitArray<N>& bitArray);
Chris Ye66fbac32020-07-06 20:36:43 -0700687
Chris Ye989bb932020-07-04 16:18:59 -0700688 void configureFd();
Prabir Pradhan341d0782023-07-14 19:04:10 +0000689 void populateAbsoluteAxisStates();
Chris Ye989bb932020-07-04 16:18:59 -0700690 bool hasKeycodeLocked(int keycode) const;
691 void loadConfigurationLocked();
692 bool loadVirtualKeyMapLocked();
693 status_t loadKeyMapLocked();
694 bool isExternalDeviceLocked();
695 bool deviceHasMicLocked();
696 void setLedForControllerLocked();
697 status_t mapLed(int32_t led, int32_t* outScanCode) const;
698 void setLedStateLocked(int32_t led, bool on);
Prabir Pradhan9762ac92023-07-13 21:45:12 +0000699
700 bool currentFrameDropped;
701 void trackInputEvent(const struct input_event& event);
702 void readDeviceState();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800703 };
704
Siarhei Vishniakoua4c502a2021-02-05 00:45:20 +0000705 /**
706 * Create a new device for the provided path.
707 */
Chris Yed3fef462021-03-07 17:10:08 -0800708 void openDeviceLocked(const std::string& devicePath) REQUIRES(mLock);
709 void openVideoDeviceLocked(const std::string& devicePath) REQUIRES(mLock);
Siarhei Vishniakouf49608d2020-08-20 19:18:21 -0500710 /**
711 * Try to associate a video device with an input device. If the association succeeds,
712 * the videoDevice is moved into the input device. 'videoDevice' will become null if this
713 * happens.
714 * Return true if the association succeeds.
715 * Return false otherwise.
716 */
Chris Yed3fef462021-03-07 17:10:08 -0800717 bool tryAddVideoDeviceLocked(Device& device, std::unique_ptr<TouchVideoDevice>& videoDevice)
718 REQUIRES(mLock);
719 void createVirtualKeyboardLocked() REQUIRES(mLock);
720 void addDeviceLocked(std::unique_ptr<Device> device) REQUIRES(mLock);
721 void assignDescriptorLocked(InputDeviceIdentifier& identifier) REQUIRES(mLock);
Prabir Pradhancb42b472022-08-23 16:01:19 +0000722 std::shared_ptr<const AssociatedDevice> obtainAssociatedDeviceLocked(
723 const std::filesystem::path& devicePath) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800724
Chris Yed3fef462021-03-07 17:10:08 -0800725 void closeDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock);
726 void closeVideoDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock);
727 void closeDeviceLocked(Device& device) REQUIRES(mLock);
728 void closeAllDevicesLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800729
Siarhei Vishniakou25920312018-12-12 15:24:44 -0800730 status_t registerFdForEpoll(int fd);
731 status_t unregisterFdFromEpoll(int fd);
Chris Yed3fef462021-03-07 17:10:08 -0800732 status_t registerDeviceForEpollLocked(Device& device) REQUIRES(mLock);
733 void registerVideoDeviceForEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock);
734 status_t unregisterDeviceFromEpollLocked(Device& device) REQUIRES(mLock);
735 void unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700736
Chris Yed3fef462021-03-07 17:10:08 -0800737 status_t scanDirLocked(const std::string& dirname) REQUIRES(mLock);
738 status_t scanVideoDirLocked(const std::string& dirname) REQUIRES(mLock);
739 void scanDevicesLocked() REQUIRES(mLock);
Prabir Pradhan952e65b2022-06-23 17:49:55 +0000740 base::Result<void> readNotifyLocked() REQUIRES(mLock);
741 void handleNotifyEventLocked(const inotify_event&) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800742
Chris Yed3fef462021-03-07 17:10:08 -0800743 Device* getDeviceLocked(int32_t deviceId) const REQUIRES(mLock);
744 Device* getDeviceByPathLocked(const std::string& devicePath) const REQUIRES(mLock);
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700745 /**
746 * Look through all available fd's (both for input devices and for video devices),
747 * and return the device pointer.
748 */
Chris Yed3fef462021-03-07 17:10:08 -0800749 Device* getDeviceByFdLocked(int fd) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800750
Chris Yed3fef462021-03-07 17:10:08 -0800751 int32_t getNextControllerNumberLocked(const std::string& name) REQUIRES(mLock);
Josh Bartel938632f2022-07-19 15:34:22 -0500752
753 bool hasDeviceWithDescriptorLocked(const std::string& descriptor) const REQUIRES(mLock);
754
Chris Yed3fef462021-03-07 17:10:08 -0800755 void releaseControllerNumberLocked(int32_t num) REQUIRES(mLock);
756 void reportDeviceAddedForStatisticsLocked(const InputDeviceIdentifier& identifier,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700757 ftl::Flags<InputDeviceClass> classes) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800758
Chris Yee2b1e5c2021-03-10 22:45:12 -0800759 const std::unordered_map<int32_t, RawBatteryInfo>& getBatteryInfoLocked(int32_t deviceId) const
760 REQUIRES(mLock);
761
762 const std::unordered_map<int32_t, RawLightInfo>& getLightInfoLocked(int32_t deviceId) const
763 REQUIRES(mLock);
764
Usama Arifb27c8e62021-06-03 16:44:09 +0100765 void addDeviceInputInotify();
766 void addDeviceInotify();
767
Michael Wrightd02c5b62014-02-10 15:10:22 -0800768 // Protect all internal state.
Chris Ye1c2e0892020-11-30 21:41:44 -0800769 mutable std::mutex mLock;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800770
771 // The actual id of the built-in keyboard, or NO_BUILT_IN_KEYBOARD if none.
772 // EventHub remaps the built-in keyboard to id 0 externally as required by the API.
773 enum {
774 // Must not conflict with any other assigned device ids, including
775 // the virtual keyboard id (-1).
776 NO_BUILT_IN_KEYBOARD = -2,
777 };
778 int32_t mBuiltInKeyboardId;
779
780 int32_t mNextDeviceId;
781
782 BitSet32 mControllerNumbers;
783
Chris Ye989bb932020-07-04 16:18:59 -0700784 std::unordered_map<int32_t, std::unique_ptr<Device>> mDevices;
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800785 /**
786 * Video devices that report touchscreen heatmap, but have not (yet) been paired
787 * with a specific input device. Video device discovery is independent from input device
788 * discovery, so the two types of devices could be found in any order.
789 * Ideally, video devices in this queue do not have an open fd, or at least aren't
790 * actively streaming.
791 */
792 std::vector<std::unique_ptr<TouchVideoDevice>> mUnattachedVideoDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800793
Chris Ye989bb932020-07-04 16:18:59 -0700794 std::vector<std::unique_ptr<Device>> mOpeningDevices;
795 std::vector<std::unique_ptr<Device>> mClosingDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800796
797 bool mNeedToSendFinishedDeviceScan;
798 bool mNeedToReopenDevices;
799 bool mNeedToScanDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100800 std::vector<std::string> mExcludedDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800801
802 int mEpollFd;
803 int mINotifyFd;
804 int mWakeReadPipeFd;
805 int mWakeWritePipeFd;
806
Usama Arifb27c8e62021-06-03 16:44:09 +0100807 int mDeviceInputWd;
808 int mDeviceWd = -1;
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800809
Michael Wrightd02c5b62014-02-10 15:10:22 -0800810 // Maximum number of signalled FDs to handle at a time.
811 static const int EPOLL_MAX_EVENTS = 16;
812
813 // The array of pending epoll events and the index of the next event to be handled.
814 struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS];
815 size_t mPendingEventCount;
816 size_t mPendingEventIndex;
817 bool mPendingINotify;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800818};
819
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700820} // namespace android