blob: ff12d983347ce4ebf062a0ff419832b246d89a93 [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>
Chris Ye989bb932020-07-04 16:18:59 -070022#include <unordered_map>
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010023#include <vector>
24
Chris Ye1b0c7342020-07-28 21:57:03 -070025#include <input/Flags.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080026#include <input/Input.h>
27#include <input/InputDevice.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080028#include <input/KeyCharacterMap.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070029#include <input/KeyLayoutMap.h>
30#include <input/Keyboard.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080031#include <input/VirtualKeyMap.h>
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000032#include <linux/input.h>
33#include <sys/epoll.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080034#include <utils/BitSet.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070035#include <utils/Errors.h>
36#include <utils/KeyedVector.h>
37#include <utils/List.h>
38#include <utils/Log.h>
39#include <utils/Mutex.h>
40#include <utils/PropertyMap.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080041
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080042#include "TouchVideoDevice.h"
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000043#include "VibrationElement.h"
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080044
Michael Wrightd02c5b62014-02-10 15:10:22 -080045namespace android {
46
Michael Wrightd02c5b62014-02-10 15:10:22 -080047/*
48 * A raw event as retrieved from the EventHub.
49 */
50struct RawEvent {
51 nsecs_t when;
52 int32_t deviceId;
53 int32_t type;
54 int32_t code;
55 int32_t value;
56};
57
58/* Describes an absolute axis. */
59struct RawAbsoluteAxisInfo {
60 bool valid; // true if the information is valid, false otherwise
61
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070062 int32_t minValue; // minimum value
63 int32_t maxValue; // maximum value
64 int32_t flat; // center flat position, eg. flat == 8 means center is between -8 and 8
65 int32_t fuzz; // error tolerance, eg. fuzz == 4 means value is +/- 4 due to noise
Michael Wrightd02c5b62014-02-10 15:10:22 -080066 int32_t resolution; // resolution in units per mm or radians per mm
67
68 inline void clear() {
69 valid = false;
70 minValue = 0;
71 maxValue = 0;
72 flat = 0;
73 fuzz = 0;
74 resolution = 0;
75 }
76};
77
78/*
79 * Input device classes.
80 */
Chris Ye1b0c7342020-07-28 21:57:03 -070081enum class InputDeviceClass : uint32_t {
Michael Wrightd02c5b62014-02-10 15:10:22 -080082 /* The input device is a keyboard or has buttons. */
Chris Ye1b0c7342020-07-28 21:57:03 -070083 KEYBOARD = 0x00000001,
Michael Wrightd02c5b62014-02-10 15:10:22 -080084
85 /* The input device is an alpha-numeric keyboard (not just a dial pad). */
Chris Ye1b0c7342020-07-28 21:57:03 -070086 ALPHAKEY = 0x00000002,
Michael Wrightd02c5b62014-02-10 15:10:22 -080087
88 /* The input device is a touchscreen or a touchpad (either single-touch or multi-touch). */
Chris Ye1b0c7342020-07-28 21:57:03 -070089 TOUCH = 0x00000004,
Michael Wrightd02c5b62014-02-10 15:10:22 -080090
91 /* The input device is a cursor device such as a trackball or mouse. */
Chris Ye1b0c7342020-07-28 21:57:03 -070092 CURSOR = 0x00000008,
Michael Wrightd02c5b62014-02-10 15:10:22 -080093
94 /* The input device is a multi-touch touchscreen. */
Chris Ye1b0c7342020-07-28 21:57:03 -070095 TOUCH_MT = 0x00000010,
Michael Wrightd02c5b62014-02-10 15:10:22 -080096
97 /* The input device is a directional pad (implies keyboard, has DPAD keys). */
Chris Ye1b0c7342020-07-28 21:57:03 -070098 DPAD = 0x00000020,
Michael Wrightd02c5b62014-02-10 15:10:22 -080099
100 /* The input device is a gamepad (implies keyboard, has BUTTON keys). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700101 GAMEPAD = 0x00000040,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800102
103 /* The input device has switches. */
Chris Ye1b0c7342020-07-28 21:57:03 -0700104 SWITCH = 0x00000080,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800105
106 /* The input device is a joystick (implies gamepad, has joystick absolute axes). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700107 JOYSTICK = 0x00000100,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800108
109 /* The input device has a vibrator (supports FF_RUMBLE). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700110 VIBRATOR = 0x00000200,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800111
Tim Kilbourn063ff532015-04-08 10:26:18 -0700112 /* The input device has a microphone. */
Chris Ye1b0c7342020-07-28 21:57:03 -0700113 MIC = 0x00000400,
Tim Kilbourn063ff532015-04-08 10:26:18 -0700114
Michael Wright842500e2015-03-13 17:32:02 -0700115 /* The input device is an external stylus (has data we want to fuse with touch data). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700116 EXTERNAL_STYLUS = 0x00000800,
Michael Wright842500e2015-03-13 17:32:02 -0700117
Prashant Malani1941ff52015-08-11 18:29:28 -0700118 /* The input device has a rotary encoder */
Chris Ye1b0c7342020-07-28 21:57:03 -0700119 ROTARY_ENCODER = 0x00001000,
Prashant Malani1941ff52015-08-11 18:29:28 -0700120
Michael Wrightd02c5b62014-02-10 15:10:22 -0800121 /* The input device is virtual (not a real device, not part of UI configuration). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700122 VIRTUAL = 0x40000000,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800123
124 /* The input device is external (not built-in). */
Chris Ye1b0c7342020-07-28 21:57:03 -0700125 EXTERNAL = 0x80000000,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800126};
127
128/*
129 * Gets the class that owns an axis, in cases where multiple classes might claim
130 * the same axis for different purposes.
131 */
Chris Ye1b0c7342020-07-28 21:57:03 -0700132extern Flags<InputDeviceClass> getAbsAxisUsage(int32_t axis, Flags<InputDeviceClass> deviceClasses);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800133
134/*
135 * Grand Central Station for events.
136 *
137 * The event hub aggregates input events received across all known input
138 * devices on the system, including devices that may be emulated by the simulator
139 * environment. In addition, the event hub generates fake input events to indicate
140 * when devices are added or removed.
141 *
142 * The event hub provides a stream of input events (via the getEvent function).
143 * It also supports querying the current actual state of input devices such as identifying
144 * which keys are currently down. Finally, the event hub keeps track of the capabilities of
145 * individual input devices, such as their class and the set of key codes that they support.
146 */
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700147class EventHubInterface {
148public:
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700149 EventHubInterface() {}
150 virtual ~EventHubInterface() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800151
Michael Wrightd02c5b62014-02-10 15:10:22 -0800152 // Synthetic raw event type codes produced when devices are added or removed.
153 enum {
154 // Sent when a device is added.
155 DEVICE_ADDED = 0x10000000,
156 // Sent when a device is removed.
157 DEVICE_REMOVED = 0x20000000,
158 // Sent when all added/removed devices from the most recent scan have been reported.
159 // This event is always sent at least once.
160 FINISHED_DEVICE_SCAN = 0x30000000,
161
162 FIRST_SYNTHETIC_EVENT = DEVICE_ADDED,
163 };
164
Chris Ye1b0c7342020-07-28 21:57:03 -0700165 virtual Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800166
167 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const = 0;
168
169 virtual int32_t getDeviceControllerNumber(int32_t deviceId) const = 0;
170
171 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const = 0;
172
173 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700174 RawAbsoluteAxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800175
176 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const = 0;
177
178 virtual bool hasInputProperty(int32_t deviceId, int property) const = 0;
179
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700180 virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
181 int32_t metaState, int32_t* outKeycode, int32_t* outMetaState,
182 uint32_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800183
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700184 virtual status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800185
186 // Sets devices that are excluded from opening.
187 // This can be used to ignore input devices for sensors.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100188 virtual void setExcludedDevices(const std::vector<std::string>& devices) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800189
190 /*
191 * Wait for events to become available and returns them.
192 * After returning, the EventHub holds onto a wake lock until the next call to getEvent.
193 * This ensures that the device will not go to sleep while the event is being processed.
194 * If the device needs to remain awake longer than that, then the caller is responsible
195 * for taking care of it (say, by poking the power manager user activity timer).
196 *
197 * The timeout is advisory only. If the device is asleep, it will not wake just to
198 * service the timeout.
199 *
200 * Returns the number of events obtained, or 0 if the timeout expired.
201 */
202 virtual size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) = 0;
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800203 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800204
205 /*
206 * Query current input state.
207 */
208 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0;
209 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0;
210 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0;
211 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700212 int32_t* outValue) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800213
214 /*
215 * Examine key input devices for specific framework keycode support
216 */
217 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700218 uint8_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800219
220 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const = 0;
221
222 /* LED related functions expect Android LED constants, not scan codes or HID usages */
223 virtual bool hasLed(int32_t deviceId, int32_t led) const = 0;
224 virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0;
225
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700226 virtual void getVirtualKeyDefinitions(
227 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800228
229 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const = 0;
230 virtual bool setKeyboardLayoutOverlay(int32_t deviceId, const sp<KeyCharacterMap>& map) = 0;
231
232 /* Control the vibrator. */
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000233 virtual void vibrate(int32_t deviceId, const VibrationElement& effect) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800234 virtual void cancelVibrate(int32_t deviceId) = 0;
235
236 /* Requests the EventHub to reopen all input devices on the next call to getEvents(). */
237 virtual void requestReopenDevices() = 0;
238
239 /* Wakes up getEvents() if it is blocked on a read. */
240 virtual void wake() = 0;
241
242 /* Dump EventHub state to a string. */
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800243 virtual void dump(std::string& dump) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800244
245 /* Called by the heatbeat to ensures that the reader has not deadlocked. */
246 virtual void monitor() = 0;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700247
248 /* Return true if the device is enabled. */
249 virtual bool isDeviceEnabled(int32_t deviceId) = 0;
250
251 /* Enable an input device */
252 virtual status_t enableDevice(int32_t deviceId) = 0;
253
254 /* Disable an input device. Closes file descriptor to that device. */
255 virtual status_t disableDevice(int32_t deviceId) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800256};
257
Chris Ye66fbac32020-07-06 20:36:43 -0700258template <std::size_t BITS>
259class BitArray {
260 /* Array element type and vector of element type. */
261 using Element = std::uint32_t;
262 /* Number of bits in each BitArray element. */
263 static constexpr size_t WIDTH = sizeof(Element) * CHAR_BIT;
264 /* Number of elements to represent a bit array of the specified size of bits. */
265 static constexpr size_t COUNT = (BITS + WIDTH - 1) / WIDTH;
266
267public:
268 /* BUFFER type declaration for BitArray */
269 using Buffer = std::array<Element, COUNT>;
270 /* To tell if a bit is set in array, it selects an element from the array, and test
271 * if the relevant bit set.
272 * Note the parameter "bit" is an index to the bit, 0 <= bit < BITS.
273 */
274 inline bool test(size_t bit) const {
275 return (bit < BITS) ? mData[bit / WIDTH].test(bit % WIDTH) : false;
276 }
277 /* Returns total number of bytes needed for the array */
278 inline size_t bytes() { return (BITS + CHAR_BIT - 1) / CHAR_BIT; }
279 /* Returns true if array contains any non-zero bit from the range defined by start and end
280 * bit index [startIndex, endIndex).
281 */
282 bool any(size_t startIndex, size_t endIndex) {
283 if (startIndex >= endIndex || startIndex > BITS || endIndex > BITS + 1) {
284 ALOGE("Invalid start/end index. start = %zu, end = %zu, total bits = %zu", startIndex,
285 endIndex, BITS);
286 return false;
287 }
288 size_t se = startIndex / WIDTH; // Start of element
289 size_t ee = endIndex / WIDTH; // End of element
290 size_t si = startIndex % WIDTH; // Start index in start element
291 size_t ei = endIndex % WIDTH; // End index in end element
292 // Need to check first unaligned bitset for any non zero bit
293 if (si > 0) {
294 size_t nBits = se == ee ? ei - si : WIDTH - si;
295 // Generate the mask of interested bit range
296 Element mask = ((1 << nBits) - 1) << si;
297 if (mData[se++].to_ulong() & mask) {
298 return true;
299 }
300 }
301 // Check whole bitset for any bit set
302 for (; se < ee; se++) {
303 if (mData[se].any()) {
304 return true;
305 }
306 }
307 // Need to check last unaligned bitset for any non zero bit
308 if (ei > 0 && se <= ee) {
309 // Generate the mask of interested bit range
310 Element mask = (1 << ei) - 1;
311 if (mData[se].to_ulong() & mask) {
312 return true;
313 }
314 }
315 return false;
316 }
317 /* Load bit array values from buffer */
318 void loadFromBuffer(const Buffer& buffer) {
319 for (size_t i = 0; i < COUNT; i++) {
320 mData[i] = std::bitset<WIDTH>(buffer[i]);
321 }
322 }
323
324private:
325 std::array<std::bitset<WIDTH>, COUNT> mData;
326};
327
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700328class EventHub : public EventHubInterface {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800329public:
330 EventHub();
331
Chris Ye989bb932020-07-04 16:18:59 -0700332 Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800333
Chris Ye989bb932020-07-04 16:18:59 -0700334 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800335
Chris Ye989bb932020-07-04 16:18:59 -0700336 int32_t getDeviceControllerNumber(int32_t deviceId) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800337
Chris Ye989bb932020-07-04 16:18:59 -0700338 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800339
Chris Ye989bb932020-07-04 16:18:59 -0700340 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
341 RawAbsoluteAxisInfo* outAxisInfo) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800342
Chris Ye989bb932020-07-04 16:18:59 -0700343 bool hasRelativeAxis(int32_t deviceId, int axis) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344
Chris Ye989bb932020-07-04 16:18:59 -0700345 bool hasInputProperty(int32_t deviceId, int property) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800346
Chris Ye989bb932020-07-04 16:18:59 -0700347 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
348 int32_t* outKeycode, int32_t* outMetaState,
349 uint32_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800350
Chris Ye989bb932020-07-04 16:18:59 -0700351 status_t mapAxis(int32_t deviceId, int32_t scanCode,
352 AxisInfo* outAxisInfo) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800353
Chris Ye989bb932020-07-04 16:18:59 -0700354 void setExcludedDevices(const std::vector<std::string>& devices) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800355
Chris Ye989bb932020-07-04 16:18:59 -0700356 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override final;
357 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override final;
358 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override final;
359 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
360 int32_t* outValue) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800361
Chris Ye989bb932020-07-04 16:18:59 -0700362 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
363 uint8_t* outFlags) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800364
Chris Ye989bb932020-07-04 16:18:59 -0700365 size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) override final;
366 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800367
Chris Ye989bb932020-07-04 16:18:59 -0700368 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override final;
369 bool hasLed(int32_t deviceId, int32_t led) const override final;
370 void setLedState(int32_t deviceId, int32_t led, bool on) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800371
Chris Ye989bb932020-07-04 16:18:59 -0700372 void getVirtualKeyDefinitions(
373 int32_t deviceId,
374 std::vector<VirtualKeyDefinition>& outVirtualKeys) const override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800375
Chris Ye989bb932020-07-04 16:18:59 -0700376 sp<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const override final;
377 bool setKeyboardLayoutOverlay(int32_t deviceId, const sp<KeyCharacterMap>& map) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800378
Chris Ye989bb932020-07-04 16:18:59 -0700379 void vibrate(int32_t deviceId, const VibrationElement& effect) override final;
380 void cancelVibrate(int32_t deviceId) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800381
Chris Ye989bb932020-07-04 16:18:59 -0700382 void requestReopenDevices() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800383
Chris Ye989bb932020-07-04 16:18:59 -0700384 void wake() override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800385
Chris Ye989bb932020-07-04 16:18:59 -0700386 void dump(std::string& dump) override final;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800387
Chris Ye989bb932020-07-04 16:18:59 -0700388 void monitor() override final;
389
390 bool isDeviceEnabled(int32_t deviceId) override final;
391
392 status_t enableDevice(int32_t deviceId) override final;
393
394 status_t disableDevice(int32_t deviceId) override final;
395
396 ~EventHub() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800397
398private:
399 struct Device {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700400 int fd; // may be -1 if device is closed
Michael Wrightd02c5b62014-02-10 15:10:22 -0800401 const int32_t id;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100402 const std::string path;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800403 const InputDeviceIdentifier identifier;
404
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -0800405 std::unique_ptr<TouchVideoDevice> videoDevice;
406
Chris Ye1b0c7342020-07-28 21:57:03 -0700407 Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800408
Chris Ye66fbac32020-07-06 20:36:43 -0700409 BitArray<KEY_MAX> keyBitmask;
410 BitArray<KEY_MAX> keyState;
411 BitArray<ABS_MAX> absBitmask;
412 BitArray<REL_MAX> relBitmask;
413 BitArray<SW_MAX> swBitmask;
414 BitArray<SW_MAX> swState;
415 BitArray<LED_MAX> ledBitmask;
416 BitArray<FF_MAX> ffBitmask;
417 BitArray<INPUT_PROP_MAX> propBitmask;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800418
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100419 std::string configurationFile;
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500420 std::unique_ptr<PropertyMap> configuration;
Siarhei Vishniakou3e78dec2019-02-20 16:21:46 -0600421 std::unique_ptr<VirtualKeyMap> virtualKeyMap;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800422 KeyMap keyMap;
423
424 sp<KeyCharacterMap> overlayKeyMap;
425 sp<KeyCharacterMap> combinedKeyMap;
426
427 bool ffEffectPlaying;
428 int16_t ffEffectId; // initially -1
429
430 int32_t controllerNumber;
431
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100432 Device(int fd, int32_t id, const std::string& path,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700433 const InputDeviceIdentifier& identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800434 ~Device();
435
436 void close();
437
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700438 bool enabled; // initially true
439 status_t enable();
440 status_t disable();
Chris Ye989bb932020-07-04 16:18:59 -0700441 bool hasValidFd() const;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700442 const bool isVirtual; // set if fd < 0 is passed to constructor
Michael Wrightd02c5b62014-02-10 15:10:22 -0800443
Chris Ye989bb932020-07-04 16:18:59 -0700444 const sp<KeyCharacterMap>& getKeyCharacterMap() const;
Chris Ye66fbac32020-07-06 20:36:43 -0700445
446 template <std::size_t N>
Chris Ye989bb932020-07-04 16:18:59 -0700447 status_t readDeviceBitMask(unsigned long ioctlCode, BitArray<N>& bitArray);
Chris Ye66fbac32020-07-06 20:36:43 -0700448
Chris Ye989bb932020-07-04 16:18:59 -0700449 void configureFd();
450 bool hasKeycodeLocked(int keycode) const;
451 void loadConfigurationLocked();
452 bool loadVirtualKeyMapLocked();
453 status_t loadKeyMapLocked();
454 bool isExternalDeviceLocked();
455 bool deviceHasMicLocked();
456 void setLedForControllerLocked();
457 status_t mapLed(int32_t led, int32_t* outScanCode) const;
458 void setLedStateLocked(int32_t led, bool on);
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();
Chris Ye989bb932020-07-04 16:18:59 -0700464 void addDeviceLocked(std::unique_ptr<Device> device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800465 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);
Chris Ye989bb932020-07-04 16:18:59 -0700469 void closeDeviceLocked(Device& device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800470 void closeAllDevicesLocked();
471
Siarhei Vishniakou25920312018-12-12 15:24:44 -0800472 status_t registerFdForEpoll(int fd);
473 status_t unregisterFdFromEpoll(int fd);
Chris Ye989bb932020-07-04 16:18:59 -0700474 status_t registerDeviceForEpollLocked(Device& device);
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700475 void registerVideoDeviceForEpollLocked(const TouchVideoDevice& videoDevice);
Chris Ye989bb932020-07-04 16:18:59 -0700476 status_t unregisterDeviceFromEpollLocked(Device& device);
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700477 void unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& videoDevice);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700478
Chris Ye8594e192020-07-14 10:34:06 -0700479 status_t scanDirLocked(const std::string& dirname);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800480 status_t scanVideoDirLocked(const std::string& dirname);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800481 void scanDevicesLocked();
482 status_t readNotifyLocked();
483
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100484 Device* getDeviceByDescriptorLocked(const std::string& descriptor) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800485 Device* getDeviceLocked(int32_t deviceId) const;
Chris Ye8594e192020-07-14 10:34:06 -0700486 Device* getDeviceByPathLocked(const std::string& devicePath) const;
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700487 /**
488 * Look through all available fd's (both for input devices and for video devices),
489 * and return the device pointer.
490 */
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -0700491 Device* getDeviceByFdLocked(int fd) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800492
Chris Ye989bb932020-07-04 16:18:59 -0700493 int32_t getNextControllerNumberLocked(const std::string& name);
494 void releaseControllerNumberLocked(int32_t num);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800495
496 // Protect all internal state.
497 mutable Mutex mLock;
498
499 // The actual id of the built-in keyboard, or NO_BUILT_IN_KEYBOARD if none.
500 // EventHub remaps the built-in keyboard to id 0 externally as required by the API.
501 enum {
502 // Must not conflict with any other assigned device ids, including
503 // the virtual keyboard id (-1).
504 NO_BUILT_IN_KEYBOARD = -2,
505 };
506 int32_t mBuiltInKeyboardId;
507
508 int32_t mNextDeviceId;
509
510 BitSet32 mControllerNumbers;
511
Chris Ye989bb932020-07-04 16:18:59 -0700512 std::unordered_map<int32_t, std::unique_ptr<Device>> mDevices;
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800513 /**
514 * Video devices that report touchscreen heatmap, but have not (yet) been paired
515 * with a specific input device. Video device discovery is independent from input device
516 * discovery, so the two types of devices could be found in any order.
517 * Ideally, video devices in this queue do not have an open fd, or at least aren't
518 * actively streaming.
519 */
520 std::vector<std::unique_ptr<TouchVideoDevice>> mUnattachedVideoDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800521
Chris Ye989bb932020-07-04 16:18:59 -0700522 std::vector<std::unique_ptr<Device>> mOpeningDevices;
523 std::vector<std::unique_ptr<Device>> mClosingDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800524
525 bool mNeedToSendFinishedDeviceScan;
526 bool mNeedToReopenDevices;
527 bool mNeedToScanDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100528 std::vector<std::string> mExcludedDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800529
530 int mEpollFd;
531 int mINotifyFd;
532 int mWakeReadPipeFd;
533 int mWakeWritePipeFd;
534
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800535 int mInputWd;
536 int mVideoWd;
537
Michael Wrightd02c5b62014-02-10 15:10:22 -0800538 // Maximum number of signalled FDs to handle at a time.
539 static const int EPOLL_MAX_EVENTS = 16;
540
541 // The array of pending epoll events and the index of the next event to be handled.
542 struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS];
543 size_t mPendingEventCount;
544 size_t mPendingEventIndex;
545 bool mPendingINotify;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800546};
547
548}; // namespace android
549
550#endif // _RUNTIME_EVENT_HUB_H