blob: 9fd25f9cb7b31f7f4ac8f601c747fb4b950b32bb [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
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -080032const char* motionClassificationToString(MotionClassification classification) {
33 switch (classification) {
34 case MotionClassification::NONE:
35 return "NONE";
36 case MotionClassification::AMBIGUOUS_GESTURE:
37 return "AMBIGUOUS_GESTURE";
38 case MotionClassification::DEEP_PRESS:
39 return "DEEP_PRESS";
40 }
41}
42
Jeff Brown5912f952013-07-01 19:10:31 -070043// --- InputEvent ---
44
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010045void InputEvent::initialize(int32_t deviceId, int32_t source, int32_t displayId) {
Jeff Brown5912f952013-07-01 19:10:31 -070046 mDeviceId = deviceId;
47 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010048 mDisplayId = displayId;
Jeff Brown5912f952013-07-01 19:10:31 -070049}
50
51void InputEvent::initialize(const InputEvent& from) {
52 mDeviceId = from.mDeviceId;
53 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010054 mDisplayId = from.mDisplayId;
Jeff Brown5912f952013-07-01 19:10:31 -070055}
56
57// --- KeyEvent ---
58
Michael Wright872db4f2014-04-22 15:03:51 -070059const char* KeyEvent::getLabel(int32_t keyCode) {
60 return getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -070061}
62
Michael Wright872db4f2014-04-22 15:03:51 -070063int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
64 return getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -070065}
66
67void KeyEvent::initialize(
68 int32_t deviceId,
69 int32_t source,
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010070 int32_t displayId,
Jeff Brown5912f952013-07-01 19:10:31 -070071 int32_t action,
72 int32_t flags,
73 int32_t keyCode,
74 int32_t scanCode,
75 int32_t metaState,
76 int32_t repeatCount,
77 nsecs_t downTime,
78 nsecs_t eventTime) {
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010079 InputEvent::initialize(deviceId, source, displayId);
Jeff Brown5912f952013-07-01 19:10:31 -070080 mAction = action;
81 mFlags = flags;
82 mKeyCode = keyCode;
83 mScanCode = scanCode;
84 mMetaState = metaState;
85 mRepeatCount = repeatCount;
86 mDownTime = downTime;
87 mEventTime = eventTime;
88}
89
90void KeyEvent::initialize(const KeyEvent& from) {
91 InputEvent::initialize(from);
92 mAction = from.mAction;
93 mFlags = from.mFlags;
94 mKeyCode = from.mKeyCode;
95 mScanCode = from.mScanCode;
96 mMetaState = from.mMetaState;
97 mRepeatCount = from.mRepeatCount;
98 mDownTime = from.mDownTime;
99 mEventTime = from.mEventTime;
100}
101
102
103// --- PointerCoords ---
104
105float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700106 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700107 return 0;
108 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700109 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700110}
111
112status_t PointerCoords::setAxisValue(int32_t axis, float value) {
113 if (axis < 0 || axis > 63) {
114 return NAME_NOT_FOUND;
115 }
116
Michael Wright38dcdff2014-03-19 12:06:10 -0700117 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
118 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700119 if (value == 0) {
120 return OK; // axes with value 0 do not need to be stored
121 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700122
123 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700124 if (count >= MAX_AXES) {
125 tooManyAxes(axis);
126 return NO_MEMORY;
127 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700128 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700129 for (uint32_t i = count; i > index; i--) {
130 values[i] = values[i - 1];
131 }
132 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700133
Jeff Brown5912f952013-07-01 19:10:31 -0700134 values[index] = value;
135 return OK;
136}
137
138static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
139 float value = c.getAxisValue(axis);
140 if (value != 0) {
141 c.setAxisValue(axis, value * scaleFactor);
142 }
143}
144
Robert Carre07e1032018-11-26 12:55:53 -0800145void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700146 // No need to scale pressure or size since they are normalized.
147 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800148
149 // If there is a global scale factor, it is included in the windowX/YScale
150 // so we don't need to apply it twice to the X/Y axes.
151 // However we don't want to apply any windowXYScale not included in the global scale
152 // to the TOUCH_MAJOR/MINOR coordinates.
153 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
154 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
155 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
156 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
157 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
158 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
159}
160
161void PointerCoords::scale(float globalScaleFactor) {
162 scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700163}
164
Jeff Brownf086ddb2014-02-11 14:28:48 -0800165void PointerCoords::applyOffset(float xOffset, float yOffset) {
166 setAxisValue(AMOTION_EVENT_AXIS_X, getX() + xOffset);
167 setAxisValue(AMOTION_EVENT_AXIS_Y, getY() + yOffset);
168}
169
Elliott Hughes6071da72015-08-12 15:27:47 -0700170#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700171status_t PointerCoords::readFromParcel(Parcel* parcel) {
172 bits = parcel->readInt64();
173
Michael Wright38dcdff2014-03-19 12:06:10 -0700174 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700175 if (count > MAX_AXES) {
176 return BAD_VALUE;
177 }
178
179 for (uint32_t i = 0; i < count; i++) {
180 values[i] = parcel->readFloat();
181 }
182 return OK;
183}
184
185status_t PointerCoords::writeToParcel(Parcel* parcel) const {
186 parcel->writeInt64(bits);
187
Michael Wright38dcdff2014-03-19 12:06:10 -0700188 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700189 for (uint32_t i = 0; i < count; i++) {
190 parcel->writeFloat(values[i]);
191 }
192 return OK;
193}
194#endif
195
196void PointerCoords::tooManyAxes(int axis) {
197 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
198 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
199}
200
201bool PointerCoords::operator==(const PointerCoords& other) const {
202 if (bits != other.bits) {
203 return false;
204 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700205 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700206 for (uint32_t i = 0; i < count; i++) {
207 if (values[i] != other.values[i]) {
208 return false;
209 }
210 }
211 return true;
212}
213
214void PointerCoords::copyFrom(const PointerCoords& other) {
215 bits = other.bits;
Michael Wright38dcdff2014-03-19 12:06:10 -0700216 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700217 for (uint32_t i = 0; i < count; i++) {
218 values[i] = other.values[i];
219 }
220}
221
222
223// --- PointerProperties ---
224
225bool PointerProperties::operator==(const PointerProperties& other) const {
226 return id == other.id
227 && toolType == other.toolType;
228}
229
230void PointerProperties::copyFrom(const PointerProperties& other) {
231 id = other.id;
232 toolType = other.toolType;
233}
234
235
236// --- MotionEvent ---
237
238void MotionEvent::initialize(
239 int32_t deviceId,
240 int32_t source,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800241 int32_t displayId,
Jeff Brown5912f952013-07-01 19:10:31 -0700242 int32_t action,
Michael Wright7b159c92015-05-14 14:48:03 +0100243 int32_t actionButton,
Jeff Brown5912f952013-07-01 19:10:31 -0700244 int32_t flags,
245 int32_t edgeFlags,
246 int32_t metaState,
247 int32_t buttonState,
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800248 MotionClassification classification,
Jeff Brown5912f952013-07-01 19:10:31 -0700249 float xOffset,
250 float yOffset,
251 float xPrecision,
252 float yPrecision,
253 nsecs_t downTime,
254 nsecs_t eventTime,
255 size_t pointerCount,
256 const PointerProperties* pointerProperties,
257 const PointerCoords* pointerCoords) {
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100258 InputEvent::initialize(deviceId, source, displayId);
Jeff Brown5912f952013-07-01 19:10:31 -0700259 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100260 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700261 mFlags = flags;
262 mEdgeFlags = edgeFlags;
263 mMetaState = metaState;
264 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800265 mClassification = classification;
Jeff Brown5912f952013-07-01 19:10:31 -0700266 mXOffset = xOffset;
267 mYOffset = yOffset;
268 mXPrecision = xPrecision;
269 mYPrecision = yPrecision;
270 mDownTime = downTime;
271 mPointerProperties.clear();
272 mPointerProperties.appendArray(pointerProperties, pointerCount);
273 mSampleEventTimes.clear();
274 mSamplePointerCoords.clear();
275 addSample(eventTime, pointerCoords);
276}
277
278void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100279 InputEvent::initialize(other->mDeviceId, other->mSource, other->mDisplayId);
Jeff Brown5912f952013-07-01 19:10:31 -0700280 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100281 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700282 mFlags = other->mFlags;
283 mEdgeFlags = other->mEdgeFlags;
284 mMetaState = other->mMetaState;
285 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800286 mClassification = other->mClassification;
Jeff Brown5912f952013-07-01 19:10:31 -0700287 mXOffset = other->mXOffset;
288 mYOffset = other->mYOffset;
289 mXPrecision = other->mXPrecision;
290 mYPrecision = other->mYPrecision;
291 mDownTime = other->mDownTime;
292 mPointerProperties = other->mPointerProperties;
293
294 if (keepHistory) {
295 mSampleEventTimes = other->mSampleEventTimes;
296 mSamplePointerCoords = other->mSamplePointerCoords;
297 } else {
298 mSampleEventTimes.clear();
299 mSampleEventTimes.push(other->getEventTime());
300 mSamplePointerCoords.clear();
301 size_t pointerCount = other->getPointerCount();
302 size_t historySize = other->getHistorySize();
303 mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array()
304 + (historySize * pointerCount), pointerCount);
305 }
306}
307
308void MotionEvent::addSample(
309 int64_t eventTime,
310 const PointerCoords* pointerCoords) {
311 mSampleEventTimes.push(eventTime);
312 mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
313}
314
315const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
316 return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
317}
318
319float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
320 return getRawPointerCoords(pointerIndex)->getAxisValue(axis);
321}
322
323float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
324 float value = getRawPointerCoords(pointerIndex)->getAxisValue(axis);
325 switch (axis) {
326 case AMOTION_EVENT_AXIS_X:
327 return value + mXOffset;
328 case AMOTION_EVENT_AXIS_Y:
329 return value + mYOffset;
330 }
331 return value;
332}
333
334const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
335 size_t pointerIndex, size_t historicalIndex) const {
336 return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
337}
338
339float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
340 size_t historicalIndex) const {
341 return getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
342}
343
344float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
345 size_t historicalIndex) const {
346 float value = getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
347 switch (axis) {
348 case AMOTION_EVENT_AXIS_X:
349 return value + mXOffset;
350 case AMOTION_EVENT_AXIS_Y:
351 return value + mYOffset;
352 }
353 return value;
354}
355
356ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
357 size_t pointerCount = mPointerProperties.size();
358 for (size_t i = 0; i < pointerCount; i++) {
359 if (mPointerProperties.itemAt(i).id == pointerId) {
360 return i;
361 }
362 }
363 return -1;
364}
365
366void MotionEvent::offsetLocation(float xOffset, float yOffset) {
367 mXOffset += xOffset;
368 mYOffset += yOffset;
369}
370
Robert Carre07e1032018-11-26 12:55:53 -0800371void MotionEvent::scale(float globalScaleFactor) {
372 mXOffset *= globalScaleFactor;
373 mYOffset *= globalScaleFactor;
374 mXPrecision *= globalScaleFactor;
375 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700376
377 size_t numSamples = mSamplePointerCoords.size();
378 for (size_t i = 0; i < numSamples; i++) {
Robert Carre07e1032018-11-26 12:55:53 -0800379 mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700380 }
381}
382
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700383static void transformPoint(const float matrix[9], float x, float y, float *outX, float *outY) {
384 // Apply perspective transform like Skia.
385 float newX = matrix[0] * x + matrix[1] * y + matrix[2];
386 float newY = matrix[3] * x + matrix[4] * y + matrix[5];
387 float newZ = matrix[6] * x + matrix[7] * y + matrix[8];
388 if (newZ) {
389 newZ = 1.0f / newZ;
390 }
391 *outX = newX * newZ;
392 *outY = newY * newZ;
393}
394
395static float transformAngle(const float matrix[9], float angleRadians,
396 float originX, float originY) {
Jeff Brown5912f952013-07-01 19:10:31 -0700397 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
398 // Coordinate system: down is increasing Y, right is increasing X.
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700399 float x = sinf(angleRadians);
400 float y = -cosf(angleRadians);
401 transformPoint(matrix, x, y, &x, &y);
402 x -= originX;
403 y -= originY;
Jeff Brown5912f952013-07-01 19:10:31 -0700404
405 // Derive the transformed vector's clockwise angle from vertical.
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700406 float result = atan2f(x, -y);
Jeff Brown5912f952013-07-01 19:10:31 -0700407 if (result < - M_PI_2) {
408 result += M_PI;
409 } else if (result > M_PI_2) {
410 result -= M_PI;
411 }
412 return result;
413}
414
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700415void MotionEvent::transform(const float matrix[9]) {
Jeff Brown5912f952013-07-01 19:10:31 -0700416 // The tricky part of this implementation is to preserve the value of
417 // rawX and rawY. So we apply the transformation to the first point
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700418 // then derive an appropriate new X/Y offset that will preserve rawX
419 // and rawY for that point.
420 float oldXOffset = mXOffset;
421 float oldYOffset = mYOffset;
422 float newX, newY;
Jeff Brown5912f952013-07-01 19:10:31 -0700423 float rawX = getRawX(0);
424 float rawY = getRawY(0);
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700425 transformPoint(matrix, rawX + oldXOffset, rawY + oldYOffset, &newX, &newY);
426 mXOffset = newX - rawX;
427 mYOffset = newY - rawY;
Jeff Brown5912f952013-07-01 19:10:31 -0700428
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700429 // Determine how the origin is transformed by the matrix so that we
430 // can transform orientation vectors.
431 float originX, originY;
432 transformPoint(matrix, 0, 0, &originX, &originY);
Jeff Brown5912f952013-07-01 19:10:31 -0700433
434 // Apply the transformation to all samples.
435 size_t numSamples = mSamplePointerCoords.size();
436 for (size_t i = 0; i < numSamples; i++) {
437 PointerCoords& c = mSamplePointerCoords.editItemAt(i);
438 float x = c.getAxisValue(AMOTION_EVENT_AXIS_X) + oldXOffset;
439 float y = c.getAxisValue(AMOTION_EVENT_AXIS_Y) + oldYOffset;
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700440 transformPoint(matrix, x, y, &x, &y);
441 c.setAxisValue(AMOTION_EVENT_AXIS_X, x - mXOffset);
442 c.setAxisValue(AMOTION_EVENT_AXIS_Y, y - mYOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700443
444 float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700445 c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
446 transformAngle(matrix, orientation, originX, originY));
Jeff Brown5912f952013-07-01 19:10:31 -0700447 }
448}
449
Elliott Hughes6071da72015-08-12 15:27:47 -0700450#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700451status_t MotionEvent::readFromParcel(Parcel* parcel) {
452 size_t pointerCount = parcel->readInt32();
453 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800454 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
455 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700456 return BAD_VALUE;
457 }
458
459 mDeviceId = parcel->readInt32();
460 mSource = parcel->readInt32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800461 mDisplayId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700462 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100463 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700464 mFlags = parcel->readInt32();
465 mEdgeFlags = parcel->readInt32();
466 mMetaState = parcel->readInt32();
467 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800468 mClassification = static_cast<MotionClassification>(parcel->readByte());
Jeff Brown5912f952013-07-01 19:10:31 -0700469 mXOffset = parcel->readFloat();
470 mYOffset = parcel->readFloat();
471 mXPrecision = parcel->readFloat();
472 mYPrecision = parcel->readFloat();
473 mDownTime = parcel->readInt64();
474
475 mPointerProperties.clear();
476 mPointerProperties.setCapacity(pointerCount);
477 mSampleEventTimes.clear();
478 mSampleEventTimes.setCapacity(sampleCount);
479 mSamplePointerCoords.clear();
480 mSamplePointerCoords.setCapacity(sampleCount * pointerCount);
481
482 for (size_t i = 0; i < pointerCount; i++) {
483 mPointerProperties.push();
484 PointerProperties& properties = mPointerProperties.editTop();
485 properties.id = parcel->readInt32();
486 properties.toolType = parcel->readInt32();
487 }
488
Dan Austinc94fc452015-09-22 14:22:41 -0700489 while (sampleCount > 0) {
490 sampleCount--;
Jeff Brown5912f952013-07-01 19:10:31 -0700491 mSampleEventTimes.push(parcel->readInt64());
492 for (size_t i = 0; i < pointerCount; i++) {
493 mSamplePointerCoords.push();
494 status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel);
495 if (status) {
496 return status;
497 }
498 }
499 }
500 return OK;
501}
502
503status_t MotionEvent::writeToParcel(Parcel* parcel) const {
504 size_t pointerCount = mPointerProperties.size();
505 size_t sampleCount = mSampleEventTimes.size();
506
507 parcel->writeInt32(pointerCount);
508 parcel->writeInt32(sampleCount);
509
510 parcel->writeInt32(mDeviceId);
511 parcel->writeInt32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800512 parcel->writeInt32(mDisplayId);
Jeff Brown5912f952013-07-01 19:10:31 -0700513 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100514 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700515 parcel->writeInt32(mFlags);
516 parcel->writeInt32(mEdgeFlags);
517 parcel->writeInt32(mMetaState);
518 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800519 parcel->writeByte(static_cast<int8_t>(mClassification));
Jeff Brown5912f952013-07-01 19:10:31 -0700520 parcel->writeFloat(mXOffset);
521 parcel->writeFloat(mYOffset);
522 parcel->writeFloat(mXPrecision);
523 parcel->writeFloat(mYPrecision);
524 parcel->writeInt64(mDownTime);
525
526 for (size_t i = 0; i < pointerCount; i++) {
527 const PointerProperties& properties = mPointerProperties.itemAt(i);
528 parcel->writeInt32(properties.id);
529 parcel->writeInt32(properties.toolType);
530 }
531
532 const PointerCoords* pc = mSamplePointerCoords.array();
533 for (size_t h = 0; h < sampleCount; h++) {
534 parcel->writeInt64(mSampleEventTimes.itemAt(h));
535 for (size_t i = 0; i < pointerCount; i++) {
536 status_t status = (pc++)->writeToParcel(parcel);
537 if (status) {
538 return status;
539 }
540 }
541 }
542 return OK;
543}
544#endif
545
546bool MotionEvent::isTouchEvent(int32_t source, int32_t action) {
547 if (source & AINPUT_SOURCE_CLASS_POINTER) {
548 // Specifically excludes HOVER_MOVE and SCROLL.
549 switch (action & AMOTION_EVENT_ACTION_MASK) {
550 case AMOTION_EVENT_ACTION_DOWN:
551 case AMOTION_EVENT_ACTION_MOVE:
552 case AMOTION_EVENT_ACTION_UP:
553 case AMOTION_EVENT_ACTION_POINTER_DOWN:
554 case AMOTION_EVENT_ACTION_POINTER_UP:
555 case AMOTION_EVENT_ACTION_CANCEL:
556 case AMOTION_EVENT_ACTION_OUTSIDE:
557 return true;
558 }
559 }
560 return false;
561}
562
Michael Wright872db4f2014-04-22 15:03:51 -0700563const char* MotionEvent::getLabel(int32_t axis) {
564 return getAxisLabel(axis);
565}
566
567int32_t MotionEvent::getAxisFromLabel(const char* label) {
568 return getAxisByLabel(label);
569}
570
Jeff Brown5912f952013-07-01 19:10:31 -0700571
572// --- PooledInputEventFactory ---
573
574PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
575 mMaxPoolSize(maxPoolSize) {
576}
577
578PooledInputEventFactory::~PooledInputEventFactory() {
579 for (size_t i = 0; i < mKeyEventPool.size(); i++) {
580 delete mKeyEventPool.itemAt(i);
581 }
582 for (size_t i = 0; i < mMotionEventPool.size(); i++) {
583 delete mMotionEventPool.itemAt(i);
584 }
585}
586
587KeyEvent* PooledInputEventFactory::createKeyEvent() {
588 if (!mKeyEventPool.isEmpty()) {
589 KeyEvent* event = mKeyEventPool.top();
590 mKeyEventPool.pop();
591 return event;
592 }
593 return new KeyEvent();
594}
595
596MotionEvent* PooledInputEventFactory::createMotionEvent() {
597 if (!mMotionEventPool.isEmpty()) {
598 MotionEvent* event = mMotionEventPool.top();
599 mMotionEventPool.pop();
600 return event;
601 }
602 return new MotionEvent();
603}
604
605void PooledInputEventFactory::recycle(InputEvent* event) {
606 switch (event->getType()) {
607 case AINPUT_EVENT_TYPE_KEY:
608 if (mKeyEventPool.size() < mMaxPoolSize) {
609 mKeyEventPool.push(static_cast<KeyEvent*>(event));
610 return;
611 }
612 break;
613 case AINPUT_EVENT_TYPE_MOTION:
614 if (mMotionEventPool.size() < mMaxPoolSize) {
615 mMotionEventPool.push(static_cast<MotionEvent*>(event));
616 return;
617 }
618 break;
619 }
620 delete event;
621}
622
623} // namespace android