blob: 35d60eaaa22a658bd549e7e6e9dd164abd6c864f [file] [log] [blame]
Siarhei Vishniakou78513032022-09-15 18:42:05 -07001/*
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
28using android::base::StringPrintf;
29
30namespace android {
31
Prabir Pradhane3da4bb2023-04-05 23:51:23 +000032// --- NotifyInputDevicesChangedArgs ---
33
34NotifyInputDevicesChangedArgs::NotifyInputDevicesChangedArgs(int32_t id,
35 std::vector<InputDeviceInfo> infos)
36 : id(id), inputDeviceInfos(std::move(infos)) {}
37
Siarhei Vishniakou78513032022-09-15 18:42:05 -070038// --- NotifyConfigurationChangedArgs ---
39
40NotifyConfigurationChangedArgs::NotifyConfigurationChangedArgs(int32_t id, nsecs_t eventTime)
41 : id(id), eventTime(eventTime) {}
42
43// --- NotifyKeyArgs ---
44
45NotifyKeyArgs::NotifyKeyArgs(int32_t id, nsecs_t eventTime, nsecs_t readTime, int32_t deviceId,
46 uint32_t source, int32_t displayId, uint32_t policyFlags,
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
65NotifyMotionArgs::NotifyMotionArgs(
66 int32_t id, nsecs_t eventTime, nsecs_t readTime, int32_t deviceId, uint32_t source,
67 int32_t displayId, uint32_t policyFlags, int32_t action, int32_t actionButton,
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),
86 pointerCount(pointerCount),
87 xPrecision(xPrecision),
88 yPrecision(yPrecision),
89 xCursorPosition(xCursorPosition),
90 yCursorPosition(yCursorPosition),
91 downTime(downTime),
92 readTime(readTime),
93 videoFrames(videoFrames) {
94 for (uint32_t i = 0; i < pointerCount; i++) {
95 this->pointerProperties[i].copyFrom(pointerProperties[i]);
96 this->pointerCoords[i].copyFrom(pointerCoords[i]);
97 }
98}
99
100NotifyMotionArgs::NotifyMotionArgs(const NotifyMotionArgs& other)
101 : id(other.id),
102 eventTime(other.eventTime),
103 deviceId(other.deviceId),
104 source(other.source),
105 displayId(other.displayId),
106 policyFlags(other.policyFlags),
107 action(other.action),
108 actionButton(other.actionButton),
109 flags(other.flags),
110 metaState(other.metaState),
111 buttonState(other.buttonState),
112 classification(other.classification),
113 edgeFlags(other.edgeFlags),
114 pointerCount(other.pointerCount),
115 xPrecision(other.xPrecision),
116 yPrecision(other.yPrecision),
117 xCursorPosition(other.xCursorPosition),
118 yCursorPosition(other.yCursorPosition),
119 downTime(other.downTime),
120 readTime(other.readTime),
121 videoFrames(other.videoFrames) {
122 for (uint32_t i = 0; i < pointerCount; i++) {
123 pointerProperties[i].copyFrom(other.pointerProperties[i]);
124 pointerCoords[i].copyFrom(other.pointerCoords[i]);
125 }
126}
127
128static inline bool isCursorPositionEqual(float lhs, float rhs) {
129 return (isnan(lhs) && isnan(rhs)) || lhs == rhs;
130}
131
132bool NotifyMotionArgs::operator==(const NotifyMotionArgs& rhs) const {
133 bool equal = id == rhs.id && eventTime == rhs.eventTime && readTime == rhs.readTime &&
134 deviceId == rhs.deviceId && source == rhs.source && displayId == rhs.displayId &&
135 policyFlags == rhs.policyFlags && action == rhs.action &&
136 actionButton == rhs.actionButton && flags == rhs.flags && metaState == rhs.metaState &&
137 buttonState == rhs.buttonState && classification == rhs.classification &&
138 edgeFlags == rhs.edgeFlags &&
139 pointerCount == rhs.pointerCount
140 // PointerProperties and PointerCoords are compared separately below
141 && xPrecision == rhs.xPrecision && yPrecision == rhs.yPrecision &&
142 isCursorPositionEqual(xCursorPosition, rhs.xCursorPosition) &&
143 isCursorPositionEqual(yCursorPosition, rhs.yCursorPosition) &&
144 downTime == rhs.downTime && videoFrames == rhs.videoFrames;
145 if (!equal) {
146 return false;
147 }
148
149 for (size_t i = 0; i < pointerCount; i++) {
150 equal = pointerProperties[i] == rhs.pointerProperties[i] &&
151 pointerCoords[i] == rhs.pointerCoords[i];
152 if (!equal) {
153 return false;
154 }
155 }
156 return true;
157}
158
159std::string NotifyMotionArgs::dump() const {
160 std::string coords;
161 for (uint32_t i = 0; i < pointerCount; i++) {
162 if (!coords.empty()) {
163 coords += ", ";
164 }
165 coords += StringPrintf("{%" PRIu32 ": ", i);
166 coords +=
167 StringPrintf("id=%" PRIu32 " x=%.1f y=%.1f pressure=%.1f", pointerProperties[i].id,
168 pointerCoords[i].getX(), pointerCoords[i].getY(),
169 pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700170 const ToolType toolType = pointerProperties[i].toolType;
171 if (toolType != ToolType::FINGER) {
172 coords += StringPrintf(" toolType=%s", ftl::enum_string(toolType).c_str());
Siarhei Vishniakou78513032022-09-15 18:42:05 -0700173 }
174 const float major = pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR);
175 const float minor = pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR);
176 const float orientation = pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
177 if (major != 0 || minor != 0) {
178 coords += StringPrintf(" major=%.1f minor=%.1f orientation=%.1f", major, minor,
179 orientation);
180 }
181 coords += "}";
182 }
183 return StringPrintf("NotifyMotionArgs(id=%" PRId32 ", eventTime=%" PRId64 ", deviceId=%" PRId32
184 ", source=%s, action=%s, pointerCount=%" PRIu32
185 " pointers=%s, flags=0x%08x)",
186 id, eventTime, deviceId, inputEventSourceToString(source).c_str(),
187 MotionEvent::actionToString(action).c_str(), pointerCount, coords.c_str(),
188 flags);
189}
190
191// --- NotifySwitchArgs ---
192
193NotifySwitchArgs::NotifySwitchArgs(int32_t id, nsecs_t eventTime, uint32_t policyFlags,
194 uint32_t switchValues, uint32_t switchMask)
195 : id(id),
196 eventTime(eventTime),
197 policyFlags(policyFlags),
198 switchValues(switchValues),
199 switchMask(switchMask) {}
200
201// --- NotifySensorArgs ---
202
203NotifySensorArgs::NotifySensorArgs(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
204 InputDeviceSensorType sensorType,
205 InputDeviceSensorAccuracy accuracy, bool accuracyChanged,
206 nsecs_t hwTimestamp, std::vector<float> values)
207 : id(id),
208 eventTime(eventTime),
209 deviceId(deviceId),
210 source(source),
211 sensorType(sensorType),
212 accuracy(accuracy),
213 accuracyChanged(accuracyChanged),
214 hwTimestamp(hwTimestamp),
215 values(std::move(values)) {}
216
217// --- NotifyVibratorStateArgs ---
218
219NotifyVibratorStateArgs::NotifyVibratorStateArgs(int32_t id, nsecs_t eventTime, int32_t deviceId,
220 bool isOn)
221 : id(id), eventTime(eventTime), deviceId(deviceId), isOn(isOn) {}
222
223// --- NotifyDeviceResetArgs ---
224
225NotifyDeviceResetArgs::NotifyDeviceResetArgs(int32_t id, nsecs_t eventTime, int32_t deviceId)
226 : id(id), eventTime(eventTime), deviceId(deviceId) {}
227
228// --- NotifyPointerCaptureChangedArgs ---
229
230NotifyPointerCaptureChangedArgs::NotifyPointerCaptureChangedArgs(
231 int32_t id, nsecs_t eventTime, const PointerCaptureRequest& request)
232 : id(id), eventTime(eventTime), request(request) {}
233
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700234// Helper to std::visit with lambdas.
235template <typename... V>
AdityaK1c6fe002023-10-23 10:52:32 -0700236struct Visitor : V... { using V::operator()...; };
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700237// explicit deduction guide (not needed as of C++20)
238template <typename... V>
239Visitor(V...) -> Visitor<V...>;
240
241const char* toString(const NotifyArgs& args) {
242 Visitor toStringVisitor{
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000243 [&](const NotifyInputDevicesChangedArgs&) { return "NotifyInputDevicesChangedArgs"; },
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700244 [&](const NotifyConfigurationChangedArgs&) { return "NotifyConfigurationChangedArgs"; },
245 [&](const NotifyKeyArgs&) { return "NotifyKeyArgs"; },
246 [&](const NotifyMotionArgs&) { return "NotifyMotionArgs"; },
247 [&](const NotifySensorArgs&) { return "NotifySensorArgs"; },
248 [&](const NotifySwitchArgs&) { return "NotifySwitchArgs"; },
249 [&](const NotifyDeviceResetArgs&) { return "NotifyDeviceResetArgs"; },
250 [&](const NotifyPointerCaptureChangedArgs&) {
251 return "NotifyPointerCaptureChangedArgs";
252 },
253 [&](const NotifyVibratorStateArgs&) { return "NotifyVibratorStateArgs"; },
254 };
255 return std::visit(toStringVisitor, args);
256}
257
Siarhei Vishniakou78513032022-09-15 18:42:05 -0700258} // namespace android