blob: 682772cacbbcd47fd0ae88c15b832125213d36f7 [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>
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010022#include <vector>
23
Chris Ye1b0c7342020-07-28 21:57:03 -070024#include <input/Flags.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080025#include <input/Input.h>
26#include <input/InputDevice.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080027#include <input/KeyCharacterMap.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070028#include <input/KeyLayoutMap.h>
29#include <input/Keyboard.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080030#include <input/VirtualKeyMap.h>
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000031#include <linux/input.h>
32#include <sys/epoll.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080033#include <utils/BitSet.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070034#include <utils/Errors.h>
35#include <utils/KeyedVector.h>
36#include <utils/List.h>
37#include <utils/Log.h>
38#include <utils/Mutex.h>
39#include <utils/PropertyMap.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080040
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080041#include "TouchVideoDevice.h"
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000042#include "VibrationElement.h"
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080043
Michael Wrightd02c5b62014-02-10 15:10:22 -080044namespace android {
45
Michael Wrightd02c5b62014-02-10 15:10:22 -080046/*
47 * A raw event as retrieved from the EventHub.
48 */
49struct RawEvent {
50 nsecs_t when;
51 int32_t deviceId;
52 int32_t type;
53 int32_t code;
54 int32_t value;
55};
56
57/* Describes an absolute axis. */
58struct RawAbsoluteAxisInfo {
59 bool valid; // true if the information is valid, false otherwise
60
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070061 int32_t minValue; // minimum value
62 int32_t maxValue; // maximum value
63 int32_t flat; // center flat position, eg. flat == 8 means center is between -8 and 8
64 int32_t fuzz; // error tolerance, eg. fuzz == 4 means value is +/- 4 due to noise
Michael Wrightd02c5b62014-02-10 15:10:22 -080065 int32_t resolution; // resolution in units per mm or radians per mm
66
67 inline void clear() {
68 valid = false;
69 minValue = 0;
70 maxValue = 0;
71 flat = 0;
72 fuzz = 0;
73 resolution = 0;
74 }
75};
76
77/*
78 * Input device classes.
79 */
Chris Ye1b0c7342020-07-28 21:57:03 -070080enum class InputDeviceClass : uint32_t {
Michael Wrightd02c5b62014-02-10 15:10:22 -080081 /* The input device is a keyboard or has buttons. */
Chris Ye1b0c7342020-07-28 21:57:03 -070082 KEYBOARD = 0x00000001,
Michael Wrightd02c5b62014-02-10 15:10:22 -080083
84 /* The input device is an alpha-numeric keyboard (not just a dial pad). */
Chris Ye1b0c7342020-07-28 21:57:03 -070085 ALPHAKEY = 0x00000002,
Michael Wrightd02c5b62014-02-10 15:10:22 -080086
87 /* The input device is a touchscreen or a touchpad (either single-touch or multi-touch). */
Chris Ye1b0c7342020-07-28 21:57:03 -070088 TOUCH = 0x00000004,
Michael Wrightd02c5b62014-02-10 15:10:22 -080089
90 /* The input device is a cursor device such as a trackball or mouse. */
Chris Ye1b0c7342020-07-28 21:57:03 -070091 CURSOR = 0x00000008,
Michael Wrightd02c5b62014-02-10 15:10:22 -080092
93 /* The input device is a multi-touch touchscreen. */
Chris Ye1b0c7342020-07-28 21:57:03 -070094 TOUCH_MT = 0x00000010,
Michael Wrightd02c5b62014-02-10 15:10:22 -080095
96 /* The input device is a directional pad (implies keyboard, has DPAD keys). */
Chris Ye1b0c7342020-07-28 21:57:03 -070097 DPAD = 0x00000020,
Michael Wrightd02c5b62014-02-10 15:10:22 -080098
99 /* The input device is a gamepad (implies keyboard, has BUTTON keys). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700100 GAMEPAD = 0x00000040,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800101
102 /* The input device has switches. */
Chris Ye1b0c7342020-07-28 21:57:03 -0700103 SWITCH = 0x00000080,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800104
105 /* The input device is a joystick (implies gamepad, has joystick absolute axes). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700106 JOYSTICK = 0x00000100,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800107
108 /* The input device has a vibrator (supports FF_RUMBLE). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700109 VIBRATOR = 0x00000200,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800110
Tim Kilbourn063ff532015-04-08 10:26:18 -0700111 /* The input device has a microphone. */
Chris Ye1b0c7342020-07-28 21:57:03 -0700112 MIC = 0x00000400,
Tim Kilbourn063ff532015-04-08 10:26:18 -0700113
Michael Wright842500e2015-03-13 17:32:02 -0700114 /* The input device is an external stylus (has data we want to fuse with touch data). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700115 EXTERNAL_STYLUS = 0x00000800,
Michael Wright842500e2015-03-13 17:32:02 -0700116
Prashant Malani1941ff52015-08-11 18:29:28 -0700117 /* The input device has a rotary encoder */
Chris Ye1b0c7342020-07-28 21:57:03 -0700118 ROTARY_ENCODER = 0x00001000,
Prashant Malani1941ff52015-08-11 18:29:28 -0700119
Michael Wrightd02c5b62014-02-10 15:10:22 -0800120 /* The input device is virtual (not a real device, not part of UI configuration). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700121 VIRTUAL = 0x40000000,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800122
123 /* The input device is external (not built-in). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700124 EXTERNAL = 0x80000000,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800125};
126
127/*
128 * Gets the class that owns an axis, in cases where multiple classes might claim
129 * the same axis for different purposes.
130 */
Chris Ye1b0c7342020-07-28 21:57:03 -0700131extern Flags<InputDeviceClass> getAbsAxisUsage(int32_t axis, Flags<InputDeviceClass> deviceClasses);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800132
133/*
134 * Grand Central Station for events.
135 *
136 * The event hub aggregates input events received across all known input
137 * devices on the system, including devices that may be emulated by the simulator
138 * environment. In addition, the event hub generates fake input events to indicate
139 * when devices are added or removed.
140 *
141 * The event hub provides a stream of input events (via the getEvent function).
142 * It also supports querying the current actual state of input devices such as identifying
143 * which keys are currently down. Finally, the event hub keeps track of the capabilities of
144 * individual input devices, such as their class and the set of key codes that they support.
145 */
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700146class EventHubInterface {
147public:
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700148 EventHubInterface() {}
149 virtual ~EventHubInterface() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800150
Michael Wrightd02c5b62014-02-10 15:10:22 -0800151 // Synthetic raw event type codes produced when devices are added or removed.
152 enum {
153 // Sent when a device is added.
154 DEVICE_ADDED = 0x10000000,
155 // Sent when a device is removed.
156 DEVICE_REMOVED = 0x20000000,
157 // Sent when all added/removed devices from the most recent scan have been reported.
158 // This event is always sent at least once.
159 FINISHED_DEVICE_SCAN = 0x30000000,
160
161 FIRST_SYNTHETIC_EVENT = DEVICE_ADDED,
162 };
163
Chris Ye1b0c7342020-07-28 21:57:03 -0700164 virtual Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800165
166 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const = 0;
167
168 virtual int32_t getDeviceControllerNumber(int32_t deviceId) const = 0;
169
170 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const = 0;
171
172 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700173 RawAbsoluteAxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800174
175 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const = 0;
176
177 virtual bool hasInputProperty(int32_t deviceId, int property) const = 0;
178
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700179 virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
180 int32_t metaState, int32_t* outKeycode, int32_t* outMetaState,
181 uint32_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800182
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700183 virtual status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800184
185 // Sets devices that are excluded from opening.
186 // This can be used to ignore input devices for sensors.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100187 virtual void setExcludedDevices(const std::vector<std::string>& devices) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188
189 /*
190 * Wait for events to become available and returns them.
191 * After returning, the EventHub holds onto a wake lock until the next call to getEvent.
192 * This ensures that the device will not go to sleep while the event is being processed.
193 * If the device needs to remain awake longer than that, then the caller is responsible
194 * for taking care of it (say, by poking the power manager user activity timer).
195 *
196 * The timeout is advisory only. If the device is asleep, it will not wake just to
197 * service the timeout.
198 *
199 * Returns the number of events obtained, or 0 if the timeout expired.
200 */
201 virtual size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) = 0;
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800202 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800203
204 /*
205 * Query current input state.
206 */
207 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0;
208 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0;
209 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0;
210 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700211 int32_t* outValue) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800212
213 /*
214 * Examine key input devices for specific framework keycode support
215 */
216 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700217 uint8_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800218
219 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const = 0;
220
221 /* LED related functions expect Android LED constants, not scan codes or HID usages */
222 virtual bool hasLed(int32_t deviceId, int32_t led) const = 0;
223 virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0;
224
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700225 virtual void getVirtualKeyDefinitions(
226 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800227
228 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const = 0;
229 virtual bool setKeyboardLayoutOverlay(int32_t deviceId, const sp<KeyCharacterMap>& map) = 0;
230
231 /* Control the vibrator. */
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000232 virtual void vibrate(int32_t deviceId, const VibrationElement& effect) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800233 virtual void cancelVibrate(int32_t deviceId) = 0;
234
235 /* Requests the EventHub to reopen all input devices on the next call to getEvents(). */
236 virtual void requestReopenDevices() = 0;
237
238 /* Wakes up getEvents() if it is blocked on a read. */
239 virtual void wake() = 0;
240
241 /* Dump EventHub state to a string. */
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800242 virtual void dump(std::string& dump) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800243
244 /* Called by the heatbeat to ensures that the reader has not deadlocked. */
245 virtual void monitor() = 0;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700246
247 /* Return true if the device is enabled. */
248 virtual bool isDeviceEnabled(int32_t deviceId) = 0;
249
250 /* Enable an input device */
251 virtual status_t enableDevice(int32_t deviceId) = 0;
252
253 /* Disable an input device. Closes file descriptor to that device. */
254 virtual status_t disableDevice(int32_t deviceId) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800255};
256
Chris Ye66fbac32020-07-06 20:36:43 -0700257template <std::size_t BITS>
258class BitArray {
259 /* Array element type and vector of element type. */
260 using Element = std::uint32_t;
261 /* Number of bits in each BitArray element. */
262 static constexpr size_t WIDTH = sizeof(Element) * CHAR_BIT;
263 /* Number of elements to represent a bit array of the specified size of bits. */
264 static constexpr size_t COUNT = (BITS + WIDTH - 1) / WIDTH;
265
266public:
267 /* BUFFER type declaration for BitArray */
268 using Buffer = std::array<Element, COUNT>;
269 /* To tell if a bit is set in array, it selects an element from the array, and test
270 * if the relevant bit set.
271 * Note the parameter "bit" is an index to the bit, 0 <= bit < BITS.
272 */
273 inline bool test(size_t bit) const {
274 return (bit < BITS) ? mData[bit / WIDTH].test(bit % WIDTH) : false;
275 }
276 /* Returns total number of bytes needed for the array */
277 inline size_t bytes() { return (BITS + CHAR_BIT - 1) / CHAR_BIT; }
278 /* Returns true if array contains any non-zero bit from the range defined by start and end
279 * bit index [startIndex, endIndex).
280 */
281 bool any(size_t startIndex, size_t endIndex) {
282 if (startIndex >= endIndex || startIndex > BITS || endIndex > BITS + 1) {
283 ALOGE("Invalid start/end index. start = %zu, end = %zu, total bits = %zu", startIndex,
284 endIndex, BITS);
285 return false;
286 }
287 size_t se = startIndex / WIDTH; // Start of element
288 size_t ee = endIndex / WIDTH; // End of element
289 size_t si = startIndex % WIDTH; // Start index in start element
290 size_t ei = endIndex % WIDTH; // End index in end element
291 // Need to check first unaligned bitset for any non zero bit
292 if (si > 0) {
293 size_t nBits = se == ee ? ei - si : WIDTH - si;
294 // Generate the mask of interested bit range
295 Element mask = ((1 << nBits) - 1) << si;
296 if (mData[se++].to_ulong() & mask) {
297 return true;
298 }
299 }
300 // Check whole bitset for any bit set
301 for (; se < ee; se++) {
302 if (mData[se].any()) {
303 return true;
304 }
305 }
306 // Need to check last unaligned bitset for any non zero bit
307 if (ei > 0 && se <= ee) {
308 // Generate the mask of interested bit range
309 Element mask = (1 << ei) - 1;
310 if (mData[se].to_ulong() & mask) {
311 return true;
312 }
313 }
314 return false;
315 }
316 /* Load bit array values from buffer */
317 void loadFromBuffer(const Buffer& buffer) {
318 for (size_t i = 0; i < COUNT; i++) {
319 mData[i] = std::bitset<WIDTH>(buffer[i]);
320 }
321 }
322
323private:
324 std::array<std::bitset<WIDTH>, COUNT> mData;
325};
326
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700327class EventHub : public EventHubInterface {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800328public:
329 EventHub();
330
Chris Ye1b0c7342020-07-28 21:57:03 -0700331 virtual Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800332
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700333 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800334
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700335 virtual int32_t getDeviceControllerNumber(int32_t deviceId) const override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800336
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700337 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338
339 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700340 RawAbsoluteAxisInfo* outAxisInfo) const override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800341
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700342 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800343
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700344 virtual bool hasInputProperty(int32_t deviceId, int property) const override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800345
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700346 virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
347 int32_t metaState, int32_t* outKeycode, int32_t* outMetaState,
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700348 uint32_t* outFlags) const override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800349
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700350 virtual status_t mapAxis(int32_t deviceId, int32_t scanCode,
351 AxisInfo* outAxisInfo) const override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800352
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700353 virtual void setExcludedDevices(const std::vector<std::string>& devices) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800354
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700355 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override;
356 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override;
357 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const override;
358 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
359 int32_t* outValue) const override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800360
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700361 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700362 uint8_t* outFlags) const override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800363
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700364 virtual size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) override;
365 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800366
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700367 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const override;
368 virtual bool hasLed(int32_t deviceId, int32_t led) const override;
369 virtual void setLedState(int32_t deviceId, int32_t led, bool on) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800370
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700371 virtual void getVirtualKeyDefinitions(
372 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800373
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700374 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const override;
375 virtual bool setKeyboardLayoutOverlay(int32_t deviceId,
376 const sp<KeyCharacterMap>& map) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800377
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000378 virtual void vibrate(int32_t deviceId, const VibrationElement& effect) override;
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700379 virtual void cancelVibrate(int32_t deviceId) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800380
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700381 virtual void requestReopenDevices() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800382
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700383 virtual void wake() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800384
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700385 virtual void dump(std::string& dump) override;
386 virtual void monitor() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800387
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700388 virtual ~EventHub() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800389
390private:
391 struct Device {
392 Device* next;
393
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700394 int fd; // may be -1 if device is closed
Michael Wrightd02c5b62014-02-10 15:10:22 -0800395 const int32_t id;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100396 const std::string path;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800397 const InputDeviceIdentifier identifier;
398
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -0800399 std::unique_ptr<TouchVideoDevice> videoDevice;
400
Chris Ye1b0c7342020-07-28 21:57:03 -0700401 Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800402
Chris Ye66fbac32020-07-06 20:36:43 -0700403 BitArray<KEY_MAX> keyBitmask;
404 BitArray<KEY_MAX> keyState;
405 BitArray<ABS_MAX> absBitmask;
406 BitArray<REL_MAX> relBitmask;
407 BitArray<SW_MAX> swBitmask;
408 BitArray<SW_MAX> swState;
409 BitArray<LED_MAX> ledBitmask;
410 BitArray<FF_MAX> ffBitmask;
411 BitArray<INPUT_PROP_MAX> propBitmask;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800412
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100413 std::string configurationFile;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800414 PropertyMap* configuration;
Siarhei Vishniakou3e78dec2019-02-20 16:21:46 -0600415 std::unique_ptr<VirtualKeyMap> virtualKeyMap;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800416 KeyMap keyMap;
417
418 sp<KeyCharacterMap> overlayKeyMap;
419 sp<KeyCharacterMap> combinedKeyMap;
420
421 bool ffEffectPlaying;
422 int16_t ffEffectId; // initially -1
423
424 int32_t controllerNumber;
425
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100426 Device(int fd, int32_t id, const std::string& path,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700427 const InputDeviceIdentifier& identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800428 ~Device();
429
430 void close();
431
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700432 bool enabled; // initially true
433 status_t enable();
434 status_t disable();
435 bool hasValidFd();
436 const bool isVirtual; // set if fd < 0 is passed to constructor
Michael Wrightd02c5b62014-02-10 15:10:22 -0800437
438 const sp<KeyCharacterMap>& getKeyCharacterMap() const {
Yi Kong9b14ac62018-07-17 13:48:38 -0700439 if (combinedKeyMap != nullptr) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800440 return combinedKeyMap;
441 }
442 return keyMap.keyCharacterMap;
443 }
Chris Ye66fbac32020-07-06 20:36:43 -0700444
445 template <std::size_t N>
446 status_t readDeviceBitMask(unsigned long ioctlCode, BitArray<N>& bitArray) {
447 if (!hasValidFd()) {
448 return BAD_VALUE;
449 }
450 if ((_IOC_SIZE(ioctlCode) == 0)) {
451 ioctlCode |= _IOC(0, 0, 0, bitArray.bytes());
452 }
453
454 typename BitArray<N>::Buffer buffer;
455 status_t ret = ioctl(fd, ioctlCode, buffer.data());
456 bitArray.loadFromBuffer(buffer);
457 return ret;
458 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800459 };
460
Chris Ye8594e192020-07-14 10:34:06 -0700461 status_t openDeviceLocked(const std::string& devicePath);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800462 void openVideoDeviceLocked(const std::string& devicePath);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800463 void createVirtualKeyboardLocked();
464 void addDeviceLocked(Device* device);
465 void assignDescriptorLocked(InputDeviceIdentifier& identifier);
466
Chris Ye8594e192020-07-14 10:34:06 -0700467 void closeDeviceByPathLocked(const std::string& devicePath);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800468 void closeVideoDeviceByPathLocked(const std::string& devicePath);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800469 void closeDeviceLocked(Device* device);
470 void closeAllDevicesLocked();
471
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700472 void configureFd(Device* device);
473
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700474 bool isDeviceEnabled(int32_t deviceId) override;
475 status_t enableDevice(int32_t deviceId) override;
476 status_t disableDevice(int32_t deviceId) override;
Siarhei Vishniakou25920312018-12-12 15:24:44 -0800477 status_t registerFdForEpoll(int fd);
478 status_t unregisterFdFromEpoll(int fd);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700479 status_t registerDeviceForEpollLocked(Device* device);
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700480 void registerVideoDeviceForEpollLocked(const TouchVideoDevice& videoDevice);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700481 status_t unregisterDeviceFromEpollLocked(Device* device);
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700482 void unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& videoDevice);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700483
Chris Ye8594e192020-07-14 10:34:06 -0700484 status_t scanDirLocked(const std::string& dirname);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800485 status_t scanVideoDirLocked(const std::string& dirname);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800486 void scanDevicesLocked();
487 status_t readNotifyLocked();
488
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100489 Device* getDeviceByDescriptorLocked(const std::string& descriptor) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800490 Device* getDeviceLocked(int32_t deviceId) const;
Chris Ye8594e192020-07-14 10:34:06 -0700491 Device* getDeviceByPathLocked(const std::string& devicePath) const;
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700492 /**
493 * Look through all available fd's (both for input devices and for video devices),
494 * and return the device pointer.
495 */
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -0700496 Device* getDeviceByFdLocked(int fd) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800497
498 bool hasKeycodeLocked(Device* device, int keycode) const;
499
500 void loadConfigurationLocked(Device* device);
Siarhei Vishniakou3e78dec2019-02-20 16:21:46 -0600501 bool loadVirtualKeyMapLocked(Device* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800502 status_t loadKeyMapLocked(Device* device);
503
504 bool isExternalDeviceLocked(Device* device);
Tim Kilbourn063ff532015-04-08 10:26:18 -0700505 bool deviceHasMicLocked(Device* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800506
507 int32_t getNextControllerNumberLocked(Device* device);
508 void releaseControllerNumberLocked(Device* device);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700509 void setLedForControllerLocked(Device* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800510
511 status_t mapLed(Device* device, int32_t led, int32_t* outScanCode) const;
512 void setLedStateLocked(Device* device, int32_t led, bool on);
513
514 // Protect all internal state.
515 mutable Mutex mLock;
516
517 // The actual id of the built-in keyboard, or NO_BUILT_IN_KEYBOARD if none.
518 // EventHub remaps the built-in keyboard to id 0 externally as required by the API.
519 enum {
520 // Must not conflict with any other assigned device ids, including
521 // the virtual keyboard id (-1).
522 NO_BUILT_IN_KEYBOARD = -2,
523 };
524 int32_t mBuiltInKeyboardId;
525
526 int32_t mNextDeviceId;
527
528 BitSet32 mControllerNumbers;
529
530 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800531 /**
532 * Video devices that report touchscreen heatmap, but have not (yet) been paired
533 * with a specific input device. Video device discovery is independent from input device
534 * discovery, so the two types of devices could be found in any order.
535 * Ideally, video devices in this queue do not have an open fd, or at least aren't
536 * actively streaming.
537 */
538 std::vector<std::unique_ptr<TouchVideoDevice>> mUnattachedVideoDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800539
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700540 Device* mOpeningDevices;
541 Device* mClosingDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800542
543 bool mNeedToSendFinishedDeviceScan;
544 bool mNeedToReopenDevices;
545 bool mNeedToScanDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100546 std::vector<std::string> mExcludedDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800547
548 int mEpollFd;
549 int mINotifyFd;
550 int mWakeReadPipeFd;
551 int mWakeWritePipeFd;
552
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800553 int mInputWd;
554 int mVideoWd;
555
Michael Wrightd02c5b62014-02-10 15:10:22 -0800556 // Maximum number of signalled FDs to handle at a time.
557 static const int EPOLL_MAX_EVENTS = 16;
558
559 // The array of pending epoll events and the index of the next event to be handled.
560 struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS];
561 size_t mPendingEventCount;
562 size_t mPendingEventIndex;
563 bool mPendingINotify;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800564};
565
566}; // namespace android
567
568#endif // _RUNTIME_EVENT_HUB_H