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 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 std::shared_ptr<InjectionState> injectionState; 52 53 mutable 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 EventEntry(const EventEntry&) = delete; 76 EventEntry& operator=(const EventEntry&) = delete; 77 virtual ~EventEntry() = default; 78 }; 79 80 struct DeviceResetEntry : EventEntry { 81 int32_t deviceId; 82 83 DeviceResetEntry(int32_t id, nsecs_t eventTime, int32_t deviceId); 84 std::string getDescription() const override; 85 }; 86 87 struct FocusEntry : EventEntry { 88 sp<IBinder> connectionToken; 89 bool hasFocus; 90 std::string reason; 91 92 FocusEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool hasFocus, 93 const std::string& reason); 94 std::string getDescription() const override; 95 }; 96 97 struct PointerCaptureChangedEntry : EventEntry { 98 const PointerCaptureRequest pointerCaptureRequest; 99 100 PointerCaptureChangedEntry(int32_t id, nsecs_t eventTime, const PointerCaptureRequest&); 101 std::string getDescription() const override; 102 }; 103 104 struct DragEntry : EventEntry { 105 sp<IBinder> connectionToken; 106 bool isExiting; 107 float x, y; 108 109 DragEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool isExiting, float x, 110 float y); 111 std::string getDescription() const override; 112 }; 113 114 struct KeyEntry : EventEntry { 115 int32_t deviceId; 116 uint32_t source; 117 ui::LogicalDisplayId displayId; 118 int32_t action; 119 int32_t keyCode; 120 int32_t scanCode; 121 int32_t metaState; 122 nsecs_t downTime; 123 std::unique_ptr<trace::EventTrackerInterface> traceTracker; 124 125 bool syntheticRepeat; // set to true for synthetic key repeats 126 127 enum class InterceptKeyResult { 128 // The interception result is unknown. 129 UNKNOWN, 130 // The event should be skipped and not sent to the application. 131 SKIP, 132 // The event should be sent to the application. 133 CONTINUE, 134 // The event should eventually be sent to the application, after a delay. 135 TRY_AGAIN_LATER, 136 // The event should not be initially sent to the application, but instead go through 137 // post-processing to generate a fallback key event and then sent to the application. 138 FALLBACK, 139 }; 140 // These are special fields that may need to be modified while the event is being dispatched. 141 mutable InterceptKeyResult interceptKeyResult; // set based on the interception result 142 mutable nsecs_t interceptKeyWakeupTime; // used with INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER 143 mutable int32_t flags; 144 // TODO(b/328618922): Refactor key repeat generation to make repeatCount non-mutable. 145 mutable int32_t repeatCount; 146 147 KeyEntry(int32_t id, std::shared_ptr<InjectionState> injectionState, nsecs_t eventTime, 148 int32_t deviceId, uint32_t source, ui::LogicalDisplayId 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); 151 std::string getDescription() const override; 152 }; 153 154 std::ostream& operator<<(std::ostream& out, const KeyEntry& motionEntry); 155 156 struct MotionEntry : EventEntry { 157 int32_t deviceId; 158 uint32_t source; 159 ui::LogicalDisplayId displayId; 160 int32_t action; 161 int32_t actionButton; 162 int32_t flags; 163 int32_t metaState; 164 int32_t buttonState; 165 MotionClassification classification; 166 int32_t edgeFlags; 167 float xPrecision; 168 float yPrecision; 169 float xCursorPosition; 170 float yCursorPosition; 171 nsecs_t downTime; 172 std::vector<PointerProperties> pointerProperties; 173 std::vector<PointerCoords> pointerCoords; 174 std::unique_ptr<trace::EventTrackerInterface> traceTracker; 175 getPointerCountMotionEntry176 size_t getPointerCount() const { return pointerProperties.size(); } 177 178 MotionEntry(int32_t id, std::shared_ptr<InjectionState> injectionState, nsecs_t eventTime, 179 int32_t deviceId, uint32_t source, ui::LogicalDisplayId 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, 184 const std::vector<PointerProperties>& pointerProperties, 185 const std::vector<PointerCoords>& pointerCoords); 186 std::string getDescription() const override; 187 }; 188 189 std::ostream& operator<<(std::ostream& out, const MotionEntry& motionEntry); 190 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 }; 207 208 struct TouchModeEntry : EventEntry { 209 bool inTouchMode; 210 ui::LogicalDisplayId displayId; 211 212 TouchModeEntry(int32_t id, nsecs_t eventTime, bool inTouchMode, ui::LogicalDisplayId displayId); 213 std::string getDescription() const override; 214 }; 215 216 // Tracks the progress of dispatching a particular event to a particular connection. 217 struct DispatchEntry { 218 const uint32_t seq; // unique sequence number, never 0 219 220 std::shared_ptr<const EventEntry> eventEntry; // the event to dispatch 221 const ftl::Flags<InputTargetFlags> targetFlags; 222 ui::Transform transform; 223 ui::Transform rawTransform; 224 float globalScaleFactor; 225 // Both deliveryTime and timeoutTime are only populated when the entry is sent to the app, 226 // and will be undefined before that. 227 nsecs_t deliveryTime; // time when the event was actually delivered 228 // An ANR will be triggered if a response for this entry is not received by timeoutTime 229 nsecs_t timeoutTime; 230 231 int32_t resolvedFlags; 232 233 // Information about the dispatch window used for tracing. We avoid holding a window handle 234 // here because information in a window handle may be dynamically updated within the lifespan 235 // of this dispatch entry. 236 gui::Uid targetUid; 237 int64_t vsyncId; 238 // The window that this event is targeting. The only case when this windowId is not populated 239 // is when dispatching an event to a global monitor. 240 std::optional<int32_t> windowId; 241 242 DispatchEntry(std::shared_ptr<const EventEntry> eventEntry, 243 ftl::Flags<InputTargetFlags> targetFlags, const ui::Transform& transform, 244 const ui::Transform& rawTransform, float globalScaleFactor, gui::Uid targetUid, 245 int64_t vsyncId, std::optional<int32_t> windowId); 246 DispatchEntry(const DispatchEntry&) = delete; 247 DispatchEntry& operator=(const DispatchEntry&) = delete; 248 hasForegroundTargetDispatchEntry249 inline bool hasForegroundTarget() const { 250 return targetFlags.test(InputTargetFlags::FOREGROUND); 251 } 252 isSplitDispatchEntry253 inline bool isSplit() const { return targetFlags.test(InputTargetFlags::SPLIT); } 254 255 private: 256 static volatile int32_t sNextSeqAtomic; 257 258 static uint32_t nextSeq(); 259 }; 260 261 std::ostream& operator<<(std::ostream& out, const DispatchEntry& entry); 262 263 VerifiedKeyEvent verifiedKeyEventFromKeyEntry(const KeyEntry& entry); 264 VerifiedMotionEvent verifiedMotionEventFromMotionEntry(const MotionEntry& entry, 265 const ui::Transform& rawTransform); 266 267 } // namespace android::inputdispatcher 268