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 "InputTarget.h" 21 22 #include <gui/InputApplication.h> 23 #include <input/Input.h> 24 #include <stdint.h> 25 #include <utils/Timers.h> 26 #include <functional> 27 #include <string> 28 29 namespace android::inputdispatcher { 30 31 struct EventEntry { 32 enum class Type { 33 CONFIGURATION_CHANGED, 34 DEVICE_RESET, 35 FOCUS, 36 KEY, 37 MOTION, 38 SENSOR, 39 POINTER_CAPTURE_CHANGED, 40 DRAG, 41 TOUCH_MODE_CHANGED, 42 43 ftl_last = TOUCH_MODE_CHANGED 44 }; 45 46 int32_t id; 47 Type type; 48 nsecs_t eventTime; 49 uint32_t policyFlags; 50 InjectionState* injectionState; 51 52 bool dispatchInProgress; // initially false, set to true while dispatching 53 54 /** 55 * Injected keys are events from an external (probably untrusted) application 56 * and are not related to real hardware state. They come in via 57 * InputDispatcher::injectInputEvent, which sets policy flag POLICY_FLAG_INJECTED. 58 */ isInjectedEventEntry59 inline bool isInjected() const { return injectionState != nullptr; } 60 61 /** 62 * Synthesized events are either injected events, or events that come 63 * from real hardware, but aren't directly attributable to a specific hardware event. 64 * Key repeat is a synthesized event, because it is related to an actual hardware state 65 * (a key is currently pressed), but the repeat itself is generated by the framework. 66 */ isSynthesizedEventEntry67 inline bool isSynthesized() const { 68 return isInjected() || IdGenerator::getSource(id) != IdGenerator::Source::INPUT_READER; 69 } 70 71 virtual std::string getDescription() const = 0; 72 73 EventEntry(int32_t id, Type type, nsecs_t eventTime, uint32_t policyFlags); 74 virtual ~EventEntry(); 75 76 protected: 77 void releaseInjectionState(); 78 }; 79 80 struct ConfigurationChangedEntry : EventEntry { 81 explicit ConfigurationChangedEntry(int32_t id, nsecs_t eventTime); 82 std::string getDescription() const override; 83 84 ~ConfigurationChangedEntry() override; 85 }; 86 87 struct DeviceResetEntry : EventEntry { 88 int32_t deviceId; 89 90 DeviceResetEntry(int32_t id, nsecs_t eventTime, int32_t deviceId); 91 std::string getDescription() const override; 92 93 ~DeviceResetEntry() override; 94 }; 95 96 struct FocusEntry : EventEntry { 97 sp<IBinder> connectionToken; 98 bool hasFocus; 99 std::string reason; 100 101 FocusEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool hasFocus, 102 const std::string& reason); 103 std::string getDescription() const override; 104 105 ~FocusEntry() override; 106 }; 107 108 struct PointerCaptureChangedEntry : EventEntry { 109 const PointerCaptureRequest pointerCaptureRequest; 110 111 PointerCaptureChangedEntry(int32_t id, nsecs_t eventTime, const PointerCaptureRequest&); 112 std::string getDescription() const override; 113 114 ~PointerCaptureChangedEntry() override; 115 }; 116 117 struct DragEntry : EventEntry { 118 sp<IBinder> connectionToken; 119 bool isExiting; 120 float x, y; 121 122 DragEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool isExiting, float x, 123 float y); 124 std::string getDescription() const override; 125 126 ~DragEntry() override; 127 }; 128 129 struct KeyEntry : EventEntry { 130 int32_t deviceId; 131 uint32_t source; 132 int32_t displayId; 133 int32_t action; 134 int32_t flags; 135 int32_t keyCode; 136 int32_t scanCode; 137 int32_t metaState; 138 int32_t repeatCount; 139 nsecs_t downTime; 140 141 bool syntheticRepeat; // set to true for synthetic key repeats 142 143 enum class InterceptKeyResult { 144 UNKNOWN, 145 SKIP, 146 CONTINUE, 147 TRY_AGAIN_LATER, 148 }; 149 InterceptKeyResult interceptKeyResult; // set based on the interception result 150 nsecs_t interceptKeyWakeupTime; // used with INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER 151 152 KeyEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source, int32_t displayId, 153 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode, 154 int32_t metaState, int32_t repeatCount, nsecs_t downTime); 155 std::string getDescription() const override; 156 void recycle(); 157 158 ~KeyEntry() override; 159 }; 160 161 struct MotionEntry : EventEntry { 162 int32_t deviceId; 163 uint32_t source; 164 int32_t displayId; 165 int32_t action; 166 int32_t actionButton; 167 int32_t flags; 168 int32_t metaState; 169 int32_t buttonState; 170 MotionClassification classification; 171 int32_t edgeFlags; 172 float xPrecision; 173 float yPrecision; 174 float xCursorPosition; 175 float yCursorPosition; 176 nsecs_t downTime; 177 uint32_t pointerCount; 178 PointerProperties pointerProperties[MAX_POINTERS]; 179 PointerCoords pointerCoords[MAX_POINTERS]; 180 181 MotionEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source, int32_t displayId, 182 uint32_t policyFlags, int32_t action, int32_t actionButton, int32_t flags, 183 int32_t metaState, int32_t buttonState, MotionClassification classification, 184 int32_t edgeFlags, float xPrecision, float yPrecision, float xCursorPosition, 185 float yCursorPosition, nsecs_t downTime, uint32_t pointerCount, 186 const PointerProperties* pointerProperties, const PointerCoords* pointerCoords); 187 std::string getDescription() const override; 188 189 ~MotionEntry() override; 190 }; 191 192 struct SensorEntry : EventEntry { 193 int32_t deviceId; 194 uint32_t source; 195 InputDeviceSensorType sensorType; 196 InputDeviceSensorAccuracy accuracy; 197 bool accuracyChanged; 198 nsecs_t hwTimestamp; 199 200 std::vector<float> values; 201 202 SensorEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source, 203 uint32_t policyFlags, nsecs_t hwTimestamp, InputDeviceSensorType sensorType, 204 InputDeviceSensorAccuracy accuracy, bool accuracyChanged, 205 std::vector<float> values); 206 std::string getDescription() const override; 207 208 ~SensorEntry() override; 209 }; 210 211 struct TouchModeEntry : EventEntry { 212 bool inTouchMode; 213 int32_t displayId; 214 215 TouchModeEntry(int32_t id, nsecs_t eventTime, bool inTouchMode, int32_t displayId); 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 ftl::Flags<InputTarget::Flags> 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, 242 ftl::Flags<InputTarget::Flags> targetFlags, const ui::Transform& transform, 243 const ui::Transform& rawTransform, float globalScaleFactor); 244 hasForegroundTargetDispatchEntry245 inline bool hasForegroundTarget() const { 246 return targetFlags.test(InputTarget::Flags::FOREGROUND); 247 } 248 isSplitDispatchEntry249 inline bool isSplit() const { return targetFlags.test(InputTarget::Flags::SPLIT); } 250 251 private: 252 static volatile int32_t sNextSeqAtomic; 253 254 static uint32_t nextSeq(); 255 }; 256 257 std::ostream& operator<<(std::ostream& out, const DispatchEntry& entry); 258 259 VerifiedKeyEvent verifiedKeyEventFromKeyEntry(const KeyEntry& entry); 260 VerifiedMotionEvent verifiedMotionEventFromMotionEntry(const MotionEntry& entry, 261 const ui::Transform& rawTransform); 262 263 } // namespace android::inputdispatcher 264