blob: 19a4f26a1ee819578e0ce686dfdca590a6f0aa7e [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,
Linnan Li13bf76a2024-05-05 19:18:02 +080046 uint32_t source, ui::LogicalDisplayId displayId, uint32_t policyFlags,
Siarhei Vishniakou78513032022-09-15 18:42:05 -070047 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,
Linnan Li13bf76a2024-05-05 19:18:02 +080067 ui::LogicalDisplayId displayId, uint32_t policyFlags, int32_t action, int32_t actionButton,
Siarhei Vishniakou78513032022-09-15 18:42:05 -070068 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 Vishniakou78513032022-09-15 18:42:05 -070086 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 Vishniakou73e6d372023-07-06 18:07:21 -070094 this->pointerProperties.emplace_back(pointerProperties[i]);
95 this->pointerCoords.emplace_back(pointerCoords[i]);
Siarhei Vishniakou78513032022-09-15 18:42:05 -070096 }
97}
98
99static inline bool isCursorPositionEqual(float lhs, float rhs) {
100 return (isnan(lhs) && isnan(rhs)) || lhs == rhs;
101}
102
103bool NotifyMotionArgs::operator==(const NotifyMotionArgs& rhs) const {
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -0700104 return id == rhs.id && eventTime == rhs.eventTime && readTime == rhs.readTime &&
Siarhei Vishniakou78513032022-09-15 18:42:05 -0700105 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 Vishniakou3218fc02023-06-15 20:41:02 -0700109 edgeFlags == rhs.edgeFlags && pointerProperties == rhs.pointerProperties &&
110 pointerCoords == rhs.pointerCoords && xPrecision == rhs.xPrecision &&
111 yPrecision == rhs.yPrecision &&
Siarhei Vishniakou78513032022-09-15 18:42:05 -0700112 isCursorPositionEqual(xCursorPosition, rhs.xCursorPosition) &&
113 isCursorPositionEqual(yCursorPosition, rhs.yCursorPosition) &&
114 downTime == rhs.downTime && videoFrames == rhs.videoFrames;
Siarhei Vishniakou78513032022-09-15 18:42:05 -0700115}
116
117std::string NotifyMotionArgs::dump() const {
118 std::string coords;
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -0700119 for (uint32_t i = 0; i < getPointerCount(); i++) {
Siarhei Vishniakou78513032022-09-15 18:42:05 -0700120 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 Vishniakou6d73f832022-07-21 17:27:03 -0700128 const ToolType toolType = pointerProperties[i].toolType;
129 if (toolType != ToolType::FINGER) {
130 coords += StringPrintf(" toolType=%s", ftl::enum_string(toolType).c_str());
Siarhei Vishniakou78513032022-09-15 18:42:05 -0700131 }
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 Vishniakou3218fc02023-06-15 20:41:02 -0700142 ", source=%s, action=%s, pointerCount=%zu pointers=%s, flags=0x%08x)",
Siarhei Vishniakou78513032022-09-15 18:42:05 -0700143 id, eventTime, deviceId, inputEventSourceToString(source).c_str(),
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -0700144 MotionEvent::actionToString(action).c_str(), getPointerCount(),
145 coords.c_str(), flags);
Siarhei Vishniakou78513032022-09-15 18:42:05 -0700146}
147
148// --- NotifySwitchArgs ---
149
150NotifySwitchArgs::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
160NotifySensorArgs::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
176NotifyVibratorStateArgs::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
182NotifyDeviceResetArgs::NotifyDeviceResetArgs(int32_t id, nsecs_t eventTime, int32_t deviceId)
183 : id(id), eventTime(eventTime), deviceId(deviceId) {}
184
185// --- NotifyPointerCaptureChangedArgs ---
186
187NotifyPointerCaptureChangedArgs::NotifyPointerCaptureChangedArgs(
188 int32_t id, nsecs_t eventTime, const PointerCaptureRequest& request)
189 : id(id), eventTime(eventTime), request(request) {}
190
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700191// Helper to std::visit with lambdas.
192template <typename... V>
AdityaK1c6fe002023-10-23 10:52:32 -0700193struct Visitor : V... { using V::operator()...; };
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700194// explicit deduction guide (not needed as of C++20)
195template <typename... V>
196Visitor(V...) -> Visitor<V...>;
197
198const char* toString(const NotifyArgs& args) {
199 Visitor toStringVisitor{
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000200 [&](const NotifyInputDevicesChangedArgs&) { return "NotifyInputDevicesChangedArgs"; },
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700201 [&](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 Vishniakou78513032022-09-15 18:42:05 -0700215} // namespace android