blob: 7d4196a05c709be01cec7dfc6c8a7e1736ab5817 [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
Michael Wrightd02c5b62014-02-10 15:10:22 -080017#ifndef _RUNTIME_EVENT_HUB_H
18#define _RUNTIME_EVENT_HUB_H
19
Chris Ye66fbac32020-07-06 20:36:43 -070020#include <bitset>
21#include <climits>
Dominik Laskowski2f01d772022-03-23 16:01:29 -070022#include <filesystem>
Chris Ye989bb932020-07-04 16:18:59 -070023#include <unordered_map>
Prabir Pradhan51894782022-08-23 16:29:10 +000024#include <utility>
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010025#include <vector>
26
Kim Low03ea0352020-11-06 12:45:07 -080027#include <batteryservice/BatteryService.h>
Dominik Laskowski2f01d772022-03-23 16:01:29 -070028#include <ftl/flags.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080029#include <input/Input.h>
30#include <input/InputDevice.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080031#include <input/KeyCharacterMap.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070032#include <input/KeyLayoutMap.h>
33#include <input/Keyboard.h>
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070034#include <input/PropertyMap.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080035#include <input/VirtualKeyMap.h>
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000036#include <linux/input.h>
37#include <sys/epoll.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080038#include <utils/BitSet.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070039#include <utils/Errors.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070040#include <utils/List.h>
41#include <utils/Log.h>
42#include <utils/Mutex.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080043
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080044#include "TouchVideoDevice.h"
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000045#include "VibrationElement.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 {
70 bool valid; // true if the information is valid, false otherwise
71
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070072 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
Michael Wrightd02c5b62014-02-10 15:10:22 -080076 int32_t resolution; // resolution in units per mm or radians per mm
77
78 inline void clear() {
79 valid = false;
80 minValue = 0;
81 maxValue = 0;
82 flat = 0;
83 fuzz = 0;
84 resolution = 0;
85 }
86};
87
88/*
89 * Input device classes.
90 */
Chris Ye1b0c7342020-07-28 21:57:03 -070091enum class InputDeviceClass : uint32_t {
Michael Wrightd02c5b62014-02-10 15:10:22 -080092 /* The input device is a keyboard or has buttons. */
Chris Ye1b0c7342020-07-28 21:57:03 -070093 KEYBOARD = 0x00000001,
Michael Wrightd02c5b62014-02-10 15:10:22 -080094
95 /* The input device is an alpha-numeric keyboard (not just a dial pad). */
Chris Ye1b0c7342020-07-28 21:57:03 -070096 ALPHAKEY = 0x00000002,
Michael Wrightd02c5b62014-02-10 15:10:22 -080097
98 /* The input device is a touchscreen or a touchpad (either single-touch or multi-touch). */
Chris Ye1b0c7342020-07-28 21:57:03 -070099 TOUCH = 0x00000004,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800100
101 /* The input device is a cursor device such as a trackball or mouse. */
Chris Ye1b0c7342020-07-28 21:57:03 -0700102 CURSOR = 0x00000008,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800103
104 /* The input device is a multi-touch touchscreen. */
Chris Ye1b0c7342020-07-28 21:57:03 -0700105 TOUCH_MT = 0x00000010,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800106
107 /* The input device is a directional pad (implies keyboard, has DPAD keys). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700108 DPAD = 0x00000020,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800109
110 /* The input device is a gamepad (implies keyboard, has BUTTON keys). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700111 GAMEPAD = 0x00000040,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800112
113 /* The input device has switches. */
Chris Ye1b0c7342020-07-28 21:57:03 -0700114 SWITCH = 0x00000080,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800115
116 /* The input device is a joystick (implies gamepad, has joystick absolute axes). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700117 JOYSTICK = 0x00000100,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800118
119 /* The input device has a vibrator (supports FF_RUMBLE). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700120 VIBRATOR = 0x00000200,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800121
Tim Kilbourn063ff532015-04-08 10:26:18 -0700122 /* The input device has a microphone. */
Chris Ye1b0c7342020-07-28 21:57:03 -0700123 MIC = 0x00000400,
Tim Kilbourn063ff532015-04-08 10:26:18 -0700124
Michael Wright842500e2015-03-13 17:32:02 -0700125 /* The input device is an external stylus (has data we want to fuse with touch data). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700126 EXTERNAL_STYLUS = 0x00000800,
Michael Wright842500e2015-03-13 17:32:02 -0700127
Prashant Malani1941ff52015-08-11 18:29:28 -0700128 /* The input device has a rotary encoder */
Chris Ye1b0c7342020-07-28 21:57:03 -0700129 ROTARY_ENCODER = 0x00001000,
Prashant Malani1941ff52015-08-11 18:29:28 -0700130
Chris Yef59a2f42020-10-16 12:55:26 -0700131 /* The input device has a sensor like accelerometer, gyro, etc */
132 SENSOR = 0x00002000,
133
Kim Low03ea0352020-11-06 12:45:07 -0800134 /* The input device has a battery */
135 BATTERY = 0x00004000,
136
Chris Ye3fdbfef2021-01-06 18:45:18 -0800137 /* The input device has sysfs controllable lights */
138 LIGHT = 0x00008000,
139
Michael Wrightd02c5b62014-02-10 15:10:22 -0800140 /* The input device is virtual (not a real device, not part of UI configuration). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700141 VIRTUAL = 0x40000000,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800142
143 /* The input device is external (not built-in). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700144 EXTERNAL = 0x80000000,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145};
146
Chris Ye3fdbfef2021-01-06 18:45:18 -0800147enum class SysfsClass : uint32_t {
148 POWER_SUPPLY = 0,
149 LEDS = 1,
Dominik Laskowski75788452021-02-09 18:51:25 -0800150
151 ftl_last = LEDS
Chris Ye3fdbfef2021-01-06 18:45:18 -0800152};
153
154enum class LightColor : uint32_t {
155 RED = 0,
156 GREEN = 1,
157 BLUE = 2,
158};
159
160enum class InputLightClass : uint32_t {
161 /* The input light has brightness node. */
162 BRIGHTNESS = 0x00000001,
163 /* The input light has red name. */
164 RED = 0x00000002,
165 /* The input light has green name. */
166 GREEN = 0x00000004,
167 /* The input light has blue name. */
168 BLUE = 0x00000008,
169 /* The input light has global name. */
170 GLOBAL = 0x00000010,
171 /* The input light has multi index node. */
172 MULTI_INDEX = 0x00000020,
173 /* The input light has multi intensity node. */
174 MULTI_INTENSITY = 0x00000040,
175 /* The input light has max brightness node. */
176 MAX_BRIGHTNESS = 0x00000080,
177};
178
Chris Yee2b1e5c2021-03-10 22:45:12 -0800179enum class InputBatteryClass : uint32_t {
180 /* The input device battery has capacity node. */
181 CAPACITY = 0x00000001,
182 /* The input device battery has capacity_level node. */
183 CAPACITY_LEVEL = 0x00000002,
184 /* The input device battery has status node. */
185 STATUS = 0x00000004,
186};
187
Chris Ye3fdbfef2021-01-06 18:45:18 -0800188/* Describes a raw light. */
189struct RawLightInfo {
190 int32_t id;
191 std::string name;
192 std::optional<int32_t> maxBrightness;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700193 ftl::Flags<InputLightClass> flags;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800194 std::array<int32_t, COLOR_NUM> rgbIndex;
195 std::filesystem::path path;
196};
197
Chris Yee2b1e5c2021-03-10 22:45:12 -0800198/* Describes a raw battery. */
199struct RawBatteryInfo {
200 int32_t id;
201 std::string name;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700202 ftl::Flags<InputBatteryClass> flags;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800203 std::filesystem::path path;
204};
205
Michael Wrightd02c5b62014-02-10 15:10:22 -0800206/*
207 * Gets the class that owns an axis, in cases where multiple classes might claim
208 * the same axis for different purposes.
209 */
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700210extern ftl::Flags<InputDeviceClass> getAbsAxisUsage(int32_t axis,
211 ftl::Flags<InputDeviceClass> deviceClasses);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800212
213/*
214 * Grand Central Station for events.
215 *
216 * The event hub aggregates input events received across all known input
217 * devices on the system, including devices that may be emulated by the simulator
218 * environment. In addition, the event hub generates fake input events to indicate
219 * when devices are added or removed.
220 *
221 * The event hub provides a stream of input events (via the getEvent function).
222 * It also supports querying the current actual state of input devices such as identifying
223 * which keys are currently down. Finally, the event hub keeps track of the capabilities of
224 * individual input devices, such as their class and the set of key codes that they support.
225 */
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700226class EventHubInterface {
227public:
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700228 EventHubInterface() {}
229 virtual ~EventHubInterface() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800230
Michael Wrightd02c5b62014-02-10 15:10:22 -0800231 // Synthetic raw event type codes produced when devices are added or removed.
232 enum {
233 // Sent when a device is added.
234 DEVICE_ADDED = 0x10000000,
235 // Sent when a device is removed.
236 DEVICE_REMOVED = 0x20000000,
237 // Sent when all added/removed devices from the most recent scan have been reported.
238 // This event is always sent at least once.
239 FINISHED_DEVICE_SCAN = 0x30000000,
240
241 FIRST_SYNTHETIC_EVENT = DEVICE_ADDED,
242 };
243
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700244 virtual ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800245
246 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const = 0;
247
248 virtual int32_t getDeviceControllerNumber(int32_t deviceId) const = 0;
249
250 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const = 0;
251
252 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700253 RawAbsoluteAxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800254
255 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const = 0;
256
257 virtual bool hasInputProperty(int32_t deviceId, int property) const = 0;
258
Chris Yef59a2f42020-10-16 12:55:26 -0700259 virtual bool hasMscEvent(int32_t deviceId, int mscEvent) const = 0;
260
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700261 virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
262 int32_t metaState, int32_t* outKeycode, int32_t* outMetaState,
263 uint32_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800264
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700265 virtual status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800266
267 // Sets devices that are excluded from opening.
268 // This can be used to ignore input devices for sensors.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100269 virtual void setExcludedDevices(const std::vector<std::string>& devices) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800270
271 /*
272 * Wait for events to become available and returns them.
273 * After returning, the EventHub holds onto a wake lock until the next call to getEvent.
274 * This ensures that the device will not go to sleep while the event is being processed.
275 * If the device needs to remain awake longer than that, then the caller is responsible
276 * for taking care of it (say, by poking the power manager user activity timer).
277 *
278 * The timeout is advisory only. If the device is asleep, it will not wake just to
279 * service the timeout.
280 *
281 * Returns the number of events obtained, or 0 if the timeout expired.
282 */
283 virtual size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) = 0;
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800284 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) = 0;
Chris Yef59a2f42020-10-16 12:55:26 -0700285 virtual base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
286 int32_t absCode) = 0;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800287 // Raw batteries are sysfs power_supply nodes we found from the EventHub device sysfs node,
288 // containing the raw info of the sysfs node structure.
289 virtual const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) = 0;
290 virtual std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
291 int32_t BatteryId) = 0;
292
Chris Ye3fdbfef2021-01-06 18:45:18 -0800293 // Raw lights are sysfs led light nodes we found from the EventHub device sysfs node,
294 // containing the raw info of the sysfs node structure.
295 virtual const std::vector<int32_t> getRawLightIds(int32_t deviceId) = 0;
296 virtual std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) = 0;
297 virtual std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) = 0;
298 virtual void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) = 0;
299 virtual std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
300 int32_t deviceId, int32_t lightId) = 0;
301 virtual void setLightIntensities(int32_t deviceId, int32_t lightId,
302 std::unordered_map<LightColor, int32_t> intensities) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800303 /*
304 * Query current input state.
305 */
306 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0;
307 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0;
308 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0;
309 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700310 int32_t* outValue) const = 0;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100311 virtual int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800312
313 /*
314 * Examine key input devices for specific framework keycode support
315 */
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700316 virtual bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700317 uint8_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800318
319 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const = 0;
Arthur Hungcb40a002021-08-03 14:31:01 +0000320 virtual bool hasKeyCode(int32_t deviceId, int32_t keyCode) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800321
322 /* LED related functions expect Android LED constants, not scan codes or HID usages */
323 virtual bool hasLed(int32_t deviceId, int32_t led) const = 0;
324 virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0;
325
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700326 virtual void getVirtualKeyDefinitions(
327 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800328
Chris Ye3a1e4462020-08-12 10:13:15 -0700329 virtual const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const = 0;
330 virtual bool setKeyboardLayoutOverlay(int32_t deviceId,
331 std::shared_ptr<KeyCharacterMap> map) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800332
333 /* Control the vibrator. */
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000334 virtual void vibrate(int32_t deviceId, const VibrationElement& effect) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800335 virtual void cancelVibrate(int32_t deviceId) = 0;
Chris Ye87143712020-11-10 05:05:58 +0000336 virtual std::vector<int32_t> getVibratorIds(int32_t deviceId) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800337
Kim Low03ea0352020-11-06 12:45:07 -0800338 /* Query battery level. */
Chris Yee2b1e5c2021-03-10 22:45:12 -0800339 virtual std::optional<int32_t> getBatteryCapacity(int32_t deviceId,
340 int32_t batteryId) const = 0;
Kim Low03ea0352020-11-06 12:45:07 -0800341
342 /* Query battery status. */
Chris Yee2b1e5c2021-03-10 22:45:12 -0800343 virtual std::optional<int32_t> getBatteryStatus(int32_t deviceId, int32_t batteryId) const = 0;
Kim Low03ea0352020-11-06 12:45:07 -0800344
Michael Wrightd02c5b62014-02-10 15:10:22 -0800345 /* Requests the EventHub to reopen all input devices on the next call to getEvents(). */
346 virtual void requestReopenDevices() = 0;
347
348 /* Wakes up getEvents() if it is blocked on a read. */
349 virtual void wake() = 0;
350
351 /* Dump EventHub state to a string. */
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800352 virtual void dump(std::string& dump) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800353
354 /* Called by the heatbeat to ensures that the reader has not deadlocked. */
355 virtual void monitor() = 0;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700356
357 /* Return true if the device is enabled. */
358 virtual bool isDeviceEnabled(int32_t deviceId) = 0;
359
360 /* Enable an input device */
361 virtual status_t enableDevice(int32_t deviceId) = 0;
362
363 /* Disable an input device. Closes file descriptor to that device. */
364 virtual status_t disableDevice(int32_t deviceId) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800365};
366
Chris Ye66fbac32020-07-06 20:36:43 -0700367template <std::size_t BITS>
368class BitArray {
369 /* Array element type and vector of element type. */
370 using Element = std::uint32_t;
371 /* Number of bits in each BitArray element. */
372 static constexpr size_t WIDTH = sizeof(Element) * CHAR_BIT;
373 /* Number of elements to represent a bit array of the specified size of bits. */
374 static constexpr size_t COUNT = (BITS + WIDTH - 1) / WIDTH;
375
376public:
377 /* BUFFER type declaration for BitArray */
378 using Buffer = std::array<Element, COUNT>;
379 /* To tell if a bit is set in array, it selects an element from the array, and test
380 * if the relevant bit set.
381 * Note the parameter "bit" is an index to the bit, 0 <= bit < BITS.
382 */
383 inline bool test(size_t bit) const {
384 return (bit < BITS) ? mData[bit / WIDTH].test(bit % WIDTH) : false;
385 }
386 /* Returns total number of bytes needed for the array */
387 inline size_t bytes() { return (BITS + CHAR_BIT - 1) / CHAR_BIT; }
388 /* Returns true if array contains any non-zero bit from the range defined by start and end
389 * bit index [startIndex, endIndex).
390 */
391 bool any(size_t startIndex, size_t endIndex) {
392 if (startIndex >= endIndex || startIndex > BITS || endIndex > BITS + 1) {
393 ALOGE("Invalid start/end index. start = %zu, end = %zu, total bits = %zu", startIndex,
394 endIndex, BITS);
395 return false;
396 }
397 size_t se = startIndex / WIDTH; // Start of element
398 size_t ee = endIndex / WIDTH; // End of element
399 size_t si = startIndex % WIDTH; // Start index in start element
400 size_t ei = endIndex % WIDTH; // End index in end element
401 // Need to check first unaligned bitset for any non zero bit
402 if (si > 0) {
403 size_t nBits = se == ee ? ei - si : WIDTH - si;
404 // Generate the mask of interested bit range
405 Element mask = ((1 << nBits) - 1) << si;
406 if (mData[se++].to_ulong() & mask) {
407 return true;
408 }
409 }
410 // Check whole bitset for any bit set
411 for (; se < ee; se++) {
412 if (mData[se].any()) {
413 return true;
414 }
415 }
416 // Need to check last unaligned bitset for any non zero bit
417 if (ei > 0 && se <= ee) {
418 // Generate the mask of interested bit range
419 Element mask = (1 << ei) - 1;
420 if (mData[se].to_ulong() & mask) {
421 return true;
422 }
423 }
424 return false;
425 }
426 /* Load bit array values from buffer */
427 void loadFromBuffer(const Buffer& buffer) {
428 for (size_t i = 0; i < COUNT; i++) {
429 mData[i] = std::bitset<WIDTH>(buffer[i]);
430 }
431 }
432
433private:
434 std::array<std::bitset<WIDTH>, COUNT> mData;
435};
436
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700437class EventHub : public EventHubInterface {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800438public:
439 EventHub();
440
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700441 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800442
Chris Ye989bb932020-07-04 16:18:59 -0700443 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800444
Chris Ye989bb932020-07-04 16:18:59 -0700445 int32_t getDeviceControllerNumber(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800446
Chris Ye989bb932020-07-04 16:18:59 -0700447 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800448
Chris Ye989bb932020-07-04 16:18:59 -0700449 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
450 RawAbsoluteAxisInfo* outAxisInfo) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800451
Chris Ye989bb932020-07-04 16:18:59 -0700452 bool hasRelativeAxis(int32_t deviceId, int axis) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800453
Chris Ye989bb932020-07-04 16:18:59 -0700454 bool hasInputProperty(int32_t deviceId, int property) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800455
Chris Yef59a2f42020-10-16 12:55:26 -0700456 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final;
457
Chris Ye989bb932020-07-04 16:18:59 -0700458 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
459 int32_t* outKeycode, int32_t* outMetaState,
460 uint32_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800461
Chris Ye989bb932020-07-04 16:18:59 -0700462 status_t mapAxis(int32_t deviceId, int32_t scanCode,
463 AxisInfo* outAxisInfo) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800464
Chris Yef59a2f42020-10-16 12:55:26 -0700465 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
466 int32_t deviceId, int32_t absCode) override final;
467
Chris Yee2b1e5c2021-03-10 22:45:12 -0800468 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) override final;
469 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
470 int32_t BatteryId) override final;
471
Chris Ye3fdbfef2021-01-06 18:45:18 -0800472 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override final;
473
474 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override final;
475
476 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override final;
477 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override final;
478 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
479 int32_t deviceId, int32_t lightId) override final;
480 void setLightIntensities(int32_t deviceId, int32_t lightId,
481 std::unordered_map<LightColor, int32_t> intensities) override final;
482
Chris Ye989bb932020-07-04 16:18:59 -0700483 void setExcludedDevices(const std::vector<std::string>& devices) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800484
Chris Ye989bb932020-07-04 16:18:59 -0700485 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override final;
486 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override final;
487 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override final;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100488 int32_t getKeyCodeForKeyLocation(int32_t deviceId,
489 int32_t locationKeyCode) const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700490 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
491 int32_t* outValue) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800492
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700493 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Chris Ye989bb932020-07-04 16:18:59 -0700494 uint8_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800495
Chris Ye989bb932020-07-04 16:18:59 -0700496 size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) override final;
497 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800498
Chris Ye989bb932020-07-04 16:18:59 -0700499 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override final;
Arthur Hungcb40a002021-08-03 14:31:01 +0000500 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override final;
Chris Ye989bb932020-07-04 16:18:59 -0700501 bool hasLed(int32_t deviceId, int32_t led) const override final;
502 void setLedState(int32_t deviceId, int32_t led, bool on) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800503
Chris Ye989bb932020-07-04 16:18:59 -0700504 void getVirtualKeyDefinitions(
505 int32_t deviceId,
506 std::vector<VirtualKeyDefinition>& outVirtualKeys) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800507
Chris Ye3a1e4462020-08-12 10:13:15 -0700508 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(
509 int32_t deviceId) const override final;
510 bool setKeyboardLayoutOverlay(int32_t deviceId,
511 std::shared_ptr<KeyCharacterMap> map) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800512
Chris Ye989bb932020-07-04 16:18:59 -0700513 void vibrate(int32_t deviceId, const VibrationElement& effect) override final;
514 void cancelVibrate(int32_t deviceId) override final;
Chris Ye87143712020-11-10 05:05:58 +0000515 std::vector<int32_t> getVibratorIds(int32_t deviceId) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800516
Chris Ye989bb932020-07-04 16:18:59 -0700517 void requestReopenDevices() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800518
Chris Ye989bb932020-07-04 16:18:59 -0700519 void wake() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800520
Chris Ye989bb932020-07-04 16:18:59 -0700521 void dump(std::string& dump) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800522
Chris Ye989bb932020-07-04 16:18:59 -0700523 void monitor() override final;
524
Chris Yee2b1e5c2021-03-10 22:45:12 -0800525 std::optional<int32_t> getBatteryCapacity(int32_t deviceId,
526 int32_t batteryId) const override final;
Kim Low03ea0352020-11-06 12:45:07 -0800527
Chris Yee2b1e5c2021-03-10 22:45:12 -0800528 std::optional<int32_t> getBatteryStatus(int32_t deviceId,
529 int32_t batteryId) const override final;
Kim Low03ea0352020-11-06 12:45:07 -0800530
Chris Ye989bb932020-07-04 16:18:59 -0700531 bool isDeviceEnabled(int32_t deviceId) override final;
532
533 status_t enableDevice(int32_t deviceId) override final;
534
535 status_t disableDevice(int32_t deviceId) override final;
536
537 ~EventHub() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800538
539private:
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700540 struct AssociatedDevice {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800541 // The sysfs root path of the misc device.
542 std::filesystem::path sysfsRootPath;
543
544 int32_t nextBatteryId;
545 int32_t nextLightId;
546 std::unordered_map<int32_t, RawBatteryInfo> batteryInfos;
547 std::unordered_map<int32_t, RawLightInfo> lightInfos;
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700548 explicit AssociatedDevice(std::filesystem::path sysfsRootPath)
Chris Yee2b1e5c2021-03-10 22:45:12 -0800549 : sysfsRootPath(sysfsRootPath), nextBatteryId(0), nextLightId(0) {}
550 bool configureBatteryLocked();
551 bool configureLightsLocked();
552 };
553
Michael Wrightd02c5b62014-02-10 15:10:22 -0800554 struct Device {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700555 int fd; // may be -1 if device is closed
Michael Wrightd02c5b62014-02-10 15:10:22 -0800556 const int32_t id;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100557 const std::string path;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800558 const InputDeviceIdentifier identifier;
559
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -0800560 std::unique_ptr<TouchVideoDevice> videoDevice;
561
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700562 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800563
Chris Ye66fbac32020-07-06 20:36:43 -0700564 BitArray<KEY_MAX> keyBitmask;
565 BitArray<KEY_MAX> keyState;
566 BitArray<ABS_MAX> absBitmask;
567 BitArray<REL_MAX> relBitmask;
568 BitArray<SW_MAX> swBitmask;
569 BitArray<SW_MAX> swState;
570 BitArray<LED_MAX> ledBitmask;
571 BitArray<FF_MAX> ffBitmask;
572 BitArray<INPUT_PROP_MAX> propBitmask;
Chris Yef59a2f42020-10-16 12:55:26 -0700573 BitArray<MSC_MAX> mscBitmask;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800574
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100575 std::string configurationFile;
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500576 std::unique_ptr<PropertyMap> configuration;
Siarhei Vishniakou3e78dec2019-02-20 16:21:46 -0600577 std::unique_ptr<VirtualKeyMap> virtualKeyMap;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800578 KeyMap keyMap;
579
Michael Wrightd02c5b62014-02-10 15:10:22 -0800580 bool ffEffectPlaying;
581 int16_t ffEffectId; // initially -1
582
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700583 // A shared_ptr of a device associated with the input device.
Prabir Pradhan51894782022-08-23 16:29:10 +0000584 // The input devices that have the same sysfs path have the same associated device.
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700585 std::shared_ptr<AssociatedDevice> associatedDevice;
Kim Low03ea0352020-11-06 12:45:07 -0800586
Michael Wrightd02c5b62014-02-10 15:10:22 -0800587 int32_t controllerNumber;
588
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100589 Device(int fd, int32_t id, const std::string& path,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700590 const InputDeviceIdentifier& identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800591 ~Device();
592
593 void close();
594
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700595 bool enabled; // initially true
596 status_t enable();
597 status_t disable();
Chris Ye989bb932020-07-04 16:18:59 -0700598 bool hasValidFd() const;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700599 const bool isVirtual; // set if fd < 0 is passed to constructor
Michael Wrightd02c5b62014-02-10 15:10:22 -0800600
Chris Ye3a1e4462020-08-12 10:13:15 -0700601 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const;
Chris Ye66fbac32020-07-06 20:36:43 -0700602
603 template <std::size_t N>
Chris Ye989bb932020-07-04 16:18:59 -0700604 status_t readDeviceBitMask(unsigned long ioctlCode, BitArray<N>& bitArray);
Chris Ye66fbac32020-07-06 20:36:43 -0700605
Chris Ye989bb932020-07-04 16:18:59 -0700606 void configureFd();
607 bool hasKeycodeLocked(int keycode) const;
608 void loadConfigurationLocked();
609 bool loadVirtualKeyMapLocked();
610 status_t loadKeyMapLocked();
611 bool isExternalDeviceLocked();
612 bool deviceHasMicLocked();
613 void setLedForControllerLocked();
614 status_t mapLed(int32_t led, int32_t* outScanCode) const;
615 void setLedStateLocked(int32_t led, bool on);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800616 };
617
Siarhei Vishniakoua4c502a2021-02-05 00:45:20 +0000618 /**
619 * Create a new device for the provided path.
620 */
Chris Yed3fef462021-03-07 17:10:08 -0800621 void openDeviceLocked(const std::string& devicePath) REQUIRES(mLock);
622 void openVideoDeviceLocked(const std::string& devicePath) REQUIRES(mLock);
Siarhei Vishniakouf49608d2020-08-20 19:18:21 -0500623 /**
624 * Try to associate a video device with an input device. If the association succeeds,
625 * the videoDevice is moved into the input device. 'videoDevice' will become null if this
626 * happens.
627 * Return true if the association succeeds.
628 * Return false otherwise.
629 */
Chris Yed3fef462021-03-07 17:10:08 -0800630 bool tryAddVideoDeviceLocked(Device& device, std::unique_ptr<TouchVideoDevice>& videoDevice)
631 REQUIRES(mLock);
632 void createVirtualKeyboardLocked() REQUIRES(mLock);
633 void addDeviceLocked(std::unique_ptr<Device> device) REQUIRES(mLock);
634 void assignDescriptorLocked(InputDeviceIdentifier& identifier) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800635
Chris Yed3fef462021-03-07 17:10:08 -0800636 void closeDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock);
637 void closeVideoDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock);
638 void closeDeviceLocked(Device& device) REQUIRES(mLock);
639 void closeAllDevicesLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800640
Siarhei Vishniakou25920312018-12-12 15:24:44 -0800641 status_t registerFdForEpoll(int fd);
642 status_t unregisterFdFromEpoll(int fd);
Chris Yed3fef462021-03-07 17:10:08 -0800643 status_t registerDeviceForEpollLocked(Device& device) REQUIRES(mLock);
644 void registerVideoDeviceForEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock);
645 status_t unregisterDeviceFromEpollLocked(Device& device) REQUIRES(mLock);
646 void unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700647
Chris Yed3fef462021-03-07 17:10:08 -0800648 status_t scanDirLocked(const std::string& dirname) REQUIRES(mLock);
649 status_t scanVideoDirLocked(const std::string& dirname) REQUIRES(mLock);
650 void scanDevicesLocked() REQUIRES(mLock);
Prabir Pradhan952e65b2022-06-23 17:49:55 +0000651 base::Result<void> readNotifyLocked() REQUIRES(mLock);
652 void handleNotifyEventLocked(const inotify_event&) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800653
Chris Yed3fef462021-03-07 17:10:08 -0800654 Device* getDeviceLocked(int32_t deviceId) const REQUIRES(mLock);
655 Device* getDeviceByPathLocked(const std::string& devicePath) const REQUIRES(mLock);
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700656 /**
657 * Look through all available fd's (both for input devices and for video devices),
658 * and return the device pointer.
659 */
Chris Yed3fef462021-03-07 17:10:08 -0800660 Device* getDeviceByFdLocked(int fd) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800661
Chris Yed3fef462021-03-07 17:10:08 -0800662 int32_t getNextControllerNumberLocked(const std::string& name) REQUIRES(mLock);
Josh Bartel938632f2022-07-19 15:34:22 -0500663
664 bool hasDeviceWithDescriptorLocked(const std::string& descriptor) const REQUIRES(mLock);
665
Chris Yed3fef462021-03-07 17:10:08 -0800666 void releaseControllerNumberLocked(int32_t num) REQUIRES(mLock);
667 void reportDeviceAddedForStatisticsLocked(const InputDeviceIdentifier& identifier,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700668 ftl::Flags<InputDeviceClass> classes) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800669
Chris Yee2b1e5c2021-03-10 22:45:12 -0800670 const std::unordered_map<int32_t, RawBatteryInfo>& getBatteryInfoLocked(int32_t deviceId) const
671 REQUIRES(mLock);
672
673 const std::unordered_map<int32_t, RawLightInfo>& getLightInfoLocked(int32_t deviceId) const
674 REQUIRES(mLock);
675
Usama Arifb27c8e62021-06-03 16:44:09 +0100676 void addDeviceInputInotify();
677 void addDeviceInotify();
678
Michael Wrightd02c5b62014-02-10 15:10:22 -0800679 // Protect all internal state.
Chris Ye1c2e0892020-11-30 21:41:44 -0800680 mutable std::mutex mLock;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800681
682 // The actual id of the built-in keyboard, or NO_BUILT_IN_KEYBOARD if none.
683 // EventHub remaps the built-in keyboard to id 0 externally as required by the API.
684 enum {
685 // Must not conflict with any other assigned device ids, including
686 // the virtual keyboard id (-1).
687 NO_BUILT_IN_KEYBOARD = -2,
688 };
689 int32_t mBuiltInKeyboardId;
690
691 int32_t mNextDeviceId;
692
693 BitSet32 mControllerNumbers;
694
Chris Ye989bb932020-07-04 16:18:59 -0700695 std::unordered_map<int32_t, std::unique_ptr<Device>> mDevices;
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800696 /**
697 * Video devices that report touchscreen heatmap, but have not (yet) been paired
698 * with a specific input device. Video device discovery is independent from input device
699 * discovery, so the two types of devices could be found in any order.
700 * Ideally, video devices in this queue do not have an open fd, or at least aren't
701 * actively streaming.
702 */
703 std::vector<std::unique_ptr<TouchVideoDevice>> mUnattachedVideoDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800704
Chris Ye989bb932020-07-04 16:18:59 -0700705 std::vector<std::unique_ptr<Device>> mOpeningDevices;
706 std::vector<std::unique_ptr<Device>> mClosingDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800707
708 bool mNeedToSendFinishedDeviceScan;
709 bool mNeedToReopenDevices;
710 bool mNeedToScanDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100711 std::vector<std::string> mExcludedDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800712
713 int mEpollFd;
714 int mINotifyFd;
715 int mWakeReadPipeFd;
716 int mWakeWritePipeFd;
717
Usama Arifb27c8e62021-06-03 16:44:09 +0100718 int mDeviceInputWd;
719 int mDeviceWd = -1;
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800720
Michael Wrightd02c5b62014-02-10 15:10:22 -0800721 // Maximum number of signalled FDs to handle at a time.
722 static const int EPOLL_MAX_EVENTS = 16;
723
724 // The array of pending epoll events and the index of the next event to be handled.
725 struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS];
726 size_t mPendingEventCount;
727 size_t mPendingEventIndex;
728 bool mPendingINotify;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800729};
730
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700731} // namespace android
Michael Wrightd02c5b62014-02-10 15:10:22 -0800732
733#endif // _RUNTIME_EVENT_HUB_H