blob: da0f497b9063a16b10e86bc1cf1541b0bf6462ec [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
17//
18#ifndef _RUNTIME_EVENT_HUB_H
19#define _RUNTIME_EVENT_HUB_H
20
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010021#include <vector>
22
Michael Wrightd02c5b62014-02-10 15:10:22 -080023#include <input/Input.h>
24#include <input/InputDevice.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080025#include <input/KeyCharacterMap.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070026#include <input/KeyLayoutMap.h>
27#include <input/Keyboard.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080028#include <input/VirtualKeyMap.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080029#include <utils/BitSet.h>
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070030#include <utils/Errors.h>
31#include <utils/KeyedVector.h>
32#include <utils/List.h>
33#include <utils/Log.h>
34#include <utils/Mutex.h>
35#include <utils/PropertyMap.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080036
37#include <linux/input.h>
38#include <sys/epoll.h>
39
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080040#include "TouchVideoDevice.h"
41
Michael Wrightd02c5b62014-02-10 15:10:22 -080042/* Convenience constants. */
43
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070044#define BTN_FIRST 0x100 // first button code
45#define BTN_LAST 0x15f // last button code
Michael Wrightd02c5b62014-02-10 15:10:22 -080046
Michael Wrightd02c5b62014-02-10 15:10:22 -080047namespace android {
48
Michael Wrightd02c5b62014-02-10 15:10:22 -080049/*
50 * A raw event as retrieved from the EventHub.
51 */
52struct RawEvent {
53 nsecs_t when;
54 int32_t deviceId;
55 int32_t type;
56 int32_t code;
57 int32_t value;
58};
59
60/* Describes an absolute axis. */
61struct RawAbsoluteAxisInfo {
62 bool valid; // true if the information is valid, false otherwise
63
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070064 int32_t minValue; // minimum value
65 int32_t maxValue; // maximum value
66 int32_t flat; // center flat position, eg. flat == 8 means center is between -8 and 8
67 int32_t fuzz; // error tolerance, eg. fuzz == 4 means value is +/- 4 due to noise
Michael Wrightd02c5b62014-02-10 15:10:22 -080068 int32_t resolution; // resolution in units per mm or radians per mm
69
70 inline void clear() {
71 valid = false;
72 minValue = 0;
73 maxValue = 0;
74 flat = 0;
75 fuzz = 0;
76 resolution = 0;
77 }
78};
79
80/*
81 * Input device classes.
82 */
83enum {
84 /* The input device is a keyboard or has buttons. */
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070085 INPUT_DEVICE_CLASS_KEYBOARD = 0x00000001,
Michael Wrightd02c5b62014-02-10 15:10:22 -080086
87 /* The input device is an alpha-numeric keyboard (not just a dial pad). */
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070088 INPUT_DEVICE_CLASS_ALPHAKEY = 0x00000002,
Michael Wrightd02c5b62014-02-10 15:10:22 -080089
90 /* The input device is a touchscreen or a touchpad (either single-touch or multi-touch). */
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070091 INPUT_DEVICE_CLASS_TOUCH = 0x00000004,
Michael Wrightd02c5b62014-02-10 15:10:22 -080092
93 /* The input device is a cursor device such as a trackball or mouse. */
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070094 INPUT_DEVICE_CLASS_CURSOR = 0x00000008,
Michael Wrightd02c5b62014-02-10 15:10:22 -080095
96 /* The input device is a multi-touch touchscreen. */
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070097 INPUT_DEVICE_CLASS_TOUCH_MT = 0x00000010,
Michael Wrightd02c5b62014-02-10 15:10:22 -080098
99 /* The input device is a directional pad (implies keyboard, has DPAD keys). */
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700100 INPUT_DEVICE_CLASS_DPAD = 0x00000020,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800101
102 /* The input device is a gamepad (implies keyboard, has BUTTON keys). */
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700103 INPUT_DEVICE_CLASS_GAMEPAD = 0x00000040,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800104
105 /* The input device has switches. */
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700106 INPUT_DEVICE_CLASS_SWITCH = 0x00000080,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800107
108 /* The input device is a joystick (implies gamepad, has joystick absolute axes). */
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700109 INPUT_DEVICE_CLASS_JOYSTICK = 0x00000100,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800110
111 /* The input device has a vibrator (supports FF_RUMBLE). */
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700112 INPUT_DEVICE_CLASS_VIBRATOR = 0x00000200,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800113
Tim Kilbourn063ff532015-04-08 10:26:18 -0700114 /* The input device has a microphone. */
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700115 INPUT_DEVICE_CLASS_MIC = 0x00000400,
Tim Kilbourn063ff532015-04-08 10:26:18 -0700116
Michael Wright842500e2015-03-13 17:32:02 -0700117 /* The input device is an external stylus (has data we want to fuse with touch data). */
118 INPUT_DEVICE_CLASS_EXTERNAL_STYLUS = 0x00000800,
119
Prashant Malani1941ff52015-08-11 18:29:28 -0700120 /* The input device has a rotary encoder */
121 INPUT_DEVICE_CLASS_ROTARY_ENCODER = 0x00001000,
122
Michael Wrightd02c5b62014-02-10 15:10:22 -0800123 /* The input device is virtual (not a real device, not part of UI configuration). */
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700124 INPUT_DEVICE_CLASS_VIRTUAL = 0x40000000,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800125
126 /* The input device is external (not built-in). */
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700127 INPUT_DEVICE_CLASS_EXTERNAL = 0x80000000,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800128};
129
130/*
131 * Gets the class that owns an axis, in cases where multiple classes might claim
132 * the same axis for different purposes.
133 */
134extern uint32_t getAbsAxisUsage(int32_t axis, uint32_t deviceClasses);
135
136/*
137 * Grand Central Station for events.
138 *
139 * The event hub aggregates input events received across all known input
140 * devices on the system, including devices that may be emulated by the simulator
141 * environment. In addition, the event hub generates fake input events to indicate
142 * when devices are added or removed.
143 *
144 * The event hub provides a stream of input events (via the getEvent function).
145 * It also supports querying the current actual state of input devices such as identifying
146 * which keys are currently down. Finally, the event hub keeps track of the capabilities of
147 * individual input devices, such as their class and the set of key codes that they support.
148 */
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700149class EventHubInterface {
150public:
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700151 EventHubInterface() {}
152 virtual ~EventHubInterface() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800153
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154 // Synthetic raw event type codes produced when devices are added or removed.
155 enum {
156 // Sent when a device is added.
157 DEVICE_ADDED = 0x10000000,
158 // Sent when a device is removed.
159 DEVICE_REMOVED = 0x20000000,
160 // Sent when all added/removed devices from the most recent scan have been reported.
161 // This event is always sent at least once.
162 FINISHED_DEVICE_SCAN = 0x30000000,
163
164 FIRST_SYNTHETIC_EVENT = DEVICE_ADDED,
165 };
166
167 virtual uint32_t getDeviceClasses(int32_t deviceId) const = 0;
168
169 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const = 0;
170
171 virtual int32_t getDeviceControllerNumber(int32_t deviceId) const = 0;
172
173 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const = 0;
174
175 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700176 RawAbsoluteAxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800177
178 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const = 0;
179
180 virtual bool hasInputProperty(int32_t deviceId, int property) const = 0;
181
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700182 virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
183 int32_t metaState, int32_t* outKeycode, int32_t* outMetaState,
184 uint32_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800185
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700186 virtual status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800187
188 // Sets devices that are excluded from opening.
189 // This can be used to ignore input devices for sensors.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100190 virtual void setExcludedDevices(const std::vector<std::string>& devices) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800191
192 /*
193 * Wait for events to become available and returns them.
194 * After returning, the EventHub holds onto a wake lock until the next call to getEvent.
195 * This ensures that the device will not go to sleep while the event is being processed.
196 * If the device needs to remain awake longer than that, then the caller is responsible
197 * for taking care of it (say, by poking the power manager user activity timer).
198 *
199 * The timeout is advisory only. If the device is asleep, it will not wake just to
200 * service the timeout.
201 *
202 * Returns the number of events obtained, or 0 if the timeout expired.
203 */
204 virtual size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) = 0;
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800205 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800206
207 /*
208 * Query current input state.
209 */
210 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0;
211 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0;
212 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0;
213 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700214 int32_t* outValue) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800215
216 /*
217 * Examine key input devices for specific framework keycode support
218 */
219 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700220 uint8_t* outFlags) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800221
222 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const = 0;
223
224 /* LED related functions expect Android LED constants, not scan codes or HID usages */
225 virtual bool hasLed(int32_t deviceId, int32_t led) const = 0;
226 virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0;
227
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700228 virtual void getVirtualKeyDefinitions(
229 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800230
231 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const = 0;
232 virtual bool setKeyboardLayoutOverlay(int32_t deviceId, const sp<KeyCharacterMap>& map) = 0;
233
234 /* Control the vibrator. */
235 virtual void vibrate(int32_t deviceId, nsecs_t duration) = 0;
236 virtual void cancelVibrate(int32_t deviceId) = 0;
237
238 /* Requests the EventHub to reopen all input devices on the next call to getEvents(). */
239 virtual void requestReopenDevices() = 0;
240
241 /* Wakes up getEvents() if it is blocked on a read. */
242 virtual void wake() = 0;
243
244 /* Dump EventHub state to a string. */
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800245 virtual void dump(std::string& dump) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800246
247 /* Called by the heatbeat to ensures that the reader has not deadlocked. */
248 virtual void monitor() = 0;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700249
250 /* Return true if the device is enabled. */
251 virtual bool isDeviceEnabled(int32_t deviceId) = 0;
252
253 /* Enable an input device */
254 virtual status_t enableDevice(int32_t deviceId) = 0;
255
256 /* Disable an input device. Closes file descriptor to that device. */
257 virtual status_t disableDevice(int32_t deviceId) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800258};
259
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700260class EventHub : public EventHubInterface {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800261public:
262 EventHub();
263
264 virtual uint32_t getDeviceClasses(int32_t deviceId) const;
265
266 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const;
267
268 virtual int32_t getDeviceControllerNumber(int32_t deviceId) const;
269
270 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const;
271
272 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700273 RawAbsoluteAxisInfo* outAxisInfo) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800274
275 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const;
276
277 virtual bool hasInputProperty(int32_t deviceId, int property) const;
278
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700279 virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
280 int32_t metaState, int32_t* outKeycode, int32_t* outMetaState,
281 uint32_t* outFlags) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800282
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700283 virtual status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800284
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100285 virtual void setExcludedDevices(const std::vector<std::string>& devices);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800286
287 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const;
288 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const;
289 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const;
290 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t* outValue) const;
291
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700292 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
293 uint8_t* outFlags) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800294
295 virtual size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize);
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800296 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800297
298 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const;
299 virtual bool hasLed(int32_t deviceId, int32_t led) const;
300 virtual void setLedState(int32_t deviceId, int32_t led, bool on);
301
302 virtual void getVirtualKeyDefinitions(int32_t deviceId,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700303 std::vector<VirtualKeyDefinition>& outVirtualKeys) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800304
305 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const;
306 virtual bool setKeyboardLayoutOverlay(int32_t deviceId, const sp<KeyCharacterMap>& map);
307
308 virtual void vibrate(int32_t deviceId, nsecs_t duration);
309 virtual void cancelVibrate(int32_t deviceId);
310
311 virtual void requestReopenDevices();
312
313 virtual void wake();
314
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800315 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800316 virtual void monitor();
317
Michael Wrightd02c5b62014-02-10 15:10:22 -0800318 virtual ~EventHub();
319
320private:
321 struct Device {
322 Device* next;
323
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700324 int fd; // may be -1 if device is closed
Michael Wrightd02c5b62014-02-10 15:10:22 -0800325 const int32_t id;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100326 const std::string path;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800327 const InputDeviceIdentifier identifier;
328
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -0800329 std::unique_ptr<TouchVideoDevice> videoDevice;
330
Michael Wrightd02c5b62014-02-10 15:10:22 -0800331 uint32_t classes;
332
333 uint8_t keyBitmask[(KEY_MAX + 1) / 8];
334 uint8_t absBitmask[(ABS_MAX + 1) / 8];
335 uint8_t relBitmask[(REL_MAX + 1) / 8];
336 uint8_t swBitmask[(SW_MAX + 1) / 8];
337 uint8_t ledBitmask[(LED_MAX + 1) / 8];
338 uint8_t ffBitmask[(FF_MAX + 1) / 8];
339 uint8_t propBitmask[(INPUT_PROP_MAX + 1) / 8];
340
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100341 std::string configurationFile;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800342 PropertyMap* configuration;
Siarhei Vishniakou3e78dec2019-02-20 16:21:46 -0600343 std::unique_ptr<VirtualKeyMap> virtualKeyMap;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344 KeyMap keyMap;
345
346 sp<KeyCharacterMap> overlayKeyMap;
347 sp<KeyCharacterMap> combinedKeyMap;
348
349 bool ffEffectPlaying;
350 int16_t ffEffectId; // initially -1
351
352 int32_t controllerNumber;
353
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100354 Device(int fd, int32_t id, const std::string& path,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700355 const InputDeviceIdentifier& identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800356 ~Device();
357
358 void close();
359
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700360 bool enabled; // initially true
361 status_t enable();
362 status_t disable();
363 bool hasValidFd();
364 const bool isVirtual; // set if fd < 0 is passed to constructor
Michael Wrightd02c5b62014-02-10 15:10:22 -0800365
366 const sp<KeyCharacterMap>& getKeyCharacterMap() const {
Yi Kong9b14ac62018-07-17 13:48:38 -0700367 if (combinedKeyMap != nullptr) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800368 return combinedKeyMap;
369 }
370 return keyMap.keyCharacterMap;
371 }
372 };
373
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700374 status_t openDeviceLocked(const char* devicePath);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800375 void openVideoDeviceLocked(const std::string& devicePath);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800376 void createVirtualKeyboardLocked();
377 void addDeviceLocked(Device* device);
378 void assignDescriptorLocked(InputDeviceIdentifier& identifier);
379
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700380 void closeDeviceByPathLocked(const char* devicePath);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800381 void closeVideoDeviceByPathLocked(const std::string& devicePath);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800382 void closeDeviceLocked(Device* device);
383 void closeAllDevicesLocked();
384
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700385 void configureFd(Device* device);
386
387 bool isDeviceEnabled(int32_t deviceId);
388 status_t enableDevice(int32_t deviceId);
389 status_t disableDevice(int32_t deviceId);
Siarhei Vishniakou25920312018-12-12 15:24:44 -0800390 status_t registerFdForEpoll(int fd);
391 status_t unregisterFdFromEpoll(int fd);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700392 status_t registerDeviceForEpollLocked(Device* device);
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700393 void registerVideoDeviceForEpollLocked(const TouchVideoDevice& videoDevice);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700394 status_t unregisterDeviceFromEpollLocked(Device* device);
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700395 void unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& videoDevice);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700396
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700397 status_t scanDirLocked(const char* dirname);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800398 status_t scanVideoDirLocked(const std::string& dirname);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800399 void scanDevicesLocked();
400 status_t readNotifyLocked();
401
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100402 Device* getDeviceByDescriptorLocked(const std::string& descriptor) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800403 Device* getDeviceLocked(int32_t deviceId) const;
404 Device* getDeviceByPathLocked(const char* devicePath) const;
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700405 /**
406 * Look through all available fd's (both for input devices and for video devices),
407 * and return the device pointer.
408 */
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -0700409 Device* getDeviceByFdLocked(int fd) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800410
411 bool hasKeycodeLocked(Device* device, int keycode) const;
412
413 void loadConfigurationLocked(Device* device);
Siarhei Vishniakou3e78dec2019-02-20 16:21:46 -0600414 bool loadVirtualKeyMapLocked(Device* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800415 status_t loadKeyMapLocked(Device* device);
416
417 bool isExternalDeviceLocked(Device* device);
Tim Kilbourn063ff532015-04-08 10:26:18 -0700418 bool deviceHasMicLocked(Device* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800419
420 int32_t getNextControllerNumberLocked(Device* device);
421 void releaseControllerNumberLocked(Device* device);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700422 void setLedForControllerLocked(Device* device);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800423
424 status_t mapLed(Device* device, int32_t led, int32_t* outScanCode) const;
425 void setLedStateLocked(Device* device, int32_t led, bool on);
426
427 // Protect all internal state.
428 mutable Mutex mLock;
429
430 // The actual id of the built-in keyboard, or NO_BUILT_IN_KEYBOARD if none.
431 // EventHub remaps the built-in keyboard to id 0 externally as required by the API.
432 enum {
433 // Must not conflict with any other assigned device ids, including
434 // the virtual keyboard id (-1).
435 NO_BUILT_IN_KEYBOARD = -2,
436 };
437 int32_t mBuiltInKeyboardId;
438
439 int32_t mNextDeviceId;
440
441 BitSet32 mControllerNumbers;
442
443 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800444 /**
445 * Video devices that report touchscreen heatmap, but have not (yet) been paired
446 * with a specific input device. Video device discovery is independent from input device
447 * discovery, so the two types of devices could be found in any order.
448 * Ideally, video devices in this queue do not have an open fd, or at least aren't
449 * actively streaming.
450 */
451 std::vector<std::unique_ptr<TouchVideoDevice>> mUnattachedVideoDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800452
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700453 Device* mOpeningDevices;
454 Device* mClosingDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800455
456 bool mNeedToSendFinishedDeviceScan;
457 bool mNeedToReopenDevices;
458 bool mNeedToScanDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100459 std::vector<std::string> mExcludedDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800460
461 int mEpollFd;
462 int mINotifyFd;
463 int mWakeReadPipeFd;
464 int mWakeWritePipeFd;
465
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800466 int mInputWd;
467 int mVideoWd;
468
Michael Wrightd02c5b62014-02-10 15:10:22 -0800469 // Maximum number of signalled FDs to handle at a time.
470 static const int EPOLL_MAX_EVENTS = 16;
471
472 // The array of pending epoll events and the index of the next event to be handled.
473 struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS];
474 size_t mPendingEventCount;
475 size_t mPendingEventIndex;
476 bool mPendingINotify;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800477};
478
479}; // namespace android
480
481#endif // _RUNTIME_EVENT_HUB_H