Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 18 | |
| 19 | #include "InjectionState.h" |
| 20 | #include "InputTarget.h" |
| 21 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 22 | #include <gui/InputApplication.h> |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 23 | #include <input/Input.h> |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 24 | #include <stdint.h> |
| 25 | #include <utils/Timers.h> |
| 26 | #include <functional> |
Siarhei Vishniakou | 72945a0 | 2023-09-18 18:30:25 -0700 | [diff] [blame] | 27 | #include <ostream> |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 28 | #include <string> |
| 29 | |
| 30 | namespace android::inputdispatcher { |
| 31 | |
| 32 | struct EventEntry { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 33 | enum class Type { |
| 34 | CONFIGURATION_CHANGED, |
| 35 | DEVICE_RESET, |
| 36 | FOCUS, |
| 37 | KEY, |
| 38 | MOTION, |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 39 | SENSOR, |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 40 | POINTER_CAPTURE_CHANGED, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 41 | DRAG, |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 42 | TOUCH_MODE_CHANGED, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 43 | |
| 44 | ftl_last = TOUCH_MODE_CHANGED |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 45 | }; |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 46 | |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 47 | int32_t id; |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 48 | Type type; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 49 | nsecs_t eventTime; |
| 50 | uint32_t policyFlags; |
| 51 | InjectionState* injectionState; |
| 52 | |
| 53 | bool dispatchInProgress; // initially false, set to true while dispatching |
| 54 | |
Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 55 | /** |
| 56 | * Injected keys are events from an external (probably untrusted) application |
| 57 | * and are not related to real hardware state. They come in via |
| 58 | * InputDispatcher::injectInputEvent, which sets policy flag POLICY_FLAG_INJECTED. |
| 59 | */ |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 60 | inline bool isInjected() const { return injectionState != nullptr; } |
| 61 | |
Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 62 | /** |
| 63 | * Synthesized events are either injected events, or events that come |
| 64 | * from real hardware, but aren't directly attributable to a specific hardware event. |
| 65 | * Key repeat is a synthesized event, because it is related to an actual hardware state |
| 66 | * (a key is currently pressed), but the repeat itself is generated by the framework. |
| 67 | */ |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 68 | inline bool isSynthesized() const { |
| 69 | return isInjected() || IdGenerator::getSource(id) != IdGenerator::Source::INPUT_READER; |
| 70 | } |
Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 71 | |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 72 | virtual std::string getDescription() const = 0; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 73 | |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 74 | EventEntry(int32_t id, Type type, nsecs_t eventTime, uint32_t policyFlags); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 75 | virtual ~EventEntry(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 76 | |
| 77 | protected: |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 78 | void releaseInjectionState(); |
| 79 | }; |
| 80 | |
| 81 | struct ConfigurationChangedEntry : EventEntry { |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 82 | explicit ConfigurationChangedEntry(int32_t id, nsecs_t eventTime); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 83 | std::string getDescription() const override; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 84 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 85 | ~ConfigurationChangedEntry() override; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | struct DeviceResetEntry : EventEntry { |
| 89 | int32_t deviceId; |
| 90 | |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 91 | DeviceResetEntry(int32_t id, nsecs_t eventTime, int32_t deviceId); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 92 | std::string getDescription() const override; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 93 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 94 | ~DeviceResetEntry() override; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 97 | struct FocusEntry : EventEntry { |
| 98 | sp<IBinder> connectionToken; |
| 99 | bool hasFocus; |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 100 | std::string reason; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 101 | |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 102 | FocusEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 103 | const std::string& reason); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 104 | std::string getDescription() const override; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 105 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 106 | ~FocusEntry() override; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 107 | }; |
| 108 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 109 | struct PointerCaptureChangedEntry : EventEntry { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 110 | const PointerCaptureRequest pointerCaptureRequest; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 111 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 112 | PointerCaptureChangedEntry(int32_t id, nsecs_t eventTime, const PointerCaptureRequest&); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 113 | std::string getDescription() const override; |
| 114 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 115 | ~PointerCaptureChangedEntry() override; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 116 | }; |
| 117 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 118 | struct DragEntry : EventEntry { |
| 119 | sp<IBinder> connectionToken; |
| 120 | bool isExiting; |
| 121 | float x, y; |
| 122 | |
| 123 | DragEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool isExiting, float x, |
| 124 | float y); |
| 125 | std::string getDescription() const override; |
| 126 | |
| 127 | ~DragEntry() override; |
| 128 | }; |
| 129 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 130 | struct KeyEntry : EventEntry { |
| 131 | int32_t deviceId; |
| 132 | uint32_t source; |
| 133 | int32_t displayId; |
| 134 | int32_t action; |
| 135 | int32_t flags; |
| 136 | int32_t keyCode; |
| 137 | int32_t scanCode; |
| 138 | int32_t metaState; |
| 139 | int32_t repeatCount; |
| 140 | nsecs_t downTime; |
| 141 | |
| 142 | bool syntheticRepeat; // set to true for synthetic key repeats |
| 143 | |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 144 | enum class InterceptKeyResult { |
| 145 | UNKNOWN, |
| 146 | SKIP, |
| 147 | CONTINUE, |
| 148 | TRY_AGAIN_LATER, |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 149 | }; |
| 150 | InterceptKeyResult interceptKeyResult; // set based on the interception result |
| 151 | nsecs_t interceptKeyWakeupTime; // used with INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER |
| 152 | |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 153 | KeyEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source, int32_t displayId, |
| 154 | uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode, |
| 155 | int32_t metaState, int32_t repeatCount, nsecs_t downTime); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 156 | std::string getDescription() const override; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 157 | void recycle(); |
| 158 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 159 | ~KeyEntry() override; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | struct MotionEntry : EventEntry { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 163 | int32_t deviceId; |
| 164 | uint32_t source; |
| 165 | int32_t displayId; |
| 166 | int32_t action; |
| 167 | int32_t actionButton; |
| 168 | int32_t flags; |
| 169 | int32_t metaState; |
| 170 | int32_t buttonState; |
| 171 | MotionClassification classification; |
| 172 | int32_t edgeFlags; |
| 173 | float xPrecision; |
| 174 | float yPrecision; |
| 175 | float xCursorPosition; |
| 176 | float yCursorPosition; |
| 177 | nsecs_t downTime; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame^] | 178 | std::vector<PointerProperties> pointerProperties; |
| 179 | std::vector<PointerCoords> pointerCoords; |
| 180 | |
| 181 | size_t getPointerCount() const { return pointerProperties.size(); } |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 182 | |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 183 | MotionEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source, int32_t displayId, |
| 184 | uint32_t policyFlags, int32_t action, int32_t actionButton, int32_t flags, |
| 185 | int32_t metaState, int32_t buttonState, MotionClassification classification, |
| 186 | int32_t edgeFlags, float xPrecision, float yPrecision, float xCursorPosition, |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame^] | 187 | float yCursorPosition, nsecs_t downTime, |
| 188 | const std::vector<PointerProperties>& pointerProperties, |
| 189 | const std::vector<PointerCoords>& pointerCoords); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 190 | std::string getDescription() const override; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 191 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 192 | ~MotionEntry() override; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 193 | }; |
| 194 | |
Siarhei Vishniakou | 72945a0 | 2023-09-18 18:30:25 -0700 | [diff] [blame] | 195 | std::ostream& operator<<(std::ostream& out, const MotionEntry& motionEntry); |
| 196 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 197 | struct SensorEntry : EventEntry { |
| 198 | int32_t deviceId; |
| 199 | uint32_t source; |
| 200 | InputDeviceSensorType sensorType; |
| 201 | InputDeviceSensorAccuracy accuracy; |
| 202 | bool accuracyChanged; |
| 203 | nsecs_t hwTimestamp; |
| 204 | |
| 205 | std::vector<float> values; |
| 206 | |
| 207 | SensorEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source, |
| 208 | uint32_t policyFlags, nsecs_t hwTimestamp, InputDeviceSensorType sensorType, |
| 209 | InputDeviceSensorAccuracy accuracy, bool accuracyChanged, |
| 210 | std::vector<float> values); |
| 211 | std::string getDescription() const override; |
| 212 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 213 | ~SensorEntry() override; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 214 | }; |
| 215 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 216 | struct TouchModeEntry : EventEntry { |
| 217 | bool inTouchMode; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 218 | int32_t displayId; |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 219 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 220 | TouchModeEntry(int32_t id, nsecs_t eventTime, bool inTouchMode, int32_t displayId); |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 221 | std::string getDescription() const override; |
| 222 | |
| 223 | ~TouchModeEntry() override; |
| 224 | }; |
| 225 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 226 | // Tracks the progress of dispatching a particular event to a particular connection. |
| 227 | struct DispatchEntry { |
| 228 | const uint32_t seq; // unique sequence number, never 0 |
| 229 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 230 | std::shared_ptr<EventEntry> eventEntry; // the event to dispatch |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 231 | const ftl::Flags<InputTarget::Flags> targetFlags; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 232 | ui::Transform transform; |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 233 | ui::Transform rawTransform; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 234 | float globalScaleFactor; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 235 | // Both deliveryTime and timeoutTime are only populated when the entry is sent to the app, |
| 236 | // and will be undefined before that. |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 237 | nsecs_t deliveryTime; // time when the event was actually delivered |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 238 | // An ANR will be triggered if a response for this entry is not received by timeoutTime |
| 239 | nsecs_t timeoutTime; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 240 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 241 | // Set to the resolved ID, action and flags when the event is enqueued. |
| 242 | int32_t resolvedEventId; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 243 | int32_t resolvedAction; |
| 244 | int32_t resolvedFlags; |
| 245 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 246 | DispatchEntry(std::shared_ptr<EventEntry> eventEntry, |
| 247 | ftl::Flags<InputTarget::Flags> targetFlags, const ui::Transform& transform, |
| 248 | const ui::Transform& rawTransform, float globalScaleFactor); |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 249 | DispatchEntry(const DispatchEntry&) = delete; |
| 250 | DispatchEntry& operator=(const DispatchEntry&) = delete; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 251 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 252 | inline bool hasForegroundTarget() const { |
| 253 | return targetFlags.test(InputTarget::Flags::FOREGROUND); |
| 254 | } |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 255 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 256 | inline bool isSplit() const { return targetFlags.test(InputTarget::Flags::SPLIT); } |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 257 | |
| 258 | private: |
| 259 | static volatile int32_t sNextSeqAtomic; |
| 260 | |
| 261 | static uint32_t nextSeq(); |
| 262 | }; |
| 263 | |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 264 | std::ostream& operator<<(std::ostream& out, const DispatchEntry& entry); |
| 265 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 266 | VerifiedKeyEvent verifiedKeyEventFromKeyEntry(const KeyEntry& entry); |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 267 | VerifiedMotionEvent verifiedMotionEventFromMotionEntry(const MotionEntry& entry, |
| 268 | const ui::Transform& rawTransform); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 269 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 270 | } // namespace android::inputdispatcher |