blob: baff6e3ec3e57828bdd09446e3f22a5cd2b640fd [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
Michael Wrightd02c5b62014-02-10 15:10:22 -080024#include <input/Input.h>
25#include <input/InputDevice.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080026#include <input/KeyCharacterMap.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070027#include <input/KeyLayoutMap.h>
28#include <input/Keyboard.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080029#include <input/VirtualKeyMap.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080030#include <utils/BitSet.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070031#include <utils/Errors.h>
32#include <utils/KeyedVector.h>
33#include <utils/List.h>
34#include <utils/Log.h>
35#include <utils/Mutex.h>
36#include <utils/PropertyMap.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080037
38#include <linux/input.h>
39#include <sys/epoll.h>
40
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080041#include "TouchVideoDevice.h"
42
Michael Wrightd02c5b62014-02-10 15:10:22 -080043namespace android {
44
Michael Wrightd02c5b62014-02-10 15:10:22 -080045/*
46 * A raw event as retrieved from the EventHub.
47 */
48struct RawEvent {
49 nsecs_t when;
50 int32_t deviceId;
51 int32_t type;
52 int32_t code;
53 int32_t value;
54};
55
56/* Describes an absolute axis. */
57struct RawAbsoluteAxisInfo {
58 bool valid; // true if the information is valid, false otherwise
59
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070060 int32_t minValue; // minimum value
61 int32_t maxValue; // maximum value
62 int32_t flat; // center flat position, eg. flat == 8 means center is between -8 and 8
63 int32_t fuzz; // error tolerance, eg. fuzz == 4 means value is +/- 4 due to noise
Michael Wrightd02c5b62014-02-10 15:10:22 -080064 int32_t resolution; // resolution in units per mm or radians per mm
65
66 inline void clear() {
67 valid = false;
68 minValue = 0;
69 maxValue = 0;
70 flat = 0;
71 fuzz = 0;
72 resolution = 0;
73 }
74};
75
76/*
77 * Input device classes.
78 */
79enum {
80 /* The input device is a keyboard or has buttons. */
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070081 INPUT_DEVICE_CLASS_KEYBOARD = 0x00000001,
Michael Wrightd02c5b62014-02-10 15:10:22 -080082
83 /* The input device is an alpha-numeric keyboard (not just a dial pad). */
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070084 INPUT_DEVICE_CLASS_ALPHAKEY = 0x00000002,
Michael Wrightd02c5b62014-02-10 15:10:22 -080085
86 /* The input device is a touchscreen or a touchpad (either single-touch or multi-touch). */
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070087 INPUT_DEVICE_CLASS_TOUCH = 0x00000004,
Michael Wrightd02c5b62014-02-10 15:10:22 -080088
89 /* The input device is a cursor device such as a trackball or mouse. */
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070090 INPUT_DEVICE_CLASS_CURSOR = 0x00000008,
Michael Wrightd02c5b62014-02-10 15:10:22 -080091
92 /* The input device is a multi-touch touchscreen. */
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070093 INPUT_DEVICE_CLASS_TOUCH_MT = 0x00000010,
Michael Wrightd02c5b62014-02-10 15:10:22 -080094
95 /* The input device is a directional pad (implies keyboard, has DPAD keys). */
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070096 INPUT_DEVICE_CLASS_DPAD = 0x00000020,
Michael Wrightd02c5b62014-02-10 15:10:22 -080097
98 /* The input device is a gamepad (implies keyboard, has BUTTON keys). */
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070099 INPUT_DEVICE_CLASS_GAMEPAD = 0x00000040,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800100
101 /* The input device has switches. */
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700102 INPUT_DEVICE_CLASS_SWITCH = 0x00000080,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800103
104 /* The input device is a joystick (implies gamepad, has joystick absolute axes). */
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700105 INPUT_DEVICE_CLASS_JOYSTICK = 0x00000100,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800106
107 /* The input device has a vibrator (supports FF_RUMBLE). */
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700108 INPUT_DEVICE_CLASS_VIBRATOR = 0x00000200,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800109
Tim Kilbourn063ff532015-04-08 10:26:18 -0700110 /* The input device has a microphone. */
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700111 INPUT_DEVICE_CLASS_MIC = 0x00000400,
Tim Kilbourn063ff532015-04-08 10:26:18 -0700112
Michael Wright842500e2015-03-13 17:32:02 -0700113 /* The input device is an external stylus (has data we want to fuse with touch data). */
114 INPUT_DEVICE_CLASS_EXTERNAL_STYLUS = 0x00000800,
115
Prashant Malani1941ff52015-08-11 18:29:28 -0700116 /* The input device has a rotary encoder */
117 INPUT_DEVICE_CLASS_ROTARY_ENCODER = 0x00001000,
118
Michael Wrightd02c5b62014-02-10 15:10:22 -0800119 /* The input device is virtual (not a real device, not part of UI configuration). */
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700120 INPUT_DEVICE_CLASS_VIRTUAL = 0x40000000,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800121
122 /* The input device is external (not built-in). */
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700123 INPUT_DEVICE_CLASS_EXTERNAL = 0x80000000,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800124};
125
126/*
127 * Gets the class that owns an axis, in cases where multiple classes might claim
128 * the same axis for different purposes.
129 */
130extern uint32_t getAbsAxisUsage(int32_t axis, uint32_t deviceClasses);
131
132/*
133 * Grand Central Station for events.
134 *
135 * The event hub aggregates input events received across all known input
136 * devices on the system, including devices that may be emulated by the simulator
137 * environment. In addition, the event hub generates fake input events to indicate
138 * when devices are added or removed.
139 *
140 * The event hub provides a stream of input events (via the getEvent function).
141 * It also supports querying the current actual state of input devices such as identifying
142 * which keys are currently down. Finally, the event hub keeps track of the capabilities of
143 * individual input devices, such as their class and the set of key codes that they support.
144 */
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700145class EventHubInterface {
146public:
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700147 EventHubInterface() {}
148 virtual ~EventHubInterface() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800149
Michael Wrightd02c5b62014-02-10 15:10:22 -0800150 // Synthetic raw event type codes produced when devices are added or removed.
151 enum {
152 // Sent when a device is added.
153 DEVICE_ADDED = 0x10000000,
154 // Sent when a device is removed.
155 DEVICE_REMOVED = 0x20000000,
156 // Sent when all added/removed devices from the most recent scan have been reported.
157 // This event is always sent at least once.
158 FINISHED_DEVICE_SCAN = 0x30000000,
159
160 FIRST_SYNTHETIC_EVENT = DEVICE_ADDED,
161 };
162
163 virtual uint32_t getDeviceClasses(int32_t deviceId) const = 0;
164
165 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const = 0;
166
167 virtual int32_t getDeviceControllerNumber(int32_t deviceId) const = 0;
168
169 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const = 0;
170
171 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700172 RawAbsoluteAxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800173
174 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const = 0;
175
176 virtual bool hasInputProperty(int32_t deviceId, int property) const = 0;
177
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700178 virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
179 int32_t metaState, int32_t* outKeycode, int32_t* outMetaState,
180 uint32_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800181
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700182 virtual status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800183
184 // Sets devices that are excluded from opening.
185 // This can be used to ignore input devices for sensors.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100186 virtual void setExcludedDevices(const std::vector<std::string>& devices) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800187
188 /*
189 * Wait for events to become available and returns them.
190 * After returning, the EventHub holds onto a wake lock until the next call to getEvent.
191 * This ensures that the device will not go to sleep while the event is being processed.
192 * If the device needs to remain awake longer than that, then the caller is responsible
193 * for taking care of it (say, by poking the power manager user activity timer).
194 *
195 * The timeout is advisory only. If the device is asleep, it will not wake just to
196 * service the timeout.
197 *
198 * Returns the number of events obtained, or 0 if the timeout expired.
199 */
200 virtual size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) = 0;
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800201 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800202
203 /*
204 * Query current input state.
205 */
206 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0;
207 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0;
208 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0;
209 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700210 int32_t* outValue) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800211
212 /*
213 * Examine key input devices for specific framework keycode support
214 */
215 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700216 uint8_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800217
218 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const = 0;
219
220 /* LED related functions expect Android LED constants, not scan codes or HID usages */
221 virtual bool hasLed(int32_t deviceId, int32_t led) const = 0;
222 virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0;
223
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700224 virtual void getVirtualKeyDefinitions(
225 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800226
227 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const = 0;
228 virtual bool setKeyboardLayoutOverlay(int32_t deviceId, const sp<KeyCharacterMap>& map) = 0;
229
230 /* Control the vibrator. */
231 virtual void vibrate(int32_t deviceId, nsecs_t duration) = 0;
232 virtual void cancelVibrate(int32_t deviceId) = 0;
233
234 /* Requests the EventHub to reopen all input devices on the next call to getEvents(). */
235 virtual void requestReopenDevices() = 0;
236
237 /* Wakes up getEvents() if it is blocked on a read. */
238 virtual void wake() = 0;
239
240 /* Dump EventHub state to a string. */
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800241 virtual void dump(std::string& dump) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800242
243 /* Called by the heatbeat to ensures that the reader has not deadlocked. */
244 virtual void monitor() = 0;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700245
246 /* Return true if the device is enabled. */
247 virtual bool isDeviceEnabled(int32_t deviceId) = 0;
248
249 /* Enable an input device */
250 virtual status_t enableDevice(int32_t deviceId) = 0;
251
252 /* Disable an input device. Closes file descriptor to that device. */
253 virtual status_t disableDevice(int32_t deviceId) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800254};
255
Chris Ye66fbac32020-07-06 20:36:43 -0700256template <std::size_t BITS>
257class BitArray {
258 /* Array element type and vector of element type. */
259 using Element = std::uint32_t;
260 /* Number of bits in each BitArray element. */
261 static constexpr size_t WIDTH = sizeof(Element) * CHAR_BIT;
262 /* Number of elements to represent a bit array of the specified size of bits. */
263 static constexpr size_t COUNT = (BITS + WIDTH - 1) / WIDTH;
264
265public:
266 /* BUFFER type declaration for BitArray */
267 using Buffer = std::array<Element, COUNT>;
268 /* To tell if a bit is set in array, it selects an element from the array, and test
269 * if the relevant bit set.
270 * Note the parameter "bit" is an index to the bit, 0 <= bit < BITS.
271 */
272 inline bool test(size_t bit) const {
273 return (bit < BITS) ? mData[bit / WIDTH].test(bit % WIDTH) : false;
274 }
275 /* Returns total number of bytes needed for the array */
276 inline size_t bytes() { return (BITS + CHAR_BIT - 1) / CHAR_BIT; }
277 /* Returns true if array contains any non-zero bit from the range defined by start and end
278 * bit index [startIndex, endIndex).
279 */
280 bool any(size_t startIndex, size_t endIndex) {
281 if (startIndex >= endIndex || startIndex > BITS || endIndex > BITS + 1) {
282 ALOGE("Invalid start/end index. start = %zu, end = %zu, total bits = %zu", startIndex,
283 endIndex, BITS);
284 return false;
285 }
286 size_t se = startIndex / WIDTH; // Start of element
287 size_t ee = endIndex / WIDTH; // End of element
288 size_t si = startIndex % WIDTH; // Start index in start element
289 size_t ei = endIndex % WIDTH; // End index in end element
290 // Need to check first unaligned bitset for any non zero bit
291 if (si > 0) {
292 size_t nBits = se == ee ? ei - si : WIDTH - si;
293 // Generate the mask of interested bit range
294 Element mask = ((1 << nBits) - 1) << si;
295 if (mData[se++].to_ulong() & mask) {
296 return true;
297 }
298 }
299 // Check whole bitset for any bit set
300 for (; se < ee; se++) {
301 if (mData[se].any()) {
302 return true;
303 }
304 }
305 // Need to check last unaligned bitset for any non zero bit
306 if (ei > 0 && se <= ee) {
307 // Generate the mask of interested bit range
308 Element mask = (1 << ei) - 1;
309 if (mData[se].to_ulong() & mask) {
310 return true;
311 }
312 }
313 return false;
314 }
315 /* Load bit array values from buffer */
316 void loadFromBuffer(const Buffer& buffer) {
317 for (size_t i = 0; i < COUNT; i++) {
318 mData[i] = std::bitset<WIDTH>(buffer[i]);
319 }
320 }
321
322private:
323 std::array<std::bitset<WIDTH>, COUNT> mData;
324};
325
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700326class EventHub : public EventHubInterface {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800327public:
328 EventHub();
329
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700330 virtual uint32_t getDeviceClasses(int32_t deviceId) const override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800331
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700332 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800333
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700334 virtual int32_t getDeviceControllerNumber(int32_t deviceId) const override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800335
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700336 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800337
338 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700339 RawAbsoluteAxisInfo* outAxisInfo) const override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800340
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700341 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800342
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700343 virtual bool hasInputProperty(int32_t deviceId, int property) const override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700345 virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
346 int32_t metaState, int32_t* outKeycode, int32_t* outMetaState,
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700347 uint32_t* outFlags) const override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800348
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700349 virtual status_t mapAxis(int32_t deviceId, int32_t scanCode,
350 AxisInfo* outAxisInfo) const override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700352 virtual void setExcludedDevices(const std::vector<std::string>& devices) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800353
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700354 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override;
355 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override;
356 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const override;
357 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
358 int32_t* outValue) const override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800359
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700360 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700361 uint8_t* outFlags) const override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800362
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700363 virtual size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) override;
364 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800365
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700366 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const override;
367 virtual bool hasLed(int32_t deviceId, int32_t led) const override;
368 virtual void setLedState(int32_t deviceId, int32_t led, bool on) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800369
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700370 virtual void getVirtualKeyDefinitions(
371 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800372
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700373 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const override;
374 virtual bool setKeyboardLayoutOverlay(int32_t deviceId,
375 const sp<KeyCharacterMap>& map) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800376
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700377 virtual void vibrate(int32_t deviceId, nsecs_t duration) override;
378 virtual void cancelVibrate(int32_t deviceId) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800379
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700380 virtual void requestReopenDevices() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800381
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700382 virtual void wake() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800383
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700384 virtual void dump(std::string& dump) override;
385 virtual void monitor() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800386
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700387 virtual ~EventHub() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800388
389private:
390 struct Device {
391 Device* next;
392
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700393 int fd; // may be -1 if device is closed
Michael Wrightd02c5b62014-02-10 15:10:22 -0800394 const int32_t id;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100395 const std::string path;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800396 const InputDeviceIdentifier identifier;
397
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -0800398 std::unique_ptr<TouchVideoDevice> videoDevice;
399
Michael Wrightd02c5b62014-02-10 15:10:22 -0800400 uint32_t classes;
401
Chris Ye66fbac32020-07-06 20:36:43 -0700402 BitArray<KEY_MAX> keyBitmask;
403 BitArray<KEY_MAX> keyState;
404 BitArray<ABS_MAX> absBitmask;
405 BitArray<REL_MAX> relBitmask;
406 BitArray<SW_MAX> swBitmask;
407 BitArray<SW_MAX> swState;
408 BitArray<LED_MAX> ledBitmask;
409 BitArray<FF_MAX> ffBitmask;
410 BitArray<INPUT_PROP_MAX> propBitmask;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800411
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100412 std::string configurationFile;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800413 PropertyMap* configuration;
Siarhei Vishniakou3e78dec2019-02-20 16:21:46 -0600414 std::unique_ptr<VirtualKeyMap> virtualKeyMap;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800415 KeyMap keyMap;
416
417 sp<KeyCharacterMap> overlayKeyMap;
418 sp<KeyCharacterMap> combinedKeyMap;
419
420 bool ffEffectPlaying;
421 int16_t ffEffectId; // initially -1
422
423 int32_t controllerNumber;
424
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100425 Device(int fd, int32_t id, const std::string& path,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700426 const InputDeviceIdentifier& identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800427 ~Device();
428
429 void close();
430
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700431 bool enabled; // initially true
432 status_t enable();
433 status_t disable();
434 bool hasValidFd();
435 const bool isVirtual; // set if fd < 0 is passed to constructor
Michael Wrightd02c5b62014-02-10 15:10:22 -0800436
437 const sp<KeyCharacterMap>& getKeyCharacterMap() const {
Yi Kong9b14ac62018-07-17 13:48:38 -0700438 if (combinedKeyMap != nullptr) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800439 return combinedKeyMap;
440 }
441 return keyMap.keyCharacterMap;
442 }
Chris Ye66fbac32020-07-06 20:36:43 -0700443
444 template <std::size_t N>
445 status_t readDeviceBitMask(unsigned long ioctlCode, BitArray<N>& bitArray) {
446 if (!hasValidFd()) {
447 return BAD_VALUE;
448 }
449 if ((_IOC_SIZE(ioctlCode) == 0)) {
450 ioctlCode |= _IOC(0, 0, 0, bitArray.bytes());
451 }
452
453 typename BitArray<N>::Buffer buffer;
454 status_t ret = ioctl(fd, ioctlCode, buffer.data());
455 bitArray.loadFromBuffer(buffer);
456 return ret;
457 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800458 };
459
Chris Ye8594e192020-07-14 10:34:06 -0700460 status_t openDeviceLocked(const std::string& devicePath);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800461 void openVideoDeviceLocked(const std::string& devicePath);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800462 void createVirtualKeyboardLocked();
463 void addDeviceLocked(Device* device);
464 void assignDescriptorLocked(InputDeviceIdentifier& identifier);
465
Chris Ye8594e192020-07-14 10:34:06 -0700466 void closeDeviceByPathLocked(const std::string& devicePath);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800467 void closeVideoDeviceByPathLocked(const std::string& devicePath);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800468 void closeDeviceLocked(Device* device);
469 void closeAllDevicesLocked();
470
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700471 void configureFd(Device* device);
472
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700473 bool isDeviceEnabled(int32_t deviceId) override;
474 status_t enableDevice(int32_t deviceId) override;
475 status_t disableDevice(int32_t deviceId) override;
Siarhei Vishniakou25920312018-12-12 15:24:44 -0800476 status_t registerFdForEpoll(int fd);
477 status_t unregisterFdFromEpoll(int fd);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700478 status_t registerDeviceForEpollLocked(Device* device);
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700479 void registerVideoDeviceForEpollLocked(const TouchVideoDevice& videoDevice);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700480 status_t unregisterDeviceFromEpollLocked(Device* device);
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700481 void unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& videoDevice);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700482
Chris Ye8594e192020-07-14 10:34:06 -0700483 status_t scanDirLocked(const std::string& dirname);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800484 status_t scanVideoDirLocked(const std::string& dirname);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800485 void scanDevicesLocked();
486 status_t readNotifyLocked();
487
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100488 Device* getDeviceByDescriptorLocked(const std::string& descriptor) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800489 Device* getDeviceLocked(int32_t deviceId) const;
Chris Ye8594e192020-07-14 10:34:06 -0700490 Device* getDeviceByPathLocked(const std::string& devicePath) const;
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700491 /**
492 * Look through all available fd's (both for input devices and for video devices),
493 * and return the device pointer.
494 */
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -0700495 Device* getDeviceByFdLocked(int fd) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800496
497 bool hasKeycodeLocked(Device* device, int keycode) const;
498
499 void loadConfigurationLocked(Device* device);
Siarhei Vishniakou3e78dec2019-02-20 16:21:46 -0600500 bool loadVirtualKeyMapLocked(Device* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800501 status_t loadKeyMapLocked(Device* device);
502
503 bool isExternalDeviceLocked(Device* device);
Tim Kilbourn063ff532015-04-08 10:26:18 -0700504 bool deviceHasMicLocked(Device* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800505
506 int32_t getNextControllerNumberLocked(Device* device);
507 void releaseControllerNumberLocked(Device* device);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700508 void setLedForControllerLocked(Device* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800509
510 status_t mapLed(Device* device, int32_t led, int32_t* outScanCode) const;
511 void setLedStateLocked(Device* device, int32_t led, bool on);
512
513 // Protect all internal state.
514 mutable Mutex mLock;
515
516 // The actual id of the built-in keyboard, or NO_BUILT_IN_KEYBOARD if none.
517 // EventHub remaps the built-in keyboard to id 0 externally as required by the API.
518 enum {
519 // Must not conflict with any other assigned device ids, including
520 // the virtual keyboard id (-1).
521 NO_BUILT_IN_KEYBOARD = -2,
522 };
523 int32_t mBuiltInKeyboardId;
524
525 int32_t mNextDeviceId;
526
527 BitSet32 mControllerNumbers;
528
529 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800530 /**
531 * Video devices that report touchscreen heatmap, but have not (yet) been paired
532 * with a specific input device. Video device discovery is independent from input device
533 * discovery, so the two types of devices could be found in any order.
534 * Ideally, video devices in this queue do not have an open fd, or at least aren't
535 * actively streaming.
536 */
537 std::vector<std::unique_ptr<TouchVideoDevice>> mUnattachedVideoDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800538
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700539 Device* mOpeningDevices;
540 Device* mClosingDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800541
542 bool mNeedToSendFinishedDeviceScan;
543 bool mNeedToReopenDevices;
544 bool mNeedToScanDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100545 std::vector<std::string> mExcludedDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800546
547 int mEpollFd;
548 int mINotifyFd;
549 int mWakeReadPipeFd;
550 int mWakeWritePipeFd;
551
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800552 int mInputWd;
553 int mVideoWd;
554
Michael Wrightd02c5b62014-02-10 15:10:22 -0800555 // Maximum number of signalled FDs to handle at a time.
556 static const int EPOLL_MAX_EVENTS = 16;
557
558 // The array of pending epoll events and the index of the next event to be handled.
559 struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS];
560 size_t mPendingEventCount;
561 size_t mPendingEventIndex;
562 bool mPendingINotify;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800563};
564
565}; // namespace android
566
567#endif // _RUNTIME_EVENT_HUB_H