• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(const sp<InputListenerInterface> & listener) const47 void NotifyConfigurationChangedArgs::notify(const sp<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(const sp<InputListenerInterface> & listener) const92 void NotifyKeyArgs::notify(const sp<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 
notify(const sp<InputListenerInterface> & listener) const191 void NotifyMotionArgs::notify(const sp<InputListenerInterface>& listener) const {
192     listener->notifyMotion(this);
193 }
194 
195 // --- NotifySwitchArgs ---
196 
NotifySwitchArgs(int32_t id,nsecs_t eventTime,uint32_t policyFlags,uint32_t switchValues,uint32_t switchMask)197 NotifySwitchArgs::NotifySwitchArgs(int32_t id, nsecs_t eventTime, uint32_t policyFlags,
198                                    uint32_t switchValues, uint32_t switchMask)
199       : NotifyArgs(id, eventTime),
200         policyFlags(policyFlags),
201         switchValues(switchValues),
202         switchMask(switchMask) {}
203 
NotifySwitchArgs(const NotifySwitchArgs & other)204 NotifySwitchArgs::NotifySwitchArgs(const NotifySwitchArgs& other)
205       : NotifyArgs(other.id, other.eventTime),
206         policyFlags(other.policyFlags),
207         switchValues(other.switchValues),
208         switchMask(other.switchMask) {}
209 
operator ==(const NotifySwitchArgs rhs) const210 bool NotifySwitchArgs::operator==(const NotifySwitchArgs rhs) const {
211     return id == rhs.id && eventTime == rhs.eventTime && policyFlags == rhs.policyFlags &&
212             switchValues == rhs.switchValues && switchMask == rhs.switchMask;
213 }
214 
notify(const sp<InputListenerInterface> & listener) const215 void NotifySwitchArgs::notify(const sp<InputListenerInterface>& listener) const {
216     listener->notifySwitch(this);
217 }
218 
219 // --- NotifySensorArgs ---
220 
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)221 NotifySensorArgs::NotifySensorArgs(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
222                                    InputDeviceSensorType sensorType,
223                                    InputDeviceSensorAccuracy accuracy, bool accuracyChanged,
224                                    nsecs_t hwTimestamp, std::vector<float> values)
225       : NotifyArgs(id, eventTime),
226         deviceId(deviceId),
227         source(source),
228         sensorType(sensorType),
229         accuracy(accuracy),
230         accuracyChanged(accuracyChanged),
231         hwTimestamp(hwTimestamp),
232         values(std::move(values)) {}
233 
NotifySensorArgs(const NotifySensorArgs & other)234 NotifySensorArgs::NotifySensorArgs(const NotifySensorArgs& other)
235       : NotifyArgs(other.id, other.eventTime),
236         deviceId(other.deviceId),
237         source(other.source),
238         sensorType(other.sensorType),
239         accuracy(other.accuracy),
240         accuracyChanged(other.accuracyChanged),
241         hwTimestamp(other.hwTimestamp),
242         values(other.values) {}
243 
operator ==(const NotifySensorArgs rhs) const244 bool NotifySensorArgs::operator==(const NotifySensorArgs rhs) const {
245     return id == rhs.id && eventTime == rhs.eventTime && sensorType == rhs.sensorType &&
246             accuracy == rhs.accuracy && accuracyChanged == rhs.accuracyChanged &&
247             hwTimestamp == rhs.hwTimestamp && values == rhs.values;
248 }
249 
notify(const sp<InputListenerInterface> & listener) const250 void NotifySensorArgs::notify(const sp<InputListenerInterface>& listener) const {
251     listener->notifySensor(this);
252 }
253 
254 // --- NotifyVibratorStateArgs ---
255 
NotifyVibratorStateArgs(int32_t id,nsecs_t eventTime,int32_t deviceId,bool isOn)256 NotifyVibratorStateArgs::NotifyVibratorStateArgs(int32_t id, nsecs_t eventTime, int32_t deviceId,
257                                                  bool isOn)
258       : NotifyArgs(id, eventTime), deviceId(deviceId), isOn(isOn) {}
259 
NotifyVibratorStateArgs(const NotifyVibratorStateArgs & other)260 NotifyVibratorStateArgs::NotifyVibratorStateArgs(const NotifyVibratorStateArgs& other)
261       : NotifyArgs(other.id, other.eventTime), deviceId(other.deviceId), isOn(other.isOn) {}
262 
operator ==(const NotifyVibratorStateArgs rhs) const263 bool NotifyVibratorStateArgs::operator==(const NotifyVibratorStateArgs rhs) const {
264     return id == rhs.id && eventTime == rhs.eventTime && deviceId == rhs.deviceId &&
265             isOn == rhs.isOn;
266 }
267 
notify(const sp<InputListenerInterface> & listener) const268 void NotifyVibratorStateArgs::notify(const sp<InputListenerInterface>& listener) const {
269     listener->notifyVibratorState(this);
270 }
271 
272 // --- NotifyDeviceResetArgs ---
273 
NotifyDeviceResetArgs(int32_t id,nsecs_t eventTime,int32_t deviceId)274 NotifyDeviceResetArgs::NotifyDeviceResetArgs(int32_t id, nsecs_t eventTime, int32_t deviceId)
275       : NotifyArgs(id, eventTime), deviceId(deviceId) {}
276 
NotifyDeviceResetArgs(const NotifyDeviceResetArgs & other)277 NotifyDeviceResetArgs::NotifyDeviceResetArgs(const NotifyDeviceResetArgs& other)
278       : NotifyArgs(other.id, other.eventTime), deviceId(other.deviceId) {}
279 
operator ==(const NotifyDeviceResetArgs & rhs) const280 bool NotifyDeviceResetArgs::operator==(const NotifyDeviceResetArgs& rhs) const {
281     return id == rhs.id && eventTime == rhs.eventTime && deviceId == rhs.deviceId;
282 }
283 
notify(const sp<InputListenerInterface> & listener) const284 void NotifyDeviceResetArgs::notify(const sp<InputListenerInterface>& listener) const {
285     listener->notifyDeviceReset(this);
286 }
287 
288 // --- NotifyPointerCaptureChangedArgs ---
289 
NotifyPointerCaptureChangedArgs(int32_t id,nsecs_t eventTime,bool enabled)290 NotifyPointerCaptureChangedArgs::NotifyPointerCaptureChangedArgs(int32_t id, nsecs_t eventTime,
291                                                                  bool enabled)
292       : NotifyArgs(id, eventTime), enabled(enabled) {}
293 
NotifyPointerCaptureChangedArgs(const NotifyPointerCaptureChangedArgs & other)294 NotifyPointerCaptureChangedArgs::NotifyPointerCaptureChangedArgs(
295         const NotifyPointerCaptureChangedArgs& other)
296       : NotifyArgs(other.id, other.eventTime), enabled(other.enabled) {}
297 
operator ==(const NotifyPointerCaptureChangedArgs & rhs) const298 bool NotifyPointerCaptureChangedArgs::operator==(const NotifyPointerCaptureChangedArgs& rhs) const {
299     return id == rhs.id && eventTime == rhs.eventTime && enabled == rhs.enabled;
300 }
301 
notify(const sp<InputListenerInterface> & listener) const302 void NotifyPointerCaptureChangedArgs::notify(const sp<InputListenerInterface>& listener) const {
303     listener->notifyPointerCaptureChanged(this);
304 }
305 
306 // --- QueuedInputListener ---
307 
traceEvent(const char * functionName,int32_t id)308 static inline void traceEvent(const char* functionName, int32_t id) {
309     if (ATRACE_ENABLED()) {
310         std::string message = StringPrintf("%s(id=0x%" PRIx32 ")", functionName, id);
311         ATRACE_NAME(message.c_str());
312     }
313 }
314 
QueuedInputListener(const sp<InputListenerInterface> & innerListener)315 QueuedInputListener::QueuedInputListener(const sp<InputListenerInterface>& innerListener) :
316         mInnerListener(innerListener) {
317 }
318 
~QueuedInputListener()319 QueuedInputListener::~QueuedInputListener() {
320     size_t count = mArgsQueue.size();
321     for (size_t i = 0; i < count; i++) {
322         delete mArgsQueue[i];
323     }
324 }
325 
notifyConfigurationChanged(const NotifyConfigurationChangedArgs * args)326 void QueuedInputListener::notifyConfigurationChanged(
327         const NotifyConfigurationChangedArgs* args) {
328     traceEvent(__func__, args->id);
329     mArgsQueue.push_back(new NotifyConfigurationChangedArgs(*args));
330 }
331 
notifyKey(const NotifyKeyArgs * args)332 void QueuedInputListener::notifyKey(const NotifyKeyArgs* args) {
333     traceEvent(__func__, args->id);
334     mArgsQueue.push_back(new NotifyKeyArgs(*args));
335 }
336 
notifyMotion(const NotifyMotionArgs * args)337 void QueuedInputListener::notifyMotion(const NotifyMotionArgs* args) {
338     traceEvent(__func__, args->id);
339     mArgsQueue.push_back(new NotifyMotionArgs(*args));
340 }
341 
notifySwitch(const NotifySwitchArgs * args)342 void QueuedInputListener::notifySwitch(const NotifySwitchArgs* args) {
343     traceEvent(__func__, args->id);
344     mArgsQueue.push_back(new NotifySwitchArgs(*args));
345 }
346 
notifySensor(const NotifySensorArgs * args)347 void QueuedInputListener::notifySensor(const NotifySensorArgs* args) {
348     traceEvent(__func__, args->id);
349     mArgsQueue.push_back(new NotifySensorArgs(*args));
350 }
351 
notifyVibratorState(const NotifyVibratorStateArgs * args)352 void QueuedInputListener::notifyVibratorState(const NotifyVibratorStateArgs* args) {
353     traceEvent(__func__, args->id);
354     mArgsQueue.push_back(new NotifyVibratorStateArgs(*args));
355 }
356 
notifyDeviceReset(const NotifyDeviceResetArgs * args)357 void QueuedInputListener::notifyDeviceReset(const NotifyDeviceResetArgs* args) {
358     traceEvent(__func__, args->id);
359     mArgsQueue.push_back(new NotifyDeviceResetArgs(*args));
360 }
361 
notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs * args)362 void QueuedInputListener::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) {
363     traceEvent(__func__, args->id);
364     mArgsQueue.push_back(new NotifyPointerCaptureChangedArgs(*args));
365 }
366 
flush()367 void QueuedInputListener::flush() {
368     size_t count = mArgsQueue.size();
369     for (size_t i = 0; i < count; i++) {
370         NotifyArgs* args = mArgsQueue[i];
371         args->notify(mInnerListener);
372         delete args;
373     }
374     mArgsQueue.clear();
375 }
376 
377 } // namespace android
378