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