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