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