blob: e2e698ef7b3b3672138d4745e6a7e961175b3fdc [file] [log] [blame]
Jeff Browne839a582010-04-22 18:58:52 -07001//
2// Copyright 2010 The Android Open Source Project
3//
4// Provides a pipe-based transport for native events in the NDK.
5//
6#define LOG_TAG "Input"
7
8//#define LOG_NDEBUG 0
9
Jeff Brown66888372010-11-29 17:37:49 -080010#define DEBUG_PROBE 0
11
12#include <stdlib.h>
13#include <unistd.h>
Jeff Browndb360642010-12-02 13:50:46 -080014#include <ctype.h>
Jeff Brown66888372010-11-29 17:37:49 -080015
Jeff Browne839a582010-04-22 18:58:52 -070016#include <ui/Input.h>
17
Jeff Brown3e341462011-02-14 17:03:18 -080018#include <math.h>
19
20#ifdef HAVE_ANDROID_OS
21#include <binder/Parcel.h>
22
23#include "SkPoint.h"
24#include "SkMatrix.h"
25#include "SkScalar.h"
26#endif
27
Jeff Browne839a582010-04-22 18:58:52 -070028namespace android {
29
Jeff Brown66888372010-11-29 17:37:49 -080030static const char* CONFIGURATION_FILE_DIR[] = {
31 "idc/",
32 "keylayout/",
33 "keychars/",
34};
35
36static const char* CONFIGURATION_FILE_EXTENSION[] = {
37 ".idc",
38 ".kl",
39 ".kcm",
40};
41
Jeff Browndb360642010-12-02 13:50:46 -080042static bool isValidNameChar(char ch) {
43 return isascii(ch) && (isdigit(ch) || isalpha(ch) || ch == '-' || ch == '_');
44}
45
Jeff Brown66888372010-11-29 17:37:49 -080046static void appendInputDeviceConfigurationFileRelativePath(String8& path,
47 const String8& name, InputDeviceConfigurationFileType type) {
48 path.append(CONFIGURATION_FILE_DIR[type]);
49 for (size_t i = 0; i < name.length(); i++) {
50 char ch = name[i];
Jeff Browndb360642010-12-02 13:50:46 -080051 if (!isValidNameChar(ch)) {
Jeff Brown66888372010-11-29 17:37:49 -080052 ch = '_';
53 }
54 path.append(&ch, 1);
55 }
56 path.append(CONFIGURATION_FILE_EXTENSION[type]);
57}
58
Jeff Browndb360642010-12-02 13:50:46 -080059String8 getInputDeviceConfigurationFilePathByDeviceIdentifier(
60 const InputDeviceIdentifier& deviceIdentifier,
61 InputDeviceConfigurationFileType type) {
62 if (deviceIdentifier.vendor !=0 && deviceIdentifier.product != 0) {
63 if (deviceIdentifier.version != 0) {
64 // Try vendor product version.
65 String8 versionPath(getInputDeviceConfigurationFilePathByName(
66 String8::format("Vendor_%04x_Product_%04x_Version_%04x",
67 deviceIdentifier.vendor, deviceIdentifier.product,
68 deviceIdentifier.version),
69 type));
70 if (!versionPath.isEmpty()) {
71 return versionPath;
72 }
73 }
74
75 // Try vendor product.
76 String8 productPath(getInputDeviceConfigurationFilePathByName(
77 String8::format("Vendor_%04x_Product_%04x",
78 deviceIdentifier.vendor, deviceIdentifier.product),
79 type));
80 if (!productPath.isEmpty()) {
81 return productPath;
82 }
83 }
84
85 // Try device name.
86 return getInputDeviceConfigurationFilePathByName(deviceIdentifier.name, type);
87}
88
89String8 getInputDeviceConfigurationFilePathByName(
Jeff Brown66888372010-11-29 17:37:49 -080090 const String8& name, InputDeviceConfigurationFileType type) {
91 // Search system repository.
92 String8 path;
93 path.setTo(getenv("ANDROID_ROOT"));
94 path.append("/usr/");
95 appendInputDeviceConfigurationFileRelativePath(path, name, type);
96#if DEBUG_PROBE
97 LOGD("Probing for system provided input device configuration file: path='%s'", path.string());
98#endif
99 if (!access(path.string(), R_OK)) {
100#if DEBUG_PROBE
101 LOGD("Found");
102#endif
103 return path;
104 }
105
106 // Search user repository.
107 // TODO Should only look here if not in safe mode.
108 path.setTo(getenv("ANDROID_DATA"));
109 path.append("/system/devices/");
110 appendInputDeviceConfigurationFileRelativePath(path, name, type);
111#if DEBUG_PROBE
112 LOGD("Probing for system user input device configuration file: path='%s'", path.string());
113#endif
114 if (!access(path.string(), R_OK)) {
115#if DEBUG_PROBE
116 LOGD("Found");
117#endif
118 return path;
119 }
120
121 // Not found.
122#if DEBUG_PROBE
123 LOGD("Probe failed to find input device configuration file: name='%s', type=%d",
124 name.string(), type);
125#endif
126 return String8();
127}
128
129
130// --- InputEvent ---
Jeff Browne839a582010-04-22 18:58:52 -0700131
Jeff Brown5c1ed842010-07-14 18:48:53 -0700132void InputEvent::initialize(int32_t deviceId, int32_t source) {
Jeff Browne839a582010-04-22 18:58:52 -0700133 mDeviceId = deviceId;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700134 mSource = source;
Jeff Browne839a582010-04-22 18:58:52 -0700135}
136
Dianne Hackborn0e885272010-07-15 17:44:53 -0700137void InputEvent::initialize(const InputEvent& from) {
138 mDeviceId = from.mDeviceId;
139 mSource = from.mSource;
140}
141
Jeff Brown66888372010-11-29 17:37:49 -0800142// --- KeyEvent ---
Jeff Browne839a582010-04-22 18:58:52 -0700143
Dianne Hackborn189ed232010-06-29 19:20:40 -0700144bool KeyEvent::hasDefaultAction(int32_t keyCode) {
145 switch (keyCode) {
Jeff Brown8575a872010-06-30 16:10:35 -0700146 case AKEYCODE_HOME:
147 case AKEYCODE_BACK:
148 case AKEYCODE_CALL:
149 case AKEYCODE_ENDCALL:
150 case AKEYCODE_VOLUME_UP:
151 case AKEYCODE_VOLUME_DOWN:
Jeff Brown7e5660f2010-11-01 15:24:01 -0700152 case AKEYCODE_VOLUME_MUTE:
Jeff Brown8575a872010-06-30 16:10:35 -0700153 case AKEYCODE_POWER:
154 case AKEYCODE_CAMERA:
155 case AKEYCODE_HEADSETHOOK:
156 case AKEYCODE_MENU:
157 case AKEYCODE_NOTIFICATION:
158 case AKEYCODE_FOCUS:
159 case AKEYCODE_SEARCH:
Jeff Brown7e5660f2010-11-01 15:24:01 -0700160 case AKEYCODE_MEDIA_PLAY:
161 case AKEYCODE_MEDIA_PAUSE:
Jeff Brown8575a872010-06-30 16:10:35 -0700162 case AKEYCODE_MEDIA_PLAY_PAUSE:
163 case AKEYCODE_MEDIA_STOP:
164 case AKEYCODE_MEDIA_NEXT:
165 case AKEYCODE_MEDIA_PREVIOUS:
166 case AKEYCODE_MEDIA_REWIND:
Jeff Brown7e5660f2010-11-01 15:24:01 -0700167 case AKEYCODE_MEDIA_RECORD:
Jeff Brown8575a872010-06-30 16:10:35 -0700168 case AKEYCODE_MEDIA_FAST_FORWARD:
169 case AKEYCODE_MUTE:
Dianne Hackborn189ed232010-06-29 19:20:40 -0700170 return true;
171 }
172
173 return false;
174}
175
176bool KeyEvent::hasDefaultAction() const {
177 return hasDefaultAction(getKeyCode());
178}
179
180bool KeyEvent::isSystemKey(int32_t keyCode) {
181 switch (keyCode) {
Jeff Brown8575a872010-06-30 16:10:35 -0700182 case AKEYCODE_MENU:
183 case AKEYCODE_SOFT_RIGHT:
184 case AKEYCODE_HOME:
185 case AKEYCODE_BACK:
186 case AKEYCODE_CALL:
187 case AKEYCODE_ENDCALL:
188 case AKEYCODE_VOLUME_UP:
189 case AKEYCODE_VOLUME_DOWN:
Jeff Brown7e5660f2010-11-01 15:24:01 -0700190 case AKEYCODE_VOLUME_MUTE:
Jeff Brown8575a872010-06-30 16:10:35 -0700191 case AKEYCODE_MUTE:
192 case AKEYCODE_POWER:
193 case AKEYCODE_HEADSETHOOK:
Jeff Brown7e5660f2010-11-01 15:24:01 -0700194 case AKEYCODE_MEDIA_PLAY:
195 case AKEYCODE_MEDIA_PAUSE:
Jeff Brown8575a872010-06-30 16:10:35 -0700196 case AKEYCODE_MEDIA_PLAY_PAUSE:
197 case AKEYCODE_MEDIA_STOP:
198 case AKEYCODE_MEDIA_NEXT:
199 case AKEYCODE_MEDIA_PREVIOUS:
200 case AKEYCODE_MEDIA_REWIND:
Jeff Brown7e5660f2010-11-01 15:24:01 -0700201 case AKEYCODE_MEDIA_RECORD:
Jeff Brown8575a872010-06-30 16:10:35 -0700202 case AKEYCODE_MEDIA_FAST_FORWARD:
203 case AKEYCODE_CAMERA:
204 case AKEYCODE_FOCUS:
205 case AKEYCODE_SEARCH:
Dianne Hackborn189ed232010-06-29 19:20:40 -0700206 return true;
207 }
208
209 return false;
210}
211
212bool KeyEvent::isSystemKey() const {
213 return isSystemKey(getKeyCode());
214}
215
Jeff Browne839a582010-04-22 18:58:52 -0700216void KeyEvent::initialize(
217 int32_t deviceId,
Jeff Brown5c1ed842010-07-14 18:48:53 -0700218 int32_t source,
Jeff Browne839a582010-04-22 18:58:52 -0700219 int32_t action,
220 int32_t flags,
221 int32_t keyCode,
222 int32_t scanCode,
223 int32_t metaState,
224 int32_t repeatCount,
225 nsecs_t downTime,
226 nsecs_t eventTime) {
Jeff Brown5c1ed842010-07-14 18:48:53 -0700227 InputEvent::initialize(deviceId, source);
Jeff Browne839a582010-04-22 18:58:52 -0700228 mAction = action;
229 mFlags = flags;
230 mKeyCode = keyCode;
231 mScanCode = scanCode;
232 mMetaState = metaState;
233 mRepeatCount = repeatCount;
234 mDownTime = downTime;
235 mEventTime = eventTime;
236}
237
Dianne Hackborn0e885272010-07-15 17:44:53 -0700238void KeyEvent::initialize(const KeyEvent& from) {
239 InputEvent::initialize(from);
240 mAction = from.mAction;
241 mFlags = from.mFlags;
242 mKeyCode = from.mKeyCode;
243 mScanCode = from.mScanCode;
244 mMetaState = from.mMetaState;
245 mRepeatCount = from.mRepeatCount;
246 mDownTime = from.mDownTime;
247 mEventTime = from.mEventTime;
248}
249
Jeff Brown3e341462011-02-14 17:03:18 -0800250
251// --- PointerCoords ---
252
Jeff Brown3ea4de82011-02-19 01:08:02 -0800253float PointerCoords::getAxisValue(int32_t axis) const {
254 if (axis < 0 || axis > 63) {
255 return 0;
256 }
257
258 uint64_t axisBit = 1LL << axis;
259 if (!(bits & axisBit)) {
260 return 0;
261 }
262 uint32_t index = __builtin_popcountll(bits & (axisBit - 1LL));
263 return values[index];
264}
265
266status_t PointerCoords::setAxisValue(int32_t axis, float value) {
267 if (axis < 0 || axis > 63) {
268 return NAME_NOT_FOUND;
269 }
270
271 uint64_t axisBit = 1LL << axis;
272 uint32_t index = __builtin_popcountll(bits & (axisBit - 1LL));
273 if (!(bits & axisBit)) {
274 uint32_t count = __builtin_popcountll(bits);
275 if (count >= MAX_AXES) {
276 tooManyAxes(axis);
277 return NO_MEMORY;
278 }
279 bits |= axisBit;
280 for (uint32_t i = count; i > index; i--) {
281 values[i] = values[i - 1];
282 }
283 }
284 values[index] = value;
285 return OK;
286}
287
288float* PointerCoords::editAxisValue(int32_t axis) {
289 if (axis < 0 || axis > 63) {
290 return NULL;
291 }
292
293 uint64_t axisBit = 1LL << axis;
294 if (!(bits & axisBit)) {
295 return NULL;
296 }
297 uint32_t index = __builtin_popcountll(bits & (axisBit - 1LL));
298 return &values[index];
299}
300
Jeff Brown3e341462011-02-14 17:03:18 -0800301#ifdef HAVE_ANDROID_OS
302status_t PointerCoords::readFromParcel(Parcel* parcel) {
Jeff Brown3ea4de82011-02-19 01:08:02 -0800303 bits = parcel->readInt64();
Jeff Brown3e341462011-02-14 17:03:18 -0800304
Jeff Brown3ea4de82011-02-19 01:08:02 -0800305 uint32_t count = __builtin_popcountll(bits);
Jeff Brown3e341462011-02-14 17:03:18 -0800306 if (count > MAX_AXES) {
307 return BAD_VALUE;
308 }
309
310 for (uint32_t i = 0; i < count; i++) {
311 values[i] = parcel->readInt32();
312 }
313 return OK;
314}
315
316status_t PointerCoords::writeToParcel(Parcel* parcel) const {
Jeff Brown3ea4de82011-02-19 01:08:02 -0800317 parcel->writeInt64(bits);
Jeff Brown3e341462011-02-14 17:03:18 -0800318
Jeff Brown3ea4de82011-02-19 01:08:02 -0800319 uint32_t count = __builtin_popcountll(bits);
Jeff Brown3e341462011-02-14 17:03:18 -0800320 for (uint32_t i = 0; i < count; i++) {
321 parcel->writeInt32(values[i]);
322 }
323 return OK;
324}
325#endif
326
327void PointerCoords::tooManyAxes(int axis) {
328 LOGW("Could not set value for axis %d because the PointerCoords structure is full and "
329 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
330}
331
332
Jeff Brown66888372010-11-29 17:37:49 -0800333// --- MotionEvent ---
Jeff Browne839a582010-04-22 18:58:52 -0700334
335void MotionEvent::initialize(
336 int32_t deviceId,
Jeff Brown5c1ed842010-07-14 18:48:53 -0700337 int32_t source,
Jeff Browne839a582010-04-22 18:58:52 -0700338 int32_t action,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700339 int32_t flags,
Jeff Browne839a582010-04-22 18:58:52 -0700340 int32_t edgeFlags,
341 int32_t metaState,
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700342 float xOffset,
343 float yOffset,
Jeff Browne839a582010-04-22 18:58:52 -0700344 float xPrecision,
345 float yPrecision,
346 nsecs_t downTime,
347 nsecs_t eventTime,
348 size_t pointerCount,
349 const int32_t* pointerIds,
350 const PointerCoords* pointerCoords) {
Jeff Brown5c1ed842010-07-14 18:48:53 -0700351 InputEvent::initialize(deviceId, source);
Jeff Browne839a582010-04-22 18:58:52 -0700352 mAction = action;
Jeff Brownaf30ff62010-09-01 17:01:00 -0700353 mFlags = flags;
Jeff Browne839a582010-04-22 18:58:52 -0700354 mEdgeFlags = edgeFlags;
355 mMetaState = metaState;
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700356 mXOffset = xOffset;
357 mYOffset = yOffset;
Jeff Browne839a582010-04-22 18:58:52 -0700358 mXPrecision = xPrecision;
359 mYPrecision = yPrecision;
360 mDownTime = downTime;
361 mPointerIds.clear();
362 mPointerIds.appendArray(pointerIds, pointerCount);
363 mSampleEventTimes.clear();
364 mSamplePointerCoords.clear();
365 addSample(eventTime, pointerCoords);
366}
367
Jeff Brown3e341462011-02-14 17:03:18 -0800368void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
369 InputEvent::initialize(other->mDeviceId, other->mSource);
370 mAction = other->mAction;
371 mFlags = other->mFlags;
372 mEdgeFlags = other->mEdgeFlags;
373 mMetaState = other->mMetaState;
374 mXOffset = other->mXOffset;
375 mYOffset = other->mYOffset;
376 mXPrecision = other->mXPrecision;
377 mYPrecision = other->mYPrecision;
378 mDownTime = other->mDownTime;
379 mPointerIds = other->mPointerIds;
380
381 if (keepHistory) {
382 mSampleEventTimes = other->mSampleEventTimes;
383 mSamplePointerCoords = other->mSamplePointerCoords;
384 } else {
385 mSampleEventTimes.clear();
386 mSampleEventTimes.push(other->getEventTime());
387 mSamplePointerCoords.clear();
388 size_t pointerCount = other->getPointerCount();
389 size_t historySize = other->getHistorySize();
390 mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array()
391 + (historySize * pointerCount), pointerCount);
392 }
393}
394
Jeff Browne839a582010-04-22 18:58:52 -0700395void MotionEvent::addSample(
396 int64_t eventTime,
397 const PointerCoords* pointerCoords) {
398 mSampleEventTimes.push(eventTime);
399 mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
400}
401
Jeff Brown3e341462011-02-14 17:03:18 -0800402const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
403 return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
404}
405
406float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
407 return getRawPointerCoords(pointerIndex)->getAxisValue(axis);
408}
409
410float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
411 float value = getRawPointerCoords(pointerIndex)->getAxisValue(axis);
412 switch (axis) {
Jeff Brownb2d44352011-02-17 13:01:34 -0800413 case AMOTION_EVENT_AXIS_X:
Jeff Brown3e341462011-02-14 17:03:18 -0800414 value += mXOffset;
415 break;
Jeff Brownb2d44352011-02-17 13:01:34 -0800416 case AMOTION_EVENT_AXIS_Y:
Jeff Brown3e341462011-02-14 17:03:18 -0800417 value += mYOffset;
418 break;
419 }
420 return value;
421}
422
423const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
424 size_t pointerIndex, size_t historicalIndex) const {
425 return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
426}
427
428float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
429 size_t historicalIndex) const {
430 return getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
431}
432
433float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
434 size_t historicalIndex) const {
435 float value = getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
436 switch (axis) {
Jeff Brownb2d44352011-02-17 13:01:34 -0800437 case AMOTION_EVENT_AXIS_X:
Jeff Brown3e341462011-02-14 17:03:18 -0800438 value += mXOffset;
439 break;
Jeff Brownb2d44352011-02-17 13:01:34 -0800440 case AMOTION_EVENT_AXIS_Y:
Jeff Brown3e341462011-02-14 17:03:18 -0800441 value += mYOffset;
442 break;
443 }
444 return value;
445}
446
Jeff Browne839a582010-04-22 18:58:52 -0700447void MotionEvent::offsetLocation(float xOffset, float yOffset) {
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700448 mXOffset += xOffset;
449 mYOffset += yOffset;
Jeff Browne839a582010-04-22 18:58:52 -0700450}
451
Jeff Brown3e341462011-02-14 17:03:18 -0800452static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
453 float* value = c.editAxisValue(axis);
454 if (value) {
455 *value *= scaleFactor;
456 }
457}
458
459void MotionEvent::scale(float scaleFactor) {
460 mXOffset *= scaleFactor;
461 mYOffset *= scaleFactor;
462 mXPrecision *= scaleFactor;
463 mYPrecision *= scaleFactor;
464
465 size_t numSamples = mSamplePointerCoords.size();
466 for (size_t i = 0; i < numSamples; i++) {
467 PointerCoords& c = mSamplePointerCoords.editItemAt(i);
468 // No need to scale pressure or size since they are normalized.
469 // No need to scale orientation since it is meaningless to do so.
Jeff Brownb2d44352011-02-17 13:01:34 -0800470 scaleAxisValue(c, AMOTION_EVENT_AXIS_X, scaleFactor);
471 scaleAxisValue(c, AMOTION_EVENT_AXIS_Y, scaleFactor);
472 scaleAxisValue(c, AMOTION_EVENT_AXIS_TOUCH_MAJOR, scaleFactor);
473 scaleAxisValue(c, AMOTION_EVENT_AXIS_TOUCH_MINOR, scaleFactor);
474 scaleAxisValue(c, AMOTION_EVENT_AXIS_TOOL_MAJOR, scaleFactor);
475 scaleAxisValue(c, AMOTION_EVENT_AXIS_TOOL_MINOR, scaleFactor);
Jeff Brown3e341462011-02-14 17:03:18 -0800476 }
477}
478
479#ifdef HAVE_ANDROID_OS
480static inline float transformAngle(const SkMatrix* matrix, float angleRadians) {
481 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
482 // Coordinate system: down is increasing Y, right is increasing X.
483 SkPoint vector;
484 vector.fX = SkFloatToScalar(sinf(angleRadians));
485 vector.fY = SkFloatToScalar(-cosf(angleRadians));
486 matrix->mapVectors(& vector, 1);
487
488 // Derive the transformed vector's clockwise angle from vertical.
489 float result = atan2f(SkScalarToFloat(vector.fX), SkScalarToFloat(-vector.fY));
490 if (result < - M_PI_2) {
491 result += M_PI;
492 } else if (result > M_PI_2) {
493 result -= M_PI;
494 }
495 return result;
496}
497
498void MotionEvent::transform(const SkMatrix* matrix) {
499 float oldXOffset = mXOffset;
500 float oldYOffset = mYOffset;
501
502 // The tricky part of this implementation is to preserve the value of
503 // rawX and rawY. So we apply the transformation to the first point
504 // then derive an appropriate new X/Y offset that will preserve rawX and rawY.
505 SkPoint point;
506 float rawX = getRawX(0);
507 float rawY = getRawY(0);
508 matrix->mapXY(SkFloatToScalar(rawX + oldXOffset), SkFloatToScalar(rawY + oldYOffset),
509 & point);
510 float newX = SkScalarToFloat(point.fX);
511 float newY = SkScalarToFloat(point.fY);
512 float newXOffset = newX - rawX;
513 float newYOffset = newY - rawY;
514
515 mXOffset = newXOffset;
516 mYOffset = newYOffset;
517
518 // Apply the transformation to all samples.
519 size_t numSamples = mSamplePointerCoords.size();
520 for (size_t i = 0; i < numSamples; i++) {
521 PointerCoords& c = mSamplePointerCoords.editItemAt(i);
Jeff Brownb2d44352011-02-17 13:01:34 -0800522 float* xPtr = c.editAxisValue(AMOTION_EVENT_AXIS_X);
523 float* yPtr = c.editAxisValue(AMOTION_EVENT_AXIS_Y);
Jeff Brown3e341462011-02-14 17:03:18 -0800524 if (xPtr && yPtr) {
525 float x = *xPtr + oldXOffset;
526 float y = *yPtr + oldYOffset;
527 matrix->mapXY(SkFloatToScalar(x), SkFloatToScalar(y), & point);
528 *xPtr = SkScalarToFloat(point.fX) - newXOffset;
529 *yPtr = SkScalarToFloat(point.fY) - newYOffset;
530 }
531
Jeff Brownb2d44352011-02-17 13:01:34 -0800532 float* orientationPtr = c.editAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
Jeff Brown3e341462011-02-14 17:03:18 -0800533 if (orientationPtr) {
534 *orientationPtr = transformAngle(matrix, *orientationPtr);
535 }
536 }
537}
538
539status_t MotionEvent::readFromParcel(Parcel* parcel) {
540 size_t pointerCount = parcel->readInt32();
541 size_t sampleCount = parcel->readInt32();
542 if (pointerCount == 0 || pointerCount > MAX_POINTERS || sampleCount == 0) {
543 return BAD_VALUE;
544 }
545
546 mDeviceId = parcel->readInt32();
547 mSource = parcel->readInt32();
548 mAction = parcel->readInt32();
549 mFlags = parcel->readInt32();
550 mEdgeFlags = parcel->readInt32();
551 mMetaState = parcel->readInt32();
552 mXOffset = parcel->readFloat();
553 mYOffset = parcel->readFloat();
554 mXPrecision = parcel->readFloat();
555 mYPrecision = parcel->readFloat();
556 mDownTime = parcel->readInt64();
557
558 mPointerIds.clear();
559 mPointerIds.setCapacity(pointerCount);
560 mSampleEventTimes.clear();
561 mSampleEventTimes.setCapacity(sampleCount);
562 mSamplePointerCoords.clear();
563 mSamplePointerCoords.setCapacity(sampleCount * pointerCount);
564
565 for (size_t i = 0; i < pointerCount; i++) {
566 mPointerIds.push(parcel->readInt32());
567 }
568
569 while (sampleCount-- > 0) {
570 mSampleEventTimes.push(parcel->readInt64());
571 for (size_t i = 0; i < pointerCount; i++) {
572 mSamplePointerCoords.push();
573 status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel);
Jeff Brownb2d44352011-02-17 13:01:34 -0800574 if (status) {
Jeff Brown3e341462011-02-14 17:03:18 -0800575 return status;
576 }
577 }
578 }
579 return OK;
580}
581
582status_t MotionEvent::writeToParcel(Parcel* parcel) const {
583 size_t pointerCount = mPointerIds.size();
584 size_t sampleCount = mSampleEventTimes.size();
585
586 parcel->writeInt32(pointerCount);
587 parcel->writeInt32(sampleCount);
588
589 parcel->writeInt32(mDeviceId);
590 parcel->writeInt32(mSource);
591 parcel->writeInt32(mAction);
592 parcel->writeInt32(mFlags);
593 parcel->writeInt32(mEdgeFlags);
594 parcel->writeInt32(mMetaState);
595 parcel->writeFloat(mXOffset);
596 parcel->writeFloat(mYOffset);
597 parcel->writeFloat(mXPrecision);
598 parcel->writeFloat(mYPrecision);
599 parcel->writeInt64(mDownTime);
600
601 for (size_t i = 0; i < pointerCount; i++) {
602 parcel->writeInt32(mPointerIds.itemAt(i));
603 }
604
605 const PointerCoords* pc = mSamplePointerCoords.array();
606 for (size_t h = 0; h < sampleCount; h++) {
607 parcel->writeInt64(mSampleEventTimes.itemAt(h));
608 for (size_t i = 0; i < pointerCount; i++) {
609 status_t status = (pc++)->writeToParcel(parcel);
Jeff Brownb2d44352011-02-17 13:01:34 -0800610 if (status) {
Jeff Brown3e341462011-02-14 17:03:18 -0800611 return status;
612 }
613 }
614 }
615 return OK;
616}
617#endif
618
Jeff Brownd5ed2852011-03-02 19:23:13 -0800619bool MotionEvent::isTouchEvent(int32_t source, int32_t action) {
620 if (source & AINPUT_SOURCE_CLASS_POINTER) {
621 // Specifically excludes HOVER_MOVE and SCROLL.
622 switch (action & AMOTION_EVENT_ACTION_MASK) {
623 case AMOTION_EVENT_ACTION_DOWN:
624 case AMOTION_EVENT_ACTION_MOVE:
625 case AMOTION_EVENT_ACTION_UP:
626 case AMOTION_EVENT_ACTION_POINTER_DOWN:
627 case AMOTION_EVENT_ACTION_POINTER_UP:
628 case AMOTION_EVENT_ACTION_CANCEL:
629 case AMOTION_EVENT_ACTION_OUTSIDE:
630 return true;
631 }
632 }
633 return false;
634}
635
Jeff Brown3e341462011-02-14 17:03:18 -0800636
Jeff Brown66888372010-11-29 17:37:49 -0800637// --- InputDeviceInfo ---
Jeff Browne57e8952010-07-23 21:28:06 -0700638
639InputDeviceInfo::InputDeviceInfo() {
640 initialize(-1, String8("uninitialized device info"));
641}
642
643InputDeviceInfo::InputDeviceInfo(const InputDeviceInfo& other) :
644 mId(other.mId), mName(other.mName), mSources(other.mSources),
645 mKeyboardType(other.mKeyboardType),
646 mMotionRanges(other.mMotionRanges) {
647}
648
649InputDeviceInfo::~InputDeviceInfo() {
650}
651
652void InputDeviceInfo::initialize(int32_t id, const String8& name) {
653 mId = id;
654 mName = name;
655 mSources = 0;
656 mKeyboardType = AINPUT_KEYBOARD_TYPE_NONE;
657 mMotionRanges.clear();
658}
659
Jeff Brown46689da2011-03-08 15:13:06 -0800660const InputDeviceInfo::MotionRange* InputDeviceInfo::getMotionRange(
661 int32_t axis, uint32_t source) const {
662 size_t numRanges = mMotionRanges.size();
663 for (size_t i = 0; i < numRanges; i++) {
664 const MotionRange& range = mMotionRanges.itemAt(i);
665 if (range.axis == axis && range.source == source) {
666 return &range;
667 }
668 }
669 return NULL;
Jeff Browne57e8952010-07-23 21:28:06 -0700670}
671
672void InputDeviceInfo::addSource(uint32_t source) {
673 mSources |= source;
674}
675
Jeff Brown46689da2011-03-08 15:13:06 -0800676void InputDeviceInfo::addMotionRange(int32_t axis, uint32_t source, float min, float max,
Jeff Browne57e8952010-07-23 21:28:06 -0700677 float flat, float fuzz) {
Jeff Brown46689da2011-03-08 15:13:06 -0800678 MotionRange range = { axis, source, min, max, flat, fuzz };
679 mMotionRanges.add(range);
Jeff Browne57e8952010-07-23 21:28:06 -0700680}
681
Jeff Brown46689da2011-03-08 15:13:06 -0800682void InputDeviceInfo::addMotionRange(const MotionRange& range) {
683 mMotionRanges.add(range);
Jeff Browne57e8952010-07-23 21:28:06 -0700684}
685
Jeff Browne839a582010-04-22 18:58:52 -0700686} // namespace android