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 | |
Prabir Pradhan | e3da4bb | 2023-04-05 23:51:23 +0000 | [diff] [blame] | 32 | // --- NotifyInputDevicesChangedArgs --- |
| 33 | |
| 34 | NotifyInputDevicesChangedArgs::NotifyInputDevicesChangedArgs(int32_t id, |
| 35 | std::vector<InputDeviceInfo> infos) |
| 36 | : id(id), inputDeviceInfos(std::move(infos)) {} |
| 37 | |
Siarhei Vishniakou | 7851303 | 2022-09-15 18:42:05 -0700 | [diff] [blame] | 38 | // --- NotifyKeyArgs --- |
| 39 | |
| 40 | NotifyKeyArgs::NotifyKeyArgs(int32_t id, nsecs_t eventTime, nsecs_t readTime, int32_t deviceId, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 41 | uint32_t source, ui::LogicalDisplayId displayId, uint32_t policyFlags, |
Siarhei Vishniakou | 7851303 | 2022-09-15 18:42:05 -0700 | [diff] [blame] | 42 | int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode, |
| 43 | int32_t metaState, nsecs_t downTime) |
| 44 | : id(id), |
| 45 | eventTime(eventTime), |
| 46 | deviceId(deviceId), |
| 47 | source(source), |
| 48 | displayId(displayId), |
| 49 | policyFlags(policyFlags), |
| 50 | action(action), |
| 51 | flags(flags), |
| 52 | keyCode(keyCode), |
| 53 | scanCode(scanCode), |
| 54 | metaState(metaState), |
| 55 | downTime(downTime), |
| 56 | readTime(readTime) {} |
| 57 | |
| 58 | // --- NotifyMotionArgs --- |
| 59 | |
| 60 | NotifyMotionArgs::NotifyMotionArgs( |
| 61 | int32_t id, nsecs_t eventTime, nsecs_t readTime, int32_t deviceId, uint32_t source, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 62 | ui::LogicalDisplayId displayId, uint32_t policyFlags, int32_t action, int32_t actionButton, |
Siarhei Vishniakou | 7851303 | 2022-09-15 18:42:05 -0700 | [diff] [blame] | 63 | int32_t flags, int32_t metaState, int32_t buttonState, MotionClassification classification, |
| 64 | int32_t edgeFlags, uint32_t pointerCount, const PointerProperties* pointerProperties, |
| 65 | const PointerCoords* pointerCoords, float xPrecision, float yPrecision, |
| 66 | float xCursorPosition, float yCursorPosition, nsecs_t downTime, |
| 67 | const std::vector<TouchVideoFrame>& videoFrames) |
| 68 | : id(id), |
| 69 | eventTime(eventTime), |
| 70 | deviceId(deviceId), |
| 71 | source(source), |
| 72 | displayId(displayId), |
| 73 | policyFlags(policyFlags), |
| 74 | action(action), |
| 75 | actionButton(actionButton), |
| 76 | flags(flags), |
| 77 | metaState(metaState), |
| 78 | buttonState(buttonState), |
| 79 | classification(classification), |
| 80 | edgeFlags(edgeFlags), |
Siarhei Vishniakou | 7851303 | 2022-09-15 18:42:05 -0700 | [diff] [blame] | 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++) { |
Siarhei Vishniakou | 73e6d37 | 2023-07-06 18:07:21 -0700 | [diff] [blame] | 89 | this->pointerProperties.emplace_back(pointerProperties[i]); |
| 90 | this->pointerCoords.emplace_back(pointerCoords[i]); |
Siarhei Vishniakou | 7851303 | 2022-09-15 18:42:05 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | static inline bool isCursorPositionEqual(float lhs, float rhs) { |
| 95 | return (isnan(lhs) && isnan(rhs)) || lhs == rhs; |
| 96 | } |
| 97 | |
| 98 | bool NotifyMotionArgs::operator==(const NotifyMotionArgs& rhs) const { |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 99 | return id == rhs.id && eventTime == rhs.eventTime && readTime == rhs.readTime && |
Siarhei Vishniakou | 7851303 | 2022-09-15 18:42:05 -0700 | [diff] [blame] | 100 | deviceId == rhs.deviceId && source == rhs.source && displayId == rhs.displayId && |
| 101 | policyFlags == rhs.policyFlags && action == rhs.action && |
| 102 | actionButton == rhs.actionButton && flags == rhs.flags && metaState == rhs.metaState && |
| 103 | buttonState == rhs.buttonState && classification == rhs.classification && |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 104 | edgeFlags == rhs.edgeFlags && pointerProperties == rhs.pointerProperties && |
| 105 | pointerCoords == rhs.pointerCoords && xPrecision == rhs.xPrecision && |
| 106 | yPrecision == rhs.yPrecision && |
Siarhei Vishniakou | 7851303 | 2022-09-15 18:42:05 -0700 | [diff] [blame] | 107 | isCursorPositionEqual(xCursorPosition, rhs.xCursorPosition) && |
| 108 | isCursorPositionEqual(yCursorPosition, rhs.yCursorPosition) && |
| 109 | downTime == rhs.downTime && videoFrames == rhs.videoFrames; |
Siarhei Vishniakou | 7851303 | 2022-09-15 18:42:05 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | std::string NotifyMotionArgs::dump() const { |
| 113 | std::string coords; |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 114 | for (uint32_t i = 0; i < getPointerCount(); i++) { |
Siarhei Vishniakou | 7851303 | 2022-09-15 18:42:05 -0700 | [diff] [blame] | 115 | if (!coords.empty()) { |
| 116 | coords += ", "; |
| 117 | } |
| 118 | coords += StringPrintf("{%" PRIu32 ": ", i); |
| 119 | coords += |
| 120 | StringPrintf("id=%" PRIu32 " x=%.1f y=%.1f pressure=%.1f", pointerProperties[i].id, |
| 121 | pointerCoords[i].getX(), pointerCoords[i].getY(), |
| 122 | pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE)); |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 123 | const ToolType toolType = pointerProperties[i].toolType; |
| 124 | if (toolType != ToolType::FINGER) { |
| 125 | coords += StringPrintf(" toolType=%s", ftl::enum_string(toolType).c_str()); |
Siarhei Vishniakou | 7851303 | 2022-09-15 18:42:05 -0700 | [diff] [blame] | 126 | } |
| 127 | const float major = pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR); |
| 128 | const float minor = pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR); |
| 129 | const float orientation = pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION); |
| 130 | if (major != 0 || minor != 0) { |
| 131 | coords += StringPrintf(" major=%.1f minor=%.1f orientation=%.1f", major, minor, |
| 132 | orientation); |
| 133 | } |
| 134 | coords += "}"; |
| 135 | } |
| 136 | return StringPrintf("NotifyMotionArgs(id=%" PRId32 ", eventTime=%" PRId64 ", deviceId=%" PRId32 |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 137 | ", source=%s, action=%s, pointerCount=%zu pointers=%s, flags=0x%08x)", |
Siarhei Vishniakou | 7851303 | 2022-09-15 18:42:05 -0700 | [diff] [blame] | 138 | id, eventTime, deviceId, inputEventSourceToString(source).c_str(), |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 139 | MotionEvent::actionToString(action).c_str(), getPointerCount(), |
| 140 | coords.c_str(), flags); |
Siarhei Vishniakou | 7851303 | 2022-09-15 18:42:05 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | // --- NotifySwitchArgs --- |
| 144 | |
| 145 | NotifySwitchArgs::NotifySwitchArgs(int32_t id, nsecs_t eventTime, uint32_t policyFlags, |
| 146 | uint32_t switchValues, uint32_t switchMask) |
| 147 | : id(id), |
| 148 | eventTime(eventTime), |
| 149 | policyFlags(policyFlags), |
| 150 | switchValues(switchValues), |
| 151 | switchMask(switchMask) {} |
| 152 | |
| 153 | // --- NotifySensorArgs --- |
| 154 | |
| 155 | NotifySensorArgs::NotifySensorArgs(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source, |
| 156 | InputDeviceSensorType sensorType, |
| 157 | InputDeviceSensorAccuracy accuracy, bool accuracyChanged, |
| 158 | nsecs_t hwTimestamp, std::vector<float> values) |
| 159 | : id(id), |
| 160 | eventTime(eventTime), |
| 161 | deviceId(deviceId), |
| 162 | source(source), |
| 163 | sensorType(sensorType), |
| 164 | accuracy(accuracy), |
| 165 | accuracyChanged(accuracyChanged), |
| 166 | hwTimestamp(hwTimestamp), |
| 167 | values(std::move(values)) {} |
| 168 | |
| 169 | // --- NotifyVibratorStateArgs --- |
| 170 | |
| 171 | NotifyVibratorStateArgs::NotifyVibratorStateArgs(int32_t id, nsecs_t eventTime, int32_t deviceId, |
| 172 | bool isOn) |
| 173 | : id(id), eventTime(eventTime), deviceId(deviceId), isOn(isOn) {} |
| 174 | |
| 175 | // --- NotifyDeviceResetArgs --- |
| 176 | |
| 177 | NotifyDeviceResetArgs::NotifyDeviceResetArgs(int32_t id, nsecs_t eventTime, int32_t deviceId) |
| 178 | : id(id), eventTime(eventTime), deviceId(deviceId) {} |
| 179 | |
| 180 | // --- NotifyPointerCaptureChangedArgs --- |
| 181 | |
| 182 | NotifyPointerCaptureChangedArgs::NotifyPointerCaptureChangedArgs( |
| 183 | int32_t id, nsecs_t eventTime, const PointerCaptureRequest& request) |
| 184 | : id(id), eventTime(eventTime), request(request) {} |
| 185 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 186 | // Helper to std::visit with lambdas. |
| 187 | template <typename... V> |
AdityaK | 1c6fe00 | 2023-10-23 10:52:32 -0700 | [diff] [blame] | 188 | struct Visitor : V... { using V::operator()...; }; |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 189 | // explicit deduction guide (not needed as of C++20) |
| 190 | template <typename... V> |
| 191 | Visitor(V...) -> Visitor<V...>; |
| 192 | |
| 193 | const char* toString(const NotifyArgs& args) { |
| 194 | Visitor toStringVisitor{ |
Prabir Pradhan | e3da4bb | 2023-04-05 23:51:23 +0000 | [diff] [blame] | 195 | [&](const NotifyInputDevicesChangedArgs&) { return "NotifyInputDevicesChangedArgs"; }, |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 196 | [&](const NotifyKeyArgs&) { return "NotifyKeyArgs"; }, |
| 197 | [&](const NotifyMotionArgs&) { return "NotifyMotionArgs"; }, |
| 198 | [&](const NotifySensorArgs&) { return "NotifySensorArgs"; }, |
| 199 | [&](const NotifySwitchArgs&) { return "NotifySwitchArgs"; }, |
| 200 | [&](const NotifyDeviceResetArgs&) { return "NotifyDeviceResetArgs"; }, |
| 201 | [&](const NotifyPointerCaptureChangedArgs&) { |
| 202 | return "NotifyPointerCaptureChangedArgs"; |
| 203 | }, |
| 204 | [&](const NotifyVibratorStateArgs&) { return "NotifyVibratorStateArgs"; }, |
| 205 | }; |
| 206 | return std::visit(toStringVisitor, args); |
| 207 | } |
| 208 | |
Siarhei Vishniakou | 7851303 | 2022-09-15 18:42:05 -0700 | [diff] [blame] | 209 | } // namespace android |