blob: 8a15e2f40fa487284fd631f0c94db1b9201bfc95 [file] [log] [blame]
Jeff Brown5912f952013-07-01 19:10:31 -07001/*
2 * Copyright (C) 2010 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 "Input"
18//#define LOG_NDEBUG 0
19
20#include <math.h>
21#include <limits.h>
22
23#include <input/Input.h>
Michael Wright872db4f2014-04-22 15:03:51 -070024#include <input/InputEventLabels.h>
Jeff Brown5912f952013-07-01 19:10:31 -070025
Elliott Hughes6071da72015-08-12 15:27:47 -070026#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -070027#include <binder/Parcel.h>
Jeff Brown5912f952013-07-01 19:10:31 -070028#endif
29
30namespace android {
31
32// --- InputEvent ---
33
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010034void InputEvent::initialize(int32_t deviceId, int32_t source, int32_t displayId) {
Jeff Brown5912f952013-07-01 19:10:31 -070035 mDeviceId = deviceId;
36 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010037 mDisplayId = displayId;
Jeff Brown5912f952013-07-01 19:10:31 -070038}
39
40void InputEvent::initialize(const InputEvent& from) {
41 mDeviceId = from.mDeviceId;
42 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010043 mDisplayId = from.mDisplayId;
Jeff Brown5912f952013-07-01 19:10:31 -070044}
45
46// --- KeyEvent ---
47
Michael Wright872db4f2014-04-22 15:03:51 -070048const char* KeyEvent::getLabel(int32_t keyCode) {
49 return getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -070050}
51
Michael Wright872db4f2014-04-22 15:03:51 -070052int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
53 return getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -070054}
55
56void KeyEvent::initialize(
57 int32_t deviceId,
58 int32_t source,
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010059 int32_t displayId,
Jeff Brown5912f952013-07-01 19:10:31 -070060 int32_t action,
61 int32_t flags,
62 int32_t keyCode,
63 int32_t scanCode,
64 int32_t metaState,
65 int32_t repeatCount,
66 nsecs_t downTime,
67 nsecs_t eventTime) {
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010068 InputEvent::initialize(deviceId, source, displayId);
Jeff Brown5912f952013-07-01 19:10:31 -070069 mAction = action;
70 mFlags = flags;
71 mKeyCode = keyCode;
72 mScanCode = scanCode;
73 mMetaState = metaState;
74 mRepeatCount = repeatCount;
75 mDownTime = downTime;
76 mEventTime = eventTime;
77}
78
79void KeyEvent::initialize(const KeyEvent& from) {
80 InputEvent::initialize(from);
81 mAction = from.mAction;
82 mFlags = from.mFlags;
83 mKeyCode = from.mKeyCode;
84 mScanCode = from.mScanCode;
85 mMetaState = from.mMetaState;
86 mRepeatCount = from.mRepeatCount;
87 mDownTime = from.mDownTime;
88 mEventTime = from.mEventTime;
89}
90
91
92// --- PointerCoords ---
93
94float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -070095 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -070096 return 0;
97 }
Michael Wright38dcdff2014-03-19 12:06:10 -070098 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -070099}
100
101status_t PointerCoords::setAxisValue(int32_t axis, float value) {
102 if (axis < 0 || axis > 63) {
103 return NAME_NOT_FOUND;
104 }
105
Michael Wright38dcdff2014-03-19 12:06:10 -0700106 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
107 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700108 if (value == 0) {
109 return OK; // axes with value 0 do not need to be stored
110 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700111
112 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700113 if (count >= MAX_AXES) {
114 tooManyAxes(axis);
115 return NO_MEMORY;
116 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700117 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700118 for (uint32_t i = count; i > index; i--) {
119 values[i] = values[i - 1];
120 }
121 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700122
Jeff Brown5912f952013-07-01 19:10:31 -0700123 values[index] = value;
124 return OK;
125}
126
127static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
128 float value = c.getAxisValue(axis);
129 if (value != 0) {
130 c.setAxisValue(axis, value * scaleFactor);
131 }
132}
133
134void PointerCoords::scale(float scaleFactor) {
135 // No need to scale pressure or size since they are normalized.
136 // No need to scale orientation since it is meaningless to do so.
137 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, scaleFactor);
138 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, scaleFactor);
139 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, scaleFactor);
140 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, scaleFactor);
141 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, scaleFactor);
142 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, scaleFactor);
143}
144
Jeff Brownf086ddb2014-02-11 14:28:48 -0800145void PointerCoords::applyOffset(float xOffset, float yOffset) {
146 setAxisValue(AMOTION_EVENT_AXIS_X, getX() + xOffset);
147 setAxisValue(AMOTION_EVENT_AXIS_Y, getY() + yOffset);
148}
149
Elliott Hughes6071da72015-08-12 15:27:47 -0700150#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700151status_t PointerCoords::readFromParcel(Parcel* parcel) {
152 bits = parcel->readInt64();
153
Michael Wright38dcdff2014-03-19 12:06:10 -0700154 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700155 if (count > MAX_AXES) {
156 return BAD_VALUE;
157 }
158
159 for (uint32_t i = 0; i < count; i++) {
160 values[i] = parcel->readFloat();
161 }
162 return OK;
163}
164
165status_t PointerCoords::writeToParcel(Parcel* parcel) const {
166 parcel->writeInt64(bits);
167
Michael Wright38dcdff2014-03-19 12:06:10 -0700168 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700169 for (uint32_t i = 0; i < count; i++) {
170 parcel->writeFloat(values[i]);
171 }
172 return OK;
173}
174#endif
175
176void PointerCoords::tooManyAxes(int axis) {
177 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
178 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
179}
180
181bool PointerCoords::operator==(const PointerCoords& other) const {
182 if (bits != other.bits) {
183 return false;
184 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700185 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700186 for (uint32_t i = 0; i < count; i++) {
187 if (values[i] != other.values[i]) {
188 return false;
189 }
190 }
191 return true;
192}
193
194void PointerCoords::copyFrom(const PointerCoords& other) {
195 bits = other.bits;
Michael Wright38dcdff2014-03-19 12:06:10 -0700196 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700197 for (uint32_t i = 0; i < count; i++) {
198 values[i] = other.values[i];
199 }
200}
201
202
203// --- PointerProperties ---
204
205bool PointerProperties::operator==(const PointerProperties& other) const {
206 return id == other.id
207 && toolType == other.toolType;
208}
209
210void PointerProperties::copyFrom(const PointerProperties& other) {
211 id = other.id;
212 toolType = other.toolType;
213}
214
215
216// --- MotionEvent ---
217
218void MotionEvent::initialize(
219 int32_t deviceId,
220 int32_t source,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800221 int32_t displayId,
Jeff Brown5912f952013-07-01 19:10:31 -0700222 int32_t action,
Michael Wright7b159c92015-05-14 14:48:03 +0100223 int32_t actionButton,
Jeff Brown5912f952013-07-01 19:10:31 -0700224 int32_t flags,
225 int32_t edgeFlags,
226 int32_t metaState,
227 int32_t buttonState,
228 float xOffset,
229 float yOffset,
230 float xPrecision,
231 float yPrecision,
232 nsecs_t downTime,
233 nsecs_t eventTime,
234 size_t pointerCount,
235 const PointerProperties* pointerProperties,
236 const PointerCoords* pointerCoords) {
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100237 InputEvent::initialize(deviceId, source, displayId);
Jeff Brown5912f952013-07-01 19:10:31 -0700238 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100239 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700240 mFlags = flags;
241 mEdgeFlags = edgeFlags;
242 mMetaState = metaState;
243 mButtonState = buttonState;
244 mXOffset = xOffset;
245 mYOffset = yOffset;
246 mXPrecision = xPrecision;
247 mYPrecision = yPrecision;
248 mDownTime = downTime;
249 mPointerProperties.clear();
250 mPointerProperties.appendArray(pointerProperties, pointerCount);
251 mSampleEventTimes.clear();
252 mSamplePointerCoords.clear();
253 addSample(eventTime, pointerCoords);
254}
255
256void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100257 InputEvent::initialize(other->mDeviceId, other->mSource, other->mDisplayId);
Jeff Brown5912f952013-07-01 19:10:31 -0700258 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100259 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700260 mFlags = other->mFlags;
261 mEdgeFlags = other->mEdgeFlags;
262 mMetaState = other->mMetaState;
263 mButtonState = other->mButtonState;
264 mXOffset = other->mXOffset;
265 mYOffset = other->mYOffset;
266 mXPrecision = other->mXPrecision;
267 mYPrecision = other->mYPrecision;
268 mDownTime = other->mDownTime;
269 mPointerProperties = other->mPointerProperties;
270
271 if (keepHistory) {
272 mSampleEventTimes = other->mSampleEventTimes;
273 mSamplePointerCoords = other->mSamplePointerCoords;
274 } else {
275 mSampleEventTimes.clear();
276 mSampleEventTimes.push(other->getEventTime());
277 mSamplePointerCoords.clear();
278 size_t pointerCount = other->getPointerCount();
279 size_t historySize = other->getHistorySize();
280 mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array()
281 + (historySize * pointerCount), pointerCount);
282 }
283}
284
285void MotionEvent::addSample(
286 int64_t eventTime,
287 const PointerCoords* pointerCoords) {
288 mSampleEventTimes.push(eventTime);
289 mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
290}
291
292const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
293 return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
294}
295
296float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
297 return getRawPointerCoords(pointerIndex)->getAxisValue(axis);
298}
299
300float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
301 float value = getRawPointerCoords(pointerIndex)->getAxisValue(axis);
302 switch (axis) {
303 case AMOTION_EVENT_AXIS_X:
304 return value + mXOffset;
305 case AMOTION_EVENT_AXIS_Y:
306 return value + mYOffset;
307 }
308 return value;
309}
310
311const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
312 size_t pointerIndex, size_t historicalIndex) const {
313 return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
314}
315
316float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
317 size_t historicalIndex) const {
318 return getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
319}
320
321float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
322 size_t historicalIndex) const {
323 float value = getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
324 switch (axis) {
325 case AMOTION_EVENT_AXIS_X:
326 return value + mXOffset;
327 case AMOTION_EVENT_AXIS_Y:
328 return value + mYOffset;
329 }
330 return value;
331}
332
333ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
334 size_t pointerCount = mPointerProperties.size();
335 for (size_t i = 0; i < pointerCount; i++) {
336 if (mPointerProperties.itemAt(i).id == pointerId) {
337 return i;
338 }
339 }
340 return -1;
341}
342
343void MotionEvent::offsetLocation(float xOffset, float yOffset) {
344 mXOffset += xOffset;
345 mYOffset += yOffset;
346}
347
348void MotionEvent::scale(float scaleFactor) {
349 mXOffset *= scaleFactor;
350 mYOffset *= scaleFactor;
351 mXPrecision *= scaleFactor;
352 mYPrecision *= scaleFactor;
353
354 size_t numSamples = mSamplePointerCoords.size();
355 for (size_t i = 0; i < numSamples; i++) {
356 mSamplePointerCoords.editItemAt(i).scale(scaleFactor);
357 }
358}
359
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700360static void transformPoint(const float matrix[9], float x, float y, float *outX, float *outY) {
361 // Apply perspective transform like Skia.
362 float newX = matrix[0] * x + matrix[1] * y + matrix[2];
363 float newY = matrix[3] * x + matrix[4] * y + matrix[5];
364 float newZ = matrix[6] * x + matrix[7] * y + matrix[8];
365 if (newZ) {
366 newZ = 1.0f / newZ;
367 }
368 *outX = newX * newZ;
369 *outY = newY * newZ;
370}
371
372static float transformAngle(const float matrix[9], float angleRadians,
373 float originX, float originY) {
Jeff Brown5912f952013-07-01 19:10:31 -0700374 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
375 // Coordinate system: down is increasing Y, right is increasing X.
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700376 float x = sinf(angleRadians);
377 float y = -cosf(angleRadians);
378 transformPoint(matrix, x, y, &x, &y);
379 x -= originX;
380 y -= originY;
Jeff Brown5912f952013-07-01 19:10:31 -0700381
382 // Derive the transformed vector's clockwise angle from vertical.
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700383 float result = atan2f(x, -y);
Jeff Brown5912f952013-07-01 19:10:31 -0700384 if (result < - M_PI_2) {
385 result += M_PI;
386 } else if (result > M_PI_2) {
387 result -= M_PI;
388 }
389 return result;
390}
391
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700392void MotionEvent::transform(const float matrix[9]) {
Jeff Brown5912f952013-07-01 19:10:31 -0700393 // The tricky part of this implementation is to preserve the value of
394 // rawX and rawY. So we apply the transformation to the first point
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700395 // then derive an appropriate new X/Y offset that will preserve rawX
396 // and rawY for that point.
397 float oldXOffset = mXOffset;
398 float oldYOffset = mYOffset;
399 float newX, newY;
Jeff Brown5912f952013-07-01 19:10:31 -0700400 float rawX = getRawX(0);
401 float rawY = getRawY(0);
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700402 transformPoint(matrix, rawX + oldXOffset, rawY + oldYOffset, &newX, &newY);
403 mXOffset = newX - rawX;
404 mYOffset = newY - rawY;
Jeff Brown5912f952013-07-01 19:10:31 -0700405
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700406 // Determine how the origin is transformed by the matrix so that we
407 // can transform orientation vectors.
408 float originX, originY;
409 transformPoint(matrix, 0, 0, &originX, &originY);
Jeff Brown5912f952013-07-01 19:10:31 -0700410
411 // Apply the transformation to all samples.
412 size_t numSamples = mSamplePointerCoords.size();
413 for (size_t i = 0; i < numSamples; i++) {
414 PointerCoords& c = mSamplePointerCoords.editItemAt(i);
415 float x = c.getAxisValue(AMOTION_EVENT_AXIS_X) + oldXOffset;
416 float y = c.getAxisValue(AMOTION_EVENT_AXIS_Y) + oldYOffset;
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700417 transformPoint(matrix, x, y, &x, &y);
418 c.setAxisValue(AMOTION_EVENT_AXIS_X, x - mXOffset);
419 c.setAxisValue(AMOTION_EVENT_AXIS_Y, y - mYOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700420
421 float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700422 c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
423 transformAngle(matrix, orientation, originX, originY));
Jeff Brown5912f952013-07-01 19:10:31 -0700424 }
425}
426
Elliott Hughes6071da72015-08-12 15:27:47 -0700427#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700428status_t MotionEvent::readFromParcel(Parcel* parcel) {
429 size_t pointerCount = parcel->readInt32();
430 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800431 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
432 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700433 return BAD_VALUE;
434 }
435
436 mDeviceId = parcel->readInt32();
437 mSource = parcel->readInt32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800438 mDisplayId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700439 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100440 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700441 mFlags = parcel->readInt32();
442 mEdgeFlags = parcel->readInt32();
443 mMetaState = parcel->readInt32();
444 mButtonState = parcel->readInt32();
445 mXOffset = parcel->readFloat();
446 mYOffset = parcel->readFloat();
447 mXPrecision = parcel->readFloat();
448 mYPrecision = parcel->readFloat();
449 mDownTime = parcel->readInt64();
450
451 mPointerProperties.clear();
452 mPointerProperties.setCapacity(pointerCount);
453 mSampleEventTimes.clear();
454 mSampleEventTimes.setCapacity(sampleCount);
455 mSamplePointerCoords.clear();
456 mSamplePointerCoords.setCapacity(sampleCount * pointerCount);
457
458 for (size_t i = 0; i < pointerCount; i++) {
459 mPointerProperties.push();
460 PointerProperties& properties = mPointerProperties.editTop();
461 properties.id = parcel->readInt32();
462 properties.toolType = parcel->readInt32();
463 }
464
Dan Austinc94fc452015-09-22 14:22:41 -0700465 while (sampleCount > 0) {
466 sampleCount--;
Jeff Brown5912f952013-07-01 19:10:31 -0700467 mSampleEventTimes.push(parcel->readInt64());
468 for (size_t i = 0; i < pointerCount; i++) {
469 mSamplePointerCoords.push();
470 status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel);
471 if (status) {
472 return status;
473 }
474 }
475 }
476 return OK;
477}
478
479status_t MotionEvent::writeToParcel(Parcel* parcel) const {
480 size_t pointerCount = mPointerProperties.size();
481 size_t sampleCount = mSampleEventTimes.size();
482
483 parcel->writeInt32(pointerCount);
484 parcel->writeInt32(sampleCount);
485
486 parcel->writeInt32(mDeviceId);
487 parcel->writeInt32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800488 parcel->writeInt32(mDisplayId);
Jeff Brown5912f952013-07-01 19:10:31 -0700489 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100490 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700491 parcel->writeInt32(mFlags);
492 parcel->writeInt32(mEdgeFlags);
493 parcel->writeInt32(mMetaState);
494 parcel->writeInt32(mButtonState);
495 parcel->writeFloat(mXOffset);
496 parcel->writeFloat(mYOffset);
497 parcel->writeFloat(mXPrecision);
498 parcel->writeFloat(mYPrecision);
499 parcel->writeInt64(mDownTime);
500
501 for (size_t i = 0; i < pointerCount; i++) {
502 const PointerProperties& properties = mPointerProperties.itemAt(i);
503 parcel->writeInt32(properties.id);
504 parcel->writeInt32(properties.toolType);
505 }
506
507 const PointerCoords* pc = mSamplePointerCoords.array();
508 for (size_t h = 0; h < sampleCount; h++) {
509 parcel->writeInt64(mSampleEventTimes.itemAt(h));
510 for (size_t i = 0; i < pointerCount; i++) {
511 status_t status = (pc++)->writeToParcel(parcel);
512 if (status) {
513 return status;
514 }
515 }
516 }
517 return OK;
518}
519#endif
520
521bool MotionEvent::isTouchEvent(int32_t source, int32_t action) {
522 if (source & AINPUT_SOURCE_CLASS_POINTER) {
523 // Specifically excludes HOVER_MOVE and SCROLL.
524 switch (action & AMOTION_EVENT_ACTION_MASK) {
525 case AMOTION_EVENT_ACTION_DOWN:
526 case AMOTION_EVENT_ACTION_MOVE:
527 case AMOTION_EVENT_ACTION_UP:
528 case AMOTION_EVENT_ACTION_POINTER_DOWN:
529 case AMOTION_EVENT_ACTION_POINTER_UP:
530 case AMOTION_EVENT_ACTION_CANCEL:
531 case AMOTION_EVENT_ACTION_OUTSIDE:
532 return true;
533 }
534 }
535 return false;
536}
537
Michael Wright872db4f2014-04-22 15:03:51 -0700538const char* MotionEvent::getLabel(int32_t axis) {
539 return getAxisLabel(axis);
540}
541
542int32_t MotionEvent::getAxisFromLabel(const char* label) {
543 return getAxisByLabel(label);
544}
545
Jeff Brown5912f952013-07-01 19:10:31 -0700546
547// --- PooledInputEventFactory ---
548
549PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
550 mMaxPoolSize(maxPoolSize) {
551}
552
553PooledInputEventFactory::~PooledInputEventFactory() {
554 for (size_t i = 0; i < mKeyEventPool.size(); i++) {
555 delete mKeyEventPool.itemAt(i);
556 }
557 for (size_t i = 0; i < mMotionEventPool.size(); i++) {
558 delete mMotionEventPool.itemAt(i);
559 }
560}
561
562KeyEvent* PooledInputEventFactory::createKeyEvent() {
563 if (!mKeyEventPool.isEmpty()) {
564 KeyEvent* event = mKeyEventPool.top();
565 mKeyEventPool.pop();
566 return event;
567 }
568 return new KeyEvent();
569}
570
571MotionEvent* PooledInputEventFactory::createMotionEvent() {
572 if (!mMotionEventPool.isEmpty()) {
573 MotionEvent* event = mMotionEventPool.top();
574 mMotionEventPool.pop();
575 return event;
576 }
577 return new MotionEvent();
578}
579
580void PooledInputEventFactory::recycle(InputEvent* event) {
581 switch (event->getType()) {
582 case AINPUT_EVENT_TYPE_KEY:
583 if (mKeyEventPool.size() < mMaxPoolSize) {
584 mKeyEventPool.push(static_cast<KeyEvent*>(event));
585 return;
586 }
587 break;
588 case AINPUT_EVENT_TYPE_MOTION:
589 if (mMotionEventPool.size() < mMaxPoolSize) {
590 mMotionEventPool.push(static_cast<MotionEvent*>(event));
591 return;
592 }
593 break;
594 }
595 delete event;
596}
597
598} // namespace android