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