xref: /aosp_15_r20/frameworks/native/services/inputflinger/NotifyArgs.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
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 
32 // --- NotifyInputDevicesChangedArgs ---
33 
NotifyInputDevicesChangedArgs(int32_t id,std::vector<InputDeviceInfo> infos)34 NotifyInputDevicesChangedArgs::NotifyInputDevicesChangedArgs(int32_t id,
35                                                              std::vector<InputDeviceInfo> infos)
36       : id(id), inputDeviceInfos(std::move(infos)) {}
37 
38 // --- NotifyKeyArgs ---
39 
NotifyKeyArgs(int32_t id,nsecs_t eventTime,nsecs_t readTime,int32_t deviceId,uint32_t source,ui::LogicalDisplayId displayId,uint32_t policyFlags,int32_t action,int32_t flags,int32_t keyCode,int32_t scanCode,int32_t metaState,nsecs_t downTime)40 NotifyKeyArgs::NotifyKeyArgs(int32_t id, nsecs_t eventTime, nsecs_t readTime, int32_t deviceId,
41                              uint32_t source, ui::LogicalDisplayId displayId, uint32_t policyFlags,
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 
NotifyMotionArgs(int32_t id,nsecs_t eventTime,nsecs_t readTime,int32_t deviceId,uint32_t source,ui::LogicalDisplayId displayId,uint32_t policyFlags,int32_t action,int32_t actionButton,int32_t flags,int32_t metaState,int32_t buttonState,MotionClassification classification,int32_t edgeFlags,uint32_t pointerCount,const PointerProperties * pointerProperties,const PointerCoords * pointerCoords,float xPrecision,float yPrecision,float xCursorPosition,float yCursorPosition,nsecs_t downTime,const std::vector<TouchVideoFrame> & videoFrames)60 NotifyMotionArgs::NotifyMotionArgs(
61         int32_t id, nsecs_t eventTime, nsecs_t readTime, int32_t deviceId, uint32_t source,
62         ui::LogicalDisplayId displayId, uint32_t policyFlags, int32_t action, int32_t actionButton,
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),
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++) {
89         this->pointerProperties.emplace_back(pointerProperties[i]);
90         this->pointerCoords.emplace_back(pointerCoords[i]);
91     }
92 }
93 
isCursorPositionEqual(float lhs,float rhs)94 static inline bool isCursorPositionEqual(float lhs, float rhs) {
95     return (isnan(lhs) && isnan(rhs)) || lhs == rhs;
96 }
97 
operator ==(const NotifyMotionArgs & rhs) const98 bool NotifyMotionArgs::operator==(const NotifyMotionArgs& rhs) const {
99     return id == rhs.id && eventTime == rhs.eventTime && readTime == rhs.readTime &&
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 &&
104             edgeFlags == rhs.edgeFlags && pointerProperties == rhs.pointerProperties &&
105             pointerCoords == rhs.pointerCoords && xPrecision == rhs.xPrecision &&
106             yPrecision == rhs.yPrecision &&
107             isCursorPositionEqual(xCursorPosition, rhs.xCursorPosition) &&
108             isCursorPositionEqual(yCursorPosition, rhs.yCursorPosition) &&
109             downTime == rhs.downTime && videoFrames == rhs.videoFrames;
110 }
111 
dump() const112 std::string NotifyMotionArgs::dump() const {
113     std::string coords;
114     for (uint32_t i = 0; i < getPointerCount(); i++) {
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));
123         const ToolType toolType = pointerProperties[i].toolType;
124         if (toolType != ToolType::FINGER) {
125             coords += StringPrintf(" toolType=%s", ftl::enum_string(toolType).c_str());
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
137                         ", source=%s, action=%s, pointerCount=%zu pointers=%s, flags=0x%08x)",
138                         id, eventTime, deviceId, inputEventSourceToString(source).c_str(),
139                         MotionEvent::actionToString(action).c_str(), getPointerCount(),
140                         coords.c_str(), flags);
141 }
142 
143 // --- NotifySwitchArgs ---
144 
NotifySwitchArgs(int32_t id,nsecs_t eventTime,uint32_t policyFlags,uint32_t switchValues,uint32_t switchMask)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 
NotifySensorArgs(int32_t id,nsecs_t eventTime,int32_t deviceId,uint32_t source,InputDeviceSensorType sensorType,InputDeviceSensorAccuracy accuracy,bool accuracyChanged,nsecs_t hwTimestamp,std::vector<float> values)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 
NotifyVibratorStateArgs(int32_t id,nsecs_t eventTime,int32_t deviceId,bool isOn)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 
NotifyDeviceResetArgs(int32_t id,nsecs_t eventTime,int32_t deviceId)177 NotifyDeviceResetArgs::NotifyDeviceResetArgs(int32_t id, nsecs_t eventTime, int32_t deviceId)
178       : id(id), eventTime(eventTime), deviceId(deviceId) {}
179 
180 // --- NotifyPointerCaptureChangedArgs ---
181 
NotifyPointerCaptureChangedArgs(int32_t id,nsecs_t eventTime,const PointerCaptureRequest & request)182 NotifyPointerCaptureChangedArgs::NotifyPointerCaptureChangedArgs(
183         int32_t id, nsecs_t eventTime, const PointerCaptureRequest& request)
184       : id(id), eventTime(eventTime), request(request) {}
185 
186 // Helper to std::visit with lambdas.
187 template <typename... V>
188 struct Visitor : V... { using V::operator()...; };
189 // explicit deduction guide (not needed as of C++20)
190 template <typename... V>
191 Visitor(V...) -> Visitor<V...>;
192 
toString(const NotifyArgs & args)193 const char* toString(const NotifyArgs& args) {
194     Visitor toStringVisitor{
195             [&](const NotifyInputDevicesChangedArgs&) { return "NotifyInputDevicesChangedArgs"; },
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 
209 } // namespace android
210