Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 17 | #define LOG_TAG "Input" |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 20 | #include <math.h> |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 21 | #include <limits.h> |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 22 | |
Jeff Brown | 8a90e6e | 2012-05-11 12:24:35 -0700 | [diff] [blame] | 23 | #include <androidfw/Input.h> |
| 24 | |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 25 | #ifdef HAVE_ANDROID_OS |
| 26 | #include <binder/Parcel.h> |
| 27 | |
| 28 | #include "SkPoint.h" |
| 29 | #include "SkMatrix.h" |
| 30 | #include "SkScalar.h" |
| 31 | #endif |
| 32 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 33 | namespace android { |
| 34 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 35 | // --- InputEvent --- |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 36 | |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 37 | void InputEvent::initialize(int32_t deviceId, int32_t source) { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 38 | mDeviceId = deviceId; |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 39 | mSource = source; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 42 | void InputEvent::initialize(const InputEvent& from) { |
| 43 | mDeviceId = from.mDeviceId; |
| 44 | mSource = from.mSource; |
| 45 | } |
| 46 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 47 | // --- KeyEvent --- |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 48 | |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 49 | bool KeyEvent::hasDefaultAction(int32_t keyCode) { |
| 50 | switch (keyCode) { |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 51 | case AKEYCODE_HOME: |
| 52 | case AKEYCODE_BACK: |
| 53 | case AKEYCODE_CALL: |
| 54 | case AKEYCODE_ENDCALL: |
| 55 | case AKEYCODE_VOLUME_UP: |
| 56 | case AKEYCODE_VOLUME_DOWN: |
Jeff Brown | b0418da | 2010-11-01 15:24:01 -0700 | [diff] [blame] | 57 | case AKEYCODE_VOLUME_MUTE: |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 58 | case AKEYCODE_POWER: |
| 59 | case AKEYCODE_CAMERA: |
| 60 | case AKEYCODE_HEADSETHOOK: |
| 61 | case AKEYCODE_MENU: |
| 62 | case AKEYCODE_NOTIFICATION: |
| 63 | case AKEYCODE_FOCUS: |
| 64 | case AKEYCODE_SEARCH: |
Jeff Brown | b0418da | 2010-11-01 15:24:01 -0700 | [diff] [blame] | 65 | case AKEYCODE_MEDIA_PLAY: |
| 66 | case AKEYCODE_MEDIA_PAUSE: |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 67 | case AKEYCODE_MEDIA_PLAY_PAUSE: |
| 68 | case AKEYCODE_MEDIA_STOP: |
| 69 | case AKEYCODE_MEDIA_NEXT: |
| 70 | case AKEYCODE_MEDIA_PREVIOUS: |
| 71 | case AKEYCODE_MEDIA_REWIND: |
Jeff Brown | b0418da | 2010-11-01 15:24:01 -0700 | [diff] [blame] | 72 | case AKEYCODE_MEDIA_RECORD: |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 73 | case AKEYCODE_MEDIA_FAST_FORWARD: |
| 74 | case AKEYCODE_MUTE: |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 75 | return true; |
| 76 | } |
| 77 | |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | bool KeyEvent::hasDefaultAction() const { |
| 82 | return hasDefaultAction(getKeyCode()); |
| 83 | } |
| 84 | |
| 85 | bool KeyEvent::isSystemKey(int32_t keyCode) { |
| 86 | switch (keyCode) { |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 87 | case AKEYCODE_MENU: |
| 88 | case AKEYCODE_SOFT_RIGHT: |
| 89 | case AKEYCODE_HOME: |
| 90 | case AKEYCODE_BACK: |
| 91 | case AKEYCODE_CALL: |
| 92 | case AKEYCODE_ENDCALL: |
| 93 | case AKEYCODE_VOLUME_UP: |
| 94 | case AKEYCODE_VOLUME_DOWN: |
Jeff Brown | b0418da | 2010-11-01 15:24:01 -0700 | [diff] [blame] | 95 | case AKEYCODE_VOLUME_MUTE: |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 96 | case AKEYCODE_MUTE: |
| 97 | case AKEYCODE_POWER: |
| 98 | case AKEYCODE_HEADSETHOOK: |
Jeff Brown | b0418da | 2010-11-01 15:24:01 -0700 | [diff] [blame] | 99 | case AKEYCODE_MEDIA_PLAY: |
| 100 | case AKEYCODE_MEDIA_PAUSE: |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 101 | case AKEYCODE_MEDIA_PLAY_PAUSE: |
| 102 | case AKEYCODE_MEDIA_STOP: |
| 103 | case AKEYCODE_MEDIA_NEXT: |
| 104 | case AKEYCODE_MEDIA_PREVIOUS: |
| 105 | case AKEYCODE_MEDIA_REWIND: |
Jeff Brown | b0418da | 2010-11-01 15:24:01 -0700 | [diff] [blame] | 106 | case AKEYCODE_MEDIA_RECORD: |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 107 | case AKEYCODE_MEDIA_FAST_FORWARD: |
| 108 | case AKEYCODE_CAMERA: |
| 109 | case AKEYCODE_FOCUS: |
| 110 | case AKEYCODE_SEARCH: |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 111 | return true; |
| 112 | } |
| 113 | |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | bool KeyEvent::isSystemKey() const { |
| 118 | return isSystemKey(getKeyCode()); |
| 119 | } |
| 120 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 121 | void KeyEvent::initialize( |
| 122 | int32_t deviceId, |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 123 | int32_t source, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 124 | int32_t action, |
| 125 | int32_t flags, |
| 126 | int32_t keyCode, |
| 127 | int32_t scanCode, |
| 128 | int32_t metaState, |
| 129 | int32_t repeatCount, |
| 130 | nsecs_t downTime, |
| 131 | nsecs_t eventTime) { |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 132 | InputEvent::initialize(deviceId, source); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 133 | mAction = action; |
| 134 | mFlags = flags; |
| 135 | mKeyCode = keyCode; |
| 136 | mScanCode = scanCode; |
| 137 | mMetaState = metaState; |
| 138 | mRepeatCount = repeatCount; |
| 139 | mDownTime = downTime; |
| 140 | mEventTime = eventTime; |
| 141 | } |
| 142 | |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 143 | void KeyEvent::initialize(const KeyEvent& from) { |
| 144 | InputEvent::initialize(from); |
| 145 | mAction = from.mAction; |
| 146 | mFlags = from.mFlags; |
| 147 | mKeyCode = from.mKeyCode; |
| 148 | mScanCode = from.mScanCode; |
| 149 | mMetaState = from.mMetaState; |
| 150 | mRepeatCount = from.mRepeatCount; |
| 151 | mDownTime = from.mDownTime; |
| 152 | mEventTime = from.mEventTime; |
| 153 | } |
| 154 | |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 155 | |
| 156 | // --- PointerCoords --- |
| 157 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 158 | float PointerCoords::getAxisValue(int32_t axis) const { |
| 159 | if (axis < 0 || axis > 63) { |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | uint64_t axisBit = 1LL << axis; |
| 164 | if (!(bits & axisBit)) { |
| 165 | return 0; |
| 166 | } |
| 167 | uint32_t index = __builtin_popcountll(bits & (axisBit - 1LL)); |
| 168 | return values[index]; |
| 169 | } |
| 170 | |
| 171 | status_t PointerCoords::setAxisValue(int32_t axis, float value) { |
| 172 | if (axis < 0 || axis > 63) { |
| 173 | return NAME_NOT_FOUND; |
| 174 | } |
| 175 | |
| 176 | uint64_t axisBit = 1LL << axis; |
| 177 | uint32_t index = __builtin_popcountll(bits & (axisBit - 1LL)); |
| 178 | if (!(bits & axisBit)) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 179 | if (value == 0) { |
| 180 | return OK; // axes with value 0 do not need to be stored |
| 181 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 182 | uint32_t count = __builtin_popcountll(bits); |
| 183 | if (count >= MAX_AXES) { |
| 184 | tooManyAxes(axis); |
| 185 | return NO_MEMORY; |
| 186 | } |
| 187 | bits |= axisBit; |
| 188 | for (uint32_t i = count; i > index; i--) { |
| 189 | values[i] = values[i - 1]; |
| 190 | } |
| 191 | } |
| 192 | values[index] = value; |
| 193 | return OK; |
| 194 | } |
| 195 | |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 196 | static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 197 | float value = c.getAxisValue(axis); |
| 198 | if (value != 0) { |
| 199 | c.setAxisValue(axis, value * scaleFactor); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
| 203 | void PointerCoords::scale(float scaleFactor) { |
| 204 | // No need to scale pressure or size since they are normalized. |
| 205 | // No need to scale orientation since it is meaningless to do so. |
| 206 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, scaleFactor); |
| 207 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, scaleFactor); |
| 208 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, scaleFactor); |
| 209 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, scaleFactor); |
| 210 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, scaleFactor); |
| 211 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, scaleFactor); |
| 212 | } |
| 213 | |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 214 | void PointerCoords::lerp(const PointerCoords& a, const PointerCoords& b, float alpha) { |
| 215 | bits = 0; |
| 216 | for (uint64_t bitsRemaining = a.bits | b.bits; bitsRemaining; ) { |
| 217 | int32_t axis = __builtin_ctz(bitsRemaining); |
| 218 | uint64_t axisBit = 1LL << axis; |
| 219 | bitsRemaining &= ~axisBit; |
| 220 | if (a.bits & axisBit) { |
| 221 | if (b.bits & axisBit) { |
| 222 | float aval = a.getAxisValue(axis); |
| 223 | float bval = b.getAxisValue(axis); |
| 224 | setAxisValue(axis, aval + alpha * (bval - aval)); |
| 225 | } else { |
| 226 | setAxisValue(axis, a.getAxisValue(axis)); |
| 227 | } |
| 228 | } else { |
| 229 | setAxisValue(axis, b.getAxisValue(axis)); |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 234 | #ifdef HAVE_ANDROID_OS |
| 235 | status_t PointerCoords::readFromParcel(Parcel* parcel) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 236 | bits = parcel->readInt64(); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 237 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 238 | uint32_t count = __builtin_popcountll(bits); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 239 | if (count > MAX_AXES) { |
| 240 | return BAD_VALUE; |
| 241 | } |
| 242 | |
| 243 | for (uint32_t i = 0; i < count; i++) { |
| 244 | values[i] = parcel->readInt32(); |
| 245 | } |
| 246 | return OK; |
| 247 | } |
| 248 | |
| 249 | status_t PointerCoords::writeToParcel(Parcel* parcel) const { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 250 | parcel->writeInt64(bits); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 251 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 252 | uint32_t count = __builtin_popcountll(bits); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 253 | for (uint32_t i = 0; i < count; i++) { |
| 254 | parcel->writeInt32(values[i]); |
| 255 | } |
| 256 | return OK; |
| 257 | } |
| 258 | #endif |
| 259 | |
| 260 | void PointerCoords::tooManyAxes(int axis) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 261 | ALOGW("Could not set value for axis %d because the PointerCoords structure is full and " |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 262 | "cannot contain more than %d axis values.", axis, int(MAX_AXES)); |
| 263 | } |
| 264 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 265 | bool PointerCoords::operator==(const PointerCoords& other) const { |
| 266 | if (bits != other.bits) { |
| 267 | return false; |
| 268 | } |
| 269 | uint32_t count = __builtin_popcountll(bits); |
| 270 | for (uint32_t i = 0; i < count; i++) { |
| 271 | if (values[i] != other.values[i]) { |
| 272 | return false; |
| 273 | } |
| 274 | } |
| 275 | return true; |
| 276 | } |
| 277 | |
| 278 | void PointerCoords::copyFrom(const PointerCoords& other) { |
| 279 | bits = other.bits; |
| 280 | uint32_t count = __builtin_popcountll(bits); |
| 281 | for (uint32_t i = 0; i < count; i++) { |
| 282 | values[i] = other.values[i]; |
| 283 | } |
| 284 | } |
| 285 | |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 286 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 287 | // --- PointerProperties --- |
| 288 | |
| 289 | bool PointerProperties::operator==(const PointerProperties& other) const { |
| 290 | return id == other.id |
| 291 | && toolType == other.toolType; |
| 292 | } |
| 293 | |
| 294 | void PointerProperties::copyFrom(const PointerProperties& other) { |
| 295 | id = other.id; |
| 296 | toolType = other.toolType; |
| 297 | } |
| 298 | |
| 299 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 300 | // --- MotionEvent --- |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 301 | |
| 302 | void MotionEvent::initialize( |
| 303 | int32_t deviceId, |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 304 | int32_t source, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 305 | int32_t action, |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 306 | int32_t flags, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 307 | int32_t edgeFlags, |
| 308 | int32_t metaState, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 309 | int32_t buttonState, |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 310 | float xOffset, |
| 311 | float yOffset, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 312 | float xPrecision, |
| 313 | float yPrecision, |
| 314 | nsecs_t downTime, |
| 315 | nsecs_t eventTime, |
| 316 | size_t pointerCount, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 317 | const PointerProperties* pointerProperties, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 318 | const PointerCoords* pointerCoords) { |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 319 | InputEvent::initialize(deviceId, source); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 320 | mAction = action; |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 321 | mFlags = flags; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 322 | mEdgeFlags = edgeFlags; |
| 323 | mMetaState = metaState; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 324 | mButtonState = buttonState; |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 325 | mXOffset = xOffset; |
| 326 | mYOffset = yOffset; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 327 | mXPrecision = xPrecision; |
| 328 | mYPrecision = yPrecision; |
| 329 | mDownTime = downTime; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 330 | mPointerProperties.clear(); |
| 331 | mPointerProperties.appendArray(pointerProperties, pointerCount); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 332 | mSampleEventTimes.clear(); |
| 333 | mSamplePointerCoords.clear(); |
| 334 | addSample(eventTime, pointerCoords); |
| 335 | } |
| 336 | |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 337 | void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) { |
| 338 | InputEvent::initialize(other->mDeviceId, other->mSource); |
| 339 | mAction = other->mAction; |
| 340 | mFlags = other->mFlags; |
| 341 | mEdgeFlags = other->mEdgeFlags; |
| 342 | mMetaState = other->mMetaState; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 343 | mButtonState = other->mButtonState; |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 344 | mXOffset = other->mXOffset; |
| 345 | mYOffset = other->mYOffset; |
| 346 | mXPrecision = other->mXPrecision; |
| 347 | mYPrecision = other->mYPrecision; |
| 348 | mDownTime = other->mDownTime; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 349 | mPointerProperties = other->mPointerProperties; |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 350 | |
| 351 | if (keepHistory) { |
| 352 | mSampleEventTimes = other->mSampleEventTimes; |
| 353 | mSamplePointerCoords = other->mSamplePointerCoords; |
| 354 | } else { |
| 355 | mSampleEventTimes.clear(); |
| 356 | mSampleEventTimes.push(other->getEventTime()); |
| 357 | mSamplePointerCoords.clear(); |
| 358 | size_t pointerCount = other->getPointerCount(); |
| 359 | size_t historySize = other->getHistorySize(); |
| 360 | mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array() |
| 361 | + (historySize * pointerCount), pointerCount); |
| 362 | } |
| 363 | } |
| 364 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 365 | void MotionEvent::addSample( |
| 366 | int64_t eventTime, |
| 367 | const PointerCoords* pointerCoords) { |
| 368 | mSampleEventTimes.push(eventTime); |
| 369 | mSamplePointerCoords.appendArray(pointerCoords, getPointerCount()); |
| 370 | } |
| 371 | |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 372 | const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const { |
| 373 | return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex]; |
| 374 | } |
| 375 | |
| 376 | float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const { |
| 377 | return getRawPointerCoords(pointerIndex)->getAxisValue(axis); |
| 378 | } |
| 379 | |
| 380 | float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const { |
| 381 | float value = getRawPointerCoords(pointerIndex)->getAxisValue(axis); |
| 382 | switch (axis) { |
Jeff Brown | ebbd5d1 | 2011-02-17 13:01:34 -0800 | [diff] [blame] | 383 | case AMOTION_EVENT_AXIS_X: |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 384 | return value + mXOffset; |
Jeff Brown | ebbd5d1 | 2011-02-17 13:01:34 -0800 | [diff] [blame] | 385 | case AMOTION_EVENT_AXIS_Y: |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 386 | return value + mYOffset; |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 387 | } |
| 388 | return value; |
| 389 | } |
| 390 | |
| 391 | const PointerCoords* MotionEvent::getHistoricalRawPointerCoords( |
| 392 | size_t pointerIndex, size_t historicalIndex) const { |
| 393 | return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex]; |
| 394 | } |
| 395 | |
| 396 | float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex, |
| 397 | size_t historicalIndex) const { |
| 398 | return getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis); |
| 399 | } |
| 400 | |
| 401 | float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex, |
| 402 | size_t historicalIndex) const { |
| 403 | float value = getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis); |
| 404 | switch (axis) { |
Jeff Brown | ebbd5d1 | 2011-02-17 13:01:34 -0800 | [diff] [blame] | 405 | case AMOTION_EVENT_AXIS_X: |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 406 | return value + mXOffset; |
Jeff Brown | ebbd5d1 | 2011-02-17 13:01:34 -0800 | [diff] [blame] | 407 | case AMOTION_EVENT_AXIS_Y: |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 408 | return value + mYOffset; |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 409 | } |
| 410 | return value; |
| 411 | } |
| 412 | |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 413 | ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 414 | size_t pointerCount = mPointerProperties.size(); |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 415 | for (size_t i = 0; i < pointerCount; i++) { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 416 | if (mPointerProperties.itemAt(i).id == pointerId) { |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 417 | return i; |
| 418 | } |
| 419 | } |
| 420 | return -1; |
| 421 | } |
| 422 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 423 | void MotionEvent::offsetLocation(float xOffset, float yOffset) { |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 424 | mXOffset += xOffset; |
| 425 | mYOffset += yOffset; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 428 | void MotionEvent::scale(float scaleFactor) { |
| 429 | mXOffset *= scaleFactor; |
| 430 | mYOffset *= scaleFactor; |
| 431 | mXPrecision *= scaleFactor; |
| 432 | mYPrecision *= scaleFactor; |
| 433 | |
| 434 | size_t numSamples = mSamplePointerCoords.size(); |
| 435 | for (size_t i = 0; i < numSamples; i++) { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 436 | mSamplePointerCoords.editItemAt(i).scale(scaleFactor); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
| 440 | #ifdef HAVE_ANDROID_OS |
| 441 | static inline float transformAngle(const SkMatrix* matrix, float angleRadians) { |
| 442 | // Construct and transform a vector oriented at the specified clockwise angle from vertical. |
| 443 | // Coordinate system: down is increasing Y, right is increasing X. |
| 444 | SkPoint vector; |
| 445 | vector.fX = SkFloatToScalar(sinf(angleRadians)); |
| 446 | vector.fY = SkFloatToScalar(-cosf(angleRadians)); |
| 447 | matrix->mapVectors(& vector, 1); |
| 448 | |
| 449 | // Derive the transformed vector's clockwise angle from vertical. |
| 450 | float result = atan2f(SkScalarToFloat(vector.fX), SkScalarToFloat(-vector.fY)); |
| 451 | if (result < - M_PI_2) { |
| 452 | result += M_PI; |
| 453 | } else if (result > M_PI_2) { |
| 454 | result -= M_PI; |
| 455 | } |
| 456 | return result; |
| 457 | } |
| 458 | |
| 459 | void MotionEvent::transform(const SkMatrix* matrix) { |
| 460 | float oldXOffset = mXOffset; |
| 461 | float oldYOffset = mYOffset; |
| 462 | |
| 463 | // The tricky part of this implementation is to preserve the value of |
| 464 | // rawX and rawY. So we apply the transformation to the first point |
| 465 | // then derive an appropriate new X/Y offset that will preserve rawX and rawY. |
| 466 | SkPoint point; |
| 467 | float rawX = getRawX(0); |
| 468 | float rawY = getRawY(0); |
| 469 | matrix->mapXY(SkFloatToScalar(rawX + oldXOffset), SkFloatToScalar(rawY + oldYOffset), |
| 470 | & point); |
| 471 | float newX = SkScalarToFloat(point.fX); |
| 472 | float newY = SkScalarToFloat(point.fY); |
| 473 | float newXOffset = newX - rawX; |
| 474 | float newYOffset = newY - rawY; |
| 475 | |
| 476 | mXOffset = newXOffset; |
| 477 | mYOffset = newYOffset; |
| 478 | |
| 479 | // Apply the transformation to all samples. |
| 480 | size_t numSamples = mSamplePointerCoords.size(); |
| 481 | for (size_t i = 0; i < numSamples; i++) { |
| 482 | PointerCoords& c = mSamplePointerCoords.editItemAt(i); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 483 | float x = c.getAxisValue(AMOTION_EVENT_AXIS_X) + oldXOffset; |
| 484 | float y = c.getAxisValue(AMOTION_EVENT_AXIS_Y) + oldYOffset; |
| 485 | matrix->mapXY(SkFloatToScalar(x), SkFloatToScalar(y), &point); |
| 486 | c.setAxisValue(AMOTION_EVENT_AXIS_X, SkScalarToFloat(point.fX) - newXOffset); |
| 487 | c.setAxisValue(AMOTION_EVENT_AXIS_Y, SkScalarToFloat(point.fY) - newYOffset); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 488 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 489 | float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION); |
| 490 | c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(matrix, orientation)); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
| 494 | status_t MotionEvent::readFromParcel(Parcel* parcel) { |
| 495 | size_t pointerCount = parcel->readInt32(); |
| 496 | size_t sampleCount = parcel->readInt32(); |
| 497 | if (pointerCount == 0 || pointerCount > MAX_POINTERS || sampleCount == 0) { |
| 498 | return BAD_VALUE; |
| 499 | } |
| 500 | |
| 501 | mDeviceId = parcel->readInt32(); |
| 502 | mSource = parcel->readInt32(); |
| 503 | mAction = parcel->readInt32(); |
| 504 | mFlags = parcel->readInt32(); |
| 505 | mEdgeFlags = parcel->readInt32(); |
| 506 | mMetaState = parcel->readInt32(); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 507 | mButtonState = parcel->readInt32(); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 508 | mXOffset = parcel->readFloat(); |
| 509 | mYOffset = parcel->readFloat(); |
| 510 | mXPrecision = parcel->readFloat(); |
| 511 | mYPrecision = parcel->readFloat(); |
| 512 | mDownTime = parcel->readInt64(); |
| 513 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 514 | mPointerProperties.clear(); |
| 515 | mPointerProperties.setCapacity(pointerCount); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 516 | mSampleEventTimes.clear(); |
| 517 | mSampleEventTimes.setCapacity(sampleCount); |
| 518 | mSamplePointerCoords.clear(); |
| 519 | mSamplePointerCoords.setCapacity(sampleCount * pointerCount); |
| 520 | |
| 521 | for (size_t i = 0; i < pointerCount; i++) { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 522 | mPointerProperties.push(); |
| 523 | PointerProperties& properties = mPointerProperties.editTop(); |
| 524 | properties.id = parcel->readInt32(); |
| 525 | properties.toolType = parcel->readInt32(); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | while (sampleCount-- > 0) { |
| 529 | mSampleEventTimes.push(parcel->readInt64()); |
| 530 | for (size_t i = 0; i < pointerCount; i++) { |
| 531 | mSamplePointerCoords.push(); |
| 532 | status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel); |
Jeff Brown | ebbd5d1 | 2011-02-17 13:01:34 -0800 | [diff] [blame] | 533 | if (status) { |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 534 | return status; |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | return OK; |
| 539 | } |
| 540 | |
| 541 | status_t MotionEvent::writeToParcel(Parcel* parcel) const { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 542 | size_t pointerCount = mPointerProperties.size(); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 543 | size_t sampleCount = mSampleEventTimes.size(); |
| 544 | |
| 545 | parcel->writeInt32(pointerCount); |
| 546 | parcel->writeInt32(sampleCount); |
| 547 | |
| 548 | parcel->writeInt32(mDeviceId); |
| 549 | parcel->writeInt32(mSource); |
| 550 | parcel->writeInt32(mAction); |
| 551 | parcel->writeInt32(mFlags); |
| 552 | parcel->writeInt32(mEdgeFlags); |
| 553 | parcel->writeInt32(mMetaState); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 554 | parcel->writeInt32(mButtonState); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 555 | parcel->writeFloat(mXOffset); |
| 556 | parcel->writeFloat(mYOffset); |
| 557 | parcel->writeFloat(mXPrecision); |
| 558 | parcel->writeFloat(mYPrecision); |
| 559 | parcel->writeInt64(mDownTime); |
| 560 | |
| 561 | for (size_t i = 0; i < pointerCount; i++) { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 562 | const PointerProperties& properties = mPointerProperties.itemAt(i); |
| 563 | parcel->writeInt32(properties.id); |
| 564 | parcel->writeInt32(properties.toolType); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | const PointerCoords* pc = mSamplePointerCoords.array(); |
| 568 | for (size_t h = 0; h < sampleCount; h++) { |
| 569 | parcel->writeInt64(mSampleEventTimes.itemAt(h)); |
| 570 | for (size_t i = 0; i < pointerCount; i++) { |
| 571 | status_t status = (pc++)->writeToParcel(parcel); |
Jeff Brown | ebbd5d1 | 2011-02-17 13:01:34 -0800 | [diff] [blame] | 572 | if (status) { |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 573 | return status; |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | return OK; |
| 578 | } |
| 579 | #endif |
| 580 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 581 | bool MotionEvent::isTouchEvent(int32_t source, int32_t action) { |
| 582 | if (source & AINPUT_SOURCE_CLASS_POINTER) { |
| 583 | // Specifically excludes HOVER_MOVE and SCROLL. |
| 584 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
| 585 | case AMOTION_EVENT_ACTION_DOWN: |
| 586 | case AMOTION_EVENT_ACTION_MOVE: |
| 587 | case AMOTION_EVENT_ACTION_UP: |
| 588 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 589 | case AMOTION_EVENT_ACTION_POINTER_UP: |
| 590 | case AMOTION_EVENT_ACTION_CANCEL: |
| 591 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 592 | return true; |
| 593 | } |
| 594 | } |
| 595 | return false; |
| 596 | } |
| 597 | |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 598 | |
Jeff Brown | 2b6c32c | 2012-03-13 15:00:09 -0700 | [diff] [blame] | 599 | // --- PooledInputEventFactory --- |
| 600 | |
| 601 | PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) : |
| 602 | mMaxPoolSize(maxPoolSize) { |
| 603 | } |
| 604 | |
| 605 | PooledInputEventFactory::~PooledInputEventFactory() { |
| 606 | for (size_t i = 0; i < mKeyEventPool.size(); i++) { |
| 607 | delete mKeyEventPool.itemAt(i); |
| 608 | } |
| 609 | for (size_t i = 0; i < mMotionEventPool.size(); i++) { |
| 610 | delete mMotionEventPool.itemAt(i); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | KeyEvent* PooledInputEventFactory::createKeyEvent() { |
| 615 | if (!mKeyEventPool.isEmpty()) { |
| 616 | KeyEvent* event = mKeyEventPool.top(); |
| 617 | mKeyEventPool.pop(); |
| 618 | return event; |
| 619 | } |
| 620 | return new KeyEvent(); |
| 621 | } |
| 622 | |
| 623 | MotionEvent* PooledInputEventFactory::createMotionEvent() { |
| 624 | if (!mMotionEventPool.isEmpty()) { |
| 625 | MotionEvent* event = mMotionEventPool.top(); |
| 626 | mMotionEventPool.pop(); |
| 627 | return event; |
| 628 | } |
| 629 | return new MotionEvent(); |
| 630 | } |
| 631 | |
| 632 | void PooledInputEventFactory::recycle(InputEvent* event) { |
| 633 | switch (event->getType()) { |
| 634 | case AINPUT_EVENT_TYPE_KEY: |
| 635 | if (mKeyEventPool.size() < mMaxPoolSize) { |
| 636 | mKeyEventPool.push(static_cast<KeyEvent*>(event)); |
| 637 | return; |
| 638 | } |
| 639 | break; |
| 640 | case AINPUT_EVENT_TYPE_MOTION: |
| 641 | if (mMotionEventPool.size() < mMaxPoolSize) { |
| 642 | mMotionEventPool.push(static_cast<MotionEvent*>(event)); |
| 643 | return; |
| 644 | } |
| 645 | break; |
| 646 | } |
| 647 | delete event; |
| 648 | } |
| 649 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 650 | } // namespace android |