| 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 |  | 
|  | 17 | #ifndef _UI_INPUT_INPUTDISPATCHER_ENTRY_H | 
|  | 18 | #define _UI_INPUT_INPUTDISPATCHER_ENTRY_H | 
|  | 19 |  | 
|  | 20 | #include "InjectionState.h" | 
|  | 21 | #include "InputTarget.h" | 
|  | 22 |  | 
|  | 23 | #include <input/Input.h> | 
|  | 24 | #include <input/InputApplication.h> | 
|  | 25 | #include <stdint.h> | 
|  | 26 | #include <utils/Timers.h> | 
|  | 27 | #include <functional> | 
|  | 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, | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 41 | }; | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 42 |  | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 43 | int32_t id; | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 44 | Type type; | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 45 | nsecs_t eventTime; | 
|  | 46 | uint32_t policyFlags; | 
|  | 47 | InjectionState* injectionState; | 
|  | 48 |  | 
|  | 49 | bool dispatchInProgress; // initially false, set to true while dispatching | 
|  | 50 |  | 
| Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 51 | /** | 
|  | 52 | * Injected keys are events from an external (probably untrusted) application | 
|  | 53 | * and are not related to real hardware state. They come in via | 
|  | 54 | * InputDispatcher::injectInputEvent, which sets policy flag POLICY_FLAG_INJECTED. | 
|  | 55 | */ | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 56 | inline bool isInjected() const { return injectionState != nullptr; } | 
|  | 57 |  | 
| Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 58 | /** | 
|  | 59 | * Synthesized events are either injected events, or events that come | 
|  | 60 | * from real hardware, but aren't directly attributable to a specific hardware event. | 
|  | 61 | * Key repeat is a synthesized event, because it is related to an actual hardware state | 
|  | 62 | * (a key is currently pressed), but the repeat itself is generated by the framework. | 
|  | 63 | */ | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 64 | inline bool isSynthesized() const { | 
|  | 65 | return isInjected() || IdGenerator::getSource(id) != IdGenerator::Source::INPUT_READER; | 
|  | 66 | } | 
| Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 67 |  | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 68 | virtual std::string getDescription() const = 0; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 69 |  | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 70 | EventEntry(int32_t id, Type type, nsecs_t eventTime, uint32_t policyFlags); | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 71 | virtual ~EventEntry(); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 72 |  | 
|  | 73 | protected: | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 74 | void releaseInjectionState(); | 
|  | 75 | }; | 
|  | 76 |  | 
|  | 77 | struct ConfigurationChangedEntry : EventEntry { | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 78 | explicit ConfigurationChangedEntry(int32_t id, nsecs_t eventTime); | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 79 | std::string getDescription() const override; | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 80 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 81 | virtual ~ConfigurationChangedEntry(); | 
|  | 82 | }; | 
|  | 83 |  | 
|  | 84 | struct DeviceResetEntry : EventEntry { | 
|  | 85 | int32_t deviceId; | 
|  | 86 |  | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 87 | DeviceResetEntry(int32_t id, nsecs_t eventTime, int32_t deviceId); | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 88 | std::string getDescription() const override; | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 89 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 90 | virtual ~DeviceResetEntry(); | 
|  | 91 | }; | 
|  | 92 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 93 | struct FocusEntry : EventEntry { | 
|  | 94 | sp<IBinder> connectionToken; | 
|  | 95 | bool hasFocus; | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 96 | std::string reason; | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 97 |  | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 98 | FocusEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool hasFocus, | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 99 | const std::string& reason); | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 100 | std::string getDescription() const override; | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 101 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 102 | virtual ~FocusEntry(); | 
|  | 103 | }; | 
|  | 104 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 105 | struct PointerCaptureChangedEntry : EventEntry { | 
|  | 106 | bool pointerCaptureEnabled; | 
|  | 107 |  | 
|  | 108 | PointerCaptureChangedEntry(int32_t id, nsecs_t eventTime, bool hasPointerCapture); | 
|  | 109 | std::string getDescription() const override; | 
|  | 110 |  | 
|  | 111 | virtual ~PointerCaptureChangedEntry(); | 
|  | 112 | }; | 
|  | 113 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 114 | struct KeyEntry : EventEntry { | 
|  | 115 | int32_t deviceId; | 
|  | 116 | uint32_t source; | 
|  | 117 | int32_t displayId; | 
|  | 118 | int32_t action; | 
|  | 119 | int32_t flags; | 
|  | 120 | int32_t keyCode; | 
|  | 121 | int32_t scanCode; | 
|  | 122 | int32_t metaState; | 
|  | 123 | int32_t repeatCount; | 
|  | 124 | nsecs_t downTime; | 
|  | 125 |  | 
|  | 126 | bool syntheticRepeat; // set to true for synthetic key repeats | 
|  | 127 |  | 
|  | 128 | enum InterceptKeyResult { | 
|  | 129 | INTERCEPT_KEY_RESULT_UNKNOWN, | 
|  | 130 | INTERCEPT_KEY_RESULT_SKIP, | 
|  | 131 | INTERCEPT_KEY_RESULT_CONTINUE, | 
|  | 132 | INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER, | 
|  | 133 | }; | 
|  | 134 | InterceptKeyResult interceptKeyResult; // set based on the interception result | 
|  | 135 | nsecs_t interceptKeyWakeupTime;        // used with INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER | 
|  | 136 |  | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 137 | KeyEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source, int32_t displayId, | 
|  | 138 | uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode, | 
|  | 139 | int32_t metaState, int32_t repeatCount, nsecs_t downTime); | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 140 | std::string getDescription() const override; | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 141 | void recycle(); | 
|  | 142 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 143 | virtual ~KeyEntry(); | 
|  | 144 | }; | 
|  | 145 |  | 
|  | 146 | struct MotionEntry : EventEntry { | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 147 | int32_t deviceId; | 
|  | 148 | uint32_t source; | 
|  | 149 | int32_t displayId; | 
|  | 150 | int32_t action; | 
|  | 151 | int32_t actionButton; | 
|  | 152 | int32_t flags; | 
|  | 153 | int32_t metaState; | 
|  | 154 | int32_t buttonState; | 
|  | 155 | MotionClassification classification; | 
|  | 156 | int32_t edgeFlags; | 
|  | 157 | float xPrecision; | 
|  | 158 | float yPrecision; | 
|  | 159 | float xCursorPosition; | 
|  | 160 | float yCursorPosition; | 
|  | 161 | nsecs_t downTime; | 
|  | 162 | uint32_t pointerCount; | 
|  | 163 | PointerProperties pointerProperties[MAX_POINTERS]; | 
|  | 164 | PointerCoords pointerCoords[MAX_POINTERS]; | 
|  | 165 |  | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 166 | MotionEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source, int32_t displayId, | 
|  | 167 | uint32_t policyFlags, int32_t action, int32_t actionButton, int32_t flags, | 
|  | 168 | int32_t metaState, int32_t buttonState, MotionClassification classification, | 
|  | 169 | int32_t edgeFlags, float xPrecision, float yPrecision, float xCursorPosition, | 
|  | 170 | float yCursorPosition, nsecs_t downTime, uint32_t pointerCount, | 
|  | 171 | const PointerProperties* pointerProperties, const PointerCoords* pointerCoords, | 
|  | 172 | float xOffset, float yOffset); | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 173 | std::string getDescription() const override; | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 174 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 175 | virtual ~MotionEntry(); | 
|  | 176 | }; | 
|  | 177 |  | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 178 | struct SensorEntry : EventEntry { | 
|  | 179 | int32_t deviceId; | 
|  | 180 | uint32_t source; | 
|  | 181 | InputDeviceSensorType sensorType; | 
|  | 182 | InputDeviceSensorAccuracy accuracy; | 
|  | 183 | bool accuracyChanged; | 
|  | 184 | nsecs_t hwTimestamp; | 
|  | 185 |  | 
|  | 186 | std::vector<float> values; | 
|  | 187 |  | 
|  | 188 | SensorEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source, | 
|  | 189 | uint32_t policyFlags, nsecs_t hwTimestamp, InputDeviceSensorType sensorType, | 
|  | 190 | InputDeviceSensorAccuracy accuracy, bool accuracyChanged, | 
|  | 191 | std::vector<float> values); | 
|  | 192 | std::string getDescription() const override; | 
|  | 193 |  | 
|  | 194 | virtual ~SensorEntry(); | 
|  | 195 | }; | 
|  | 196 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 197 | // Tracks the progress of dispatching a particular event to a particular connection. | 
|  | 198 | struct DispatchEntry { | 
|  | 199 | const uint32_t seq; // unique sequence number, never 0 | 
|  | 200 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 201 | std::shared_ptr<EventEntry> eventEntry; // the event to dispatch | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 202 | int32_t targetFlags; | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 203 | ui::Transform transform; | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 204 | float globalScaleFactor; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 205 | // Both deliveryTime and timeoutTime are only populated when the entry is sent to the app, | 
|  | 206 | // and will be undefined before that. | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 207 | nsecs_t deliveryTime; // time when the event was actually delivered | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 208 | // An ANR will be triggered if a response for this entry is not received by timeoutTime | 
|  | 209 | nsecs_t timeoutTime; | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 210 |  | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 211 | // Set to the resolved ID, action and flags when the event is enqueued. | 
|  | 212 | int32_t resolvedEventId; | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 213 | int32_t resolvedAction; | 
|  | 214 | int32_t resolvedFlags; | 
|  | 215 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 216 | DispatchEntry(std::shared_ptr<EventEntry> eventEntry, int32_t targetFlags, | 
|  | 217 | ui::Transform transform, float globalScaleFactor); | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 218 |  | 
|  | 219 | inline bool hasForegroundTarget() const { return targetFlags & InputTarget::FLAG_FOREGROUND; } | 
|  | 220 |  | 
|  | 221 | inline bool isSplit() const { return targetFlags & InputTarget::FLAG_SPLIT; } | 
|  | 222 |  | 
|  | 223 | private: | 
|  | 224 | static volatile int32_t sNextSeqAtomic; | 
|  | 225 |  | 
|  | 226 | static uint32_t nextSeq(); | 
|  | 227 | }; | 
|  | 228 |  | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 229 | VerifiedKeyEvent verifiedKeyEventFromKeyEntry(const KeyEntry& entry); | 
|  | 230 | VerifiedMotionEvent verifiedMotionEventFromMotionEntry(const MotionEntry& entry); | 
|  | 231 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 232 | class InputDispatcher; | 
|  | 233 | // A command entry captures state and behavior for an action to be performed in the | 
|  | 234 | // dispatch loop after the initial processing has taken place.  It is essentially | 
|  | 235 | // a kind of continuation used to postpone sensitive policy interactions to a point | 
|  | 236 | // in the dispatch loop where it is safe to release the lock (generally after finishing | 
|  | 237 | // the critical parts of the dispatch cycle). | 
|  | 238 | // | 
|  | 239 | // The special thing about commands is that they can voluntarily release and reacquire | 
|  | 240 | // the dispatcher lock at will.  Initially when the command starts running, the | 
|  | 241 | // dispatcher lock is held.  However, if the command needs to call into the policy to | 
|  | 242 | // do some work, it can release the lock, do the work, then reacquire the lock again | 
|  | 243 | // before returning. | 
|  | 244 | // | 
|  | 245 | // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch | 
|  | 246 | // never calls into the policy while holding its lock. | 
|  | 247 | // | 
|  | 248 | // Commands are implicitly 'LockedInterruptible'. | 
|  | 249 | struct CommandEntry; | 
|  | 250 | typedef std::function<void(InputDispatcher&, CommandEntry*)> Command; | 
|  | 251 |  | 
|  | 252 | class Connection; | 
|  | 253 | struct CommandEntry { | 
|  | 254 | explicit CommandEntry(Command command); | 
|  | 255 | ~CommandEntry(); | 
|  | 256 |  | 
|  | 257 | Command command; | 
|  | 258 |  | 
|  | 259 | // parameters for the command (usage varies by command) | 
|  | 260 | sp<Connection> connection; | 
|  | 261 | nsecs_t eventTime; | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 262 | std::shared_ptr<KeyEntry> keyEntry; | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 263 | std::shared_ptr<SensorEntry> sensorEntry; | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 264 | std::shared_ptr<InputApplicationHandle> inputApplicationHandle; | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 265 | std::string reason; | 
|  | 266 | int32_t userActivityEventType; | 
|  | 267 | uint32_t seq; | 
|  | 268 | bool handled; | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 269 | sp<IBinder> connectionToken; | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 270 | sp<IBinder> oldToken; | 
|  | 271 | sp<IBinder> newToken; | 
| Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 272 | std::string obscuringPackage; | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 273 | bool enabled; | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 274 | int32_t pid; | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 275 | }; | 
|  | 276 |  | 
|  | 277 | } // namespace android::inputdispatcher | 
|  | 278 |  | 
|  | 279 | #endif // _UI_INPUT_INPUTDISPATCHER_ENTRY_H |