1 /*
2 * Copyright (C) 2011 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 "InputListener"
18
19 #define ATRACE_TAG ATRACE_TAG_INPUT
20
21 //#define LOG_NDEBUG 0
22
23 #include "InputListener.h"
24
25 #include <android-base/stringprintf.h>
26 #include <android/log.h>
27 #include <math.h>
28 #include <utils/Trace.h>
29
30 using android::base::StringPrintf;
31
32 namespace android {
33
34 // --- NotifyConfigurationChangedArgs ---
35
NotifyConfigurationChangedArgs(int32_t id,nsecs_t eventTime)36 NotifyConfigurationChangedArgs::NotifyConfigurationChangedArgs(int32_t id, nsecs_t eventTime)
37 : NotifyArgs(id, eventTime) {}
38
NotifyConfigurationChangedArgs(const NotifyConfigurationChangedArgs & other)39 NotifyConfigurationChangedArgs::NotifyConfigurationChangedArgs(
40 const NotifyConfigurationChangedArgs& other)
41 : NotifyArgs(other.id, other.eventTime) {}
42
operator ==(const NotifyConfigurationChangedArgs & rhs) const43 bool NotifyConfigurationChangedArgs::operator==(const NotifyConfigurationChangedArgs& rhs) const {
44 return id == rhs.id && eventTime == rhs.eventTime;
45 }
46
notify(InputListenerInterface & listener) const47 void NotifyConfigurationChangedArgs::notify(InputListenerInterface& listener) const {
48 listener.notifyConfigurationChanged(this);
49 }
50
51 // --- NotifyKeyArgs ---
52
NotifyKeyArgs(int32_t id,nsecs_t eventTime,nsecs_t readTime,int32_t deviceId,uint32_t source,int32_t displayId,uint32_t policyFlags,int32_t action,int32_t flags,int32_t keyCode,int32_t scanCode,int32_t metaState,nsecs_t downTime)53 NotifyKeyArgs::NotifyKeyArgs(int32_t id, nsecs_t eventTime, nsecs_t readTime, int32_t deviceId,
54 uint32_t source, int32_t displayId, uint32_t policyFlags,
55 int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode,
56 int32_t metaState, nsecs_t downTime)
57 : NotifyArgs(id, eventTime),
58 deviceId(deviceId),
59 source(source),
60 displayId(displayId),
61 policyFlags(policyFlags),
62 action(action),
63 flags(flags),
64 keyCode(keyCode),
65 scanCode(scanCode),
66 metaState(metaState),
67 downTime(downTime),
68 readTime(readTime) {}
69
NotifyKeyArgs(const NotifyKeyArgs & other)70 NotifyKeyArgs::NotifyKeyArgs(const NotifyKeyArgs& other)
71 : NotifyArgs(other.id, other.eventTime),
72 deviceId(other.deviceId),
73 source(other.source),
74 displayId(other.displayId),
75 policyFlags(other.policyFlags),
76 action(other.action),
77 flags(other.flags),
78 keyCode(other.keyCode),
79 scanCode(other.scanCode),
80 metaState(other.metaState),
81 downTime(other.downTime),
82 readTime(other.readTime) {}
83
operator ==(const NotifyKeyArgs & rhs) const84 bool NotifyKeyArgs::operator==(const NotifyKeyArgs& rhs) const {
85 return id == rhs.id && eventTime == rhs.eventTime && readTime == rhs.readTime &&
86 deviceId == rhs.deviceId && source == rhs.source && displayId == rhs.displayId &&
87 policyFlags == rhs.policyFlags && action == rhs.action && flags == rhs.flags &&
88 keyCode == rhs.keyCode && scanCode == rhs.scanCode && metaState == rhs.metaState &&
89 downTime == rhs.downTime;
90 }
91
notify(InputListenerInterface & listener) const92 void NotifyKeyArgs::notify(InputListenerInterface& listener) const {
93 listener.notifyKey(this);
94 }
95
96 // --- NotifyMotionArgs ---
97
NotifyMotionArgs(int32_t id,nsecs_t eventTime,nsecs_t readTime,int32_t deviceId,uint32_t source,int32_t 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)98 NotifyMotionArgs::NotifyMotionArgs(
99 int32_t id, nsecs_t eventTime, nsecs_t readTime, int32_t deviceId, uint32_t source,
100 int32_t displayId, uint32_t policyFlags, int32_t action, int32_t actionButton,
101 int32_t flags, int32_t metaState, int32_t buttonState, MotionClassification classification,
102 int32_t edgeFlags, uint32_t pointerCount, const PointerProperties* pointerProperties,
103 const PointerCoords* pointerCoords, float xPrecision, float yPrecision,
104 float xCursorPosition, float yCursorPosition, nsecs_t downTime,
105 const std::vector<TouchVideoFrame>& videoFrames)
106 : NotifyArgs(id, eventTime),
107 deviceId(deviceId),
108 source(source),
109 displayId(displayId),
110 policyFlags(policyFlags),
111 action(action),
112 actionButton(actionButton),
113 flags(flags),
114 metaState(metaState),
115 buttonState(buttonState),
116 classification(classification),
117 edgeFlags(edgeFlags),
118 pointerCount(pointerCount),
119 xPrecision(xPrecision),
120 yPrecision(yPrecision),
121 xCursorPosition(xCursorPosition),
122 yCursorPosition(yCursorPosition),
123 downTime(downTime),
124 readTime(readTime),
125 videoFrames(videoFrames) {
126 for (uint32_t i = 0; i < pointerCount; i++) {
127 this->pointerProperties[i].copyFrom(pointerProperties[i]);
128 this->pointerCoords[i].copyFrom(pointerCoords[i]);
129 }
130 }
131
NotifyMotionArgs(const NotifyMotionArgs & other)132 NotifyMotionArgs::NotifyMotionArgs(const NotifyMotionArgs& other)
133 : NotifyArgs(other.id, other.eventTime),
134 deviceId(other.deviceId),
135 source(other.source),
136 displayId(other.displayId),
137 policyFlags(other.policyFlags),
138 action(other.action),
139 actionButton(other.actionButton),
140 flags(other.flags),
141 metaState(other.metaState),
142 buttonState(other.buttonState),
143 classification(other.classification),
144 edgeFlags(other.edgeFlags),
145 pointerCount(other.pointerCount),
146 xPrecision(other.xPrecision),
147 yPrecision(other.yPrecision),
148 xCursorPosition(other.xCursorPosition),
149 yCursorPosition(other.yCursorPosition),
150 downTime(other.downTime),
151 readTime(other.readTime),
152 videoFrames(other.videoFrames) {
153 for (uint32_t i = 0; i < pointerCount; i++) {
154 pointerProperties[i].copyFrom(other.pointerProperties[i]);
155 pointerCoords[i].copyFrom(other.pointerCoords[i]);
156 }
157 }
158
isCursorPositionEqual(float lhs,float rhs)159 static inline bool isCursorPositionEqual(float lhs, float rhs) {
160 return (isnan(lhs) && isnan(rhs)) || lhs == rhs;
161 }
162
operator ==(const NotifyMotionArgs & rhs) const163 bool NotifyMotionArgs::operator==(const NotifyMotionArgs& rhs) const {
164 bool equal = id == rhs.id && eventTime == rhs.eventTime && readTime == rhs.readTime &&
165 deviceId == rhs.deviceId && source == rhs.source && displayId == rhs.displayId &&
166 policyFlags == rhs.policyFlags && action == rhs.action &&
167 actionButton == rhs.actionButton && flags == rhs.flags && metaState == rhs.metaState &&
168 buttonState == rhs.buttonState && classification == rhs.classification &&
169 edgeFlags == rhs.edgeFlags &&
170 pointerCount == rhs.pointerCount
171 // PointerProperties and PointerCoords are compared separately below
172 && xPrecision == rhs.xPrecision && yPrecision == rhs.yPrecision &&
173 isCursorPositionEqual(xCursorPosition, rhs.xCursorPosition) &&
174 isCursorPositionEqual(yCursorPosition, rhs.yCursorPosition) &&
175 downTime == rhs.downTime && videoFrames == rhs.videoFrames;
176 if (!equal) {
177 return false;
178 }
179
180 for (size_t i = 0; i < pointerCount; i++) {
181 equal =
182 pointerProperties[i] == rhs.pointerProperties[i]
183 && pointerCoords[i] == rhs.pointerCoords[i];
184 if (!equal) {
185 return false;
186 }
187 }
188 return true;
189 }
190
dump() const191 std::string NotifyMotionArgs::dump() const {
192 std::string coords;
193 for (uint32_t i = 0; i < pointerCount; i++) {
194 if (!coords.empty()) {
195 coords += ", ";
196 }
197 coords += StringPrintf("{%" PRIu32 ": ", i);
198 coords +=
199 StringPrintf("id=%" PRIu32 " x=%.1f y=%.1f pressure=%.1f", pointerProperties[i].id,
200 pointerCoords[i].getX(), pointerCoords[i].getY(),
201 pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
202 const int32_t toolType = pointerProperties[i].toolType;
203 if (toolType != AMOTION_EVENT_TOOL_TYPE_FINGER) {
204 coords += StringPrintf(" toolType=%s", motionToolTypeToString(toolType));
205 }
206 const float major = pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR);
207 const float minor = pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR);
208 const float orientation = pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
209 if (major != 0 || minor != 0) {
210 coords += StringPrintf(" major=%.1f minor=%.1f orientation=%.1f", major, minor,
211 orientation);
212 }
213 coords += "}";
214 }
215 return StringPrintf("NotifyMotionArgs(id=%" PRId32 ", eventTime=%" PRId64 ", deviceId=%" PRId32
216 ", source=%s, action=%s, pointerCount=%" PRIu32
217 " pointers=%s, flags=0x%08x)",
218 id, eventTime, deviceId, inputEventSourceToString(source).c_str(),
219 MotionEvent::actionToString(action).c_str(), pointerCount, coords.c_str(),
220 flags);
221 }
222
notify(InputListenerInterface & listener) const223 void NotifyMotionArgs::notify(InputListenerInterface& listener) const {
224 listener.notifyMotion(this);
225 }
226
227 // --- NotifySwitchArgs ---
228
NotifySwitchArgs(int32_t id,nsecs_t eventTime,uint32_t policyFlags,uint32_t switchValues,uint32_t switchMask)229 NotifySwitchArgs::NotifySwitchArgs(int32_t id, nsecs_t eventTime, uint32_t policyFlags,
230 uint32_t switchValues, uint32_t switchMask)
231 : NotifyArgs(id, eventTime),
232 policyFlags(policyFlags),
233 switchValues(switchValues),
234 switchMask(switchMask) {}
235
NotifySwitchArgs(const NotifySwitchArgs & other)236 NotifySwitchArgs::NotifySwitchArgs(const NotifySwitchArgs& other)
237 : NotifyArgs(other.id, other.eventTime),
238 policyFlags(other.policyFlags),
239 switchValues(other.switchValues),
240 switchMask(other.switchMask) {}
241
operator ==(const NotifySwitchArgs rhs) const242 bool NotifySwitchArgs::operator==(const NotifySwitchArgs rhs) const {
243 return id == rhs.id && eventTime == rhs.eventTime && policyFlags == rhs.policyFlags &&
244 switchValues == rhs.switchValues && switchMask == rhs.switchMask;
245 }
246
notify(InputListenerInterface & listener) const247 void NotifySwitchArgs::notify(InputListenerInterface& listener) const {
248 listener.notifySwitch(this);
249 }
250
251 // --- NotifySensorArgs ---
252
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)253 NotifySensorArgs::NotifySensorArgs(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
254 InputDeviceSensorType sensorType,
255 InputDeviceSensorAccuracy accuracy, bool accuracyChanged,
256 nsecs_t hwTimestamp, std::vector<float> values)
257 : NotifyArgs(id, eventTime),
258 deviceId(deviceId),
259 source(source),
260 sensorType(sensorType),
261 accuracy(accuracy),
262 accuracyChanged(accuracyChanged),
263 hwTimestamp(hwTimestamp),
264 values(std::move(values)) {}
265
NotifySensorArgs(const NotifySensorArgs & other)266 NotifySensorArgs::NotifySensorArgs(const NotifySensorArgs& other)
267 : NotifyArgs(other.id, other.eventTime),
268 deviceId(other.deviceId),
269 source(other.source),
270 sensorType(other.sensorType),
271 accuracy(other.accuracy),
272 accuracyChanged(other.accuracyChanged),
273 hwTimestamp(other.hwTimestamp),
274 values(other.values) {}
275
operator ==(const NotifySensorArgs rhs) const276 bool NotifySensorArgs::operator==(const NotifySensorArgs rhs) const {
277 return id == rhs.id && eventTime == rhs.eventTime && sensorType == rhs.sensorType &&
278 accuracy == rhs.accuracy && accuracyChanged == rhs.accuracyChanged &&
279 hwTimestamp == rhs.hwTimestamp && values == rhs.values;
280 }
281
notify(InputListenerInterface & listener) const282 void NotifySensorArgs::notify(InputListenerInterface& listener) const {
283 listener.notifySensor(this);
284 }
285
286 // --- NotifyVibratorStateArgs ---
287
NotifyVibratorStateArgs(int32_t id,nsecs_t eventTime,int32_t deviceId,bool isOn)288 NotifyVibratorStateArgs::NotifyVibratorStateArgs(int32_t id, nsecs_t eventTime, int32_t deviceId,
289 bool isOn)
290 : NotifyArgs(id, eventTime), deviceId(deviceId), isOn(isOn) {}
291
NotifyVibratorStateArgs(const NotifyVibratorStateArgs & other)292 NotifyVibratorStateArgs::NotifyVibratorStateArgs(const NotifyVibratorStateArgs& other)
293 : NotifyArgs(other.id, other.eventTime), deviceId(other.deviceId), isOn(other.isOn) {}
294
operator ==(const NotifyVibratorStateArgs rhs) const295 bool NotifyVibratorStateArgs::operator==(const NotifyVibratorStateArgs rhs) const {
296 return id == rhs.id && eventTime == rhs.eventTime && deviceId == rhs.deviceId &&
297 isOn == rhs.isOn;
298 }
299
notify(InputListenerInterface & listener) const300 void NotifyVibratorStateArgs::notify(InputListenerInterface& listener) const {
301 listener.notifyVibratorState(this);
302 }
303
304 // --- NotifyDeviceResetArgs ---
305
NotifyDeviceResetArgs(int32_t id,nsecs_t eventTime,int32_t deviceId)306 NotifyDeviceResetArgs::NotifyDeviceResetArgs(int32_t id, nsecs_t eventTime, int32_t deviceId)
307 : NotifyArgs(id, eventTime), deviceId(deviceId) {}
308
NotifyDeviceResetArgs(const NotifyDeviceResetArgs & other)309 NotifyDeviceResetArgs::NotifyDeviceResetArgs(const NotifyDeviceResetArgs& other)
310 : NotifyArgs(other.id, other.eventTime), deviceId(other.deviceId) {}
311
operator ==(const NotifyDeviceResetArgs & rhs) const312 bool NotifyDeviceResetArgs::operator==(const NotifyDeviceResetArgs& rhs) const {
313 return id == rhs.id && eventTime == rhs.eventTime && deviceId == rhs.deviceId;
314 }
315
notify(InputListenerInterface & listener) const316 void NotifyDeviceResetArgs::notify(InputListenerInterface& listener) const {
317 listener.notifyDeviceReset(this);
318 }
319
320 // --- NotifyPointerCaptureChangedArgs ---
321
NotifyPointerCaptureChangedArgs(int32_t id,nsecs_t eventTime,const PointerCaptureRequest & request)322 NotifyPointerCaptureChangedArgs::NotifyPointerCaptureChangedArgs(
323 int32_t id, nsecs_t eventTime, const PointerCaptureRequest& request)
324 : NotifyArgs(id, eventTime), request(request) {}
325
NotifyPointerCaptureChangedArgs(const NotifyPointerCaptureChangedArgs & other)326 NotifyPointerCaptureChangedArgs::NotifyPointerCaptureChangedArgs(
327 const NotifyPointerCaptureChangedArgs& other)
328 : NotifyArgs(other.id, other.eventTime), request(other.request) {}
329
operator ==(const NotifyPointerCaptureChangedArgs & rhs) const330 bool NotifyPointerCaptureChangedArgs::operator==(const NotifyPointerCaptureChangedArgs& rhs) const {
331 return id == rhs.id && eventTime == rhs.eventTime && request == rhs.request;
332 }
333
notify(InputListenerInterface & listener) const334 void NotifyPointerCaptureChangedArgs::notify(InputListenerInterface& listener) const {
335 listener.notifyPointerCaptureChanged(this);
336 }
337
338 // --- QueuedInputListener ---
339
traceEvent(const char * functionName,int32_t id)340 static inline void traceEvent(const char* functionName, int32_t id) {
341 if (ATRACE_ENABLED()) {
342 std::string message = StringPrintf("%s(id=0x%" PRIx32 ")", functionName, id);
343 ATRACE_NAME(message.c_str());
344 }
345 }
346
QueuedInputListener(InputListenerInterface & innerListener)347 QueuedInputListener::QueuedInputListener(InputListenerInterface& innerListener)
348 : mInnerListener(innerListener) {}
349
notifyConfigurationChanged(const NotifyConfigurationChangedArgs * args)350 void QueuedInputListener::notifyConfigurationChanged(
351 const NotifyConfigurationChangedArgs* args) {
352 traceEvent(__func__, args->id);
353 mArgsQueue.emplace_back(std::make_unique<NotifyConfigurationChangedArgs>(*args));
354 }
355
notifyKey(const NotifyKeyArgs * args)356 void QueuedInputListener::notifyKey(const NotifyKeyArgs* args) {
357 traceEvent(__func__, args->id);
358 mArgsQueue.emplace_back(std::make_unique<NotifyKeyArgs>(*args));
359 }
360
notifyMotion(const NotifyMotionArgs * args)361 void QueuedInputListener::notifyMotion(const NotifyMotionArgs* args) {
362 traceEvent(__func__, args->id);
363 mArgsQueue.emplace_back(std::make_unique<NotifyMotionArgs>(*args));
364 }
365
notifySwitch(const NotifySwitchArgs * args)366 void QueuedInputListener::notifySwitch(const NotifySwitchArgs* args) {
367 traceEvent(__func__, args->id);
368 mArgsQueue.emplace_back(std::make_unique<NotifySwitchArgs>(*args));
369 }
370
notifySensor(const NotifySensorArgs * args)371 void QueuedInputListener::notifySensor(const NotifySensorArgs* args) {
372 traceEvent(__func__, args->id);
373 mArgsQueue.emplace_back(std::make_unique<NotifySensorArgs>(*args));
374 }
375
notifyVibratorState(const NotifyVibratorStateArgs * args)376 void QueuedInputListener::notifyVibratorState(const NotifyVibratorStateArgs* args) {
377 traceEvent(__func__, args->id);
378 mArgsQueue.emplace_back(std::make_unique<NotifyVibratorStateArgs>(*args));
379 }
380
notifyDeviceReset(const NotifyDeviceResetArgs * args)381 void QueuedInputListener::notifyDeviceReset(const NotifyDeviceResetArgs* args) {
382 traceEvent(__func__, args->id);
383 mArgsQueue.emplace_back(std::make_unique<NotifyDeviceResetArgs>(*args));
384 }
385
notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs * args)386 void QueuedInputListener::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) {
387 traceEvent(__func__, args->id);
388 mArgsQueue.emplace_back(std::make_unique<NotifyPointerCaptureChangedArgs>(*args));
389 }
390
flush()391 void QueuedInputListener::flush() {
392 for (const std::unique_ptr<NotifyArgs>& args : mArgsQueue) {
393 args->notify(mInnerListener);
394 }
395 mArgsQueue.clear();
396 }
397
398 } // namespace android
399