blob: 86acadb428474dcc2efbb209d786be91a8279543 [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>
Harry Cuttsea73eaa2023-01-16 17:55:46 +000022#include <ostream>
23#include <string>
Chris Ye989bb932020-07-04 16:18:59 -070024#include <unordered_map>
Prabir Pradhan51894782022-08-23 16:29:10 +000025#include <utility>
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010026#include <vector>
27
Kim Low03ea0352020-11-06 12:45:07 -080028#include <batteryservice/BatteryService.h>
Dominik Laskowski2f01d772022-03-23 16:01:29 -070029#include <ftl/flags.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080030#include <input/Input.h>
31#include <input/InputDevice.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080032#include <input/KeyCharacterMap.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070033#include <input/KeyLayoutMap.h>
34#include <input/Keyboard.h>
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070035#include <input/PropertyMap.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080036#include <input/VirtualKeyMap.h>
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000037#include <linux/input.h>
38#include <sys/epoll.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080039#include <utils/BitSet.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070040#include <utils/Errors.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070041#include <utils/List.h>
42#include <utils/Log.h>
43#include <utils/Mutex.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080044
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080045#include "TouchVideoDevice.h"
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000046#include "VibrationElement.h"
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080047
Prabir Pradhan952e65b2022-06-23 17:49:55 +000048struct inotify_event;
49
Michael Wrightd02c5b62014-02-10 15:10:22 -080050namespace android {
51
Chris Ye3fdbfef2021-01-06 18:45:18 -080052/* Number of colors : {red, green, blue} */
53static constexpr size_t COLOR_NUM = 3;
Michael Wrightd02c5b62014-02-10 15:10:22 -080054/*
55 * A raw event as retrieved from the EventHub.
56 */
57struct RawEvent {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000058 // Time when the event happened
Michael Wrightd02c5b62014-02-10 15:10:22 -080059 nsecs_t when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000060 // Time when the event was read by EventHub. Only populated for input events.
61 // For other events (device added/removed/etc), this value is undefined and should not be read.
62 nsecs_t readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -080063 int32_t deviceId;
64 int32_t type;
65 int32_t code;
66 int32_t value;
67};
68
69/* Describes an absolute axis. */
70struct RawAbsoluteAxisInfo {
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000071 bool valid{false}; // true if the information is valid, false otherwise
Michael Wrightd02c5b62014-02-10 15:10:22 -080072
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000073 int32_t minValue{}; // minimum value
74 int32_t maxValue{}; // maximum value
75 int32_t flat{}; // center flat position, eg. flat == 8 means center is between -8 and 8
76 int32_t fuzz{}; // error tolerance, eg. fuzz == 4 means value is +/- 4 due to noise
77 int32_t resolution{}; // resolution in units per mm or radians per mm
Michael Wrightd02c5b62014-02-10 15:10:22 -080078
Prabir Pradhand6ccedb2022-09-27 21:04:06 +000079 inline void clear() { *this = RawAbsoluteAxisInfo(); }
Michael Wrightd02c5b62014-02-10 15:10:22 -080080};
81
Harry Cuttsea73eaa2023-01-16 17:55:46 +000082std::ostream& operator<<(std::ostream& out, const RawAbsoluteAxisInfo& info);
83
Michael Wrightd02c5b62014-02-10 15:10:22 -080084/*
85 * Input device classes.
86 */
Chris Ye1b0c7342020-07-28 21:57:03 -070087enum class InputDeviceClass : uint32_t {
Michael Wrightd02c5b62014-02-10 15:10:22 -080088 /* The input device is a keyboard or has buttons. */
Chris Ye1b0c7342020-07-28 21:57:03 -070089 KEYBOARD = 0x00000001,
Michael Wrightd02c5b62014-02-10 15:10:22 -080090
91 /* The input device is an alpha-numeric keyboard (not just a dial pad). */
Chris Ye1b0c7342020-07-28 21:57:03 -070092 ALPHAKEY = 0x00000002,
Michael Wrightd02c5b62014-02-10 15:10:22 -080093
94 /* The input device is a touchscreen or a touchpad (either single-touch or multi-touch). */
Chris Ye1b0c7342020-07-28 21:57:03 -070095 TOUCH = 0x00000004,
Michael Wrightd02c5b62014-02-10 15:10:22 -080096
97 /* The input device is a cursor device such as a trackball or mouse. */
Chris Ye1b0c7342020-07-28 21:57:03 -070098 CURSOR = 0x00000008,
Michael Wrightd02c5b62014-02-10 15:10:22 -080099
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000100 /* The input device is a multi-touch touchscreen or touchpad. */
Chris Ye1b0c7342020-07-28 21:57:03 -0700101 TOUCH_MT = 0x00000010,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800102
103 /* The input device is a directional pad (implies keyboard, has DPAD keys). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700104 DPAD = 0x00000020,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800105
106 /* The input device is a gamepad (implies keyboard, has BUTTON keys). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700107 GAMEPAD = 0x00000040,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800108
109 /* The input device has switches. */
Chris Ye1b0c7342020-07-28 21:57:03 -0700110 SWITCH = 0x00000080,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800111
112 /* The input device is a joystick (implies gamepad, has joystick absolute axes). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700113 JOYSTICK = 0x00000100,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800114
115 /* The input device has a vibrator (supports FF_RUMBLE). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700116 VIBRATOR = 0x00000200,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800117
Tim Kilbourn063ff532015-04-08 10:26:18 -0700118 /* The input device has a microphone. */
Chris Ye1b0c7342020-07-28 21:57:03 -0700119 MIC = 0x00000400,
Tim Kilbourn063ff532015-04-08 10:26:18 -0700120
Michael Wright842500e2015-03-13 17:32:02 -0700121 /* The input device is an external stylus (has data we want to fuse with touch data). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700122 EXTERNAL_STYLUS = 0x00000800,
Michael Wright842500e2015-03-13 17:32:02 -0700123
Prashant Malani1941ff52015-08-11 18:29:28 -0700124 /* The input device has a rotary encoder */
Chris Ye1b0c7342020-07-28 21:57:03 -0700125 ROTARY_ENCODER = 0x00001000,
Prashant Malani1941ff52015-08-11 18:29:28 -0700126
Chris Yef59a2f42020-10-16 12:55:26 -0700127 /* The input device has a sensor like accelerometer, gyro, etc */
128 SENSOR = 0x00002000,
129
Kim Low03ea0352020-11-06 12:45:07 -0800130 /* The input device has a battery */
131 BATTERY = 0x00004000,
132
Chris Ye3fdbfef2021-01-06 18:45:18 -0800133 /* The input device has sysfs controllable lights */
134 LIGHT = 0x00008000,
135
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000136 /* The input device is a touchpad, requiring an on-screen cursor. */
137 TOUCHPAD = 0x00010000,
138
Michael Wrightd02c5b62014-02-10 15:10:22 -0800139 /* The input device is virtual (not a real device, not part of UI configuration). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700140 VIRTUAL = 0x40000000,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800141
142 /* The input device is external (not built-in). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700143 EXTERNAL = 0x80000000,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800144};
145
Chris Ye3fdbfef2021-01-06 18:45:18 -0800146enum class SysfsClass : uint32_t {
147 POWER_SUPPLY = 0,
148 LEDS = 1,
Dominik Laskowski75788452021-02-09 18:51:25 -0800149
150 ftl_last = LEDS
Chris Ye3fdbfef2021-01-06 18:45:18 -0800151};
152
153enum class LightColor : uint32_t {
154 RED = 0,
155 GREEN = 1,
156 BLUE = 2,
157};
158
159enum class InputLightClass : uint32_t {
160 /* The input light has brightness node. */
161 BRIGHTNESS = 0x00000001,
162 /* The input light has red name. */
163 RED = 0x00000002,
164 /* The input light has green name. */
165 GREEN = 0x00000004,
166 /* The input light has blue name. */
167 BLUE = 0x00000008,
168 /* The input light has global name. */
169 GLOBAL = 0x00000010,
170 /* The input light has multi index node. */
171 MULTI_INDEX = 0x00000020,
172 /* The input light has multi intensity node. */
173 MULTI_INTENSITY = 0x00000040,
174 /* The input light has max brightness node. */
175 MAX_BRIGHTNESS = 0x00000080,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +0000176 /* The input light has kbd_backlight name */
177 KEYBOARD_BACKLIGHT = 0x00000100,
Chris Ye3fdbfef2021-01-06 18:45:18 -0800178};
179
Chris Yee2b1e5c2021-03-10 22:45:12 -0800180enum class InputBatteryClass : uint32_t {
181 /* The input device battery has capacity node. */
182 CAPACITY = 0x00000001,
183 /* The input device battery has capacity_level node. */
184 CAPACITY_LEVEL = 0x00000002,
185 /* The input device battery has status node. */
186 STATUS = 0x00000004,
187};
188
Chris Ye3fdbfef2021-01-06 18:45:18 -0800189/* Describes a raw light. */
190struct RawLightInfo {
191 int32_t id;
192 std::string name;
193 std::optional<int32_t> maxBrightness;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700194 ftl::Flags<InputLightClass> flags;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800195 std::array<int32_t, COLOR_NUM> rgbIndex;
196 std::filesystem::path path;
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000197
198 bool operator==(const RawLightInfo&) const = default;
199 bool operator!=(const RawLightInfo&) const = default;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800200};
201
Chris Yee2b1e5c2021-03-10 22:45:12 -0800202/* Describes a raw battery. */
203struct RawBatteryInfo {
204 int32_t id;
205 std::string name;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700206 ftl::Flags<InputBatteryClass> flags;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800207 std::filesystem::path path;
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000208
209 bool operator==(const RawBatteryInfo&) const = default;
210 bool operator!=(const RawBatteryInfo&) const = default;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800211};
212
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000213/* Layout information associated with the device */
214struct RawLayoutInfo {
215 std::string languageTag;
216 std::string layoutType;
217
218 bool operator==(const RawLayoutInfo&) const = default;
219 bool operator!=(const RawLayoutInfo&) const = default;
220};
221
Michael Wrightd02c5b62014-02-10 15:10:22 -0800222/*
223 * Gets the class that owns an axis, in cases where multiple classes might claim
224 * the same axis for different purposes.
225 */
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700226extern ftl::Flags<InputDeviceClass> getAbsAxisUsage(int32_t axis,
227 ftl::Flags<InputDeviceClass> deviceClasses);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800228
229/*
230 * Grand Central Station for events.
231 *
232 * The event hub aggregates input events received across all known input
233 * devices on the system, including devices that may be emulated by the simulator
234 * environment. In addition, the event hub generates fake input events to indicate
235 * when devices are added or removed.
236 *
237 * The event hub provides a stream of input events (via the getEvent function).
238 * It also supports querying the current actual state of input devices such as identifying
239 * which keys are currently down. Finally, the event hub keeps track of the capabilities of
240 * individual input devices, such as their class and the set of key codes that they support.
241 */
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700242class EventHubInterface {
243public:
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700244 EventHubInterface() {}
245 virtual ~EventHubInterface() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800246
Michael Wrightd02c5b62014-02-10 15:10:22 -0800247 // Synthetic raw event type codes produced when devices are added or removed.
248 enum {
249 // Sent when a device is added.
250 DEVICE_ADDED = 0x10000000,
251 // Sent when a device is removed.
252 DEVICE_REMOVED = 0x20000000,
253 // Sent when all added/removed devices from the most recent scan have been reported.
254 // This event is always sent at least once.
255 FINISHED_DEVICE_SCAN = 0x30000000,
256
257 FIRST_SYNTHETIC_EVENT = DEVICE_ADDED,
258 };
259
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700260 virtual ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800261
262 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const = 0;
263
264 virtual int32_t getDeviceControllerNumber(int32_t deviceId) const = 0;
265
266 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const = 0;
267
268 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700269 RawAbsoluteAxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800270
271 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const = 0;
272
273 virtual bool hasInputProperty(int32_t deviceId, int property) const = 0;
274
Chris Yef59a2f42020-10-16 12:55:26 -0700275 virtual bool hasMscEvent(int32_t deviceId, int mscEvent) const = 0;
276
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000277 virtual void addKeyRemapping(int32_t deviceId, int32_t fromKeyCode,
278 int32_t toKeyCode) const = 0;
279
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700280 virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
281 int32_t metaState, int32_t* outKeycode, int32_t* outMetaState,
282 uint32_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800283
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700284 virtual status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800285
286 // Sets devices that are excluded from opening.
287 // This can be used to ignore input devices for sensors.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100288 virtual void setExcludedDevices(const std::vector<std::string>& devices) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800289
290 /*
291 * Wait for events to become available and returns them.
292 * After returning, the EventHub holds onto a wake lock until the next call to getEvent.
293 * This ensures that the device will not go to sleep while the event is being processed.
294 * If the device needs to remain awake longer than that, then the caller is responsible
295 * for taking care of it (say, by poking the power manager user activity timer).
296 *
297 * The timeout is advisory only. If the device is asleep, it will not wake just to
298 * service the timeout.
299 *
300 * Returns the number of events obtained, or 0 if the timeout expired.
301 */
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700302 virtual std::vector<RawEvent> getEvents(int timeoutMillis) = 0;
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800303 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) = 0;
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000304 virtual base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
305 int32_t deviceId, int32_t absCode) const = 0;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800306 // Raw batteries are sysfs power_supply nodes we found from the EventHub device sysfs node,
307 // containing the raw info of the sysfs node structure.
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000308 virtual std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const = 0;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800309 virtual std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000310 int32_t BatteryId) const = 0;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800311
Chris Ye3fdbfef2021-01-06 18:45:18 -0800312 // Raw lights are sysfs led light nodes we found from the EventHub device sysfs node,
313 // containing the raw info of the sysfs node structure.
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000314 virtual std::vector<int32_t> getRawLightIds(int32_t deviceId) const = 0;
315 virtual std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId,
316 int32_t lightId) const = 0;
317 virtual std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const = 0;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800318 virtual void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) = 0;
319 virtual std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000320 int32_t deviceId, int32_t lightId) const = 0;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800321 virtual void setLightIntensities(int32_t deviceId, int32_t lightId,
322 std::unordered_map<LightColor, int32_t> intensities) = 0;
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000323 /* Query Layout info associated with the input device. */
324 virtual std::optional<RawLayoutInfo> getRawLayoutInfo(int32_t deviceId) const = 0;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000325 /* Query current input state. */
Michael Wrightd02c5b62014-02-10 15:10:22 -0800326 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0;
327 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0;
328 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0;
329 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700330 int32_t* outValue) const = 0;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100331 virtual int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800332
333 /*
334 * Examine key input devices for specific framework keycode support
335 */
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700336 virtual bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700337 uint8_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338
339 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const = 0;
Arthur Hungcb40a002021-08-03 14:31:01 +0000340 virtual bool hasKeyCode(int32_t deviceId, int32_t keyCode) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800341
342 /* LED related functions expect Android LED constants, not scan codes or HID usages */
343 virtual bool hasLed(int32_t deviceId, int32_t led) const = 0;
344 virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0;
345
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700346 virtual void getVirtualKeyDefinitions(
347 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800348
Chris Ye3a1e4462020-08-12 10:13:15 -0700349 virtual const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const = 0;
350 virtual bool setKeyboardLayoutOverlay(int32_t deviceId,
351 std::shared_ptr<KeyCharacterMap> map) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800352
353 /* Control the vibrator. */
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000354 virtual void vibrate(int32_t deviceId, const VibrationElement& effect) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800355 virtual void cancelVibrate(int32_t deviceId) = 0;
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000356 virtual std::vector<int32_t> getVibratorIds(int32_t deviceId) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800357
Kim Low03ea0352020-11-06 12:45:07 -0800358 /* Query battery level. */
Chris Yee2b1e5c2021-03-10 22:45:12 -0800359 virtual std::optional<int32_t> getBatteryCapacity(int32_t deviceId,
360 int32_t batteryId) const = 0;
Kim Low03ea0352020-11-06 12:45:07 -0800361
362 /* Query battery status. */
Chris Yee2b1e5c2021-03-10 22:45:12 -0800363 virtual std::optional<int32_t> getBatteryStatus(int32_t deviceId, int32_t batteryId) const = 0;
Kim Low03ea0352020-11-06 12:45:07 -0800364
Michael Wrightd02c5b62014-02-10 15:10:22 -0800365 /* Requests the EventHub to reopen all input devices on the next call to getEvents(). */
366 virtual void requestReopenDevices() = 0;
367
368 /* Wakes up getEvents() if it is blocked on a read. */
369 virtual void wake() = 0;
370
371 /* Dump EventHub state to a string. */
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000372 virtual void dump(std::string& dump) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800373
374 /* Called by the heatbeat to ensures that the reader has not deadlocked. */
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000375 virtual void monitor() const = 0;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700376
377 /* Return true if the device is enabled. */
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000378 virtual bool isDeviceEnabled(int32_t deviceId) const = 0;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700379
380 /* Enable an input device */
381 virtual status_t enableDevice(int32_t deviceId) = 0;
382
383 /* Disable an input device. Closes file descriptor to that device. */
384 virtual status_t disableDevice(int32_t deviceId) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800385};
386
Chris Ye66fbac32020-07-06 20:36:43 -0700387template <std::size_t BITS>
388class BitArray {
389 /* Array element type and vector of element type. */
390 using Element = std::uint32_t;
391 /* Number of bits in each BitArray element. */
392 static constexpr size_t WIDTH = sizeof(Element) * CHAR_BIT;
393 /* Number of elements to represent a bit array of the specified size of bits. */
394 static constexpr size_t COUNT = (BITS + WIDTH - 1) / WIDTH;
395
396public:
397 /* BUFFER type declaration for BitArray */
398 using Buffer = std::array<Element, COUNT>;
399 /* To tell if a bit is set in array, it selects an element from the array, and test
400 * if the relevant bit set.
401 * Note the parameter "bit" is an index to the bit, 0 <= bit < BITS.
402 */
403 inline bool test(size_t bit) const {
404 return (bit < BITS) ? mData[bit / WIDTH].test(bit % WIDTH) : false;
405 }
406 /* Returns total number of bytes needed for the array */
407 inline size_t bytes() { return (BITS + CHAR_BIT - 1) / CHAR_BIT; }
408 /* Returns true if array contains any non-zero bit from the range defined by start and end
409 * bit index [startIndex, endIndex).
410 */
411 bool any(size_t startIndex, size_t endIndex) {
412 if (startIndex >= endIndex || startIndex > BITS || endIndex > BITS + 1) {
413 ALOGE("Invalid start/end index. start = %zu, end = %zu, total bits = %zu", startIndex,
414 endIndex, BITS);
415 return false;
416 }
417 size_t se = startIndex / WIDTH; // Start of element
418 size_t ee = endIndex / WIDTH; // End of element
419 size_t si = startIndex % WIDTH; // Start index in start element
420 size_t ei = endIndex % WIDTH; // End index in end element
421 // Need to check first unaligned bitset for any non zero bit
422 if (si > 0) {
423 size_t nBits = se == ee ? ei - si : WIDTH - si;
424 // Generate the mask of interested bit range
425 Element mask = ((1 << nBits) - 1) << si;
426 if (mData[se++].to_ulong() & mask) {
427 return true;
428 }
429 }
430 // Check whole bitset for any bit set
431 for (; se < ee; se++) {
432 if (mData[se].any()) {
433 return true;
434 }
435 }
436 // Need to check last unaligned bitset for any non zero bit
437 if (ei > 0 && se <= ee) {
438 // Generate the mask of interested bit range
439 Element mask = (1 << ei) - 1;
440 if (mData[se].to_ulong() & mask) {
441 return true;
442 }
443 }
444 return false;
445 }
446 /* Load bit array values from buffer */
447 void loadFromBuffer(const Buffer& buffer) {
448 for (size_t i = 0; i < COUNT; i++) {
449 mData[i] = std::bitset<WIDTH>(buffer[i]);
450 }
451 }
452
453private:
454 std::array<std::bitset<WIDTH>, COUNT> mData;
455};
456
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700457class EventHub : public EventHubInterface {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800458public:
459 EventHub();
460
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700461 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800462
Chris Ye989bb932020-07-04 16:18:59 -0700463 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800464
Chris Ye989bb932020-07-04 16:18:59 -0700465 int32_t getDeviceControllerNumber(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800466
Chris Ye989bb932020-07-04 16:18:59 -0700467 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800468
Chris Ye989bb932020-07-04 16:18:59 -0700469 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
470 RawAbsoluteAxisInfo* outAxisInfo) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800471
Chris Ye989bb932020-07-04 16:18:59 -0700472 bool hasRelativeAxis(int32_t deviceId, int axis) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800473
Chris Ye989bb932020-07-04 16:18:59 -0700474 bool hasInputProperty(int32_t deviceId, int property) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800475
Chris Yef59a2f42020-10-16 12:55:26 -0700476 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final;
477
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000478 void addKeyRemapping(int32_t deviceId, int32_t fromKeyCode,
479 int32_t toKeyCode) const override final;
480
Chris Ye989bb932020-07-04 16:18:59 -0700481 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
482 int32_t* outKeycode, int32_t* outMetaState,
483 uint32_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800484
Chris Ye989bb932020-07-04 16:18:59 -0700485 status_t mapAxis(int32_t deviceId, int32_t scanCode,
486 AxisInfo* outAxisInfo) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800487
Chris Yef59a2f42020-10-16 12:55:26 -0700488 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000489 int32_t deviceId, int32_t absCode) const override final;
Chris Yef59a2f42020-10-16 12:55:26 -0700490
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000491 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override final;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800492 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000493 int32_t BatteryId) const override final;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800494
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000495 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800496
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000497 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId,
498 int32_t lightId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800499
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000500 std::optional<int32_t> getLightBrightness(int32_t deviceId,
501 int32_t lightId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800502 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override final;
503 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000504 int32_t deviceId, int32_t lightId) const override final;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800505 void setLightIntensities(int32_t deviceId, int32_t lightId,
506 std::unordered_map<LightColor, int32_t> intensities) override final;
507
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000508 std::optional<RawLayoutInfo> getRawLayoutInfo(int32_t deviceId) const override final;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000509
Chris Ye989bb932020-07-04 16:18:59 -0700510 void setExcludedDevices(const std::vector<std::string>& devices) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800511
Chris Ye989bb932020-07-04 16:18:59 -0700512 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override final;
513 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override final;
514 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override final;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100515 int32_t getKeyCodeForKeyLocation(int32_t deviceId,
516 int32_t locationKeyCode) const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700517 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
518 int32_t* outValue) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800519
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700520 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Chris Ye989bb932020-07-04 16:18:59 -0700521 uint8_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800522
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700523 std::vector<RawEvent> getEvents(int timeoutMillis) override final;
Chris Ye989bb932020-07-04 16:18:59 -0700524 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800525
Chris Ye989bb932020-07-04 16:18:59 -0700526 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override final;
Arthur Hungcb40a002021-08-03 14:31:01 +0000527 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700528 bool hasLed(int32_t deviceId, int32_t led) const override final;
529 void setLedState(int32_t deviceId, int32_t led, bool on) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800530
Chris Ye989bb932020-07-04 16:18:59 -0700531 void getVirtualKeyDefinitions(
532 int32_t deviceId,
533 std::vector<VirtualKeyDefinition>& outVirtualKeys) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800534
Chris Ye3a1e4462020-08-12 10:13:15 -0700535 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(
536 int32_t deviceId) const override final;
537 bool setKeyboardLayoutOverlay(int32_t deviceId,
538 std::shared_ptr<KeyCharacterMap> map) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800539
Chris Ye989bb932020-07-04 16:18:59 -0700540 void vibrate(int32_t deviceId, const VibrationElement& effect) override final;
541 void cancelVibrate(int32_t deviceId) override final;
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000542 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800543
Chris Ye989bb932020-07-04 16:18:59 -0700544 void requestReopenDevices() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800545
Chris Ye989bb932020-07-04 16:18:59 -0700546 void wake() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800547
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000548 void dump(std::string& dump) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800549
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000550 void monitor() const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700551
Chris Yee2b1e5c2021-03-10 22:45:12 -0800552 std::optional<int32_t> getBatteryCapacity(int32_t deviceId,
553 int32_t batteryId) const override final;
Kim Low03ea0352020-11-06 12:45:07 -0800554
Chris Yee2b1e5c2021-03-10 22:45:12 -0800555 std::optional<int32_t> getBatteryStatus(int32_t deviceId,
556 int32_t batteryId) const override final;
Kim Low03ea0352020-11-06 12:45:07 -0800557
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000558 bool isDeviceEnabled(int32_t deviceId) const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700559
560 status_t enableDevice(int32_t deviceId) override final;
561
562 status_t disableDevice(int32_t deviceId) override final;
563
564 ~EventHub() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800565
566private:
Prabir Pradhancb42b472022-08-23 16:01:19 +0000567 // Holds information about the sysfs device associated with the Device.
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700568 struct AssociatedDevice {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800569 // The sysfs root path of the misc device.
570 std::filesystem::path sysfsRootPath;
Prabir Pradhancb42b472022-08-23 16:01:19 +0000571 std::unordered_map<int32_t /*batteryId*/, RawBatteryInfo> batteryInfos;
572 std::unordered_map<int32_t /*lightId*/, RawLightInfo> lightInfos;
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000573 std::optional<RawLayoutInfo> layoutInfo;
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000574
575 bool operator==(const AssociatedDevice&) const = default;
576 bool operator!=(const AssociatedDevice&) const = default;
577 std::string dump() const;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800578 };
579
Michael Wrightd02c5b62014-02-10 15:10:22 -0800580 struct Device {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700581 int fd; // may be -1 if device is closed
Michael Wrightd02c5b62014-02-10 15:10:22 -0800582 const int32_t id;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100583 const std::string path;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800584 const InputDeviceIdentifier identifier;
585
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -0800586 std::unique_ptr<TouchVideoDevice> videoDevice;
587
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700588 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800589
Chris Ye66fbac32020-07-06 20:36:43 -0700590 BitArray<KEY_MAX> keyBitmask;
591 BitArray<KEY_MAX> keyState;
592 BitArray<ABS_MAX> absBitmask;
593 BitArray<REL_MAX> relBitmask;
594 BitArray<SW_MAX> swBitmask;
595 BitArray<SW_MAX> swState;
596 BitArray<LED_MAX> ledBitmask;
597 BitArray<FF_MAX> ffBitmask;
598 BitArray<INPUT_PROP_MAX> propBitmask;
Chris Yef59a2f42020-10-16 12:55:26 -0700599 BitArray<MSC_MAX> mscBitmask;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800600
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100601 std::string configurationFile;
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500602 std::unique_ptr<PropertyMap> configuration;
Siarhei Vishniakou3e78dec2019-02-20 16:21:46 -0600603 std::unique_ptr<VirtualKeyMap> virtualKeyMap;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800604 KeyMap keyMap;
605
Michael Wrightd02c5b62014-02-10 15:10:22 -0800606 bool ffEffectPlaying;
607 int16_t ffEffectId; // initially -1
608
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700609 // A shared_ptr of a device associated with the input device.
Prabir Pradhan51894782022-08-23 16:29:10 +0000610 // The input devices that have the same sysfs path have the same associated device.
Prabir Pradhanedeec3b2022-08-26 22:33:55 +0000611 std::shared_ptr<const AssociatedDevice> associatedDevice;
Kim Low03ea0352020-11-06 12:45:07 -0800612
Michael Wrightd02c5b62014-02-10 15:10:22 -0800613 int32_t controllerNumber;
614
Prabir Pradhancb42b472022-08-23 16:01:19 +0000615 Device(int fd, int32_t id, std::string path, InputDeviceIdentifier identifier,
616 std::shared_ptr<const AssociatedDevice> assocDev);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800617 ~Device();
618
619 void close();
620
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700621 bool enabled; // initially true
622 status_t enable();
623 status_t disable();
Chris Ye989bb932020-07-04 16:18:59 -0700624 bool hasValidFd() const;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700625 const bool isVirtual; // set if fd < 0 is passed to constructor
Michael Wrightd02c5b62014-02-10 15:10:22 -0800626
Chris Ye3a1e4462020-08-12 10:13:15 -0700627 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const;
Chris Ye66fbac32020-07-06 20:36:43 -0700628
629 template <std::size_t N>
Chris Ye989bb932020-07-04 16:18:59 -0700630 status_t readDeviceBitMask(unsigned long ioctlCode, BitArray<N>& bitArray);
Chris Ye66fbac32020-07-06 20:36:43 -0700631
Chris Ye989bb932020-07-04 16:18:59 -0700632 void configureFd();
633 bool hasKeycodeLocked(int keycode) const;
634 void loadConfigurationLocked();
635 bool loadVirtualKeyMapLocked();
636 status_t loadKeyMapLocked();
637 bool isExternalDeviceLocked();
638 bool deviceHasMicLocked();
639 void setLedForControllerLocked();
640 status_t mapLed(int32_t led, int32_t* outScanCode) const;
641 void setLedStateLocked(int32_t led, bool on);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800642 };
643
Siarhei Vishniakoua4c502a2021-02-05 00:45:20 +0000644 /**
645 * Create a new device for the provided path.
646 */
Chris Yed3fef462021-03-07 17:10:08 -0800647 void openDeviceLocked(const std::string& devicePath) REQUIRES(mLock);
648 void openVideoDeviceLocked(const std::string& devicePath) REQUIRES(mLock);
Siarhei Vishniakouf49608d2020-08-20 19:18:21 -0500649 /**
650 * Try to associate a video device with an input device. If the association succeeds,
651 * the videoDevice is moved into the input device. 'videoDevice' will become null if this
652 * happens.
653 * Return true if the association succeeds.
654 * Return false otherwise.
655 */
Chris Yed3fef462021-03-07 17:10:08 -0800656 bool tryAddVideoDeviceLocked(Device& device, std::unique_ptr<TouchVideoDevice>& videoDevice)
657 REQUIRES(mLock);
658 void createVirtualKeyboardLocked() REQUIRES(mLock);
659 void addDeviceLocked(std::unique_ptr<Device> device) REQUIRES(mLock);
660 void assignDescriptorLocked(InputDeviceIdentifier& identifier) REQUIRES(mLock);
Prabir Pradhancb42b472022-08-23 16:01:19 +0000661 std::shared_ptr<const AssociatedDevice> obtainAssociatedDeviceLocked(
662 const std::filesystem::path& devicePath) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800663
Chris Yed3fef462021-03-07 17:10:08 -0800664 void closeDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock);
665 void closeVideoDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock);
666 void closeDeviceLocked(Device& device) REQUIRES(mLock);
667 void closeAllDevicesLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800668
Siarhei Vishniakou25920312018-12-12 15:24:44 -0800669 status_t registerFdForEpoll(int fd);
670 status_t unregisterFdFromEpoll(int fd);
Chris Yed3fef462021-03-07 17:10:08 -0800671 status_t registerDeviceForEpollLocked(Device& device) REQUIRES(mLock);
672 void registerVideoDeviceForEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock);
673 status_t unregisterDeviceFromEpollLocked(Device& device) REQUIRES(mLock);
674 void unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700675
Chris Yed3fef462021-03-07 17:10:08 -0800676 status_t scanDirLocked(const std::string& dirname) REQUIRES(mLock);
677 status_t scanVideoDirLocked(const std::string& dirname) REQUIRES(mLock);
678 void scanDevicesLocked() REQUIRES(mLock);
Prabir Pradhan952e65b2022-06-23 17:49:55 +0000679 base::Result<void> readNotifyLocked() REQUIRES(mLock);
680 void handleNotifyEventLocked(const inotify_event&) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800681
Chris Yed3fef462021-03-07 17:10:08 -0800682 Device* getDeviceLocked(int32_t deviceId) const REQUIRES(mLock);
683 Device* getDeviceByPathLocked(const std::string& devicePath) const REQUIRES(mLock);
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700684 /**
685 * Look through all available fd's (both for input devices and for video devices),
686 * and return the device pointer.
687 */
Chris Yed3fef462021-03-07 17:10:08 -0800688 Device* getDeviceByFdLocked(int fd) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800689
Chris Yed3fef462021-03-07 17:10:08 -0800690 int32_t getNextControllerNumberLocked(const std::string& name) REQUIRES(mLock);
Josh Bartel938632f2022-07-19 15:34:22 -0500691
692 bool hasDeviceWithDescriptorLocked(const std::string& descriptor) const REQUIRES(mLock);
693
Chris Yed3fef462021-03-07 17:10:08 -0800694 void releaseControllerNumberLocked(int32_t num) REQUIRES(mLock);
695 void reportDeviceAddedForStatisticsLocked(const InputDeviceIdentifier& identifier,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700696 ftl::Flags<InputDeviceClass> classes) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800697
Chris Yee2b1e5c2021-03-10 22:45:12 -0800698 const std::unordered_map<int32_t, RawBatteryInfo>& getBatteryInfoLocked(int32_t deviceId) const
699 REQUIRES(mLock);
700
701 const std::unordered_map<int32_t, RawLightInfo>& getLightInfoLocked(int32_t deviceId) const
702 REQUIRES(mLock);
703
Usama Arifb27c8e62021-06-03 16:44:09 +0100704 void addDeviceInputInotify();
705 void addDeviceInotify();
706
Michael Wrightd02c5b62014-02-10 15:10:22 -0800707 // Protect all internal state.
Chris Ye1c2e0892020-11-30 21:41:44 -0800708 mutable std::mutex mLock;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800709
710 // The actual id of the built-in keyboard, or NO_BUILT_IN_KEYBOARD if none.
711 // EventHub remaps the built-in keyboard to id 0 externally as required by the API.
712 enum {
713 // Must not conflict with any other assigned device ids, including
714 // the virtual keyboard id (-1).
715 NO_BUILT_IN_KEYBOARD = -2,
716 };
717 int32_t mBuiltInKeyboardId;
718
719 int32_t mNextDeviceId;
720
721 BitSet32 mControllerNumbers;
722
Chris Ye989bb932020-07-04 16:18:59 -0700723 std::unordered_map<int32_t, std::unique_ptr<Device>> mDevices;
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800724 /**
725 * Video devices that report touchscreen heatmap, but have not (yet) been paired
726 * with a specific input device. Video device discovery is independent from input device
727 * discovery, so the two types of devices could be found in any order.
728 * Ideally, video devices in this queue do not have an open fd, or at least aren't
729 * actively streaming.
730 */
731 std::vector<std::unique_ptr<TouchVideoDevice>> mUnattachedVideoDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800732
Chris Ye989bb932020-07-04 16:18:59 -0700733 std::vector<std::unique_ptr<Device>> mOpeningDevices;
734 std::vector<std::unique_ptr<Device>> mClosingDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800735
736 bool mNeedToSendFinishedDeviceScan;
737 bool mNeedToReopenDevices;
738 bool mNeedToScanDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100739 std::vector<std::string> mExcludedDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800740
741 int mEpollFd;
742 int mINotifyFd;
743 int mWakeReadPipeFd;
744 int mWakeWritePipeFd;
745
Usama Arifb27c8e62021-06-03 16:44:09 +0100746 int mDeviceInputWd;
747 int mDeviceWd = -1;
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800748
Michael Wrightd02c5b62014-02-10 15:10:22 -0800749 // Maximum number of signalled FDs to handle at a time.
750 static const int EPOLL_MAX_EVENTS = 16;
751
752 // The array of pending epoll events and the index of the next event to be handled.
753 struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS];
754 size_t mPendingEventCount;
755 size_t mPendingEventIndex;
756 bool mPendingINotify;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800757};
758
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700759} // namespace android