Jeff Brown | 46b9ac0 | 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 | 47e6b1b | 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 | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 14 | #include <ctype.h> |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 15 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 16 | #include <ui/Input.h> |
| 17 | |
Jeff Brown | 91c69ab | 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 | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 28 | namespace android { |
| 29 | |
Jeff Brown | 47e6b1b | 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 | 9065504 | 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 | 47e6b1b | 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 | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 51 | if (!isValidNameChar(ch)) { |
Jeff Brown | 47e6b1b | 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 | 9065504 | 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 | 47e6b1b | 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 | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 131 | |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 132 | void InputEvent::initialize(int32_t deviceId, int32_t source) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 133 | mDeviceId = deviceId; |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 134 | mSource = source; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Dianne Hackborn | 2c6081c | 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 | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 142 | // --- KeyEvent --- |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 143 | |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 144 | bool KeyEvent::hasDefaultAction(int32_t keyCode) { |
| 145 | switch (keyCode) { |
Jeff Brown | fd03582 | 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 | b0418da | 2010-11-01 15:24:01 -0700 | [diff] [blame] | 152 | case AKEYCODE_VOLUME_MUTE: |
Jeff Brown | fd03582 | 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 | b0418da | 2010-11-01 15:24:01 -0700 | [diff] [blame] | 160 | case AKEYCODE_MEDIA_PLAY: |
| 161 | case AKEYCODE_MEDIA_PAUSE: |
Jeff Brown | fd03582 | 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 | b0418da | 2010-11-01 15:24:01 -0700 | [diff] [blame] | 167 | case AKEYCODE_MEDIA_RECORD: |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 168 | case AKEYCODE_MEDIA_FAST_FORWARD: |
| 169 | case AKEYCODE_MUTE: |
Dianne Hackborn | 3c80a4a | 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 | fd03582 | 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 | b0418da | 2010-11-01 15:24:01 -0700 | [diff] [blame] | 190 | case AKEYCODE_VOLUME_MUTE: |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 191 | case AKEYCODE_MUTE: |
| 192 | case AKEYCODE_POWER: |
| 193 | case AKEYCODE_HEADSETHOOK: |
Jeff Brown | b0418da | 2010-11-01 15:24:01 -0700 | [diff] [blame] | 194 | case AKEYCODE_MEDIA_PLAY: |
| 195 | case AKEYCODE_MEDIA_PAUSE: |
Jeff Brown | fd03582 | 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 | b0418da | 2010-11-01 15:24:01 -0700 | [diff] [blame] | 201 | case AKEYCODE_MEDIA_RECORD: |
Jeff Brown | fd03582 | 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 | 3c80a4a | 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 | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 216 | void KeyEvent::initialize( |
| 217 | int32_t deviceId, |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 218 | int32_t source, |
Jeff Brown | 46b9ac0 | 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 | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 227 | InputEvent::initialize(deviceId, source); |
Jeff Brown | 46b9ac0 | 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 | 2c6081c | 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 | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 250 | |
| 251 | // --- PointerCoords --- |
| 252 | |
| 253 | #ifdef HAVE_ANDROID_OS |
| 254 | status_t PointerCoords::readFromParcel(Parcel* parcel) { |
| 255 | bits = parcel->readInt32(); |
| 256 | |
| 257 | uint32_t count = __builtin_popcount(bits); |
| 258 | if (count > MAX_AXES) { |
| 259 | return BAD_VALUE; |
| 260 | } |
| 261 | |
| 262 | for (uint32_t i = 0; i < count; i++) { |
| 263 | values[i] = parcel->readInt32(); |
| 264 | } |
| 265 | return OK; |
| 266 | } |
| 267 | |
| 268 | status_t PointerCoords::writeToParcel(Parcel* parcel) const { |
| 269 | parcel->writeInt32(bits); |
| 270 | |
| 271 | uint32_t count = __builtin_popcount(bits); |
| 272 | for (uint32_t i = 0; i < count; i++) { |
| 273 | parcel->writeInt32(values[i]); |
| 274 | } |
| 275 | return OK; |
| 276 | } |
| 277 | #endif |
| 278 | |
| 279 | void PointerCoords::tooManyAxes(int axis) { |
| 280 | LOGW("Could not set value for axis %d because the PointerCoords structure is full and " |
| 281 | "cannot contain more than %d axis values.", axis, int(MAX_AXES)); |
| 282 | } |
| 283 | |
| 284 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 285 | // --- MotionEvent --- |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 286 | |
| 287 | void MotionEvent::initialize( |
| 288 | int32_t deviceId, |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 289 | int32_t source, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 290 | int32_t action, |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 291 | int32_t flags, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 292 | int32_t edgeFlags, |
| 293 | int32_t metaState, |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 294 | float xOffset, |
| 295 | float yOffset, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 296 | float xPrecision, |
| 297 | float yPrecision, |
| 298 | nsecs_t downTime, |
| 299 | nsecs_t eventTime, |
| 300 | size_t pointerCount, |
| 301 | const int32_t* pointerIds, |
| 302 | const PointerCoords* pointerCoords) { |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 303 | InputEvent::initialize(deviceId, source); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 304 | mAction = action; |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 305 | mFlags = flags; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 306 | mEdgeFlags = edgeFlags; |
| 307 | mMetaState = metaState; |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 308 | mXOffset = xOffset; |
| 309 | mYOffset = yOffset; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 310 | mXPrecision = xPrecision; |
| 311 | mYPrecision = yPrecision; |
| 312 | mDownTime = downTime; |
| 313 | mPointerIds.clear(); |
| 314 | mPointerIds.appendArray(pointerIds, pointerCount); |
| 315 | mSampleEventTimes.clear(); |
| 316 | mSamplePointerCoords.clear(); |
| 317 | addSample(eventTime, pointerCoords); |
| 318 | } |
| 319 | |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 320 | void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) { |
| 321 | InputEvent::initialize(other->mDeviceId, other->mSource); |
| 322 | mAction = other->mAction; |
| 323 | mFlags = other->mFlags; |
| 324 | mEdgeFlags = other->mEdgeFlags; |
| 325 | mMetaState = other->mMetaState; |
| 326 | mXOffset = other->mXOffset; |
| 327 | mYOffset = other->mYOffset; |
| 328 | mXPrecision = other->mXPrecision; |
| 329 | mYPrecision = other->mYPrecision; |
| 330 | mDownTime = other->mDownTime; |
| 331 | mPointerIds = other->mPointerIds; |
| 332 | |
| 333 | if (keepHistory) { |
| 334 | mSampleEventTimes = other->mSampleEventTimes; |
| 335 | mSamplePointerCoords = other->mSamplePointerCoords; |
| 336 | } else { |
| 337 | mSampleEventTimes.clear(); |
| 338 | mSampleEventTimes.push(other->getEventTime()); |
| 339 | mSamplePointerCoords.clear(); |
| 340 | size_t pointerCount = other->getPointerCount(); |
| 341 | size_t historySize = other->getHistorySize(); |
| 342 | mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array() |
| 343 | + (historySize * pointerCount), pointerCount); |
| 344 | } |
| 345 | } |
| 346 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 347 | void MotionEvent::addSample( |
| 348 | int64_t eventTime, |
| 349 | const PointerCoords* pointerCoords) { |
| 350 | mSampleEventTimes.push(eventTime); |
| 351 | mSamplePointerCoords.appendArray(pointerCoords, getPointerCount()); |
| 352 | } |
| 353 | |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 354 | const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const { |
| 355 | return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex]; |
| 356 | } |
| 357 | |
| 358 | float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const { |
| 359 | return getRawPointerCoords(pointerIndex)->getAxisValue(axis); |
| 360 | } |
| 361 | |
| 362 | float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const { |
| 363 | float value = getRawPointerCoords(pointerIndex)->getAxisValue(axis); |
| 364 | switch (axis) { |
| 365 | case AINPUT_MOTION_AXIS_X: |
| 366 | value += mXOffset; |
| 367 | break; |
| 368 | case AINPUT_MOTION_AXIS_Y: |
| 369 | value += mYOffset; |
| 370 | break; |
| 371 | } |
| 372 | return value; |
| 373 | } |
| 374 | |
| 375 | const PointerCoords* MotionEvent::getHistoricalRawPointerCoords( |
| 376 | size_t pointerIndex, size_t historicalIndex) const { |
| 377 | return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex]; |
| 378 | } |
| 379 | |
| 380 | float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex, |
| 381 | size_t historicalIndex) const { |
| 382 | return getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis); |
| 383 | } |
| 384 | |
| 385 | float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex, |
| 386 | size_t historicalIndex) const { |
| 387 | float value = getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis); |
| 388 | switch (axis) { |
| 389 | case AINPUT_MOTION_AXIS_X: |
| 390 | value += mXOffset; |
| 391 | break; |
| 392 | case AINPUT_MOTION_AXIS_Y: |
| 393 | value += mYOffset; |
| 394 | break; |
| 395 | } |
| 396 | return value; |
| 397 | } |
| 398 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 399 | void MotionEvent::offsetLocation(float xOffset, float yOffset) { |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 400 | mXOffset += xOffset; |
| 401 | mYOffset += yOffset; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 404 | static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) { |
| 405 | float* value = c.editAxisValue(axis); |
| 406 | if (value) { |
| 407 | *value *= scaleFactor; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | void MotionEvent::scale(float scaleFactor) { |
| 412 | mXOffset *= scaleFactor; |
| 413 | mYOffset *= scaleFactor; |
| 414 | mXPrecision *= scaleFactor; |
| 415 | mYPrecision *= scaleFactor; |
| 416 | |
| 417 | size_t numSamples = mSamplePointerCoords.size(); |
| 418 | for (size_t i = 0; i < numSamples; i++) { |
| 419 | PointerCoords& c = mSamplePointerCoords.editItemAt(i); |
| 420 | // No need to scale pressure or size since they are normalized. |
| 421 | // No need to scale orientation since it is meaningless to do so. |
| 422 | scaleAxisValue(c, AINPUT_MOTION_AXIS_X, scaleFactor); |
| 423 | scaleAxisValue(c, AINPUT_MOTION_AXIS_Y, scaleFactor); |
| 424 | scaleAxisValue(c, AINPUT_MOTION_AXIS_TOUCH_MAJOR, scaleFactor); |
| 425 | scaleAxisValue(c, AINPUT_MOTION_AXIS_TOUCH_MINOR, scaleFactor); |
| 426 | scaleAxisValue(c, AINPUT_MOTION_AXIS_TOOL_MAJOR, scaleFactor); |
| 427 | scaleAxisValue(c, AINPUT_MOTION_AXIS_TOOL_MINOR, scaleFactor); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | #ifdef HAVE_ANDROID_OS |
| 432 | static inline float transformAngle(const SkMatrix* matrix, float angleRadians) { |
| 433 | // Construct and transform a vector oriented at the specified clockwise angle from vertical. |
| 434 | // Coordinate system: down is increasing Y, right is increasing X. |
| 435 | SkPoint vector; |
| 436 | vector.fX = SkFloatToScalar(sinf(angleRadians)); |
| 437 | vector.fY = SkFloatToScalar(-cosf(angleRadians)); |
| 438 | matrix->mapVectors(& vector, 1); |
| 439 | |
| 440 | // Derive the transformed vector's clockwise angle from vertical. |
| 441 | float result = atan2f(SkScalarToFloat(vector.fX), SkScalarToFloat(-vector.fY)); |
| 442 | if (result < - M_PI_2) { |
| 443 | result += M_PI; |
| 444 | } else if (result > M_PI_2) { |
| 445 | result -= M_PI; |
| 446 | } |
| 447 | return result; |
| 448 | } |
| 449 | |
| 450 | void MotionEvent::transform(const SkMatrix* matrix) { |
| 451 | float oldXOffset = mXOffset; |
| 452 | float oldYOffset = mYOffset; |
| 453 | |
| 454 | // The tricky part of this implementation is to preserve the value of |
| 455 | // rawX and rawY. So we apply the transformation to the first point |
| 456 | // then derive an appropriate new X/Y offset that will preserve rawX and rawY. |
| 457 | SkPoint point; |
| 458 | float rawX = getRawX(0); |
| 459 | float rawY = getRawY(0); |
| 460 | matrix->mapXY(SkFloatToScalar(rawX + oldXOffset), SkFloatToScalar(rawY + oldYOffset), |
| 461 | & point); |
| 462 | float newX = SkScalarToFloat(point.fX); |
| 463 | float newY = SkScalarToFloat(point.fY); |
| 464 | float newXOffset = newX - rawX; |
| 465 | float newYOffset = newY - rawY; |
| 466 | |
| 467 | mXOffset = newXOffset; |
| 468 | mYOffset = newYOffset; |
| 469 | |
| 470 | // Apply the transformation to all samples. |
| 471 | size_t numSamples = mSamplePointerCoords.size(); |
| 472 | for (size_t i = 0; i < numSamples; i++) { |
| 473 | PointerCoords& c = mSamplePointerCoords.editItemAt(i); |
| 474 | float* xPtr = c.editAxisValue(AINPUT_MOTION_AXIS_X); |
| 475 | float* yPtr = c.editAxisValue(AINPUT_MOTION_AXIS_Y); |
| 476 | if (xPtr && yPtr) { |
| 477 | float x = *xPtr + oldXOffset; |
| 478 | float y = *yPtr + oldYOffset; |
| 479 | matrix->mapXY(SkFloatToScalar(x), SkFloatToScalar(y), & point); |
| 480 | *xPtr = SkScalarToFloat(point.fX) - newXOffset; |
| 481 | *yPtr = SkScalarToFloat(point.fY) - newYOffset; |
| 482 | } |
| 483 | |
| 484 | float* orientationPtr = c.editAxisValue(AINPUT_MOTION_AXIS_ORIENTATION); |
| 485 | if (orientationPtr) { |
| 486 | *orientationPtr = transformAngle(matrix, *orientationPtr); |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | status_t MotionEvent::readFromParcel(Parcel* parcel) { |
| 492 | size_t pointerCount = parcel->readInt32(); |
| 493 | size_t sampleCount = parcel->readInt32(); |
| 494 | if (pointerCount == 0 || pointerCount > MAX_POINTERS || sampleCount == 0) { |
| 495 | return BAD_VALUE; |
| 496 | } |
| 497 | |
| 498 | mDeviceId = parcel->readInt32(); |
| 499 | mSource = parcel->readInt32(); |
| 500 | mAction = parcel->readInt32(); |
| 501 | mFlags = parcel->readInt32(); |
| 502 | mEdgeFlags = parcel->readInt32(); |
| 503 | mMetaState = parcel->readInt32(); |
| 504 | mXOffset = parcel->readFloat(); |
| 505 | mYOffset = parcel->readFloat(); |
| 506 | mXPrecision = parcel->readFloat(); |
| 507 | mYPrecision = parcel->readFloat(); |
| 508 | mDownTime = parcel->readInt64(); |
| 509 | |
| 510 | mPointerIds.clear(); |
| 511 | mPointerIds.setCapacity(pointerCount); |
| 512 | mSampleEventTimes.clear(); |
| 513 | mSampleEventTimes.setCapacity(sampleCount); |
| 514 | mSamplePointerCoords.clear(); |
| 515 | mSamplePointerCoords.setCapacity(sampleCount * pointerCount); |
| 516 | |
| 517 | for (size_t i = 0; i < pointerCount; i++) { |
| 518 | mPointerIds.push(parcel->readInt32()); |
| 519 | } |
| 520 | |
| 521 | while (sampleCount-- > 0) { |
| 522 | mSampleEventTimes.push(parcel->readInt64()); |
| 523 | for (size_t i = 0; i < pointerCount; i++) { |
| 524 | mSamplePointerCoords.push(); |
| 525 | status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel); |
| 526 | if (!status) { |
| 527 | return status; |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | return OK; |
| 532 | } |
| 533 | |
| 534 | status_t MotionEvent::writeToParcel(Parcel* parcel) const { |
| 535 | size_t pointerCount = mPointerIds.size(); |
| 536 | size_t sampleCount = mSampleEventTimes.size(); |
| 537 | |
| 538 | parcel->writeInt32(pointerCount); |
| 539 | parcel->writeInt32(sampleCount); |
| 540 | |
| 541 | parcel->writeInt32(mDeviceId); |
| 542 | parcel->writeInt32(mSource); |
| 543 | parcel->writeInt32(mAction); |
| 544 | parcel->writeInt32(mFlags); |
| 545 | parcel->writeInt32(mEdgeFlags); |
| 546 | parcel->writeInt32(mMetaState); |
| 547 | parcel->writeFloat(mXOffset); |
| 548 | parcel->writeFloat(mYOffset); |
| 549 | parcel->writeFloat(mXPrecision); |
| 550 | parcel->writeFloat(mYPrecision); |
| 551 | parcel->writeInt64(mDownTime); |
| 552 | |
| 553 | for (size_t i = 0; i < pointerCount; i++) { |
| 554 | parcel->writeInt32(mPointerIds.itemAt(i)); |
| 555 | } |
| 556 | |
| 557 | const PointerCoords* pc = mSamplePointerCoords.array(); |
| 558 | for (size_t h = 0; h < sampleCount; h++) { |
| 559 | parcel->writeInt64(mSampleEventTimes.itemAt(h)); |
| 560 | for (size_t i = 0; i < pointerCount; i++) { |
| 561 | status_t status = (pc++)->writeToParcel(parcel); |
| 562 | if (!status) { |
| 563 | return status; |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | return OK; |
| 568 | } |
| 569 | #endif |
| 570 | |
| 571 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 572 | // --- InputDeviceInfo --- |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 573 | |
| 574 | InputDeviceInfo::InputDeviceInfo() { |
| 575 | initialize(-1, String8("uninitialized device info")); |
| 576 | } |
| 577 | |
| 578 | InputDeviceInfo::InputDeviceInfo(const InputDeviceInfo& other) : |
| 579 | mId(other.mId), mName(other.mName), mSources(other.mSources), |
| 580 | mKeyboardType(other.mKeyboardType), |
| 581 | mMotionRanges(other.mMotionRanges) { |
| 582 | } |
| 583 | |
| 584 | InputDeviceInfo::~InputDeviceInfo() { |
| 585 | } |
| 586 | |
| 587 | void InputDeviceInfo::initialize(int32_t id, const String8& name) { |
| 588 | mId = id; |
| 589 | mName = name; |
| 590 | mSources = 0; |
| 591 | mKeyboardType = AINPUT_KEYBOARD_TYPE_NONE; |
| 592 | mMotionRanges.clear(); |
| 593 | } |
| 594 | |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 595 | const InputDeviceInfo::MotionRange* InputDeviceInfo::getMotionRange(int32_t axis) const { |
| 596 | ssize_t index = mMotionRanges.indexOfKey(axis); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 597 | return index >= 0 ? & mMotionRanges.valueAt(index) : NULL; |
| 598 | } |
| 599 | |
| 600 | void InputDeviceInfo::addSource(uint32_t source) { |
| 601 | mSources |= source; |
| 602 | } |
| 603 | |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 604 | void InputDeviceInfo::addMotionRange(int32_t axis, float min, float max, |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 605 | float flat, float fuzz) { |
| 606 | MotionRange range = { min, max, flat, fuzz }; |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 607 | addMotionRange(axis, range); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 610 | void InputDeviceInfo::addMotionRange(int32_t axis, const MotionRange& range) { |
| 611 | mMotionRanges.add(axis, range); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 614 | } // namespace android |