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 #pragma once 18 19 #include "InjectionState.h" 20 #include "InputTargetFlags.h" 21 #include "trace/EventTrackerInterface.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 <ostream> 29 #include <string> 30 31 namespace android::inputdispatcher { 32 33 struct EventEntry { 34 enum class Type { 35 CONFIGURATION_CHANGED, 36 DEVICE_RESET, 37 FOCUS, 38 KEY, 39 MOTION, 40 SENSOR, 41 POINTER_CAPTURE_CHANGED, 42 DRAG, 43 TOUCH_MODE_CHANGED, 44 45 ftl_last = TOUCH_MODE_CHANGED 46 }; 47 48 int32_t id; 49 Type type; 50 nsecs_t eventTime; 51 uint32_t policyFlags; 52 std::shared_ptr<InjectionState> injectionState; 53 54 mutable bool dispatchInProgress; // initially false, set to true while dispatching 55 56 /** 57 * Injected keys are events from an external (probably untrusted) application 58 * and are not related to real hardware state. They come in via 59 * InputDispatcher::injectInputEvent, which sets policy flag POLICY_FLAG_INJECTED. 60 */ isInjectedEventEntry61 inline bool isInjected() const { return injectionState != nullptr; } 62 63 /** 64 * Synthesized events are either injected events, or events that come 65 * from real hardware, but aren't directly attributable to a specific hardware event. 66 * Key repeat is a synthesized event, because it is related to an actual hardware state 67 * (a key is currently pressed), but the repeat itself is generated by the framework. 68 */ isSynthesizedEventEntry69 inline bool isSynthesized() const { 70 return isInjected() || IdGenerator::getSource(id) != IdGenerator::Source::INPUT_READER; 71 } 72 73 virtual std::string getDescription() const = 0; 74 75 EventEntry(int32_t id, Type type, nsecs_t eventTime, uint32_t policyFlags); 76 EventEntry(const EventEntry&) = delete; 77 EventEntry& operator=(const EventEntry&) = delete; 78 virtual ~EventEntry() = default; 79 }; 80 81 struct ConfigurationChangedEntry : EventEntry { 82 explicit ConfigurationChangedEntry(int32_t id, nsecs_t eventTime); 83 std::string getDescription() const override; 84 }; 85 86 struct DeviceResetEntry : EventEntry { 87 int32_t deviceId; 88 89 DeviceResetEntry(int32_t id, nsecs_t eventTime, int32_t deviceId); 90 std::string getDescription() const override; 91 }; 92 93 struct FocusEntry : EventEntry { 94 sp<IBinder> connectionToken; 95 bool hasFocus; 96 std::string reason; 97 98 FocusEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool hasFocus, 99 const std::string& reason); 100 std::string getDescription() const override; 101 }; 102 103 struct PointerCaptureChangedEntry : EventEntry { 104 const PointerCaptureRequest pointerCaptureRequest; 105 106 PointerCaptureChangedEntry(int32_t id, nsecs_t eventTime, const PointerCaptureRequest&); 107 std::string getDescription() const override; 108 }; 109 110 struct DragEntry : EventEntry { 111 sp<IBinder> connectionToken; 112 bool isExiting; 113 float x, y; 114 115 DragEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool isExiting, float x, 116 float y); 117 std::string getDescription() const override; 118 }; 119 120 struct KeyEntry : EventEntry { 121 int32_t deviceId; 122 uint32_t source; 123 ui::LogicalDisplayId displayId; 124 int32_t action; 125 int32_t keyCode; 126 int32_t scanCode; 127 int32_t metaState; 128 nsecs_t downTime; 129 std::unique_ptr<trace::EventTrackerInterface> traceTracker; 130 131 bool syntheticRepeat; // set to true for synthetic key repeats 132 133 enum class InterceptKeyResult { 134 UNKNOWN, 135 SKIP, 136 CONTINUE, 137 TRY_AGAIN_LATER, 138 }; 139 // These are special fields that may need to be modified while the event is being dispatched. 140 mutable InterceptKeyResult interceptKeyResult; // set based on the interception result 141 mutable nsecs_t interceptKeyWakeupTime; // used with INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER 142 mutable int32_t flags; 143 // TODO(b/328618922): Refactor key repeat generation to make repeatCount non-mutable. 144 mutable int32_t repeatCount; 145 146 KeyEntry(int32_t id, std::shared_ptr<InjectionState> injectionState, nsecs_t eventTime, 147 int32_t deviceId, uint32_t source, ui::LogicalDisplayId displayId, 148 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode, 149 int32_t metaState, int32_t repeatCount, nsecs_t downTime); 150 std::string getDescription() const override; 151 }; 152 153 std::ostream& operator<<(std::ostream& out, const KeyEntry& motionEntry); 154 155 struct MotionEntry : EventEntry { 156 int32_t deviceId; 157 uint32_t source; 158 ui::LogicalDisplayId displayId; 159 int32_t action; 160 int32_t actionButton; 161 int32_t flags; 162 int32_t metaState; 163 int32_t buttonState; 164 MotionClassification classification; 165 int32_t edgeFlags; 166 float xPrecision; 167 float yPrecision; 168 float xCursorPosition; 169 float yCursorPosition; 170 nsecs_t downTime; 171 std::vector<PointerProperties> pointerProperties; 172 std::vector<PointerCoords> pointerCoords; 173 std::unique_ptr<trace::EventTrackerInterface> traceTracker; 174 getPointerCountMotionEntry175 size_t getPointerCount() const { return pointerProperties.size(); } 176 177 MotionEntry(int32_t id, std::shared_ptr<InjectionState> injectionState, nsecs_t eventTime, 178 int32_t deviceId, uint32_t source, ui::LogicalDisplayId 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, 183 const std::vector<PointerProperties>& pointerProperties, 184 const std::vector<PointerCoords>& pointerCoords); 185 std::string getDescription() const override; 186 }; 187 188 std::ostream& operator<<(std::ostream& out, const MotionEntry& motionEntry); 189 190 struct SensorEntry : EventEntry { 191 int32_t deviceId; 192 uint32_t source; 193 InputDeviceSensorType sensorType; 194 InputDeviceSensorAccuracy accuracy; 195 bool accuracyChanged; 196 nsecs_t hwTimestamp; 197 198 std::vector<float> values; 199 200 SensorEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source, 201 uint32_t policyFlags, nsecs_t hwTimestamp, InputDeviceSensorType sensorType, 202 InputDeviceSensorAccuracy accuracy, bool accuracyChanged, 203 std::vector<float> values); 204 std::string getDescription() const override; 205 }; 206 207 struct TouchModeEntry : EventEntry { 208 bool inTouchMode; 209 ui::LogicalDisplayId displayId; 210 211 TouchModeEntry(int32_t id, nsecs_t eventTime, bool inTouchMode, ui::LogicalDisplayId displayId); 212 std::string getDescription() const override; 213 }; 214 215 // Tracks the progress of dispatching a particular event to a particular connection. 216 struct DispatchEntry { 217 const uint32_t seq; // unique sequence number, never 0 218 219 std::shared_ptr<const EventEntry> eventEntry; // the event to dispatch 220 const ftl::Flags<InputTargetFlags> targetFlags; 221 ui::Transform transform; 222 ui::Transform rawTransform; 223 float globalScaleFactor; 224 // Both deliveryTime and timeoutTime are only populated when the entry is sent to the app, 225 // and will be undefined before that. 226 nsecs_t deliveryTime; // time when the event was actually delivered 227 // An ANR will be triggered if a response for this entry is not received by timeoutTime 228 nsecs_t timeoutTime; 229 230 int32_t resolvedFlags; 231 232 // Information about the dispatch window used for tracing. We avoid holding a window handle 233 // here because information in a window handle may be dynamically updated within the lifespan 234 // of this dispatch entry. 235 gui::Uid targetUid; 236 int64_t vsyncId; 237 // The window that this event is targeting. The only case when this windowId is not populated 238 // is when dispatching an event to a global monitor. 239 std::optional<int32_t> windowId; 240 241 DispatchEntry(std::shared_ptr<const EventEntry> eventEntry, 242 ftl::Flags<InputTargetFlags> targetFlags, const ui::Transform& transform, 243 const ui::Transform& rawTransform, float globalScaleFactor, gui::Uid targetUid, 244 int64_t vsyncId, std::optional<int32_t> windowId); 245 DispatchEntry(const DispatchEntry&) = delete; 246 DispatchEntry& operator=(const DispatchEntry&) = delete; 247 hasForegroundTargetDispatchEntry248 inline bool hasForegroundTarget() const { 249 return targetFlags.test(InputTargetFlags::FOREGROUND); 250 } 251 isSplitDispatchEntry252 inline bool isSplit() const { return targetFlags.test(InputTargetFlags::SPLIT); } 253 254 private: 255 static volatile int32_t sNextSeqAtomic; 256 257 static uint32_t nextSeq(); 258 }; 259 260 std::ostream& operator<<(std::ostream& out, const DispatchEntry& entry); 261 262 VerifiedKeyEvent verifiedKeyEventFromKeyEntry(const KeyEntry& entry); 263 VerifiedMotionEvent verifiedMotionEventFromMotionEntry(const MotionEntry& entry, 264 const ui::Transform& rawTransform); 265 266 } // namespace android::inputdispatcher 267