Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1 | // |
| 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 Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 10 | #define DEBUG_PROBE 0 |
| 11 | |
| 12 | #include <stdlib.h> |
| 13 | #include <unistd.h> |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 14 | #include <ctype.h> |
Jeff Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 15 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 16 | #include <ui/Input.h> |
| 17 | |
Jeff Brown | 3e34146 | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 18 | #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 Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 28 | namespace android { |
| 29 | |
Jeff Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 30 | static const char* CONFIGURATION_FILE_DIR[] = { |
| 31 | "idc/", |
| 32 | "keylayout/", |
| 33 | "keychars/", |
| 34 | }; |
| 35 | |
| 36 | static const char* CONFIGURATION_FILE_EXTENSION[] = { |
| 37 | ".idc", |
| 38 | ".kl", |
| 39 | ".kcm", |
| 40 | }; |
| 41 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 42 | static bool isValidNameChar(char ch) { |
| 43 | return isascii(ch) && (isdigit(ch) || isalpha(ch) || ch == '-' || ch == '_'); |
| 44 | } |
| 45 | |
Jeff Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 46 | static 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 Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 51 | if (!isValidNameChar(ch)) { |
Jeff Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 52 | ch = '_'; |
| 53 | } |
| 54 | path.append(&ch, 1); |
| 55 | } |
| 56 | path.append(CONFIGURATION_FILE_EXTENSION[type]); |
| 57 | } |
| 58 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 59 | String8 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 | |
| 89 | String8 getInputDeviceConfigurationFilePathByName( |
Jeff Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 90 | 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 Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 131 | |
Jeff Brown | 5c1ed84 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 132 | void InputEvent::initialize(int32_t deviceId, int32_t source) { |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 133 | mDeviceId = deviceId; |
Jeff Brown | 5c1ed84 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 134 | mSource = source; |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Dianne Hackborn | 0e88527 | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 137 | void InputEvent::initialize(const InputEvent& from) { |
| 138 | mDeviceId = from.mDeviceId; |
| 139 | mSource = from.mSource; |
| 140 | } |
| 141 | |
Jeff Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 142 | // --- KeyEvent --- |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 143 | |
Dianne Hackborn | 189ed23 | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 144 | bool KeyEvent::hasDefaultAction(int32_t keyCode) { |
| 145 | switch (keyCode) { |
Jeff Brown | 8575a87 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 146 | 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 Brown | 7e5660f | 2010-11-01 15:24:01 -0700 | [diff] [blame] | 152 | case AKEYCODE_VOLUME_MUTE: |
Jeff Brown | 8575a87 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 153 | 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 Brown | 7e5660f | 2010-11-01 15:24:01 -0700 | [diff] [blame] | 160 | case AKEYCODE_MEDIA_PLAY: |
| 161 | case AKEYCODE_MEDIA_PAUSE: |
Jeff Brown | 8575a87 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 162 | 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 Brown | 7e5660f | 2010-11-01 15:24:01 -0700 | [diff] [blame] | 167 | case AKEYCODE_MEDIA_RECORD: |
Jeff Brown | 8575a87 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 168 | case AKEYCODE_MEDIA_FAST_FORWARD: |
| 169 | case AKEYCODE_MUTE: |
Dianne Hackborn | 189ed23 | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 170 | return true; |
| 171 | } |
| 172 | |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | bool KeyEvent::hasDefaultAction() const { |
| 177 | return hasDefaultAction(getKeyCode()); |
| 178 | } |
| 179 | |
| 180 | bool KeyEvent::isSystemKey(int32_t keyCode) { |
| 181 | switch (keyCode) { |
Jeff Brown | 8575a87 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 182 | 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 Brown | 7e5660f | 2010-11-01 15:24:01 -0700 | [diff] [blame] | 190 | case AKEYCODE_VOLUME_MUTE: |
Jeff Brown | 8575a87 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 191 | case AKEYCODE_MUTE: |
| 192 | case AKEYCODE_POWER: |
| 193 | case AKEYCODE_HEADSETHOOK: |
Jeff Brown | 7e5660f | 2010-11-01 15:24:01 -0700 | [diff] [blame] | 194 | case AKEYCODE_MEDIA_PLAY: |
| 195 | case AKEYCODE_MEDIA_PAUSE: |
Jeff Brown | 8575a87 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 196 | 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 Brown | 7e5660f | 2010-11-01 15:24:01 -0700 | [diff] [blame] | 201 | case AKEYCODE_MEDIA_RECORD: |
Jeff Brown | 8575a87 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 202 | case AKEYCODE_MEDIA_FAST_FORWARD: |
| 203 | case AKEYCODE_CAMERA: |
| 204 | case AKEYCODE_FOCUS: |
| 205 | case AKEYCODE_SEARCH: |
Dianne Hackborn | 189ed23 | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 206 | return true; |
| 207 | } |
| 208 | |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | bool KeyEvent::isSystemKey() const { |
| 213 | return isSystemKey(getKeyCode()); |
| 214 | } |
| 215 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 216 | void KeyEvent::initialize( |
| 217 | int32_t deviceId, |
Jeff Brown | 5c1ed84 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 218 | int32_t source, |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 219 | 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 Brown | 5c1ed84 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 227 | InputEvent::initialize(deviceId, source); |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 228 | 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 Hackborn | 0e88527 | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 238 | void 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 Brown | 3e34146 | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 250 | |
| 251 | // --- PointerCoords --- |
| 252 | |
Jeff Brown | 3ea4de8 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 253 | float 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 | |
| 266 | status_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 | |
| 288 | float* 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 Brown | 3e34146 | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 301 | #ifdef HAVE_ANDROID_OS |
| 302 | status_t PointerCoords::readFromParcel(Parcel* parcel) { |
Jeff Brown | 3ea4de8 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 303 | bits = parcel->readInt64(); |
Jeff Brown | 3e34146 | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 304 | |
Jeff Brown | 3ea4de8 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 305 | uint32_t count = __builtin_popcountll(bits); |
Jeff Brown | 3e34146 | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 306 | 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 | |
| 316 | status_t PointerCoords::writeToParcel(Parcel* parcel) const { |
Jeff Brown | 3ea4de8 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 317 | parcel->writeInt64(bits); |
Jeff Brown | 3e34146 | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 318 | |
Jeff Brown | 3ea4de8 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 319 | uint32_t count = __builtin_popcountll(bits); |
Jeff Brown | 3e34146 | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 320 | for (uint32_t i = 0; i < count; i++) { |
| 321 | parcel->writeInt32(values[i]); |
| 322 | } |
| 323 | return OK; |
| 324 | } |
| 325 | #endif |
| 326 | |
| 327 | void 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 Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 333 | // --- MotionEvent --- |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 334 | |
| 335 | void MotionEvent::initialize( |
| 336 | int32_t deviceId, |
Jeff Brown | 5c1ed84 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 337 | int32_t source, |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 338 | int32_t action, |
Jeff Brown | af30ff6 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 339 | int32_t flags, |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 340 | int32_t edgeFlags, |
| 341 | int32_t metaState, |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 342 | float xOffset, |
| 343 | float yOffset, |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 344 | 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 Brown | 5c1ed84 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 351 | InputEvent::initialize(deviceId, source); |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 352 | mAction = action; |
Jeff Brown | af30ff6 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 353 | mFlags = flags; |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 354 | mEdgeFlags = edgeFlags; |
| 355 | mMetaState = metaState; |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 356 | mXOffset = xOffset; |
| 357 | mYOffset = yOffset; |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 358 | 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 Brown | 3e34146 | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 368 | void 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 Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 395 | void MotionEvent::addSample( |
| 396 | int64_t eventTime, |
| 397 | const PointerCoords* pointerCoords) { |
| 398 | mSampleEventTimes.push(eventTime); |
| 399 | mSamplePointerCoords.appendArray(pointerCoords, getPointerCount()); |
| 400 | } |
| 401 | |
Jeff Brown | 3e34146 | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 402 | const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const { |
| 403 | return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex]; |
| 404 | } |
| 405 | |
| 406 | float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const { |
| 407 | return getRawPointerCoords(pointerIndex)->getAxisValue(axis); |
| 408 | } |
| 409 | |
| 410 | float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const { |
| 411 | float value = getRawPointerCoords(pointerIndex)->getAxisValue(axis); |
| 412 | switch (axis) { |
Jeff Brown | b2d4435 | 2011-02-17 13:01:34 -0800 | [diff] [blame] | 413 | case AMOTION_EVENT_AXIS_X: |
Jeff Brown | 3e34146 | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 414 | value += mXOffset; |
| 415 | break; |
Jeff Brown | b2d4435 | 2011-02-17 13:01:34 -0800 | [diff] [blame] | 416 | case AMOTION_EVENT_AXIS_Y: |
Jeff Brown | 3e34146 | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 417 | value += mYOffset; |
| 418 | break; |
| 419 | } |
| 420 | return value; |
| 421 | } |
| 422 | |
| 423 | const PointerCoords* MotionEvent::getHistoricalRawPointerCoords( |
| 424 | size_t pointerIndex, size_t historicalIndex) const { |
| 425 | return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex]; |
| 426 | } |
| 427 | |
| 428 | float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex, |
| 429 | size_t historicalIndex) const { |
| 430 | return getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis); |
| 431 | } |
| 432 | |
| 433 | float 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 Brown | b2d4435 | 2011-02-17 13:01:34 -0800 | [diff] [blame] | 437 | case AMOTION_EVENT_AXIS_X: |
Jeff Brown | 3e34146 | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 438 | value += mXOffset; |
| 439 | break; |
Jeff Brown | b2d4435 | 2011-02-17 13:01:34 -0800 | [diff] [blame] | 440 | case AMOTION_EVENT_AXIS_Y: |
Jeff Brown | 3e34146 | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 441 | value += mYOffset; |
| 442 | break; |
| 443 | } |
| 444 | return value; |
| 445 | } |
| 446 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 447 | void MotionEvent::offsetLocation(float xOffset, float yOffset) { |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 448 | mXOffset += xOffset; |
| 449 | mYOffset += yOffset; |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Jeff Brown | 3e34146 | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 452 | static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) { |
| 453 | float* value = c.editAxisValue(axis); |
| 454 | if (value) { |
| 455 | *value *= scaleFactor; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | void 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 Brown | b2d4435 | 2011-02-17 13:01:34 -0800 | [diff] [blame] | 470 | 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 Brown | 3e34146 | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 476 | } |
| 477 | } |
| 478 | |
| 479 | #ifdef HAVE_ANDROID_OS |
| 480 | static 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 | |
| 498 | void 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 Brown | b2d4435 | 2011-02-17 13:01:34 -0800 | [diff] [blame] | 522 | float* xPtr = c.editAxisValue(AMOTION_EVENT_AXIS_X); |
| 523 | float* yPtr = c.editAxisValue(AMOTION_EVENT_AXIS_Y); |
Jeff Brown | 3e34146 | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 524 | 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 Brown | b2d4435 | 2011-02-17 13:01:34 -0800 | [diff] [blame] | 532 | float* orientationPtr = c.editAxisValue(AMOTION_EVENT_AXIS_ORIENTATION); |
Jeff Brown | 3e34146 | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 533 | if (orientationPtr) { |
| 534 | *orientationPtr = transformAngle(matrix, *orientationPtr); |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | status_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 Brown | b2d4435 | 2011-02-17 13:01:34 -0800 | [diff] [blame] | 574 | if (status) { |
Jeff Brown | 3e34146 | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 575 | return status; |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | return OK; |
| 580 | } |
| 581 | |
| 582 | status_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 Brown | b2d4435 | 2011-02-17 13:01:34 -0800 | [diff] [blame] | 610 | if (status) { |
Jeff Brown | 3e34146 | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 611 | return status; |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | return OK; |
| 616 | } |
| 617 | #endif |
| 618 | |
Jeff Brown | d5ed285 | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 619 | bool 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 Brown | 3e34146 | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 636 | |
Jeff Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 637 | // --- InputDeviceInfo --- |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 638 | |
| 639 | InputDeviceInfo::InputDeviceInfo() { |
| 640 | initialize(-1, String8("uninitialized device info")); |
| 641 | } |
| 642 | |
| 643 | InputDeviceInfo::InputDeviceInfo(const InputDeviceInfo& other) : |
| 644 | mId(other.mId), mName(other.mName), mSources(other.mSources), |
| 645 | mKeyboardType(other.mKeyboardType), |
| 646 | mMotionRanges(other.mMotionRanges) { |
| 647 | } |
| 648 | |
| 649 | InputDeviceInfo::~InputDeviceInfo() { |
| 650 | } |
| 651 | |
| 652 | void 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 Brown | 46689da | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 660 | const 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 ⦥ |
| 667 | } |
| 668 | } |
| 669 | return NULL; |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | void InputDeviceInfo::addSource(uint32_t source) { |
| 673 | mSources |= source; |
| 674 | } |
| 675 | |
Jeff Brown | 46689da | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 676 | void InputDeviceInfo::addMotionRange(int32_t axis, uint32_t source, float min, float max, |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 677 | float flat, float fuzz) { |
Jeff Brown | 46689da | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 678 | MotionRange range = { axis, source, min, max, flat, fuzz }; |
| 679 | mMotionRanges.add(range); |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 680 | } |
| 681 | |
Jeff Brown | 46689da | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 682 | void InputDeviceInfo::addMotionRange(const MotionRange& range) { |
| 683 | mMotionRanges.add(range); |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 686 | } // namespace android |