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 | #include "Entry.h" |
| 18 | |
| 19 | #include "Connection.h" |
| 20 | |
Ashwini Oruganti | d399a52 | 2019-10-10 11:16:45 -0700 | [diff] [blame] | 21 | #include <android-base/properties.h> |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 22 | #include <android-base/stringprintf.h> |
| 23 | #include <cutils/atomic.h> |
| 24 | #include <inttypes.h> |
| 25 | |
Ashwini Oruganti | d399a52 | 2019-10-10 11:16:45 -0700 | [diff] [blame] | 26 | using android::base::GetBoolProperty; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 27 | using android::base::StringPrintf; |
| 28 | |
| 29 | namespace android::inputdispatcher { |
| 30 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 31 | VerifiedKeyEvent verifiedKeyEventFromKeyEntry(const KeyEntry& entry) { |
| 32 | return {{VerifiedInputEvent::Type::KEY, entry.deviceId, entry.eventTime, entry.source, |
| 33 | entry.displayId}, |
| 34 | entry.action, |
| 35 | entry.downTime, |
| 36 | entry.flags & VERIFIED_KEY_EVENT_FLAGS, |
| 37 | entry.keyCode, |
| 38 | entry.scanCode, |
| 39 | entry.metaState, |
| 40 | entry.repeatCount}; |
| 41 | } |
| 42 | |
| 43 | VerifiedMotionEvent verifiedMotionEventFromMotionEntry(const MotionEntry& entry) { |
| 44 | const float rawX = entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X); |
| 45 | const float rawY = entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y); |
| 46 | const int actionMasked = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 47 | return {{VerifiedInputEvent::Type::MOTION, entry.deviceId, entry.eventTime, entry.source, |
| 48 | entry.displayId}, |
| 49 | rawX, |
| 50 | rawY, |
| 51 | actionMasked, |
| 52 | entry.downTime, |
| 53 | entry.flags & VERIFIED_MOTION_EVENT_FLAGS, |
| 54 | entry.metaState, |
| 55 | entry.buttonState}; |
| 56 | } |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 57 | |
| 58 | // --- EventEntry --- |
| 59 | |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 60 | EventEntry::EventEntry(int32_t id, Type type, nsecs_t eventTime, uint32_t policyFlags) |
| 61 | : id(id), |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 62 | refCount(1), |
| 63 | type(type), |
| 64 | eventTime(eventTime), |
| 65 | policyFlags(policyFlags), |
| 66 | injectionState(nullptr), |
| 67 | dispatchInProgress(false) {} |
| 68 | |
| 69 | EventEntry::~EventEntry() { |
| 70 | releaseInjectionState(); |
| 71 | } |
| 72 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 73 | std::string EventEntry::getDescription() const { |
| 74 | std::string result; |
| 75 | appendDescription(result); |
| 76 | return result; |
| 77 | } |
| 78 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 79 | void EventEntry::release() { |
| 80 | refCount -= 1; |
| 81 | if (refCount == 0) { |
| 82 | delete this; |
| 83 | } else { |
| 84 | ALOG_ASSERT(refCount > 0); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void EventEntry::releaseInjectionState() { |
| 89 | if (injectionState) { |
| 90 | injectionState->release(); |
| 91 | injectionState = nullptr; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // --- ConfigurationChangedEntry --- |
| 96 | |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 97 | ConfigurationChangedEntry::ConfigurationChangedEntry(int32_t id, nsecs_t eventTime) |
| 98 | : EventEntry(id, Type::CONFIGURATION_CHANGED, eventTime, 0) {} |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 99 | |
| 100 | ConfigurationChangedEntry::~ConfigurationChangedEntry() {} |
| 101 | |
| 102 | void ConfigurationChangedEntry::appendDescription(std::string& msg) const { |
| 103 | msg += StringPrintf("ConfigurationChangedEvent(), policyFlags=0x%08x", policyFlags); |
| 104 | } |
| 105 | |
| 106 | // --- DeviceResetEntry --- |
| 107 | |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 108 | DeviceResetEntry::DeviceResetEntry(int32_t id, nsecs_t eventTime, int32_t deviceId) |
| 109 | : EventEntry(id, Type::DEVICE_RESET, eventTime, 0), deviceId(deviceId) {} |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 110 | |
| 111 | DeviceResetEntry::~DeviceResetEntry() {} |
| 112 | |
| 113 | void DeviceResetEntry::appendDescription(std::string& msg) const { |
| 114 | msg += StringPrintf("DeviceResetEvent(deviceId=%d), policyFlags=0x%08x", deviceId, policyFlags); |
| 115 | } |
| 116 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 117 | // --- FocusEntry --- |
| 118 | |
| 119 | // Focus notifications always go to apps, so set the flag POLICY_FLAG_PASS_TO_USER for all entries |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 120 | FocusEntry::FocusEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool hasFocus) |
| 121 | : EventEntry(id, Type::FOCUS, eventTime, POLICY_FLAG_PASS_TO_USER), |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 122 | connectionToken(connectionToken), |
| 123 | hasFocus(hasFocus) {} |
| 124 | |
| 125 | FocusEntry::~FocusEntry() {} |
| 126 | |
| 127 | void FocusEntry::appendDescription(std::string& msg) const { |
| 128 | msg += StringPrintf("FocusEvent(hasFocus=%s)", hasFocus ? "true" : "false"); |
| 129 | } |
| 130 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 131 | // --- KeyEntry --- |
| 132 | |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 133 | KeyEntry::KeyEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source, |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 134 | int32_t displayId, uint32_t policyFlags, int32_t action, int32_t flags, |
| 135 | int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount, |
| 136 | nsecs_t downTime) |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 137 | : EventEntry(id, Type::KEY, eventTime, policyFlags), |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 138 | deviceId(deviceId), |
| 139 | source(source), |
| 140 | displayId(displayId), |
| 141 | action(action), |
| 142 | flags(flags), |
| 143 | keyCode(keyCode), |
| 144 | scanCode(scanCode), |
| 145 | metaState(metaState), |
| 146 | repeatCount(repeatCount), |
| 147 | downTime(downTime), |
| 148 | syntheticRepeat(false), |
| 149 | interceptKeyResult(KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN), |
| 150 | interceptKeyWakeupTime(0) {} |
| 151 | |
| 152 | KeyEntry::~KeyEntry() {} |
| 153 | |
| 154 | void KeyEntry::appendDescription(std::string& msg) const { |
Ashwini Oruganti | d399a52 | 2019-10-10 11:16:45 -0700 | [diff] [blame] | 155 | msg += StringPrintf("KeyEvent"); |
| 156 | if (!GetBoolProperty("ro.debuggable", false)) { |
| 157 | return; |
| 158 | } |
| 159 | msg += StringPrintf("(deviceId=%d, source=0x%08x, displayId=%" PRId32 ", action=%s, " |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 160 | "flags=0x%08x, keyCode=%d, scanCode=%d, metaState=0x%08x, " |
| 161 | "repeatCount=%d), policyFlags=0x%08x", |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 162 | deviceId, source, displayId, KeyEvent::actionToString(action), flags, |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 163 | keyCode, scanCode, metaState, repeatCount, policyFlags); |
| 164 | } |
| 165 | |
| 166 | void KeyEntry::recycle() { |
| 167 | releaseInjectionState(); |
| 168 | |
| 169 | dispatchInProgress = false; |
| 170 | syntheticRepeat = false; |
| 171 | interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 172 | interceptKeyWakeupTime = 0; |
| 173 | } |
| 174 | |
| 175 | // --- MotionEntry --- |
| 176 | |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 177 | MotionEntry::MotionEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source, |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 178 | int32_t displayId, uint32_t policyFlags, int32_t action, |
| 179 | int32_t actionButton, int32_t flags, int32_t metaState, |
| 180 | int32_t buttonState, MotionClassification classification, |
| 181 | int32_t edgeFlags, float xPrecision, float yPrecision, |
| 182 | float xCursorPosition, float yCursorPosition, nsecs_t downTime, |
| 183 | uint32_t pointerCount, const PointerProperties* pointerProperties, |
| 184 | const PointerCoords* pointerCoords, float xOffset, float yOffset) |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 185 | : EventEntry(id, Type::MOTION, eventTime, policyFlags), |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 186 | eventTime(eventTime), |
| 187 | deviceId(deviceId), |
| 188 | source(source), |
| 189 | displayId(displayId), |
| 190 | action(action), |
| 191 | actionButton(actionButton), |
| 192 | flags(flags), |
| 193 | metaState(metaState), |
| 194 | buttonState(buttonState), |
| 195 | classification(classification), |
| 196 | edgeFlags(edgeFlags), |
| 197 | xPrecision(xPrecision), |
| 198 | yPrecision(yPrecision), |
| 199 | xCursorPosition(xCursorPosition), |
| 200 | yCursorPosition(yCursorPosition), |
| 201 | downTime(downTime), |
| 202 | pointerCount(pointerCount) { |
| 203 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 204 | this->pointerProperties[i].copyFrom(pointerProperties[i]); |
| 205 | this->pointerCoords[i].copyFrom(pointerCoords[i]); |
| 206 | if (xOffset || yOffset) { |
| 207 | this->pointerCoords[i].applyOffset(xOffset, yOffset); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | MotionEntry::~MotionEntry() {} |
| 213 | |
| 214 | void MotionEntry::appendDescription(std::string& msg) const { |
Ashwini Oruganti | d399a52 | 2019-10-10 11:16:45 -0700 | [diff] [blame] | 215 | msg += StringPrintf("MotionEvent"); |
| 216 | if (!GetBoolProperty("ro.debuggable", false)) { |
| 217 | return; |
| 218 | } |
| 219 | msg += StringPrintf("(deviceId=%d, source=0x%08x, displayId=%" PRId32 |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 220 | ", action=%s, actionButton=0x%08x, flags=0x%08x, metaState=0x%08x, " |
| 221 | "buttonState=0x%08x, " |
| 222 | "classification=%s, edgeFlags=0x%08x, xPrecision=%.1f, yPrecision=%.1f, " |
| 223 | "xCursorPosition=%0.1f, yCursorPosition=%0.1f, pointers=[", |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 224 | deviceId, source, displayId, MotionEvent::actionToString(action), |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 225 | actionButton, flags, metaState, buttonState, |
| 226 | motionClassificationToString(classification), edgeFlags, xPrecision, |
| 227 | yPrecision, xCursorPosition, yCursorPosition); |
| 228 | |
| 229 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 230 | if (i) { |
| 231 | msg += ", "; |
| 232 | } |
| 233 | msg += StringPrintf("%d: (%.1f, %.1f)", pointerProperties[i].id, pointerCoords[i].getX(), |
| 234 | pointerCoords[i].getY()); |
| 235 | } |
| 236 | msg += StringPrintf("]), policyFlags=0x%08x", policyFlags); |
| 237 | } |
| 238 | |
| 239 | // --- DispatchEntry --- |
| 240 | |
| 241 | volatile int32_t DispatchEntry::sNextSeqAtomic; |
| 242 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame^] | 243 | DispatchEntry::DispatchEntry(EventEntry* eventEntry, int32_t targetFlags, ui::Transform transform, |
| 244 | float globalScaleFactor) |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 245 | : seq(nextSeq()), |
| 246 | eventEntry(eventEntry), |
| 247 | targetFlags(targetFlags), |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame^] | 248 | transform(transform), |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 249 | globalScaleFactor(globalScaleFactor), |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 250 | deliveryTime(0), |
| 251 | resolvedAction(0), |
| 252 | resolvedFlags(0) { |
| 253 | eventEntry->refCount += 1; |
| 254 | } |
| 255 | |
| 256 | DispatchEntry::~DispatchEntry() { |
| 257 | eventEntry->release(); |
| 258 | } |
| 259 | |
| 260 | uint32_t DispatchEntry::nextSeq() { |
| 261 | // Sequence number 0 is reserved and will never be returned. |
| 262 | uint32_t seq; |
| 263 | do { |
| 264 | seq = android_atomic_inc(&sNextSeqAtomic); |
| 265 | } while (!seq); |
| 266 | return seq; |
| 267 | } |
| 268 | |
| 269 | // --- CommandEntry --- |
| 270 | |
| 271 | CommandEntry::CommandEntry(Command command) |
| 272 | : command(command), |
| 273 | eventTime(0), |
| 274 | keyEntry(nullptr), |
| 275 | userActivityEventType(0), |
| 276 | seq(0), |
| 277 | handled(false) {} |
| 278 | |
| 279 | CommandEntry::~CommandEntry() {} |
| 280 | |
| 281 | } // namespace android::inputdispatcher |