blob: 8e5f15f3382a9af4e2a7372ee68f585959af45a8 [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"
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000045#include "android/hardware/input/InputDeviceCountryCode.h"
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080046
Prabir Pradhan952e65b2022-06-23 17:49:55 +000047struct inotify_event;
48
Michael Wrightd02c5b62014-02-10 15:10:22 -080049namespace android {
50
Chris Ye3fdbfef2021-01-06 18:45:18 -080051/* Number of colors : {red, green, blue} */
52static constexpr size_t COLOR_NUM = 3;
Michael Wrightd02c5b62014-02-10 15:10:22 -080053/*
54 * A raw event as retrieved from the EventHub.
55 */
56struct RawEvent {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000057 // Time when the event happened
Michael Wrightd02c5b62014-02-10 15:10:22 -080058 nsecs_t when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000059 // Time when the event was read by EventHub. Only populated for input events.
60 // For other events (device added/removed/etc), this value is undefined and should not be read.
61 nsecs_t readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -080062 int32_t deviceId;
63 int32_t type;
64 int32_t code;
65 int32_t value;
66};
67
68/* Describes an absolute axis. */
69struct RawAbsoluteAxisInfo {
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000070 bool valid{false}; // true if the information is valid, false otherwise
Michael Wrightd02c5b62014-02-10 15:10:22 -080071
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000072 int32_t minValue{}; // minimum value
73 int32_t maxValue{}; // maximum value
74 int32_t flat{}; // center flat position, eg. flat == 8 means center is between -8 and 8
75 int32_t fuzz{}; // error tolerance, eg. fuzz == 4 means value is +/- 4 due to noise
76 int32_t resolution{}; // resolution in units per mm or radians per mm
Michael Wrightd02c5b62014-02-10 15:10:22 -080077
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000078 inline void clear() { *this = RawAbsoluteAxisInfo(); }
Michael Wrightd02c5b62014-02-10 15:10:22 -080079};
80
81/*
82 * Input device classes.
83 */
Chris Ye1b0c7342020-07-28 21:57:03 -070084enum class InputDeviceClass : uint32_t {
Michael Wrightd02c5b62014-02-10 15:10:22 -080085 /* The input device is a keyboard or has buttons. */
Chris Ye1b0c7342020-07-28 21:57:03 -070086 KEYBOARD = 0x00000001,
Michael Wrightd02c5b62014-02-10 15:10:22 -080087
88 /* The input device is an alpha-numeric keyboard (not just a dial pad). */
Chris Ye1b0c7342020-07-28 21:57:03 -070089 ALPHAKEY = 0x00000002,
Michael Wrightd02c5b62014-02-10 15:10:22 -080090
91 /* The input device is a touchscreen or a touchpad (either single-touch or multi-touch). */
Chris Ye1b0c7342020-07-28 21:57:03 -070092 TOUCH = 0x00000004,
Michael Wrightd02c5b62014-02-10 15:10:22 -080093
94 /* The input device is a cursor device such as a trackball or mouse. */
Chris Ye1b0c7342020-07-28 21:57:03 -070095 CURSOR = 0x00000008,
Michael Wrightd02c5b62014-02-10 15:10:22 -080096
97 /* The input device is a multi-touch touchscreen. */
Chris Ye1b0c7342020-07-28 21:57:03 -070098 TOUCH_MT = 0x00000010,
Michael Wrightd02c5b62014-02-10 15:10:22 -080099
100 /* The input device is a directional pad (implies keyboard, has DPAD keys). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700101 DPAD = 0x00000020,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800102
103 /* The input device is a gamepad (implies keyboard, has BUTTON keys). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700104 GAMEPAD = 0x00000040,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800105
106 /* The input device has switches. */
Chris Ye1b0c7342020-07-28 21:57:03 -0700107 SWITCH = 0x00000080,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800108
109 /* The input device is a joystick (implies gamepad, has joystick absolute axes). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700110 JOYSTICK = 0x00000100,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800111
112 /* The input device has a vibrator (supports FF_RUMBLE). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700113 VIBRATOR = 0x00000200,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800114
Tim Kilbourn063ff532015-04-08 10:26:18 -0700115 /* The input device has a microphone. */
Chris Ye1b0c7342020-07-28 21:57:03 -0700116 MIC = 0x00000400,
Tim Kilbourn063ff532015-04-08 10:26:18 -0700117
Michael Wright842500e2015-03-13 17:32:02 -0700118 /* The input device is an external stylus (has data we want to fuse with touch data). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700119 EXTERNAL_STYLUS = 0x00000800,
Michael Wright842500e2015-03-13 17:32:02 -0700120
Prashant Malani1941ff52015-08-11 18:29:28 -0700121 /* The input device has a rotary encoder */
Chris Ye1b0c7342020-07-28 21:57:03 -0700122 ROTARY_ENCODER = 0x00001000,
Prashant Malani1941ff52015-08-11 18:29:28 -0700123
Chris Yef59a2f42020-10-16 12:55:26 -0700124 /* The input device has a sensor like accelerometer, gyro, etc */
125 SENSOR = 0x00002000,
126
Kim Low03ea0352020-11-06 12:45:07 -0800127 /* The input device has a battery */
128 BATTERY = 0x00004000,
129
Chris Ye3fdbfef2021-01-06 18:45:18 -0800130 /* The input device has sysfs controllable lights */
131 LIGHT = 0x00008000,
132
Michael Wrightd02c5b62014-02-10 15:10:22 -0800133 /* The input device is virtual (not a real device, not part of UI configuration). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700134 VIRTUAL = 0x40000000,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800135
136 /* The input device is external (not built-in). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700137 EXTERNAL = 0x80000000,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800138};
139
Chris Ye3fdbfef2021-01-06 18:45:18 -0800140enum class SysfsClass : uint32_t {
141 POWER_SUPPLY = 0,
142 LEDS = 1,
Dominik Laskowski75788452021-02-09 18:51:25 -0800143
144 ftl_last = LEDS
Chris Ye3fdbfef2021-01-06 18:45:18 -0800145};
146
147enum class LightColor : uint32_t {
148 RED = 0,
149 GREEN = 1,
150 BLUE = 2,
151};
152
153enum class InputLightClass : uint32_t {
154 /* The input light has brightness node. */
155 BRIGHTNESS = 0x00000001,
156 /* The input light has red name. */
157 RED = 0x00000002,
158 /* The input light has green name. */
159 GREEN = 0x00000004,
160 /* The input light has blue name. */
161 BLUE = 0x00000008,
162 /* The input light has global name. */
163 GLOBAL = 0x00000010,
164 /* The input light has multi index node. */
165 MULTI_INDEX = 0x00000020,
166 /* The input light has multi intensity node. */
167 MULTI_INTENSITY = 0x00000040,
168 /* The input light has max brightness node. */
169 MAX_BRIGHTNESS = 0x00000080,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +0000170 /* The input light has kbd_backlight name */
171 KEYBOARD_BACKLIGHT = 0x00000100,
Chris Ye3fdbfef2021-01-06 18:45:18 -0800172};
173
Chris Yee2b1e5c2021-03-10 22:45:12 -0800174enum class InputBatteryClass : uint32_t {
175 /* The input device battery has capacity node. */
176 CAPACITY = 0x00000001,
177 /* The input device battery has capacity_level node. */
178 CAPACITY_LEVEL = 0x00000002,
179 /* The input device battery has status node. */
180 STATUS = 0x00000004,
181};
182
Chris Ye3fdbfef2021-01-06 18:45:18 -0800183/* Describes a raw light. */
184struct RawLightInfo {
185 int32_t id;
186 std::string name;
187 std::optional<int32_t> maxBrightness;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700188 ftl::Flags<InputLightClass> flags;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800189 std::array<int32_t, COLOR_NUM> rgbIndex;
190 std::filesystem::path path;
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000191
192 bool operator==(const RawLightInfo&) const = default;
193 bool operator!=(const RawLightInfo&) const = default;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800194};
195
Chris Yee2b1e5c2021-03-10 22:45:12 -0800196/* Describes a raw battery. */
197struct RawBatteryInfo {
198 int32_t id;
199 std::string name;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700200 ftl::Flags<InputBatteryClass> flags;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800201 std::filesystem::path path;
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000202
203 bool operator==(const RawBatteryInfo&) const = default;
204 bool operator!=(const RawBatteryInfo&) const = default;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800205};
206
Michael Wrightd02c5b62014-02-10 15:10:22 -0800207/*
208 * Gets the class that owns an axis, in cases where multiple classes might claim
209 * the same axis for different purposes.
210 */
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700211extern ftl::Flags<InputDeviceClass> getAbsAxisUsage(int32_t axis,
212 ftl::Flags<InputDeviceClass> deviceClasses);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800213
214/*
215 * Grand Central Station for events.
216 *
217 * The event hub aggregates input events received across all known input
218 * devices on the system, including devices that may be emulated by the simulator
219 * environment. In addition, the event hub generates fake input events to indicate
220 * when devices are added or removed.
221 *
222 * The event hub provides a stream of input events (via the getEvent function).
223 * It also supports querying the current actual state of input devices such as identifying
224 * which keys are currently down. Finally, the event hub keeps track of the capabilities of
225 * individual input devices, such as their class and the set of key codes that they support.
226 */
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700227class EventHubInterface {
228public:
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700229 EventHubInterface() {}
230 virtual ~EventHubInterface() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800231
Michael Wrightd02c5b62014-02-10 15:10:22 -0800232 // Synthetic raw event type codes produced when devices are added or removed.
233 enum {
234 // Sent when a device is added.
235 DEVICE_ADDED = 0x10000000,
236 // Sent when a device is removed.
237 DEVICE_REMOVED = 0x20000000,
238 // Sent when all added/removed devices from the most recent scan have been reported.
239 // This event is always sent at least once.
240 FINISHED_DEVICE_SCAN = 0x30000000,
241
242 FIRST_SYNTHETIC_EVENT = DEVICE_ADDED,
243 };
244
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700245 virtual ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800246
247 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const = 0;
248
249 virtual int32_t getDeviceControllerNumber(int32_t deviceId) const = 0;
250
251 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const = 0;
252
253 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700254 RawAbsoluteAxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800255
256 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const = 0;
257
258 virtual bool hasInputProperty(int32_t deviceId, int property) const = 0;
259
Chris Yef59a2f42020-10-16 12:55:26 -0700260 virtual bool hasMscEvent(int32_t deviceId, int mscEvent) const = 0;
261
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700262 virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
263 int32_t metaState, int32_t* outKeycode, int32_t* outMetaState,
264 uint32_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800265
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700266 virtual status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800267
268 // Sets devices that are excluded from opening.
269 // This can be used to ignore input devices for sensors.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100270 virtual void setExcludedDevices(const std::vector<std::string>& devices) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800271
272 /*
273 * Wait for events to become available and returns them.
274 * After returning, the EventHub holds onto a wake lock until the next call to getEvent.
275 * This ensures that the device will not go to sleep while the event is being processed.
276 * If the device needs to remain awake longer than that, then the caller is responsible
277 * for taking care of it (say, by poking the power manager user activity timer).
278 *
279 * The timeout is advisory only. If the device is asleep, it will not wake just to
280 * service the timeout.
281 *
282 * Returns the number of events obtained, or 0 if the timeout expired.
283 */
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700284 virtual std::vector<RawEvent> getEvents(int timeoutMillis) = 0;
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800285 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) = 0;
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000286 virtual base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
287 int32_t deviceId, int32_t absCode) const = 0;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800288 // Raw batteries are sysfs power_supply nodes we found from the EventHub device sysfs node,
289 // containing the raw info of the sysfs node structure.
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000290 virtual std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const = 0;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800291 virtual std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000292 int32_t BatteryId) const = 0;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800293
Chris Ye3fdbfef2021-01-06 18:45:18 -0800294 // Raw lights are sysfs led light nodes we found from the EventHub device sysfs node,
295 // containing the raw info of the sysfs node structure.
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000296 virtual std::vector<int32_t> getRawLightIds(int32_t deviceId) const = 0;
297 virtual std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId,
298 int32_t lightId) const = 0;
299 virtual std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const = 0;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800300 virtual void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) = 0;
301 virtual std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000302 int32_t deviceId, int32_t lightId) const = 0;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800303 virtual void setLightIntensities(int32_t deviceId, int32_t lightId,
304 std::unordered_map<LightColor, int32_t> intensities) = 0;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000305 /* Query Country code associated with the input device. */
306 virtual hardware::input::InputDeviceCountryCode getCountryCode(int32_t deviceId) const = 0;
307 /* Query current input state. */
Michael Wrightd02c5b62014-02-10 15:10:22 -0800308 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0;
309 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0;
310 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0;
311 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700312 int32_t* outValue) const = 0;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100313 virtual int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800314
315 /*
316 * Examine key input devices for specific framework keycode support
317 */
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700318 virtual bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700319 uint8_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800320
321 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const = 0;
Arthur Hungcb40a002021-08-03 14:31:01 +0000322 virtual bool hasKeyCode(int32_t deviceId, int32_t keyCode) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800323
324 /* LED related functions expect Android LED constants, not scan codes or HID usages */
325 virtual bool hasLed(int32_t deviceId, int32_t led) const = 0;
326 virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0;
327
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700328 virtual void getVirtualKeyDefinitions(
329 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800330
Chris Ye3a1e4462020-08-12 10:13:15 -0700331 virtual const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const = 0;
332 virtual bool setKeyboardLayoutOverlay(int32_t deviceId,
333 std::shared_ptr<KeyCharacterMap> map) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800334
335 /* Control the vibrator. */
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000336 virtual void vibrate(int32_t deviceId, const VibrationElement& effect) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800337 virtual void cancelVibrate(int32_t deviceId) = 0;
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000338 virtual std::vector<int32_t> getVibratorIds(int32_t deviceId) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800339
Kim Low03ea0352020-11-06 12:45:07 -0800340 /* Query battery level. */
Chris Yee2b1e5c2021-03-10 22:45:12 -0800341 virtual std::optional<int32_t> getBatteryCapacity(int32_t deviceId,
342 int32_t batteryId) const = 0;
Kim Low03ea0352020-11-06 12:45:07 -0800343
344 /* Query battery status. */
Chris Yee2b1e5c2021-03-10 22:45:12 -0800345 virtual std::optional<int32_t> getBatteryStatus(int32_t deviceId, int32_t batteryId) const = 0;
Kim Low03ea0352020-11-06 12:45:07 -0800346
Michael Wrightd02c5b62014-02-10 15:10:22 -0800347 /* Requests the EventHub to reopen all input devices on the next call to getEvents(). */
348 virtual void requestReopenDevices() = 0;
349
350 /* Wakes up getEvents() if it is blocked on a read. */
351 virtual void wake() = 0;
352
353 /* Dump EventHub state to a string. */
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000354 virtual void dump(std::string& dump) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800355
356 /* Called by the heatbeat to ensures that the reader has not deadlocked. */
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000357 virtual void monitor() const = 0;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700358
359 /* Return true if the device is enabled. */
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000360 virtual bool isDeviceEnabled(int32_t deviceId) const = 0;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700361
362 /* Enable an input device */
363 virtual status_t enableDevice(int32_t deviceId) = 0;
364
365 /* Disable an input device. Closes file descriptor to that device. */
366 virtual status_t disableDevice(int32_t deviceId) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800367};
368
Chris Ye66fbac32020-07-06 20:36:43 -0700369template <std::size_t BITS>
370class BitArray {
371 /* Array element type and vector of element type. */
372 using Element = std::uint32_t;
373 /* Number of bits in each BitArray element. */
374 static constexpr size_t WIDTH = sizeof(Element) * CHAR_BIT;
375 /* Number of elements to represent a bit array of the specified size of bits. */
376 static constexpr size_t COUNT = (BITS + WIDTH - 1) / WIDTH;
377
378public:
379 /* BUFFER type declaration for BitArray */
380 using Buffer = std::array<Element, COUNT>;
381 /* To tell if a bit is set in array, it selects an element from the array, and test
382 * if the relevant bit set.
383 * Note the parameter "bit" is an index to the bit, 0 <= bit < BITS.
384 */
385 inline bool test(size_t bit) const {
386 return (bit < BITS) ? mData[bit / WIDTH].test(bit % WIDTH) : false;
387 }
388 /* Returns total number of bytes needed for the array */
389 inline size_t bytes() { return (BITS + CHAR_BIT - 1) / CHAR_BIT; }
390 /* Returns true if array contains any non-zero bit from the range defined by start and end
391 * bit index [startIndex, endIndex).
392 */
393 bool any(size_t startIndex, size_t endIndex) {
394 if (startIndex >= endIndex || startIndex > BITS || endIndex > BITS + 1) {
395 ALOGE("Invalid start/end index. start = %zu, end = %zu, total bits = %zu", startIndex,
396 endIndex, BITS);
397 return false;
398 }
399 size_t se = startIndex / WIDTH; // Start of element
400 size_t ee = endIndex / WIDTH; // End of element
401 size_t si = startIndex % WIDTH; // Start index in start element
402 size_t ei = endIndex % WIDTH; // End index in end element
403 // Need to check first unaligned bitset for any non zero bit
404 if (si > 0) {
405 size_t nBits = se == ee ? ei - si : WIDTH - si;
406 // Generate the mask of interested bit range
407 Element mask = ((1 << nBits) - 1) << si;
408 if (mData[se++].to_ulong() & mask) {
409 return true;
410 }
411 }
412 // Check whole bitset for any bit set
413 for (; se < ee; se++) {
414 if (mData[se].any()) {
415 return true;
416 }
417 }
418 // Need to check last unaligned bitset for any non zero bit
419 if (ei > 0 && se <= ee) {
420 // Generate the mask of interested bit range
421 Element mask = (1 << ei) - 1;
422 if (mData[se].to_ulong() & mask) {
423 return true;
424 }
425 }
426 return false;
427 }
428 /* Load bit array values from buffer */
429 void loadFromBuffer(const Buffer& buffer) {
430 for (size_t i = 0; i < COUNT; i++) {
431 mData[i] = std::bitset<WIDTH>(buffer[i]);
432 }
433 }
434
435private:
436 std::array<std::bitset<WIDTH>, COUNT> mData;
437};
438
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700439class EventHub : public EventHubInterface {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800440public:
441 EventHub();
442
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700443 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800444
Chris Ye989bb932020-07-04 16:18:59 -0700445 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800446
Chris Ye989bb932020-07-04 16:18:59 -0700447 int32_t getDeviceControllerNumber(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800448
Chris Ye989bb932020-07-04 16:18:59 -0700449 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800450
Chris Ye989bb932020-07-04 16:18:59 -0700451 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
452 RawAbsoluteAxisInfo* outAxisInfo) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800453
Chris Ye989bb932020-07-04 16:18:59 -0700454 bool hasRelativeAxis(int32_t deviceId, int axis) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800455
Chris Ye989bb932020-07-04 16:18:59 -0700456 bool hasInputProperty(int32_t deviceId, int property) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800457
Chris Yef59a2f42020-10-16 12:55:26 -0700458 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final;
459
Chris Ye989bb932020-07-04 16:18:59 -0700460 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
461 int32_t* outKeycode, int32_t* outMetaState,
462 uint32_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800463
Chris Ye989bb932020-07-04 16:18:59 -0700464 status_t mapAxis(int32_t deviceId, int32_t scanCode,
465 AxisInfo* outAxisInfo) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800466
Chris Yef59a2f42020-10-16 12:55:26 -0700467 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000468 int32_t deviceId, int32_t absCode) const override final;
Chris Yef59a2f42020-10-16 12:55:26 -0700469
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000470 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override final;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800471 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000472 int32_t BatteryId) const override final;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800473
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000474 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800475
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000476 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId,
477 int32_t lightId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800478
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000479 std::optional<int32_t> getLightBrightness(int32_t deviceId,
480 int32_t lightId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800481 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override final;
482 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000483 int32_t deviceId, int32_t lightId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800484 void setLightIntensities(int32_t deviceId, int32_t lightId,
485 std::unordered_map<LightColor, int32_t> intensities) override final;
486
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000487 hardware::input::InputDeviceCountryCode getCountryCode(int32_t deviceId) const override final;
488
Chris Ye989bb932020-07-04 16:18:59 -0700489 void setExcludedDevices(const std::vector<std::string>& devices) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800490
Chris Ye989bb932020-07-04 16:18:59 -0700491 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override final;
492 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override final;
493 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override final;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100494 int32_t getKeyCodeForKeyLocation(int32_t deviceId,
495 int32_t locationKeyCode) const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700496 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
497 int32_t* outValue) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800498
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700499 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Chris Ye989bb932020-07-04 16:18:59 -0700500 uint8_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800501
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700502 std::vector<RawEvent> getEvents(int timeoutMillis) override final;
Chris Ye989bb932020-07-04 16:18:59 -0700503 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800504
Chris Ye989bb932020-07-04 16:18:59 -0700505 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override final;
Arthur Hungcb40a002021-08-03 14:31:01 +0000506 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700507 bool hasLed(int32_t deviceId, int32_t led) const override final;
508 void setLedState(int32_t deviceId, int32_t led, bool on) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800509
Chris Ye989bb932020-07-04 16:18:59 -0700510 void getVirtualKeyDefinitions(
511 int32_t deviceId,
512 std::vector<VirtualKeyDefinition>& outVirtualKeys) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800513
Chris Ye3a1e4462020-08-12 10:13:15 -0700514 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(
515 int32_t deviceId) const override final;
516 bool setKeyboardLayoutOverlay(int32_t deviceId,
517 std::shared_ptr<KeyCharacterMap> map) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800518
Chris Ye989bb932020-07-04 16:18:59 -0700519 void vibrate(int32_t deviceId, const VibrationElement& effect) override final;
520 void cancelVibrate(int32_t deviceId) override final;
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000521 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800522
Chris Ye989bb932020-07-04 16:18:59 -0700523 void requestReopenDevices() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800524
Chris Ye989bb932020-07-04 16:18:59 -0700525 void wake() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800526
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000527 void dump(std::string& dump) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800528
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000529 void monitor() const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700530
Chris Yee2b1e5c2021-03-10 22:45:12 -0800531 std::optional<int32_t> getBatteryCapacity(int32_t deviceId,
532 int32_t batteryId) const override final;
Kim Low03ea0352020-11-06 12:45:07 -0800533
Chris Yee2b1e5c2021-03-10 22:45:12 -0800534 std::optional<int32_t> getBatteryStatus(int32_t deviceId,
535 int32_t batteryId) const override final;
Kim Low03ea0352020-11-06 12:45:07 -0800536
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000537 bool isDeviceEnabled(int32_t deviceId) const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700538
539 status_t enableDevice(int32_t deviceId) override final;
540
541 status_t disableDevice(int32_t deviceId) override final;
542
543 ~EventHub() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800544
545private:
Prabir Pradhancb42b472022-08-23 16:01:19 +0000546 // Holds information about the sysfs device associated with the Device.
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700547 struct AssociatedDevice {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800548 // The sysfs root path of the misc device.
549 std::filesystem::path sysfsRootPath;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000550 hardware::input::InputDeviceCountryCode countryCode;
Prabir Pradhancb42b472022-08-23 16:01:19 +0000551 std::unordered_map<int32_t /*batteryId*/, RawBatteryInfo> batteryInfos;
552 std::unordered_map<int32_t /*lightId*/, RawLightInfo> lightInfos;
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000553
554 bool operator==(const AssociatedDevice&) const = default;
555 bool operator!=(const AssociatedDevice&) const = default;
556 std::string dump() const;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800557 };
558
Michael Wrightd02c5b62014-02-10 15:10:22 -0800559 struct Device {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700560 int fd; // may be -1 if device is closed
Michael Wrightd02c5b62014-02-10 15:10:22 -0800561 const int32_t id;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100562 const std::string path;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800563 const InputDeviceIdentifier identifier;
564
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -0800565 std::unique_ptr<TouchVideoDevice> videoDevice;
566
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700567 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800568
Chris Ye66fbac32020-07-06 20:36:43 -0700569 BitArray<KEY_MAX> keyBitmask;
570 BitArray<KEY_MAX> keyState;
571 BitArray<ABS_MAX> absBitmask;
572 BitArray<REL_MAX> relBitmask;
573 BitArray<SW_MAX> swBitmask;
574 BitArray<SW_MAX> swState;
575 BitArray<LED_MAX> ledBitmask;
576 BitArray<FF_MAX> ffBitmask;
577 BitArray<INPUT_PROP_MAX> propBitmask;
Chris Yef59a2f42020-10-16 12:55:26 -0700578 BitArray<MSC_MAX> mscBitmask;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800579
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100580 std::string configurationFile;
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500581 std::unique_ptr<PropertyMap> configuration;
Siarhei Vishniakou3e78dec2019-02-20 16:21:46 -0600582 std::unique_ptr<VirtualKeyMap> virtualKeyMap;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800583 KeyMap keyMap;
584
Michael Wrightd02c5b62014-02-10 15:10:22 -0800585 bool ffEffectPlaying;
586 int16_t ffEffectId; // initially -1
587
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700588 // A shared_ptr of a device associated with the input device.
Prabir Pradhan51894782022-08-23 16:29:10 +0000589 // The input devices that have the same sysfs path have the same associated device.
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000590 std::shared_ptr<const AssociatedDevice> associatedDevice;
Kim Low03ea0352020-11-06 12:45:07 -0800591
Michael Wrightd02c5b62014-02-10 15:10:22 -0800592 int32_t controllerNumber;
593
Prabir Pradhancb42b472022-08-23 16:01:19 +0000594 Device(int fd, int32_t id, std::string path, InputDeviceIdentifier identifier,
595 std::shared_ptr<const AssociatedDevice> assocDev);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596 ~Device();
597
598 void close();
599
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700600 bool enabled; // initially true
601 status_t enable();
602 status_t disable();
Chris Ye989bb932020-07-04 16:18:59 -0700603 bool hasValidFd() const;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700604 const bool isVirtual; // set if fd < 0 is passed to constructor
Michael Wrightd02c5b62014-02-10 15:10:22 -0800605
Chris Ye3a1e4462020-08-12 10:13:15 -0700606 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const;
Chris Ye66fbac32020-07-06 20:36:43 -0700607
608 template <std::size_t N>
Chris Ye989bb932020-07-04 16:18:59 -0700609 status_t readDeviceBitMask(unsigned long ioctlCode, BitArray<N>& bitArray);
Chris Ye66fbac32020-07-06 20:36:43 -0700610
Chris Ye989bb932020-07-04 16:18:59 -0700611 void configureFd();
612 bool hasKeycodeLocked(int keycode) const;
613 void loadConfigurationLocked();
614 bool loadVirtualKeyMapLocked();
615 status_t loadKeyMapLocked();
616 bool isExternalDeviceLocked();
617 bool deviceHasMicLocked();
618 void setLedForControllerLocked();
619 status_t mapLed(int32_t led, int32_t* outScanCode) const;
620 void setLedStateLocked(int32_t led, bool on);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800621 };
622
Siarhei Vishniakoua4c502a2021-02-05 00:45:20 +0000623 /**
624 * Create a new device for the provided path.
625 */
Chris Yed3fef462021-03-07 17:10:08 -0800626 void openDeviceLocked(const std::string& devicePath) REQUIRES(mLock);
627 void openVideoDeviceLocked(const std::string& devicePath) REQUIRES(mLock);
Siarhei Vishniakouf49608d2020-08-20 19:18:21 -0500628 /**
629 * Try to associate a video device with an input device. If the association succeeds,
630 * the videoDevice is moved into the input device. 'videoDevice' will become null if this
631 * happens.
632 * Return true if the association succeeds.
633 * Return false otherwise.
634 */
Chris Yed3fef462021-03-07 17:10:08 -0800635 bool tryAddVideoDeviceLocked(Device& device, std::unique_ptr<TouchVideoDevice>& videoDevice)
636 REQUIRES(mLock);
637 void createVirtualKeyboardLocked() REQUIRES(mLock);
638 void addDeviceLocked(std::unique_ptr<Device> device) REQUIRES(mLock);
639 void assignDescriptorLocked(InputDeviceIdentifier& identifier) REQUIRES(mLock);
Prabir Pradhancb42b472022-08-23 16:01:19 +0000640 std::shared_ptr<const AssociatedDevice> obtainAssociatedDeviceLocked(
641 const std::filesystem::path& devicePath) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800642
Chris Yed3fef462021-03-07 17:10:08 -0800643 void closeDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock);
644 void closeVideoDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock);
645 void closeDeviceLocked(Device& device) REQUIRES(mLock);
646 void closeAllDevicesLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800647
Siarhei Vishniakou25920312018-12-12 15:24:44 -0800648 status_t registerFdForEpoll(int fd);
649 status_t unregisterFdFromEpoll(int fd);
Chris Yed3fef462021-03-07 17:10:08 -0800650 status_t registerDeviceForEpollLocked(Device& device) REQUIRES(mLock);
651 void registerVideoDeviceForEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock);
652 status_t unregisterDeviceFromEpollLocked(Device& device) REQUIRES(mLock);
653 void unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700654
Chris Yed3fef462021-03-07 17:10:08 -0800655 status_t scanDirLocked(const std::string& dirname) REQUIRES(mLock);
656 status_t scanVideoDirLocked(const std::string& dirname) REQUIRES(mLock);
657 void scanDevicesLocked() REQUIRES(mLock);
Prabir Pradhan952e65b2022-06-23 17:49:55 +0000658 base::Result<void> readNotifyLocked() REQUIRES(mLock);
659 void handleNotifyEventLocked(const inotify_event&) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800660
Chris Yed3fef462021-03-07 17:10:08 -0800661 Device* getDeviceLocked(int32_t deviceId) const REQUIRES(mLock);
662 Device* getDeviceByPathLocked(const std::string& devicePath) const REQUIRES(mLock);
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700663 /**
664 * Look through all available fd's (both for input devices and for video devices),
665 * and return the device pointer.
666 */
Chris Yed3fef462021-03-07 17:10:08 -0800667 Device* getDeviceByFdLocked(int fd) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800668
Chris Yed3fef462021-03-07 17:10:08 -0800669 int32_t getNextControllerNumberLocked(const std::string& name) REQUIRES(mLock);
Josh Bartel938632f2022-07-19 15:34:22 -0500670
671 bool hasDeviceWithDescriptorLocked(const std::string& descriptor) const REQUIRES(mLock);
672
Chris Yed3fef462021-03-07 17:10:08 -0800673 void releaseControllerNumberLocked(int32_t num) REQUIRES(mLock);
674 void reportDeviceAddedForStatisticsLocked(const InputDeviceIdentifier& identifier,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700675 ftl::Flags<InputDeviceClass> classes) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800676
Chris Yee2b1e5c2021-03-10 22:45:12 -0800677 const std::unordered_map<int32_t, RawBatteryInfo>& getBatteryInfoLocked(int32_t deviceId) const
678 REQUIRES(mLock);
679
680 const std::unordered_map<int32_t, RawLightInfo>& getLightInfoLocked(int32_t deviceId) const
681 REQUIRES(mLock);
682
Usama Arifb27c8e62021-06-03 16:44:09 +0100683 void addDeviceInputInotify();
684 void addDeviceInotify();
685
Michael Wrightd02c5b62014-02-10 15:10:22 -0800686 // Protect all internal state.
Chris Ye1c2e0892020-11-30 21:41:44 -0800687 mutable std::mutex mLock;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800688
689 // The actual id of the built-in keyboard, or NO_BUILT_IN_KEYBOARD if none.
690 // EventHub remaps the built-in keyboard to id 0 externally as required by the API.
691 enum {
692 // Must not conflict with any other assigned device ids, including
693 // the virtual keyboard id (-1).
694 NO_BUILT_IN_KEYBOARD = -2,
695 };
696 int32_t mBuiltInKeyboardId;
697
698 int32_t mNextDeviceId;
699
700 BitSet32 mControllerNumbers;
701
Chris Ye989bb932020-07-04 16:18:59 -0700702 std::unordered_map<int32_t, std::unique_ptr<Device>> mDevices;
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800703 /**
704 * Video devices that report touchscreen heatmap, but have not (yet) been paired
705 * with a specific input device. Video device discovery is independent from input device
706 * discovery, so the two types of devices could be found in any order.
707 * Ideally, video devices in this queue do not have an open fd, or at least aren't
708 * actively streaming.
709 */
710 std::vector<std::unique_ptr<TouchVideoDevice>> mUnattachedVideoDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800711
Chris Ye989bb932020-07-04 16:18:59 -0700712 std::vector<std::unique_ptr<Device>> mOpeningDevices;
713 std::vector<std::unique_ptr<Device>> mClosingDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800714
715 bool mNeedToSendFinishedDeviceScan;
716 bool mNeedToReopenDevices;
717 bool mNeedToScanDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100718 std::vector<std::string> mExcludedDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800719
720 int mEpollFd;
721 int mINotifyFd;
722 int mWakeReadPipeFd;
723 int mWakeWritePipeFd;
724
Usama Arifb27c8e62021-06-03 16:44:09 +0100725 int mDeviceInputWd;
726 int mDeviceWd = -1;
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800727
Michael Wrightd02c5b62014-02-10 15:10:22 -0800728 // Maximum number of signalled FDs to handle at a time.
729 static const int EPOLL_MAX_EVENTS = 16;
730
731 // The array of pending epoll events and the index of the next event to be handled.
732 struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS];
733 size_t mPendingEventCount;
734 size_t mPendingEventIndex;
735 bool mPendingINotify;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800736};
737
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700738} // namespace android