Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
Prabir Pradhan | 4810866 | 2022-09-09 21:22:04 +0000 | [diff] [blame] | 17 | #pragma once |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 18 | |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 19 | #include <bitset> |
| 20 | #include <climits> |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 21 | #include <filesystem> |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 22 | #include <unordered_map> |
Prabir Pradhan | 5189478 | 2022-08-23 16:29:10 +0000 | [diff] [blame] | 23 | #include <utility> |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 24 | #include <vector> |
| 25 | |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 26 | #include <batteryservice/BatteryService.h> |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 27 | #include <ftl/flags.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 28 | #include <input/Input.h> |
| 29 | #include <input/InputDevice.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 30 | #include <input/KeyCharacterMap.h> |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 31 | #include <input/KeyLayoutMap.h> |
| 32 | #include <input/Keyboard.h> |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 33 | #include <input/PropertyMap.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 34 | #include <input/VirtualKeyMap.h> |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 35 | #include <linux/input.h> |
| 36 | #include <sys/epoll.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 37 | #include <utils/BitSet.h> |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 38 | #include <utils/Errors.h> |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 39 | #include <utils/List.h> |
| 40 | #include <utils/Log.h> |
| 41 | #include <utils/Mutex.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 42 | |
Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 43 | #include "TouchVideoDevice.h" |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 44 | #include "VibrationElement.h" |
Vaibhav Devmurari | dd82b8e | 2022-08-16 15:34:01 +0000 | [diff] [blame] | 45 | #include "android/hardware/input/InputDeviceCountryCode.h" |
Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 46 | |
Prabir Pradhan | 952e65b | 2022-06-23 17:49:55 +0000 | [diff] [blame] | 47 | struct inotify_event; |
| 48 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 49 | namespace android { |
| 50 | |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 51 | /* Number of colors : {red, green, blue} */ |
| 52 | static constexpr size_t COLOR_NUM = 3; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 53 | /* |
| 54 | * A raw event as retrieved from the EventHub. |
| 55 | */ |
| 56 | struct RawEvent { |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 57 | // Time when the event happened |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 58 | nsecs_t when; |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 59 | // Time when the event was read by EventHub. Only populated for input events. |
| 60 | // For other events (device added/removed/etc), this value is undefined and should not be read. |
| 61 | nsecs_t readTime; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 62 | int32_t deviceId; |
| 63 | int32_t type; |
| 64 | int32_t code; |
| 65 | int32_t value; |
| 66 | }; |
| 67 | |
| 68 | /* Describes an absolute axis. */ |
| 69 | struct RawAbsoluteAxisInfo { |
Prabir Pradhan | d6ccedb | 2022-09-27 21:04:06 +0000 | [diff] [blame] | 70 | bool valid{false}; // true if the information is valid, false otherwise |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 71 | |
Prabir Pradhan | d6ccedb | 2022-09-27 21:04:06 +0000 | [diff] [blame] | 72 | int32_t minValue{}; // minimum value |
| 73 | int32_t maxValue{}; // maximum value |
| 74 | int32_t flat{}; // center flat position, eg. flat == 8 means center is between -8 and 8 |
| 75 | int32_t fuzz{}; // error tolerance, eg. fuzz == 4 means value is +/- 4 due to noise |
| 76 | int32_t resolution{}; // resolution in units per mm or radians per mm |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 77 | |
Prabir Pradhan | d6ccedb | 2022-09-27 21:04:06 +0000 | [diff] [blame] | 78 | inline void clear() { *this = RawAbsoluteAxisInfo(); } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | /* |
| 82 | * Input device classes. |
| 83 | */ |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 84 | enum class InputDeviceClass : uint32_t { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 85 | /* The input device is a keyboard or has buttons. */ |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 86 | KEYBOARD = 0x00000001, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 87 | |
| 88 | /* The input device is an alpha-numeric keyboard (not just a dial pad). */ |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 89 | ALPHAKEY = 0x00000002, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 90 | |
| 91 | /* The input device is a touchscreen or a touchpad (either single-touch or multi-touch). */ |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 92 | TOUCH = 0x00000004, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 93 | |
| 94 | /* The input device is a cursor device such as a trackball or mouse. */ |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 95 | CURSOR = 0x00000008, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 96 | |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 97 | /* The input device is a multi-touch touchscreen or touchpad. */ |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 98 | TOUCH_MT = 0x00000010, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 99 | |
| 100 | /* The input device is a directional pad (implies keyboard, has DPAD keys). */ |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 101 | DPAD = 0x00000020, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 102 | |
| 103 | /* The input device is a gamepad (implies keyboard, has BUTTON keys). */ |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 104 | GAMEPAD = 0x00000040, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 105 | |
| 106 | /* The input device has switches. */ |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 107 | SWITCH = 0x00000080, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 108 | |
| 109 | /* The input device is a joystick (implies gamepad, has joystick absolute axes). */ |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 110 | JOYSTICK = 0x00000100, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 111 | |
| 112 | /* The input device has a vibrator (supports FF_RUMBLE). */ |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 113 | VIBRATOR = 0x00000200, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 114 | |
Tim Kilbourn | 063ff53 | 2015-04-08 10:26:18 -0700 | [diff] [blame] | 115 | /* The input device has a microphone. */ |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 116 | MIC = 0x00000400, |
Tim Kilbourn | 063ff53 | 2015-04-08 10:26:18 -0700 | [diff] [blame] | 117 | |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 118 | /* The input device is an external stylus (has data we want to fuse with touch data). */ |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 119 | EXTERNAL_STYLUS = 0x00000800, |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 120 | |
Prashant Malani | 1941ff5 | 2015-08-11 18:29:28 -0700 | [diff] [blame] | 121 | /* The input device has a rotary encoder */ |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 122 | ROTARY_ENCODER = 0x00001000, |
Prashant Malani | 1941ff5 | 2015-08-11 18:29:28 -0700 | [diff] [blame] | 123 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 124 | /* The input device has a sensor like accelerometer, gyro, etc */ |
| 125 | SENSOR = 0x00002000, |
| 126 | |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 127 | /* The input device has a battery */ |
| 128 | BATTERY = 0x00004000, |
| 129 | |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 130 | /* The input device has sysfs controllable lights */ |
| 131 | LIGHT = 0x00008000, |
| 132 | |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 133 | /* The input device is a touchpad, requiring an on-screen cursor. */ |
| 134 | TOUCHPAD = 0x00010000, |
| 135 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 136 | /* The input device is virtual (not a real device, not part of UI configuration). */ |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 137 | VIRTUAL = 0x40000000, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 138 | |
| 139 | /* The input device is external (not built-in). */ |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 140 | EXTERNAL = 0x80000000, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 141 | }; |
| 142 | |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 143 | enum class SysfsClass : uint32_t { |
| 144 | POWER_SUPPLY = 0, |
| 145 | LEDS = 1, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 146 | |
| 147 | ftl_last = LEDS |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | enum class LightColor : uint32_t { |
| 151 | RED = 0, |
| 152 | GREEN = 1, |
| 153 | BLUE = 2, |
| 154 | }; |
| 155 | |
| 156 | enum class InputLightClass : uint32_t { |
| 157 | /* The input light has brightness node. */ |
| 158 | BRIGHTNESS = 0x00000001, |
| 159 | /* The input light has red name. */ |
| 160 | RED = 0x00000002, |
| 161 | /* The input light has green name. */ |
| 162 | GREEN = 0x00000004, |
| 163 | /* The input light has blue name. */ |
| 164 | BLUE = 0x00000008, |
| 165 | /* The input light has global name. */ |
| 166 | GLOBAL = 0x00000010, |
| 167 | /* The input light has multi index node. */ |
| 168 | MULTI_INDEX = 0x00000020, |
| 169 | /* The input light has multi intensity node. */ |
| 170 | MULTI_INTENSITY = 0x00000040, |
| 171 | /* The input light has max brightness node. */ |
| 172 | MAX_BRIGHTNESS = 0x00000080, |
Vaibhav Devmurari | 82b37d6 | 2022-09-12 13:36:48 +0000 | [diff] [blame] | 173 | /* The input light has kbd_backlight name */ |
| 174 | KEYBOARD_BACKLIGHT = 0x00000100, |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 175 | }; |
| 176 | |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 177 | enum class InputBatteryClass : uint32_t { |
| 178 | /* The input device battery has capacity node. */ |
| 179 | CAPACITY = 0x00000001, |
| 180 | /* The input device battery has capacity_level node. */ |
| 181 | CAPACITY_LEVEL = 0x00000002, |
| 182 | /* The input device battery has status node. */ |
| 183 | STATUS = 0x00000004, |
| 184 | }; |
| 185 | |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 186 | /* Describes a raw light. */ |
| 187 | struct RawLightInfo { |
| 188 | int32_t id; |
| 189 | std::string name; |
| 190 | std::optional<int32_t> maxBrightness; |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 191 | ftl::Flags<InputLightClass> flags; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 192 | std::array<int32_t, COLOR_NUM> rgbIndex; |
| 193 | std::filesystem::path path; |
Prabir Pradhan | edeec3b | 2022-08-26 22:33:55 +0000 | [diff] [blame] | 194 | |
| 195 | bool operator==(const RawLightInfo&) const = default; |
| 196 | bool operator!=(const RawLightInfo&) const = default; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 197 | }; |
| 198 | |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 199 | /* Describes a raw battery. */ |
| 200 | struct RawBatteryInfo { |
| 201 | int32_t id; |
| 202 | std::string name; |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 203 | ftl::Flags<InputBatteryClass> flags; |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 204 | std::filesystem::path path; |
Prabir Pradhan | edeec3b | 2022-08-26 22:33:55 +0000 | [diff] [blame] | 205 | |
| 206 | bool operator==(const RawBatteryInfo&) const = default; |
| 207 | bool operator!=(const RawBatteryInfo&) const = default; |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 208 | }; |
| 209 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 210 | /* |
| 211 | * Gets the class that owns an axis, in cases where multiple classes might claim |
| 212 | * the same axis for different purposes. |
| 213 | */ |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 214 | extern ftl::Flags<InputDeviceClass> getAbsAxisUsage(int32_t axis, |
| 215 | ftl::Flags<InputDeviceClass> deviceClasses); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 216 | |
| 217 | /* |
| 218 | * Grand Central Station for events. |
| 219 | * |
| 220 | * The event hub aggregates input events received across all known input |
| 221 | * devices on the system, including devices that may be emulated by the simulator |
| 222 | * environment. In addition, the event hub generates fake input events to indicate |
| 223 | * when devices are added or removed. |
| 224 | * |
| 225 | * The event hub provides a stream of input events (via the getEvent function). |
| 226 | * It also supports querying the current actual state of input devices such as identifying |
| 227 | * which keys are currently down. Finally, the event hub keeps track of the capabilities of |
| 228 | * individual input devices, such as their class and the set of key codes that they support. |
| 229 | */ |
Siarhei Vishniakou | 3bc7e09 | 2019-07-24 17:43:30 -0700 | [diff] [blame] | 230 | class EventHubInterface { |
| 231 | public: |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 232 | EventHubInterface() {} |
| 233 | virtual ~EventHubInterface() {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 234 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 235 | // Synthetic raw event type codes produced when devices are added or removed. |
| 236 | enum { |
| 237 | // Sent when a device is added. |
| 238 | DEVICE_ADDED = 0x10000000, |
| 239 | // Sent when a device is removed. |
| 240 | DEVICE_REMOVED = 0x20000000, |
| 241 | // Sent when all added/removed devices from the most recent scan have been reported. |
| 242 | // This event is always sent at least once. |
| 243 | FINISHED_DEVICE_SCAN = 0x30000000, |
| 244 | |
| 245 | FIRST_SYNTHETIC_EVENT = DEVICE_ADDED, |
| 246 | }; |
| 247 | |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 248 | virtual ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 249 | |
| 250 | virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const = 0; |
| 251 | |
| 252 | virtual int32_t getDeviceControllerNumber(int32_t deviceId) const = 0; |
| 253 | |
| 254 | virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const = 0; |
| 255 | |
| 256 | virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis, |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 257 | RawAbsoluteAxisInfo* outAxisInfo) const = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 258 | |
| 259 | virtual bool hasRelativeAxis(int32_t deviceId, int axis) const = 0; |
| 260 | |
| 261 | virtual bool hasInputProperty(int32_t deviceId, int property) const = 0; |
| 262 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 263 | virtual bool hasMscEvent(int32_t deviceId, int mscEvent) const = 0; |
| 264 | |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 265 | virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, |
| 266 | int32_t metaState, int32_t* outKeycode, int32_t* outMetaState, |
| 267 | uint32_t* outFlags) const = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 268 | |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 269 | virtual status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 270 | |
| 271 | // Sets devices that are excluded from opening. |
| 272 | // This can be used to ignore input devices for sensors. |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 273 | virtual void setExcludedDevices(const std::vector<std::string>& devices) = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 274 | |
| 275 | /* |
| 276 | * Wait for events to become available and returns them. |
| 277 | * After returning, the EventHub holds onto a wake lock until the next call to getEvent. |
| 278 | * This ensures that the device will not go to sleep while the event is being processed. |
| 279 | * If the device needs to remain awake longer than that, then the caller is responsible |
| 280 | * for taking care of it (say, by poking the power manager user activity timer). |
| 281 | * |
| 282 | * The timeout is advisory only. If the device is asleep, it will not wake just to |
| 283 | * service the timeout. |
| 284 | * |
| 285 | * Returns the number of events obtained, or 0 if the timeout expired. |
| 286 | */ |
Siarhei Vishniakou | 7b3ea0b | 2022-09-16 14:23:20 -0700 | [diff] [blame] | 287 | virtual std::vector<RawEvent> getEvents(int timeoutMillis) = 0; |
Siarhei Vishniakou | add8929 | 2018-12-13 19:23:36 -0800 | [diff] [blame] | 288 | virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) = 0; |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 289 | virtual base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor( |
| 290 | int32_t deviceId, int32_t absCode) const = 0; |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 291 | // Raw batteries are sysfs power_supply nodes we found from the EventHub device sysfs node, |
| 292 | // containing the raw info of the sysfs node structure. |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 293 | virtual std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const = 0; |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 294 | virtual std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 295 | int32_t BatteryId) const = 0; |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 296 | |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 297 | // Raw lights are sysfs led light nodes we found from the EventHub device sysfs node, |
| 298 | // containing the raw info of the sysfs node structure. |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 299 | virtual std::vector<int32_t> getRawLightIds(int32_t deviceId) const = 0; |
| 300 | virtual std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, |
| 301 | int32_t lightId) const = 0; |
| 302 | virtual std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const = 0; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 303 | virtual void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) = 0; |
| 304 | virtual std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities( |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 305 | int32_t deviceId, int32_t lightId) const = 0; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 306 | virtual void setLightIntensities(int32_t deviceId, int32_t lightId, |
| 307 | std::unordered_map<LightColor, int32_t> intensities) = 0; |
Vaibhav Devmurari | dd82b8e | 2022-08-16 15:34:01 +0000 | [diff] [blame] | 308 | /* Query Country code associated with the input device. */ |
| 309 | virtual hardware::input::InputDeviceCountryCode getCountryCode(int32_t deviceId) const = 0; |
| 310 | /* Query current input state. */ |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 311 | virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0; |
| 312 | virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0; |
| 313 | virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0; |
| 314 | virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis, |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 315 | int32_t* outValue) const = 0; |
Philip Junker | 4af3b3d | 2021-12-14 10:36:55 +0100 | [diff] [blame] | 316 | virtual int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 317 | |
| 318 | /* |
| 319 | * Examine key input devices for specific framework keycode support |
| 320 | */ |
Siarhei Vishniakou | 7400794 | 2022-06-13 13:57:47 -0700 | [diff] [blame] | 321 | virtual bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes, |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 322 | uint8_t* outFlags) const = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 323 | |
| 324 | virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const = 0; |
Arthur Hung | cb40a00 | 2021-08-03 14:31:01 +0000 | [diff] [blame] | 325 | virtual bool hasKeyCode(int32_t deviceId, int32_t keyCode) const = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 326 | |
| 327 | /* LED related functions expect Android LED constants, not scan codes or HID usages */ |
| 328 | virtual bool hasLed(int32_t deviceId, int32_t led) const = 0; |
| 329 | virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0; |
| 330 | |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 331 | virtual void getVirtualKeyDefinitions( |
| 332 | int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 333 | |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 334 | virtual const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const = 0; |
| 335 | virtual bool setKeyboardLayoutOverlay(int32_t deviceId, |
| 336 | std::shared_ptr<KeyCharacterMap> map) = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 337 | |
| 338 | /* Control the vibrator. */ |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 339 | virtual void vibrate(int32_t deviceId, const VibrationElement& effect) = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 340 | virtual void cancelVibrate(int32_t deviceId) = 0; |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 341 | virtual std::vector<int32_t> getVibratorIds(int32_t deviceId) const = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 342 | |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 343 | /* Query battery level. */ |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 344 | virtual std::optional<int32_t> getBatteryCapacity(int32_t deviceId, |
| 345 | int32_t batteryId) const = 0; |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 346 | |
| 347 | /* Query battery status. */ |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 348 | virtual std::optional<int32_t> getBatteryStatus(int32_t deviceId, int32_t batteryId) const = 0; |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 349 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 350 | /* Requests the EventHub to reopen all input devices on the next call to getEvents(). */ |
| 351 | virtual void requestReopenDevices() = 0; |
| 352 | |
| 353 | /* Wakes up getEvents() if it is blocked on a read. */ |
| 354 | virtual void wake() = 0; |
| 355 | |
| 356 | /* Dump EventHub state to a string. */ |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 357 | virtual void dump(std::string& dump) const = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 358 | |
| 359 | /* Called by the heatbeat to ensures that the reader has not deadlocked. */ |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 360 | virtual void monitor() const = 0; |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 361 | |
| 362 | /* Return true if the device is enabled. */ |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 363 | virtual bool isDeviceEnabled(int32_t deviceId) const = 0; |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 364 | |
| 365 | /* Enable an input device */ |
| 366 | virtual status_t enableDevice(int32_t deviceId) = 0; |
| 367 | |
| 368 | /* Disable an input device. Closes file descriptor to that device. */ |
| 369 | virtual status_t disableDevice(int32_t deviceId) = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 370 | }; |
| 371 | |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 372 | template <std::size_t BITS> |
| 373 | class BitArray { |
| 374 | /* Array element type and vector of element type. */ |
| 375 | using Element = std::uint32_t; |
| 376 | /* Number of bits in each BitArray element. */ |
| 377 | static constexpr size_t WIDTH = sizeof(Element) * CHAR_BIT; |
| 378 | /* Number of elements to represent a bit array of the specified size of bits. */ |
| 379 | static constexpr size_t COUNT = (BITS + WIDTH - 1) / WIDTH; |
| 380 | |
| 381 | public: |
| 382 | /* BUFFER type declaration for BitArray */ |
| 383 | using Buffer = std::array<Element, COUNT>; |
| 384 | /* To tell if a bit is set in array, it selects an element from the array, and test |
| 385 | * if the relevant bit set. |
| 386 | * Note the parameter "bit" is an index to the bit, 0 <= bit < BITS. |
| 387 | */ |
| 388 | inline bool test(size_t bit) const { |
| 389 | return (bit < BITS) ? mData[bit / WIDTH].test(bit % WIDTH) : false; |
| 390 | } |
| 391 | /* Returns total number of bytes needed for the array */ |
| 392 | inline size_t bytes() { return (BITS + CHAR_BIT - 1) / CHAR_BIT; } |
| 393 | /* Returns true if array contains any non-zero bit from the range defined by start and end |
| 394 | * bit index [startIndex, endIndex). |
| 395 | */ |
| 396 | bool any(size_t startIndex, size_t endIndex) { |
| 397 | if (startIndex >= endIndex || startIndex > BITS || endIndex > BITS + 1) { |
| 398 | ALOGE("Invalid start/end index. start = %zu, end = %zu, total bits = %zu", startIndex, |
| 399 | endIndex, BITS); |
| 400 | return false; |
| 401 | } |
| 402 | size_t se = startIndex / WIDTH; // Start of element |
| 403 | size_t ee = endIndex / WIDTH; // End of element |
| 404 | size_t si = startIndex % WIDTH; // Start index in start element |
| 405 | size_t ei = endIndex % WIDTH; // End index in end element |
| 406 | // Need to check first unaligned bitset for any non zero bit |
| 407 | if (si > 0) { |
| 408 | size_t nBits = se == ee ? ei - si : WIDTH - si; |
| 409 | // Generate the mask of interested bit range |
| 410 | Element mask = ((1 << nBits) - 1) << si; |
| 411 | if (mData[se++].to_ulong() & mask) { |
| 412 | return true; |
| 413 | } |
| 414 | } |
| 415 | // Check whole bitset for any bit set |
| 416 | for (; se < ee; se++) { |
| 417 | if (mData[se].any()) { |
| 418 | return true; |
| 419 | } |
| 420 | } |
| 421 | // Need to check last unaligned bitset for any non zero bit |
| 422 | if (ei > 0 && se <= ee) { |
| 423 | // Generate the mask of interested bit range |
| 424 | Element mask = (1 << ei) - 1; |
| 425 | if (mData[se].to_ulong() & mask) { |
| 426 | return true; |
| 427 | } |
| 428 | } |
| 429 | return false; |
| 430 | } |
| 431 | /* Load bit array values from buffer */ |
| 432 | void loadFromBuffer(const Buffer& buffer) { |
| 433 | for (size_t i = 0; i < COUNT; i++) { |
| 434 | mData[i] = std::bitset<WIDTH>(buffer[i]); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | private: |
| 439 | std::array<std::bitset<WIDTH>, COUNT> mData; |
| 440 | }; |
| 441 | |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 442 | class EventHub : public EventHubInterface { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 443 | public: |
| 444 | EventHub(); |
| 445 | |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 446 | ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 447 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 448 | InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 449 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 450 | int32_t getDeviceControllerNumber(int32_t deviceId) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 451 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 452 | void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 453 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 454 | status_t getAbsoluteAxisInfo(int32_t deviceId, int axis, |
| 455 | RawAbsoluteAxisInfo* outAxisInfo) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 456 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 457 | bool hasRelativeAxis(int32_t deviceId, int axis) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 458 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 459 | bool hasInputProperty(int32_t deviceId, int property) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 460 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 461 | bool hasMscEvent(int32_t deviceId, int mscEvent) const override final; |
| 462 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 463 | status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState, |
| 464 | int32_t* outKeycode, int32_t* outMetaState, |
| 465 | uint32_t* outFlags) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 466 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 467 | status_t mapAxis(int32_t deviceId, int32_t scanCode, |
| 468 | AxisInfo* outAxisInfo) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 469 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 470 | base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor( |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 471 | int32_t deviceId, int32_t absCode) const override final; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 472 | |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 473 | std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override final; |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 474 | std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 475 | int32_t BatteryId) const override final; |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 476 | |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 477 | std::vector<int32_t> getRawLightIds(int32_t deviceId) const override final; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 478 | |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 479 | std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, |
| 480 | int32_t lightId) const override final; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 481 | |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 482 | std::optional<int32_t> getLightBrightness(int32_t deviceId, |
| 483 | int32_t lightId) const override final; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 484 | void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override final; |
| 485 | std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities( |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 486 | int32_t deviceId, int32_t lightId) const override final; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 487 | void setLightIntensities(int32_t deviceId, int32_t lightId, |
| 488 | std::unordered_map<LightColor, int32_t> intensities) override final; |
| 489 | |
Vaibhav Devmurari | dd82b8e | 2022-08-16 15:34:01 +0000 | [diff] [blame] | 490 | hardware::input::InputDeviceCountryCode getCountryCode(int32_t deviceId) const override final; |
| 491 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 492 | void setExcludedDevices(const std::vector<std::string>& devices) override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 493 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 494 | int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override final; |
| 495 | int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override final; |
| 496 | int32_t getSwitchState(int32_t deviceId, int32_t sw) const override final; |
Philip Junker | 4af3b3d | 2021-12-14 10:36:55 +0100 | [diff] [blame] | 497 | int32_t getKeyCodeForKeyLocation(int32_t deviceId, |
| 498 | int32_t locationKeyCode) const override final; |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 499 | status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis, |
| 500 | int32_t* outValue) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 501 | |
Siarhei Vishniakou | 7400794 | 2022-06-13 13:57:47 -0700 | [diff] [blame] | 502 | bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes, |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 503 | uint8_t* outFlags) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 504 | |
Siarhei Vishniakou | 7b3ea0b | 2022-09-16 14:23:20 -0700 | [diff] [blame] | 505 | std::vector<RawEvent> getEvents(int timeoutMillis) override final; |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 506 | std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 507 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 508 | bool hasScanCode(int32_t deviceId, int32_t scanCode) const override final; |
Arthur Hung | cb40a00 | 2021-08-03 14:31:01 +0000 | [diff] [blame] | 509 | bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override final; |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 510 | bool hasLed(int32_t deviceId, int32_t led) const override final; |
| 511 | void setLedState(int32_t deviceId, int32_t led, bool on) override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 512 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 513 | void getVirtualKeyDefinitions( |
| 514 | int32_t deviceId, |
| 515 | std::vector<VirtualKeyDefinition>& outVirtualKeys) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 516 | |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 517 | const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap( |
| 518 | int32_t deviceId) const override final; |
| 519 | bool setKeyboardLayoutOverlay(int32_t deviceId, |
| 520 | std::shared_ptr<KeyCharacterMap> map) override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 521 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 522 | void vibrate(int32_t deviceId, const VibrationElement& effect) override final; |
| 523 | void cancelVibrate(int32_t deviceId) override final; |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 524 | std::vector<int32_t> getVibratorIds(int32_t deviceId) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 525 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 526 | void requestReopenDevices() override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 527 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 528 | void wake() override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 529 | |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 530 | void dump(std::string& dump) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 531 | |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 532 | void monitor() const override final; |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 533 | |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 534 | std::optional<int32_t> getBatteryCapacity(int32_t deviceId, |
| 535 | int32_t batteryId) const override final; |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 536 | |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 537 | std::optional<int32_t> getBatteryStatus(int32_t deviceId, |
| 538 | int32_t batteryId) const override final; |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 539 | |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 540 | bool isDeviceEnabled(int32_t deviceId) const override final; |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 541 | |
| 542 | status_t enableDevice(int32_t deviceId) override final; |
| 543 | |
| 544 | status_t disableDevice(int32_t deviceId) override final; |
| 545 | |
| 546 | ~EventHub() override; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 547 | |
| 548 | private: |
Prabir Pradhan | cb42b47 | 2022-08-23 16:01:19 +0000 | [diff] [blame] | 549 | // Holds information about the sysfs device associated with the Device. |
Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 550 | struct AssociatedDevice { |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 551 | // The sysfs root path of the misc device. |
| 552 | std::filesystem::path sysfsRootPath; |
Vaibhav Devmurari | dd82b8e | 2022-08-16 15:34:01 +0000 | [diff] [blame] | 553 | hardware::input::InputDeviceCountryCode countryCode; |
Prabir Pradhan | cb42b47 | 2022-08-23 16:01:19 +0000 | [diff] [blame] | 554 | std::unordered_map<int32_t /*batteryId*/, RawBatteryInfo> batteryInfos; |
| 555 | std::unordered_map<int32_t /*lightId*/, RawLightInfo> lightInfos; |
Prabir Pradhan | edeec3b | 2022-08-26 22:33:55 +0000 | [diff] [blame] | 556 | |
| 557 | bool operator==(const AssociatedDevice&) const = default; |
| 558 | bool operator!=(const AssociatedDevice&) const = default; |
| 559 | std::string dump() const; |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 560 | }; |
| 561 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 562 | struct Device { |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 563 | int fd; // may be -1 if device is closed |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 564 | const int32_t id; |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 565 | const std::string path; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 566 | const InputDeviceIdentifier identifier; |
| 567 | |
Siarhei Vishniakou | ec7854a | 2018-12-14 16:52:34 -0800 | [diff] [blame] | 568 | std::unique_ptr<TouchVideoDevice> videoDevice; |
| 569 | |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 570 | ftl::Flags<InputDeviceClass> classes; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 571 | |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 572 | BitArray<KEY_MAX> keyBitmask; |
| 573 | BitArray<KEY_MAX> keyState; |
| 574 | BitArray<ABS_MAX> absBitmask; |
| 575 | BitArray<REL_MAX> relBitmask; |
| 576 | BitArray<SW_MAX> swBitmask; |
| 577 | BitArray<SW_MAX> swState; |
| 578 | BitArray<LED_MAX> ledBitmask; |
| 579 | BitArray<FF_MAX> ffBitmask; |
| 580 | BitArray<INPUT_PROP_MAX> propBitmask; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 581 | BitArray<MSC_MAX> mscBitmask; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 582 | |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 583 | std::string configurationFile; |
Siarhei Vishniakou | d549b25 | 2020-08-11 11:25:26 -0500 | [diff] [blame] | 584 | std::unique_ptr<PropertyMap> configuration; |
Siarhei Vishniakou | 3e78dec | 2019-02-20 16:21:46 -0600 | [diff] [blame] | 585 | std::unique_ptr<VirtualKeyMap> virtualKeyMap; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 586 | KeyMap keyMap; |
| 587 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 588 | bool ffEffectPlaying; |
| 589 | int16_t ffEffectId; // initially -1 |
| 590 | |
Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 591 | // A shared_ptr of a device associated with the input device. |
Prabir Pradhan | 5189478 | 2022-08-23 16:29:10 +0000 | [diff] [blame] | 592 | // The input devices that have the same sysfs path have the same associated device. |
Prabir Pradhan | edeec3b | 2022-08-26 22:33:55 +0000 | [diff] [blame] | 593 | std::shared_ptr<const AssociatedDevice> associatedDevice; |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 594 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 595 | int32_t controllerNumber; |
| 596 | |
Prabir Pradhan | cb42b47 | 2022-08-23 16:01:19 +0000 | [diff] [blame] | 597 | Device(int fd, int32_t id, std::string path, InputDeviceIdentifier identifier, |
| 598 | std::shared_ptr<const AssociatedDevice> assocDev); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 599 | ~Device(); |
| 600 | |
| 601 | void close(); |
| 602 | |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 603 | bool enabled; // initially true |
| 604 | status_t enable(); |
| 605 | status_t disable(); |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 606 | bool hasValidFd() const; |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 607 | const bool isVirtual; // set if fd < 0 is passed to constructor |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 608 | |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 609 | const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const; |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 610 | |
| 611 | template <std::size_t N> |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 612 | status_t readDeviceBitMask(unsigned long ioctlCode, BitArray<N>& bitArray); |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 613 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 614 | void configureFd(); |
| 615 | bool hasKeycodeLocked(int keycode) const; |
| 616 | void loadConfigurationLocked(); |
| 617 | bool loadVirtualKeyMapLocked(); |
| 618 | status_t loadKeyMapLocked(); |
| 619 | bool isExternalDeviceLocked(); |
| 620 | bool deviceHasMicLocked(); |
| 621 | void setLedForControllerLocked(); |
| 622 | status_t mapLed(int32_t led, int32_t* outScanCode) const; |
| 623 | void setLedStateLocked(int32_t led, bool on); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 624 | }; |
| 625 | |
Siarhei Vishniakou | a4c502a | 2021-02-05 00:45:20 +0000 | [diff] [blame] | 626 | /** |
| 627 | * Create a new device for the provided path. |
| 628 | */ |
Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 629 | void openDeviceLocked(const std::string& devicePath) REQUIRES(mLock); |
| 630 | void openVideoDeviceLocked(const std::string& devicePath) REQUIRES(mLock); |
Siarhei Vishniakou | f49608d | 2020-08-20 19:18:21 -0500 | [diff] [blame] | 631 | /** |
| 632 | * Try to associate a video device with an input device. If the association succeeds, |
| 633 | * the videoDevice is moved into the input device. 'videoDevice' will become null if this |
| 634 | * happens. |
| 635 | * Return true if the association succeeds. |
| 636 | * Return false otherwise. |
| 637 | */ |
Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 638 | bool tryAddVideoDeviceLocked(Device& device, std::unique_ptr<TouchVideoDevice>& videoDevice) |
| 639 | REQUIRES(mLock); |
| 640 | void createVirtualKeyboardLocked() REQUIRES(mLock); |
| 641 | void addDeviceLocked(std::unique_ptr<Device> device) REQUIRES(mLock); |
| 642 | void assignDescriptorLocked(InputDeviceIdentifier& identifier) REQUIRES(mLock); |
Prabir Pradhan | cb42b47 | 2022-08-23 16:01:19 +0000 | [diff] [blame] | 643 | std::shared_ptr<const AssociatedDevice> obtainAssociatedDeviceLocked( |
| 644 | const std::filesystem::path& devicePath) const REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 645 | |
Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 646 | void closeDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock); |
| 647 | void closeVideoDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock); |
| 648 | void closeDeviceLocked(Device& device) REQUIRES(mLock); |
| 649 | void closeAllDevicesLocked() REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 650 | |
Siarhei Vishniakou | 2592031 | 2018-12-12 15:24:44 -0800 | [diff] [blame] | 651 | status_t registerFdForEpoll(int fd); |
| 652 | status_t unregisterFdFromEpoll(int fd); |
Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 653 | status_t registerDeviceForEpollLocked(Device& device) REQUIRES(mLock); |
| 654 | void registerVideoDeviceForEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock); |
| 655 | status_t unregisterDeviceFromEpollLocked(Device& device) REQUIRES(mLock); |
| 656 | void unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock); |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 657 | |
Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 658 | status_t scanDirLocked(const std::string& dirname) REQUIRES(mLock); |
| 659 | status_t scanVideoDirLocked(const std::string& dirname) REQUIRES(mLock); |
| 660 | void scanDevicesLocked() REQUIRES(mLock); |
Prabir Pradhan | 952e65b | 2022-06-23 17:49:55 +0000 | [diff] [blame] | 661 | base::Result<void> readNotifyLocked() REQUIRES(mLock); |
| 662 | void handleNotifyEventLocked(const inotify_event&) REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 663 | |
Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 664 | Device* getDeviceLocked(int32_t deviceId) const REQUIRES(mLock); |
| 665 | Device* getDeviceByPathLocked(const std::string& devicePath) const REQUIRES(mLock); |
Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 666 | /** |
| 667 | * Look through all available fd's (both for input devices and for video devices), |
| 668 | * and return the device pointer. |
| 669 | */ |
Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 670 | Device* getDeviceByFdLocked(int fd) const REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 671 | |
Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 672 | int32_t getNextControllerNumberLocked(const std::string& name) REQUIRES(mLock); |
Josh Bartel | 938632f | 2022-07-19 15:34:22 -0500 | [diff] [blame] | 673 | |
| 674 | bool hasDeviceWithDescriptorLocked(const std::string& descriptor) const REQUIRES(mLock); |
| 675 | |
Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 676 | void releaseControllerNumberLocked(int32_t num) REQUIRES(mLock); |
| 677 | void reportDeviceAddedForStatisticsLocked(const InputDeviceIdentifier& identifier, |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 678 | ftl::Flags<InputDeviceClass> classes) REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 679 | |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 680 | const std::unordered_map<int32_t, RawBatteryInfo>& getBatteryInfoLocked(int32_t deviceId) const |
| 681 | REQUIRES(mLock); |
| 682 | |
| 683 | const std::unordered_map<int32_t, RawLightInfo>& getLightInfoLocked(int32_t deviceId) const |
| 684 | REQUIRES(mLock); |
| 685 | |
Usama Arif | b27c8e6 | 2021-06-03 16:44:09 +0100 | [diff] [blame] | 686 | void addDeviceInputInotify(); |
| 687 | void addDeviceInotify(); |
| 688 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 689 | // Protect all internal state. |
Chris Ye | 1c2e089 | 2020-11-30 21:41:44 -0800 | [diff] [blame] | 690 | mutable std::mutex mLock; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 691 | |
| 692 | // The actual id of the built-in keyboard, or NO_BUILT_IN_KEYBOARD if none. |
| 693 | // EventHub remaps the built-in keyboard to id 0 externally as required by the API. |
| 694 | enum { |
| 695 | // Must not conflict with any other assigned device ids, including |
| 696 | // the virtual keyboard id (-1). |
| 697 | NO_BUILT_IN_KEYBOARD = -2, |
| 698 | }; |
| 699 | int32_t mBuiltInKeyboardId; |
| 700 | |
| 701 | int32_t mNextDeviceId; |
| 702 | |
| 703 | BitSet32 mControllerNumbers; |
| 704 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 705 | std::unordered_map<int32_t, std::unique_ptr<Device>> mDevices; |
Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 706 | /** |
| 707 | * Video devices that report touchscreen heatmap, but have not (yet) been paired |
| 708 | * with a specific input device. Video device discovery is independent from input device |
| 709 | * discovery, so the two types of devices could be found in any order. |
| 710 | * Ideally, video devices in this queue do not have an open fd, or at least aren't |
| 711 | * actively streaming. |
| 712 | */ |
| 713 | std::vector<std::unique_ptr<TouchVideoDevice>> mUnattachedVideoDevices; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 714 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 715 | std::vector<std::unique_ptr<Device>> mOpeningDevices; |
| 716 | std::vector<std::unique_ptr<Device>> mClosingDevices; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 717 | |
| 718 | bool mNeedToSendFinishedDeviceScan; |
| 719 | bool mNeedToReopenDevices; |
| 720 | bool mNeedToScanDevices; |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 721 | std::vector<std::string> mExcludedDevices; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 722 | |
| 723 | int mEpollFd; |
| 724 | int mINotifyFd; |
| 725 | int mWakeReadPipeFd; |
| 726 | int mWakeWritePipeFd; |
| 727 | |
Usama Arif | b27c8e6 | 2021-06-03 16:44:09 +0100 | [diff] [blame] | 728 | int mDeviceInputWd; |
| 729 | int mDeviceWd = -1; |
Siarhei Vishniakou | 951f362 | 2018-12-12 19:45:42 -0800 | [diff] [blame] | 730 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 731 | // Maximum number of signalled FDs to handle at a time. |
| 732 | static const int EPOLL_MAX_EVENTS = 16; |
| 733 | |
| 734 | // The array of pending epoll events and the index of the next event to be handled. |
| 735 | struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS]; |
| 736 | size_t mPendingEventCount; |
| 737 | size_t mPendingEventIndex; |
| 738 | bool mPendingINotify; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 739 | }; |
| 740 | |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 741 | } // namespace android |