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