blob: 19d590ab20b391fe586b1ca3d9bdf2d5cc94fd5f [file] [log] [blame]
Jeff Brown46b9ac02010-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 Brown47e6b1b2010-11-29 17:37:49 -080010#define DEBUG_PROBE 0
11
12#include <stdlib.h>
13#include <unistd.h>
Jeff Brown90655042010-12-02 13:50:46 -080014#include <ctype.h>
Jeff Brown47e6b1b2010-11-29 17:37:49 -080015
Jeff Brown46b9ac02010-04-22 18:58:52 -070016#include <ui/Input.h>
17
Jeff Brown91c69ab2011-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 Brown46b9ac02010-04-22 18:58:52 -070028namespace android {
29
Jeff Brown47e6b1b2010-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 Brown90655042010-12-02 13:50:46 -080042static bool isValidNameChar(char ch) {
43 return isascii(ch) && (isdigit(ch) || isalpha(ch) || ch == '-' || ch == '_');
44}
45
Jeff Brown47e6b1b2010-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 Brown90655042010-12-02 13:50:46 -080051 if (!isValidNameChar(ch)) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -080052 ch = '_';
53 }
54 path.append(&ch, 1);
55 }
56 path.append(CONFIGURATION_FILE_EXTENSION[type]);
57}
58
Jeff Brown90655042010-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 Brown47e6b1b2010-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 Brown46b9ac02010-04-22 18:58:52 -0700131
Jeff Brownc5ed5912010-07-14 18:48:53 -0700132void InputEvent::initialize(int32_t deviceId, int32_t source) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700133 mDeviceId = deviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700134 mSource = source;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700135}
136
Dianne Hackborn2c6081c2010-07-15 17:44:53 -0700137void InputEvent::initialize(const InputEvent& from) {
138 mDeviceId = from.mDeviceId;
139 mSource = from.mSource;
140}
141
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800142// --- KeyEvent ---
Jeff Brown46b9ac02010-04-22 18:58:52 -0700143
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700144bool KeyEvent::hasDefaultAction(int32_t keyCode) {
145 switch (keyCode) {
Jeff Brownfd035822010-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 Brownb0418da2010-11-01 15:24:01 -0700152 case AKEYCODE_VOLUME_MUTE:
Jeff Brownfd035822010-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 Brownb0418da2010-11-01 15:24:01 -0700160 case AKEYCODE_MEDIA_PLAY:
161 case AKEYCODE_MEDIA_PAUSE:
Jeff Brownfd035822010-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 Brownb0418da2010-11-01 15:24:01 -0700167 case AKEYCODE_MEDIA_RECORD:
Jeff Brownfd035822010-06-30 16:10:35 -0700168 case AKEYCODE_MEDIA_FAST_FORWARD:
169 case AKEYCODE_MUTE:
Dianne Hackborn3c80a4a2010-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 Brownfd035822010-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 Brownb0418da2010-11-01 15:24:01 -0700190 case AKEYCODE_VOLUME_MUTE:
Jeff Brownfd035822010-06-30 16:10:35 -0700191 case AKEYCODE_MUTE:
192 case AKEYCODE_POWER:
193 case AKEYCODE_HEADSETHOOK:
Jeff Brownb0418da2010-11-01 15:24:01 -0700194 case AKEYCODE_MEDIA_PLAY:
195 case AKEYCODE_MEDIA_PAUSE:
Jeff Brownfd035822010-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 Brownb0418da2010-11-01 15:24:01 -0700201 case AKEYCODE_MEDIA_RECORD:
Jeff Brownfd035822010-06-30 16:10:35 -0700202 case AKEYCODE_MEDIA_FAST_FORWARD:
203 case AKEYCODE_CAMERA:
204 case AKEYCODE_FOCUS:
205 case AKEYCODE_SEARCH:
Dianne Hackborn3c80a4a2010-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 Brown46b9ac02010-04-22 18:58:52 -0700216void KeyEvent::initialize(
217 int32_t deviceId,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700218 int32_t source,
Jeff Brown46b9ac02010-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 Brownc5ed5912010-07-14 18:48:53 -0700227 InputEvent::initialize(deviceId, source);
Jeff Brown46b9ac02010-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 Hackborn2c6081c2010-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 Brown91c69ab2011-02-14 17:03:18 -0800250
251// --- PointerCoords ---
252
Jeff Brown6f2fba42011-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
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400301static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
302 float* value = c.editAxisValue(axis);
303 if (value) {
304 *value *= scaleFactor;
305 }
306}
307
308void PointerCoords::scale(float scaleFactor) {
309 // No need to scale pressure or size since they are normalized.
310 // No need to scale orientation since it is meaningless to do so.
311 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, scaleFactor);
312 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, scaleFactor);
313 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, scaleFactor);
314 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, scaleFactor);
315 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, scaleFactor);
316 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, scaleFactor);
317}
318
Jeff Brown91c69ab2011-02-14 17:03:18 -0800319#ifdef HAVE_ANDROID_OS
320status_t PointerCoords::readFromParcel(Parcel* parcel) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800321 bits = parcel->readInt64();
Jeff Brown91c69ab2011-02-14 17:03:18 -0800322
Jeff Brown6f2fba42011-02-19 01:08:02 -0800323 uint32_t count = __builtin_popcountll(bits);
Jeff Brown91c69ab2011-02-14 17:03:18 -0800324 if (count > MAX_AXES) {
325 return BAD_VALUE;
326 }
327
328 for (uint32_t i = 0; i < count; i++) {
329 values[i] = parcel->readInt32();
330 }
331 return OK;
332}
333
334status_t PointerCoords::writeToParcel(Parcel* parcel) const {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800335 parcel->writeInt64(bits);
Jeff Brown91c69ab2011-02-14 17:03:18 -0800336
Jeff Brown6f2fba42011-02-19 01:08:02 -0800337 uint32_t count = __builtin_popcountll(bits);
Jeff Brown91c69ab2011-02-14 17:03:18 -0800338 for (uint32_t i = 0; i < count; i++) {
339 parcel->writeInt32(values[i]);
340 }
341 return OK;
342}
343#endif
344
345void PointerCoords::tooManyAxes(int axis) {
346 LOGW("Could not set value for axis %d because the PointerCoords structure is full and "
347 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
348}
349
350
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800351// --- MotionEvent ---
Jeff Brown46b9ac02010-04-22 18:58:52 -0700352
353void MotionEvent::initialize(
354 int32_t deviceId,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700355 int32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700356 int32_t action,
Jeff Brown85a31762010-09-01 17:01:00 -0700357 int32_t flags,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700358 int32_t edgeFlags,
359 int32_t metaState,
Jeff Brown5c225b12010-06-16 01:53:36 -0700360 float xOffset,
361 float yOffset,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700362 float xPrecision,
363 float yPrecision,
364 nsecs_t downTime,
365 nsecs_t eventTime,
366 size_t pointerCount,
367 const int32_t* pointerIds,
368 const PointerCoords* pointerCoords) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700369 InputEvent::initialize(deviceId, source);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700370 mAction = action;
Jeff Brown85a31762010-09-01 17:01:00 -0700371 mFlags = flags;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700372 mEdgeFlags = edgeFlags;
373 mMetaState = metaState;
Jeff Brown5c225b12010-06-16 01:53:36 -0700374 mXOffset = xOffset;
375 mYOffset = yOffset;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700376 mXPrecision = xPrecision;
377 mYPrecision = yPrecision;
378 mDownTime = downTime;
379 mPointerIds.clear();
380 mPointerIds.appendArray(pointerIds, pointerCount);
381 mSampleEventTimes.clear();
382 mSamplePointerCoords.clear();
383 addSample(eventTime, pointerCoords);
384}
385
Jeff Brown91c69ab2011-02-14 17:03:18 -0800386void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
387 InputEvent::initialize(other->mDeviceId, other->mSource);
388 mAction = other->mAction;
389 mFlags = other->mFlags;
390 mEdgeFlags = other->mEdgeFlags;
391 mMetaState = other->mMetaState;
392 mXOffset = other->mXOffset;
393 mYOffset = other->mYOffset;
394 mXPrecision = other->mXPrecision;
395 mYPrecision = other->mYPrecision;
396 mDownTime = other->mDownTime;
397 mPointerIds = other->mPointerIds;
398
399 if (keepHistory) {
400 mSampleEventTimes = other->mSampleEventTimes;
401 mSamplePointerCoords = other->mSamplePointerCoords;
402 } else {
403 mSampleEventTimes.clear();
404 mSampleEventTimes.push(other->getEventTime());
405 mSamplePointerCoords.clear();
406 size_t pointerCount = other->getPointerCount();
407 size_t historySize = other->getHistorySize();
408 mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array()
409 + (historySize * pointerCount), pointerCount);
410 }
411}
412
Jeff Brown46b9ac02010-04-22 18:58:52 -0700413void MotionEvent::addSample(
414 int64_t eventTime,
415 const PointerCoords* pointerCoords) {
416 mSampleEventTimes.push(eventTime);
417 mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
418}
419
Jeff Brown91c69ab2011-02-14 17:03:18 -0800420const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
421 return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
422}
423
424float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
425 return getRawPointerCoords(pointerIndex)->getAxisValue(axis);
426}
427
428float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
429 float value = getRawPointerCoords(pointerIndex)->getAxisValue(axis);
430 switch (axis) {
Jeff Brownebbd5d12011-02-17 13:01:34 -0800431 case AMOTION_EVENT_AXIS_X:
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400432 return value + mXOffset;
Jeff Brownebbd5d12011-02-17 13:01:34 -0800433 case AMOTION_EVENT_AXIS_Y:
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400434 return value + mYOffset;
Jeff Brown91c69ab2011-02-14 17:03:18 -0800435 }
436 return value;
437}
438
439const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
440 size_t pointerIndex, size_t historicalIndex) const {
441 return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
442}
443
444float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
445 size_t historicalIndex) const {
446 return getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
447}
448
449float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
450 size_t historicalIndex) const {
451 float value = getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
452 switch (axis) {
Jeff Brownebbd5d12011-02-17 13:01:34 -0800453 case AMOTION_EVENT_AXIS_X:
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400454 return value + mXOffset;
Jeff Brownebbd5d12011-02-17 13:01:34 -0800455 case AMOTION_EVENT_AXIS_Y:
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400456 return value + mYOffset;
Jeff Brown91c69ab2011-02-14 17:03:18 -0800457 }
458 return value;
459}
460
Jeff Brown46b9ac02010-04-22 18:58:52 -0700461void MotionEvent::offsetLocation(float xOffset, float yOffset) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700462 mXOffset += xOffset;
463 mYOffset += yOffset;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700464}
465
Jeff Brown91c69ab2011-02-14 17:03:18 -0800466void MotionEvent::scale(float scaleFactor) {
467 mXOffset *= scaleFactor;
468 mYOffset *= scaleFactor;
469 mXPrecision *= scaleFactor;
470 mYPrecision *= scaleFactor;
471
472 size_t numSamples = mSamplePointerCoords.size();
473 for (size_t i = 0; i < numSamples; i++) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400474 mSamplePointerCoords.editItemAt(i).scale(scaleFactor);
Jeff Brown91c69ab2011-02-14 17:03:18 -0800475 }
476}
477
478#ifdef HAVE_ANDROID_OS
479static inline float transformAngle(const SkMatrix* matrix, float angleRadians) {
480 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
481 // Coordinate system: down is increasing Y, right is increasing X.
482 SkPoint vector;
483 vector.fX = SkFloatToScalar(sinf(angleRadians));
484 vector.fY = SkFloatToScalar(-cosf(angleRadians));
485 matrix->mapVectors(& vector, 1);
486
487 // Derive the transformed vector's clockwise angle from vertical.
488 float result = atan2f(SkScalarToFloat(vector.fX), SkScalarToFloat(-vector.fY));
489 if (result < - M_PI_2) {
490 result += M_PI;
491 } else if (result > M_PI_2) {
492 result -= M_PI;
493 }
494 return result;
495}
496
497void MotionEvent::transform(const SkMatrix* matrix) {
498 float oldXOffset = mXOffset;
499 float oldYOffset = mYOffset;
500
501 // The tricky part of this implementation is to preserve the value of
502 // rawX and rawY. So we apply the transformation to the first point
503 // then derive an appropriate new X/Y offset that will preserve rawX and rawY.
504 SkPoint point;
505 float rawX = getRawX(0);
506 float rawY = getRawY(0);
507 matrix->mapXY(SkFloatToScalar(rawX + oldXOffset), SkFloatToScalar(rawY + oldYOffset),
508 & point);
509 float newX = SkScalarToFloat(point.fX);
510 float newY = SkScalarToFloat(point.fY);
511 float newXOffset = newX - rawX;
512 float newYOffset = newY - rawY;
513
514 mXOffset = newXOffset;
515 mYOffset = newYOffset;
516
517 // Apply the transformation to all samples.
518 size_t numSamples = mSamplePointerCoords.size();
519 for (size_t i = 0; i < numSamples; i++) {
520 PointerCoords& c = mSamplePointerCoords.editItemAt(i);
Jeff Brownebbd5d12011-02-17 13:01:34 -0800521 float* xPtr = c.editAxisValue(AMOTION_EVENT_AXIS_X);
522 float* yPtr = c.editAxisValue(AMOTION_EVENT_AXIS_Y);
Jeff Brown91c69ab2011-02-14 17:03:18 -0800523 if (xPtr && yPtr) {
524 float x = *xPtr + oldXOffset;
525 float y = *yPtr + oldYOffset;
526 matrix->mapXY(SkFloatToScalar(x), SkFloatToScalar(y), & point);
527 *xPtr = SkScalarToFloat(point.fX) - newXOffset;
528 *yPtr = SkScalarToFloat(point.fY) - newYOffset;
529 }
530
Jeff Brownebbd5d12011-02-17 13:01:34 -0800531 float* orientationPtr = c.editAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
Jeff Brown91c69ab2011-02-14 17:03:18 -0800532 if (orientationPtr) {
533 *orientationPtr = transformAngle(matrix, *orientationPtr);
534 }
535 }
536}
537
538status_t MotionEvent::readFromParcel(Parcel* parcel) {
539 size_t pointerCount = parcel->readInt32();
540 size_t sampleCount = parcel->readInt32();
541 if (pointerCount == 0 || pointerCount > MAX_POINTERS || sampleCount == 0) {
542 return BAD_VALUE;
543 }
544
545 mDeviceId = parcel->readInt32();
546 mSource = parcel->readInt32();
547 mAction = parcel->readInt32();
548 mFlags = parcel->readInt32();
549 mEdgeFlags = parcel->readInt32();
550 mMetaState = parcel->readInt32();
551 mXOffset = parcel->readFloat();
552 mYOffset = parcel->readFloat();
553 mXPrecision = parcel->readFloat();
554 mYPrecision = parcel->readFloat();
555 mDownTime = parcel->readInt64();
556
557 mPointerIds.clear();
558 mPointerIds.setCapacity(pointerCount);
559 mSampleEventTimes.clear();
560 mSampleEventTimes.setCapacity(sampleCount);
561 mSamplePointerCoords.clear();
562 mSamplePointerCoords.setCapacity(sampleCount * pointerCount);
563
564 for (size_t i = 0; i < pointerCount; i++) {
565 mPointerIds.push(parcel->readInt32());
566 }
567
568 while (sampleCount-- > 0) {
569 mSampleEventTimes.push(parcel->readInt64());
570 for (size_t i = 0; i < pointerCount; i++) {
571 mSamplePointerCoords.push();
572 status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel);
Jeff Brownebbd5d12011-02-17 13:01:34 -0800573 if (status) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800574 return status;
575 }
576 }
577 }
578 return OK;
579}
580
581status_t MotionEvent::writeToParcel(Parcel* parcel) const {
582 size_t pointerCount = mPointerIds.size();
583 size_t sampleCount = mSampleEventTimes.size();
584
585 parcel->writeInt32(pointerCount);
586 parcel->writeInt32(sampleCount);
587
588 parcel->writeInt32(mDeviceId);
589 parcel->writeInt32(mSource);
590 parcel->writeInt32(mAction);
591 parcel->writeInt32(mFlags);
592 parcel->writeInt32(mEdgeFlags);
593 parcel->writeInt32(mMetaState);
594 parcel->writeFloat(mXOffset);
595 parcel->writeFloat(mYOffset);
596 parcel->writeFloat(mXPrecision);
597 parcel->writeFloat(mYPrecision);
598 parcel->writeInt64(mDownTime);
599
600 for (size_t i = 0; i < pointerCount; i++) {
601 parcel->writeInt32(mPointerIds.itemAt(i));
602 }
603
604 const PointerCoords* pc = mSamplePointerCoords.array();
605 for (size_t h = 0; h < sampleCount; h++) {
606 parcel->writeInt64(mSampleEventTimes.itemAt(h));
607 for (size_t i = 0; i < pointerCount; i++) {
608 status_t status = (pc++)->writeToParcel(parcel);
Jeff Brownebbd5d12011-02-17 13:01:34 -0800609 if (status) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800610 return status;
611 }
612 }
613 }
614 return OK;
615}
616#endif
617
Jeff Brown56194eb2011-03-02 19:23:13 -0800618bool MotionEvent::isTouchEvent(int32_t source, int32_t action) {
619 if (source & AINPUT_SOURCE_CLASS_POINTER) {
620 // Specifically excludes HOVER_MOVE and SCROLL.
621 switch (action & AMOTION_EVENT_ACTION_MASK) {
622 case AMOTION_EVENT_ACTION_DOWN:
623 case AMOTION_EVENT_ACTION_MOVE:
624 case AMOTION_EVENT_ACTION_UP:
625 case AMOTION_EVENT_ACTION_POINTER_DOWN:
626 case AMOTION_EVENT_ACTION_POINTER_UP:
627 case AMOTION_EVENT_ACTION_CANCEL:
628 case AMOTION_EVENT_ACTION_OUTSIDE:
629 return true;
630 }
631 }
632 return false;
633}
634
Jeff Brown91c69ab2011-02-14 17:03:18 -0800635
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800636// --- InputDeviceInfo ---
Jeff Brown6d0fec22010-07-23 21:28:06 -0700637
638InputDeviceInfo::InputDeviceInfo() {
639 initialize(-1, String8("uninitialized device info"));
640}
641
642InputDeviceInfo::InputDeviceInfo(const InputDeviceInfo& other) :
643 mId(other.mId), mName(other.mName), mSources(other.mSources),
644 mKeyboardType(other.mKeyboardType),
645 mMotionRanges(other.mMotionRanges) {
646}
647
648InputDeviceInfo::~InputDeviceInfo() {
649}
650
651void InputDeviceInfo::initialize(int32_t id, const String8& name) {
652 mId = id;
653 mName = name;
654 mSources = 0;
655 mKeyboardType = AINPUT_KEYBOARD_TYPE_NONE;
656 mMotionRanges.clear();
657}
658
Jeff Brownefd32662011-03-08 15:13:06 -0800659const InputDeviceInfo::MotionRange* InputDeviceInfo::getMotionRange(
660 int32_t axis, uint32_t source) const {
661 size_t numRanges = mMotionRanges.size();
662 for (size_t i = 0; i < numRanges; i++) {
663 const MotionRange& range = mMotionRanges.itemAt(i);
664 if (range.axis == axis && range.source == source) {
665 return &range;
666 }
667 }
668 return NULL;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700669}
670
671void InputDeviceInfo::addSource(uint32_t source) {
672 mSources |= source;
673}
674
Jeff Brownefd32662011-03-08 15:13:06 -0800675void InputDeviceInfo::addMotionRange(int32_t axis, uint32_t source, float min, float max,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700676 float flat, float fuzz) {
Jeff Brownefd32662011-03-08 15:13:06 -0800677 MotionRange range = { axis, source, min, max, flat, fuzz };
678 mMotionRanges.add(range);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700679}
680
Jeff Brownefd32662011-03-08 15:13:06 -0800681void InputDeviceInfo::addMotionRange(const MotionRange& range) {
682 mMotionRanges.add(range);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700683}
684
Jeff Brown46b9ac02010-04-22 18:58:52 -0700685} // namespace android