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
52 // --- NotifyKeyArgs ---
53
NotifyKeyArgs(int32_t id,nsecs_t eventTime,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)54 NotifyKeyArgs::NotifyKeyArgs(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
55 int32_t displayId, uint32_t policyFlags, int32_t action, int32_t flags,
56 int32_t keyCode, int32_t scanCode, 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
NotifyKeyArgs(const NotifyKeyArgs & other)69 NotifyKeyArgs::NotifyKeyArgs(const NotifyKeyArgs& other)
70 : NotifyArgs(other.id, other.eventTime),
71 deviceId(other.deviceId),
72 source(other.source),
73 displayId(other.displayId),
74 policyFlags(other.policyFlags),
75 action(other.action),
76 flags(other.flags),
77 keyCode(other.keyCode),
78 scanCode(other.scanCode),
79 metaState(other.metaState),
80 downTime(other.downTime) {}
81
operator ==(const NotifyKeyArgs & rhs) const82 bool NotifyKeyArgs::operator==(const NotifyKeyArgs& rhs) const {
83 return id == rhs.id && eventTime == rhs.eventTime && deviceId == rhs.deviceId &&
84 source == rhs.source && displayId == rhs.displayId && policyFlags == rhs.policyFlags &&
85 action == rhs.action && flags == rhs.flags && keyCode == rhs.keyCode &&
86 scanCode == rhs.scanCode && metaState == rhs.metaState && downTime == rhs.downTime;
87 }
88
notify(const sp<InputListenerInterface> & listener) const89 void NotifyKeyArgs::notify(const sp<InputListenerInterface>& listener) const {
90 listener->notifyKey(this);
91 }
92
93
94 // --- NotifyMotionArgs ---
95
NotifyMotionArgs(int32_t id,nsecs_t eventTime,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)96 NotifyMotionArgs::NotifyMotionArgs(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
97 int32_t displayId, uint32_t policyFlags, int32_t action,
98 int32_t actionButton, int32_t flags, int32_t metaState,
99 int32_t buttonState, MotionClassification classification,
100 int32_t edgeFlags, uint32_t pointerCount,
101 const PointerProperties* pointerProperties,
102 const PointerCoords* pointerCoords, float xPrecision,
103 float yPrecision, float xCursorPosition, float yCursorPosition,
104 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 videoFrames(videoFrames) {
125 for (uint32_t i = 0; i < pointerCount; i++) {
126 this->pointerProperties[i].copyFrom(pointerProperties[i]);
127 this->pointerCoords[i].copyFrom(pointerCoords[i]);
128 }
129 }
130
NotifyMotionArgs(const NotifyMotionArgs & other)131 NotifyMotionArgs::NotifyMotionArgs(const NotifyMotionArgs& other)
132 : NotifyArgs(other.id, other.eventTime),
133 deviceId(other.deviceId),
134 source(other.source),
135 displayId(other.displayId),
136 policyFlags(other.policyFlags),
137 action(other.action),
138 actionButton(other.actionButton),
139 flags(other.flags),
140 metaState(other.metaState),
141 buttonState(other.buttonState),
142 classification(other.classification),
143 edgeFlags(other.edgeFlags),
144 pointerCount(other.pointerCount),
145 xPrecision(other.xPrecision),
146 yPrecision(other.yPrecision),
147 xCursorPosition(other.xCursorPosition),
148 yCursorPosition(other.yCursorPosition),
149 downTime(other.downTime),
150 videoFrames(other.videoFrames) {
151 for (uint32_t i = 0; i < pointerCount; i++) {
152 pointerProperties[i].copyFrom(other.pointerProperties[i]);
153 pointerCoords[i].copyFrom(other.pointerCoords[i]);
154 }
155 }
156
isCursorPositionEqual(float lhs,float rhs)157 static inline bool isCursorPositionEqual(float lhs, float rhs) {
158 return (isnan(lhs) && isnan(rhs)) || lhs == rhs;
159 }
160
operator ==(const NotifyMotionArgs & rhs) const161 bool NotifyMotionArgs::operator==(const NotifyMotionArgs& rhs) const {
162 bool equal = id == rhs.id && eventTime == rhs.eventTime && deviceId == rhs.deviceId &&
163 source == rhs.source && displayId == rhs.displayId && policyFlags == rhs.policyFlags &&
164 action == rhs.action && actionButton == rhs.actionButton && flags == rhs.flags &&
165 metaState == rhs.metaState && buttonState == rhs.buttonState &&
166 classification == rhs.classification && edgeFlags == rhs.edgeFlags &&
167 pointerCount == rhs.pointerCount
168 // PointerProperties and PointerCoords are compared separately below
169 && xPrecision == rhs.xPrecision && yPrecision == rhs.yPrecision &&
170 isCursorPositionEqual(xCursorPosition, rhs.xCursorPosition) &&
171 isCursorPositionEqual(yCursorPosition, rhs.yCursorPosition) &&
172 downTime == rhs.downTime && videoFrames == rhs.videoFrames;
173 if (!equal) {
174 return false;
175 }
176
177 for (size_t i = 0; i < pointerCount; i++) {
178 equal =
179 pointerProperties[i] == rhs.pointerProperties[i]
180 && pointerCoords[i] == rhs.pointerCoords[i];
181 if (!equal) {
182 return false;
183 }
184 }
185 return true;
186 }
187
notify(const sp<InputListenerInterface> & listener) const188 void NotifyMotionArgs::notify(const sp<InputListenerInterface>& listener) const {
189 listener->notifyMotion(this);
190 }
191
192
193 // --- NotifySwitchArgs ---
194
NotifySwitchArgs(int32_t id,nsecs_t eventTime,uint32_t policyFlags,uint32_t switchValues,uint32_t switchMask)195 NotifySwitchArgs::NotifySwitchArgs(int32_t id, nsecs_t eventTime, uint32_t policyFlags,
196 uint32_t switchValues, uint32_t switchMask)
197 : NotifyArgs(id, eventTime),
198 policyFlags(policyFlags),
199 switchValues(switchValues),
200 switchMask(switchMask) {}
201
NotifySwitchArgs(const NotifySwitchArgs & other)202 NotifySwitchArgs::NotifySwitchArgs(const NotifySwitchArgs& other)
203 : NotifyArgs(other.id, other.eventTime),
204 policyFlags(other.policyFlags),
205 switchValues(other.switchValues),
206 switchMask(other.switchMask) {}
207
operator ==(const NotifySwitchArgs rhs) const208 bool NotifySwitchArgs::operator==(const NotifySwitchArgs rhs) const {
209 return id == rhs.id && eventTime == rhs.eventTime && policyFlags == rhs.policyFlags &&
210 switchValues == rhs.switchValues && switchMask == rhs.switchMask;
211 }
212
notify(const sp<InputListenerInterface> & listener) const213 void NotifySwitchArgs::notify(const sp<InputListenerInterface>& listener) const {
214 listener->notifySwitch(this);
215 }
216
217
218 // --- NotifyDeviceResetArgs ---
219
NotifyDeviceResetArgs(int32_t id,nsecs_t eventTime,int32_t deviceId)220 NotifyDeviceResetArgs::NotifyDeviceResetArgs(int32_t id, nsecs_t eventTime, int32_t deviceId)
221 : NotifyArgs(id, eventTime), deviceId(deviceId) {}
222
NotifyDeviceResetArgs(const NotifyDeviceResetArgs & other)223 NotifyDeviceResetArgs::NotifyDeviceResetArgs(const NotifyDeviceResetArgs& other)
224 : NotifyArgs(other.id, other.eventTime), deviceId(other.deviceId) {}
225
operator ==(const NotifyDeviceResetArgs & rhs) const226 bool NotifyDeviceResetArgs::operator==(const NotifyDeviceResetArgs& rhs) const {
227 return id == rhs.id && eventTime == rhs.eventTime && deviceId == rhs.deviceId;
228 }
229
notify(const sp<InputListenerInterface> & listener) const230 void NotifyDeviceResetArgs::notify(const sp<InputListenerInterface>& listener) const {
231 listener->notifyDeviceReset(this);
232 }
233
234
235 // --- QueuedInputListener ---
236
traceEvent(const char * functionName,int32_t id)237 static inline void traceEvent(const char* functionName, int32_t id) {
238 if (ATRACE_ENABLED()) {
239 std::string message = StringPrintf("%s(id=0x%" PRIx32 ")", functionName, id);
240 ATRACE_NAME(message.c_str());
241 }
242 }
243
QueuedInputListener(const sp<InputListenerInterface> & innerListener)244 QueuedInputListener::QueuedInputListener(const sp<InputListenerInterface>& innerListener) :
245 mInnerListener(innerListener) {
246 }
247
~QueuedInputListener()248 QueuedInputListener::~QueuedInputListener() {
249 size_t count = mArgsQueue.size();
250 for (size_t i = 0; i < count; i++) {
251 delete mArgsQueue[i];
252 }
253 }
254
notifyConfigurationChanged(const NotifyConfigurationChangedArgs * args)255 void QueuedInputListener::notifyConfigurationChanged(
256 const NotifyConfigurationChangedArgs* args) {
257 traceEvent(__func__, args->id);
258 mArgsQueue.push_back(new NotifyConfigurationChangedArgs(*args));
259 }
260
notifyKey(const NotifyKeyArgs * args)261 void QueuedInputListener::notifyKey(const NotifyKeyArgs* args) {
262 traceEvent(__func__, args->id);
263 mArgsQueue.push_back(new NotifyKeyArgs(*args));
264 }
265
notifyMotion(const NotifyMotionArgs * args)266 void QueuedInputListener::notifyMotion(const NotifyMotionArgs* args) {
267 traceEvent(__func__, args->id);
268 mArgsQueue.push_back(new NotifyMotionArgs(*args));
269 }
270
notifySwitch(const NotifySwitchArgs * args)271 void QueuedInputListener::notifySwitch(const NotifySwitchArgs* args) {
272 traceEvent(__func__, args->id);
273 mArgsQueue.push_back(new NotifySwitchArgs(*args));
274 }
275
notifyDeviceReset(const NotifyDeviceResetArgs * args)276 void QueuedInputListener::notifyDeviceReset(const NotifyDeviceResetArgs* args) {
277 traceEvent(__func__, args->id);
278 mArgsQueue.push_back(new NotifyDeviceResetArgs(*args));
279 }
280
flush()281 void QueuedInputListener::flush() {
282 size_t count = mArgsQueue.size();
283 for (size_t i = 0; i < count; i++) {
284 NotifyArgs* args = mArgsQueue[i];
285 args->notify(mInnerListener);
286 delete args;
287 }
288 mArgsQueue.clear();
289 }
290
291
292 } // namespace android
293