Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [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 "InputReader" |
| 18 | |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | |
| 21 | // Log debug messages for each raw event received from the EventHub. |
| 22 | #define DEBUG_RAW_EVENTS 0 |
| 23 | |
| 24 | // Log debug messages about touch screen filtering hacks. |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 25 | #define DEBUG_HACKS 0 |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 26 | |
| 27 | // Log debug messages about virtual key processing. |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 28 | #define DEBUG_VIRTUAL_KEYS 0 |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 29 | |
| 30 | // Log debug messages about pointers. |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 31 | #define DEBUG_POINTERS 0 |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 32 | |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 33 | // Log debug messages about pointer assignment calculations. |
| 34 | #define DEBUG_POINTER_ASSIGNMENT 0 |
| 35 | |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 36 | // Log debug messages about gesture detection. |
| 37 | #define DEBUG_GESTURES 0 |
| 38 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 39 | #include "InputReader.h" |
| 40 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 41 | #include <cutils/log.h> |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 42 | #include <ui/Keyboard.h> |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 43 | #include <ui/VirtualKeyMap.h> |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 44 | |
| 45 | #include <stddef.h> |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 46 | #include <stdlib.h> |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 47 | #include <unistd.h> |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 48 | #include <errno.h> |
| 49 | #include <limits.h> |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 50 | #include <math.h> |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 51 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 52 | #define INDENT " " |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 53 | #define INDENT2 " " |
| 54 | #define INDENT3 " " |
| 55 | #define INDENT4 " " |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 56 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 57 | namespace android { |
| 58 | |
| 59 | // --- Static Functions --- |
| 60 | |
| 61 | template<typename T> |
| 62 | inline static T abs(const T& value) { |
| 63 | return value < 0 ? - value : value; |
| 64 | } |
| 65 | |
| 66 | template<typename T> |
| 67 | inline static T min(const T& a, const T& b) { |
| 68 | return a < b ? a : b; |
| 69 | } |
| 70 | |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 71 | template<typename T> |
| 72 | inline static void swap(T& a, T& b) { |
| 73 | T temp = a; |
| 74 | a = b; |
| 75 | b = temp; |
| 76 | } |
| 77 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 78 | inline static float avg(float x, float y) { |
| 79 | return (x + y) / 2; |
| 80 | } |
| 81 | |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 82 | inline static float distance(float x1, float y1, float x2, float y2) { |
| 83 | return hypotf(x1 - x2, y1 - y2); |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Jeff Brown | 517bb4c | 2011-01-14 19:09:23 -0800 | [diff] [blame] | 86 | inline static int32_t signExtendNybble(int32_t value) { |
| 87 | return value >= 8 ? value - 16 : value; |
| 88 | } |
| 89 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 90 | static inline const char* toString(bool value) { |
| 91 | return value ? "true" : "false"; |
| 92 | } |
| 93 | |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 94 | static int32_t rotateValueUsingRotationMap(int32_t value, int32_t orientation, |
| 95 | const int32_t map[][4], size_t mapSize) { |
| 96 | if (orientation != DISPLAY_ORIENTATION_0) { |
| 97 | for (size_t i = 0; i < mapSize; i++) { |
| 98 | if (value == map[i][0]) { |
| 99 | return map[i][orientation]; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | return value; |
| 104 | } |
| 105 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 106 | static const int32_t keyCodeRotationMap[][4] = { |
| 107 | // key codes enumerated counter-clockwise with the original (unrotated) key first |
| 108 | // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 109 | { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT }, |
| 110 | { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN }, |
| 111 | { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT }, |
| 112 | { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP }, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 113 | }; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 114 | static const size_t keyCodeRotationMapSize = |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 115 | sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]); |
| 116 | |
| 117 | int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) { |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 118 | return rotateValueUsingRotationMap(keyCode, orientation, |
| 119 | keyCodeRotationMap, keyCodeRotationMapSize); |
| 120 | } |
| 121 | |
| 122 | static const int32_t edgeFlagRotationMap[][4] = { |
| 123 | // edge flags enumerated counter-clockwise with the original (unrotated) edge flag first |
| 124 | // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation |
| 125 | { AMOTION_EVENT_EDGE_FLAG_BOTTOM, AMOTION_EVENT_EDGE_FLAG_RIGHT, |
| 126 | AMOTION_EVENT_EDGE_FLAG_TOP, AMOTION_EVENT_EDGE_FLAG_LEFT }, |
| 127 | { AMOTION_EVENT_EDGE_FLAG_RIGHT, AMOTION_EVENT_EDGE_FLAG_TOP, |
| 128 | AMOTION_EVENT_EDGE_FLAG_LEFT, AMOTION_EVENT_EDGE_FLAG_BOTTOM }, |
| 129 | { AMOTION_EVENT_EDGE_FLAG_TOP, AMOTION_EVENT_EDGE_FLAG_LEFT, |
| 130 | AMOTION_EVENT_EDGE_FLAG_BOTTOM, AMOTION_EVENT_EDGE_FLAG_RIGHT }, |
| 131 | { AMOTION_EVENT_EDGE_FLAG_LEFT, AMOTION_EVENT_EDGE_FLAG_BOTTOM, |
| 132 | AMOTION_EVENT_EDGE_FLAG_RIGHT, AMOTION_EVENT_EDGE_FLAG_TOP }, |
| 133 | }; |
| 134 | static const size_t edgeFlagRotationMapSize = |
| 135 | sizeof(edgeFlagRotationMap) / sizeof(edgeFlagRotationMap[0]); |
| 136 | |
| 137 | static int32_t rotateEdgeFlag(int32_t edgeFlag, int32_t orientation) { |
| 138 | return rotateValueUsingRotationMap(edgeFlag, orientation, |
| 139 | edgeFlagRotationMap, edgeFlagRotationMapSize); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 142 | static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) { |
| 143 | return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0; |
| 144 | } |
| 145 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 146 | static uint32_t getButtonStateForScanCode(int32_t scanCode) { |
| 147 | // Currently all buttons are mapped to the primary button. |
| 148 | switch (scanCode) { |
| 149 | case BTN_LEFT: |
| 150 | case BTN_RIGHT: |
| 151 | case BTN_MIDDLE: |
| 152 | case BTN_SIDE: |
| 153 | case BTN_EXTRA: |
| 154 | case BTN_FORWARD: |
| 155 | case BTN_BACK: |
| 156 | case BTN_TASK: |
| 157 | return BUTTON_STATE_PRIMARY; |
| 158 | default: |
| 159 | return 0; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // Returns true if the pointer should be reported as being down given the specified |
| 164 | // button states. |
| 165 | static bool isPointerDown(uint32_t buttonState) { |
| 166 | return buttonState & BUTTON_STATE_PRIMARY; |
| 167 | } |
| 168 | |
| 169 | static int32_t calculateEdgeFlagsUsingPointerBounds( |
| 170 | const sp<PointerControllerInterface>& pointerController, float x, float y) { |
| 171 | int32_t edgeFlags = 0; |
| 172 | float minX, minY, maxX, maxY; |
| 173 | if (pointerController->getBounds(&minX, &minY, &maxX, &maxY)) { |
| 174 | if (x <= minX) { |
| 175 | edgeFlags |= AMOTION_EVENT_EDGE_FLAG_LEFT; |
| 176 | } else if (x >= maxX) { |
| 177 | edgeFlags |= AMOTION_EVENT_EDGE_FLAG_RIGHT; |
| 178 | } |
| 179 | if (y <= minY) { |
| 180 | edgeFlags |= AMOTION_EVENT_EDGE_FLAG_TOP; |
| 181 | } else if (y >= maxY) { |
| 182 | edgeFlags |= AMOTION_EVENT_EDGE_FLAG_BOTTOM; |
| 183 | } |
| 184 | } |
| 185 | return edgeFlags; |
| 186 | } |
| 187 | |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 188 | static void clampPositionUsingPointerBounds( |
| 189 | const sp<PointerControllerInterface>& pointerController, float* x, float* y) { |
| 190 | float minX, minY, maxX, maxY; |
| 191 | if (pointerController->getBounds(&minX, &minY, &maxX, &maxY)) { |
| 192 | if (*x < minX) { |
| 193 | *x = minX; |
| 194 | } else if (*x > maxX) { |
| 195 | *x = maxX; |
| 196 | } |
| 197 | if (*y < minY) { |
| 198 | *y = minY; |
| 199 | } else if (*y > maxY) { |
| 200 | *y = maxY; |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | static float calculateCommonVector(float a, float b) { |
| 206 | if (a > 0 && b > 0) { |
| 207 | return a < b ? a : b; |
| 208 | } else if (a < 0 && b < 0) { |
| 209 | return a > b ? a : b; |
| 210 | } else { |
| 211 | return 0; |
| 212 | } |
| 213 | } |
| 214 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 215 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 216 | // --- InputReader --- |
| 217 | |
| 218 | InputReader::InputReader(const sp<EventHubInterface>& eventHub, |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 219 | const sp<InputReaderPolicyInterface>& policy, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 220 | const sp<InputDispatcherInterface>& dispatcher) : |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 221 | mEventHub(eventHub), mPolicy(policy), mDispatcher(dispatcher), |
Jeff Brown | 68d6075 | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 222 | mGlobalMetaState(0), mDisableVirtualKeysTimeout(LLONG_MIN), mNextTimeout(LLONG_MAX) { |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 223 | mPolicy->getReaderConfiguration(&mConfig); |
| 224 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 225 | configureExcludedDevices(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 226 | updateGlobalMetaState(); |
| 227 | updateInputConfiguration(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | InputReader::~InputReader() { |
| 231 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 232 | delete mDevices.valueAt(i); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | void InputReader::loopOnce() { |
Jeff Brown | 68d6075 | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 237 | int32_t timeoutMillis = -1; |
| 238 | if (mNextTimeout != LLONG_MAX) { |
| 239 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
| 240 | timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout); |
| 241 | } |
| 242 | |
Jeff Brown | dbf8d27 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 243 | size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE); |
| 244 | if (count) { |
| 245 | processEvents(mEventBuffer, count); |
| 246 | } |
| 247 | if (!count || timeoutMillis == 0) { |
Jeff Brown | 68d6075 | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 248 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
| 249 | #if DEBUG_RAW_EVENTS |
| 250 | LOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f); |
| 251 | #endif |
| 252 | mNextTimeout = LLONG_MAX; |
| 253 | timeoutExpired(now); |
| 254 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Jeff Brown | dbf8d27 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 257 | void InputReader::processEvents(const RawEvent* rawEvents, size_t count) { |
| 258 | for (const RawEvent* rawEvent = rawEvents; count;) { |
| 259 | int32_t type = rawEvent->type; |
| 260 | size_t batchSize = 1; |
| 261 | if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) { |
| 262 | int32_t deviceId = rawEvent->deviceId; |
| 263 | while (batchSize < count) { |
| 264 | if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT |
| 265 | || rawEvent[batchSize].deviceId != deviceId) { |
| 266 | break; |
| 267 | } |
| 268 | batchSize += 1; |
| 269 | } |
| 270 | #if DEBUG_RAW_EVENTS |
| 271 | LOGD("BatchSize: %d Count: %d", batchSize, count); |
| 272 | #endif |
| 273 | processEventsForDevice(deviceId, rawEvent, batchSize); |
| 274 | } else { |
| 275 | switch (rawEvent->type) { |
| 276 | case EventHubInterface::DEVICE_ADDED: |
| 277 | addDevice(rawEvent->deviceId); |
| 278 | break; |
| 279 | case EventHubInterface::DEVICE_REMOVED: |
| 280 | removeDevice(rawEvent->deviceId); |
| 281 | break; |
| 282 | case EventHubInterface::FINISHED_DEVICE_SCAN: |
| 283 | handleConfigurationChanged(rawEvent->when); |
| 284 | break; |
| 285 | default: |
| 286 | assert(false); // can't happen |
| 287 | break; |
| 288 | } |
| 289 | } |
| 290 | count -= batchSize; |
| 291 | rawEvent += batchSize; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 295 | void InputReader::addDevice(int32_t deviceId) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 296 | String8 name = mEventHub->getDeviceName(deviceId); |
| 297 | uint32_t classes = mEventHub->getDeviceClasses(deviceId); |
| 298 | |
| 299 | InputDevice* device = createDevice(deviceId, name, classes); |
| 300 | device->configure(); |
| 301 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 302 | if (device->isIgnored()) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 303 | LOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId, name.string()); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 304 | } else { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 305 | LOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, name.string(), |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 306 | device->getSources()); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 309 | bool added = false; |
| 310 | { // acquire device registry writer lock |
| 311 | RWLock::AutoWLock _wl(mDeviceRegistryLock); |
| 312 | |
| 313 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 314 | if (deviceIndex < 0) { |
| 315 | mDevices.add(deviceId, device); |
| 316 | added = true; |
| 317 | } |
| 318 | } // release device registry writer lock |
| 319 | |
| 320 | if (! added) { |
| 321 | LOGW("Ignoring spurious device added event for deviceId %d.", deviceId); |
| 322 | delete device; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 323 | return; |
| 324 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 327 | void InputReader::removeDevice(int32_t deviceId) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 328 | bool removed = false; |
| 329 | InputDevice* device = NULL; |
| 330 | { // acquire device registry writer lock |
| 331 | RWLock::AutoWLock _wl(mDeviceRegistryLock); |
| 332 | |
| 333 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 334 | if (deviceIndex >= 0) { |
| 335 | device = mDevices.valueAt(deviceIndex); |
| 336 | mDevices.removeItemsAt(deviceIndex, 1); |
| 337 | removed = true; |
| 338 | } |
| 339 | } // release device registry writer lock |
| 340 | |
| 341 | if (! removed) { |
| 342 | LOGW("Ignoring spurious device removed event for deviceId %d.", deviceId); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 343 | return; |
| 344 | } |
| 345 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 346 | if (device->isIgnored()) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 347 | LOGI("Device removed: id=%d, name='%s' (ignored non-input device)", |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 348 | device->getId(), device->getName().string()); |
| 349 | } else { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 350 | LOGI("Device removed: id=%d, name='%s', sources=0x%08x", |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 351 | device->getId(), device->getName().string(), device->getSources()); |
| 352 | } |
| 353 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 354 | device->reset(); |
| 355 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 356 | delete device; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 359 | InputDevice* InputReader::createDevice(int32_t deviceId, const String8& name, uint32_t classes) { |
| 360 | InputDevice* device = new InputDevice(this, deviceId, name); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 361 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 362 | // External devices. |
| 363 | if (classes & INPUT_DEVICE_CLASS_EXTERNAL) { |
| 364 | device->setExternal(true); |
| 365 | } |
| 366 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 367 | // Switch-like devices. |
| 368 | if (classes & INPUT_DEVICE_CLASS_SWITCH) { |
| 369 | device->addMapper(new SwitchInputMapper(device)); |
| 370 | } |
| 371 | |
| 372 | // Keyboard-like devices. |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 373 | uint32_t keyboardSource = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 374 | int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC; |
| 375 | if (classes & INPUT_DEVICE_CLASS_KEYBOARD) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 376 | keyboardSource |= AINPUT_SOURCE_KEYBOARD; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 377 | } |
| 378 | if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) { |
| 379 | keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC; |
| 380 | } |
| 381 | if (classes & INPUT_DEVICE_CLASS_DPAD) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 382 | keyboardSource |= AINPUT_SOURCE_DPAD; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 383 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 384 | if (classes & INPUT_DEVICE_CLASS_GAMEPAD) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 385 | keyboardSource |= AINPUT_SOURCE_GAMEPAD; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 386 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 387 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 388 | if (keyboardSource != 0) { |
| 389 | device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType)); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 392 | // Cursor-like devices. |
| 393 | if (classes & INPUT_DEVICE_CLASS_CURSOR) { |
| 394 | device->addMapper(new CursorInputMapper(device)); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 397 | // Touchscreens and touchpad devices. |
| 398 | if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 399 | device->addMapper(new MultiTouchInputMapper(device)); |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 400 | } else if (classes & INPUT_DEVICE_CLASS_TOUCH) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 401 | device->addMapper(new SingleTouchInputMapper(device)); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 404 | // Joystick-like devices. |
| 405 | if (classes & INPUT_DEVICE_CLASS_JOYSTICK) { |
| 406 | device->addMapper(new JoystickInputMapper(device)); |
| 407 | } |
| 408 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 409 | return device; |
| 410 | } |
| 411 | |
Jeff Brown | dbf8d27 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 412 | void InputReader::processEventsForDevice(int32_t deviceId, |
| 413 | const RawEvent* rawEvents, size_t count) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 414 | { // acquire device registry reader lock |
| 415 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 416 | |
| 417 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 418 | if (deviceIndex < 0) { |
| 419 | LOGW("Discarding event for unknown deviceId %d.", deviceId); |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 424 | if (device->isIgnored()) { |
| 425 | //LOGD("Discarding event for ignored deviceId %d.", deviceId); |
| 426 | return; |
| 427 | } |
| 428 | |
Jeff Brown | dbf8d27 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 429 | device->process(rawEvents, count); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 430 | } // release device registry reader lock |
| 431 | } |
| 432 | |
Jeff Brown | 68d6075 | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 433 | void InputReader::timeoutExpired(nsecs_t when) { |
| 434 | { // acquire device registry reader lock |
| 435 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 436 | |
| 437 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 438 | InputDevice* device = mDevices.valueAt(i); |
| 439 | if (!device->isIgnored()) { |
| 440 | device->timeoutExpired(when); |
| 441 | } |
| 442 | } |
| 443 | } // release device registry reader lock |
| 444 | } |
| 445 | |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 446 | void InputReader::handleConfigurationChanged(nsecs_t when) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 447 | // Reset global meta state because it depends on the list of all configured devices. |
| 448 | updateGlobalMetaState(); |
| 449 | |
| 450 | // Update input configuration. |
| 451 | updateInputConfiguration(); |
| 452 | |
| 453 | // Enqueue configuration changed. |
| 454 | mDispatcher->notifyConfigurationChanged(when); |
| 455 | } |
| 456 | |
| 457 | void InputReader::configureExcludedDevices() { |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 458 | for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) { |
| 459 | mEventHub->addExcludedDevice(mConfig.excludedDeviceNames[i]); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
| 463 | void InputReader::updateGlobalMetaState() { |
| 464 | { // acquire state lock |
| 465 | AutoMutex _l(mStateLock); |
| 466 | |
| 467 | mGlobalMetaState = 0; |
| 468 | |
| 469 | { // acquire device registry reader lock |
| 470 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 471 | |
| 472 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 473 | InputDevice* device = mDevices.valueAt(i); |
| 474 | mGlobalMetaState |= device->getMetaState(); |
| 475 | } |
| 476 | } // release device registry reader lock |
| 477 | } // release state lock |
| 478 | } |
| 479 | |
| 480 | int32_t InputReader::getGlobalMetaState() { |
| 481 | { // acquire state lock |
| 482 | AutoMutex _l(mStateLock); |
| 483 | |
| 484 | return mGlobalMetaState; |
| 485 | } // release state lock |
| 486 | } |
| 487 | |
| 488 | void InputReader::updateInputConfiguration() { |
| 489 | { // acquire state lock |
| 490 | AutoMutex _l(mStateLock); |
| 491 | |
| 492 | int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH; |
| 493 | int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS; |
| 494 | int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV; |
| 495 | { // acquire device registry reader lock |
| 496 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 497 | |
| 498 | InputDeviceInfo deviceInfo; |
| 499 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 500 | InputDevice* device = mDevices.valueAt(i); |
| 501 | device->getDeviceInfo(& deviceInfo); |
| 502 | uint32_t sources = deviceInfo.getSources(); |
| 503 | |
| 504 | if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) { |
| 505 | touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER; |
| 506 | } |
| 507 | if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) { |
| 508 | navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL; |
| 509 | } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) { |
| 510 | navigationConfig = InputConfiguration::NAVIGATION_DPAD; |
| 511 | } |
| 512 | if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) { |
| 513 | keyboardConfig = InputConfiguration::KEYBOARD_QWERTY; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 514 | } |
| 515 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 516 | } // release device registry reader lock |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 517 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 518 | mInputConfiguration.touchScreen = touchScreenConfig; |
| 519 | mInputConfiguration.keyboard = keyboardConfig; |
| 520 | mInputConfiguration.navigation = navigationConfig; |
| 521 | } // release state lock |
| 522 | } |
| 523 | |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 524 | void InputReader::disableVirtualKeysUntil(nsecs_t time) { |
| 525 | mDisableVirtualKeysTimeout = time; |
| 526 | } |
| 527 | |
| 528 | bool InputReader::shouldDropVirtualKey(nsecs_t now, |
| 529 | InputDevice* device, int32_t keyCode, int32_t scanCode) { |
| 530 | if (now < mDisableVirtualKeysTimeout) { |
| 531 | LOGI("Dropping virtual key from device %s because virtual keys are " |
| 532 | "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d", |
| 533 | device->getName().string(), |
| 534 | (mDisableVirtualKeysTimeout - now) * 0.000001, |
| 535 | keyCode, scanCode); |
| 536 | return true; |
| 537 | } else { |
| 538 | return false; |
| 539 | } |
| 540 | } |
| 541 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 542 | void InputReader::fadePointer() { |
| 543 | { // acquire device registry reader lock |
| 544 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 545 | |
| 546 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 547 | InputDevice* device = mDevices.valueAt(i); |
| 548 | device->fadePointer(); |
| 549 | } |
| 550 | } // release device registry reader lock |
| 551 | } |
| 552 | |
Jeff Brown | 68d6075 | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 553 | void InputReader::requestTimeoutAtTime(nsecs_t when) { |
| 554 | if (when < mNextTimeout) { |
| 555 | mNextTimeout = when; |
| 556 | } |
| 557 | } |
| 558 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 559 | void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) { |
| 560 | { // acquire state lock |
| 561 | AutoMutex _l(mStateLock); |
| 562 | |
| 563 | *outConfiguration = mInputConfiguration; |
| 564 | } // release state lock |
| 565 | } |
| 566 | |
| 567 | status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) { |
| 568 | { // acquire device registry reader lock |
| 569 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 570 | |
| 571 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 572 | if (deviceIndex < 0) { |
| 573 | return NAME_NOT_FOUND; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 574 | } |
| 575 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 576 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 577 | if (device->isIgnored()) { |
| 578 | return NAME_NOT_FOUND; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 579 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 580 | |
| 581 | device->getDeviceInfo(outDeviceInfo); |
| 582 | return OK; |
| 583 | } // release device registy reader lock |
| 584 | } |
| 585 | |
| 586 | void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) { |
| 587 | outDeviceIds.clear(); |
| 588 | |
| 589 | { // acquire device registry reader lock |
| 590 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 591 | |
| 592 | size_t numDevices = mDevices.size(); |
| 593 | for (size_t i = 0; i < numDevices; i++) { |
| 594 | InputDevice* device = mDevices.valueAt(i); |
| 595 | if (! device->isIgnored()) { |
| 596 | outDeviceIds.add(device->getId()); |
| 597 | } |
| 598 | } |
| 599 | } // release device registy reader lock |
| 600 | } |
| 601 | |
| 602 | int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask, |
| 603 | int32_t keyCode) { |
| 604 | return getState(deviceId, sourceMask, keyCode, & InputDevice::getKeyCodeState); |
| 605 | } |
| 606 | |
| 607 | int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask, |
| 608 | int32_t scanCode) { |
| 609 | return getState(deviceId, sourceMask, scanCode, & InputDevice::getScanCodeState); |
| 610 | } |
| 611 | |
| 612 | int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) { |
| 613 | return getState(deviceId, sourceMask, switchCode, & InputDevice::getSwitchState); |
| 614 | } |
| 615 | |
| 616 | int32_t InputReader::getState(int32_t deviceId, uint32_t sourceMask, int32_t code, |
| 617 | GetStateFunc getStateFunc) { |
| 618 | { // acquire device registry reader lock |
| 619 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 620 | |
| 621 | int32_t result = AKEY_STATE_UNKNOWN; |
| 622 | if (deviceId >= 0) { |
| 623 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 624 | if (deviceIndex >= 0) { |
| 625 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 626 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 627 | result = (device->*getStateFunc)(sourceMask, code); |
| 628 | } |
| 629 | } |
| 630 | } else { |
| 631 | size_t numDevices = mDevices.size(); |
| 632 | for (size_t i = 0; i < numDevices; i++) { |
| 633 | InputDevice* device = mDevices.valueAt(i); |
| 634 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 635 | result = (device->*getStateFunc)(sourceMask, code); |
| 636 | if (result >= AKEY_STATE_DOWN) { |
| 637 | return result; |
| 638 | } |
| 639 | } |
| 640 | } |
| 641 | } |
| 642 | return result; |
| 643 | } // release device registy reader lock |
| 644 | } |
| 645 | |
| 646 | bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask, |
| 647 | size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) { |
| 648 | memset(outFlags, 0, numCodes); |
| 649 | return markSupportedKeyCodes(deviceId, sourceMask, numCodes, keyCodes, outFlags); |
| 650 | } |
| 651 | |
| 652 | bool InputReader::markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes, |
| 653 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 654 | { // acquire device registry reader lock |
| 655 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 656 | bool result = false; |
| 657 | if (deviceId >= 0) { |
| 658 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 659 | if (deviceIndex >= 0) { |
| 660 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 661 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 662 | result = device->markSupportedKeyCodes(sourceMask, |
| 663 | numCodes, keyCodes, outFlags); |
| 664 | } |
| 665 | } |
| 666 | } else { |
| 667 | size_t numDevices = mDevices.size(); |
| 668 | for (size_t i = 0; i < numDevices; i++) { |
| 669 | InputDevice* device = mDevices.valueAt(i); |
| 670 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 671 | result |= device->markSupportedKeyCodes(sourceMask, |
| 672 | numCodes, keyCodes, outFlags); |
| 673 | } |
| 674 | } |
| 675 | } |
| 676 | return result; |
| 677 | } // release device registy reader lock |
| 678 | } |
| 679 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 680 | void InputReader::dump(String8& dump) { |
Jeff Brown | f2f48718 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 681 | mEventHub->dump(dump); |
| 682 | dump.append("\n"); |
| 683 | |
| 684 | dump.append("Input Reader State:\n"); |
| 685 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 686 | { // acquire device registry reader lock |
| 687 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 688 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 689 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 690 | mDevices.valueAt(i)->dump(dump); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 691 | } |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 692 | } // release device registy reader lock |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 693 | |
| 694 | dump.append(INDENT "Configuration:\n"); |
| 695 | dump.append(INDENT2 "ExcludedDeviceNames: ["); |
| 696 | for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) { |
| 697 | if (i != 0) { |
| 698 | dump.append(", "); |
| 699 | } |
| 700 | dump.append(mConfig.excludedDeviceNames.itemAt(i).string()); |
| 701 | } |
| 702 | dump.append("]\n"); |
| 703 | dump.appendFormat(INDENT2 "FilterTouchEvents: %s\n", |
| 704 | toString(mConfig.filterTouchEvents)); |
| 705 | dump.appendFormat(INDENT2 "FilterJumpyTouchEvents: %s\n", |
| 706 | toString(mConfig.filterJumpyTouchEvents)); |
| 707 | dump.appendFormat(INDENT2 "VirtualKeyQuietTime: %0.1fms\n", |
| 708 | mConfig.virtualKeyQuietTime * 0.000001f); |
| 709 | |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame^] | 710 | dump.appendFormat(INDENT2 "PointerVelocityControlParameters: " |
| 711 | "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n", |
| 712 | mConfig.pointerVelocityControlParameters.scale, |
| 713 | mConfig.pointerVelocityControlParameters.lowThreshold, |
| 714 | mConfig.pointerVelocityControlParameters.highThreshold, |
| 715 | mConfig.pointerVelocityControlParameters.acceleration); |
| 716 | |
| 717 | dump.appendFormat(INDENT2 "WheelVelocityControlParameters: " |
| 718 | "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n", |
| 719 | mConfig.wheelVelocityControlParameters.scale, |
| 720 | mConfig.wheelVelocityControlParameters.lowThreshold, |
| 721 | mConfig.wheelVelocityControlParameters.highThreshold, |
| 722 | mConfig.wheelVelocityControlParameters.acceleration); |
| 723 | |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 724 | dump.appendFormat(INDENT2 "PointerGesture:\n"); |
| 725 | dump.appendFormat(INDENT3 "QuietInterval: %0.1fms\n", |
| 726 | mConfig.pointerGestureQuietInterval * 0.000001f); |
| 727 | dump.appendFormat(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n", |
| 728 | mConfig.pointerGestureDragMinSwitchSpeed); |
| 729 | dump.appendFormat(INDENT3 "TapInterval: %0.1fms\n", |
| 730 | mConfig.pointerGestureTapInterval * 0.000001f); |
| 731 | dump.appendFormat(INDENT3 "TapDragInterval: %0.1fms\n", |
| 732 | mConfig.pointerGestureTapDragInterval * 0.000001f); |
| 733 | dump.appendFormat(INDENT3 "TapSlop: %0.1fpx\n", |
| 734 | mConfig.pointerGestureTapSlop); |
| 735 | dump.appendFormat(INDENT3 "MultitouchSettleInterval: %0.1fms\n", |
| 736 | mConfig.pointerGestureMultitouchSettleInterval * 0.000001f); |
| 737 | dump.appendFormat(INDENT3 "MultitouchMinSpeed: %0.1fpx/s\n", |
| 738 | mConfig.pointerGestureMultitouchMinSpeed); |
| 739 | dump.appendFormat(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n", |
| 740 | mConfig.pointerGestureSwipeTransitionAngleCosine); |
| 741 | dump.appendFormat(INDENT3 "SwipeMaxWidthRatio: %0.1f\n", |
| 742 | mConfig.pointerGestureSwipeMaxWidthRatio); |
| 743 | dump.appendFormat(INDENT3 "MovementSpeedRatio: %0.1f\n", |
| 744 | mConfig.pointerGestureMovementSpeedRatio); |
| 745 | dump.appendFormat(INDENT3 "ZoomSpeedRatio: %0.1f\n", |
| 746 | mConfig.pointerGestureZoomSpeedRatio); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 747 | } |
| 748 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 749 | |
| 750 | // --- InputReaderThread --- |
| 751 | |
| 752 | InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) : |
| 753 | Thread(/*canCallJava*/ true), mReader(reader) { |
| 754 | } |
| 755 | |
| 756 | InputReaderThread::~InputReaderThread() { |
| 757 | } |
| 758 | |
| 759 | bool InputReaderThread::threadLoop() { |
| 760 | mReader->loopOnce(); |
| 761 | return true; |
| 762 | } |
| 763 | |
| 764 | |
| 765 | // --- InputDevice --- |
| 766 | |
| 767 | InputDevice::InputDevice(InputReaderContext* context, int32_t id, const String8& name) : |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 768 | mContext(context), mId(id), mName(name), mSources(0), mIsExternal(false) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | InputDevice::~InputDevice() { |
| 772 | size_t numMappers = mMappers.size(); |
| 773 | for (size_t i = 0; i < numMappers; i++) { |
| 774 | delete mMappers[i]; |
| 775 | } |
| 776 | mMappers.clear(); |
| 777 | } |
| 778 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 779 | void InputDevice::dump(String8& dump) { |
| 780 | InputDeviceInfo deviceInfo; |
| 781 | getDeviceInfo(& deviceInfo); |
| 782 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 783 | dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(), |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 784 | deviceInfo.getName().string()); |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 785 | dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal)); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 786 | dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources()); |
| 787 | dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType()); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 788 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 789 | const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges(); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 790 | if (!ranges.isEmpty()) { |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 791 | dump.append(INDENT2 "Motion Ranges:\n"); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 792 | for (size_t i = 0; i < ranges.size(); i++) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 793 | const InputDeviceInfo::MotionRange& range = ranges.itemAt(i); |
| 794 | const char* label = getAxisLabel(range.axis); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 795 | char name[32]; |
| 796 | if (label) { |
| 797 | strncpy(name, label, sizeof(name)); |
| 798 | name[sizeof(name) - 1] = '\0'; |
| 799 | } else { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 800 | snprintf(name, sizeof(name), "%d", range.axis); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 801 | } |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 802 | dump.appendFormat(INDENT3 "%s: source=0x%08x, " |
| 803 | "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n", |
| 804 | name, range.source, range.min, range.max, range.flat, range.fuzz); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 805 | } |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | size_t numMappers = mMappers.size(); |
| 809 | for (size_t i = 0; i < numMappers; i++) { |
| 810 | InputMapper* mapper = mMappers[i]; |
| 811 | mapper->dump(dump); |
| 812 | } |
| 813 | } |
| 814 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 815 | void InputDevice::addMapper(InputMapper* mapper) { |
| 816 | mMappers.add(mapper); |
| 817 | } |
| 818 | |
| 819 | void InputDevice::configure() { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 820 | if (! isIgnored()) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 821 | mContext->getEventHub()->getConfiguration(mId, &mConfiguration); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 822 | } |
| 823 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 824 | mSources = 0; |
| 825 | |
| 826 | size_t numMappers = mMappers.size(); |
| 827 | for (size_t i = 0; i < numMappers; i++) { |
| 828 | InputMapper* mapper = mMappers[i]; |
| 829 | mapper->configure(); |
| 830 | mSources |= mapper->getSources(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 831 | } |
| 832 | } |
| 833 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 834 | void InputDevice::reset() { |
| 835 | size_t numMappers = mMappers.size(); |
| 836 | for (size_t i = 0; i < numMappers; i++) { |
| 837 | InputMapper* mapper = mMappers[i]; |
| 838 | mapper->reset(); |
| 839 | } |
| 840 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 841 | |
Jeff Brown | dbf8d27 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 842 | void InputDevice::process(const RawEvent* rawEvents, size_t count) { |
| 843 | // Process all of the events in order for each mapper. |
| 844 | // We cannot simply ask each mapper to process them in bulk because mappers may |
| 845 | // have side-effects that must be interleaved. For example, joystick movement events and |
| 846 | // gamepad button presses are handled by different mappers but they should be dispatched |
| 847 | // in the order received. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 848 | size_t numMappers = mMappers.size(); |
Jeff Brown | dbf8d27 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 849 | for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) { |
| 850 | #if DEBUG_RAW_EVENTS |
| 851 | LOGD("Input event: device=%d type=0x%04x scancode=0x%04x " |
| 852 | "keycode=0x%04x value=0x%04x flags=0x%08x", |
| 853 | rawEvent->deviceId, rawEvent->type, rawEvent->scanCode, rawEvent->keyCode, |
| 854 | rawEvent->value, rawEvent->flags); |
| 855 | #endif |
| 856 | |
| 857 | for (size_t i = 0; i < numMappers; i++) { |
| 858 | InputMapper* mapper = mMappers[i]; |
| 859 | mapper->process(rawEvent); |
| 860 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 861 | } |
| 862 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 863 | |
Jeff Brown | 68d6075 | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 864 | void InputDevice::timeoutExpired(nsecs_t when) { |
| 865 | size_t numMappers = mMappers.size(); |
| 866 | for (size_t i = 0; i < numMappers; i++) { |
| 867 | InputMapper* mapper = mMappers[i]; |
| 868 | mapper->timeoutExpired(when); |
| 869 | } |
| 870 | } |
| 871 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 872 | void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) { |
| 873 | outDeviceInfo->initialize(mId, mName); |
| 874 | |
| 875 | size_t numMappers = mMappers.size(); |
| 876 | for (size_t i = 0; i < numMappers; i++) { |
| 877 | InputMapper* mapper = mMappers[i]; |
| 878 | mapper->populateDeviceInfo(outDeviceInfo); |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 883 | return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState); |
| 884 | } |
| 885 | |
| 886 | int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 887 | return getState(sourceMask, scanCode, & InputMapper::getScanCodeState); |
| 888 | } |
| 889 | |
| 890 | int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 891 | return getState(sourceMask, switchCode, & InputMapper::getSwitchState); |
| 892 | } |
| 893 | |
| 894 | int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) { |
| 895 | int32_t result = AKEY_STATE_UNKNOWN; |
| 896 | size_t numMappers = mMappers.size(); |
| 897 | for (size_t i = 0; i < numMappers; i++) { |
| 898 | InputMapper* mapper = mMappers[i]; |
| 899 | if (sourcesMatchMask(mapper->getSources(), sourceMask)) { |
| 900 | result = (mapper->*getStateFunc)(sourceMask, code); |
| 901 | if (result >= AKEY_STATE_DOWN) { |
| 902 | return result; |
| 903 | } |
| 904 | } |
| 905 | } |
| 906 | return result; |
| 907 | } |
| 908 | |
| 909 | bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 910 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 911 | bool result = false; |
| 912 | size_t numMappers = mMappers.size(); |
| 913 | for (size_t i = 0; i < numMappers; i++) { |
| 914 | InputMapper* mapper = mMappers[i]; |
| 915 | if (sourcesMatchMask(mapper->getSources(), sourceMask)) { |
| 916 | result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags); |
| 917 | } |
| 918 | } |
| 919 | return result; |
| 920 | } |
| 921 | |
| 922 | int32_t InputDevice::getMetaState() { |
| 923 | int32_t result = 0; |
| 924 | size_t numMappers = mMappers.size(); |
| 925 | for (size_t i = 0; i < numMappers; i++) { |
| 926 | InputMapper* mapper = mMappers[i]; |
| 927 | result |= mapper->getMetaState(); |
| 928 | } |
| 929 | return result; |
| 930 | } |
| 931 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 932 | void InputDevice::fadePointer() { |
| 933 | size_t numMappers = mMappers.size(); |
| 934 | for (size_t i = 0; i < numMappers; i++) { |
| 935 | InputMapper* mapper = mMappers[i]; |
| 936 | mapper->fadePointer(); |
| 937 | } |
| 938 | } |
| 939 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 940 | |
| 941 | // --- InputMapper --- |
| 942 | |
| 943 | InputMapper::InputMapper(InputDevice* device) : |
| 944 | mDevice(device), mContext(device->getContext()) { |
| 945 | } |
| 946 | |
| 947 | InputMapper::~InputMapper() { |
| 948 | } |
| 949 | |
| 950 | void InputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 951 | info->addSource(getSources()); |
| 952 | } |
| 953 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 954 | void InputMapper::dump(String8& dump) { |
| 955 | } |
| 956 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 957 | void InputMapper::configure() { |
| 958 | } |
| 959 | |
| 960 | void InputMapper::reset() { |
| 961 | } |
| 962 | |
Jeff Brown | 68d6075 | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 963 | void InputMapper::timeoutExpired(nsecs_t when) { |
| 964 | } |
| 965 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 966 | int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 967 | return AKEY_STATE_UNKNOWN; |
| 968 | } |
| 969 | |
| 970 | int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 971 | return AKEY_STATE_UNKNOWN; |
| 972 | } |
| 973 | |
| 974 | int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 975 | return AKEY_STATE_UNKNOWN; |
| 976 | } |
| 977 | |
| 978 | bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 979 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 980 | return false; |
| 981 | } |
| 982 | |
| 983 | int32_t InputMapper::getMetaState() { |
| 984 | return 0; |
| 985 | } |
| 986 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 987 | void InputMapper::fadePointer() { |
| 988 | } |
| 989 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 990 | void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump, |
| 991 | const RawAbsoluteAxisInfo& axis, const char* name) { |
| 992 | if (axis.valid) { |
| 993 | dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d\n", |
| 994 | name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz); |
| 995 | } else { |
| 996 | dump.appendFormat(INDENT4 "%s: unknown range\n", name); |
| 997 | } |
| 998 | } |
| 999 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1000 | |
| 1001 | // --- SwitchInputMapper --- |
| 1002 | |
| 1003 | SwitchInputMapper::SwitchInputMapper(InputDevice* device) : |
| 1004 | InputMapper(device) { |
| 1005 | } |
| 1006 | |
| 1007 | SwitchInputMapper::~SwitchInputMapper() { |
| 1008 | } |
| 1009 | |
| 1010 | uint32_t SwitchInputMapper::getSources() { |
Jeff Brown | 89de57a | 2011-01-19 18:41:38 -0800 | [diff] [blame] | 1011 | return AINPUT_SOURCE_SWITCH; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | void SwitchInputMapper::process(const RawEvent* rawEvent) { |
| 1015 | switch (rawEvent->type) { |
| 1016 | case EV_SW: |
| 1017 | processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value); |
| 1018 | break; |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) { |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 1023 | getDispatcher()->notifySwitch(when, switchCode, switchValue, 0); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 1027 | return getEventHub()->getSwitchState(getDeviceId(), switchCode); |
| 1028 | } |
| 1029 | |
| 1030 | |
| 1031 | // --- KeyboardInputMapper --- |
| 1032 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1033 | KeyboardInputMapper::KeyboardInputMapper(InputDevice* device, |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1034 | uint32_t source, int32_t keyboardType) : |
| 1035 | InputMapper(device), mSource(source), |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1036 | mKeyboardType(keyboardType) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1037 | initializeLocked(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | KeyboardInputMapper::~KeyboardInputMapper() { |
| 1041 | } |
| 1042 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1043 | void KeyboardInputMapper::initializeLocked() { |
| 1044 | mLocked.metaState = AMETA_NONE; |
| 1045 | mLocked.downTime = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | uint32_t KeyboardInputMapper::getSources() { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1049 | return mSource; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 1053 | InputMapper::populateDeviceInfo(info); |
| 1054 | |
| 1055 | info->setKeyboardType(mKeyboardType); |
| 1056 | } |
| 1057 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1058 | void KeyboardInputMapper::dump(String8& dump) { |
| 1059 | { // acquire lock |
| 1060 | AutoMutex _l(mLock); |
| 1061 | dump.append(INDENT2 "Keyboard Input Mapper:\n"); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1062 | dumpParameters(dump); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1063 | dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType); |
| 1064 | dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mLocked.keyDowns.size()); |
| 1065 | dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mLocked.metaState); |
| 1066 | dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime); |
| 1067 | } // release lock |
| 1068 | } |
| 1069 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1070 | |
| 1071 | void KeyboardInputMapper::configure() { |
| 1072 | InputMapper::configure(); |
| 1073 | |
| 1074 | // Configure basic parameters. |
| 1075 | configureParameters(); |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1076 | |
| 1077 | // Reset LEDs. |
| 1078 | { |
| 1079 | AutoMutex _l(mLock); |
| 1080 | resetLedStateLocked(); |
| 1081 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | void KeyboardInputMapper::configureParameters() { |
| 1085 | mParameters.orientationAware = false; |
| 1086 | getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"), |
| 1087 | mParameters.orientationAware); |
| 1088 | |
| 1089 | mParameters.associatedDisplayId = mParameters.orientationAware ? 0 : -1; |
| 1090 | } |
| 1091 | |
| 1092 | void KeyboardInputMapper::dumpParameters(String8& dump) { |
| 1093 | dump.append(INDENT3 "Parameters:\n"); |
| 1094 | dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n", |
| 1095 | mParameters.associatedDisplayId); |
| 1096 | dump.appendFormat(INDENT4 "OrientationAware: %s\n", |
| 1097 | toString(mParameters.orientationAware)); |
| 1098 | } |
| 1099 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1100 | void KeyboardInputMapper::reset() { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1101 | for (;;) { |
| 1102 | int32_t keyCode, scanCode; |
| 1103 | { // acquire lock |
| 1104 | AutoMutex _l(mLock); |
| 1105 | |
| 1106 | // Synthesize key up event on reset if keys are currently down. |
| 1107 | if (mLocked.keyDowns.isEmpty()) { |
| 1108 | initializeLocked(); |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1109 | resetLedStateLocked(); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1110 | break; // done |
| 1111 | } |
| 1112 | |
| 1113 | const KeyDown& keyDown = mLocked.keyDowns.top(); |
| 1114 | keyCode = keyDown.keyCode; |
| 1115 | scanCode = keyDown.scanCode; |
| 1116 | } // release lock |
| 1117 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1118 | nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1119 | processKey(when, false, keyCode, scanCode, 0); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | InputMapper::reset(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1123 | getContext()->updateGlobalMetaState(); |
| 1124 | } |
| 1125 | |
| 1126 | void KeyboardInputMapper::process(const RawEvent* rawEvent) { |
| 1127 | switch (rawEvent->type) { |
| 1128 | case EV_KEY: { |
| 1129 | int32_t scanCode = rawEvent->scanCode; |
| 1130 | if (isKeyboardOrGamepadKey(scanCode)) { |
| 1131 | processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode, |
| 1132 | rawEvent->flags); |
| 1133 | } |
| 1134 | break; |
| 1135 | } |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) { |
| 1140 | return scanCode < BTN_MOUSE |
| 1141 | || scanCode >= KEY_OK |
Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 1142 | || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE) |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1143 | || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1144 | } |
| 1145 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1146 | void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode, |
| 1147 | int32_t scanCode, uint32_t policyFlags) { |
| 1148 | int32_t newMetaState; |
| 1149 | nsecs_t downTime; |
| 1150 | bool metaStateChanged = false; |
| 1151 | |
| 1152 | { // acquire lock |
| 1153 | AutoMutex _l(mLock); |
| 1154 | |
| 1155 | if (down) { |
| 1156 | // Rotate key codes according to orientation if needed. |
| 1157 | // Note: getDisplayInfo is non-reentrant so we can continue holding the lock. |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1158 | if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1159 | int32_t orientation; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1160 | if (!getPolicy()->getDisplayInfo(mParameters.associatedDisplayId, |
| 1161 | NULL, NULL, & orientation)) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1162 | orientation = DISPLAY_ORIENTATION_0; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | keyCode = rotateKeyCode(keyCode, orientation); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1166 | } |
| 1167 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1168 | // Add key down. |
| 1169 | ssize_t keyDownIndex = findKeyDownLocked(scanCode); |
| 1170 | if (keyDownIndex >= 0) { |
| 1171 | // key repeat, be sure to use same keycode as before in case of rotation |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 1172 | keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1173 | } else { |
| 1174 | // key down |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 1175 | if ((policyFlags & POLICY_FLAG_VIRTUAL) |
| 1176 | && mContext->shouldDropVirtualKey(when, |
| 1177 | getDevice(), keyCode, scanCode)) { |
| 1178 | return; |
| 1179 | } |
| 1180 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1181 | mLocked.keyDowns.push(); |
| 1182 | KeyDown& keyDown = mLocked.keyDowns.editTop(); |
| 1183 | keyDown.keyCode = keyCode; |
| 1184 | keyDown.scanCode = scanCode; |
| 1185 | } |
| 1186 | |
| 1187 | mLocked.downTime = when; |
| 1188 | } else { |
| 1189 | // Remove key down. |
| 1190 | ssize_t keyDownIndex = findKeyDownLocked(scanCode); |
| 1191 | if (keyDownIndex >= 0) { |
| 1192 | // key up, be sure to use same keycode as before in case of rotation |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 1193 | keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1194 | mLocked.keyDowns.removeAt(size_t(keyDownIndex)); |
| 1195 | } else { |
| 1196 | // key was not actually down |
| 1197 | LOGI("Dropping key up from device %s because the key was not down. " |
| 1198 | "keyCode=%d, scanCode=%d", |
| 1199 | getDeviceName().string(), keyCode, scanCode); |
| 1200 | return; |
| 1201 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1202 | } |
| 1203 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1204 | int32_t oldMetaState = mLocked.metaState; |
| 1205 | newMetaState = updateMetaState(keyCode, down, oldMetaState); |
| 1206 | if (oldMetaState != newMetaState) { |
| 1207 | mLocked.metaState = newMetaState; |
| 1208 | metaStateChanged = true; |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1209 | updateLedStateLocked(false); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1210 | } |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 1211 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1212 | downTime = mLocked.downTime; |
| 1213 | } // release lock |
| 1214 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 1215 | // Key down on external an keyboard should wake the device. |
| 1216 | // We don't do this for internal keyboards to prevent them from waking up in your pocket. |
| 1217 | // For internal keyboards, the key layout file should specify the policy flags for |
| 1218 | // each wake key individually. |
| 1219 | // TODO: Use the input device configuration to control this behavior more finely. |
| 1220 | if (down && getDevice()->isExternal() |
| 1221 | && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) { |
| 1222 | policyFlags |= POLICY_FLAG_WAKE_DROPPED; |
| 1223 | } |
| 1224 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1225 | if (metaStateChanged) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1226 | getContext()->updateGlobalMetaState(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1227 | } |
| 1228 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1229 | if (down && !isMetaKey(keyCode)) { |
| 1230 | getContext()->fadePointer(); |
| 1231 | } |
| 1232 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1233 | getDispatcher()->notifyKey(when, getDeviceId(), mSource, policyFlags, |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 1234 | down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP, |
| 1235 | AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1236 | } |
| 1237 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1238 | ssize_t KeyboardInputMapper::findKeyDownLocked(int32_t scanCode) { |
| 1239 | size_t n = mLocked.keyDowns.size(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1240 | for (size_t i = 0; i < n; i++) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1241 | if (mLocked.keyDowns[i].scanCode == scanCode) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1242 | return i; |
| 1243 | } |
| 1244 | } |
| 1245 | return -1; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1246 | } |
| 1247 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1248 | int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 1249 | return getEventHub()->getKeyCodeState(getDeviceId(), keyCode); |
| 1250 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1251 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1252 | int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 1253 | return getEventHub()->getScanCodeState(getDeviceId(), scanCode); |
| 1254 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1255 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1256 | bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 1257 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 1258 | return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags); |
| 1259 | } |
| 1260 | |
| 1261 | int32_t KeyboardInputMapper::getMetaState() { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1262 | { // acquire lock |
| 1263 | AutoMutex _l(mLock); |
| 1264 | return mLocked.metaState; |
| 1265 | } // release lock |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1266 | } |
| 1267 | |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1268 | void KeyboardInputMapper::resetLedStateLocked() { |
| 1269 | initializeLedStateLocked(mLocked.capsLockLedState, LED_CAPSL); |
| 1270 | initializeLedStateLocked(mLocked.numLockLedState, LED_NUML); |
| 1271 | initializeLedStateLocked(mLocked.scrollLockLedState, LED_SCROLLL); |
| 1272 | |
| 1273 | updateLedStateLocked(true); |
| 1274 | } |
| 1275 | |
| 1276 | void KeyboardInputMapper::initializeLedStateLocked(LockedState::LedState& ledState, int32_t led) { |
| 1277 | ledState.avail = getEventHub()->hasLed(getDeviceId(), led); |
| 1278 | ledState.on = false; |
| 1279 | } |
| 1280 | |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1281 | void KeyboardInputMapper::updateLedStateLocked(bool reset) { |
| 1282 | updateLedStateForModifierLocked(mLocked.capsLockLedState, LED_CAPSL, |
Jeff Brown | 51e7fe75 | 2010-10-29 22:19:53 -0700 | [diff] [blame] | 1283 | AMETA_CAPS_LOCK_ON, reset); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1284 | updateLedStateForModifierLocked(mLocked.numLockLedState, LED_NUML, |
Jeff Brown | 51e7fe75 | 2010-10-29 22:19:53 -0700 | [diff] [blame] | 1285 | AMETA_NUM_LOCK_ON, reset); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1286 | updateLedStateForModifierLocked(mLocked.scrollLockLedState, LED_SCROLLL, |
Jeff Brown | 51e7fe75 | 2010-10-29 22:19:53 -0700 | [diff] [blame] | 1287 | AMETA_SCROLL_LOCK_ON, reset); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | void KeyboardInputMapper::updateLedStateForModifierLocked(LockedState::LedState& ledState, |
| 1291 | int32_t led, int32_t modifier, bool reset) { |
| 1292 | if (ledState.avail) { |
| 1293 | bool desiredState = (mLocked.metaState & modifier) != 0; |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1294 | if (reset || ledState.on != desiredState) { |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1295 | getEventHub()->setLedState(getDeviceId(), led, desiredState); |
| 1296 | ledState.on = desiredState; |
| 1297 | } |
| 1298 | } |
| 1299 | } |
| 1300 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1301 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1302 | // --- CursorInputMapper --- |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1303 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1304 | CursorInputMapper::CursorInputMapper(InputDevice* device) : |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1305 | InputMapper(device) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1306 | initializeLocked(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1307 | } |
| 1308 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1309 | CursorInputMapper::~CursorInputMapper() { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1310 | } |
| 1311 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1312 | uint32_t CursorInputMapper::getSources() { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1313 | return mSource; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1314 | } |
| 1315 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1316 | void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1317 | InputMapper::populateDeviceInfo(info); |
| 1318 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1319 | if (mParameters.mode == Parameters::MODE_POINTER) { |
| 1320 | float minX, minY, maxX, maxY; |
| 1321 | if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1322 | info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f); |
| 1323 | info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1324 | } |
| 1325 | } else { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1326 | info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale); |
| 1327 | info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1328 | } |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1329 | info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1330 | |
| 1331 | if (mHaveVWheel) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1332 | info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1333 | } |
| 1334 | if (mHaveHWheel) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1335 | info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1336 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1337 | } |
| 1338 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1339 | void CursorInputMapper::dump(String8& dump) { |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1340 | { // acquire lock |
| 1341 | AutoMutex _l(mLock); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1342 | dump.append(INDENT2 "Cursor Input Mapper:\n"); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1343 | dumpParameters(dump); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1344 | dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale); |
| 1345 | dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1346 | dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision); |
| 1347 | dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1348 | dump.appendFormat(INDENT3 "HaveVWheel: %s\n", toString(mHaveVWheel)); |
| 1349 | dump.appendFormat(INDENT3 "HaveHWheel: %s\n", toString(mHaveHWheel)); |
| 1350 | dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale); |
| 1351 | dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1352 | dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mLocked.buttonState); |
| 1353 | dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mLocked.buttonState))); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1354 | dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime); |
| 1355 | } // release lock |
| 1356 | } |
| 1357 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1358 | void CursorInputMapper::configure() { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1359 | InputMapper::configure(); |
| 1360 | |
| 1361 | // Configure basic parameters. |
| 1362 | configureParameters(); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1363 | |
| 1364 | // Configure device mode. |
| 1365 | switch (mParameters.mode) { |
| 1366 | case Parameters::MODE_POINTER: |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1367 | mSource = AINPUT_SOURCE_MOUSE; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1368 | mXPrecision = 1.0f; |
| 1369 | mYPrecision = 1.0f; |
| 1370 | mXScale = 1.0f; |
| 1371 | mYScale = 1.0f; |
| 1372 | mPointerController = getPolicy()->obtainPointerController(getDeviceId()); |
| 1373 | break; |
| 1374 | case Parameters::MODE_NAVIGATION: |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1375 | mSource = AINPUT_SOURCE_TRACKBALL; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1376 | mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD; |
| 1377 | mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD; |
| 1378 | mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD; |
| 1379 | mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD; |
| 1380 | break; |
| 1381 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1382 | |
| 1383 | mVWheelScale = 1.0f; |
| 1384 | mHWheelScale = 1.0f; |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 1385 | |
| 1386 | mHaveVWheel = getEventHub()->hasRelativeAxis(getDeviceId(), REL_WHEEL); |
| 1387 | mHaveHWheel = getEventHub()->hasRelativeAxis(getDeviceId(), REL_HWHEEL); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame^] | 1388 | |
| 1389 | mPointerVelocityControl.setParameters(getConfig()->pointerVelocityControlParameters); |
| 1390 | mWheelXVelocityControl.setParameters(getConfig()->wheelVelocityControlParameters); |
| 1391 | mWheelYVelocityControl.setParameters(getConfig()->wheelVelocityControlParameters); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1392 | } |
| 1393 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1394 | void CursorInputMapper::configureParameters() { |
| 1395 | mParameters.mode = Parameters::MODE_POINTER; |
| 1396 | String8 cursorModeString; |
| 1397 | if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) { |
| 1398 | if (cursorModeString == "navigation") { |
| 1399 | mParameters.mode = Parameters::MODE_NAVIGATION; |
| 1400 | } else if (cursorModeString != "pointer" && cursorModeString != "default") { |
| 1401 | LOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string()); |
| 1402 | } |
| 1403 | } |
| 1404 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1405 | mParameters.orientationAware = false; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1406 | getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"), |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1407 | mParameters.orientationAware); |
| 1408 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1409 | mParameters.associatedDisplayId = mParameters.mode == Parameters::MODE_POINTER |
| 1410 | || mParameters.orientationAware ? 0 : -1; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1411 | } |
| 1412 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1413 | void CursorInputMapper::dumpParameters(String8& dump) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1414 | dump.append(INDENT3 "Parameters:\n"); |
| 1415 | dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n", |
| 1416 | mParameters.associatedDisplayId); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1417 | |
| 1418 | switch (mParameters.mode) { |
| 1419 | case Parameters::MODE_POINTER: |
| 1420 | dump.append(INDENT4 "Mode: pointer\n"); |
| 1421 | break; |
| 1422 | case Parameters::MODE_NAVIGATION: |
| 1423 | dump.append(INDENT4 "Mode: navigation\n"); |
| 1424 | break; |
| 1425 | default: |
| 1426 | assert(false); |
| 1427 | } |
| 1428 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1429 | dump.appendFormat(INDENT4 "OrientationAware: %s\n", |
| 1430 | toString(mParameters.orientationAware)); |
| 1431 | } |
| 1432 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1433 | void CursorInputMapper::initializeLocked() { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1434 | mAccumulator.clear(); |
| 1435 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1436 | mLocked.buttonState = 0; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1437 | mLocked.downTime = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1438 | } |
| 1439 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1440 | void CursorInputMapper::reset() { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1441 | for (;;) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1442 | uint32_t buttonState; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1443 | { // acquire lock |
| 1444 | AutoMutex _l(mLock); |
| 1445 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1446 | buttonState = mLocked.buttonState; |
| 1447 | if (!buttonState) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1448 | initializeLocked(); |
| 1449 | break; // done |
| 1450 | } |
| 1451 | } // release lock |
| 1452 | |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame^] | 1453 | // Reset velocity. |
| 1454 | mPointerVelocityControl.reset(); |
| 1455 | mWheelXVelocityControl.reset(); |
| 1456 | mWheelYVelocityControl.reset(); |
| 1457 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1458 | // Synthesize button up event on reset. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1459 | nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1460 | mAccumulator.clear(); |
| 1461 | mAccumulator.buttonDown = 0; |
| 1462 | mAccumulator.buttonUp = buttonState; |
| 1463 | mAccumulator.fields = Accumulator::FIELD_BUTTONS; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1464 | sync(when); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1465 | } |
| 1466 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1467 | InputMapper::reset(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1468 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1469 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1470 | void CursorInputMapper::process(const RawEvent* rawEvent) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1471 | switch (rawEvent->type) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1472 | case EV_KEY: { |
| 1473 | uint32_t buttonState = getButtonStateForScanCode(rawEvent->scanCode); |
| 1474 | if (buttonState) { |
| 1475 | if (rawEvent->value) { |
| 1476 | mAccumulator.buttonDown = buttonState; |
| 1477 | mAccumulator.buttonUp = 0; |
| 1478 | } else { |
| 1479 | mAccumulator.buttonDown = 0; |
| 1480 | mAccumulator.buttonUp = buttonState; |
| 1481 | } |
| 1482 | mAccumulator.fields |= Accumulator::FIELD_BUTTONS; |
| 1483 | |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 1484 | // Sync now since BTN_MOUSE is not necessarily followed by SYN_REPORT and |
| 1485 | // we need to ensure that we report the up/down promptly. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1486 | sync(rawEvent->when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1487 | break; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1488 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1489 | break; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1490 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1491 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1492 | case EV_REL: |
| 1493 | switch (rawEvent->scanCode) { |
| 1494 | case REL_X: |
| 1495 | mAccumulator.fields |= Accumulator::FIELD_REL_X; |
| 1496 | mAccumulator.relX = rawEvent->value; |
| 1497 | break; |
| 1498 | case REL_Y: |
| 1499 | mAccumulator.fields |= Accumulator::FIELD_REL_Y; |
| 1500 | mAccumulator.relY = rawEvent->value; |
| 1501 | break; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1502 | case REL_WHEEL: |
| 1503 | mAccumulator.fields |= Accumulator::FIELD_REL_WHEEL; |
| 1504 | mAccumulator.relWheel = rawEvent->value; |
| 1505 | break; |
| 1506 | case REL_HWHEEL: |
| 1507 | mAccumulator.fields |= Accumulator::FIELD_REL_HWHEEL; |
| 1508 | mAccumulator.relHWheel = rawEvent->value; |
| 1509 | break; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1510 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1511 | break; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1512 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1513 | case EV_SYN: |
| 1514 | switch (rawEvent->scanCode) { |
| 1515 | case SYN_REPORT: |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 1516 | sync(rawEvent->when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1517 | break; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1518 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1519 | break; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1520 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1521 | } |
| 1522 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1523 | void CursorInputMapper::sync(nsecs_t when) { |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 1524 | uint32_t fields = mAccumulator.fields; |
| 1525 | if (fields == 0) { |
| 1526 | return; // no new state changes, so nothing to do |
| 1527 | } |
| 1528 | |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 1529 | int32_t motionEventAction; |
| 1530 | int32_t motionEventEdgeFlags; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1531 | PointerCoords pointerCoords; |
| 1532 | nsecs_t downTime; |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1533 | float vscroll, hscroll; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1534 | { // acquire lock |
| 1535 | AutoMutex _l(mLock); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1536 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1537 | bool down, downChanged; |
| 1538 | bool wasDown = isPointerDown(mLocked.buttonState); |
| 1539 | bool buttonsChanged = fields & Accumulator::FIELD_BUTTONS; |
| 1540 | if (buttonsChanged) { |
| 1541 | mLocked.buttonState = (mLocked.buttonState | mAccumulator.buttonDown) |
| 1542 | & ~mAccumulator.buttonUp; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1543 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1544 | down = isPointerDown(mLocked.buttonState); |
| 1545 | |
| 1546 | if (!wasDown && down) { |
| 1547 | mLocked.downTime = when; |
| 1548 | downChanged = true; |
| 1549 | } else if (wasDown && !down) { |
| 1550 | downChanged = true; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1551 | } else { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1552 | downChanged = false; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1553 | } |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1554 | } else { |
| 1555 | down = wasDown; |
| 1556 | downChanged = false; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1557 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1558 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1559 | downTime = mLocked.downTime; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1560 | float deltaX = fields & Accumulator::FIELD_REL_X ? mAccumulator.relX * mXScale : 0.0f; |
| 1561 | float deltaY = fields & Accumulator::FIELD_REL_Y ? mAccumulator.relY * mYScale : 0.0f; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1562 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1563 | if (downChanged) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1564 | motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP; |
| 1565 | } else if (down || mPointerController == NULL) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1566 | motionEventAction = AMOTION_EVENT_ACTION_MOVE; |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 1567 | } else { |
| 1568 | motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1569 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1570 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1571 | if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0 |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1572 | && (deltaX != 0.0f || deltaY != 0.0f)) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1573 | // Rotate motion based on display orientation if needed. |
| 1574 | // Note: getDisplayInfo is non-reentrant so we can continue holding the lock. |
| 1575 | int32_t orientation; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1576 | if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId, |
| 1577 | NULL, NULL, & orientation)) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1578 | orientation = DISPLAY_ORIENTATION_0; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1579 | } |
| 1580 | |
| 1581 | float temp; |
| 1582 | switch (orientation) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1583 | case DISPLAY_ORIENTATION_90: |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1584 | temp = deltaX; |
| 1585 | deltaX = deltaY; |
| 1586 | deltaY = -temp; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1587 | break; |
| 1588 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1589 | case DISPLAY_ORIENTATION_180: |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1590 | deltaX = -deltaX; |
| 1591 | deltaY = -deltaY; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1592 | break; |
| 1593 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1594 | case DISPLAY_ORIENTATION_270: |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1595 | temp = deltaX; |
| 1596 | deltaX = -deltaY; |
| 1597 | deltaY = temp; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1598 | break; |
| 1599 | } |
| 1600 | } |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1601 | |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 1602 | pointerCoords.clear(); |
| 1603 | |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 1604 | motionEventEdgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 1605 | |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 1606 | if (mHaveVWheel && (fields & Accumulator::FIELD_REL_WHEEL)) { |
| 1607 | vscroll = mAccumulator.relWheel; |
| 1608 | } else { |
| 1609 | vscroll = 0; |
| 1610 | } |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame^] | 1611 | mWheelYVelocityControl.move(when, NULL, &vscroll); |
| 1612 | |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 1613 | if (mHaveHWheel && (fields & Accumulator::FIELD_REL_HWHEEL)) { |
| 1614 | hscroll = mAccumulator.relHWheel; |
| 1615 | } else { |
| 1616 | hscroll = 0; |
| 1617 | } |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame^] | 1618 | mWheelXVelocityControl.move(when, &hscroll, NULL); |
| 1619 | |
| 1620 | mPointerVelocityControl.move(when, &deltaX, &deltaY); |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 1621 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1622 | if (mPointerController != NULL) { |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 1623 | if (deltaX != 0 || deltaY != 0 || vscroll != 0 || hscroll != 0 |
| 1624 | || buttonsChanged) { |
| 1625 | mPointerController->setPresentation( |
| 1626 | PointerControllerInterface::PRESENTATION_POINTER); |
| 1627 | |
| 1628 | if (deltaX != 0 || deltaY != 0) { |
| 1629 | mPointerController->move(deltaX, deltaY); |
| 1630 | } |
| 1631 | |
| 1632 | if (buttonsChanged) { |
| 1633 | mPointerController->setButtonState(mLocked.buttonState); |
| 1634 | } |
| 1635 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 1636 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1637 | } |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1638 | |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 1639 | float x, y; |
| 1640 | mPointerController->getPosition(&x, &y); |
Jeff Brown | ebbd5d1 | 2011-02-17 13:01:34 -0800 | [diff] [blame] | 1641 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 1642 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 1643 | |
| 1644 | if (motionEventAction == AMOTION_EVENT_ACTION_DOWN) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1645 | motionEventEdgeFlags = calculateEdgeFlagsUsingPointerBounds( |
| 1646 | mPointerController, x, y); |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 1647 | } |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1648 | } else { |
Jeff Brown | ebbd5d1 | 2011-02-17 13:01:34 -0800 | [diff] [blame] | 1649 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX); |
| 1650 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1651 | } |
| 1652 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1653 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1654 | } // release lock |
| 1655 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 1656 | // Moving an external trackball or mouse should wake the device. |
| 1657 | // We don't do this for internal cursor devices to prevent them from waking up |
| 1658 | // the device in your pocket. |
| 1659 | // TODO: Use the input device configuration to control this behavior more finely. |
| 1660 | uint32_t policyFlags = 0; |
| 1661 | if (getDevice()->isExternal()) { |
| 1662 | policyFlags |= POLICY_FLAG_WAKE_DROPPED; |
| 1663 | } |
| 1664 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1665 | int32_t metaState = mContext->getGlobalMetaState(); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1666 | int32_t pointerId = 0; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1667 | getDispatcher()->notifyMotion(when, getDeviceId(), mSource, policyFlags, |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 1668 | motionEventAction, 0, metaState, motionEventEdgeFlags, |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 1669 | 1, &pointerId, &pointerCoords, mXPrecision, mYPrecision, downTime); |
| 1670 | |
| 1671 | mAccumulator.clear(); |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1672 | |
| 1673 | if (vscroll != 0 || hscroll != 0) { |
| 1674 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll); |
| 1675 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll); |
| 1676 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1677 | getDispatcher()->notifyMotion(when, getDeviceId(), mSource, policyFlags, |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1678 | AMOTION_EVENT_ACTION_SCROLL, 0, metaState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 1679 | 1, &pointerId, &pointerCoords, mXPrecision, mYPrecision, downTime); |
| 1680 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1681 | } |
| 1682 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1683 | int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
Jeff Brown | c3fc2d0 | 2010-08-10 15:47:53 -0700 | [diff] [blame] | 1684 | if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) { |
| 1685 | return getEventHub()->getScanCodeState(getDeviceId(), scanCode); |
| 1686 | } else { |
| 1687 | return AKEY_STATE_UNKNOWN; |
| 1688 | } |
| 1689 | } |
| 1690 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1691 | void CursorInputMapper::fadePointer() { |
| 1692 | { // acquire lock |
| 1693 | AutoMutex _l(mLock); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1694 | if (mPointerController != NULL) { |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 1695 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1696 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1697 | } // release lock |
| 1698 | } |
| 1699 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1700 | |
| 1701 | // --- TouchInputMapper --- |
| 1702 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1703 | TouchInputMapper::TouchInputMapper(InputDevice* device) : |
| 1704 | InputMapper(device) { |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 1705 | mConfig = getConfig(); |
| 1706 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1707 | mLocked.surfaceOrientation = -1; |
| 1708 | mLocked.surfaceWidth = -1; |
| 1709 | mLocked.surfaceHeight = -1; |
| 1710 | |
| 1711 | initializeLocked(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1712 | } |
| 1713 | |
| 1714 | TouchInputMapper::~TouchInputMapper() { |
| 1715 | } |
| 1716 | |
| 1717 | uint32_t TouchInputMapper::getSources() { |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 1718 | return mTouchSource | mPointerSource; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1719 | } |
| 1720 | |
| 1721 | void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 1722 | InputMapper::populateDeviceInfo(info); |
| 1723 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1724 | { // acquire lock |
| 1725 | AutoMutex _l(mLock); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1726 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1727 | // Ensure surface information is up to date so that orientation changes are |
| 1728 | // noticed immediately. |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1729 | if (!configureSurfaceLocked()) { |
| 1730 | return; |
| 1731 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1732 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1733 | info->addMotionRange(mLocked.orientedRanges.x); |
| 1734 | info->addMotionRange(mLocked.orientedRanges.y); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1735 | |
| 1736 | if (mLocked.orientedRanges.havePressure) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1737 | info->addMotionRange(mLocked.orientedRanges.pressure); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1738 | } |
| 1739 | |
| 1740 | if (mLocked.orientedRanges.haveSize) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1741 | info->addMotionRange(mLocked.orientedRanges.size); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1742 | } |
| 1743 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1744 | if (mLocked.orientedRanges.haveTouchSize) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1745 | info->addMotionRange(mLocked.orientedRanges.touchMajor); |
| 1746 | info->addMotionRange(mLocked.orientedRanges.touchMinor); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1747 | } |
| 1748 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1749 | if (mLocked.orientedRanges.haveToolSize) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1750 | info->addMotionRange(mLocked.orientedRanges.toolMajor); |
| 1751 | info->addMotionRange(mLocked.orientedRanges.toolMinor); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1752 | } |
| 1753 | |
| 1754 | if (mLocked.orientedRanges.haveOrientation) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1755 | info->addMotionRange(mLocked.orientedRanges.orientation); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1756 | } |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 1757 | |
| 1758 | if (mPointerController != NULL) { |
| 1759 | float minX, minY, maxX, maxY; |
| 1760 | if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) { |
| 1761 | info->addMotionRange(AMOTION_EVENT_AXIS_X, mPointerSource, |
| 1762 | minX, maxX, 0.0f, 0.0f); |
| 1763 | info->addMotionRange(AMOTION_EVENT_AXIS_Y, mPointerSource, |
| 1764 | minY, maxY, 0.0f, 0.0f); |
| 1765 | } |
| 1766 | info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mPointerSource, |
| 1767 | 0.0f, 1.0f, 0.0f, 0.0f); |
| 1768 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1769 | } // release lock |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1770 | } |
| 1771 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1772 | void TouchInputMapper::dump(String8& dump) { |
| 1773 | { // acquire lock |
| 1774 | AutoMutex _l(mLock); |
| 1775 | dump.append(INDENT2 "Touch Input Mapper:\n"); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1776 | dumpParameters(dump); |
| 1777 | dumpVirtualKeysLocked(dump); |
| 1778 | dumpRawAxes(dump); |
| 1779 | dumpCalibration(dump); |
| 1780 | dumpSurfaceLocked(dump); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1781 | |
Jeff Brown | 511ee5f | 2010-10-18 13:32:20 -0700 | [diff] [blame] | 1782 | dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n"); |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1783 | dump.appendFormat(INDENT4 "XScale: %0.3f\n", mLocked.xScale); |
| 1784 | dump.appendFormat(INDENT4 "YScale: %0.3f\n", mLocked.yScale); |
| 1785 | dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mLocked.xPrecision); |
| 1786 | dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mLocked.yPrecision); |
| 1787 | dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mLocked.geometricScale); |
| 1788 | dump.appendFormat(INDENT4 "ToolSizeLinearScale: %0.3f\n", mLocked.toolSizeLinearScale); |
| 1789 | dump.appendFormat(INDENT4 "ToolSizeLinearBias: %0.3f\n", mLocked.toolSizeLinearBias); |
| 1790 | dump.appendFormat(INDENT4 "ToolSizeAreaScale: %0.3f\n", mLocked.toolSizeAreaScale); |
| 1791 | dump.appendFormat(INDENT4 "ToolSizeAreaBias: %0.3f\n", mLocked.toolSizeAreaBias); |
| 1792 | dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mLocked.pressureScale); |
| 1793 | dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mLocked.sizeScale); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1794 | dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mLocked.orientationScale); |
| 1795 | |
| 1796 | dump.appendFormat(INDENT3 "Last Touch:\n"); |
| 1797 | dump.appendFormat(INDENT4 "Pointer Count: %d\n", mLastTouch.pointerCount); |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 1798 | dump.appendFormat(INDENT4 "Button State: 0x%08x\n", mLastTouch.buttonState); |
| 1799 | |
| 1800 | if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) { |
| 1801 | dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n"); |
| 1802 | dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n", |
| 1803 | mLocked.pointerGestureXMovementScale); |
| 1804 | dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n", |
| 1805 | mLocked.pointerGestureYMovementScale); |
| 1806 | dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n", |
| 1807 | mLocked.pointerGestureXZoomScale); |
| 1808 | dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n", |
| 1809 | mLocked.pointerGestureYZoomScale); |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 1810 | dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n", |
| 1811 | mLocked.pointerGestureMaxSwipeWidth); |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 1812 | } |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1813 | } // release lock |
| 1814 | } |
| 1815 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1816 | void TouchInputMapper::initializeLocked() { |
| 1817 | mCurrentTouch.clear(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1818 | mLastTouch.clear(); |
| 1819 | mDownTime = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1820 | |
| 1821 | for (uint32_t i = 0; i < MAX_POINTERS; i++) { |
| 1822 | mAveragingTouchFilter.historyStart[i] = 0; |
| 1823 | mAveragingTouchFilter.historyEnd[i] = 0; |
| 1824 | } |
| 1825 | |
| 1826 | mJumpyTouchFilter.jumpyPointsDropped = 0; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1827 | |
| 1828 | mLocked.currentVirtualKey.down = false; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1829 | |
| 1830 | mLocked.orientedRanges.havePressure = false; |
| 1831 | mLocked.orientedRanges.haveSize = false; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1832 | mLocked.orientedRanges.haveTouchSize = false; |
| 1833 | mLocked.orientedRanges.haveToolSize = false; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1834 | mLocked.orientedRanges.haveOrientation = false; |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 1835 | |
| 1836 | mPointerGesture.reset(); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame^] | 1837 | mPointerGesture.pointerVelocityControl.setParameters(mConfig->pointerVelocityControlParameters); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1838 | } |
| 1839 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1840 | void TouchInputMapper::configure() { |
| 1841 | InputMapper::configure(); |
| 1842 | |
| 1843 | // Configure basic parameters. |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1844 | configureParameters(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1845 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1846 | // Configure sources. |
| 1847 | switch (mParameters.deviceType) { |
| 1848 | case Parameters::DEVICE_TYPE_TOUCH_SCREEN: |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1849 | mTouchSource = AINPUT_SOURCE_TOUCHSCREEN; |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 1850 | mPointerSource = 0; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1851 | break; |
| 1852 | case Parameters::DEVICE_TYPE_TOUCH_PAD: |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1853 | mTouchSource = AINPUT_SOURCE_TOUCHPAD; |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 1854 | mPointerSource = 0; |
| 1855 | break; |
| 1856 | case Parameters::DEVICE_TYPE_POINTER: |
| 1857 | mTouchSource = AINPUT_SOURCE_TOUCHPAD; |
| 1858 | mPointerSource = AINPUT_SOURCE_MOUSE; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1859 | break; |
| 1860 | default: |
| 1861 | assert(false); |
| 1862 | } |
| 1863 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1864 | // Configure absolute axis information. |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1865 | configureRawAxes(); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1866 | |
| 1867 | // Prepare input device calibration. |
| 1868 | parseCalibration(); |
| 1869 | resolveCalibration(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1870 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1871 | { // acquire lock |
| 1872 | AutoMutex _l(mLock); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1873 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1874 | // Configure surface dimensions and orientation. |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1875 | configureSurfaceLocked(); |
| 1876 | } // release lock |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1877 | } |
| 1878 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1879 | void TouchInputMapper::configureParameters() { |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 1880 | mParameters.useBadTouchFilter = mConfig->filterTouchEvents; |
| 1881 | mParameters.useAveragingTouchFilter = mConfig->filterTouchEvents; |
| 1882 | mParameters.useJumpyTouchFilter = mConfig->filterJumpyTouchEvents; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1883 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 1884 | // TODO: select the default gesture mode based on whether the device supports |
| 1885 | // distinct multitouch |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 1886 | mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS; |
| 1887 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 1888 | String8 gestureModeString; |
| 1889 | if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"), |
| 1890 | gestureModeString)) { |
| 1891 | if (gestureModeString == "pointer") { |
| 1892 | mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER; |
| 1893 | } else if (gestureModeString == "spots") { |
| 1894 | mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS; |
| 1895 | } else if (gestureModeString != "default") { |
| 1896 | LOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string()); |
| 1897 | } |
| 1898 | } |
| 1899 | |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 1900 | if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X) |
| 1901 | || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) { |
| 1902 | // The device is a cursor device with a touch pad attached. |
| 1903 | // By default don't use the touch pad to move the pointer. |
| 1904 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD; |
| 1905 | } else { |
| 1906 | // The device is just a touch pad. |
| 1907 | // By default use the touch pad to move the pointer and to perform related gestures. |
| 1908 | mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER; |
| 1909 | } |
| 1910 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1911 | String8 deviceTypeString; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1912 | if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"), |
| 1913 | deviceTypeString)) { |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 1914 | if (deviceTypeString == "touchScreen") { |
| 1915 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1916 | } else if (deviceTypeString == "touchPad") { |
| 1917 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD; |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 1918 | } else if (deviceTypeString == "pointer") { |
| 1919 | mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 1920 | } else if (deviceTypeString != "default") { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1921 | LOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string()); |
| 1922 | } |
| 1923 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1924 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1925 | mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1926 | getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"), |
| 1927 | mParameters.orientationAware); |
| 1928 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1929 | mParameters.associatedDisplayId = mParameters.orientationAware |
| 1930 | || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 1931 | || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1932 | ? 0 : -1; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1933 | } |
| 1934 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1935 | void TouchInputMapper::dumpParameters(String8& dump) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1936 | dump.append(INDENT3 "Parameters:\n"); |
| 1937 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 1938 | switch (mParameters.gestureMode) { |
| 1939 | case Parameters::GESTURE_MODE_POINTER: |
| 1940 | dump.append(INDENT4 "GestureMode: pointer\n"); |
| 1941 | break; |
| 1942 | case Parameters::GESTURE_MODE_SPOTS: |
| 1943 | dump.append(INDENT4 "GestureMode: spots\n"); |
| 1944 | break; |
| 1945 | default: |
| 1946 | assert(false); |
| 1947 | } |
| 1948 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1949 | switch (mParameters.deviceType) { |
| 1950 | case Parameters::DEVICE_TYPE_TOUCH_SCREEN: |
| 1951 | dump.append(INDENT4 "DeviceType: touchScreen\n"); |
| 1952 | break; |
| 1953 | case Parameters::DEVICE_TYPE_TOUCH_PAD: |
| 1954 | dump.append(INDENT4 "DeviceType: touchPad\n"); |
| 1955 | break; |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 1956 | case Parameters::DEVICE_TYPE_POINTER: |
| 1957 | dump.append(INDENT4 "DeviceType: pointer\n"); |
| 1958 | break; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1959 | default: |
| 1960 | assert(false); |
| 1961 | } |
| 1962 | |
| 1963 | dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n", |
| 1964 | mParameters.associatedDisplayId); |
| 1965 | dump.appendFormat(INDENT4 "OrientationAware: %s\n", |
| 1966 | toString(mParameters.orientationAware)); |
| 1967 | |
| 1968 | dump.appendFormat(INDENT4 "UseBadTouchFilter: %s\n", |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1969 | toString(mParameters.useBadTouchFilter)); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1970 | dump.appendFormat(INDENT4 "UseAveragingTouchFilter: %s\n", |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1971 | toString(mParameters.useAveragingTouchFilter)); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1972 | dump.appendFormat(INDENT4 "UseJumpyTouchFilter: %s\n", |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1973 | toString(mParameters.useJumpyTouchFilter)); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1974 | } |
| 1975 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1976 | void TouchInputMapper::configureRawAxes() { |
| 1977 | mRawAxes.x.clear(); |
| 1978 | mRawAxes.y.clear(); |
| 1979 | mRawAxes.pressure.clear(); |
| 1980 | mRawAxes.touchMajor.clear(); |
| 1981 | mRawAxes.touchMinor.clear(); |
| 1982 | mRawAxes.toolMajor.clear(); |
| 1983 | mRawAxes.toolMinor.clear(); |
| 1984 | mRawAxes.orientation.clear(); |
| 1985 | } |
| 1986 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1987 | void TouchInputMapper::dumpRawAxes(String8& dump) { |
| 1988 | dump.append(INDENT3 "Raw Axes:\n"); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1989 | dumpRawAbsoluteAxisInfo(dump, mRawAxes.x, "X"); |
| 1990 | dumpRawAbsoluteAxisInfo(dump, mRawAxes.y, "Y"); |
| 1991 | dumpRawAbsoluteAxisInfo(dump, mRawAxes.pressure, "Pressure"); |
| 1992 | dumpRawAbsoluteAxisInfo(dump, mRawAxes.touchMajor, "TouchMajor"); |
| 1993 | dumpRawAbsoluteAxisInfo(dump, mRawAxes.touchMinor, "TouchMinor"); |
| 1994 | dumpRawAbsoluteAxisInfo(dump, mRawAxes.toolMajor, "ToolMajor"); |
| 1995 | dumpRawAbsoluteAxisInfo(dump, mRawAxes.toolMinor, "ToolMinor"); |
| 1996 | dumpRawAbsoluteAxisInfo(dump, mRawAxes.orientation, "Orientation"); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1997 | } |
| 1998 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1999 | bool TouchInputMapper::configureSurfaceLocked() { |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2000 | // Ensure we have valid X and Y axes. |
| 2001 | if (!mRawAxes.x.valid || !mRawAxes.y.valid) { |
| 2002 | LOGW(INDENT "Touch device '%s' did not report support for X or Y axis! " |
| 2003 | "The device will be inoperable.", getDeviceName().string()); |
| 2004 | return false; |
| 2005 | } |
| 2006 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2007 | // Update orientation and dimensions if needed. |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 2008 | int32_t orientation = DISPLAY_ORIENTATION_0; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2009 | int32_t width = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1; |
| 2010 | int32_t height = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2011 | |
| 2012 | if (mParameters.associatedDisplayId >= 0) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2013 | // Note: getDisplayInfo is non-reentrant so we can continue holding the lock. |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2014 | if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId, |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2015 | &mLocked.associatedDisplayWidth, &mLocked.associatedDisplayHeight, |
| 2016 | &mLocked.associatedDisplayOrientation)) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2017 | return false; |
| 2018 | } |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2019 | |
| 2020 | // A touch screen inherits the dimensions of the display. |
| 2021 | if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) { |
| 2022 | width = mLocked.associatedDisplayWidth; |
| 2023 | height = mLocked.associatedDisplayHeight; |
| 2024 | } |
| 2025 | |
| 2026 | // The device inherits the orientation of the display if it is orientation aware. |
| 2027 | if (mParameters.orientationAware) { |
| 2028 | orientation = mLocked.associatedDisplayOrientation; |
| 2029 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2030 | } |
| 2031 | |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2032 | if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER |
| 2033 | && mPointerController == NULL) { |
| 2034 | mPointerController = getPolicy()->obtainPointerController(getDeviceId()); |
| 2035 | } |
| 2036 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2037 | bool orientationChanged = mLocked.surfaceOrientation != orientation; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2038 | if (orientationChanged) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2039 | mLocked.surfaceOrientation = orientation; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2040 | } |
| 2041 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2042 | bool sizeChanged = mLocked.surfaceWidth != width || mLocked.surfaceHeight != height; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2043 | if (sizeChanged) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2044 | LOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d", |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2045 | getDeviceId(), getDeviceName().string(), width, height); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2046 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2047 | mLocked.surfaceWidth = width; |
| 2048 | mLocked.surfaceHeight = height; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2049 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2050 | // Configure X and Y factors. |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2051 | mLocked.xScale = float(width) / (mRawAxes.x.maxValue - mRawAxes.x.minValue + 1); |
| 2052 | mLocked.yScale = float(height) / (mRawAxes.y.maxValue - mRawAxes.y.minValue + 1); |
| 2053 | mLocked.xPrecision = 1.0f / mLocked.xScale; |
| 2054 | mLocked.yPrecision = 1.0f / mLocked.yScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2055 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2056 | mLocked.orientedRanges.x.axis = AMOTION_EVENT_AXIS_X; |
| 2057 | mLocked.orientedRanges.x.source = mTouchSource; |
| 2058 | mLocked.orientedRanges.y.axis = AMOTION_EVENT_AXIS_Y; |
| 2059 | mLocked.orientedRanges.y.source = mTouchSource; |
| 2060 | |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2061 | configureVirtualKeysLocked(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2062 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2063 | // Scale factor for terms that are not oriented in a particular axis. |
| 2064 | // If the pixels are square then xScale == yScale otherwise we fake it |
| 2065 | // by choosing an average. |
| 2066 | mLocked.geometricScale = avg(mLocked.xScale, mLocked.yScale); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2067 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2068 | // Size of diagonal axis. |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2069 | float diagonalSize = hypotf(width, height); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2070 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2071 | // TouchMajor and TouchMinor factors. |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2072 | if (mCalibration.touchSizeCalibration != Calibration::TOUCH_SIZE_CALIBRATION_NONE) { |
| 2073 | mLocked.orientedRanges.haveTouchSize = true; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2074 | |
| 2075 | mLocked.orientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR; |
| 2076 | mLocked.orientedRanges.touchMajor.source = mTouchSource; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2077 | mLocked.orientedRanges.touchMajor.min = 0; |
| 2078 | mLocked.orientedRanges.touchMajor.max = diagonalSize; |
| 2079 | mLocked.orientedRanges.touchMajor.flat = 0; |
| 2080 | mLocked.orientedRanges.touchMajor.fuzz = 0; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2081 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2082 | mLocked.orientedRanges.touchMinor = mLocked.orientedRanges.touchMajor; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2083 | mLocked.orientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2084 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2085 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2086 | // ToolMajor and ToolMinor factors. |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2087 | mLocked.toolSizeLinearScale = 0; |
| 2088 | mLocked.toolSizeLinearBias = 0; |
| 2089 | mLocked.toolSizeAreaScale = 0; |
| 2090 | mLocked.toolSizeAreaBias = 0; |
| 2091 | if (mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) { |
| 2092 | if (mCalibration.toolSizeCalibration == Calibration::TOOL_SIZE_CALIBRATION_LINEAR) { |
| 2093 | if (mCalibration.haveToolSizeLinearScale) { |
| 2094 | mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2095 | } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) { |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2096 | mLocked.toolSizeLinearScale = float(min(width, height)) |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2097 | / mRawAxes.toolMajor.maxValue; |
| 2098 | } |
| 2099 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2100 | if (mCalibration.haveToolSizeLinearBias) { |
| 2101 | mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias; |
| 2102 | } |
| 2103 | } else if (mCalibration.toolSizeCalibration == |
| 2104 | Calibration::TOOL_SIZE_CALIBRATION_AREA) { |
| 2105 | if (mCalibration.haveToolSizeLinearScale) { |
| 2106 | mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale; |
| 2107 | } else { |
| 2108 | mLocked.toolSizeLinearScale = min(width, height); |
| 2109 | } |
| 2110 | |
| 2111 | if (mCalibration.haveToolSizeLinearBias) { |
| 2112 | mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias; |
| 2113 | } |
| 2114 | |
| 2115 | if (mCalibration.haveToolSizeAreaScale) { |
| 2116 | mLocked.toolSizeAreaScale = mCalibration.toolSizeAreaScale; |
| 2117 | } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) { |
| 2118 | mLocked.toolSizeAreaScale = 1.0f / mRawAxes.toolMajor.maxValue; |
| 2119 | } |
| 2120 | |
| 2121 | if (mCalibration.haveToolSizeAreaBias) { |
| 2122 | mLocked.toolSizeAreaBias = mCalibration.toolSizeAreaBias; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2123 | } |
| 2124 | } |
| 2125 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2126 | mLocked.orientedRanges.haveToolSize = true; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2127 | |
| 2128 | mLocked.orientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR; |
| 2129 | mLocked.orientedRanges.toolMajor.source = mTouchSource; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2130 | mLocked.orientedRanges.toolMajor.min = 0; |
| 2131 | mLocked.orientedRanges.toolMajor.max = diagonalSize; |
| 2132 | mLocked.orientedRanges.toolMajor.flat = 0; |
| 2133 | mLocked.orientedRanges.toolMajor.fuzz = 0; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2134 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2135 | mLocked.orientedRanges.toolMinor = mLocked.orientedRanges.toolMajor; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2136 | mLocked.orientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2137 | } |
| 2138 | |
| 2139 | // Pressure factors. |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2140 | mLocked.pressureScale = 0; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2141 | if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE) { |
| 2142 | RawAbsoluteAxisInfo rawPressureAxis; |
| 2143 | switch (mCalibration.pressureSource) { |
| 2144 | case Calibration::PRESSURE_SOURCE_PRESSURE: |
| 2145 | rawPressureAxis = mRawAxes.pressure; |
| 2146 | break; |
| 2147 | case Calibration::PRESSURE_SOURCE_TOUCH: |
| 2148 | rawPressureAxis = mRawAxes.touchMajor; |
| 2149 | break; |
| 2150 | default: |
| 2151 | rawPressureAxis.clear(); |
| 2152 | } |
| 2153 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2154 | if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL |
| 2155 | || mCalibration.pressureCalibration |
| 2156 | == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) { |
| 2157 | if (mCalibration.havePressureScale) { |
| 2158 | mLocked.pressureScale = mCalibration.pressureScale; |
| 2159 | } else if (rawPressureAxis.valid && rawPressureAxis.maxValue != 0) { |
| 2160 | mLocked.pressureScale = 1.0f / rawPressureAxis.maxValue; |
| 2161 | } |
| 2162 | } |
| 2163 | |
| 2164 | mLocked.orientedRanges.havePressure = true; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2165 | |
| 2166 | mLocked.orientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE; |
| 2167 | mLocked.orientedRanges.pressure.source = mTouchSource; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2168 | mLocked.orientedRanges.pressure.min = 0; |
| 2169 | mLocked.orientedRanges.pressure.max = 1.0; |
| 2170 | mLocked.orientedRanges.pressure.flat = 0; |
| 2171 | mLocked.orientedRanges.pressure.fuzz = 0; |
| 2172 | } |
| 2173 | |
| 2174 | // Size factors. |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2175 | mLocked.sizeScale = 0; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2176 | if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2177 | if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_NORMALIZED) { |
| 2178 | if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) { |
| 2179 | mLocked.sizeScale = 1.0f / mRawAxes.toolMajor.maxValue; |
| 2180 | } |
| 2181 | } |
| 2182 | |
| 2183 | mLocked.orientedRanges.haveSize = true; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2184 | |
| 2185 | mLocked.orientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE; |
| 2186 | mLocked.orientedRanges.size.source = mTouchSource; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2187 | mLocked.orientedRanges.size.min = 0; |
| 2188 | mLocked.orientedRanges.size.max = 1.0; |
| 2189 | mLocked.orientedRanges.size.flat = 0; |
| 2190 | mLocked.orientedRanges.size.fuzz = 0; |
| 2191 | } |
| 2192 | |
| 2193 | // Orientation |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2194 | mLocked.orientationScale = 0; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2195 | if (mCalibration.orientationCalibration != Calibration::ORIENTATION_CALIBRATION_NONE) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2196 | if (mCalibration.orientationCalibration |
| 2197 | == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) { |
| 2198 | if (mRawAxes.orientation.valid && mRawAxes.orientation.maxValue != 0) { |
| 2199 | mLocked.orientationScale = float(M_PI_2) / mRawAxes.orientation.maxValue; |
| 2200 | } |
| 2201 | } |
| 2202 | |
Jeff Brown | b416e24 | 2011-05-24 15:17:57 -0700 | [diff] [blame] | 2203 | mLocked.orientedRanges.haveOrientation = true; |
| 2204 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2205 | mLocked.orientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION; |
| 2206 | mLocked.orientedRanges.orientation.source = mTouchSource; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2207 | mLocked.orientedRanges.orientation.min = - M_PI_2; |
| 2208 | mLocked.orientedRanges.orientation.max = M_PI_2; |
| 2209 | mLocked.orientedRanges.orientation.flat = 0; |
| 2210 | mLocked.orientedRanges.orientation.fuzz = 0; |
| 2211 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2212 | } |
| 2213 | |
| 2214 | if (orientationChanged || sizeChanged) { |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2215 | // Compute oriented surface dimensions, precision, scales and ranges. |
| 2216 | // Note that the maximum value reported is an inclusive maximum value so it is one |
| 2217 | // unit less than the total width or height of surface. |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2218 | switch (mLocked.surfaceOrientation) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 2219 | case DISPLAY_ORIENTATION_90: |
| 2220 | case DISPLAY_ORIENTATION_270: |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2221 | mLocked.orientedSurfaceWidth = mLocked.surfaceHeight; |
| 2222 | mLocked.orientedSurfaceHeight = mLocked.surfaceWidth; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2223 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2224 | mLocked.orientedXPrecision = mLocked.yPrecision; |
| 2225 | mLocked.orientedYPrecision = mLocked.xPrecision; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2226 | |
| 2227 | mLocked.orientedRanges.x.min = 0; |
| 2228 | mLocked.orientedRanges.x.max = (mRawAxes.y.maxValue - mRawAxes.y.minValue) |
| 2229 | * mLocked.yScale; |
| 2230 | mLocked.orientedRanges.x.flat = 0; |
| 2231 | mLocked.orientedRanges.x.fuzz = mLocked.yScale; |
| 2232 | |
| 2233 | mLocked.orientedRanges.y.min = 0; |
| 2234 | mLocked.orientedRanges.y.max = (mRawAxes.x.maxValue - mRawAxes.x.minValue) |
| 2235 | * mLocked.xScale; |
| 2236 | mLocked.orientedRanges.y.flat = 0; |
| 2237 | mLocked.orientedRanges.y.fuzz = mLocked.xScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2238 | break; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2239 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2240 | default: |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2241 | mLocked.orientedSurfaceWidth = mLocked.surfaceWidth; |
| 2242 | mLocked.orientedSurfaceHeight = mLocked.surfaceHeight; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2243 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2244 | mLocked.orientedXPrecision = mLocked.xPrecision; |
| 2245 | mLocked.orientedYPrecision = mLocked.yPrecision; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2246 | |
| 2247 | mLocked.orientedRanges.x.min = 0; |
| 2248 | mLocked.orientedRanges.x.max = (mRawAxes.x.maxValue - mRawAxes.x.minValue) |
| 2249 | * mLocked.xScale; |
| 2250 | mLocked.orientedRanges.x.flat = 0; |
| 2251 | mLocked.orientedRanges.x.fuzz = mLocked.xScale; |
| 2252 | |
| 2253 | mLocked.orientedRanges.y.min = 0; |
| 2254 | mLocked.orientedRanges.y.max = (mRawAxes.y.maxValue - mRawAxes.y.minValue) |
| 2255 | * mLocked.yScale; |
| 2256 | mLocked.orientedRanges.y.flat = 0; |
| 2257 | mLocked.orientedRanges.y.fuzz = mLocked.yScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2258 | break; |
| 2259 | } |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2260 | |
| 2261 | // Compute pointer gesture detection parameters. |
| 2262 | // TODO: These factors should not be hardcoded. |
| 2263 | if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) { |
| 2264 | int32_t rawWidth = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1; |
| 2265 | int32_t rawHeight = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1; |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2266 | float rawDiagonal = hypotf(rawWidth, rawHeight); |
| 2267 | float displayDiagonal = hypotf(mLocked.associatedDisplayWidth, |
| 2268 | mLocked.associatedDisplayHeight); |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2269 | |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2270 | // Scale movements such that one whole swipe of the touch pad covers a |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame^] | 2271 | // given area relative to the diagonal size of the display when no acceleration |
| 2272 | // is applied. |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2273 | // Assume that the touch pad has a square aspect ratio such that movements in |
| 2274 | // X and Y of the same number of raw units cover the same physical distance. |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 2275 | mLocked.pointerGestureXMovementScale = mConfig->pointerGestureMovementSpeedRatio |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2276 | * displayDiagonal / rawDiagonal; |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2277 | mLocked.pointerGestureYMovementScale = mLocked.pointerGestureXMovementScale; |
| 2278 | |
| 2279 | // Scale zooms to cover a smaller range of the display than movements do. |
| 2280 | // This value determines the area around the pointer that is affected by freeform |
| 2281 | // pointer gestures. |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 2282 | mLocked.pointerGestureXZoomScale = mConfig->pointerGestureZoomSpeedRatio |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2283 | * displayDiagonal / rawDiagonal; |
| 2284 | mLocked.pointerGestureYZoomScale = mLocked.pointerGestureXZoomScale; |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2285 | |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2286 | // Max width between pointers to detect a swipe gesture is more than some fraction |
| 2287 | // of the diagonal axis of the touch pad. Touches that are wider than this are |
| 2288 | // translated into freeform gestures. |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 2289 | mLocked.pointerGestureMaxSwipeWidth = |
| 2290 | mConfig->pointerGestureSwipeMaxWidthRatio * rawDiagonal; |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2291 | |
| 2292 | // Reset the current pointer gesture. |
| 2293 | mPointerGesture.reset(); |
| 2294 | |
| 2295 | // Remove any current spots. |
| 2296 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) { |
| 2297 | mPointerController->clearSpots(); |
| 2298 | } |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2299 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2300 | } |
| 2301 | |
| 2302 | return true; |
| 2303 | } |
| 2304 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2305 | void TouchInputMapper::dumpSurfaceLocked(String8& dump) { |
| 2306 | dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mLocked.surfaceWidth); |
| 2307 | dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mLocked.surfaceHeight); |
| 2308 | dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mLocked.surfaceOrientation); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2309 | } |
| 2310 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2311 | void TouchInputMapper::configureVirtualKeysLocked() { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2312 | Vector<VirtualKeyDefinition> virtualKeyDefinitions; |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 2313 | getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2314 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2315 | mLocked.virtualKeys.clear(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2316 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2317 | if (virtualKeyDefinitions.size() == 0) { |
| 2318 | return; |
| 2319 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2320 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2321 | mLocked.virtualKeys.setCapacity(virtualKeyDefinitions.size()); |
| 2322 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2323 | int32_t touchScreenLeft = mRawAxes.x.minValue; |
| 2324 | int32_t touchScreenTop = mRawAxes.y.minValue; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2325 | int32_t touchScreenWidth = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1; |
| 2326 | int32_t touchScreenHeight = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2327 | |
| 2328 | for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2329 | const VirtualKeyDefinition& virtualKeyDefinition = |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2330 | virtualKeyDefinitions[i]; |
| 2331 | |
| 2332 | mLocked.virtualKeys.add(); |
| 2333 | VirtualKey& virtualKey = mLocked.virtualKeys.editTop(); |
| 2334 | |
| 2335 | virtualKey.scanCode = virtualKeyDefinition.scanCode; |
| 2336 | int32_t keyCode; |
| 2337 | uint32_t flags; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 2338 | if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode, |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2339 | & keyCode, & flags)) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2340 | LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring", |
| 2341 | virtualKey.scanCode); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2342 | mLocked.virtualKeys.pop(); // drop the key |
| 2343 | continue; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2344 | } |
| 2345 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2346 | virtualKey.keyCode = keyCode; |
| 2347 | virtualKey.flags = flags; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2348 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2349 | // convert the key definition's display coordinates into touch coordinates for a hit box |
| 2350 | int32_t halfWidth = virtualKeyDefinition.width / 2; |
| 2351 | int32_t halfHeight = virtualKeyDefinition.height / 2; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2352 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2353 | virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth) |
| 2354 | * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft; |
| 2355 | virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth) |
| 2356 | * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft; |
| 2357 | virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight) |
| 2358 | * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop; |
| 2359 | virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight) |
| 2360 | * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop; |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2361 | } |
| 2362 | } |
| 2363 | |
| 2364 | void TouchInputMapper::dumpVirtualKeysLocked(String8& dump) { |
| 2365 | if (!mLocked.virtualKeys.isEmpty()) { |
| 2366 | dump.append(INDENT3 "Virtual Keys:\n"); |
| 2367 | |
| 2368 | for (size_t i = 0; i < mLocked.virtualKeys.size(); i++) { |
| 2369 | const VirtualKey& virtualKey = mLocked.virtualKeys.itemAt(i); |
| 2370 | dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, " |
| 2371 | "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n", |
| 2372 | i, virtualKey.scanCode, virtualKey.keyCode, |
| 2373 | virtualKey.hitLeft, virtualKey.hitRight, |
| 2374 | virtualKey.hitTop, virtualKey.hitBottom); |
| 2375 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2376 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2377 | } |
| 2378 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2379 | void TouchInputMapper::parseCalibration() { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2380 | const PropertyMap& in = getDevice()->getConfiguration(); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2381 | Calibration& out = mCalibration; |
| 2382 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2383 | // Touch Size |
| 2384 | out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT; |
| 2385 | String8 touchSizeCalibrationString; |
| 2386 | if (in.tryGetProperty(String8("touch.touchSize.calibration"), touchSizeCalibrationString)) { |
| 2387 | if (touchSizeCalibrationString == "none") { |
| 2388 | out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE; |
| 2389 | } else if (touchSizeCalibrationString == "geometric") { |
| 2390 | out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC; |
| 2391 | } else if (touchSizeCalibrationString == "pressure") { |
| 2392 | out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE; |
| 2393 | } else if (touchSizeCalibrationString != "default") { |
| 2394 | LOGW("Invalid value for touch.touchSize.calibration: '%s'", |
| 2395 | touchSizeCalibrationString.string()); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2396 | } |
| 2397 | } |
| 2398 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2399 | // Tool Size |
| 2400 | out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_DEFAULT; |
| 2401 | String8 toolSizeCalibrationString; |
| 2402 | if (in.tryGetProperty(String8("touch.toolSize.calibration"), toolSizeCalibrationString)) { |
| 2403 | if (toolSizeCalibrationString == "none") { |
| 2404 | out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE; |
| 2405 | } else if (toolSizeCalibrationString == "geometric") { |
| 2406 | out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC; |
| 2407 | } else if (toolSizeCalibrationString == "linear") { |
| 2408 | out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR; |
| 2409 | } else if (toolSizeCalibrationString == "area") { |
| 2410 | out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_AREA; |
| 2411 | } else if (toolSizeCalibrationString != "default") { |
| 2412 | LOGW("Invalid value for touch.toolSize.calibration: '%s'", |
| 2413 | toolSizeCalibrationString.string()); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2414 | } |
| 2415 | } |
| 2416 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2417 | out.haveToolSizeLinearScale = in.tryGetProperty(String8("touch.toolSize.linearScale"), |
| 2418 | out.toolSizeLinearScale); |
| 2419 | out.haveToolSizeLinearBias = in.tryGetProperty(String8("touch.toolSize.linearBias"), |
| 2420 | out.toolSizeLinearBias); |
| 2421 | out.haveToolSizeAreaScale = in.tryGetProperty(String8("touch.toolSize.areaScale"), |
| 2422 | out.toolSizeAreaScale); |
| 2423 | out.haveToolSizeAreaBias = in.tryGetProperty(String8("touch.toolSize.areaBias"), |
| 2424 | out.toolSizeAreaBias); |
| 2425 | out.haveToolSizeIsSummed = in.tryGetProperty(String8("touch.toolSize.isSummed"), |
| 2426 | out.toolSizeIsSummed); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2427 | |
| 2428 | // Pressure |
| 2429 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT; |
| 2430 | String8 pressureCalibrationString; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2431 | if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2432 | if (pressureCalibrationString == "none") { |
| 2433 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE; |
| 2434 | } else if (pressureCalibrationString == "physical") { |
| 2435 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL; |
| 2436 | } else if (pressureCalibrationString == "amplitude") { |
| 2437 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE; |
| 2438 | } else if (pressureCalibrationString != "default") { |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2439 | LOGW("Invalid value for touch.pressure.calibration: '%s'", |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2440 | pressureCalibrationString.string()); |
| 2441 | } |
| 2442 | } |
| 2443 | |
| 2444 | out.pressureSource = Calibration::PRESSURE_SOURCE_DEFAULT; |
| 2445 | String8 pressureSourceString; |
| 2446 | if (in.tryGetProperty(String8("touch.pressure.source"), pressureSourceString)) { |
| 2447 | if (pressureSourceString == "pressure") { |
| 2448 | out.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE; |
| 2449 | } else if (pressureSourceString == "touch") { |
| 2450 | out.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH; |
| 2451 | } else if (pressureSourceString != "default") { |
| 2452 | LOGW("Invalid value for touch.pressure.source: '%s'", |
| 2453 | pressureSourceString.string()); |
| 2454 | } |
| 2455 | } |
| 2456 | |
| 2457 | out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"), |
| 2458 | out.pressureScale); |
| 2459 | |
| 2460 | // Size |
| 2461 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT; |
| 2462 | String8 sizeCalibrationString; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2463 | if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2464 | if (sizeCalibrationString == "none") { |
| 2465 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE; |
| 2466 | } else if (sizeCalibrationString == "normalized") { |
| 2467 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED; |
| 2468 | } else if (sizeCalibrationString != "default") { |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2469 | LOGW("Invalid value for touch.size.calibration: '%s'", |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2470 | sizeCalibrationString.string()); |
| 2471 | } |
| 2472 | } |
| 2473 | |
| 2474 | // Orientation |
| 2475 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT; |
| 2476 | String8 orientationCalibrationString; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2477 | if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2478 | if (orientationCalibrationString == "none") { |
| 2479 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE; |
| 2480 | } else if (orientationCalibrationString == "interpolated") { |
| 2481 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED; |
Jeff Brown | 517bb4c | 2011-01-14 19:09:23 -0800 | [diff] [blame] | 2482 | } else if (orientationCalibrationString == "vector") { |
| 2483 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2484 | } else if (orientationCalibrationString != "default") { |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2485 | LOGW("Invalid value for touch.orientation.calibration: '%s'", |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2486 | orientationCalibrationString.string()); |
| 2487 | } |
| 2488 | } |
| 2489 | } |
| 2490 | |
| 2491 | void TouchInputMapper::resolveCalibration() { |
| 2492 | // Pressure |
| 2493 | switch (mCalibration.pressureSource) { |
| 2494 | case Calibration::PRESSURE_SOURCE_DEFAULT: |
| 2495 | if (mRawAxes.pressure.valid) { |
| 2496 | mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE; |
| 2497 | } else if (mRawAxes.touchMajor.valid) { |
| 2498 | mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH; |
| 2499 | } |
| 2500 | break; |
| 2501 | |
| 2502 | case Calibration::PRESSURE_SOURCE_PRESSURE: |
| 2503 | if (! mRawAxes.pressure.valid) { |
| 2504 | LOGW("Calibration property touch.pressure.source is 'pressure' but " |
| 2505 | "the pressure axis is not available."); |
| 2506 | } |
| 2507 | break; |
| 2508 | |
| 2509 | case Calibration::PRESSURE_SOURCE_TOUCH: |
| 2510 | if (! mRawAxes.touchMajor.valid) { |
| 2511 | LOGW("Calibration property touch.pressure.source is 'touch' but " |
| 2512 | "the touchMajor axis is not available."); |
| 2513 | } |
| 2514 | break; |
| 2515 | |
| 2516 | default: |
| 2517 | break; |
| 2518 | } |
| 2519 | |
| 2520 | switch (mCalibration.pressureCalibration) { |
| 2521 | case Calibration::PRESSURE_CALIBRATION_DEFAULT: |
| 2522 | if (mCalibration.pressureSource != Calibration::PRESSURE_SOURCE_DEFAULT) { |
| 2523 | mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE; |
| 2524 | } else { |
| 2525 | mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE; |
| 2526 | } |
| 2527 | break; |
| 2528 | |
| 2529 | default: |
| 2530 | break; |
| 2531 | } |
| 2532 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2533 | // Tool Size |
| 2534 | switch (mCalibration.toolSizeCalibration) { |
| 2535 | case Calibration::TOOL_SIZE_CALIBRATION_DEFAULT: |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2536 | if (mRawAxes.toolMajor.valid) { |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2537 | mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2538 | } else { |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2539 | mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2540 | } |
| 2541 | break; |
| 2542 | |
| 2543 | default: |
| 2544 | break; |
| 2545 | } |
| 2546 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2547 | // Touch Size |
| 2548 | switch (mCalibration.touchSizeCalibration) { |
| 2549 | case Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT: |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2550 | if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2551 | && mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) { |
| 2552 | mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2553 | } else { |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2554 | mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2555 | } |
| 2556 | break; |
| 2557 | |
| 2558 | default: |
| 2559 | break; |
| 2560 | } |
| 2561 | |
| 2562 | // Size |
| 2563 | switch (mCalibration.sizeCalibration) { |
| 2564 | case Calibration::SIZE_CALIBRATION_DEFAULT: |
| 2565 | if (mRawAxes.toolMajor.valid) { |
| 2566 | mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED; |
| 2567 | } else { |
| 2568 | mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE; |
| 2569 | } |
| 2570 | break; |
| 2571 | |
| 2572 | default: |
| 2573 | break; |
| 2574 | } |
| 2575 | |
| 2576 | // Orientation |
| 2577 | switch (mCalibration.orientationCalibration) { |
| 2578 | case Calibration::ORIENTATION_CALIBRATION_DEFAULT: |
| 2579 | if (mRawAxes.orientation.valid) { |
| 2580 | mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED; |
| 2581 | } else { |
| 2582 | mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE; |
| 2583 | } |
| 2584 | break; |
| 2585 | |
| 2586 | default: |
| 2587 | break; |
| 2588 | } |
| 2589 | } |
| 2590 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2591 | void TouchInputMapper::dumpCalibration(String8& dump) { |
| 2592 | dump.append(INDENT3 "Calibration:\n"); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2593 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2594 | // Touch Size |
| 2595 | switch (mCalibration.touchSizeCalibration) { |
| 2596 | case Calibration::TOUCH_SIZE_CALIBRATION_NONE: |
| 2597 | dump.append(INDENT4 "touch.touchSize.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2598 | break; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2599 | case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC: |
| 2600 | dump.append(INDENT4 "touch.touchSize.calibration: geometric\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2601 | break; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2602 | case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE: |
| 2603 | dump.append(INDENT4 "touch.touchSize.calibration: pressure\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2604 | break; |
| 2605 | default: |
| 2606 | assert(false); |
| 2607 | } |
| 2608 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2609 | // Tool Size |
| 2610 | switch (mCalibration.toolSizeCalibration) { |
| 2611 | case Calibration::TOOL_SIZE_CALIBRATION_NONE: |
| 2612 | dump.append(INDENT4 "touch.toolSize.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2613 | break; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2614 | case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC: |
| 2615 | dump.append(INDENT4 "touch.toolSize.calibration: geometric\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2616 | break; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2617 | case Calibration::TOOL_SIZE_CALIBRATION_LINEAR: |
| 2618 | dump.append(INDENT4 "touch.toolSize.calibration: linear\n"); |
| 2619 | break; |
| 2620 | case Calibration::TOOL_SIZE_CALIBRATION_AREA: |
| 2621 | dump.append(INDENT4 "touch.toolSize.calibration: area\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2622 | break; |
| 2623 | default: |
| 2624 | assert(false); |
| 2625 | } |
| 2626 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2627 | if (mCalibration.haveToolSizeLinearScale) { |
| 2628 | dump.appendFormat(INDENT4 "touch.toolSize.linearScale: %0.3f\n", |
| 2629 | mCalibration.toolSizeLinearScale); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2630 | } |
| 2631 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2632 | if (mCalibration.haveToolSizeLinearBias) { |
| 2633 | dump.appendFormat(INDENT4 "touch.toolSize.linearBias: %0.3f\n", |
| 2634 | mCalibration.toolSizeLinearBias); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2635 | } |
| 2636 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2637 | if (mCalibration.haveToolSizeAreaScale) { |
| 2638 | dump.appendFormat(INDENT4 "touch.toolSize.areaScale: %0.3f\n", |
| 2639 | mCalibration.toolSizeAreaScale); |
| 2640 | } |
| 2641 | |
| 2642 | if (mCalibration.haveToolSizeAreaBias) { |
| 2643 | dump.appendFormat(INDENT4 "touch.toolSize.areaBias: %0.3f\n", |
| 2644 | mCalibration.toolSizeAreaBias); |
| 2645 | } |
| 2646 | |
| 2647 | if (mCalibration.haveToolSizeIsSummed) { |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 2648 | dump.appendFormat(INDENT4 "touch.toolSize.isSummed: %s\n", |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2649 | toString(mCalibration.toolSizeIsSummed)); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2650 | } |
| 2651 | |
| 2652 | // Pressure |
| 2653 | switch (mCalibration.pressureCalibration) { |
| 2654 | case Calibration::PRESSURE_CALIBRATION_NONE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2655 | dump.append(INDENT4 "touch.pressure.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2656 | break; |
| 2657 | case Calibration::PRESSURE_CALIBRATION_PHYSICAL: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2658 | dump.append(INDENT4 "touch.pressure.calibration: physical\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2659 | break; |
| 2660 | case Calibration::PRESSURE_CALIBRATION_AMPLITUDE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2661 | dump.append(INDENT4 "touch.pressure.calibration: amplitude\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2662 | break; |
| 2663 | default: |
| 2664 | assert(false); |
| 2665 | } |
| 2666 | |
| 2667 | switch (mCalibration.pressureSource) { |
| 2668 | case Calibration::PRESSURE_SOURCE_PRESSURE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2669 | dump.append(INDENT4 "touch.pressure.source: pressure\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2670 | break; |
| 2671 | case Calibration::PRESSURE_SOURCE_TOUCH: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2672 | dump.append(INDENT4 "touch.pressure.source: touch\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2673 | break; |
| 2674 | case Calibration::PRESSURE_SOURCE_DEFAULT: |
| 2675 | break; |
| 2676 | default: |
| 2677 | assert(false); |
| 2678 | } |
| 2679 | |
| 2680 | if (mCalibration.havePressureScale) { |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2681 | dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n", |
| 2682 | mCalibration.pressureScale); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2683 | } |
| 2684 | |
| 2685 | // Size |
| 2686 | switch (mCalibration.sizeCalibration) { |
| 2687 | case Calibration::SIZE_CALIBRATION_NONE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2688 | dump.append(INDENT4 "touch.size.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2689 | break; |
| 2690 | case Calibration::SIZE_CALIBRATION_NORMALIZED: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2691 | dump.append(INDENT4 "touch.size.calibration: normalized\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2692 | break; |
| 2693 | default: |
| 2694 | assert(false); |
| 2695 | } |
| 2696 | |
| 2697 | // Orientation |
| 2698 | switch (mCalibration.orientationCalibration) { |
| 2699 | case Calibration::ORIENTATION_CALIBRATION_NONE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2700 | dump.append(INDENT4 "touch.orientation.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2701 | break; |
| 2702 | case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2703 | dump.append(INDENT4 "touch.orientation.calibration: interpolated\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2704 | break; |
Jeff Brown | 517bb4c | 2011-01-14 19:09:23 -0800 | [diff] [blame] | 2705 | case Calibration::ORIENTATION_CALIBRATION_VECTOR: |
| 2706 | dump.append(INDENT4 "touch.orientation.calibration: vector\n"); |
| 2707 | break; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2708 | default: |
| 2709 | assert(false); |
| 2710 | } |
| 2711 | } |
| 2712 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2713 | void TouchInputMapper::reset() { |
| 2714 | // Synthesize touch up event if touch is currently down. |
| 2715 | // This will also take care of finishing virtual key processing if needed. |
| 2716 | if (mLastTouch.pointerCount != 0) { |
| 2717 | nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC); |
| 2718 | mCurrentTouch.clear(); |
| 2719 | syncTouch(when, true); |
| 2720 | } |
| 2721 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2722 | { // acquire lock |
| 2723 | AutoMutex _l(mLock); |
| 2724 | initializeLocked(); |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2725 | |
| 2726 | if (mPointerController != NULL |
| 2727 | && mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) { |
| 2728 | mPointerController->clearSpots(); |
| 2729 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2730 | } // release lock |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2731 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2732 | InputMapper::reset(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2733 | } |
| 2734 | |
| 2735 | void TouchInputMapper::syncTouch(nsecs_t when, bool havePointerIds) { |
Jeff Brown | 68d6075 | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 2736 | #if DEBUG_RAW_EVENTS |
| 2737 | if (!havePointerIds) { |
| 2738 | LOGD("syncTouch: pointerCount=%d, no pointer ids", mCurrentTouch.pointerCount); |
| 2739 | } else { |
| 2740 | LOGD("syncTouch: pointerCount=%d, up=0x%08x, down=0x%08x, move=0x%08x, " |
| 2741 | "last=0x%08x, current=0x%08x", mCurrentTouch.pointerCount, |
| 2742 | mLastTouch.idBits.value & ~mCurrentTouch.idBits.value, |
| 2743 | mCurrentTouch.idBits.value & ~mLastTouch.idBits.value, |
| 2744 | mLastTouch.idBits.value & mCurrentTouch.idBits.value, |
| 2745 | mLastTouch.idBits.value, mCurrentTouch.idBits.value); |
| 2746 | } |
| 2747 | #endif |
| 2748 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2749 | // Preprocess pointer data. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2750 | if (mParameters.useBadTouchFilter) { |
| 2751 | if (applyBadTouchFilter()) { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2752 | havePointerIds = false; |
| 2753 | } |
| 2754 | } |
| 2755 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2756 | if (mParameters.useJumpyTouchFilter) { |
| 2757 | if (applyJumpyTouchFilter()) { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2758 | havePointerIds = false; |
| 2759 | } |
| 2760 | } |
| 2761 | |
Jeff Brown | 68d6075 | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 2762 | if (!havePointerIds) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2763 | calculatePointerIds(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2764 | } |
| 2765 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2766 | TouchData temp; |
| 2767 | TouchData* savedTouch; |
| 2768 | if (mParameters.useAveragingTouchFilter) { |
| 2769 | temp.copyFrom(mCurrentTouch); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2770 | savedTouch = & temp; |
| 2771 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2772 | applyAveragingTouchFilter(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2773 | } else { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2774 | savedTouch = & mCurrentTouch; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2775 | } |
| 2776 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 2777 | uint32_t policyFlags = 0; |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 2778 | if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount != 0) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2779 | if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) { |
| 2780 | // If this is a touch screen, hide the pointer on an initial down. |
| 2781 | getContext()->fadePointer(); |
| 2782 | } |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 2783 | |
| 2784 | // Initial downs on external touch devices should wake the device. |
| 2785 | // We don't do this for internal touch screens to prevent them from waking |
| 2786 | // up in your pocket. |
| 2787 | // TODO: Use the input device configuration to control this behavior more finely. |
| 2788 | if (getDevice()->isExternal()) { |
| 2789 | policyFlags |= POLICY_FLAG_WAKE_DROPPED; |
| 2790 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 2791 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2792 | |
Jeff Brown | 325bd07 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 2793 | TouchResult touchResult; |
| 2794 | if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount == 0 |
| 2795 | && mLastTouch.buttonState == mCurrentTouch.buttonState) { |
| 2796 | // Drop spurious syncs. |
| 2797 | touchResult = DROP_STROKE; |
| 2798 | } else { |
| 2799 | // Process touches and virtual keys. |
| 2800 | touchResult = consumeOffScreenTouches(when, policyFlags); |
| 2801 | if (touchResult == DISPATCH_TOUCH) { |
| 2802 | suppressSwipeOntoVirtualKeys(when); |
| 2803 | if (mPointerController != NULL) { |
| 2804 | dispatchPointerGestures(when, policyFlags, false /*isTimeout*/); |
| 2805 | } |
| 2806 | dispatchTouches(when, policyFlags); |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2807 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2808 | } |
| 2809 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2810 | // Copy current touch to last touch in preparation for the next cycle. |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2811 | // Keep the button state so we can track edge-triggered button state changes. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2812 | if (touchResult == DROP_STROKE) { |
| 2813 | mLastTouch.clear(); |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2814 | mLastTouch.buttonState = savedTouch->buttonState; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2815 | } else { |
| 2816 | mLastTouch.copyFrom(*savedTouch); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2817 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2818 | } |
| 2819 | |
Jeff Brown | 325bd07 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 2820 | void TouchInputMapper::timeoutExpired(nsecs_t when) { |
| 2821 | if (mPointerController != NULL) { |
| 2822 | dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/); |
| 2823 | } |
| 2824 | } |
| 2825 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2826 | TouchInputMapper::TouchResult TouchInputMapper::consumeOffScreenTouches( |
| 2827 | nsecs_t when, uint32_t policyFlags) { |
| 2828 | int32_t keyEventAction, keyEventFlags; |
| 2829 | int32_t keyCode, scanCode, downTime; |
| 2830 | TouchResult touchResult; |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 2831 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2832 | { // acquire lock |
| 2833 | AutoMutex _l(mLock); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2834 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2835 | // Update surface size and orientation, including virtual key positions. |
| 2836 | if (! configureSurfaceLocked()) { |
| 2837 | return DROP_STROKE; |
| 2838 | } |
| 2839 | |
| 2840 | // Check for virtual key press. |
| 2841 | if (mLocked.currentVirtualKey.down) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2842 | if (mCurrentTouch.pointerCount == 0) { |
| 2843 | // Pointer went up while virtual key was down. |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2844 | mLocked.currentVirtualKey.down = false; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2845 | #if DEBUG_VIRTUAL_KEYS |
| 2846 | LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d", |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 2847 | mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2848 | #endif |
| 2849 | keyEventAction = AKEY_EVENT_ACTION_UP; |
| 2850 | keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 2851 | touchResult = SKIP_TOUCH; |
| 2852 | goto DispatchVirtualKey; |
| 2853 | } |
| 2854 | |
| 2855 | if (mCurrentTouch.pointerCount == 1) { |
| 2856 | int32_t x = mCurrentTouch.pointers[0].x; |
| 2857 | int32_t y = mCurrentTouch.pointers[0].y; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2858 | const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y); |
| 2859 | if (virtualKey && virtualKey->keyCode == mLocked.currentVirtualKey.keyCode) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2860 | // Pointer is still within the space of the virtual key. |
| 2861 | return SKIP_TOUCH; |
| 2862 | } |
| 2863 | } |
| 2864 | |
| 2865 | // Pointer left virtual key area or another pointer also went down. |
| 2866 | // Send key cancellation and drop the stroke so subsequent motions will be |
| 2867 | // considered fresh downs. This is useful when the user swipes away from the |
| 2868 | // virtual key area into the main display surface. |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2869 | mLocked.currentVirtualKey.down = false; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2870 | #if DEBUG_VIRTUAL_KEYS |
| 2871 | LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d", |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 2872 | mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2873 | #endif |
| 2874 | keyEventAction = AKEY_EVENT_ACTION_UP; |
| 2875 | keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY |
| 2876 | | AKEY_EVENT_FLAG_CANCELED; |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 2877 | |
| 2878 | // Check whether the pointer moved inside the display area where we should |
| 2879 | // start a new stroke. |
| 2880 | int32_t x = mCurrentTouch.pointers[0].x; |
| 2881 | int32_t y = mCurrentTouch.pointers[0].y; |
| 2882 | if (isPointInsideSurfaceLocked(x, y)) { |
| 2883 | mLastTouch.clear(); |
| 2884 | touchResult = DISPATCH_TOUCH; |
| 2885 | } else { |
| 2886 | touchResult = DROP_STROKE; |
| 2887 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2888 | } else { |
| 2889 | if (mCurrentTouch.pointerCount >= 1 && mLastTouch.pointerCount == 0) { |
| 2890 | // Pointer just went down. Handle off-screen touches, if needed. |
| 2891 | int32_t x = mCurrentTouch.pointers[0].x; |
| 2892 | int32_t y = mCurrentTouch.pointers[0].y; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2893 | if (! isPointInsideSurfaceLocked(x, y)) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2894 | // If exactly one pointer went down, check for virtual key hit. |
| 2895 | // Otherwise we will drop the entire stroke. |
| 2896 | if (mCurrentTouch.pointerCount == 1) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2897 | const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2898 | if (virtualKey) { |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 2899 | if (mContext->shouldDropVirtualKey(when, getDevice(), |
| 2900 | virtualKey->keyCode, virtualKey->scanCode)) { |
| 2901 | return DROP_STROKE; |
| 2902 | } |
| 2903 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2904 | mLocked.currentVirtualKey.down = true; |
| 2905 | mLocked.currentVirtualKey.downTime = when; |
| 2906 | mLocked.currentVirtualKey.keyCode = virtualKey->keyCode; |
| 2907 | mLocked.currentVirtualKey.scanCode = virtualKey->scanCode; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2908 | #if DEBUG_VIRTUAL_KEYS |
| 2909 | LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d", |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 2910 | mLocked.currentVirtualKey.keyCode, |
| 2911 | mLocked.currentVirtualKey.scanCode); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2912 | #endif |
| 2913 | keyEventAction = AKEY_EVENT_ACTION_DOWN; |
| 2914 | keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM |
| 2915 | | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 2916 | touchResult = SKIP_TOUCH; |
| 2917 | goto DispatchVirtualKey; |
| 2918 | } |
| 2919 | } |
| 2920 | return DROP_STROKE; |
| 2921 | } |
| 2922 | } |
| 2923 | return DISPATCH_TOUCH; |
| 2924 | } |
| 2925 | |
| 2926 | DispatchVirtualKey: |
| 2927 | // Collect remaining state needed to dispatch virtual key. |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2928 | keyCode = mLocked.currentVirtualKey.keyCode; |
| 2929 | scanCode = mLocked.currentVirtualKey.scanCode; |
| 2930 | downTime = mLocked.currentVirtualKey.downTime; |
| 2931 | } // release lock |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2932 | |
| 2933 | // Dispatch virtual key. |
| 2934 | int32_t metaState = mContext->getGlobalMetaState(); |
Jeff Brown | 0eaf393 | 2010-10-01 14:55:30 -0700 | [diff] [blame] | 2935 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 2936 | getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags, |
| 2937 | keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime); |
| 2938 | return touchResult; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2939 | } |
| 2940 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2941 | void TouchInputMapper::suppressSwipeOntoVirtualKeys(nsecs_t when) { |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 2942 | // Disable all virtual key touches that happen within a short time interval of the |
| 2943 | // most recent touch. The idea is to filter out stray virtual key presses when |
| 2944 | // interacting with the touch screen. |
| 2945 | // |
| 2946 | // Problems we're trying to solve: |
| 2947 | // |
| 2948 | // 1. While scrolling a list or dragging the window shade, the user swipes down into a |
| 2949 | // virtual key area that is implemented by a separate touch panel and accidentally |
| 2950 | // triggers a virtual key. |
| 2951 | // |
| 2952 | // 2. While typing in the on screen keyboard, the user taps slightly outside the screen |
| 2953 | // area and accidentally triggers a virtual key. This often happens when virtual keys |
| 2954 | // are layed out below the screen near to where the on screen keyboard's space bar |
| 2955 | // is displayed. |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 2956 | if (mConfig->virtualKeyQuietTime > 0 && mCurrentTouch.pointerCount != 0) { |
| 2957 | mContext->disableVirtualKeysUntil(when + mConfig->virtualKeyQuietTime); |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 2958 | } |
| 2959 | } |
| 2960 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2961 | void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) { |
| 2962 | uint32_t currentPointerCount = mCurrentTouch.pointerCount; |
| 2963 | uint32_t lastPointerCount = mLastTouch.pointerCount; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2964 | if (currentPointerCount == 0 && lastPointerCount == 0) { |
| 2965 | return; // nothing to do! |
| 2966 | } |
| 2967 | |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2968 | // Update current touch coordinates. |
| 2969 | int32_t edgeFlags; |
| 2970 | float xPrecision, yPrecision; |
| 2971 | prepareTouches(&edgeFlags, &xPrecision, &yPrecision); |
| 2972 | |
| 2973 | // Dispatch motions. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2974 | BitSet32 currentIdBits = mCurrentTouch.idBits; |
| 2975 | BitSet32 lastIdBits = mLastTouch.idBits; |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2976 | uint32_t metaState = getContext()->getGlobalMetaState(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2977 | |
| 2978 | if (currentIdBits == lastIdBits) { |
| 2979 | // No pointer id changes so this is a move event. |
| 2980 | // The dispatcher takes care of batching moves so we don't have to deal with that here. |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2981 | dispatchMotion(when, policyFlags, mTouchSource, |
| 2982 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 2983 | mCurrentTouchCoords, mCurrentTouch.idToIndex, currentIdBits, -1, |
| 2984 | xPrecision, yPrecision, mDownTime); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2985 | } else { |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 2986 | // There may be pointers going up and pointers going down and pointers moving |
| 2987 | // all at the same time. |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2988 | BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value); |
| 2989 | BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value); |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 2990 | BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value); |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2991 | BitSet32 dispatchedIdBits(lastIdBits.value); |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 2992 | |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2993 | // Update last coordinates of pointers that have moved so that we observe the new |
| 2994 | // pointer positions at the same time as other pointers that have just gone up. |
| 2995 | bool moveNeeded = updateMovedPointerCoords( |
| 2996 | mCurrentTouchCoords, mCurrentTouch.idToIndex, |
| 2997 | mLastTouchCoords, mLastTouch.idToIndex, |
| 2998 | moveIdBits); |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 2999 | |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3000 | // Dispatch pointer up events. |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3001 | while (!upIdBits.isEmpty()) { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3002 | uint32_t upId = upIdBits.firstMarkedBit(); |
| 3003 | upIdBits.clearBit(upId); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3004 | |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3005 | dispatchMotion(when, policyFlags, mTouchSource, |
| 3006 | AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, 0, |
| 3007 | mLastTouchCoords, mLastTouch.idToIndex, dispatchedIdBits, upId, |
| 3008 | xPrecision, yPrecision, mDownTime); |
| 3009 | dispatchedIdBits.clearBit(upId); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3010 | } |
| 3011 | |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3012 | // Dispatch move events if any of the remaining pointers moved from their old locations. |
| 3013 | // Although applications receive new locations as part of individual pointer up |
| 3014 | // events, they do not generally handle them except when presented in a move event. |
| 3015 | if (moveNeeded) { |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3016 | assert(moveIdBits.value == dispatchedIdBits.value); |
| 3017 | dispatchMotion(when, policyFlags, mTouchSource, |
| 3018 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, 0, |
| 3019 | mCurrentTouchCoords, mCurrentTouch.idToIndex, dispatchedIdBits, -1, |
| 3020 | xPrecision, yPrecision, mDownTime); |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3021 | } |
| 3022 | |
| 3023 | // Dispatch pointer down events using the new pointer locations. |
| 3024 | while (!downIdBits.isEmpty()) { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3025 | uint32_t downId = downIdBits.firstMarkedBit(); |
| 3026 | downIdBits.clearBit(downId); |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3027 | dispatchedIdBits.markBit(downId); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3028 | |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3029 | if (dispatchedIdBits.count() == 1) { |
| 3030 | // First pointer is going down. Set down time. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3031 | mDownTime = when; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3032 | } else { |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3033 | // Only send edge flags with first pointer down. |
| 3034 | edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3035 | } |
| 3036 | |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3037 | dispatchMotion(when, policyFlags, mTouchSource, |
| 3038 | AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, edgeFlags, |
| 3039 | mCurrentTouchCoords, mCurrentTouch.idToIndex, dispatchedIdBits, downId, |
| 3040 | xPrecision, yPrecision, mDownTime); |
| 3041 | } |
| 3042 | } |
| 3043 | |
| 3044 | // Update state for next time. |
| 3045 | for (uint32_t i = 0; i < currentPointerCount; i++) { |
| 3046 | mLastTouchCoords[i].copyFrom(mCurrentTouchCoords[i]); |
| 3047 | } |
| 3048 | } |
| 3049 | |
| 3050 | void TouchInputMapper::prepareTouches(int32_t* outEdgeFlags, |
| 3051 | float* outXPrecision, float* outYPrecision) { |
| 3052 | uint32_t currentPointerCount = mCurrentTouch.pointerCount; |
| 3053 | uint32_t lastPointerCount = mLastTouch.pointerCount; |
| 3054 | |
| 3055 | AutoMutex _l(mLock); |
| 3056 | |
| 3057 | // Walk through the the active pointers and map touch screen coordinates (TouchData) into |
| 3058 | // display or surface coordinates (PointerCoords) and adjust for display orientation. |
| 3059 | for (uint32_t i = 0; i < currentPointerCount; i++) { |
| 3060 | const PointerData& in = mCurrentTouch.pointers[i]; |
| 3061 | |
| 3062 | // ToolMajor and ToolMinor |
| 3063 | float toolMajor, toolMinor; |
| 3064 | switch (mCalibration.toolSizeCalibration) { |
| 3065 | case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC: |
| 3066 | toolMajor = in.toolMajor * mLocked.geometricScale; |
| 3067 | if (mRawAxes.toolMinor.valid) { |
| 3068 | toolMinor = in.toolMinor * mLocked.geometricScale; |
| 3069 | } else { |
| 3070 | toolMinor = toolMajor; |
| 3071 | } |
| 3072 | break; |
| 3073 | case Calibration::TOOL_SIZE_CALIBRATION_LINEAR: |
| 3074 | toolMajor = in.toolMajor != 0 |
| 3075 | ? in.toolMajor * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias |
| 3076 | : 0; |
| 3077 | if (mRawAxes.toolMinor.valid) { |
| 3078 | toolMinor = in.toolMinor != 0 |
| 3079 | ? in.toolMinor * mLocked.toolSizeLinearScale |
| 3080 | + mLocked.toolSizeLinearBias |
| 3081 | : 0; |
| 3082 | } else { |
| 3083 | toolMinor = toolMajor; |
| 3084 | } |
| 3085 | break; |
| 3086 | case Calibration::TOOL_SIZE_CALIBRATION_AREA: |
| 3087 | if (in.toolMajor != 0) { |
| 3088 | float diameter = sqrtf(in.toolMajor |
| 3089 | * mLocked.toolSizeAreaScale + mLocked.toolSizeAreaBias); |
| 3090 | toolMajor = diameter * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias; |
| 3091 | } else { |
| 3092 | toolMajor = 0; |
| 3093 | } |
| 3094 | toolMinor = toolMajor; |
| 3095 | break; |
| 3096 | default: |
| 3097 | toolMajor = 0; |
| 3098 | toolMinor = 0; |
| 3099 | break; |
| 3100 | } |
| 3101 | |
| 3102 | if (mCalibration.haveToolSizeIsSummed && mCalibration.toolSizeIsSummed) { |
| 3103 | toolMajor /= currentPointerCount; |
| 3104 | toolMinor /= currentPointerCount; |
| 3105 | } |
| 3106 | |
| 3107 | // Pressure |
| 3108 | float rawPressure; |
| 3109 | switch (mCalibration.pressureSource) { |
| 3110 | case Calibration::PRESSURE_SOURCE_PRESSURE: |
| 3111 | rawPressure = in.pressure; |
| 3112 | break; |
| 3113 | case Calibration::PRESSURE_SOURCE_TOUCH: |
| 3114 | rawPressure = in.touchMajor; |
| 3115 | break; |
| 3116 | default: |
| 3117 | rawPressure = 0; |
| 3118 | } |
| 3119 | |
| 3120 | float pressure; |
| 3121 | switch (mCalibration.pressureCalibration) { |
| 3122 | case Calibration::PRESSURE_CALIBRATION_PHYSICAL: |
| 3123 | case Calibration::PRESSURE_CALIBRATION_AMPLITUDE: |
| 3124 | pressure = rawPressure * mLocked.pressureScale; |
| 3125 | break; |
| 3126 | default: |
| 3127 | pressure = 1; |
| 3128 | break; |
| 3129 | } |
| 3130 | |
| 3131 | // TouchMajor and TouchMinor |
| 3132 | float touchMajor, touchMinor; |
| 3133 | switch (mCalibration.touchSizeCalibration) { |
| 3134 | case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC: |
| 3135 | touchMajor = in.touchMajor * mLocked.geometricScale; |
| 3136 | if (mRawAxes.touchMinor.valid) { |
| 3137 | touchMinor = in.touchMinor * mLocked.geometricScale; |
| 3138 | } else { |
| 3139 | touchMinor = touchMajor; |
| 3140 | } |
| 3141 | break; |
| 3142 | case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE: |
| 3143 | touchMajor = toolMajor * pressure; |
| 3144 | touchMinor = toolMinor * pressure; |
| 3145 | break; |
| 3146 | default: |
| 3147 | touchMajor = 0; |
| 3148 | touchMinor = 0; |
| 3149 | break; |
| 3150 | } |
| 3151 | |
| 3152 | if (touchMajor > toolMajor) { |
| 3153 | touchMajor = toolMajor; |
| 3154 | } |
| 3155 | if (touchMinor > toolMinor) { |
| 3156 | touchMinor = toolMinor; |
| 3157 | } |
| 3158 | |
| 3159 | // Size |
| 3160 | float size; |
| 3161 | switch (mCalibration.sizeCalibration) { |
| 3162 | case Calibration::SIZE_CALIBRATION_NORMALIZED: { |
| 3163 | float rawSize = mRawAxes.toolMinor.valid |
| 3164 | ? avg(in.toolMajor, in.toolMinor) |
| 3165 | : in.toolMajor; |
| 3166 | size = rawSize * mLocked.sizeScale; |
| 3167 | break; |
| 3168 | } |
| 3169 | default: |
| 3170 | size = 0; |
| 3171 | break; |
| 3172 | } |
| 3173 | |
| 3174 | // Orientation |
| 3175 | float orientation; |
| 3176 | switch (mCalibration.orientationCalibration) { |
| 3177 | case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED: |
| 3178 | orientation = in.orientation * mLocked.orientationScale; |
| 3179 | break; |
| 3180 | case Calibration::ORIENTATION_CALIBRATION_VECTOR: { |
| 3181 | int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4); |
| 3182 | int32_t c2 = signExtendNybble(in.orientation & 0x0f); |
| 3183 | if (c1 != 0 || c2 != 0) { |
| 3184 | orientation = atan2f(c1, c2) * 0.5f; |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3185 | float scale = 1.0f + hypotf(c1, c2) / 16.0f; |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3186 | touchMajor *= scale; |
| 3187 | touchMinor /= scale; |
| 3188 | toolMajor *= scale; |
| 3189 | toolMinor /= scale; |
| 3190 | } else { |
| 3191 | orientation = 0; |
| 3192 | } |
| 3193 | break; |
| 3194 | } |
| 3195 | default: |
| 3196 | orientation = 0; |
| 3197 | } |
| 3198 | |
| 3199 | // X and Y |
| 3200 | // Adjust coords for surface orientation. |
| 3201 | float x, y; |
| 3202 | switch (mLocked.surfaceOrientation) { |
| 3203 | case DISPLAY_ORIENTATION_90: |
| 3204 | x = float(in.y - mRawAxes.y.minValue) * mLocked.yScale; |
| 3205 | y = float(mRawAxes.x.maxValue - in.x) * mLocked.xScale; |
| 3206 | orientation -= M_PI_2; |
| 3207 | if (orientation < - M_PI_2) { |
| 3208 | orientation += M_PI; |
| 3209 | } |
| 3210 | break; |
| 3211 | case DISPLAY_ORIENTATION_180: |
| 3212 | x = float(mRawAxes.x.maxValue - in.x) * mLocked.xScale; |
| 3213 | y = float(mRawAxes.y.maxValue - in.y) * mLocked.yScale; |
| 3214 | break; |
| 3215 | case DISPLAY_ORIENTATION_270: |
| 3216 | x = float(mRawAxes.y.maxValue - in.y) * mLocked.yScale; |
| 3217 | y = float(in.x - mRawAxes.x.minValue) * mLocked.xScale; |
| 3218 | orientation += M_PI_2; |
| 3219 | if (orientation > M_PI_2) { |
| 3220 | orientation -= M_PI; |
| 3221 | } |
| 3222 | break; |
| 3223 | default: |
| 3224 | x = float(in.x - mRawAxes.x.minValue) * mLocked.xScale; |
| 3225 | y = float(in.y - mRawAxes.y.minValue) * mLocked.yScale; |
| 3226 | break; |
| 3227 | } |
| 3228 | |
| 3229 | // Write output coords. |
| 3230 | PointerCoords& out = mCurrentTouchCoords[i]; |
| 3231 | out.clear(); |
| 3232 | out.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 3233 | out.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 3234 | out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure); |
| 3235 | out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size); |
| 3236 | out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor); |
| 3237 | out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor); |
| 3238 | out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor); |
| 3239 | out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor); |
| 3240 | out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation); |
| 3241 | } |
| 3242 | |
| 3243 | // Check edge flags by looking only at the first pointer since the flags are |
| 3244 | // global to the event. |
| 3245 | *outEdgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 3246 | if (lastPointerCount == 0 && currentPointerCount > 0) { |
| 3247 | const PointerData& in = mCurrentTouch.pointers[0]; |
| 3248 | |
| 3249 | if (in.x <= mRawAxes.x.minValue) { |
| 3250 | *outEdgeFlags |= rotateEdgeFlag(AMOTION_EVENT_EDGE_FLAG_LEFT, |
| 3251 | mLocked.surfaceOrientation); |
| 3252 | } else if (in.x >= mRawAxes.x.maxValue) { |
| 3253 | *outEdgeFlags |= rotateEdgeFlag(AMOTION_EVENT_EDGE_FLAG_RIGHT, |
| 3254 | mLocked.surfaceOrientation); |
| 3255 | } |
| 3256 | if (in.y <= mRawAxes.y.minValue) { |
| 3257 | *outEdgeFlags |= rotateEdgeFlag(AMOTION_EVENT_EDGE_FLAG_TOP, |
| 3258 | mLocked.surfaceOrientation); |
| 3259 | } else if (in.y >= mRawAxes.y.maxValue) { |
| 3260 | *outEdgeFlags |= rotateEdgeFlag(AMOTION_EVENT_EDGE_FLAG_BOTTOM, |
| 3261 | mLocked.surfaceOrientation); |
| 3262 | } |
| 3263 | } |
| 3264 | |
| 3265 | *outXPrecision = mLocked.orientedXPrecision; |
| 3266 | *outYPrecision = mLocked.orientedYPrecision; |
| 3267 | } |
| 3268 | |
Jeff Brown | 325bd07 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3269 | void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, |
| 3270 | bool isTimeout) { |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3271 | // Update current gesture coordinates. |
| 3272 | bool cancelPreviousGesture, finishPreviousGesture; |
Jeff Brown | 325bd07 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3273 | bool sendEvents = preparePointerGestures(when, |
| 3274 | &cancelPreviousGesture, &finishPreviousGesture, isTimeout); |
| 3275 | if (!sendEvents) { |
| 3276 | return; |
| 3277 | } |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame^] | 3278 | if (finishPreviousGesture) { |
| 3279 | cancelPreviousGesture = false; |
| 3280 | } |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3281 | |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 3282 | // Switch pointer presentation. |
| 3283 | mPointerController->setPresentation( |
| 3284 | mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS |
| 3285 | ? PointerControllerInterface::PRESENTATION_SPOT |
| 3286 | : PointerControllerInterface::PRESENTATION_POINTER); |
| 3287 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 3288 | // Show or hide the pointer if needed. |
| 3289 | switch (mPointerGesture.currentGestureMode) { |
| 3290 | case PointerGesture::NEUTRAL: |
| 3291 | case PointerGesture::QUIET: |
| 3292 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS |
| 3293 | && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE |
| 3294 | || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) { |
| 3295 | // Remind the user of where the pointer is after finishing a gesture with spots. |
| 3296 | mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 3297 | } |
| 3298 | break; |
| 3299 | case PointerGesture::TAP: |
| 3300 | case PointerGesture::TAP_DRAG: |
| 3301 | case PointerGesture::BUTTON_CLICK_OR_DRAG: |
| 3302 | case PointerGesture::HOVER: |
| 3303 | case PointerGesture::PRESS: |
| 3304 | // Unfade the pointer when the current gesture manipulates the |
| 3305 | // area directly under the pointer. |
| 3306 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
| 3307 | break; |
| 3308 | case PointerGesture::SWIPE: |
| 3309 | case PointerGesture::FREEFORM: |
| 3310 | // Fade the pointer when the current gesture manipulates a different |
| 3311 | // area and there are spots to guide the user experience. |
| 3312 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) { |
| 3313 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 3314 | } else { |
| 3315 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
| 3316 | } |
| 3317 | break; |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3318 | } |
| 3319 | |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3320 | // Send events! |
| 3321 | uint32_t metaState = getContext()->getGlobalMetaState(); |
| 3322 | |
| 3323 | // Update last coordinates of pointers that have moved so that we observe the new |
| 3324 | // pointer positions at the same time as other pointers that have just gone up. |
Jeff Brown | 325bd07 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3325 | bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP |
| 3326 | || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG |
| 3327 | || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3328 | || mPointerGesture.currentGestureMode == PointerGesture::PRESS |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3329 | || mPointerGesture.currentGestureMode == PointerGesture::SWIPE |
| 3330 | || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM; |
| 3331 | bool moveNeeded = false; |
| 3332 | if (down && !cancelPreviousGesture && !finishPreviousGesture |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3333 | && !mPointerGesture.lastGestureIdBits.isEmpty() |
| 3334 | && !mPointerGesture.currentGestureIdBits.isEmpty()) { |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3335 | BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value |
| 3336 | & mPointerGesture.lastGestureIdBits.value); |
| 3337 | moveNeeded = updateMovedPointerCoords( |
| 3338 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
| 3339 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 3340 | movedGestureIdBits); |
| 3341 | } |
| 3342 | |
| 3343 | // Send motion events for all pointers that went up or were canceled. |
| 3344 | BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits); |
| 3345 | if (!dispatchedGestureIdBits.isEmpty()) { |
| 3346 | if (cancelPreviousGesture) { |
| 3347 | dispatchMotion(when, policyFlags, mPointerSource, |
| 3348 | AMOTION_EVENT_ACTION_CANCEL, 0, metaState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 3349 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 3350 | dispatchedGestureIdBits, -1, |
| 3351 | 0, 0, mPointerGesture.downTime); |
| 3352 | |
| 3353 | dispatchedGestureIdBits.clear(); |
| 3354 | } else { |
| 3355 | BitSet32 upGestureIdBits; |
| 3356 | if (finishPreviousGesture) { |
| 3357 | upGestureIdBits = dispatchedGestureIdBits; |
| 3358 | } else { |
| 3359 | upGestureIdBits.value = dispatchedGestureIdBits.value |
| 3360 | & ~mPointerGesture.currentGestureIdBits.value; |
| 3361 | } |
| 3362 | while (!upGestureIdBits.isEmpty()) { |
| 3363 | uint32_t id = upGestureIdBits.firstMarkedBit(); |
| 3364 | upGestureIdBits.clearBit(id); |
| 3365 | |
| 3366 | dispatchMotion(when, policyFlags, mPointerSource, |
| 3367 | AMOTION_EVENT_ACTION_POINTER_UP, 0, |
| 3368 | metaState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 3369 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 3370 | dispatchedGestureIdBits, id, |
| 3371 | 0, 0, mPointerGesture.downTime); |
| 3372 | |
| 3373 | dispatchedGestureIdBits.clearBit(id); |
| 3374 | } |
| 3375 | } |
| 3376 | } |
| 3377 | |
| 3378 | // Send motion events for all pointers that moved. |
| 3379 | if (moveNeeded) { |
| 3380 | dispatchMotion(when, policyFlags, mPointerSource, |
| 3381 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 3382 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
| 3383 | dispatchedGestureIdBits, -1, |
| 3384 | 0, 0, mPointerGesture.downTime); |
| 3385 | } |
| 3386 | |
| 3387 | // Send motion events for all pointers that went down. |
| 3388 | if (down) { |
| 3389 | BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value |
| 3390 | & ~dispatchedGestureIdBits.value); |
| 3391 | while (!downGestureIdBits.isEmpty()) { |
| 3392 | uint32_t id = downGestureIdBits.firstMarkedBit(); |
| 3393 | downGestureIdBits.clearBit(id); |
| 3394 | dispatchedGestureIdBits.markBit(id); |
| 3395 | |
| 3396 | int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 3397 | if (dispatchedGestureIdBits.count() == 1) { |
| 3398 | // First pointer is going down. Calculate edge flags and set down time. |
| 3399 | uint32_t index = mPointerGesture.currentGestureIdToIndex[id]; |
| 3400 | const PointerCoords& downCoords = mPointerGesture.currentGestureCoords[index]; |
| 3401 | edgeFlags = calculateEdgeFlagsUsingPointerBounds(mPointerController, |
| 3402 | downCoords.getAxisValue(AMOTION_EVENT_AXIS_X), |
| 3403 | downCoords.getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 3404 | mPointerGesture.downTime = when; |
| 3405 | } |
| 3406 | |
| 3407 | dispatchMotion(when, policyFlags, mPointerSource, |
| 3408 | AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, edgeFlags, |
| 3409 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
| 3410 | dispatchedGestureIdBits, id, |
| 3411 | 0, 0, mPointerGesture.downTime); |
| 3412 | } |
| 3413 | } |
| 3414 | |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3415 | // Send motion events for hover. |
| 3416 | if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) { |
| 3417 | dispatchMotion(when, policyFlags, mPointerSource, |
| 3418 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 3419 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
| 3420 | mPointerGesture.currentGestureIdBits, -1, |
| 3421 | 0, 0, mPointerGesture.downTime); |
| 3422 | } |
| 3423 | |
| 3424 | // Update state. |
| 3425 | mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode; |
| 3426 | if (!down) { |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3427 | mPointerGesture.lastGestureIdBits.clear(); |
| 3428 | } else { |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3429 | mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits; |
| 3430 | for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) { |
| 3431 | uint32_t id = idBits.firstMarkedBit(); |
| 3432 | idBits.clearBit(id); |
| 3433 | uint32_t index = mPointerGesture.currentGestureIdToIndex[id]; |
| 3434 | mPointerGesture.lastGestureCoords[index].copyFrom( |
| 3435 | mPointerGesture.currentGestureCoords[index]); |
| 3436 | mPointerGesture.lastGestureIdToIndex[id] = index; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3437 | } |
| 3438 | } |
| 3439 | } |
| 3440 | |
Jeff Brown | 325bd07 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3441 | bool TouchInputMapper::preparePointerGestures(nsecs_t when, |
| 3442 | bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) { |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3443 | *outCancelPreviousGesture = false; |
| 3444 | *outFinishPreviousGesture = false; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3445 | |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3446 | AutoMutex _l(mLock); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3447 | |
Jeff Brown | 325bd07 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3448 | // Handle TAP timeout. |
| 3449 | if (isTimeout) { |
| 3450 | #if DEBUG_GESTURES |
| 3451 | LOGD("Gestures: Processing timeout"); |
| 3452 | #endif |
| 3453 | |
| 3454 | if (mPointerGesture.lastGestureMode == PointerGesture::TAP) { |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 3455 | if (when <= mPointerGesture.tapUpTime + mConfig->pointerGestureTapDragInterval) { |
Jeff Brown | 325bd07 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3456 | // The tap/drag timeout has not yet expired. |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 3457 | getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime |
| 3458 | + mConfig->pointerGestureTapDragInterval); |
Jeff Brown | 325bd07 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3459 | } else { |
| 3460 | // The tap is finished. |
| 3461 | #if DEBUG_GESTURES |
| 3462 | LOGD("Gestures: TAP finished"); |
| 3463 | #endif |
| 3464 | *outFinishPreviousGesture = true; |
| 3465 | |
| 3466 | mPointerGesture.activeGestureId = -1; |
| 3467 | mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL; |
| 3468 | mPointerGesture.currentGestureIdBits.clear(); |
| 3469 | |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame^] | 3470 | mPointerGesture.pointerVelocityControl.reset(); |
| 3471 | |
Jeff Brown | 325bd07 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3472 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) { |
| 3473 | mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_NEUTRAL; |
| 3474 | mPointerGesture.spotIdBits.clear(); |
| 3475 | moveSpotsLocked(); |
| 3476 | } |
| 3477 | return true; |
| 3478 | } |
| 3479 | } |
| 3480 | |
| 3481 | // We did not handle this timeout. |
| 3482 | return false; |
| 3483 | } |
| 3484 | |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3485 | // Update the velocity tracker. |
| 3486 | { |
| 3487 | VelocityTracker::Position positions[MAX_POINTERS]; |
| 3488 | uint32_t count = 0; |
| 3489 | for (BitSet32 idBits(mCurrentTouch.idBits); !idBits.isEmpty(); count++) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3490 | uint32_t id = idBits.firstMarkedBit(); |
| 3491 | idBits.clearBit(id); |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3492 | uint32_t index = mCurrentTouch.idToIndex[id]; |
| 3493 | positions[count].x = mCurrentTouch.pointers[index].x |
| 3494 | * mLocked.pointerGestureXMovementScale; |
| 3495 | positions[count].y = mCurrentTouch.pointers[index].y |
| 3496 | * mLocked.pointerGestureYMovementScale; |
| 3497 | } |
| 3498 | mPointerGesture.velocityTracker.addMovement(when, mCurrentTouch.idBits, positions); |
| 3499 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3500 | |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3501 | // Pick a new active touch id if needed. |
| 3502 | // Choose an arbitrary pointer that just went down, if there is one. |
| 3503 | // Otherwise choose an arbitrary remaining pointer. |
| 3504 | // This guarantees we always have an active touch id when there is at least one pointer. |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3505 | // We keep the same active touch id for as long as possible. |
| 3506 | bool activeTouchChanged = false; |
| 3507 | int32_t lastActiveTouchId = mPointerGesture.activeTouchId; |
| 3508 | int32_t activeTouchId = lastActiveTouchId; |
| 3509 | if (activeTouchId < 0) { |
| 3510 | if (!mCurrentTouch.idBits.isEmpty()) { |
| 3511 | activeTouchChanged = true; |
| 3512 | activeTouchId = mPointerGesture.activeTouchId = mCurrentTouch.idBits.firstMarkedBit(); |
| 3513 | mPointerGesture.firstTouchTime = when; |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3514 | } |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3515 | } else if (!mCurrentTouch.idBits.hasBit(activeTouchId)) { |
| 3516 | activeTouchChanged = true; |
| 3517 | if (!mCurrentTouch.idBits.isEmpty()) { |
| 3518 | activeTouchId = mPointerGesture.activeTouchId = mCurrentTouch.idBits.firstMarkedBit(); |
| 3519 | } else { |
| 3520 | activeTouchId = mPointerGesture.activeTouchId = -1; |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3521 | } |
| 3522 | } |
| 3523 | |
| 3524 | // Determine whether we are in quiet time. |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3525 | bool isQuietTime = false; |
| 3526 | if (activeTouchId < 0) { |
| 3527 | mPointerGesture.resetQuietTime(); |
| 3528 | } else { |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 3529 | isQuietTime = when < mPointerGesture.quietTime + mConfig->pointerGestureQuietInterval; |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3530 | if (!isQuietTime) { |
| 3531 | if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS |
| 3532 | || mPointerGesture.lastGestureMode == PointerGesture::SWIPE |
| 3533 | || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM) |
| 3534 | && mCurrentTouch.pointerCount < 2) { |
| 3535 | // Enter quiet time when exiting swipe or freeform state. |
| 3536 | // This is to prevent accidentally entering the hover state and flinging the |
| 3537 | // pointer when finishing a swipe and there is still one pointer left onscreen. |
| 3538 | isQuietTime = true; |
Jeff Brown | 325bd07 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3539 | } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3540 | && mCurrentTouch.pointerCount >= 2 |
| 3541 | && !isPointerDown(mCurrentTouch.buttonState)) { |
| 3542 | // Enter quiet time when releasing the button and there are still two or more |
| 3543 | // fingers down. This may indicate that one finger was used to press the button |
| 3544 | // but it has not gone up yet. |
| 3545 | isQuietTime = true; |
| 3546 | } |
| 3547 | if (isQuietTime) { |
| 3548 | mPointerGesture.quietTime = when; |
| 3549 | } |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3550 | } |
| 3551 | } |
| 3552 | |
| 3553 | // Switch states based on button and pointer state. |
| 3554 | if (isQuietTime) { |
| 3555 | // Case 1: Quiet time. (QUIET) |
| 3556 | #if DEBUG_GESTURES |
| 3557 | LOGD("Gestures: QUIET for next %0.3fms", |
| 3558 | (mPointerGesture.quietTime + QUIET_INTERVAL - when) * 0.000001f); |
| 3559 | #endif |
| 3560 | *outFinishPreviousGesture = true; |
| 3561 | |
| 3562 | mPointerGesture.activeGestureId = -1; |
| 3563 | mPointerGesture.currentGestureMode = PointerGesture::QUIET; |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3564 | mPointerGesture.currentGestureIdBits.clear(); |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3565 | |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame^] | 3566 | mPointerGesture.pointerVelocityControl.reset(); |
| 3567 | |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3568 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) { |
| 3569 | mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_NEUTRAL; |
| 3570 | mPointerGesture.spotIdBits.clear(); |
| 3571 | moveSpotsLocked(); |
| 3572 | } |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3573 | } else if (isPointerDown(mCurrentTouch.buttonState)) { |
Jeff Brown | 325bd07 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3574 | // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG) |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3575 | // The pointer follows the active touch point. |
| 3576 | // Emit DOWN, MOVE, UP events at the pointer location. |
| 3577 | // |
| 3578 | // Only the active touch matters; other fingers are ignored. This policy helps |
| 3579 | // to handle the case where the user places a second finger on the touch pad |
| 3580 | // to apply the necessary force to depress an integrated button below the surface. |
| 3581 | // We don't want the second finger to be delivered to applications. |
| 3582 | // |
| 3583 | // For this to work well, we need to make sure to track the pointer that is really |
| 3584 | // active. If the user first puts one finger down to click then adds another |
| 3585 | // finger to drag then the active pointer should switch to the finger that is |
| 3586 | // being dragged. |
| 3587 | #if DEBUG_GESTURES |
Jeff Brown | 325bd07 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3588 | LOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, " |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3589 | "currentTouchPointerCount=%d", activeTouchId, mCurrentTouch.pointerCount); |
| 3590 | #endif |
| 3591 | // Reset state when just starting. |
Jeff Brown | 325bd07 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3592 | if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) { |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3593 | *outFinishPreviousGesture = true; |
| 3594 | mPointerGesture.activeGestureId = 0; |
| 3595 | } |
| 3596 | |
| 3597 | // Switch pointers if needed. |
| 3598 | // Find the fastest pointer and follow it. |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame^] | 3599 | if (activeTouchId >= 0 && mCurrentTouch.pointerCount > 1) { |
| 3600 | int32_t bestId = -1; |
| 3601 | float bestSpeed = mConfig->pointerGestureDragMinSwitchSpeed; |
| 3602 | for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) { |
| 3603 | uint32_t id = mCurrentTouch.pointers[i].id; |
| 3604 | float vx, vy; |
| 3605 | if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) { |
| 3606 | float speed = hypotf(vx, vy); |
| 3607 | if (speed > bestSpeed) { |
| 3608 | bestId = id; |
| 3609 | bestSpeed = speed; |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3610 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3611 | } |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame^] | 3612 | } |
| 3613 | if (bestId >= 0 && bestId != activeTouchId) { |
| 3614 | mPointerGesture.activeTouchId = activeTouchId = bestId; |
| 3615 | activeTouchChanged = true; |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3616 | #if DEBUG_GESTURES |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame^] | 3617 | LOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, " |
| 3618 | "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed); |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3619 | #endif |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3620 | } |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame^] | 3621 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3622 | |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame^] | 3623 | if (activeTouchId >= 0 && mLastTouch.idBits.hasBit(activeTouchId)) { |
| 3624 | const PointerData& currentPointer = |
| 3625 | mCurrentTouch.pointers[mCurrentTouch.idToIndex[activeTouchId]]; |
| 3626 | const PointerData& lastPointer = |
| 3627 | mLastTouch.pointers[mLastTouch.idToIndex[activeTouchId]]; |
| 3628 | float deltaX = (currentPointer.x - lastPointer.x) |
| 3629 | * mLocked.pointerGestureXMovementScale; |
| 3630 | float deltaY = (currentPointer.y - lastPointer.y) |
| 3631 | * mLocked.pointerGestureYMovementScale; |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3632 | |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame^] | 3633 | mPointerGesture.pointerVelocityControl.move(when, &deltaX, &deltaY); |
| 3634 | |
| 3635 | // Move the pointer using a relative motion. |
| 3636 | // When using spots, the click will occur at the position of the anchor |
| 3637 | // spot and all other spots will move there. |
| 3638 | mPointerController->move(deltaX, deltaY); |
| 3639 | } else { |
| 3640 | mPointerGesture.pointerVelocityControl.reset(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3641 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3642 | |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3643 | float x, y; |
| 3644 | mPointerController->getPosition(&x, &y); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 3645 | |
Jeff Brown | 325bd07 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3646 | mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG; |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3647 | mPointerGesture.currentGestureIdBits.clear(); |
| 3648 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 3649 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; |
| 3650 | mPointerGesture.currentGestureCoords[0].clear(); |
| 3651 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 3652 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 3653 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3654 | |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3655 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) { |
| 3656 | if (activeTouchId >= 0) { |
| 3657 | // Collapse all spots into one point at the pointer location. |
| 3658 | mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_BUTTON_DRAG; |
| 3659 | mPointerGesture.spotIdBits.clear(); |
| 3660 | for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) { |
| 3661 | uint32_t id = mCurrentTouch.pointers[i].id; |
| 3662 | mPointerGesture.spotIdBits.markBit(id); |
| 3663 | mPointerGesture.spotIdToIndex[id] = i; |
| 3664 | mPointerGesture.spotCoords[i] = mPointerGesture.currentGestureCoords[0]; |
| 3665 | } |
| 3666 | } else { |
| 3667 | // No fingers. Generate a spot at the pointer location so the |
| 3668 | // anchor appears to be pressed. |
| 3669 | mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_BUTTON_CLICK; |
| 3670 | mPointerGesture.spotIdBits.clear(); |
| 3671 | mPointerGesture.spotIdBits.markBit(0); |
| 3672 | mPointerGesture.spotIdToIndex[0] = 0; |
| 3673 | mPointerGesture.spotCoords[0] = mPointerGesture.currentGestureCoords[0]; |
| 3674 | } |
| 3675 | moveSpotsLocked(); |
| 3676 | } |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3677 | } else if (mCurrentTouch.pointerCount == 0) { |
| 3678 | // Case 3. No fingers down and button is not pressed. (NEUTRAL) |
| 3679 | *outFinishPreviousGesture = true; |
| 3680 | |
Jeff Brown | 325bd07 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3681 | // Watch for taps coming out of HOVER or TAP_DRAG mode. |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 3682 | // Checking for taps after TAP_DRAG allows us to detect double-taps. |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3683 | bool tapped = false; |
Jeff Brown | 325bd07 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3684 | if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER |
| 3685 | || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3686 | && mLastTouch.pointerCount == 1) { |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 3687 | if (when <= mPointerGesture.tapDownTime + mConfig->pointerGestureTapInterval) { |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3688 | float x, y; |
| 3689 | mPointerController->getPosition(&x, &y); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 3690 | if (fabs(x - mPointerGesture.tapX) <= mConfig->pointerGestureTapSlop |
| 3691 | && fabs(y - mPointerGesture.tapY) <= mConfig->pointerGestureTapSlop) { |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3692 | #if DEBUG_GESTURES |
| 3693 | LOGD("Gestures: TAP"); |
| 3694 | #endif |
Jeff Brown | 325bd07 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3695 | |
| 3696 | mPointerGesture.tapUpTime = when; |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 3697 | getContext()->requestTimeoutAtTime(when |
| 3698 | + mConfig->pointerGestureTapDragInterval); |
Jeff Brown | 325bd07 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3699 | |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3700 | mPointerGesture.activeGestureId = 0; |
| 3701 | mPointerGesture.currentGestureMode = PointerGesture::TAP; |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3702 | mPointerGesture.currentGestureIdBits.clear(); |
| 3703 | mPointerGesture.currentGestureIdBits.markBit( |
| 3704 | mPointerGesture.activeGestureId); |
| 3705 | mPointerGesture.currentGestureIdToIndex[ |
| 3706 | mPointerGesture.activeGestureId] = 0; |
| 3707 | mPointerGesture.currentGestureCoords[0].clear(); |
| 3708 | mPointerGesture.currentGestureCoords[0].setAxisValue( |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3709 | AMOTION_EVENT_AXIS_X, mPointerGesture.tapX); |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3710 | mPointerGesture.currentGestureCoords[0].setAxisValue( |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3711 | AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY); |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3712 | mPointerGesture.currentGestureCoords[0].setAxisValue( |
| 3713 | AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3714 | |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3715 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) { |
| 3716 | mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_TAP; |
| 3717 | mPointerGesture.spotIdBits.clear(); |
| 3718 | mPointerGesture.spotIdBits.markBit(lastActiveTouchId); |
| 3719 | mPointerGesture.spotIdToIndex[lastActiveTouchId] = 0; |
| 3720 | mPointerGesture.spotCoords[0] = mPointerGesture.currentGestureCoords[0]; |
| 3721 | moveSpotsLocked(); |
| 3722 | } |
| 3723 | |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3724 | tapped = true; |
| 3725 | } else { |
| 3726 | #if DEBUG_GESTURES |
| 3727 | LOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f", |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3728 | x - mPointerGesture.tapX, |
| 3729 | y - mPointerGesture.tapY); |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3730 | #endif |
| 3731 | } |
| 3732 | } else { |
| 3733 | #if DEBUG_GESTURES |
Jeff Brown | 325bd07 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3734 | LOGD("Gestures: Not a TAP, %0.3fms since down", |
| 3735 | (when - mPointerGesture.tapDownTime) * 0.000001f); |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3736 | #endif |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3737 | } |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3738 | } |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3739 | |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame^] | 3740 | mPointerGesture.pointerVelocityControl.reset(); |
| 3741 | |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3742 | if (!tapped) { |
| 3743 | #if DEBUG_GESTURES |
| 3744 | LOGD("Gestures: NEUTRAL"); |
| 3745 | #endif |
| 3746 | mPointerGesture.activeGestureId = -1; |
| 3747 | mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL; |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3748 | mPointerGesture.currentGestureIdBits.clear(); |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3749 | |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3750 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) { |
| 3751 | mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_NEUTRAL; |
| 3752 | mPointerGesture.spotIdBits.clear(); |
| 3753 | moveSpotsLocked(); |
| 3754 | } |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3755 | } |
| 3756 | } else if (mCurrentTouch.pointerCount == 1) { |
Jeff Brown | 325bd07 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3757 | // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG) |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3758 | // The pointer follows the active touch point. |
Jeff Brown | 325bd07 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3759 | // When in HOVER, emit HOVER_MOVE events at the pointer location. |
| 3760 | // When in TAP_DRAG, emit MOVE events at the pointer location. |
| 3761 | LOG_ASSERT(activeTouchId >= 0); |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3762 | |
Jeff Brown | 325bd07 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3763 | mPointerGesture.currentGestureMode = PointerGesture::HOVER; |
| 3764 | if (mPointerGesture.lastGestureMode == PointerGesture::TAP) { |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 3765 | if (when <= mPointerGesture.tapUpTime + mConfig->pointerGestureTapDragInterval) { |
Jeff Brown | 325bd07 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3766 | float x, y; |
| 3767 | mPointerController->getPosition(&x, &y); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 3768 | if (fabs(x - mPointerGesture.tapX) <= mConfig->pointerGestureTapSlop |
| 3769 | && fabs(y - mPointerGesture.tapY) <= mConfig->pointerGestureTapSlop) { |
Jeff Brown | 325bd07 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3770 | mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG; |
| 3771 | } else { |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3772 | #if DEBUG_GESTURES |
Jeff Brown | 325bd07 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3773 | LOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f", |
| 3774 | x - mPointerGesture.tapX, |
| 3775 | y - mPointerGesture.tapY); |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3776 | #endif |
Jeff Brown | 325bd07 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3777 | } |
| 3778 | } else { |
| 3779 | #if DEBUG_GESTURES |
| 3780 | LOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up", |
| 3781 | (when - mPointerGesture.tapUpTime) * 0.000001f); |
| 3782 | #endif |
| 3783 | } |
| 3784 | } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) { |
| 3785 | mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG; |
| 3786 | } |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3787 | |
| 3788 | if (mLastTouch.idBits.hasBit(activeTouchId)) { |
| 3789 | const PointerData& currentPointer = |
| 3790 | mCurrentTouch.pointers[mCurrentTouch.idToIndex[activeTouchId]]; |
| 3791 | const PointerData& lastPointer = |
| 3792 | mLastTouch.pointers[mLastTouch.idToIndex[activeTouchId]]; |
| 3793 | float deltaX = (currentPointer.x - lastPointer.x) |
| 3794 | * mLocked.pointerGestureXMovementScale; |
| 3795 | float deltaY = (currentPointer.y - lastPointer.y) |
| 3796 | * mLocked.pointerGestureYMovementScale; |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3797 | |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame^] | 3798 | mPointerGesture.pointerVelocityControl.move(when, &deltaX, &deltaY); |
| 3799 | |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3800 | // Move the pointer using a relative motion. |
Jeff Brown | 325bd07 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3801 | // When using spots, the hover or drag will occur at the position of the anchor spot. |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3802 | mPointerController->move(deltaX, deltaY); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame^] | 3803 | } else { |
| 3804 | mPointerGesture.pointerVelocityControl.reset(); |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3805 | } |
| 3806 | |
Jeff Brown | 325bd07 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3807 | bool down; |
| 3808 | if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) { |
| 3809 | #if DEBUG_GESTURES |
| 3810 | LOGD("Gestures: TAP_DRAG"); |
| 3811 | #endif |
| 3812 | down = true; |
| 3813 | } else { |
| 3814 | #if DEBUG_GESTURES |
| 3815 | LOGD("Gestures: HOVER"); |
| 3816 | #endif |
| 3817 | *outFinishPreviousGesture = true; |
| 3818 | mPointerGesture.activeGestureId = 0; |
| 3819 | down = false; |
| 3820 | } |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3821 | |
| 3822 | float x, y; |
| 3823 | mPointerController->getPosition(&x, &y); |
| 3824 | |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3825 | mPointerGesture.currentGestureIdBits.clear(); |
| 3826 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 3827 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; |
| 3828 | mPointerGesture.currentGestureCoords[0].clear(); |
| 3829 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 3830 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
Jeff Brown | 325bd07 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3831 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, |
| 3832 | down ? 1.0f : 0.0f); |
| 3833 | |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3834 | if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount != 0) { |
Jeff Brown | 325bd07 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3835 | mPointerGesture.resetTap(); |
| 3836 | mPointerGesture.tapDownTime = when; |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3837 | mPointerGesture.tapX = x; |
| 3838 | mPointerGesture.tapY = y; |
| 3839 | } |
| 3840 | |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3841 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) { |
Jeff Brown | 325bd07 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3842 | mPointerGesture.spotGesture = down ? PointerControllerInterface::SPOT_GESTURE_DRAG |
| 3843 | : PointerControllerInterface::SPOT_GESTURE_HOVER; |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3844 | mPointerGesture.spotIdBits.clear(); |
| 3845 | mPointerGesture.spotIdBits.markBit(activeTouchId); |
| 3846 | mPointerGesture.spotIdToIndex[activeTouchId] = 0; |
| 3847 | mPointerGesture.spotCoords[0] = mPointerGesture.currentGestureCoords[0]; |
| 3848 | moveSpotsLocked(); |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3849 | } |
| 3850 | } else { |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3851 | // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM) |
| 3852 | // We need to provide feedback for each finger that goes down so we cannot wait |
| 3853 | // for the fingers to move before deciding what to do. |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3854 | // |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3855 | // The ambiguous case is deciding what to do when there are two fingers down but they |
| 3856 | // have not moved enough to determine whether they are part of a drag or part of a |
| 3857 | // freeform gesture, or just a press or long-press at the pointer location. |
| 3858 | // |
| 3859 | // When there are two fingers we start with the PRESS hypothesis and we generate a |
| 3860 | // down at the pointer location. |
| 3861 | // |
| 3862 | // When the two fingers move enough or when additional fingers are added, we make |
| 3863 | // a decision to transition into SWIPE or FREEFORM mode accordingly. |
| 3864 | LOG_ASSERT(activeTouchId >= 0); |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3865 | |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 3866 | bool settled = when >= mPointerGesture.firstTouchTime |
| 3867 | + mConfig->pointerGestureMultitouchSettleInterval; |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3868 | if (mPointerGesture.lastGestureMode != PointerGesture::PRESS |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3869 | && mPointerGesture.lastGestureMode != PointerGesture::SWIPE |
| 3870 | && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) { |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3871 | *outFinishPreviousGesture = true; |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame^] | 3872 | } else if (!settled && mCurrentTouch.pointerCount > mLastTouch.pointerCount) { |
| 3873 | // Additional pointers have gone down but not yet settled. |
| 3874 | // Reset the gesture. |
| 3875 | #if DEBUG_GESTURES |
| 3876 | LOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, " |
| 3877 | "settle time remaining %0.3fms", |
| 3878 | (mPointerGesture.firstTouchTime + MULTITOUCH_SETTLE_INTERVAL - when) |
| 3879 | * 0.000001f); |
| 3880 | #endif |
| 3881 | *outCancelPreviousGesture = true; |
| 3882 | } else { |
| 3883 | // Continue previous gesture. |
| 3884 | mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode; |
| 3885 | } |
| 3886 | |
| 3887 | if (*outFinishPreviousGesture || *outCancelPreviousGesture) { |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3888 | mPointerGesture.currentGestureMode = PointerGesture::PRESS; |
| 3889 | mPointerGesture.activeGestureId = 0; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 3890 | mPointerGesture.referenceIdBits.clear(); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame^] | 3891 | mPointerGesture.pointerVelocityControl.reset(); |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3892 | |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3893 | if (settled && mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS |
| 3894 | && mLastTouch.idBits.hasBit(mPointerGesture.activeTouchId)) { |
| 3895 | // The spot is already visible and has settled, use it as the reference point |
| 3896 | // for the gesture. Other spots will be positioned relative to this one. |
| 3897 | #if DEBUG_GESTURES |
| 3898 | LOGD("Gestures: Using active spot as reference for MULTITOUCH, " |
| 3899 | "settle time expired %0.3fms ago", |
| 3900 | (when - mPointerGesture.firstTouchTime - MULTITOUCH_SETTLE_INTERVAL) |
| 3901 | * 0.000001f); |
| 3902 | #endif |
| 3903 | const PointerData& d = mLastTouch.pointers[mLastTouch.idToIndex[ |
| 3904 | mPointerGesture.activeTouchId]]; |
| 3905 | mPointerGesture.referenceTouchX = d.x; |
| 3906 | mPointerGesture.referenceTouchY = d.y; |
| 3907 | const PointerCoords& c = mPointerGesture.spotCoords[mPointerGesture.spotIdToIndex[ |
| 3908 | mPointerGesture.activeTouchId]]; |
| 3909 | mPointerGesture.referenceGestureX = c.getAxisValue(AMOTION_EVENT_AXIS_X); |
| 3910 | mPointerGesture.referenceGestureY = c.getAxisValue(AMOTION_EVENT_AXIS_Y); |
| 3911 | } else { |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame^] | 3912 | // Use the centroid and pointer location as the reference points for the gesture. |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3913 | #if DEBUG_GESTURES |
| 3914 | LOGD("Gestures: Using centroid as reference for MULTITOUCH, " |
| 3915 | "settle time remaining %0.3fms", |
| 3916 | (mPointerGesture.firstTouchTime + MULTITOUCH_SETTLE_INTERVAL - when) |
| 3917 | * 0.000001f); |
| 3918 | #endif |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame^] | 3919 | mCurrentTouch.getCentroid(&mPointerGesture.referenceTouchX, |
| 3920 | &mPointerGesture.referenceTouchY); |
| 3921 | mPointerController->getPosition(&mPointerGesture.referenceGestureX, |
| 3922 | &mPointerGesture.referenceGestureY); |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3923 | } |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3924 | } |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3925 | |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3926 | if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) { |
| 3927 | float d; |
| 3928 | if (mCurrentTouch.pointerCount > 2) { |
| 3929 | // There are more than two pointers, switch to FREEFORM. |
| 3930 | #if DEBUG_GESTURES |
| 3931 | LOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2", |
| 3932 | mCurrentTouch.pointerCount); |
| 3933 | #endif |
| 3934 | *outCancelPreviousGesture = true; |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3935 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3936 | } else if (((d = distance( |
| 3937 | mCurrentTouch.pointers[0].x, mCurrentTouch.pointers[0].y, |
| 3938 | mCurrentTouch.pointers[1].x, mCurrentTouch.pointers[1].y)) |
| 3939 | > mLocked.pointerGestureMaxSwipeWidth)) { |
| 3940 | // There are two pointers but they are too far apart, switch to FREEFORM. |
| 3941 | #if DEBUG_GESTURES |
| 3942 | LOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f", |
| 3943 | d, mLocked.pointerGestureMaxSwipeWidth); |
| 3944 | #endif |
| 3945 | *outCancelPreviousGesture = true; |
| 3946 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
| 3947 | } else { |
| 3948 | // There are two pointers. Wait for both pointers to start moving |
| 3949 | // before deciding whether this is a SWIPE or FREEFORM gesture. |
| 3950 | uint32_t id1 = mCurrentTouch.pointers[0].id; |
| 3951 | uint32_t id2 = mCurrentTouch.pointers[1].id; |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3952 | |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3953 | float vx1, vy1, vx2, vy2; |
| 3954 | mPointerGesture.velocityTracker.getVelocity(id1, &vx1, &vy1); |
| 3955 | mPointerGesture.velocityTracker.getVelocity(id2, &vx2, &vy2); |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3956 | |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3957 | float speed1 = hypotf(vx1, vy1); |
| 3958 | float speed2 = hypotf(vx2, vy2); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 3959 | if (speed1 >= mConfig->pointerGestureMultitouchMinSpeed |
| 3960 | && speed2 >= mConfig->pointerGestureMultitouchMinSpeed) { |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3961 | // Calculate the dot product of the velocity vectors. |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3962 | // When the vectors are oriented in approximately the same direction, |
| 3963 | // the angle betweeen them is near zero and the cosine of the angle |
| 3964 | // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2). |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3965 | float dot = vx1 * vx2 + vy1 * vy2; |
| 3966 | float cosine = dot / (speed1 * speed2); // denominator always > 0 |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 3967 | if (cosine >= mConfig->pointerGestureSwipeTransitionAngleCosine) { |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3968 | // Pointers are moving in the same direction. Switch to SWIPE. |
| 3969 | #if DEBUG_GESTURES |
| 3970 | LOGD("Gestures: PRESS transitioned to SWIPE, " |
| 3971 | "speed1 %0.3f >= %0.3f, speed2 %0.3f >= %0.3f, " |
| 3972 | "cosine %0.3f >= %0.3f", |
| 3973 | speed1, MULTITOUCH_MIN_SPEED, speed2, MULTITOUCH_MIN_SPEED, |
| 3974 | cosine, SWIPE_TRANSITION_ANGLE_COSINE); |
| 3975 | #endif |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3976 | mPointerGesture.currentGestureMode = PointerGesture::SWIPE; |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3977 | } else { |
| 3978 | // Pointers are moving in different directions. Switch to FREEFORM. |
| 3979 | #if DEBUG_GESTURES |
| 3980 | LOGD("Gestures: PRESS transitioned to FREEFORM, " |
| 3981 | "speed1 %0.3f >= %0.3f, speed2 %0.3f >= %0.3f, " |
| 3982 | "cosine %0.3f < %0.3f", |
| 3983 | speed1, MULTITOUCH_MIN_SPEED, speed2, MULTITOUCH_MIN_SPEED, |
| 3984 | cosine, SWIPE_TRANSITION_ANGLE_COSINE); |
| 3985 | #endif |
| 3986 | *outCancelPreviousGesture = true; |
| 3987 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3988 | } |
| 3989 | } |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3990 | } |
| 3991 | } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) { |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3992 | // Switch from SWIPE to FREEFORM if additional pointers go down. |
| 3993 | // Cancel previous gesture. |
| 3994 | if (mCurrentTouch.pointerCount > 2) { |
| 3995 | #if DEBUG_GESTURES |
| 3996 | LOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2", |
| 3997 | mCurrentTouch.pointerCount); |
| 3998 | #endif |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3999 | *outCancelPreviousGesture = true; |
| 4000 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4001 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4002 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4003 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 4004 | // Clear the reference deltas for fingers not yet included in the reference calculation. |
| 4005 | for (BitSet32 idBits(mCurrentTouch.idBits.value & ~mPointerGesture.referenceIdBits.value); |
| 4006 | !idBits.isEmpty(); ) { |
| 4007 | uint32_t id = idBits.firstMarkedBit(); |
| 4008 | idBits.clearBit(id); |
| 4009 | |
| 4010 | mPointerGesture.referenceDeltas[id].dx = 0; |
| 4011 | mPointerGesture.referenceDeltas[id].dy = 0; |
| 4012 | } |
| 4013 | mPointerGesture.referenceIdBits = mCurrentTouch.idBits; |
| 4014 | |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4015 | // Move the reference points based on the overall group motion of the fingers. |
| 4016 | // The objective is to calculate a vector delta that is common to the movement |
| 4017 | // of all fingers. |
| 4018 | BitSet32 commonIdBits(mLastTouch.idBits.value & mCurrentTouch.idBits.value); |
| 4019 | if (!commonIdBits.isEmpty()) { |
| 4020 | float commonDeltaX = 0, commonDeltaY = 0; |
| 4021 | for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) { |
| 4022 | bool first = (idBits == commonIdBits); |
| 4023 | uint32_t id = idBits.firstMarkedBit(); |
| 4024 | idBits.clearBit(id); |
| 4025 | |
| 4026 | const PointerData& cpd = mCurrentTouch.pointers[mCurrentTouch.idToIndex[id]]; |
| 4027 | const PointerData& lpd = mLastTouch.pointers[mLastTouch.idToIndex[id]]; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 4028 | PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; |
| 4029 | delta.dx += cpd.x - lpd.x; |
| 4030 | delta.dy += cpd.y - lpd.y; |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4031 | |
| 4032 | if (first) { |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 4033 | commonDeltaX = delta.dx; |
| 4034 | commonDeltaY = delta.dy; |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4035 | } else { |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 4036 | commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx); |
| 4037 | commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy); |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4038 | } |
| 4039 | } |
| 4040 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 4041 | if (commonDeltaX || commonDeltaY) { |
| 4042 | for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) { |
| 4043 | uint32_t id = idBits.firstMarkedBit(); |
| 4044 | idBits.clearBit(id); |
| 4045 | |
| 4046 | PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; |
| 4047 | delta.dx = 0; |
| 4048 | delta.dy = 0; |
| 4049 | } |
| 4050 | |
| 4051 | mPointerGesture.referenceTouchX += commonDeltaX; |
| 4052 | mPointerGesture.referenceTouchY += commonDeltaY; |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame^] | 4053 | |
| 4054 | commonDeltaX *= mLocked.pointerGestureXMovementScale; |
| 4055 | commonDeltaY *= mLocked.pointerGestureYMovementScale; |
| 4056 | mPointerGesture.pointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY); |
| 4057 | |
| 4058 | mPointerGesture.referenceGestureX += commonDeltaX; |
| 4059 | mPointerGesture.referenceGestureY += commonDeltaY; |
| 4060 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 4061 | clampPositionUsingPointerBounds(mPointerController, |
| 4062 | &mPointerGesture.referenceGestureX, |
| 4063 | &mPointerGesture.referenceGestureY); |
| 4064 | } |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4065 | } |
| 4066 | |
| 4067 | // Report gestures. |
| 4068 | if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) { |
| 4069 | // PRESS mode. |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4070 | #if DEBUG_GESTURES |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4071 | LOGD("Gestures: PRESS activeTouchId=%d," |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4072 | "activeGestureId=%d, currentTouchPointerCount=%d", |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4073 | activeTouchId, mPointerGesture.activeGestureId, mCurrentTouch.pointerCount); |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4074 | #endif |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4075 | LOG_ASSERT(mPointerGesture.activeGestureId >= 0); |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4076 | |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4077 | mPointerGesture.currentGestureIdBits.clear(); |
| 4078 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 4079 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; |
| 4080 | mPointerGesture.currentGestureCoords[0].clear(); |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4081 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 4082 | mPointerGesture.referenceGestureX); |
| 4083 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, |
| 4084 | mPointerGesture.referenceGestureY); |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4085 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4086 | |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4087 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) { |
| 4088 | mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_PRESS; |
| 4089 | } |
| 4090 | } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) { |
| 4091 | // SWIPE mode. |
| 4092 | #if DEBUG_GESTURES |
| 4093 | LOGD("Gestures: SWIPE activeTouchId=%d," |
| 4094 | "activeGestureId=%d, currentTouchPointerCount=%d", |
| 4095 | activeTouchId, mPointerGesture.activeGestureId, mCurrentTouch.pointerCount); |
| 4096 | #endif |
| 4097 | assert(mPointerGesture.activeGestureId >= 0); |
| 4098 | |
| 4099 | mPointerGesture.currentGestureIdBits.clear(); |
| 4100 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 4101 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; |
| 4102 | mPointerGesture.currentGestureCoords[0].clear(); |
| 4103 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 4104 | mPointerGesture.referenceGestureX); |
| 4105 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, |
| 4106 | mPointerGesture.referenceGestureY); |
| 4107 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
| 4108 | |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4109 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) { |
| 4110 | mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_SWIPE; |
| 4111 | } |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4112 | } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) { |
| 4113 | // FREEFORM mode. |
| 4114 | #if DEBUG_GESTURES |
| 4115 | LOGD("Gestures: FREEFORM activeTouchId=%d," |
| 4116 | "activeGestureId=%d, currentTouchPointerCount=%d", |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4117 | activeTouchId, mPointerGesture.activeGestureId, mCurrentTouch.pointerCount); |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4118 | #endif |
| 4119 | assert(mPointerGesture.activeGestureId >= 0); |
| 4120 | |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4121 | mPointerGesture.currentGestureIdBits.clear(); |
| 4122 | |
| 4123 | BitSet32 mappedTouchIdBits; |
| 4124 | BitSet32 usedGestureIdBits; |
| 4125 | if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) { |
| 4126 | // Initially, assign the active gesture id to the active touch point |
| 4127 | // if there is one. No other touch id bits are mapped yet. |
| 4128 | if (!*outCancelPreviousGesture) { |
| 4129 | mappedTouchIdBits.markBit(activeTouchId); |
| 4130 | usedGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 4131 | mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] = |
| 4132 | mPointerGesture.activeGestureId; |
| 4133 | } else { |
| 4134 | mPointerGesture.activeGestureId = -1; |
| 4135 | } |
| 4136 | } else { |
| 4137 | // Otherwise, assume we mapped all touches from the previous frame. |
| 4138 | // Reuse all mappings that are still applicable. |
| 4139 | mappedTouchIdBits.value = mLastTouch.idBits.value & mCurrentTouch.idBits.value; |
| 4140 | usedGestureIdBits = mPointerGesture.lastGestureIdBits; |
| 4141 | |
| 4142 | // Check whether we need to choose a new active gesture id because the |
| 4143 | // current went went up. |
| 4144 | for (BitSet32 upTouchIdBits(mLastTouch.idBits.value & ~mCurrentTouch.idBits.value); |
| 4145 | !upTouchIdBits.isEmpty(); ) { |
| 4146 | uint32_t upTouchId = upTouchIdBits.firstMarkedBit(); |
| 4147 | upTouchIdBits.clearBit(upTouchId); |
| 4148 | uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId]; |
| 4149 | if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) { |
| 4150 | mPointerGesture.activeGestureId = -1; |
| 4151 | break; |
| 4152 | } |
| 4153 | } |
| 4154 | } |
| 4155 | |
| 4156 | #if DEBUG_GESTURES |
| 4157 | LOGD("Gestures: FREEFORM follow up " |
| 4158 | "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, " |
| 4159 | "activeGestureId=%d", |
| 4160 | mappedTouchIdBits.value, usedGestureIdBits.value, |
| 4161 | mPointerGesture.activeGestureId); |
| 4162 | #endif |
| 4163 | |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4164 | for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) { |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4165 | uint32_t touchId = mCurrentTouch.pointers[i].id; |
| 4166 | uint32_t gestureId; |
| 4167 | if (!mappedTouchIdBits.hasBit(touchId)) { |
| 4168 | gestureId = usedGestureIdBits.firstUnmarkedBit(); |
| 4169 | usedGestureIdBits.markBit(gestureId); |
| 4170 | mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId; |
| 4171 | #if DEBUG_GESTURES |
| 4172 | LOGD("Gestures: FREEFORM " |
| 4173 | "new mapping for touch id %d -> gesture id %d", |
| 4174 | touchId, gestureId); |
| 4175 | #endif |
| 4176 | } else { |
| 4177 | gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId]; |
| 4178 | #if DEBUG_GESTURES |
| 4179 | LOGD("Gestures: FREEFORM " |
| 4180 | "existing mapping for touch id %d -> gesture id %d", |
| 4181 | touchId, gestureId); |
| 4182 | #endif |
| 4183 | } |
| 4184 | mPointerGesture.currentGestureIdBits.markBit(gestureId); |
| 4185 | mPointerGesture.currentGestureIdToIndex[gestureId] = i; |
| 4186 | |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4187 | float x = (mCurrentTouch.pointers[i].x - mPointerGesture.referenceTouchX) |
| 4188 | * mLocked.pointerGestureXZoomScale + mPointerGesture.referenceGestureX; |
| 4189 | float y = (mCurrentTouch.pointers[i].y - mPointerGesture.referenceTouchY) |
| 4190 | * mLocked.pointerGestureYZoomScale + mPointerGesture.referenceGestureY; |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4191 | |
| 4192 | mPointerGesture.currentGestureCoords[i].clear(); |
| 4193 | mPointerGesture.currentGestureCoords[i].setAxisValue( |
| 4194 | AMOTION_EVENT_AXIS_X, x); |
| 4195 | mPointerGesture.currentGestureCoords[i].setAxisValue( |
| 4196 | AMOTION_EVENT_AXIS_Y, y); |
| 4197 | mPointerGesture.currentGestureCoords[i].setAxisValue( |
| 4198 | AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
| 4199 | } |
| 4200 | |
| 4201 | if (mPointerGesture.activeGestureId < 0) { |
| 4202 | mPointerGesture.activeGestureId = |
| 4203 | mPointerGesture.currentGestureIdBits.firstMarkedBit(); |
| 4204 | #if DEBUG_GESTURES |
| 4205 | LOGD("Gestures: FREEFORM new " |
| 4206 | "activeGestureId=%d", mPointerGesture.activeGestureId); |
| 4207 | #endif |
| 4208 | } |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4209 | |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4210 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) { |
| 4211 | mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_FREEFORM; |
| 4212 | } |
| 4213 | } |
| 4214 | |
| 4215 | // Update spot locations for PRESS, SWIPE and FREEFORM. |
| 4216 | // We use the same calculation as we do to calculate the gesture pointers |
| 4217 | // for FREEFORM so that the spots smoothly track gestures. |
| 4218 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) { |
| 4219 | mPointerGesture.spotIdBits.clear(); |
| 4220 | for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) { |
| 4221 | uint32_t id = mCurrentTouch.pointers[i].id; |
| 4222 | mPointerGesture.spotIdBits.markBit(id); |
| 4223 | mPointerGesture.spotIdToIndex[id] = i; |
| 4224 | |
| 4225 | float x = (mCurrentTouch.pointers[i].x - mPointerGesture.referenceTouchX) |
| 4226 | * mLocked.pointerGestureXZoomScale + mPointerGesture.referenceGestureX; |
| 4227 | float y = (mCurrentTouch.pointers[i].y - mPointerGesture.referenceTouchY) |
| 4228 | * mLocked.pointerGestureYZoomScale + mPointerGesture.referenceGestureY; |
| 4229 | |
| 4230 | mPointerGesture.spotCoords[i].clear(); |
| 4231 | mPointerGesture.spotCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 4232 | mPointerGesture.spotCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 4233 | mPointerGesture.spotCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
| 4234 | } |
| 4235 | moveSpotsLocked(); |
| 4236 | } |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4237 | } |
| 4238 | |
Jeff Brown | 4e3f720 | 2011-05-31 15:00:18 -0700 | [diff] [blame] | 4239 | mPointerController->setButtonState(mCurrentTouch.buttonState); |
| 4240 | |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4241 | #if DEBUG_GESTURES |
| 4242 | LOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, " |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4243 | "currentGestureMode=%d, currentGestureIdBits=0x%08x, " |
| 4244 | "lastGestureMode=%d, lastGestureIdBits=0x%08x", |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4245 | toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture), |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4246 | mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value, |
| 4247 | mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value); |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4248 | for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) { |
| 4249 | uint32_t id = idBits.firstMarkedBit(); |
| 4250 | idBits.clearBit(id); |
| 4251 | uint32_t index = mPointerGesture.currentGestureIdToIndex[id]; |
| 4252 | const PointerCoords& coords = mPointerGesture.currentGestureCoords[index]; |
| 4253 | LOGD(" currentGesture[%d]: index=%d, x=%0.3f, y=%0.3f, pressure=%0.3f", |
| 4254 | id, index, coords.getAxisValue(AMOTION_EVENT_AXIS_X), |
| 4255 | coords.getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 4256 | coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE)); |
| 4257 | } |
| 4258 | for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) { |
| 4259 | uint32_t id = idBits.firstMarkedBit(); |
| 4260 | idBits.clearBit(id); |
| 4261 | uint32_t index = mPointerGesture.lastGestureIdToIndex[id]; |
| 4262 | const PointerCoords& coords = mPointerGesture.lastGestureCoords[index]; |
| 4263 | LOGD(" lastGesture[%d]: index=%d, x=%0.3f, y=%0.3f, pressure=%0.3f", |
| 4264 | id, index, coords.getAxisValue(AMOTION_EVENT_AXIS_X), |
| 4265 | coords.getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 4266 | coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE)); |
| 4267 | } |
| 4268 | #endif |
Jeff Brown | 325bd07 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4269 | return true; |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4270 | } |
| 4271 | |
Jeff Brown | 86ea1f5 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4272 | void TouchInputMapper::moveSpotsLocked() { |
| 4273 | mPointerController->setSpots(mPointerGesture.spotGesture, |
| 4274 | mPointerGesture.spotCoords, mPointerGesture.spotIdToIndex, mPointerGesture.spotIdBits); |
| 4275 | } |
| 4276 | |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4277 | void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source, |
| 4278 | int32_t action, int32_t flags, uint32_t metaState, int32_t edgeFlags, |
| 4279 | const PointerCoords* coords, const uint32_t* idToIndex, BitSet32 idBits, |
| 4280 | int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) { |
| 4281 | PointerCoords pointerCoords[MAX_POINTERS]; |
| 4282 | int32_t pointerIds[MAX_POINTERS]; |
| 4283 | uint32_t pointerCount = 0; |
| 4284 | while (!idBits.isEmpty()) { |
| 4285 | uint32_t id = idBits.firstMarkedBit(); |
| 4286 | idBits.clearBit(id); |
| 4287 | uint32_t index = idToIndex[id]; |
| 4288 | pointerIds[pointerCount] = id; |
| 4289 | pointerCoords[pointerCount].copyFrom(coords[index]); |
| 4290 | |
| 4291 | if (changedId >= 0 && id == uint32_t(changedId)) { |
| 4292 | action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
| 4293 | } |
| 4294 | |
| 4295 | pointerCount += 1; |
| 4296 | } |
| 4297 | |
| 4298 | assert(pointerCount != 0); |
| 4299 | |
| 4300 | if (changedId >= 0 && pointerCount == 1) { |
| 4301 | // Replace initial down and final up action. |
| 4302 | // We can compare the action without masking off the changed pointer index |
| 4303 | // because we know the index is 0. |
| 4304 | if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 4305 | action = AMOTION_EVENT_ACTION_DOWN; |
| 4306 | } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 4307 | action = AMOTION_EVENT_ACTION_UP; |
| 4308 | } else { |
| 4309 | // Can't happen. |
| 4310 | assert(false); |
| 4311 | } |
| 4312 | } |
| 4313 | |
| 4314 | getDispatcher()->notifyMotion(when, getDeviceId(), source, policyFlags, |
| 4315 | action, flags, metaState, edgeFlags, |
| 4316 | pointerCount, pointerIds, pointerCoords, xPrecision, yPrecision, downTime); |
| 4317 | } |
| 4318 | |
| 4319 | bool TouchInputMapper::updateMovedPointerCoords( |
| 4320 | const PointerCoords* inCoords, const uint32_t* inIdToIndex, |
| 4321 | PointerCoords* outCoords, const uint32_t* outIdToIndex, BitSet32 idBits) const { |
| 4322 | bool changed = false; |
| 4323 | while (!idBits.isEmpty()) { |
| 4324 | uint32_t id = idBits.firstMarkedBit(); |
| 4325 | idBits.clearBit(id); |
| 4326 | |
| 4327 | uint32_t inIndex = inIdToIndex[id]; |
| 4328 | uint32_t outIndex = outIdToIndex[id]; |
| 4329 | const PointerCoords& curInCoords = inCoords[inIndex]; |
| 4330 | PointerCoords& curOutCoords = outCoords[outIndex]; |
| 4331 | |
| 4332 | if (curInCoords != curOutCoords) { |
| 4333 | curOutCoords.copyFrom(curInCoords); |
| 4334 | changed = true; |
| 4335 | } |
| 4336 | } |
| 4337 | return changed; |
| 4338 | } |
| 4339 | |
| 4340 | void TouchInputMapper::fadePointer() { |
| 4341 | { // acquire lock |
| 4342 | AutoMutex _l(mLock); |
| 4343 | if (mPointerController != NULL) { |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 4344 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4345 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4346 | } // release lock |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4347 | } |
| 4348 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4349 | bool TouchInputMapper::isPointInsideSurfaceLocked(int32_t x, int32_t y) { |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 4350 | return x >= mRawAxes.x.minValue && x <= mRawAxes.x.maxValue |
| 4351 | && y >= mRawAxes.y.minValue && y <= mRawAxes.y.maxValue; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4352 | } |
| 4353 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4354 | const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHitLocked( |
| 4355 | int32_t x, int32_t y) { |
| 4356 | size_t numVirtualKeys = mLocked.virtualKeys.size(); |
| 4357 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 4358 | const VirtualKey& virtualKey = mLocked.virtualKeys[i]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4359 | |
| 4360 | #if DEBUG_VIRTUAL_KEYS |
| 4361 | LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, " |
| 4362 | "left=%d, top=%d, right=%d, bottom=%d", |
| 4363 | x, y, |
| 4364 | virtualKey.keyCode, virtualKey.scanCode, |
| 4365 | virtualKey.hitLeft, virtualKey.hitTop, |
| 4366 | virtualKey.hitRight, virtualKey.hitBottom); |
| 4367 | #endif |
| 4368 | |
| 4369 | if (virtualKey.isHit(x, y)) { |
| 4370 | return & virtualKey; |
| 4371 | } |
| 4372 | } |
| 4373 | |
| 4374 | return NULL; |
| 4375 | } |
| 4376 | |
| 4377 | void TouchInputMapper::calculatePointerIds() { |
| 4378 | uint32_t currentPointerCount = mCurrentTouch.pointerCount; |
| 4379 | uint32_t lastPointerCount = mLastTouch.pointerCount; |
| 4380 | |
| 4381 | if (currentPointerCount == 0) { |
| 4382 | // No pointers to assign. |
| 4383 | mCurrentTouch.idBits.clear(); |
| 4384 | } else if (lastPointerCount == 0) { |
| 4385 | // All pointers are new. |
| 4386 | mCurrentTouch.idBits.clear(); |
| 4387 | for (uint32_t i = 0; i < currentPointerCount; i++) { |
| 4388 | mCurrentTouch.pointers[i].id = i; |
| 4389 | mCurrentTouch.idToIndex[i] = i; |
| 4390 | mCurrentTouch.idBits.markBit(i); |
| 4391 | } |
| 4392 | } else if (currentPointerCount == 1 && lastPointerCount == 1) { |
| 4393 | // Only one pointer and no change in count so it must have the same id as before. |
| 4394 | uint32_t id = mLastTouch.pointers[0].id; |
| 4395 | mCurrentTouch.pointers[0].id = id; |
| 4396 | mCurrentTouch.idToIndex[id] = 0; |
| 4397 | mCurrentTouch.idBits.value = BitSet32::valueForBit(id); |
| 4398 | } else { |
| 4399 | // General case. |
| 4400 | // We build a heap of squared euclidean distances between current and last pointers |
| 4401 | // associated with the current and last pointer indices. Then, we find the best |
| 4402 | // match (by distance) for each current pointer. |
| 4403 | PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS]; |
| 4404 | |
| 4405 | uint32_t heapSize = 0; |
| 4406 | for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount; |
| 4407 | currentPointerIndex++) { |
| 4408 | for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount; |
| 4409 | lastPointerIndex++) { |
| 4410 | int64_t deltaX = mCurrentTouch.pointers[currentPointerIndex].x |
| 4411 | - mLastTouch.pointers[lastPointerIndex].x; |
| 4412 | int64_t deltaY = mCurrentTouch.pointers[currentPointerIndex].y |
| 4413 | - mLastTouch.pointers[lastPointerIndex].y; |
| 4414 | |
| 4415 | uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY); |
| 4416 | |
| 4417 | // Insert new element into the heap (sift up). |
| 4418 | heap[heapSize].currentPointerIndex = currentPointerIndex; |
| 4419 | heap[heapSize].lastPointerIndex = lastPointerIndex; |
| 4420 | heap[heapSize].distance = distance; |
| 4421 | heapSize += 1; |
| 4422 | } |
| 4423 | } |
| 4424 | |
| 4425 | // Heapify |
| 4426 | for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) { |
| 4427 | startIndex -= 1; |
| 4428 | for (uint32_t parentIndex = startIndex; ;) { |
| 4429 | uint32_t childIndex = parentIndex * 2 + 1; |
| 4430 | if (childIndex >= heapSize) { |
| 4431 | break; |
| 4432 | } |
| 4433 | |
| 4434 | if (childIndex + 1 < heapSize |
| 4435 | && heap[childIndex + 1].distance < heap[childIndex].distance) { |
| 4436 | childIndex += 1; |
| 4437 | } |
| 4438 | |
| 4439 | if (heap[parentIndex].distance <= heap[childIndex].distance) { |
| 4440 | break; |
| 4441 | } |
| 4442 | |
| 4443 | swap(heap[parentIndex], heap[childIndex]); |
| 4444 | parentIndex = childIndex; |
| 4445 | } |
| 4446 | } |
| 4447 | |
| 4448 | #if DEBUG_POINTER_ASSIGNMENT |
| 4449 | LOGD("calculatePointerIds - initial distance min-heap: size=%d", heapSize); |
| 4450 | for (size_t i = 0; i < heapSize; i++) { |
| 4451 | LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld", |
| 4452 | i, heap[i].currentPointerIndex, heap[i].lastPointerIndex, |
| 4453 | heap[i].distance); |
| 4454 | } |
| 4455 | #endif |
| 4456 | |
| 4457 | // Pull matches out by increasing order of distance. |
| 4458 | // To avoid reassigning pointers that have already been matched, the loop keeps track |
| 4459 | // of which last and current pointers have been matched using the matchedXXXBits variables. |
| 4460 | // It also tracks the used pointer id bits. |
| 4461 | BitSet32 matchedLastBits(0); |
| 4462 | BitSet32 matchedCurrentBits(0); |
| 4463 | BitSet32 usedIdBits(0); |
| 4464 | bool first = true; |
| 4465 | for (uint32_t i = min(currentPointerCount, lastPointerCount); i > 0; i--) { |
| 4466 | for (;;) { |
| 4467 | if (first) { |
| 4468 | // The first time through the loop, we just consume the root element of |
| 4469 | // the heap (the one with smallest distance). |
| 4470 | first = false; |
| 4471 | } else { |
| 4472 | // Previous iterations consumed the root element of the heap. |
| 4473 | // Pop root element off of the heap (sift down). |
| 4474 | heapSize -= 1; |
| 4475 | assert(heapSize > 0); |
| 4476 | |
| 4477 | // Sift down. |
| 4478 | heap[0] = heap[heapSize]; |
| 4479 | for (uint32_t parentIndex = 0; ;) { |
| 4480 | uint32_t childIndex = parentIndex * 2 + 1; |
| 4481 | if (childIndex >= heapSize) { |
| 4482 | break; |
| 4483 | } |
| 4484 | |
| 4485 | if (childIndex + 1 < heapSize |
| 4486 | && heap[childIndex + 1].distance < heap[childIndex].distance) { |
| 4487 | childIndex += 1; |
| 4488 | } |
| 4489 | |
| 4490 | if (heap[parentIndex].distance <= heap[childIndex].distance) { |
| 4491 | break; |
| 4492 | } |
| 4493 | |
| 4494 | swap(heap[parentIndex], heap[childIndex]); |
| 4495 | parentIndex = childIndex; |
| 4496 | } |
| 4497 | |
| 4498 | #if DEBUG_POINTER_ASSIGNMENT |
| 4499 | LOGD("calculatePointerIds - reduced distance min-heap: size=%d", heapSize); |
| 4500 | for (size_t i = 0; i < heapSize; i++) { |
| 4501 | LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld", |
| 4502 | i, heap[i].currentPointerIndex, heap[i].lastPointerIndex, |
| 4503 | heap[i].distance); |
| 4504 | } |
| 4505 | #endif |
| 4506 | } |
| 4507 | |
| 4508 | uint32_t currentPointerIndex = heap[0].currentPointerIndex; |
| 4509 | if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched |
| 4510 | |
| 4511 | uint32_t lastPointerIndex = heap[0].lastPointerIndex; |
| 4512 | if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched |
| 4513 | |
| 4514 | matchedCurrentBits.markBit(currentPointerIndex); |
| 4515 | matchedLastBits.markBit(lastPointerIndex); |
| 4516 | |
| 4517 | uint32_t id = mLastTouch.pointers[lastPointerIndex].id; |
| 4518 | mCurrentTouch.pointers[currentPointerIndex].id = id; |
| 4519 | mCurrentTouch.idToIndex[id] = currentPointerIndex; |
| 4520 | usedIdBits.markBit(id); |
| 4521 | |
| 4522 | #if DEBUG_POINTER_ASSIGNMENT |
| 4523 | LOGD("calculatePointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld", |
| 4524 | lastPointerIndex, currentPointerIndex, id, heap[0].distance); |
| 4525 | #endif |
| 4526 | break; |
| 4527 | } |
| 4528 | } |
| 4529 | |
| 4530 | // Assign fresh ids to new pointers. |
| 4531 | if (currentPointerCount > lastPointerCount) { |
| 4532 | for (uint32_t i = currentPointerCount - lastPointerCount; ;) { |
| 4533 | uint32_t currentPointerIndex = matchedCurrentBits.firstUnmarkedBit(); |
| 4534 | uint32_t id = usedIdBits.firstUnmarkedBit(); |
| 4535 | |
| 4536 | mCurrentTouch.pointers[currentPointerIndex].id = id; |
| 4537 | mCurrentTouch.idToIndex[id] = currentPointerIndex; |
| 4538 | usedIdBits.markBit(id); |
| 4539 | |
| 4540 | #if DEBUG_POINTER_ASSIGNMENT |
| 4541 | LOGD("calculatePointerIds - assigned: cur=%d, id=%d", |
| 4542 | currentPointerIndex, id); |
| 4543 | #endif |
| 4544 | |
| 4545 | if (--i == 0) break; // done |
| 4546 | matchedCurrentBits.markBit(currentPointerIndex); |
| 4547 | } |
| 4548 | } |
| 4549 | |
| 4550 | // Fix id bits. |
| 4551 | mCurrentTouch.idBits = usedIdBits; |
| 4552 | } |
| 4553 | } |
| 4554 | |
| 4555 | /* Special hack for devices that have bad screen data: if one of the |
| 4556 | * points has moved more than a screen height from the last position, |
| 4557 | * then drop it. */ |
| 4558 | bool TouchInputMapper::applyBadTouchFilter() { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4559 | uint32_t pointerCount = mCurrentTouch.pointerCount; |
| 4560 | |
| 4561 | // Nothing to do if there are no points. |
| 4562 | if (pointerCount == 0) { |
| 4563 | return false; |
| 4564 | } |
| 4565 | |
| 4566 | // Don't do anything if a finger is going down or up. We run |
| 4567 | // here before assigning pointer IDs, so there isn't a good |
| 4568 | // way to do per-finger matching. |
| 4569 | if (pointerCount != mLastTouch.pointerCount) { |
| 4570 | return false; |
| 4571 | } |
| 4572 | |
| 4573 | // We consider a single movement across more than a 7/16 of |
| 4574 | // the long size of the screen to be bad. This was a magic value |
| 4575 | // determined by looking at the maximum distance it is feasible |
| 4576 | // to actually move in one sample. |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 4577 | int32_t maxDeltaY = (mRawAxes.y.maxValue - mRawAxes.y.minValue + 1) * 7 / 16; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4578 | |
| 4579 | // XXX The original code in InputDevice.java included commented out |
| 4580 | // code for testing the X axis. Note that when we drop a point |
| 4581 | // we don't actually restore the old X either. Strange. |
| 4582 | // The old code also tries to track when bad points were previously |
| 4583 | // detected but it turns out that due to the placement of a "break" |
| 4584 | // at the end of the loop, we never set mDroppedBadPoint to true |
| 4585 | // so it is effectively dead code. |
| 4586 | // Need to figure out if the old code is busted or just overcomplicated |
| 4587 | // but working as intended. |
| 4588 | |
| 4589 | // Look through all new points and see if any are farther than |
| 4590 | // acceptable from all previous points. |
| 4591 | for (uint32_t i = pointerCount; i-- > 0; ) { |
| 4592 | int32_t y = mCurrentTouch.pointers[i].y; |
| 4593 | int32_t closestY = INT_MAX; |
| 4594 | int32_t closestDeltaY = 0; |
| 4595 | |
| 4596 | #if DEBUG_HACKS |
| 4597 | LOGD("BadTouchFilter: Looking at next point #%d: y=%d", i, y); |
| 4598 | #endif |
| 4599 | |
| 4600 | for (uint32_t j = pointerCount; j-- > 0; ) { |
| 4601 | int32_t lastY = mLastTouch.pointers[j].y; |
| 4602 | int32_t deltaY = abs(y - lastY); |
| 4603 | |
| 4604 | #if DEBUG_HACKS |
| 4605 | LOGD("BadTouchFilter: Comparing with last point #%d: y=%d deltaY=%d", |
| 4606 | j, lastY, deltaY); |
| 4607 | #endif |
| 4608 | |
| 4609 | if (deltaY < maxDeltaY) { |
| 4610 | goto SkipSufficientlyClosePoint; |
| 4611 | } |
| 4612 | if (deltaY < closestDeltaY) { |
| 4613 | closestDeltaY = deltaY; |
| 4614 | closestY = lastY; |
| 4615 | } |
| 4616 | } |
| 4617 | |
| 4618 | // Must not have found a close enough match. |
| 4619 | #if DEBUG_HACKS |
| 4620 | LOGD("BadTouchFilter: Dropping bad point #%d: newY=%d oldY=%d deltaY=%d maxDeltaY=%d", |
| 4621 | i, y, closestY, closestDeltaY, maxDeltaY); |
| 4622 | #endif |
| 4623 | |
| 4624 | mCurrentTouch.pointers[i].y = closestY; |
| 4625 | return true; // XXX original code only corrects one point |
| 4626 | |
| 4627 | SkipSufficientlyClosePoint: ; |
| 4628 | } |
| 4629 | |
| 4630 | // No change. |
| 4631 | return false; |
| 4632 | } |
| 4633 | |
| 4634 | /* Special hack for devices that have bad screen data: drop points where |
| 4635 | * the coordinate value for one axis has jumped to the other pointer's location. |
| 4636 | */ |
| 4637 | bool TouchInputMapper::applyJumpyTouchFilter() { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4638 | uint32_t pointerCount = mCurrentTouch.pointerCount; |
| 4639 | if (mLastTouch.pointerCount != pointerCount) { |
| 4640 | #if DEBUG_HACKS |
| 4641 | LOGD("JumpyTouchFilter: Different pointer count %d -> %d", |
| 4642 | mLastTouch.pointerCount, pointerCount); |
| 4643 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 4644 | LOGD(" Pointer %d (%d, %d)", i, |
| 4645 | mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y); |
| 4646 | } |
| 4647 | #endif |
| 4648 | |
| 4649 | if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_TRANSITION_DROPS) { |
| 4650 | if (mLastTouch.pointerCount == 1 && pointerCount == 2) { |
| 4651 | // Just drop the first few events going from 1 to 2 pointers. |
| 4652 | // They're bad often enough that they're not worth considering. |
| 4653 | mCurrentTouch.pointerCount = 1; |
| 4654 | mJumpyTouchFilter.jumpyPointsDropped += 1; |
| 4655 | |
| 4656 | #if DEBUG_HACKS |
| 4657 | LOGD("JumpyTouchFilter: Pointer 2 dropped"); |
| 4658 | #endif |
| 4659 | return true; |
| 4660 | } else if (mLastTouch.pointerCount == 2 && pointerCount == 1) { |
| 4661 | // The event when we go from 2 -> 1 tends to be messed up too |
| 4662 | mCurrentTouch.pointerCount = 2; |
| 4663 | mCurrentTouch.pointers[0] = mLastTouch.pointers[0]; |
| 4664 | mCurrentTouch.pointers[1] = mLastTouch.pointers[1]; |
| 4665 | mJumpyTouchFilter.jumpyPointsDropped += 1; |
| 4666 | |
| 4667 | #if DEBUG_HACKS |
| 4668 | for (int32_t i = 0; i < 2; i++) { |
| 4669 | LOGD("JumpyTouchFilter: Pointer %d replaced (%d, %d)", i, |
| 4670 | mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y); |
| 4671 | } |
| 4672 | #endif |
| 4673 | return true; |
| 4674 | } |
| 4675 | } |
| 4676 | // Reset jumpy points dropped on other transitions or if limit exceeded. |
| 4677 | mJumpyTouchFilter.jumpyPointsDropped = 0; |
| 4678 | |
| 4679 | #if DEBUG_HACKS |
| 4680 | LOGD("JumpyTouchFilter: Transition - drop limit reset"); |
| 4681 | #endif |
| 4682 | return false; |
| 4683 | } |
| 4684 | |
| 4685 | // We have the same number of pointers as last time. |
| 4686 | // A 'jumpy' point is one where the coordinate value for one axis |
| 4687 | // has jumped to the other pointer's location. No need to do anything |
| 4688 | // else if we only have one pointer. |
| 4689 | if (pointerCount < 2) { |
| 4690 | return false; |
| 4691 | } |
| 4692 | |
| 4693 | if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_DROP_LIMIT) { |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 4694 | int jumpyEpsilon = (mRawAxes.y.maxValue - mRawAxes.y.minValue + 1) / JUMPY_EPSILON_DIVISOR; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4695 | |
| 4696 | // We only replace the single worst jumpy point as characterized by pointer distance |
| 4697 | // in a single axis. |
| 4698 | int32_t badPointerIndex = -1; |
| 4699 | int32_t badPointerReplacementIndex = -1; |
| 4700 | int32_t badPointerDistance = INT_MIN; // distance to be corrected |
| 4701 | |
| 4702 | for (uint32_t i = pointerCount; i-- > 0; ) { |
| 4703 | int32_t x = mCurrentTouch.pointers[i].x; |
| 4704 | int32_t y = mCurrentTouch.pointers[i].y; |
| 4705 | |
| 4706 | #if DEBUG_HACKS |
| 4707 | LOGD("JumpyTouchFilter: Point %d (%d, %d)", i, x, y); |
| 4708 | #endif |
| 4709 | |
| 4710 | // Check if a touch point is too close to another's coordinates |
| 4711 | bool dropX = false, dropY = false; |
| 4712 | for (uint32_t j = 0; j < pointerCount; j++) { |
| 4713 | if (i == j) { |
| 4714 | continue; |
| 4715 | } |
| 4716 | |
| 4717 | if (abs(x - mCurrentTouch.pointers[j].x) <= jumpyEpsilon) { |
| 4718 | dropX = true; |
| 4719 | break; |
| 4720 | } |
| 4721 | |
| 4722 | if (abs(y - mCurrentTouch.pointers[j].y) <= jumpyEpsilon) { |
| 4723 | dropY = true; |
| 4724 | break; |
| 4725 | } |
| 4726 | } |
| 4727 | if (! dropX && ! dropY) { |
| 4728 | continue; // not jumpy |
| 4729 | } |
| 4730 | |
| 4731 | // Find a replacement candidate by comparing with older points on the |
| 4732 | // complementary (non-jumpy) axis. |
| 4733 | int32_t distance = INT_MIN; // distance to be corrected |
| 4734 | int32_t replacementIndex = -1; |
| 4735 | |
| 4736 | if (dropX) { |
| 4737 | // X looks too close. Find an older replacement point with a close Y. |
| 4738 | int32_t smallestDeltaY = INT_MAX; |
| 4739 | for (uint32_t j = 0; j < pointerCount; j++) { |
| 4740 | int32_t deltaY = abs(y - mLastTouch.pointers[j].y); |
| 4741 | if (deltaY < smallestDeltaY) { |
| 4742 | smallestDeltaY = deltaY; |
| 4743 | replacementIndex = j; |
| 4744 | } |
| 4745 | } |
| 4746 | distance = abs(x - mLastTouch.pointers[replacementIndex].x); |
| 4747 | } else { |
| 4748 | // Y looks too close. Find an older replacement point with a close X. |
| 4749 | int32_t smallestDeltaX = INT_MAX; |
| 4750 | for (uint32_t j = 0; j < pointerCount; j++) { |
| 4751 | int32_t deltaX = abs(x - mLastTouch.pointers[j].x); |
| 4752 | if (deltaX < smallestDeltaX) { |
| 4753 | smallestDeltaX = deltaX; |
| 4754 | replacementIndex = j; |
| 4755 | } |
| 4756 | } |
| 4757 | distance = abs(y - mLastTouch.pointers[replacementIndex].y); |
| 4758 | } |
| 4759 | |
| 4760 | // If replacing this pointer would correct a worse error than the previous ones |
| 4761 | // considered, then use this replacement instead. |
| 4762 | if (distance > badPointerDistance) { |
| 4763 | badPointerIndex = i; |
| 4764 | badPointerReplacementIndex = replacementIndex; |
| 4765 | badPointerDistance = distance; |
| 4766 | } |
| 4767 | } |
| 4768 | |
| 4769 | // Correct the jumpy pointer if one was found. |
| 4770 | if (badPointerIndex >= 0) { |
| 4771 | #if DEBUG_HACKS |
| 4772 | LOGD("JumpyTouchFilter: Replacing bad pointer %d with (%d, %d)", |
| 4773 | badPointerIndex, |
| 4774 | mLastTouch.pointers[badPointerReplacementIndex].x, |
| 4775 | mLastTouch.pointers[badPointerReplacementIndex].y); |
| 4776 | #endif |
| 4777 | |
| 4778 | mCurrentTouch.pointers[badPointerIndex].x = |
| 4779 | mLastTouch.pointers[badPointerReplacementIndex].x; |
| 4780 | mCurrentTouch.pointers[badPointerIndex].y = |
| 4781 | mLastTouch.pointers[badPointerReplacementIndex].y; |
| 4782 | mJumpyTouchFilter.jumpyPointsDropped += 1; |
| 4783 | return true; |
| 4784 | } |
| 4785 | } |
| 4786 | |
| 4787 | mJumpyTouchFilter.jumpyPointsDropped = 0; |
| 4788 | return false; |
| 4789 | } |
| 4790 | |
| 4791 | /* Special hack for devices that have bad screen data: aggregate and |
| 4792 | * compute averages of the coordinate data, to reduce the amount of |
| 4793 | * jitter seen by applications. */ |
| 4794 | void TouchInputMapper::applyAveragingTouchFilter() { |
| 4795 | for (uint32_t currentIndex = 0; currentIndex < mCurrentTouch.pointerCount; currentIndex++) { |
| 4796 | uint32_t id = mCurrentTouch.pointers[currentIndex].id; |
| 4797 | int32_t x = mCurrentTouch.pointers[currentIndex].x; |
| 4798 | int32_t y = mCurrentTouch.pointers[currentIndex].y; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 4799 | int32_t pressure; |
| 4800 | switch (mCalibration.pressureSource) { |
| 4801 | case Calibration::PRESSURE_SOURCE_PRESSURE: |
| 4802 | pressure = mCurrentTouch.pointers[currentIndex].pressure; |
| 4803 | break; |
| 4804 | case Calibration::PRESSURE_SOURCE_TOUCH: |
| 4805 | pressure = mCurrentTouch.pointers[currentIndex].touchMajor; |
| 4806 | break; |
| 4807 | default: |
| 4808 | pressure = 1; |
| 4809 | break; |
| 4810 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4811 | |
| 4812 | if (mLastTouch.idBits.hasBit(id)) { |
| 4813 | // Pointer was down before and is still down now. |
| 4814 | // Compute average over history trace. |
| 4815 | uint32_t start = mAveragingTouchFilter.historyStart[id]; |
| 4816 | uint32_t end = mAveragingTouchFilter.historyEnd[id]; |
| 4817 | |
| 4818 | int64_t deltaX = x - mAveragingTouchFilter.historyData[end].pointers[id].x; |
| 4819 | int64_t deltaY = y - mAveragingTouchFilter.historyData[end].pointers[id].y; |
| 4820 | uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY); |
| 4821 | |
| 4822 | #if DEBUG_HACKS |
| 4823 | LOGD("AveragingTouchFilter: Pointer id %d - Distance from last sample: %lld", |
| 4824 | id, distance); |
| 4825 | #endif |
| 4826 | |
| 4827 | if (distance < AVERAGING_DISTANCE_LIMIT) { |
| 4828 | // Increment end index in preparation for recording new historical data. |
| 4829 | end += 1; |
| 4830 | if (end > AVERAGING_HISTORY_SIZE) { |
| 4831 | end = 0; |
| 4832 | } |
| 4833 | |
| 4834 | // If the end index has looped back to the start index then we have filled |
| 4835 | // the historical trace up to the desired size so we drop the historical |
| 4836 | // data at the start of the trace. |
| 4837 | if (end == start) { |
| 4838 | start += 1; |
| 4839 | if (start > AVERAGING_HISTORY_SIZE) { |
| 4840 | start = 0; |
| 4841 | } |
| 4842 | } |
| 4843 | |
| 4844 | // Add the raw data to the historical trace. |
| 4845 | mAveragingTouchFilter.historyStart[id] = start; |
| 4846 | mAveragingTouchFilter.historyEnd[id] = end; |
| 4847 | mAveragingTouchFilter.historyData[end].pointers[id].x = x; |
| 4848 | mAveragingTouchFilter.historyData[end].pointers[id].y = y; |
| 4849 | mAveragingTouchFilter.historyData[end].pointers[id].pressure = pressure; |
| 4850 | |
| 4851 | // Average over all historical positions in the trace by total pressure. |
| 4852 | int32_t averagedX = 0; |
| 4853 | int32_t averagedY = 0; |
| 4854 | int32_t totalPressure = 0; |
| 4855 | for (;;) { |
| 4856 | int32_t historicalX = mAveragingTouchFilter.historyData[start].pointers[id].x; |
| 4857 | int32_t historicalY = mAveragingTouchFilter.historyData[start].pointers[id].y; |
| 4858 | int32_t historicalPressure = mAveragingTouchFilter.historyData[start] |
| 4859 | .pointers[id].pressure; |
| 4860 | |
| 4861 | averagedX += historicalX * historicalPressure; |
| 4862 | averagedY += historicalY * historicalPressure; |
| 4863 | totalPressure += historicalPressure; |
| 4864 | |
| 4865 | if (start == end) { |
| 4866 | break; |
| 4867 | } |
| 4868 | |
| 4869 | start += 1; |
| 4870 | if (start > AVERAGING_HISTORY_SIZE) { |
| 4871 | start = 0; |
| 4872 | } |
| 4873 | } |
| 4874 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 4875 | if (totalPressure != 0) { |
| 4876 | averagedX /= totalPressure; |
| 4877 | averagedY /= totalPressure; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4878 | |
| 4879 | #if DEBUG_HACKS |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 4880 | LOGD("AveragingTouchFilter: Pointer id %d - " |
| 4881 | "totalPressure=%d, averagedX=%d, averagedY=%d", id, totalPressure, |
| 4882 | averagedX, averagedY); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4883 | #endif |
| 4884 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 4885 | mCurrentTouch.pointers[currentIndex].x = averagedX; |
| 4886 | mCurrentTouch.pointers[currentIndex].y = averagedY; |
| 4887 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4888 | } else { |
| 4889 | #if DEBUG_HACKS |
| 4890 | LOGD("AveragingTouchFilter: Pointer id %d - Exceeded max distance", id); |
| 4891 | #endif |
| 4892 | } |
| 4893 | } else { |
| 4894 | #if DEBUG_HACKS |
| 4895 | LOGD("AveragingTouchFilter: Pointer id %d - Pointer went up", id); |
| 4896 | #endif |
| 4897 | } |
| 4898 | |
| 4899 | // Reset pointer history. |
| 4900 | mAveragingTouchFilter.historyStart[id] = 0; |
| 4901 | mAveragingTouchFilter.historyEnd[id] = 0; |
| 4902 | mAveragingTouchFilter.historyData[0].pointers[id].x = x; |
| 4903 | mAveragingTouchFilter.historyData[0].pointers[id].y = y; |
| 4904 | mAveragingTouchFilter.historyData[0].pointers[id].pressure = pressure; |
| 4905 | } |
| 4906 | } |
| 4907 | |
| 4908 | int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4909 | { // acquire lock |
| 4910 | AutoMutex _l(mLock); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4911 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4912 | if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.keyCode == keyCode) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4913 | return AKEY_STATE_VIRTUAL; |
| 4914 | } |
| 4915 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4916 | size_t numVirtualKeys = mLocked.virtualKeys.size(); |
| 4917 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 4918 | const VirtualKey& virtualKey = mLocked.virtualKeys[i]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4919 | if (virtualKey.keyCode == keyCode) { |
| 4920 | return AKEY_STATE_UP; |
| 4921 | } |
| 4922 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4923 | } // release lock |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4924 | |
| 4925 | return AKEY_STATE_UNKNOWN; |
| 4926 | } |
| 4927 | |
| 4928 | int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4929 | { // acquire lock |
| 4930 | AutoMutex _l(mLock); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4931 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4932 | if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.scanCode == scanCode) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4933 | return AKEY_STATE_VIRTUAL; |
| 4934 | } |
| 4935 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4936 | size_t numVirtualKeys = mLocked.virtualKeys.size(); |
| 4937 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 4938 | const VirtualKey& virtualKey = mLocked.virtualKeys[i]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4939 | if (virtualKey.scanCode == scanCode) { |
| 4940 | return AKEY_STATE_UP; |
| 4941 | } |
| 4942 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4943 | } // release lock |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4944 | |
| 4945 | return AKEY_STATE_UNKNOWN; |
| 4946 | } |
| 4947 | |
| 4948 | bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 4949 | const int32_t* keyCodes, uint8_t* outFlags) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4950 | { // acquire lock |
| 4951 | AutoMutex _l(mLock); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4952 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4953 | size_t numVirtualKeys = mLocked.virtualKeys.size(); |
| 4954 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 4955 | const VirtualKey& virtualKey = mLocked.virtualKeys[i]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4956 | |
| 4957 | for (size_t i = 0; i < numCodes; i++) { |
| 4958 | if (virtualKey.keyCode == keyCodes[i]) { |
| 4959 | outFlags[i] = 1; |
| 4960 | } |
| 4961 | } |
| 4962 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4963 | } // release lock |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4964 | |
| 4965 | return true; |
| 4966 | } |
| 4967 | |
| 4968 | |
| 4969 | // --- SingleTouchInputMapper --- |
| 4970 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 4971 | SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) : |
| 4972 | TouchInputMapper(device) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4973 | initialize(); |
| 4974 | } |
| 4975 | |
| 4976 | SingleTouchInputMapper::~SingleTouchInputMapper() { |
| 4977 | } |
| 4978 | |
| 4979 | void SingleTouchInputMapper::initialize() { |
| 4980 | mAccumulator.clear(); |
| 4981 | |
| 4982 | mDown = false; |
| 4983 | mX = 0; |
| 4984 | mY = 0; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 4985 | mPressure = 0; // default to 0 for devices that don't report pressure |
| 4986 | mToolWidth = 0; // default to 0 for devices that don't report tool width |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4987 | mButtonState = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4988 | } |
| 4989 | |
| 4990 | void SingleTouchInputMapper::reset() { |
| 4991 | TouchInputMapper::reset(); |
| 4992 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4993 | initialize(); |
| 4994 | } |
| 4995 | |
| 4996 | void SingleTouchInputMapper::process(const RawEvent* rawEvent) { |
| 4997 | switch (rawEvent->type) { |
| 4998 | case EV_KEY: |
| 4999 | switch (rawEvent->scanCode) { |
| 5000 | case BTN_TOUCH: |
| 5001 | mAccumulator.fields |= Accumulator::FIELD_BTN_TOUCH; |
| 5002 | mAccumulator.btnTouch = rawEvent->value != 0; |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5003 | // Don't sync immediately. Wait until the next SYN_REPORT since we might |
| 5004 | // not have received valid position information yet. This logic assumes that |
| 5005 | // BTN_TOUCH is always followed by SYN_REPORT as part of a complete packet. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5006 | break; |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5007 | default: |
| 5008 | if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) { |
| 5009 | uint32_t buttonState = getButtonStateForScanCode(rawEvent->scanCode); |
| 5010 | if (buttonState) { |
| 5011 | if (rawEvent->value) { |
| 5012 | mAccumulator.buttonDown |= buttonState; |
| 5013 | } else { |
| 5014 | mAccumulator.buttonUp |= buttonState; |
| 5015 | } |
| 5016 | mAccumulator.fields |= Accumulator::FIELD_BUTTONS; |
| 5017 | } |
| 5018 | } |
| 5019 | break; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5020 | } |
| 5021 | break; |
| 5022 | |
| 5023 | case EV_ABS: |
| 5024 | switch (rawEvent->scanCode) { |
| 5025 | case ABS_X: |
| 5026 | mAccumulator.fields |= Accumulator::FIELD_ABS_X; |
| 5027 | mAccumulator.absX = rawEvent->value; |
| 5028 | break; |
| 5029 | case ABS_Y: |
| 5030 | mAccumulator.fields |= Accumulator::FIELD_ABS_Y; |
| 5031 | mAccumulator.absY = rawEvent->value; |
| 5032 | break; |
| 5033 | case ABS_PRESSURE: |
| 5034 | mAccumulator.fields |= Accumulator::FIELD_ABS_PRESSURE; |
| 5035 | mAccumulator.absPressure = rawEvent->value; |
| 5036 | break; |
| 5037 | case ABS_TOOL_WIDTH: |
| 5038 | mAccumulator.fields |= Accumulator::FIELD_ABS_TOOL_WIDTH; |
| 5039 | mAccumulator.absToolWidth = rawEvent->value; |
| 5040 | break; |
| 5041 | } |
| 5042 | break; |
| 5043 | |
| 5044 | case EV_SYN: |
| 5045 | switch (rawEvent->scanCode) { |
| 5046 | case SYN_REPORT: |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5047 | sync(rawEvent->when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5048 | break; |
| 5049 | } |
| 5050 | break; |
| 5051 | } |
| 5052 | } |
| 5053 | |
| 5054 | void SingleTouchInputMapper::sync(nsecs_t when) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5055 | uint32_t fields = mAccumulator.fields; |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5056 | if (fields == 0) { |
| 5057 | return; // no new state changes, so nothing to do |
| 5058 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5059 | |
| 5060 | if (fields & Accumulator::FIELD_BTN_TOUCH) { |
| 5061 | mDown = mAccumulator.btnTouch; |
| 5062 | } |
| 5063 | |
| 5064 | if (fields & Accumulator::FIELD_ABS_X) { |
| 5065 | mX = mAccumulator.absX; |
| 5066 | } |
| 5067 | |
| 5068 | if (fields & Accumulator::FIELD_ABS_Y) { |
| 5069 | mY = mAccumulator.absY; |
| 5070 | } |
| 5071 | |
| 5072 | if (fields & Accumulator::FIELD_ABS_PRESSURE) { |
| 5073 | mPressure = mAccumulator.absPressure; |
| 5074 | } |
| 5075 | |
| 5076 | if (fields & Accumulator::FIELD_ABS_TOOL_WIDTH) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5077 | mToolWidth = mAccumulator.absToolWidth; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5078 | } |
| 5079 | |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5080 | if (fields & Accumulator::FIELD_BUTTONS) { |
| 5081 | mButtonState = (mButtonState | mAccumulator.buttonDown) & ~mAccumulator.buttonUp; |
| 5082 | } |
| 5083 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5084 | mCurrentTouch.clear(); |
| 5085 | |
| 5086 | if (mDown) { |
| 5087 | mCurrentTouch.pointerCount = 1; |
| 5088 | mCurrentTouch.pointers[0].id = 0; |
| 5089 | mCurrentTouch.pointers[0].x = mX; |
| 5090 | mCurrentTouch.pointers[0].y = mY; |
| 5091 | mCurrentTouch.pointers[0].pressure = mPressure; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5092 | mCurrentTouch.pointers[0].touchMajor = 0; |
| 5093 | mCurrentTouch.pointers[0].touchMinor = 0; |
| 5094 | mCurrentTouch.pointers[0].toolMajor = mToolWidth; |
| 5095 | mCurrentTouch.pointers[0].toolMinor = mToolWidth; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5096 | mCurrentTouch.pointers[0].orientation = 0; |
| 5097 | mCurrentTouch.idToIndex[0] = 0; |
| 5098 | mCurrentTouch.idBits.markBit(0); |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5099 | mCurrentTouch.buttonState = mButtonState; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5100 | } |
| 5101 | |
| 5102 | syncTouch(when, true); |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5103 | |
| 5104 | mAccumulator.clear(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5105 | } |
| 5106 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5107 | void SingleTouchInputMapper::configureRawAxes() { |
| 5108 | TouchInputMapper::configureRawAxes(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5109 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5110 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_X, & mRawAxes.x); |
| 5111 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_Y, & mRawAxes.y); |
| 5112 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_PRESSURE, & mRawAxes.pressure); |
| 5113 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_TOOL_WIDTH, & mRawAxes.toolMajor); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5114 | } |
| 5115 | |
| 5116 | |
| 5117 | // --- MultiTouchInputMapper --- |
| 5118 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 5119 | MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) : |
| 5120 | TouchInputMapper(device) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5121 | initialize(); |
| 5122 | } |
| 5123 | |
| 5124 | MultiTouchInputMapper::~MultiTouchInputMapper() { |
| 5125 | } |
| 5126 | |
| 5127 | void MultiTouchInputMapper::initialize() { |
| 5128 | mAccumulator.clear(); |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5129 | mButtonState = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5130 | } |
| 5131 | |
| 5132 | void MultiTouchInputMapper::reset() { |
| 5133 | TouchInputMapper::reset(); |
| 5134 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5135 | initialize(); |
| 5136 | } |
| 5137 | |
| 5138 | void MultiTouchInputMapper::process(const RawEvent* rawEvent) { |
| 5139 | switch (rawEvent->type) { |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5140 | case EV_KEY: { |
| 5141 | if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) { |
| 5142 | uint32_t buttonState = getButtonStateForScanCode(rawEvent->scanCode); |
| 5143 | if (buttonState) { |
| 5144 | if (rawEvent->value) { |
| 5145 | mAccumulator.buttonDown |= buttonState; |
| 5146 | } else { |
| 5147 | mAccumulator.buttonUp |= buttonState; |
| 5148 | } |
| 5149 | } |
| 5150 | } |
| 5151 | break; |
| 5152 | } |
| 5153 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5154 | case EV_ABS: { |
| 5155 | uint32_t pointerIndex = mAccumulator.pointerCount; |
| 5156 | Accumulator::Pointer* pointer = & mAccumulator.pointers[pointerIndex]; |
| 5157 | |
| 5158 | switch (rawEvent->scanCode) { |
| 5159 | case ABS_MT_POSITION_X: |
| 5160 | pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_X; |
| 5161 | pointer->absMTPositionX = rawEvent->value; |
| 5162 | break; |
| 5163 | case ABS_MT_POSITION_Y: |
| 5164 | pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_Y; |
| 5165 | pointer->absMTPositionY = rawEvent->value; |
| 5166 | break; |
| 5167 | case ABS_MT_TOUCH_MAJOR: |
| 5168 | pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MAJOR; |
| 5169 | pointer->absMTTouchMajor = rawEvent->value; |
| 5170 | break; |
| 5171 | case ABS_MT_TOUCH_MINOR: |
| 5172 | pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MINOR; |
| 5173 | pointer->absMTTouchMinor = rawEvent->value; |
| 5174 | break; |
| 5175 | case ABS_MT_WIDTH_MAJOR: |
| 5176 | pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MAJOR; |
| 5177 | pointer->absMTWidthMajor = rawEvent->value; |
| 5178 | break; |
| 5179 | case ABS_MT_WIDTH_MINOR: |
| 5180 | pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MINOR; |
| 5181 | pointer->absMTWidthMinor = rawEvent->value; |
| 5182 | break; |
| 5183 | case ABS_MT_ORIENTATION: |
| 5184 | pointer->fields |= Accumulator::FIELD_ABS_MT_ORIENTATION; |
| 5185 | pointer->absMTOrientation = rawEvent->value; |
| 5186 | break; |
| 5187 | case ABS_MT_TRACKING_ID: |
| 5188 | pointer->fields |= Accumulator::FIELD_ABS_MT_TRACKING_ID; |
| 5189 | pointer->absMTTrackingId = rawEvent->value; |
| 5190 | break; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5191 | case ABS_MT_PRESSURE: |
| 5192 | pointer->fields |= Accumulator::FIELD_ABS_MT_PRESSURE; |
| 5193 | pointer->absMTPressure = rawEvent->value; |
| 5194 | break; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5195 | } |
| 5196 | break; |
| 5197 | } |
| 5198 | |
| 5199 | case EV_SYN: |
| 5200 | switch (rawEvent->scanCode) { |
| 5201 | case SYN_MT_REPORT: { |
| 5202 | // MultiTouch Sync: The driver has returned all data for *one* of the pointers. |
| 5203 | uint32_t pointerIndex = mAccumulator.pointerCount; |
| 5204 | |
| 5205 | if (mAccumulator.pointers[pointerIndex].fields) { |
| 5206 | if (pointerIndex == MAX_POINTERS) { |
| 5207 | LOGW("MultiTouch device driver returned more than maximum of %d pointers.", |
| 5208 | MAX_POINTERS); |
| 5209 | } else { |
| 5210 | pointerIndex += 1; |
| 5211 | mAccumulator.pointerCount = pointerIndex; |
| 5212 | } |
| 5213 | } |
| 5214 | |
| 5215 | mAccumulator.pointers[pointerIndex].clear(); |
| 5216 | break; |
| 5217 | } |
| 5218 | |
| 5219 | case SYN_REPORT: |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5220 | sync(rawEvent->when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5221 | break; |
| 5222 | } |
| 5223 | break; |
| 5224 | } |
| 5225 | } |
| 5226 | |
| 5227 | void MultiTouchInputMapper::sync(nsecs_t when) { |
| 5228 | static const uint32_t REQUIRED_FIELDS = |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5229 | Accumulator::FIELD_ABS_MT_POSITION_X | Accumulator::FIELD_ABS_MT_POSITION_Y; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5230 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5231 | uint32_t inCount = mAccumulator.pointerCount; |
| 5232 | uint32_t outCount = 0; |
| 5233 | bool havePointerIds = true; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5234 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5235 | mCurrentTouch.clear(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5236 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5237 | for (uint32_t inIndex = 0; inIndex < inCount; inIndex++) { |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5238 | const Accumulator::Pointer& inPointer = mAccumulator.pointers[inIndex]; |
| 5239 | uint32_t fields = inPointer.fields; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5240 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5241 | if ((fields & REQUIRED_FIELDS) != REQUIRED_FIELDS) { |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5242 | // Some drivers send empty MT sync packets without X / Y to indicate a pointer up. |
| 5243 | // Drop this finger. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5244 | continue; |
| 5245 | } |
| 5246 | |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5247 | PointerData& outPointer = mCurrentTouch.pointers[outCount]; |
| 5248 | outPointer.x = inPointer.absMTPositionX; |
| 5249 | outPointer.y = inPointer.absMTPositionY; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5250 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5251 | if (fields & Accumulator::FIELD_ABS_MT_PRESSURE) { |
| 5252 | if (inPointer.absMTPressure <= 0) { |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 5253 | // Some devices send sync packets with X / Y but with a 0 pressure to indicate |
| 5254 | // a pointer going up. Drop this finger. |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5255 | continue; |
| 5256 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5257 | outPointer.pressure = inPointer.absMTPressure; |
| 5258 | } else { |
| 5259 | // Default pressure to 0 if absent. |
| 5260 | outPointer.pressure = 0; |
| 5261 | } |
| 5262 | |
| 5263 | if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MAJOR) { |
| 5264 | if (inPointer.absMTTouchMajor <= 0) { |
| 5265 | // Some devices send sync packets with X / Y but with a 0 touch major to indicate |
| 5266 | // a pointer going up. Drop this finger. |
| 5267 | continue; |
| 5268 | } |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5269 | outPointer.touchMajor = inPointer.absMTTouchMajor; |
| 5270 | } else { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5271 | // Default touch area to 0 if absent. |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5272 | outPointer.touchMajor = 0; |
| 5273 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5274 | |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5275 | if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MINOR) { |
| 5276 | outPointer.touchMinor = inPointer.absMTTouchMinor; |
| 5277 | } else { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5278 | // Assume touch area is circular. |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5279 | outPointer.touchMinor = outPointer.touchMajor; |
| 5280 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5281 | |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5282 | if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MAJOR) { |
| 5283 | outPointer.toolMajor = inPointer.absMTWidthMajor; |
| 5284 | } else { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5285 | // Default tool area to 0 if absent. |
| 5286 | outPointer.toolMajor = 0; |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5287 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5288 | |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5289 | if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MINOR) { |
| 5290 | outPointer.toolMinor = inPointer.absMTWidthMinor; |
| 5291 | } else { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5292 | // Assume tool area is circular. |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5293 | outPointer.toolMinor = outPointer.toolMajor; |
| 5294 | } |
| 5295 | |
| 5296 | if (fields & Accumulator::FIELD_ABS_MT_ORIENTATION) { |
| 5297 | outPointer.orientation = inPointer.absMTOrientation; |
| 5298 | } else { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5299 | // Default orientation to vertical if absent. |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5300 | outPointer.orientation = 0; |
| 5301 | } |
| 5302 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5303 | // Assign pointer id using tracking id if available. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5304 | if (havePointerIds) { |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5305 | if (fields & Accumulator::FIELD_ABS_MT_TRACKING_ID) { |
| 5306 | uint32_t id = uint32_t(inPointer.absMTTrackingId); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5307 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5308 | if (id > MAX_POINTER_ID) { |
| 5309 | #if DEBUG_POINTERS |
| 5310 | LOGD("Pointers: Ignoring driver provided pointer id %d because " |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 5311 | "it is larger than max supported id %d", |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5312 | id, MAX_POINTER_ID); |
| 5313 | #endif |
| 5314 | havePointerIds = false; |
| 5315 | } |
| 5316 | else { |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5317 | outPointer.id = id; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5318 | mCurrentTouch.idToIndex[id] = outCount; |
| 5319 | mCurrentTouch.idBits.markBit(id); |
| 5320 | } |
| 5321 | } else { |
| 5322 | havePointerIds = false; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5323 | } |
| 5324 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5325 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5326 | outCount += 1; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5327 | } |
| 5328 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5329 | mCurrentTouch.pointerCount = outCount; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5330 | |
Jeff Brown | 96ad397 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5331 | mButtonState = (mButtonState | mAccumulator.buttonDown) & ~mAccumulator.buttonUp; |
| 5332 | mCurrentTouch.buttonState = mButtonState; |
| 5333 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5334 | syncTouch(when, havePointerIds); |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5335 | |
| 5336 | mAccumulator.clear(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5337 | } |
| 5338 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5339 | void MultiTouchInputMapper::configureRawAxes() { |
| 5340 | TouchInputMapper::configureRawAxes(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5341 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5342 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_X, & mRawAxes.x); |
| 5343 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_Y, & mRawAxes.y); |
| 5344 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MAJOR, & mRawAxes.touchMajor); |
| 5345 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MINOR, & mRawAxes.touchMinor); |
| 5346 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MAJOR, & mRawAxes.toolMajor); |
| 5347 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MINOR, & mRawAxes.toolMinor); |
| 5348 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_ORIENTATION, & mRawAxes.orientation); |
| 5349 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_PRESSURE, & mRawAxes.pressure); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 5350 | } |
| 5351 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5352 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5353 | // --- JoystickInputMapper --- |
| 5354 | |
| 5355 | JoystickInputMapper::JoystickInputMapper(InputDevice* device) : |
| 5356 | InputMapper(device) { |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5357 | } |
| 5358 | |
| 5359 | JoystickInputMapper::~JoystickInputMapper() { |
| 5360 | } |
| 5361 | |
| 5362 | uint32_t JoystickInputMapper::getSources() { |
| 5363 | return AINPUT_SOURCE_JOYSTICK; |
| 5364 | } |
| 5365 | |
| 5366 | void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 5367 | InputMapper::populateDeviceInfo(info); |
| 5368 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5369 | for (size_t i = 0; i < mAxes.size(); i++) { |
| 5370 | const Axis& axis = mAxes.valueAt(i); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 5371 | info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK, |
| 5372 | axis.min, axis.max, axis.flat, axis.fuzz); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5373 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 5374 | info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK, |
| 5375 | axis.min, axis.max, axis.flat, axis.fuzz); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5376 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5377 | } |
| 5378 | } |
| 5379 | |
| 5380 | void JoystickInputMapper::dump(String8& dump) { |
| 5381 | dump.append(INDENT2 "Joystick Input Mapper:\n"); |
| 5382 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5383 | dump.append(INDENT3 "Axes:\n"); |
| 5384 | size_t numAxes = mAxes.size(); |
| 5385 | for (size_t i = 0; i < numAxes; i++) { |
| 5386 | const Axis& axis = mAxes.valueAt(i); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5387 | const char* label = getAxisLabel(axis.axisInfo.axis); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5388 | if (label) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5389 | dump.appendFormat(INDENT4 "%s", label); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5390 | } else { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5391 | dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5392 | } |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5393 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 5394 | label = getAxisLabel(axis.axisInfo.highAxis); |
| 5395 | if (label) { |
| 5396 | dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue); |
| 5397 | } else { |
| 5398 | dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis, |
| 5399 | axis.axisInfo.splitValue); |
| 5400 | } |
| 5401 | } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) { |
| 5402 | dump.append(" (invert)"); |
| 5403 | } |
| 5404 | |
| 5405 | dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n", |
| 5406 | axis.min, axis.max, axis.flat, axis.fuzz); |
| 5407 | dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, " |
| 5408 | "highScale=%0.5f, highOffset=%0.5f\n", |
| 5409 | axis.scale, axis.offset, axis.highScale, axis.highOffset); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5410 | dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, rawFlat=%d, rawFuzz=%d\n", |
| 5411 | mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue, |
| 5412 | axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5413 | } |
| 5414 | } |
| 5415 | |
| 5416 | void JoystickInputMapper::configure() { |
| 5417 | InputMapper::configure(); |
| 5418 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5419 | // Collect all axes. |
| 5420 | for (int32_t abs = 0; abs <= ABS_MAX; abs++) { |
| 5421 | RawAbsoluteAxisInfo rawAxisInfo; |
| 5422 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), abs, &rawAxisInfo); |
| 5423 | if (rawAxisInfo.valid) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5424 | // Map axis. |
| 5425 | AxisInfo axisInfo; |
| 5426 | bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5427 | if (!explicitlyMapped) { |
| 5428 | // Axis is not explicitly mapped, will choose a generic axis later. |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5429 | axisInfo.mode = AxisInfo::MODE_NORMAL; |
| 5430 | axisInfo.axis = -1; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5431 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5432 | |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5433 | // Apply flat override. |
| 5434 | int32_t rawFlat = axisInfo.flatOverride < 0 |
| 5435 | ? rawAxisInfo.flat : axisInfo.flatOverride; |
| 5436 | |
| 5437 | // Calculate scaling factors and limits. |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5438 | Axis axis; |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5439 | if (axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 5440 | float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue); |
| 5441 | float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue); |
| 5442 | axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, |
| 5443 | scale, 0.0f, highScale, 0.0f, |
| 5444 | 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale); |
| 5445 | } else if (isCenteredAxis(axisInfo.axis)) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5446 | float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue); |
| 5447 | float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale; |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5448 | axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, |
| 5449 | scale, offset, scale, offset, |
| 5450 | -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5451 | } else { |
| 5452 | float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5453 | axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, |
| 5454 | scale, 0.0f, scale, 0.0f, |
| 5455 | 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5456 | } |
| 5457 | |
| 5458 | // To eliminate noise while the joystick is at rest, filter out small variations |
| 5459 | // in axis values up front. |
| 5460 | axis.filter = axis.flat * 0.25f; |
| 5461 | |
| 5462 | mAxes.add(abs, axis); |
| 5463 | } |
| 5464 | } |
| 5465 | |
| 5466 | // If there are too many axes, start dropping them. |
| 5467 | // Prefer to keep explicitly mapped axes. |
| 5468 | if (mAxes.size() > PointerCoords::MAX_AXES) { |
| 5469 | LOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.", |
| 5470 | getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES); |
| 5471 | pruneAxes(true); |
| 5472 | pruneAxes(false); |
| 5473 | } |
| 5474 | |
| 5475 | // Assign generic axis ids to remaining axes. |
| 5476 | int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1; |
| 5477 | size_t numAxes = mAxes.size(); |
| 5478 | for (size_t i = 0; i < numAxes; i++) { |
| 5479 | Axis& axis = mAxes.editValueAt(i); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5480 | if (axis.axisInfo.axis < 0) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5481 | while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16 |
| 5482 | && haveAxis(nextGenericAxisId)) { |
| 5483 | nextGenericAxisId += 1; |
| 5484 | } |
| 5485 | |
| 5486 | if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5487 | axis.axisInfo.axis = nextGenericAxisId; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5488 | nextGenericAxisId += 1; |
| 5489 | } else { |
| 5490 | LOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids " |
| 5491 | "have already been assigned to other axes.", |
| 5492 | getDeviceName().string(), mAxes.keyAt(i)); |
| 5493 | mAxes.removeItemsAt(i--); |
| 5494 | numAxes -= 1; |
| 5495 | } |
| 5496 | } |
| 5497 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5498 | } |
| 5499 | |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5500 | bool JoystickInputMapper::haveAxis(int32_t axisId) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5501 | size_t numAxes = mAxes.size(); |
| 5502 | for (size_t i = 0; i < numAxes; i++) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5503 | const Axis& axis = mAxes.valueAt(i); |
| 5504 | if (axis.axisInfo.axis == axisId |
| 5505 | || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT |
| 5506 | && axis.axisInfo.highAxis == axisId)) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5507 | return true; |
| 5508 | } |
| 5509 | } |
| 5510 | return false; |
| 5511 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5512 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5513 | void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) { |
| 5514 | size_t i = mAxes.size(); |
| 5515 | while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) { |
| 5516 | if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) { |
| 5517 | continue; |
| 5518 | } |
| 5519 | LOGI("Discarding joystick '%s' axis %d because there are too many axes.", |
| 5520 | getDeviceName().string(), mAxes.keyAt(i)); |
| 5521 | mAxes.removeItemsAt(i); |
| 5522 | } |
| 5523 | } |
| 5524 | |
| 5525 | bool JoystickInputMapper::isCenteredAxis(int32_t axis) { |
| 5526 | switch (axis) { |
| 5527 | case AMOTION_EVENT_AXIS_X: |
| 5528 | case AMOTION_EVENT_AXIS_Y: |
| 5529 | case AMOTION_EVENT_AXIS_Z: |
| 5530 | case AMOTION_EVENT_AXIS_RX: |
| 5531 | case AMOTION_EVENT_AXIS_RY: |
| 5532 | case AMOTION_EVENT_AXIS_RZ: |
| 5533 | case AMOTION_EVENT_AXIS_HAT_X: |
| 5534 | case AMOTION_EVENT_AXIS_HAT_Y: |
| 5535 | case AMOTION_EVENT_AXIS_ORIENTATION: |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5536 | case AMOTION_EVENT_AXIS_RUDDER: |
| 5537 | case AMOTION_EVENT_AXIS_WHEEL: |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5538 | return true; |
| 5539 | default: |
| 5540 | return false; |
| 5541 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5542 | } |
| 5543 | |
| 5544 | void JoystickInputMapper::reset() { |
| 5545 | // Recenter all axes. |
| 5546 | nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5547 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5548 | size_t numAxes = mAxes.size(); |
| 5549 | for (size_t i = 0; i < numAxes; i++) { |
| 5550 | Axis& axis = mAxes.editValueAt(i); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5551 | axis.resetValue(); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5552 | } |
| 5553 | |
| 5554 | sync(when, true /*force*/); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5555 | |
| 5556 | InputMapper::reset(); |
| 5557 | } |
| 5558 | |
| 5559 | void JoystickInputMapper::process(const RawEvent* rawEvent) { |
| 5560 | switch (rawEvent->type) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5561 | case EV_ABS: { |
| 5562 | ssize_t index = mAxes.indexOfKey(rawEvent->scanCode); |
| 5563 | if (index >= 0) { |
| 5564 | Axis& axis = mAxes.editValueAt(index); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5565 | float newValue, highNewValue; |
| 5566 | switch (axis.axisInfo.mode) { |
| 5567 | case AxisInfo::MODE_INVERT: |
| 5568 | newValue = (axis.rawAxisInfo.maxValue - rawEvent->value) |
| 5569 | * axis.scale + axis.offset; |
| 5570 | highNewValue = 0.0f; |
| 5571 | break; |
| 5572 | case AxisInfo::MODE_SPLIT: |
| 5573 | if (rawEvent->value < axis.axisInfo.splitValue) { |
| 5574 | newValue = (axis.axisInfo.splitValue - rawEvent->value) |
| 5575 | * axis.scale + axis.offset; |
| 5576 | highNewValue = 0.0f; |
| 5577 | } else if (rawEvent->value > axis.axisInfo.splitValue) { |
| 5578 | newValue = 0.0f; |
| 5579 | highNewValue = (rawEvent->value - axis.axisInfo.splitValue) |
| 5580 | * axis.highScale + axis.highOffset; |
| 5581 | } else { |
| 5582 | newValue = 0.0f; |
| 5583 | highNewValue = 0.0f; |
| 5584 | } |
| 5585 | break; |
| 5586 | default: |
| 5587 | newValue = rawEvent->value * axis.scale + axis.offset; |
| 5588 | highNewValue = 0.0f; |
| 5589 | break; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5590 | } |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5591 | axis.newValue = newValue; |
| 5592 | axis.highNewValue = highNewValue; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5593 | } |
| 5594 | break; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5595 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5596 | |
| 5597 | case EV_SYN: |
| 5598 | switch (rawEvent->scanCode) { |
| 5599 | case SYN_REPORT: |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5600 | sync(rawEvent->when, false /*force*/); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5601 | break; |
| 5602 | } |
| 5603 | break; |
| 5604 | } |
| 5605 | } |
| 5606 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5607 | void JoystickInputMapper::sync(nsecs_t when, bool force) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5608 | if (!filterAxes(force)) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5609 | return; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5610 | } |
| 5611 | |
| 5612 | int32_t metaState = mContext->getGlobalMetaState(); |
| 5613 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5614 | PointerCoords pointerCoords; |
| 5615 | pointerCoords.clear(); |
| 5616 | |
| 5617 | size_t numAxes = mAxes.size(); |
| 5618 | for (size_t i = 0; i < numAxes; i++) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5619 | const Axis& axis = mAxes.valueAt(i); |
| 5620 | pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue); |
| 5621 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 5622 | pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue); |
| 5623 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5624 | } |
| 5625 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 5626 | // Moving a joystick axis should not wake the devide because joysticks can |
| 5627 | // be fairly noisy even when not in use. On the other hand, pushing a gamepad |
| 5628 | // button will likely wake the device. |
| 5629 | // TODO: Use the input device configuration to control this behavior more finely. |
| 5630 | uint32_t policyFlags = 0; |
| 5631 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5632 | int32_t pointerId = 0; |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 5633 | getDispatcher()->notifyMotion(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags, |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5634 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 5635 | 1, &pointerId, &pointerCoords, 0, 0, 0); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5636 | } |
| 5637 | |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5638 | bool JoystickInputMapper::filterAxes(bool force) { |
| 5639 | bool atLeastOneSignificantChange = force; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5640 | size_t numAxes = mAxes.size(); |
| 5641 | for (size_t i = 0; i < numAxes; i++) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5642 | Axis& axis = mAxes.editValueAt(i); |
| 5643 | if (force || hasValueChangedSignificantly(axis.filter, |
| 5644 | axis.newValue, axis.currentValue, axis.min, axis.max)) { |
| 5645 | axis.currentValue = axis.newValue; |
| 5646 | atLeastOneSignificantChange = true; |
| 5647 | } |
| 5648 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 5649 | if (force || hasValueChangedSignificantly(axis.filter, |
| 5650 | axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) { |
| 5651 | axis.highCurrentValue = axis.highNewValue; |
| 5652 | atLeastOneSignificantChange = true; |
| 5653 | } |
| 5654 | } |
| 5655 | } |
| 5656 | return atLeastOneSignificantChange; |
| 5657 | } |
| 5658 | |
| 5659 | bool JoystickInputMapper::hasValueChangedSignificantly( |
| 5660 | float filter, float newValue, float currentValue, float min, float max) { |
| 5661 | if (newValue != currentValue) { |
| 5662 | // Filter out small changes in value unless the value is converging on the axis |
| 5663 | // bounds or center point. This is intended to reduce the amount of information |
| 5664 | // sent to applications by particularly noisy joysticks (such as PS3). |
| 5665 | if (fabs(newValue - currentValue) > filter |
| 5666 | || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min) |
| 5667 | || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max) |
| 5668 | || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) { |
| 5669 | return true; |
| 5670 | } |
| 5671 | } |
| 5672 | return false; |
| 5673 | } |
| 5674 | |
| 5675 | bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange( |
| 5676 | float filter, float newValue, float currentValue, float thresholdValue) { |
| 5677 | float newDistance = fabs(newValue - thresholdValue); |
| 5678 | if (newDistance < filter) { |
| 5679 | float oldDistance = fabs(currentValue - thresholdValue); |
| 5680 | if (newDistance < oldDistance) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5681 | return true; |
| 5682 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5683 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5684 | return false; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5685 | } |
| 5686 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5687 | } // namespace android |