blob: a558970b58c73a2146efb1e67c67357b969d13be [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
Robert Carre07e1032018-11-26 12:55:53 -0800134void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700135 // No need to scale pressure or size since they are normalized.
136 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800137
138 // If there is a global scale factor, it is included in the windowX/YScale
139 // so we don't need to apply it twice to the X/Y axes.
140 // However we don't want to apply any windowXYScale not included in the global scale
141 // to the TOUCH_MAJOR/MINOR coordinates.
142 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
143 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
144 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
145 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
146 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
147 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
148}
149
150void PointerCoords::scale(float globalScaleFactor) {
151 scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700152}
153
Jeff Brownf086ddb2014-02-11 14:28:48 -0800154void PointerCoords::applyOffset(float xOffset, float yOffset) {
155 setAxisValue(AMOTION_EVENT_AXIS_X, getX() + xOffset);
156 setAxisValue(AMOTION_EVENT_AXIS_Y, getY() + yOffset);
157}
158
Elliott Hughes6071da72015-08-12 15:27:47 -0700159#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700160status_t PointerCoords::readFromParcel(Parcel* parcel) {
161 bits = parcel->readInt64();
162
Michael Wright38dcdff2014-03-19 12:06:10 -0700163 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700164 if (count > MAX_AXES) {
165 return BAD_VALUE;
166 }
167
168 for (uint32_t i = 0; i < count; i++) {
169 values[i] = parcel->readFloat();
170 }
171 return OK;
172}
173
174status_t PointerCoords::writeToParcel(Parcel* parcel) const {
175 parcel->writeInt64(bits);
176
Michael Wright38dcdff2014-03-19 12:06:10 -0700177 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700178 for (uint32_t i = 0; i < count; i++) {
179 parcel->writeFloat(values[i]);
180 }
181 return OK;
182}
183#endif
184
185void PointerCoords::tooManyAxes(int axis) {
186 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
187 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
188}
189
190bool PointerCoords::operator==(const PointerCoords& other) const {
191 if (bits != other.bits) {
192 return false;
193 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700194 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700195 for (uint32_t i = 0; i < count; i++) {
196 if (values[i] != other.values[i]) {
197 return false;
198 }
199 }
200 return true;
201}
202
203void PointerCoords::copyFrom(const PointerCoords& other) {
204 bits = other.bits;
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 values[i] = other.values[i];
208 }
209}
210
211
212// --- PointerProperties ---
213
214bool PointerProperties::operator==(const PointerProperties& other) const {
215 return id == other.id
216 && toolType == other.toolType;
217}
218
219void PointerProperties::copyFrom(const PointerProperties& other) {
220 id = other.id;
221 toolType = other.toolType;
222}
223
224
225// --- MotionEvent ---
226
227void MotionEvent::initialize(
228 int32_t deviceId,
229 int32_t source,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800230 int32_t displayId,
Jeff Brown5912f952013-07-01 19:10:31 -0700231 int32_t action,
Michael Wright7b159c92015-05-14 14:48:03 +0100232 int32_t actionButton,
Jeff Brown5912f952013-07-01 19:10:31 -0700233 int32_t flags,
234 int32_t edgeFlags,
235 int32_t metaState,
236 int32_t buttonState,
237 float xOffset,
238 float yOffset,
239 float xPrecision,
240 float yPrecision,
241 nsecs_t downTime,
242 nsecs_t eventTime,
243 size_t pointerCount,
244 const PointerProperties* pointerProperties,
245 const PointerCoords* pointerCoords) {
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100246 InputEvent::initialize(deviceId, source, displayId);
Jeff Brown5912f952013-07-01 19:10:31 -0700247 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100248 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700249 mFlags = flags;
250 mEdgeFlags = edgeFlags;
251 mMetaState = metaState;
252 mButtonState = buttonState;
253 mXOffset = xOffset;
254 mYOffset = yOffset;
255 mXPrecision = xPrecision;
256 mYPrecision = yPrecision;
257 mDownTime = downTime;
258 mPointerProperties.clear();
259 mPointerProperties.appendArray(pointerProperties, pointerCount);
260 mSampleEventTimes.clear();
261 mSamplePointerCoords.clear();
262 addSample(eventTime, pointerCoords);
263}
264
265void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100266 InputEvent::initialize(other->mDeviceId, other->mSource, other->mDisplayId);
Jeff Brown5912f952013-07-01 19:10:31 -0700267 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100268 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700269 mFlags = other->mFlags;
270 mEdgeFlags = other->mEdgeFlags;
271 mMetaState = other->mMetaState;
272 mButtonState = other->mButtonState;
273 mXOffset = other->mXOffset;
274 mYOffset = other->mYOffset;
275 mXPrecision = other->mXPrecision;
276 mYPrecision = other->mYPrecision;
277 mDownTime = other->mDownTime;
278 mPointerProperties = other->mPointerProperties;
279
280 if (keepHistory) {
281 mSampleEventTimes = other->mSampleEventTimes;
282 mSamplePointerCoords = other->mSamplePointerCoords;
283 } else {
284 mSampleEventTimes.clear();
285 mSampleEventTimes.push(other->getEventTime());
286 mSamplePointerCoords.clear();
287 size_t pointerCount = other->getPointerCount();
288 size_t historySize = other->getHistorySize();
289 mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array()
290 + (historySize * pointerCount), pointerCount);
291 }
292}
293
294void MotionEvent::addSample(
295 int64_t eventTime,
296 const PointerCoords* pointerCoords) {
297 mSampleEventTimes.push(eventTime);
298 mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
299}
300
301const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
302 return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
303}
304
305float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
306 return getRawPointerCoords(pointerIndex)->getAxisValue(axis);
307}
308
309float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
310 float value = getRawPointerCoords(pointerIndex)->getAxisValue(axis);
311 switch (axis) {
312 case AMOTION_EVENT_AXIS_X:
313 return value + mXOffset;
314 case AMOTION_EVENT_AXIS_Y:
315 return value + mYOffset;
316 }
317 return value;
318}
319
320const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
321 size_t pointerIndex, size_t historicalIndex) const {
322 return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
323}
324
325float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
326 size_t historicalIndex) const {
327 return getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
328}
329
330float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
331 size_t historicalIndex) const {
332 float value = getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
333 switch (axis) {
334 case AMOTION_EVENT_AXIS_X:
335 return value + mXOffset;
336 case AMOTION_EVENT_AXIS_Y:
337 return value + mYOffset;
338 }
339 return value;
340}
341
342ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
343 size_t pointerCount = mPointerProperties.size();
344 for (size_t i = 0; i < pointerCount; i++) {
345 if (mPointerProperties.itemAt(i).id == pointerId) {
346 return i;
347 }
348 }
349 return -1;
350}
351
352void MotionEvent::offsetLocation(float xOffset, float yOffset) {
353 mXOffset += xOffset;
354 mYOffset += yOffset;
355}
356
Robert Carre07e1032018-11-26 12:55:53 -0800357void MotionEvent::scale(float globalScaleFactor) {
358 mXOffset *= globalScaleFactor;
359 mYOffset *= globalScaleFactor;
360 mXPrecision *= globalScaleFactor;
361 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700362
363 size_t numSamples = mSamplePointerCoords.size();
364 for (size_t i = 0; i < numSamples; i++) {
Robert Carre07e1032018-11-26 12:55:53 -0800365 mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700366 }
367}
368
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700369static void transformPoint(const float matrix[9], float x, float y, float *outX, float *outY) {
370 // Apply perspective transform like Skia.
371 float newX = matrix[0] * x + matrix[1] * y + matrix[2];
372 float newY = matrix[3] * x + matrix[4] * y + matrix[5];
373 float newZ = matrix[6] * x + matrix[7] * y + matrix[8];
374 if (newZ) {
375 newZ = 1.0f / newZ;
376 }
377 *outX = newX * newZ;
378 *outY = newY * newZ;
379}
380
381static float transformAngle(const float matrix[9], float angleRadians,
382 float originX, float originY) {
Jeff Brown5912f952013-07-01 19:10:31 -0700383 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
384 // Coordinate system: down is increasing Y, right is increasing X.
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700385 float x = sinf(angleRadians);
386 float y = -cosf(angleRadians);
387 transformPoint(matrix, x, y, &x, &y);
388 x -= originX;
389 y -= originY;
Jeff Brown5912f952013-07-01 19:10:31 -0700390
391 // Derive the transformed vector's clockwise angle from vertical.
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700392 float result = atan2f(x, -y);
Jeff Brown5912f952013-07-01 19:10:31 -0700393 if (result < - M_PI_2) {
394 result += M_PI;
395 } else if (result > M_PI_2) {
396 result -= M_PI;
397 }
398 return result;
399}
400
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700401void MotionEvent::transform(const float matrix[9]) {
Jeff Brown5912f952013-07-01 19:10:31 -0700402 // The tricky part of this implementation is to preserve the value of
403 // rawX and rawY. So we apply the transformation to the first point
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700404 // then derive an appropriate new X/Y offset that will preserve rawX
405 // and rawY for that point.
406 float oldXOffset = mXOffset;
407 float oldYOffset = mYOffset;
408 float newX, newY;
Jeff Brown5912f952013-07-01 19:10:31 -0700409 float rawX = getRawX(0);
410 float rawY = getRawY(0);
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700411 transformPoint(matrix, rawX + oldXOffset, rawY + oldYOffset, &newX, &newY);
412 mXOffset = newX - rawX;
413 mYOffset = newY - rawY;
Jeff Brown5912f952013-07-01 19:10:31 -0700414
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700415 // Determine how the origin is transformed by the matrix so that we
416 // can transform orientation vectors.
417 float originX, originY;
418 transformPoint(matrix, 0, 0, &originX, &originY);
Jeff Brown5912f952013-07-01 19:10:31 -0700419
420 // Apply the transformation to all samples.
421 size_t numSamples = mSamplePointerCoords.size();
422 for (size_t i = 0; i < numSamples; i++) {
423 PointerCoords& c = mSamplePointerCoords.editItemAt(i);
424 float x = c.getAxisValue(AMOTION_EVENT_AXIS_X) + oldXOffset;
425 float y = c.getAxisValue(AMOTION_EVENT_AXIS_Y) + oldYOffset;
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700426 transformPoint(matrix, x, y, &x, &y);
427 c.setAxisValue(AMOTION_EVENT_AXIS_X, x - mXOffset);
428 c.setAxisValue(AMOTION_EVENT_AXIS_Y, y - mYOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700429
430 float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700431 c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
432 transformAngle(matrix, orientation, originX, originY));
Jeff Brown5912f952013-07-01 19:10:31 -0700433 }
434}
435
Elliott Hughes6071da72015-08-12 15:27:47 -0700436#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700437status_t MotionEvent::readFromParcel(Parcel* parcel) {
438 size_t pointerCount = parcel->readInt32();
439 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800440 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
441 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700442 return BAD_VALUE;
443 }
444
445 mDeviceId = parcel->readInt32();
446 mSource = parcel->readInt32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800447 mDisplayId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700448 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100449 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700450 mFlags = parcel->readInt32();
451 mEdgeFlags = parcel->readInt32();
452 mMetaState = parcel->readInt32();
453 mButtonState = parcel->readInt32();
454 mXOffset = parcel->readFloat();
455 mYOffset = parcel->readFloat();
456 mXPrecision = parcel->readFloat();
457 mYPrecision = parcel->readFloat();
458 mDownTime = parcel->readInt64();
459
460 mPointerProperties.clear();
461 mPointerProperties.setCapacity(pointerCount);
462 mSampleEventTimes.clear();
463 mSampleEventTimes.setCapacity(sampleCount);
464 mSamplePointerCoords.clear();
465 mSamplePointerCoords.setCapacity(sampleCount * pointerCount);
466
467 for (size_t i = 0; i < pointerCount; i++) {
468 mPointerProperties.push();
469 PointerProperties& properties = mPointerProperties.editTop();
470 properties.id = parcel->readInt32();
471 properties.toolType = parcel->readInt32();
472 }
473
Dan Austinc94fc452015-09-22 14:22:41 -0700474 while (sampleCount > 0) {
475 sampleCount--;
Jeff Brown5912f952013-07-01 19:10:31 -0700476 mSampleEventTimes.push(parcel->readInt64());
477 for (size_t i = 0; i < pointerCount; i++) {
478 mSamplePointerCoords.push();
479 status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel);
480 if (status) {
481 return status;
482 }
483 }
484 }
485 return OK;
486}
487
488status_t MotionEvent::writeToParcel(Parcel* parcel) const {
489 size_t pointerCount = mPointerProperties.size();
490 size_t sampleCount = mSampleEventTimes.size();
491
492 parcel->writeInt32(pointerCount);
493 parcel->writeInt32(sampleCount);
494
495 parcel->writeInt32(mDeviceId);
496 parcel->writeInt32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800497 parcel->writeInt32(mDisplayId);
Jeff Brown5912f952013-07-01 19:10:31 -0700498 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100499 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700500 parcel->writeInt32(mFlags);
501 parcel->writeInt32(mEdgeFlags);
502 parcel->writeInt32(mMetaState);
503 parcel->writeInt32(mButtonState);
504 parcel->writeFloat(mXOffset);
505 parcel->writeFloat(mYOffset);
506 parcel->writeFloat(mXPrecision);
507 parcel->writeFloat(mYPrecision);
508 parcel->writeInt64(mDownTime);
509
510 for (size_t i = 0; i < pointerCount; i++) {
511 const PointerProperties& properties = mPointerProperties.itemAt(i);
512 parcel->writeInt32(properties.id);
513 parcel->writeInt32(properties.toolType);
514 }
515
516 const PointerCoords* pc = mSamplePointerCoords.array();
517 for (size_t h = 0; h < sampleCount; h++) {
518 parcel->writeInt64(mSampleEventTimes.itemAt(h));
519 for (size_t i = 0; i < pointerCount; i++) {
520 status_t status = (pc++)->writeToParcel(parcel);
521 if (status) {
522 return status;
523 }
524 }
525 }
526 return OK;
527}
528#endif
529
530bool MotionEvent::isTouchEvent(int32_t source, int32_t action) {
531 if (source & AINPUT_SOURCE_CLASS_POINTER) {
532 // Specifically excludes HOVER_MOVE and SCROLL.
533 switch (action & AMOTION_EVENT_ACTION_MASK) {
534 case AMOTION_EVENT_ACTION_DOWN:
535 case AMOTION_EVENT_ACTION_MOVE:
536 case AMOTION_EVENT_ACTION_UP:
537 case AMOTION_EVENT_ACTION_POINTER_DOWN:
538 case AMOTION_EVENT_ACTION_POINTER_UP:
539 case AMOTION_EVENT_ACTION_CANCEL:
540 case AMOTION_EVENT_ACTION_OUTSIDE:
541 return true;
542 }
543 }
544 return false;
545}
546
Michael Wright872db4f2014-04-22 15:03:51 -0700547const char* MotionEvent::getLabel(int32_t axis) {
548 return getAxisLabel(axis);
549}
550
551int32_t MotionEvent::getAxisFromLabel(const char* label) {
552 return getAxisByLabel(label);
553}
554
Jeff Brown5912f952013-07-01 19:10:31 -0700555
556// --- PooledInputEventFactory ---
557
558PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
559 mMaxPoolSize(maxPoolSize) {
560}
561
562PooledInputEventFactory::~PooledInputEventFactory() {
563 for (size_t i = 0; i < mKeyEventPool.size(); i++) {
564 delete mKeyEventPool.itemAt(i);
565 }
566 for (size_t i = 0; i < mMotionEventPool.size(); i++) {
567 delete mMotionEventPool.itemAt(i);
568 }
569}
570
571KeyEvent* PooledInputEventFactory::createKeyEvent() {
572 if (!mKeyEventPool.isEmpty()) {
573 KeyEvent* event = mKeyEventPool.top();
574 mKeyEventPool.pop();
575 return event;
576 }
577 return new KeyEvent();
578}
579
580MotionEvent* PooledInputEventFactory::createMotionEvent() {
581 if (!mMotionEventPool.isEmpty()) {
582 MotionEvent* event = mMotionEventPool.top();
583 mMotionEventPool.pop();
584 return event;
585 }
586 return new MotionEvent();
587}
588
589void PooledInputEventFactory::recycle(InputEvent* event) {
590 switch (event->getType()) {
591 case AINPUT_EVENT_TYPE_KEY:
592 if (mKeyEventPool.size() < mMaxPoolSize) {
593 mKeyEventPool.push(static_cast<KeyEvent*>(event));
594 return;
595 }
596 break;
597 case AINPUT_EVENT_TYPE_MOTION:
598 if (mMotionEventPool.size() < mMaxPoolSize) {
599 mMotionEventPool.push(static_cast<MotionEvent*>(event));
600 return;
601 }
602 break;
603 }
604 delete event;
605}
606
607} // namespace android