blob: b2680a21397b2da901b23cfce9c2e13ac547248f [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// --- NotifyKeyArgs ---
39
40NotifyKeyArgs::NotifyKeyArgs(int32_t id, nsecs_t eventTime, nsecs_t readTime, int32_t deviceId,
Linnan Li13bf76a2024-05-05 19:18:02 +080041 uint32_t source, ui::LogicalDisplayId displayId, uint32_t policyFlags,
Siarhei Vishniakou78513032022-09-15 18:42:05 -070042 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
60NotifyMotionArgs::NotifyMotionArgs(
61 int32_t id, nsecs_t eventTime, nsecs_t readTime, int32_t deviceId, uint32_t source,
Linnan Li13bf76a2024-05-05 19:18:02 +080062 ui::LogicalDisplayId displayId, uint32_t policyFlags, int32_t action, int32_t actionButton,
Siarhei Vishniakou78513032022-09-15 18:42:05 -070063 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 Vishniakou78513032022-09-15 18:42:05 -070081 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 Vishniakou73e6d372023-07-06 18:07:21 -070089 this->pointerProperties.emplace_back(pointerProperties[i]);
90 this->pointerCoords.emplace_back(pointerCoords[i]);
Siarhei Vishniakou78513032022-09-15 18:42:05 -070091 }
92}
93
94static inline bool isCursorPositionEqual(float lhs, float rhs) {
95 return (isnan(lhs) && isnan(rhs)) || lhs == rhs;
96}
97
98bool NotifyMotionArgs::operator==(const NotifyMotionArgs& rhs) const {
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070099 return id == rhs.id && eventTime == rhs.eventTime && readTime == rhs.readTime &&
Siarhei Vishniakou78513032022-09-15 18:42:05 -0700100 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 Vishniakou3218fc02023-06-15 20:41:02 -0700104 edgeFlags == rhs.edgeFlags && pointerProperties == rhs.pointerProperties &&
105 pointerCoords == rhs.pointerCoords && xPrecision == rhs.xPrecision &&
106 yPrecision == rhs.yPrecision &&
Siarhei Vishniakou78513032022-09-15 18:42:05 -0700107 isCursorPositionEqual(xCursorPosition, rhs.xCursorPosition) &&
108 isCursorPositionEqual(yCursorPosition, rhs.yCursorPosition) &&
109 downTime == rhs.downTime && videoFrames == rhs.videoFrames;
Siarhei Vishniakou78513032022-09-15 18:42:05 -0700110}
111
112std::string NotifyMotionArgs::dump() const {
113 std::string coords;
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -0700114 for (uint32_t i = 0; i < getPointerCount(); i++) {
Siarhei Vishniakou78513032022-09-15 18:42:05 -0700115 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 Vishniakou6d73f832022-07-21 17:27:03 -0700123 const ToolType toolType = pointerProperties[i].toolType;
124 if (toolType != ToolType::FINGER) {
125 coords += StringPrintf(" toolType=%s", ftl::enum_string(toolType).c_str());
Siarhei Vishniakou78513032022-09-15 18:42:05 -0700126 }
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 Vishniakou3218fc02023-06-15 20:41:02 -0700137 ", source=%s, action=%s, pointerCount=%zu pointers=%s, flags=0x%08x)",
Siarhei Vishniakou78513032022-09-15 18:42:05 -0700138 id, eventTime, deviceId, inputEventSourceToString(source).c_str(),
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -0700139 MotionEvent::actionToString(action).c_str(), getPointerCount(),
140 coords.c_str(), flags);
Siarhei Vishniakou78513032022-09-15 18:42:05 -0700141}
142
143// --- NotifySwitchArgs ---
144
145NotifySwitchArgs::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
155NotifySensorArgs::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
171NotifyVibratorStateArgs::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
177NotifyDeviceResetArgs::NotifyDeviceResetArgs(int32_t id, nsecs_t eventTime, int32_t deviceId)
178 : id(id), eventTime(eventTime), deviceId(deviceId) {}
179
180// --- NotifyPointerCaptureChangedArgs ---
181
182NotifyPointerCaptureChangedArgs::NotifyPointerCaptureChangedArgs(
183 int32_t id, nsecs_t eventTime, const PointerCaptureRequest& request)
184 : id(id), eventTime(eventTime), request(request) {}
185
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700186// Helper to std::visit with lambdas.
187template <typename... V>
AdityaK1c6fe002023-10-23 10:52:32 -0700188struct Visitor : V... { using V::operator()...; };
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700189// explicit deduction guide (not needed as of C++20)
190template <typename... V>
191Visitor(V...) -> Visitor<V...>;
192
193const char* toString(const NotifyArgs& args) {
194 Visitor toStringVisitor{
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000195 [&](const NotifyInputDevicesChangedArgs&) { return "NotifyInputDevicesChangedArgs"; },
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700196 [&](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 Vishniakou78513032022-09-15 18:42:05 -0700209} // namespace android