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 | |
Vaibhav Devmurari | cbba14c | 2022-10-10 16:54:49 +0000 | [diff] [blame^] | 265 | virtual void addKeyRemapping(int32_t deviceId, int32_t fromKeyCode, |
| 266 | int32_t toKeyCode) const = 0; |
| 267 | |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 268 | virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, |
| 269 | int32_t metaState, int32_t* outKeycode, int32_t* outMetaState, |
| 270 | uint32_t* outFlags) const = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 271 | |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 272 | 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] | 273 | |
| 274 | // Sets devices that are excluded from opening. |
| 275 | // This can be used to ignore input devices for sensors. |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 276 | virtual void setExcludedDevices(const std::vector<std::string>& devices) = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 277 | |
| 278 | /* |
| 279 | * Wait for events to become available and returns them. |
| 280 | * After returning, the EventHub holds onto a wake lock until the next call to getEvent. |
| 281 | * This ensures that the device will not go to sleep while the event is being processed. |
| 282 | * If the device needs to remain awake longer than that, then the caller is responsible |
| 283 | * for taking care of it (say, by poking the power manager user activity timer). |
| 284 | * |
| 285 | * The timeout is advisory only. If the device is asleep, it will not wake just to |
| 286 | * service the timeout. |
| 287 | * |
| 288 | * Returns the number of events obtained, or 0 if the timeout expired. |
| 289 | */ |
Siarhei Vishniakou | 7b3ea0b | 2022-09-16 14:23:20 -0700 | [diff] [blame] | 290 | virtual std::vector<RawEvent> getEvents(int timeoutMillis) = 0; |
Siarhei Vishniakou | add8929 | 2018-12-13 19:23:36 -0800 | [diff] [blame] | 291 | virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) = 0; |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 292 | virtual base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor( |
| 293 | int32_t deviceId, int32_t absCode) const = 0; |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 294 | // Raw batteries are sysfs power_supply nodes we found from the EventHub device sysfs node, |
| 295 | // containing the raw info of the sysfs node structure. |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 296 | virtual std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const = 0; |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 297 | virtual std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 298 | int32_t BatteryId) const = 0; |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 299 | |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 300 | // Raw lights are sysfs led light nodes we found from the EventHub device sysfs node, |
| 301 | // containing the raw info of the sysfs node structure. |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 302 | virtual std::vector<int32_t> getRawLightIds(int32_t deviceId) const = 0; |
| 303 | virtual std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, |
| 304 | int32_t lightId) const = 0; |
| 305 | 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] | 306 | virtual void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) = 0; |
| 307 | virtual std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities( |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 308 | int32_t deviceId, int32_t lightId) const = 0; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 309 | virtual void setLightIntensities(int32_t deviceId, int32_t lightId, |
| 310 | std::unordered_map<LightColor, int32_t> intensities) = 0; |
Vaibhav Devmurari | dd82b8e | 2022-08-16 15:34:01 +0000 | [diff] [blame] | 311 | /* Query Country code associated with the input device. */ |
| 312 | virtual hardware::input::InputDeviceCountryCode getCountryCode(int32_t deviceId) const = 0; |
| 313 | /* Query current input state. */ |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 314 | virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0; |
| 315 | virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0; |
| 316 | virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0; |
| 317 | virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis, |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 318 | int32_t* outValue) const = 0; |
Philip Junker | 4af3b3d | 2021-12-14 10:36:55 +0100 | [diff] [blame] | 319 | virtual int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 320 | |
| 321 | /* |
| 322 | * Examine key input devices for specific framework keycode support |
| 323 | */ |
Siarhei Vishniakou | 7400794 | 2022-06-13 13:57:47 -0700 | [diff] [blame] | 324 | virtual bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes, |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 325 | uint8_t* outFlags) const = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 326 | |
| 327 | virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const = 0; |
Arthur Hung | cb40a00 | 2021-08-03 14:31:01 +0000 | [diff] [blame] | 328 | virtual bool hasKeyCode(int32_t deviceId, int32_t keyCode) const = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 329 | |
| 330 | /* LED related functions expect Android LED constants, not scan codes or HID usages */ |
| 331 | virtual bool hasLed(int32_t deviceId, int32_t led) const = 0; |
| 332 | virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0; |
| 333 | |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 334 | virtual void getVirtualKeyDefinitions( |
| 335 | int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 336 | |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 337 | virtual const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const = 0; |
| 338 | virtual bool setKeyboardLayoutOverlay(int32_t deviceId, |
| 339 | std::shared_ptr<KeyCharacterMap> map) = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 340 | |
| 341 | /* Control the vibrator. */ |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 342 | virtual void vibrate(int32_t deviceId, const VibrationElement& effect) = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 343 | virtual void cancelVibrate(int32_t deviceId) = 0; |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 344 | virtual std::vector<int32_t> getVibratorIds(int32_t deviceId) const = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 345 | |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 346 | /* Query battery level. */ |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 347 | virtual std::optional<int32_t> getBatteryCapacity(int32_t deviceId, |
| 348 | int32_t batteryId) const = 0; |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 349 | |
| 350 | /* Query battery status. */ |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 351 | 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] | 352 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 353 | /* Requests the EventHub to reopen all input devices on the next call to getEvents(). */ |
| 354 | virtual void requestReopenDevices() = 0; |
| 355 | |
| 356 | /* Wakes up getEvents() if it is blocked on a read. */ |
| 357 | virtual void wake() = 0; |
| 358 | |
| 359 | /* Dump EventHub state to a string. */ |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 360 | virtual void dump(std::string& dump) const = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 361 | |
| 362 | /* Called by the heatbeat to ensures that the reader has not deadlocked. */ |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 363 | virtual void monitor() const = 0; |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 364 | |
| 365 | /* Return true if the device is enabled. */ |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 366 | virtual bool isDeviceEnabled(int32_t deviceId) const = 0; |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 367 | |
| 368 | /* Enable an input device */ |
| 369 | virtual status_t enableDevice(int32_t deviceId) = 0; |
| 370 | |
| 371 | /* Disable an input device. Closes file descriptor to that device. */ |
| 372 | virtual status_t disableDevice(int32_t deviceId) = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 373 | }; |
| 374 | |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 375 | template <std::size_t BITS> |
| 376 | class BitArray { |
| 377 | /* Array element type and vector of element type. */ |
| 378 | using Element = std::uint32_t; |
| 379 | /* Number of bits in each BitArray element. */ |
| 380 | static constexpr size_t WIDTH = sizeof(Element) * CHAR_BIT; |
| 381 | /* Number of elements to represent a bit array of the specified size of bits. */ |
| 382 | static constexpr size_t COUNT = (BITS + WIDTH - 1) / WIDTH; |
| 383 | |
| 384 | public: |
| 385 | /* BUFFER type declaration for BitArray */ |
| 386 | using Buffer = std::array<Element, COUNT>; |
| 387 | /* To tell if a bit is set in array, it selects an element from the array, and test |
| 388 | * if the relevant bit set. |
| 389 | * Note the parameter "bit" is an index to the bit, 0 <= bit < BITS. |
| 390 | */ |
| 391 | inline bool test(size_t bit) const { |
| 392 | return (bit < BITS) ? mData[bit / WIDTH].test(bit % WIDTH) : false; |
| 393 | } |
| 394 | /* Returns total number of bytes needed for the array */ |
| 395 | inline size_t bytes() { return (BITS + CHAR_BIT - 1) / CHAR_BIT; } |
| 396 | /* Returns true if array contains any non-zero bit from the range defined by start and end |
| 397 | * bit index [startIndex, endIndex). |
| 398 | */ |
| 399 | bool any(size_t startIndex, size_t endIndex) { |
| 400 | if (startIndex >= endIndex || startIndex > BITS || endIndex > BITS + 1) { |
| 401 | ALOGE("Invalid start/end index. start = %zu, end = %zu, total bits = %zu", startIndex, |
| 402 | endIndex, BITS); |
| 403 | return false; |
| 404 | } |
| 405 | size_t se = startIndex / WIDTH; // Start of element |
| 406 | size_t ee = endIndex / WIDTH; // End of element |
| 407 | size_t si = startIndex % WIDTH; // Start index in start element |
| 408 | size_t ei = endIndex % WIDTH; // End index in end element |
| 409 | // Need to check first unaligned bitset for any non zero bit |
| 410 | if (si > 0) { |
| 411 | size_t nBits = se == ee ? ei - si : WIDTH - si; |
| 412 | // Generate the mask of interested bit range |
| 413 | Element mask = ((1 << nBits) - 1) << si; |
| 414 | if (mData[se++].to_ulong() & mask) { |
| 415 | return true; |
| 416 | } |
| 417 | } |
| 418 | // Check whole bitset for any bit set |
| 419 | for (; se < ee; se++) { |
| 420 | if (mData[se].any()) { |
| 421 | return true; |
| 422 | } |
| 423 | } |
| 424 | // Need to check last unaligned bitset for any non zero bit |
| 425 | if (ei > 0 && se <= ee) { |
| 426 | // Generate the mask of interested bit range |
| 427 | Element mask = (1 << ei) - 1; |
| 428 | if (mData[se].to_ulong() & mask) { |
| 429 | return true; |
| 430 | } |
| 431 | } |
| 432 | return false; |
| 433 | } |
| 434 | /* Load bit array values from buffer */ |
| 435 | void loadFromBuffer(const Buffer& buffer) { |
| 436 | for (size_t i = 0; i < COUNT; i++) { |
| 437 | mData[i] = std::bitset<WIDTH>(buffer[i]); |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | private: |
| 442 | std::array<std::bitset<WIDTH>, COUNT> mData; |
| 443 | }; |
| 444 | |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 445 | class EventHub : public EventHubInterface { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 446 | public: |
| 447 | EventHub(); |
| 448 | |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 449 | ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 450 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 451 | InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 452 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 453 | int32_t getDeviceControllerNumber(int32_t deviceId) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 454 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 455 | void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) 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 | status_t getAbsoluteAxisInfo(int32_t deviceId, int axis, |
| 458 | RawAbsoluteAxisInfo* outAxisInfo) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 459 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 460 | bool hasRelativeAxis(int32_t deviceId, int axis) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 461 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 462 | bool hasInputProperty(int32_t deviceId, int property) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 463 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 464 | bool hasMscEvent(int32_t deviceId, int mscEvent) const override final; |
| 465 | |
Vaibhav Devmurari | cbba14c | 2022-10-10 16:54:49 +0000 | [diff] [blame^] | 466 | void addKeyRemapping(int32_t deviceId, int32_t fromKeyCode, |
| 467 | int32_t toKeyCode) const override final; |
| 468 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 469 | status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState, |
| 470 | int32_t* outKeycode, int32_t* outMetaState, |
| 471 | uint32_t* outFlags) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 472 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 473 | status_t mapAxis(int32_t deviceId, int32_t scanCode, |
| 474 | AxisInfo* outAxisInfo) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 475 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 476 | base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor( |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 477 | int32_t deviceId, int32_t absCode) const override final; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 478 | |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 479 | std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override final; |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 480 | std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 481 | int32_t BatteryId) const override final; |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 482 | |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 483 | std::vector<int32_t> getRawLightIds(int32_t deviceId) const override final; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 484 | |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 485 | std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, |
| 486 | int32_t lightId) const override final; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 487 | |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 488 | std::optional<int32_t> getLightBrightness(int32_t deviceId, |
| 489 | int32_t lightId) const override final; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 490 | void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override final; |
| 491 | std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities( |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 492 | int32_t deviceId, int32_t lightId) const override final; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 493 | void setLightIntensities(int32_t deviceId, int32_t lightId, |
| 494 | std::unordered_map<LightColor, int32_t> intensities) override final; |
| 495 | |
Vaibhav Devmurari | dd82b8e | 2022-08-16 15:34:01 +0000 | [diff] [blame] | 496 | hardware::input::InputDeviceCountryCode getCountryCode(int32_t deviceId) const override final; |
| 497 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 498 | void setExcludedDevices(const std::vector<std::string>& devices) override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 499 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 500 | int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override final; |
| 501 | int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override final; |
| 502 | int32_t getSwitchState(int32_t deviceId, int32_t sw) const override final; |
Philip Junker | 4af3b3d | 2021-12-14 10:36:55 +0100 | [diff] [blame] | 503 | int32_t getKeyCodeForKeyLocation(int32_t deviceId, |
| 504 | int32_t locationKeyCode) const override final; |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 505 | status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis, |
| 506 | int32_t* outValue) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 507 | |
Siarhei Vishniakou | 7400794 | 2022-06-13 13:57:47 -0700 | [diff] [blame] | 508 | bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes, |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 509 | uint8_t* outFlags) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 510 | |
Siarhei Vishniakou | 7b3ea0b | 2022-09-16 14:23:20 -0700 | [diff] [blame] | 511 | std::vector<RawEvent> getEvents(int timeoutMillis) override final; |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 512 | std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 513 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 514 | bool hasScanCode(int32_t deviceId, int32_t scanCode) const override final; |
Arthur Hung | cb40a00 | 2021-08-03 14:31:01 +0000 | [diff] [blame] | 515 | bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override final; |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 516 | bool hasLed(int32_t deviceId, int32_t led) const override final; |
| 517 | void setLedState(int32_t deviceId, int32_t led, bool on) override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 518 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 519 | void getVirtualKeyDefinitions( |
| 520 | int32_t deviceId, |
| 521 | std::vector<VirtualKeyDefinition>& outVirtualKeys) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 522 | |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 523 | const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap( |
| 524 | int32_t deviceId) const override final; |
| 525 | bool setKeyboardLayoutOverlay(int32_t deviceId, |
| 526 | std::shared_ptr<KeyCharacterMap> map) 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 vibrate(int32_t deviceId, const VibrationElement& effect) override final; |
| 529 | void cancelVibrate(int32_t deviceId) override final; |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 530 | std::vector<int32_t> getVibratorIds(int32_t deviceId) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 531 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 532 | void requestReopenDevices() override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 533 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 534 | void wake() override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 535 | |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 536 | void dump(std::string& dump) const override final; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 537 | |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 538 | void monitor() const override final; |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 539 | |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 540 | std::optional<int32_t> getBatteryCapacity(int32_t deviceId, |
| 541 | int32_t batteryId) const override final; |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 542 | |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 543 | std::optional<int32_t> getBatteryStatus(int32_t deviceId, |
| 544 | int32_t batteryId) const override final; |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 545 | |
Prabir Pradhan | ae4ff28 | 2022-08-23 16:21:39 +0000 | [diff] [blame] | 546 | bool isDeviceEnabled(int32_t deviceId) const override final; |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 547 | |
| 548 | status_t enableDevice(int32_t deviceId) override final; |
| 549 | |
| 550 | status_t disableDevice(int32_t deviceId) override final; |
| 551 | |
| 552 | ~EventHub() override; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 553 | |
| 554 | private: |
Prabir Pradhan | cb42b47 | 2022-08-23 16:01:19 +0000 | [diff] [blame] | 555 | // Holds information about the sysfs device associated with the Device. |
Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 556 | struct AssociatedDevice { |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 557 | // The sysfs root path of the misc device. |
| 558 | std::filesystem::path sysfsRootPath; |
Vaibhav Devmurari | dd82b8e | 2022-08-16 15:34:01 +0000 | [diff] [blame] | 559 | hardware::input::InputDeviceCountryCode countryCode; |
Prabir Pradhan | cb42b47 | 2022-08-23 16:01:19 +0000 | [diff] [blame] | 560 | std::unordered_map<int32_t /*batteryId*/, RawBatteryInfo> batteryInfos; |
| 561 | std::unordered_map<int32_t /*lightId*/, RawLightInfo> lightInfos; |
Prabir Pradhan | edeec3b | 2022-08-26 22:33:55 +0000 | [diff] [blame] | 562 | |
| 563 | bool operator==(const AssociatedDevice&) const = default; |
| 564 | bool operator!=(const AssociatedDevice&) const = default; |
| 565 | std::string dump() const; |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 566 | }; |
| 567 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 568 | struct Device { |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 569 | int fd; // may be -1 if device is closed |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 570 | const int32_t id; |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 571 | const std::string path; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 572 | const InputDeviceIdentifier identifier; |
| 573 | |
Siarhei Vishniakou | ec7854a | 2018-12-14 16:52:34 -0800 | [diff] [blame] | 574 | std::unique_ptr<TouchVideoDevice> videoDevice; |
| 575 | |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 576 | ftl::Flags<InputDeviceClass> classes; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 577 | |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 578 | BitArray<KEY_MAX> keyBitmask; |
| 579 | BitArray<KEY_MAX> keyState; |
| 580 | BitArray<ABS_MAX> absBitmask; |
| 581 | BitArray<REL_MAX> relBitmask; |
| 582 | BitArray<SW_MAX> swBitmask; |
| 583 | BitArray<SW_MAX> swState; |
| 584 | BitArray<LED_MAX> ledBitmask; |
| 585 | BitArray<FF_MAX> ffBitmask; |
| 586 | BitArray<INPUT_PROP_MAX> propBitmask; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 587 | BitArray<MSC_MAX> mscBitmask; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 588 | |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 589 | std::string configurationFile; |
Siarhei Vishniakou | d549b25 | 2020-08-11 11:25:26 -0500 | [diff] [blame] | 590 | std::unique_ptr<PropertyMap> configuration; |
Siarhei Vishniakou | 3e78dec | 2019-02-20 16:21:46 -0600 | [diff] [blame] | 591 | std::unique_ptr<VirtualKeyMap> virtualKeyMap; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 592 | KeyMap keyMap; |
| 593 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 594 | bool ffEffectPlaying; |
| 595 | int16_t ffEffectId; // initially -1 |
| 596 | |
Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 597 | // A shared_ptr of a device associated with the input device. |
Prabir Pradhan | 5189478 | 2022-08-23 16:29:10 +0000 | [diff] [blame] | 598 | // 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] | 599 | std::shared_ptr<const AssociatedDevice> associatedDevice; |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 600 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 601 | int32_t controllerNumber; |
| 602 | |
Prabir Pradhan | cb42b47 | 2022-08-23 16:01:19 +0000 | [diff] [blame] | 603 | Device(int fd, int32_t id, std::string path, InputDeviceIdentifier identifier, |
| 604 | std::shared_ptr<const AssociatedDevice> assocDev); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 605 | ~Device(); |
| 606 | |
| 607 | void close(); |
| 608 | |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 609 | bool enabled; // initially true |
| 610 | status_t enable(); |
| 611 | status_t disable(); |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 612 | bool hasValidFd() const; |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 613 | const bool isVirtual; // set if fd < 0 is passed to constructor |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 614 | |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 615 | const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const; |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 616 | |
| 617 | template <std::size_t N> |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 618 | status_t readDeviceBitMask(unsigned long ioctlCode, BitArray<N>& bitArray); |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 619 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 620 | void configureFd(); |
| 621 | bool hasKeycodeLocked(int keycode) const; |
| 622 | void loadConfigurationLocked(); |
| 623 | bool loadVirtualKeyMapLocked(); |
| 624 | status_t loadKeyMapLocked(); |
| 625 | bool isExternalDeviceLocked(); |
| 626 | bool deviceHasMicLocked(); |
| 627 | void setLedForControllerLocked(); |
| 628 | status_t mapLed(int32_t led, int32_t* outScanCode) const; |
| 629 | void setLedStateLocked(int32_t led, bool on); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 630 | }; |
| 631 | |
Siarhei Vishniakou | a4c502a | 2021-02-05 00:45:20 +0000 | [diff] [blame] | 632 | /** |
| 633 | * Create a new device for the provided path. |
| 634 | */ |
Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 635 | void openDeviceLocked(const std::string& devicePath) REQUIRES(mLock); |
| 636 | void openVideoDeviceLocked(const std::string& devicePath) REQUIRES(mLock); |
Siarhei Vishniakou | f49608d | 2020-08-20 19:18:21 -0500 | [diff] [blame] | 637 | /** |
| 638 | * Try to associate a video device with an input device. If the association succeeds, |
| 639 | * the videoDevice is moved into the input device. 'videoDevice' will become null if this |
| 640 | * happens. |
| 641 | * Return true if the association succeeds. |
| 642 | * Return false otherwise. |
| 643 | */ |
Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 644 | bool tryAddVideoDeviceLocked(Device& device, std::unique_ptr<TouchVideoDevice>& videoDevice) |
| 645 | REQUIRES(mLock); |
| 646 | void createVirtualKeyboardLocked() REQUIRES(mLock); |
| 647 | void addDeviceLocked(std::unique_ptr<Device> device) REQUIRES(mLock); |
| 648 | void assignDescriptorLocked(InputDeviceIdentifier& identifier) REQUIRES(mLock); |
Prabir Pradhan | cb42b47 | 2022-08-23 16:01:19 +0000 | [diff] [blame] | 649 | std::shared_ptr<const AssociatedDevice> obtainAssociatedDeviceLocked( |
| 650 | const std::filesystem::path& devicePath) const REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 651 | |
Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 652 | void closeDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock); |
| 653 | void closeVideoDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock); |
| 654 | void closeDeviceLocked(Device& device) REQUIRES(mLock); |
| 655 | void closeAllDevicesLocked() REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 656 | |
Siarhei Vishniakou | 2592031 | 2018-12-12 15:24:44 -0800 | [diff] [blame] | 657 | status_t registerFdForEpoll(int fd); |
| 658 | status_t unregisterFdFromEpoll(int fd); |
Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 659 | status_t registerDeviceForEpollLocked(Device& device) REQUIRES(mLock); |
| 660 | void registerVideoDeviceForEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock); |
| 661 | status_t unregisterDeviceFromEpollLocked(Device& device) REQUIRES(mLock); |
| 662 | void unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock); |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 663 | |
Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 664 | status_t scanDirLocked(const std::string& dirname) REQUIRES(mLock); |
| 665 | status_t scanVideoDirLocked(const std::string& dirname) REQUIRES(mLock); |
| 666 | void scanDevicesLocked() REQUIRES(mLock); |
Prabir Pradhan | 952e65b | 2022-06-23 17:49:55 +0000 | [diff] [blame] | 667 | base::Result<void> readNotifyLocked() REQUIRES(mLock); |
| 668 | void handleNotifyEventLocked(const inotify_event&) REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 669 | |
Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 670 | Device* getDeviceLocked(int32_t deviceId) const REQUIRES(mLock); |
| 671 | Device* getDeviceByPathLocked(const std::string& devicePath) const REQUIRES(mLock); |
Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 672 | /** |
| 673 | * Look through all available fd's (both for input devices and for video devices), |
| 674 | * and return the device pointer. |
| 675 | */ |
Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 676 | Device* getDeviceByFdLocked(int fd) const 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 | int32_t getNextControllerNumberLocked(const std::string& name) REQUIRES(mLock); |
Josh Bartel | 938632f | 2022-07-19 15:34:22 -0500 | [diff] [blame] | 679 | |
| 680 | bool hasDeviceWithDescriptorLocked(const std::string& descriptor) const REQUIRES(mLock); |
| 681 | |
Chris Ye | d3fef46 | 2021-03-07 17:10:08 -0800 | [diff] [blame] | 682 | void releaseControllerNumberLocked(int32_t num) REQUIRES(mLock); |
| 683 | void reportDeviceAddedForStatisticsLocked(const InputDeviceIdentifier& identifier, |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 684 | ftl::Flags<InputDeviceClass> classes) REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 685 | |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 686 | const std::unordered_map<int32_t, RawBatteryInfo>& getBatteryInfoLocked(int32_t deviceId) const |
| 687 | REQUIRES(mLock); |
| 688 | |
| 689 | const std::unordered_map<int32_t, RawLightInfo>& getLightInfoLocked(int32_t deviceId) const |
| 690 | REQUIRES(mLock); |
| 691 | |
Usama Arif | b27c8e6 | 2021-06-03 16:44:09 +0100 | [diff] [blame] | 692 | void addDeviceInputInotify(); |
| 693 | void addDeviceInotify(); |
| 694 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 695 | // Protect all internal state. |
Chris Ye | 1c2e089 | 2020-11-30 21:41:44 -0800 | [diff] [blame] | 696 | mutable std::mutex mLock; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 697 | |
| 698 | // The actual id of the built-in keyboard, or NO_BUILT_IN_KEYBOARD if none. |
| 699 | // EventHub remaps the built-in keyboard to id 0 externally as required by the API. |
| 700 | enum { |
| 701 | // Must not conflict with any other assigned device ids, including |
| 702 | // the virtual keyboard id (-1). |
| 703 | NO_BUILT_IN_KEYBOARD = -2, |
| 704 | }; |
| 705 | int32_t mBuiltInKeyboardId; |
| 706 | |
| 707 | int32_t mNextDeviceId; |
| 708 | |
| 709 | BitSet32 mControllerNumbers; |
| 710 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 711 | std::unordered_map<int32_t, std::unique_ptr<Device>> mDevices; |
Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 712 | /** |
| 713 | * Video devices that report touchscreen heatmap, but have not (yet) been paired |
| 714 | * with a specific input device. Video device discovery is independent from input device |
| 715 | * discovery, so the two types of devices could be found in any order. |
| 716 | * Ideally, video devices in this queue do not have an open fd, or at least aren't |
| 717 | * actively streaming. |
| 718 | */ |
| 719 | std::vector<std::unique_ptr<TouchVideoDevice>> mUnattachedVideoDevices; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 720 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 721 | std::vector<std::unique_ptr<Device>> mOpeningDevices; |
| 722 | std::vector<std::unique_ptr<Device>> mClosingDevices; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 723 | |
| 724 | bool mNeedToSendFinishedDeviceScan; |
| 725 | bool mNeedToReopenDevices; |
| 726 | bool mNeedToScanDevices; |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 727 | std::vector<std::string> mExcludedDevices; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 728 | |
| 729 | int mEpollFd; |
| 730 | int mINotifyFd; |
| 731 | int mWakeReadPipeFd; |
| 732 | int mWakeWritePipeFd; |
| 733 | |
Usama Arif | b27c8e6 | 2021-06-03 16:44:09 +0100 | [diff] [blame] | 734 | int mDeviceInputWd; |
| 735 | int mDeviceWd = -1; |
Siarhei Vishniakou | 951f362 | 2018-12-12 19:45:42 -0800 | [diff] [blame] | 736 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 737 | // Maximum number of signalled FDs to handle at a time. |
| 738 | static const int EPOLL_MAX_EVENTS = 16; |
| 739 | |
| 740 | // The array of pending epoll events and the index of the next event to be handled. |
| 741 | struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS]; |
| 742 | size_t mPendingEventCount; |
| 743 | size_t mPendingEventIndex; |
| 744 | bool mPendingINotify; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 745 | }; |
| 746 | |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 747 | } // namespace android |