Siarhei Vishniakou | 7851303 | 2022-09-15 18:42:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 | #define LOG_TAG "NotifyArgs" |
| 18 | |
| 19 | #define ATRACE_TAG ATRACE_TAG_INPUT |
| 20 | |
| 21 | #include "NotifyArgs.h" |
| 22 | |
| 23 | #include <android-base/stringprintf.h> |
| 24 | #include <android/log.h> |
| 25 | #include <math.h> |
| 26 | #include <utils/Trace.h> |
| 27 | |
| 28 | using android::base::StringPrintf; |
| 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | // --- NotifyConfigurationChangedArgs --- |
| 33 | |
| 34 | NotifyConfigurationChangedArgs::NotifyConfigurationChangedArgs(int32_t id, nsecs_t eventTime) |
| 35 | : id(id), eventTime(eventTime) {} |
| 36 | |
| 37 | // --- NotifyKeyArgs --- |
| 38 | |
| 39 | NotifyKeyArgs::NotifyKeyArgs(int32_t id, nsecs_t eventTime, nsecs_t readTime, int32_t deviceId, |
| 40 | uint32_t source, int32_t displayId, uint32_t policyFlags, |
| 41 | int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode, |
| 42 | int32_t metaState, nsecs_t downTime) |
| 43 | : id(id), |
| 44 | eventTime(eventTime), |
| 45 | deviceId(deviceId), |
| 46 | source(source), |
| 47 | displayId(displayId), |
| 48 | policyFlags(policyFlags), |
| 49 | action(action), |
| 50 | flags(flags), |
| 51 | keyCode(keyCode), |
| 52 | scanCode(scanCode), |
| 53 | metaState(metaState), |
| 54 | downTime(downTime), |
| 55 | readTime(readTime) {} |
| 56 | |
| 57 | // --- NotifyMotionArgs --- |
| 58 | |
| 59 | NotifyMotionArgs::NotifyMotionArgs( |
| 60 | int32_t id, nsecs_t eventTime, nsecs_t readTime, int32_t deviceId, uint32_t source, |
| 61 | int32_t displayId, uint32_t policyFlags, int32_t action, int32_t actionButton, |
| 62 | int32_t flags, int32_t metaState, int32_t buttonState, MotionClassification classification, |
| 63 | int32_t edgeFlags, uint32_t pointerCount, const PointerProperties* pointerProperties, |
| 64 | const PointerCoords* pointerCoords, float xPrecision, float yPrecision, |
| 65 | float xCursorPosition, float yCursorPosition, nsecs_t downTime, |
| 66 | const std::vector<TouchVideoFrame>& videoFrames) |
| 67 | : id(id), |
| 68 | eventTime(eventTime), |
| 69 | deviceId(deviceId), |
| 70 | source(source), |
| 71 | displayId(displayId), |
| 72 | policyFlags(policyFlags), |
| 73 | action(action), |
| 74 | actionButton(actionButton), |
| 75 | flags(flags), |
| 76 | metaState(metaState), |
| 77 | buttonState(buttonState), |
| 78 | classification(classification), |
| 79 | edgeFlags(edgeFlags), |
| 80 | pointerCount(pointerCount), |
| 81 | xPrecision(xPrecision), |
| 82 | yPrecision(yPrecision), |
| 83 | xCursorPosition(xCursorPosition), |
| 84 | yCursorPosition(yCursorPosition), |
| 85 | downTime(downTime), |
| 86 | readTime(readTime), |
| 87 | videoFrames(videoFrames) { |
| 88 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 89 | this->pointerProperties[i].copyFrom(pointerProperties[i]); |
| 90 | this->pointerCoords[i].copyFrom(pointerCoords[i]); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | NotifyMotionArgs::NotifyMotionArgs(const NotifyMotionArgs& other) |
| 95 | : id(other.id), |
| 96 | eventTime(other.eventTime), |
| 97 | deviceId(other.deviceId), |
| 98 | source(other.source), |
| 99 | displayId(other.displayId), |
| 100 | policyFlags(other.policyFlags), |
| 101 | action(other.action), |
| 102 | actionButton(other.actionButton), |
| 103 | flags(other.flags), |
| 104 | metaState(other.metaState), |
| 105 | buttonState(other.buttonState), |
| 106 | classification(other.classification), |
| 107 | edgeFlags(other.edgeFlags), |
| 108 | pointerCount(other.pointerCount), |
| 109 | xPrecision(other.xPrecision), |
| 110 | yPrecision(other.yPrecision), |
| 111 | xCursorPosition(other.xCursorPosition), |
| 112 | yCursorPosition(other.yCursorPosition), |
| 113 | downTime(other.downTime), |
| 114 | readTime(other.readTime), |
| 115 | videoFrames(other.videoFrames) { |
| 116 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 117 | pointerProperties[i].copyFrom(other.pointerProperties[i]); |
| 118 | pointerCoords[i].copyFrom(other.pointerCoords[i]); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | static inline bool isCursorPositionEqual(float lhs, float rhs) { |
| 123 | return (isnan(lhs) && isnan(rhs)) || lhs == rhs; |
| 124 | } |
| 125 | |
| 126 | bool NotifyMotionArgs::operator==(const NotifyMotionArgs& rhs) const { |
| 127 | bool equal = id == rhs.id && eventTime == rhs.eventTime && readTime == rhs.readTime && |
| 128 | deviceId == rhs.deviceId && source == rhs.source && displayId == rhs.displayId && |
| 129 | policyFlags == rhs.policyFlags && action == rhs.action && |
| 130 | actionButton == rhs.actionButton && flags == rhs.flags && metaState == rhs.metaState && |
| 131 | buttonState == rhs.buttonState && classification == rhs.classification && |
| 132 | edgeFlags == rhs.edgeFlags && |
| 133 | pointerCount == rhs.pointerCount |
| 134 | // PointerProperties and PointerCoords are compared separately below |
| 135 | && xPrecision == rhs.xPrecision && yPrecision == rhs.yPrecision && |
| 136 | isCursorPositionEqual(xCursorPosition, rhs.xCursorPosition) && |
| 137 | isCursorPositionEqual(yCursorPosition, rhs.yCursorPosition) && |
| 138 | downTime == rhs.downTime && videoFrames == rhs.videoFrames; |
| 139 | if (!equal) { |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | for (size_t i = 0; i < pointerCount; i++) { |
| 144 | equal = pointerProperties[i] == rhs.pointerProperties[i] && |
| 145 | pointerCoords[i] == rhs.pointerCoords[i]; |
| 146 | if (!equal) { |
| 147 | return false; |
| 148 | } |
| 149 | } |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | std::string NotifyMotionArgs::dump() const { |
| 154 | std::string coords; |
| 155 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 156 | if (!coords.empty()) { |
| 157 | coords += ", "; |
| 158 | } |
| 159 | coords += StringPrintf("{%" PRIu32 ": ", i); |
| 160 | coords += |
| 161 | StringPrintf("id=%" PRIu32 " x=%.1f y=%.1f pressure=%.1f", pointerProperties[i].id, |
| 162 | pointerCoords[i].getX(), pointerCoords[i].getY(), |
| 163 | pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE)); |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame^] | 164 | const ToolType toolType = pointerProperties[i].toolType; |
| 165 | if (toolType != ToolType::FINGER) { |
| 166 | coords += StringPrintf(" toolType=%s", ftl::enum_string(toolType).c_str()); |
Siarhei Vishniakou | 7851303 | 2022-09-15 18:42:05 -0700 | [diff] [blame] | 167 | } |
| 168 | const float major = pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR); |
| 169 | const float minor = pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR); |
| 170 | const float orientation = pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION); |
| 171 | if (major != 0 || minor != 0) { |
| 172 | coords += StringPrintf(" major=%.1f minor=%.1f orientation=%.1f", major, minor, |
| 173 | orientation); |
| 174 | } |
| 175 | coords += "}"; |
| 176 | } |
| 177 | return StringPrintf("NotifyMotionArgs(id=%" PRId32 ", eventTime=%" PRId64 ", deviceId=%" PRId32 |
| 178 | ", source=%s, action=%s, pointerCount=%" PRIu32 |
| 179 | " pointers=%s, flags=0x%08x)", |
| 180 | id, eventTime, deviceId, inputEventSourceToString(source).c_str(), |
| 181 | MotionEvent::actionToString(action).c_str(), pointerCount, coords.c_str(), |
| 182 | flags); |
| 183 | } |
| 184 | |
| 185 | // --- NotifySwitchArgs --- |
| 186 | |
| 187 | NotifySwitchArgs::NotifySwitchArgs(int32_t id, nsecs_t eventTime, uint32_t policyFlags, |
| 188 | uint32_t switchValues, uint32_t switchMask) |
| 189 | : id(id), |
| 190 | eventTime(eventTime), |
| 191 | policyFlags(policyFlags), |
| 192 | switchValues(switchValues), |
| 193 | switchMask(switchMask) {} |
| 194 | |
| 195 | // --- NotifySensorArgs --- |
| 196 | |
| 197 | NotifySensorArgs::NotifySensorArgs(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source, |
| 198 | InputDeviceSensorType sensorType, |
| 199 | InputDeviceSensorAccuracy accuracy, bool accuracyChanged, |
| 200 | nsecs_t hwTimestamp, std::vector<float> values) |
| 201 | : id(id), |
| 202 | eventTime(eventTime), |
| 203 | deviceId(deviceId), |
| 204 | source(source), |
| 205 | sensorType(sensorType), |
| 206 | accuracy(accuracy), |
| 207 | accuracyChanged(accuracyChanged), |
| 208 | hwTimestamp(hwTimestamp), |
| 209 | values(std::move(values)) {} |
| 210 | |
| 211 | // --- NotifyVibratorStateArgs --- |
| 212 | |
| 213 | NotifyVibratorStateArgs::NotifyVibratorStateArgs(int32_t id, nsecs_t eventTime, int32_t deviceId, |
| 214 | bool isOn) |
| 215 | : id(id), eventTime(eventTime), deviceId(deviceId), isOn(isOn) {} |
| 216 | |
| 217 | // --- NotifyDeviceResetArgs --- |
| 218 | |
| 219 | NotifyDeviceResetArgs::NotifyDeviceResetArgs(int32_t id, nsecs_t eventTime, int32_t deviceId) |
| 220 | : id(id), eventTime(eventTime), deviceId(deviceId) {} |
| 221 | |
| 222 | // --- NotifyPointerCaptureChangedArgs --- |
| 223 | |
| 224 | NotifyPointerCaptureChangedArgs::NotifyPointerCaptureChangedArgs( |
| 225 | int32_t id, nsecs_t eventTime, const PointerCaptureRequest& request) |
| 226 | : id(id), eventTime(eventTime), request(request) {} |
| 227 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 228 | // Helper to std::visit with lambdas. |
| 229 | template <typename... V> |
| 230 | struct Visitor : V... {}; |
| 231 | // explicit deduction guide (not needed as of C++20) |
| 232 | template <typename... V> |
| 233 | Visitor(V...) -> Visitor<V...>; |
| 234 | |
| 235 | const char* toString(const NotifyArgs& args) { |
| 236 | Visitor toStringVisitor{ |
| 237 | [&](const NotifyConfigurationChangedArgs&) { return "NotifyConfigurationChangedArgs"; }, |
| 238 | [&](const NotifyKeyArgs&) { return "NotifyKeyArgs"; }, |
| 239 | [&](const NotifyMotionArgs&) { return "NotifyMotionArgs"; }, |
| 240 | [&](const NotifySensorArgs&) { return "NotifySensorArgs"; }, |
| 241 | [&](const NotifySwitchArgs&) { return "NotifySwitchArgs"; }, |
| 242 | [&](const NotifyDeviceResetArgs&) { return "NotifyDeviceResetArgs"; }, |
| 243 | [&](const NotifyPointerCaptureChangedArgs&) { |
| 244 | return "NotifyPointerCaptureChangedArgs"; |
| 245 | }, |
| 246 | [&](const NotifyVibratorStateArgs&) { return "NotifyVibratorStateArgs"; }, |
| 247 | }; |
| 248 | return std::visit(toStringVisitor, args); |
| 249 | } |
| 250 | |
Siarhei Vishniakou | 7851303 | 2022-09-15 18:42:05 -0700 | [diff] [blame] | 251 | } // namespace android |