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