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 <gui/InputApplication.h> 24 #include <input/Input.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 { 33 enum class Type { 34 CONFIGURATION_CHANGED, 35 DEVICE_RESET, 36 FOCUS, 37 KEY, 38 MOTION, 39 SENSOR, 40 POINTER_CAPTURE_CHANGED, 41 DRAG, 42 TOUCH_MODE_CHANGED, 43 44 ftl_last = TOUCH_MODE_CHANGED 45 }; 46 47 int32_t id; 48 Type type; 49 nsecs_t eventTime; 50 uint32_t policyFlags; 51 InjectionState* injectionState; 52 53 bool dispatchInProgress; // initially false, set to true while dispatching 54 55 /** 56 * Injected keys are events from an external (probably untrusted) application 57 * and are not related to real hardware state. They come in via 58 * InputDispatcher::injectInputEvent, which sets policy flag POLICY_FLAG_INJECTED. 59 */ isInjectedEventEntry60 inline bool isInjected() const { return injectionState != nullptr; } 61 62 /** 63 * Synthesized events are either injected events, or events that come 64 * from real hardware, but aren't directly attributable to a specific hardware event. 65 * Key repeat is a synthesized event, because it is related to an actual hardware state 66 * (a key is currently pressed), but the repeat itself is generated by the framework. 67 */ isSynthesizedEventEntry68 inline bool isSynthesized() const { 69 return isInjected() || IdGenerator::getSource(id) != IdGenerator::Source::INPUT_READER; 70 } 71 72 virtual std::string getDescription() const = 0; 73 74 EventEntry(int32_t id, Type type, nsecs_t eventTime, uint32_t policyFlags); 75 virtual ~EventEntry(); 76 77 protected: 78 void releaseInjectionState(); 79 }; 80 81 struct ConfigurationChangedEntry : EventEntry { 82 explicit ConfigurationChangedEntry(int32_t id, nsecs_t eventTime); 83 std::string getDescription() const override; 84 85 ~ConfigurationChangedEntry() override; 86 }; 87 88 struct DeviceResetEntry : EventEntry { 89 int32_t deviceId; 90 91 DeviceResetEntry(int32_t id, nsecs_t eventTime, int32_t deviceId); 92 std::string getDescription() const override; 93 94 ~DeviceResetEntry() override; 95 }; 96 97 struct FocusEntry : EventEntry { 98 sp<IBinder> connectionToken; 99 bool hasFocus; 100 std::string reason; 101 102 FocusEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool hasFocus, 103 const std::string& reason); 104 std::string getDescription() const override; 105 106 ~FocusEntry() override; 107 }; 108 109 struct PointerCaptureChangedEntry : EventEntry { 110 const PointerCaptureRequest pointerCaptureRequest; 111 112 PointerCaptureChangedEntry(int32_t id, nsecs_t eventTime, const PointerCaptureRequest&); 113 std::string getDescription() const override; 114 115 ~PointerCaptureChangedEntry() override; 116 }; 117 118 struct DragEntry : EventEntry { 119 sp<IBinder> connectionToken; 120 bool isExiting; 121 float x, y; 122 123 DragEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool isExiting, float x, 124 float y); 125 std::string getDescription() const override; 126 127 ~DragEntry() override; 128 }; 129 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 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); 156 std::string getDescription() const override; 157 void recycle(); 158 159 ~KeyEntry() override; 160 }; 161 162 struct MotionEntry : EventEntry { 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 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 std::string getDescription() const override; 189 190 ~MotionEntry() override; 191 }; 192 193 struct SensorEntry : EventEntry { 194 int32_t deviceId; 195 uint32_t source; 196 InputDeviceSensorType sensorType; 197 InputDeviceSensorAccuracy accuracy; 198 bool accuracyChanged; 199 nsecs_t hwTimestamp; 200 201 std::vector<float> values; 202 203 SensorEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source, 204 uint32_t policyFlags, nsecs_t hwTimestamp, InputDeviceSensorType sensorType, 205 InputDeviceSensorAccuracy accuracy, bool accuracyChanged, 206 std::vector<float> values); 207 std::string getDescription() const override; 208 209 ~SensorEntry() override; 210 }; 211 212 struct TouchModeEntry : EventEntry { 213 bool inTouchMode; 214 215 TouchModeEntry(int32_t id, nsecs_t eventTime, bool inTouchMode); 216 std::string getDescription() const override; 217 218 ~TouchModeEntry() override; 219 }; 220 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 225 std::shared_ptr<EventEntry> eventEntry; // the event to dispatch 226 int32_t targetFlags; 227 ui::Transform transform; 228 ui::Transform rawTransform; 229 float globalScaleFactor; 230 // Both deliveryTime and timeoutTime are only populated when the entry is sent to the app, 231 // and will be undefined before that. 232 nsecs_t deliveryTime; // time when the event was actually delivered 233 // An ANR will be triggered if a response for this entry is not received by timeoutTime 234 nsecs_t timeoutTime; 235 236 // Set to the resolved ID, action and flags when the event is enqueued. 237 int32_t resolvedEventId; 238 int32_t resolvedAction; 239 int32_t resolvedFlags; 240 241 DispatchEntry(std::shared_ptr<EventEntry> eventEntry, int32_t targetFlags, 242 const ui::Transform& transform, const ui::Transform& rawTransform, 243 float globalScaleFactor); 244 hasForegroundTargetDispatchEntry245 inline bool hasForegroundTarget() const { return targetFlags & InputTarget::FLAG_FOREGROUND; } 246 isSplitDispatchEntry247 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 255 VerifiedKeyEvent verifiedKeyEventFromKeyEntry(const KeyEntry& entry); 256 VerifiedMotionEvent verifiedMotionEventFromMotionEntry(const MotionEntry& entry, 257 const ui::Transform& rawTransform); 258 259 } // namespace android::inputdispatcher 260 261 #endif // _UI_INPUT_INPUTDISPATCHER_ENTRY_H 262