blob: 8a844b2088cca026a7e0e0c70ac376cdeb074ec9 [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
Harry Cutts79cc9fa2022-10-28 15:32:39 +000097 /* The input device is a multi-touch touchscreen or touchpad. */
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
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000133 /* The input device is a touchpad, requiring an on-screen cursor. */
134 TOUCHPAD = 0x00010000,
135
Michael Wrightd02c5b62014-02-10 15:10:22 -0800136 /* The input device is virtual (not a real device, not part of UI configuration). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700137 VIRTUAL = 0x40000000,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800138
139 /* The input device is external (not built-in). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700140 EXTERNAL = 0x80000000,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800141};
142
Chris Ye3fdbfef2021-01-06 18:45:18 -0800143enum class SysfsClass : uint32_t {
144 POWER_SUPPLY = 0,
145 LEDS = 1,
Dominik Laskowski75788452021-02-09 18:51:25 -0800146
147 ftl_last = LEDS
Chris Ye3fdbfef2021-01-06 18:45:18 -0800148};
149
150enum class LightColor : uint32_t {
151 RED = 0,
152 GREEN = 1,
153 BLUE = 2,
154};
155
156enum class InputLightClass : uint32_t {
157 /* The input light has brightness node. */
158 BRIGHTNESS = 0x00000001,
159 /* The input light has red name. */
160 RED = 0x00000002,
161 /* The input light has green name. */
162 GREEN = 0x00000004,
163 /* The input light has blue name. */
164 BLUE = 0x00000008,
165 /* The input light has global name. */
166 GLOBAL = 0x00000010,
167 /* The input light has multi index node. */
168 MULTI_INDEX = 0x00000020,
169 /* The input light has multi intensity node. */
170 MULTI_INTENSITY = 0x00000040,
171 /* The input light has max brightness node. */
172 MAX_BRIGHTNESS = 0x00000080,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +0000173 /* The input light has kbd_backlight name */
174 KEYBOARD_BACKLIGHT = 0x00000100,
Chris Ye3fdbfef2021-01-06 18:45:18 -0800175};
176
Chris Yee2b1e5c2021-03-10 22:45:12 -0800177enum class InputBatteryClass : uint32_t {
178 /* The input device battery has capacity node. */
179 CAPACITY = 0x00000001,
180 /* The input device battery has capacity_level node. */
181 CAPACITY_LEVEL = 0x00000002,
182 /* The input device battery has status node. */
183 STATUS = 0x00000004,
184};
185
Chris Ye3fdbfef2021-01-06 18:45:18 -0800186/* Describes a raw light. */
187struct RawLightInfo {
188 int32_t id;
189 std::string name;
190 std::optional<int32_t> maxBrightness;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700191 ftl::Flags<InputLightClass> flags;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800192 std::array<int32_t, COLOR_NUM> rgbIndex;
193 std::filesystem::path path;
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000194
195 bool operator==(const RawLightInfo&) const = default;
196 bool operator!=(const RawLightInfo&) const = default;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800197};
198
Chris Yee2b1e5c2021-03-10 22:45:12 -0800199/* Describes a raw battery. */
200struct RawBatteryInfo {
201 int32_t id;
202 std::string name;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700203 ftl::Flags<InputBatteryClass> flags;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800204 std::filesystem::path path;
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000205
206 bool operator==(const RawBatteryInfo&) const = default;
207 bool operator!=(const RawBatteryInfo&) const = default;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800208};
209
Michael Wrightd02c5b62014-02-10 15:10:22 -0800210/*
211 * Gets the class that owns an axis, in cases where multiple classes might claim
212 * the same axis for different purposes.
213 */
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700214extern ftl::Flags<InputDeviceClass> getAbsAxisUsage(int32_t axis,
215 ftl::Flags<InputDeviceClass> deviceClasses);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800216
217/*
218 * Grand Central Station for events.
219 *
220 * The event hub aggregates input events received across all known input
221 * devices on the system, including devices that may be emulated by the simulator
222 * environment. In addition, the event hub generates fake input events to indicate
223 * when devices are added or removed.
224 *
225 * The event hub provides a stream of input events (via the getEvent function).
226 * It also supports querying the current actual state of input devices such as identifying
227 * which keys are currently down. Finally, the event hub keeps track of the capabilities of
228 * individual input devices, such as their class and the set of key codes that they support.
229 */
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700230class EventHubInterface {
231public:
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700232 EventHubInterface() {}
233 virtual ~EventHubInterface() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800234
Michael Wrightd02c5b62014-02-10 15:10:22 -0800235 // Synthetic raw event type codes produced when devices are added or removed.
236 enum {
237 // Sent when a device is added.
238 DEVICE_ADDED = 0x10000000,
239 // Sent when a device is removed.
240 DEVICE_REMOVED = 0x20000000,
241 // Sent when all added/removed devices from the most recent scan have been reported.
242 // This event is always sent at least once.
243 FINISHED_DEVICE_SCAN = 0x30000000,
244
245 FIRST_SYNTHETIC_EVENT = DEVICE_ADDED,
246 };
247
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700248 virtual ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800249
250 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const = 0;
251
252 virtual int32_t getDeviceControllerNumber(int32_t deviceId) const = 0;
253
254 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const = 0;
255
256 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700257 RawAbsoluteAxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800258
259 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const = 0;
260
261 virtual bool hasInputProperty(int32_t deviceId, int property) const = 0;
262
Chris Yef59a2f42020-10-16 12:55:26 -0700263 virtual bool hasMscEvent(int32_t deviceId, int mscEvent) const = 0;
264
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000265 virtual void addKeyRemapping(int32_t deviceId, int32_t fromKeyCode,
266 int32_t toKeyCode) const = 0;
267
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700268 virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
269 int32_t metaState, int32_t* outKeycode, int32_t* outMetaState,
270 uint32_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800271
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700272 virtual status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800273
274 // Sets devices that are excluded from opening.
275 // This can be used to ignore input devices for sensors.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100276 virtual void setExcludedDevices(const std::vector<std::string>& devices) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800277
278 /*
279 * Wait for events to become available and returns them.
280 * After returning, the EventHub holds onto a wake lock until the next call to getEvent.
281 * This ensures that the device will not go to sleep while the event is being processed.
282 * If the device needs to remain awake longer than that, then the caller is responsible
283 * for taking care of it (say, by poking the power manager user activity timer).
284 *
285 * The timeout is advisory only. If the device is asleep, it will not wake just to
286 * service the timeout.
287 *
288 * Returns the number of events obtained, or 0 if the timeout expired.
289 */
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700290 virtual std::vector<RawEvent> getEvents(int timeoutMillis) = 0;
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800291 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) = 0;
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000292 virtual base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
293 int32_t deviceId, int32_t absCode) const = 0;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800294 // Raw batteries are sysfs power_supply 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> getRawBatteryIds(int32_t deviceId) const = 0;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800297 virtual std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000298 int32_t BatteryId) const = 0;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800299
Chris Ye3fdbfef2021-01-06 18:45:18 -0800300 // Raw lights are sysfs led light nodes we found from the EventHub device sysfs node,
301 // containing the raw info of the sysfs node structure.
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000302 virtual std::vector<int32_t> getRawLightIds(int32_t deviceId) const = 0;
303 virtual std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId,
304 int32_t lightId) const = 0;
305 virtual std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const = 0;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800306 virtual void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) = 0;
307 virtual std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000308 int32_t deviceId, int32_t lightId) const = 0;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800309 virtual void setLightIntensities(int32_t deviceId, int32_t lightId,
310 std::unordered_map<LightColor, int32_t> intensities) = 0;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000311 /* Query Country code associated with the input device. */
312 virtual hardware::input::InputDeviceCountryCode getCountryCode(int32_t deviceId) const = 0;
313 /* Query current input state. */
Michael Wrightd02c5b62014-02-10 15:10:22 -0800314 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0;
315 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0;
316 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0;
317 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700318 int32_t* outValue) const = 0;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100319 virtual int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800320
321 /*
322 * Examine key input devices for specific framework keycode support
323 */
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700324 virtual bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700325 uint8_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800326
327 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const = 0;
Arthur Hungcb40a002021-08-03 14:31:01 +0000328 virtual bool hasKeyCode(int32_t deviceId, int32_t keyCode) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800329
330 /* LED related functions expect Android LED constants, not scan codes or HID usages */
331 virtual bool hasLed(int32_t deviceId, int32_t led) const = 0;
332 virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0;
333
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700334 virtual void getVirtualKeyDefinitions(
335 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800336
Chris Ye3a1e4462020-08-12 10:13:15 -0700337 virtual const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const = 0;
338 virtual bool setKeyboardLayoutOverlay(int32_t deviceId,
339 std::shared_ptr<KeyCharacterMap> map) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800340
341 /* Control the vibrator. */
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000342 virtual void vibrate(int32_t deviceId, const VibrationElement& effect) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800343 virtual void cancelVibrate(int32_t deviceId) = 0;
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000344 virtual std::vector<int32_t> getVibratorIds(int32_t deviceId) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800345
Kim Low03ea0352020-11-06 12:45:07 -0800346 /* Query battery level. */
Chris Yee2b1e5c2021-03-10 22:45:12 -0800347 virtual std::optional<int32_t> getBatteryCapacity(int32_t deviceId,
348 int32_t batteryId) const = 0;
Kim Low03ea0352020-11-06 12:45:07 -0800349
350 /* Query battery status. */
Chris Yee2b1e5c2021-03-10 22:45:12 -0800351 virtual std::optional<int32_t> getBatteryStatus(int32_t deviceId, int32_t batteryId) const = 0;
Kim Low03ea0352020-11-06 12:45:07 -0800352
Michael Wrightd02c5b62014-02-10 15:10:22 -0800353 /* Requests the EventHub to reopen all input devices on the next call to getEvents(). */
354 virtual void requestReopenDevices() = 0;
355
356 /* Wakes up getEvents() if it is blocked on a read. */
357 virtual void wake() = 0;
358
359 /* Dump EventHub state to a string. */
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000360 virtual void dump(std::string& dump) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800361
362 /* Called by the heatbeat to ensures that the reader has not deadlocked. */
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000363 virtual void monitor() const = 0;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700364
365 /* Return true if the device is enabled. */
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000366 virtual bool isDeviceEnabled(int32_t deviceId) const = 0;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700367
368 /* Enable an input device */
369 virtual status_t enableDevice(int32_t deviceId) = 0;
370
371 /* Disable an input device. Closes file descriptor to that device. */
372 virtual status_t disableDevice(int32_t deviceId) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800373};
374
Chris Ye66fbac32020-07-06 20:36:43 -0700375template <std::size_t BITS>
376class BitArray {
377 /* Array element type and vector of element type. */
378 using Element = std::uint32_t;
379 /* Number of bits in each BitArray element. */
380 static constexpr size_t WIDTH = sizeof(Element) * CHAR_BIT;
381 /* Number of elements to represent a bit array of the specified size of bits. */
382 static constexpr size_t COUNT = (BITS + WIDTH - 1) / WIDTH;
383
384public:
385 /* BUFFER type declaration for BitArray */
386 using Buffer = std::array<Element, COUNT>;
387 /* To tell if a bit is set in array, it selects an element from the array, and test
388 * if the relevant bit set.
389 * Note the parameter "bit" is an index to the bit, 0 <= bit < BITS.
390 */
391 inline bool test(size_t bit) const {
392 return (bit < BITS) ? mData[bit / WIDTH].test(bit % WIDTH) : false;
393 }
394 /* Returns total number of bytes needed for the array */
395 inline size_t bytes() { return (BITS + CHAR_BIT - 1) / CHAR_BIT; }
396 /* Returns true if array contains any non-zero bit from the range defined by start and end
397 * bit index [startIndex, endIndex).
398 */
399 bool any(size_t startIndex, size_t endIndex) {
400 if (startIndex >= endIndex || startIndex > BITS || endIndex > BITS + 1) {
401 ALOGE("Invalid start/end index. start = %zu, end = %zu, total bits = %zu", startIndex,
402 endIndex, BITS);
403 return false;
404 }
405 size_t se = startIndex / WIDTH; // Start of element
406 size_t ee = endIndex / WIDTH; // End of element
407 size_t si = startIndex % WIDTH; // Start index in start element
408 size_t ei = endIndex % WIDTH; // End index in end element
409 // Need to check first unaligned bitset for any non zero bit
410 if (si > 0) {
411 size_t nBits = se == ee ? ei - si : WIDTH - si;
412 // Generate the mask of interested bit range
413 Element mask = ((1 << nBits) - 1) << si;
414 if (mData[se++].to_ulong() & mask) {
415 return true;
416 }
417 }
418 // Check whole bitset for any bit set
419 for (; se < ee; se++) {
420 if (mData[se].any()) {
421 return true;
422 }
423 }
424 // Need to check last unaligned bitset for any non zero bit
425 if (ei > 0 && se <= ee) {
426 // Generate the mask of interested bit range
427 Element mask = (1 << ei) - 1;
428 if (mData[se].to_ulong() & mask) {
429 return true;
430 }
431 }
432 return false;
433 }
434 /* Load bit array values from buffer */
435 void loadFromBuffer(const Buffer& buffer) {
436 for (size_t i = 0; i < COUNT; i++) {
437 mData[i] = std::bitset<WIDTH>(buffer[i]);
438 }
439 }
440
441private:
442 std::array<std::bitset<WIDTH>, COUNT> mData;
443};
444
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700445class EventHub : public EventHubInterface {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800446public:
447 EventHub();
448
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700449 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800450
Chris Ye989bb932020-07-04 16:18:59 -0700451 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800452
Chris Ye989bb932020-07-04 16:18:59 -0700453 int32_t getDeviceControllerNumber(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800454
Chris Ye989bb932020-07-04 16:18:59 -0700455 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800456
Chris Ye989bb932020-07-04 16:18:59 -0700457 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
458 RawAbsoluteAxisInfo* outAxisInfo) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800459
Chris Ye989bb932020-07-04 16:18:59 -0700460 bool hasRelativeAxis(int32_t deviceId, int axis) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800461
Chris Ye989bb932020-07-04 16:18:59 -0700462 bool hasInputProperty(int32_t deviceId, int property) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800463
Chris Yef59a2f42020-10-16 12:55:26 -0700464 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final;
465
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000466 void addKeyRemapping(int32_t deviceId, int32_t fromKeyCode,
467 int32_t toKeyCode) const override final;
468
Chris Ye989bb932020-07-04 16:18:59 -0700469 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
470 int32_t* outKeycode, int32_t* outMetaState,
471 uint32_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800472
Chris Ye989bb932020-07-04 16:18:59 -0700473 status_t mapAxis(int32_t deviceId, int32_t scanCode,
474 AxisInfo* outAxisInfo) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800475
Chris Yef59a2f42020-10-16 12:55:26 -0700476 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000477 int32_t deviceId, int32_t absCode) const override final;
Chris Yef59a2f42020-10-16 12:55:26 -0700478
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000479 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override final;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800480 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000481 int32_t BatteryId) const override final;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800482
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000483 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800484
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000485 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId,
486 int32_t lightId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800487
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000488 std::optional<int32_t> getLightBrightness(int32_t deviceId,
489 int32_t lightId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800490 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override final;
491 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000492 int32_t deviceId, int32_t lightId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800493 void setLightIntensities(int32_t deviceId, int32_t lightId,
494 std::unordered_map<LightColor, int32_t> intensities) override final;
495
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000496 hardware::input::InputDeviceCountryCode getCountryCode(int32_t deviceId) const override final;
497
Chris Ye989bb932020-07-04 16:18:59 -0700498 void setExcludedDevices(const std::vector<std::string>& devices) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800499
Chris Ye989bb932020-07-04 16:18:59 -0700500 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override final;
501 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override final;
502 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override final;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100503 int32_t getKeyCodeForKeyLocation(int32_t deviceId,
504 int32_t locationKeyCode) const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700505 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
506 int32_t* outValue) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800507
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700508 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Chris Ye989bb932020-07-04 16:18:59 -0700509 uint8_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800510
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700511 std::vector<RawEvent> getEvents(int timeoutMillis) override final;
Chris Ye989bb932020-07-04 16:18:59 -0700512 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800513
Chris Ye989bb932020-07-04 16:18:59 -0700514 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override final;
Arthur Hungcb40a002021-08-03 14:31:01 +0000515 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700516 bool hasLed(int32_t deviceId, int32_t led) const override final;
517 void setLedState(int32_t deviceId, int32_t led, bool on) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800518
Chris Ye989bb932020-07-04 16:18:59 -0700519 void getVirtualKeyDefinitions(
520 int32_t deviceId,
521 std::vector<VirtualKeyDefinition>& outVirtualKeys) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800522
Chris Ye3a1e4462020-08-12 10:13:15 -0700523 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(
524 int32_t deviceId) const override final;
525 bool setKeyboardLayoutOverlay(int32_t deviceId,
526 std::shared_ptr<KeyCharacterMap> map) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800527
Chris Ye989bb932020-07-04 16:18:59 -0700528 void vibrate(int32_t deviceId, const VibrationElement& effect) override final;
529 void cancelVibrate(int32_t deviceId) override final;
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000530 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800531
Chris Ye989bb932020-07-04 16:18:59 -0700532 void requestReopenDevices() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800533
Chris Ye989bb932020-07-04 16:18:59 -0700534 void wake() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800535
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000536 void dump(std::string& dump) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800537
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000538 void monitor() const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700539
Chris Yee2b1e5c2021-03-10 22:45:12 -0800540 std::optional<int32_t> getBatteryCapacity(int32_t deviceId,
541 int32_t batteryId) const override final;
Kim Low03ea0352020-11-06 12:45:07 -0800542
Chris Yee2b1e5c2021-03-10 22:45:12 -0800543 std::optional<int32_t> getBatteryStatus(int32_t deviceId,
544 int32_t batteryId) const override final;
Kim Low03ea0352020-11-06 12:45:07 -0800545
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000546 bool isDeviceEnabled(int32_t deviceId) const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700547
548 status_t enableDevice(int32_t deviceId) override final;
549
550 status_t disableDevice(int32_t deviceId) override final;
551
552 ~EventHub() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800553
554private:
Prabir Pradhancb42b472022-08-23 16:01:19 +0000555 // Holds information about the sysfs device associated with the Device.
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700556 struct AssociatedDevice {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800557 // The sysfs root path of the misc device.
558 std::filesystem::path sysfsRootPath;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000559 hardware::input::InputDeviceCountryCode countryCode;
Prabir Pradhancb42b472022-08-23 16:01:19 +0000560 std::unordered_map<int32_t /*batteryId*/, RawBatteryInfo> batteryInfos;
561 std::unordered_map<int32_t /*lightId*/, RawLightInfo> lightInfos;
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000562
563 bool operator==(const AssociatedDevice&) const = default;
564 bool operator!=(const AssociatedDevice&) const = default;
565 std::string dump() const;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800566 };
567
Michael Wrightd02c5b62014-02-10 15:10:22 -0800568 struct Device {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700569 int fd; // may be -1 if device is closed
Michael Wrightd02c5b62014-02-10 15:10:22 -0800570 const int32_t id;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100571 const std::string path;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800572 const InputDeviceIdentifier identifier;
573
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -0800574 std::unique_ptr<TouchVideoDevice> videoDevice;
575
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700576 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800577
Chris Ye66fbac32020-07-06 20:36:43 -0700578 BitArray<KEY_MAX> keyBitmask;
579 BitArray<KEY_MAX> keyState;
580 BitArray<ABS_MAX> absBitmask;
581 BitArray<REL_MAX> relBitmask;
582 BitArray<SW_MAX> swBitmask;
583 BitArray<SW_MAX> swState;
584 BitArray<LED_MAX> ledBitmask;
585 BitArray<FF_MAX> ffBitmask;
586 BitArray<INPUT_PROP_MAX> propBitmask;
Chris Yef59a2f42020-10-16 12:55:26 -0700587 BitArray<MSC_MAX> mscBitmask;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800588
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100589 std::string configurationFile;
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500590 std::unique_ptr<PropertyMap> configuration;
Siarhei Vishniakou3e78dec2019-02-20 16:21:46 -0600591 std::unique_ptr<VirtualKeyMap> virtualKeyMap;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800592 KeyMap keyMap;
593
Michael Wrightd02c5b62014-02-10 15:10:22 -0800594 bool ffEffectPlaying;
595 int16_t ffEffectId; // initially -1
596
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700597 // A shared_ptr of a device associated with the input device.
Prabir Pradhan51894782022-08-23 16:29:10 +0000598 // The input devices that have the same sysfs path have the same associated device.
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000599 std::shared_ptr<const AssociatedDevice> associatedDevice;
Kim Low03ea0352020-11-06 12:45:07 -0800600
Michael Wrightd02c5b62014-02-10 15:10:22 -0800601 int32_t controllerNumber;
602
Prabir Pradhancb42b472022-08-23 16:01:19 +0000603 Device(int fd, int32_t id, std::string path, InputDeviceIdentifier identifier,
604 std::shared_ptr<const AssociatedDevice> assocDev);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800605 ~Device();
606
607 void close();
608
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700609 bool enabled; // initially true
610 status_t enable();
611 status_t disable();
Chris Ye989bb932020-07-04 16:18:59 -0700612 bool hasValidFd() const;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700613 const bool isVirtual; // set if fd < 0 is passed to constructor
Michael Wrightd02c5b62014-02-10 15:10:22 -0800614
Chris Ye3a1e4462020-08-12 10:13:15 -0700615 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const;
Chris Ye66fbac32020-07-06 20:36:43 -0700616
617 template <std::size_t N>
Chris Ye989bb932020-07-04 16:18:59 -0700618 status_t readDeviceBitMask(unsigned long ioctlCode, BitArray<N>& bitArray);
Chris Ye66fbac32020-07-06 20:36:43 -0700619
Chris Ye989bb932020-07-04 16:18:59 -0700620 void configureFd();
621 bool hasKeycodeLocked(int keycode) const;
622 void loadConfigurationLocked();
623 bool loadVirtualKeyMapLocked();
624 status_t loadKeyMapLocked();
625 bool isExternalDeviceLocked();
626 bool deviceHasMicLocked();
627 void setLedForControllerLocked();
628 status_t mapLed(int32_t led, int32_t* outScanCode) const;
629 void setLedStateLocked(int32_t led, bool on);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800630 };
631
Siarhei Vishniakoua4c502a2021-02-05 00:45:20 +0000632 /**
633 * Create a new device for the provided path.
634 */
Chris Yed3fef462021-03-07 17:10:08 -0800635 void openDeviceLocked(const std::string& devicePath) REQUIRES(mLock);
636 void openVideoDeviceLocked(const std::string& devicePath) REQUIRES(mLock);
Siarhei Vishniakouf49608d2020-08-20 19:18:21 -0500637 /**
638 * Try to associate a video device with an input device. If the association succeeds,
639 * the videoDevice is moved into the input device. 'videoDevice' will become null if this
640 * happens.
641 * Return true if the association succeeds.
642 * Return false otherwise.
643 */
Chris Yed3fef462021-03-07 17:10:08 -0800644 bool tryAddVideoDeviceLocked(Device& device, std::unique_ptr<TouchVideoDevice>& videoDevice)
645 REQUIRES(mLock);
646 void createVirtualKeyboardLocked() REQUIRES(mLock);
647 void addDeviceLocked(std::unique_ptr<Device> device) REQUIRES(mLock);
648 void assignDescriptorLocked(InputDeviceIdentifier& identifier) REQUIRES(mLock);
Prabir Pradhancb42b472022-08-23 16:01:19 +0000649 std::shared_ptr<const AssociatedDevice> obtainAssociatedDeviceLocked(
650 const std::filesystem::path& devicePath) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800651
Chris Yed3fef462021-03-07 17:10:08 -0800652 void closeDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock);
653 void closeVideoDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock);
654 void closeDeviceLocked(Device& device) REQUIRES(mLock);
655 void closeAllDevicesLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800656
Siarhei Vishniakou25920312018-12-12 15:24:44 -0800657 status_t registerFdForEpoll(int fd);
658 status_t unregisterFdFromEpoll(int fd);
Chris Yed3fef462021-03-07 17:10:08 -0800659 status_t registerDeviceForEpollLocked(Device& device) REQUIRES(mLock);
660 void registerVideoDeviceForEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock);
661 status_t unregisterDeviceFromEpollLocked(Device& device) REQUIRES(mLock);
662 void unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700663
Chris Yed3fef462021-03-07 17:10:08 -0800664 status_t scanDirLocked(const std::string& dirname) REQUIRES(mLock);
665 status_t scanVideoDirLocked(const std::string& dirname) REQUIRES(mLock);
666 void scanDevicesLocked() REQUIRES(mLock);
Prabir Pradhan952e65b2022-06-23 17:49:55 +0000667 base::Result<void> readNotifyLocked() REQUIRES(mLock);
668 void handleNotifyEventLocked(const inotify_event&) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800669
Chris Yed3fef462021-03-07 17:10:08 -0800670 Device* getDeviceLocked(int32_t deviceId) const REQUIRES(mLock);
671 Device* getDeviceByPathLocked(const std::string& devicePath) const REQUIRES(mLock);
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700672 /**
673 * Look through all available fd's (both for input devices and for video devices),
674 * and return the device pointer.
675 */
Chris Yed3fef462021-03-07 17:10:08 -0800676 Device* getDeviceByFdLocked(int fd) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800677
Chris Yed3fef462021-03-07 17:10:08 -0800678 int32_t getNextControllerNumberLocked(const std::string& name) REQUIRES(mLock);
Josh Bartel938632f2022-07-19 15:34:22 -0500679
680 bool hasDeviceWithDescriptorLocked(const std::string& descriptor) const REQUIRES(mLock);
681
Chris Yed3fef462021-03-07 17:10:08 -0800682 void releaseControllerNumberLocked(int32_t num) REQUIRES(mLock);
683 void reportDeviceAddedForStatisticsLocked(const InputDeviceIdentifier& identifier,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700684 ftl::Flags<InputDeviceClass> classes) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800685
Chris Yee2b1e5c2021-03-10 22:45:12 -0800686 const std::unordered_map<int32_t, RawBatteryInfo>& getBatteryInfoLocked(int32_t deviceId) const
687 REQUIRES(mLock);
688
689 const std::unordered_map<int32_t, RawLightInfo>& getLightInfoLocked(int32_t deviceId) const
690 REQUIRES(mLock);
691
Usama Arifb27c8e62021-06-03 16:44:09 +0100692 void addDeviceInputInotify();
693 void addDeviceInotify();
694
Michael Wrightd02c5b62014-02-10 15:10:22 -0800695 // Protect all internal state.
Chris Ye1c2e0892020-11-30 21:41:44 -0800696 mutable std::mutex mLock;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800697
698 // The actual id of the built-in keyboard, or NO_BUILT_IN_KEYBOARD if none.
699 // EventHub remaps the built-in keyboard to id 0 externally as required by the API.
700 enum {
701 // Must not conflict with any other assigned device ids, including
702 // the virtual keyboard id (-1).
703 NO_BUILT_IN_KEYBOARD = -2,
704 };
705 int32_t mBuiltInKeyboardId;
706
707 int32_t mNextDeviceId;
708
709 BitSet32 mControllerNumbers;
710
Chris Ye989bb932020-07-04 16:18:59 -0700711 std::unordered_map<int32_t, std::unique_ptr<Device>> mDevices;
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800712 /**
713 * Video devices that report touchscreen heatmap, but have not (yet) been paired
714 * with a specific input device. Video device discovery is independent from input device
715 * discovery, so the two types of devices could be found in any order.
716 * Ideally, video devices in this queue do not have an open fd, or at least aren't
717 * actively streaming.
718 */
719 std::vector<std::unique_ptr<TouchVideoDevice>> mUnattachedVideoDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800720
Chris Ye989bb932020-07-04 16:18:59 -0700721 std::vector<std::unique_ptr<Device>> mOpeningDevices;
722 std::vector<std::unique_ptr<Device>> mClosingDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800723
724 bool mNeedToSendFinishedDeviceScan;
725 bool mNeedToReopenDevices;
726 bool mNeedToScanDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100727 std::vector<std::string> mExcludedDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800728
729 int mEpollFd;
730 int mINotifyFd;
731 int mWakeReadPipeFd;
732 int mWakeWritePipeFd;
733
Usama Arifb27c8e62021-06-03 16:44:09 +0100734 int mDeviceInputWd;
735 int mDeviceWd = -1;
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800736
Michael Wrightd02c5b62014-02-10 15:10:22 -0800737 // Maximum number of signalled FDs to handle at a time.
738 static const int EPOLL_MAX_EVENTS = 16;
739
740 // The array of pending epoll events and the index of the next event to be handled.
741 struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS];
742 size_t mPendingEventCount;
743 size_t mPendingEventIndex;
744 bool mPendingINotify;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800745};
746
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700747} // namespace android