| Garfield Tan | 73007b6 | 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 |  | 
|  | 21 | #include <android-base/stringprintf.h> | 
|  | 22 | #include <cutils/atomic.h> | 
|  | 23 | #include <inttypes.h> | 
|  | 24 |  | 
|  | 25 | using android::base::StringPrintf; | 
|  | 26 |  | 
|  | 27 | namespace android::inputdispatcher { | 
|  | 28 |  | 
|  | 29 | static std::string motionActionToString(int32_t action) { | 
|  | 30 | // Convert MotionEvent action to string | 
|  | 31 | switch (action & AMOTION_EVENT_ACTION_MASK) { | 
|  | 32 | case AMOTION_EVENT_ACTION_DOWN: | 
|  | 33 | return "DOWN"; | 
|  | 34 | case AMOTION_EVENT_ACTION_MOVE: | 
|  | 35 | return "MOVE"; | 
|  | 36 | case AMOTION_EVENT_ACTION_UP: | 
|  | 37 | return "UP"; | 
|  | 38 | case AMOTION_EVENT_ACTION_POINTER_DOWN: | 
|  | 39 | return "POINTER_DOWN"; | 
|  | 40 | case AMOTION_EVENT_ACTION_POINTER_UP: | 
|  | 41 | return "POINTER_UP"; | 
|  | 42 | } | 
|  | 43 | return StringPrintf("%" PRId32, action); | 
|  | 44 | } | 
|  | 45 |  | 
|  | 46 | static std::string keyActionToString(int32_t action) { | 
|  | 47 | // Convert KeyEvent action to string | 
|  | 48 | switch (action) { | 
|  | 49 | case AKEY_EVENT_ACTION_DOWN: | 
|  | 50 | return "DOWN"; | 
|  | 51 | case AKEY_EVENT_ACTION_UP: | 
|  | 52 | return "UP"; | 
|  | 53 | case AKEY_EVENT_ACTION_MULTIPLE: | 
|  | 54 | return "MULTIPLE"; | 
|  | 55 | } | 
|  | 56 | return StringPrintf("%" PRId32, action); | 
|  | 57 | } | 
|  | 58 |  | 
|  | 59 | // --- EventEntry --- | 
|  | 60 |  | 
|  | 61 | EventEntry::EventEntry(uint32_t sequenceNum, int32_t type, nsecs_t eventTime, uint32_t policyFlags) | 
|  | 62 | : sequenceNum(sequenceNum), | 
|  | 63 | refCount(1), | 
|  | 64 | type(type), | 
|  | 65 | eventTime(eventTime), | 
|  | 66 | policyFlags(policyFlags), | 
|  | 67 | injectionState(nullptr), | 
|  | 68 | dispatchInProgress(false) {} | 
|  | 69 |  | 
|  | 70 | EventEntry::~EventEntry() { | 
|  | 71 | releaseInjectionState(); | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | void EventEntry::release() { | 
|  | 75 | refCount -= 1; | 
|  | 76 | if (refCount == 0) { | 
|  | 77 | delete this; | 
|  | 78 | } else { | 
|  | 79 | ALOG_ASSERT(refCount > 0); | 
|  | 80 | } | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | void EventEntry::releaseInjectionState() { | 
|  | 84 | if (injectionState) { | 
|  | 85 | injectionState->release(); | 
|  | 86 | injectionState = nullptr; | 
|  | 87 | } | 
|  | 88 | } | 
|  | 89 |  | 
|  | 90 | // --- ConfigurationChangedEntry --- | 
|  | 91 |  | 
|  | 92 | ConfigurationChangedEntry::ConfigurationChangedEntry(uint32_t sequenceNum, nsecs_t eventTime) | 
|  | 93 | : EventEntry(sequenceNum, TYPE_CONFIGURATION_CHANGED, eventTime, 0) {} | 
|  | 94 |  | 
|  | 95 | ConfigurationChangedEntry::~ConfigurationChangedEntry() {} | 
|  | 96 |  | 
|  | 97 | void ConfigurationChangedEntry::appendDescription(std::string& msg) const { | 
|  | 98 | msg += StringPrintf("ConfigurationChangedEvent(), policyFlags=0x%08x", policyFlags); | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 | // --- DeviceResetEntry --- | 
|  | 102 |  | 
|  | 103 | DeviceResetEntry::DeviceResetEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId) | 
|  | 104 | : EventEntry(sequenceNum, TYPE_DEVICE_RESET, eventTime, 0), deviceId(deviceId) {} | 
|  | 105 |  | 
|  | 106 | DeviceResetEntry::~DeviceResetEntry() {} | 
|  | 107 |  | 
|  | 108 | void DeviceResetEntry::appendDescription(std::string& msg) const { | 
|  | 109 | msg += StringPrintf("DeviceResetEvent(deviceId=%d), policyFlags=0x%08x", deviceId, policyFlags); | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | // --- KeyEntry --- | 
|  | 113 |  | 
|  | 114 | KeyEntry::KeyEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId, uint32_t source, | 
|  | 115 | int32_t displayId, uint32_t policyFlags, int32_t action, int32_t flags, | 
|  | 116 | int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount, | 
|  | 117 | nsecs_t downTime) | 
|  | 118 | : EventEntry(sequenceNum, TYPE_KEY, eventTime, policyFlags), | 
|  | 119 | deviceId(deviceId), | 
|  | 120 | source(source), | 
|  | 121 | displayId(displayId), | 
|  | 122 | action(action), | 
|  | 123 | flags(flags), | 
|  | 124 | keyCode(keyCode), | 
|  | 125 | scanCode(scanCode), | 
|  | 126 | metaState(metaState), | 
|  | 127 | repeatCount(repeatCount), | 
|  | 128 | downTime(downTime), | 
|  | 129 | syntheticRepeat(false), | 
|  | 130 | interceptKeyResult(KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN), | 
|  | 131 | interceptKeyWakeupTime(0) {} | 
|  | 132 |  | 
|  | 133 | KeyEntry::~KeyEntry() {} | 
|  | 134 |  | 
|  | 135 | void KeyEntry::appendDescription(std::string& msg) const { | 
|  | 136 | msg += StringPrintf("KeyEvent(deviceId=%d, source=0x%08x, displayId=%" PRId32 ", action=%s, " | 
|  | 137 | "flags=0x%08x, keyCode=%d, scanCode=%d, metaState=0x%08x, " | 
|  | 138 | "repeatCount=%d), policyFlags=0x%08x", | 
|  | 139 | deviceId, source, displayId, keyActionToString(action).c_str(), flags, | 
|  | 140 | keyCode, scanCode, metaState, repeatCount, policyFlags); | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 | void KeyEntry::recycle() { | 
|  | 144 | releaseInjectionState(); | 
|  | 145 |  | 
|  | 146 | dispatchInProgress = false; | 
|  | 147 | syntheticRepeat = false; | 
|  | 148 | interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; | 
|  | 149 | interceptKeyWakeupTime = 0; | 
|  | 150 | } | 
|  | 151 |  | 
|  | 152 | // --- MotionEntry --- | 
|  | 153 |  | 
|  | 154 | MotionEntry::MotionEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId, uint32_t source, | 
|  | 155 | int32_t displayId, uint32_t policyFlags, int32_t action, | 
|  | 156 | int32_t actionButton, int32_t flags, int32_t metaState, | 
|  | 157 | int32_t buttonState, MotionClassification classification, | 
|  | 158 | int32_t edgeFlags, float xPrecision, float yPrecision, nsecs_t downTime, | 
|  | 159 | uint32_t pointerCount, const PointerProperties* pointerProperties, | 
|  | 160 | const PointerCoords* pointerCoords, float xOffset, float yOffset) | 
|  | 161 | : EventEntry(sequenceNum, TYPE_MOTION, eventTime, policyFlags), | 
|  | 162 | eventTime(eventTime), | 
|  | 163 | deviceId(deviceId), | 
|  | 164 | source(source), | 
|  | 165 | displayId(displayId), | 
|  | 166 | action(action), | 
|  | 167 | actionButton(actionButton), | 
|  | 168 | flags(flags), | 
|  | 169 | metaState(metaState), | 
|  | 170 | buttonState(buttonState), | 
|  | 171 | classification(classification), | 
|  | 172 | edgeFlags(edgeFlags), | 
|  | 173 | xPrecision(xPrecision), | 
|  | 174 | yPrecision(yPrecision), | 
|  | 175 | downTime(downTime), | 
|  | 176 | pointerCount(pointerCount) { | 
|  | 177 | for (uint32_t i = 0; i < pointerCount; i++) { | 
|  | 178 | this->pointerProperties[i].copyFrom(pointerProperties[i]); | 
|  | 179 | this->pointerCoords[i].copyFrom(pointerCoords[i]); | 
|  | 180 | if (xOffset || yOffset) { | 
|  | 181 | this->pointerCoords[i].applyOffset(xOffset, yOffset); | 
|  | 182 | } | 
|  | 183 | } | 
|  | 184 | } | 
|  | 185 |  | 
|  | 186 | MotionEntry::~MotionEntry() {} | 
|  | 187 |  | 
|  | 188 | void MotionEntry::appendDescription(std::string& msg) const { | 
|  | 189 | msg += StringPrintf("MotionEvent(deviceId=%d, source=0x%08x, displayId=%" PRId32 | 
|  | 190 | ", action=%s, actionButton=0x%08x, flags=0x%08x, metaState=0x%08x, " | 
|  | 191 | "buttonState=0x%08x, " | 
|  | 192 | "classification=%s, edgeFlags=0x%08x, xPrecision=%.1f, yPrecision=%.1f, " | 
|  | 193 | "pointers=[", | 
|  | 194 | deviceId, source, displayId, motionActionToString(action).c_str(), | 
|  | 195 | actionButton, flags, metaState, buttonState, | 
|  | 196 | motionClassificationToString(classification), edgeFlags, xPrecision, | 
|  | 197 | yPrecision); | 
|  | 198 |  | 
|  | 199 | for (uint32_t i = 0; i < pointerCount; i++) { | 
|  | 200 | if (i) { | 
|  | 201 | msg += ", "; | 
|  | 202 | } | 
|  | 203 | msg += StringPrintf("%d: (%.1f, %.1f)", pointerProperties[i].id, pointerCoords[i].getX(), | 
|  | 204 | pointerCoords[i].getY()); | 
|  | 205 | } | 
|  | 206 | msg += StringPrintf("]), policyFlags=0x%08x", policyFlags); | 
|  | 207 | } | 
|  | 208 |  | 
|  | 209 | // --- DispatchEntry --- | 
|  | 210 |  | 
|  | 211 | volatile int32_t DispatchEntry::sNextSeqAtomic; | 
|  | 212 |  | 
|  | 213 | DispatchEntry::DispatchEntry(EventEntry* eventEntry, int32_t targetFlags, float xOffset, | 
|  | 214 | float yOffset, float globalScaleFactor, float windowXScale, | 
|  | 215 | float windowYScale) | 
|  | 216 | : seq(nextSeq()), | 
|  | 217 | eventEntry(eventEntry), | 
|  | 218 | targetFlags(targetFlags), | 
|  | 219 | xOffset(xOffset), | 
|  | 220 | yOffset(yOffset), | 
|  | 221 | globalScaleFactor(globalScaleFactor), | 
|  | 222 | windowXScale(windowXScale), | 
|  | 223 | windowYScale(windowYScale), | 
|  | 224 | deliveryTime(0), | 
|  | 225 | resolvedAction(0), | 
|  | 226 | resolvedFlags(0) { | 
|  | 227 | eventEntry->refCount += 1; | 
|  | 228 | } | 
|  | 229 |  | 
|  | 230 | DispatchEntry::~DispatchEntry() { | 
|  | 231 | eventEntry->release(); | 
|  | 232 | } | 
|  | 233 |  | 
|  | 234 | uint32_t DispatchEntry::nextSeq() { | 
|  | 235 | // Sequence number 0 is reserved and will never be returned. | 
|  | 236 | uint32_t seq; | 
|  | 237 | do { | 
|  | 238 | seq = android_atomic_inc(&sNextSeqAtomic); | 
|  | 239 | } while (!seq); | 
|  | 240 | return seq; | 
|  | 241 | } | 
|  | 242 |  | 
|  | 243 | // --- CommandEntry --- | 
|  | 244 |  | 
|  | 245 | CommandEntry::CommandEntry(Command command) | 
|  | 246 | : command(command), | 
|  | 247 | eventTime(0), | 
|  | 248 | keyEntry(nullptr), | 
|  | 249 | userActivityEventType(0), | 
|  | 250 | seq(0), | 
|  | 251 | handled(false) {} | 
|  | 252 |  | 
|  | 253 | CommandEntry::~CommandEntry() {} | 
|  | 254 |  | 
|  | 255 | } // namespace android::inputdispatcher |