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