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