blob: a3ecf418bb28d56c2265fa2cbd00774881db8127 [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>
Chris Ye989bb932020-07-04 16:18:59 -070022#include <unordered_map>
Prabir Pradhan51894782022-08-23 16:29:10 +000023#include <utility>
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010024#include <vector>
25
Kim Low03ea0352020-11-06 12:45:07 -080026#include <batteryservice/BatteryService.h>
Dominik Laskowski2f01d772022-03-23 16:01:29 -070027#include <ftl/flags.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080028#include <input/Input.h>
29#include <input/InputDevice.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080030#include <input/KeyCharacterMap.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070031#include <input/KeyLayoutMap.h>
32#include <input/Keyboard.h>
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070033#include <input/PropertyMap.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080034#include <input/VirtualKeyMap.h>
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000035#include <linux/input.h>
36#include <sys/epoll.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080037#include <utils/BitSet.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070038#include <utils/Errors.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070039#include <utils/List.h>
40#include <utils/Log.h>
41#include <utils/Mutex.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080042
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080043#include "TouchVideoDevice.h"
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000044#include "VibrationElement.h"
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080045
Prabir Pradhan952e65b2022-06-23 17:49:55 +000046struct inotify_event;
47
Michael Wrightd02c5b62014-02-10 15:10:22 -080048namespace android {
49
Chris Ye3fdbfef2021-01-06 18:45:18 -080050/* Number of colors : {red, green, blue} */
51static constexpr size_t COLOR_NUM = 3;
Michael Wrightd02c5b62014-02-10 15:10:22 -080052/*
53 * A raw event as retrieved from the EventHub.
54 */
55struct RawEvent {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000056 // Time when the event happened
Michael Wrightd02c5b62014-02-10 15:10:22 -080057 nsecs_t when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000058 // Time when the event was read by EventHub. Only populated for input events.
59 // For other events (device added/removed/etc), this value is undefined and should not be read.
60 nsecs_t readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -080061 int32_t deviceId;
62 int32_t type;
63 int32_t code;
64 int32_t value;
65};
66
67/* Describes an absolute axis. */
68struct RawAbsoluteAxisInfo {
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000069 bool valid{false}; // true if the information is valid, false otherwise
Michael Wrightd02c5b62014-02-10 15:10:22 -080070
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000071 int32_t minValue{}; // minimum value
72 int32_t maxValue{}; // maximum value
73 int32_t flat{}; // center flat position, eg. flat == 8 means center is between -8 and 8
74 int32_t fuzz{}; // error tolerance, eg. fuzz == 4 means value is +/- 4 due to noise
75 int32_t resolution{}; // resolution in units per mm or radians per mm
Michael Wrightd02c5b62014-02-10 15:10:22 -080076
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000077 inline void clear() { *this = RawAbsoluteAxisInfo(); }
Michael Wrightd02c5b62014-02-10 15:10:22 -080078};
79
80/*
81 * Input device classes.
82 */
Chris Ye1b0c7342020-07-28 21:57:03 -070083enum class InputDeviceClass : uint32_t {
Michael Wrightd02c5b62014-02-10 15:10:22 -080084 /* The input device is a keyboard or has buttons. */
Chris Ye1b0c7342020-07-28 21:57:03 -070085 KEYBOARD = 0x00000001,
Michael Wrightd02c5b62014-02-10 15:10:22 -080086
87 /* The input device is an alpha-numeric keyboard (not just a dial pad). */
Chris Ye1b0c7342020-07-28 21:57:03 -070088 ALPHAKEY = 0x00000002,
Michael Wrightd02c5b62014-02-10 15:10:22 -080089
90 /* The input device is a touchscreen or a touchpad (either single-touch or multi-touch). */
Chris Ye1b0c7342020-07-28 21:57:03 -070091 TOUCH = 0x00000004,
Michael Wrightd02c5b62014-02-10 15:10:22 -080092
93 /* The input device is a cursor device such as a trackball or mouse. */
Chris Ye1b0c7342020-07-28 21:57:03 -070094 CURSOR = 0x00000008,
Michael Wrightd02c5b62014-02-10 15:10:22 -080095
Harry Cutts79cc9fa2022-10-28 15:32:39 +000096 /* The input device is a multi-touch touchscreen or touchpad. */
Chris Ye1b0c7342020-07-28 21:57:03 -070097 TOUCH_MT = 0x00000010,
Michael Wrightd02c5b62014-02-10 15:10:22 -080098
99 /* The input device is a directional pad (implies keyboard, has DPAD keys). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700100 DPAD = 0x00000020,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800101
102 /* The input device is a gamepad (implies keyboard, has BUTTON keys). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700103 GAMEPAD = 0x00000040,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800104
105 /* The input device has switches. */
Chris Ye1b0c7342020-07-28 21:57:03 -0700106 SWITCH = 0x00000080,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800107
108 /* The input device is a joystick (implies gamepad, has joystick absolute axes). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700109 JOYSTICK = 0x00000100,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800110
111 /* The input device has a vibrator (supports FF_RUMBLE). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700112 VIBRATOR = 0x00000200,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800113
Tim Kilbourn063ff532015-04-08 10:26:18 -0700114 /* The input device has a microphone. */
Chris Ye1b0c7342020-07-28 21:57:03 -0700115 MIC = 0x00000400,
Tim Kilbourn063ff532015-04-08 10:26:18 -0700116
Michael Wright842500e2015-03-13 17:32:02 -0700117 /* The input device is an external stylus (has data we want to fuse with touch data). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700118 EXTERNAL_STYLUS = 0x00000800,
Michael Wright842500e2015-03-13 17:32:02 -0700119
Prashant Malani1941ff52015-08-11 18:29:28 -0700120 /* The input device has a rotary encoder */
Chris Ye1b0c7342020-07-28 21:57:03 -0700121 ROTARY_ENCODER = 0x00001000,
Prashant Malani1941ff52015-08-11 18:29:28 -0700122
Chris Yef59a2f42020-10-16 12:55:26 -0700123 /* The input device has a sensor like accelerometer, gyro, etc */
124 SENSOR = 0x00002000,
125
Kim Low03ea0352020-11-06 12:45:07 -0800126 /* The input device has a battery */
127 BATTERY = 0x00004000,
128
Chris Ye3fdbfef2021-01-06 18:45:18 -0800129 /* The input device has sysfs controllable lights */
130 LIGHT = 0x00008000,
131
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000132 /* The input device is a touchpad, requiring an on-screen cursor. */
133 TOUCHPAD = 0x00010000,
134
Michael Wrightd02c5b62014-02-10 15:10:22 -0800135 /* The input device is virtual (not a real device, not part of UI configuration). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700136 VIRTUAL = 0x40000000,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800137
138 /* The input device is external (not built-in). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700139 EXTERNAL = 0x80000000,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800140};
141
Chris Ye3fdbfef2021-01-06 18:45:18 -0800142enum class SysfsClass : uint32_t {
143 POWER_SUPPLY = 0,
144 LEDS = 1,
Dominik Laskowski75788452021-02-09 18:51:25 -0800145
146 ftl_last = LEDS
Chris Ye3fdbfef2021-01-06 18:45:18 -0800147};
148
149enum class LightColor : uint32_t {
150 RED = 0,
151 GREEN = 1,
152 BLUE = 2,
153};
154
155enum class InputLightClass : uint32_t {
156 /* The input light has brightness node. */
157 BRIGHTNESS = 0x00000001,
158 /* The input light has red name. */
159 RED = 0x00000002,
160 /* The input light has green name. */
161 GREEN = 0x00000004,
162 /* The input light has blue name. */
163 BLUE = 0x00000008,
164 /* The input light has global name. */
165 GLOBAL = 0x00000010,
166 /* The input light has multi index node. */
167 MULTI_INDEX = 0x00000020,
168 /* The input light has multi intensity node. */
169 MULTI_INTENSITY = 0x00000040,
170 /* The input light has max brightness node. */
171 MAX_BRIGHTNESS = 0x00000080,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +0000172 /* The input light has kbd_backlight name */
173 KEYBOARD_BACKLIGHT = 0x00000100,
Chris Ye3fdbfef2021-01-06 18:45:18 -0800174};
175
Chris Yee2b1e5c2021-03-10 22:45:12 -0800176enum class InputBatteryClass : uint32_t {
177 /* The input device battery has capacity node. */
178 CAPACITY = 0x00000001,
179 /* The input device battery has capacity_level node. */
180 CAPACITY_LEVEL = 0x00000002,
181 /* The input device battery has status node. */
182 STATUS = 0x00000004,
183};
184
Chris Ye3fdbfef2021-01-06 18:45:18 -0800185/* Describes a raw light. */
186struct RawLightInfo {
187 int32_t id;
188 std::string name;
189 std::optional<int32_t> maxBrightness;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700190 ftl::Flags<InputLightClass> flags;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800191 std::array<int32_t, COLOR_NUM> rgbIndex;
192 std::filesystem::path path;
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000193
194 bool operator==(const RawLightInfo&) const = default;
195 bool operator!=(const RawLightInfo&) const = default;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800196};
197
Chris Yee2b1e5c2021-03-10 22:45:12 -0800198/* Describes a raw battery. */
199struct RawBatteryInfo {
200 int32_t id;
201 std::string name;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700202 ftl::Flags<InputBatteryClass> flags;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800203 std::filesystem::path path;
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000204
205 bool operator==(const RawBatteryInfo&) const = default;
206 bool operator!=(const RawBatteryInfo&) const = default;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800207};
208
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000209/* Layout information associated with the device */
210struct RawLayoutInfo {
211 std::string languageTag;
212 std::string layoutType;
213
214 bool operator==(const RawLayoutInfo&) const = default;
215 bool operator!=(const RawLayoutInfo&) const = default;
216};
217
Michael Wrightd02c5b62014-02-10 15:10:22 -0800218/*
219 * Gets the class that owns an axis, in cases where multiple classes might claim
220 * the same axis for different purposes.
221 */
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700222extern ftl::Flags<InputDeviceClass> getAbsAxisUsage(int32_t axis,
223 ftl::Flags<InputDeviceClass> deviceClasses);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800224
225/*
226 * Grand Central Station for events.
227 *
228 * The event hub aggregates input events received across all known input
229 * devices on the system, including devices that may be emulated by the simulator
230 * environment. In addition, the event hub generates fake input events to indicate
231 * when devices are added or removed.
232 *
233 * The event hub provides a stream of input events (via the getEvent function).
234 * It also supports querying the current actual state of input devices such as identifying
235 * which keys are currently down. Finally, the event hub keeps track of the capabilities of
236 * individual input devices, such as their class and the set of key codes that they support.
237 */
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700238class EventHubInterface {
239public:
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700240 EventHubInterface() {}
241 virtual ~EventHubInterface() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800242
Michael Wrightd02c5b62014-02-10 15:10:22 -0800243 // Synthetic raw event type codes produced when devices are added or removed.
244 enum {
245 // Sent when a device is added.
246 DEVICE_ADDED = 0x10000000,
247 // Sent when a device is removed.
248 DEVICE_REMOVED = 0x20000000,
249 // Sent when all added/removed devices from the most recent scan have been reported.
250 // This event is always sent at least once.
251 FINISHED_DEVICE_SCAN = 0x30000000,
252
253 FIRST_SYNTHETIC_EVENT = DEVICE_ADDED,
254 };
255
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700256 virtual ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800257
258 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const = 0;
259
260 virtual int32_t getDeviceControllerNumber(int32_t deviceId) const = 0;
261
262 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const = 0;
263
264 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700265 RawAbsoluteAxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800266
267 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const = 0;
268
269 virtual bool hasInputProperty(int32_t deviceId, int property) const = 0;
270
Chris Yef59a2f42020-10-16 12:55:26 -0700271 virtual bool hasMscEvent(int32_t deviceId, int mscEvent) const = 0;
272
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000273 virtual void addKeyRemapping(int32_t deviceId, int32_t fromKeyCode,
274 int32_t toKeyCode) const = 0;
275
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700276 virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
277 int32_t metaState, int32_t* outKeycode, int32_t* outMetaState,
278 uint32_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800279
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700280 virtual status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800281
282 // Sets devices that are excluded from opening.
283 // This can be used to ignore input devices for sensors.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100284 virtual void setExcludedDevices(const std::vector<std::string>& devices) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800285
286 /*
287 * Wait for events to become available and returns them.
288 * After returning, the EventHub holds onto a wake lock until the next call to getEvent.
289 * This ensures that the device will not go to sleep while the event is being processed.
290 * If the device needs to remain awake longer than that, then the caller is responsible
291 * for taking care of it (say, by poking the power manager user activity timer).
292 *
293 * The timeout is advisory only. If the device is asleep, it will not wake just to
294 * service the timeout.
295 *
296 * Returns the number of events obtained, or 0 if the timeout expired.
297 */
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700298 virtual std::vector<RawEvent> getEvents(int timeoutMillis) = 0;
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800299 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) = 0;
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000300 virtual base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
301 int32_t deviceId, int32_t absCode) const = 0;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800302 // Raw batteries are sysfs power_supply nodes we found from the EventHub device sysfs node,
303 // containing the raw info of the sysfs node structure.
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000304 virtual std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const = 0;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800305 virtual std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000306 int32_t BatteryId) const = 0;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800307
Chris Ye3fdbfef2021-01-06 18:45:18 -0800308 // Raw lights are sysfs led light nodes we found from the EventHub device sysfs node,
309 // containing the raw info of the sysfs node structure.
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000310 virtual std::vector<int32_t> getRawLightIds(int32_t deviceId) const = 0;
311 virtual std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId,
312 int32_t lightId) const = 0;
313 virtual std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const = 0;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800314 virtual void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) = 0;
315 virtual std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000316 int32_t deviceId, int32_t lightId) const = 0;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800317 virtual void setLightIntensities(int32_t deviceId, int32_t lightId,
318 std::unordered_map<LightColor, int32_t> intensities) = 0;
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000319 /* Query Layout info associated with the input device. */
320 virtual std::optional<RawLayoutInfo> getRawLayoutInfo(int32_t deviceId) const = 0;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000321 /* Query current input state. */
Michael Wrightd02c5b62014-02-10 15:10:22 -0800322 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0;
323 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0;
324 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0;
325 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700326 int32_t* outValue) const = 0;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100327 virtual int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800328
329 /*
330 * Examine key input devices for specific framework keycode support
331 */
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700332 virtual bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700333 uint8_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800334
335 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const = 0;
Arthur Hungcb40a002021-08-03 14:31:01 +0000336 virtual bool hasKeyCode(int32_t deviceId, int32_t keyCode) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800337
338 /* LED related functions expect Android LED constants, not scan codes or HID usages */
339 virtual bool hasLed(int32_t deviceId, int32_t led) const = 0;
340 virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0;
341
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700342 virtual void getVirtualKeyDefinitions(
343 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344
Chris Ye3a1e4462020-08-12 10:13:15 -0700345 virtual const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const = 0;
346 virtual bool setKeyboardLayoutOverlay(int32_t deviceId,
347 std::shared_ptr<KeyCharacterMap> map) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800348
349 /* Control the vibrator. */
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000350 virtual void vibrate(int32_t deviceId, const VibrationElement& effect) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351 virtual void cancelVibrate(int32_t deviceId) = 0;
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000352 virtual std::vector<int32_t> getVibratorIds(int32_t deviceId) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800353
Kim Low03ea0352020-11-06 12:45:07 -0800354 /* Query battery level. */
Chris Yee2b1e5c2021-03-10 22:45:12 -0800355 virtual std::optional<int32_t> getBatteryCapacity(int32_t deviceId,
356 int32_t batteryId) const = 0;
Kim Low03ea0352020-11-06 12:45:07 -0800357
358 /* Query battery status. */
Chris Yee2b1e5c2021-03-10 22:45:12 -0800359 virtual std::optional<int32_t> getBatteryStatus(int32_t deviceId, int32_t batteryId) const = 0;
Kim Low03ea0352020-11-06 12:45:07 -0800360
Michael Wrightd02c5b62014-02-10 15:10:22 -0800361 /* Requests the EventHub to reopen all input devices on the next call to getEvents(). */
362 virtual void requestReopenDevices() = 0;
363
364 /* Wakes up getEvents() if it is blocked on a read. */
365 virtual void wake() = 0;
366
367 /* Dump EventHub state to a string. */
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000368 virtual void dump(std::string& dump) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800369
370 /* Called by the heatbeat to ensures that the reader has not deadlocked. */
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000371 virtual void monitor() const = 0;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700372
373 /* Return true if the device is enabled. */
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000374 virtual bool isDeviceEnabled(int32_t deviceId) const = 0;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700375
376 /* Enable an input device */
377 virtual status_t enableDevice(int32_t deviceId) = 0;
378
379 /* Disable an input device. Closes file descriptor to that device. */
380 virtual status_t disableDevice(int32_t deviceId) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800381};
382
Chris Ye66fbac32020-07-06 20:36:43 -0700383template <std::size_t BITS>
384class BitArray {
385 /* Array element type and vector of element type. */
386 using Element = std::uint32_t;
387 /* Number of bits in each BitArray element. */
388 static constexpr size_t WIDTH = sizeof(Element) * CHAR_BIT;
389 /* Number of elements to represent a bit array of the specified size of bits. */
390 static constexpr size_t COUNT = (BITS + WIDTH - 1) / WIDTH;
391
392public:
393 /* BUFFER type declaration for BitArray */
394 using Buffer = std::array<Element, COUNT>;
395 /* To tell if a bit is set in array, it selects an element from the array, and test
396 * if the relevant bit set.
397 * Note the parameter "bit" is an index to the bit, 0 <= bit < BITS.
398 */
399 inline bool test(size_t bit) const {
400 return (bit < BITS) ? mData[bit / WIDTH].test(bit % WIDTH) : false;
401 }
402 /* Returns total number of bytes needed for the array */
403 inline size_t bytes() { return (BITS + CHAR_BIT - 1) / CHAR_BIT; }
404 /* Returns true if array contains any non-zero bit from the range defined by start and end
405 * bit index [startIndex, endIndex).
406 */
407 bool any(size_t startIndex, size_t endIndex) {
408 if (startIndex >= endIndex || startIndex > BITS || endIndex > BITS + 1) {
409 ALOGE("Invalid start/end index. start = %zu, end = %zu, total bits = %zu", startIndex,
410 endIndex, BITS);
411 return false;
412 }
413 size_t se = startIndex / WIDTH; // Start of element
414 size_t ee = endIndex / WIDTH; // End of element
415 size_t si = startIndex % WIDTH; // Start index in start element
416 size_t ei = endIndex % WIDTH; // End index in end element
417 // Need to check first unaligned bitset for any non zero bit
418 if (si > 0) {
419 size_t nBits = se == ee ? ei - si : WIDTH - si;
420 // Generate the mask of interested bit range
421 Element mask = ((1 << nBits) - 1) << si;
422 if (mData[se++].to_ulong() & mask) {
423 return true;
424 }
425 }
426 // Check whole bitset for any bit set
427 for (; se < ee; se++) {
428 if (mData[se].any()) {
429 return true;
430 }
431 }
432 // Need to check last unaligned bitset for any non zero bit
433 if (ei > 0 && se <= ee) {
434 // Generate the mask of interested bit range
435 Element mask = (1 << ei) - 1;
436 if (mData[se].to_ulong() & mask) {
437 return true;
438 }
439 }
440 return false;
441 }
442 /* Load bit array values from buffer */
443 void loadFromBuffer(const Buffer& buffer) {
444 for (size_t i = 0; i < COUNT; i++) {
445 mData[i] = std::bitset<WIDTH>(buffer[i]);
446 }
447 }
448
449private:
450 std::array<std::bitset<WIDTH>, COUNT> mData;
451};
452
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700453class EventHub : public EventHubInterface {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800454public:
455 EventHub();
456
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700457 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800458
Chris Ye989bb932020-07-04 16:18:59 -0700459 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800460
Chris Ye989bb932020-07-04 16:18:59 -0700461 int32_t getDeviceControllerNumber(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800462
Chris Ye989bb932020-07-04 16:18:59 -0700463 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800464
Chris Ye989bb932020-07-04 16:18:59 -0700465 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
466 RawAbsoluteAxisInfo* outAxisInfo) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800467
Chris Ye989bb932020-07-04 16:18:59 -0700468 bool hasRelativeAxis(int32_t deviceId, int axis) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800469
Chris Ye989bb932020-07-04 16:18:59 -0700470 bool hasInputProperty(int32_t deviceId, int property) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800471
Chris Yef59a2f42020-10-16 12:55:26 -0700472 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final;
473
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000474 void addKeyRemapping(int32_t deviceId, int32_t fromKeyCode,
475 int32_t toKeyCode) const override final;
476
Chris Ye989bb932020-07-04 16:18:59 -0700477 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
478 int32_t* outKeycode, int32_t* outMetaState,
479 uint32_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800480
Chris Ye989bb932020-07-04 16:18:59 -0700481 status_t mapAxis(int32_t deviceId, int32_t scanCode,
482 AxisInfo* outAxisInfo) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800483
Chris Yef59a2f42020-10-16 12:55:26 -0700484 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000485 int32_t deviceId, int32_t absCode) const override final;
Chris Yef59a2f42020-10-16 12:55:26 -0700486
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000487 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override final;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800488 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000489 int32_t BatteryId) const override final;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800490
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000491 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800492
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000493 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId,
494 int32_t lightId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800495
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000496 std::optional<int32_t> getLightBrightness(int32_t deviceId,
497 int32_t lightId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800498 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override final;
499 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000500 int32_t deviceId, int32_t lightId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800501 void setLightIntensities(int32_t deviceId, int32_t lightId,
502 std::unordered_map<LightColor, int32_t> intensities) override final;
503
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000504 std::optional<RawLayoutInfo> getRawLayoutInfo(int32_t deviceId) const override final;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000505
Chris Ye989bb932020-07-04 16:18:59 -0700506 void setExcludedDevices(const std::vector<std::string>& devices) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800507
Chris Ye989bb932020-07-04 16:18:59 -0700508 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override final;
509 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override final;
510 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override final;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100511 int32_t getKeyCodeForKeyLocation(int32_t deviceId,
512 int32_t locationKeyCode) const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700513 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
514 int32_t* outValue) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800515
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700516 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Chris Ye989bb932020-07-04 16:18:59 -0700517 uint8_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800518
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700519 std::vector<RawEvent> getEvents(int timeoutMillis) override final;
Chris Ye989bb932020-07-04 16:18:59 -0700520 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800521
Chris Ye989bb932020-07-04 16:18:59 -0700522 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override final;
Arthur Hungcb40a002021-08-03 14:31:01 +0000523 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700524 bool hasLed(int32_t deviceId, int32_t led) const override final;
525 void setLedState(int32_t deviceId, int32_t led, bool on) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800526
Chris Ye989bb932020-07-04 16:18:59 -0700527 void getVirtualKeyDefinitions(
528 int32_t deviceId,
529 std::vector<VirtualKeyDefinition>& outVirtualKeys) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800530
Chris Ye3a1e4462020-08-12 10:13:15 -0700531 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(
532 int32_t deviceId) const override final;
533 bool setKeyboardLayoutOverlay(int32_t deviceId,
534 std::shared_ptr<KeyCharacterMap> map) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800535
Chris Ye989bb932020-07-04 16:18:59 -0700536 void vibrate(int32_t deviceId, const VibrationElement& effect) override final;
537 void cancelVibrate(int32_t deviceId) override final;
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000538 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800539
Chris Ye989bb932020-07-04 16:18:59 -0700540 void requestReopenDevices() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800541
Chris Ye989bb932020-07-04 16:18:59 -0700542 void wake() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800543
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000544 void dump(std::string& dump) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800545
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000546 void monitor() const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700547
Chris Yee2b1e5c2021-03-10 22:45:12 -0800548 std::optional<int32_t> getBatteryCapacity(int32_t deviceId,
549 int32_t batteryId) const override final;
Kim Low03ea0352020-11-06 12:45:07 -0800550
Chris Yee2b1e5c2021-03-10 22:45:12 -0800551 std::optional<int32_t> getBatteryStatus(int32_t deviceId,
552 int32_t batteryId) const override final;
Kim Low03ea0352020-11-06 12:45:07 -0800553
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000554 bool isDeviceEnabled(int32_t deviceId) const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700555
556 status_t enableDevice(int32_t deviceId) override final;
557
558 status_t disableDevice(int32_t deviceId) override final;
559
560 ~EventHub() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800561
562private:
Prabir Pradhancb42b472022-08-23 16:01:19 +0000563 // Holds information about the sysfs device associated with the Device.
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700564 struct AssociatedDevice {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800565 // The sysfs root path of the misc device.
566 std::filesystem::path sysfsRootPath;
Prabir Pradhancb42b472022-08-23 16:01:19 +0000567 std::unordered_map<int32_t /*batteryId*/, RawBatteryInfo> batteryInfos;
568 std::unordered_map<int32_t /*lightId*/, RawLightInfo> lightInfos;
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000569 std::optional<RawLayoutInfo> layoutInfo;
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000570
571 bool operator==(const AssociatedDevice&) const = default;
572 bool operator!=(const AssociatedDevice&) const = default;
573 std::string dump() const;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800574 };
575
Michael Wrightd02c5b62014-02-10 15:10:22 -0800576 struct Device {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700577 int fd; // may be -1 if device is closed
Michael Wrightd02c5b62014-02-10 15:10:22 -0800578 const int32_t id;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100579 const std::string path;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800580 const InputDeviceIdentifier identifier;
581
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -0800582 std::unique_ptr<TouchVideoDevice> videoDevice;
583
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700584 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800585
Chris Ye66fbac32020-07-06 20:36:43 -0700586 BitArray<KEY_MAX> keyBitmask;
587 BitArray<KEY_MAX> keyState;
588 BitArray<ABS_MAX> absBitmask;
589 BitArray<REL_MAX> relBitmask;
590 BitArray<SW_MAX> swBitmask;
591 BitArray<SW_MAX> swState;
592 BitArray<LED_MAX> ledBitmask;
593 BitArray<FF_MAX> ffBitmask;
594 BitArray<INPUT_PROP_MAX> propBitmask;
Chris Yef59a2f42020-10-16 12:55:26 -0700595 BitArray<MSC_MAX> mscBitmask;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100597 std::string configurationFile;
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500598 std::unique_ptr<PropertyMap> configuration;
Siarhei Vishniakou3e78dec2019-02-20 16:21:46 -0600599 std::unique_ptr<VirtualKeyMap> virtualKeyMap;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800600 KeyMap keyMap;
601
Michael Wrightd02c5b62014-02-10 15:10:22 -0800602 bool ffEffectPlaying;
603 int16_t ffEffectId; // initially -1
604
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700605 // A shared_ptr of a device associated with the input device.
Prabir Pradhan51894782022-08-23 16:29:10 +0000606 // The input devices that have the same sysfs path have the same associated device.
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000607 std::shared_ptr<const AssociatedDevice> associatedDevice;
Kim Low03ea0352020-11-06 12:45:07 -0800608
Michael Wrightd02c5b62014-02-10 15:10:22 -0800609 int32_t controllerNumber;
610
Prabir Pradhancb42b472022-08-23 16:01:19 +0000611 Device(int fd, int32_t id, std::string path, InputDeviceIdentifier identifier,
612 std::shared_ptr<const AssociatedDevice> assocDev);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800613 ~Device();
614
615 void close();
616
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700617 bool enabled; // initially true
618 status_t enable();
619 status_t disable();
Chris Ye989bb932020-07-04 16:18:59 -0700620 bool hasValidFd() const;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700621 const bool isVirtual; // set if fd < 0 is passed to constructor
Michael Wrightd02c5b62014-02-10 15:10:22 -0800622
Chris Ye3a1e4462020-08-12 10:13:15 -0700623 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const;
Chris Ye66fbac32020-07-06 20:36:43 -0700624
625 template <std::size_t N>
Chris Ye989bb932020-07-04 16:18:59 -0700626 status_t readDeviceBitMask(unsigned long ioctlCode, BitArray<N>& bitArray);
Chris Ye66fbac32020-07-06 20:36:43 -0700627
Chris Ye989bb932020-07-04 16:18:59 -0700628 void configureFd();
629 bool hasKeycodeLocked(int keycode) const;
630 void loadConfigurationLocked();
631 bool loadVirtualKeyMapLocked();
632 status_t loadKeyMapLocked();
633 bool isExternalDeviceLocked();
634 bool deviceHasMicLocked();
635 void setLedForControllerLocked();
636 status_t mapLed(int32_t led, int32_t* outScanCode) const;
637 void setLedStateLocked(int32_t led, bool on);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800638 };
639
Siarhei Vishniakoua4c502a2021-02-05 00:45:20 +0000640 /**
641 * Create a new device for the provided path.
642 */
Chris Yed3fef462021-03-07 17:10:08 -0800643 void openDeviceLocked(const std::string& devicePath) REQUIRES(mLock);
644 void openVideoDeviceLocked(const std::string& devicePath) REQUIRES(mLock);
Siarhei Vishniakouf49608d2020-08-20 19:18:21 -0500645 /**
646 * Try to associate a video device with an input device. If the association succeeds,
647 * the videoDevice is moved into the input device. 'videoDevice' will become null if this
648 * happens.
649 * Return true if the association succeeds.
650 * Return false otherwise.
651 */
Chris Yed3fef462021-03-07 17:10:08 -0800652 bool tryAddVideoDeviceLocked(Device& device, std::unique_ptr<TouchVideoDevice>& videoDevice)
653 REQUIRES(mLock);
654 void createVirtualKeyboardLocked() REQUIRES(mLock);
655 void addDeviceLocked(std::unique_ptr<Device> device) REQUIRES(mLock);
656 void assignDescriptorLocked(InputDeviceIdentifier& identifier) REQUIRES(mLock);
Prabir Pradhancb42b472022-08-23 16:01:19 +0000657 std::shared_ptr<const AssociatedDevice> obtainAssociatedDeviceLocked(
658 const std::filesystem::path& devicePath) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800659
Chris Yed3fef462021-03-07 17:10:08 -0800660 void closeDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock);
661 void closeVideoDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock);
662 void closeDeviceLocked(Device& device) REQUIRES(mLock);
663 void closeAllDevicesLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800664
Siarhei Vishniakou25920312018-12-12 15:24:44 -0800665 status_t registerFdForEpoll(int fd);
666 status_t unregisterFdFromEpoll(int fd);
Chris Yed3fef462021-03-07 17:10:08 -0800667 status_t registerDeviceForEpollLocked(Device& device) REQUIRES(mLock);
668 void registerVideoDeviceForEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock);
669 status_t unregisterDeviceFromEpollLocked(Device& device) REQUIRES(mLock);
670 void unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700671
Chris Yed3fef462021-03-07 17:10:08 -0800672 status_t scanDirLocked(const std::string& dirname) REQUIRES(mLock);
673 status_t scanVideoDirLocked(const std::string& dirname) REQUIRES(mLock);
674 void scanDevicesLocked() REQUIRES(mLock);
Prabir Pradhan952e65b2022-06-23 17:49:55 +0000675 base::Result<void> readNotifyLocked() REQUIRES(mLock);
676 void handleNotifyEventLocked(const inotify_event&) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800677
Chris Yed3fef462021-03-07 17:10:08 -0800678 Device* getDeviceLocked(int32_t deviceId) const REQUIRES(mLock);
679 Device* getDeviceByPathLocked(const std::string& devicePath) const REQUIRES(mLock);
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700680 /**
681 * Look through all available fd's (both for input devices and for video devices),
682 * and return the device pointer.
683 */
Chris Yed3fef462021-03-07 17:10:08 -0800684 Device* getDeviceByFdLocked(int fd) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800685
Chris Yed3fef462021-03-07 17:10:08 -0800686 int32_t getNextControllerNumberLocked(const std::string& name) REQUIRES(mLock);
Josh Bartel938632f2022-07-19 15:34:22 -0500687
688 bool hasDeviceWithDescriptorLocked(const std::string& descriptor) const REQUIRES(mLock);
689
Chris Yed3fef462021-03-07 17:10:08 -0800690 void releaseControllerNumberLocked(int32_t num) REQUIRES(mLock);
691 void reportDeviceAddedForStatisticsLocked(const InputDeviceIdentifier& identifier,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700692 ftl::Flags<InputDeviceClass> classes) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800693
Chris Yee2b1e5c2021-03-10 22:45:12 -0800694 const std::unordered_map<int32_t, RawBatteryInfo>& getBatteryInfoLocked(int32_t deviceId) const
695 REQUIRES(mLock);
696
697 const std::unordered_map<int32_t, RawLightInfo>& getLightInfoLocked(int32_t deviceId) const
698 REQUIRES(mLock);
699
Usama Arifb27c8e62021-06-03 16:44:09 +0100700 void addDeviceInputInotify();
701 void addDeviceInotify();
702
Michael Wrightd02c5b62014-02-10 15:10:22 -0800703 // Protect all internal state.
Chris Ye1c2e0892020-11-30 21:41:44 -0800704 mutable std::mutex mLock;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800705
706 // The actual id of the built-in keyboard, or NO_BUILT_IN_KEYBOARD if none.
707 // EventHub remaps the built-in keyboard to id 0 externally as required by the API.
708 enum {
709 // Must not conflict with any other assigned device ids, including
710 // the virtual keyboard id (-1).
711 NO_BUILT_IN_KEYBOARD = -2,
712 };
713 int32_t mBuiltInKeyboardId;
714
715 int32_t mNextDeviceId;
716
717 BitSet32 mControllerNumbers;
718
Chris Ye989bb932020-07-04 16:18:59 -0700719 std::unordered_map<int32_t, std::unique_ptr<Device>> mDevices;
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800720 /**
721 * Video devices that report touchscreen heatmap, but have not (yet) been paired
722 * with a specific input device. Video device discovery is independent from input device
723 * discovery, so the two types of devices could be found in any order.
724 * Ideally, video devices in this queue do not have an open fd, or at least aren't
725 * actively streaming.
726 */
727 std::vector<std::unique_ptr<TouchVideoDevice>> mUnattachedVideoDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800728
Chris Ye989bb932020-07-04 16:18:59 -0700729 std::vector<std::unique_ptr<Device>> mOpeningDevices;
730 std::vector<std::unique_ptr<Device>> mClosingDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800731
732 bool mNeedToSendFinishedDeviceScan;
733 bool mNeedToReopenDevices;
734 bool mNeedToScanDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100735 std::vector<std::string> mExcludedDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800736
737 int mEpollFd;
738 int mINotifyFd;
739 int mWakeReadPipeFd;
740 int mWakeWritePipeFd;
741
Usama Arifb27c8e62021-06-03 16:44:09 +0100742 int mDeviceInputWd;
743 int mDeviceWd = -1;
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800744
Michael Wrightd02c5b62014-02-10 15:10:22 -0800745 // Maximum number of signalled FDs to handle at a time.
746 static const int EPOLL_MAX_EVENTS = 16;
747
748 // The array of pending epoll events and the index of the next event to be handled.
749 struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS];
750 size_t mPendingEventCount;
751 size_t mPendingEventIndex;
752 bool mPendingINotify;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800753};
754
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700755} // namespace android